1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
28/** A quaternion class. It can be used to represent an orientation in 3D space.*/
34 /** Data type of vector components.*/
35 #ifdef OSG_USE_FLOAT_QUAT
36 typedef float value_type;
38 typedef double value_type;
41 /** Number of vector components. */
42 enum { num_components = 4 };
44 value_type _v[4]; // a four-vector
46 inline Quat() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0; _v[3]=1.0; }
48 inline Quat( value_type x, value_type y, value_type z, value_type w )
56 inline Quat( const Vec4f& v )
64 inline Quat( const Vec4d& v )
72 inline Quat( value_type angle, const Vec3f& axis)
74 makeRotate(angle,axis);
76 inline Quat( value_type angle, const Vec3d& axis)
78 makeRotate(angle,axis);
81 inline Quat( value_type angle1, const Vec3f& axis1,
82 value_type angle2, const Vec3f& axis2,
83 value_type angle3, const Vec3f& axis3)
85 makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
88 inline Quat( value_type angle1, const Vec3d& axis1,
89 value_type angle2, const Vec3d& axis2,
90 value_type angle3, const Vec3d& axis3)
92 makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
95 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; }
97 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]; }
99 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]; }
101 inline bool operator < (const Quat& v) const
103 if (_v[0]<v._v[0]) return true;
104 else if (_v[0]>v._v[0]) return false;
105 else if (_v[1]<v._v[1]) return true;
106 else if (_v[1]>v._v[1]) return false;
107 else if (_v[2]<v._v[2]) return true;
108 else if (_v[2]>v._v[2]) return false;
109 else return (_v[3]<v._v[3]);
112 /* ----------------------------------
113 Methods to access data members
114 ---------------------------------- */
116 inline Vec4d asVec4() const
118 return Vec4d(_v[0], _v[1], _v[2], _v[3]);
121 inline Vec3d asVec3() const
123 return Vec3d(_v[0], _v[1], _v[2]);
126 inline void set(value_type x, value_type y, value_type z, value_type w)
134 inline void set(const osg::Vec4f& v)
142 inline void set(const osg::Vec4d& v)
150 void set(const Matrixf& matrix);
152 void set(const Matrixd& matrix);
154 void get(Matrixf& matrix) const;
156 void get(Matrixd& matrix) const;
159 inline value_type & operator [] (int i) { return _v[i]; }
160 inline value_type operator [] (int i) const { return _v[i]; }
162 inline value_type & x() { return _v[0]; }
163 inline value_type & y() { return _v[1]; }
164 inline value_type & z() { return _v[2]; }
165 inline value_type & w() { return _v[3]; }
167 inline value_type x() const { return _v[0]; }
168 inline value_type y() const { return _v[1]; }
169 inline value_type z() const { return _v[2]; }
170 inline value_type w() const { return _v[3]; }
172 /** return true if the Quat represents a zero rotation, and therefore can be ignored in computations.*/
173 bool zeroRotation() const { return _v[0]==0.0 && _v[1]==0.0 && _v[2]==0.0 && _v[3]==1.0; }
176 /* -------------------------------------------------------------
177 BASIC ARITHMETIC METHODS
178 Implemented in terms of Vec4s. Some Vec4 operators, e.g.
179 operator* are not appropriate for quaternions (as
180 mathematical objects) so they are implemented differently.
181 Also define methods for conjugate and the multiplicative inverse.
182 ------------------------------------------------------------- */
183 /// Multiply by scalar
184 inline const Quat operator * (value_type rhs) const
186 return Quat(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
189 /// Unary multiply by scalar
190 inline Quat& operator *= (value_type rhs)
196 return *this; // enable nesting
200 inline const Quat operator*(const Quat& rhs) const
202 return Quat( rhs._v[3]*_v[0] + rhs._v[0]*_v[3] + rhs._v[1]*_v[2] - rhs._v[2]*_v[1],
203 rhs._v[3]*_v[1] - rhs._v[0]*_v[2] + rhs._v[1]*_v[3] + rhs._v[2]*_v[0],
204 rhs._v[3]*_v[2] + rhs._v[0]*_v[1] - rhs._v[1]*_v[0] + rhs._v[2]*_v[3],
205 rhs._v[3]*_v[3] - rhs._v[0]*_v[0] - rhs._v[1]*_v[1] - rhs._v[2]*_v[2] );
209 inline Quat& operator*=(const Quat& rhs)
211 value_type x = rhs._v[3]*_v[0] + rhs._v[0]*_v[3] + rhs._v[1]*_v[2] - rhs._v[2]*_v[1];
212 value_type y = rhs._v[3]*_v[1] - rhs._v[0]*_v[2] + rhs._v[1]*_v[3] + rhs._v[2]*_v[0];
213 value_type z = rhs._v[3]*_v[2] + rhs._v[0]*_v[1] - rhs._v[1]*_v[0] + rhs._v[2]*_v[3];
214 _v[3] = rhs._v[3]*_v[3] - rhs._v[0]*_v[0] - rhs._v[1]*_v[1] - rhs._v[2]*_v[2];
220 return (*this); // enable nesting
224 inline Quat operator / (value_type rhs) const
226 value_type div = 1.0/rhs;
227 return Quat(_v[0]*div, _v[1]*div, _v[2]*div, _v[3]*div);
230 /// Unary divide by scalar
231 inline Quat& operator /= (value_type rhs)
233 value_type div = 1.0/rhs;
242 inline const Quat operator/(const Quat& denom) const
244 return ( (*this) * denom.inverse() );
248 inline Quat& operator/=(const Quat& denom)
250 (*this) = (*this) * denom.inverse();
251 return (*this); // enable nesting
255 inline const Quat operator + (const Quat& rhs) const
257 return Quat(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
258 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
262 inline Quat& operator += (const Quat& rhs)
268 return *this; // enable nesting
271 /// Binary subtraction
272 inline const Quat operator - (const Quat& rhs) const
274 return Quat(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
275 _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
278 /// Unary subtraction
279 inline Quat& operator -= (const Quat& rhs)
285 return *this; // enable nesting
288 /** Negation operator - returns the negative of the quaternion.
289 Basically just calls operator - () on the Vec4 */
290 inline const Quat operator - () const
292 return Quat (-_v[0], -_v[1], -_v[2], -_v[3]);
295 /// Length of the quaternion = sqrt( vec . vec )
296 value_type length() const
298 return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
301 /// Length of the quaternion = vec . vec
302 value_type length2() const
304 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
308 inline Quat conj () const
310 return Quat( -_v[0], -_v[1], -_v[2], _v[3] );
313 /// Multiplicative inverse method: q^(-1) = q^*/(q.q^*)
314 inline const Quat inverse () const
316 return conj() / length2();
319 /* --------------------------------------------------------
320 METHODS RELATED TO ROTATIONS
321 Set a quaternion which will perform a rotation of an
322 angle around the axis given by the vector (x,y,z).
323 Should be written to also accept an angle and a Vec3?
325 Define Spherical Linear interpolation method also
327 Not inlined - see the Quat.cpp file for implementation
328 -------------------------------------------------------- */
329 void makeRotate( value_type angle,
330 value_type x, value_type y, value_type z );
331 void makeRotate ( value_type angle, const Vec3f& vec );
332 void makeRotate ( value_type angle, const Vec3d& vec );
334 void makeRotate ( value_type angle1, const Vec3f& axis1,
335 value_type angle2, const Vec3f& axis2,
336 value_type angle3, const Vec3f& axis3);
337 void makeRotate ( value_type angle1, const Vec3d& axis1,
338 value_type angle2, const Vec3d& axis2,
339 value_type angle3, const Vec3d& axis3);
341 /** Make a rotation Quat which will rotate vec1 to vec2.
342 Generally take a dot product to get the angle between these
343 and then use a cross product to get the rotation axis
344 Watch out for the two special cases when the vectors
345 are co-incident or opposite in direction.*/
346 void makeRotate( const Vec3f& vec1, const Vec3f& vec2 );
347 /** Make a rotation Quat which will rotate vec1 to vec2.
348 Generally take a dot product to get the angle between these
349 and then use a cross product to get the rotation axis
350 Watch out for the two special cases of when the vectors
351 are co-incident or opposite in direction.*/
352 void makeRotate( const Vec3d& vec1, const Vec3d& vec2 );
354 void makeRotate_original( const Vec3d& vec1, const Vec3d& vec2 );
356 /** Return the angle and vector components represented by the quaternion.*/
357 void getRotate ( value_type & angle, value_type & x, value_type & y, value_type & z ) const;
359 /** Return the angle and vector represented by the quaternion.*/
360 void getRotate ( value_type & angle, Vec3f& vec ) const;
362 /** Return the angle and vector represented by the quaternion.*/
363 void getRotate ( value_type & angle, Vec3d& vec ) const;
365 /** Spherical Linear Interpolation.
366 As t goes from 0 to 1, the Quat object goes from "from" to "to". */
367 void slerp ( value_type t, const Quat& from, const Quat& to);
369 /** Rotate a vector by this quaternion.*/
370 Vec3f operator* (const Vec3f& v) const
372 // nVidia SDK implementation
374 Vec3f qvec(_v[0], _v[1], _v[2]);
377 uv *= ( 2.0f * _v[3] );
382 /** Rotate a vector by this quaternion.*/
383 Vec3d operator* (const Vec3d& v) const
385 // nVidia SDK implementation
387 Vec3d qvec(_v[0], _v[1], _v[2]);
390 uv *= ( 2.0f * _v[3] );
397}; // end of class prototype