reformatting

This commit is contained in:
Mathias Agopian
2019-08-06 17:54:56 -07:00
committed by Mathias Agopian
parent f728776714
commit 67f75f0ecf
11 changed files with 183 additions and 156 deletions

View File

@@ -110,7 +110,7 @@ constexpr MATRIX MATH_PURE gaussJordanInverse(const MATRIX& src) {
//------------------------------------------------------------------------------
// 2x2 matrix inverse is easy.
template <typename MATRIX>
template<typename MATRIX>
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 <typename MATRIX>
template<typename MATRIX>
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<T, ORDER - 1> 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 <typename MATRIX>
template<typename MATRIX>
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 <typename MATRIX>
template<typename MATRIX>
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 <typename MATRIX>
template<typename MATRIX>
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>(matrix) :
@@ -376,7 +377,7 @@ inline constexpr MATRIX MATH_PURE cof(const MATRIX& matrix) {
/**
* Determinant of a matrix
*/
template <typename MATRIX>
template<typename MATRIX>
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 <typename MATRIX>
template<typename MATRIX>
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) :
((MATRIX::NUM_ROWS == 3) ? fastInverse3<MATRIX>(matrix) :
gaussJordanInverse<MATRIX>(matrix));
((MATRIX::NUM_ROWS == 3) ? fastInverse3<MATRIX>(matrix) :
gaussJordanInverse<MATRIX>(matrix));
}
template<typename MATRIX_R, typename MATRIX_A, typename MATRIX_B>
@@ -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 <typename MATRIX>
template<typename MATRIX>
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 <typename MATRIX>
template<typename MATRIX>
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 <typename MATRIX>
template<typename MATRIX>
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 <typename MATRIX>
template<typename MATRIX>
TQuaternion<typename MATRIX::value_type> extractQuat(const MATRIX& mat) {
typedef typename MATRIX::value_type T;
@@ -518,11 +519,11 @@ TQuaternion<typename MATRIX::value_type> extractQuat(const MATRIX& mat) {
* get all the functionality here.
*/
template <template<typename T> class BASE, typename T>
template<template<typename T> class BASE, typename T>
class TMatProductOperators {
public:
// multiply by a scalar
constexpr BASE<T>& operator *= (T v) {
constexpr BASE<T>& operator*=(T v) {
BASE<T>& lhs(static_cast< BASE<T>& >(*this));
for (size_t col = 0; col < BASE<T>::NUM_COLS; ++col) {
lhs[col] *= v;
@@ -532,14 +533,14 @@ public:
// matrix *= matrix
template<typename U>
constexpr BASE<T>& operator *= (const BASE<U>& rhs) {
constexpr BASE<T>& operator*=(const BASE<U>& rhs) {
BASE<T>& lhs(static_cast< BASE<T>& >(*this));
lhs = matrix::multiply<BASE<T> >(lhs, rhs);
return lhs;
}
// divide by a scalar
constexpr BASE<T>& operator /= (T v) {
constexpr BASE<T>& operator/=(T v) {
BASE<T>& lhs(static_cast< BASE<T>& >(*this));
for (size_t col = 0; col < BASE<T>::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<typename U>
friend inline constexpr BASE<T> MATH_PURE operator *(const BASE<T>& lhs, const BASE<U>& rhs) {
friend inline constexpr BASE<T> MATH_PURE operator*(const BASE<T>& lhs, const BASE<U>& rhs) {
return matrix::multiply<BASE<T> >(lhs, rhs);
}
@@ -592,15 +593,19 @@ public:
friend inline constexpr BASE<T> MATH_PURE inverse(const BASE<T>& matrix) {
return matrix::inverse(matrix);
}
friend inline constexpr BASE<T> MATH_PURE cof(const BASE<T>& matrix) {
return matrix::cof(matrix);
}
friend inline constexpr BASE<T> MATH_PURE transpose(const BASE<T>& m) {
return matrix::transpose(m);
}
friend inline constexpr T MATH_PURE trace(const BASE<T>& m) {
return matrix::trace(m);
}
friend inline constexpr T MATH_PURE det(const BASE<T>& 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<std::is_arithmetic<Y>::value >::type,
typename = typename std::enable_if<std::is_arithmetic<P>::value >::type,
typename = typename std::enable_if<std::is_arithmetic<R>::value >::type
template<
typename Y, typename P, typename R,
typename = typename std::enable_if<std::is_arithmetic<Y>::value>::type,
typename = typename std::enable_if<std::is_arithmetic<P>::value>::type,
typename = typename std::enable_if<std::is_arithmetic<R>::value>::type
>
static BASE<T> 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<std::is_arithmetic<Y>::value >::type,
typename = typename std::enable_if<std::is_arithmetic<P>::value >::type,
typename = typename std::enable_if<std::is_arithmetic<R>::value >::type
template<
typename Y, typename P, typename R,
typename = typename std::enable_if<std::is_arithmetic<Y>::value>::type,
typename = typename std::enable_if<std::is_arithmetic<P>::value>::type,
typename = typename std::enable_if<std::is_arithmetic<R>::value>::type
>
static BASE<T> eulerZYX(Y yaw, P pitch, R roll) {
BASE<T> r;
@@ -748,13 +753,13 @@ public:
return r;
}
TQuaternion<T> toQuaternion() const {
TQuaternion <T> toQuaternion() const {
return matrix::extractQuat(static_cast<const BASE<T>&>(*this));
}
};
template <template<typename T> class BASE, typename T>
template<template<typename T> class BASE, typename T>
class TMatDebug {
public:
friend std::ostream& operator<<(std::ostream& stream, const BASE<T>& m) {
@@ -764,7 +769,7 @@ public:
}
if (row == 0) {
stream << "/ ";
} else if (row == BASE<T>::NUM_ROWS-1) {
} else if (row == BASE<T>::NUM_ROWS - 1) {
stream << "\\ ";
} else {
stream << "| ";
@@ -774,7 +779,7 @@ public:
}
if (row == 0) {
stream << " \\";
} else if (row == BASE<T>::NUM_ROWS-1) {
} else if (row == BASE<T>::NUM_ROWS - 1) {
stream << " /";
} else {
stream << " |";

View File

@@ -48,14 +48,14 @@ namespace details {
* get all the functionality here.
*/
template <template<typename T> class QUATERNION, typename T>
template<template<typename T> class QUATERNION, typename T>
class TQuatProductOperators {
public:
/* compound assignment from a another quaternion of the same size but different
* element type.
*/
template <typename OTHER>
constexpr QUATERNION<T>& operator *= (const QUATERNION<OTHER>& r) {
template<typename OTHER>
constexpr QUATERNION<T>& operator*=(const QUATERNION<OTHER>& r) {
QUATERNION<T>& q = static_cast<QUATERNION<T>&>(*this);
q = q * r;
return q;
@@ -63,14 +63,15 @@ public:
/* compound assignment products by a scalar
*/
constexpr QUATERNION<T>& operator *= (T v) {
constexpr QUATERNION<T>& operator*=(T v) {
QUATERNION<T>& lhs = static_cast<QUATERNION<T>&>(*this);
for (size_t i = 0; i < QUATERNION<T>::size(); i++) {
lhs[i] *= v;
}
return lhs;
}
constexpr QUATERNION<T>& operator /= (T v) {
constexpr QUATERNION<T>& operator/=(T v) {
QUATERNION<T>& lhs = static_cast<QUATERNION<T>&>(*this);
for (size_t i = 0; i < QUATERNION<T>::size(); i++) {
lhs[i] /= v;
@@ -91,22 +92,22 @@ public:
*/
template<typename RT>
friend inline
constexpr QUATERNION<T> MATH_PURE operator *(const QUATERNION<T>& q, const QUATERNION<RT>& r) {
constexpr QUATERNION<T> MATH_PURE operator*(const QUATERNION<T>& q, const QUATERNION<RT>& r) {
// could be written as:
// return QUATERNION<T>(
// q.w*r.w - dot(q.xyz, r.xyz),
// q.w*r.xyz + r.w*q.xyz + cross(q.xyz, r.xyz));
return QUATERNION<T>(
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<typename RT>
friend inline
constexpr TVec3<T> MATH_PURE operator *(const QUATERNION<T>& q, const TVec3<RT>& v) {
constexpr TVec3<T> MATH_PURE operator*(const QUATERNION<T>& q, const TVec3<RT>& v) {
// note: if q is known to be a unit quaternion, then this simplifies to:
// TVec3<T> t = 2 * cross(q.xyz, v)
// return v + (q.w * t) + cross(q.xyz, t)
@@ -127,18 +128,19 @@ public:
*
*/
friend inline
constexpr QUATERNION<T> MATH_PURE operator *(QUATERNION<T> q, T scalar) {
// don't pass q by reference because we need a copy anyways
return q *= scalar;
}
friend inline
constexpr QUATERNION<T> MATH_PURE operator *(T scalar, QUATERNION<T> q) {
constexpr QUATERNION<T> MATH_PURE operator*(QUATERNION<T> q, T scalar) {
// don't pass q by reference because we need a copy anyways
return q *= scalar;
}
friend inline
constexpr QUATERNION<T> MATH_PURE operator /(QUATERNION<T> q, T scalar) {
constexpr QUATERNION<T> MATH_PURE operator*(T scalar, QUATERNION<T> q) {
// don't pass q by reference because we need a copy anyways
return q *= scalar;
}
friend inline
constexpr QUATERNION<T> MATH_PURE operator/(QUATERNION<T> 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, T> BASE will automatically
* get all the functionality here.
*/
template <template<typename T> class QUATERNION, typename T>
template<template<typename T> class QUATERNION, typename T>
class TQuatFunctions {
public:
/*
@@ -174,7 +176,7 @@ public:
friend inline
T MATH_PURE norm(const QUATERNION<T>& q) {
return std::sqrt( dot(q, q) );
return std::sqrt(dot(q, q));
}
friend inline
@@ -219,26 +221,26 @@ public:
friend inline
constexpr QUATERNION<T> MATH_PURE cross(const QUATERNION<T>& p, const QUATERNION<T>& q) {
return unreal(p*q);
return unreal(p * q);
}
friend inline
QUATERNION<T> MATH_PURE exp(const QUATERNION<T>& q) {
const T nq(norm(q.xyz));
return std::exp(q.w)*QUATERNION<T>((sin(nq)/nq)*q.xyz, cos(nq));
return std::exp(q.w) * QUATERNION<T>((sin(nq) / nq) * q.xyz, cos(nq));
}
friend inline
QUATERNION<T> MATH_PURE log(const QUATERNION<T>& q) {
const T nq(norm(q));
return QUATERNION<T>((std::acos(q.w/nq)/norm(q.xyz))*q.xyz, log(nq));
return QUATERNION<T>((std::acos(q.w / nq) / norm(q.xyz)) * q.xyz, log(nq));
}
friend inline
QUATERNION<T> MATH_PURE pow(const QUATERNION<T>& 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<T>(normalize(q.xyz) * std::sin(theta), std::cos(theta));
}
@@ -289,7 +291,7 @@ public:
* By simply inheriting from TQuatDebug<BASE, T> BASE will automatically
* get all the functionality here.
*/
template <template<typename T> class QUATERNION, typename T>
template<template<typename T> 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<T> being known).
*/
friend std::ostream& operator<< (std::ostream& stream, const QUATERNION<T>& q) {
friend std::ostream& operator<<(std::ostream& stream, const QUATERNION<T>& q) {
return stream << "< " << q.w << " + " << q.x << "i + " << q.y << "j + " << q.z << "k >";
}
};

View File

@@ -38,6 +38,7 @@ template<typename U>
inline constexpr U min(U a, U b) noexcept {
return a < b ? a : b;
}
template<typename U>
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 <template<typename T> class VECTOR, typename T>
template<template<typename T> 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<typename RT>
friend inline constexpr VECTOR<T> MATH_PURE operator +(VECTOR<T> lv, const VECTOR<RT>& rv) {
friend inline constexpr VECTOR<T> MATH_PURE operator+(VECTOR<T> lv, const VECTOR<RT>& rv) {
// don't pass lv by reference because we need a copy anyways
return lv += rv;
}
template<typename RT>
friend inline constexpr VECTOR<T> MATH_PURE operator -(VECTOR<T> lv, const VECTOR<RT>& rv) {
friend inline constexpr VECTOR<T> MATH_PURE operator-(VECTOR<T> lv, const VECTOR<RT>& 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<T> allows it).
*/
friend inline constexpr VECTOR<T> MATH_PURE operator +(VECTOR<T> lv, const VECTOR<T>& rv) {
friend inline constexpr VECTOR<T> MATH_PURE operator+(VECTOR<T> lv, const VECTOR<T>& rv) {
// don't pass lv by reference because we need a copy anyways
return lv += rv;
}
friend inline constexpr VECTOR<T> MATH_PURE operator -(VECTOR<T> lv, const VECTOR<T>& rv) {
friend inline constexpr VECTOR<T> MATH_PURE operator-(VECTOR<T> lv, const VECTOR<T>& rv) {
// don't pass lv by reference because we need a copy anyways
return lv -= rv;
}
@@ -238,7 +241,7 @@ public:
template<template<typename T> class VECTOR, typename T>
class TVecUnaryOperators {
public:
constexpr VECTOR<T> operator -() const {
constexpr VECTOR<T> operator-() const {
VECTOR<T> r{};
VECTOR<T> const& rv(static_cast<VECTOR<T> const&>(*this));
for (size_t i = 0; i < r.size(); i++) {
@@ -467,13 +470,14 @@ public:
}
friend inline constexpr VECTOR<T> MATH_PURE clamp(VECTOR<T> v, VECTOR<T> min, VECTOR<T> 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<T> MATH_PURE fma(const VECTOR<T>& lv, const VECTOR<T>& rv, VECTOR<T> a) {
friend inline constexpr VECTOR<T> MATH_PURE fma(const VECTOR<T>& lv, const VECTOR<T>& rv,
VECTOR<T> 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<T>& v) {
T r(std::numeric_limits<T>::max());
for (size_t i=0 ; i<v.size() ; i++) {
for (size_t i = 0; i < v.size(); i++) {
r = min(r, v[i]);
}
return r;

View File

@@ -45,7 +45,7 @@ constexpr T MATH_PURE cos(T x) noexcept {
// fast sin(x), ~8 cycles (vs. 66 cycles on ARM)
// can be vectorized
// x between -pi and pi
template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
constexpr T MATH_PURE sin(T x) noexcept {
return filament::math::fast::cos<T>(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<typename T, typename = typename std::enable_if<
std::is_same<uint8_t, T>::value ||
std::is_same<uint16_t, T>::value ||
std::is_same<uint32_t, T>::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<typename T, typename = typename std::enable_if<
std::is_same<uint8_t, T>::value ||
std::is_same<uint16_t, T>::value ||
std::is_same<uint32_t, T>::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<typename T>
inline T MATH_PURE qinc(T a) noexcept {
inline T MATH_PURE qinc(T a) noexcept {
return qadd(a, T(1));
}
template<typename T>
inline T MATH_PURE qdec(T a) noexcept {
inline T MATH_PURE qdec(T a) noexcept {
return qsub(a, T(1));
}

View File

@@ -86,8 +86,8 @@ public:
typedef T& reference;
typedef T const& const_reference;
typedef size_t size_type;
typedef TVec3 <T> col_type;
typedef TVec3 <T> row_type;
typedef TVec3<T> col_type;
typedef TVec3<T> 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<typename U>
constexpr explicit TMat33(const TVec3 <U>& v);
constexpr explicit TMat33(const TVec3<U>& v);
/**
* construct from another matrix of the same size
@@ -199,7 +199,7 @@ public:
* \f$
*/
template<typename A, typename B, typename C>
constexpr TMat33(const TVec3 <A>& v0, const TVec3 <B>& v1, const TVec3 <C>& v2);
constexpr TMat33(const TVec3<A>& v0, const TVec3<B>& v1, const TVec3<C>& v2);
/** construct from 9 elements in column-major form.
*
@@ -245,7 +245,7 @@ public:
* construct from a quaternion
*/
template<typename U>
constexpr explicit TMat33(const TQuaternion <U>& q);
constexpr explicit TMat33(const TQuaternion<U>& 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 <T> packTangentFrame(const TMat33& m,
size_t storageSize = sizeof(int16_t));
static constexpr TQuaternion<T> packTangentFrame(
const TMat33& m, size_t storageSize = sizeof(int16_t));
template<typename A>
static constexpr TMat33 translation(const TVec3 <A>& t) {
static constexpr TMat33 translation(const TVec3<A>& t) {
TMat33 r;
r[2] = t;
return r;
}
template<typename A>
static constexpr TMat33 scaling(const TVec3 <A>& s) {
static constexpr TMat33 scaling(const TVec3<A>& s) {
return TMat33{ s };
}
template<typename A>
static constexpr TMat33 scaling(A s) {
return TMat33{ TVec3 < T > { s }};
return TMat33{ TVec3<T>{ s }};
}
};
@@ -327,7 +327,7 @@ constexpr TMat33<T>::TMat33(U v)
template<typename T>
template<typename U>
constexpr TMat33<T>::TMat33(const TVec3 <U>& v)
constexpr TMat33<T>::TMat33(const TVec3<U>& v)
: m_value{
col_type(v[0], 0, 0),
col_type(0, v[1], 0),
@@ -362,13 +362,13 @@ constexpr TMat33<T>::TMat33(const TMat33<U>& rhs) {
// Construct from 3 column vectors.
template<typename T>
template<typename A, typename B, typename C>
constexpr TMat33<T>::TMat33(const TVec3 <A>& v0, const TVec3 <B>& v1, const TVec3 <C>& v2)
constexpr TMat33<T>::TMat33(const TVec3<A>& v0, const TVec3<B>& v1, const TVec3<C>& v2)
: m_value{ v0, v1, v2 } {
}
template<typename T>
template<typename U>
constexpr TMat33<T>::TMat33(const TQuaternion <U>& q) : m_value{} {
constexpr TMat33<T>::TMat33(const TQuaternion<U>& 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<T>::TMat33(const TQuaternion <U>& q) : m_value{} {
// matrix * column-vector, result is a vector of the same type than the input vector
template<typename T, typename U>
constexpr typename TMat33<U>::col_type MATH_PURE operator*(const TMat33<T>& lhs,
const TVec3 <U>& rhs) {
const TVec3<U>& rhs) {
// Result is initialized to zero.
typename TMat33<U>::col_type result{};
for (size_t col = 0; col < TMat33<T>::NUM_COLS; ++col) {
@@ -414,7 +414,7 @@ constexpr typename TMat33<U>::col_type MATH_PURE operator*(const TMat33<T>& lhs,
// row-vector * matrix, result is a vector of the same type than the input vector
template<typename T, typename U>
constexpr typename TMat33<U>::row_type MATH_PURE operator*(const TVec3 <U>& lhs,
constexpr typename TMat33<U>::row_type MATH_PURE operator*(const TVec3<U>& lhs,
const TMat33<T>& rhs) {
typename TMat33<U>::row_type result{};
for (size_t col = 0; col < TMat33<T>::NUM_COLS; ++col) {
@@ -439,8 +439,8 @@ operator*(U lhs, const TMat33<T>& rhs) {
//------------------------------------------------------------------------------
template<typename T>
constexpr TQuaternion <T> TMat33<T>::packTangentFrame(const TMat33<T>& m, size_t storageSize) {
TQuaternion <T> q = TMat33<T>{ m[0], cross(m[2], m[0]), m[2] }.toQuaternion();
constexpr TQuaternion<T> TMat33<T>::packTangentFrame(const TMat33<T>& m, size_t storageSize) {
TQuaternion<T> q = TMat33<T>{ m[0], cross(m[2], m[0]), m[2] }.toQuaternion();
q = positive(normalize(q));
// Ensure w is never 0.0

View File

@@ -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

View File

@@ -32,16 +32,18 @@ namespace math {
namespace details {
template <typename T>
template<typename T>
class MATH_EMPTY_BASES TQuaternion :
public TVecAddOperators<TQuaternion, T>,
public TVecUnaryOperators<TQuaternion, T>,
public TVecComparisonOperators<TQuaternion, T>,
public TQuatProductOperators<TQuaternion, T>,
public TQuatFunctions<TQuaternion, T>,
public TQuatDebug<TQuaternion, T> {
public TVecAddOperators<TQuaternion, T>,
public TVecUnaryOperators<TQuaternion, T>,
public TVecComparisonOperators<TQuaternion, T>,
public TQuatProductOperators<TQuaternion, T>,
public TQuatFunctions<TQuaternion, T>,
public TQuatDebug<TQuaternion, T> {
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<typename A>
@@ -102,19 +104,19 @@ public:
// initialize from 4 values to w + xi + yj + zk
template<typename A, typename B, typename C, typename D>
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<typename A, typename B>
constexpr TQuaternion(const TVec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { }
constexpr TQuaternion(const TVec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) {}
// initialize from a double4
template<typename A>
constexpr explicit TQuaternion(const TVec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
constexpr explicit TQuaternion(const TVec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
// initialize from a quaternion of a different type
template<typename A>
constexpr explicit TQuaternion(const TQuaternion<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
constexpr explicit TQuaternion(const TQuaternion<A>& 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 <typename A, typename B>
template<typename A, typename B>
constexpr static TQuaternion MATH_PURE fromAxisAngle(const TVec3<A>& 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<double> quat;
typedef details::TQuaternion<float> quatf;
typedef details::TQuaternion<half> 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));
}

View File

@@ -44,7 +44,7 @@ inline constexpr T MATH_PURE lerp(T x, T y, T a) noexcept {
return mix(x, y, a);
}
template <typename T>
template<typename T>
inline constexpr T sign(T x) noexcept {
return x < T(0) ? T(-1) : T(1);
}

View File

@@ -31,14 +31,14 @@ namespace math {
namespace details {
template <typename T>
template<typename T>
class MATH_EMPTY_BASES TVec2 :
public TVecProductOperators<TVec2, T>,
public TVecAddOperators<TVec2, T>,
public TVecUnaryOperators<TVec2, T>,
public TVecComparisonOperators<TVec2, T>,
public TVecFunctions<TVec2, T>,
public TVecDebug<TVec2, T> {
public TVecProductOperators<TVec2, T>,
public TVecAddOperators<TVec2, T>,
public TVecUnaryOperators<TVec2, T>,
public TVecComparisonOperators<TVec2, T>,
public TVecFunctions<TVec2, T>,
public TVecDebug<TVec2, T> {
public:
typedef T value_type;
typedef T& reference;
@@ -94,7 +94,7 @@ public:
// ----------------------------------------------------------------------------------------
template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value >::type>
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
using vec2 = details::TVec2<T>;
using double2 = vec2<double>;

View File

@@ -29,14 +29,14 @@ namespace math {
namespace details {
template <typename T>
template<typename T>
class MATH_EMPTY_BASES TVec3 :
public TVecProductOperators<TVec3, T>,
public TVecAddOperators<TVec3, T>,
public TVecUnaryOperators<TVec3, T>,
public TVecComparisonOperators<TVec3, T>,
public TVecFunctions<TVec3, T>,
public TVecDebug<TVec3, T> {
public TVecProductOperators<TVec3, T>,
public TVecAddOperators<TVec3, T>,
public TVecUnaryOperators<TVec3, T>,
public TVecComparisonOperators<TVec3, T>,
public TVecFunctions<TVec3, T>,
public TVecDebug<TVec3, T> {
public:
typedef T value_type;
typedef T& reference;
@@ -99,7 +99,7 @@ public:
constexpr TVec3(const TVec3<A>& v) : v{ T(v[0]), T(v[1]), T(v[2]) } {}
// cross product works only on vectors of size 3
template <typename RT>
template<typename RT>
friend inline constexpr TVec3 cross(const TVec3& u, const TVec3<RT>& v) {
return TVec3(
u[1] * v[2] - u[2] * v[1],
@@ -112,7 +112,7 @@ public:
// ----------------------------------------------------------------------------------------
template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value >::type>
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
using vec3 = details::TVec3<T>;
using double3 = vec3<double>;

View File

@@ -29,13 +29,14 @@ namespace math {
namespace details {
template <typename T>
class MATH_EMPTY_BASES TVec4 : public TVecProductOperators<TVec4, T>,
public TVecAddOperators<TVec4, T>,
public TVecUnaryOperators<TVec4, T>,
public TVecComparisonOperators<TVec4, T>,
public TVecFunctions<TVec4, T>,
public TVecDebug<TVec4, T> {
template<typename T>
class MATH_EMPTY_BASES TVec4 :
public TVecProductOperators<TVec4, T>,
public TVecAddOperators<TVec4, T>,
public TVecUnaryOperators<TVec4, T>,
public TVecComparisonOperators<TVec4, T>,
public TVecFunctions<TVec4, T>,
public TVecDebug<TVec4, T> {
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<typename A, typename B, typename C>
constexpr TVec4(const TVec2<A>& v, B z, C w) : v{ T(v[0]), T(v[1]), T(z), T(w) } {}
constexpr TVec4(const TVec2 <A>& v, B z, C w) : v{ T(v[0]), T(v[1]), T(z), T(w) } {}
template<typename A, typename B>
constexpr TVec4(const TVec2<A>& v, const TVec2<B>& w) : v{ T(v[0]), T(v[1]), T(w[0]), T(w[1]) } {}
constexpr TVec4(const TVec2 <A>& v, const TVec2 <B>& w) : v{
T(v[0]), T(v[1]), T(w[0]), T(w[1]) } {}
template<typename A, typename B>
constexpr TVec4(const TVec3<A>& v, B w) : v{ T(v[0]), T(v[1]), T(v[2]), T(w) } {}
constexpr TVec4(const TVec3 <A>& v, B w) : v{ T(v[0]), T(v[1]), T(v[2]), T(w) } {}
template<typename A>
constexpr TVec4(const TVec4<A>& v) : v{ T(v[0]), T(v[1]), T(v[2]), T(v[3]) } {}
@@ -108,7 +110,7 @@ public:
// ----------------------------------------------------------------------------------------
template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value >::type>
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
using vec4 = details::TVec4<T>;
using double4 = vec4<double>;