--- /dev/null
+/****************************************************************************************/\r
+/* QUATERN.H */\r
+/* */\r
+/* Author: Mike Sandige */\r
+/* Description: Quaternion mathematical system interface */\r
+/* */\r
+/* The contents of this file are subject to the Genesis3D Public License */\r
+/* Version 1.01 (the "License"); you may not use this file except in */\r
+/* compliance with the License. You may obtain a copy of the License at */\r
+/* http://www.genesis3d.com */\r
+/* */\r
+/* Software distributed under the License is distributed on an "AS IS" */\r
+/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */\r
+/* the License for the specific language governing rights and limitations */\r
+/* under the License. */\r
+/* */\r
+/* The Original Code is Genesis3D, released March 25, 1999. */\r
+/* Genesis3D Version 1.1 released November 15, 1999 */\r
+/* Copyright (C) 1999 WildTangent, Inc. All Rights Reserved */\r
+/* */\r
+/****************************************************************************************/\r
+#ifndef GE_QUATERNION_H\r
+#define GE_QUATERNION_H\r
+\r
+/***************************************************************************\r
+\r
+ The quatern module contains basic support for a quaternion object.\r
+\r
+ quaternions are an extension of complex numbers that allows an\r
+ expression for rotation that can be easily interpolated. geQuaternion_s are also \r
+ more numericaly stable for repeated rotations than matrices.\r
+\r
+ \r
+ A quaternion is a 4 element 'vector' [w,x,y,z] where:\r
+\r
+ q = w + xi + yj + zk\r
+ i*i = -1\r
+ j*j = -1\r
+ k*k = -1\r
+ i*j = -j*i = k\r
+ j*k = -k*j = i\r
+ k*i = -i*k = j\r
+ q' (conjugate) = w - xi - yj - zk\r
+ ||q|| (magnitude) = sqrt(q*q') = sqrt(w*w + x*x + y*y + z*z)\r
+ unit quaternion ||q|| == 1; this implies q' == qinverse \r
+ quaternions are associative (q1*q2)*q3 == q1*(q2*q3)\r
+ quaternions are not commutative q1*q2 != q2*q1\r
+ qinverse (inverse (1/q) ) = q'/(q*q')\r
+ \r
+ q can be expressed by w + xi + yj + zk or [w,x,y,z] \r
+ or as in this implementation (s,v) where s=w, and v=[x,y,z]\r
+\r
+ quaternions can represent a rotation. The rotation is an angle t, around a \r
+ unit vector u. q=(s,v); s= cos(t/2); v= u*sin(t/2).\r
+\r
+ quaternions can apply the rotation to a point. let the point be p [px,py,pz],\r
+ and let P be a quaternion(0,p). Protated = q*P*qinverse \r
+ ( Protated = q*P*q' if q is a unit quaternion)\r
+\r
+ concatenation rotations is similar to matrix concatenation. given two rotations\r
+ q1 and q2, to rotate by q1, then q2: let qc = (q2*q1), then the combined \r
+ rotation is given by qc*P*qcinverse (= qc*P*qc' if q is a unit quaternion)\r
+\r
+ multiplication: \r
+ q1 = w1 + x1i + y1j + z1k\r
+ q2 = w2 + x2i + y2j + z2k\r
+ q1*q2 = q3 =\r
+ (w1*w2 - x1*x2 - y1*y2 - z1*z2) {w3}\r
+ (w1*x2 + x1*w2 + y1*z2 - z1*y2)i {x3}\r
+ (w1*y2 - x1*z2 + y1*w2 + z1*x2)j {y3}\r
+ (w1*z2 + x1*y2 + y1*x2 + z1*w2)k {z3}\r
+\r
+ also, \r
+ q1 = (s1,v1) = [s1,(x1,y1,z1)]\r
+ q2 = (s2,v2) = [s2,(x2,y2,z2)]\r
+ q1*q2 = q3 = (s1*s2 - dot_product(v1,v2), {s3}\r
+ (s1*v2 + s2*v1 + cross_product(v1,v2)) {v3}\r
+\r
+\r
+ interpolation - it is possible (and sometimes reasonable) to interpolate between\r
+ two quaternions by interpolating each component. This does not quarantee a \r
+ resulting unit quaternion, and will result in an animation with non-linear \r
+ rotational velocity.\r
+\r
+ spherical interpolation: (slerp) treat the quaternions as vectors \r
+ find the angle between them (w = arccos(q1 dot q2) ).\r
+ given 0<=t<=1, q(t) = q1*(sin((1-t)*w)/sin(w) + q2 * sin(t*w)/sin(w).\r
+ since q == -q, care must be taken to rotate the proper way. \r
+\r
+ this implementation uses the notation quaternion q = (quatS,quatV) \r
+ where quatS is a scalar, and quatV is a 3 element vector.\r
+\r
+********************************************/\r
+\r
+#include "BaseType.h"\r
+#include "XForm3d.h"\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+typedef struct \r
+{\r
+ geFloat W;\r
+ geFloat X,Y,Z;\r
+ //geVec3d QuatV;\r
+} geQuaternion;\r
+\r
+\r
+#define QUATERNION_PI (GE_PI)\r
+\r
+geBoolean GENESISCC geQuaternion_IsValid( const geQuaternion *Q );\r
+ // return GE_TRUE if Q is non null and for has no NAN's in its components\r
+\r
+void GENESISCC geQuaternion_Set( geQuaternion *Q, geFloat W, geFloat X, geFloat Y, geFloat Z);\r
+ // set quaternion components. Doesn't normalize\r
+void GENESISCC geQuaternion_SetVec3d( geQuaternion *Q, geFloat W, const geVec3d *V);\r
+ // set quaternion components. Doesn't normalize\r
+GENESISAPI void GENESISCC geQuaternion_SetFromAxisAngle(geQuaternion *Q, const geVec3d *Axis, geFloat Theta);\r
+ // set a quaternion from an axis and a rotation around the axis\r
+geBoolean GENESISCC geQuaternion_GetAxisAngle(const geQuaternion *Q, geVec3d *Axis, geFloat *Theta);\r
+ // gets an axis and angle of rotation around the axis from a quaternion\r
+ // returns GE_TRUE if there is an axis. \r
+ // returns GE_FALSE if there is no axis (and Axis is set to 0,0,0, and Theta is 0)\r
+\r
+void GENESISCC geQuaternion_Get( const geQuaternion *Q, \r
+ geFloat *W, geFloat *X, geFloat *Y, geFloat *Z);\r
+ // get quaternion components into W,X,Y,Z\r
+void GENESISCC geQuaternion_GetVec3d( const geQuaternion *Q, geFloat *W, geVec3d *V);\r
+ // get quaternion components into W and V\r
+\r
+void GENESISCC geQuaternion_FromMatrix(\r
+ const geXForm3d *RotationMatrix,\r
+ geQuaternion *QDest);\r
+ // takes upper 3 by 3 portion of matrix (rotation sub matrix) \r
+ // and generates a quaternion\r
+\r
+GENESISAPI void GENESISCC geQuaternion_ToMatrix(\r
+ const geQuaternion *Q, \r
+ geXForm3d *RotationMatrixDest);\r
+ // takes a unit quaternion and makes RotationMatrixDest an equivelant rotation xform.\r
+ // (any translation in RotationMatrixDest will be list)\r
+\r
+void GENESISCC geQuaternion_Slerp(\r
+ const geQuaternion *Q0, \r
+ const geQuaternion *Q1, \r
+ geFloat T, \r
+ geQuaternion *QT);\r
+ // spherical interpolation between q0 and q1. 0<=t<=1 \r
+ // resulting quaternion is 'between' q0 and q1\r
+ // with t==0 being all q0, and t==1 being all q1.\r
+ // returns a quaternion with a positive W - always takes shortest route\r
+ // through the positive W domain.\r
+\r
+void GENESISCC geQuaternion_SlerpNotShortest(\r
+ const geQuaternion *Q0, \r
+ const geQuaternion *Q1, \r
+ geFloat T, \r
+ geQuaternion *QT);\r
+ // spherical interpolation between q0 and q1. 0<=t<=1 \r
+ // resulting quaternion is 'between' q0 and q1\r
+ // with t==0 being all q0, and t==1 being all q1.\r
+\r
+\r
+void GENESISCC geQuaternion_Multiply(\r
+ const geQuaternion *Q1, \r
+ const geQuaternion *Q2, \r
+ geQuaternion *QProduct);\r
+ // multiplies q1 * q2, and places the result in q.\r
+ // no failure. renormalization not automatic\r
+\r
+void GENESISCC geQuaternion_Rotate(\r
+ const geQuaternion *Q, \r
+ const geVec3d *V, \r
+ geVec3d *VRotated);\r
+ // Rotates V by the quaternion Q, places the result in VRotated.\r
+\r
+geBoolean GENESISCC geQuaternion_IsUnit(const geQuaternion *Q);\r
+ // returns GE_TRUE if q is a unit quaternion. GE_FALSE otherwise.\r
+\r
+GENESISAPI geFloat GENESISCC geQuaternion_Normalize(geQuaternion *Q);\r
+ // normalizes q to be a unit quaternion. returns original magnitude of q\r
+\r
+GENESISAPI void GENESISCC geQuaternion_Copy(const geQuaternion *QSrc, geQuaternion *QDst);\r
+ // copies quaternion QSrc into QDst\r
+\r
+void GENESISCC geQuaternion_SetNoRotation(geQuaternion *Q);\r
+ // sets Q to be a quaternion with no rotation (like an identity matrix)\r
+\r
+void GENESISCC geQuaternion_Ln(\r
+ const geQuaternion *Q, \r
+ geQuaternion *LnQ);\r
+ // ln(Q) for unit quaternion only!\r
+\r
+void GENESISCC geQuaternion_Exp(\r
+ const geQuaternion *Q,\r
+ geQuaternion *ExpQ);\r
+ // exp(Q) for pure quaternion only! (zero scalar part (W))\r
+\r
+void GENESISCC geQuaternion_Scale(\r
+ const geQuaternion *Q,\r
+ geFloat Scale,\r
+ geQuaternion *QScaled);\r
+ // Q = Q * Scale (result is not generally a unit quaternion!)\r
+\r
+void GENESISCC geQuaternion_Add(\r
+ const geQuaternion *Q1,\r
+ const geQuaternion *Q2,\r
+ geQuaternion *QSum);\r
+ // QSum = Q1 + Q2 (result is not generally a unit quaternion!)\r
+\r
+void GENESISCC geQuaternion_Subtract(\r
+ const geQuaternion *Q1, \r
+ const geQuaternion *Q2, \r
+ geQuaternion *QSum);\r
+ // QSum = Q1 - Q2 (result is not generally a unit quaternion!)\r
+\r
+void GENESISCC geQuaternion_Inverse(const geQuaternion *Q, geQuaternion *QInv);\r
+ // sets QInv to the inverse of Q. \r
+\r
+geFloat GENESISCC geQuaternion_Magnitude(const geQuaternion *Q);\r
+ // returns Magnitude of Q. \r
+\r
+geBoolean GENESISCC geQuaternion_Compare( geQuaternion *Q1, geQuaternion *Q2, geFloat Tolerance );\r
+ // return GE_TRUE if quaternions differ elementwise by less than Tolerance.\r
+\r
+\r
+#ifndef NDEBUG\r
+void GENESISCC geQuaternion_SetMaximalAssertionMode( geBoolean Enable );\r
+#endif\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+\r
+#endif // GE_QUATERNION_H\r