HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Quat.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
33 
34 #include "Mat.h"
35 #include "Mat3.h"
36 #include "Math.h"
37 #include "Vec3.h"
38 #include <openvdb/Exceptions.h>
39 #include <cmath>
40 #include <iostream>
41 #include <sstream>
42 #include <string>
43 
44 
45 namespace openvdb {
47 namespace OPENVDB_VERSION_NAME {
48 namespace math {
49 
50 template<typename T> class Quat;
51 
52 /// Linear interpolation between the two quaternions
53 template <typename T>
54 Quat<T> slerp(const Quat<T> &q1, const Quat<T> &q2, T t, T tolerance=0.00001)
55 {
56  T qdot, angle, sineAngle;
57 
58  qdot = q1.dot(q2);
59 
60  if (fabs(qdot) >= 1.0) {
61  angle = 0; // not necessary but suppresses compiler warning
62  sineAngle = 0;
63  } else {
64  angle = acos(qdot);
65  sineAngle = sin(angle);
66  }
67 
68  //
69  // Denominator close to 0 corresponds to the case where the
70  // two quaternions are close to the same rotation. In this
71  // case linear interpolation is used but we normalize to
72  // guarantee unit length
73  //
74  if (sineAngle <= tolerance) {
75  T s = 1.0 - t;
76 
77  Quat<T> qtemp(s * q1[0] + t * q2[0], s * q1[1] + t * q2[1],
78  s * q1[2] + t * q2[2], s * q1[3] + t * q2[3]);
79  //
80  // Check the case where two close to antipodal quaternions were
81  // blended resulting in a nearly zero result which can happen,
82  // for example, if t is close to 0.5. In this case it is not safe
83  // to project back onto the sphere.
84  //
85  double lengthSquared = qtemp.dot(qtemp);
86 
87  if (lengthSquared <= tolerance * tolerance) {
88  qtemp = (t < 0.5) ? q1 : q2;
89  } else {
90  qtemp *= 1.0 / sqrt(lengthSquared);
91  }
92  return qtemp;
93  } else {
94 
95  T sine = 1.0 / sineAngle;
96  T a = sin((1.0 - t) * angle) * sine;
97  T b = sin(t * angle) * sine;
98  return Quat<T>(a * q1[0] + b * q2[0], a * q1[1] + b * q2[1],
99  a * q1[2] + b * q2[2], a * q1[3] + b * q2[3]);
100  }
101 
102 }
103 
104 template<typename T>
105 class Quat
106 {
107 public:
108  /// Trivial constructor, the quaternion is NOT initialized
109  Quat() {}
110 
111  /// Constructor with four arguments, e.g. Quatf q(1,2,3,4);
112  Quat(T x, T y, T z, T w)
113  {
114  mm[0] = x;
115  mm[1] = y;
116  mm[2] = z;
117  mm[3] = w;
118 
119  }
120 
121  /// Constructor with array argument, e.g. float a[4]; Quatf q(a);
122  Quat(T *a)
123  {
124  mm[0] = a[0];
125  mm[1] = a[1];
126  mm[2] = a[2];
127  mm[3] = a[3];
128 
129  }
130 
131  /// Constructor given rotation as axis and angle, the axis must be
132  /// unit vector
133  Quat(const Vec3<T> &axis, T angle)
134  {
135  // assert( REL_EQ(axis.length(), 1.) );
136 
137  T s = T(sin(angle*T(0.5)));
138 
139  mm[0] = axis.x() * s;
140  mm[1] = axis.y() * s;
141  mm[2] = axis.z() * s;
142 
143  mm[3] = T(cos(angle*T(0.5)));
144 
145  }
146 
147  /// Constructor given rotation as axis and angle
149  {
150  T s = T(sin(angle*T(0.5)));
151 
152  mm[0] = (axis==math::X_AXIS) * s;
153  mm[1] = (axis==math::Y_AXIS) * s;
154  mm[2] = (axis==math::Z_AXIS) * s;
155 
156  mm[3] = T(cos(angle*T(0.5)));
157  }
158 
159  /// Constructor given a rotation matrix
160  template<typename T1>
161  Quat(const Mat3<T1> &rot) {
162 
163  // verify that the matrix is really a rotation
164  if(!isUnitary(rot)) { // unitary is reflection or rotation
165  OPENVDB_THROW(ArithmeticError,
166  "A non-rotation matrix can not be used to construct a quaternion");
167  }
168  if (!isApproxEqual(rot.det(), T1(1))) { // rule out reflection
169  OPENVDB_THROW(ArithmeticError,
170  "A reflection matrix can not be used to construct a quaternion");
171  }
172 
173  T trace(rot.trace());
174  if (trace > 0) {
175 
176  T q_w = 0.5 * std::sqrt(trace+1);
177  T factor = 0.25 / q_w;
178 
179  mm[0] = factor * (rot(1,2) - rot(2,1));
180  mm[1] = factor * (rot(2,0) - rot(0,2));
181  mm[2] = factor * (rot(0,1) - rot(1,0));
182  mm[3] = q_w;
183  } else if (rot(0,0) > rot(1,1) && rot(0,0) > rot(2,2)) {
184 
185  T q_x = 0.5 * sqrt(rot(0,0)- rot(1,1)-rot(2,2)+1);
186  T factor = 0.25 / q_x;
187 
188  mm[0] = q_x;
189  mm[1] = factor * (rot(0,1) + rot(1,0));
190  mm[2] = factor * (rot(2,0) + rot(0,2));
191  mm[3] = factor * (rot(1,2) - rot(2,1));
192  } else if (rot(1,1) > rot(2,2)) {
193 
194  T q_y = 0.5 * sqrt(rot(1,1)-rot(0,0)-rot(2,2)+1);
195  T factor = 0.25 / q_y;
196 
197  mm[0] = factor * (rot(0,1) + rot(1,0));
198  mm[1] = q_y;
199  mm[2] = factor * (rot(1,2) + rot(2,1));
200  mm[3] = factor * (rot(2,0) - rot(0,2));
201  } else {
202 
203  T q_z = 0.5 * sqrt(rot(2,2)-rot(0,0)-rot(1,1)+1);
204  T factor = 0.25 / q_z;
205 
206  mm[0] = factor * (rot(2,0) + rot(0,2));
207  mm[1] = factor * (rot(1,2) + rot(2,1));
208  mm[2] = q_z;
209  mm[3] = factor * (rot(0,1) - rot(1,0));
210  }
211  }
212 
213  /// Copy constructor
214  Quat(const Quat &q)
215  {
216  mm[0] = q.mm[0];
217  mm[1] = q.mm[1];
218  mm[2] = q.mm[2];
219  mm[3] = q.mm[3];
220 
221  }
222 
223  /// Reference to the component, e.g. q.x() = 4.5f;
224  T& x() { return mm[0]; }
225  T& y() { return mm[1]; }
226  T& z() { return mm[2]; }
227  T& w() { return mm[3]; }
228 
229  /// Get the component, e.g. float f = q.w();
230  T x() const { return mm[0]; }
231  T y() const { return mm[1]; }
232  T z() const { return mm[2]; }
233  T w() const { return mm[3]; }
234 
235  // Number of elements
236  static unsigned numElements() { return 4; }
237 
238  /// Array style reference to the components, e.g. q[3] = 1.34f;
239  T& operator[](int i) { return mm[i]; }
240 
241  /// Array style constant reference to the components, e.g. float f = q[1];
242  T operator[](int i) const { return mm[i]; }
243 
244  /// Cast to T*
245  operator T*() { return mm; }
246  operator const T*() const { return mm; }
247 
248  /// Alternative indexed reference to the elements
249  T& operator()(int i) { return mm[i]; }
250 
251  /// Alternative indexed constant reference to the elements,
252  T operator()(int i) const { return mm[i]; }
253 
254  /// Return angle of rotation
255  T angle() const
256  {
257  T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
258 
259  if ( sqrLength > 1.0e-8 ) {
260 
261  return T(T(2.0) * acos(mm[3]));
262 
263  } else {
264 
265  return T(0.0);
266  }
267  }
268 
269  /// Return axis of rotation
270  Vec3<T> axis() const
271  {
272  T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
273 
274  if ( sqrLength > 1.0e-8 ) {
275 
276  T invLength = T(T(1)/sqrt(sqrLength));
277 
278  return Vec3<T>( mm[0]*invLength, mm[1]*invLength, mm[2]*invLength );
279  } else {
280 
281  return Vec3<T>(1,0,0);
282  }
283  }
284 
285 
286  /// "this" quaternion gets initialized to [x, y, z, w]
287  Quat& init(T x, T y, T z, T w)
288  {
289  mm[0] = x; mm[1] = y; mm[2] = z; mm[3] = w;
290  return *this;
291  }
292 
293  /// "this" quaternion gets initialized to identity, same as setIdentity()
294  Quat& init() { return setIdentity(); }
295 
296  /// Set "this" quaternion to rotation specified by axis and angle,
297  /// the axis must be unit vector
299  {
300 
301  T s = T(sin(angle*T(0.5)));
302 
303  mm[0] = axis.x() * s;
304  mm[1] = axis.y() * s;
305  mm[2] = axis.z() * s;
306 
307  mm[3] = T(cos(angle*T(0.5)));
308 
309  return *this;
310  } // axisAngleTest
311 
312  /// Set "this" vector to zero
314  {
315  mm[0] = mm[1] = mm[2] = mm[3] = 0;
316  return *this;
317  }
318 
319  /// Set "this" vector to identity
321  {
322  mm[0] = mm[1] = mm[2] = 0;
323  mm[3] = 1;
324  return *this;
325  }
326 
327  /// Returns vector of x,y,z rotational components
328  Vec3<T> eulerAngles(RotationOrder rotationOrder) const
329  { return math::eulerAngles(Mat3<T>(*this), rotationOrder); }
330 
331  /// Assignment operator
332  Quat& operator=(const Quat &q)
333  {
334  mm[0] = q.mm[0];
335  mm[1] = q.mm[1];
336  mm[2] = q.mm[2];
337  mm[3] = q.mm[3];
338 
339  return *this;
340  }
341 
342  /// Equality operator, does exact floating point comparisons
343  bool operator==(const Quat &q) const
344  {
345  return (isExactlyEqual(mm[0],q.mm[0]) &&
346  isExactlyEqual(mm[1],q.mm[1]) &&
347  isExactlyEqual(mm[2],q.mm[2]) &&
348  isExactlyEqual(mm[3],q.mm[3]) );
349  }
350 
351  /// Test if "this" is equivalent to q with tolerance of eps value
352  bool eq(const Quat &q, T eps=1.0e-7) const
353  {
354  return isApproxEqual(mm[0],q.mm[0],eps) && isApproxEqual(mm[1],q.mm[1],eps) &&
355  isApproxEqual(mm[2],q.mm[2],eps) && isApproxEqual(mm[3],q.mm[3],eps) ;
356  } // trivial
357 
358  /// Add quaternion q to "this" quaternion, e.g. q += q1;
359  Quat& operator+=(const Quat &q)
360  {
361  mm[0] += q.mm[0];
362  mm[1] += q.mm[1];
363  mm[2] += q.mm[2];
364  mm[3] += q.mm[3];
365 
366  return *this;
367  }
368 
369  /// Subtract quaternion q from "this" quaternion, e.g. q -= q1;
370  Quat& operator-=(const Quat &q)
371  {
372  mm[0] -= q.mm[0];
373  mm[1] -= q.mm[1];
374  mm[2] -= q.mm[2];
375  mm[3] -= q.mm[3];
376 
377  return *this;
378  }
379 
380  /// Scale "this" quaternion by scalar, e.g. q *= scalar;
381  Quat& operator*=(T scalar)
382  {
383  mm[0] *= scalar;
384  mm[1] *= scalar;
385  mm[2] *= scalar;
386  mm[3] *= scalar;
387 
388  return *this;
389  }
390 
391  /// Return (this+q), e.g. q = q1 + q2;
392  Quat operator+(const Quat &q) const
393  {
394  return Quat<T>(mm[0]+q.mm[0], mm[1]+q.mm[1], mm[2]+q.mm[2], mm[3]+q.mm[3]);
395  }
396 
397  /// Return (this-q), e.g. q = q1 - q2;
398  Quat operator-(const Quat &q) const
399  {
400  return Quat<T>(mm[0]-q.mm[0], mm[1]-q.mm[1], mm[2]-q.mm[2], mm[3]-q.mm[3]);
401  }
402 
403  /// Return (this*q), e.g. q = q1 * q2;
404  Quat operator*(const Quat &q) const
405  {
406  Quat<T> prod;
407 
408  prod.mm[0] = mm[3]*q.mm[0] + mm[0]*q.mm[3] + mm[1]*q.mm[2] - mm[2]*q.mm[1];
409  prod.mm[1] = mm[3]*q.mm[1] + mm[1]*q.mm[3] + mm[2]*q.mm[0] - mm[0]*q.mm[2];
410  prod.mm[2] = mm[3]*q.mm[2] + mm[2]*q.mm[3] + mm[0]*q.mm[1] - mm[1]*q.mm[0];
411  prod.mm[3] = mm[3]*q.mm[3] - mm[0]*q.mm[0] - mm[1]*q.mm[1] - mm[2]*q.mm[2];
412 
413  return prod;
414 
415  }
416 
417  /// Assigns this to (this*q), e.g. q *= q1;
418  Quat operator*=(const Quat &q)
419  {
420  *this = *this * q;
421  return *this;
422  }
423 
424  /// Return (this*scalar), e.g. q = q1 * scalar;
425  Quat operator*(T scalar) const
426  {
427  return Quat<T>(mm[0]*scalar, mm[1]*scalar, mm[2]*scalar, mm[3]*scalar);
428  }
429 
430  /// Return (this/scalar), e.g. q = q1 / scalar;
431  Quat operator/(T scalar) const
432  {
433  return Quat<T>(mm[0]/scalar, mm[1]/scalar, mm[2]/scalar, mm[3]/scalar);
434  }
435 
436  /// Negation operator, e.g. q = -q;
437  Quat operator-() const
438  { return Quat<T>(-mm[0], -mm[1], -mm[2], -mm[3]); }
439 
440  /// this = q1 + q2
441  /// "this", q1 and q2 need not be distinct objects, e.g. q.add(q1,q);
442  Quat& add(const Quat &q1, const Quat &q2)
443  {
444  mm[0] = q1.mm[0] + q2.mm[0];
445  mm[1] = q1.mm[1] + q2.mm[1];
446  mm[2] = q1.mm[2] + q2.mm[2];
447  mm[3] = q1.mm[3] + q2.mm[3];
448 
449  return *this;
450  }
451 
452  /// this = q1 - q2
453  /// "this", q1 and q2 need not be distinct objects, e.g. q.sub(q1,q);
454  Quat& sub(const Quat &q1, const Quat &q2)
455  {
456  mm[0] = q1.mm[0] - q2.mm[0];
457  mm[1] = q1.mm[1] - q2.mm[1];
458  mm[2] = q1.mm[2] - q2.mm[2];
459  mm[3] = q1.mm[3] - q2.mm[3];
460 
461  return *this;
462  }
463 
464  /// this = q1 * q2
465  /// q1 and q2 must be distinct objects than "this", e.g. q.mult(q1,q2);
466  Quat& mult(const Quat &q1, const Quat &q2)
467  {
468  mm[0] = q1.mm[3]*q2.mm[0] + q1.mm[0]*q2.mm[3] +
469  q1.mm[1]*q2.mm[2] - q1.mm[2]*q2.mm[1];
470  mm[1] = q1.mm[3]*q2.mm[1] + q1.mm[1]*q2.mm[3] +
471  q1.mm[2]*q2.mm[0] - q1.mm[0]*q2.mm[2];
472  mm[2] = q1.mm[3]*q2.mm[2] + q1.mm[2]*q2.mm[3] +
473  q1.mm[0]*q2.mm[1] - q1.mm[1]*q2.mm[0];
474  mm[3] = q1.mm[3]*q2.mm[3] - q1.mm[0]*q2.mm[0] -
475  q1.mm[1]*q2.mm[1] - q1.mm[2]*q2.mm[2];
476 
477  return *this;
478  }
479 
480  /// this = scalar*q, q need not be distinct object than "this",
481  /// e.g. q.scale(1.5,q1);
482  Quat& scale(T scale, const Quat &q)
483  {
484  mm[0] = scale * q.mm[0];
485  mm[1] = scale * q.mm[1];
486  mm[2] = scale * q.mm[2];
487  mm[3] = scale * q.mm[3];
488 
489  return *this;
490  }
491 
492  /// Dot product
493  T dot(const Quat &q) const
494  {
495  return (mm[0]*q.mm[0] + mm[1]*q.mm[1] + mm[2]*q.mm[2] + mm[3]*q.mm[3]);
496  }
497 
498  /// Return the quaternion rate corrsponding to the angular velocity omega
499  /// and "this" current rotation
500  Quat derivative(const Vec3<T>& omega) const
501  {
502  return Quat<T>( +w()*omega.x() -z()*omega.y() +y()*omega.z() ,
503  +z()*omega.x() +w()*omega.y() -x()*omega.z() ,
504  -y()*omega.x() +x()*omega.y() +w()*omega.z() ,
505  -x()*omega.x() -y()*omega.y() -z()*omega.z() );
506  }
507 
508  /// this = normalized this
509  bool normalize(T eps = T(1.0e-8))
510  {
511  T d = T(sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]));
512  if( isApproxEqual(d, T(0.0), eps) ) return false;
513  *this *= ( T(1)/d );
514  return true;
515  }
516 
517  /// this = normalized this
518  Quat unit() const
519  {
520  T d = sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]);
521  if( isExactlyEqual(d , T(0.0) ) )
522  OPENVDB_THROW(ArithmeticError,
523  "Normalizing degenerate quaternion");
524  return *this / d;
525  }
526 
527  /// returns inverse of this
528  Quat inverse(T tolerance = T(0))
529  {
530  T d = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3];
531  if( isApproxEqual(d, T(0.0), tolerance) )
532  OPENVDB_THROW(ArithmeticError,
533  "Cannot invert degenerate quaternion");
534  Quat result = *this/-d;
535  result.mm[3] = -result.mm[3];
536  return result;
537  }
538 
539 
540  /// Return the conjugate of "this", same as invert without
541  /// unit quaternion test
542  Quat conjugate() const
543  {
544  return Quat<T>(-mm[0], -mm[1], -mm[2], mm[3]);
545  }
546 
547  /// Return rotated vector by "this" quaternion
549  {
550  Mat3<T> m(*this);
551  return m.transform(v);
552  }
553 
554  /// Predefined constants, e.g. Quat q = Quat::identity();
555  static Quat zero() { return Quat<T>(0,0,0,0); }
556  static Quat identity() { return Quat<T>(0,0,0,1); }
557 
558  /// @return string representation of Classname
559  std::string str() const
560  {
561  std::ostringstream buffer;
562 
563  buffer << "[";
564 
565  // For each column
566  for (unsigned j(0); j < 4; j++) {
567  if (j) buffer << ", ";
568  buffer << mm[j];
569  }
570 
571  buffer << "]";
572 
573  return buffer.str();
574  }
575 
576  /// Output to the stream, e.g. std::cout << q << std::endl;
577  friend std::ostream& operator<<(std::ostream &stream, const Quat &q)
578  {
579  stream << q.str();
580  return stream;
581  }
582 
583  friend Quat slerp<>(const Quat &q1, const Quat &q2, T t, T tolerance);
584 
585  void write(std::ostream& os) const { os.write(static_cast<char*>(&mm), sizeof(T) * 4); }
586  void read(std::istream& is) { is.read(static_cast<char*>(&mm), sizeof(T) * 4); }
587 
588 protected:
589  T mm[4];
590 };
591 
592 /// Multiply each element of the given quaternion by @a scalar and return the result.
593 template <typename S, typename T>
594 Quat<T> operator*(S scalar, const Quat<T> &q) { return q*scalar; }
595 
596 
597 /// @brief Interpolate between m1 and m2.
598 /// Converts to quaternion form and uses slerp
599 /// m1 and m2 must be rotation matrices!
600 template <typename T, typename T0>
601 Mat3<T> slerp(const Mat3<T0> &m1, const Mat3<T0> &m2, T t)
602 {
603  using MatType = Mat3<T>;
604 
605  Quat<T> q1(m1);
606  Quat<T> q2(m2);
607 
608  if (q1.dot(q2) < 0) q2 *= -1;
609 
610  Quat<T> qslerp = slerp<T>(q1, q2, static_cast<T>(t));
611  MatType m = rotation<MatType>(qslerp);
612  return m;
613 }
614 
615 
616 
617 /// Interpolate between m1 and m4 by converting m1 ... m4 into
618 /// quaternions and treating them as control points of a Bezier
619 /// curve using slerp in place of lerp in the De Castlejeau evaluation
620 /// algorithm. Just like a cubic Bezier curve, this will interpolate
621 /// m1 at t = 0 and m4 at t = 1 but in general will not pass through
622 /// m2 and m3. Unlike a standard Bezier curve this curve will not have
623 /// the convex hull property.
624 /// m1 ... m4 must be rotation matrices!
625 template <typename T, typename T0>
626 Mat3<T> bezLerp(const Mat3<T0> &m1, const Mat3<T0> &m2,
627  const Mat3<T0> &m3, const Mat3<T0> &m4,
628  T t)
629 {
630  Mat3<T> m00, m01, m02, m10, m11;
631 
632  m00 = slerp(m1, m2, t);
633  m01 = slerp(m2, m3, t);
634  m02 = slerp(m3, m4, t);
635 
636  m10 = slerp(m00, m01, t);
637  m11 = slerp(m01, m02, t);
638 
639  return slerp(m10, m11, t);
640 }
641 
644 
645 } // namespace math
646 
647 
648 template<> inline math::Quats zeroVal<math::Quats >() { return math::Quats::zero(); }
649 template<> inline math::Quatd zeroVal<math::Quatd >() { return math::Quatd::zero(); }
650 
651 } // namespace OPENVDB_VERSION_NAME
652 } // namespace openvdb
653 
654 #endif //OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
655 
656 // Copyright (c) 2012-2018 DreamWorks Animation LLC
657 // All rights reserved. This software is distributed under the
658 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
GLuint GLuint stream
Definition: glcorearb.h:1831
Quat operator*(T scalar) const
Return (this*scalar), e.g. q = q1 * scalar;.
Definition: Quat.h:425
SYS_API double cos(double x)
Definition: SYS_FPUMath.h:69
Quat(T x, T y, T z, T w)
Constructor with four arguments, e.g. Quatf q(1,2,3,4);.
Definition: Quat.h:112
Vec3< T > axis() const
Return axis of rotation.
Definition: Quat.h:270
Quat & sub(const Quat &q1, const Quat &q2)
Definition: Quat.h:454
T operator[](int i) const
Array style constant reference to the components, e.g. float f = q[1];.
Definition: Quat.h:242
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:395
Quat derivative(const Vec3< T > &omega) const
Definition: Quat.h:500
Quat & scale(T scale, const Quat &q)
Definition: Quat.h:482
const GLdouble * v
Definition: glcorearb.h:836
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
Quat operator*(const Quat &q) const
Return (this*q), e.g. q = q1 * q2;.
Definition: Quat.h:404
GA_API const UT_StringHolder rot
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:645
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
bool operator==(const Quat &q) const
Equality operator, does exact floating point comparisons.
Definition: Quat.h:343
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
Quat unit() const
this = normalized this
Definition: Quat.h:518
GLint y
Definition: glcorearb.h:102
Quat & init(T x, T y, T z, T w)
"this" quaternion gets initialized to [x, y, z, w]
Definition: Quat.h:287
Quat & operator=(const Quat &q)
Assignment operator.
Definition: Quat.h:332
GLuint buffer
Definition: glcorearb.h:659
png_uint_32 i
Definition: png.h:2877
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:365
Quat operator-(const Quat &q) const
Return (this-q), e.g. q = q1 - q2;.
Definition: Quat.h:398
Quat operator+(const Quat &q) const
Return (this+q), e.g. q = q1 + q2;.
Definition: Quat.h:392
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:553
Quat operator*=(const Quat &q)
Assigns this to (this*q), e.g. q *= q1;.
Definition: Quat.h:418
Quat operator/(T scalar) const
Return (this/scalar), e.g. q = q1 / scalar;.
Definition: Quat.h:431
Quat inverse(T tolerance=T(0))
returns inverse of this
Definition: Quat.h:528
Mat3< T > bezLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, const Mat3< T0 > &m3, const Mat3< T0 > &m4, T t)
Definition: Quat.h:626
Quat(T *a)
Constructor with array argument, e.g. float a[4]; Quatf q(a);.
Definition: Quat.h:122
Quat(const Mat3< T1 > &rot)
Constructor given a rotation matrix.
Definition: Quat.h:161
Quat & operator-=(const Quat &q)
Subtract quaternion q from "this" quaternion, e.g. q -= q1;.
Definition: Quat.h:370
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:352
void read(std::istream &is)
Definition: Quat.h:586
Quat(math::Axis axis, T angle)
Constructor given rotation as axis and angle.
Definition: Quat.h:148
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
bool normalize(T eps=T(1.0e-8))
this = normalized this
Definition: Quat.h:509
Vec3< T > rotateVector(const Vec3< T > &v) const
Return rotated vector by "this" quaternion.
Definition: Quat.h:548
Quat(const Quat &q)
Copy constructor.
Definition: Quat.h:214
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:472
bool isUnitary(const MatType &m)
Determine if a matrix is unitary (i.e., rotation or reflection).
Definition: Mat.h:921
Quat operator-() const
Negation operator, e.g. q = -q;.
Definition: Quat.h:437
Vec3< T > eulerAngles(RotationOrder rotationOrder) const
Returns vector of x,y,z rotational components.
Definition: Quat.h:328
T det() const
Determinant of matrix.
Definition: Mat3.h:527
SYS_API double acos(double x)
Definition: SYS_FPUMath.h:83
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
Quat & setIdentity()
Set "this" vector to identity.
Definition: Quat.h:320
Quat()
Trivial constructor, the quaternion is NOT initialized.
Definition: Quat.h:109
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:358
Quat & init()
"this" quaternion gets initialized to identity, same as setIdentity()
Definition: Quat.h:294
T dot(const Quat &q) const
Dot product.
Definition: Quat.h:493
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Quat.h:249
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:110
T & operator[](int i)
Array style reference to the components, e.g. q[3] = 1.34f;.
Definition: Quat.h:239
T trace() const
Trace of matrix.
Definition: Mat3.h:536
T & x()
Reference to the component, e.g. q.x() = 4.5f;.
Definition: Quat.h:224
T angle() const
Return angle of rotation.
Definition: Quat.h:255
GLint GLenum GLint x
Definition: glcorearb.h:408
void write(std::ostream &os) const
Definition: Quat.h:585
Quat & setAxisAngle(const Vec3< T > &axis, T angle)
Definition: Quat.h:298
T x() const
Get the component, e.g. float f = q.w();.
Definition: Quat.h:230
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
Quat & operator*=(T scalar)
Scale "this" quaternion by scalar, e.g. q *= scalar;.
Definition: Quat.h:381
Quat & add(const Quat &q1, const Quat &q2)
Definition: Quat.h:442
Quat< T > slerp(const Quat< T > &q1, const Quat< T > &q2, T t, T tolerance=0.00001)
Linear interpolation between the two quaternions.
Definition: Quat.h:54
static Quat zero()
Predefined constants, e.g. Quat q = Quat::identity();.
Definition: Quat.h:555
Quat & mult(const Quat &q1, const Quat &q2)
Definition: Quat.h:466
Quat & setZero()
Set "this" vector to zero.
Definition: Quat.h:313
Quat(const Vec3< T > &axis, T angle)
Definition: Quat.h:133
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
SYS_API double sin(double x)
Definition: SYS_FPUMath.h:71
Quat & operator+=(const Quat &q)
Add quaternion q to "this" quaternion, e.g. q += q1;.
Definition: Quat.h:359
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Quat.h:252
friend std::ostream & operator<<(std::ostream &stream, const Quat &q)
Output to the stream, e.g. std::cout << q << std::endl;.
Definition: Quat.h:577