nuclear@0: /* nuclear@0: --------------------------------------------------------------------------- nuclear@0: Open Asset Import Library (assimp) nuclear@0: --------------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2018, assimp team nuclear@0: nuclear@0: nuclear@0: nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the following nuclear@0: conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: --------------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: /** @file quaternion.inl nuclear@0: * @brief Inline implementation of aiQuaterniont operators nuclear@0: */ nuclear@0: #pragma once nuclear@0: #ifndef AI_QUATERNION_INL_INC nuclear@0: #define AI_QUATERNION_INL_INC nuclear@0: nuclear@0: #ifdef __cplusplus nuclear@0: #include "quaternion.h" nuclear@0: nuclear@0: #include nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: template nuclear@0: bool aiQuaterniont::operator== (const aiQuaterniont& o) const nuclear@0: { nuclear@0: return x == o.x && y == o.y && z == o.z && w == o.w; nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: template nuclear@0: bool aiQuaterniont::operator!= (const aiQuaterniont& o) const nuclear@0: { nuclear@0: return !(*this == o); nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: template nuclear@0: inline bool aiQuaterniont::Equal(const aiQuaterniont& o, TReal epsilon) const { nuclear@0: return nuclear@0: std::abs(x - o.x) <= epsilon && nuclear@0: std::abs(y - o.y) <= epsilon && nuclear@0: std::abs(z - o.z) <= epsilon && nuclear@0: std::abs(w - o.w) <= epsilon; nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: // Constructs a quaternion from a rotation matrix nuclear@0: template nuclear@0: inline aiQuaterniont::aiQuaterniont( const aiMatrix3x3t &pRotMatrix) nuclear@0: { nuclear@0: TReal t = pRotMatrix.a1 + pRotMatrix.b2 + pRotMatrix.c3; nuclear@0: nuclear@0: // large enough nuclear@0: if( t > static_cast(0)) nuclear@0: { nuclear@0: TReal s = std::sqrt(1 + t) * static_cast(2.0); nuclear@0: x = (pRotMatrix.c2 - pRotMatrix.b3) / s; nuclear@0: y = (pRotMatrix.a3 - pRotMatrix.c1) / s; nuclear@0: z = (pRotMatrix.b1 - pRotMatrix.a2) / s; nuclear@0: w = static_cast(0.25) * s; nuclear@0: } // else we have to check several cases nuclear@0: else if( pRotMatrix.a1 > pRotMatrix.b2 && pRotMatrix.a1 > pRotMatrix.c3 ) nuclear@0: { nuclear@0: // Column 0: nuclear@0: TReal s = std::sqrt( static_cast(1.0) + pRotMatrix.a1 - pRotMatrix.b2 - pRotMatrix.c3) * static_cast(2.0); nuclear@0: x = static_cast(0.25) * s; nuclear@0: y = (pRotMatrix.b1 + pRotMatrix.a2) / s; nuclear@0: z = (pRotMatrix.a3 + pRotMatrix.c1) / s; nuclear@0: w = (pRotMatrix.c2 - pRotMatrix.b3) / s; nuclear@0: } nuclear@0: else if( pRotMatrix.b2 > pRotMatrix.c3) nuclear@0: { nuclear@0: // Column 1: nuclear@0: TReal s = std::sqrt( static_cast(1.0) + pRotMatrix.b2 - pRotMatrix.a1 - pRotMatrix.c3) * static_cast(2.0); nuclear@0: x = (pRotMatrix.b1 + pRotMatrix.a2) / s; nuclear@0: y = static_cast(0.25) * s; nuclear@0: z = (pRotMatrix.c2 + pRotMatrix.b3) / s; nuclear@0: w = (pRotMatrix.a3 - pRotMatrix.c1) / s; nuclear@0: } else nuclear@0: { nuclear@0: // Column 2: nuclear@0: TReal s = std::sqrt( static_cast(1.0) + pRotMatrix.c3 - pRotMatrix.a1 - pRotMatrix.b2) * static_cast(2.0); nuclear@0: x = (pRotMatrix.a3 + pRotMatrix.c1) / s; nuclear@0: y = (pRotMatrix.c2 + pRotMatrix.b3) / s; nuclear@0: z = static_cast(0.25) * s; nuclear@0: w = (pRotMatrix.b1 - pRotMatrix.a2) / s; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: // Construction from euler angles nuclear@0: template nuclear@0: inline aiQuaterniont::aiQuaterniont( TReal fPitch, TReal fYaw, TReal fRoll ) nuclear@0: { nuclear@0: const TReal fSinPitch(std::sin(fPitch*static_cast(0.5))); nuclear@0: const TReal fCosPitch(std::cos(fPitch*static_cast(0.5))); nuclear@0: const TReal fSinYaw(std::sin(fYaw*static_cast(0.5))); nuclear@0: const TReal fCosYaw(std::cos(fYaw*static_cast(0.5))); nuclear@0: const TReal fSinRoll(std::sin(fRoll*static_cast(0.5))); nuclear@0: const TReal fCosRoll(std::cos(fRoll*static_cast(0.5))); nuclear@0: const TReal fCosPitchCosYaw(fCosPitch*fCosYaw); nuclear@0: const TReal fSinPitchSinYaw(fSinPitch*fSinYaw); nuclear@0: x = fSinRoll * fCosPitchCosYaw - fCosRoll * fSinPitchSinYaw; nuclear@0: y = fCosRoll * fSinPitch * fCosYaw + fSinRoll * fCosPitch * fSinYaw; nuclear@0: z = fCosRoll * fCosPitch * fSinYaw - fSinRoll * fSinPitch * fCosYaw; nuclear@0: w = fCosRoll * fCosPitchCosYaw + fSinRoll * fSinPitchSinYaw; nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: // Returns a matrix representation of the quaternion nuclear@0: template nuclear@0: inline aiMatrix3x3t aiQuaterniont::GetMatrix() const nuclear@0: { nuclear@0: aiMatrix3x3t resMatrix; nuclear@0: resMatrix.a1 = static_cast(1.0) - static_cast(2.0) * (y * y + z * z); nuclear@0: resMatrix.a2 = static_cast(2.0) * (x * y - z * w); nuclear@0: resMatrix.a3 = static_cast(2.0) * (x * z + y * w); nuclear@0: resMatrix.b1 = static_cast(2.0) * (x * y + z * w); nuclear@0: resMatrix.b2 = static_cast(1.0) - static_cast(2.0) * (x * x + z * z); nuclear@0: resMatrix.b3 = static_cast(2.0) * (y * z - x * w); nuclear@0: resMatrix.c1 = static_cast(2.0) * (x * z - y * w); nuclear@0: resMatrix.c2 = static_cast(2.0) * (y * z + x * w); nuclear@0: resMatrix.c3 = static_cast(1.0) - static_cast(2.0) * (x * x + y * y); nuclear@0: nuclear@0: return resMatrix; nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: // Construction from an axis-angle pair nuclear@0: template nuclear@0: inline aiQuaterniont::aiQuaterniont( aiVector3t axis, TReal angle) nuclear@0: { nuclear@0: axis.Normalize(); nuclear@0: nuclear@0: const TReal sin_a = std::sin( angle / 2 ); nuclear@0: const TReal cos_a = std::cos( angle / 2 ); nuclear@0: x = axis.x * sin_a; nuclear@0: y = axis.y * sin_a; nuclear@0: z = axis.z * sin_a; nuclear@0: w = cos_a; nuclear@0: } nuclear@0: // --------------------------------------------------------------------------- nuclear@0: // Construction from am existing, normalized quaternion nuclear@0: template nuclear@0: inline aiQuaterniont::aiQuaterniont( aiVector3t normalized) nuclear@0: { nuclear@0: x = normalized.x; nuclear@0: y = normalized.y; nuclear@0: z = normalized.z; nuclear@0: nuclear@0: const TReal t = static_cast(1.0) - (x*x) - (y*y) - (z*z); nuclear@0: nuclear@0: if (t < static_cast(0.0)) { nuclear@0: w = static_cast(0.0); nuclear@0: } nuclear@0: else w = std::sqrt (t); nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: // Performs a spherical interpolation between two quaternions nuclear@0: // Implementation adopted from the gmtl project. All others I found on the net fail in some cases. nuclear@0: // Congrats, gmtl! nuclear@0: template nuclear@0: inline void aiQuaterniont::Interpolate( aiQuaterniont& pOut, const aiQuaterniont& pStart, const aiQuaterniont& pEnd, TReal pFactor) nuclear@0: { nuclear@0: // calc cosine theta nuclear@0: TReal cosom = pStart.x * pEnd.x + pStart.y * pEnd.y + pStart.z * pEnd.z + pStart.w * pEnd.w; nuclear@0: nuclear@0: // adjust signs (if necessary) nuclear@0: aiQuaterniont end = pEnd; nuclear@0: if( cosom < static_cast(0.0)) nuclear@0: { nuclear@0: cosom = -cosom; nuclear@0: end.x = -end.x; // Reverse all signs nuclear@0: end.y = -end.y; nuclear@0: end.z = -end.z; nuclear@0: end.w = -end.w; nuclear@0: } nuclear@0: nuclear@0: // Calculate coefficients nuclear@0: TReal sclp, sclq; nuclear@0: if( (static_cast(1.0) - cosom) > static_cast(0.0001)) // 0.0001 -> some epsillon nuclear@0: { nuclear@0: // Standard case (slerp) nuclear@0: TReal omega, sinom; nuclear@0: omega = std::acos( cosom); // extract theta from dot product's cos theta nuclear@0: sinom = std::sin( omega); nuclear@0: sclp = std::sin( (static_cast(1.0) - pFactor) * omega) / sinom; nuclear@0: sclq = std::sin( pFactor * omega) / sinom; nuclear@0: } else nuclear@0: { nuclear@0: // Very close, do linear interp (because it's faster) nuclear@0: sclp = static_cast(1.0) - pFactor; nuclear@0: sclq = pFactor; nuclear@0: } nuclear@0: nuclear@0: pOut.x = sclp * pStart.x + sclq * end.x; nuclear@0: pOut.y = sclp * pStart.y + sclq * end.y; nuclear@0: pOut.z = sclp * pStart.z + sclq * end.z; nuclear@0: pOut.w = sclp * pStart.w + sclq * end.w; nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: template nuclear@0: inline aiQuaterniont& aiQuaterniont::Normalize() nuclear@0: { nuclear@0: // compute the magnitude and divide through it nuclear@0: const TReal mag = std::sqrt(x*x + y*y + z*z + w*w); nuclear@0: if (mag) nuclear@0: { nuclear@0: const TReal invMag = static_cast(1.0)/mag; nuclear@0: x *= invMag; nuclear@0: y *= invMag; nuclear@0: z *= invMag; nuclear@0: w *= invMag; nuclear@0: } nuclear@0: return *this; nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: template nuclear@0: inline aiQuaterniont aiQuaterniont::operator* (const aiQuaterniont& t) const nuclear@0: { nuclear@0: return aiQuaterniont(w*t.w - x*t.x - y*t.y - z*t.z, nuclear@0: w*t.x + x*t.w + y*t.z - z*t.y, nuclear@0: w*t.y + y*t.w + z*t.x - x*t.z, nuclear@0: w*t.z + z*t.w + x*t.y - y*t.x); nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: template nuclear@0: inline aiQuaterniont& aiQuaterniont::Conjugate () nuclear@0: { nuclear@0: x = -x; nuclear@0: y = -y; nuclear@0: z = -z; nuclear@0: return *this; nuclear@0: } nuclear@0: nuclear@0: // --------------------------------------------------------------------------- nuclear@0: template nuclear@0: inline aiVector3t aiQuaterniont::Rotate (const aiVector3t& v) nuclear@0: { nuclear@0: aiQuaterniont q2(0.f,v.x,v.y,v.z), q = *this, qinv = q; nuclear@0: qinv.Conjugate(); nuclear@0: nuclear@0: q = q*q2*qinv; nuclear@0: return aiVector3t(q.x,q.y,q.z); nuclear@0: } nuclear@0: nuclear@0: #endif nuclear@0: #endif // AI_QUATERNION_INL_INC