Added mulVec function for matrices
Added function to multiply a matrix with a vector (Matrix* Vec).
This commit is contained in:
parent
a3671ab0ef
commit
3527937086
1 changed files with 52 additions and 0 deletions
|
|
@ -44,6 +44,9 @@ pub fn Mat(
|
||||||
/// The Vec type corresponding to the number of rows, e.g. Mat3x3.RowVec == Vec3
|
/// The Vec type corresponding to the number of rows, e.g. Mat3x3.RowVec == Vec3
|
||||||
pub const RowVec = vec.Vec(rows, T);
|
pub const RowVec = vec.Vec(rows, T);
|
||||||
|
|
||||||
|
/// The Vec type corresponding to the numebr of cols, e.g. Mat3x4.ColVec = Vec4
|
||||||
|
pub const ColVec = vec.Vec(cols, T);
|
||||||
|
|
||||||
const Matrix = @This();
|
const Matrix = @This();
|
||||||
|
|
||||||
/// Identity matrix
|
/// Identity matrix
|
||||||
|
|
@ -357,6 +360,19 @@ pub fn Mat(
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Matrix - Vector multiplicatino: Mat * Vec
|
||||||
|
pub inline fn mulVec(a: *const Matrix, b: *const ColVec) ColVec {
|
||||||
|
var result = [_]ColVec.T{0}**ColVec.n;
|
||||||
|
inline for (0..Matrix.rows) |row| {
|
||||||
|
inline for (0..ColVec.n) |i| {
|
||||||
|
result[i] += a.v[row].v[i] * b.v[row];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vec.Vec(ColVec.n, ColVec.T){
|
||||||
|
.v = result
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: the below code was correct in our old implementation, it just needs to be updated
|
// TODO: the below code was correct in our old implementation, it just needs to be updated
|
||||||
// to work with this new Mat approach, swapping f32 for the generic T float type, moving 3x3
|
// to work with this new Mat approach, swapping f32 for the generic T float type, moving 3x3
|
||||||
// and 4x4 specific functions into the mixin above, writing new tests, etc.
|
// and 4x4 specific functions into the mixin above, writing new tests, etc.
|
||||||
|
|
@ -636,3 +652,39 @@ test "Mat4x4_perspective" {
|
||||||
|
|
||||||
try testing.expect(math.Mat4x4, expected).eql(m);
|
try testing.expect(math.Mat4x4, expected).eql(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "Mat3x3_mulVec_vec3_ident" {
|
||||||
|
const v = math.Vec3.splat(1);
|
||||||
|
const ident = math.Mat3x3.ident;
|
||||||
|
const expected = v;
|
||||||
|
var m = math.Mat3x3.mulVec(&ident, &v);
|
||||||
|
|
||||||
|
try testing.expect(math.Vec3, expected).eql(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Mat3x3_mulVec_vec3" {
|
||||||
|
const v = math.Vec3.splat(1);
|
||||||
|
const mat = math.Mat3x3.init(
|
||||||
|
&math.vec3(2, 0, 0),
|
||||||
|
&math.vec3(0, 2, 0),
|
||||||
|
&math.vec3(0, 0, 3),
|
||||||
|
);
|
||||||
|
|
||||||
|
const m = math.Mat3x3.mulVec(&mat, &v);
|
||||||
|
const expected = math.vec3(2,2,3);
|
||||||
|
try testing.expect(math.Vec3, expected).eql(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Mat4x4_mulVec_ve4" {
|
||||||
|
const v = math.vec4(2, 5, 1, 8);
|
||||||
|
const mat = math.Mat4x4.init(
|
||||||
|
&math.vec4(1, 0, 2, 0),
|
||||||
|
&math.vec4(0, 3, 0, 4),
|
||||||
|
&math.vec4(0, 0, 5, 0),
|
||||||
|
&math.vec4(6, 0, 0, 7),
|
||||||
|
);
|
||||||
|
|
||||||
|
const m = math.Mat4x4.mulVec(&mat, &v);
|
||||||
|
const expected = math.vec4(4, 47, 5, 68);
|
||||||
|
try testing.expect(math.Vec4, expected).eql(m);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue