From: <axl...@us...> - 2009-05-31 04:04:36
|
Revision: 285 http://hgengine.svn.sourceforge.net/hgengine/?rev=285&view=rev Author: axlecrusher Date: 2009-05-31 04:04:35 +0000 (Sun, 31 May 2009) Log Message: ----------- updates Modified Paths: -------------- Mercury2/src/MQuaternion.cpp Mercury2/src/MQuaternion.h Modified: Mercury2/src/MQuaternion.cpp =================================================================== --- Mercury2/src/MQuaternion.cpp 2009-05-31 01:25:41 UTC (rev 284) +++ Mercury2/src/MQuaternion.cpp 2009-05-31 04:04:35 UTC (rev 285) @@ -1,5 +1,6 @@ #include <MQuaternion.h> #include <MercuryMath.h> +#include <MercuryMatrix.h> MQuaternion::MQuaternion() { @@ -60,6 +61,24 @@ m_wxyz[3] = sn * p[2]; } +void MQuaternion::ToAxisAngle(float& angle, float& x, float& y, float& z) const +{ + float scale = SQRT(x*x + y*y + z*z); + + //prevent infinite axis + if (scale == 0) + { + x=y=0; z=1; + angle = 0; + return; + } + + angle = 2*acos(m_wxyz[0]); + x = m_wxyz[1] / scale; + y = m_wxyz[2] / scale; + z = m_wxyz[3] / scale; +} + //Returns the magnitude float MQuaternion::magnitude() const { return SQRT((m_wxyz[0]*m_wxyz[0])+(m_wxyz[1]*m_wxyz[1])+(m_wxyz[2]*m_wxyz[2])+(m_wxyz[3]*m_wxyz[3])); @@ -215,7 +234,7 @@ return true; } -MercuryVertex MQuaternion::ToVertex() const +MercuryVertex MQuaternion::ToVector() const { MercuryVertex v(m_wxyz[1], m_wxyz[2], m_wxyz[3], m_wxyz[0]); return v; @@ -226,7 +245,12 @@ printf("%s: %f %f %f %f\n", s.c_str(), m_wxyz[0], m_wxyz[1], m_wxyz[2], m_wxyz[3]); } +MercuryVector MQuaternion::operator * (const MercuryVector &rhs) const +{ + return (*this * MQuaternion(0, rhs) * reciprocal()).ToVector(); +} + //Returns the Euclidian Inner Product of two MQuaternions (Similar to Vector Dot-Product) float innerProduct(const MQuaternion & a, const MQuaternion &b) { Modified: Mercury2/src/MQuaternion.h =================================================================== --- Mercury2/src/MQuaternion.h 2009-05-31 01:25:41 UTC (rev 284) +++ Mercury2/src/MQuaternion.h 2009-05-31 04:04:35 UTC (rev 285) @@ -2,8 +2,10 @@ #define MQUATERNION_H #include <MercuryVertex.h> -#include <MercuryMatrix.h> +#include <MercuryUtil.h> +class MercuryMatrix; + ///Mathematical Quaternion (Used for Rotation) class MQuaternion { public: @@ -19,6 +21,7 @@ ///Make the quaternion represent a given angle radians around an axis p void CreateFromAxisAngle(const MercuryVertex& p, const float radians); + void ToAxisAngle(float& angle, float& x, float& y, float& z) const; ///Access a component of the quaternion with the [] operator float & operator[] ( const WXYZ rhs ); @@ -39,7 +42,7 @@ ///Converts Quaternion to complete 4x4 Matrix void toMatrix4( MercuryMatrix & mat ) const; ///Convert the quaternion to a point. - MercuryVertex ToVertex() const; + MercuryVertex ToVector() const; /****************************************************** * NOTE: Quaternion multipication is not commutative * @@ -58,6 +61,8 @@ MQuaternion& operator *= (const float &rhs); MQuaternion& operator /= (const float &rhs); + MercuryVector operator * (const MercuryVector &rhs) const; + bool operator==(const MQuaternion &rhs) const; inline bool operator!=(const MQuaternion &rhs) const { return !(*this == rhs); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |