31 #ifndef OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED 32 #define OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED 49 template<
typename T>
class Quat;
55 T qdot,
angle, sineAngle;
59 if (fabs(qdot) >= 1.0) {
64 sineAngle = sin(angle);
73 if (sineAngle <= tolerance) {
76 Quat<T> qtemp(s * q1[0] + t * q2[0], s * q1[1] + t * q2[1],
77 s * q1[2] + t * q2[2], s * q1[3] + t * q2[3]);
84 double lengthSquared = qtemp.
dot(qtemp);
86 if (lengthSquared <= tolerance * tolerance) {
87 qtemp = (t < 0.5) ? q1 : q2;
89 qtemp *= 1.0 / sqrt(lengthSquared);
94 T sine = 1.0 / sineAngle;
95 T a = sin((1.0 - t) * angle) * sine;
96 T b = sin(t * angle) * sine;
97 return Quat<T>(a * q1[0] + b * q2[0], a * q1[1] + b * q2[1],
98 a * q1[2] + b * q2[2], a * q1[3] + b * q2[3]);
136 T s = T(sin(angle*T(0.5)));
138 mm[0] = axis.
x() * s;
139 mm[1] = axis.
y() * s;
140 mm[2] = axis.
z() * s;
142 mm[3] = T(cos(angle*T(0.5)));
149 T s = T(sin(angle*T(0.5)));
155 mm[3] = T(cos(angle*T(0.5)));
159 template<
typename T1>
165 "A non-rotation matrix can not be used to construct a quaternion");
169 "A reflection matrix can not be used to construct a quaternion");
172 T trace(rot.
trace());
175 T q_w = 0.5 * std::sqrt(trace+1);
176 T factor = 0.25 / q_w;
178 mm[0] = factor * (rot(1,2) - rot(2,1));
179 mm[1] = factor * (rot(2,0) - rot(0,2));
180 mm[2] = factor * (rot(0,1) - rot(1,0));
182 }
else if (rot(0,0) > rot(1,1) && rot(0,0) > rot(2,2)) {
184 T q_x = 0.5 * sqrt(rot(0,0)- rot(1,1)-rot(2,2)+1);
185 T factor = 0.25 / q_x;
188 mm[1] = factor * (rot(0,1) + rot(1,0));
189 mm[2] = factor * (rot(2,0) + rot(0,2));
190 mm[3] = factor * (rot(1,2) - rot(2,1));
191 }
else if (rot(1,1) > rot(2,2)) {
193 T q_y = 0.5 * sqrt(rot(1,1)-rot(0,0)-rot(2,2)+1);
194 T factor = 0.25 / q_y;
196 mm[0] = factor * (rot(0,1) + rot(1,0));
198 mm[2] = factor * (rot(1,2) + rot(2,1));
199 mm[3] = factor * (rot(2,0) - rot(0,2));
202 T q_z = 0.5 * sqrt(rot(2,2)-rot(0,0)-rot(1,1)+1);
203 T factor = 0.25 / q_z;
205 mm[0] = factor * (rot(2,0) + rot(0,2));
206 mm[1] = factor * (rot(1,2) + rot(2,1));
208 mm[3] = factor * (rot(0,1) - rot(1,0));
223 T&
x() {
return mm[0]; }
224 T&
y() {
return mm[1]; }
225 T&
z() {
return mm[2]; }
226 T&
w() {
return mm[3]; }
229 T
x()
const {
return mm[0]; }
230 T
y()
const {
return mm[1]; }
231 T
z()
const {
return mm[2]; }
232 T
w()
const {
return mm[3]; }
244 operator T*() {
return mm; }
245 operator const T*()
const {
return mm; }
256 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
258 if ( sqrLength > 1.0e-8 ) {
260 return T(T(2.0) * acos(mm[3]));
271 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
273 if ( sqrLength > 1.0e-8 ) {
275 T invLength = T(T(1)/sqrt(sqrLength));
277 return Vec3<T>( mm[0]*invLength, mm[1]*invLength, mm[2]*invLength );
288 mm[0] = x; mm[1] = y; mm[2] = z; mm[3] = w;
300 T s = T(sin(angle*T(0.5)));
302 mm[0] = axis.
x() * s;
303 mm[1] = axis.
y() * s;
304 mm[2] = axis.
z() * s;
306 mm[3] = T(cos(angle*T(0.5)));
314 mm[0] = mm[1] = mm[2] = mm[3] = 0;
321 mm[0] = mm[1] = mm[2] = 0;
351 bool eq(
const Quat &q, T eps=1.0e-7)
const 393 return Quat<T>(mm[0]+q.
mm[0], mm[1]+q.
mm[1], mm[2]+q.
mm[2], mm[3]+q.
mm[3]);
399 return Quat<T>(mm[0]-q.
mm[0], mm[1]-q.
mm[1], mm[2]-q.
mm[2], mm[3]-q.
mm[3]);
407 prod.
mm[0] = mm[3]*q.
mm[0] + mm[0]*q.
mm[3] + mm[1]*q.
mm[2] - mm[2]*q.
mm[1];
408 prod.
mm[1] = mm[3]*q.
mm[1] + mm[1]*q.
mm[3] + mm[2]*q.
mm[0] - mm[0]*q.
mm[2];
409 prod.
mm[2] = mm[3]*q.
mm[2] + mm[2]*q.
mm[3] + mm[0]*q.
mm[1] - mm[1]*q.
mm[0];
410 prod.
mm[3] = mm[3]*q.
mm[3] - mm[0]*q.
mm[0] - mm[1]*q.
mm[1] - mm[2]*q.
mm[2];
426 return Quat<T>(mm[0]*scalar, mm[1]*scalar, mm[2]*scalar, mm[3]*scalar);
432 return Quat<T>(mm[0]/scalar, mm[1]/scalar, mm[2]/scalar, mm[3]/scalar);
437 {
return Quat<T>(-mm[0], -mm[1], -mm[2], -mm[3]); }
443 mm[0] = q1.
mm[0] + q2.
mm[0];
444 mm[1] = q1.
mm[1] + q2.
mm[1];
445 mm[2] = q1.
mm[2] + q2.
mm[2];
446 mm[3] = q1.
mm[3] + q2.
mm[3];
455 mm[0] = q1.
mm[0] - q2.
mm[0];
456 mm[1] = q1.
mm[1] - q2.
mm[1];
457 mm[2] = q1.
mm[2] - q2.
mm[2];
458 mm[3] = q1.
mm[3] - q2.
mm[3];
467 mm[0] = q1.
mm[3]*q2.
mm[0] + q1.
mm[0]*q2.
mm[3] +
468 q1.
mm[1]*q2.
mm[2] - q1.
mm[2]*q2.
mm[1];
469 mm[1] = q1.
mm[3]*q2.
mm[1] + q1.
mm[1]*q2.
mm[3] +
470 q1.
mm[2]*q2.
mm[0] - q1.
mm[0]*q2.
mm[2];
471 mm[2] = q1.
mm[3]*q2.
mm[2] + q1.
mm[2]*q2.
mm[3] +
472 q1.
mm[0]*q2.
mm[1] - q1.
mm[1]*q2.
mm[0];
473 mm[3] = q1.
mm[3]*q2.
mm[3] - q1.
mm[0]*q2.
mm[0] -
474 q1.
mm[1]*q2.
mm[1] - q1.
mm[2]*q2.
mm[2];
483 mm[0] = scale * q.
mm[0];
484 mm[1] = scale * q.
mm[1];
485 mm[2] = scale * q.
mm[2];
486 mm[3] = scale * q.
mm[3];
494 return (mm[0]*q.
mm[0] + mm[1]*q.
mm[1] + mm[2]*q.
mm[2] + mm[3]*q.
mm[3]);
501 return Quat<T>( +w()*omega.
x() -z()*omega.
y() +y()*omega.
z() ,
502 +z()*omega.
x() +w()*omega.
y() -x()*omega.
z() ,
503 -y()*omega.
x() +x()*omega.
y() +w()*omega.
z() ,
504 -x()*omega.
x() -y()*omega.
y() -z()*omega.
z() );
510 T d = T(sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]));
519 T d = sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]);
522 "Normalizing degenerate quaternion");
529 T d = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3];
532 "Cannot invert degenerate quaternion");
533 Quat result = *
this/-d;
534 result.
mm[3] = -result.
mm[3];
543 return Quat<T>(-mm[0], -mm[1], -mm[2], mm[3]);
560 std::ostringstream buffer;
565 for (
unsigned j(0); j < 4; j++) {
566 if (j) buffer <<
", ";
582 friend Quat slerp<>(
const Quat &q1,
const Quat &q2, T t, T tolerance);
584 void write(std::ostream& os)
const { os.write(static_cast<char*>(&mm),
sizeof(T) * 4); }
585 void read(std::istream& is) { is.read(static_cast<char*>(&mm),
sizeof(T) * 4); }
592 template <
typename S,
typename T>
599 template <
typename T,
typename T0>
607 if (q1.
dot(q2) < 0) q2 *= -1;
609 Quat<T> qslerp = slerp<T>(q1, q2,
static_cast<T
>(t));
610 MatType m = rotation<MatType>(qslerp);
624 template <
typename T,
typename T0>
629 Mat3<T> m00, m01, m02, m10, m11;
631 m00 =
slerp(m1, m2, t);
632 m01 =
slerp(m2, m3, t);
633 m02 =
slerp(m3, m4, t);
635 m10 =
slerp(m00, m01, t);
636 m11 =
slerp(m01, m02, t);
638 return slerp(m10, m11, t);
647 template<>
inline math::Quats zeroVal<math::Quats >() {
return math::Quats::zero(); }
648 template<>
inline math::Quatd zeroVal<math::Quatd >() {
return math::Quatd::zero(); }
653 #endif //OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED Quat & mult(const Quat &q1, const Quat &q2)
Definition: Quat.h:465
Quat operator-(const Quat &q) const
Return (this-q), e.g. q = q1 - q2;.
Definition: Quat.h:397
Quat< T > operator*(S scalar, const Quat< T > &q)
Returns V, where for .
Definition: Quat.h:593
Quat & setAxisAngle(const Vec3< T > &axis, T angle)
Definition: Quat.h:297
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:370
T w() const
Definition: Quat.h:232
T z() const
Definition: Quat.h:231
static Quat identity()
Definition: Quat.h:555
T & w()
Definition: Quat.h:226
T mm[4]
Definition: Quat.h:588
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Quat(const Mat3< T1 > &rot)
Constructor given a rotation matrix.
Definition: Quat.h:160
T & y()
Definition: Quat.h:224
bool operator==(const Quat &q) const
Equality operator, does exact floating point comparisons.
Definition: Quat.h:342
T y() const
Definition: Quat.h:230
Quat(math::Axis axis, T angle)
Constructor given rotation as axis and angle.
Definition: Quat.h:147
Quat conjugate() const
Definition: Quat.h:541
Quat()
Trivial constructor, the quaternion is NOT initialized.
Definition: Quat.h:108
T dot(const Quat &q) const
Dot product.
Definition: Quat.h:492
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:480
T & z()
Definition: Vec3.h:111
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
Quat(const Vec3< T > &axis, T angle)
Definition: Quat.h:132
Quat operator*(T scalar) const
Return (this*scalar), e.g. q = q1 * scalar;.
Definition: Quat.h:424
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:610
friend std::ostream & operator<<(std::ostream &stream, const Quat &q)
Output to the stream, e.g. std::cout << q << std::endl;.
Definition: Quat.h:576
static Quat zero()
Predefined constants, e.g. Quat q = Quat::identity();.
Definition: Quat.h:554
Quat & operator-=(const Quat &q)
Subtract quaternion q from "this" quaternion, e.g. q -= q1;.
Definition: Quat.h:369
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:600
Quat & setZero()
Set "this" vector to zero.
Definition: Quat.h:312
RotationOrder
Definition: Math.h:863
void read(std::istream &is)
Definition: Quat.h:585
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Quat(T *a)
Constructor with array argument, e.g. float a[4]; Quatf q(a);.
Definition: Quat.h:121
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Quat.h:251
3x3 matrix class.
Definition: Mat3.h:54
T trace() const
Trace of matrix.
Definition: Mat3.h:542
T det() const
Determinant of matrix.
Definition: Mat3.h:533
Quat operator/(T scalar) const
Return (this/scalar), e.g. q = q1 / scalar;.
Definition: Quat.h:430
T & x()
Reference to the component, e.g. q.x() = 4.5f;.
Definition: Quat.h:223
Quat(T x, T y, T z, T w)
Constructor with four arguments, e.g. Quatf q(1,2,3,4);.
Definition: Quat.h:111
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Quat.h:248
Definition: Exceptions.h:39
Quat< double > Quatd
Definition: Quat.h:642
T operator[](int i) const
Array style constant reference to the components, e.g. float f = q[1];.
Definition: Quat.h:241
bool normalize(T eps=T(1.0e-8))
this = normalized this
Definition: Quat.h:508
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:109
Quat derivative(const Vec3< T > &omega) const
Definition: Quat.h:499
Quat(const Quat &q)
Copy constructor.
Definition: Quat.h:213
T & y()
Definition: Vec3.h:110
Vec3< T > rotateVector(const Vec3< T > &v) const
Return rotated vector by "this" quaternion.
Definition: Quat.h:547
Quat operator-() const
Negation operator, e.g. q = -q;.
Definition: Quat.h:436
Quat inverse(T tolerance=T(0))
returns inverse of this
Definition: Quat.h:527
std::string str() const
Definition: Quat.h:559
Quat & operator+=(const Quat &q)
Add quaternion q to "this" quaternion, e.g. q += q1;.
Definition: Quat.h:358
Definition: Exceptions.h:82
Quat & sub(const Quat &q1, const Quat &q2)
Definition: Quat.h:453
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:330
bool isUnitary(const MatType &m)
Determine if a matrix is unitary (i.e., rotation or reflection).
Definition: Mat.h:884
Quat & init(T x, T y, T z, T w)
"this" quaternion gets initialized to [x, y, z, w]
Definition: Quat.h:286
void write(std::ostream &os) const
Definition: Quat.h:584
Quat operator*=(const Quat &q)
Assigns this to (this*q), e.g. q *= q1;.
Definition: Quat.h:417
Quat unit() const
this = normalized this
Definition: Quat.h:517
Quat operator+(const Quat &q) const
Return (this+q), e.g. q = q1 + q2;.
Definition: Quat.h:391
Quat & operator=(const Quat &q)
Assignment operator.
Definition: Quat.h:331
Quat operator*(const Quat &q) const
Return (this*q), e.g. q = q1 * q2;.
Definition: Quat.h:403
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
Vec3< T > eulerAngles(RotationOrder rotationOrder) const
Returns vector of x,y,z rotational components.
Definition: Quat.h:327
T x() const
Get the component, e.g. float f = q.w();.
Definition: Quat.h:229
Quat & init()
"this" quaternion gets initialized to identity, same as setIdentity()
Definition: Quat.h:293
T & operator[](int i)
Array style reference to the components, e.g. q[3] = 1.34f;.
Definition: Quat.h:238
Axis
Definition: Math.h:856
Quat & operator*=(T scalar)
Scale "this" quaternion by scalar, e.g. q *= scalar;.
Definition: Quat.h:380
Quat & setIdentity()
Set "this" vector to identity.
Definition: Quat.h:319
Mat3< T > bezLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, const Mat3< T0 > &m3, const Mat3< T0 > &m4, T t)
Definition: Quat.h:625
T angle() const
Return angle of rotation.
Definition: Quat.h:254
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
static unsigned numElements()
Definition: Quat.h:235
Vec3< T > axis() const
Return axis of rotation.
Definition: Quat.h:269
T & z()
Definition: Quat.h:225
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:559
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:351
Quat & add(const Quat &q1, const Quat &q2)
Definition: Quat.h:441
Quat< float > Quats
Definition: Quat.h:641
Quat & scale(T scale, const Quat &q)
Definition: Quat.h:481