Go to the documentation of this file.
4 #ifndef OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
5 #define OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
23 template<
typename T>
class Quat;
29 T qdot,
angle, sineAngle;
33 if (fabs(qdot) >= 1.0) {
38 sineAngle = sin(
angle);
47 if (sineAngle <= tolerance) {
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]);
58 double lengthSquared = qtemp.
dot(qtemp);
60 if (lengthSquared <= tolerance * tolerance) {
61 qtemp = (t < 0.5) ? q1 : q2;
63 qtemp *= 1.0 / sqrt(lengthSquared);
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]);
83 static const int size = 4;
114 T s = T(sin(
angle*T(0.5)));
116 mm[0] = axis.
x() * s;
117 mm[1] = axis.
y() * s;
118 mm[2] = axis.
z() * s;
120 mm[3] = T(cos(
angle*T(0.5)));
127 T s = T(sin(
angle*T(0.5)));
133 mm[3] = T(cos(
angle*T(0.5)));
137 template<
typename T1>
143 "A non-rotation matrix can not be used to construct a quaternion");
147 "A reflection matrix can not be used to construct a quaternion");
150 T trace(rot.
trace());
153 T q_w = 0.5 * std::sqrt(trace+1);
154 T factor = 0.25 / q_w;
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));
160 }
else if (rot(0,0) > rot(1,1) && rot(0,0) > rot(2,2)) {
162 T q_x = 0.5 * sqrt(rot(0,0)- rot(1,1)-rot(2,2)+1);
163 T factor = 0.25 / 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)) {
171 T q_y = 0.5 * sqrt(rot(1,1)-rot(0,0)-rot(2,2)+1);
172 T factor = 0.25 / q_y;
174 mm[0] = factor * (rot(0,1) + rot(1,0));
176 mm[2] = factor * (rot(1,2) + rot(2,1));
177 mm[3] = factor * (rot(2,0) - rot(0,2));
180 T q_z = 0.5 * sqrt(rot(2,2)-rot(0,0)-rot(1,1)+1);
181 T factor = 0.25 / q_z;
183 mm[0] = factor * (rot(2,0) + rot(0,2));
184 mm[1] = factor * (rot(1,2) + rot(2,1));
186 mm[3] = factor * (rot(0,1) - rot(1,0));
201 T&
x() {
return mm[0]; }
202 T&
y() {
return mm[1]; }
203 T&
z() {
return mm[2]; }
204 T&
w() {
return mm[3]; }
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]; }
222 operator T*() {
return mm; }
223 operator const T*()
const {
return mm; }
234 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
236 if ( sqrLength > 1.0e-8 ) {
238 return T(T(2.0) * acos(mm[3]));
249 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
251 if ( sqrLength > 1.0e-8 ) {
253 T invLength = T(T(1)/sqrt(sqrLength));
255 return Vec3<T>( mm[0]*invLength, mm[1]*invLength, mm[2]*invLength );
266 mm[0] = x; mm[1] = y; mm[2] = z; mm[3] = w;
278 T s = T(sin(
angle*T(0.5)));
280 mm[0] = axis.
x() * s;
281 mm[1] = axis.
y() * s;
282 mm[2] = axis.
z() * s;
284 mm[3] = T(cos(
angle*T(0.5)));
292 mm[0] = mm[1] = mm[2] = mm[3] = 0;
299 mm[0] = mm[1] = mm[2] = 0;
329 bool eq(
const Quat &q, T eps=1.0e-7)
const
371 return Quat<T>(mm[0]+q.
mm[0], mm[1]+q.
mm[1], mm[2]+q.
mm[2], mm[3]+q.
mm[3]);
377 return Quat<T>(mm[0]-q.
mm[0], mm[1]-q.
mm[1], mm[2]-q.
mm[2], mm[3]-q.
mm[3]);
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];
404 return Quat<T>(mm[0]*scalar, mm[1]*scalar, mm[2]*scalar, mm[3]*scalar);
410 return Quat<T>(mm[0]/scalar, mm[1]/scalar, mm[2]/scalar, mm[3]/scalar);
415 {
return Quat<T>(-mm[0], -mm[1], -mm[2], -mm[3]); }
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];
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];
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];
472 return (mm[0]*q.
mm[0] + mm[1]*q.
mm[1] + mm[2]*q.
mm[2] + mm[3]*q.
mm[3]);
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() );
488 T d = T(sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]));
497 T d = sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]);
500 "Normalizing degenerate quaternion");
507 T d = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3];
510 "Cannot invert degenerate quaternion");
511 Quat result = *
this/-d;
512 result.
mm[3] = -result.
mm[3];
521 return Quat<T>(-mm[0], -mm[1], -mm[2], mm[3]);
538 std::ostringstream buffer;
543 for (
unsigned j(0); j < 4; j++) {
544 if (j) buffer <<
", ";
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); }
570 template <
typename S,
typename T>
577 template <
typename T,
typename T0>
585 if (q1.
dot(q2) < 0) q2 *= -1;
587 Quat<T> qslerp = slerp<T>(q1, q2,
static_cast<T
>(t));
588 MatType m = rotation<MatType>(qslerp);
602 template <
typename T,
typename T0>
607 Mat3<T> m00, m01, m02, m10, m11;
609 m00 =
slerp(m1, m2, t);
610 m01 =
slerp(m2, m3, t);
611 m02 =
slerp(m3, m4, t);
613 m10 =
slerp(m00, m01, t);
614 m11 =
slerp(m01, m02, t);
616 return slerp(m10, m11, t);
625 template<>
inline math::Quats zeroVal<math::Quats >() {
return math::Quats::zero(); }
626 template<>
inline math::Quatd zeroVal<math::Quatd >() {
return math::Quatd::zero(); }
631 #endif //OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Quat.h:226
Quat & setIdentity()
Set "this" vector to identity.
Definition: Quat.h:297
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:83
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:371
T z() const
Definition: Quat.h:209
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:445
Quat operator/(T scalar) const
Return (this/scalar), e.g. q = q1 / scalar;.
Definition: Quat.h:408
static Quat identity()
Definition: Quat.h:533
std::string str() const
Definition: Quat.h:536
@ Z_AXIS
Definition: Math.h:872
Quat & init()
"this" quaternion gets initialized to identity, same as setIdentity()
Definition: Quat.h:271
Quat & add(const Quat &q1, const Quat &q2)
Definition: Quat.h:419
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
T det() const
Determinant of matrix.
Definition: Mat3.h:500
Quat operator*(const Quat &q) const
Return (this*q), e.g. q = q1 * q2;.
Definition: Quat.h:381
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
Quat operator-() const
Negation operator, e.g. q = -q;.
Definition: Quat.h:414
T ValueType
Definition: Quat.h:82
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
Quat & sub(const Quat &q1, const Quat &q2)
Definition: Quat.h:431
bool normalize(T eps=T(1.0e-8))
this = normalized this
Definition: Quat.h:486
T y() const
Definition: Quat.h:208
T dot(const Quat &q) const
Dot product.
Definition: Quat.h:470
Quat operator*=(const Quat &q)
Assigns this to (this*q), e.g. q *= q1;.
Definition: Quat.h:395
Quat & init(T x, T y, T z, T w)
"this" quaternion gets initialized to [x, y, z, w]
Definition: Quat.h:264
Vec3< T > eulerAngles(RotationOrder rotationOrder) const
Returns vector of x,y,z rotational components.
Definition: Quat.h:305
Quat operator-(const Quat &q) const
Return (this-q), e.g. q = q1 - q2;.
Definition: Quat.h:375
3x3 matrix class.
Definition: Mat3.h:29
T x() const
Get the component, e.g. float f = q.w();.
Definition: Quat.h:207
T mm[4]
Definition: Quat.h:566
@ X_AXIS
Definition: Math.h:870
T & y()
Definition: Quat.h:202
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Quat.h:229
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
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:620
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:526
T & x()
Reference to the component, e.g. q.x() = 4.5f;.
Definition: Quat.h:201
Quat derivative(const Vec3< T > &omega) const
Definition: Quat.h:477
Quat & operator-=(const Quat &q)
Subtract quaternion q from "this" quaternion, e.g. q -= q1;.
Definition: Quat.h:347
T & y()
Definition: Vec3.h:84
Quat & scale(T scale, const Quat &q)
Definition: Quat.h:459
@ Y_AXIS
Definition: Math.h:871
T & z()
Definition: Quat.h:203
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
T w() const
Definition: Quat.h:210
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:408
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
T & z()
Definition: Vec3.h:85
void write(std::ostream &os) const
Definition: Quat.h:562
RotationOrder
Definition: Math.h:876
Quat(const Vec3< T > &axis, T angle)
Definition: Quat.h:110
void read(std::istream &is)
Definition: Quat.h:563
bool operator==(const Quat &q) const
Equality operator, does exact floating point comparisons.
Definition: Quat.h:320
T trace() const
Trace of matrix.
Definition: Mat3.h:509
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
static unsigned numElements()
Definition: Quat.h:213
Quat & operator=(const Quat &q)
Assignment operator.
Definition: Quat.h:309
Quat operator+(const Quat &q) const
Return (this+q), e.g. q = q1 + q2;.
Definition: Quat.h:369
bool isUnitary(const MatType &m)
Determine if a matrix is unitary (i.e., rotation or reflection).
Definition: Mat.h:894
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
Quat & operator*=(T scalar)
Scale "this" quaternion by scalar, e.g. q *= scalar;.
Definition: Quat.h:358
Quat(const Mat3< T1 > &rot)
Constructor given a rotation matrix.
Definition: Quat.h:138
Quat unit() const
this = normalized this
Definition: Quat.h:495
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
Quat()
Trivial constructor, the quaternion is NOT initialized.
Definition: Quat.h:86
Quat(T *a)
Constructor with array argument, e.g. float a[4]; Quatf q(a);.
Definition: Quat.h:99
Quat conjugate() const
Definition: Quat.h:519
Quat(const Quat &q)
Copy constructor.
Definition: Quat.h:191
Quat & mult(const Quat &q1, const Quat &q2)
Definition: Quat.h:443
T & w()
Definition: Quat.h:204
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
Definition: Exceptions.h:56
T operator[](int i) const
Array style constant reference to the components, e.g. float f = q[1];.
Definition: Quat.h:219
Quat(math::Axis axis, T angle)
Constructor given rotation as axis and angle.
Definition: Quat.h:125
static Quat zero()
Predefined constants, e.g. Quat q = Quat::identity();.
Definition: Quat.h:532
Quat & setZero()
Set "this" vector to zero.
Definition: Quat.h:290
Quat inverse(T tolerance=T(0)) const
returns inverse of this
Definition: Quat.h:505
T & operator[](int i)
Array style reference to the components, e.g. q[3] = 1.34f;.
Definition: Quat.h:216
Definition: Exceptions.h:13
Vec3< T > rotateVector(const Vec3< T > &v) const
Return rotated vector by "this" quaternion.
Definition: Quat.h:525
T value_type
Definition: Quat.h:81
T angle() const
Return angle of rotation.
Definition: Quat.h:232
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
Quat operator*(T scalar) const
Return (this*scalar), e.g. q = q1 * scalar;.
Definition: Quat.h:402
Quat & operator+=(const Quat &q)
Add quaternion q to "this" quaternion, e.g. q += q1;.
Definition: Quat.h:336
Quat & setAxisAngle(const Vec3< T > &axis, T angle)
Definition: Quat.h:275
Axis
Definition: Math.h:869
Vec3< T > axis() const
Return axis of rotation.
Definition: Quat.h:247