Quat 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_QUAT
  14. #define OSG_QUAT 1
  15. #include <osg/Export>
  16. #include <osg/Vec3f>
  17. #include <osg/Vec4f>
  18. #include <osg/Vec3d>
  19. #include <osg/Vec4d>
  20. namespace osg {
  21. class Matrixf;
  22. class Matrixd;
  23. /** A quaternion class. It can be used to represent an orientation in 3D space.*/
  24. class OSG_EXPORT Quat
  25. {
  26. public:
  27. /** Data type of vector components.*/
  28. #ifdef OSG_USE_FLOAT_QUAT
  29. typedef float value_type;
  30. #else
  31. typedef double value_type;
  32. #endif
  33. /** Number of vector components. */
  34. enum { num_components = 4 };
  35. value_type _v[4]; // a four-vector
  36. inline Quat() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0; _v[3]=1.0; }
  37. inline Quat( value_type x, value_type y, value_type z, value_type w )
  38. {
  39. _v[0]=x;
  40. _v[1]=y;
  41. _v[2]=z;
  42. _v[3]=w;
  43. }
  44. inline Quat( const Vec4f& v )
  45. {
  46. _v[0]=v.x();
  47. _v[1]=v.y();
  48. _v[2]=v.z();
  49. _v[3]=v.w();
  50. }
  51. inline Quat( const Vec4d& v )
  52. {
  53. _v[0]=v.x();
  54. _v[1]=v.y();
  55. _v[2]=v.z();
  56. _v[3]=v.w();
  57. }
  58. inline Quat( value_type angle, const Vec3f& axis)
  59. {
  60. makeRotate(angle,axis);
  61. }
  62. inline Quat( value_type angle, const Vec3d& axis)
  63. {
  64. makeRotate(angle,axis);
  65. }
  66. inline Quat( value_type angle1, const Vec3f& axis1,
  67. value_type angle2, const Vec3f& axis2,
  68. value_type angle3, const Vec3f& axis3)
  69. {
  70. makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
  71. }
  72. inline Quat( value_type angle1, const Vec3d& axis1,
  73. value_type angle2, const Vec3d& axis2,
  74. value_type angle3, const Vec3d& axis3)
  75. {
  76. makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
  77. }
  78. inline Quat& operator = (const Quat& v) { _v[0]=v._v[0]; _v[1]=v._v[1]; _v[2]=v._v[2]; _v[3]=v._v[3]; return *this; }
  79. inline bool operator == (const Quat& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1] && _v[2]==v._v[2] && _v[3]==v._v[3]; }
  80. inline bool operator != (const Quat& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1] || _v[2]!=v._v[2] || _v[3]!=v._v[3]; }
  81. inline bool operator < (const Quat& v) const
  82. {
  83. if (_v[0]<v._v[0]) return true;
  84. else if (_v[0]>v._v[0]) return false;
  85. else if (_v[1]<v._v[1]) return true;
  86. else if (_v[1]>v._v[1]) return false;
  87. else if (_v[2]<v._v[2]) return true;
  88. else if (_v[2]>v._v[2]) return false;
  89. else return (_v[3]<v._v[3]);
  90. }
  91. /* ----------------------------------
  92. Methods to access data members
  93. ---------------------------------- */
  94. inline Vec4d asVec4() const
  95. {
  96. return Vec4d(_v[0], _v[1], _v[2], _v[3]);
  97. }
  98. inline Vec3d asVec3() const
  99. {
  100. return Vec3d(_v[0], _v[1], _v[2]);
  101. }
  102. inline void set(value_type x, value_type y, value_type z, value_type w)
  103. {
  104. _v[0]=x;
  105. _v[1]=y;
  106. _v[2]=z;
  107. _v[3]=w;
  108. }
  109. inline void set(const osg::Vec4f& v)
  110. {
  111. _v[0]=v.x();
  112. _v[1]=v.y();
  113. _v[2]=v.z();
  114. _v[3]=v.w();
  115. }
  116. inline void set(const osg::Vec4d& v)
  117. {
  118. _v[0]=v.x();
  119. _v[1]=v.y();
  120. _v[2]=v.z();
  121. _v[3]=v.w();
  122. }
  123. void set(const Matrixf& matrix);
  124. void set(const Matrixd& matrix);
  125. void get(Matrixf& matrix) const;
  126. void get(Matrixd& matrix) const;
  127. inline value_type & operator [] (int i) { return _v[i]; }
  128. inline value_type operator [] (int i) const { return _v[i]; }
  129. inline value_type & x() { return _v[0]; }
  130. inline value_type & y() { return _v[1]; }
  131. inline value_type & z() { return _v[2]; }
  132. inline value_type & w() { return _v[3]; }
  133. inline value_type x() const { return _v[0]; }
  134. inline value_type y() const { return _v[1]; }
  135. inline value_type z() const { return _v[2]; }
  136. inline value_type w() const { return _v[3]; }
  137. /** return true if the Quat represents a zero rotation, and therefore can be ignored in computations.*/
  138. bool zeroRotation() const { return _v[0]==0.0 && _v[1]==0.0 && _v[2]==0.0 && _v[3]==1.0; }
  139. /* -------------------------------------------------------------
  140. BASIC ARITHMETIC METHODS
  141. Implemented in terms of Vec4s. Some Vec4 operators, e.g.
  142. operator* are not appropriate for quaternions (as
  143. mathematical objects) so they are implemented differently.
  144. Also define methods for conjugate and the multiplicative inverse.
  145. ------------------------------------------------------------- */
  146. /// Multiply by scalar
  147. inline const Quat operator * (value_type rhs) const
  148. {
  149. return Quat(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
  150. }
  151. /// Unary multiply by scalar
  152. inline Quat& operator *= (value_type rhs)
  153. {
  154. _v[0]*=rhs;
  155. _v[1]*=rhs;
  156. _v[2]*=rhs;
  157. _v[3]*=rhs;
  158. return *this; // enable nesting
  159. }
  160. /// Binary multiply
  161. inline const Quat operator*(const Quat& rhs) const
  162. {
  163. return Quat( rhs._v[3]*_v[0] + rhs._v[0]*_v[3] + rhs._v[1]*_v[2] - rhs._v[2]*_v[1],
  164. rhs._v[3]*_v[1] - rhs._v[0]*_v[2] + rhs._v[1]*_v[3] + rhs._v[2]*_v[0],
  165. rhs._v[3]*_v[2] + rhs._v[0]*_v[1] - rhs._v[1]*_v[0] + rhs._v[2]*_v[3],
  166. rhs._v[3]*_v[3] - rhs._v[0]*_v[0] - rhs._v[1]*_v[1] - rhs._v[2]*_v[2] );
  167. }
  168. /// Unary multiply
  169. inline Quat& operator*=(const Quat& rhs)
  170. {
  171. value_type x = rhs._v[3]*_v[0] + rhs._v[0]*_v[3] + rhs._v[1]*_v[2] - rhs._v[2]*_v[1];
  172. value_type y = rhs._v[3]*_v[1] - rhs._v[0]*_v[2] + rhs._v[1]*_v[3] + rhs._v[2]*_v[0];
  173. value_type z = rhs._v[3]*_v[2] + rhs._v[0]*_v[1] - rhs._v[1]*_v[0] + rhs._v[2]*_v[3];
  174. _v[3] = rhs._v[3]*_v[3] - rhs._v[0]*_v[0] - rhs._v[1]*_v[1] - rhs._v[2]*_v[2];
  175. _v[2] = z;
  176. _v[1] = y;
  177. _v[0] = x;
  178. return (*this); // enable nesting
  179. }
  180. /// Divide by scalar
  181. inline Quat operator / (value_type rhs) const
  182. {
  183. value_type div = 1.0/rhs;
  184. return Quat(_v[0]*div, _v[1]*div, _v[2]*div, _v[3]*div);
  185. }
  186. /// Unary divide by scalar
  187. inline Quat& operator /= (value_type rhs)
  188. {
  189. value_type div = 1.0/rhs;
  190. _v[0]*=div;
  191. _v[1]*=div;
  192. _v[2]*=div;
  193. _v[3]*=div;
  194. return *this;
  195. }
  196. /// Binary divide
  197. inline const Quat operator/(const Quat& denom) const
  198. {
  199. return ( (*this) * denom.inverse() );
  200. }
  201. /// Unary divide
  202. inline Quat& operator/=(const Quat& denom)
  203. {
  204. (*this) = (*this) * denom.inverse();
  205. return (*this); // enable nesting
  206. }
  207. /// Binary addition
  208. inline const Quat operator + (const Quat& rhs) const
  209. {
  210. return Quat(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
  211. _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
  212. }
  213. /// Unary addition
  214. inline Quat& operator += (const Quat& rhs)
  215. {
  216. _v[0] += rhs._v[0];
  217. _v[1] += rhs._v[1];
  218. _v[2] += rhs._v[2];
  219. _v[3] += rhs._v[3];
  220. return *this; // enable nesting
  221. }
  222. /// Binary subtraction
  223. inline const Quat operator - (const Quat& rhs) const
  224. {
  225. return Quat(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
  226. _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
  227. }
  228. /// Unary subtraction
  229. inline Quat& operator -= (const Quat& rhs)
  230. {
  231. _v[0]-=rhs._v[0];
  232. _v[1]-=rhs._v[1];
  233. _v[2]-=rhs._v[2];
  234. _v[3]-=rhs._v[3];
  235. return *this; // enable nesting
  236. }
  237. /** Negation operator - returns the negative of the quaternion.
  238. Basically just calls operator - () on the Vec4 */
  239. inline const Quat operator - () const
  240. {
  241. return Quat (-_v[0], -_v[1], -_v[2], -_v[3]);
  242. }
  243. /// Length of the quaternion = sqrt( vec . vec )
  244. value_type length() const
  245. {
  246. return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
  247. }
  248. /// Length of the quaternion = vec . vec
  249. value_type length2() const
  250. {
  251. return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
  252. }
  253. /// Conjugate
  254. inline Quat conj () const
  255. {
  256. return Quat( -_v[0], -_v[1], -_v[2], _v[3] );
  257. }
  258. /// Multiplicative inverse method: q^(-1) = q^*/(q.q^*)
  259. inline const Quat inverse () const
  260. {
  261. return conj() / length2();
  262. }
  263. /* --------------------------------------------------------
  264. METHODS RELATED TO ROTATIONS
  265. Set a quaternion which will perform a rotation of an
  266. angle around the axis given by the vector (x,y,z).
  267. Should be written to also accept an angle and a Vec3?
  268. Define Spherical Linear interpolation method also
  269. Not inlined - see the Quat.cpp file for implementation
  270. -------------------------------------------------------- */
  271. void makeRotate( value_type angle,
  272. value_type x, value_type y, value_type z );
  273. void makeRotate ( value_type angle, const Vec3f& vec );
  274. void makeRotate ( value_type angle, const Vec3d& vec );
  275. void makeRotate ( value_type angle1, const Vec3f& axis1,
  276. value_type angle2, const Vec3f& axis2,
  277. value_type angle3, const Vec3f& axis3);
  278. void makeRotate ( value_type angle1, const Vec3d& axis1,
  279. value_type angle2, const Vec3d& axis2,
  280. value_type angle3, const Vec3d& axis3);
  281. /** Make a rotation Quat which will rotate vec1 to vec2.
  282. Generally take a dot product to get the angle between these
  283. and then use a cross product to get the rotation axis
  284. Watch out for the two special cases when the vectors
  285. are co-incident or opposite in direction.*/
  286. void makeRotate( const Vec3f& vec1, const Vec3f& vec2 );
  287. /** Make a rotation Quat which will rotate vec1 to vec2.
  288. Generally take a dot product to get the angle between these
  289. and then use a cross product to get the rotation axis
  290. Watch out for the two special cases of when the vectors
  291. are co-incident or opposite in direction.*/
  292. void makeRotate( const Vec3d& vec1, const Vec3d& vec2 );
  293. void makeRotate_original( const Vec3d& vec1, const Vec3d& vec2 );
  294. /** Return the angle and vector components represented by the quaternion.*/
  295. void getRotate ( value_type & angle, value_type & x, value_type & y, value_type & z ) const;
  296. /** Return the angle and vector represented by the quaternion.*/
  297. void getRotate ( value_type & angle, Vec3f& vec ) const;
  298. /** Return the angle and vector represented by the quaternion.*/
  299. void getRotate ( value_type & angle, Vec3d& vec ) const;
  300. /** Spherical Linear Interpolation.
  301. As t goes from 0 to 1, the Quat object goes from "from" to "to". */
  302. void slerp ( value_type t, const Quat& from, const Quat& to);
  303. /** Rotate a vector by this quaternion.*/
  304. Vec3f operator* (const Vec3f& v) const
  305. {
  306. // nVidia SDK implementation
  307. Vec3f uv, uuv;
  308. Vec3f qvec(_v[0], _v[1], _v[2]);
  309. uv = qvec ^ v;
  310. uuv = qvec ^ uv;
  311. uv *= ( 2.0f * _v[3] );
  312. uuv *= 2.0f;
  313. return v + uv + uuv;
  314. }
  315. /** Rotate a vector by this quaternion.*/
  316. Vec3d operator* (const Vec3d& v) const
  317. {
  318. // nVidia SDK implementation
  319. Vec3d uv, uuv;
  320. Vec3d qvec(_v[0], _v[1], _v[2]);
  321. uv = qvec ^ v;
  322. uuv = qvec ^ uv;
  323. uv *= ( 2.0f * _v[3] );
  324. uuv *= 2.0f;
  325. return v + uv + uuv;
  326. }
  327. protected:
  328. }; // end of class prototype
  329. } // end of namespace
  330. #endif