17#ifndef IGNITION_MATH_QUATERNION_HH_
18#define IGNITION_MATH_QUATERNION_HH_
24#include <ignition/math/config.hh>
30 inline namespace IGNITION_MATH_VERSION_NAMESPACE
47 : qw(1), qx(0), qy(0), qz(0)
58 public:
Quaternion(
const T &_w,
const T &_x,
const T &_y,
const T &_z)
59 : qw(_w), qx(_x), qy(_y), qz(_z)
66 public:
Quaternion(
const T &_roll,
const T &_pitch,
const T &_yaw)
76 this->
Axis(_axis, _angle);
124 this->qx = -this->qx;
125 this->qy = -this->qy;
126 this->qz = -this->qz;
137 s = q.qw * q.qw + q.qx * q.qx + q.qy * q.qy + q.qz * q.qz;
169 if (std::abs(this->qw) < 1.0)
171 T fAngle = acos(this->qw);
172 T fSin = sin(fAngle);
173 if (std::abs(fSin) >= 1e-3)
175 T fCoeff = fAngle/fSin;
176 result.qx = fCoeff*this->qx;
177 result.qy = fCoeff*this->qy;
178 result.qz = fCoeff*this->qz;
183 result.qx = this->qx;
184 result.qy = this->qy;
185 result.qz = this->qz;
198 T fAngle = sqrt(this->qx*this->qx+
199 this->qy*this->qy+this->qz*this->qz);
200 T fSin = sin(fAngle);
203 result.qw = cos(fAngle);
205 if (std::abs(fSin) >= 1e-3)
207 T fCoeff = fSin/fAngle;
208 result.qx = fCoeff*this->qx;
209 result.qy = fCoeff*this->qy;
210 result.qz = fCoeff*this->qz;
214 result.qx = this->qx;
215 result.qy = this->qy;
216 result.qz = this->qz;
227 s = T(sqrt(this->qw * this->qw + this->qx * this->qx +
228 this->qy * this->qy + this->qz * this->qz));
251 public:
void Axis(T _ax, T _ay, T _az, T _aa)
255 l = _ax * _ax + _ay * _ay + _az * _az;
267 l = sin(_aa) / sqrt(l);
282 this->
Axis(_axis.
X(), _axis.
Y(), _axis.
Z(), _a);
290 public:
void Set(T _w, T _x, T _y, T _z)
305 this->
Euler(_vec.
X(), _vec.
Y(), _vec.
Z());
312 public:
void Euler(T _roll, T _pitch, T _yaw)
316 phi = _roll / T(2.0);
317 the = _pitch / T(2.0);
320 this->qw = T(cos(phi) * cos(the) * cos(psi) +
321 sin(phi) * sin(the) * sin(psi));
322 this->qx = T(sin(phi) * cos(the) * cos(psi) -
323 cos(phi) * sin(the) * sin(psi));
324 this->qy = T(cos(phi) * sin(the) * cos(psi) +
325 sin(phi) * cos(the) * sin(psi));
326 this->qz = T(cos(phi) * cos(the) * sin(psi) -
327 sin(phi) * sin(the) * cos(psi));
338 T tol =
static_cast<T
>(1e-15);
348 squ = copy.qw * copy.qw;
349 sqx = copy.qx * copy.qx;
350 sqy = copy.qy * copy.qy;
351 sqz = copy.qz * copy.qz;
354 T sarg = -2 * (copy.qx*copy.qz - copy.qw * copy.qy);
359 else if (sarg >= T(1.0))
365 vec.
Y(T(asin(sarg)));
373 if (std::abs(sarg - 1) < tol)
376 vec.
X(T(atan2(2 * (copy.qx*copy.qy - copy.qz*copy.qw),
377 squ - sqx + sqy - sqz)));
380 else if (std::abs(sarg + 1) < tol)
383 vec.
X(T(atan2(-2 * (copy.qx*copy.qy - copy.qz*copy.qw),
384 squ - sqx + sqy - sqz)));
389 vec.
X(T(atan2(2 * (copy.qy*copy.qz + copy.qw*copy.qx),
390 squ - sqx - sqy + sqz)));
393 vec.
Z(T(atan2(2 * (copy.qx*copy.qy + copy.qw*copy.qz),
394 squ + sqx - sqy - sqz)));
424 return this->
Euler().X();
431 return this->
Euler().Y();
438 return this->
Euler().Z();
446 T len = this->qx*this->qx + this->qy*this->qy + this->qz*this->qz;
447 if (
equal<T>(len,
static_cast<T
>(0)))
454 _angle = 2.0 * acos(this->qw);
455 T invLen = 1.0 / sqrt(len);
456 _axis.
Set(this->qx*invLen, this->qy*invLen, this->qz*invLen);
469 const T trace = _mat(0, 0) + _mat(1, 1) + _mat(2, 2);
470 if (trace > 0.0000001)
472 qw = sqrt(1 + trace) / 2;
473 const T s = 1.0 / (4 * qw);
474 qx = (_mat(2, 1) - _mat(1, 2)) * s;
475 qy = (_mat(0, 2) - _mat(2, 0)) * s;
476 qz = (_mat(1, 0) - _mat(0, 1)) * s;
478 else if (_mat(0, 0) > _mat(1, 1) && _mat(0, 0) > _mat(2, 2))
480 qx = sqrt(1.0 + _mat(0, 0) - _mat(1, 1) - _mat(2, 2)) / 2;
481 const T s = 1.0 / (4 * qx);
482 qw = (_mat(2, 1) - _mat(1, 2)) * s;
483 qy = (_mat(1, 0) + _mat(0, 1)) * s;
484 qz = (_mat(0, 2) + _mat(2, 0)) * s;
486 else if (_mat(1, 1) > _mat(2, 2))
488 qy = sqrt(1.0 - _mat(0, 0) + _mat(1, 1) - _mat(2, 2)) / 2;
489 const T s = 1.0 / (4 * qy);
490 qw = (_mat(0, 2) - _mat(2, 0)) * s;
491 qx = (_mat(0, 1) + _mat(1, 0)) * s;
492 qz = (_mat(1, 2) + _mat(2, 1)) * s;
496 qz = sqrt(1.0 - _mat(0, 0) - _mat(1, 1) + _mat(2, 2)) / 2;
497 const T s = 1.0 / (4 * qz);
498 qw = (_mat(1, 0) - _mat(0, 1)) * s;
499 qx = (_mat(0, 2) + _mat(2, 0)) * s;
500 qy = (_mat(1, 2) + _mat(2, 1)) * s;
530 const T kCosTheta = _v1.
Dot(_v2);
533 if (fabs(kCosTheta/k + 1) < 1e-6)
540 if (_v1Abs.
X() < _v1Abs.
Y())
542 if (_v1Abs.
X() < _v1Abs.
Z())
553 if (_v1Abs.
Y() < _v1Abs.
Z())
592 this->
ToAxis(axis, angle);
595 this->
Axis(axis.
X(), axis.
Y(), axis.
Z(), angle);
604 this->qy + _qt.qy, this->qz + _qt.qz);
624 this->qy - _qt.qy, this->qz - _qt.qz);
643 this->qw*_q.qw-this->qx*_q.qx-this->qy*_q.qy-this->qz*_q.qz,
644 this->qw*_q.qx+this->qx*_q.qw+this->qy*_q.qz-this->qz*_q.qy,
645 this->qw*_q.qy-this->qx*_q.qz+this->qy*_q.qw+this->qz*_q.qx,
646 this->qw*_q.qz+this->qx*_q.qy-this->qy*_q.qx+this->qz*_q.qw);
655 this->qy*_f, this->qz*_f);
673 Vector3<T> qvec(this->qx, this->qy, this->qz);
675 uuv = qvec.
Cross(uv);
676 uv *= (2.0f * this->qw);
679 return _v + uv + uuv;
687 return equal(this->qx, _qt.qx,
static_cast<T
>(0.001)) &&
688 equal(this->qy, _qt.qy,
static_cast<T
>(0.001)) &&
689 equal(this->qz, _qt.qz,
static_cast<T
>(0.001)) &&
690 equal(this->qw, _qt.qw,
static_cast<T
>(0.001));
698 return !
equal(this->qx, _qt.qx,
static_cast<T
>(0.001)) ||
699 !
equal(this->qy, _qt.qy,
static_cast<T
>(0.001)) ||
700 !
equal(this->qz, _qt.qz,
static_cast<T
>(0.001)) ||
701 !
equal(this->qw, _qt.qw,
static_cast<T
>(0.001));
708 return Quaternion<T>(-this->qw, -this->qx, -this->qy, -this->qz);
717 _vec.
X(), _vec.
Y(), _vec.
Z());
718 tmp = (*this) * (tmp * this->
Inverse());
729 tmp = this->
Inverse() * (tmp * (*this));
740 return std::isfinite(
static_cast<double>(this->qw)) &&
741 std::isfinite(
static_cast<double>(this->qx)) &&
742 std::isfinite(
static_cast<double>(this->qy)) &&
743 std::isfinite(
static_cast<double>(this->qz));
751 if (!std::isfinite(
static_cast<double>(this->qx)))
753 if (!std::isfinite(
static_cast<double>(this->qy)))
755 if (!std::isfinite(
static_cast<double>(this->qz)))
757 if (!std::isfinite(
static_cast<double>(this->qw)))
760 if (
equal(this->qw,
static_cast<T
>(0)) &&
761 equal(this->qx,
static_cast<T
>(0)) &&
762 equal(this->qy,
static_cast<T
>(0)) &&
763 equal(this->qz,
static_cast<T
>(0)))
773 T fTy = 2.0f*this->qy;
774 T fTz = 2.0f*this->qz;
776 T fTwy = fTy*this->qw;
777 T fTwz = fTz*this->qw;
778 T fTxy = fTy*this->qx;
779 T fTxz = fTz*this->qx;
780 T fTyy = fTy*this->qy;
781 T fTzz = fTz*this->qz;
783 return Vector3<T>(1.0f-(fTyy+fTzz), fTxy+fTwz, fTxz-fTwy);
790 T fTx = 2.0f*this->qx;
791 T fTy = 2.0f*this->qy;
792 T fTz = 2.0f*this->qz;
793 T fTwx = fTx*this->qw;
794 T fTwz = fTz*this->qw;
795 T fTxx = fTx*this->qx;
796 T fTxy = fTy*this->qx;
797 T fTyz = fTz*this->qy;
798 T fTzz = fTz*this->qz;
800 return Vector3<T>(fTxy-fTwz, 1.0f-(fTxx+fTzz), fTyz+fTwx);
807 T fTx = 2.0f*this->qx;
808 T fTy = 2.0f*this->qy;
809 T fTz = 2.0f*this->qz;
810 T fTwx = fTx*this->qw;
811 T fTwy = fTy*this->qw;
812 T fTxx = fTx*this->qx;
813 T fTxz = fTz*this->qx;
814 T fTyy = fTy*this->qy;
815 T fTyz = fTz*this->qy;
817 return Vector3<T>(fTxz+fTwy, fTyz-fTwx, 1.0f-(fTxx+fTyy));
824 this->qx =
precision(this->qx, _precision);
825 this->qy =
precision(this->qy, _precision);
826 this->qz =
precision(this->qz, _precision);
827 this->qw =
precision(this->qw, _precision);
835 return this->qw*_q.qw + this->qx * _q.qx +
836 this->qy*_q.qy + this->qz*_q.qz;
852 bool _shortestPath =
false)
854 T fSlerpT = 2.0f*_fT*(1.0f-_fT);
857 return Slerp(fSlerpT, kSlerpP, kSlerpQ);
870 bool _shortestPath =
false)
872 T fCos = _rkP.
Dot(_rkQ);
876 if (fCos < 0.0f && _shortestPath)
886 if (std::abs(fCos) < 1 - 1e-03)
889 T fSin = sqrt(1 - (fCos*fCos));
890 T fAngle = atan2(fSin, fCos);
892 T fInvSin = 1.0f / fSin;
893 T fCoeff0 = sin((1.0f - _fT) * fAngle) * fInvSin;
894 T fCoeff1 = sin(_fT * fAngle) * fInvSin;
895 return _rkP * fCoeff0 + rkT * fCoeff1;
923 const T _deltaT)
const
926 Vector3<T> theta = _angularVelocity * _deltaT * 0.5;
929 if (thetaMagSq * thetaMagSq / 24.0 <
MIN_D)
931 deltaQ.
W() = 1.0 - thetaMagSq / 2.0;
932 s = 1.0 - thetaMagSq / 6.0;
936 double thetaMag = sqrt(thetaMagSq);
937 deltaQ.
W() = cos(thetaMag);
938 s = sin(thetaMag) / thetaMag;
940 deltaQ.
X() = theta.
X() * s;
941 deltaQ.
Y() = theta.
Y() * s;
942 deltaQ.
Z() = theta.
Z() * s;
943 return deltaQ * (*this);
948 public:
inline const T &
W()
const
955 public:
inline const T &
X()
const
962 public:
inline const T &
Y()
const
969 public:
inline const T &
Z()
const
977 public:
inline T &
W()
984 public:
inline T &
X()
991 public:
inline T &
Y()
998 public:
inline T &
Z()
1005 public:
inline void X(T _v)
1012 public:
inline void Y(T _v)
1019 public:
inline void Z(T _v)
1026 public:
inline void W(T _v)
1051 Angle roll, pitch, yaw;
1054 _in.setf(std::ios_base::skipws);
1055 _in >> roll >> pitch >> yaw;
#define IGN_PI
Define IGN_PI, IGN_PI_2, and IGN_PI_4.
Definition Helpers.hh:174
An angle and related functions.
Definition Angle.hh:48
A 3x3 matrix class.
Definition Quaternion.hh:32
A quaternion class.
Definition Quaternion.hh:38
void Matrix(const Matrix3< T > &_mat)
Set from a rotation matrix.
Definition Quaternion.hh:467
void X(T _v)
Set the x component.
Definition Quaternion.hh:1005
Quaternion< T > Integrate(const Vector3< T > &_angularVelocity, const T _deltaT) const
Integrate quaternion for constant angular velocity vector along specified interval _deltaT.
Definition Quaternion.hh:922
Quaternion< T > & operator=(const Quaternion< T > &_qt)
Equal operator.
Definition Quaternion.hh:109
T & Y()
Get a mutable y component.
Definition Quaternion.hh:991
friend std::ostream & operator<<(std::ostream &_out, const ignition::math::Quaternion< T > &_q)
Stream insertion operator.
Definition Quaternion.hh:1035
static Quaternion< T > EulerToQuaternion(const Vector3< T > &_vec)
Convert euler angles to quatern.
Definition Quaternion.hh:403
void Normalize()
Normalize the quaternion.
Definition Quaternion.hh:223
T Roll() const
Get the Euler roll angle in radians.
Definition Quaternion.hh:422
Quaternion(const Matrix3< T > &_mat)
Construct from rotation matrix.
Definition Quaternion.hh:89
Vector3< T > RotateVector(const Vector3< T > &_vec) const
Rotate a vector using the quaternion.
Definition Quaternion.hh:714
void Invert()
Invert the quaternion.
Definition Quaternion.hh:120
const T & X() const
Get the x component.
Definition Quaternion.hh:955
Vector3< T > Euler() const
Return the rotation in Euler angles.
Definition Quaternion.hh:334
Vector3< T > XAxis() const
Return the X axis.
Definition Quaternion.hh:771
Quaternion< T > operator*(const T &_f) const
Multiplication operator by a scalar.
Definition Quaternion.hh:652
Quaternion< T > operator*(const Quaternion< T > &_q) const
Multiplication operator.
Definition Quaternion.hh:640
static Quaternion< T > EulerToQuaternion(T _x, T _y, T _z)
Convert euler angles to quatern.
Definition Quaternion.hh:415
Quaternion(const T &_w, const T &_x, const T &_y, const T &_z)
Constructor.
Definition Quaternion.hh:58
void ToAxis(Vector3< T > &_axis, T &_angle) const
Return rotation as axis and angle.
Definition Quaternion.hh:444
void Round(int _precision)
Round all values to _precision decimal places.
Definition Quaternion.hh:822
static Quaternion< T > Slerp(T _fT, const Quaternion< T > &_rkP, const Quaternion< T > &_rkQ, bool _shortestPath=false)
Spherical linear interpolation between 2 quaternions, given the ends and an interpolation parameter b...
Definition Quaternion.hh:868
const T & Y() const
Get the y component.
Definition Quaternion.hh:962
void Y(T _v)
Set the y component.
Definition Quaternion.hh:1012
bool operator==(const Quaternion< T > &_qt) const
Equal to operator.
Definition Quaternion.hh:685
void Euler(const Vector3< T > &_vec)
Set the quaternion from Euler angles.
Definition Quaternion.hh:303
Quaternion< T > operator-(const Quaternion< T > &_qt) const
Subtraction operator.
Definition Quaternion.hh:621
void From2Axes(const Vector3< T > &_v1, const Vector3< T > &_v2)
Set this quaternion to represent rotation from vector _v1 to vector _v2, so that _v2....
Definition Quaternion.hh:513
Vector3< T > YAxis() const
Return the Y axis.
Definition Quaternion.hh:788
Quaternion< T > operator-() const
Unary minus operator.
Definition Quaternion.hh:706
T Dot(const Quaternion< T > &_q) const
Dot product.
Definition Quaternion.hh:833
Quaternion< T > Inverse() const
Get the inverse of this quaternion.
Definition Quaternion.hh:131
bool IsFinite() const
See if a quaternion is finite (e.g., not nan)
Definition Quaternion.hh:736
T & X()
Get a mutable x component.
Definition Quaternion.hh:984
T & Z()
Get a mutable z component.
Definition Quaternion.hh:998
Vector3< T > RotateVectorReverse(Vector3< T > _vec) const
Do the reverse rotation of a vector by this quaternion.
Definition Quaternion.hh:725
Quaternion(const Vector3< T > &_axis, const T &_angle)
Constructor from axis angle.
Definition Quaternion.hh:74
Quaternion< T > Log() const
Return the logarithm.
Definition Quaternion.hh:160
static const Quaternion Zero
math::Quaternion(0, 0, 0, 0)
Definition Quaternion.hh:43
void Z(T _v)
Set the z component.
Definition Quaternion.hh:1019
~Quaternion()
Destructor.
Definition Quaternion.hh:105
void Euler(T _roll, T _pitch, T _yaw)
Set the quaternion from Euler angles.
Definition Quaternion.hh:312
T Yaw() const
Get the Euler yaw angle in radians.
Definition Quaternion.hh:436
const T & Z() const
Get the z component.
Definition Quaternion.hh:969
Quaternion< T > operator-=(const Quaternion< T > &_qt)
Subtraction operator.
Definition Quaternion.hh:631
void Correct()
Correct any nan values in this quaternion.
Definition Quaternion.hh:747
void W(T _v)
Set the w component.
Definition Quaternion.hh:1026
Quaternion< T > operator*=(const Quaternion< T > &qt)
Multiplication operator.
Definition Quaternion.hh:661
Quaternion< T > Exp() const
Return the exponent.
Definition Quaternion.hh:192
Quaternion(const Vector3< T > &_rpy)
Constructor.
Definition Quaternion.hh:81
Vector3< T > operator*(const Vector3< T > &_v) const
Vector3 multiplication operator.
Definition Quaternion.hh:670
void Axis(const Vector3< T > &_axis, T _a)
Set the quaternion from an axis and angle.
Definition Quaternion.hh:280
bool operator!=(const Quaternion< T > &_qt) const
Not equal to operator.
Definition Quaternion.hh:696
Quaternion< T > operator+=(const Quaternion< T > &_qt)
Addition operator.
Definition Quaternion.hh:611
friend std::istream & operator>>(std::istream &_in, ignition::math::Quaternion< T > &_q)
Stream extraction operator.
Definition Quaternion.hh:1048
void Scale(T _scale)
Scale a Quaternion<T>ion.
Definition Quaternion.hh:585
Quaternion(const T &_roll, const T &_pitch, const T &_yaw)
Constructor from Euler angles in radians.
Definition Quaternion.hh:66
void Axis(T _ax, T _ay, T _az, T _aa)
Set the quaternion from an axis and angle.
Definition Quaternion.hh:251
static const Quaternion Identity
math::Quaternion(1, 0, 0, 0)
Definition Quaternion.hh:40
const T & W() const
Get the w component.
Definition Quaternion.hh:948
Quaternion()
Default Constructor.
Definition Quaternion.hh:46
T Pitch() const
Get the Euler pitch angle in radians.
Definition Quaternion.hh:429
Vector3< T > ZAxis() const
Return the Z axis.
Definition Quaternion.hh:805
void Set(T _w, T _x, T _y, T _z)
Set this quaternion from 4 floating numbers.
Definition Quaternion.hh:290
T & W()
Get a mutable w component.
Definition Quaternion.hh:977
Quaternion(const Quaternion< T > &_qt)
Copy constructor.
Definition Quaternion.hh:96
static Quaternion< T > Squad(T _fT, const Quaternion< T > &_rkP, const Quaternion< T > &_rkA, const Quaternion< T > &_rkB, const Quaternion< T > &_rkQ, bool _shortestPath=false)
Spherical quadratic interpolation given the ends and an interpolation parameter between 0 and 1.
Definition Quaternion.hh:849
Quaternion< T > operator+(const Quaternion< T > &_qt) const
Addition operator.
Definition Quaternion.hh:601
The Vector3 class represents the generic vector containing 3 elements.
Definition Vector3.hh:40
Vector3 Abs() const
Get the absolute value of the vector.
Definition Vector3.hh:222
void Set(T _x=0, T _y=0, T _z=0)
Set the contents of the vector.
Definition Vector3.hh:178
T Z() const
Get the z value.
Definition Vector3.hh:661
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition Vector3.hh:123
T Y() const
Get the y value.
Definition Vector3.hh:654
Vector3 Normalize()
Normalize the vector length.
Definition Vector3.hh:132
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition Vector3.hh:198
Vector3 Cross(const Vector3< T > &_v) const
Return the cross product of this vector with another vector.
Definition Vector3.hh:188
T X() const
Get the x value.
Definition Vector3.hh:647
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition Helpers.hh:579
Quaternion< int > Quaternioni
Definition Quaternion.hh:1083
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition Helpers.hh:545
static const double MIN_D
Double min value. This value will be similar to 2.22507e-308.
Definition Helpers.hh:249
Quaternion< double > Quaterniond
Definition Quaternion.hh:1081
Quaternion< float > Quaternionf
Definition Quaternion.hh:1082