17 #ifndef IGNITION_MATH_QUATERNION_HH_ 18 #define IGNITION_MATH_QUATERNION_HH_ 29 template <
typename T>
class Matrix3;
44 : qw(1), qx(0), qy(0), qz(0)
55 public:
Quaternion(
const T &_w,
const T &_x,
const T &_y,
const T &_z)
56 : qw(_w), qx(_x), qy(_y), qz(_z)
63 public:
Quaternion(
const T &_roll,
const T &_pitch,
const T &_yaw)
73 this->
Axis(_axis, _angle);
121 this->qx = -this->qx;
122 this->qy = -this->qy;
123 this->qz = -this->qz;
134 s = q.qw * q.qw + q.qx * q.qx + q.qy * q.qy + q.qz * q.qz;
136 if (equal<T>(s, static_cast<T>(0)))
166 if (std::abs(this->qw) < 1.0)
168 T fAngle = acos(this->qw);
169 T fSin = sin(fAngle);
170 if (std::abs(fSin) >= 1e-3)
172 T fCoeff = fAngle/fSin;
173 result.qx = fCoeff*this->qx;
174 result.qy = fCoeff*this->qy;
175 result.qz = fCoeff*this->qz;
180 result.qx = this->qx;
181 result.qy = this->qy;
182 result.qz = this->qz;
195 T fAngle = sqrt(this->qx*this->qx+
196 this->qy*this->qy+this->qz*this->qz);
197 T fSin = sin(fAngle);
200 result.qw = cos(fAngle);
202 if (std::abs(fSin) >= 1e-3)
204 T fCoeff = fSin/fAngle;
205 result.qx = fCoeff*this->qx;
206 result.qy = fCoeff*this->qy;
207 result.qz = fCoeff*this->qz;
211 result.qx = this->qx;
212 result.qy = this->qy;
213 result.qz = this->qz;
224 s = sqrt(this->qw * this->qw + this->qx * this->qx +
225 this->qy * this->qy + this->qz * this->qz);
227 if (equal<T>(s, static_cast<T>(0)))
248 public:
void Axis(T _ax, T _ay, T _az, T _aa)
252 l = _ax * _ax + _ay * _ay + _az * _az;
254 if (equal<T>(l, static_cast<T>(0)))
264 l = sin(_aa) / sqrt(l);
279 this->
Axis(_axis.
X(), _axis.
Y(), _axis.
Z(), _a);
287 public:
void Set(T _w, T _x, T _y, T _z)
302 this->
Euler(_vec.
X(), _vec.
Y(), _vec.
Z());
309 public:
void Euler(T _roll, T _pitch, T _yaw)
317 this->qw = cos(phi) * cos(the) * cos(psi) +
318 sin(phi) * sin(the) * sin(psi);
319 this->qx = sin(phi) * cos(the) * cos(psi) -
320 cos(phi) * sin(the) * sin(psi);
321 this->qy = cos(phi) * sin(the) * cos(psi) +
322 sin(phi) * cos(the) * sin(psi);
323 this->qz = cos(phi) * cos(the) * sin(psi) -
324 sin(phi) * sin(the) * cos(psi);
343 squ = copy.qw * copy.qw;
344 sqx = copy.qx * copy.qx;
345 sqy = copy.qy * copy.qy;
346 sqz = copy.qz * copy.qz;
349 vec.
X(atan2(2 * (copy.qy*copy.qz + copy.qw*copy.qx),
350 squ - sqx - sqy + sqz));
353 T sarg = -2 * (copy.qx*copy.qz - copy.qw * copy.qy);
354 vec.
Y(sarg <= -1.0 ? -0.5*
IGN_PI :
355 (sarg >= 1.0 ? 0.5*
IGN_PI : asin(sarg)));
358 vec.
Z(atan2(2 * (copy.qx*copy.qy + copy.qw*copy.qz),
359 squ + sqx - sqy - sqz));
388 return this->
Euler().X();
395 return this->
Euler().Y();
402 return this->
Euler().Z();
410 T len = this->qx*this->qx + this->qy*this->qy + this->qz*this->qz;
411 if (equal<T>(len, static_cast<T>(0)))
418 _angle = 2.0 * acos(this->qw);
419 T invLen = 1.0 / sqrt(len);
420 _axis.
Set(this->qx*invLen, this->qy*invLen, this->qz*invLen);
433 const T trace = _mat(0, 0) + _mat(1, 1) + _mat(2, 2);
434 if (trace > 0.0000001)
436 qw = sqrt(1 + trace) / 2;
437 const T s = 1.0 / (4 * qw);
438 qx = (_mat(2, 1) - _mat(1, 2)) * s;
439 qy = (_mat(0, 2) - _mat(2, 0)) * s;
440 qz = (_mat(1, 0) - _mat(0, 1)) * s;
442 else if (_mat(0, 0) > _mat(1, 1) && _mat(0, 0) > _mat(2, 2))
444 qx = sqrt(1.0 + _mat(0, 0) - _mat(1, 1) - _mat(2, 2)) / 2;
445 const T s = 1.0 / (4 * qx);
446 qw = (_mat(2, 1) - _mat(1, 2)) * s;
447 qy = (_mat(1, 0) + _mat(0, 1)) * s;
448 qz = (_mat(0, 2) + _mat(2, 0)) * s;
450 else if (_mat(1, 1) > _mat(2, 2))
452 qy = sqrt(1.0 - _mat(0, 0) + _mat(1, 1) - _mat(2, 2)) / 2;
453 const T s = 1.0 / (4 * qy);
454 qw = (_mat(0, 2) - _mat(2, 0)) * s;
455 qx = (_mat(0, 1) + _mat(1, 0)) * s;
456 qz = (_mat(1, 2) + _mat(2, 1)) * s;
460 qz = sqrt(1.0 - _mat(0, 0) - _mat(1, 1) + _mat(2, 2)) / 2;
461 const T s = 1.0 / (4 * qz);
462 qw = (_mat(1, 0) - _mat(0, 1)) * s;
463 qx = (_mat(0, 2) + _mat(2, 0)) * s;
464 qy = (_mat(1, 2) + _mat(2, 1)) * s;
494 const T kCosTheta = _v1.
Dot(_v2);
497 if (fabs(kCosTheta/k + 1) < 1e-6)
504 if (_v1Abs.X() < _v1Abs.Y())
506 if (_v1Abs.X() < _v1Abs.Z())
517 if (_v1Abs.Y() < _v1Abs.Z())
556 this->
ToAxis(axis, angle);
559 this->
Axis(axis.
X(), axis.
Y(), axis.
Z(), angle);
568 this->qy + _qt.qy, this->qz + _qt.qz);
588 this->qy - _qt.qy, this->qz - _qt.qz);
607 this->qw*_q.qw-this->qx*_q.qx-this->qy*_q.qy-this->qz*_q.qz,
608 this->qw*_q.qx+this->qx*_q.qw+this->qy*_q.qz-this->qz*_q.qy,
609 this->qw*_q.qy-this->qx*_q.qz+this->qy*_q.qw+this->qz*_q.qx,
610 this->qw*_q.qz+this->qx*_q.qy-this->qy*_q.qx+this->qz*_q.qw);
619 this->qy*_f, this->qz*_f);
637 Vector3<T> qvec(this->qx, this->qy, this->qz);
639 uuv = qvec.
Cross(uv);
640 uv *= (2.0f * this->qw);
643 return _v + uv + uuv;
651 return equal(this->qx, _qt.qx, static_cast<T>(0.001)) &&
652 equal(this->qy, _qt.qy, static_cast<T>(0.001)) &&
653 equal(this->qz, _qt.qz, static_cast<T>(0.001)) &&
654 equal(this->qw, _qt.qw, static_cast<T>(0.001));
662 return !
equal(this->qx, _qt.qx, static_cast<T>(0.001)) ||
663 !
equal(this->qy, _qt.qy, static_cast<T>(0.001)) ||
664 !
equal(this->qz, _qt.qz, static_cast<T>(0.001)) ||
665 !
equal(this->qw, _qt.qw, static_cast<T>(0.001));
672 return Quaternion<T>(-this->qw, -this->qx, -this->qy, -this->qz);
681 _vec.
X(), _vec.
Y(), _vec.
Z());
682 tmp = (*this) * (tmp * this->
Inverse());
693 tmp = this->
Inverse() * (tmp * (*this));
704 return std::isfinite(static_cast<double>(this->qw)) &&
705 std::isfinite(static_cast<double>(this->qx)) &&
706 std::isfinite(static_cast<double>(this->qy)) &&
707 std::isfinite(static_cast<double>(this->qz));
715 if (!std::isfinite(static_cast<double>(this->qx)))
717 if (!std::isfinite(static_cast<double>(this->qy)))
719 if (!std::isfinite(static_cast<double>(this->qz)))
721 if (!std::isfinite(static_cast<double>(this->qw)))
724 if (
equal(this->qw, static_cast<T>(0)) &&
725 equal(this->qx, static_cast<T>(0)) &&
726 equal(this->qy, static_cast<T>(0)) &&
727 equal(this->qz, static_cast<T>(0)))
737 T fTy = 2.0f*this->qy;
738 T fTz = 2.0f*this->qz;
740 T fTwy = fTy*this->qw;
741 T fTwz = fTz*this->qw;
742 T fTxy = fTy*this->qx;
743 T fTxz = fTz*this->qx;
744 T fTyy = fTy*this->qy;
745 T fTzz = fTz*this->qz;
747 return Vector3<T>(1.0f-(fTyy+fTzz), fTxy+fTwz, fTxz-fTwy);
754 T fTx = 2.0f*this->qx;
755 T fTy = 2.0f*this->qy;
756 T fTz = 2.0f*this->qz;
757 T fTwx = fTx*this->qw;
758 T fTwz = fTz*this->qw;
759 T fTxx = fTx*this->qx;
760 T fTxy = fTy*this->qx;
761 T fTyz = fTz*this->qy;
762 T fTzz = fTz*this->qz;
764 return Vector3<T>(fTxy-fTwz, 1.0f-(fTxx+fTzz), fTyz+fTwx);
771 T fTx = 2.0f*this->qx;
772 T fTy = 2.0f*this->qy;
773 T fTz = 2.0f*this->qz;
774 T fTwx = fTx*this->qw;
775 T fTwy = fTy*this->qw;
776 T fTxx = fTx*this->qx;
777 T fTxz = fTz*this->qx;
778 T fTyy = fTy*this->qy;
779 T fTyz = fTz*this->qy;
781 return Vector3<T>(fTxz+fTwy, fTyz-fTwx, 1.0f-(fTxx+fTyy));
788 this->qx =
precision(this->qx, _precision);
789 this->qy =
precision(this->qy, _precision);
790 this->qz =
precision(this->qz, _precision);
791 this->qw =
precision(this->qw, _precision);
799 return this->qw*_q.qw + this->qx * _q.qx +
800 this->qy*_q.qy + this->qz*_q.qz;
816 bool _shortestPath =
false)
818 T fSlerpT = 2.0f*_fT*(1.0f-_fT);
821 return Slerp(fSlerpT, kSlerpP, kSlerpQ);
834 bool _shortestPath =
false)
836 T fCos = _rkP.Dot(_rkQ);
840 if (fCos < 0.0f && _shortestPath)
850 if (std::abs(fCos) < 1 - 1e-03)
853 T fSin = sqrt(1 - (fCos*fCos));
854 T fAngle = atan2(fSin, fCos);
856 T fInvSin = 1.0f / fSin;
857 T fCoeff0 = sin((1.0f - _fT) * fAngle) * fInvSin;
858 T fCoeff1 = sin(_fT * fAngle) * fInvSin;
859 return _rkP * fCoeff0 + rkT * fCoeff1;
887 const T _deltaT)
const 890 Vector3<T> theta = _angularVelocity * _deltaT * 0.5;
895 deltaQ.W() = 1.0 - thetaMagSq / 2.0;
896 s = 1.0 - thetaMagSq / 6.0;
900 double thetaMag = sqrt(thetaMagSq);
901 deltaQ.W() = cos(thetaMag);
902 s = sin(thetaMag) / thetaMag;
904 deltaQ.X() = theta.
X() * s;
905 deltaQ.Y() = theta.
Y() * s;
906 deltaQ.Z() = theta.
Z() * s;
907 return deltaQ * (*this);
912 public:
inline const T &
W()
const 919 public:
inline const T &
X()
const 926 public:
inline const T &
Y()
const 933 public:
inline const T &
Z()
const 941 public:
inline T &
W()
948 public:
inline T &
X()
955 public:
inline T &
Y()
962 public:
inline T &
Z()
969 public:
inline void X(T _v)
976 public:
inline void Y(T _v)
983 public:
inline void Z(T _v)
990 public:
inline void W(T _v)
1015 Angle roll, pitch, yaw;
1018 _in.setf(std::ios_base::skipws);
1019 _in >> roll >> pitch >> yaw;
An angle and related functions.
Definition: Angle.hh:44
Quaternion< double > Quaterniond
Definition: Quaternion.hh:1045
void Round(int _precision)
Round all values to _precision decimal places.
Definition: Quaternion.hh:786
void Y(T _v)
Set the y component.
Definition: Quaternion.hh:976
Quaternion(const Vector3< T > &_axis, const T &_angle)
Constructor from axis angle.
Definition: Quaternion.hh:71
void Set(T _x=0, T _y=0, T _z=0)
Set the contents of the vector.
Definition: Vector3.hh:176
~Quaternion()
Destructor.
Definition: Quaternion.hh:102
void X(T _v)
Set the x component.
Definition: Quaternion.hh:969
void Euler(T _roll, T _pitch, T _yaw)
Set the quaternion from Euler angles.
Definition: Quaternion.hh:309
static const Quaternion Zero
math::Quaternion(0, 0, 0, 0)
Definition: Quaternion.hh:40
Quaternion(const Vector3< T > &_rpy)
Constructor.
Definition: Quaternion.hh:78
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:363
Vector3< T > operator*(const Vector3< T > &_v) const
Vector3 multiplication operator.
Definition: Quaternion.hh:634
bool operator==(const Quaternion< T > &_qt) const
Equal to operator.
Definition: Quaternion.hh:649
void Z(T _v)
Set the z component.
Definition: Quaternion.hh:983
#define IGN_DBL_MIN
Double min value. This value will be similar to 2.22507e-308.
Definition: Helpers.hh:33
Quaternion< T > Inverse() const
Get the inverse of this quaternion.
Definition: Quaternion.hh:128
void Invert()
Invert the quaternion.
Definition: Quaternion.hh:117
Quaternion< T > operator-(const Quaternion< T > &_qt) const
Subtraction operator.
Definition: Quaternion.hh:585
T & X()
Get a mutable x component.
Definition: Quaternion.hh:948
T Roll() const
Get the Euler roll angle in radians.
Definition: Quaternion.hh:386
Quaternion< float > Quaternionf
Definition: Quaternion.hh:1046
friend std::istream & operator>>(std::istream &_in, ignition::math::Quaternion< T > &_q)
Stream extraction operator.
Definition: Quaternion.hh:1012
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:813
const T & Y() const
Get the y component.
Definition: Quaternion.hh:926
T & Y()
Get a mutable y component.
Definition: Quaternion.hh:955
Vector3< T > Euler() const
Return the rotation in Euler angles.
Definition: Quaternion.hh:331
T Pitch() const
Get the Euler pitch angle in radians.
Definition: Quaternion.hh:393
T X() const
Get the x value.
Definition: Vector3.hh:639
Quaternion< T > operator*(const T &_f) const
Multiplication operator by a scalar.
Definition: Quaternion.hh:616
const T & Z() const
Get the z component.
Definition: Quaternion.hh:933
Vector3 Abs() const
Get the absolute value of the vector.
Definition: Vector3.hh:220
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:196
Quaternion< T > operator+=(const Quaternion< T > &_qt)
Addition operator.
Definition: Quaternion.hh:575
void Correct()
Correct any nan values in this quaternion.
Definition: Quaternion.hh:711
Vector3 Cross(const Vector3< T > &_v) const
Return the cross product of this vector with another vector.
Definition: Vector3.hh:186
Quaternion(const Quaternion< T > &_qt)
Copy constructor.
Definition: Quaternion.hh:93
Vector3< T > YAxis() const
Return the Y axis.
Definition: Quaternion.hh:752
static Quaternion< T > EulerToQuaternion(const Vector3< T > &_vec)
Convert euler angles to quatern.
Definition: Quaternion.hh:367
T Dot(const Quaternion< T > &_q) const
Dot product.
Definition: Quaternion.hh:797
Quaternion< T > & operator=(const Quaternion< T > &_qt)
Equal operator.
Definition: Quaternion.hh:106
T Y() const
Get the y value.
Definition: Vector3.hh:646
void Axis(const Vector3< T > &_axis, T _a)
Set the quaternion from an axis and angle.
Definition: Quaternion.hh:277
void Axis(T _ax, T _ay, T _az, T _aa)
Set the quaternion from an axis and angle.
Definition: Quaternion.hh:248
A 3x3 matrix class.
Definition: Matrix3.hh:35
Quaternion(const T &_w, const T &_x, const T &_y, const T &_z)
Constructor.
Definition: Quaternion.hh:55
Vector3< T > RotateVectorReverse(Vector3< T > _vec) const
Do the reverse rotation of a vector by this quaternion.
Definition: Quaternion.hh:689
static const Quaternion Identity
math::Quaternion(1, 0, 0, 0)
Definition: Quaternion.hh:37
friend std::ostream & operator<<(std::ostream &_out, const ignition::math::Quaternion< T > &_q)
Stream insertion operator.
Definition: Quaternion.hh:999
Vector3< T > XAxis() const
Return the X axis.
Definition: Quaternion.hh:735
Quaternion< T > operator*(const Quaternion< T > &_q) const
Multiplication operator.
Definition: Quaternion.hh:604
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:477
T & W()
Get a mutable w component.
Definition: Quaternion.hh:941
T Z() const
Get the z value.
Definition: Vector3.hh:653
void Scale(T _scale)
Scale a Quaternion<T>ion.
Definition: Quaternion.hh:549
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:832
void ToAxis(Vector3< T > &_axis, T &_angle) const
Return rotation as axis and angle.
Definition: Quaternion.hh:408
Quaternion< T > operator-() const
Unary minus operator.
Definition: Quaternion.hh:670
Quaternion< T > operator+(const Quaternion< T > &_qt) const
Addition operator.
Definition: Quaternion.hh:565
Quaternion(const Matrix3< T > &_mat)
Construct from rotation matrix.
Definition: Quaternion.hh:86
T & Z()
Get a mutable z component.
Definition: Quaternion.hh:962
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:37
bool IsFinite() const
See if a quaternion is finite (e.g., not nan)
Definition: Quaternion.hh:700
bool operator!=(const Quaternion< T > &_qt) const
Not equal to operator.
Definition: Quaternion.hh:660
void W(T _v)
Set the w component.
Definition: Quaternion.hh:990
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition: Vector3.hh:121
Quaternion< T > operator*=(const Quaternion< T > &qt)
Multiplication operator.
Definition: Quaternion.hh:625
Quaternion< int > Quaternioni
Definition: Quaternion.hh:1047
void Euler(const Vector3< T > &_vec)
Set the quaternion from Euler angles.
Definition: Quaternion.hh:300
T Yaw() const
Get the Euler yaw angle in radians.
Definition: Quaternion.hh:400
Quaternion()
Default Constructor.
Definition: Quaternion.hh:43
const T & W() const
Get the w component.
Definition: Quaternion.hh:912
void Matrix(const Matrix3< T > &_mat)
Set from a rotation matrix.
Definition: Quaternion.hh:431
Definition: AffineException.hh:30
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:351
void Normalize()
Normalize the quaternion.
Definition: Quaternion.hh:220
A quaternion class.
Definition: Matrix3.hh:30
Quaternion< T > Exp() const
Return the exponent.
Definition: Quaternion.hh:189
Quaternion< T > operator-=(const Quaternion< T > &_qt)
Subtraction operator.
Definition: Quaternion.hh:595
#define IGN_PI
Define IGN_PI, IGN_PI_2, and IGN_PI_4.
Definition: Helpers.hh:139
Vector3< T > RotateVector(const Vector3< T > &_vec) const
Rotate a vector using the quaternion.
Definition: Quaternion.hh:678
Quaternion(const T &_roll, const T &_pitch, const T &_yaw)
Constructor from Euler angles in radians.
Definition: Quaternion.hh:63
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:886
Quaternion< T > Log() const
Return the logarithm.
Definition: Quaternion.hh:157
Vector3< T > ZAxis() const
Return the Z axis.
Definition: Quaternion.hh:769
static Quaternion< T > EulerToQuaternion(T _x, T _y, T _z)
Convert euler angles to quatern.
Definition: Quaternion.hh:379
void Set(T _w, T _x, T _y, T _z)
Set this quaternion from 4 floating numbers.
Definition: Quaternion.hh:287
const T & X() const
Get the x component.
Definition: Quaternion.hh:919