OpenVDB: Quat.h Source File

OpenVDB 7.0.0

Quat.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #ifndef OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
5 #define OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
6
7 #include "Mat.h"
8 #include "Mat3.h"
9 #include "Math.h"
10 #include "Vec3.h"
11 #include <openvdb/Exceptions.h>
12 #include <cmath>
13 #include <iostream>
14 #include <sstream>
15 #include <string>
16
17
18 namespace openvdb {
19 OPENVDB_USE_VERSION_NAMESPACE
20 namespace OPENVDB_VERSION_NAME {
21 namespace math {
22
23 template<typename T> class Quat;
24
26 template <typename T>
27 Quat<T> slerp(const Quat<T> &q1, const Quat<T> &q2, T t, T tolerance=0.00001)
28 {
29 T qdot, angle, sineAngle;
30
31 qdot = q1.dot(q2);
32
33 if (fabs(qdot) >= 1.0) {
34 angle = 0; // not necessary but suppresses compiler warning
35 sineAngle = 0;
36 } else {
37 angle = acos(qdot);
38 sineAngle = sin(angle);
39 }
40
41 //
42 // Denominator close to 0 corresponds to the case where the
43 // two quaternions are close to the same rotation. In this
44 // case linear interpolation is used but we normalize to
45 // guarantee unit length
46 //
47 if (sineAngle <= tolerance) {
48 T s = 1.0 - t;
49
50 Quat<T> qtemp(s * q1[0] + t * q2[0], s * q1[1] + t * q2[1],
51 s * q1[2] + t * q2[2], s * q1[3] + t * q2[3]);
52 //
53 // Check the case where two close to antipodal quaternions were
54 // blended resulting in a nearly zero result which can happen,
55 // for example, if t is close to 0.5. In this case it is not safe
56 // to project back onto the sphere.
57 //
58 double lengthSquared = qtemp.dot(qtemp);
59
60 if (lengthSquared <= tolerance * tolerance) {
61 qtemp = (t < 0.5) ? q1 : q2;
62 } else {
63 qtemp *= 1.0 / sqrt(lengthSquared);
64 }
65 return qtemp;
66 } else {
67
68 T sine = 1.0 / sineAngle;
69 T a = sin((1.0 - t) * angle) * sine;
70 T b = sin(t * angle) * sine;
71 return Quat<T>(a * q1[0] + b * q2[0], a * q1[1] + b * q2[1],
72 a * q1[2] + b * q2[2], a * q1[3] + b * q2[3]);
73 }
74
75 }
76
77 template<typename T>
78 class Quat
79 {
80 public:
81 using value_type = T;
82 using ValueType = T;
83 static const int size = 4;
84
86 Quat() {}
87
89 Quat(T x, T y, T z, T w)
90 {
91 mm[0] = x;
92 mm[1] = y;
93 mm[2] = z;
94 mm[3] = w;
95
96 }
97
99 Quat(T *a)
100 {
101 mm[0] = a[0];
102 mm[1] = a[1];
103 mm[2] = a[2];
104 mm[3] = a[3];
105
106 }
107
110 Quat(const Vec3<T> &axis, T angle)
111 {
112 // assert( REL_EQ(axis.length(), 1.) );
113
114 T s = T(sin(angle*T(0.5)));
115
116 mm[0] = axis.x() * s;
117 mm[1] = axis.y() * s;
118 mm[2] = axis.z() * s;
119
120 mm[3] = T(cos(angle*T(0.5)));
121
122 }
123
125 Quat(math::Axis axis, T angle)
126 {
127 T s = T(sin(angle*T(0.5)));
128
129 mm[0] = (axis==math::X_AXIS) * s;
130 mm[1] = (axis==math::Y_AXIS) * s;
131 mm[2] = (axis==math::Z_AXIS) * s;
132
133 mm[3] = T(cos(angle*T(0.5)));
134 }
135
137 template<typename T1>
138 Quat(const Mat3<T1> &rot) {
139
140 // verify that the matrix is really a rotation
141 if(!isUnitary(rot)) { // unitary is reflection or rotation
142 OPENVDB_THROW(ArithmeticError,
143 "A non-rotation matrix can not be used to construct a quaternion");
144 }
145 if (!isApproxEqual(rot.det(), T1(1))) { // rule out reflection
146 OPENVDB_THROW(ArithmeticError,
147 "A reflection matrix can not be used to construct a quaternion");
148 }
149
150 T trace(rot.trace());
151 if (trace > 0) {
152
153 T q_w = 0.5 * std::sqrt(trace+1);
154 T factor = 0.25 / q_w;
155
156 mm[0] = factor * (rot(1,2) - rot(2,1));
157 mm[1] = factor * (rot(2,0) - rot(0,2));
158 mm[2] = factor * (rot(0,1) - rot(1,0));
159 mm[3] = q_w;
160 } else if (rot(0,0) > rot(1,1) && rot(0,0) > rot(2,2)) {
161
162 T q_x = 0.5 * sqrt(rot(0,0)- rot(1,1)-rot(2,2)+1);
163 T factor = 0.25 / q_x;
164
165 mm[0] = q_x;
166 mm[1] = factor * (rot(0,1) + rot(1,0));
167 mm[2] = factor * (rot(2,0) + rot(0,2));
168 mm[3] = factor * (rot(1,2) - rot(2,1));
169 } else if (rot(1,1) > rot(2,2)) {
170
171 T q_y = 0.5 * sqrt(rot(1,1)-rot(0,0)-rot(2,2)+1);
172 T factor = 0.25 / q_y;
173
174 mm[0] = factor * (rot(0,1) + rot(1,0));
175 mm[1] = q_y;
176 mm[2] = factor * (rot(1,2) + rot(2,1));
177 mm[3] = factor * (rot(2,0) - rot(0,2));
178 } else {
179
180 T q_z = 0.5 * sqrt(rot(2,2)-rot(0,0)-rot(1,1)+1);
181 T factor = 0.25 / q_z;
182
183 mm[0] = factor * (rot(2,0) + rot(0,2));
184 mm[1] = factor * (rot(1,2) + rot(2,1));
185 mm[2] = q_z;
186 mm[3] = factor * (rot(0,1) - rot(1,0));
187 }
188 }
189
191 Quat(const Quat &q)
192 {
193 mm[0] = q.mm[0];
194 mm[1] = q.mm[1];
195 mm[2] = q.mm[2];
196 mm[3] = q.mm[3];
197
198 }
199
201 T& x() { return mm[0]; }
202 T& y() { return mm[1]; }
203 T& z() { return mm[2]; }
204 T& w() { return mm[3]; }
205
207 T x() const { return mm[0]; }
208 T y() const { return mm[1]; }
209 T z() const { return mm[2]; }
210 T w() const { return mm[3]; }
211
212 // Number of elements
213 static unsigned numElements() { return 4; }
214
216 T& operator[](int i) { return mm[i]; }
217
219 T operator[](int i) const { return mm[i]; }
220
222 operator T*() { return mm; }
223 operator const T*() const { return mm; }
224
226 T& operator()(int i) { return mm[i]; }
227
229 T operator()(int i) const { return mm[i]; }
230
232 T angle() const
233 {
234 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
235
236 if ( sqrLength > 1.0e-8 ) {
237
238 return T(T(2.0) * acos(mm[3]));
239
240 } else {
241
242 return T(0.0);
243 }
244 }
245
247 Vec3<T> axis() const
248 {
249 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
250
251 if ( sqrLength > 1.0e-8 ) {
252
253 T invLength = T(T(1)/sqrt(sqrLength));
254
255 return Vec3<T>( mm[0]*invLength, mm[1]*invLength, mm[2]*invLength );
256 } else {
257
258 return Vec3<T>(1,0,0);
259 }
260 }
261
262
264 Quat& init(T x, T y, T z, T w)
265 {
266 mm[0] = x; mm[1] = y; mm[2] = z; mm[3] = w;
267 return *this;
268 }
269
271 Quat& init() { return setIdentity(); }
272
275 Quat& setAxisAngle(const Vec3<T>& axis, T angle)
276 {
277
278 T s = T(sin(angle*T(0.5)));
279
280 mm[0] = axis.x() * s;
281 mm[1] = axis.y() * s;
282 mm[2] = axis.z() * s;
283
284 mm[3] = T(cos(angle*T(0.5)));
285
286 return *this;
287 } // axisAngleTest
288
290 Quat& setZero()
291 {
292 mm[0] = mm[1] = mm[2] = mm[3] = 0;
293 return *this;
294 }
295
297 Quat& setIdentity()
298 {
299 mm[0] = mm[1] = mm[2] = 0;
300 mm[3] = 1;
301 return *this;
302 }
303
305 Vec3<T> eulerAngles(RotationOrder rotationOrder) const
306 { return math::eulerAngles(Mat3<T>(*this), rotationOrder); }
307
309 Quat& operator=(const Quat &q)
310 {
311 mm[0] = q.mm[0];
312 mm[1] = q.mm[1];
313 mm[2] = q.mm[2];
314 mm[3] = q.mm[3];
315
316 return *this;
317 }
318
320 bool operator==(const Quat &q) const
321 {
322 return (isExactlyEqual(mm[0],q.mm[0]) &&
323 isExactlyEqual(mm[1],q.mm[1]) &&
324 isExactlyEqual(mm[2],q.mm[2]) &&
325 isExactlyEqual(mm[3],q.mm[3]) );
326 }
327
329 bool eq(const Quat &q, T eps=1.0e-7) const
330 {
331 return isApproxEqual(mm[0],q.mm[0],eps) && isApproxEqual(mm[1],q.mm[1],eps) &&
332 isApproxEqual(mm[2],q.mm[2],eps) && isApproxEqual(mm[3],q.mm[3],eps) ;
333 } // trivial
334
336 Quat& operator+=(const Quat &q)
337 {
338 mm[0] += q.mm[0];
339 mm[1] += q.mm[1];
340 mm[2] += q.mm[2];
341 mm[3] += q.mm[3];
342
343 return *this;
344 }
345
347 Quat& operator-=(const Quat &q)
348 {
349 mm[0] -= q.mm[0];
350 mm[1] -= q.mm[1];
351 mm[2] -= q.mm[2];
352 mm[3] -= q.mm[3];
353
354 return *this;
355 }
356
358 Quat& operator*=(T scalar)
359 {
360 mm[0] *= scalar;
361 mm[1] *= scalar;
362 mm[2] *= scalar;
363 mm[3] *= scalar;
364
365 return *this;
366 }
367
369 Quat operator+(const Quat &q) const
370 {
371 return Quat<T>(mm[0]+q.mm[0], mm[1]+q.mm[1], mm[2]+q.mm[2], mm[3]+q.mm[3]);
372 }
373
375 Quat operator-(const Quat &q) const
376 {
377 return Quat<T>(mm[0]-q.mm[0], mm[1]-q.mm[1], mm[2]-q.mm[2], mm[3]-q.mm[3]);
378 }
379
381 Quat operator*(const Quat &q) const
382 {
383 Quat<T> prod;
384
385 prod.mm[0] = mm[3]*q.mm[0] + mm[0]*q.mm[3] + mm[1]*q.mm[2] - mm[2]*q.mm[1];
386 prod.mm[1] = mm[3]*q.mm[1] + mm[1]*q.mm[3] + mm[2]*q.mm[0] - mm[0]*q.mm[2];
387 prod.mm[2] = mm[3]*q.mm[2] + mm[2]*q.mm[3] + mm[0]*q.mm[1] - mm[1]*q.mm[0];
388 prod.mm[3] = mm[3]*q.mm[3] - mm[0]*q.mm[0] - mm[1]*q.mm[1] - mm[2]*q.mm[2];
389
390 return prod;
391
392 }
393
395 Quat operator*=(const Quat &q)
396 {
397 *this = *this * q;
398 return *this;
399 }
400
402 Quat operator*(T scalar) const
403 {
404 return Quat<T>(mm[0]*scalar, mm[1]*scalar, mm[2]*scalar, mm[3]*scalar);
405 }
406
408 Quat operator/(T scalar) const
409 {
410 return Quat<T>(mm[0]/scalar, mm[1]/scalar, mm[2]/scalar, mm[3]/scalar);
411 }
412
414 Quat operator-() const
415 { return Quat<T>(-mm[0], -mm[1], -mm[2], -mm[3]); }
416
419 Quat& add(const Quat &q1, const Quat &q2)
420 {
421 mm[0] = q1.mm[0] + q2.mm[0];
422 mm[1] = q1.mm[1] + q2.mm[1];
423 mm[2] = q1.mm[2] + q2.mm[2];
424 mm[3] = q1.mm[3] + q2.mm[3];
425
426 return *this;
427 }
428
431 Quat& sub(const Quat &q1, const Quat &q2)
432 {
433 mm[0] = q1.mm[0] - q2.mm[0];
434 mm[1] = q1.mm[1] - q2.mm[1];
435 mm[2] = q1.mm[2] - q2.mm[2];
436 mm[3] = q1.mm[3] - q2.mm[3];
437
438 return *this;
439 }
440
443 Quat& mult(const Quat &q1, const Quat &q2)
444 {
445 mm[0] = q1.mm[3]*q2.mm[0] + q1.mm[0]*q2.mm[3] +
446 q1.mm[1]*q2.mm[2] - q1.mm[2]*q2.mm[1];
447 mm[1] = q1.mm[3]*q2.mm[1] + q1.mm[1]*q2.mm[3] +
448 q1.mm[2]*q2.mm[0] - q1.mm[0]*q2.mm[2];
449 mm[2] = q1.mm[3]*q2.mm[2] + q1.mm[2]*q2.mm[3] +
450 q1.mm[0]*q2.mm[1] - q1.mm[1]*q2.mm[0];
451 mm[3] = q1.mm[3]*q2.mm[3] - q1.mm[0]*q2.mm[0] -
452 q1.mm[1]*q2.mm[1] - q1.mm[2]*q2.mm[2];
453
454 return *this;
455 }
456
459 Quat& scale(T scale, const Quat &q)
460 {
461 mm[0] = scale * q.mm[0];
462 mm[1] = scale * q.mm[1];
463 mm[2] = scale * q.mm[2];
464 mm[3] = scale * q.mm[3];
465
466 return *this;
467 }
468
470 T dot(const Quat &q) const
471 {
472 return (mm[0]*q.mm[0] + mm[1]*q.mm[1] + mm[2]*q.mm[2] + mm[3]*q.mm[3]);
473 }
474
477 Quat derivative(const Vec3<T>& omega) const
478 {
479 return Quat<T>( +w()*omega.x() -z()*omega.y() +y()*omega.z() ,
480 +z()*omega.x() +w()*omega.y() -x()*omega.z() ,
481 -y()*omega.x() +x()*omega.y() +w()*omega.z() ,
482 -x()*omega.x() -y()*omega.y() -z()*omega.z() );
483 }
484
486 bool normalize(T eps = T(1.0e-8))
487 {
488 T d = T(sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]));
489 if( isApproxEqual(d, T(0.0), eps) ) return false;
490 *this *= ( T(1)/d );
491 return true;
492 }
493
495 Quat unit() const
496 {
497 T d = sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]);
498 if( isExactlyEqual(d , T(0.0) ) )
499 OPENVDB_THROW(ArithmeticError,
500 "Normalizing degenerate quaternion");
501 return *this / d;
502 }
503
505 Quat inverse(T tolerance = T(0))
506 {
507 T d = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3];
508 if( isApproxEqual(d, T(0.0), tolerance) )
509 OPENVDB_THROW(ArithmeticError,
510 "Cannot invert degenerate quaternion");
511 Quat result = *this/-d;
512 result.mm[3] = -result.mm[3];
513 return result;
514 }
515
516
519 Quat conjugate() const
520 {
521 return Quat<T>(-mm[0], -mm[1], -mm[2], mm[3]);
522 }
523
525 Vec3<T> rotateVector(const Vec3<T> &v) const
526 {
527 Mat3<T> m(*this);
528 return m.transform(v);
529 }
530
532 static Quat zero() { return Quat<T>(0,0,0,0); }
533 static Quat identity() { return Quat<T>(0,0,0,1); }
534
536 std::string str() const
537 {
538 std::ostringstream buffer;
539
540 buffer << "[";
541
542 // For each column
543 for (unsigned j(0); j < 4; j++) {
544 if (j) buffer << ", ";
545 buffer << mm[j];
546 }
547
548 buffer << "]";
549
550 return buffer.str();
551 }
552
554 friend std::ostream& operator<<(std::ostream &stream, const Quat &q)
555 {
556 stream << q.str();
557 return stream;
558 }
559
560 friend Quat slerp<>(const Quat &q1, const Quat &q2, T t, T tolerance);
561
562 void write(std::ostream& os) const { os.write(static_cast<char*>(&mm), sizeof(T) * 4); }
563 void read(std::istream& is) { is.read(static_cast<char*>(&mm), sizeof(T) * 4); }
564
565 protected:
566 T mm[4];
567 };
568
570 template <typename S, typename T>
571 Quat<T> operator*(S scalar, const Quat<T> &q) { return q*scalar; }
572
573
577 template <typename T, typename T0>
578 Mat3<T> slerp(const Mat3<T0> &m1, const Mat3<T0> &m2, T t)
579 {
580 using MatType = Mat3<T>;
581
582 Quat<T> q1(m1);
583 Quat<T> q2(m2);
584
585 if (q1.dot(q2) < 0) q2 *= -1;
586
587 Quat<T> qslerp = slerp<T>(q1, q2, static_cast<T>(t));
588 MatType m = rotation<MatType>(qslerp);
589 return m;
590 }
591
592
593
602 template <typename T, typename T0>
603 Mat3<T> bezLerp(const Mat3<T0> &m1, const Mat3<T0> &m2,
604 const Mat3<T0> &m3, const Mat3<T0> &m4,
605 T t)
606 {
607 Mat3<T> m00, m01, m02, m10, m11;
608
609 m00 = slerp(m1, m2, t);
610 m01 = slerp(m2, m3, t);
611 m02 = slerp(m3, m4, t);
612
613 m10 = slerp(m00, m01, t);
614 m11 = slerp(m01, m02, t);
615
616 return slerp(m10, m11, t);
617 }
618
619 using Quats = Quat<float>;
620 using Quatd = Quat<double>;
621
622 } // namespace math
623
624
625 template<> inline math::Quats zeroVal<math::Quats >() { return math::Quats::zero(); }
626 template<> inline math::Quatd zeroVal<math::Quatd >() { return math::Quatd::zero(); }
627
628 } // namespace OPENVDB_VERSION_NAME
629 } // namespace openvdb
630
631 #endif //OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
openvdb::v7_0::math::Quat::operator()
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Quat.h:226
openvdb::v7_0::math::Quat::setIdentity
Quat & setIdentity()
Set "this" vector to identity.
Definition: Quat.h:297
openvdb::v7_0::math::Vec3::x
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:83
openvdb::v7_0::math::Quat::z
T z() const
Definition: Quat.h:209
openvdb::v7_0::math::Quat::operator/
Quat operator/(T scalar) const
Return (this/scalar), e.g. q = q1 / scalar;.
Definition: Quat.h:408
openvdb::v7_0::math::Quat::identity
static Quat identity()
Definition: Quat.h:533
openvdb::v7_0::math::Quat::str
std::string str() const
Definition: Quat.h:536
openvdb::v7_0::math::Quat::init
Quat & init()
"this" quaternion gets initialized to identity, same as setIdentity()
Definition: Quat.h:271
openvdb::v7_0::math::Quat::add
Quat & add(const Quat &q1, const Quat &q2)
Definition: Quat.h:419
openvdb::v7_0::math::Quat::Quat
Quat(T x, T y, T z, T w)
Constructor with four arguments, e.g. Quatf q(1,2,3,4);.
Definition: Quat.h:89
openvdb::v7_0::math::isApproxEqual
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:351
openvdb::v7_0::math::Mat3::det
T det() const
Determinant of matrix.
Definition: Mat3.h:500
openvdb::v7_0::math::Quat::operator*
Quat operator*(const Quat &q) const
Return (this*q), e.g. q = q1 * q2;.
Definition: Quat.h:381
openvdb::v7_0::math::Quat::operator-
Quat operator-() const
Negation operator, e.g. q = -q;.
Definition: Quat.h:414
openvdb::v7_0::math::RotationOrder
RotationOrder
Definition: Math.h:856
openvdb::v7_0::math::Z_AXIS
@ Z_AXIS
Definition: Math.h:852
openvdb::v7_0::math::Quat::ValueType
T ValueType
Definition: Quat.h:82
openvdb::v7_0::math::Quat::sub
Quat & sub(const Quat &q1, const Quat &q2)
Definition: Quat.h:431
openvdb::v7_0::math::Quat::normalize
bool normalize(T eps=T(1.0e-8))
this = normalized this
Definition: Quat.h:486
openvdb::v7_0::math::Quat::y
T y() const
Definition: Quat.h:208
openvdb::v7_0::math::Quat
Definition: Mat.h:169
openvdb::v7_0::math::Quat::dot
T dot(const Quat &q) const
Dot product.
Definition: Quat.h:470
openvdb::v7_0::math::Quat::operator*=
Quat operator*=(const Quat &q)
Assigns this to (this*q), e.g. q *= q1;.
Definition: Quat.h:395
openvdb::v7_0::math::Quat::init
Quat & init(T x, T y, T z, T w)
"this" quaternion gets initialized to [x, y, z, w]
Definition: Quat.h:264
openvdb::v7_0::math::Quat::eulerAngles
Vec3< T > eulerAngles(RotationOrder rotationOrder) const
Returns vector of x,y,z rotational components.
Definition: Quat.h:305
openvdb::v7_0::math::Quat::operator-
Quat operator-(const Quat &q) const
Return (this-q), e.g. q = q1 - q2;.
Definition: Quat.h:375
openvdb::v7_0::math::X_AXIS
@ X_AXIS
Definition: Math.h:850
openvdb::v7_0::math::Quat::x
T x() const
Get the component, e.g. float f = q.w();.
Definition: Quat.h:207
openvdb::v7_0::math::Quat::mm
T mm[4]
Definition: Quat.h:566
openvdb::v7_0::math::Quat::y
T & y()
Definition: Quat.h:202
openvdb::v7_0::math::slerp
Mat3< T > slerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, T t)
Interpolate between m1 and m2. Converts to quaternion form and uses slerp m1 and m2 must be rotation ...
Definition: Quat.h:578
openvdb::v7_0::math::Quat::operator()
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Quat.h:229
openvdb::v7_0::math::Quat::eq
bool eq(const Quat &q, T eps=1.0e-7) const
Test if "this" is equivalent to q with tolerance of eps value.
Definition: Quat.h:329
openvdb::v7_0::math::Axis
Axis
Definition: Math.h:849
openvdb::v7_0::math::Quat::x
T & x()
Reference to the component, e.g. q.x() = 4.5f;.
Definition: Quat.h:201
openvdb::v7_0::math::Mat3::transform
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:526
openvdb::v7_0::math::Quat::derivative
Quat derivative(const Vec3< T > &omega) const
Definition: Quat.h:477
openvdb::v7_0::math::Quat::operator-=
Quat & operator-=(const Quat &q)
Subtract quaternion q from "this" quaternion, e.g. q -= q1;.
Definition: Quat.h:347
openvdb::v7_0::math::Vec3::y
T & y()
Definition: Vec3.h:84
openvdb::v7_0::math::Quat::scale
Quat & scale(T scale, const Quat &q)
Definition: Quat.h:459
openvdb::v7_0::math::Quat::inverse
Quat inverse(T tolerance=T(0))
returns inverse of this
Definition: Quat.h:505
openvdb::v7_0::math::Quat::z
T & z()
Definition: Quat.h:203
openvdb::v7_0::math::Quat::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Quat &q)
Output to the stream, e.g. std::cout << q << std::endl;.
Definition: Quat.h:554
Vec3.h
openvdb::v7_0::math::Y_AXIS
@ Y_AXIS
Definition: Math.h:851
openvdb::v7_0::math::Quat::w
T w() const
Definition: Quat.h:210
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v7_0::math::Vec3::z
T & z()
Definition: Vec3.h:85
openvdb::v7_0::math::Quat::write
void write(std::ostream &os) const
Definition: Quat.h:562
openvdb::v7_0::math::Quat::Quat
Quat(const Vec3< T > &axis, T angle)
Definition: Quat.h:110
openvdb::v7_0::math::Quat::read
void read(std::istream &is)
Definition: Quat.h:563
openvdb::v7_0::math::Mat3::trace
T trace() const
Trace of matrix.
Definition: Mat3.h:509
openvdb::v7_0::math::Quat::operator==
bool operator==(const Quat &q) const
Equality operator, does exact floating point comparisons.
Definition: Quat.h:320
openvdb::v7_0::math::bezLerp
Mat3< T > bezLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, const Mat3< T0 > &m3, const Mat3< T0 > &m4, T t)
Definition: Quat.h:603
openvdb::v7_0::math::Quat::numElements
static unsigned numElements()
Definition: Quat.h:213
openvdb::v7_0::math::Vec3
Definition: Mat.h:170
openvdb::v7_0::math::Quat::operator=
Quat & operator=(const Quat &q)
Assignment operator.
Definition: Quat.h:309
openvdb::v7_0::math::Quat::operator+
Quat operator+(const Quat &q) const
Return (this+q), e.g. q = q1 + q2;.
Definition: Quat.h:369
openvdb::v7_0::math::Mat3
3x3 matrix class.
Definition: Mat3.h:29
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
Mat3.h
openvdb::v7_0::math::Quat::operator*=
Quat & operator*=(T scalar)
Scale "this" quaternion by scalar, e.g. q *= scalar;.
Definition: Quat.h:358
openvdb::v7_0::math::Quat::Quat
Quat(const Mat3< T1 > &rot)
Constructor given a rotation matrix.
Definition: Quat.h:138
openvdb::v7_0::math::isUnitary
bool isUnitary(const MatType &m)
Determine if a matrix is unitary (i.e., rotation or reflection).
Definition: Mat.h:894
openvdb::v7_0::math::Quat::unit
Quat unit() const
this = normalized this
Definition: Quat.h:495
openvdb::v7_0::math::Quat::Quat
Quat()
Trivial constructor, the quaternion is NOT initialized.
Definition: Quat.h:86
openvdb::v7_0::math::Quat::Quat
Quat(T *a)
Constructor with array argument, e.g. float a[4]; Quatf q(a);.
Definition: Quat.h:99
openvdb::v7_0::math::Quat::conjugate
Quat conjugate() const
Definition: Quat.h:519
openvdb::v7_0::math::Quat::Quat
Quat(const Quat &q)
Copy constructor.
Definition: Quat.h:191
openvdb::v7_0::math::angle
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:445
openvdb::v7_0::math::Quat::mult
Quat & mult(const Quat &q1, const Quat &q2)
Definition: Quat.h:443
openvdb::v7_0::math::operator*
Quat< T > operator*(S scalar, const Quat< T > &q)
Multiply each element of the given quaternion by scalar and return the result.
Definition: Quat.h:571
openvdb::v7_0::math::isExactlyEqual
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:388
openvdb::v7_0::math::Quat::w
T & w()
Definition: Quat.h:204
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::math::scale
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:620
openvdb::v7_0::math::Quat::Quat
Quat(math::Axis axis, T angle)
Constructor given rotation as axis and angle.
Definition: Quat.h:125
openvdb::v7_0::math::Quat::operator[]
T operator[](int i) const
Array style constant reference to the components, e.g. float f = q[1];.
Definition: Quat.h:219
openvdb::v7_0::math::Quat::zero
static Quat zero()
Predefined constants, e.g. Quat q = Quat::identity();.
Definition: Quat.h:532
openvdb::v7_0::math::Quat::setZero
Quat & setZero()
Set "this" vector to zero.
Definition: Quat.h:290
openvdb::v7_0::math::Quat::operator[]
T & operator[](int i)
Array style reference to the components, e.g. q[3] = 1.34f;.
Definition: Quat.h:216
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::math::Quat::rotateVector
Vec3< T > rotateVector(const Vec3< T > &v) const
Return rotated vector by "this" quaternion.
Definition: Quat.h:525
openvdb::v7_0::math::Quat::value_type
T value_type
Definition: Quat.h:81
openvdb::v7_0::math::eulerAngles
Vec3< typename MatType::value_type > eulerAngles(const MatType &mat, RotationOrder rotationOrder, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return the Euler angles composing the given rotation matrix.
Definition: Mat.h:338
openvdb::v7_0::math::Quat::angle
T angle() const
Return angle of rotation.
Definition: Quat.h:232
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
Exceptions.h
openvdb::v7_0::math::Quat::operator+=
Quat & operator+=(const Quat &q)
Add quaternion q to "this" quaternion, e.g. q += q1;.
Definition: Quat.h:336
openvdb::v7_0::math::Quat::operator*
Quat operator*(T scalar) const
Return (this*scalar), e.g. q = q1 * scalar;.
Definition: Quat.h:402
Mat.h
openvdb::v7_0::math::Quat::setAxisAngle
Quat & setAxisAngle(const Vec3< T > &axis, T angle)
Definition: Quat.h:275
openvdb::v7_0::ArithmeticError
Definition: Exceptions.h:56
openvdb::v7_0::math::Quat::axis
Vec3< T > axis() const
Return axis of rotation.
Definition: Quat.h:247