Quaternion.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 
18 #ifndef _GAZEBO_MATH_QUATERNION_HH_
19 #define _GAZEBO_MATH_QUATERNION_HH_
20 
21 #include <math.h>
22 #include <iostream>
23 #include <cmath>
24 
25 #include "gazebo/math/Helpers.hh"
26 #include "gazebo/math/Angle.hh"
27 #include "gazebo/math/Vector3.hh"
28 #include "gazebo/math/Matrix3.hh"
29 #include "gazebo/math/Matrix4.hh"
30 #include "gazebo/util/system.hh"
31 
32 namespace gazebo
33 {
34  namespace math
35  {
38 
42  {
44  public: Quaternion();
45 
51  public: Quaternion(const double &_w, const double &_x, const double &_y,
52  const double &_z);
53 
58  public: Quaternion(const double &_roll, const double &_pitch,
59  const double &_yaw);
60 
64  public: Quaternion(const Vector3 &_axis, const double &_angle);
65 
68  public: Quaternion(const Vector3 &_rpy);
69 
72  public: Quaternion(const Quaternion &_qt);
73 
75  public: ~Quaternion();
76 
79  public: Quaternion &operator =(const Quaternion &_qt);
80 
82  public: void Invert();
83 
86  public: inline Quaternion GetInverse() const
87  {
88  double s = 0;
89  Quaternion q(this->w, this->x, this->y, this->z);
90 
91  // use s to test if quaternion is valid
92  s = q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z;
93 
94  if (math::equal(s, 0.0))
95  {
96  q.w = 1.0;
97  q.x = 0.0;
98  q.y = 0.0;
99  q.z = 0.0;
100  }
101  else
102  {
103  // deal with non-normalized quaternion
104  // div by s so q * qinv = identity
105  q.w = q.w / s;
106  q.x = -q.x / s;
107  q.y = -q.y / s;
108  q.z = -q.z / s;
109  }
110  return q;
111  }
112 
114  public: void SetToIdentity();
115 
118  public: Quaternion GetLog() const;
119 
122  public: Quaternion GetExp() const;
123 
125  public: void Normalize();
126 
132  public: void SetFromAxis(double _x, double _y, double _z, double _a);
133 
137  public: void SetFromAxis(const Vector3 &_axis, double _a);
138 
144  public: void Set(double _u, double _x, double _y, double _z);
145 
149  public: void SetFromEuler(const Vector3 &_vec);
150 
155  public: void SetFromEuler(double _roll, double _pitch, double _yaw);
156 
159  public: Vector3 GetAsEuler() const;
160 
163  public: static Quaternion EulerToQuaternion(const Vector3 &_vec);
164 
169  public: static Quaternion EulerToQuaternion(double _x,
170  double _y,
171  double _z);
172 
175  public: double GetRoll();
176 
179  public: double GetPitch();
180 
183  public: double GetYaw();
184 
188  public: void GetAsAxis(Vector3 &_axis, double &_angle) const;
189 
192  public: void Scale(double _scale);
193 
197  public: Quaternion operator+(const Quaternion &_qt) const;
198 
202  public: Quaternion operator+=(const Quaternion &_qt);
203 
207  public: Quaternion operator-(const Quaternion &_qt) const;
208 
212  public: Quaternion operator-=(const Quaternion &_qt);
213 
217  public: inline Quaternion operator*(const Quaternion &_q) const
218  {
219  return Quaternion(
220  this->w*_q.w - this->x*_q.x - this->y*_q.y - this->z*_q.z,
221  this->w*_q.x + this->x*_q.w + this->y*_q.z - this->z*_q.y,
222  this->w*_q.y - this->x*_q.z + this->y*_q.w + this->z*_q.x,
223  this->w*_q.z + this->x*_q.y - this->y*_q.x + this->z*_q.w);
224  }
225 
229  public: Quaternion operator*(const double &_f) const;
230 
234  public: Quaternion operator*=(const Quaternion &qt);
235 
238  public: Vector3 operator*(const Vector3 &_v) const;
239 
243  public: bool operator ==(const Quaternion &_qt) const;
244 
248  public: bool operator!=(const Quaternion &_qt) const;
249 
252  public: Quaternion operator-() const;
253 
257  public: inline Vector3 RotateVector(const Vector3 &_vec) const
258  {
259  Quaternion tmp(0.0, _vec.x, _vec.y, _vec.z);
260  tmp = (*this) * (tmp * this->GetInverse());
261  return Vector3(tmp.x, tmp.y, tmp.z);
262  }
263 
267  public: Vector3 RotateVectorReverse(Vector3 _vec) const;
268 
271  public: bool IsFinite() const;
272 
274  public: inline void Correct()
275  {
276  if (!std::isfinite(this->x))
277  this->x = 0;
278  if (!std::isfinite(this->y))
279  this->y = 0;
280  if (!std::isfinite(this->z))
281  this->z = 0;
282  if (!std::isfinite(this->w))
283  this->w = 1;
284 
285  if (math::equal(this->w, 0.0) &&
286  math::equal(this->x, 0.0) &&
287  math::equal(this->y, 0.0) &&
288  math::equal(this->z, 0.0))
289  {
290  this->w = 1;
291  }
292  }
293 
295  public: Matrix3 GetAsMatrix3() const;
296 
299  public: Matrix4 GetAsMatrix4() const;
300 
303  public: Vector3 GetXAxis() const;
304 
307  public: Vector3 GetYAxis() const;
308 
311  public: Vector3 GetZAxis() const;
312 
315  public: void Round(int _precision);
316 
320  public: double Dot(const Quaternion &_q) const;
321 
331  public: static Quaternion Squad(double _fT, const Quaternion &_rkP,
332  const Quaternion &_rkA, const Quaternion &_rkB,
333  const Quaternion &_rkQ, bool _shortestPath = false);
334 
342  public: static Quaternion Slerp(double _fT, const Quaternion &_rkP,
343  const Quaternion &_rkQ, bool _shortestPath = false);
344 
351  public: Quaternion Integrate(const Vector3 &_angularVelocity,
352  const double _deltaT) const;
353 
355  public: double w;
356 
358  public: double x;
359 
361  public: double y;
362 
364  public: double z;
365 
370  public: friend std::ostream &operator<<(std::ostream &_out,
371  const gazebo::math::Quaternion &_q)
372  {
373  Vector3 v(_q.GetAsEuler());
374  _out << precision(v.x, 6) << " " << precision(v.y, 6) << " "
375  << precision(v.z, 6);
376  return _out;
377  }
378 
383  public: friend std::istream &operator>>(std::istream &_in,
385  {
386  Angle roll, pitch, yaw;
387 
388  // Skip white spaces
389  _in.setf(std::ios_base::skipws);
390  _in >> roll >> pitch >> yaw;
391 
392  _q.SetFromEuler(Vector3(*roll, *pitch, *yaw));
393 
394  return _in;
395  }
396  };
398  }
399 }
400 #endif
401 
double z
Attributes of the quaternion.
Definition: Quaternion.hh:364
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:43
A 3x3 matrix class.
Definition: Matrix4.hh:39
double x
X location.
Definition: Vector3.hh:302
Vector3 RotateVector(const Vector3 &_vec) const
Rotate a vector using the quaternion.
Definition: Quaternion.hh:257
void SetFromEuler(const Vector3 &_vec)
Set the quaternion from Euler angles.
double z
Z location.
Definition: Vector3.hh:308
Quaternion operator*(const Quaternion &_q) const
Multiplication operator.
Definition: Quaternion.hh:217
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:168
A 3x3 matrix class.
Definition: Matrix3.hh:34
void Correct()
Correct any nan.
Definition: Quaternion.hh:274
double w
Attributes of the quaternion.
Definition: Quaternion.hh:355
A quaternion class.
Definition: Quaternion.hh:41
friend std::istream & operator>>(std::istream &_in, gazebo::math::Quaternion &_q)
Stream extraction operator.
Definition: Quaternion.hh:383
GAZEBO_VISIBLE void Set(common::Image &_img, const msgs::Image &_msg)
Convert a msgs::Image to a common::Image.
double y
Attributes of the quaternion.
Definition: Quaternion.hh:361
Quaternion GetInverse() const
Get the inverse of this quaternion.
Definition: Quaternion.hh:86
double x
Attributes of the quaternion.
Definition: Quaternion.hh:358
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:179
An angle and related functions.
Definition: Angle.hh:52
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:48
double y
Y location.
Definition: Vector3.hh:305
friend std::ostream & operator<<(std::ostream &_out, const gazebo::math::Quaternion &_q)
Stream insertion operator.
Definition: Quaternion.hh:370
Vector3 GetAsEuler() const
Return the rotation in Euler angles.