OpenSceneGraph 3.6.5
Quat
Go to the documentation of this file.
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
14#ifndef OSG_QUAT
15#define OSG_QUAT 1
16
17#include <osg/Export>
18#include <osg/Vec3f>
19#include <osg/Vec4f>
20#include <osg/Vec3d>
21#include <osg/Vec4d>
22
23namespace osg {
24
25class Matrixf;
26class Matrixd;
27
28/** A quaternion class. It can be used to represent an orientation in 3D space.*/
29class OSG_EXPORT Quat
30{
31
32 public:
33
34 /** Data type of vector components.*/
35 #ifdef OSG_USE_FLOAT_QUAT
36 typedef float value_type;
37 #else
38 typedef double value_type;
39 #endif
40
41 /** Number of vector components. */
42 enum { num_components = 4 };
43
44 value_type _v[4]; // a four-vector
45
46 inline Quat() { _v[0]=0.0; _v[1]=0.0; _v[2]=0.0; _v[3]=1.0; }
47
48 inline Quat( value_type x, value_type y, value_type z, value_type w )
49 {
50 _v[0]=x;
51 _v[1]=y;
52 _v[2]=z;
53 _v[3]=w;
54 }
55
56 inline Quat( const Vec4f& v )
57 {
58 _v[0]=v.x();
59 _v[1]=v.y();
60 _v[2]=v.z();
61 _v[3]=v.w();
62 }
63
64 inline Quat( const Vec4d& v )
65 {
66 _v[0]=v.x();
67 _v[1]=v.y();
68 _v[2]=v.z();
69 _v[3]=v.w();
70 }
71
72 inline Quat( value_type angle, const Vec3f& axis)
73 {
74 makeRotate(angle,axis);
75 }
76 inline Quat( value_type angle, const Vec3d& axis)
77 {
78 makeRotate(angle,axis);
79 }
80
81 inline Quat( value_type angle1, const Vec3f& axis1,
82 value_type angle2, const Vec3f& axis2,
83 value_type angle3, const Vec3f& axis3)
84 {
85 makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
86 }
87
88 inline Quat( value_type angle1, const Vec3d& axis1,
89 value_type angle2, const Vec3d& axis2,
90 value_type angle3, const Vec3d& axis3)
91 {
92 makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
93 }
94
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; }
96
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]; }
98
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]; }
100
101 inline bool operator < (const Quat& v) const
102 {
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]);
110 }
111
112 /* ----------------------------------
113 Methods to access data members
114 ---------------------------------- */
115
116 inline Vec4d asVec4() const
117 {
118 return Vec4d(_v[0], _v[1], _v[2], _v[3]);
119 }
120
121 inline Vec3d asVec3() const
122 {
123 return Vec3d(_v[0], _v[1], _v[2]);
124 }
125
126 inline void set(value_type x, value_type y, value_type z, value_type w)
127 {
128 _v[0]=x;
129 _v[1]=y;
130 _v[2]=z;
131 _v[3]=w;
132 }
133
134 inline void set(const osg::Vec4f& v)
135 {
136 _v[0]=v.x();
137 _v[1]=v.y();
138 _v[2]=v.z();
139 _v[3]=v.w();
140 }
141
142 inline void set(const osg::Vec4d& v)
143 {
144 _v[0]=v.x();
145 _v[1]=v.y();
146 _v[2]=v.z();
147 _v[3]=v.w();
148 }
149
150 void set(const Matrixf& matrix);
151
152 void set(const Matrixd& matrix);
153
154 void get(Matrixf& matrix) const;
155
156 void get(Matrixd& matrix) const;
157
158
159 inline value_type & operator [] (int i) { return _v[i]; }
160 inline value_type operator [] (int i) const { return _v[i]; }
161
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]; }
166
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]; }
171
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; }
174
175
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
185 {
186 return Quat(_v[0]*rhs, _v[1]*rhs, _v[2]*rhs, _v[3]*rhs);
187 }
188
189 /// Unary multiply by scalar
190 inline Quat& operator *= (value_type rhs)
191 {
192 _v[0]*=rhs;
193 _v[1]*=rhs;
194 _v[2]*=rhs;
195 _v[3]*=rhs;
196 return *this; // enable nesting
197 }
198
199 /// Binary multiply
200 inline const Quat operator*(const Quat& rhs) const
201 {
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] );
206 }
207
208 /// Unary multiply
209 inline Quat& operator*=(const Quat& rhs)
210 {
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];
215
216 _v[2] = z;
217 _v[1] = y;
218 _v[0] = x;
219
220 return (*this); // enable nesting
221 }
222
223 /// Divide by scalar
224 inline Quat operator / (value_type rhs) const
225 {
226 value_type div = 1.0/rhs;
227 return Quat(_v[0]*div, _v[1]*div, _v[2]*div, _v[3]*div);
228 }
229
230 /// Unary divide by scalar
231 inline Quat& operator /= (value_type rhs)
232 {
233 value_type div = 1.0/rhs;
234 _v[0]*=div;
235 _v[1]*=div;
236 _v[2]*=div;
237 _v[3]*=div;
238 return *this;
239 }
240
241 /// Binary divide
242 inline const Quat operator/(const Quat& denom) const
243 {
244 return ( (*this) * denom.inverse() );
245 }
246
247 /// Unary divide
248 inline Quat& operator/=(const Quat& denom)
249 {
250 (*this) = (*this) * denom.inverse();
251 return (*this); // enable nesting
252 }
253
254 /// Binary addition
255 inline const Quat operator + (const Quat& rhs) const
256 {
257 return Quat(_v[0]+rhs._v[0], _v[1]+rhs._v[1],
258 _v[2]+rhs._v[2], _v[3]+rhs._v[3]);
259 }
260
261 /// Unary addition
262 inline Quat& operator += (const Quat& rhs)
263 {
264 _v[0] += rhs._v[0];
265 _v[1] += rhs._v[1];
266 _v[2] += rhs._v[2];
267 _v[3] += rhs._v[3];
268 return *this; // enable nesting
269 }
270
271 /// Binary subtraction
272 inline const Quat operator - (const Quat& rhs) const
273 {
274 return Quat(_v[0]-rhs._v[0], _v[1]-rhs._v[1],
275 _v[2]-rhs._v[2], _v[3]-rhs._v[3] );
276 }
277
278 /// Unary subtraction
279 inline Quat& operator -= (const Quat& rhs)
280 {
281 _v[0]-=rhs._v[0];
282 _v[1]-=rhs._v[1];
283 _v[2]-=rhs._v[2];
284 _v[3]-=rhs._v[3];
285 return *this; // enable nesting
286 }
287
288 /** Negation operator - returns the negative of the quaternion.
289 Basically just calls operator - () on the Vec4 */
290 inline const Quat operator - () const
291 {
292 return Quat (-_v[0], -_v[1], -_v[2], -_v[3]);
293 }
294
295 /// Length of the quaternion = sqrt( vec . vec )
296 value_type length() const
297 {
298 return sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]);
299 }
300
301 /// Length of the quaternion = vec . vec
302 value_type length2() const
303 {
304 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
305 }
306
307 /// Conjugate
308 inline Quat conj () const
309 {
310 return Quat( -_v[0], -_v[1], -_v[2], _v[3] );
311 }
312
313 /// Multiplicative inverse method: q^(-1) = q^*/(q.q^*)
314 inline const Quat inverse () const
315 {
316 return conj() / length2();
317 }
318
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?
324
325 Define Spherical Linear interpolation method also
326
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 );
333
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);
340
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 );
353
354 void makeRotate_original( const Vec3d& vec1, const Vec3d& vec2 );
355
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;
358
359 /** Return the angle and vector represented by the quaternion.*/
360 void getRotate ( value_type & angle, Vec3f& vec ) const;
361
362 /** Return the angle and vector represented by the quaternion.*/
363 void getRotate ( value_type & angle, Vec3d& vec ) const;
364
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);
368
369 /** Rotate a vector by this quaternion.*/
370 Vec3f operator* (const Vec3f& v) const
371 {
372 // nVidia SDK implementation
373 Vec3f uv, uuv;
374 Vec3f qvec(_v[0], _v[1], _v[2]);
375 uv = qvec ^ v;
376 uuv = qvec ^ uv;
377 uv *= ( 2.0f * _v[3] );
378 uuv *= 2.0f;
379 return v + uv + uuv;
380 }
381
382 /** Rotate a vector by this quaternion.*/
383 Vec3d operator* (const Vec3d& v) const
384 {
385 // nVidia SDK implementation
386 Vec3d uv, uuv;
387 Vec3d qvec(_v[0], _v[1], _v[2]);
388 uv = qvec ^ v;
389 uuv = qvec ^ uv;
390 uv *= ( 2.0f * _v[3] );
391 uuv *= 2.0f;
392 return v + uv + uuv;
393 }
394
395 protected:
396
397}; // end of class prototype
398
399} // end of namespace
400
401#endif

osg logo
Generated at Tue Jun 11 2024 00:00:00 for the OpenSceneGraph by doxygen 1.11.0.