assimp/include/assimp/quaternion.inl

312 lines
12 KiB
Plaintext
Raw Normal View History

2015-05-19 04:16:51 +00:00
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2022-01-10 20:13:43 +00:00
Copyright (c) 2006-2022, assimp team
2018-01-28 18:42:05 +00:00
2017-05-09 17:57:36 +00:00
2015-05-19 04:16:51 +00:00
All rights reserved.
2015-05-19 04:17:45 +00:00
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
2015-05-19 04:16:51 +00:00
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
2015-05-19 04:17:45 +00:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2015-05-19 04:16:51 +00:00
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2015-05-19 04:17:45 +00:00
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2015-05-19 04:16:51 +00:00
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2015-05-19 04:17:45 +00:00
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2015-05-19 04:16:51 +00:00
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2015-05-19 04:17:45 +00:00
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2015-05-19 04:16:51 +00:00
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file quaternion.inl
* @brief Inline implementation of aiQuaterniont<TReal> operators
*/
#pragma once
2015-05-19 04:16:51 +00:00
#ifndef AI_QUATERNION_INL_INC
#define AI_QUATERNION_INL_INC
2019-10-11 11:27:36 +00:00
#ifdef __GNUC__
# pragma GCC system_header
#endif
2015-05-19 04:16:51 +00:00
#ifdef __cplusplus
2019-10-11 11:27:36 +00:00
#include <assimp/quaternion.h>
2015-05-19 04:16:51 +00:00
#include <cmath>
2020-12-17 07:07:03 +00:00
// ------------------------------------------------------------------------------------------------
/** Transformation of a quaternion by a 4x4 matrix */
template <typename TReal>
AI_FORCE_INLINE
2020-12-17 08:13:35 +00:00
aiQuaterniont<TReal> operator * (const aiMatrix4x4t<TReal>& pMatrix, const aiQuaterniont<TReal>& pQuaternion) {
2020-12-17 09:08:06 +00:00
aiQuaterniont<TReal> res;
2020-12-17 07:07:03 +00:00
res.x = pMatrix.a1 * pQuaternion.x + pMatrix.a2 * pQuaternion.y + pMatrix.a3 * pQuaternion.z + pMatrix.a4 * pQuaternion.w;
res.y = pMatrix.b1 * pQuaternion.x + pMatrix.b2 * pQuaternion.y + pMatrix.b3 * pQuaternion.z + pMatrix.b4 * pQuaternion.w;
res.z = pMatrix.c1 * pQuaternion.x + pMatrix.c2 * pQuaternion.y + pMatrix.c3 * pQuaternion.z + pMatrix.c4 * pQuaternion.w;
res.w = pMatrix.d1 * pQuaternion.x + pMatrix.d2 * pQuaternion.y + pMatrix.d3 * pQuaternion.z + pMatrix.d4 * pQuaternion.w;
return res;
}
2015-05-19 04:16:51 +00:00
// ---------------------------------------------------------------------------
template<typename TReal>
bool aiQuaterniont<TReal>::operator== (const aiQuaterniont& o) const
{
2015-05-19 04:18:29 +00:00
return x == o.x && y == o.y && z == o.z && w == o.w;
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
template<typename TReal>
bool aiQuaterniont<TReal>::operator!= (const aiQuaterniont& o) const
{
2015-05-19 04:18:29 +00:00
return !(*this == o);
2015-05-19 04:16:51 +00:00
}
2020-12-17 07:07:03 +00:00
// ------------------------------------------------------------------------------------------------
template <typename TReal>
AI_FORCE_INLINE
aiQuaterniont<TReal>& aiQuaterniont<TReal>::operator *= (const aiMatrix4x4t<TReal>& mat){
return (*this = mat * (*this));
}
// ------------------------------------------------------------------------------------------------
2015-05-19 04:16:51 +00:00
// ---------------------------------------------------------------------------
template<typename TReal>
inline bool aiQuaterniont<TReal>::Equal(const aiQuaterniont& o, TReal epsilon) const {
2015-05-19 04:18:29 +00:00
return
std::abs(x - o.x) <= epsilon &&
std::abs(y - o.y) <= epsilon &&
std::abs(z - o.z) <= epsilon &&
std::abs(w - o.w) <= epsilon;
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
// Constructs a quaternion from a rotation matrix
template<typename TReal>
inline aiQuaterniont<TReal>::aiQuaterniont( const aiMatrix3x3t<TReal> &pRotMatrix)
{
2015-05-19 04:18:29 +00:00
TReal t = pRotMatrix.a1 + pRotMatrix.b2 + pRotMatrix.c3;
// large enough
if( t > static_cast<TReal>(0))
{
TReal s = std::sqrt(1 + t) * static_cast<TReal>(2.0);
x = (pRotMatrix.c2 - pRotMatrix.b3) / s;
y = (pRotMatrix.a3 - pRotMatrix.c1) / s;
z = (pRotMatrix.b1 - pRotMatrix.a2) / s;
w = static_cast<TReal>(0.25) * s;
} // else we have to check several cases
else if( pRotMatrix.a1 > pRotMatrix.b2 && pRotMatrix.a1 > pRotMatrix.c3 )
{
// Column 0:
TReal s = std::sqrt( static_cast<TReal>(1.0) + pRotMatrix.a1 - pRotMatrix.b2 - pRotMatrix.c3) * static_cast<TReal>(2.0);
x = static_cast<TReal>(0.25) * s;
y = (pRotMatrix.b1 + pRotMatrix.a2) / s;
z = (pRotMatrix.a3 + pRotMatrix.c1) / s;
w = (pRotMatrix.c2 - pRotMatrix.b3) / s;
}
else if( pRotMatrix.b2 > pRotMatrix.c3)
{
// Column 1:
TReal s = std::sqrt( static_cast<TReal>(1.0) + pRotMatrix.b2 - pRotMatrix.a1 - pRotMatrix.c3) * static_cast<TReal>(2.0);
x = (pRotMatrix.b1 + pRotMatrix.a2) / s;
y = static_cast<TReal>(0.25) * s;
z = (pRotMatrix.c2 + pRotMatrix.b3) / s;
w = (pRotMatrix.a3 - pRotMatrix.c1) / s;
} else
{
// Column 2:
TReal s = std::sqrt( static_cast<TReal>(1.0) + pRotMatrix.c3 - pRotMatrix.a1 - pRotMatrix.b2) * static_cast<TReal>(2.0);
x = (pRotMatrix.a3 + pRotMatrix.c1) / s;
y = (pRotMatrix.c2 + pRotMatrix.b3) / s;
z = static_cast<TReal>(0.25) * s;
w = (pRotMatrix.b1 - pRotMatrix.a2) / s;
}
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
// Construction from euler angles
template<typename TReal>
inline aiQuaterniont<TReal>::aiQuaterniont( TReal fPitch, TReal fYaw, TReal fRoll )
{
2015-05-19 04:18:29 +00:00
const TReal fSinPitch(std::sin(fPitch*static_cast<TReal>(0.5)));
const TReal fCosPitch(std::cos(fPitch*static_cast<TReal>(0.5)));
const TReal fSinYaw(std::sin(fYaw*static_cast<TReal>(0.5)));
const TReal fCosYaw(std::cos(fYaw*static_cast<TReal>(0.5)));
const TReal fSinRoll(std::sin(fRoll*static_cast<TReal>(0.5)));
const TReal fCosRoll(std::cos(fRoll*static_cast<TReal>(0.5)));
const TReal fCosPitchCosYaw(fCosPitch*fCosYaw);
const TReal fSinPitchSinYaw(fSinPitch*fSinYaw);
x = fSinRoll * fCosPitchCosYaw - fCosRoll * fSinPitchSinYaw;
y = fCosRoll * fSinPitch * fCosYaw + fSinRoll * fCosPitch * fSinYaw;
z = fCosRoll * fCosPitch * fSinYaw - fSinRoll * fSinPitch * fCosYaw;
w = fCosRoll * fCosPitchCosYaw + fSinRoll * fSinPitchSinYaw;
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
// Returns a matrix representation of the quaternion
template<typename TReal>
inline aiMatrix3x3t<TReal> aiQuaterniont<TReal>::GetMatrix() const
{
2015-05-19 04:18:29 +00:00
aiMatrix3x3t<TReal> resMatrix;
resMatrix.a1 = static_cast<TReal>(1.0) - static_cast<TReal>(2.0) * (y * y + z * z);
resMatrix.a2 = static_cast<TReal>(2.0) * (x * y - z * w);
resMatrix.a3 = static_cast<TReal>(2.0) * (x * z + y * w);
resMatrix.b1 = static_cast<TReal>(2.0) * (x * y + z * w);
resMatrix.b2 = static_cast<TReal>(1.0) - static_cast<TReal>(2.0) * (x * x + z * z);
resMatrix.b3 = static_cast<TReal>(2.0) * (y * z - x * w);
resMatrix.c1 = static_cast<TReal>(2.0) * (x * z - y * w);
resMatrix.c2 = static_cast<TReal>(2.0) * (y * z + x * w);
resMatrix.c3 = static_cast<TReal>(1.0) - static_cast<TReal>(2.0) * (x * x + y * y);
return resMatrix;
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
// Construction from an axis-angle pair
template<typename TReal>
inline aiQuaterniont<TReal>::aiQuaterniont( aiVector3t<TReal> axis, TReal angle)
{
2015-05-19 04:18:29 +00:00
axis.Normalize();
const TReal sin_a = std::sin( angle / 2 );
const TReal cos_a = std::cos( angle / 2 );
x = axis.x * sin_a;
y = axis.y * sin_a;
z = axis.z * sin_a;
w = cos_a;
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
// Construction from am existing, normalized quaternion
template<typename TReal>
inline aiQuaterniont<TReal>::aiQuaterniont( aiVector3t<TReal> normalized)
{
2015-05-19 04:18:29 +00:00
x = normalized.x;
y = normalized.y;
z = normalized.z;
2015-05-19 04:16:51 +00:00
2015-05-19 04:18:29 +00:00
const TReal t = static_cast<TReal>(1.0) - (x*x) - (y*y) - (z*z);
2015-05-19 04:16:51 +00:00
2015-05-19 04:18:29 +00:00
if (t < static_cast<TReal>(0.0)) {
w = static_cast<TReal>(0.0);
}
else w = std::sqrt (t);
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
2015-05-19 04:17:45 +00:00
// Performs a spherical interpolation between two quaternions
2015-05-19 04:16:51 +00:00
// Implementation adopted from the gmtl project. All others I found on the net fail in some cases.
// Congrats, gmtl!
template<typename TReal>
inline void aiQuaterniont<TReal>::Interpolate( aiQuaterniont& pOut, const aiQuaterniont& pStart, const aiQuaterniont& pEnd, TReal pFactor)
{
2015-05-19 04:18:29 +00:00
// calc cosine theta
TReal cosom = pStart.x * pEnd.x + pStart.y * pEnd.y + pStart.z * pEnd.z + pStart.w * pEnd.w;
// adjust signs (if necessary)
aiQuaterniont end = pEnd;
if( cosom < static_cast<TReal>(0.0))
{
cosom = -cosom;
end.x = -end.x; // Reverse all signs
end.y = -end.y;
end.z = -end.z;
end.w = -end.w;
}
// Calculate coefficients
TReal sclp, sclq;
2023-01-16 08:12:35 +00:00
2022-10-15 14:11:02 +00:00
if ((static_cast<TReal>(1.0) - cosom) > ai_epsilon) // 0.0001 -> some epsillon
2015-05-19 04:18:29 +00:00
{
// Standard case (slerp)
TReal omega, sinom;
omega = std::acos( cosom); // extract theta from dot product's cos theta
sinom = std::sin( omega);
sclp = std::sin( (static_cast<TReal>(1.0) - pFactor) * omega) / sinom;
sclq = std::sin( pFactor * omega) / sinom;
} else
{
// Very close, do linear interp (because it's faster)
sclp = static_cast<TReal>(1.0) - pFactor;
sclq = pFactor;
}
pOut.x = sclp * pStart.x + sclq * end.x;
pOut.y = sclp * pStart.y + sclq * end.y;
pOut.z = sclp * pStart.z + sclq * end.z;
pOut.w = sclp * pStart.w + sclq * end.w;
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
template<typename TReal>
inline aiQuaterniont<TReal>& aiQuaterniont<TReal>::Normalize()
{
2015-05-19 04:18:29 +00:00
// compute the magnitude and divide through it
const TReal mag = std::sqrt(x*x + y*y + z*z + w*w);
if (mag)
{
const TReal invMag = static_cast<TReal>(1.0)/mag;
x *= invMag;
y *= invMag;
z *= invMag;
w *= invMag;
}
return *this;
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
template<typename TReal>
inline aiQuaterniont<TReal> aiQuaterniont<TReal>::operator* (const aiQuaterniont& t) const
{
2015-05-19 04:18:29 +00:00
return aiQuaterniont(w*t.w - x*t.x - y*t.y - z*t.z,
w*t.x + x*t.w + y*t.z - z*t.y,
w*t.y + y*t.w + z*t.x - x*t.z,
w*t.z + z*t.w + x*t.y - y*t.x);
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
template<typename TReal>
inline aiQuaterniont<TReal>& aiQuaterniont<TReal>::Conjugate ()
{
2015-05-19 04:18:29 +00:00
x = -x;
y = -y;
z = -z;
return *this;
2015-05-19 04:16:51 +00:00
}
// ---------------------------------------------------------------------------
template<typename TReal>
Added missing functionalities to C API. The C API functions that have been added are the following: Vector2: - aiVector2AreEqual - aiVector2AreEqualEpsilon - aiVector2Add - aiVector2Subtract - aiVector2Scale - aiVector2SymMul - aiVector2DivideByScalar - aiVector2DivideByVector - aiVector2Length - aiVector2SquareLength - aiVector2Negate - aiVector2DotProduct - aiVector2Normalize Vector3: - aiVector3AreEqual - aiVector3AreEqualEpsilon - aiVector3LessThan - aiVector3Add - aiVector3Subtract - aiVector3Scale - aiVector3SymMul - aiVector3DivideByScalar - aiVector3DivideByVector - aiVector3Length - aiVector3SquareLength - aiVector3Negate - aiVector3DotProduct - aiVector3CrossProduct - aiVector3Normalize - aiVector3NormalizeSafe - aiVector3RotateByQuaternion Matrix3x3: - aiMatrix3FromMatrix4 - aiMatrix3FromQuaternion - aiMatrix3AreEqual - aiMatrix3AreEqualEpsilon - aiMatrix3Inverse - aiMatrix3Determinant - aiMatrix3RotationZ - aiMatrix3FromRotationAroundAxis - aiMatrix3Translation - aiMatrix3FromTo Matrix4x4: - aiMatrix4FromMatrix3 - aiMatrix4FromScalingQuaternionPosition - aiMatrix4Add - aiMatrix4AreEqual - aiMatrix4AreEqualEpsilon - aiMatrix4Inverse - aiMatrix4Determinant - aiMatrix4IsIdentity - aiMatrix4DecomposeIntoScalingEulerAnglesPosition - aiMatrix4DecomposeIntoScalingAxisAnglePosition - aiMatrix4DecomposeNoScaling - aiMatrix4FromEulerAngles - aiMatrix4RotationX - aiMatrix4RotationY - aiMatrix4RotationZ - aiMatrix4FromRotationAroundAxis - aiMatrix4Translation - aiMatrix4Scaling - aiMatrix4FromTo Quaternion: - aiQuaternionFromEulerAngles - aiQuaternionFromAxisAngle - aiQuaternionFromNormalizedQuaternion - aiQuaternionAreEqual - aiQuaternionAreEqualEpsilon - aiQuaternionNormalize - aiQuaternionConjugate - aiQuaternionMultiply - aiQuaternionInterpolate In addition, a const qualifier has been added to aiQuaterniont::Rotate to allow call to this method via a const aiQuaterniont pointer.
2020-03-26 17:08:40 +00:00
inline aiVector3t<TReal> aiQuaterniont<TReal>::Rotate (const aiVector3t<TReal>& v) const
2015-05-19 04:16:51 +00:00
{
2015-05-19 04:18:29 +00:00
aiQuaterniont q2(0.f,v.x,v.y,v.z), q = *this, qinv = q;
qinv.Conjugate();
2015-05-19 04:16:51 +00:00
2015-05-19 04:18:29 +00:00
q = q*q2*qinv;
return aiVector3t<TReal>(q.x,q.y,q.z);
2015-05-19 04:16:51 +00:00
}
#endif
#endif // AI_QUATERNION_INL_INC