2008-07-18 20:29:46 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development Team
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
following 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 Development Team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
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
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2009-01-13 18:58:07 +00:00
|
|
|
/** @file aiQuaternion.h
|
|
|
|
* @brief Quaternion structure, including operators when compiling in C++
|
|
|
|
*/
|
2008-05-05 12:36:31 +00:00
|
|
|
#ifndef AI_QUATERNION_H_INC
|
|
|
|
#define AI_QUATERNION_H_INC
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include "aiTypes.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/** Represents a quaternion in a 4D vector. */
|
2008-05-09 17:24:28 +00:00
|
|
|
struct aiQuaternion
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
|
|
|
#ifdef __cplusplus
|
|
|
|
aiQuaternion() : w(0.0f), x(0.0f), y(0.0f), z(0.0f) {}
|
|
|
|
aiQuaternion(float _w, float _x, float _y, float _z) : w(_w), x(_x), y(_y), z(_z) {}
|
2008-06-22 10:09:26 +00:00
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
/** Construct from rotation matrix. Result is undefined if the matrix is not orthonormal. */
|
|
|
|
aiQuaternion( const aiMatrix3x3& pRotMatrix);
|
|
|
|
|
2008-06-22 10:09:26 +00:00
|
|
|
/** Construct from euler angles */
|
|
|
|
aiQuaternion( float rotx, float roty, float rotz);
|
|
|
|
|
2008-08-08 11:59:09 +00:00
|
|
|
/** Construct from an axis-angle pair */
|
|
|
|
aiQuaternion( aiVector3D axis, float angle);
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
/** Construct from a normalized quaternion stored in a vec3 */
|
|
|
|
aiQuaternion( aiVector3D normalized);
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
/** Returns a matrix representation of the quaternion */
|
|
|
|
aiMatrix3x3 GetMatrix() const;
|
2008-07-18 20:29:46 +00:00
|
|
|
|
2008-10-31 19:32:00 +00:00
|
|
|
|
|
|
|
bool operator== (const aiQuaternion& o) const
|
|
|
|
{return x == o.x && y == o.y && z == o.z && w == o.w;}
|
|
|
|
|
|
|
|
bool operator!= (const aiQuaternion& o) const
|
|
|
|
{return !(*this == o);}
|
|
|
|
|
ASE: Added WIP support for *SMOOTHSKINMESH elements in ASE/ASC files. Fixes in the ASE loader. Fixed animation parsing. Temporary implementation of target lights and cameras, including animations.
3DS: Fixed transformation problems (Pivot points), added WIP animation support. No target animation yet (cameras, spot lights). Not yet fully tested, but static models that worked before should still work now, except all look correct now :-) (some problems with very large models remaining)
Further work on the IRR and IRRMESH loaders. IRR still WIP, IRRMESH more stable now.
Work on the LWo loader. Added support for the "one-layer-only" mode. Hierarchy bug still unfixed, UV coords bug still unfixed.
Further work on the FindInvalidDataprocess. Improved validation for normals, no false positives anymore.
Further work on the MDR loader, still WIP.
Moved DeterminePType-Step to ScenePreprocessor.
aiAnimation::mDuration is optional now, ScenePreprocessor computes it automatically if set to -1.
Fixes in the SMD loader. Still crashes on some files.
Updated animation documentation.
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@236 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2008-11-09 23:17:19 +00:00
|
|
|
/** Normalize the quaternion */
|
|
|
|
aiQuaternion& Normalize();
|
|
|
|
|
|
|
|
/** Multiply two quaternions */
|
|
|
|
aiQuaternion operator* (const aiQuaternion& two) const;
|
|
|
|
|
2008-11-02 17:20:29 +00:00
|
|
|
/** Performs a spherical interpolation between two quaternions and writes the result into the third.
|
|
|
|
* @param pOut Target object to received the interpolated rotation.
|
|
|
|
* @param pStart Start rotation of the interpolation at factor == 0.
|
|
|
|
* @param pEnd End rotation, factor == 1.
|
|
|
|
* @param pFactor Interpolation factor between 0 and 1. Values outside of this range yield undefined results.
|
|
|
|
*/
|
|
|
|
static void Interpolate( aiQuaternion& pOut, const aiQuaternion& pStart, const aiQuaternion& pEnd, float pFactor);
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
#endif // __cplusplus
|
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
//! w,x,y,z components of the quaternion
|
2008-05-05 12:36:31 +00:00
|
|
|
float w, x, y, z;
|
2008-05-09 17:24:28 +00:00
|
|
|
} ;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Constructs a quaternion from a rotation matrix
|
|
|
|
inline aiQuaternion::aiQuaternion( const aiMatrix3x3 &pRotMatrix)
|
|
|
|
{
|
2008-05-09 17:24:28 +00:00
|
|
|
float t = 1 + pRotMatrix.a1 + pRotMatrix.b2 + pRotMatrix.c3;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// large enough
|
2008-10-26 18:58:34 +00:00
|
|
|
if( t > 0.001f)
|
2008-05-09 17:24:28 +00:00
|
|
|
{
|
|
|
|
float s = sqrt( t) * 2.0f;
|
|
|
|
x = (pRotMatrix.b3 - pRotMatrix.c2) / s;
|
|
|
|
y = (pRotMatrix.c1 - pRotMatrix.a3) / s;
|
|
|
|
z = (pRotMatrix.a2 - pRotMatrix.b1) / s;
|
|
|
|
w = 0.25f * s;
|
|
|
|
} // else we have to check several cases
|
|
|
|
else if( pRotMatrix.a1 > pRotMatrix.b2 && pRotMatrix.a1 > pRotMatrix.c3 )
|
|
|
|
{
|
2008-10-26 18:58:34 +00:00
|
|
|
// Column 0:
|
2008-05-09 17:24:28 +00:00
|
|
|
float s = sqrt( 1.0f + pRotMatrix.a1 - pRotMatrix.b2 - pRotMatrix.c3) * 2.0f;
|
2008-10-26 18:58:34 +00:00
|
|
|
x = 0.25f * s;
|
2008-05-09 17:24:28 +00:00
|
|
|
y = (pRotMatrix.a2 + pRotMatrix.b1) / s;
|
|
|
|
z = (pRotMatrix.c1 + pRotMatrix.a3) / s;
|
2008-10-26 18:58:34 +00:00
|
|
|
w = (pRotMatrix.b3 - pRotMatrix.c2) / s;
|
2008-05-09 17:24:28 +00:00
|
|
|
}
|
|
|
|
else if( pRotMatrix.b2 > pRotMatrix.c3)
|
|
|
|
{
|
2008-10-26 18:58:34 +00:00
|
|
|
// Column 1:
|
2008-05-09 17:24:28 +00:00
|
|
|
float s = sqrt( 1.0f + pRotMatrix.b2 - pRotMatrix.a1 - pRotMatrix.c3) * 2.0f;
|
|
|
|
x = (pRotMatrix.a2 + pRotMatrix.b1) / s;
|
2008-10-26 18:58:34 +00:00
|
|
|
y = 0.25f * s;
|
2008-05-09 17:24:28 +00:00
|
|
|
z = (pRotMatrix.b3 + pRotMatrix.c2) / s;
|
2008-10-26 18:58:34 +00:00
|
|
|
w = (pRotMatrix.c1 - pRotMatrix.a3) / s;
|
2008-06-08 20:26:06 +00:00
|
|
|
} else
|
2008-05-09 17:24:28 +00:00
|
|
|
{
|
2008-10-26 18:58:34 +00:00
|
|
|
// Column 2:
|
2008-05-09 17:24:28 +00:00
|
|
|
float s = sqrt( 1.0f + pRotMatrix.c3 - pRotMatrix.a1 - pRotMatrix.b2) * 2.0f;
|
|
|
|
x = (pRotMatrix.c1 + pRotMatrix.a3) / s;
|
|
|
|
y = (pRotMatrix.b3 + pRotMatrix.c2) / s;
|
2008-10-26 18:58:34 +00:00
|
|
|
z = 0.25f * s;
|
|
|
|
w = (pRotMatrix.a2 - pRotMatrix.b1) / s;
|
2008-05-09 17:24:28 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
2008-06-22 10:09:26 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Construction from euler angles
|
|
|
|
inline aiQuaternion::aiQuaternion( float fPitch, float fYaw, float fRoll )
|
|
|
|
{
|
|
|
|
const float fSinPitch(sin(fPitch*0.5F));
|
|
|
|
const float fCosPitch(cos(fPitch*0.5F));
|
|
|
|
const float fSinYaw(sin(fYaw*0.5F));
|
|
|
|
const float fCosYaw(cos(fYaw*0.5F));
|
|
|
|
const float fSinRoll(sin(fRoll*0.5F));
|
|
|
|
const float fCosRoll(cos(fRoll*0.5F));
|
|
|
|
const float fCosPitchCosYaw(fCosPitch*fCosYaw);
|
|
|
|
const float 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;
|
|
|
|
}
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Returns a matrix representation of the quaternion
|
|
|
|
inline aiMatrix3x3 aiQuaternion::GetMatrix() const
|
|
|
|
{
|
|
|
|
aiMatrix3x3 resMatrix;
|
|
|
|
resMatrix.a1 = 1.0f - 2.0f * (y * y + z * z);
|
|
|
|
resMatrix.a2 = 2.0f * (x * y + z * w);
|
|
|
|
resMatrix.a3 = 2.0f * (x * z - y * w);
|
|
|
|
resMatrix.b1 = 2.0f * (x * y - z * w);
|
|
|
|
resMatrix.b2 = 1.0f - 2.0f * (x * x + z * z);
|
|
|
|
resMatrix.b3 = 2.0f * (y * z + x * w);
|
|
|
|
resMatrix.c1 = 2.0f * (x * z + y * w);
|
|
|
|
resMatrix.c2 = 2.0f * (y * z - x * w);
|
|
|
|
resMatrix.c3 = 1.0f - 2.0f * (x * x + y * y);
|
|
|
|
|
|
|
|
return resMatrix;
|
|
|
|
}
|
|
|
|
|
2008-07-18 20:29:46 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Construction from an axis-angle pair
|
|
|
|
inline aiQuaternion::aiQuaternion( aiVector3D axis, float angle)
|
|
|
|
{
|
|
|
|
axis.Normalize();
|
|
|
|
|
|
|
|
const float sin_a = sin( angle / 2 );
|
|
|
|
const float cos_a = cos( angle / 2 );
|
|
|
|
x = axis.x * sin_a;
|
|
|
|
y = axis.y * sin_a;
|
|
|
|
z = axis.z * sin_a;
|
|
|
|
w = cos_a;
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Construction from am existing, normalized quaternion
|
|
|
|
inline aiQuaternion::aiQuaternion( aiVector3D normalized)
|
|
|
|
{
|
|
|
|
x = normalized.x;
|
|
|
|
y = normalized.y;
|
|
|
|
z = normalized.z;
|
2008-07-18 20:29:46 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
float t = 1.0f - (normalized.x * normalized.x) -
|
|
|
|
(normalized.y * normalized.y) - (normalized.z * normalized.z);
|
|
|
|
|
|
|
|
if (t < 0.0f)
|
|
|
|
w = 0.0f;
|
|
|
|
else w = sqrt (t);
|
|
|
|
|
|
|
|
}
|
2008-07-18 20:29:46 +00:00
|
|
|
|
2008-11-02 17:20:29 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Performs a spherical interpolation between two quaternions
|
|
|
|
// Implementation adopted from the gmtl project. All others I found on the net fail in some cases.
|
|
|
|
// Congrats, gmtl!
|
|
|
|
inline void aiQuaternion::Interpolate( aiQuaternion& pOut, const aiQuaternion& pStart, const aiQuaternion& pEnd, float pFactor)
|
|
|
|
{
|
2008-11-02 22:30:37 +00:00
|
|
|
// calc cosine theta
|
|
|
|
float cosom = pStart.x * pEnd.x + pStart.y * pEnd.y + pStart.z * pEnd.z + pStart.w * pEnd.w;
|
|
|
|
|
|
|
|
// adjust signs (if necessary)
|
|
|
|
aiQuaternion end = pEnd;
|
|
|
|
if( cosom < 0.0f)
|
|
|
|
{
|
|
|
|
cosom = -cosom;
|
|
|
|
end.x = -end.x; // Reverse all signs
|
|
|
|
end.y = -end.y;
|
|
|
|
end.z = -end.z;
|
|
|
|
end.w = -end.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate coefficients
|
|
|
|
float sclp, sclq;
|
|
|
|
if( (1.0f - cosom) > 0.0001f) // 0.0001 -> some epsillon
|
|
|
|
{
|
|
|
|
// Standard case (slerp)
|
|
|
|
float omega, sinom;
|
|
|
|
omega = acos( cosom); // extract theta from dot product's cos theta
|
|
|
|
sinom = sin( omega);
|
|
|
|
sclp = sin( (1.0f - pFactor) * omega) / sinom;
|
|
|
|
sclq = sin( pFactor * omega) / sinom;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
// Very close, do linear interp (because it's faster)
|
|
|
|
sclp = 1.0f - 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;
|
2008-11-02 17:20:29 +00:00
|
|
|
}
|
2008-06-22 10:09:26 +00:00
|
|
|
|
ASE: Added WIP support for *SMOOTHSKINMESH elements in ASE/ASC files. Fixes in the ASE loader. Fixed animation parsing. Temporary implementation of target lights and cameras, including animations.
3DS: Fixed transformation problems (Pivot points), added WIP animation support. No target animation yet (cameras, spot lights). Not yet fully tested, but static models that worked before should still work now, except all look correct now :-) (some problems with very large models remaining)
Further work on the IRR and IRRMESH loaders. IRR still WIP, IRRMESH more stable now.
Work on the LWo loader. Added support for the "one-layer-only" mode. Hierarchy bug still unfixed, UV coords bug still unfixed.
Further work on the FindInvalidDataprocess. Improved validation for normals, no false positives anymore.
Further work on the MDR loader, still WIP.
Moved DeterminePType-Step to ScenePreprocessor.
aiAnimation::mDuration is optional now, ScenePreprocessor computes it automatically if set to -1.
Fixes in the SMD loader. Still crashes on some files.
Updated animation documentation.
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@236 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2008-11-09 23:17:19 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
inline aiQuaternion& aiQuaternion::Normalize()
|
|
|
|
{
|
|
|
|
// compute the magnitude and divide through it
|
|
|
|
const float mag = x*x+y*y+z*z+w*w;
|
|
|
|
if (mag)
|
|
|
|
{
|
|
|
|
x /= mag;
|
|
|
|
y /= mag;
|
|
|
|
z /= mag;
|
|
|
|
w /= mag;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
inline aiQuaternion aiQuaternion::operator* (const aiQuaternion& t) const
|
|
|
|
{
|
|
|
|
return aiQuaternion(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);
|
|
|
|
}
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
} // end extern "C"
|
|
|
|
#endif // __cplusplus
|
|
|
|
|
2008-08-08 11:48:50 +00:00
|
|
|
#endif // AI_QUATERNION_H_INC
|