Skip to content

Commit 3ce00a4

Browse files
committed
math: refactor Vec and Mat mixin structs
This change refactors the vector and matrix mixin structs to instead share a single generic struct. This allows for more generalized functions and less boilerplate. It also adds a new implementation of swizzle that is more flexible, more suitable for generalized vectors, and supports different axis names.
1 parent af1cf98 commit 3ce00a4

File tree

4 files changed

+535
-844
lines changed

4 files changed

+535
-844
lines changed

src/math/main.zig

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,71 +49,92 @@ const ray = @import("ray.zig");
4949
/// Public namespaces
5050
pub const collision = @import("collision.zig");
5151

52+
pub const Vec = vec.Vec;
53+
pub const Mat = mat.Mat;
54+
5255
/// Standard f32 precision types
5356
pub const Vec2 = vec.Vec2(f32);
5457
pub const Vec3 = vec.Vec3(f32);
5558
pub const Vec4 = vec.Vec4(f32);
59+
pub fn VecN(comptime length: usize) type {
60+
return Vec(f32, length);
61+
}
5662
pub const Quat = q.Quat(f32);
5763
pub const Mat2x2 = mat.Mat2x2(f32);
5864
pub const Mat3x3 = mat.Mat3x3(f32);
5965
pub const Mat4x4 = mat.Mat4x4(f32);
66+
pub fn MatMxN(comptime rows: usize, comptime cols: usize) type {
67+
return Mat(f32, rows, cols);
68+
}
6069
pub const Ray = ray.Ray3(f32);
6170

6271
/// Half-precision f16 types
6372
pub const Vec2h = vec.Vec2(f16);
6473
pub const Vec3h = vec.Vec3(f16);
6574
pub const Vec4h = vec.Vec4(f16);
75+
pub fn VecNh(comptime length: usize) type {
76+
return Vec(f16, length);
77+
}
6678
pub const Quath = q.Quat(f16);
6779
pub const Mat2x2h = mat.Mat2x2(f16);
6880
pub const Mat3x3h = mat.Mat3x3(f16);
6981
pub const Mat4x4h = mat.Mat4x4(f16);
82+
pub fn MatMxNh(comptime rows: usize, comptime cols: usize) type {
83+
return Mat(f16, rows, cols);
84+
}
7085
pub const Rayh = ray.Ray3(f16);
7186

7287
/// Double-precision f64 types
7388
pub const Vec2d = vec.Vec2(f64);
7489
pub const Vec3d = vec.Vec3(f64);
7590
pub const Vec4d = vec.Vec4(f64);
91+
pub fn VecNd(comptime length: usize) type {
92+
return Vec(f64, length);
93+
}
7694
pub const Quatd = q.Quat(f64);
7795
pub const Mat2x2d = mat.Mat2x2(f64);
7896
pub const Mat3x3d = mat.Mat3x3(f64);
7997
pub const Mat4x4d = mat.Mat4x4(f64);
98+
pub fn MatMxNd(comptime rows: usize, comptime cols: usize) type {
99+
return Mat(f64, rows, cols);
100+
}
80101
pub const Rayd = ray.Ray3(f64);
81102

82103
/// Standard f32 precision initializers
83-
pub const vec2 = Vec2.init;
84-
pub const vec3 = Vec3.init;
85-
pub const vec4 = Vec4.init;
86-
pub const vec2FromInt = Vec2.fromInt;
87-
pub const vec3FromInt = Vec3.fromInt;
88-
pub const vec4FromInt = Vec4.fromInt;
104+
pub const vec2 = Vec2.init2;
105+
pub const vec3 = Vec3.init3;
106+
pub const vec4 = Vec4.init4;
107+
pub const vec2FromInt = Vec2.fromInt2;
108+
pub const vec3FromInt = Vec3.fromInt3;
109+
pub const vec4FromInt = Vec4.fromInt4;
89110
pub const quat = Quat.init;
90-
pub const mat2x2 = Mat2x2.init;
91-
pub const mat3x3 = Mat3x3.init;
92-
pub const mat4x4 = Mat4x4.init;
111+
pub const mat2x2 = Mat2x2.init2;
112+
pub const mat3x3 = Mat3x3.init3;
113+
pub const mat4x4 = Mat4x4.init4;
93114

94115
/// Half-precision f16 initializers
95-
pub const vec2h = Vec2h.init;
96-
pub const vec3h = Vec3h.init;
97-
pub const vec4h = Vec4h.init;
98-
pub const vec2hFromInt = Vec2h.fromInt;
99-
pub const vec3hFromInt = Vec3h.fromInt;
100-
pub const vec4hFromInt = Vec4h.fromInt;
116+
pub const vec2h = Vec2h.init2;
117+
pub const vec3h = Vec3h.init3;
118+
pub const vec4h = Vec4h.init4;
119+
pub const vec2hFromInt = Vec2h.fromInt2;
120+
pub const vec3hFromInt = Vec3h.fromInt3;
121+
pub const vec4hFromInt = Vec4h.fromInt4;
101122
pub const quath = Quath.init;
102-
pub const mat2x2h = Mat2x2h.init;
103-
pub const mat3x3h = Mat3x3h.init;
104-
pub const mat4x4h = Mat4x4h.init;
123+
pub const mat2x2h = Mat2x2h.init2;
124+
pub const mat3x3h = Mat3x3h.init3;
125+
pub const mat4x4h = Mat4x4h.init4;
105126

106127
/// Double-precision f64 initializers
107-
pub const vec2d = Vec2d.init;
108-
pub const vec3d = Vec3d.init;
109-
pub const vec4d = Vec4d.init;
110-
pub const vec2dFromInt = Vec2d.fromInt;
111-
pub const vec3dFromInt = Vec3d.fromInt;
112-
pub const vec4dFromInt = Vec4d.fromInt;
128+
pub const vec2d = Vec2d.init2;
129+
pub const vec3d = Vec3d.init3;
130+
pub const vec4d = Vec4d.init4;
131+
pub const vec2dFromInt = Vec2d.fromInt2;
132+
pub const vec3dFromInt = Vec3d.fromInt3;
133+
pub const vec4dFromInt = Vec4d.fromInt4;
113134
pub const quatd = Quatd.init;
114-
pub const mat2x2d = Mat2x2d.init;
115-
pub const mat3x3d = Mat3x3d.init;
116-
pub const mat4x4d = Mat4x4d.init;
135+
pub const mat2x2d = Mat2x2d.init2;
136+
pub const mat3x3d = Mat3x3d.init3;
137+
pub const mat4x4d = Mat4x4d.init4;
117138

118139
test {
119140
testing.refAllDeclsRecursive(@This());

0 commit comments

Comments
 (0)