Skip to content

Commit ec62993

Browse files
add static direction vectors
1 parent 362daea commit ec62993

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

inmath/linalg.d

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,82 @@ static assert(dimension > 0, "0 dimensional vectors don't exist.");
6666
size_t toHash() const { return typeid(this).getHash(&this); }
6767

6868
@safe pure nothrow:
69+
70+
/**
71+
A vector with all zeroes
72+
*/
73+
static auto zero() {
74+
return Vector!(type, dimension_)(0);
75+
}
76+
77+
/**
78+
A vector with all ones
79+
*/
80+
static auto one() {
81+
return Vector!(type, dimension_)(1);
82+
}
83+
84+
/**
85+
Up vector (0, -1) OR (0, 1, 0)
86+
*/
87+
static auto up() {
88+
static if (dimension_ == 2) return Vector!(type, dimension_)(cast(type)0, cast(type)-1);
89+
else static if (dimension_ == 3) return Vector!(type, dimension_)(cast(type)0, cast(type)1, cast(type)0);
90+
else static if (dimension_ == 4) return Vector!(type, dimension_)(cast(type)0, cast(type)1, cast(type)0, cast(type)0);
91+
else return Vector!(type, dimension_).init;
92+
}
93+
94+
/**
95+
Down vector (0, 1) OR (0, -1, 0)
96+
*/
97+
static auto down() {
98+
static if (dimension_ == 2) return Vector!(type, dimension_)(cast(type)0, cast(type)1);
99+
else static if (dimension_ == 3) return Vector!(type, dimension_)(cast(type)0, cast(type)-1, cast(type)0);
100+
else static if (dimension_ == 4) return Vector!(type, dimension_)(cast(type)0, cast(type)-1, cast(type)0, cast(type)0);
101+
else return Vector!(type, dimension_).init;
102+
}
103+
104+
/**
105+
Left vector (-1, 0, 0)
106+
*/
107+
static auto left() {
108+
static if (dimension_ == 2) return Vector!(type, dimension_)(cast(type)-1, cast(type)0);
109+
else static if (dimension_ == 3) return Vector!(type, dimension_)(cast(type)-1, cast(type)0, cast(type)0);
110+
else static if (dimension_ == 4) return Vector!(type, dimension_)(cast(type)-1, cast(type)0, cast(type)0, cast(type)0);
111+
else return Vector!(type, dimension_).init;
112+
}
113+
114+
/**
115+
Right vector (1, 0, 0)
116+
*/
117+
static auto right() {
118+
static if (dimension_ == 2) return Vector!(type, dimension_)(cast(type)1, cast(type)0);
119+
else static if (dimension_ == 3) return Vector!(type, dimension_)(cast(type)1, cast(type)0, cast(type)0);
120+
else static if (dimension_ == 4) return Vector!(type, dimension_)(cast(type)1, cast(type)0, cast(type)0, cast(type)0);
121+
else return Vector!(type, dimension_).init;
122+
}
123+
124+
static if (dimension_ >= 3) {
125+
126+
/**
127+
Forward vector (0, 0, 1)
128+
*/
129+
static auto forward() {
130+
static if (dimension_ == 3) return Vector!(type, dimension_)(cast(type)0, cast(type)0, cast(type)1);
131+
else static if (dimension_ == 4) return Vector!(type, dimension_)(cast(type)0, cast(type)0, cast(type)1, cast(type)0);
132+
else return Vector!(type, dimension_).init;
133+
}
134+
135+
/**
136+
Back vector (0, 0, -1)
137+
*/
138+
static auto back() {
139+
static if (dimension_ == 3) return Vector!(type, dimension_)(cast(type)0, cast(type)0, cast(type)-1);
140+
else static if (dimension_ == 4) return Vector!(type, dimension_)(cast(type)0, cast(type)0, cast(type)-1, cast(type)0);
141+
else return Vector!(type, dimension_).init;
142+
}
143+
}
144+
69145
///
70146
ref inout(vt) get_(char coord)() inout {
71147
return vector[coordToIndex!coord];
@@ -199,7 +275,6 @@ static assert(dimension > 0, "0 dimensional vectors don't exist.");
199275
return true;
200276
}
201277
}
202-
deprecated("Use isFinite instead of ok") alias ok = isFinite;
203278

204279
/// Sets all values of the vector to value.
205280
void clear(vt value) {

0 commit comments

Comments
 (0)