All Classes Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
Types.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Nate Koenig
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 _SDF_TYPES_HH_
19 #define _SDF_TYPES_HH_
20 
21 #include <algorithm>
22 #include <cmath>
23 
24 #if defined(__GNUC__)
25 #define SDF_DEPRECATED(version) __attribute__((deprecated))
26 #define SDF_FORCEINLINE __attribute__((always_inline))
27 #elif defined(MSVC)
28 #define SDF_DEPRECATED(version)
29 #define SDF_FORCEINLINE __forceinline
30 #else
31 #define SDF_DEPRECATED(version)
32 #define SDF_FORCEINLINE
33 #endif
34 
35 namespace sdf
36 {
41  template<typename T>
42  inline bool equal(const T &_a, const T &_b,
43  const T &_epsilon = 1e-6)
44  {
45  return std::fabs(_a - _b) <= _epsilon;
46  }
47 
49  class Color
50  {
56  public: Color(double _r = 0.0, double _g = 0.0,
57  double _b = 0.0, double _a = 1.0)
58  : r(_r), g(_g), b(_b), a(_a) {}
59 
64  public: friend std::ostream &operator<< (std::ostream &_out,
65  const Color &_pt)
66  {
67  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
68  return _out;
69  }
70 
74  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
75  {
76  // Skip white spaces
77  _in.setf(std::ios_base::skipws);
78  _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
79  return _in;
80  }
81 
85  public: bool operator ==(const Color &_clr) const
86  {
87  return equal(this->r, _clr.r) &&
88  equal(this->g, _clr.g) &&
89  equal(this->b, _clr.b) &&
90  equal(this->a, _clr.a);
91  }
92 
94  public: float r;
95 
97  public: float g;
98 
100  public: float b;
101 
103  public: float a;
104  };
105 
107  class Vector2i
108  {
112  public: Vector2i(int _x = 0, int _y = 0)
113  : x(_x), y(_y) {}
114 
119  public: friend std::ostream &operator<<(std::ostream &_out,
120  const Vector2i &_pt)
121  {
122  _out << _pt.x << " " << _pt.y;
123  return _out;
124  }
125 
130  public: friend std::istream &operator>>(std::istream &_in,
131  Vector2i &_pt)
132  {
133  // Skip white spaces
134  _in.setf(std::ios_base::skipws);
135  _in >> _pt.x >> _pt.y;
136  return _in;
137  }
138 
142  public: bool operator ==(const Vector2i &_pt) const
143  {
144  return this->x == _pt.x && this->y == _pt.y;
145  }
146 
148  public: int x;
149 
151  public: int y;
152  };
153 
155  class Vector2d
156  {
160  public: Vector2d(double _x = 0.0, double _y = 0.0)
161  : x(_x), y(_y) {}
162 
167  public: friend std::ostream &operator<<(std::ostream &_out,
168  const Vector2d &_pt)
169  {
170  _out << _pt.x << " " << _pt.y;
171  return _out;
172  }
173 
178  public: friend std::istream &operator>>(std::istream &_in,
179  Vector2d &_pt)
180  {
181  // Skip white spaces
182  _in.setf(std::ios_base::skipws);
183  _in >> _pt.x >> _pt.y;
184  return _in;
185  }
186 
191  public: bool operator ==(const Vector2d &_pt) const
192  {
193  return equal(this->x, _pt.x) && equal(this->y, _pt.y);
194  }
195 
196 
198  public: double x;
199 
201  public: double y;
202  };
203 
207  class Vector3
208  {
211  public: Vector3(const Vector3 &_v)
212  : x(_v.x), y(_v.y), z(_v.z) {}
213 
218  public: Vector3(double _x = 0.0, double _y = 0.0, double _z = 0.0)
219  : x(_x), y(_y), z(_z) {}
220 
224  public: Vector3 operator+(const Vector3 &_v) const
225  {
226  return Vector3(this->x + _v.x, this->y + _v.y, this->z + _v.z);
227  }
228 
231  public: Vector3 Cross(const Vector3 &_pt) const
232  {
233  Vector3 c(0, 0, 0);
234 
235  c.x = this->y * _pt.z - this->z * _pt.y;
236  c.y = this->z * _pt.x - this->x * _pt.z;
237  c.z = this->x * _pt.y - this->y * _pt.x;
238 
239  return c;
240  }
241 
245  public: Vector3 operator*(double _v) const
246  {
247  return Vector3(this->x * _v, this->y * _v, this->z * _v);
248  }
249 
254  public: friend std::ostream &operator<<(std::ostream &_out,
255  const Vector3 &_pt)
256  {
257  _out << _pt.x << " " << _pt.y << " " << _pt.z;
258  return _out;
259  }
260 
264  public: const Vector3 &operator*=(double _v)
265  {
266  this->x *= _v;
267  this->y *= _v;
268  this->z *= _v;
269 
270  return *this;
271  }
272 
277  public: bool operator==(const sdf::Vector3 &_pt) const
278  {
279  return equal(this->x, _pt.x, 0.001) &&
280  equal(this->y, _pt.y, 0.001) &&
281  equal(this->z, _pt.z, 0.001);
282  }
283 
288  public: friend std::istream &operator>>(std::istream &_in,
289  Vector3 &_pt)
290  {
291  // Skip white spaces
292  _in.setf(std::ios_base::skipws);
293  _in >> _pt.x >> _pt.y >> _pt.z;
294  return _in;
295  }
296 
298  public: double x;
299 
301  public: double y;
302 
304  public: double z;
305  };
306 
309  {
311  public: Quaternion() : x(0), y(0), z(0), w(1)
312  {}
313 
316  public: Quaternion(const Quaternion &_q)
317  : x(_q.x), y(_q.y), z(_q.z), w(_q.w) {}
318 
319  public: Quaternion(const double &_roll, const double &_pitch,
320  const double &_yaw)
321  {
322  this->SetFromEuler(Vector3(_roll, _pitch, _yaw));
323  }
324 
330  public: Quaternion(double _w, double _x, double _y, double _z)
331  : x(_x), y(_y), z(_z), w(_w) {}
332 
337  public: static Quaternion EulerToQuaternion(double _x, double _y, double _z)
338  {
339  return EulerToQuaternion(Vector3(_x, _y, _z));
340  }
341 
344  public: static Quaternion EulerToQuaternion(const Vector3 &_vec)
345  {
346  Quaternion result;
347  result.SetFromEuler(_vec);
348  return result;
349  }
350 
353  public: Quaternion &operator =(const Quaternion &_qt)
354  {
355  this->w = _qt.w;
356  this->x = _qt.x;
357  this->y = _qt.y;
358  this->z = _qt.z;
359 
360  return *this;
361  }
362 
366  public: inline Quaternion operator*(const Quaternion &_q) const
367  {
368  return Quaternion(
369  this->w*_q.w - this->x*_q.x - this->y*_q.y - this->z*_q.z,
370  this->w*_q.x + this->x*_q.w + this->y*_q.z - this->z*_q.y,
371  this->w*_q.y - this->x*_q.z + this->y*_q.w + this->z*_q.x,
372  this->w*_q.z + this->x*_q.y - this->y*_q.x + this->z*_q.w);
373  }
374 
377  public: inline Quaternion GetInverse() const
378  {
379  double s = 0;
380  Quaternion q(this->w, this->x, this->y, this->z);
381 
382  // use s to test if quaternion is valid
383  s = q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z;
384 
385  if (equal(s, 0.0))
386  {
387  q.w = 1.0;
388  q.x = 0.0;
389  q.y = 0.0;
390  q.z = 0.0;
391  }
392  else
393  {
394  // deal with non-normalized quaternion
395  // div by s so q * qinv = identity
396  q.w = q.w / s;
397  q.x = -q.x / s;
398  q.y = -q.y / s;
399  q.z = -q.z / s;
400  }
401  return q;
402  }
403 
406  public: Vector3 GetAsEuler() const
407  {
408  Vector3 vec;
409 
410  Quaternion copy = *this;
411  double squ;
412  double sqx;
413  double sqy;
414  double sqz;
415 
416  copy.Normalize();
417 
418  squ = copy.w * copy.w;
419  sqx = copy.x * copy.x;
420  sqy = copy.y * copy.y;
421  sqz = copy.z * copy.z;
422 
423  // Roll
424  vec.x = atan2(2 * (copy.y*copy.z + copy.w*copy.x),
425  squ - sqx - sqy + sqz);
426 
427  // Pitch
428  double sarg = -2 * (copy.x*copy.z - copy.w * copy.y);
429  vec.y = sarg <= -1.0 ? -0.5*M_PI :
430  (sarg >= 1.0 ? 0.5*M_PI : asin(sarg));
431 
432  // Yaw
433  vec.z = atan2(2 * (copy.x*copy.y + copy.w*copy.z),
434  squ + sqx - sqy - sqz);
435 
436  return vec;
437  }
438 
441  public: void SetFromEuler(const Vector3 &_vec)
442  {
443  double phi, the, psi;
444 
445  phi = _vec.x / 2.0;
446  the = _vec.y / 2.0;
447  psi = _vec.z / 2.0;
448 
449  this->w = cos(phi) * cos(the) * cos(psi) + sin(phi) *
450  sin(the) * sin(psi);
451  this->x = sin(phi) * cos(the) * cos(psi) - cos(phi) *
452  sin(the) * sin(psi);
453  this->y = cos(phi) * sin(the) * cos(psi) + sin(phi) *
454  cos(the) * sin(psi);
455  this->z = cos(phi) * cos(the) * sin(psi) - sin(phi) *
456  sin(the) * cos(psi);
457 
458  this->Normalize();
459  }
460 
462  public: void Normalize()
463  {
464  double s = 0;
465 
466  s = sqrt(this->w * this->w + this->x * this->x +
467  this->y * this->y +
468  this->z * this->z);
469 
470  if (equal(s, 0.0))
471  {
472  this->w = 1.0;
473  this->x = 0.0;
474  this->y = 0.0;
475  this->z = 0.0;
476  }
477  else
478  {
479  this->w /= s;
480  this->x /= s;
481  this->y /= s;
482  this->z /= s;
483  }
484  }
485 
489  public: inline Vector3 RotateVector(const Vector3 &_vec) const
490  {
491  Quaternion tmp(0.0, _vec.x, _vec.y, _vec.z);
492  tmp = (*this) * (tmp * this->GetInverse());
493  return Vector3(tmp.x, tmp.y, tmp.z);
494  }
495 
500  public: friend std::ostream &operator<<(std::ostream &_out,
501  const Quaternion &_q)
502  {
503  Vector3 v(_q.GetAsEuler());
504  _out << v.x << " " << v.y << " " << v.z;
505  return _out;
506  }
507 
510  public: Vector3 operator*(const Vector3 &_v) const
511  {
512  Vector3 uv, uuv;
513  Vector3 qvec(this->x, this->y, this->z);
514  uv = qvec.Cross(_v);
515  uuv = qvec.Cross(uv);
516  uv *= (2.0f * this->w);
517  uuv *= 2.0f;
518 
519  return _v + uv + uuv;
520  }
521 
526  public: friend std::istream &operator>>(std::istream &_in,
527  Quaternion &_q)
528  {
529  double roll, pitch, yaw;
530 
531  // Skip white spaces
532  _in.setf(std::ios_base::skipws);
533  _in >> roll >> pitch >> yaw;
534 
535  _q.SetFromEuler(Vector3(roll, pitch, yaw));
536 
537  return _in;
538  }
539 
543  public: bool operator ==(const Quaternion &_qt) const
544  {
545  return this->GetAsEuler() == _qt.GetAsEuler();
546  }
547 
548  public: inline void Correct()
549  {
550  if (!std::isfinite(this->x))
551  this->x = 0;
552  if (!std::isfinite(this->y))
553  this->y = 0;
554  if (!std::isfinite(this->z))
555  this->z = 0;
556  if (!std::isfinite(this->w))
557  this->w = 1;
558 
559  if (equal(this->w, 0.0) && equal(this->x, 0.0) &&
560  equal(this->y, 0.0) && equal(this->z, 0.0))
561  {
562  this->w = 1;
563  }
564  }
565 
567  public: double x;
568 
570  public: double y;
571 
573  public: double z;
574 
576  public: double w;
577  };
578 
580  class Pose
581  {
583  public: Pose()
584  :pos(0, 0, 0), rot(1, 0, 0, 0)
585  {}
586 
590  public: Pose(Vector3 _pos, Quaternion _rot)
591  : pos(_pos), rot(_rot) {}
592 
600  public: Pose(double _x, double _y, double _z,
601  double _roll, double _pitch, double _yaw)
602  : pos(_x, _y, _z), rot(_roll, _pitch, _yaw)
603  {
604  rot.Correct();
605  }
606 
611  public: friend std::ostream &operator<<(std::ostream &_out,
612  const Pose &_pose)
613  {
614  _out << _pose.pos << " " << _pose.rot;
615  return _out;
616  }
617 
622  public: friend std::istream &operator>>(std::istream &_in,
623  Pose &_pose)
624  {
625  // Skip white spaces
626  _in.setf(std::ios_base::skipws);
627  _in >> _pose.pos >> _pose.rot;
628  return _in;
629  }
630 
634  public: Pose operator*(const Pose &pose)
635  {
636  return Pose(this->CoordPositionAdd(pose), pose.rot * this->rot);
637  }
638 
642  public: Vector3 CoordPositionAdd(const Pose &_pose) const
643  {
644  Quaternion tmp;
645  Vector3 result;
646 
647  // result = _pose.rot + _pose.rot * this->pos * _pose.rot!
648  tmp.w = 0.0;
649  tmp.x = this->pos.x;
650  tmp.y = this->pos.y;
651  tmp.z = this->pos.z;
652 
653  tmp = _pose.rot * (tmp * _pose.rot.GetInverse());
654 
655  result.x = _pose.pos.x + tmp.x;
656  result.y = _pose.pos.y + tmp.y;
657  result.z = _pose.pos.z + tmp.z;
658 
659  return result;
660  }
661 
665  public: bool operator ==(const Pose &_pose) const
666  {
667  return this->pos == _pose.pos && this->rot == _pose.rot;
668  }
669 
671  public: Vector3 pos;
672 
674  public: Quaternion rot;
675  };
676 
679  class Time
680  {
682  public: Time()
683  : sec(0), nsec(0)
684  {
685  }
686 
690  public: Time(int32_t _sec, int32_t _nsec)
691  : sec(_sec), nsec(_nsec)
692  {
693  }
694 
699  public: friend std::ostream &operator<<(std::ostream &_out,
700  const Time &_time)
701  {
702  _out << _time.sec << " " << _time.nsec;
703  return _out;
704  }
705 
710  public: friend std::istream &operator>>(std::istream &_in,
711  Time &_time)
712  {
713  // Skip white spaces
714  _in.setf(std::ios_base::skipws);
715  _in >> _time.sec >> _time.nsec;
716  return _in;
717  }
718 
722  public: bool operator ==(const Time &_time) const
723  {
724  return this->sec == _time.sec && this->nsec == _time.nsec;
725  }
726 
728  public: int32_t sec;
729 
731  public: int32_t nsec;
732  };
733 
735  class Inertia
736  {
737  public: double mass;
738  };
739 }
740 #endif
Generic double x, y vector.
Definition: Types.hh:155
Quaternion operator*(const Quaternion &_q) const
Multiplication operator.
Definition: Types.hh:366
friend std::istream & operator>>(std::istream &_in, Pose &_pose)
Stream extraction operator.
Definition: Types.hh:622
friend std::ostream & operator<<(std::ostream &_out, const Quaternion &_q)
Stream insertion operator.
Definition: Types.hh:500
bool operator==(const sdf::Vector3 &_pt) const
Equal to operator.
Definition: Types.hh:277
bool operator==(const Vector2i &_pt) const
Equality operator.
Definition: Types.hh:142
friend std::istream & operator>>(std::istream &_in, Quaternion &_q)
Stream extraction operator.
Definition: Types.hh:526
Vector3 RotateVector(const Vector3 &_vec) const
Rotate a vector using the quaternion.
Definition: Types.hh:489
Quaternion()
Default Constructor.
Definition: Types.hh:311
Quaternion(double _w, double _x, double _y, double _z)
Constructor.
Definition: Types.hh:330
Quaternion & operator=(const Quaternion &_qt)
Equal operator.
Definition: Types.hh:353
double z
z data
Definition: Types.hh:573
float b
Blue value.
Definition: Types.hh:100
int32_t sec
Seconds.
Definition: Types.hh:728
double y
y Data
Definition: Types.hh:301
friend std::istream & operator>>(std::istream &_in, Vector3 &_pt)
Stream extraction operator.
Definition: Types.hh:288
void Normalize()
Normalize the quaternion.
Definition: Types.hh:462
Vector2i(int _x=0, int _y=0)
Constructor.
Definition: Types.hh:112
int x
x data
Definition: Types.hh:148
Vector3 operator*(double _v) const
Multiplication operator.
Definition: Types.hh:245
Vector3 GetAsEuler() const
Return the rotation in Euler angles.
Definition: Types.hh:406
double x
x Data
Definition: Types.hh:298
bool operator==(const Vector2d &_pt) const
Equal to operator.
Definition: Types.hh:191
A quaternion class.
Definition: Types.hh:308
double x
x data
Definition: Types.hh:567
friend std::istream & operator>>(std::istream &_in, Vector2d &_pt)
Stream extraction operator.
Definition: Types.hh:178
bool operator==(const Pose &_pose) const
Equality operator.
Definition: Types.hh:665
double z
z Data
Definition: Types.hh:304
Vector3 pos
Position data.
Definition: Types.hh:671
double x
x data
Definition: Types.hh:198
static Quaternion EulerToQuaternion(const Vector3 &_vec)
Convert euler angles to quatern.
Definition: Types.hh:344
Generic integer x, y vector.
Definition: Types.hh:107
Pose(Vector3 _pos, Quaternion _rot)
Constructor.
Definition: Types.hh:590
double mass
Definition: Types.hh:737
Vector3(double _x=0.0, double _y=0.0, double _z=0.0)
Constructor.
Definition: Types.hh:218
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Types.hh:42
Pose()
Constructor.
Definition: Types.hh:583
Vector3 CoordPositionAdd(const Pose &_pose) const
Add one point to another: result = this + pose.
Definition: Types.hh:642
Pose(double _x, double _y, double _z, double _roll, double _pitch, double _yaw)
Constructor.
Definition: Types.hh:600
Time()
Constructor.
Definition: Types.hh:682
Vector3 operator*(const Vector3 &_v) const
Vector3 multiplication operator.
Definition: Types.hh:510
bool operator==(const Quaternion &_qt) const
Equal to operator.
Definition: Types.hh:543
Vector3(const Vector3 &_v)
Copy constructor.
Definition: Types.hh:211
int y
y data
Definition: Types.hh:151
const Vector3 & operator*=(double _v)
Multiplication by a double.
Definition: Types.hh:264
Time(int32_t _sec, int32_t _nsec)
Constructor.
Definition: Types.hh:690
friend std::istream & operator>>(std::istream &_in, Vector2i &_pt)
Stream extraction operator.
Definition: Types.hh:130
Defines a color.
Definition: Types.hh:49
Quaternion rot
Orientation data.
Definition: Types.hh:674
float r
Red value.
Definition: Types.hh:94
double y
y data
Definition: Types.hh:570
double w
w data
Definition: Types.hh:576
friend std::ostream & operator<<(std::ostream &_out, const Vector3 &_pt)
Stream insertion operator.
Definition: Types.hh:254
bool operator==(const Time &_time) const
Equal to operator.
Definition: Types.hh:722
bool operator==(const Color &_clr) const
Equality operator.
Definition: Types.hh:85
A class for inertial information about a link.
Definition: Types.hh:735
void Correct()
Definition: Types.hh:548
friend std::ostream & operator<<(std::ostream &_out, const Color &_pt)
Stream insertion operator.
Definition: Types.hh:64
Quaternion(const Quaternion &_q)
Copy constructor.
Definition: Types.hh:316
Quaternion(const double &_roll, const double &_pitch, const double &_yaw)
Definition: Types.hh:319
Vector3 Cross(const Vector3 &_pt) const
Return the cross product of this vector and pt.
Definition: Types.hh:231
Color(double _r=0.0, double _g=0.0, double _b=0.0, double _a=1.0)
Constructor.
Definition: Types.hh:56
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:679
friend std::ostream & operator<<(std::ostream &_out, const Time &_time)
Stream insertion operator.
Definition: Types.hh:699
friend std::istream & operator>>(std::istream &_in, Time &_time)
Stream extraction operator.
Definition: Types.hh:710
friend std::ostream & operator<<(std::ostream &_out, const Vector2d &_pt)
Stream extraction operator.
Definition: Types.hh:167
void SetFromEuler(const Vector3 &_vec)
Set the quaternion from Euler angles.
Definition: Types.hh:441
Encapsulates a position and rotation in three space.
Definition: Types.hh:580
friend std::istream & operator>>(std::istream &_in, Color &_pt)
Stream insertion operator.
Definition: Types.hh:74
friend std::ostream & operator<<(std::ostream &_out, const Vector2i &_pt)
Stream insertion operator.
Definition: Types.hh:119
The Vector3 class represents the generic vector containing 3 elements.
Definition: Types.hh:207
friend std::ostream & operator<<(std::ostream &_out, const Pose &_pose)
Stream insertion operator.
Definition: Types.hh:611
static Quaternion EulerToQuaternion(double _x, double _y, double _z)
Convert euler angles to quatern.
Definition: Types.hh:337
float g
Green value.
Definition: Types.hh:97
Pose operator*(const Pose &pose)
Multiplication operator.
Definition: Types.hh:634
Quaternion GetInverse() const
Get the inverse of this quaternion.
Definition: Types.hh:377
double y
y data
Definition: Types.hh:201
Vector2d(double _x=0.0, double _y=0.0)
Constructor.
Definition: Types.hh:160
float a
Alpha value.
Definition: Types.hh:103
Vector3 operator+(const Vector3 &_v) const
Addition operator.
Definition: Types.hh:224
int32_t nsec
Nanoseconds.
Definition: Types.hh:731