All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Quaternion.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2012 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 /* Desc: External interfaces for Gazebo
18  * Author: Nate Koenig
19  * Date: 03 Apr 2007
20  */
21 
22 #ifndef _QUATERNION_HH_
23 #define _QUATERNION_HH_
24 
25 #include <math.h>
26 #include <iostream>
27 #include <cmath>
28 
29 #include "gazebo/math/Helpers.hh"
30 #include "gazebo/math/Angle.hh"
31 #include "gazebo/math/Vector3.hh"
32 #include "gazebo/math/Matrix3.hh"
33 #include "gazebo/math/Matrix4.hh"
34 
35 namespace gazebo
36 {
37  namespace math
38  {
41 
44  class Quaternion
45  {
47  public: Quaternion();
48 
54  public: Quaternion(const double &_w, const double &_x, const double &_y,
55  const double &_z);
56 
61  public: Quaternion(const double &_roll, const double &_pitch,
62  const double &_yaw);
63 
67  public: Quaternion(const Vector3 &_axis, const double &_angle);
68 
71  public: Quaternion(const Vector3 &_rpy);
72 
75  public: Quaternion(const Quaternion &_qt);
76 
78  public: ~Quaternion();
79 
82  public: Quaternion &operator =(const Quaternion &_qt);
83 
85  public: void Invert();
86 
89  public: inline Quaternion GetInverse() const
90  {
91  double s = 0;
92  Quaternion q(this->w, this->x, this->y, this->z);
93 
94  // use s to test if quaternion is valid
95  s = q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z;
96 
97  if (math::equal(s, 0.0))
98  {
99  q.w = 1.0;
100  q.x = 0.0;
101  q.y = 0.0;
102  q.z = 0.0;
103  }
104  else
105  {
106  // deal with non-normalized quaternion
107  // div by s so q * qinv = identity
108  q.w = q.w / s;
109  q.x = -q.x / s;
110  q.y = -q.y / s;
111  q.z = -q.z / s;
112  }
113  return q;
114  }
115 
117  public: void SetToIdentity();
118 
121  public: Quaternion GetLog() const;
122 
125  public: Quaternion GetExp() const;
126 
128  public: void Normalize();
129 
135  public: void SetFromAxis(double _x, double _y, double _z, double _a);
136 
140  public: void SetFromAxis(const Vector3 &_axis, double _a);
141 
147  public: void Set(double _u, double _x, double _y, double _z);
148 
151  public: void SetFromEuler(const Vector3 &_vec);
152 
157  public: void SetFromEuler(double _roll, double _pitch, double _yaw);
158 
161  public: Vector3 GetAsEuler() const;
162 
165  public: static Quaternion EulerToQuaternion(const Vector3 &_vec);
166 
171  public: static Quaternion EulerToQuaternion(double _x,
172  double _y,
173  double _z);
174 
177  public: double GetRoll();
178 
181  public: double GetPitch();
182 
185  public: double GetYaw();
186 
190  public: void GetAsAxis(Vector3 &_axis, double &_angle) const;
191 
194  public: void Scale(double _scale);
195 
199  public: Quaternion operator+(const Quaternion &_qt) const;
200 
204  public: Quaternion operator+=(const Quaternion &_qt);
205 
209  public: Quaternion operator-(const Quaternion &_qt) const;
210 
214  public: Quaternion operator-=(const Quaternion &_qt);
215 
219  public: inline Quaternion operator*(const Quaternion &_q) const
220  {
221  return Quaternion(
222  this->w*_q.w - this->x*_q.x - this->y*_q.y - this->z*_q.z,
223  this->w*_q.x + this->x*_q.w + this->y*_q.z - this->z*_q.y,
224  this->w*_q.y - this->x*_q.z + this->y*_q.w + this->z*_q.x,
225  this->w*_q.z + this->x*_q.y - this->y*_q.x + this->z*_q.w);
226  }
227 
231  public: Quaternion operator*(const double &_f) const;
232 
236  public: Quaternion operator*=(const Quaternion &qt);
237 
240  public: Vector3 operator*(const Vector3 &_v) const;
241 
245  public: bool operator ==(const Quaternion &_qt) const;
246 
250  public: bool operator!=(const Quaternion &_qt) const;
251 
254  public: Quaternion operator-() const;
255 
259  public: inline Vector3 RotateVector(const Vector3 &_vec) const
260  {
261  Quaternion tmp(0.0, _vec.x, _vec.y, _vec.z);
262  tmp = (*this) * (tmp * this->GetInverse());
263  return Vector3(tmp.x, tmp.y, tmp.z);
264  }
265 
269  public: Vector3 RotateVectorReverse(Vector3 _vec) const;
270 
273  public: bool IsFinite() const;
274 
276  public: inline void Correct()
277  {
278  if (!finite(this->x))
279  this->x = 0;
280  if (!finite(this->y))
281  this->y = 0;
282  if (!finite(this->z))
283  this->z = 0;
284  if (!finite(this->w))
285  this->w = 1;
286 
287  if (math::equal(this->w, 0.0) &&
288  math::equal(this->x, 0.0) &&
289  math::equal(this->y, 0.0) &&
290  math::equal(this->z, 0.0))
291  {
292  this->w = 1;
293  }
294  }
295 
297  public: Matrix3 GetAsMatrix3() const;
298 
301  public: Matrix4 GetAsMatrix4() const;
302 
305  public: Vector3 GetXAxis() const;
306 
309  public: Vector3 GetYAxis() const;
310 
313  public: Vector3 GetZAxis() const;
314 
317  public: void Round(int _precision);
318 
322  public: double Dot(const Quaternion &_q) const;
323 
333  public: static Quaternion Squad(double _fT, const Quaternion &_rkP,
334  const Quaternion &_rkA, const Quaternion &_rkB,
335  const Quaternion &_rkQ, bool _shortestPath = false);
336 
344  public: static Quaternion Slerp(double _fT, const Quaternion &_rkP,
345  const Quaternion &_rkQ, bool _shortestPath = false);
346 
347 
349  public: double w;
350 
352  public: double x;
353 
355  public: double y;
356 
358  public: double z;
359 
364  public: friend std::ostream &operator<<(std::ostream &_out,
365  const gazebo::math::Quaternion &_q)
366  {
367  Vector3 v(_q.GetAsEuler());
368  _out << precision(v.x, 6) << " " << precision(v.y, 6) << " "
369  << precision(v.z, 6);
370  return _out;
371  }
372 
377  public: friend std::istream &operator>>(std::istream &_in,
379  {
380  Angle r, p, y;
381 
382  // Skip white spaces
383  _in.setf(std::ios_base::skipws);
384  _in >> r >> p >> y;
385 
386  _q.SetFromEuler(Vector3(*r, *p, *y));
387 
388  return _in;
389  }
390  };
392  }
393 }
394 #endif
395