All Classes Namespaces Files Functions Variables Typedefs Friends Macros Modules
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 #include "sdf/system_util.hh"
25 
26 #if defined(__GNUC__)
27 #define SDF_DEPRECATED(version) __attribute__((deprecated))
28 #define SDF_FORCEINLINE __attribute__((always_inline))
29 #elif defined(MSVC)
30 #define SDF_DEPRECATED(version)
31 #define SDF_FORCEINLINE __forceinline
32 #else
33 #define SDF_DEPRECATED(version)
34 #define SDF_FORCEINLINE
35 #endif
36 
37 namespace sdf
38 {
43  template<typename T>
44  inline bool equal(const T &_a, const T &_b,
45  const T &_epsilon = 1e-6)
46  {
47  return std::fabs(_a - _b) <= _epsilon;
48  }
49 
52  {
58  public: Color(double _r = 0.0, double _g = 0.0,
59  double _b = 0.0, double _a = 1.0)
60  : r(_r), g(_g), b(_b), a(_a) {}
61 
66  public: friend std::ostream &operator<< (std::ostream &_out,
67  const Color &_pt)
68  {
69  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
70  return _out;
71  }
72 
76  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
77  {
78  // Skip white spaces
79  _in.setf(std::ios_base::skipws);
80  _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
81  return _in;
82  }
83 
87  public: bool operator ==(const Color &_clr) const
88  {
89  return equal(this->r, _clr.r) &&
90  equal(this->g, _clr.g) &&
91  equal(this->b, _clr.b) &&
92  equal(this->a, _clr.a);
93  }
94 
96  public: float r;
97 
99  public: float g;
100 
102  public: float b;
103 
105  public: float a;
106  };
107 
110  {
114  public: Vector2i(int _x = 0, int _y = 0)
115  : x(_x), y(_y) {}
116 
121  public: friend std::ostream &operator<<(std::ostream &_out,
122  const Vector2i &_pt)
123  {
124  _out << _pt.x << " " << _pt.y;
125  return _out;
126  }
127 
132  public: friend std::istream &operator>>(std::istream &_in,
133  Vector2i &_pt)
134  {
135  // Skip white spaces
136  _in.setf(std::ios_base::skipws);
137  _in >> _pt.x >> _pt.y;
138  return _in;
139  }
140 
144  public: bool operator ==(const Vector2i &_pt) const
145  {
146  return this->x == _pt.x && this->y == _pt.y;
147  }
148 
150  public: int x;
151 
153  public: int y;
154  };
155 
158  {
162  public: Vector2d(double _x = 0.0, double _y = 0.0)
163  : x(_x), y(_y) {}
164 
169  public: friend std::ostream &operator<<(std::ostream &_out,
170  const Vector2d &_pt)
171  {
172  _out << _pt.x << " " << _pt.y;
173  return _out;
174  }
175 
180  public: friend std::istream &operator>>(std::istream &_in,
181  Vector2d &_pt)
182  {
183  // Skip white spaces
184  _in.setf(std::ios_base::skipws);
185  _in >> _pt.x >> _pt.y;
186  return _in;
187  }
188 
193  public: bool operator ==(const Vector2d &_pt) const
194  {
195  return equal(this->x, _pt.x) && equal(this->y, _pt.y);
196  }
197 
198 
200  public: double x;
201 
203  public: double y;
204  };
205 
210  {
213  public: Vector3(const Vector3 &_v)
214  : x(_v.x), y(_v.y), z(_v.z) {}
215 
220  public: Vector3(double _x = 0.0, double _y = 0.0, double _z = 0.0)
221  : x(_x), y(_y), z(_z) {}
222 
226  public: Vector3 operator+(const Vector3 &_v) const
227  {
228  return Vector3(this->x + _v.x, this->y + _v.y, this->z + _v.z);
229  }
230 
233  public: Vector3 Cross(const Vector3 &_pt) const
234  {
235  Vector3 c(0, 0, 0);
236 
237  c.x = this->y * _pt.z - this->z * _pt.y;
238  c.y = this->z * _pt.x - this->x * _pt.z;
239  c.z = this->x * _pt.y - this->y * _pt.x;
240 
241  return c;
242  }
243 
247  public: Vector3 operator*(double _v) const
248  {
249  return Vector3(this->x * _v, this->y * _v, this->z * _v);
250  }
251 
256  public: friend std::ostream &operator<<(std::ostream &_out,
257  const Vector3 &_pt)
258  {
259  _out << _pt.x << " " << _pt.y << " " << _pt.z;
260  return _out;
261  }
262 
266  public: const Vector3 &operator*=(double _v)
267  {
268  this->x *= _v;
269  this->y *= _v;
270  this->z *= _v;
271 
272  return *this;
273  }
274 
279  public: bool operator==(const sdf::Vector3 &_pt) const
280  {
281  return equal(this->x, _pt.x, 0.001) &&
282  equal(this->y, _pt.y, 0.001) &&
283  equal(this->z, _pt.z, 0.001);
284  }
285 
290  public: friend std::istream &operator>>(std::istream &_in,
291  Vector3 &_pt)
292  {
293  // Skip white spaces
294  _in.setf(std::ios_base::skipws);
295  _in >> _pt.x >> _pt.y >> _pt.z;
296  return _in;
297  }
298 
300  public: double x;
301 
303  public: double y;
304 
306  public: double z;
307  };
308 
311  {
313  public: Quaternion() : x(0), y(0), z(0), w(1)
314  {}
315 
318  public: Quaternion(const Quaternion &_q)
319  : x(_q.x), y(_q.y), z(_q.z), w(_q.w) {}
320 
321  public: Quaternion(const double &_roll, const double &_pitch,
322  const double &_yaw)
323  {
324  this->SetFromEuler(Vector3(_roll, _pitch, _yaw));
325  }
326 
332  public: Quaternion(double _w, double _x, double _y, double _z)
333  : x(_x), y(_y), z(_z), w(_w) {}
334 
339  public: static Quaternion EulerToQuaternion(double _x, double _y, double _z)
340  {
341  return EulerToQuaternion(Vector3(_x, _y, _z));
342  }
343 
346  public: static Quaternion EulerToQuaternion(const Vector3 &_vec)
347  {
348  Quaternion result;
349  result.SetFromEuler(_vec);
350  return result;
351  }
352 
355  public: Quaternion &operator =(const Quaternion &_qt)
356  {
357  this->w = _qt.w;
358  this->x = _qt.x;
359  this->y = _qt.y;
360  this->z = _qt.z;
361 
362  return *this;
363  }
364 
368  public: inline Quaternion operator*(const Quaternion &_q) const
369  {
370  return Quaternion(
371  this->w*_q.w - this->x*_q.x - this->y*_q.y - this->z*_q.z,
372  this->w*_q.x + this->x*_q.w + this->y*_q.z - this->z*_q.y,
373  this->w*_q.y - this->x*_q.z + this->y*_q.w + this->z*_q.x,
374  this->w*_q.z + this->x*_q.y - this->y*_q.x + this->z*_q.w);
375  }
376 
379  public: inline Quaternion GetInverse() const
380  {
381  double s = 0;
382  Quaternion q(this->w, this->x, this->y, this->z);
383 
384  // use s to test if quaternion is valid
385  s = q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z;
386 
387  if (equal(s, 0.0))
388  {
389  q.w = 1.0;
390  q.x = 0.0;
391  q.y = 0.0;
392  q.z = 0.0;
393  }
394  else
395  {
396  // deal with non-normalized quaternion
397  // div by s so q * qinv = identity
398  q.w = q.w / s;
399  q.x = -q.x / s;
400  q.y = -q.y / s;
401  q.z = -q.z / s;
402  }
403  return q;
404  }
405 
408  public: Vector3 GetAsEuler() const
409  {
410  Vector3 vec;
411 
412  Quaternion copy = *this;
413  double squ;
414  double sqx;
415  double sqy;
416  double sqz;
417 
418  copy.Normalize();
419 
420  squ = copy.w * copy.w;
421  sqx = copy.x * copy.x;
422  sqy = copy.y * copy.y;
423  sqz = copy.z * copy.z;
424 
425  // Roll
426  vec.x = atan2(2 * (copy.y*copy.z + copy.w*copy.x),
427  squ - sqx - sqy + sqz);
428 
429  // Pitch
430  double sarg = -2 * (copy.x*copy.z - copy.w * copy.y);
431  vec.y = sarg <= -1.0 ? -0.5*M_PI :
432  (sarg >= 1.0 ? 0.5*M_PI : asin(sarg));
433 
434  // Yaw
435  vec.z = atan2(2 * (copy.x*copy.y + copy.w*copy.z),
436  squ + sqx - sqy - sqz);
437 
438  return vec;
439  }
440 
443  public: void SetFromEuler(const Vector3 &_vec)
444  {
445  double phi, the, psi;
446 
447  phi = _vec.x / 2.0;
448  the = _vec.y / 2.0;
449  psi = _vec.z / 2.0;
450 
451  this->w = cos(phi) * cos(the) * cos(psi) + sin(phi) *
452  sin(the) * sin(psi);
453  this->x = sin(phi) * cos(the) * cos(psi) - cos(phi) *
454  sin(the) * sin(psi);
455  this->y = cos(phi) * sin(the) * cos(psi) + sin(phi) *
456  cos(the) * sin(psi);
457  this->z = cos(phi) * cos(the) * sin(psi) - sin(phi) *
458  sin(the) * cos(psi);
459 
460  this->Normalize();
461  }
462 
464  public: void Normalize()
465  {
466  double s = 0;
467 
468  s = sqrt(this->w * this->w + this->x * this->x +
469  this->y * this->y +
470  this->z * this->z);
471 
472  if (equal(s, 0.0))
473  {
474  this->w = 1.0;
475  this->x = 0.0;
476  this->y = 0.0;
477  this->z = 0.0;
478  }
479  else
480  {
481  this->w /= s;
482  this->x /= s;
483  this->y /= s;
484  this->z /= s;
485  }
486  }
487 
491  public: inline Vector3 RotateVector(const Vector3 &_vec) const
492  {
493  Quaternion tmp(0.0, _vec.x, _vec.y, _vec.z);
494  tmp = (*this) * (tmp * this->GetInverse());
495  return Vector3(tmp.x, tmp.y, tmp.z);
496  }
497 
502  public: friend std::ostream &operator<<(std::ostream &_out,
503  const Quaternion &_q)
504  {
505  Vector3 v(_q.GetAsEuler());
506  _out << v.x << " " << v.y << " " << v.z;
507  return _out;
508  }
509 
512  public: Vector3 operator*(const Vector3 &_v) const
513  {
514  Vector3 uv, uuv;
515  Vector3 qvec(this->x, this->y, this->z);
516  uv = qvec.Cross(_v);
517  uuv = qvec.Cross(uv);
518  uv *= (2.0f * this->w);
519  uuv *= 2.0f;
520 
521  return _v + uv + uuv;
522  }
523 
528  public: friend std::istream &operator>>(std::istream &_in,
529  Quaternion &_q)
530  {
531  double roll, pitch, yaw;
532 
533  // Skip white spaces
534  _in.setf(std::ios_base::skipws);
535  _in >> roll >> pitch >> yaw;
536 
537  _q.SetFromEuler(Vector3(roll, pitch, yaw));
538 
539  return _in;
540  }
541 
545  public: bool operator ==(const Quaternion &_qt) const
546  {
547  return this->GetAsEuler() == _qt.GetAsEuler();
548  }
549 
550  public: inline void Correct()
551  {
552  if (!std::isfinite(this->x))
553  this->x = 0;
554  if (!std::isfinite(this->y))
555  this->y = 0;
556  if (!std::isfinite(this->z))
557  this->z = 0;
558  if (!std::isfinite(this->w))
559  this->w = 1;
560 
561  if (equal(this->w, 0.0) && equal(this->x, 0.0) &&
562  equal(this->y, 0.0) && equal(this->z, 0.0))
563  {
564  this->w = 1;
565  }
566  }
567 
569  public: double x;
570 
572  public: double y;
573 
575  public: double z;
576 
578  public: double w;
579  };
580 
583  {
585  public: Pose()
586  :pos(0, 0, 0), rot(1, 0, 0, 0)
587  {}
588 
592  public: Pose(Vector3 _pos, Quaternion _rot)
593  : pos(_pos), rot(_rot) {}
594 
602  public: Pose(double _x, double _y, double _z,
603  double _roll, double _pitch, double _yaw)
604  : pos(_x, _y, _z), rot(_roll, _pitch, _yaw)
605  {
606  rot.Correct();
607  }
608 
613  public: friend std::ostream &operator<<(std::ostream &_out,
614  const Pose &_pose)
615  {
616  _out << _pose.pos << " " << _pose.rot;
617  return _out;
618  }
619 
624  public: friend std::istream &operator>>(std::istream &_in,
625  Pose &_pose)
626  {
627  // Skip white spaces
628  _in.setf(std::ios_base::skipws);
629  _in >> _pose.pos >> _pose.rot;
630  return _in;
631  }
632 
636  public: Pose operator*(const Pose &pose)
637  {
638  return Pose(this->CoordPositionAdd(pose), pose.rot * this->rot);
639  }
640 
644  public: Vector3 CoordPositionAdd(const Pose &_pose) const
645  {
646  Quaternion tmp;
647  Vector3 result;
648 
649  // result = _pose.rot + _pose.rot * this->pos * _pose.rot!
650  tmp.w = 0.0;
651  tmp.x = this->pos.x;
652  tmp.y = this->pos.y;
653  tmp.z = this->pos.z;
654 
655  tmp = _pose.rot * (tmp * _pose.rot.GetInverse());
656 
657  result.x = _pose.pos.x + tmp.x;
658  result.y = _pose.pos.y + tmp.y;
659  result.z = _pose.pos.z + tmp.z;
660 
661  return result;
662  }
663 
667  public: bool operator ==(const Pose &_pose) const
668  {
669  return this->pos == _pose.pos && this->rot == _pose.rot;
670  }
671 
673  public: Vector3 pos;
674 
676  public: Quaternion rot;
677  };
678 
682  {
684  public: Time()
685  : sec(0), nsec(0)
686  {
687  }
688 
692  public: Time(int32_t _sec, int32_t _nsec)
693  : sec(_sec), nsec(_nsec)
694  {
695  }
696 
701  public: friend std::ostream &operator<<(std::ostream &_out,
702  const Time &_time)
703  {
704  _out << _time.sec << " " << _time.nsec;
705  return _out;
706  }
707 
712  public: friend std::istream &operator>>(std::istream &_in,
713  Time &_time)
714  {
715  // Skip white spaces
716  _in.setf(std::ios_base::skipws);
717  _in >> _time.sec >> _time.nsec;
718  return _in;
719  }
720 
724  public: bool operator ==(const Time &_time) const
725  {
726  return this->sec == _time.sec && this->nsec == _time.nsec;
727  }
728 
730  public: int32_t sec;
731 
733  public: int32_t nsec;
734  };
735 
738  {
739  public: double mass;
740  };
741 }
742 #endif
Generic double x, y vector.
Definition: Types.hh:157
Quaternion operator*(const Quaternion &_q) const
Multiplication operator.
Definition: Types.hh:368
friend std::istream & operator>>(std::istream &_in, Pose &_pose)
Stream extraction operator.
Definition: Types.hh:624
friend std::ostream & operator<<(std::ostream &_out, const Quaternion &_q)
Stream insertion operator.
Definition: Types.hh:502
bool operator==(const sdf::Vector3 &_pt) const
Equal to operator.
Definition: Types.hh:279
friend std::istream & operator>>(std::istream &_in, Quaternion &_q)
Stream extraction operator.
Definition: Types.hh:528
Vector3 RotateVector(const Vector3 &_vec) const
Rotate a vector using the quaternion.
Definition: Types.hh:491
Quaternion()
Default Constructor.
Definition: Types.hh:313
Quaternion(double _w, double _x, double _y, double _z)
Constructor.
Definition: Types.hh:332
double z
z data
Definition: Types.hh:575
float b
Blue value.
Definition: Types.hh:102
int32_t sec
Seconds.
Definition: Types.hh:730
double y
y Data
Definition: Types.hh:303
friend std::istream & operator>>(std::istream &_in, Vector3 &_pt)
Stream extraction operator.
Definition: Types.hh:290
void Normalize()
Normalize the quaternion.
Definition: Types.hh:464
Vector2i(int _x=0, int _y=0)
Constructor.
Definition: Types.hh:114
int x
x data
Definition: Types.hh:150
Vector3 operator*(double _v) const
Multiplication operator.
Definition: Types.hh:247
Vector3 GetAsEuler() const
Return the rotation in Euler angles.
Definition: Types.hh:408
double x
x Data
Definition: Types.hh:300
A quaternion class.
Definition: Types.hh:310
double x
x data
Definition: Types.hh:569
friend std::istream & operator>>(std::istream &_in, Vector2d &_pt)
Stream extraction operator.
Definition: Types.hh:180
double z
z Data
Definition: Types.hh:306
Vector3 pos
Position data.
Definition: Types.hh:673
double x
x data
Definition: Types.hh:200
static Quaternion EulerToQuaternion(const Vector3 &_vec)
Convert euler angles to quatern.
Definition: Types.hh:346
Generic integer x, y vector.
Definition: Types.hh:109
Pose(Vector3 _pos, Quaternion _rot)
Constructor.
Definition: Types.hh:592
double mass
Definition: Types.hh:739
Vector3(double _x=0.0, double _y=0.0, double _z=0.0)
Constructor.
Definition: Types.hh:220
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:44
Pose()
Constructor.
Definition: Types.hh:585
Vector3 CoordPositionAdd(const Pose &_pose) const
Add one point to another: result = this + pose.
Definition: Types.hh:644
#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:602
Time()
Constructor.
Definition: Types.hh:684
Vector3 operator*(const Vector3 &_v) const
Vector3 multiplication operator.
Definition: Types.hh:512
Vector3(const Vector3 &_v)
Copy constructor.
Definition: Types.hh:213
int y
y data
Definition: Types.hh:153
const Vector3 & operator*=(double _v)
Multiplication by a double.
Definition: Types.hh:266
Time(int32_t _sec, int32_t _nsec)
Constructor.
Definition: Types.hh:692
friend std::istream & operator>>(std::istream &_in, Vector2i &_pt)
Stream extraction operator.
Definition: Types.hh:132
Defines a color.
Definition: Types.hh:51
Quaternion rot
Orientation data.
Definition: Types.hh:676
float r
Red value.
Definition: Types.hh:96
double y
y data
Definition: Types.hh:572
double w
w data
Definition: Types.hh:578
friend std::ostream & operator<<(std::ostream &_out, const Vector3 &_pt)
Stream insertion operator.
Definition: Types.hh:256
A class for inertial information about a link.
Definition: Types.hh:737
void Correct()
Definition: Types.hh:550
namespace for Simulation Description Format parser
Definition: Console.hh:29
Quaternion(const Quaternion &_q)
Copy constructor.
Definition: Types.hh:318
Quaternion(const double &_roll, const double &_pitch, const double &_yaw)
Definition: Types.hh:321
Vector3 Cross(const Vector3 &_pt) const
Return the cross product of this vector and pt.
Definition: Types.hh:233
Color(double _r=0.0, double _g=0.0, double _b=0.0, double _a=1.0)
Constructor.
Definition: Types.hh:58
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:681
friend std::ostream & operator<<(std::ostream &_out, const Time &_time)
Stream insertion operator.
Definition: Types.hh:701
friend std::istream & operator>>(std::istream &_in, Time &_time)
Stream extraction operator.
Definition: Types.hh:712
friend std::ostream & operator<<(std::ostream &_out, const Vector2d &_pt)
Stream extraction operator.
Definition: Types.hh:169
void SetFromEuler(const Vector3 &_vec)
Set the quaternion from Euler angles.
Definition: Types.hh:443
Encapsulates a position and rotation in three space.
Definition: Types.hh:582
friend std::ostream & operator<<(std::ostream &_out, const Vector2i &_pt)
Stream insertion operator.
Definition: Types.hh:121
The Vector3 class represents the generic vector containing 3 elements.
Definition: Types.hh:209
friend std::ostream & operator<<(std::ostream &_out, const Pose &_pose)
Stream insertion operator.
Definition: Types.hh:613
static Quaternion EulerToQuaternion(double _x, double _y, double _z)
Convert euler angles to quatern.
Definition: Types.hh:339
float g
Green value.
Definition: Types.hh:99
Pose operator*(const Pose &pose)
Multiplication operator.
Definition: Types.hh:636
Quaternion GetInverse() const
Get the inverse of this quaternion.
Definition: Types.hh:379
double y
y data
Definition: Types.hh:203
Vector2d(double _x=0.0, double _y=0.0)
Constructor.
Definition: Types.hh:162
float a
Alpha value.
Definition: Types.hh:105
Vector3 operator+(const Vector3 &_v) const
Addition operator.
Definition: Types.hh:226
int32_t nsec
Nanoseconds.
Definition: Types.hh:733