diff --git a/libs/math/include/math/TQuatHelpers.h b/libs/math/include/math/TQuatHelpers.h index cd3342c075..e2a89dc0d7 100644 --- a/libs/math/include/math/TQuatHelpers.h +++ b/libs/math/include/math/TQuatHelpers.h @@ -58,7 +58,8 @@ public: /* compound assignment products by a scalar */ - constexpr QUATERNION& operator*=(T v) { + template + constexpr QUATERNION& operator*=(U v) { QUATERNION& lhs = static_cast&>(*this); for (size_t i = 0; i < QUATERNION::size(); i++) { lhs[i] *= v; @@ -66,7 +67,8 @@ public: return lhs; } - constexpr QUATERNION& operator/=(T v) { + template + constexpr QUATERNION& operator/=(U v) { QUATERNION& lhs = static_cast&>(*this); for (size_t i = 0; i < QUATERNION::size(); i++) { lhs[i] /= v; @@ -85,28 +87,29 @@ public: /* The operators below handle operation between quaternions of the same size * but of a different element type. */ - template - friend inline - constexpr QUATERNION MATH_PURE operator*(const QUATERNION& q, const QUATERNION& r) { + template + friend inline constexpr + QUATERNION> MATH_PURE operator*( + const QUATERNION& q, const QUATERNION& r) { // could be written as: // return QUATERNION( // q.w*r.w - dot(q.xyz, r.xyz), // q.w*r.xyz + r.w*q.xyz + cross(q.xyz, r.xyz)); - - return QUATERNION( + return { q.w * r.w - q.x * r.x - q.y * r.y - q.z * r.z, q.w * r.x + q.x * r.w + q.y * r.z - q.z * r.y, q.w * r.y - q.x * r.z + q.y * r.w + q.z * r.x, - q.w * r.z + q.x * r.y - q.y * r.x + q.z * r.w); + q.w * r.z + q.x * r.y - q.y * r.x + q.z * r.w + }; } - template - friend inline - constexpr TVec3 MATH_PURE operator*(const QUATERNION& q, const TVec3& v) { + template + friend inline constexpr + TVec3> MATH_PURE operator*(const QUATERNION& q, const TVec3& v) { // note: if q is known to be a unit quaternion, then this simplifies to: // TVec3 t = 2 * cross(q.xyz, v) // return v + (q.w * t) + cross(q.xyz, t) - return imaginary(q * QUATERNION(v, 0) * inverse(q)); + return imaginary(q * QUATERNION(v, 0) * inverse(q)); } @@ -122,20 +125,23 @@ public: * q.w*r.z + q.x*r.y - q.y*r.x + q.z*r.w); * */ - friend inline - constexpr QUATERNION MATH_PURE operator*(QUATERNION q, T scalar) { + template + friend inline constexpr + QUATERNION> MATH_PURE operator*(QUATERNION q, U scalar) { // don't pass q by reference because we need a copy anyway return q *= scalar; } - friend inline - constexpr QUATERNION MATH_PURE operator*(T scalar, QUATERNION q) { + template + friend inline constexpr + QUATERNION> MATH_PURE operator*(T scalar, QUATERNION q) { // don't pass q by reference because we need a copy anyway return q *= scalar; } - friend inline - constexpr QUATERNION MATH_PURE operator/(QUATERNION q, T scalar) { + template + friend inline constexpr + QUATERNION> MATH_PURE operator/(QUATERNION q, U scalar) { // don't pass q by reference because we need a copy anyway return q /= scalar; } @@ -160,9 +166,10 @@ public: * (the first one, BASE being known). */ - template - friend inline - constexpr T MATH_PURE dot(const QUATERNION& p, const QUATERNION& q) { + template + friend inline constexpr + arithmetic_result_t MATH_PURE dot( + const QUATERNION& p, const QUATERNION& q) { return p.x * q.x + p.y * q.y + p.z * q.z + @@ -196,7 +203,7 @@ public: friend inline constexpr QUATERNION MATH_PURE inverse(const QUATERNION& q) { - return conj(q) * (1 / dot(q, q)); + return conj(q) * (T(1) / dot(q, q)); } friend inline @@ -214,8 +221,10 @@ public: return QUATERNION(q.xyz, 0); } - friend inline - constexpr QUATERNION MATH_PURE cross(const QUATERNION& p, const QUATERNION& q) { + template + friend inline constexpr + QUATERNION> MATH_PURE cross( + const QUATERNION& p, const QUATERNION& q) { return unreal(p * q); }