Made GLM_FORCE_QUAT_DATA_XYZW be supported by most major functions

This commit is contained in:
Woob 2023-04-01 14:50:37 -07:00 committed by Christophe
parent c7c2c3833f
commit 7770dc37a6
10 changed files with 51 additions and 40 deletions

View File

@ -95,6 +95,8 @@ namespace glm
GLM_FUNC_DECL GLM_CONSTEXPR qua(T w, T x, T y, T z); GLM_FUNC_DECL GLM_CONSTEXPR qua(T w, T x, T y, T z);
# endif # endif
GLM_FUNC_DECL static GLM_CONSTEXPR qua<T, Q> wxyz(T w, T x, T y, T z);
// -- Conversion constructors -- // -- Conversion constructors --
template<typename U, qualifier P> template<typename U, qualifier P>

View File

@ -28,7 +28,7 @@ namespace detail
{ {
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua<T, Q> call(qua<T, Q> const& q, qua<T, Q> const& p) GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua<T, Q> call(qua<T, Q> const& q, qua<T, Q> const& p)
{ {
return qua<T, Q>(q.w + p.w, q.x + p.x, q.y + p.y, q.z + p.z); return qua<T, Q>::wxyz(q.w + p.w, q.x + p.x, q.y + p.y, q.z + p.z);
} }
}; };
@ -37,7 +37,7 @@ namespace detail
{ {
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua<T, Q> call(qua<T, Q> const& q, qua<T, Q> const& p) GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua<T, Q> call(qua<T, Q> const& q, qua<T, Q> const& p)
{ {
return qua<T, Q>(q.w - p.w, q.x - p.x, q.y - p.y, q.z - p.z); return qua<T, Q>::wxyz(q.w - p.w, q.x - p.x, q.y - p.y, q.z - p.z);
} }
}; };
@ -46,7 +46,7 @@ namespace detail
{ {
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua<T, Q> call(qua<T, Q> const& q, T s) GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua<T, Q> call(qua<T, Q> const& q, T s)
{ {
return qua<T, Q>(q.w * s, q.x * s, q.y * s, q.z * s); return qua<T, Q>::wxyz(q.w * s, q.x * s, q.y * s, q.z * s);
} }
}; };
@ -55,7 +55,7 @@ namespace detail
{ {
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua<T, Q> call(qua<T, Q> const& q, T s) GLM_FUNC_QUALIFIER GLM_CONSTEXPR static qua<T, Q> call(qua<T, Q> const& q, T s)
{ {
return qua<T, Q>(q.w / s, q.x / s, q.y / s, q.z / s); return qua<T, Q>::wxyz(q.w / s, q.x / s, q.y / s, q.z / s);
} }
}; };
@ -150,6 +150,15 @@ namespace detail
# endif # endif
{} {}
template <typename T, qualifier Q>
GLM_CONSTEXPR qua<T, Q> qua<T, Q>::wxyz(T w, T x, T y, T z) {
# ifdef GLM_FORCE_QUAT_DATA_XYZW
return qua<T, Q>(x, y, z, w);
# else
return qua<T, Q>(w, x, y, z);
# endif
}
// -- Conversion constructors -- // -- Conversion constructors --
template<typename T, qualifier Q> template<typename T, qualifier Q>
@ -201,7 +210,7 @@ namespace detail
t = cross(u, v); t = cross(u, v);
} }
*this = normalize(qua<T, Q>(real_part, t.x, t.y, t.z)); *this = normalize(qua<T, Q>::wxyz(real_part, t.x, t.y, t.z));
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
@ -320,7 +329,7 @@ namespace detail
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> operator-(qua<T, Q> const& q) GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> operator-(qua<T, Q> const& q)
{ {
return qua<T, Q>(-q.w, -q.x, -q.y, -q.z); return qua<T, Q>::wxyz(-q.w, -q.x, -q.y, -q.z);
} }
// -- Binary operators -- // -- Binary operators --
@ -374,7 +383,7 @@ namespace detail
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> operator*(qua<T, Q> const& q, T const& s) GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> operator*(qua<T, Q> const& q, T const& s)
{ {
return qua<T, Q>( return qua<T, Q>::wxyz(
q.w * s, q.x * s, q.y * s, q.z * s); q.w * s, q.x * s, q.y * s, q.z * s);
} }
@ -387,7 +396,7 @@ namespace detail
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> operator/(qua<T, Q> const& q, T const& s) GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> operator/(qua<T, Q> const& q, T const& s)
{ {
return qua<T, Q>( return qua<T, Q>::wxyz(
q.w / s, q.x / s, q.y / s, q.z / s); q.w / s, q.x / s, q.y / s, q.z / s);
} }

View File

@ -11,7 +11,7 @@ namespace glm
if(cosTheta > static_cast<T>(1) - epsilon<T>()) if(cosTheta > static_cast<T>(1) - epsilon<T>())
{ {
// Linear interpolation // Linear interpolation
return qua<T, Q>( return qua<T, Q>::wxyz(
mix(x.w, y.w, a), mix(x.w, y.w, a),
mix(x.x, y.x, a), mix(x.x, y.x, a),
mix(x.y, y.y, a), mix(x.y, y.y, a),
@ -58,7 +58,7 @@ namespace glm
if(cosTheta > static_cast<T>(1) - epsilon<T>()) if(cosTheta > static_cast<T>(1) - epsilon<T>())
{ {
// Linear interpolation // Linear interpolation
return qua<T, Q>( return qua<T, Q>::wxyz(
mix(x.w, z.w, a), mix(x.w, z.w, a),
mix(x.x, z.x, a), mix(x.x, z.x, a),
mix(x.y, z.y, a), mix(x.y, z.y, a),
@ -94,7 +94,7 @@ namespace glm
if (cosTheta > static_cast<T>(1) - epsilon<T>()) if (cosTheta > static_cast<T>(1) - epsilon<T>())
{ {
// Linear interpolation // Linear interpolation
return qua<T, Q>( return qua<T, Q>::wxyz(
mix(x.w, z.w, a), mix(x.w, z.w, a),
mix(x.x, z.x, a), mix(x.x, z.x, a),
mix(x.y, z.y, a), mix(x.y, z.y, a),
@ -112,7 +112,7 @@ namespace glm
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER qua<T, Q> conjugate(qua<T, Q> const& q) GLM_FUNC_QUALIFIER qua<T, Q> conjugate(qua<T, Q> const& q)
{ {
return qua<T, Q>(q.w, -q.x, -q.y, -q.z); return qua<T, Q>::wxyz(q.w, -q.x, -q.y, -q.z);
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>

View File

@ -23,17 +23,17 @@ namespace glm
if (Vec3Len < epsilon<T>()) if (Vec3Len < epsilon<T>())
{ {
if(q.w > static_cast<T>(0)) if(q.w > static_cast<T>(0))
return qua<T, Q>(log(q.w), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)); return qua<T, Q>::wxyz(log(q.w), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0));
else if(q.w < static_cast<T>(0)) else if(q.w < static_cast<T>(0))
return qua<T, Q>(log(-q.w), pi<T>(), static_cast<T>(0), static_cast<T>(0)); return qua<T, Q>::wxyz(log(-q.w), pi<T>(), static_cast<T>(0), static_cast<T>(0));
else else
return qua<T, Q>(std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity()); return qua<T, Q>::wxyz(std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity());
} }
else else
{ {
T t = atan(Vec3Len, T(q.w)) / Vec3Len; T t = atan(Vec3Len, T(q.w)) / Vec3Len;
T QuatLen2 = Vec3Len * Vec3Len + q.w * q.w; T QuatLen2 = Vec3Len * Vec3Len + q.w * q.w;
return qua<T, Q>(static_cast<T>(0.5) * log(QuatLen2), t * q.x, t * q.y, t * q.z); return qua<T, Q>::wxyz(static_cast<T>(0.5) * log(QuatLen2), t * q.x, t * q.y, t * q.z);
} }
} }
@ -43,7 +43,7 @@ namespace glm
//Raising to the power of 0 should yield 1 //Raising to the power of 0 should yield 1
//Needed to prevent a division by 0 error later on //Needed to prevent a division by 0 error later on
if(y > -epsilon<T>() && y < epsilon<T>()) if(y > -epsilon<T>() && y < epsilon<T>())
return qua<T, Q>(1,0,0,0); return qua<T, Q>::wxyz(1,0,0,0);
//To deal with non-unit quaternions //To deal with non-unit quaternions
T magnitude = sqrt(x.x * x.x + x.y * x.y + x.z * x.z + x.w *x.w); T magnitude = sqrt(x.x * x.x + x.y * x.y + x.z * x.z + x.w *x.w);
@ -62,7 +62,7 @@ namespace glm
//always false, even when VectorMagnitude is 0. //always false, even when VectorMagnitude is 0.
if (VectorMagnitude < std::numeric_limits<T>::min()) { if (VectorMagnitude < std::numeric_limits<T>::min()) {
//Equivalent to raising a real number to a power //Equivalent to raising a real number to a power
return qua<T, Q>(pow(x.w, y), 0, 0, 0); return qua<T, Q>::wxyz(pow(x.w, y), 0, 0, 0);
} }
Angle = asin(sqrt(VectorMagnitude) / magnitude); Angle = asin(sqrt(VectorMagnitude) / magnitude);
@ -76,7 +76,7 @@ namespace glm
T NewAngle = Angle * y; T NewAngle = Angle * y;
T Div = sin(NewAngle) / sin(Angle); T Div = sin(NewAngle) / sin(Angle);
T Mag = pow(magnitude, y - static_cast<T>(1)); T Mag = pow(magnitude, y - static_cast<T>(1));
return qua<T, Q>(cos(NewAngle) * magnitude * Mag, x.x * Div * Mag, x.y * Div * Mag, x.z * Div * Mag); return qua<T, Q>::wxyz(cos(NewAngle) * magnitude * Mag, x.x * Div * Mag, x.y * Div * Mag, x.z * Div * Mag);
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>

View File

@ -18,15 +18,15 @@ namespace glm
{ {
T len = length(q); T len = length(q);
if(len <= static_cast<T>(0)) // Problem if(len <= static_cast<T>(0)) // Problem
return qua<T, Q>(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)); return qua<T, Q>::wxyz(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0));
T oneOverLen = static_cast<T>(1) / len; T oneOverLen = static_cast<T>(1) / len;
return qua<T, Q>(q.w * oneOverLen, q.x * oneOverLen, q.y * oneOverLen, q.z * oneOverLen); return qua<T, Q>::wxyz(q.w * oneOverLen, q.x * oneOverLen, q.y * oneOverLen, q.z * oneOverLen);
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER qua<T, Q> cross(qua<T, Q> const& q1, qua<T, Q> const& q2) GLM_FUNC_QUALIFIER qua<T, Q> cross(qua<T, Q> const& q1, qua<T, Q> const& q2)
{ {
return qua<T, Q>( return qua<T, Q>::wxyz(
q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z, q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z,
q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y, q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z, q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z,

View File

@ -18,7 +18,7 @@ namespace glm
T const AngleRad(angle); T const AngleRad(angle);
T const Sin = sin(AngleRad * static_cast<T>(0.5)); T const Sin = sin(AngleRad * static_cast<T>(0.5));
return q * qua<T, Q>(cos(AngleRad * static_cast<T>(0.5)), Tmp.x * Sin, Tmp.y * Sin, Tmp.z * Sin); return q * qua<T, Q>::wxyz(cos(AngleRad * static_cast<T>(0.5)), Tmp.x * Sin, Tmp.y * Sin, Tmp.z * Sin);
} }
}//namespace glm }//namespace glm

View File

@ -109,16 +109,16 @@ namespace glm
switch(biggestIndex) switch(biggestIndex)
{ {
case 0: case 0:
return qua<T, Q>(biggestVal, (m[1][2] - m[2][1]) * mult, (m[2][0] - m[0][2]) * mult, (m[0][1] - m[1][0]) * mult); return qua<T, Q>::wxyz(biggestVal, (m[1][2] - m[2][1]) * mult, (m[2][0] - m[0][2]) * mult, (m[0][1] - m[1][0]) * mult);
case 1: case 1:
return qua<T, Q>((m[1][2] - m[2][1]) * mult, biggestVal, (m[0][1] + m[1][0]) * mult, (m[2][0] + m[0][2]) * mult); return qua<T, Q>::wxyz((m[1][2] - m[2][1]) * mult, biggestVal, (m[0][1] + m[1][0]) * mult, (m[2][0] + m[0][2]) * mult);
case 2: case 2:
return qua<T, Q>((m[2][0] - m[0][2]) * mult, (m[0][1] + m[1][0]) * mult, biggestVal, (m[1][2] + m[2][1]) * mult); return qua<T, Q>::wxyz((m[2][0] - m[0][2]) * mult, (m[0][1] + m[1][0]) * mult, biggestVal, (m[1][2] + m[2][1]) * mult);
case 3: case 3:
return qua<T, Q>((m[0][1] - m[1][0]) * mult, (m[2][0] + m[0][2]) * mult, (m[1][2] + m[2][1]) * mult, biggestVal); return qua<T, Q>::wxyz((m[0][1] - m[1][0]) * mult, (m[2][0] + m[0][2]) * mult, (m[1][2] + m[2][1]) * mult, biggestVal);
default: // Silence a -Wswitch-default warning in GCC. Should never actually get here. Assert is just for sanity. default: // Silence a -Wswitch-default warning in GCC. Should never actually get here. Assert is just for sanity.
assert(false); assert(false);
return qua<T, Q>(1, 0, 0, 0); return qua<T, Q>::wxyz(1, 0, 0, 0);
} }
} }

View File

@ -28,7 +28,7 @@ namespace glm
GLM_DEFAULTED_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat() GLM_DEFAULTED_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat()
# if GLM_CONFIG_DEFAULTED_FUNCTIONS != GLM_DISABLE # if GLM_CONFIG_DEFAULTED_FUNCTIONS != GLM_DISABLE
: real(qua<T, Q>()) : real(qua<T, Q>())
, dual(qua<T, Q>(0, 0, 0, 0)) , dual(qua<T, Q>::wxyz(0, 0, 0, 0))
# endif # endif
{} {}
@ -50,16 +50,16 @@ namespace glm
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(qua<T, Q> const& r) GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(qua<T, Q> const& r)
: real(r), dual(qua<T, Q>(0, 0, 0, 0)) : real(r), dual(qua<T, Q>::wxyz(0, 0, 0, 0))
{} {}
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(qua<T, Q> const& q, vec<3, T, Q> const& p) GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(qua<T, Q> const& q, vec<3, T, Q> const& p)
: real(q), dual( : real(q), dual(qua<T, Q>::wxyz(
T(-0.5) * ( p.x*q.x + p.y*q.y + p.z*q.z), T(-0.5) * ( p.x*q.x + p.y*q.y + p.z*q.z),
T(+0.5) * ( p.x*q.w + p.y*q.z - p.z*q.y), T(+0.5) * ( p.x*q.w + p.y*q.z - p.z*q.y),
T(+0.5) * (-p.x*q.z + p.y*q.w + p.z*q.x), T(+0.5) * (-p.x*q.z + p.y*q.w + p.z*q.x),
T(+0.5) * ( p.x*q.y - p.y*q.x + p.z*q.w)) T(+0.5) * ( p.x*q.y - p.y*q.x + p.z*q.w)))
{} {}
template<typename T, qualifier Q> template<typename T, qualifier Q>
@ -219,8 +219,8 @@ namespace glm
GLM_FUNC_QUALIFIER tdualquat<T, Q> dual_quat_identity() GLM_FUNC_QUALIFIER tdualquat<T, Q> dual_quat_identity()
{ {
return tdualquat<T, Q>( return tdualquat<T, Q>(
qua<T, Q>(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)), qua<T, Q>::wxyz(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)),
qua<T, Q>(static_cast<T>(0), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0))); qua<T, Q>::wxyz(static_cast<T>(0), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)));
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
@ -295,8 +295,8 @@ namespace glm
GLM_FUNC_QUALIFIER tdualquat<T, Q> dualquat_cast(mat<2, 4, T, Q> const& x) GLM_FUNC_QUALIFIER tdualquat<T, Q> dualquat_cast(mat<2, 4, T, Q> const& x)
{ {
return tdualquat<T, Q>( return tdualquat<T, Q>(
qua<T, Q>( x[0].w, x[0].x, x[0].y, x[0].z ), qua<T, Q>::wxyz( x[0].w, x[0].x, x[0].y, x[0].z ),
qua<T, Q>( x[1].w, x[1].x, x[1].y, x[1].z )); qua<T, Q>::wxyz( x[1].w, x[1].x, x[1].y, x[1].z ));
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>

View File

@ -8,7 +8,7 @@ namespace glm
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> quat_identity() GLM_FUNC_QUALIFIER GLM_CONSTEXPR qua<T, Q> quat_identity()
{ {
return qua<T, Q>(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)); return qua<T, Q>::wxyz(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0));
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
@ -105,7 +105,7 @@ namespace glm
k1 = sin((static_cast<T>(0) + a) * fAngle) * fOneOverSin; k1 = sin((static_cast<T>(0) + a) * fAngle) * fOneOverSin;
} }
return qua<T, Q>( return qua<T, Q>::wxyz(
k0 * x.w + k1 * y2.w, k0 * x.w + k1 * y2.w,
k0 * x.x + k1 * y2.x, k0 * x.x + k1 * y2.x,
k0 * x.y + k1 * y2.y, k0 * x.y + k1 * y2.y,
@ -150,7 +150,7 @@ namespace glm
T s = sqrt((T(1) + cosTheta) * static_cast<T>(2)); T s = sqrt((T(1) + cosTheta) * static_cast<T>(2));
T invs = static_cast<T>(1) / s; T invs = static_cast<T>(1) / s;
return qua<T, Q>( return qua<T, Q>::wxyz(
s * static_cast<T>(0.5f), s * static_cast<T>(0.5f),
rotationAxis.x * invs, rotationAxis.x * invs,
rotationAxis.y * invs, rotationAxis.y * invs,

View File

@ -52,7 +52,7 @@ namespace glm
T const AngleRad(angle); T const AngleRad(angle);
T const Sin = sin(AngleRad * T(0.5)); T const Sin = sin(AngleRad * T(0.5));
return q * qua<T, Q>(cos(AngleRad * static_cast<T>(0.5)), Tmp.x * Sin, Tmp.y * Sin, Tmp.z * Sin); return q * qua<T, Q>::wxyz(cos(AngleRad * static_cast<T>(0.5)), Tmp.x * Sin, Tmp.y * Sin, Tmp.z * Sin);
//return gtc::quaternion::cross(q, tquat<T, Q>(cos(AngleRad * T(0.5)), Tmp.x * fSin, Tmp.y * fSin, Tmp.z * fSin)); //return gtc::quaternion::cross(q, tquat<T, Q>(cos(AngleRad * T(0.5)), Tmp.x * fSin, Tmp.y * fSin, Tmp.z * fSin));
} }
}//namespace glm }//namespace glm