diff --git a/libs/math/include/math/TMatHelpers.h b/libs/math/include/math/TMatHelpers.h index 7281f81445..ca4b171c76 100644 --- a/libs/math/include/math/TMatHelpers.h +++ b/libs/math/include/math/TMatHelpers.h @@ -110,7 +110,7 @@ constexpr MATRIX MATH_PURE gaussJordanInverse(const MATRIX& src) { //------------------------------------------------------------------------------ // 2x2 matrix inverse is easy. -template +template constexpr MATRIX MATH_PURE fastInverse2(const MATRIX& x) { typedef typename MATRIX::value_type T; @@ -143,7 +143,7 @@ constexpr MATRIX MATH_PURE fastInverse2(const MATRIX& x) { // From the Wikipedia article on matrix inversion's section on fast 3x3 // matrix inversion: // http://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_3.C3.973_matrices -template +template constexpr MATRIX MATH_PURE fastInverse3(const MATRIX& x) { typedef typename MATRIX::value_type T; @@ -217,6 +217,7 @@ class Matrix { T m[ORDER][ORDER]; public: constexpr auto operator[](size_t i) const noexcept { return m[i]; } + constexpr auto& operator[](size_t i) noexcept { return m[i]; } static constexpr Matrix submatrix(Matrix in, size_t row, size_t col) noexcept { @@ -287,7 +288,7 @@ constexpr MATRIX MATH_PURE cofactor(const MATRIX& m) { return out; } -template +template constexpr MATRIX MATH_PURE fastCofactor2(const MATRIX& m) { typedef typename MATRIX::value_type T; @@ -315,7 +316,7 @@ constexpr MATRIX MATH_PURE fastCofactor2(const MATRIX& m) { return cof; } -template +template constexpr MATRIX MATH_PURE fastCofactor3(const MATRIX& m) { typedef typename MATRIX::value_type T; @@ -365,7 +366,7 @@ constexpr MATRIX MATH_PURE fastCofactor3(const MATRIX& m) { /** * Cofactor function which switches on the matrix size. */ -template +template inline constexpr MATRIX MATH_PURE cof(const MATRIX& matrix) { static_assert(MATRIX::NUM_ROWS == MATRIX::NUM_COLS, "only square matrices"); return (MATRIX::NUM_ROWS == 2) ? fastCofactor2(matrix) : @@ -376,7 +377,7 @@ inline constexpr MATRIX MATH_PURE cof(const MATRIX& matrix) { /** * Determinant of a matrix */ -template +template inline constexpr typename MATRIX::value_type MATH_PURE det(const MATRIX& matrix) { typedef typename MATRIX::value_type T; static_assert(MATRIX::NUM_ROWS == MATRIX::NUM_COLS, "only square matrices"); @@ -396,12 +397,12 @@ inline constexpr typename MATRIX::value_type MATH_PURE det(const MATRIX& matrix) * undefined if it is not. It is the responsibility of the caller to * make sure the matrix is not singular. */ -template +template inline constexpr MATRIX MATH_PURE inverse(const MATRIX& matrix) { static_assert(MATRIX::NUM_ROWS == MATRIX::NUM_COLS, "only square matrices can be inverted"); return (MATRIX::NUM_ROWS == 2) ? fastInverse2(matrix) : - ((MATRIX::NUM_ROWS == 3) ? fastInverse3(matrix) : - gaussJordanInverse(matrix)); + ((MATRIX::NUM_ROWS == 3) ? fastInverse3(matrix) : + gaussJordanInverse(matrix)); } template @@ -426,7 +427,7 @@ constexpr MATRIX_R MATH_PURE multiply(const MATRIX_A& lhs, const MATRIX_B& rhs) } // transpose. this handles matrices of matrices -template +template constexpr MATRIX MATH_PURE transpose(const MATRIX& m) { // for now we only handle square matrix transpose static_assert(MATRIX::NUM_COLS == MATRIX::NUM_ROWS, "transpose only supports square matrices"); @@ -440,7 +441,7 @@ constexpr MATRIX MATH_PURE transpose(const MATRIX& m) { } // trace. this handles matrices of matrices -template +template constexpr typename MATRIX::value_type MATH_PURE trace(const MATRIX& m) { static_assert(MATRIX::NUM_COLS == MATRIX::NUM_ROWS, "trace only defined for square matrices"); typename MATRIX::value_type result(0); @@ -451,7 +452,7 @@ constexpr typename MATRIX::value_type MATH_PURE trace(const MATRIX& m) { } // diag. this handles matrices of matrices -template +template constexpr typename MATRIX::col_type MATH_PURE diag(const MATRIX& m) { static_assert(MATRIX::NUM_COLS == MATRIX::NUM_ROWS, "diag only defined for square matrices"); typename MATRIX::col_type result{}; @@ -463,7 +464,7 @@ constexpr typename MATRIX::col_type MATH_PURE diag(const MATRIX& m) { //------------------------------------------------------------------------------ // This is taken from the Imath MatrixAlgo code, and is identical to Eigen. -template +template TQuaternion extractQuat(const MATRIX& mat) { typedef typename MATRIX::value_type T; @@ -518,11 +519,11 @@ TQuaternion extractQuat(const MATRIX& mat) { * get all the functionality here. */ -template class BASE, typename T> +template class BASE, typename T> class TMatProductOperators { public: // multiply by a scalar - constexpr BASE& operator *= (T v) { + constexpr BASE& operator*=(T v) { BASE& lhs(static_cast< BASE& >(*this)); for (size_t col = 0; col < BASE::NUM_COLS; ++col) { lhs[col] *= v; @@ -532,14 +533,14 @@ public: // matrix *= matrix template - constexpr BASE& operator *= (const BASE& rhs) { + constexpr BASE& operator*=(const BASE& rhs) { BASE& lhs(static_cast< BASE& >(*this)); lhs = matrix::multiply >(lhs, rhs); return lhs; } // divide by a scalar - constexpr BASE& operator /= (T v) { + constexpr BASE& operator/=(T v) { BASE& lhs(static_cast< BASE& >(*this)); for (size_t col = 0; col < BASE::NUM_COLS; ++col) { lhs[col] /= v; @@ -549,7 +550,7 @@ public: // matrix * matrix, result is a matrix of the same type than the lhs matrix template - friend inline constexpr BASE MATH_PURE operator *(const BASE& lhs, const BASE& rhs) { + friend inline constexpr BASE MATH_PURE operator*(const BASE& lhs, const BASE& rhs) { return matrix::multiply >(lhs, rhs); } @@ -592,15 +593,19 @@ public: friend inline constexpr BASE MATH_PURE inverse(const BASE& matrix) { return matrix::inverse(matrix); } + friend inline constexpr BASE MATH_PURE cof(const BASE& matrix) { return matrix::cof(matrix); } + friend inline constexpr BASE MATH_PURE transpose(const BASE& m) { return matrix::transpose(m); } + friend inline constexpr T MATH_PURE trace(const BASE& m) { return matrix::trace(m); } + friend inline constexpr T MATH_PURE det(const BASE& m) { return matrix::det(m); } @@ -692,11 +697,11 @@ public: * @param pitch about X axis * @param roll about Z axis */ - template < - typename Y, typename P, typename R, - typename = typename std::enable_if::value >::type, - typename = typename std::enable_if::value >::type, - typename = typename std::enable_if::value >::type + template< + typename Y, typename P, typename R, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type > static BASE eulerYXZ(Y yaw, P pitch, R roll) { return eulerZYX(roll, pitch, yaw); @@ -711,11 +716,11 @@ public: * The euler angles are applied in ZYX order. i.e: a vector is first rotated * about X (roll) then Y (pitch) and then Z (yaw). */ - template < - typename Y, typename P, typename R, - typename = typename std::enable_if::value >::type, - typename = typename std::enable_if::value >::type, - typename = typename std::enable_if::value >::type + template< + typename Y, typename P, typename R, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::type > static BASE eulerZYX(Y yaw, P pitch, R roll) { BASE r; @@ -748,13 +753,13 @@ public: return r; } - TQuaternion toQuaternion() const { + TQuaternion toQuaternion() const { return matrix::extractQuat(static_cast&>(*this)); } }; -template class BASE, typename T> +template class BASE, typename T> class TMatDebug { public: friend std::ostream& operator<<(std::ostream& stream, const BASE& m) { @@ -764,7 +769,7 @@ public: } if (row == 0) { stream << "/ "; - } else if (row == BASE::NUM_ROWS-1) { + } else if (row == BASE::NUM_ROWS - 1) { stream << "\\ "; } else { stream << "| "; @@ -774,7 +779,7 @@ public: } if (row == 0) { stream << " \\"; - } else if (row == BASE::NUM_ROWS-1) { + } else if (row == BASE::NUM_ROWS - 1) { stream << " /"; } else { stream << " |"; diff --git a/libs/math/include/math/TQuatHelpers.h b/libs/math/include/math/TQuatHelpers.h index 9e6cb76445..4e06fe4bc1 100644 --- a/libs/math/include/math/TQuatHelpers.h +++ b/libs/math/include/math/TQuatHelpers.h @@ -48,14 +48,14 @@ namespace details { * get all the functionality here. */ -template class QUATERNION, typename T> +template class QUATERNION, typename T> class TQuatProductOperators { public: /* compound assignment from a another quaternion of the same size but different * element type. */ - template - constexpr QUATERNION& operator *= (const QUATERNION& r) { + template + constexpr QUATERNION& operator*=(const QUATERNION& r) { QUATERNION& q = static_cast&>(*this); q = q * r; return q; @@ -63,14 +63,15 @@ public: /* compound assignment products by a scalar */ - constexpr QUATERNION& operator *= (T v) { + constexpr QUATERNION& operator*=(T v) { QUATERNION& lhs = static_cast&>(*this); for (size_t i = 0; i < QUATERNION::size(); i++) { lhs[i] *= v; } return lhs; } - constexpr QUATERNION& operator /= (T v) { + + constexpr QUATERNION& operator/=(T v) { QUATERNION& lhs = static_cast&>(*this); for (size_t i = 0; i < QUATERNION::size(); i++) { lhs[i] /= v; @@ -91,22 +92,22 @@ public: */ template friend inline - constexpr QUATERNION MATH_PURE operator *(const QUATERNION& q, const QUATERNION& r) { + 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( - 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.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); } template friend inline - constexpr TVec3 MATH_PURE operator *(const QUATERNION& q, const TVec3& v) { + 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) @@ -127,18 +128,19 @@ public: * */ friend inline - constexpr QUATERNION MATH_PURE operator *(QUATERNION q, T scalar) { - // don't pass q by reference because we need a copy anyways - return q *= scalar; - } - friend inline - constexpr QUATERNION MATH_PURE operator *(T scalar, QUATERNION q) { + constexpr QUATERNION MATH_PURE operator*(QUATERNION q, T scalar) { // don't pass q by reference because we need a copy anyways return q *= scalar; } friend inline - constexpr QUATERNION MATH_PURE operator /(QUATERNION q, T scalar) { + constexpr QUATERNION MATH_PURE operator*(T scalar, QUATERNION q) { + // don't pass q by reference because we need a copy anyways + return q *= scalar; + } + + friend inline + constexpr QUATERNION MATH_PURE operator/(QUATERNION q, T scalar) { // don't pass q by reference because we need a copy anyways return q /= scalar; } @@ -152,7 +154,7 @@ public: * By simply inheriting from TQuatFunctions BASE will automatically * get all the functionality here. */ -template class QUATERNION, typename T> +template class QUATERNION, typename T> class TQuatFunctions { public: /* @@ -174,7 +176,7 @@ public: friend inline T MATH_PURE norm(const QUATERNION& q) { - return std::sqrt( dot(q, q) ); + return std::sqrt(dot(q, q)); } friend inline @@ -219,26 +221,26 @@ public: friend inline constexpr QUATERNION MATH_PURE cross(const QUATERNION& p, const QUATERNION& q) { - return unreal(p*q); + return unreal(p * q); } friend inline QUATERNION MATH_PURE exp(const QUATERNION& q) { const T nq(norm(q.xyz)); - return std::exp(q.w)*QUATERNION((sin(nq)/nq)*q.xyz, cos(nq)); + return std::exp(q.w) * QUATERNION((sin(nq) / nq) * q.xyz, cos(nq)); } friend inline QUATERNION MATH_PURE log(const QUATERNION& q) { const T nq(norm(q)); - return QUATERNION((std::acos(q.w/nq)/norm(q.xyz))*q.xyz, log(nq)); + return QUATERNION((std::acos(q.w / nq) / norm(q.xyz)) * q.xyz, log(nq)); } friend inline QUATERNION MATH_PURE pow(const QUATERNION& q, T a) { // could also be computed as: exp(a*log(q)); const T nq(norm(q)); - const T theta(a*std::acos(q.w / nq)); + const T theta(a * std::acos(q.w / nq)); return std::pow(nq, a) * QUATERNION(normalize(q.xyz) * std::sin(theta), std::cos(theta)); } @@ -289,7 +291,7 @@ public: * By simply inheriting from TQuatDebug BASE will automatically * get all the functionality here. */ -template class QUATERNION, typename T> +template class QUATERNION, typename T> class TQuatDebug { public: /* @@ -299,7 +301,7 @@ public: * is instantiated, at which point they're only templated on the 2nd parameter * (the first one, BASE being known). */ - friend std::ostream& operator<< (std::ostream& stream, const QUATERNION& q) { + friend std::ostream& operator<<(std::ostream& stream, const QUATERNION& q) { return stream << "< " << q.w << " + " << q.x << "i + " << q.y << "j + " << q.z << "k >"; } }; diff --git a/libs/math/include/math/TVecHelpers.h b/libs/math/include/math/TVecHelpers.h index f2a1910511..438a4a9275 100644 --- a/libs/math/include/math/TVecHelpers.h +++ b/libs/math/include/math/TVecHelpers.h @@ -38,6 +38,7 @@ template inline constexpr U min(U a, U b) noexcept { return a < b ? a : b; } + template inline constexpr U max(U a, U b) noexcept { return a > b ? a : b; @@ -58,7 +59,7 @@ inline constexpr U max(U a, U b) noexcept { * get all the functionality here. */ -template class VECTOR, typename T> +template class VECTOR, typename T> class TVecAddOperators { public: /* compound assignment from a another vector of the same size but different @@ -115,12 +116,13 @@ public: * but of a different element type. */ template - friend inline constexpr VECTOR MATH_PURE operator +(VECTOR lv, const VECTOR& rv) { + friend inline constexpr VECTOR MATH_PURE operator+(VECTOR lv, const VECTOR& rv) { // don't pass lv by reference because we need a copy anyways return lv += rv; } + template - friend inline constexpr VECTOR MATH_PURE operator -(VECTOR lv, const VECTOR& rv) { + friend inline constexpr VECTOR MATH_PURE operator-(VECTOR lv, const VECTOR& rv) { // don't pass lv by reference because we need a copy anyways return lv -= rv; } @@ -131,11 +133,12 @@ public: * letting the compiler implicitly convert a scalar to a vector (assuming * the BASE allows it). */ - friend inline constexpr VECTOR MATH_PURE operator +(VECTOR lv, const VECTOR& rv) { + friend inline constexpr VECTOR MATH_PURE operator+(VECTOR lv, const VECTOR& rv) { // don't pass lv by reference because we need a copy anyways return lv += rv; } - friend inline constexpr VECTOR MATH_PURE operator -(VECTOR lv, const VECTOR& rv) { + + friend inline constexpr VECTOR MATH_PURE operator-(VECTOR lv, const VECTOR& rv) { // don't pass lv by reference because we need a copy anyways return lv -= rv; } @@ -238,7 +241,7 @@ public: template class VECTOR, typename T> class TVecUnaryOperators { public: - constexpr VECTOR operator -() const { + constexpr VECTOR operator-() const { VECTOR r{}; VECTOR const& rv(static_cast const&>(*this)); for (size_t i = 0; i < r.size(); i++) { @@ -467,13 +470,14 @@ public: } friend inline constexpr VECTOR MATH_PURE clamp(VECTOR v, VECTOR min, VECTOR max) { - for (size_t i=0 ; i< v.size() ; i++) { + for (size_t i = 0; i < v.size(); i++) { v[i] = details::min(max[i], details::max(min[i], v[i])); } return v; } - friend inline constexpr VECTOR MATH_PURE fma(const VECTOR& lv, const VECTOR& rv, VECTOR a) { + friend inline constexpr VECTOR MATH_PURE fma(const VECTOR& lv, const VECTOR& rv, + VECTOR a) { for (size_t i = 0; i < lv.size(); i++) { a[i] += (lv[i] * rv[i]); } @@ -504,7 +508,7 @@ public: friend inline constexpr T MATH_PURE min(const VECTOR& v) { T r(std::numeric_limits::max()); - for (size_t i=0 ; i::value>::type> +template::value>::type> constexpr T MATH_PURE sin(T x) noexcept { return filament::math::fast::cos(x - T(M_PI_2)); } @@ -111,9 +111,9 @@ constexpr unsigned int factorial(unsigned int x) noexcept { constexpr double exp(double x) noexcept { return 1.0 + x + pow(x, 2) / factorial(2) + pow(x, 3) / factorial(3) - + pow(x, 4) / factorial(4) + pow(x, 5) / factorial(5) - + pow(x, 6) / factorial(6) + pow(x, 7) / factorial(7) - + pow(x, 8) / factorial(8) + pow(x, 9) / factorial(9); + + pow(x, 4) / factorial(4) + pow(x, 5) / factorial(5) + + pow(x, 6) / factorial(6) + pow(x, 7) / factorial(7) + + pow(x, 8) / factorial(8) + pow(x, 9) / factorial(9); } constexpr float exp(float x) noexcept { @@ -122,16 +122,22 @@ constexpr float exp(float x) noexcept { inline float MATH_PURE pow(float a, float b) noexcept { constexpr int fudgeMinRMSE = 486411; - constexpr int K = (127<<23) - fudgeMinRMSE; - union { float f; int x; } u = { a }; + constexpr int K = (127 << 23) - fudgeMinRMSE; + union { + float f; + int x; + } u = { a }; u.x = (int)(b * (u.x - K) + K); return u.f; } // this is more precise than pow() above inline float MATH_PURE pow2dot2(float a) noexcept { - union { float f; int x; } u = { a }; - constexpr int K = (127<<23); + union { + float f; + int x; + } u = { a }; + constexpr int K = (127 << 23); u.x = (int)(0.2f * (u.x - K) + K); return a * a * u.f; // a^2 * a^0.2 } @@ -149,11 +155,12 @@ inline uint8_t MATH_PURE qsub(uint8_t a, uint8_t b) noexcept { return vqsubb_ inline uint16_t MATH_PURE qsub(uint16_t a, uint16_t b) noexcept { return vqsubh_s16(a, b); } inline uint32_t MATH_PURE qsub(uint32_t a, uint32_t b) noexcept { return vqsubs_s32(a, b); } #else + template::value || std::is_same::value || std::is_same::value>::type> -inline T MATH_PURE qadd(T a, T b) noexcept { +inline T MATH_PURE qadd(T a, T b) noexcept { T r = a + b; return r | -T(r < a); } @@ -162,19 +169,20 @@ template::value || std::is_same::value || std::is_same::value>::type> -inline T MATH_PURE qsub(T a, T b) noexcept { +inline T MATH_PURE qsub(T a, T b) noexcept { T r = a - b; return r & -T(r <= a); } + #endif template -inline T MATH_PURE qinc(T a) noexcept { +inline T MATH_PURE qinc(T a) noexcept { return qadd(a, T(1)); } template -inline T MATH_PURE qdec(T a) noexcept { +inline T MATH_PURE qdec(T a) noexcept { return qsub(a, T(1)); } diff --git a/libs/math/include/math/mat3.h b/libs/math/include/math/mat3.h index 2d82fe8644..905da31ea1 100644 --- a/libs/math/include/math/mat3.h +++ b/libs/math/include/math/mat3.h @@ -86,8 +86,8 @@ public: typedef T& reference; typedef T const& const_reference; typedef size_t size_type; - typedef TVec3 col_type; - typedef TVec3 row_type; + typedef TVec3 col_type; + typedef TVec3 row_type; static constexpr size_t COL_SIZE = col_type::SIZE; // size of a column (i.e.: number of rows) static constexpr size_t ROW_SIZE = row_type::SIZE; // size of a row (i.e.: number of columns) @@ -179,7 +179,7 @@ public: * \f$ */ template - constexpr explicit TMat33(const TVec3 & v); + constexpr explicit TMat33(const TVec3& v); /** * construct from another matrix of the same size @@ -199,7 +199,7 @@ public: * \f$ */ template - constexpr TMat33(const TVec3 & v0, const TVec3 & v1, const TVec3 & v2); + constexpr TMat33(const TVec3& v0, const TVec3& v1, const TVec3& v2); /** construct from 9 elements in column-major form. * @@ -245,7 +245,7 @@ public: * construct from a quaternion */ template - constexpr explicit TMat33(const TQuaternion & q); + constexpr explicit TMat33(const TQuaternion& q); /** * orthogonalize only works on matrices of size 3x3 @@ -280,24 +280,24 @@ public: * to 2 bytes, making the resulting quaternion suitable for storage into an SNORM16 * vector. */ - static constexpr TQuaternion packTangentFrame(const TMat33& m, - size_t storageSize = sizeof(int16_t)); + static constexpr TQuaternion packTangentFrame( + const TMat33& m, size_t storageSize = sizeof(int16_t)); template - static constexpr TMat33 translation(const TVec3 & t) { + static constexpr TMat33 translation(const TVec3& t) { TMat33 r; r[2] = t; return r; } template - static constexpr TMat33 scaling(const TVec3 & s) { + static constexpr TMat33 scaling(const TVec3& s) { return TMat33{ s }; } template static constexpr TMat33 scaling(A s) { - return TMat33{ TVec3 < T > { s }}; + return TMat33{ TVec3{ s }}; } }; @@ -327,7 +327,7 @@ constexpr TMat33::TMat33(U v) template template -constexpr TMat33::TMat33(const TVec3 & v) +constexpr TMat33::TMat33(const TVec3& v) : m_value{ col_type(v[0], 0, 0), col_type(0, v[1], 0), @@ -362,13 +362,13 @@ constexpr TMat33::TMat33(const TMat33& rhs) { // Construct from 3 column vectors. template template -constexpr TMat33::TMat33(const TVec3 & v0, const TVec3 & v1, const TVec3 & v2) +constexpr TMat33::TMat33(const TVec3& v0, const TVec3& v1, const TVec3& v2) : m_value{ v0, v1, v2 } { } template template -constexpr TMat33::TMat33(const TQuaternion & q) : m_value{} { +constexpr TMat33::TMat33(const TQuaternion& q) : m_value{} { const U n = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w; const U s = n > 0 ? 2 / n : 0; const U x = s * q.x; @@ -403,7 +403,7 @@ constexpr TMat33::TMat33(const TQuaternion & q) : m_value{} { // matrix * column-vector, result is a vector of the same type than the input vector template constexpr typename TMat33::col_type MATH_PURE operator*(const TMat33& lhs, - const TVec3 & rhs) { + const TVec3& rhs) { // Result is initialized to zero. typename TMat33::col_type result{}; for (size_t col = 0; col < TMat33::NUM_COLS; ++col) { @@ -414,7 +414,7 @@ constexpr typename TMat33::col_type MATH_PURE operator*(const TMat33& lhs, // row-vector * matrix, result is a vector of the same type than the input vector template -constexpr typename TMat33::row_type MATH_PURE operator*(const TVec3 & lhs, +constexpr typename TMat33::row_type MATH_PURE operator*(const TVec3& lhs, const TMat33& rhs) { typename TMat33::row_type result{}; for (size_t col = 0; col < TMat33::NUM_COLS; ++col) { @@ -439,8 +439,8 @@ operator*(U lhs, const TMat33& rhs) { //------------------------------------------------------------------------------ template -constexpr TQuaternion TMat33::packTangentFrame(const TMat33& m, size_t storageSize) { - TQuaternion q = TMat33{ m[0], cross(m[2], m[0]), m[2] }.toQuaternion(); +constexpr TQuaternion TMat33::packTangentFrame(const TMat33& m, size_t storageSize) { + TQuaternion q = TMat33{ m[0], cross(m[2], m[0]), m[2] }.toQuaternion(); q = positive(normalize(q)); // Ensure w is never 0.0 diff --git a/libs/math/include/math/norm.h b/libs/math/include/math/norm.h index 627fe9e8fb..d4d99ba8a1 100644 --- a/libs/math/include/math/norm.h +++ b/libs/math/include/math/norm.h @@ -31,7 +31,7 @@ inline uint16_t packUnorm16(float v) noexcept { } inline ushort4 packUnorm16(float4 v) noexcept { - return ushort4{packUnorm16(v.x), packUnorm16(v.y), packUnorm16(v.z), packUnorm16(v.w)}; + return ushort4{ packUnorm16(v.x), packUnorm16(v.y), packUnorm16(v.z), packUnorm16(v.w) }; } inline int16_t packSnorm16(float v) noexcept { @@ -39,11 +39,11 @@ inline int16_t packSnorm16(float v) noexcept { } inline short2 packSnorm16(float2 v) noexcept { - return short2{packSnorm16(v.x), packSnorm16(v.y)}; + return short2{ packSnorm16(v.x), packSnorm16(v.y) }; } inline short4 packSnorm16(float4 v) noexcept { - return short4{packSnorm16(v.x), packSnorm16(v.y), packSnorm16(v.z), packSnorm16(v.w)}; + return short4{ packSnorm16(v.x), packSnorm16(v.y), packSnorm16(v.z), packSnorm16(v.w) }; } inline float unpackUnorm16(uint16_t v) noexcept { @@ -51,7 +51,7 @@ inline float unpackUnorm16(uint16_t v) noexcept { } inline float4 unpackUnorm16(ushort4 v) noexcept { - return float4{unpackUnorm16(v.x), unpackUnorm16(v.y), unpackUnorm16(v.z), unpackUnorm16(v.w)}; + return float4{ unpackUnorm16(v.x), unpackUnorm16(v.y), unpackUnorm16(v.z), unpackUnorm16(v.w) }; } inline float unpackSnorm16(int16_t v) noexcept { @@ -59,7 +59,7 @@ inline float unpackSnorm16(int16_t v) noexcept { } inline float4 unpackSnorm16(short4 v) noexcept { - return float4{unpackSnorm16(v.x), unpackSnorm16(v.y), unpackSnorm16(v.z), unpackSnorm16(v.w)}; + return float4{ unpackSnorm16(v.x), unpackSnorm16(v.y), unpackSnorm16(v.z), unpackSnorm16(v.w) }; } inline uint8_t packUnorm8(float v) noexcept { @@ -67,7 +67,7 @@ inline uint8_t packUnorm8(float v) noexcept { } inline ubyte4 packUnorm8(float4 v) noexcept { - return ubyte4{packUnorm8(v.x), packUnorm8(v.y), packUnorm8(v.z), packUnorm8(v.w)}; + return ubyte4{ packUnorm8(v.x), packUnorm8(v.y), packUnorm8(v.z), packUnorm8(v.w) }; } inline int8_t packSnorm8(float v) noexcept { @@ -75,7 +75,7 @@ inline int8_t packSnorm8(float v) noexcept { } inline byte4 packSnorm8(float4 v) noexcept { - return byte4{packSnorm8(v.x), packSnorm8(v.y), packSnorm8(v.z), packSnorm8(v.w)}; + return byte4{ packSnorm8(v.x), packSnorm8(v.y), packSnorm8(v.z), packSnorm8(v.w) }; } inline float unpackUnorm8(uint8_t v) noexcept { @@ -83,7 +83,7 @@ inline float unpackUnorm8(uint8_t v) noexcept { } inline float4 unpackUnorm8(ubyte4 v) noexcept { - return float4{unpackUnorm8(v.x), unpackUnorm8(v.y), unpackUnorm8(v.z), unpackUnorm8(v.w)}; + return float4{ unpackUnorm8(v.x), unpackUnorm8(v.y), unpackUnorm8(v.z), unpackUnorm8(v.w) }; } inline float unpackSnorm8(int8_t v) noexcept { @@ -91,7 +91,7 @@ inline float unpackSnorm8(int8_t v) noexcept { } inline float4 unpackSnorm8(byte4 v) noexcept { - return float4{unpackSnorm8(v.x), unpackSnorm8(v.y), unpackSnorm8(v.z), unpackSnorm8(v.w)}; + return float4{ unpackSnorm8(v.x), unpackSnorm8(v.y), unpackSnorm8(v.z), unpackSnorm8(v.w) }; } } // namespace math diff --git a/libs/math/include/math/quat.h b/libs/math/include/math/quat.h index 6f6ed5e07c..06c3cc2c19 100644 --- a/libs/math/include/math/quat.h +++ b/libs/math/include/math/quat.h @@ -32,16 +32,18 @@ namespace math { namespace details { -template +template class MATH_EMPTY_BASES TQuaternion : - public TVecAddOperators, - public TVecUnaryOperators, - public TVecComparisonOperators, - public TQuatProductOperators, - public TQuatFunctions, - public TQuatDebug { + public TVecAddOperators, + public TVecUnaryOperators, + public TVecComparisonOperators, + public TQuatProductOperators, + public TQuatFunctions, + public TQuatDebug { public: - enum no_init { NO_INIT }; + enum no_init { + NO_INIT + }; typedef T value_type; typedef T& reference; typedef T const& const_reference; @@ -84,7 +86,7 @@ public: // we want the compiler generated versions for these... TQuaternion(const TQuaternion&) = default; ~TQuaternion() = default; - TQuaternion& operator = (const TQuaternion&) = default; + TQuaternion& operator=(const TQuaternion&) = default; // constructors @@ -92,7 +94,7 @@ public: explicit constexpr TQuaternion(no_init) {} // default constructor. sets all values to zero. - constexpr TQuaternion() : x(0), y(0), z(0), w(0) { } + constexpr TQuaternion() : x(0), y(0), z(0), w(0) {} // handles implicit conversion to a tvec4. must not be explicit. template @@ -102,19 +104,19 @@ public: // initialize from 4 values to w + xi + yj + zk template - constexpr TQuaternion(A w, B x, C y, D z) : x(x), y(y), z(z), w(w) { } + constexpr TQuaternion(A w, B x, C y, D z) : x(x), y(y), z(z), w(w) {} // initialize from a vec3 + a value to : v.xi + v.yj + v.zk + w template - constexpr TQuaternion(const TVec3& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { } + constexpr TQuaternion(const TVec3& v, B w) : x(v.x), y(v.y), z(v.z), w(w) {} // initialize from a double4 template - constexpr explicit TQuaternion(const TVec4& v) : x(v.x), y(v.y), z(v.z), w(v.w) { } + constexpr explicit TQuaternion(const TVec4& v) : x(v.x), y(v.y), z(v.z), w(v.w) {} // initialize from a quaternion of a different type template - constexpr explicit TQuaternion(const TQuaternion& v) : x(v.x), y(v.y), z(v.z), w(v.w) { } + constexpr explicit TQuaternion(const TQuaternion& v) : x(v.x), y(v.y), z(v.z), w(v.w) {} // conjugate operator constexpr TQuaternion operator~() const { @@ -122,9 +124,9 @@ public: } // constructs a quaternion from an axis and angle - template + template constexpr static TQuaternion MATH_PURE fromAxisAngle(const TVec3& axis, B angle) { - return TQuaternion(std::sin(angle*0.5) * normalize(axis), std::cos(angle*0.5)); + return TQuaternion(std::sin(angle * 0.5) * normalize(axis), std::cos(angle * 0.5)); } }; @@ -136,23 +138,27 @@ typedef details::TQuaternion quat; typedef details::TQuaternion quatf; typedef details::TQuaternion quath; -constexpr inline quat operator"" _i(long double v) { +constexpr inline quat operator "" _i(long double v) { return quat(0.0, double(v), 0.0, 0.0); } -constexpr inline quat operator"" _j(long double v) { + +constexpr inline quat operator "" _j(long double v) { return quat(0.0, 0.0, double(v), 0.0); } -constexpr inline quat operator"" _k(long double v) { + +constexpr inline quat operator "" _k(long double v) { return quat(0.0, 0.0, 0.0, double(v)); } -constexpr inline quat operator"" _i(unsigned long long v) { +constexpr inline quat operator "" _i(unsigned long long v) { return quat(0.0, double(v), 0.0, 0.0); } -constexpr inline quat operator"" _j(unsigned long long v) { + +constexpr inline quat operator "" _j(unsigned long long v) { return quat(0.0, 0.0, double(v), 0.0); } -constexpr inline quat operator"" _k(unsigned long long v) { + +constexpr inline quat operator "" _k(unsigned long long v) { return quat(0.0, 0.0, 0.0, double(v)); } diff --git a/libs/math/include/math/scalar.h b/libs/math/include/math/scalar.h index d09937e0c2..656c240a43 100644 --- a/libs/math/include/math/scalar.h +++ b/libs/math/include/math/scalar.h @@ -44,7 +44,7 @@ inline constexpr T MATH_PURE lerp(T x, T y, T a) noexcept { return mix(x, y, a); } -template +template inline constexpr T sign(T x) noexcept { return x < T(0) ? T(-1) : T(1); } diff --git a/libs/math/include/math/vec2.h b/libs/math/include/math/vec2.h index de5f4731d7..5efea76c28 100644 --- a/libs/math/include/math/vec2.h +++ b/libs/math/include/math/vec2.h @@ -31,14 +31,14 @@ namespace math { namespace details { -template +template class MATH_EMPTY_BASES TVec2 : - public TVecProductOperators, - public TVecAddOperators, - public TVecUnaryOperators, - public TVecComparisonOperators, - public TVecFunctions, - public TVecDebug { + public TVecProductOperators, + public TVecAddOperators, + public TVecUnaryOperators, + public TVecComparisonOperators, + public TVecFunctions, + public TVecDebug { public: typedef T value_type; typedef T& reference; @@ -94,7 +94,7 @@ public: // ---------------------------------------------------------------------------------------- -template ::value >::type> +template::value>::type> using vec2 = details::TVec2; using double2 = vec2; diff --git a/libs/math/include/math/vec3.h b/libs/math/include/math/vec3.h index 7ff58b43f7..179abde7f7 100644 --- a/libs/math/include/math/vec3.h +++ b/libs/math/include/math/vec3.h @@ -29,14 +29,14 @@ namespace math { namespace details { -template +template class MATH_EMPTY_BASES TVec3 : - public TVecProductOperators, - public TVecAddOperators, - public TVecUnaryOperators, - public TVecComparisonOperators, - public TVecFunctions, - public TVecDebug { + public TVecProductOperators, + public TVecAddOperators, + public TVecUnaryOperators, + public TVecComparisonOperators, + public TVecFunctions, + public TVecDebug { public: typedef T value_type; typedef T& reference; @@ -99,7 +99,7 @@ public: constexpr TVec3(const TVec3& v) : v{ T(v[0]), T(v[1]), T(v[2]) } {} // cross product works only on vectors of size 3 - template + template friend inline constexpr TVec3 cross(const TVec3& u, const TVec3& v) { return TVec3( u[1] * v[2] - u[2] * v[1], @@ -112,7 +112,7 @@ public: // ---------------------------------------------------------------------------------------- -template ::value >::type> +template::value>::type> using vec3 = details::TVec3; using double3 = vec3; diff --git a/libs/math/include/math/vec4.h b/libs/math/include/math/vec4.h index 2f66da220c..bd9bfddb4a 100644 --- a/libs/math/include/math/vec4.h +++ b/libs/math/include/math/vec4.h @@ -29,13 +29,14 @@ namespace math { namespace details { -template -class MATH_EMPTY_BASES TVec4 : public TVecProductOperators, - public TVecAddOperators, - public TVecUnaryOperators, - public TVecComparisonOperators, - public TVecFunctions, - public TVecDebug { +template +class MATH_EMPTY_BASES TVec4 : + public TVecProductOperators, + public TVecAddOperators, + public TVecUnaryOperators, + public TVecComparisonOperators, + public TVecFunctions, + public TVecDebug { public: typedef T value_type; typedef T& reference; @@ -92,13 +93,14 @@ public: constexpr TVec4(A x, B y, C z, D w) : v{ T(x), T(y), T(z), T(w) } {} template - constexpr TVec4(const TVec2& v, B z, C w) : v{ T(v[0]), T(v[1]), T(z), T(w) } {} + constexpr TVec4(const TVec2 & v, B z, C w) : v{ T(v[0]), T(v[1]), T(z), T(w) } {} template - constexpr TVec4(const TVec2& v, const TVec2& w) : v{ T(v[0]), T(v[1]), T(w[0]), T(w[1]) } {} + constexpr TVec4(const TVec2 & v, const TVec2 & w) : v{ + T(v[0]), T(v[1]), T(w[0]), T(w[1]) } {} template - constexpr TVec4(const TVec3& v, B w) : v{ T(v[0]), T(v[1]), T(v[2]), T(w) } {} + constexpr TVec4(const TVec3 & v, B w) : v{ T(v[0]), T(v[1]), T(v[2]), T(w) } {} template constexpr TVec4(const TVec4& v) : v{ T(v[0]), T(v[1]), T(v[2]), T(v[3]) } {} @@ -108,7 +110,7 @@ public: // ---------------------------------------------------------------------------------------- -template ::value >::type> +template::value>::type> using vec4 = details::TVec4; using double4 = vec4;