add mix & vector * scalar ops

This commit is contained in:
Matthew 2025-12-31 19:10:57 +11:00
parent 1160d747cc
commit 10c7fdcaed

27
math.d
View File

@ -164,7 +164,7 @@ struct Vector(T, int N)
Vector opUnary(string op)() if(op == "+" || op == "-" || op == "~" || op == "!")
{
Vector result;
Vector res;
mixin(GenerateLoop!("res.v[@] = " ~ op ~ " v[@];", N)());
return res;
}
@ -202,6 +202,14 @@ struct Vector(T, int N)
}
}
Vector
opBinaryRight(string op, U)(U operand) if(IsConvertible!(U) && (op == "*" || op == "+" || op == "-" || op == "/"))
{
Vector res;
mixin(GenerateLoop!("res.v[@] = v[@] "~op~" operand;", N));
return res;
}
static if(N == 4)
{
Vector opBinary(string op, U)(U operand) if((is(U: Vector!(f32, 4)) && is(T: f32)) && (op == "*" || op == "+" || op == "-" || op == "/"))
@ -837,6 +845,12 @@ Ortho(f32 left, f32 bottom, f32 right, f32 top, f32 near, f32 far)
return mat;
}
T
Mix(T, U)(T a, T b, U t) if((IsVector!(T) || is(T == f32) || is(T == f64)) && (is(U == f32) || is(U == f64)))
{
return a + t * (b - a);
}
pragma(inline) Vec3
Rotate(Quat q, Vec3 vec)
{
@ -1192,5 +1206,16 @@ version(DLIB_TEST) unittest
test = Clamp(test, low, high);
assert(test == high);
}
{
Vec4 m0 = Vec4(0.0);
Vec4 m1 = Vec4(10.0);
assert(Mix(m0, m1, 0.0) == m0);
assert(Mix(m0, m1, 1.0) == m1);
assert(Mix(m0, m1, 0.5) == Vec4(5.0));
assert(Mix(m0, m1, 0.75) == Vec4(7.5));
}
}