17 #ifndef INCLUDED_ATOMSMATHQUAT_H
18 #define INCLUDED_ATOMSMATHQUAT_H
20 #include <AtomsMath/ImathMatrix.h>
21 #include <AtomsMath/ImathNamespace.h>
26 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_ENTER
28 #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
30 # pragma warning(disable : 4244)
40 template <
class T>
class Quat
57 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T&
operator[] (
int index) noexcept;
61 ATOMSMATH_HOSTDEVICE constexpr T
operator[] (
int index)
const noexcept;
67 ATOMSMATH_HOSTDEVICE constexpr
Quat() noexcept;
70 ATOMSMATH_HOSTDEVICE constexpr
Quat (const
Quat& q) noexcept;
73 template <class S> ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
Quat (const
Quat<S>& q) noexcept;
76 ATOMSMATH_HOSTDEVICE constexpr
Quat (T s, T i, T j, T k) noexcept;
79 ATOMSMATH_HOSTDEVICE constexpr
Quat (T s,
Vec3<T> d) noexcept;
82 ATOMSMATH_HOSTDEVICE constexpr static
Quat<T>
identity() noexcept;
85 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Quat<T>& operator= (const
Quat<T>& q) noexcept;
88 ~
Quat() noexcept = default;
99 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Quat<T>& operator*= (const
Quat<T>& q) noexcept;
103 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Quat<T>& operator*= (T t) noexcept;
106 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Quat<T>& operator/= (const
Quat<T>& q) noexcept;
110 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Quat<T>& operator/= (T t) noexcept;
113 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Quat<T>& operator+= (const
Quat<T>& q) noexcept;
116 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const
Quat<T>& operator-= (const
Quat<T>& q) noexcept;
119 template <class S> ATOMSMATH_HOSTDEVICE constexpr
bool operator== (const
Quat<S>& q) const noexcept;
122 template <class S> ATOMSMATH_HOSTDEVICE constexpr
bool operator!= (const
Quat<S>& q) const noexcept;
131 ATOMSMATH_HOSTDEVICE constexpr T
length() const noexcept;
134 ATOMSMATH_HOSTDEVICE constexpr T
angle() const noexcept;
137 ATOMSMATH_HOSTDEVICE constexpr
Vec3<T>
axis() const noexcept;
146 ATOMSMATH_HOSTDEVICE
Quat<T>
log() const noexcept;
149 ATOMSMATH_HOSTDEVICE
Quat<T>
exp() const noexcept;
158 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
Quat<T>&
invert() noexcept;
161 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
Quat<T>
inverse() const noexcept;
165 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
Quat<T>&
normalize() noexcept;
168 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
Quat<T>
normalized() const noexcept;
171 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
Vec3<T>
rotateVector (const
Vec3<T>& original) const noexcept;
184 ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14
Quat<T>&
194 ATOMSMATH_HOSTDEVICE
void setRotationInternal (const
Vec3<T>& f0, const
Vec3<T>& t0,
Quat<T>& q) noexcept;
197 template <class T> ATOMSMATH_CONSTEXPR14
Quat<T> slerp (const
Quat<T>& q1, const
Quat<T>& q2, T t) noexcept;
200 ATOMSMATH_CONSTEXPR14
Quat<T> slerpShortestArc (const
Quat<T>& q1, const
Quat<T>& q2, T t) noexcept;
203 ATOMSMATH_CONSTEXPR14
Quat<T>
204 squad (const
Quat<T>& q1, const
Quat<T>& q2, const
Quat<T>& qa, const
Quat<T>& qb, T t) noexcept;
213 void intermediate (const
Quat<T>& q0,
218 Quat<T>& qb) noexcept;
220 template <class T> constexpr
Matrix33<T> operator* (const
Matrix33<T>& M, const
Quat<T>& q) noexcept;
222 template <class T> constexpr
Matrix33<T> operator* (const
Quat<T>& q, const
Matrix33<T>& M) noexcept;
224 template <class T> std::ostream& operator<< (std::ostream& o, const
Quat<T>& q);
226 template <class T> constexpr
Quat<T> operator* (const
Quat<T>& q1, const
Quat<T>& q2) noexcept;
228 template <class T> constexpr
Quat<T> operator/ (const
Quat<T>& q1, const
Quat<T>& q2) noexcept;
230 template <class T> constexpr
Quat<T> operator/ (const
Quat<T>& q, T t) noexcept;
232 template <class T> constexpr
Quat<T> operator* (const
Quat<T>& q, T t) noexcept;
234 template <class T> constexpr
Quat<T> operator* (T t, const
Quat<T>& q) noexcept;
236 template <class T> constexpr
Quat<T> operator+ (const
Quat<T>& q1, const
Quat<T>& q2) noexcept;
238 template <class T> constexpr
Quat<T> operator- (const
Quat<T>& q1, const
Quat<T>& q2) noexcept;
240 template <class T> constexpr
Quat<T> operator~ (const
Quat<T>& q) noexcept;
242 template <class T> constexpr
Quat<T> operator- (const
Quat<T>& q) noexcept;
244 template <class T> ATOMSMATH_CONSTEXPR14
Vec3<T> operator* (const
Vec3<T>&
v, const
Quat<T>& q) noexcept;
256 template <class T> constexpr inline
Quat<T>::
Quat() noexcept :
r (1),
v (0, 0, 0)
268 template <
class T> constexpr
inline Quat<T>::Quat (T s, T i, T j, T k) noexcept :
r (s),
v (i, j, k)
291 ATOMSMATH_CONSTEXPR14
inline const Quat<T>&
300 ATOMSMATH_CONSTEXPR14
inline const Quat<T>&
303 T rtmp =
r * q.r - (
v ^ q.v);
304 v =
r * q.v +
v * q.r +
v % q.v;
310 ATOMSMATH_CONSTEXPR14
inline const Quat<T>&
319 ATOMSMATH_CONSTEXPR14
inline const Quat<T>&
322 *
this = *
this * q.inverse();
327 ATOMSMATH_CONSTEXPR14
inline const Quat<T>&
336 ATOMSMATH_CONSTEXPR14
inline const Quat<T>&
345 ATOMSMATH_CONSTEXPR14
inline const Quat<T>&
354 ATOMSMATH_CONSTEXPR14
inline T&
357 return index ?
v[index - 1] :
r;
364 return index ?
v[index - 1] :
r;
369 constexpr
inline bool
372 return r == q.r &&
v == q.v;
377 constexpr
inline bool
380 return r != q.r ||
v != q.v;
385 ATOMSMATH_HOSTDEVICE constexpr
inline T
388 return q1.r * q2.r + (q1.v ^ q2.v);
395 return std::sqrt (
r *
r + (
v ^
v));
399 ATOMSMATH_CONSTEXPR14
inline Quat<T>&
417 ATOMSMATH_CONSTEXPR14
inline Quat<T>
421 return Quat (
r / l,
v / l);
427 ATOMSMATH_CONSTEXPR14
inline Quat<T>
436 T qdot = *
this ^ *
this;
437 return Quat (
r / qdot, -
v / qdot);
441 ATOMSMATH_CONSTEXPR14
inline Quat<T>&
444 T qdot = (*this) ^ (*this);
451 ATOMSMATH_CONSTEXPR14
inline Vec3<T>
466 Quat<T> result = *
this * vec * inv;
474 return r * q.r +
v.x * q.v.x +
v.y * q.v.y +
v.z * q.v.z;
481 ATOMSMATH_CONSTEXPR14
inline T
485 T lengthD = std::sqrt (d ^ d);
488 T lengthS = std::sqrt (s ^ s);
490 return 2 * std::atan2 (lengthD, lengthS);
511 ATOMSMATH_CONSTEXPR14
inline Quat<T>
514 T a = angle4D (q1, q2);
517 Quat<T> q = sinx_over_x (s * a) / sinx_over_x (a) * s * q1 +
518 sinx_over_x (t * a) / sinx_over_x (a) * t * q2;
528 ATOMSMATH_CONSTEXPR14
inline Quat<T>
529 slerpShortestArc (
const Quat<T>& q1,
const Quat<T>& q2, T t) noexcept
532 return slerp (q1, q2, t);
534 return slerp (q1, -q2, t);
556 ATOMSMATH_CONSTEXPR14
inline Quat<T>
559 Quat<T> qa = intermediate (q0, q1, q2);
560 Quat<T> qb = intermediate (q1, q2, q3);
561 Quat<T> result = squad (q1, qa, qb, q2, t);
574 ATOMSMATH_CONSTEXPR14
inline Quat<T>
577 Quat<T> r1 = slerp (q1, q2, t);
578 Quat<T> r2 = slerp (qa, qb, t);
579 Quat<T> result = slerp (r1, r2, 2 * t * (1 - t));
587 ATOMSMATH_CONSTEXPR14
inline Quat<T>
608 T theta = std::acos (std::min (
r, (T) 1.0));
613 T sintheta = std::sin (theta);
616 if (std::abs(sintheta) < 1 && std::abs(theta) >= std::numeric_limits<T>::max() * std::abs(sintheta))
619 k = theta / sintheta;
621 return Quat<T> ((T) 0,
v.x * k,
v.y * k,
v.z * k);
634 T theta =
v.length();
635 T sintheta = std::sin (theta);
638 if (abs (theta) < 1 && abs (sintheta) >= std::numeric_limits<T>::max() * abs (theta))
641 k = sintheta / theta;
643 T costheta = std::cos (theta);
645 return Quat<T> (costheta,
v.x * k,
v.y * k,
v.z * k);
652 return 2 * std::atan2 (
v.length(),
r);
659 return v.normalized();
663 ATOMSMATH_CONSTEXPR14
inline Quat<T>&
666 r = std::cos (radians / 2);
667 v =
axis.normalized() * std::sin (radians / 2);
672 ATOMSMATH_CONSTEXPR14
inline Quat<T>&
700 setRotationInternal (f0, t0, *
this);
714 setRotationInternal (f0, h0, *
this);
717 setRotationInternal (h0, t0, q);
733 if (f02.x <= f02.y && f02.x <= f02.z)
735 else if (f02.y <= f02.z)
781 2 * (
v.x *
v.y +
v.z *
r),
782 2 * (
v.z *
v.x -
v.y *
r),
784 2 * (
v.x *
v.y -
v.z *
r),
785 1 - 2 * (
v.z *
v.z +
v.x *
v.x),
786 2 * (
v.y *
v.z +
v.x *
r),
788 2 * (
v.z *
v.x +
v.y *
r),
789 2 * (
v.y *
v.z -
v.x *
r),
790 1 - 2 * (
v.y *
v.y +
v.x *
v.x));
798 2 * (
v.x *
v.y +
v.z *
r),
799 2 * (
v.z *
v.x -
v.y *
r),
801 2 * (
v.x *
v.y -
v.z *
r),
802 1 - 2 * (
v.z *
v.z +
v.x *
v.x),
803 2 * (
v.y *
v.z +
v.x *
r),
805 2 * (
v.z *
v.x +
v.y *
r),
806 2 * (
v.y *
v.z -
v.x *
r),
807 1 - 2 * (
v.y *
v.y +
v.x *
v.x),
836 operator<< (std::ostream& o,
const Quat<T>& q)
838 return o <<
"(" << q.
r <<
", " << q.
v.x <<
", " << q.
v.y <<
", " << q.
v.z <<
")";
846 return Quat<T> (q1.r * q2.r - (q1.v ^ q2.v), q1.r * q2.v + q1.v * q2.r + q1.v % q2.v);
860 operator/ (
const Quat<T>& q, T t) noexcept
869 operator* (
const Quat<T>& q, T t) noexcept
878 operator* (T t,
const Quat<T>& q) noexcept
888 return Quat<T> (q1.r + q2.r, q1.v + q2.v);
896 return Quat<T> (q1.r - q2.r, q1.v - q2.v);
902 operator~ (
const Quat<T>& q) noexcept
910 operator- (
const Quat<T>& q) noexcept
918 ATOMSMATH_CONSTEXPR14
inline Vec3<T>
923 return v + T (2) * (q.
r * a + b);
926 #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
927 # pragma warning(default : 4244)
930 ATOMSMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImathMatrix.h:305
Definition: ImathMatrix.h:631
Definition: ImathQuat.h:41
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Quat< T > & setRotation(const Vec3< T > &fromDirection, const Vec3< T > &toDirection) noexcept
Definition: ImathQuat.h:673
constexpr ATOMSMATH_HOSTDEVICE T euclideanInnerProduct(const Quat< T > &q) const noexcept
Return the Euclidean inner product.
Definition: ImathQuat.h:472
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Quat< T > & operator/=(const Quat< T > &q) noexcept
Quaterion division, using the inverse()
Definition: ImathQuat.h:320
constexpr ATOMSMATH_HOSTDEVICE bool operator==(const Quat< S > &q) const noexcept
Equality.
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Quat< T > & operator-=(const Quat< T > &q) noexcept
Quaternion subtraction.
Definition: ImathQuat.h:346
Vec3< T > v
The imaginary vector.
Definition: ImathQuat.h:51
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Quat< T > & invert() noexcept
Definition: ImathQuat.h:442
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Quat< T > & operator+=(const Quat< T > &q) noexcept
Quaternion addition.
Definition: ImathQuat.h:337
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Quat< T > & operator=(const Quat< T > &q) noexcept
Assignment.
Definition: ImathQuat.h:292
constexpr ATOMSMATH_HOSTDEVICE T length() const noexcept
Return the R4 length.
Definition: ImathQuat.h:393
ATOMSMATH_HOSTDEVICE Quat< T > log() const noexcept
Return the logarithm of the quaterion.
Definition: ImathQuat.h:601
constexpr ATOMSMATH_HOSTDEVICE Quat() noexcept
Default constructor is the identity quat.
Definition: ImathQuat.h:256
ATOMSMATH_HOSTDEVICE Quat< T > exp() const noexcept
Return the exponent of the quaterion.
Definition: ImathQuat.h:626
constexpr ATOMSMATH_HOSTDEVICE T angle() const noexcept
Return the angle of the axis/angle representation.
Definition: ImathQuat.h:650
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Quat< T > inverse() const noexcept
Return 1/this, leaving this unchanged.
Definition: ImathQuat.h:428
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Quat< T > normalized() const noexcept
Return a normalized quaternion, leaving this unmodified.
Definition: ImathQuat.h:418
T r
The real part.
Definition: ImathQuat.h:48
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 const Quat< T > & operator*=(const Quat< T > &q) noexcept
Quaternion multiplication.
Definition: ImathQuat.h:301
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Quat< T > & setAxisAngle(const Vec3< T > &axis, T radians) noexcept
Definition: ImathQuat.h:664
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 T & operator[](int index) noexcept
Definition: ImathQuat.h:355
constexpr ATOMSMATH_HOSTDEVICE bool operator!=(const Quat< S > &q) const noexcept
Inequality.
constexpr ATOMSMATH_HOSTDEVICE Matrix33< T > toMatrix33() const noexcept
Return a 3x3 rotation matrix.
Definition: ImathQuat.h:778
constexpr static ATOMSMATH_HOSTDEVICE Quat< T > identity() noexcept
The identity quaternion.
Definition: ImathQuat.h:285
constexpr ATOMSMATH_HOSTDEVICE Vec3< T > axis() const noexcept
Return the axis of the axis/angle representation.
Definition: ImathQuat.h:657
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Vec3< T > rotateVector(const Vec3< T > &original) const noexcept
Rotate the given point by the quaterion.
Definition: ImathQuat.h:452
ATOMSMATH_HOSTDEVICE ATOMSMATH_CONSTEXPR14 Quat< T > & normalize() noexcept
Definition: ImathQuat.h:400
T BaseType
Definition: ImathQuat.h:191
constexpr ATOMSMATH_HOSTDEVICE Matrix44< T > toMatrix44() const noexcept
Return a 4x4 rotation matrix.
Definition: ImathQuat.h:795
Definition: ImathVec.h:260
ATOMSMATH_HOSTDEVICE Vec3< T > normalized() const noexcept
Return a normalized vector. Does not modify *this.
Definition: ImathVec.h:1629