Quaternion.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_MATH_QUATERNION_HH_
18 #define IGNITION_MATH_QUATERNION_HH_
19 
20 #include <ignition/math/Helpers.hh>
21 #include <ignition/math/Angle.hh>
22 #include <ignition/math/Vector3.hh>
23 #include <ignition/math/Matrix3.hh>
24 
25 namespace ignition
26 {
27  namespace math
28  {
29  template <typename T> class Matrix3;
30 
33  template<typename T>
34  class Quaternion
35  {
37  public: static const Quaternion Identity;
38 
40  public: static const Quaternion Zero;
41 
43  public: Quaternion()
44  : qw(1), qx(0), qy(0), qz(0)
45  {
46  // quaternion not normalized, because that breaks
47  // Pose::CoordPositionAdd(...)
48  }
49 
55  public: Quaternion(const T &_w, const T &_x, const T &_y, const T &_z)
56  : qw(_w), qx(_x), qy(_y), qz(_z)
57  {}
58 
63  public: Quaternion(const T &_roll, const T &_pitch, const T &_yaw)
64  {
65  this->Euler(Vector3<T>(_roll, _pitch, _yaw));
66  }
67 
71  public: Quaternion(const Vector3<T> &_axis, const T &_angle)
72  {
73  this->Axis(_axis, _angle);
74  }
75 
78  public: Quaternion(const Vector3<T> &_rpy)
79  {
80  this->Euler(_rpy);
81  }
82 
86  public: explicit Quaternion(const Matrix3<T> &_mat)
87  {
88  this->Matrix(_mat);
89  }
90 
93  public: Quaternion(const Quaternion<T> &_qt)
94  {
95  this->qw = _qt.qw;
96  this->qx = _qt.qx;
97  this->qy = _qt.qy;
98  this->qz = _qt.qz;
99  }
100 
102  public: ~Quaternion() {}
103 
106  public: Quaternion<T> &operator=(const Quaternion<T> &_qt)
107  {
108  this->qw = _qt.qw;
109  this->qx = _qt.qx;
110  this->qy = _qt.qy;
111  this->qz = _qt.qz;
112 
113  return *this;
114  }
115 
117  public: void Invert()
118  {
119  this->Normalize();
120  // this->qw = this->qw;
121  this->qx = -this->qx;
122  this->qy = -this->qy;
123  this->qz = -this->qz;
124  }
125 
128  public: inline Quaternion<T> Inverse() const
129  {
130  T s = 0;
131  Quaternion<T> q(this->qw, this->qx, this->qy, this->qz);
132 
133  // use s to test if quaternion is valid
134  s = q.qw * q.qw + q.qx * q.qx + q.qy * q.qy + q.qz * q.qz;
135 
136  if (equal<T>(s, static_cast<T>(0)))
137  {
138  q.qw = 1.0;
139  q.qx = 0.0;
140  q.qy = 0.0;
141  q.qz = 0.0;
142  }
143  else
144  {
145  // deal with non-normalized quaternion
146  // div by s so q * qinv = identity
147  q.qw = q.qw / s;
148  q.qx = -q.qx / s;
149  q.qy = -q.qy / s;
150  q.qz = -q.qz / s;
151  }
152  return q;
153  }
154 
157  public: Quaternion<T> Log() const
158  {
159  // If q = cos(A)+sin(A)*(x*i+y*j+z*k) where (x, y, z) is unit length,
160  // then log(q) = A*(x*i+y*j+z*k). If sin(A) is near zero, use log(q) =
161  // sin(A)*(x*i+y*j+z*k) since sin(A)/A has limit 1.
162 
163  Quaternion<T> result;
164  result.qw = 0.0;
165 
166  if (std::abs(this->qw) < 1.0)
167  {
168  T fAngle = acos(this->qw);
169  T fSin = sin(fAngle);
170  if (std::abs(fSin) >= 1e-3)
171  {
172  T fCoeff = fAngle/fSin;
173  result.qx = fCoeff*this->qx;
174  result.qy = fCoeff*this->qy;
175  result.qz = fCoeff*this->qz;
176  return result;
177  }
178  }
179 
180  result.qx = this->qx;
181  result.qy = this->qy;
182  result.qz = this->qz;
183 
184  return result;
185  }
186 
189  public: Quaternion<T> Exp() const
190  {
191  // If q = A*(x*i+y*j+z*k) where (x, y, z) is unit length, then
192  // exp(q) = cos(A)+sin(A)*(x*i+y*j+z*k). If sin(A) is near zero,
193  // use exp(q) = cos(A)+A*(x*i+y*j+z*k) since A/sin(A) has limit 1.
194 
195  T fAngle = sqrt(this->qx*this->qx+
196  this->qy*this->qy+this->qz*this->qz);
197  T fSin = sin(fAngle);
198 
199  Quaternion<T> result;
200  result.qw = cos(fAngle);
201 
202  if (std::abs(fSin) >= 1e-3)
203  {
204  T fCoeff = fSin/fAngle;
205  result.qx = fCoeff*this->qx;
206  result.qy = fCoeff*this->qy;
207  result.qz = fCoeff*this->qz;
208  }
209  else
210  {
211  result.qx = this->qx;
212  result.qy = this->qy;
213  result.qz = this->qz;
214  }
215 
216  return result;
217  }
218 
220  public: void Normalize()
221  {
222  T s = 0;
223 
224  s = sqrt(this->qw * this->qw + this->qx * this->qx +
225  this->qy * this->qy + this->qz * this->qz);
226 
227  if (equal<T>(s, static_cast<T>(0)))
228  {
229  this->qw = 1.0;
230  this->qx = 0.0;
231  this->qy = 0.0;
232  this->qz = 0.0;
233  }
234  else
235  {
236  this->qw /= s;
237  this->qx /= s;
238  this->qy /= s;
239  this->qz /= s;
240  }
241  }
242 
248  public: void Axis(T _ax, T _ay, T _az, T _aa)
249  {
250  T l;
251 
252  l = _ax * _ax + _ay * _ay + _az * _az;
253 
254  if (equal<T>(l, static_cast<T>(0)))
255  {
256  this->qw = 1;
257  this->qx = 0;
258  this->qy = 0;
259  this->qz = 0;
260  }
261  else
262  {
263  _aa *= 0.5;
264  l = sin(_aa) / sqrt(l);
265  this->qw = cos(_aa);
266  this->qx = _ax * l;
267  this->qy = _ay * l;
268  this->qz = _az * l;
269  }
270 
271  this->Normalize();
272  }
273 
277  public: void Axis(const Vector3<T> &_axis, T _a)
278  {
279  this->Axis(_axis.X(), _axis.Y(), _axis.Z(), _a);
280  }
281 
287  public: void Set(T _w, T _x, T _y, T _z)
288  {
289  this->qw = _w;
290  this->qx = _x;
291  this->qy = _y;
292  this->qz = _z;
293  }
294 
300  public: void Euler(const Vector3<T> &_vec)
301  {
302  this->Euler(_vec.X(), _vec.Y(), _vec.Z());
303  }
304 
309  public: void Euler(T _roll, T _pitch, T _yaw)
310  {
311  T phi, the, psi;
312 
313  phi = _roll / 2.0;
314  the = _pitch / 2.0;
315  psi = _yaw / 2.0;
316 
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);
325 
326  this->Normalize();
327  }
328 
331  public: Vector3<T> Euler() const
332  {
333  Vector3<T> vec;
334 
335  Quaternion<T> copy = *this;
336  T squ;
337  T sqx;
338  T sqy;
339  T sqz;
340 
341  copy.Normalize();
342 
343  squ = copy.qw * copy.qw;
344  sqx = copy.qx * copy.qx;
345  sqy = copy.qy * copy.qy;
346  sqz = copy.qz * copy.qz;
347 
348  // Roll
349  vec.X(atan2(2 * (copy.qy*copy.qz + copy.qw*copy.qx),
350  squ - sqx - sqy + sqz));
351 
352  // Pitch
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)));
356 
357  // Yaw
358  vec.Z(atan2(2 * (copy.qx*copy.qy + copy.qw*copy.qz),
359  squ + sqx - sqy - sqz));
360 
361  return vec;
362  }
363 
367  public: static Quaternion<T> EulerToQuaternion(const Vector3<T> &_vec)
368  {
369  Quaternion<T> result;
370  result.Euler(_vec);
371  return result;
372  }
373 
379  public: static Quaternion<T> EulerToQuaternion(T _x, T _y, T _z)
380  {
381  return EulerToQuaternion(Vector3<T>(_x, _y, _z));
382  }
383 
386  public: T Roll() const
387  {
388  return this->Euler().X();
389  }
390 
393  public: T Pitch() const
394  {
395  return this->Euler().Y();
396  }
397 
400  public: T Yaw() const
401  {
402  return this->Euler().Z();
403  }
404 
408  public: void ToAxis(Vector3<T> &_axis, T &_angle) const
409  {
410  T len = this->qx*this->qx + this->qy*this->qy + this->qz*this->qz;
411  if (equal<T>(len, static_cast<T>(0)))
412  {
413  _angle = 0.0;
414  _axis.Set(1, 0, 0);
415  }
416  else
417  {
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);
421  }
422  }
423 
431  void Matrix(const Matrix3<T> &_mat)
432  {
433  const T trace = _mat(0, 0) + _mat(1, 1) + _mat(2, 2);
434  if (trace > 0.0000001)
435  {
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;
441  }
442  else if (_mat(0, 0) > _mat(1, 1) && _mat(0, 0) > _mat(2, 2))
443  {
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;
449  }
450  else if (_mat(1, 1) > _mat(2, 2))
451  {
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;
457  }
458  else
459  {
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;
465  }
466  }
467 
477  public: void From2Axes(const Vector3<T> &_v1, const Vector3<T> &_v2)
478  {
479  // generally, we utilize the fact that a quat (w, x, y, z) represents
480  // rotation of angle 2*w about axis (x, y, z)
481  //
482  // so we want to take get a vector half-way between no rotation and the
483  // double rotation, which is
484  // [ (1, (0, 0, 0)) + (_v1 dot _v2, _v1 x _v2) ] / 2
485  // if _v1 and _v2 are unit quaternions
486  //
487  // since we normalize the result anyway, we can omit the division,
488  // getting the result:
489  // [ (1, (0, 0, 0)) + (_v1 dot _v2, _v1 x _v2) ].Normalized()
490  //
491  // if _v1 and _v2 are not normalized, the magnitude (1 + _v1 dot _v2)
492  // is multiplied by k = norm(_v1)*norm(_v2)
493 
494  const T kCosTheta = _v1.Dot(_v2);
495  const T k = sqrt(_v1.SquaredLength() * _v2.SquaredLength());
496 
497  if (fabs(kCosTheta/k + 1) < 1e-6)
498  {
499  // the vectors are opposite
500  // any vector orthogonal to _v1
501  Vector3<T> other;
502  {
503  const Vector3<T> _v1Abs(_v1.Abs());
504  if (_v1Abs.X() < _v1Abs.Y())
505  {
506  if (_v1Abs.X() < _v1Abs.Z())
507  {
508  other = {1, 0, 0};
509  }
510  else
511  {
512  other = {0, 0, 1};
513  }
514  }
515  else
516  {
517  if (_v1Abs.Y() < _v1Abs.Z())
518  {
519  other = {0, 1, 0};
520  }
521  else
522  {
523  other = {0, 0, 1};
524  }
525  }
526  }
527 
528  const Vector3<T> axis(_v1.Cross(other).Normalize());
529 
530  qw = 0;
531  qx = axis.X();
532  qy = axis.Y();
533  qz = axis.Z();
534  }
535  else
536  {
537  // the vectors are in general position
538  const Vector3<T> axis(_v1.Cross(_v2));
539  qw = kCosTheta + k;
540  qx = axis.X();
541  qy = axis.Y();
542  qz = axis.Z();
543  this->Normalize();
544  }
545  }
546 
549  public: void Scale(T _scale)
550  {
551  Quaternion<T> b;
552  Vector3<T> axis;
553  T angle;
554 
555  // Convert to axis-and-angle
556  this->ToAxis(axis, angle);
557  angle *= _scale;
558 
559  this->Axis(axis.X(), axis.Y(), axis.Z(), angle);
560  }
561 
565  public: Quaternion<T> operator+(const Quaternion<T> &_qt) const
566  {
567  Quaternion<T> result(this->qw + _qt.qw, this->qx + _qt.qx,
568  this->qy + _qt.qy, this->qz + _qt.qz);
569  return result;
570  }
571 
576  {
577  *this = *this + _qt;
578 
579  return *this;
580  }
581 
585  public: Quaternion<T> operator-(const Quaternion<T> &_qt) const
586  {
587  Quaternion<T> result(this->qw - _qt.qw, this->qx - _qt.qx,
588  this->qy - _qt.qy, this->qz - _qt.qz);
589  return result;
590  }
591 
596  {
597  *this = *this - _qt;
598  return *this;
599  }
600 
604  public: inline Quaternion<T> operator*(const Quaternion<T> &_q) const
605  {
606  return Quaternion<T>(
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);
611  }
612 
616  public: Quaternion<T> operator*(const T &_f) const
617  {
618  return Quaternion<T>(this->qw*_f, this->qx*_f,
619  this->qy*_f, this->qz*_f);
620  }
621 
626  {
627  *this = *this * qt;
628  return *this;
629  }
630 
634  public: Vector3<T> operator*(const Vector3<T> &_v) const
635  {
636  Vector3<T> uv, uuv;
637  Vector3<T> qvec(this->qx, this->qy, this->qz);
638  uv = qvec.Cross(_v);
639  uuv = qvec.Cross(uv);
640  uv *= (2.0f * this->qw);
641  uuv *= 2.0f;
642 
643  return _v + uv + uuv;
644  }
645 
649  public: bool operator==(const Quaternion<T> &_qt) const
650  {
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));
655  }
656 
660  public: bool operator!=(const Quaternion<T> &_qt) const
661  {
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));
666  }
667 
670  public: Quaternion<T> operator-() const
671  {
672  return Quaternion<T>(-this->qw, -this->qx, -this->qy, -this->qz);
673  }
674 
678  public: inline Vector3<T> RotateVector(const Vector3<T> &_vec) const
679  {
680  Quaternion<T> tmp(static_cast<T>(0),
681  _vec.X(), _vec.Y(), _vec.Z());
682  tmp = (*this) * (tmp * this->Inverse());
683  return Vector3<T>(tmp.qx, tmp.qy, tmp.qz);
684  }
685 
690  {
691  Quaternion<T> tmp(0.0, _vec.X(), _vec.Y(), _vec.Z());
692 
693  tmp = this->Inverse() * (tmp * (*this));
694 
695  return Vector3<T>(tmp.qx, tmp.qy, tmp.qz);
696  }
697 
700  public: bool IsFinite() const
701  {
702  // std::isfinite works with floating point values, need to explicit
703  // cast to avoid ambiguity in vc++.
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));
708  }
709 
711  public: inline void Correct()
712  {
713  // std::isfinite works with floating point values, need to explicit
714  // cast to avoid ambiguity in vc++.
715  if (!std::isfinite(static_cast<double>(this->qx)))
716  this->qx = 0;
717  if (!std::isfinite(static_cast<double>(this->qy)))
718  this->qy = 0;
719  if (!std::isfinite(static_cast<double>(this->qz)))
720  this->qz = 0;
721  if (!std::isfinite(static_cast<double>(this->qw)))
722  this->qw = 1;
723 
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)))
728  {
729  this->qw = 1;
730  }
731  }
732 
735  public: Vector3<T> XAxis() const
736  {
737  T fTy = 2.0f*this->qy;
738  T fTz = 2.0f*this->qz;
739 
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;
746 
747  return Vector3<T>(1.0f-(fTyy+fTzz), fTxy+fTwz, fTxz-fTwy);
748  }
749 
752  public: Vector3<T> YAxis() const
753  {
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;
763 
764  return Vector3<T>(fTxy-fTwz, 1.0f-(fTxx+fTzz), fTyz+fTwx);
765  }
766 
769  public: Vector3<T> ZAxis() const
770  {
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;
780 
781  return Vector3<T>(fTxz+fTwy, fTyz-fTwx, 1.0f-(fTxx+fTyy));
782  }
783 
786  public: void Round(int _precision)
787  {
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);
792  }
793 
797  public: T Dot(const Quaternion<T> &_q) const
798  {
799  return this->qw*_q.qw + this->qx * _q.qx +
800  this->qy*_q.qy + this->qz*_q.qz;
801  }
802 
813  public: static Quaternion<T> Squad(T _fT,
814  const Quaternion<T> &_rkP, const Quaternion<T> &_rkA,
815  const Quaternion<T> &_rkB, const Quaternion<T> &_rkQ,
816  bool _shortestPath = false)
817  {
818  T fSlerpT = 2.0f*_fT*(1.0f-_fT);
819  Quaternion<T> kSlerpP = Slerp(_fT, _rkP, _rkQ, _shortestPath);
820  Quaternion<T> kSlerpQ = Slerp(_fT, _rkA, _rkB);
821  return Slerp(fSlerpT, kSlerpP, kSlerpQ);
822  }
823 
832  public: static Quaternion<T> Slerp(T _fT,
833  const Quaternion<T> &_rkP, const Quaternion<T> &_rkQ,
834  bool _shortestPath = false)
835  {
836  T fCos = _rkP.Dot(_rkQ);
837  Quaternion<T> rkT;
838 
839  // Do we need to invert rotation?
840  if (fCos < 0.0f && _shortestPath)
841  {
842  fCos = -fCos;
843  rkT = -_rkQ;
844  }
845  else
846  {
847  rkT = _rkQ;
848  }
849 
850  if (std::abs(fCos) < 1 - 1e-03)
851  {
852  // Standard case (slerp)
853  T fSin = sqrt(1 - (fCos*fCos));
854  T fAngle = atan2(fSin, fCos);
855  // FIXME: should check if (std::abs(fSin) >= 1e-3)
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;
860  }
861  else
862  {
863  // There are two situations:
864  // 1. "rkP" and "rkQ" are very close (fCos ~= +1),
865  // so we can do a linear interpolation safely.
866  // 2. "rkP" and "rkQ" are almost inverse of each
867  // other (fCos ~= -1), there
868  // are an infinite number of possibilities interpolation.
869  // but we haven't have method to fix this case, so just use
870  // linear interpolation here.
871  Quaternion<T> t = _rkP * (1.0f - _fT) + rkT * _fT;
872  // taking the complement requires renormalisation
873  t.Normalize();
874  return t;
875  }
876  }
877 
886  public: Quaternion<T> Integrate(const Vector3<T> &_angularVelocity,
887  const T _deltaT) const
888  {
889  Quaternion<T> deltaQ;
890  Vector3<T> theta = _angularVelocity * _deltaT * 0.5;
891  T thetaMagSq = theta.SquaredLength();
892  T s;
893  if (thetaMagSq * thetaMagSq / 24.0 < IGN_DBL_MIN)
894  {
895  deltaQ.W() = 1.0 - thetaMagSq / 2.0;
896  s = 1.0 - thetaMagSq / 6.0;
897  }
898  else
899  {
900  double thetaMag = sqrt(thetaMagSq);
901  deltaQ.W() = cos(thetaMag);
902  s = sin(thetaMag) / thetaMag;
903  }
904  deltaQ.X() = theta.X() * s;
905  deltaQ.Y() = theta.Y() * s;
906  deltaQ.Z() = theta.Z() * s;
907  return deltaQ * (*this);
908  }
909 
912  public: inline const T &W() const
913  {
914  return this->qw;
915  }
916 
919  public: inline const T &X() const
920  {
921  return this->qx;
922  }
923 
926  public: inline const T &Y() const
927  {
928  return this->qy;
929  }
930 
933  public: inline const T &Z() const
934  {
935  return this->qz;
936  }
937 
938 
941  public: inline T &W()
942  {
943  return this->qw;
944  }
945 
948  public: inline T &X()
949  {
950  return this->qx;
951  }
952 
955  public: inline T &Y()
956  {
957  return this->qy;
958  }
959 
962  public: inline T &Z()
963  {
964  return this->qz;
965  }
966 
969  public: inline void X(T _v)
970  {
971  this->qx = _v;
972  }
973 
976  public: inline void Y(T _v)
977  {
978  this->qy = _v;
979  }
980 
983  public: inline void Z(T _v)
984  {
985  this->qz = _v;
986  }
987 
990  public: inline void W(T _v)
991  {
992  this->qw = _v;
993  }
994 
999  public: friend std::ostream &operator<<(std::ostream &_out,
1001  {
1002  Vector3<T> v(_q.Euler());
1003  _out << precision(v.X(), 6) << " " << precision(v.Y(), 6) << " "
1004  << precision(v.Z(), 6);
1005  return _out;
1006  }
1007 
1012  public: friend std::istream &operator>>(std::istream &_in,
1014  {
1015  Angle roll, pitch, yaw;
1016 
1017  // Skip white spaces
1018  _in.setf(std::ios_base::skipws);
1019  _in >> roll >> pitch >> yaw;
1020 
1021  _q.Euler(Vector3<T>(*roll, *pitch, *yaw));
1022 
1023  return _in;
1024  }
1025 
1027  private: T qw;
1028 
1030  private: T qx;
1031 
1033  private: T qy;
1034 
1036  private: T qz;
1037  };
1038 
1039  template<typename T> const Quaternion<T>
1040  Quaternion<T>::Identity(1, 0, 0, 0);
1041 
1042  template<typename T> const Quaternion<T>
1043  Quaternion<T>::Zero(0, 0, 0, 0);
1044 
1048  }
1049 }
1050 #endif
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