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 #include <cstdint>
24 #include <sstream>
25 #include <vector>
26 #include <boost/shared_ptr.hpp>
27 
28 #include "sdf/system_util.hh"
29 
30 #if defined(__GNUC__)
31 #define SDF_DEPRECATED(version) __attribute__((deprecated))
32 #define SDF_FORCEINLINE __attribute__((always_inline))
33 #elif defined(MSVC)
34 #define SDF_DEPRECATED(version)
35 #define SDF_FORCEINLINE __forceinline
36 #else
37 #define SDF_DEPRECATED(version)
38 #define SDF_FORCEINLINE
39 #endif
40 
41 namespace sdf
42 {
48  const char *winGetEnv(const char *_name);
49 
54  template<typename T>
55  inline bool equal(const T &_a, const T &_b,
56  const T &_epsilon = 1e-6)
57  {
58  return std::fabs(_a - _b) <= _epsilon;
59  }
60 
63  {
69  public: Color(float _r = 0.0f, float _g = 0.0f,
70  float _b = 0.0f, float _a = 1.0f)
71  : r(_r), g(_g), b(_b), a(_a) {}
72 
77  public: friend std::ostream &operator<< (std::ostream &_out,
78  const Color &_pt)
79  {
80  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
81  return _out;
82  }
83 
87  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
88  {
89  // Skip white spaces
90  _in.setf(std::ios_base::skipws);
91  _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
92  return _in;
93  }
94 
98  public: bool operator ==(const Color &_clr) const
99  {
100  return equal(this->r, _clr.r) &&
101  equal(this->g, _clr.g) &&
102  equal(this->b, _clr.b) &&
103  equal(this->a, _clr.a);
104  }
105 
107  public: float r;
108 
110  public: float g;
111 
113  public: float b;
114 
116  public: float a;
117  };
118 
122  {
126  public: Vector2i(int _x = 0, int _y = 0)
127  : x(_x), y(_y) {}
128 
133  public: friend std::ostream &operator<<(std::ostream &_out,
134  const Vector2i &_pt)
135  {
136  _out << _pt.x << " " << _pt.y;
137  return _out;
138  }
139 
144  public: friend std::istream &operator>>(std::istream &_in,
145  Vector2i &_pt)
146  {
147  // Skip white spaces
148  _in.setf(std::ios_base::skipws);
149  _in >> _pt.x >> _pt.y;
150  return _in;
151  }
152 
156  public: bool operator ==(const Vector2i &_pt) const
157  {
158  return this->x == _pt.x && this->y == _pt.y;
159  }
160 
162  public: int x;
163 
165  public: int y;
166  } SDF_DEPRECATED(4.0);
167 
171  {
175  public: Vector2d(double _x = 0.0, double _y = 0.0)
176  : x(_x), y(_y) {}
177 
182  public: friend std::ostream &operator<<(std::ostream &_out,
183  const Vector2d &_pt)
184  {
185  _out << _pt.x << " " << _pt.y;
186  return _out;
187  }
188 
193  public: friend std::istream &operator>>(std::istream &_in,
194  Vector2d &_pt)
195  {
196  // Skip white spaces
197  _in.setf(std::ios_base::skipws);
198  _in >> _pt.x >> _pt.y;
199  return _in;
200  }
201 
206  public: bool operator ==(const Vector2d &_pt) const
207  {
208  return equal(this->x, _pt.x) && equal(this->y, _pt.y);
209  }
210 
211 
213  public: double x;
214 
216  public: double y;
217  } SDF_DEPRECATED(4.0);
218 
224  {
227  public: Vector3(const Vector3 &_v)
228  : x(_v.x), y(_v.y), z(_v.z) {}
229 
234  public: Vector3(double _x = 0.0, double _y = 0.0, double _z = 0.0)
235  : x(_x), y(_y), z(_z) {}
236 
240  public: Vector3 operator+(const Vector3 &_v) const
241  {
242  return Vector3(this->x + _v.x, this->y + _v.y, this->z + _v.z);
243  }
244 
247  public: Vector3 Cross(const Vector3 &_pt) const
248  {
249  Vector3 c(0, 0, 0);
250 
251  c.x = this->y * _pt.z - this->z * _pt.y;
252  c.y = this->z * _pt.x - this->x * _pt.z;
253  c.z = this->x * _pt.y - this->y * _pt.x;
254 
255  return c;
256  }
257 
261  public: Vector3 operator*(double _v) const
262  {
263  return Vector3(this->x * _v, this->y * _v, this->z * _v);
264  }
265 
270  public: friend std::ostream &operator<<(std::ostream &_out,
271  const Vector3 &_pt)
272  {
273  _out << _pt.x << " " << _pt.y << " " << _pt.z;
274  return _out;
275  }
276 
280  public: const Vector3 &operator*=(double _v)
281  {
282  this->x *= _v;
283  this->y *= _v;
284  this->z *= _v;
285 
286  return *this;
287  }
288 
293  public: bool operator==(const sdf::Vector3 &_pt) const
294  {
295  return equal(this->x, _pt.x, 0.001) &&
296  equal(this->y, _pt.y, 0.001) &&
297  equal(this->z, _pt.z, 0.001);
298  }
299 
304  public: friend std::istream &operator>>(std::istream &_in,
305  Vector3 &_pt)
306  {
307  // Skip white spaces
308  _in.setf(std::ios_base::skipws);
309  _in >> _pt.x >> _pt.y >> _pt.z;
310  return _in;
311  }
312 
314  public: double x;
315 
317  public: double y;
318 
320  public: double z;
321  } SDF_DEPRECATED(4.0);
322 
326  {
328  public: Quaternion() : x(0), y(0), z(0), w(1)
329  {}
330 
333  public: Quaternion(const Quaternion &_q)
334  : x(_q.x), y(_q.y), z(_q.z), w(_q.w) {}
335 
336  public: Quaternion(const double &_roll, const double &_pitch,
337  const double &_yaw)
338  {
339  this->SetFromEuler(Vector3(_roll, _pitch, _yaw));
340  }
341 
347  public: Quaternion(double _w, double _x, double _y, double _z)
348  : x(_x), y(_y), z(_z), w(_w) {}
349 
354  public: static Quaternion EulerToQuaternion(double _x, double _y, double _z)
355  {
356  return EulerToQuaternion(Vector3(_x, _y, _z));
357  }
358 
361  public: static Quaternion EulerToQuaternion(const Vector3 &_vec)
362  {
363  Quaternion result;
364  result.SetFromEuler(_vec);
365  return result;
366  }
367 
370  public: Quaternion &operator =(const Quaternion &_qt)
371  {
372  this->w = _qt.w;
373  this->x = _qt.x;
374  this->y = _qt.y;
375  this->z = _qt.z;
376 
377  return *this;
378  }
379 
383  public: inline Quaternion operator*(const Quaternion &_q) const
384  {
385  return Quaternion(
386  this->w*_q.w - this->x*_q.x - this->y*_q.y - this->z*_q.z,
387  this->w*_q.x + this->x*_q.w + this->y*_q.z - this->z*_q.y,
388  this->w*_q.y - this->x*_q.z + this->y*_q.w + this->z*_q.x,
389  this->w*_q.z + this->x*_q.y - this->y*_q.x + this->z*_q.w);
390  }
391 
394  public: inline Quaternion GetInverse() const
395  {
396  double s = 0;
397  Quaternion q(this->w, this->x, this->y, this->z);
398 
399  // use s to test if quaternion is valid
400  s = q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z;
401 
402  if (equal(s, 0.0))
403  {
404  q.w = 1.0;
405  q.x = 0.0;
406  q.y = 0.0;
407  q.z = 0.0;
408  }
409  else
410  {
411  // deal with non-normalized quaternion
412  // div by s so q * qinv = identity
413  q.w = q.w / s;
414  q.x = -q.x / s;
415  q.y = -q.y / s;
416  q.z = -q.z / s;
417  }
418  return q;
419  }
420 
423  public: Vector3 GetAsEuler() const
424  {
425  Vector3 vec;
426 
427  Quaternion copy = *this;
428  double squ;
429  double sqx;
430  double sqy;
431  double sqz;
432 
433  copy.Normalize();
434 
435  squ = copy.w * copy.w;
436  sqx = copy.x * copy.x;
437  sqy = copy.y * copy.y;
438  sqz = copy.z * copy.z;
439 
440  // Roll
441  vec.x = atan2(2 * (copy.y*copy.z + copy.w*copy.x),
442  squ - sqx - sqy + sqz);
443 
444  // Pitch
445  double sarg = -2 * (copy.x*copy.z - copy.w * copy.y);
446  vec.y = sarg <= -1.0 ? -0.5*M_PI :
447  (sarg >= 1.0 ? 0.5*M_PI : asin(sarg));
448 
449  // Yaw
450  vec.z = atan2(2 * (copy.x*copy.y + copy.w*copy.z),
451  squ + sqx - sqy - sqz);
452 
453  return vec;
454  }
455 
458  public: void SetFromEuler(const Vector3 &_vec)
459  {
460  double phi, the, psi;
461 
462  phi = _vec.x / 2.0;
463  the = _vec.y / 2.0;
464  psi = _vec.z / 2.0;
465 
466  this->w = cos(phi) * cos(the) * cos(psi) + sin(phi) *
467  sin(the) * sin(psi);
468  this->x = sin(phi) * cos(the) * cos(psi) - cos(phi) *
469  sin(the) * sin(psi);
470  this->y = cos(phi) * sin(the) * cos(psi) + sin(phi) *
471  cos(the) * sin(psi);
472  this->z = cos(phi) * cos(the) * sin(psi) - sin(phi) *
473  sin(the) * cos(psi);
474 
475  this->Normalize();
476  }
477 
479  public: void Normalize()
480  {
481  double s = 0;
482 
483  s = sqrt(this->w * this->w + this->x * this->x +
484  this->y * this->y +
485  this->z * this->z);
486 
487  if (equal(s, 0.0))
488  {
489  this->w = 1.0;
490  this->x = 0.0;
491  this->y = 0.0;
492  this->z = 0.0;
493  }
494  else
495  {
496  this->w /= s;
497  this->x /= s;
498  this->y /= s;
499  this->z /= s;
500  }
501  }
502 
506  public: inline Vector3 RotateVector(const Vector3 &_vec) const
507  {
508  Quaternion tmp(0.0, _vec.x, _vec.y, _vec.z);
509  tmp = (*this) * (tmp * this->GetInverse());
510  return Vector3(tmp.x, tmp.y, tmp.z);
511  }
512 
517  public: friend std::ostream &operator<<(std::ostream &_out,
518  const Quaternion &_q)
519  {
520  Vector3 v(_q.GetAsEuler());
521  _out << v.x << " " << v.y << " " << v.z;
522  return _out;
523  }
524 
527  public: Vector3 operator*(const Vector3 &_v) const
528  {
529  Vector3 uv, uuv;
530  Vector3 qvec(this->x, this->y, this->z);
531  uv = qvec.Cross(_v);
532  uuv = qvec.Cross(uv);
533  uv *= (2.0f * this->w);
534  uuv *= 2.0f;
535 
536  return _v + uv + uuv;
537  }
538 
543  public: friend std::istream &operator>>(std::istream &_in,
544  Quaternion &_q)
545  {
546  double roll, pitch, yaw;
547 
548  // Skip white spaces
549  _in.setf(std::ios_base::skipws);
550  _in >> roll >> pitch >> yaw;
551 
552  _q.SetFromEuler(Vector3(roll, pitch, yaw));
553 
554  return _in;
555  }
556 
560  public: bool operator ==(const Quaternion &_qt) const
561  {
562  return this->GetAsEuler() == _qt.GetAsEuler();
563  }
564 
565  public: inline void Correct()
566  {
567  if (!std::isfinite(this->x))
568  this->x = 0;
569  if (!std::isfinite(this->y))
570  this->y = 0;
571  if (!std::isfinite(this->z))
572  this->z = 0;
573  if (!std::isfinite(this->w))
574  this->w = 1;
575 
576  if (equal(this->w, 0.0) && equal(this->x, 0.0) &&
577  equal(this->y, 0.0) && equal(this->z, 0.0))
578  {
579  this->w = 1;
580  }
581  }
582 
584  public: double x;
585 
587  public: double y;
588 
590  public: double z;
591 
593  public: double w;
594  } SDF_DEPRECATED(4.0);
595 
599  {
601  public: Pose()
602  :pos(0, 0, 0), rot(1, 0, 0, 0)
603  {}
604 
608  public: Pose(Vector3 _pos, Quaternion _rot)
609  : pos(_pos), rot(_rot) {}
610 
618  public: Pose(double _x, double _y, double _z,
619  double _roll, double _pitch, double _yaw)
620  : pos(_x, _y, _z), rot(_roll, _pitch, _yaw)
621  {
622  rot.Correct();
623  }
624 
629  public: friend std::ostream &operator<<(std::ostream &_out,
630  const Pose &_pose)
631  {
632  _out << _pose.pos << " " << _pose.rot;
633  return _out;
634  }
635 
640  public: friend std::istream &operator>>(std::istream &_in,
641  Pose &_pose)
642  {
643  // Skip white spaces
644  _in.setf(std::ios_base::skipws);
645  _in >> _pose.pos >> _pose.rot;
646  return _in;
647  }
648 
652  public: Pose operator*(const Pose &pose)
653  {
654  return Pose(this->CoordPositionAdd(pose), pose.rot * this->rot);
655  }
656 
660  public: Vector3 CoordPositionAdd(const Pose &_pose) const
661  {
662  Quaternion tmp;
663  Vector3 result;
664 
665  // result = _pose.rot + _pose.rot * this->pos * _pose.rot!
666  tmp.w = 0.0;
667  tmp.x = this->pos.x;
668  tmp.y = this->pos.y;
669  tmp.z = this->pos.z;
670 
671  tmp = _pose.rot * (tmp * _pose.rot.GetInverse());
672 
673  result.x = _pose.pos.x + tmp.x;
674  result.y = _pose.pos.y + tmp.y;
675  result.z = _pose.pos.z + tmp.z;
676 
677  return result;
678  }
679 
683  public: bool operator ==(const Pose &_pose) const
684  {
685  return this->pos == _pose.pos && this->rot == _pose.rot;
686  }
687 
689  public: Vector3 pos;
690 
692  public: Quaternion rot;
693  } SDF_DEPRECATED(4.0);
694 
698  {
700  public: Time()
701  : sec(0), nsec(0)
702  {
703  }
704 
708  public: Time(int32_t _sec, int32_t _nsec)
709  : sec(_sec), nsec(_nsec)
710  {
711  }
712 
717  public: friend std::ostream &operator<<(std::ostream &_out,
718  const Time &_time)
719  {
720  _out << _time.sec << " " << _time.nsec;
721  return _out;
722  }
723 
728  public: friend std::istream &operator>>(std::istream &_in,
729  Time &_time)
730  {
731  // Skip white spaces
732  _in.setf(std::ios_base::skipws);
733  _in >> _time.sec >> _time.nsec;
734  return _in;
735  }
736 
740  public: bool operator ==(const Time &_time) const
741  {
742  return this->sec == _time.sec && this->nsec == _time.nsec;
743  }
744 
746  public: int32_t sec;
747 
749  public: int32_t nsec;
750  };
751 
754  {
755  public: double mass;
756  };
757 }
758 #endif
Generic double x, y vector.
Definition: Types.hh:170
Quaternion operator*(const Quaternion &_q) const
Multiplication operator.
Definition: Types.hh:383
friend std::istream & operator>>(std::istream &_in, Pose &_pose)
Stream extraction operator.
Definition: Types.hh:640
friend std::ostream & operator<<(std::ostream &_out, const Quaternion &_q)
Stream insertion operator.
Definition: Types.hh:517
bool operator==(const sdf::Vector3 &_pt) const
Equal to operator.
Definition: Types.hh:293
friend std::istream & operator>>(std::istream &_in, Quaternion &_q)
Stream extraction operator.
Definition: Types.hh:543
Vector3 RotateVector(const Vector3 &_vec) const
Rotate a vector using the quaternion.
Definition: Types.hh:506
Quaternion()
Default Constructor.
Definition: Types.hh:328
Quaternion(double _w, double _x, double _y, double _z)
Constructor.
Definition: Types.hh:347
double z
z data
Definition: Types.hh:590
float b
Blue value.
Definition: Types.hh:113
int32_t sec
Seconds.
Definition: Types.hh:746
double y
y Data
Definition: Types.hh:317
friend std::istream & operator>>(std::istream &_in, Vector3 &_pt)
Stream extraction operator.
Definition: Types.hh:304
void Normalize()
Normalize the quaternion.
Definition: Types.hh:479
Vector2i(int _x=0, int _y=0)
Constructor.
Definition: Types.hh:126
int x
x data
Definition: Types.hh:162
Vector3 operator*(double _v) const
Multiplication operator.
Definition: Types.hh:261
Vector3 GetAsEuler() const
Return the rotation in Euler angles.
Definition: Types.hh:423
double x
x Data
Definition: Types.hh:314
A quaternion class.
Definition: Types.hh:325
double x
x data
Definition: Types.hh:584
friend std::istream & operator>>(std::istream &_in, Vector2d &_pt)
Stream extraction operator.
Definition: Types.hh:193
double z
z Data
Definition: Types.hh:320
Vector3 pos
Position data.
Definition: Types.hh:689
double x
x data
Definition: Types.hh:213
static Quaternion EulerToQuaternion(const Vector3 &_vec)
Convert euler angles to quatern.
Definition: Types.hh:361
Generic integer x, y vector.
Definition: Types.hh:121
Pose(Vector3 _pos, Quaternion _rot)
Constructor.
Definition: Types.hh:608
double mass
Definition: Types.hh:755
Vector3(double _x=0.0, double _y=0.0, double _z=0.0)
Constructor.
Definition: Types.hh:234
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:55
Pose()
Constructor.
Definition: Types.hh:601
Vector3 CoordPositionAdd(const Pose &_pose) const
Add one point to another: result = this + pose.
Definition: Types.hh:660
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
Pose(double _x, double _y, double _z, double _roll, double _pitch, double _yaw)
Constructor.
Definition: Types.hh:618
Time()
Constructor.
Definition: Types.hh:700
Vector3 operator*(const Vector3 &_v) const
Vector3 multiplication operator.
Definition: Types.hh:527
SDFORMAT_VISIBLE const char * winGetEnv(const char *_name)
Windows equivalent of getEnv.
Vector3(const Vector3 &_v)
Copy constructor.
Definition: Types.hh:227
int y
y data
Definition: Types.hh:165
const Vector3 & operator*=(double _v)
Multiplication by a double.
Definition: Types.hh:280
Time(int32_t _sec, int32_t _nsec)
Constructor.
Definition: Types.hh:708
friend std::istream & operator>>(std::istream &_in, Vector2i &_pt)
Stream extraction operator.
Definition: Types.hh:144
Defines a color.
Definition: Types.hh:62
Quaternion rot
Orientation data.
Definition: Types.hh:692
float r
Red value.
Definition: Types.hh:107
double y
y data
Definition: Types.hh:587
double w
w data
Definition: Types.hh:593
friend std::ostream & operator<<(std::ostream &_out, const Vector3 &_pt)
Stream insertion operator.
Definition: Types.hh:270
A class for inertial information about a link.
Definition: Types.hh:753
void Correct()
Definition: Types.hh:565
Quaternion(const Quaternion &_q)
Copy constructor.
Definition: Types.hh:333
Quaternion(const double &_roll, const double &_pitch, const double &_yaw)
Definition: Types.hh:336
Vector3 Cross(const Vector3 &_pt) const
Return the cross product of this vector and pt.
Definition: Types.hh:247
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:697
friend std::ostream & operator<<(std::ostream &_out, const Time &_time)
Stream insertion operator.
Definition: Types.hh:717
friend std::istream & operator>>(std::istream &_in, Time &_time)
Stream extraction operator.
Definition: Types.hh:728
friend std::ostream & operator<<(std::ostream &_out, const Vector2d &_pt)
Stream extraction operator.
Definition: Types.hh:182
void SetFromEuler(const Vector3 &_vec)
Set the quaternion from Euler angles.
Definition: Types.hh:458
Encapsulates a position and rotation in three space.
Definition: Types.hh:598
#define SDF_DEPRECATED(version)
Definition: Types.hh:37
friend std::ostream & operator<<(std::ostream &_out, const Vector2i &_pt)
Stream insertion operator.
Definition: Types.hh:133
Color(float _r=0.0f, float _g=0.0f, float _b=0.0f, float _a=1.0f)
Constructor.
Definition: Types.hh:69
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: Types.hh:223
friend std::ostream & operator<<(std::ostream &_out, const Pose &_pose)
Stream insertion operator.
Definition: Types.hh:629
static Quaternion EulerToQuaternion(double _x, double _y, double _z)
Convert euler angles to quatern.
Definition: Types.hh:354
float g
Green value.
Definition: Types.hh:110
Pose operator*(const Pose &pose)
Multiplication operator.
Definition: Types.hh:652
Quaternion GetInverse() const
Get the inverse of this quaternion.
Definition: Types.hh:394
double y
y data
Definition: Types.hh:216
Vector2d(double _x=0.0, double _y=0.0)
Constructor.
Definition: Types.hh:175
float a
Alpha value.
Definition: Types.hh:116
Vector3 operator+(const Vector3 &_v) const
Addition operator.
Definition: Types.hh:240
int32_t nsec
Nanoseconds.
Definition: Types.hh:749