reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofQuaternion.h
Go to the documentation of this file.
1/*
2 * ofQuaternion.h
3 *
4 * Created by Aaron Meyers on 6/22/09 -- modified by Arturo Castro, Zach Lieberman, Memo Akten
5 * based on code from OSG -
6 * see OSG license for more details:
7 * http://www.openscenegraph.org/projects/osg/wiki/Legal
8 *
9 */
10
11#pragma once
12
13#include "ofVec3f.h"
14#include "ofVec4f.h"
15#include "ofConstants.h"
16#include <cmath>
17
18#if (_MSC_VER)
19// make microsoft visual studio complain less about double / float conversion.
20#pragma warning(disable : 4244)
21#endif
22
23
24class ofMatrix4x4;
25
26
28public:
29 // float _v[4];
31 ofVec4f _v;
33
34
35
36 //---------------------
39
40 inline ofQuaternion();
41 inline ofQuaternion(float x, float y, float z, float w);
42 inline ofQuaternion(const ofVec4f& v);
43 inline ofQuaternion(float angle, const ofVec3f& axis);
44
45 // rotation order is axis3,axis2,axis1
46 inline ofQuaternion(float angle1, const ofVec3f& axis1, float angle2, const ofVec3f& axis2, float angle3, const ofVec3f& axis3);
47
48 ofQuaternion(const glm::quat & q);
49 operator glm::quat() const;
50
52
53 //---------------------
56
57 inline void set(float x, float y, float z, float w);
58 inline void set(const ofVec4f& v);
59
60 void set(const ofMatrix4x4& matrix);
61
62
64
65 //---------------------
68
69
70 inline float& operator [](int i);
71 inline float operator [](int i) const;
72
73 void get(ofMatrix4x4& matrix) const;
74
75
76 inline float& x();
77 inline float& y();
78 inline float& z();
79 inline float& w();
80
81 inline float x() const;
82 inline float y() const;
83 inline float z() const;
84 inline float w() const;
85
86 inline ofVec4f asVec4() const;
87 inline ofVec3f asVec3() const;
88
89
91
92 //---------------------
95
96
99 inline bool zeroRotation() const;
100
101
103 inline float length() const;
104
106 inline float length2() const;
107
109 inline ofQuaternion conj() const;
110
114 inline const ofQuaternion inverse() const;
115
116
117
122 void makeRotate(float angle, float x, float y, float z);
123 void makeRotate(float angle, const ofVec3f& vec);
124 void makeRotate(float angle1, const ofVec3f& axis1, float angle2, const ofVec3f& axis2, float angle3, const ofVec3f& axis3);
125
126
132 void makeRotate(const ofVec3f& vec1, const ofVec3f& vec2);
133
134 void makeRotate_original(const ofVec3f& vec1, const ofVec3f& vec2);
135
137 void getRotate(float&angle, float& x, float& y, float& z) const;
138 void getRotate(float& angle, ofVec3f& vec) const;
139
141 ofVec3f getEuler() const;
142
143
147 void slerp(float t, const ofQuaternion& from, const ofQuaternion& to);
148
149 inline void normalize();
150
152
153 //---------------------
156
157
158 // Implemented in terms of Vec4s. Some Vec4 operators, e.g.
159 // operator* are not appropriate for quaternions (as
160 // mathematical objects) so they are implemented differently.
161 // Also define methods for conjugate and the multiplicative inverse.
162
163 inline ofQuaternion& operator =(const ofQuaternion& q);
164 inline bool operator ==(const ofQuaternion& q) const;
165 inline bool operator !=(const ofQuaternion& q) const;
166 // inline bool operator <(const ofQuaternion& q) const; // why?
167
168
169 inline const ofQuaternion operator *(float rhs) const;
170 inline const ofQuaternion operator*(const ofQuaternion& rhs) const;
171 inline ofVec3f operator*(const ofVec3f& v) const;
172 inline ofQuaternion& operator *=(float rhs);
173 inline ofQuaternion& operator*=(const ofQuaternion& rhs);
174 inline ofQuaternion operator /(float rhs) const;
175 inline const ofQuaternion operator/(const ofQuaternion& denom) const;
176 inline ofQuaternion& operator /=(float rhs);
177 inline ofQuaternion& operator/=(const ofQuaternion& denom);
178 inline const ofQuaternion operator +(const ofQuaternion& rhs) const;
179 inline ofQuaternion& operator +=(const ofQuaternion& rhs);
180 inline ofQuaternion& operator -=(const ofQuaternion& rhs);
181 inline const ofQuaternion operator -(const ofQuaternion& rhs) const;
182 inline const ofQuaternion operator -() const;
183
184 friend std::ostream& operator<<(std::ostream& os, const ofQuaternion &q);
185 friend std::istream& operator>>(std::istream& is, ofQuaternion &q);
186
188};
189
190// ----------------------------------------------------------------
191// IMPLEMENTATION
192// ----------------------------------------------------------------
193
194
195
196//----------------------------------------
198 _v.set(0, 0, 0, 1);
199}
200
201
202//----------------------------------------
203ofQuaternion::ofQuaternion(float x, float y, float z, float w) {
204 _v.set(x, y, z, w);
205}
206
207
208//----------------------------------------
210 _v = v;
211}
212
213
214//----------------------------------------
215ofQuaternion::ofQuaternion(float angle, const ofVec3f& axis) {
216 makeRotate(angle, axis);
217}
218
219
220//----------------------------------------
221ofQuaternion::ofQuaternion(float angle1, const ofVec3f& axis1, float angle2, const ofVec3f& axis2, float angle3, const ofVec3f& axis3) {
222 makeRotate(angle1, axis1, angle2, axis2, angle3, axis3);
223}
224
225//----------------------------------------
227 _v = q._v;
228 return *this;
229}
230
231
232//----------------------------------------
234 return _v == q._v;
235}
236
237
238//----------------------------------------
240 return _v != q._v;
241}
242
243
244//----------------------------------------
245//bool ofQuaternion::operator <(const ofQuaternion& q) const {
246// if(_v.x < v._v.x) return true;
247// else if(_v.x > v._v.x) return false;
248// else if(_v.y < v._v.y) return true;
249// else if(_v.y > v._v.y) return false;
250// else if(_v.z < v._v.z) return true;
251// else if(_v.z > v._v.z) return false;
252// else return (_v.w < v._v.w);
253//}
254
255
256
257//----------------------------------------
259 return _v;
260}
261
262
263//----------------------------------------
265 return ofVec3f(_v);
266}
267
268
269//----------------------------------------
270void ofQuaternion::set(float x, float y, float z, float w) {
271 _v.set(x, y, z, w);
272}
273
274
275//----------------------------------------
277 _v = v;
278}
279
280
281//----------------------------------------
283 return _v[i];
284}
285
286
287
288//----------------------------------------
289float ofQuaternion::operator [](int i) const {
290 return _v[i];
291}
292
293
294//----------------------------------------
296 return _v.x;
297}
298
299
300//----------------------------------------
302 return _v.y;
303}
304
305
306//----------------------------------------
308 return _v.z;
309}
310
311
312//----------------------------------------
314 return _v.w;
315}
316
317
318//----------------------------------------
319float ofQuaternion::x() const {
320 return _v.x;
321}
322
323
324//----------------------------------------
325float ofQuaternion::y() const {
326 return _v.y;
327}
328
329
330//----------------------------------------
331float ofQuaternion::z() const {
332 return _v.z;
333}
334
335
336//----------------------------------------
337float ofQuaternion::w() const {
338 return _v.w;
339}
340
341
342//----------------------------------------
344 return _v.x == 0.0 && _v.y == 0.0 && _v.z == 0.0 && _v.w == 1.0;
345}
346
347
348
349//----------------------------------------
351 return ofQuaternion(_v.x*rhs, _v.y*rhs, _v.z*rhs, _v.w*rhs);
352}
353
354
355//----------------------------------------
357 _v.x *= rhs;
358 _v.y *= rhs;
359 _v.z *= rhs;
360 _v.w *= rhs;
361 return *this; // enable nesting
362}
363
364
365//----------------------------------------
367 return ofQuaternion(rhs._v.w*_v.x + rhs._v.x*_v.w + rhs._v.y*_v.z - rhs._v.z*_v.y,
368 rhs._v.w*_v.y - rhs._v.x*_v.z + rhs._v.y*_v.w + rhs._v.z*_v.x,
369 rhs._v.w*_v.z + rhs._v.x*_v.y - rhs._v.y*_v.x + rhs._v.z*_v.w,
370 rhs._v.w*_v.w - rhs._v.x*_v.x - rhs._v.y*_v.y - rhs._v.z*_v.z);
371}
372
373
374//----------------------------------------
376 float x = rhs._v.w * _v.x + rhs._v.x * _v.w + rhs._v.y * _v.z - rhs._v.z * _v.y;
377 float y = rhs._v.w * _v.y - rhs._v.x * _v.z + rhs._v.y * _v.w + rhs._v.z * _v.x;
378 float z = rhs._v.w * _v.z + rhs._v.x * _v.y - rhs._v.y * _v.x + rhs._v.z * _v.w;
379 _v.w = rhs._v.w * _v.w - rhs._v.x * _v.x - rhs._v.y * _v.y - rhs._v.z * _v.z;
380
381 _v.z = z;
382 _v.y = y;
383 _v.x = x;
384
385 return (*this); // enable nesting
386}
387
388
389//----------------------------------------
391 float div = 1.0f / rhs;
392 return ofQuaternion(_v.x*div, _v.y*div, _v.z*div, _v.w*div);
393}
394
395
396//----------------------------------------
398 float div = 1.0f / rhs;
399 _v.x *= div;
400 _v.y *= div;
401 _v.z *= div;
402 _v.w *= div;
403 return *this;
404}
405
406
407//----------------------------------------
409 return ((*this) * denom.inverse());
410}
411
412
413//----------------------------------------
415 (*this) = (*this) * denom.inverse();
416 return (*this); // enable nesting
417}
418
419
420//----------------------------------------
422 return ofQuaternion(_v.x + rhs._v.x, _v.y + rhs._v.y,
423 _v.z + rhs._v.z, _v.w + rhs._v.w);
424}
425
426
427//----------------------------------------
429 _v.x += rhs._v.x;
430 _v.y += rhs._v.y;
431 _v.z += rhs._v.z;
432 _v.w += rhs._v.w;
433 return *this; // enable nesting
434}
435
436
437//----------------------------------------
439 return ofQuaternion(_v.x - rhs._v.x, _v.y - rhs._v.y,
440 _v.z - rhs._v.z, _v.w - rhs._v.w);
441}
442
443
444//----------------------------------------
446 _v.x -= rhs._v.x;
447 _v.y -= rhs._v.y;
448 _v.z -= rhs._v.z;
449 _v.w -= rhs._v.w;
450 return *this; // enable nesting
451}
452
453
454//----------------------------------------
456 return ofQuaternion(-_v.x, -_v.y, -_v.z, -_v.w);
457}
458
459
460//----------------------------------------
461float ofQuaternion::length() const {
462 return sqrt(_v.x*_v.x + _v.y*_v.y + _v.z*_v.z + _v.w*_v.w);
463}
464
465
466//----------------------------------------
468 return _v.x*_v.x + _v.y*_v.y + _v.z*_v.z + _v.w*_v.w;
469}
470
471
472//----------------------------------------
474 return ofQuaternion(-_v.x, -_v.y, -_v.z, _v.w);
475}
476
477
478//----------------------------------------
480 return conj() / length2();
481}
482
483
484
485//----------------------------------------
487 // nVidia SDK implementation
488 ofVec3f uv, uuv;
489 ofVec3f qvec(_v.x, _v.y, _v.z);
490 uv = qvec.getCrossed(v); //uv = qvec ^ v;
491 uuv = qvec.getCrossed(uv); //uuv = qvec ^ uv;
492 uv *= (2.0f * _v.w);
493 uuv *= 2.0f;
494 return v + uv + uuv;
495}
496
498 float len = _v.w*_v.w + _v.x*_v.x + _v.y*_v.y + _v.z*_v.z;
499 float factor = 1.0f / sqrt(len);
500 _v.x *= factor;
501 _v.y *= factor;
502 _v.z *= factor;
503 _v.w *= factor;
504}
The ofMatrix4x4 is the big class of the math part of openFrameworks.
Definition ofMatrix4x4.h:58
Definition ofQuaternion.h:27
bool operator!=(const ofQuaternion &q) const
Definition ofQuaternion.h:239
bool zeroRotation() const
return true if the Quat represents a zero rotation, and therefore can be ignored in computations.
Definition ofQuaternion.h:343
friend std::ostream & operator<<(std::ostream &os, const ofQuaternion &q)
Definition ofQuaternion.cpp:313
const ofQuaternion inverse() const
Multiplicative inverse method.
Definition ofQuaternion.h:479
void makeRotate_original(const ofVec3f &vec1, const ofVec3f &vec2)
Definition ofQuaternion.cpp:163
const ofQuaternion operator-() const
returns the negative of the quaternion. calls operator -() on the Vec4
Definition ofQuaternion.h:455
ofVec3f getEuler() const
Calculate and return the rotation as euler angles.
Definition ofQuaternion.cpp:282
void slerp(float t, const ofQuaternion &from, const ofQuaternion &to)
Spherical Linear Interpolation.
Definition ofQuaternion.cpp:243
ofVec4f asVec4() const
Definition ofQuaternion.h:258
ofQuaternion & operator/=(float rhs)
Unary divide by scalar.
Definition ofQuaternion.h:397
const ofQuaternion operator-(const ofQuaternion &rhs) const
Binary subtraction.
Definition ofQuaternion.h:438
void makeRotate(float angle, float x, float y, float z)
Definition ofQuaternion.cpp:28
void get(ofMatrix4x4 &matrix) const
Definition ofQuaternion.cpp:21
ofQuaternion & operator=(const ofQuaternion &q)
Definition ofQuaternion.h:226
void getRotate(float &angle, float &x, float &y, float &z) const
Return the angle and vector components represented by the quaternion.
Definition ofQuaternion.cpp:220
bool operator==(const ofQuaternion &q) const
Definition ofQuaternion.h:233
ofQuaternion & operator+=(const ofQuaternion &rhs)
Unary addition.
Definition ofQuaternion.h:428
float & w()
Definition ofQuaternion.h:313
ofVec3f asVec3() const
Definition ofQuaternion.h:264
ofQuaternion()
Definition ofQuaternion.h:197
friend std::istream & operator>>(std::istream &is, ofQuaternion &q)
Definition ofQuaternion.cpp:320
float & y()
Definition ofQuaternion.h:301
float & z()
Definition ofQuaternion.h:307
ofQuaternion conj() const
Conjugate.
Definition ofQuaternion.h:473
float & x()
Definition ofQuaternion.h:295
float length() const
Length of the quaternion = sqrt(vec . vec)
Definition ofQuaternion.h:461
void set(float x, float y, float z, float w)
Definition ofQuaternion.h:270
float & operator[](int i)
Definition ofQuaternion.h:282
void normalize()
Definition ofQuaternion.h:497
const ofQuaternion operator+(const ofQuaternion &rhs) const
Binary addition.
Definition ofQuaternion.h:421
ofQuaternion & operator*=(float rhs)
Unary multiply by scalar.
Definition ofQuaternion.h:356
ofQuaternion & operator-=(const ofQuaternion &rhs)
Unary subtraction.
Definition ofQuaternion.h:445
float length2() const
Length of the quaternion = vec . vec.
Definition ofQuaternion.h:467
const ofQuaternion operator*(float rhs) const
Multiply by scalar.
Definition ofQuaternion.h:350
ofQuaternion operator/(float rhs) const
Divide by scalar.
Definition ofQuaternion.h:390
ofVec3f is a class for storing a three dimensional vector.
Definition ofVec3f.h:79
ofVec3f getCrossed(const ofVec3f &vec) const
Returns the cross product (vector product) of this vector and 'vec'. This is a binary operation on tw...
float x
Stores the X component of this vector.
Definition ofVec3f.h:86
Definition ofVec4f.h:11
float x
Definition ofVec4f.h:17