|
0 |
const std = @import("std");
|
|
1 |
const math = std.math;
|
|
2 |
|
|
3 |
/// The type of a vector.
|
|
4 |
pub fn Vector(comptime N: usize) type {
|
|
5 |
return packed struct {
|
|
6 |
const Self = @This();
|
|
7 |
/// The scalar type manager by this vector.
|
|
8 |
pub const Scalar = f32;
|
|
9 |
|
|
10 |
values: [N]Scalar,
|
|
11 |
|
|
12 |
/// Initializes a vector from its scalar values.
|
|
13 |
pub fn init(values: [N]Scalar) Self {
|
|
14 |
return .{ .values = values };
|
|
15 |
}
|
|
16 |
|
|
17 |
/// Creates a vector filled with the given scalar value.
|
|
18 |
pub fn filled(n: Scalar) Self {
|
|
19 |
var values: [N]Scalar = undefined;
|
|
20 |
comptime var i = 0;
|
|
21 |
inline while (i < N) : (i += 1) {
|
|
22 |
values[i] = n;
|
|
23 |
}
|
|
24 |
return .{ .values = values };
|
|
25 |
}
|
|
26 |
|
|
27 |
/// Creates a vector filled with zeroes.
|
|
28 |
pub fn zeroes() Self {
|
|
29 |
return comptime Self.filled(0);
|
|
30 |
}
|
|
31 |
|
|
32 |
/// Sums all scalars in this vector.
|
|
33 |
pub fn sum(self: Self) Scalar {
|
|
34 |
var total: Scalar = 0;
|
|
35 |
|
|
36 |
comptime var i = 0;
|
|
37 |
inline while (i < N) : (i += 1) {
|
|
38 |
total += self.values[i];
|
|
39 |
}
|
|
40 |
|
|
41 |
return total;
|
|
42 |
}
|
|
43 |
|
|
44 |
/// Adds 2 vector together.
|
|
45 |
pub fn add(self: Self, other: Self) Self {
|
|
46 |
const left: @Vector(N, Scalar) = self.values;
|
|
47 |
const right: @Vector(N, Scalar) = other.values;
|
|
48 |
|
|
49 |
return .{ .values = left + right };
|
|
50 |
}
|
|
51 |
|
|
52 |
/// Adds another vector to this vector.
|
|
53 |
pub fn addAssign(self: *Self, other: Self) void {
|
|
54 |
const left: @Vector(N, Scalar) = self.values;
|
|
55 |
const right: @Vector(N, Scalar) = other.values;
|
|
56 |
|
|
57 |
self.values = left + right;
|
|
58 |
}
|
|
59 |
|
|
60 |
/// Subtracts 2 vector together.
|
|
61 |
pub fn sub(self: Self, other: Self) Self {
|
|
62 |
const left: @Vector(N, Scalar) = self.values;
|
|
63 |
const right: @Vector(N, Scalar) = other.values;
|
|
64 |
|
|
65 |
return .{ .values = left - right };
|
|
66 |
}
|
|
67 |
|
|
68 |
/// Adds another vector to this vector.
|
|
69 |
pub fn subAssign(self: *Self, other: Self) void {
|
|
70 |
const left: @Vector(N, Scalar) = self.values;
|
|
71 |
const right: @Vector(N, Scalar) = other.values;
|
|
72 |
|
|
73 |
self.values = left - right;
|
|
74 |
}
|
|
75 |
|
|
76 |
/// Multiplies 2 vectors together.
|
|
77 |
pub fn mul(self: Self, other: Self) Self {
|
|
78 |
const left: @Vector(N, Scalar) = self.values;
|
|
79 |
const right: @Vector(N, Scalar) = other.values;
|
|
80 |
|
|
81 |
return .{ .values = left * right };
|
|
82 |
}
|
|
83 |
|
|
84 |
/// Multiplies a vector and a scalar together.
|
|
85 |
pub fn mulScalar(self: Self, n: Scalar) Self {
|
|
86 |
const left: @Vector(N, Scalar) = self.values;
|
|
87 |
const right: @Vector(N, Scalar) = Self.filled(n).values;
|
|
88 |
|
|
89 |
return .{ .values = left * right };
|
|
90 |
}
|
|
91 |
|
|
92 |
/// Multiplies this vector with another vector.
|
|
93 |
pub fn mulAssign(self: *Self, other: Self) void {
|
|
94 |
const left: @Vector(N, Scalar) = self.values;
|
|
95 |
const right: @Vector(N, Scalar) = other.values;
|
|
96 |
|
|
97 |
self.values = left * right;
|
|
98 |
}
|
|
99 |
|
|
100 |
/// Multiplies this vector with a scalar.
|
|
101 |
pub fn mulAssignScalar(self: *Self, n: Scalar) void {
|
|
102 |
const left: @Vector(N, Scalar) = self.values;
|
|
103 |
const right: @Vector(N, Scalar) = Self.filled(n).values;
|
|
104 |
|
|
105 |
self.values = left * right;
|
|
106 |
}
|
|
107 |
|
|
108 |
/// Multiplies 2 vectors together.
|
|
109 |
pub fn div(self: Self, other: Self) Self {
|
|
110 |
const left: @Vector(N, Scalar) = self.values;
|
|
111 |
const right: @Vector(N, Scalar) = other.values;
|
|
112 |
|
|
113 |
return .{ .values = left / right };
|
|
114 |
}
|
|
115 |
|
|
116 |
/// Multiplies a vector and a scalar together.
|
|
117 |
pub fn divScalar(self: Self, n: Scalar) Self {
|
|
118 |
const left: @Vector(N, Scalar) = self.values;
|
|
119 |
const right: @Vector(N, Scalar) = Self.filled(n).values;
|
|
120 |
|
|
121 |
return .{ .values = left / right };
|
|
122 |
}
|
|
123 |
|
|
124 |
/// Multiplies this vector with another vector.
|
|
125 |
pub fn divAssign(self: *Self, other: Self) void {
|
|
126 |
const left: @Vector(N, Scalar) = self.values;
|
|
127 |
const right: @Vector(N, Scalar) = other.values;
|
|
128 |
|
|
129 |
self.values = left / right;
|
|
130 |
}
|
|
131 |
|
|
132 |
/// Multiplies this vector with a scalar.
|
|
133 |
pub fn divAssignScalar(self: *Self, n: Scalar) void {
|
|
134 |
const left: @Vector(N, Scalar) = self.values;
|
|
135 |
const right: @Vector(N, Scalar) = Self.filled(n).values;
|
|
136 |
|
|
137 |
self.values = left / right;
|
|
138 |
}
|
|
139 |
|
|
140 |
/// Calculates the dot product of 2 vectors.
|
|
141 |
pub fn dot(self: Self, other: Self) Scalar {
|
|
142 |
return self.mul(other).sum();
|
|
143 |
}
|
|
144 |
|
|
145 |
/// Calculates the norm squared of this vector.
|
|
146 |
pub fn normSquared(self: Self) Scalar {
|
|
147 |
return self.dot(self);
|
|
148 |
}
|
|
149 |
|
|
150 |
/// Calculates the norm of this vector.
|
|
151 |
pub fn norm(self: Self) Scalar {
|
|
152 |
return math.sqrt(self.normSquared());
|
|
153 |
}
|
|
154 |
|
|
155 |
/// Returns a normalized version of this vector.
|
|
156 |
pub fn normalize(self: Self) Self {
|
|
157 |
const values: @Vector(N, Scalar) = self.values;
|
|
158 |
const norms: @Vector(N, Scalar) = Self.filled(self.norm()).values;
|
|
159 |
|
|
160 |
return .{ .values = values / norms };
|
|
161 |
}
|
|
162 |
|
|
163 |
/// Make this vector normalized.
|
|
164 |
pub fn normalizeAssign(self: *Self) void {
|
|
165 |
const values: @Vector(N, Scalar) = self.values;
|
|
166 |
const norms: @Vector(N, Scalar) = Self.filled(self.norm()).values;
|
|
167 |
|
|
168 |
self.values = values / norms;
|
|
169 |
}
|
|
170 |
|
|
171 |
/// Calculates the cross product of 2 vectors.
|
|
172 |
pub fn cross(self: Self, other: Self) Self {
|
|
173 |
if (N != 3) {
|
|
174 |
@compileError("A cross product can only be calculated for a 3D vector.");
|
|
175 |
}
|
|
176 |
|
|
177 |
const values = [3]Scalar{
|
|
178 |
self.values[1] * other.values[2] - self.values[2] * other.values[1],
|
|
179 |
self.values[2] * other.values[0] - self.values[0] * other.values[2],
|
|
180 |
self.values[0] * other.values[1] - self.values[1] * other.values[0],
|
|
181 |
};
|
|
182 |
|
|
183 |
return Self{ .values = values };
|
|
184 |
}
|
|
185 |
};
|
|
186 |
}
|
|
187 |
|
|
188 |
fn expectVectorEqual(comptime N: usize, expected: Vector(N), actual: Vector(N)) void {
|
|
189 |
comptime var i = 0;
|
|
190 |
inline while (i < N) : (i += 1) {
|
|
191 |
std.testing.expectEqual(expected.values[i], actual.values[i]);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
test "vector initialization" {
|
|
196 |
const actual = Vector(2).init(.{ 1, 2 });
|
|
197 |
std.testing.expectEqual(@floatCast(f32, 1), actual.values[0]);
|
|
198 |
std.testing.expectEqual(@floatCast(f32, 2), actual.values[1]);
|
|
199 |
}
|
|
200 |
|
|
201 |
test "vector scalar initialization" {
|
|
202 |
const v = Vector(3).filled(3);
|
|
203 |
expectVectorEqual(3, Vector(3).init(.{ 3, 3, 3 }), v);
|
|
204 |
}
|
|
205 |
|
|
206 |
test "vector zero initialization" {
|
|
207 |
const v = Vector(3).zeroes();
|
|
208 |
expectVectorEqual(3, Vector(3).init(.{ 0, 0, 0 }), v);
|
|
209 |
}
|
|
210 |
|
|
211 |
test "vector summation" {
|
|
212 |
const v = Vector(3).init(.{ 1, 2, 3 });
|
|
213 |
std.testing.expectEqual(@floatCast(f32, 6), v.sum());
|
|
214 |
}
|
|
215 |
|
|
216 |
test "vectors addition" {
|
|
217 |
const v1 = Vector(3).init(.{ 3, 4, 5 });
|
|
218 |
const v2 = Vector(3).init(.{ 6, 7, 8 });
|
|
219 |
expectVectorEqual(3, Vector(3).init(.{ 9, 11, 13 }), v1.add(v2));
|
|
220 |
|
|
221 |
var v3 = Vector(3).init(.{ 1, 2, 3 });
|
|
222 |
v3.addAssign(Vector(3).init(.{ 3, 2, 1 }));
|
|
223 |
expectVectorEqual(3, Vector(3).init(.{ 4, 4, 4 }), v3);
|
|
224 |
}
|
|
225 |
|
|
226 |
test "vectors subtraction" {
|
|
227 |
const v1 = Vector(3).init(.{ 3, 4, 5 });
|
|
228 |
const v2 = Vector(3).init(.{ 6, 7, 8 });
|
|
229 |
expectVectorEqual(3, Vector(3).init(.{ -3, -3, -3 }), v1.sub(v2));
|
|
230 |
|
|
231 |
var v3 = Vector(3).init(.{ 1, 2, 3 });
|
|
232 |
v3.subAssign(Vector(3).init(.{ 3, 2, 1 }));
|
|
233 |
expectVectorEqual(3, Vector(3).init(.{ -2, 0, 2 }), v3);
|
|
234 |
}
|
|
235 |
|
|
236 |
test "vector multiplication" {
|
|
237 |
const v1 = Vector(3).init(.{ 3, 4, 5 });
|
|
238 |
const v2 = Vector(3).init(.{ 6, 7, 8 });
|
|
239 |
expectVectorEqual(3, Vector(3).init(.{ 18, 28, 40 }), v1.mul(v2));
|
|
240 |
|
|
241 |
var v3 = Vector(3).init(.{ 1, 2, 3 });
|
|
242 |
v3.mulAssign(Vector(3).init(.{ 3, 2, 1 }));
|
|
243 |
expectVectorEqual(3, Vector(3).init(.{ 3, 4, 3 }), v3);
|
|
244 |
}
|
|
245 |
|
|
246 |
test "vector scalar multiplication" {
|
|
247 |
const v1 = Vector(3).init(.{ 3, 4, 5 });
|
|
248 |
expectVectorEqual(3, Vector(3).init(.{ 6, 8, 10 }), v1.mulScalar(2));
|
|
249 |
|
|
250 |
var v3 = Vector(3).init(.{ 1, 2, 3 });
|
|
251 |
v3.mulAssignScalar(3);
|
|
252 |
expectVectorEqual(3, Vector(3).init(.{ 3, 6, 9 }), v3);
|
|
253 |
}
|
|
254 |
|
|
255 |
test "vector division" {
|
|
256 |
const v1 = Vector(3).init(.{ 10, 20, 30 });
|
|
257 |
const v2 = Vector(3).init(.{ 5, 2, 6 });
|
|
258 |
expectVectorEqual(3, Vector(3).init(.{ 2, 10, 5 }), v1.div(v2));
|
|
259 |
|
|
260 |
var v3 = Vector(3).init(.{ 3, 2, 6 });
|
|
261 |
v3.divAssign(Vector(3).init(.{ 1, 2, 3 }));
|
|
262 |
expectVectorEqual(3, Vector(3).init(.{ 3, 1, 2 }), v3);
|
|
263 |
}
|
|
264 |
|
|
265 |
test "vector scalar division" {
|
|
266 |
const v1 = Vector(3).init(.{ 12, 24, 36 });
|
|
267 |
expectVectorEqual(3, Vector(3).init(.{ 1, 2, 3 }), v1.divScalar(12));
|
|
268 |
|
|
269 |
var v3 = Vector(3).init(.{ 1, 2, 3 });
|
|
270 |
v3.divAssignScalar(1);
|
|
271 |
expectVectorEqual(3, Vector(3).init(.{ 1, 2, 3 }), v3);
|
|
272 |
}
|
|
273 |
|
|
274 |
test "vector dot product" {
|
|
275 |
const v1 = Vector(3).init(.{ 1, 2, 3 });
|
|
276 |
const v2 = Vector(3).init(.{ 4, 5, 6 });
|
|
277 |
std.testing.expectEqual(@floatCast(f32, 32), v1.dot(v2));
|
|
278 |
}
|
|
279 |
|
|
280 |
test "vector norm squared" {
|
|
281 |
const v1 = Vector(2).init(.{ 3, 4 });
|
|
282 |
std.testing.expectEqual(@floatCast(f32, 25), v1.normSquared());
|
|
283 |
}
|
|
284 |
|
|
285 |
test "vector norm" {
|
|
286 |
const v1 = Vector(2).init(.{ 3, 4 });
|
|
287 |
std.testing.expectEqual(@floatCast(f32, 5), v1.norm());
|
|
288 |
}
|
|
289 |
|
|
290 |
test "vector normalization" {
|
|
291 |
const v1 = Vector(3).init(.{ 30, 10, 20 });
|
|
292 |
expectVectorEqual(3, Vector(3).init(.{0.80178372573729, 0.26726124191243, 0.53452248382486}), v1.normalize());
|
|
293 |
|
|
294 |
var v2 = Vector(3).init(.{ 60, 20, 40 });
|
|
295 |
v2.normalizeAssign();
|
|
296 |
expectVectorEqual(3, Vector(3).init(.{0.80178372573729, 0.26726124191243, 0.53452248382486}), v2);
|
|
297 |
}
|
|
298 |
|
|
299 |
test "vector cross product" {
|
|
300 |
const v1 = Vector(3).init(.{ 4, 5, 6 });
|
|
301 |
const v2 = Vector(3).init(.{ 7, 8, 9 });
|
|
302 |
expectVectorEqual(3, Vector(3).init(.{ -3, 6, -3 }), v1.cross(v2));
|
|
303 |
}
|