Ignition Math

API Reference

4.0.0
Pose3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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 #ifndef IGNITION_MATH_POSE_HH_
18 #define IGNITION_MATH_POSE_HH_
19 
21 #include <ignition/math/Vector3.hh>
22 #include <ignition/math/config.hh>
23 
24 namespace ignition
25 {
26  namespace math
27  {
28  // Inline bracket to help doxygen filtering.
29  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
30  //
33  template<typename T>
34  class Pose3
35  {
37  public: static const Pose3<T> Zero;
38 
40  public: Pose3() : p(0, 0, 0), q(1, 0, 0, 0)
41  {
42  }
43 
47  public: Pose3(const Vector3<T> &_pos, const Quaternion<T> &_rot)
48  : p(_pos), q(_rot)
49  {
50  }
51 
59  public: Pose3(T _x, T _y, T _z, T _roll, T _pitch, T _yaw)
60  : p(_x, _y, _z), q(_roll, _pitch, _yaw)
61  {
62  }
63 
72  public: Pose3(T _x, T _y, T _z, T _qw, T _qx, T _qy, T _qz)
73  : p(_x, _y, _z), q(_qw, _qx, _qy, _qz)
74  {
75  }
76 
79  public: Pose3(const Pose3<T> &_pose)
80  : p(_pose.p), q(_pose.q)
81  {
82  }
83 
85  public: virtual ~Pose3()
86  {
87  }
88 
92  public: void Set(const Vector3<T> &_pos, const Quaternion<T> &_rot)
93  {
94  this->p = _pos;
95  this->q = _rot;
96  }
97 
101  public: void Set(const Vector3<T> &_pos, const Vector3<T> &_rpy)
102  {
103  this->p = _pos;
104  this->q.Euler(_rpy);
105  }
106 
114  public: void Set(T _x, T _y, T _z, T _roll, T _pitch, T _yaw)
115  {
116  this->p.Set(_x, _y, _z);
117  this->q.Euler(math::Vector3<T>(_roll, _pitch, _yaw));
118  }
119 
121  public: bool IsFinite() const
122  {
123  return this->p.IsFinite() && this->q.IsFinite();
124  }
125 
127  public: inline void Correct()
128  {
129  this->p.Correct();
130  this->q.Correct();
131  }
132 
135  public: Pose3<T> Inverse() const
136  {
137  Quaternion<T> inv = this->q.Inverse();
138  return Pose3<T>(inv * (this->p*-1), inv);
139  }
140 
147  public: Pose3<T> operator+(const Pose3<T> &_pose) const
148  {
149  Pose3<T> result;
150 
151  result.p = this->CoordPositionAdd(_pose);
152  result.q = this->CoordRotationAdd(_pose.q);
153 
154  return result;
155  }
156 
160  public: const Pose3<T> &operator+=(const Pose3<T> &_pose)
161  {
162  this->p = this->CoordPositionAdd(_pose);
163  this->q = this->CoordRotationAdd(_pose.q);
164 
165  return *this;
166  }
167 
172  public: inline Pose3<T> operator-() const
173  {
174  return Pose3<T>() - *this;
175  }
176 
183  public: inline Pose3<T> operator-(const Pose3<T> &_pose) const
184  {
185  return Pose3<T>(this->CoordPositionSub(_pose),
186  this->CoordRotationSub(_pose.q));
187  }
188 
192  public: const Pose3<T> &operator-=(const Pose3<T> &_pose)
193  {
194  this->p = this->CoordPositionSub(_pose);
195  this->q = this->CoordRotationSub(_pose.q);
196 
197  return *this;
198  }
199 
203  public: bool operator==(const Pose3<T> &_pose) const
204  {
205  return this->p == _pose.p && this->q == _pose.q;
206  }
207 
211  public: bool operator!=(const Pose3<T> &_pose) const
212  {
213  return this->p != _pose.p || this->q != _pose.q;
214  }
215 
219  public: Pose3<T> operator*(const Pose3<T> &_pose) const
220  {
221  return Pose3<T>(this->CoordPositionAdd(_pose), _pose.q * this->q);
222  }
223 
226  public: Pose3<T> &operator=(const Pose3<T> &_pose)
227  {
228  this->p = _pose.p;
229  this->q = _pose.q;
230  return *this;
231  }
232 
236  public: Vector3<T> CoordPositionAdd(const Vector3<T> &_pos) const
237  {
238  Quaternion<T> tmp(0.0, _pos.X(), _pos.Y(), _pos.Z());
239 
240  // result = pose.q + pose.q * this->p * pose.q!
241  tmp = this->q * (tmp * this->q.Inverse());
242 
243  return Vector3<T>(this->p.X() + tmp.X(),
244  this->p.Y() + tmp.Y(),
245  this->p.Z() + tmp.Z());
246  }
247 
251  public: Vector3<T> CoordPositionAdd(const Pose3<T> &_pose) const
252  {
253  Quaternion<T> tmp(static_cast<T>(0),
254  this->p.X(), this->p.Y(), this->p.Z());
255 
256  // result = _pose.q + _pose.q * this->p * _pose.q!
257  tmp = _pose.q * (tmp * _pose.q.Inverse());
258 
259  return Vector3<T>(_pose.p.X() + tmp.X(),
260  _pose.p.Y() + tmp.Y(),
261  _pose.p.Z() + tmp.Z());
262  }
263 
267  public: inline Vector3<T> CoordPositionSub(const Pose3<T> &_pose) const
268  {
269  Quaternion<T> tmp(0,
270  this->p.X() - _pose.p.X(),
271  this->p.Y() - _pose.p.Y(),
272  this->p.Z() - _pose.p.Z());
273 
274  tmp = _pose.q.Inverse() * (tmp * _pose.q);
275  return Vector3<T>(tmp.X(), tmp.Y(), tmp.Z());
276  }
277 
281  public: Quaternion<T> CoordRotationAdd(const Quaternion<T> &_rot) const
282  {
283  return Quaternion<T>(_rot * this->q);
284  }
285 
290  const Quaternion<T> &_rot) const
291  {
292  Quaternion<T> result(_rot.Inverse() * this->q);
293  result.Normalize();
294  return result;
295  }
296 
300  public: Pose3<T> CoordPoseSolve(const Pose3<T> &_b) const
301  {
302  Quaternion<T> qt;
303  Pose3<T> a;
304 
305  a.q = this->q.Inverse() * _b.q;
306  qt = a.q * Quaternion<T>(0, this->p.X(), this->p.Y(), this->p.Z());
307  qt = qt * a.q.Inverse();
308  a.p = _b.p - Vector3<T>(qt.X(), qt.Y(), qt.Z());
309 
310  return a;
311  }
312 
314  public: void Reset()
315  {
316  // set the position to zero
317  this->p.Set();
318  this->q = Quaterniond::Identity;
319  }
320 
325  {
326  Pose3<T> a = *this;
327  a.p.X((1.0 - 2.0*_q.Y()*_q.Y() - 2.0*_q.Z()*_q.Z()) * this->p.X()
328  +(2.0*(_q.X()*_q.Y()+_q.W()*_q.Z())) * this->p.Y()
329  +(2.0*(_q.X()*_q.Z()-_q.W()*_q.Y())) * this->p.Z());
330  a.p.Y((2.0*(_q.X()*_q.Y()-_q.W()*_q.Z())) * this->p.X()
331  +(1.0 - 2.0*_q.X()*_q.X() - 2.0*_q.Z()*_q.Z()) * this->p.Y()
332  +(2.0*(_q.Y()*_q.Z()+_q.W()*_q.X())) * this->p.Z());
333  a.p.Z((2.0*(_q.X()*_q.Z()+_q.W()*_q.Y())) * this->p.X()
334  +(2.0*(_q.Y()*_q.Z()-_q.W()*_q.X())) * this->p.Y()
335  +(1.0 - 2.0*_q.X()*_q.X() - 2.0*_q.Y()*_q.Y()) * this->p.Z());
336  return a;
337  }
338 
341  public: void Round(int _precision)
342  {
343  this->q.Round(_precision);
344  this->p.Round(_precision);
345  }
346 
349  public: inline const Vector3<T> &Pos() const
350  {
351  return this->p;
352  }
353 
356  public: inline Vector3<T> &Pos()
357  {
358  return this->p;
359  }
360 
363  public: inline const Quaternion<T> &Rot() const
364  {
365  return this->q;
366  }
367 
370  public: inline Quaternion<T> &Rot()
371  {
372  return this->q;
373  }
374 
379  public: friend std::ostream &operator<<(
380  std::ostream &_out, const ignition::math::Pose3<T> &_pose)
381  {
382  _out << _pose.Pos() << " " << _pose.Rot();
383  return _out;
384  }
385 
390  public: friend std::istream &operator>>(
392  {
393  // Skip white spaces
394  _in.setf(std::ios_base::skipws);
395  Vector3<T> pos;
396  Quaternion<T> rot;
397  _in >> pos >> rot;
398  _pose.Set(pos, rot);
399  return _in;
400  }
401 
403  private: Vector3<T> p;
404 
406  private: Quaternion<T> q;
407  };
408  template<typename T> const Pose3<T> Pose3<T>::Zero(0, 0, 0, 0, 0, 0);
409 
413  }
414  }
415 }
416 #endif
friend std::istream & operator>>(std::istream &_in, Pose3< T > &_pose)
Stream extraction operator.
Definition: Pose3.hh:390
Pose3< T > CoordPoseSolve(const Pose3< T > &_b) const
Find the inverse of a pose; i.e., if b = this + a, given b and this, find a.
Definition: Pose3.hh:300
T setf(T... args)
Pose3< T > & operator=(const Pose3< T > &_pose)
Equal operator.
Definition: Pose3.hh:226
const T & W() const
Get the w component.
Definition: Quaternion.hh:949
Quaternion< T > & Rot()
Get a mutuable reference to the rotation.
Definition: Pose3.hh:370
bool IsFinite() const
See if a pose is finite (e.g., not nan)
Definition: Pose3.hh:121
const T & Z() const
Get the z component.
Definition: Quaternion.hh:970
void Reset()
Reset the pose.
Definition: Pose3.hh:314
Pose3< T > operator-() const
Negation operator A is the transform from O to P in frame O then -A is transform from P to O specifie...
Definition: Pose3.hh:172
bool operator!=(const Pose3< T > &_pose) const
Inequality operator.
Definition: Pose3.hh:211
void Normalize()
Normalize the quaternion.
Definition: Quaternion.hh:224
Pose3(T _x, T _y, T _z, T _roll, T _pitch, T _yaw)
Constructor.
Definition: Pose3.hh:59
Quaternion< T > CoordRotationSub(const Quaternion< T > &_rot) const
Subtract one rotation from another: result = this->q - rot.
Definition: Pose3.hh:289
void Round(int _precision)
Round all values to _precision decimal places.
Definition: Pose3.hh:341
Encapsulates a position and rotation in three space.
Definition: Pose3.hh:34
void Correct()
Fix any nan values.
Definition: Pose3.hh:127
const Pose3< T > & operator+=(const Pose3< T > &_pose)
Add-Equals operator.
Definition: Pose3.hh:160
const T & X() const
Get the x component.
Definition: Quaternion.hh:956
Pose3< T > operator-(const Pose3< T > &_pose) const
Subtraction operator A is the transform from O to P in frame O B is the transform from O to Q in fram...
Definition: Pose3.hh:183
static const Pose3< T > Zero
math::Pose3<T>(0, 0, 0, 0, 0, 0)
Definition: Pose3.hh:37
Pose3(const Vector3< T > &_pos, const Quaternion< T > &_rot)
Constructor.
Definition: Pose3.hh:47
const T & Y() const
Get the y component.
Definition: Quaternion.hh:963
STL class.
T X() const
Get the x value.
Definition: Vector3.hh:648
T Z() const
Get the z value.
Definition: Vector3.hh:662
Pose3< T > Inverse() const
Get the inverse of this pose.
Definition: Pose3.hh:135
Pose3< T > operator+(const Pose3< T > &_pose) const
Addition operator A is the transform from O to P specified in frame O B is the transform from P to Q ...
Definition: Pose3.hh:147
T Y() const
Get the y value.
Definition: Vector3.hh:655
void Set(const Vector3< T > &_pos, const Quaternion< T > &_rot)
Set the pose from a Vector3 and a Quaternion<T>
Definition: Pose3.hh:92
Vector3< T > CoordPositionAdd(const Vector3< T > &_pos) const
Add one point to a vector: result = this + pos.
Definition: Pose3.hh:236
void Set(T _x, T _y, T _z, T _roll, T _pitch, T _yaw)
Set the pose from a six tuple.
Definition: Pose3.hh:114
const Pose3< T > & operator-=(const Pose3< T > &_pose)
Subtraction operator.
Definition: Pose3.hh:192
Pose3()
Default constructors.
Definition: Pose3.hh:40
Quaternion< T > CoordRotationAdd(const Quaternion< T > &_rot) const
Add one rotation to another: result = this->q + rot.
Definition: Pose3.hh:281
Pose3< double > Pose3d
Definition: Pose3.hh:411
Pose3(T _x, T _y, T _z, T _qw, T _qx, T _qy, T _qz)
Constructor.
Definition: Pose3.hh:72
Pose3< int > Pose3i
Definition: Pose3.hh:410
static const Quaternion Identity
math::Quaternion(1, 0, 0, 0)
Definition: Quaternion.hh:41
Vector3< T > & Pos()
Get a mutable reference to the position.
Definition: Pose3.hh:356
const Quaternion< T > & Rot() const
Get the rotation.
Definition: Pose3.hh:363
friend std::ostream & operator<<(std::ostream &_out, const Pose3< T > &_pose)
Stream insertion operator.
Definition: Pose3.hh:379
Pose3< float > Pose3f
Definition: Pose3.hh:412
Pose3< T > operator*(const Pose3< T > &_pose) const
Multiplication operator.
Definition: Pose3.hh:219
The Vector3 class represents the generic vector containing 3 elements. Since it&#39;s commonly used to ke...
Definition: Vector3.hh:40
Vector3< T > CoordPositionAdd(const Pose3< T > &_pose) const
Add one point to another: result = this + pose.
Definition: Pose3.hh:251
const Vector3< T > & Pos() const
Get the position.
Definition: Pose3.hh:349
Pose3(const Pose3< T > &_pose)
Copy constructor.
Definition: Pose3.hh:79
Vector3< T > CoordPositionSub(const Pose3< T > &_pose) const
Subtract one position from another: result = this - pose.
Definition: Pose3.hh:267
bool operator==(const Pose3< T > &_pose) const
Equality operator.
Definition: Pose3.hh:203
virtual ~Pose3()
Destructor.
Definition: Pose3.hh:85
void Set(const Vector3< T > &_pos, const Vector3< T > &_rpy)
Set the pose from pos and rpy vectors.
Definition: Pose3.hh:101
Definition: Angle.hh:39
A quaternion class.
Definition: Matrix3.hh:34
Quaternion< T > Inverse() const
Get the inverse of this quaternion.
Definition: Quaternion.hh:132
STL class.
Pose3< T > RotatePositionAboutOrigin(const Quaternion< T > &_q) const
Rotate vector part of a pose about the origin.
Definition: Pose3.hh:324