17 #ifndef IGNITION_MATH_QUATERNION_HH_ 18 #define IGNITION_MATH_QUATERNION_HH_ 24 #include <ignition/math/config.hh> 31 inline namespace IGNITION_MATH_VERSION_NAMESPACE {
33 template <
typename T>
class Matrix3;
48 : qw(1), qx(0), qy(0), qz(0)
59 public:
Quaternion(
const T &_w,
const T &_x,
const T &_y,
const T &_z)
60 : qw(_w), qx(_x), qy(_y), qz(_z)
67 public:
Quaternion(
const T &_roll,
const T &_pitch,
const T &_yaw)
77 this->Axis(_axis, _angle);
125 this->qx = -this->qx;
126 this->qy = -this->qy;
127 this->qz = -this->qz;
138 s = q.qw * q.qw + q.qx * q.qx + q.qy * q.qy + q.qz * q.qz;
140 if (equal<T>(s, static_cast<T>(0)))
170 if (std::abs(this->qw) < 1.0)
172 T fAngle = acos(this->qw);
173 T fSin = sin(fAngle);
174 if (std::abs(fSin) >= 1e-3)
176 T fCoeff = fAngle/fSin;
177 result.qx = fCoeff*this->qx;
178 result.qy = fCoeff*this->qy;
179 result.qz = fCoeff*this->qz;
184 result.qx = this->qx;
185 result.qy = this->qy;
186 result.qz = this->qz;
199 T fAngle = sqrt(this->qx*this->qx+
200 this->qy*this->qy+this->qz*this->qz);
201 T fSin = sin(fAngle);
204 result.qw = cos(fAngle);
206 if (std::abs(fSin) >= 1e-3)
208 T fCoeff = fSin/fAngle;
209 result.qx = fCoeff*this->qx;
210 result.qy = fCoeff*this->qy;
211 result.qz = fCoeff*this->qz;
215 result.qx = this->qx;
216 result.qy = this->qy;
217 result.qz = this->qz;
228 s = T(sqrt(this->qw * this->qw + this->qx * this->qx +
229 this->qy * this->qy + this->qz * this->qz));
231 if (equal<T>(s, static_cast<T>(0)))
261 public:
void Axis(T _ax, T _ay, T _az, T _aa)
265 l = _ax * _ax + _ay * _ay + _az * _az;
267 if (equal<T>(l, static_cast<T>(0)))
277 l = sin(_aa) / sqrt(l);
292 this->Axis(_axis.
X(), _axis.
Y(), _axis.
Z(), _a);
300 public:
void Set(T _w, T _x, T _y, T _z)
315 this->Euler(_vec.
X(), _vec.
Y(), _vec.
Z());
322 public:
void Euler(T _roll, T _pitch, T _yaw)
326 phi = _roll / T(2.0);
327 the = _pitch / T(2.0);
330 this->qw = T(cos(phi) * cos(the) * cos(psi) +
331 sin(phi) * sin(the) * sin(psi));
332 this->qx = T(sin(phi) * cos(the) * cos(psi) -
333 cos(phi) * sin(the) * sin(psi));
334 this->qy = T(cos(phi) * sin(the) * cos(psi) +
335 sin(phi) * cos(the) * sin(psi));
336 this->qz = T(cos(phi) * cos(the) * sin(psi) -
337 sin(phi) * sin(the) * cos(psi));
348 T tol =
static_cast<T
>(1e-15);
358 squ = copy.qw * copy.qw;
359 sqx = copy.qx * copy.qx;
360 sqy = copy.qy * copy.qy;
361 sqz = copy.qz * copy.qz;
364 T sarg = -2 * (copy.qx*copy.qz - copy.qw * copy.qy);
369 else if (sarg >= T(1.0))
375 vec.
Y(T(asin(sarg)));
383 if (std::abs(sarg - 1) < tol)
386 vec.
X(T(atan2(2 * (copy.qx*copy.qy - copy.qz*copy.qw),
387 squ - sqx + sqy - sqz)));
390 else if (std::abs(sarg + 1) < tol)
393 vec.
X(T(atan2(-2 * (copy.qx*copy.qy - copy.qz*copy.qw),
394 squ - sqx + sqy - sqz)));
399 vec.
X(T(atan2(2 * (copy.qy*copy.qz + copy.qw*copy.qx),
400 squ - sqx - sqy + sqz)));
403 vec.
Z(T(atan2(2 * (copy.qx*copy.qy + copy.qw*copy.qz),
404 squ + sqx - sqy - sqz)));
427 return EulerToQuaternion(
Vector3<T>(_x, _y, _z));
434 return this->Euler().X();
441 return this->Euler().Y();
448 return this->Euler().Z();
456 T len = this->qx*this->qx + this->qy*this->qy + this->qz*this->qz;
457 if (equal<T>(len, static_cast<T>(0)))
464 _angle = 2.0 * acos(this->qw);
465 T invLen = 1.0 / sqrt(len);
466 _axis.
Set(this->qx*invLen, this->qy*invLen, this->qz*invLen);
479 const T trace = _mat(0, 0) + _mat(1, 1) + _mat(2, 2);
480 if (trace > 0.0000001)
482 qw = sqrt(1 + trace) / 2;
483 const T s = 1.0 / (4 * qw);
484 qx = (_mat(2, 1) - _mat(1, 2)) * s;
485 qy = (_mat(0, 2) - _mat(2, 0)) * s;
486 qz = (_mat(1, 0) - _mat(0, 1)) * s;
488 else if (_mat(0, 0) > _mat(1, 1) && _mat(0, 0) > _mat(2, 2))
490 qx = sqrt(1.0 + _mat(0, 0) - _mat(1, 1) - _mat(2, 2)) / 2;
491 const T s = 1.0 / (4 * qx);
492 qw = (_mat(2, 1) - _mat(1, 2)) * s;
493 qy = (_mat(1, 0) + _mat(0, 1)) * s;
494 qz = (_mat(0, 2) + _mat(2, 0)) * s;
496 else if (_mat(1, 1) > _mat(2, 2))
498 qy = sqrt(1.0 - _mat(0, 0) + _mat(1, 1) - _mat(2, 2)) / 2;
499 const T s = 1.0 / (4 * qy);
500 qw = (_mat(0, 2) - _mat(2, 0)) * s;
501 qx = (_mat(0, 1) + _mat(1, 0)) * s;
502 qz = (_mat(1, 2) + _mat(2, 1)) * s;
506 qz = sqrt(1.0 - _mat(0, 0) - _mat(1, 1) + _mat(2, 2)) / 2;
507 const T s = 1.0 / (4 * qz);
508 qw = (_mat(1, 0) - _mat(0, 1)) * s;
509 qx = (_mat(0, 2) + _mat(2, 0)) * s;
510 qy = (_mat(1, 2) + _mat(2, 1)) * s;
540 const T kCosTheta = _v1.
Dot(_v2);
543 if (fabs(kCosTheta/k + 1) < 1e-6)
550 if (_v1Abs.X() < _v1Abs.Y())
552 if (_v1Abs.X() < _v1Abs.Z())
563 if (_v1Abs.Y() < _v1Abs.Z())
602 this->ToAxis(axis, angle);
605 this->Axis(axis.
X(), axis.
Y(), axis.
Z(), angle);
614 this->qy + _qt.qy, this->qz + _qt.qz);
634 this->qy - _qt.qy, this->qz - _qt.qz);
653 this->qw*_q.qw-this->qx*_q.qx-this->qy*_q.qy-this->qz*_q.qz,
654 this->qw*_q.qx+this->qx*_q.qw+this->qy*_q.qz-this->qz*_q.qy,
655 this->qw*_q.qy-this->qx*_q.qz+this->qy*_q.qw+this->qz*_q.qx,
656 this->qw*_q.qz+this->qx*_q.qy-this->qy*_q.qx+this->qz*_q.qw);
665 this->qy*_f, this->qz*_f);
683 Vector3<T> qvec(this->qx, this->qy, this->qz);
685 uuv = qvec.
Cross(uv);
686 uv *= (2.0f * this->qw);
689 return _v + uv + uuv;
697 return equal(this->qx, _qt.qx, static_cast<T>(0.001)) &&
698 equal(this->qy, _qt.qy, static_cast<T>(0.001)) &&
699 equal(this->qz, _qt.qz, static_cast<T>(0.001)) &&
700 equal(this->qw, _qt.qw, static_cast<T>(0.001));
708 return !
equal(this->qx, _qt.qx, static_cast<T>(0.001)) ||
709 !
equal(this->qy, _qt.qy, static_cast<T>(0.001)) ||
710 !
equal(this->qz, _qt.qz, static_cast<T>(0.001)) ||
711 !
equal(this->qw, _qt.qw, static_cast<T>(0.001));
718 return Quaternion<T>(-this->qw, -this->qx, -this->qy, -this->qz);
727 _vec.
X(), _vec.
Y(), _vec.
Z());
728 tmp = (*this) * (tmp * this->Inverse());
739 tmp = this->Inverse() * (tmp * (*this));
770 if (
equal(this->qw, static_cast<T>(0)) &&
771 equal(this->qx, static_cast<T>(0)) &&
772 equal(this->qy, static_cast<T>(0)) &&
773 equal(this->qz, static_cast<T>(0)))
783 T fTy = 2.0f*this->qy;
784 T fTz = 2.0f*this->qz;
786 T fTwy = fTy*this->qw;
787 T fTwz = fTz*this->qw;
788 T fTxy = fTy*this->qx;
789 T fTxz = fTz*this->qx;
790 T fTyy = fTy*this->qy;
791 T fTzz = fTz*this->qz;
793 return Vector3<T>(1.0f-(fTyy+fTzz), fTxy+fTwz, fTxz-fTwy);
800 T fTx = 2.0f*this->qx;
801 T fTy = 2.0f*this->qy;
802 T fTz = 2.0f*this->qz;
803 T fTwx = fTx*this->qw;
804 T fTwz = fTz*this->qw;
805 T fTxx = fTx*this->qx;
806 T fTxy = fTy*this->qx;
807 T fTyz = fTz*this->qy;
808 T fTzz = fTz*this->qz;
810 return Vector3<T>(fTxy-fTwz, 1.0f-(fTxx+fTzz), fTyz+fTwx);
817 T fTx = 2.0f*this->qx;
818 T fTy = 2.0f*this->qy;
819 T fTz = 2.0f*this->qz;
820 T fTwx = fTx*this->qw;
821 T fTwy = fTy*this->qw;
822 T fTxx = fTx*this->qx;
823 T fTxz = fTz*this->qx;
824 T fTyy = fTy*this->qy;
825 T fTyz = fTz*this->qy;
827 return Vector3<T>(fTxz+fTwy, fTyz-fTwx, 1.0f-(fTxx+fTyy));
834 this->qx =
precision(this->qx, _precision);
835 this->qy =
precision(this->qy, _precision);
836 this->qz =
precision(this->qz, _precision);
837 this->qw =
precision(this->qw, _precision);
845 return this->qw*_q.qw + this->qx * _q.qx +
846 this->qy*_q.qy + this->qz*_q.qz;
862 bool _shortestPath =
false)
864 T fSlerpT = 2.0f*_fT*(1.0f-_fT);
865 Quaternion<T> kSlerpP = Slerp(_fT, _rkP, _rkQ, _shortestPath);
867 return Slerp(fSlerpT, kSlerpP, kSlerpQ);
880 bool _shortestPath =
false)
882 T fCos = _rkP.Dot(_rkQ);
886 if (fCos < 0.0f && _shortestPath)
896 if (std::abs(fCos) < 1 - 1e-03)
899 T fSin = sqrt(1 - (fCos*fCos));
900 T fAngle = atan2(fSin, fCos);
902 T fInvSin = 1.0f / fSin;
903 T fCoeff0 = sin((1.0f - _fT) * fAngle) * fInvSin;
904 T fCoeff1 = sin(_fT * fAngle) * fInvSin;
905 return _rkP * fCoeff0 + rkT * fCoeff1;
933 const T _deltaT)
const 936 Vector3<T> theta = _angularVelocity * _deltaT * 0.5;
939 if (thetaMagSq * thetaMagSq / 24.0 <
MIN_D)
941 deltaQ.W() = 1.0 - thetaMagSq / 2.0;
942 s = 1.0 - thetaMagSq / 6.0;
946 double thetaMag = sqrt(thetaMagSq);
947 deltaQ.W() = cos(thetaMag);
948 s = sin(thetaMag) / thetaMag;
950 deltaQ.X() = theta.
X() * s;
951 deltaQ.Y() = theta.
Y() * s;
952 deltaQ.Z() = theta.
Z() * s;
953 return deltaQ * (*this);
958 public:
inline const T &
W()
const 965 public:
inline const T &
X()
const 972 public:
inline const T &
Y()
const 979 public:
inline const T &
Z()
const 987 public:
inline T &
W()
994 public:
inline T &
X()
1001 public:
inline T &
Y()
1008 public:
inline T &
Z()
1015 public:
inline void X(T _v)
1022 public:
inline void Y(T _v)
1029 public:
inline void Z(T _v)
1036 public:
inline void W(T _v)
1061 Angle roll, pitch, yaw;
1064 _in.
setf(std::ios_base::skipws);
1065 _in >> roll >> pitch >> yaw;
The Angle class is used to simplify and clarify the use of radians and degrees measurements. A default constructed Angle instance has a value of zero radians/degrees.
Definition: Angle.hh:61
void Round(int _precision)
Round all values to _precision decimal places.
Definition: Quaternion.hh:832
void ToAxis(Vector3< T > &_axis, T &_angle) const
Return rotation as axis and angle.
Definition: Quaternion.hh:454
Quaternion< T > operator*=(const Quaternion< T > &_qt)
Multiplication operator.
Definition: Quaternion.hh:671
Quaternion(const Quaternion< T > &_qt)
Copy constructor.
Definition: Quaternion.hh:97
Quaternion< double > Quaterniond
Definition: Quaternion.hh:1091
friend std::ostream & operator<<(std::ostream &_out, const Quaternion< T > &_q)
Stream insertion operator.
Definition: Quaternion.hh:1045
Vector3< T > Euler() const
Return the rotation in Euler angles.
Definition: Quaternion.hh:344
Quaternion()
Default Constructor.
Definition: Quaternion.hh:47
Quaternion(const Matrix3< T > &_mat)
Construct from rotation matrix.
Definition: Quaternion.hh:90
const T & W() const
Get the w component.
Definition: Quaternion.hh:958
void Matrix(const Matrix3< T > &_mat)
Set from a rotation matrix.
Definition: Quaternion.hh:477
T & W()
Get a mutable w component.
Definition: Quaternion.hh:987
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.Normalize() == this * _v1.Normalize() holds.
Definition: Quaternion.hh:523
void Set(T _x=0, T _y=0, T _z=0)
Set the contents of the vector.
Definition: Vector3.hh:179
Quaternion< T > operator-=(const Quaternion< T > &_qt)
Subtraction operator.
Definition: Quaternion.hh:641
Quaternion< float > Quaternionf
Definition: Quaternion.hh:1092
const T & Z() const
Get the z component.
Definition: Quaternion.hh:979
Quaternion< T > operator-(const Quaternion< T > &_qt) const
Subtraction operator.
Definition: Quaternion.hh:631
Vector3< T > XAxis() const
Return the X axis.
Definition: Quaternion.hh:781
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:590
Vector3< T > RotateVectorReverse(const Vector3< T > &_vec) const
Do the reverse rotation of a vector by this quaternion.
Definition: Quaternion.hh:735
void Normalize()
Normalize the quaternion.
Definition: Quaternion.hh:224
void Correct()
Correct any nan values in this quaternion.
Definition: Quaternion.hh:757
friend std::istream & operator>>(std::istream &_in, Quaternion< T > &_q)
Stream extraction operator.
Definition: Quaternion.hh:1058
Quaternion< T > operator+(const Quaternion< T > &_qt) const
Addition operator.
Definition: Quaternion.hh:611
Quaternion< T > operator-() const
Unary minus operator.
Definition: Quaternion.hh:716
Quaternion< T > Log() const
Return the logarithm.
Definition: Quaternion.hh:161
const T & X() const
Get the x component.
Definition: Quaternion.hh:965
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:556
Vector3< T > ZAxis() const
Return the Z axis.
Definition: Quaternion.hh:815
Vector3< T > RotateVector(const Vector3< T > &_vec) const
Rotate a vector using the quaternion.
Definition: Quaternion.hh:724
void Y(T _v)
Set the y component.
Definition: Quaternion.hh:1022
const T & Y() const
Get the y component.
Definition: Quaternion.hh:972
void Z(T _v)
Set the z component.
Definition: Quaternion.hh:1029
static const double MIN_D
Double min value. This value will be similar to 2.22507e-308.
Definition: Helpers.hh:260
bool IsFinite() const
See if a quaternion is finite (e.g., not nan)
Definition: Quaternion.hh:746
Quaternion< T > & operator=(const Quaternion< T > &_qt)
Assignment operator.
Definition: Quaternion.hh:110
Quaternion(const T &_w, const T &_x, const T &_y, const T &_z)
Constructor.
Definition: Quaternion.hh:59
T X() const
Get the x value.
Definition: Vector3.hh:648
void Set(T _w, T _x, T _y, T _z)
Set this quaternion from 4 floating numbers.
Definition: Quaternion.hh:300
static Quaternion< T > EulerToQuaternion(const Vector3< T > &_vec)
Convert euler angles to quatern.
Definition: Quaternion.hh:413
static Quaternion< T > EulerToQuaternion(T _x, T _y, T _z)
Convert euler angles to quatern.
Definition: Quaternion.hh:425
T Z() const
Get the z value.
Definition: Vector3.hh:662
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:859
void Axis(T _ax, T _ay, T _az, T _aa)
Set the quaternion from an axis and angle.
Definition: Quaternion.hh:261
T Y() const
Get the y value.
Definition: Vector3.hh:655
T Dot(const Quaternion< T > &_q) const
Dot product.
Definition: Quaternion.hh:843
Vector3 Abs() const
Get the absolute value of the vector.
Definition: Vector3.hh:223
Quaternion(const Vector3< T > &_axis, const T &_angle)
Constructor from axis angle.
Definition: Quaternion.hh:75
void X(T _v)
Set the x component.
Definition: Quaternion.hh:1015
void Euler(const Vector3< T > &_vec)
Set the quaternion from Euler angles. The order of operations is roll, pitch, yaw around a fixed body...
Definition: Quaternion.hh:313
Quaternion< T > operator*(const T &_f) const
Multiplication operator by a scalar.
Definition: Quaternion.hh:662
Quaternion< T > operator*(const Quaternion< T > &_q) const
Multiplication operator.
Definition: Quaternion.hh:650
A 3x3 matrix class.
Definition: Matrix3.hh:40
T Pitch() const
Get the Euler pitch angle in radians.
Definition: Quaternion.hh:439
T & Y()
Get a mutable y component.
Definition: Quaternion.hh:1001
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:199
T & X()
Get a mutable x component.
Definition: Quaternion.hh:994
Vector3 Cross(const Vector3< T > &_v) const
Return the cross product of this vector with another vector.
Definition: Vector3.hh:189
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:878
bool operator!=(const Quaternion< T > &_qt) const
Not equal to operator.
Definition: Quaternion.hh:706
static const Quaternion Identity
math::Quaternion(1, 0, 0, 0)
Definition: Quaternion.hh:41
void Scale(T _scale)
Scale a Quaternion<T>ion.
Definition: Quaternion.hh:595
void W(T _v)
Set the w component.
Definition: Quaternion.hh:1036
T Roll() const
Get the Euler roll angle in radians.
Definition: Quaternion.hh:432
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition: Vector3.hh:124
void Invert()
Invert the quaternion.
Definition: Quaternion.hh:121
bool operator==(const Quaternion< T > &_qt) const
Equal to operator.
Definition: Quaternion.hh:695
~Quaternion()
Destructor.
Definition: Quaternion.hh:106
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: Vector3.hh:40
Quaternion< int > Quaternioni
Definition: Quaternion.hh:1093
Quaternion< T > Normalized() const
Gets a normalized version of this quaternion.
Definition: Quaternion.hh:249
Vector3< T > YAxis() const
Return the Y axis.
Definition: Quaternion.hh:798
static const Quaternion Zero
math::Quaternion(0, 0, 0, 0)
Definition: Quaternion.hh:44
Quaternion(const Vector3< T > &_rpy)
Constructor.
Definition: Quaternion.hh:82
void Euler(T _roll, T _pitch, T _yaw)
Set the quaternion from Euler angles.
Definition: Quaternion.hh:322
T & Z()
Get a mutable z component.
Definition: Quaternion.hh:1008
Quaternion< T > operator+=(const Quaternion< T > &_qt)
Addition operator.
Definition: Quaternion.hh:621
Quaternion(const T &_roll, const T &_pitch, const T &_yaw)
Constructor from Euler angles in radians.
Definition: Quaternion.hh:67
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:932
Quaternion< T > Exp() const
Return the exponent.
Definition: Quaternion.hh:193
A quaternion class.
Definition: Matrix3.hh:35
Quaternion< T > Inverse() const
Get the inverse of this quaternion.
Definition: Quaternion.hh:132
#define IGN_PI
Define IGN_PI, IGN_PI_2, and IGN_PI_4. This was put here for Windows support.
Definition: Helpers.hh:184
Vector3< T > operator*(const Vector3< T > &_v) const
Vector3 multiplication operator.
Definition: Quaternion.hh:680
void Axis(const Vector3< T > &_axis, T _a)
Set the quaternion from an axis and angle.
Definition: Quaternion.hh:290
T Yaw() const
Get the Euler yaw angle in radians.
Definition: Quaternion.hh:446