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;
86 #if OPENVDB_ABI_VERSION_NUMBER >= 8 141 T s = T(sin(angle*T(0.5)));
143 mm[0] = axis.
x() * s;
144 mm[1] = axis.
y() * s;
145 mm[2] = axis.
z() * s;
147 mm[3] = T(cos(angle*T(0.5)));
154 T s = T(sin(angle*T(0.5)));
160 mm[3] = T(cos(angle*T(0.5)));
164 template<
typename T1>
170 "A non-rotation matrix can not be used to construct a quaternion");
174 "A reflection matrix can not be used to construct a quaternion");
177 T trace(rot.
trace());
180 T q_w = 0.5 * std::sqrt(trace+1);
181 T factor = 0.25 / q_w;
183 mm[0] = factor * (rot(1,2) - rot(2,1));
184 mm[1] = factor * (rot(2,0) - rot(0,2));
185 mm[2] = factor * (rot(0,1) - rot(1,0));
187 }
else if (rot(0,0) > rot(1,1) && rot(0,0) > rot(2,2)) {
189 T q_x = 0.5 * sqrt(rot(0,0)- rot(1,1)-rot(2,2)+1);
190 T factor = 0.25 / q_x;
193 mm[1] = factor * (rot(0,1) + rot(1,0));
194 mm[2] = factor * (rot(2,0) + rot(0,2));
195 mm[3] = factor * (rot(1,2) - rot(2,1));
196 }
else if (rot(1,1) > rot(2,2)) {
198 T q_y = 0.5 * sqrt(rot(1,1)-rot(0,0)-rot(2,2)+1);
199 T factor = 0.25 / q_y;
201 mm[0] = factor * (rot(0,1) + rot(1,0));
203 mm[2] = factor * (rot(1,2) + rot(2,1));
204 mm[3] = factor * (rot(2,0) - rot(0,2));
207 T q_z = 0.5 * sqrt(rot(2,2)-rot(0,0)-rot(1,1)+1);
208 T factor = 0.25 / q_z;
210 mm[0] = factor * (rot(2,0) + rot(0,2));
211 mm[1] = factor * (rot(1,2) + rot(2,1));
213 mm[3] = factor * (rot(0,1) - rot(1,0));
218 T&
x() {
return mm[0]; }
219 T&
y() {
return mm[1]; }
220 T&
z() {
return mm[2]; }
221 T&
w() {
return mm[3]; }
224 T
x()
const {
return mm[0]; }
225 T
y()
const {
return mm[1]; }
226 T
z()
const {
return mm[2]; }
227 T
w()
const {
return mm[3]; }
239 operator T*() {
return mm; }
240 operator const T*()
const {
return mm; }
251 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
253 if ( sqrLength > 1.0e-8 ) {
255 return T(T(2.0) * acos(mm[3]));
266 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
268 if ( sqrLength > 1.0e-8 ) {
270 T invLength = T(T(1)/sqrt(sqrLength));
272 return Vec3<T>( mm[0]*invLength, mm[1]*invLength, mm[2]*invLength );
283 mm[0] = x; mm[1] = y; mm[2] = z; mm[3] = w;
295 T s = T(sin(angle*T(0.5)));
297 mm[0] = axis.
x() * s;
298 mm[1] = axis.
y() * s;
299 mm[2] = axis.
z() * s;
301 mm[3] = T(cos(angle*T(0.5)));
309 mm[0] = mm[1] = mm[2] = mm[3] = 0;
316 mm[0] = mm[1] = mm[2] = 0;
335 bool eq(
const Quat &q, T eps=1.0e-7)
const 377 return Quat<T>(mm[0]+q.
mm[0], mm[1]+q.
mm[1], mm[2]+q.
mm[2], mm[3]+q.
mm[3]);
383 return Quat<T>(mm[0]-q.
mm[0], mm[1]-q.
mm[1], mm[2]-q.
mm[2], mm[3]-q.
mm[3]);
391 prod.
mm[0] = mm[3]*q.
mm[0] + mm[0]*q.
mm[3] + mm[1]*q.
mm[2] - mm[2]*q.
mm[1];
392 prod.
mm[1] = mm[3]*q.
mm[1] + mm[1]*q.
mm[3] + mm[2]*q.
mm[0] - mm[0]*q.
mm[2];
393 prod.
mm[2] = mm[3]*q.
mm[2] + mm[2]*q.
mm[3] + mm[0]*q.
mm[1] - mm[1]*q.
mm[0];
394 prod.
mm[3] = mm[3]*q.
mm[3] - mm[0]*q.
mm[0] - mm[1]*q.
mm[1] - mm[2]*q.
mm[2];
410 return Quat<T>(mm[0]*scalar, mm[1]*scalar, mm[2]*scalar, mm[3]*scalar);
416 return Quat<T>(mm[0]/scalar, mm[1]/scalar, mm[2]/scalar, mm[3]/scalar);
421 {
return Quat<T>(-mm[0], -mm[1], -mm[2], -mm[3]); }
427 mm[0] = q1.
mm[0] + q2.
mm[0];
428 mm[1] = q1.
mm[1] + q2.
mm[1];
429 mm[2] = q1.
mm[2] + q2.
mm[2];
430 mm[3] = q1.
mm[3] + q2.
mm[3];
439 mm[0] = q1.
mm[0] - q2.
mm[0];
440 mm[1] = q1.
mm[1] - q2.
mm[1];
441 mm[2] = q1.
mm[2] - q2.
mm[2];
442 mm[3] = q1.
mm[3] - q2.
mm[3];
451 mm[0] = q1.
mm[3]*q2.
mm[0] + q1.
mm[0]*q2.
mm[3] +
452 q1.
mm[1]*q2.
mm[2] - q1.
mm[2]*q2.
mm[1];
453 mm[1] = q1.
mm[3]*q2.
mm[1] + q1.
mm[1]*q2.
mm[3] +
454 q1.
mm[2]*q2.
mm[0] - q1.
mm[0]*q2.
mm[2];
455 mm[2] = q1.
mm[3]*q2.
mm[2] + q1.
mm[2]*q2.
mm[3] +
456 q1.
mm[0]*q2.
mm[1] - q1.
mm[1]*q2.
mm[0];
457 mm[3] = q1.
mm[3]*q2.
mm[3] - q1.
mm[0]*q2.
mm[0] -
458 q1.
mm[1]*q2.
mm[1] - q1.
mm[2]*q2.
mm[2];
467 mm[0] = scale * q.
mm[0];
468 mm[1] = scale * q.
mm[1];
469 mm[2] = scale * q.
mm[2];
470 mm[3] = scale * q.
mm[3];
478 return (mm[0]*q.
mm[0] + mm[1]*q.
mm[1] + mm[2]*q.
mm[2] + mm[3]*q.
mm[3]);
485 return Quat<T>( +w()*omega.
x() -z()*omega.
y() +y()*omega.
z() ,
486 +z()*omega.
x() +w()*omega.
y() -x()*omega.
z() ,
487 -y()*omega.
x() +x()*omega.
y() +w()*omega.
z() ,
488 -x()*omega.
x() -y()*omega.
y() -z()*omega.
z() );
494 T d = T(sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]));
503 T d = sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]);
506 "Normalizing degenerate quaternion");
513 T d = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3];
516 "Cannot invert degenerate quaternion");
517 Quat result = *
this/-d;
518 result.
mm[3] = -result.
mm[3];
527 return Quat<T>(-mm[0], -mm[1], -mm[2], mm[3]);
544 std::ostringstream buffer;
549 for (
unsigned j(0); j < 4; j++) {
550 if (j) buffer <<
", ";
566 friend Quat slerp<>(
const Quat &q1,
const Quat &q2, T t, T tolerance);
568 void write(std::ostream& os)
const { os.write(static_cast<char*>(&mm),
sizeof(T) * 4); }
569 void read(std::istream& is) { is.read(static_cast<char*>(&mm),
sizeof(T) * 4); }
576 template <
typename S,
typename T>
583 template <
typename T,
typename T0>
591 if (q1.
dot(q2) < 0) q2 *= -1;
593 Quat<T> qslerp = slerp<T>(q1, q2,
static_cast<T
>(t));
594 MatType m = rotation<MatType>(qslerp);
608 template <
typename T,
typename T0>
613 Mat3<T> m00, m01, m02, m10, m11;
615 m00 =
slerp(m1, m2, t);
616 m01 =
slerp(m2, m3, t);
617 m02 =
slerp(m3, m4, t);
619 m10 =
slerp(m00, m01, t);
620 m11 =
slerp(m01, m02, t);
622 return slerp(m10, m11, t);
628 #if OPENVDB_ABI_VERSION_NUMBER >= 8 636 template<>
inline math::Quats zeroVal<math::Quats >() {
return math::Quats::zero(); }
637 template<>
inline math::Quatd zeroVal<math::Quatd >() {
return math::Quatd::zero(); }
642 #endif //OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED Definition: Exceptions.h:56
std::string str() const
Definition: Quat.h:542
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:444
Mat3< T > bezLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, const Mat3< T0 > &m3, const Mat3< T0 > &m4, T t)
Definition: Quat.h:609
T & x()
Reference to the component, e.g. q.x() = 4.5f;.
Definition: Quat.h:218
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
void write(std::ostream &os) const
Definition: Quat.h:568
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Quat(const Vec3< T > &axis, T angle)
Definition: Quat.h:137
T & z()
Definition: Vec3.h:91
T & y()
Definition: Vec3.h:90
RotationOrder
Definition: Math.h:911
Quat(math::Axis axis, T angle)
Constructor given rotation as axis and angle.
Definition: Quat.h:152
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:584
Quat & operator+=(const Quat &q)
Add quaternion q to "this" quaternion, e.g. q += q1;.
Definition: Quat.h:342
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:335
Quat operator*(T scalar) const
Return (this*scalar), e.g. q = q1 * scalar;.
Definition: Quat.h:408
bool isUnitary(const MatType &m)
Determine if a matrix is unitary (i.e., rotation or reflection).
Definition: Mat.h:911
Quat & mult(const Quat &q1, const Quat &q2)
Definition: Quat.h:449
T operator[](int i) const
Array style constant reference to the components, e.g. float f = q[1];.
Definition: Quat.h:236
T dot(const Quat &q) const
Dot product.
Definition: Quat.h:476
static unsigned numElements()
Definition: Quat.h:230
bool operator==(const Quat &q) const
Equality operator, does exact floating point comparisons.
Definition: Quat.h:326
T value_type
Definition: Quat.h:81
T z() const
Definition: Quat.h:226
Quat(T *a)
Constructor with array argument, e.g. float a[4]; Quatf q(a);.
Definition: Quat.h:126
T x() const
Get the component, e.g. float f = q.w();.
Definition: Quat.h:224
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:577
Axis
Definition: Math.h:904
T w() const
Definition: Quat.h:227
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Quat.h:243
Quat & setAxisAngle(const Vec3< T > &axis, T angle)
Definition: Quat.h:292
Quat & operator*=(T scalar)
Scale "this" quaternion by scalar, e.g. q *= scalar;.
Definition: Quat.h:364
Quat(const Mat3< T1 > &rot)
Constructor given a rotation matrix.
Definition: Quat.h:165
Quat & setIdentity()
Set "this" vector to identity.
Definition: Quat.h:314
T & w()
Definition: Quat.h:221
static Quat zero()
Predefined constants, e.g. Quat q = Quat::identity();.
Definition: Quat.h:538
Quat unit() const
this = normalized this
Definition: Quat.h:501
Vec3< T > axis() const
Return axis of rotation.
Definition: Quat.h:264
Quat(T x, T y, T z, T w)
Constructor with four arguments, e.g. Quatf q(1,2,3,4);.
Definition: Quat.h:116
T mm[4]
Definition: Quat.h:572
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:450
Vec3< T > rotateVector(const Vec3< T > &v) const
Return rotated vector by "this" quaternion.
Definition: Quat.h:531
Definition: Exceptions.h:13
T y() const
Definition: Quat.h:225
static Quat identity()
Definition: Quat.h:539
T ValueType
Definition: Quat.h:82
Quat operator*=(const Quat &q)
Assigns this to (this*q), e.g. q *= q1;.
Definition: Quat.h:401
Quat inverse(T tolerance=T(0)) const
returns inverse of this
Definition: Quat.h:511
T trace() const
Trace of matrix.
Definition: Mat3.h:502
Quat & setZero()
Set "this" vector to zero.
Definition: Quat.h:307
Quat operator-(const Quat &q) const
Return (this-q), e.g. q = q1 - q2;.
Definition: Quat.h:381
T & operator[](int i)
Array style reference to the components, e.g. q[3] = 1.34f;.
Definition: Quat.h:233
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:355
3x3 matrix class.
Definition: Mat3.h:28
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:637
Quat & sub(const Quat &q1, const Quat &q2)
Definition: Quat.h:437
Quat & add(const Quat &q1, const Quat &q2)
Definition: Quat.h:425
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Quat.h:246
T & z()
Definition: Quat.h:220
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition: Math.h:407
T & y()
Definition: Quat.h:219
Quat operator/(T scalar) const
Return (this/scalar), e.g. q = q1 / scalar;.
Definition: Quat.h:414
T det() const
Determinant of matrix.
Definition: Mat3.h:493
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:89
Quat & operator-=(const Quat &q)
Subtract quaternion q from "this" quaternion, e.g. q -= q1;.
Definition: Quat.h:353
Quat conjugate() const
Definition: Quat.h:525
Quat & scale(T scale, const Quat &q)
Definition: Quat.h:465
bool normalize(T eps=T(1.0e-8))
this = normalized this
Definition: Quat.h:492
Quat & init()
"this" quaternion gets initialized to identity, same as setIdentity()
Definition: Quat.h:288
Quat operator-() const
Negation operator, e.g. q = -q;.
Definition: Quat.h:420
Quat & init(T x, T y, T z, T w)
"this" quaternion gets initialized to [x, y, z, w]
Definition: Quat.h:281
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
Quat operator+(const Quat &q) const
Return (this+q), e.g. q = q1 + q2;.
Definition: Quat.h:375
void read(std::istream &is)
Definition: Quat.h:569
T angle() const
Return angle of rotation.
Definition: Quat.h:249
Vec3< T > eulerAngles(RotationOrder rotationOrder) const
Returns vector of x,y,z rotational components.
Definition: Quat.h:322
friend std::ostream & operator<<(std::ostream &stream, const Quat &q)
Output to the stream, e.g. std::cout << q << std::endl;.
Definition: Quat.h:560
Quat operator*(const Quat &q) const
Return (this*q), e.g. q = q1 * q2;.
Definition: Quat.h:387
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:519
Quat derivative(const Vec3< T > &omega) const
Definition: Quat.h:483
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
#define OPENVDB_IS_POD(Type)
Definition: Math.h:55