csgeom/quaternion.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2000 by Norman Kramer 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_QUATERNION_H__ 00020 #define __CS_QUATERNION_H__ 00021 00029 #include "csextern.h" 00030 #include "csqsqrt.h" 00031 00032 #include "csgeom/vector3.h" 00033 00034 class csMatrix3; 00035 00039 class CS_CRYSTALSPACE_EXPORT csQuaternion 00040 { 00041 public: 00043 inline void Init (float theR, float theX, float theY, float theZ) 00044 { r = theR; x = theX; y = theY; z = theZ; } 00045 00047 csQuaternion () { Init(0, 0, 0, 0 ); } 00049 csQuaternion (float theR, float theX=0.0, float theY=0.0, float theZ=0.0) 00050 { Init (theR, theX, theY, theZ ); } 00052 csQuaternion (const csQuaternion& q) { Init (q.r, q.x, q.y, q.z); } 00054 csQuaternion (const csVector3& q) { Init (0, q.x, q.y, q.z); } 00055 00057 csQuaternion (const csMatrix3& smat); 00058 00060 inline friend csQuaternion operator+ (const csQuaternion& q1, 00061 const csQuaternion& q2) 00062 { 00063 return csQuaternion (q1.r + q2.r, q1.x + q2.x, q1.y + q2.y, q1.z + q2.z ); 00064 } 00065 00067 inline friend csQuaternion operator- (const csQuaternion& q1, 00068 const csQuaternion& q2) 00069 { 00070 return csQuaternion (q1.r - q2.r, q1.x - q2.x, q1.y - q2.y, q1.z - q2.z ); 00071 } 00072 00074 inline friend csQuaternion operator* (const csQuaternion& q1, 00075 const csQuaternion& q2) 00076 { 00077 return csQuaternion (q1.r*q2.r - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z, 00078 q1.y*q2.z - q1.z*q2.y + q1.r*q2.x + q1.x*q2.r, 00079 q1.z*q2.x - q1.x*q2.z + q1.r*q2.y + q1.y*q2.r, 00080 q1.x*q2.y - q1.y*q2.x + q1.r*q2.z + q1.z*q2.r); 00081 } 00082 00084 csQuaternion& operator*= (const csQuaternion& q2) 00085 { 00086 Init (r*q2.r - x*q2.x - y*q2.y - z*q2.z, 00087 y*q2.z - z*q2.y + r*q2.x + x*q2.r, 00088 z*q2.x - x*q2.z + r*q2.y + y*q2.r, 00089 x*q2.y - y*q2.x + r*q2.z + z*q2.r); 00090 return *this; 00091 } 00092 00094 void Conjugate () { Init (r, -x, -y, -z); } 00095 00097 void Negate () { Init(-r, -x, -y, -z); } 00098 00100 void Invert(); 00101 00107 void GetAxisAngle(csVector3& axis, float& phi) const; 00108 00114 void SetWithAxisAngle(csVector3 axis, float phi); 00115 00121 void PrepRotation (float angle, csVector3 vec) 00122 { 00123 double theSin = sin (angle / 2.0f); 00124 Init ((float) cos (angle / 2.0f), vec.x * theSin, vec.y * theSin, 00125 vec.z * theSin); 00126 } 00127 00129 csVector3 Rotate (csVector3 vec) 00130 { 00131 csQuaternion p (vec); 00132 csQuaternion qConj (r, -x, -y, -z); 00133 00134 p = *this * p; 00135 p *= qConj; 00136 return csVector3 (p.x, p.y, p.z); 00137 } 00138 00140 void Normalize () 00141 { 00142 float dist, square; 00143 square = x * x + y * y + z * z + r * r; 00144 00145 if (square > 0.0) dist = (float)csQisqrt(square); 00146 else dist = 1; 00147 00148 x *= dist; 00149 y *= dist; 00150 z *= dist; 00151 r *= dist; 00152 00153 /*if(x*x + y*y + z*z > .999) 00154 { 00155 // Severe problems... 00156 float inverselen = 1.0f / (x*x + y*y + z*z); 00157 x *= inverselen; 00158 y *= inverselen; 00159 z *= inverselen; 00160 if(r > 0) r = -1 + r; 00161 else r = 1 + r; 00162 } 00163 else 00164 { 00165 r = csQsqrt(1.0f - x*x - y*y - z*z); 00166 }*/ 00167 } 00168 00174 void SetWithEuler (const csVector3 &rot); 00175 00180 void GetEulerAngles (csVector3& angles, bool radians = false); 00181 00185 csQuaternion ToAxisAngle () const; 00186 00192 csQuaternion Slerp (const csQuaternion &quat2, float slerp) const; 00193 00194 //csQuaternion Lerp(const csQuaternion& quat2, float ratio) const; 00195 00196 float r,x,y,z; 00197 }; 00198 00201 #endif // __CS_QUATERNION_H__ 00202
Generated for Crystal Space by doxygen 1.4.6