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;
116 T s = T(sin(angle*T(0.5)));
118 mm[0] = axis.
x() * s;
119 mm[1] = axis.
y() * s;
120 mm[2] = axis.
z() * s;
122 mm[3] = T(cos(angle*T(0.5)));
129 T s = T(sin(angle*T(0.5)));
135 mm[3] = T(cos(angle*T(0.5)));
139 template<
typename T1>
145 "A non-rotation matrix can not be used to construct a quaternion");
149 "A reflection matrix can not be used to construct a quaternion");
152 T trace(rot.
trace());
155 T q_w = 0.5 * std::sqrt(trace+1);
156 T factor = 0.25 / q_w;
158 mm[0] = factor * (rot(1,2) - rot(2,1));
159 mm[1] = factor * (rot(2,0) - rot(0,2));
160 mm[2] = factor * (rot(0,1) - rot(1,0));
162 }
else if (rot(0,0) > rot(1,1) && rot(0,0) > rot(2,2)) {
164 T q_x = 0.5 * sqrt(rot(0,0)- rot(1,1)-rot(2,2)+1);
165 T factor = 0.25 / q_x;
168 mm[1] = factor * (rot(0,1) + rot(1,0));
169 mm[2] = factor * (rot(2,0) + rot(0,2));
170 mm[3] = factor * (rot(1,2) - rot(2,1));
171 }
else if (rot(1,1) > rot(2,2)) {
173 T q_y = 0.5 * sqrt(rot(1,1)-rot(0,0)-rot(2,2)+1);
174 T factor = 0.25 / q_y;
176 mm[0] = factor * (rot(0,1) + rot(1,0));
178 mm[2] = factor * (rot(1,2) + rot(2,1));
179 mm[3] = factor * (rot(2,0) - rot(0,2));
182 T q_z = 0.5 * sqrt(rot(2,2)-rot(0,0)-rot(1,1)+1);
183 T factor = 0.25 / q_z;
185 mm[0] = factor * (rot(2,0) + rot(0,2));
186 mm[1] = factor * (rot(1,2) + rot(2,1));
188 mm[3] = factor * (rot(0,1) - rot(1,0));
193 T&
x() {
return mm[0]; }
194 T&
y() {
return mm[1]; }
195 T&
z() {
return mm[2]; }
196 T&
w() {
return mm[3]; }
199 T
x()
const {
return mm[0]; }
200 T
y()
const {
return mm[1]; }
201 T
z()
const {
return mm[2]; }
202 T
w()
const {
return mm[3]; }
214 operator T*() {
return mm; }
215 operator const T*()
const {
return mm; }
226 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
228 if ( sqrLength > 1.0e-8 ) {
230 return T(T(2.0) * acos(mm[3]));
241 T sqrLength = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2];
243 if ( sqrLength > 1.0e-8 ) {
245 T invLength = T(T(1)/sqrt(sqrLength));
247 return Vec3<T>( mm[0]*invLength, mm[1]*invLength, mm[2]*invLength );
258 mm[0] = x; mm[1] = y; mm[2] = z; mm[3] = w;
270 T s = T(sin(angle*T(0.5)));
272 mm[0] = axis.
x() * s;
273 mm[1] = axis.
y() * s;
274 mm[2] = axis.
z() * s;
276 mm[3] = T(cos(angle*T(0.5)));
284 mm[0] = mm[1] = mm[2] = mm[3] = 0;
291 mm[0] = mm[1] = mm[2] = 0;
310 bool eq(
const Quat &q, T eps=1.0e-7)
const 352 return Quat<T>(mm[0]+q.
mm[0], mm[1]+q.
mm[1], mm[2]+q.
mm[2], mm[3]+q.
mm[3]);
358 return Quat<T>(mm[0]-q.
mm[0], mm[1]-q.
mm[1], mm[2]-q.
mm[2], mm[3]-q.
mm[3]);
366 prod.
mm[0] = mm[3]*q.
mm[0] + mm[0]*q.
mm[3] + mm[1]*q.
mm[2] - mm[2]*q.
mm[1];
367 prod.
mm[1] = mm[3]*q.
mm[1] + mm[1]*q.
mm[3] + mm[2]*q.
mm[0] - mm[0]*q.
mm[2];
368 prod.
mm[2] = mm[3]*q.
mm[2] + mm[2]*q.
mm[3] + mm[0]*q.
mm[1] - mm[1]*q.
mm[0];
369 prod.
mm[3] = mm[3]*q.
mm[3] - mm[0]*q.
mm[0] - mm[1]*q.
mm[1] - mm[2]*q.
mm[2];
385 return Quat<T>(mm[0]*scalar, mm[1]*scalar, mm[2]*scalar, mm[3]*scalar);
391 return Quat<T>(mm[0]/scalar, mm[1]/scalar, mm[2]/scalar, mm[3]/scalar);
396 {
return Quat<T>(-mm[0], -mm[1], -mm[2], -mm[3]); }
402 mm[0] = q1.
mm[0] + q2.
mm[0];
403 mm[1] = q1.
mm[1] + q2.
mm[1];
404 mm[2] = q1.
mm[2] + q2.
mm[2];
405 mm[3] = q1.
mm[3] + q2.
mm[3];
414 mm[0] = q1.
mm[0] - q2.
mm[0];
415 mm[1] = q1.
mm[1] - q2.
mm[1];
416 mm[2] = q1.
mm[2] - q2.
mm[2];
417 mm[3] = q1.
mm[3] - q2.
mm[3];
426 mm[0] = q1.
mm[3]*q2.
mm[0] + q1.
mm[0]*q2.
mm[3] +
427 q1.
mm[1]*q2.
mm[2] - q1.
mm[2]*q2.
mm[1];
428 mm[1] = q1.
mm[3]*q2.
mm[1] + q1.
mm[1]*q2.
mm[3] +
429 q1.
mm[2]*q2.
mm[0] - q1.
mm[0]*q2.
mm[2];
430 mm[2] = q1.
mm[3]*q2.
mm[2] + q1.
mm[2]*q2.
mm[3] +
431 q1.
mm[0]*q2.
mm[1] - q1.
mm[1]*q2.
mm[0];
432 mm[3] = q1.
mm[3]*q2.
mm[3] - q1.
mm[0]*q2.
mm[0] -
433 q1.
mm[1]*q2.
mm[1] - q1.
mm[2]*q2.
mm[2];
442 mm[0] = scale * q.
mm[0];
443 mm[1] = scale * q.
mm[1];
444 mm[2] = scale * q.
mm[2];
445 mm[3] = scale * q.
mm[3];
453 return (mm[0]*q.
mm[0] + mm[1]*q.
mm[1] + mm[2]*q.
mm[2] + mm[3]*q.
mm[3]);
460 return Quat<T>( +w()*omega.
x() -z()*omega.
y() +y()*omega.
z() ,
461 +z()*omega.
x() +w()*omega.
y() -x()*omega.
z() ,
462 -y()*omega.
x() +x()*omega.
y() +w()*omega.
z() ,
463 -x()*omega.
x() -y()*omega.
y() -z()*omega.
z() );
469 T d = T(sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]));
478 T d = sqrt(mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3]);
481 "Normalizing degenerate quaternion");
488 T d = mm[0]*mm[0] + mm[1]*mm[1] + mm[2]*mm[2] + mm[3]*mm[3];
491 "Cannot invert degenerate quaternion");
492 Quat result = *
this/-d;
493 result.
mm[3] = -result.
mm[3];
502 return Quat<T>(-mm[0], -mm[1], -mm[2], mm[3]);
519 std::ostringstream buffer;
524 for (
unsigned j(0); j < 4; j++) {
525 if (j) buffer <<
", ";
541 friend Quat slerp<>(
const Quat &q1,
const Quat &q2, T t, T tolerance);
543 void write(std::ostream& os)
const { os.write(static_cast<char*>(&mm),
sizeof(T) * 4); }
544 void read(std::istream& is) { is.read(static_cast<char*>(&mm),
sizeof(T) * 4); }
551 template <
typename S,
typename T>
558 template <
typename T,
typename T0>
566 if (q1.
dot(q2) < 0) q2 *= -1;
568 Quat<T> qslerp = slerp<T>(q1, q2,
static_cast<T
>(t));
569 MatType m = rotation<MatType>(qslerp);
583 template <
typename T,
typename T0>
588 Mat3<T> m00, m01, m02, m10, m11;
590 m00 =
slerp(m1, m2, t);
591 m01 =
slerp(m2, m3, t);
592 m02 =
slerp(m3, m4, t);
594 m10 =
slerp(m00, m01, t);
595 m11 =
slerp(m01, m02, t);
597 return slerp(m10, m11, t);
610 template<>
inline math::Quatd zeroVal<math::Quatd >() {
return math::Quatd::zero(); }
615 #endif //OPENVDB_MATH_QUAT_H_HAS_BEEN_INCLUDED friend std::ostream & operator<<(std::ostream &stream, const Quat &q)
Output to the stream, e.g. std::cout << q << std::endl;.
Definition: Quat.h:535
Quat unit() const
this = normalized this
Definition: Quat.h:476
static unsigned numElements()
Definition: Quat.h:205
RotationOrder
Definition: Math.h:908
Quat(T *a)
Constructor with array argument, e.g. float a[4]; Quatf q(a);.
Definition: Quat.h:101
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:70
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:443
Vec3< T > rotateVector(const Vec3< T > &v) const
Return rotated vector by "this" quaternion.
Definition: Quat.h:506
Vec3< T > eulerAngles(RotationOrder rotationOrder) const
Returns vector of x,y,z rotational components.
Definition: Quat.h:297
T & operator[](int i)
Array style reference to the components, e.g. q[3] = 1.34f;.
Definition: Quat.h:208
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Quat.h:221
Definition: Exceptions.h:56
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:615
T operator[](int i) const
Array style constant reference to the components, e.g. float f = q[1];.
Definition: Quat.h:211
Quat & init()
"this" quaternion gets initialized to identity, same as setIdentity()
Definition: Quat.h:263
Quat inverse(T tolerance=T(0)) const
returns inverse of this
Definition: Quat.h:486
Quat & init(T x, T y, T z, T w)
"this" quaternion gets initialized to [x, y, z, w]
Definition: Quat.h:256
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:559
Quat(math::Axis axis, T angle)
Constructor given rotation as axis and angle.
Definition: Quat.h:127
static Quat identity()
Definition: Quat.h:514
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:505
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:446
bool normalize(T eps=T(1.0e-8))
this = normalized this
Definition: Quat.h:467
Quat conjugate() const
Definition: Quat.h:500
T w() const
Definition: Quat.h:202
Quat(const Mat3< T1 > &rot)
Constructor given a rotation matrix.
Definition: Quat.h:140
Vec3< T > axis() const
Return axis of rotation.
Definition: Quat.h:239
T & y()
Definition: Vec3.h:86
Quat derivative(const Vec3< T > &omega) const
Definition: Quat.h:458
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:310
void read(std::istream &is)
Definition: Quat.h:544
static Quat zero()
Predefined constants, e.g. Quat q = Quat::identity();.
Definition: Quat.h:513
bool operator==(const Quat &q) const
Equality operator, does exact floating point comparisons.
Definition: Quat.h:301
Quat & mult(const Quat &q1, const Quat &q2)
Definition: Quat.h:424
void write(std::ostream &os) const
Definition: Quat.h:543
bool isUnitary(const MatType &m)
Determine if a matrix is unitary (i.e., rotation or reflection).
Definition: Mat.h:889
Axis
Definition: Math.h:901
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:333
Quat operator*(const Quat &q) const
Return (this*q), e.g. q = q1 * q2;.
Definition: Quat.h:362
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:406
Definition: Exceptions.h:13
Quat & operator+=(const Quat &q)
Add quaternion q to "this" quaternion, e.g. q += q1;.
Definition: Quat.h:317
T dot(const Quat &q) const
Dot product.
Definition: Quat.h:451
T value_type
Definition: Quat.h:81
Quat & scale(T scale, const Quat &q)
Definition: Quat.h:440
Quat operator*(T scalar) const
Return (this*scalar), e.g. q = q1 * scalar;.
Definition: Quat.h:383
Quat & setIdentity()
Set "this" vector to identity.
Definition: Quat.h:289
T y() const
Definition: Quat.h:200
Quat operator*=(const Quat &q)
Assigns this to (this*q), e.g. q *= q1;.
Definition: Quat.h:376
T angle() const
Return angle of rotation.
Definition: Quat.h:224
T & z()
Definition: Vec3.h:87
T mm[4]
Definition: Quat.h:547
T & w()
Definition: Quat.h:196
Quat operator-() const
Negation operator, e.g. q = -q;.
Definition: Quat.h:395
T ValueType
Definition: Quat.h:82
3x3 matrix class.
Definition: Mat3.h:28
Quat & operator*=(T scalar)
Scale "this" quaternion by scalar, e.g. q *= scalar;.
Definition: Quat.h:339
T x() const
Get the component, e.g. float f = q.w();.
Definition: Quat.h:199
Quat & add(const Quat &q1, const Quat &q2)
Definition: Quat.h:400
Quat(const Vec3< T > &axis, T angle)
Definition: Quat.h:112
Quat< float > Quats
Definition: Quat.h:600
Quat & operator-=(const Quat &q)
Subtract quaternion q from "this" quaternion, e.g. q -= q1;.
Definition: Quat.h:328
T & x()
Reference to the component, e.g. q.x() = 4.5f;.
Definition: Quat.h:193
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Quat.h:218
Quat operator/(T scalar) const
Return (this/scalar), e.g. q = q1 / scalar;.
Definition: Quat.h:389
Quat operator-(const Quat &q) const
Return (this-q), e.g. q = q1 - q2;.
Definition: Quat.h:356
T trace() const
Trace of matrix.
Definition: Mat3.h:488
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:552
Mat3< T > bezLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, const Mat3< T0 > &m3, const Mat3< T0 > &m4, T t)
Definition: Quat.h:584
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
Quat & sub(const Quat &q1, const Quat &q2)
Definition: Quat.h:412
Quat operator+(const Quat &q) const
Return (this+q), e.g. q = q1 + q2;.
Definition: Quat.h:350
T z() const
Definition: Quat.h:201
T & y()
Definition: Quat.h:194
T & z()
Definition: Quat.h:195
Quat & setZero()
Set "this" vector to zero.
Definition: Quat.h:282
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:85
Quat & setAxisAngle(const Vec3< T > &axis, T angle)
Definition: Quat.h:267
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212
std::string str() const
Definition: Quat.h:517
T det() const
Determinant of matrix.
Definition: Mat3.h:479
Quat(T x, T y, T z, T w)
Constructor with four arguments, e.g. Quatf q(1,2,3,4);.
Definition: Quat.h:91
#define OPENVDB_IS_POD(Type)
Definition: Math.h:56