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