Add mulVec function for vectors.

Add function to allow a matrix to be left multiplied by a vector.
This commit is contained in:
Om Prakaash 2023-10-04 14:06:21 -07:00 committed by Stephen Gutekanst
parent 1c16060eaa
commit 7bd2bc8d24
2 changed files with 64 additions and 3 deletions

View file

@ -361,16 +361,17 @@ pub fn Mat(
}
/// Matrix * Vector multiplication
pub inline fn mulVec(a: *const Matrix, b: *const ColVec) ColVec {
var result = [_]ColVec.T{0} ** ColVec.n;
pub inline fn mulVec(matrix: *const Matrix, vector: *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];
result[i] += matrix.v[row].v[i] * vector.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
// 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.
@ -686,3 +687,6 @@ test "Mat4x4_mulVec_vec4" {
const expected = math.vec4(4, 47, 5, 68);
try testing.expect(math.Vec4, expected).eql(m);
}