Main MRPT website > C++ reference for MRPT 1.9.9
lightweight_geom_data.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef LIGHTWEIGHT_GEOM_DATA_H
10 #define LIGHTWEIGHT_GEOM_DATA_H
11 
12 #include <mrpt/utils/utils_defs.h>
13 #include <mrpt/config.h>
14 #include <mrpt/utils/TPixelCoord.h>
15 #include <mrpt/utils/TTypeName.h>
16 #include <mrpt/math/math_frwds.h> // forward declarations
17 #include <mrpt/math/eigen_frwds.h> // forward declarations
18 #include <mrpt/math/wrap2pi.h>
19 #include <vector>
20 #include <stdexcept>
21 
22 namespace mrpt
23 {
24 namespace math
25 {
26 /** \addtogroup geometry_grp
27  * @{ */
28 
29 // Set of typedefs for lightweight geometric items.
30 /**
31  * Lightweight 2D point. Allows coordinate access using [] operator.
32  * \sa mrpt::poses::CPoint2D
33  */
34 struct TPoint2D
35 {
36  enum
37  {
39  };
40  /** X,Y coordinates */
41  double x, y;
42  /** Constructor from TPose2D, discarding phi.
43  * \sa TPose2D
44  */
45  explicit TPoint2D(const TPose2D& p);
46  /**
47  * Constructor from TPoint3D, discarding z.
48  * \sa TPoint3D
49  */
50  explicit TPoint2D(const TPoint3D& p);
51  /**
52  * Constructor from TPose3D, discarding z and the angular coordinates.
53  * \sa TPose3D
54  */
55  explicit TPoint2D(const TPose3D& p);
56  /**
57  * Constructor from CPoseOrPoint, perhaps losing 3D information
58  * \sa CPoseOrPoint mrpt::poses::CPoint3D,CPose2D,CPose3D
59  */
60  template <class DERIVEDCLASS>
62  : x(p.x()), y(p.y())
63  {
64  }
65 
66  /** Implicit transformation constructor from TPixelCoordf */
67  inline TPoint2D(const mrpt::utils::TPixelCoordf& p) : x(p.x), y(p.y) {}
68  /** Implicit constructor from mrpt::poses::CPoint2D */
70  /**
71  * Constructor from coordinates.
72  */
73  inline TPoint2D(double xx, double yy) : x(xx), y(yy) {}
74  /**
75  * Default fast constructor. Initializes to garbage.
76  */
77  inline TPoint2D() {}
78  /** Coordinate access using operator[]. Order: x,y */
79  inline double& operator[](size_t i)
80  {
81  switch (i)
82  {
83  case 0:
84  return x;
85  case 1:
86  return y;
87  default:
88  throw std::out_of_range("index out of range");
89  }
90  }
91  /** Coordinate access using operator[]. Order: x,y */
92  inline const double& operator[](size_t i) const
93  {
94  switch (i)
95  {
96  case 0:
97  return x;
98  case 1:
99  return y;
100  default:
101  throw std::out_of_range("index out of range");
102  }
103  }
104  /**
105  * Transformation into vector.
106  */
107  inline void getAsVector(std::vector<double>& v) const
108  {
109  v.resize(2);
110  v[0] = x;
111  v[1] = y;
112  }
113 
114  bool operator<(const TPoint2D& p) const;
115 
116  inline TPoint2D& operator+=(const TPoint2D& p)
117  {
118  x += p.x;
119  y += p.y;
120  return *this;
121  }
122 
123  inline TPoint2D& operator-=(const TPoint2D& p)
124  {
125  x -= p.x;
126  y -= p.y;
127  return *this;
128  }
129 
130  inline TPoint2D& operator*=(double d)
131  {
132  x *= d;
133  y *= d;
134  return *this;
135  }
136 
137  inline TPoint2D& operator/=(double d)
138  {
139  x /= d;
140  y /= d;
141  return *this;
142  }
143 
144  inline TPoint2D operator+(const TPoint2D& p) const
145  {
146  TPoint2D r(*this);
147  return r += p;
148  }
149 
150  inline TPoint2D operator-(const TPoint2D& p) const
151  {
152  TPoint2D r(*this);
153  return r -= p;
154  }
155 
156  inline TPoint2D operator*(double d) const
157  {
158  TPoint2D r(*this);
159  return r *= d;
160  }
161 
162  inline TPoint2D operator/(double d) const
163  {
164  TPoint2D r(*this);
165  return r /= d;
166  }
167  /** Returns a human-readable textual representation of the object (eg:
168  * "[0.02 1.04]" )
169  * \sa fromString
170  */
171  void asString(std::string& s) const { s = mrpt::format("[%f %f]", x, y); }
172  inline std::string asString() const
173  {
174  std::string s;
175  asString(s);
176  return s;
177  }
178 
179  /** Set the current object value from a string generated by 'asString' (eg:
180  * "[0.02 1.04]" )
181  * \sa asString
182  * \exception std::exception On invalid format
183  */
184  void fromString(const std::string& s);
185  static size_t size() { return 2; }
186  /** Point norm. */
187  inline double norm() const { return sqrt(square(x) + square(y)); }
188 };
189 
190 /**
191  * Lightweight 2D pose. Allows coordinate access using [] operator.
192  * \sa mrpt::poses::CPose2D
193  */
194 struct TPose2D
195 {
196  enum
197  {
199  };
200  /** X,Y coordinates */
201  double x, y;
202  /** Orientation (rads) */
203  double phi;
204  /** Implicit constructor from TPoint2D. Zeroes the phi coordinate.
205  * \sa TPoint2D
206  */
207  TPose2D(const TPoint2D& p);
208  /**
209  * Constructor from TPoint3D, losing information. Zeroes the phi
210  * coordinate.
211  * \sa TPoint3D
212  */
213  explicit TPose2D(const TPoint3D& p);
214  /**
215  * Constructor from TPose3D, losing information. The phi corresponds to the
216  * original pose's yaw.
217  * \sa TPose3D
218  */
219  explicit TPose2D(const TPose3D& p);
220  /**
221  * Implicit constructor from heavyweight type.
222  * \sa mrpt::poses::CPose2D
223  */
225  /**
226  * Constructor from coordinates.
227  */
228  inline TPose2D(double xx, double yy, double pphi) : x(xx), y(yy), phi(pphi)
229  {
230  }
231  /**
232  * Default fast constructor. Initializes to garbage.
233  */
234  inline TPose2D() {}
235  /** Coordinate access using operator[]. Order: x,y,phi */
236  inline double& operator[](size_t i)
237  {
238  switch (i)
239  {
240  case 0:
241  return x;
242  case 1:
243  return y;
244  case 2:
245  return phi;
246  default:
247  throw std::out_of_range("index out of range");
248  }
249  }
250  /** Coordinate access using operator[]. Order: x,y,phi */
251  inline const double& operator[](size_t i) const
252  {
253  switch (i)
254  {
255  case 0:
256  return x;
257  case 1:
258  return y;
259  case 2:
260  return phi;
261  default:
262  throw std::out_of_range("index out of range");
263  }
264  }
265  /**
266  * Transformation into vector.
267  */
268  inline void getAsVector(std::vector<double>& v) const
269  {
270  v.resize(3);
271  v[0] = x;
272  v[1] = y;
273  v[2] = phi;
274  }
275  /** Returns a human-readable textual representation of the object (eg: "[x y
276  * yaw]", yaw in degrees)
277  * \sa fromString
278  */
279  void asString(std::string& s) const;
280  inline std::string asString() const
281  {
282  std::string s;
283  asString(s);
284  return s;
285  }
286 
287  /** Operator "oplus" pose composition: "ret=this \oplus b" \sa CPose2D */
289  /** Operator "ominus" pose composition: "ret=this \ominus b" \sa CPose2D */
291  inline void composePoint(const TPoint2D l, TPoint2D& g) const
292  {
293  const double ccos = ::cos(phi), csin = ::sin(phi);
294  g.x = x + l.x * ccos - l.y * csin;
295  g.y = y + l.x * csin + l.y * ccos;
296  }
297  inline void inverseComposePoint(const TPoint2D g, TPoint2D& l) const
298  {
299  const double Ax = g.x - x, Ay = g.y - y, ccos = ::cos(phi),
300  csin = ::sin(phi);
301  l.x = Ax * ccos + Ay * csin;
302  l.y = -Ax * csin + Ay * ccos;
303  }
304  /** Returns the norm of the (x,y) vector (phi is not used) */
305  inline double norm() const { return mrpt::math::hypot_fast(x, y); }
306  /** Set the current object value from a string generated by 'asString' (eg:
307  * "[0.02 1.04 -45.0]" )
308  * \sa asString
309  * \exception std::exception On invalid format
310  */
311  void fromString(const std::string& s);
312  static size_t size() { return 3; }
313 };
314 
315 /** Lightweight 3D point (float version).
316  * \sa mrpt::poses::CPoint3D, mrpt::math::TPoint3D
317  */
318 struct TPoint3Df
319 {
320  enum
321  {
323  };
324  float x;
325  float y;
326  float z;
327 
328  inline TPoint3Df() {}
329  inline TPoint3Df(const float xx, const float yy, const float zz)
330  : x(xx), y(yy), z(zz)
331  {
332  }
333  inline TPoint3Df& operator+=(const TPoint3Df& p)
334  {
335  x += p.x;
336  y += p.y;
337  z += p.z;
338  return *this;
339  }
340  inline TPoint3Df operator*(const float s)
341  {
342  return TPoint3Df(x * s, y * s, z * s);
343  }
344  /** Coordinate access using operator[]. Order: x,y,z */
345  inline float& operator[](size_t i)
346  {
347  switch (i)
348  {
349  case 0:
350  return x;
351  case 1:
352  return y;
353  case 2:
354  return z;
355  default:
356  throw std::out_of_range("index out of range");
357  }
358  }
359 
360  /** Coordinate access using operator[]. Order: x,y,z */
361  inline const float& operator[](size_t i) const
362  {
363  switch (i)
364  {
365  case 0:
366  return x;
367  case 1:
368  return y;
369  case 2:
370  return z;
371  default:
372  throw std::out_of_range("index out of range");
373  }
374  }
375 };
376 
377 /**
378  * Lightweight 3D point. Allows coordinate access using [] operator.
379  * \sa mrpt::poses::CPoint3D, mrpt::math::TPoint3Df
380  */
381 struct TPoint3D
382 {
383  enum
384  {
386  };
387  /** X,Y,Z coordinates */
388  double x, y, z;
389 
390  /** Constructor from coordinates. */
391  inline TPoint3D(double xx, double yy, double zz) : x(xx), y(yy), z(zz) {}
392  /** Default fast constructor. Initializes to garbage. */
393  inline TPoint3D() {}
394  /** Explicit constructor from coordinates. */
395  explicit inline TPoint3D(const TPoint3Df& p) : x(p.x), y(p.y), z(p.z) {}
396  /** Implicit constructor from TPoint2D. Zeroes the z.
397  * \sa TPoint2D
398  */
399  TPoint3D(const TPoint2D& p);
400  /**
401  * Constructor from TPose2D, losing information. Zeroes the z.
402  * \sa TPose2D
403  */
404  explicit TPoint3D(const TPose2D& p);
405  /**
406  * Constructor from TPose3D, losing information.
407  * \sa TPose3D
408  */
409  explicit TPoint3D(const TPose3D& p);
410  /**
411  * Implicit constructor from heavyweight type.
412  * \sa mrpt::poses::CPoint3D
413  */
415  /**
416  * Constructor from heavyweight 3D pose.
417  * \sa mrpt::poses::CPose3D.
418  */
419  explicit TPoint3D(const mrpt::poses::CPose3D& p);
420  /** Coordinate access using operator[]. Order: x,y,z */
421  inline double& operator[](size_t i)
422  {
423  switch (i)
424  {
425  case 0:
426  return x;
427  case 1:
428  return y;
429  case 2:
430  return z;
431  default:
432  throw std::out_of_range("index out of range");
433  }
434  }
435  /** Coordinate access using operator[]. Order: x,y,z */
436  inline const double& operator[](size_t i) const
437  {
438  switch (i)
439  {
440  case 0:
441  return x;
442  case 1:
443  return y;
444  case 2:
445  return z;
446  default:
447  throw std::out_of_range("index out of range");
448  }
449  }
450  /**
451  * Point-to-point distance.
452  */
453  inline double distanceTo(const TPoint3D& p) const
454  {
455  return sqrt(square(p.x - x) + square(p.y - y) + square(p.z - z));
456  }
457  /**
458  * Point-to-point distance, squared.
459  */
460  inline double sqrDistanceTo(const TPoint3D& p) const
461  {
462  return square(p.x - x) + square(p.y - y) + square(p.z - z);
463  }
464  /**
465  * Point norm.
466  */
467  inline double norm() const
468  {
469  return sqrt(square(x) + square(y) + square(z));
470  }
471  /**
472  * Point scale.
473  */
474  inline TPoint3D& operator*=(const double f)
475  {
476  x *= f;
477  y *= f;
478  z *= f;
479  return *this;
480  }
481  /**
482  * Transformation into vector.
483  */
484  template <class VECTORLIKE>
485  void getAsVector(VECTORLIKE& v) const
486  {
487  v.resize(3);
488  v[0] = x;
489  v[1] = y;
490  v[2] = z;
491  }
492  /**
493  * Translation.
494  */
495  inline TPoint3D& operator+=(const TPoint3D& p)
496  {
497  x += p.x;
498  y += p.y;
499  z += p.z;
500  return *this;
501  }
502  /**
503  * Difference between points.
504  */
505  inline TPoint3D& operator-=(const TPoint3D& p)
506  {
507  x -= p.x;
508  y -= p.y;
509  z -= p.z;
510  return *this;
511  }
512  /**
513  * Points addition.
514  */
515  inline TPoint3D operator+(const TPoint3D& p) const
516  {
517  return TPoint3D(x + p.x, y + p.y, z + p.z);
518  }
519  /**
520  * Points substraction.
521  */
522  inline TPoint3D operator-(const TPoint3D& p) const
523  {
524  return TPoint3D(x - p.x, y - p.y, z - p.z);
525  }
526 
527  inline TPoint3D operator*(double d) const
528  {
529  return TPoint3D(x * d, y * d, z * d);
530  }
531 
532  inline TPoint3D operator/(double d) const
533  {
534  return TPoint3D(x / d, y / d, z / d);
535  }
536 
537  bool operator<(const TPoint3D& p) const;
538 
539  /** Returns a human-readable textual representation of the object (eg:
540  * "[0.02 1.04 -0.8]" )
541  * \sa fromString
542  */
543  void asString(std::string& s) const
544  {
545  s = mrpt::format("[%f %f %f]", x, y, z);
546  }
547  inline std::string asString() const
548  {
549  std::string s;
550  asString(s);
551  return s;
552  }
553 
554  /** Set the current object value from a string generated by 'asString' (eg:
555  * "[0.02 1.04 -0.8]" )
556  * \sa asString
557  * \exception std::exception On invalid format
558  */
559  void fromString(const std::string& s);
560  static size_t size() { return 3; }
561 };
562 
563 /** XYZ point (double) + Intensity(u8) \sa mrpt::math::TPoint3D */
565 {
568  inline TPointXYZIu8() : pt(), intensity(0) {}
569  inline TPointXYZIu8(double x, double y, double z, uint8_t intensity_val)
570  : pt(x, y, z), intensity(intensity_val)
571  {
572  }
573 };
574 /** XYZ point (double) + RGB(u8) \sa mrpt::math::TPoint3D */
576 {
579  inline TPointXYZRGBu8() : pt(), R(0), G(0), B(0) {}
581  double x, double y, double z, uint8_t R_val, uint8_t G_val,
582  uint8_t B_val)
583  : pt(x, y, z), R(R_val), G(G_val), B(B_val)
584  {
585  }
586 };
587 /** XYZ point (float) + Intensity(u8) \sa mrpt::math::TPoint3D */
589 {
592  inline TPointXYZfIu8() : pt(), intensity(0) {}
593  inline TPointXYZfIu8(float x, float y, float z, uint8_t intensity_val)
594  : pt(x, y, z), intensity(intensity_val)
595  {
596  }
597 };
598 /** XYZ point (float) + RGB(u8) \sa mrpt::math::TPoint3D */
600 {
603  inline TPointXYZfRGBu8() : pt(), R(0), G(0), B(0) {}
605  float x, float y, float z, uint8_t R_val, uint8_t G_val, uint8_t B_val)
606  : pt(x, y, z), R(R_val), G(G_val), B(B_val)
607  {
608  }
609 };
610 
611 /**
612  * Lightweight 3D pose (three spatial coordinates, plus three angular
613  * coordinates). Allows coordinate access using [] operator.
614  * \sa mrpt::poses::CPose3D
615  */
616 struct TPose3D
617 {
618  enum
619  {
621  };
622  /** X,Y,Z, coords */
623  double x, y, z;
624  /** Yaw coordinate (rotation angle over Z axis). */
625  double yaw;
626  /** Pitch coordinate (rotation angle over Y axis). */
627  double pitch;
628  /** Roll coordinate (rotation angle over X coordinate). */
629  double roll;
630  /** Implicit constructor from TPoint2D. Zeroes all the unprovided
631  * information.
632  * \sa TPoint2D
633  */
634  TPose3D(const TPoint2D& p);
635  /**
636  * Implicit constructor from TPose2D. Gets the yaw from the 2D pose's phi,
637  * zeroing all the unprovided information.
638  * \sa TPose2D
639  */
640  TPose3D(const TPose2D& p);
641  /**
642  * Implicit constructor from TPoint3D. Zeroes angular information.
643  * \sa TPoint3D
644  */
645  TPose3D(const TPoint3D& p);
646  /**
647  * Implicit constructor from heavyweight type.
648  * \sa mrpt::poses::CPose3D
649  */
651  /**
652  * Constructor from coordinates.
653  */
655  double _x, double _y, double _z, double _yaw, double _pitch,
656  double _roll)
657  : x(_x), y(_y), z(_z), yaw(_yaw), pitch(_pitch), roll(_roll)
658  {
659  }
660  /**
661  * Default fast constructor. Initializes to garbage.
662  */
663  inline TPose3D() {}
664  /** Coordinate access using operator[]. Order: x,y,z,yaw,pitch,roll */
665  inline double& operator[](size_t i)
666  {
667  switch (i)
668  {
669  case 0:
670  return x;
671  case 1:
672  return y;
673  case 2:
674  return z;
675  case 3:
676  return yaw;
677  case 4:
678  return pitch;
679  case 5:
680  return roll;
681  default:
682  throw std::out_of_range("index out of range");
683  }
684  }
685  /** Coordinate access using operator[]. Order: x,y,z,yaw,pitch,roll */
686  inline const double& operator[](size_t i) const
687  {
688  switch (i)
689  {
690  case 0:
691  return x;
692  case 1:
693  return y;
694  case 2:
695  return z;
696  case 3:
697  return yaw;
698  case 4:
699  return pitch;
700  case 5:
701  return roll;
702  default:
703  throw std::out_of_range("index out of range");
704  }
705  }
706  /**
707  * Pose's spatial coordinates norm.
708  */
709  double norm() const { return std::sqrt(square(x) + square(y) + square(z)); }
710  /**
711  * Gets the pose as a vector of doubles.
712  */
713  void getAsVector(std::vector<double>& v) const
714  {
715  v.resize(6);
716  v[0] = x;
717  v[1] = y;
718  v[2] = z;
719  v[3] = yaw;
720  v[4] = pitch;
721  v[5] = roll;
722  }
723  /** Returns a human-readable textual representation of the object (eg: "[x y
724  * z yaw pitch roll]", angles in degrees.)
725  * \sa fromString
726  */
727  void asString(std::string& s) const;
728  inline std::string asString() const
729  {
730  std::string s;
731  asString(s);
732  return s;
733  }
734 
735  /** Returns the quaternion associated to the rotation of this object (NOTE:
736  * XYZ translation is ignored)
737  * \f[ \mathbf{q} = \left( \begin{array}{c} \cos (\phi /2) \cos (\theta /2)
738  * \cos (\psi /2) + \sin (\phi /2) \sin (\theta /2) \sin (\psi /2) \\ \sin
739  * (\phi /2) \cos (\theta /2) \cos (\psi /2) - \cos (\phi /2) \sin (\theta
740  * /2) \sin (\psi /2) \\ \cos (\phi /2) \sin (\theta /2) \cos (\psi /2) +
741  * \sin (\phi /2) \cos (\theta /2) \sin (\psi /2) \\ \cos (\phi /2) \cos
742  * (\theta /2) \sin (\psi /2) - \sin (\phi /2) \sin (\theta /2) \cos (\psi
743  * /2) \\ \end{array}\right) \f]
744  * With : \f$ \phi = roll \f$, \f$ \theta = pitch \f$ and \f$ \psi = yaw
745  * \f$.
746  * \param out_dq_dr If provided, the 4x3 Jacobian of the transformation will
747  * be computed and stored here. It's the Jacobian of the transformation from
748  * (yaw pitch roll) to (qr qx qy qz).
749  */
750  void getAsQuaternion(
752  mrpt::math::CMatrixFixedNumeric<double, 4, 3>* out_dq_dr = NULL) const;
753 
754  /** Set the current object value from a string generated by 'asString' (eg:
755  * "[0.02 1.04 -0.8]" )
756  * \sa asString
757  * \exception std::exception On invalid format
758  */
759  void fromString(const std::string& s);
760  static size_t size() { return 6; }
761 };
762 
763 /** Lightweight 3D pose (three spatial coordinates, plus a quaternion ). Allows
764  * coordinate access using [] operator.
765  * \sa mrpt::poses::CPose3DQuat
766  */
768 {
769  enum
770  {
772  };
773  /** Translation in x,y,z */
774  double x, y, z;
775  /** Unit quaternion part, qr,qx,qy,qz */
776  double qr, qx, qy, qz;
777 
778  /** Constructor from coordinates. */
779  inline TPose3DQuat(
780  double _x, double _y, double _z, double _qr, double _qx, double _qy,
781  double _qz)
782  : x(_x), y(_y), z(_z), qr(_qr), qx(_qx), qy(_qy), qz(_qz)
783  {
784  }
785  /** Default fast constructor. Initializes to garbage. */
786  inline TPose3DQuat() {}
787  /** Constructor from a CPose3DQuat */
789 
790  /** Coordinate access using operator[]. Order: x,y,z,qr,qx,qy,qz */
791  inline double& operator[](size_t i)
792  {
793  switch (i)
794  {
795  case 0:
796  return x;
797  case 1:
798  return y;
799  case 2:
800  return z;
801  case 3:
802  return qr;
803  case 4:
804  return qx;
805  case 5:
806  return qy;
807  case 6:
808  return qz;
809  default:
810  throw std::out_of_range("index out of range");
811  }
812  }
813  /** Coordinate access using operator[]. Order: x,y,z,qr,qx,qy,qz */
814  inline const double& operator[](size_t i) const
815  {
816  switch (i)
817  {
818  case 0:
819  return x;
820  case 1:
821  return y;
822  case 2:
823  return z;
824  case 3:
825  return qr;
826  case 4:
827  return qx;
828  case 5:
829  return qy;
830  case 6:
831  return qz;
832  default:
833  throw std::out_of_range("index out of range");
834  }
835  }
836  /** Pose's spatial coordinates norm. */
837  double norm() const { return sqrt(square(x) + square(y) + square(z)); }
838  /** Gets the pose as a vector of doubles. */
839  void getAsVector(std::vector<double>& v) const
840  {
841  v.resize(7);
842  for (size_t i = 0; i < 7; i++) v[i] = (*this)[i];
843  }
844  /** Returns a human-readable textual representation of the object as "[x y z
845  * qr qx qy qz]"
846  * \sa fromString
847  */
848  void asString(std::string& s) const
849  {
850  s = mrpt::format("[%f %f %f %f %f %f %f]", x, y, z, qr, qx, qy, qz);
851  }
852  inline std::string asString() const
853  {
854  std::string s;
855  asString(s);
856  return s;
857  }
858 
859  /** Set the current object value from a string generated by 'asString' (eg:
860  * "[0.02 1.04 -0.8 1.0 0.0 0.0 0.0]" )
861  * \sa asString
862  * \exception std::exception On invalid format
863  */
864  void fromString(const std::string& s);
865  static size_t size() { return 7; }
866 };
867 
868 // Text streaming functions:
869 std::ostream& operator<<(std::ostream& o, const TPoint2D& p);
870 std::ostream& operator<<(std::ostream& o, const TPoint3D& p);
871 std::ostream& operator<<(std::ostream& o, const TPose2D& p);
872 std::ostream& operator<<(std::ostream& o, const TPose3D& p);
873 std::ostream& operator<<(std::ostream& o, const TPose3DQuat& p);
874 
875 /**
876  * Unary minus operator for 3D points.
877  */
878 inline TPoint3D operator-(const TPoint3D& p1)
879 {
880  return TPoint3D(-p1.x, -p1.y, -p1.z);
881 }
882 /**
883  * Exact comparison between 2D points.
884  */
885 inline bool operator==(const TPoint2D& p1, const TPoint2D& p2)
886 {
887  return (p1.x == p2.x) && (p1.y == p2.y); //-V550
888 }
889 /**
890  * Exact comparison between 2D points.
891  */
892 inline bool operator!=(const TPoint2D& p1, const TPoint2D& p2)
893 {
894  return (p1.x != p2.x) || (p1.y != p2.y); //-V550
895 }
896 /**
897  * Exact comparison between 3D points.
898  */
899 inline bool operator==(const TPoint3D& p1, const TPoint3D& p2)
900 {
901  return (p1.x == p2.x) && (p1.y == p2.y) && (p1.z == p2.z); //-V550
902 }
903 /**
904  * Exact comparison between 3D points.
905  */
906 inline bool operator!=(const TPoint3D& p1, const TPoint3D& p2)
907 {
908  return (p1.x != p2.x) || (p1.y != p2.y) || (p1.z != p2.z); //-V550
909 }
910 /**
911  * Exact comparison between 2D poses, taking possible cycles into account.
912  */
913 inline bool operator==(const TPose2D& p1, const TPose2D& p2)
914 {
915  return (p1.x == p2.x) && (p1.y == p2.y) &&
916  (mrpt::math::wrapTo2Pi(p1.phi) ==
917  mrpt::math::wrapTo2Pi(p2.phi)); //-V550
918 }
919 /**
920  * Exact comparison between 2D poses, taking possible cycles into account.
921  */
922 inline bool operator!=(const TPose2D& p1, const TPose2D& p2)
923 {
924  return (p1.x != p2.x) || (p1.y != p2.y) ||
925  (mrpt::math::wrapTo2Pi(p1.phi) !=
926  mrpt::math::wrapTo2Pi(p2.phi)); //-V550
927 }
928 /**
929  * Exact comparison between 3D poses, taking possible cycles into account.
930  */
931 inline bool operator==(const TPose3D& p1, const TPose3D& p2)
932 {
933  return (p1.x == p2.x) && (p1.y == p2.y) && (p1.z == p2.z) &&
938  mrpt::math::wrapTo2Pi(p2.roll)); //-V550
939 }
940 /**
941  * Exact comparison between 3D poses, taking possible cycles into account.
942  */
943 inline bool operator!=(const TPose3D& p1, const TPose3D& p2)
944 {
945  return (p1.x != p2.x) || (p1.y != p2.y) || (p1.z != p2.z) ||
950  mrpt::math::wrapTo2Pi(p2.roll)); //-V550
951 }
952 // Forward declarations
953 struct TSegment3D;
954 struct TLine3D;
955 class TPolygon3D;
956 struct TObject3D;
957 
958 // Pragma defined to ensure no structure packing
959 /**
960  * 2D segment, consisting of two points.
961  * \sa TSegment3D,TLine2D,TPolygon2D,TPoint2D
962  */
964 {
965  public:
966  /**
967  * Origin point.
968  */
970  /**
971  * Destiny point.
972  */
974  /**
975  * Segment length.
976  */
977  double length() const;
978  /**
979  * Distance to point.
980  */
981  double distance(const TPoint2D& point) const;
982  /**
983  * Distance with sign to point (sign indicates which side the point is).
984  */
985  double signedDistance(const TPoint2D& point) const;
986  /**
987  * Check whether a point is inside a segment.
988  */
989  bool contains(const TPoint2D& point) const;
990  /** Access to points using operator[0-1] */
991  inline TPoint2D& operator[](size_t i)
992  {
993  switch (i)
994  {
995  case 0:
996  return point1;
997  case 1:
998  return point2;
999  default:
1000  throw std::out_of_range("index out of range");
1001  }
1002  }
1003  /** Access to points using operator[0-1] */
1004  inline const TPoint2D& operator[](size_t i) const
1005  {
1006  switch (i)
1007  {
1008  case 0:
1009  return point1;
1010  case 1:
1011  return point2;
1012  default:
1013  throw std::out_of_range("index out of range");
1014  }
1015  }
1016  /**
1017  * Project into 3D space, setting the z to 0.
1018  */
1019  void generate3DObject(TSegment3D& s) const;
1020  /**
1021  * Segment's central point.
1022  */
1023  inline void getCenter(TPoint2D& p) const
1024  {
1025  p.x = (point1.x + point2.x) / 2;
1026  p.y = (point1.y + point2.y) / 2;
1027  }
1028  /**
1029  * Constructor from both points.
1030  */
1031  TSegment2D(const TPoint2D& p1, const TPoint2D& p2) : point1(p1), point2(p2)
1032  {
1033  }
1034  /**
1035  * Fast default constructor. Initializes to garbage.
1036  */
1038  /**
1039  * Explicit constructor from 3D object, discarding the z.
1040  */
1041  explicit TSegment2D(const TSegment3D& s);
1042 
1043  bool operator<(const TSegment2D& s) const;
1044 };
1045 /**
1046  * 3D segment, consisting of two points.
1047  * \sa TSegment2D,TLine3D,TPlane,TPolygon3D,TPoint3D
1048  */
1050 {
1051  public:
1052  /**
1053  * Origin point.
1054  */
1056  /**
1057  * Destiny point.
1058  */
1060  /**
1061  * Segment length.
1062  */
1063  double length() const;
1064  /**
1065  * Distance to point.
1066  */
1067  double distance(const TPoint3D& point) const;
1068  /**
1069  * Distance to another segment.
1070  */
1071  double distance(const TSegment3D& segment) const;
1072  /**
1073  * Check whether a point is inside the segment.
1074  */
1075  bool contains(const TPoint3D& point) const;
1076  /** Access to points using operator[0-1] */
1077  inline TPoint3D& operator[](size_t i)
1078  {
1079  switch (i)
1080  {
1081  case 0:
1082  return point1;
1083  case 1:
1084  return point2;
1085  default:
1086  throw std::out_of_range("index out of range");
1087  }
1088  }
1089  /** Access to points using operator[0-1] */
1090  inline const TPoint3D& operator[](size_t i) const
1091  {
1092  switch (i)
1093  {
1094  case 0:
1095  return point1;
1096  case 1:
1097  return point2;
1098  default:
1099  throw std::out_of_range("index out of range");
1100  }
1101  }
1102  /**
1103  * Projection into 2D space, discarding the z.
1104  */
1105  inline void generate2DObject(TSegment2D& s) const { s = TSegment2D(*this); }
1106  /**
1107  * Segment's central point.
1108  */
1109  inline void getCenter(TPoint3D& p) const
1110  {
1111  p.x = (point1.x + point2.x) / 2;
1112  p.y = (point1.y + point2.y) / 2;
1113  p.z = (point1.z + point2.z) / 2;
1114  }
1115  /**
1116  * Constructor from both points.
1117  */
1118  TSegment3D(const TPoint3D& p1, const TPoint3D& p2) : point1(p1), point2(p2)
1119  {
1120  }
1121  /**
1122  * Fast default constructor. Initializes to garbage.
1123  */
1125  /**
1126  * Constructor from 2D object. Sets the z to zero.
1127  */
1128  explicit TSegment3D(const TSegment2D& s)
1129  : point1(s.point1), point2(s.point2)
1130  {
1131  }
1132 
1133  bool operator<(const TSegment3D& s) const;
1134 };
1135 
1136 inline bool operator==(const TSegment2D& s1, const TSegment2D& s2)
1137 {
1138  return (s1.point1 == s2.point1) && (s1.point2 == s2.point2);
1139 }
1140 
1141 inline bool operator!=(const TSegment2D& s1, const TSegment2D& s2)
1142 {
1143  return (s1.point1 != s2.point1) || (s1.point2 != s2.point2);
1144 }
1145 
1146 inline bool operator==(const TSegment3D& s1, const TSegment3D& s2)
1147 {
1148  return (s1.point1 == s2.point1) && (s1.point2 == s2.point2);
1149 }
1150 
1151 inline bool operator!=(const TSegment3D& s1, const TSegment3D& s2)
1152 {
1153  return (s1.point1 != s2.point1) || (s1.point2 != s2.point2);
1154 }
1155 
1156 /**
1157  * 2D line without bounds, represented by its equation \f$Ax+By+C=0\f$.
1158  * \sa TLine3D,TSegment2D,TPolygon2D,TPoint2D
1159  */
1160 struct TLine2D
1161 {
1162  public:
1163  /**
1164  * Line coefficients, stored as an array: \f$\left[A,B,C\right]\f$.
1165  */
1166  double coefs[3];
1167  /**
1168  * Evaluate point in the line's equation.
1169  */
1170  double evaluatePoint(const TPoint2D& point) const;
1171  /**
1172  * Check whether a point is inside the line.
1173  */
1174  bool contains(const TPoint2D& point) const;
1175  /**
1176  * Distance from a given point.
1177  */
1178  double distance(const TPoint2D& point) const;
1179  /**
1180  * Distance with sign from a given point (sign indicates side).
1181  */
1182  double signedDistance(const TPoint2D& point) const;
1183  /**
1184  * Get line's normal vector.
1185  */
1186  void getNormalVector(double (&vector)[2]) const;
1187  /**
1188  * Unitarize line's normal vector.
1189  */
1190  void unitarize();
1191  /**
1192  * Get line's normal vector after unitarizing line.
1193  */
1194  inline void getUnitaryNormalVector(double (&vector)[2])
1195  {
1196  unitarize();
1197  getNormalVector(vector);
1198  }
1199  /**
1200  * Get line's director vector.
1201  */
1202  void getDirectorVector(double (&vector)[2]) const;
1203  /**
1204  * Unitarize line and then get director vector.
1205  */
1206  inline void getUnitaryDirectorVector(double (&vector)[2])
1207  {
1208  unitarize();
1209  getDirectorVector(vector);
1210  }
1211  /**
1212  * Project into 3D space, setting the z to 0.
1213  */
1214  void generate3DObject(TLine3D& l) const;
1215  /**
1216  * Get a pose2D whose X axis corresponds to the line.
1217  * \sa mrpt::poses::CPose2D.
1218  */
1219  void getAsPose2D(mrpt::poses::CPose2D& outPose) const;
1220  /**
1221  * Get a pose2D whose X axis corresponds to the line, forcing the base
1222  * point to one given.
1223  * \throw logic_error if the point is not inside the line.
1224  * \sa mrpt::poses::CPose2D.
1225  */
1227  const TPoint2D& origin, mrpt::poses::CPose2D& outPose) const;
1228  /**
1229  * Constructor from two points, through which the line will pass.
1230  * \throw logic_error if both points are the same
1231  */
1232  TLine2D(const TPoint2D& p1, const TPoint2D& p2);
1233  /**
1234  * Constructor from a segment.
1235  */
1236  explicit TLine2D(const TSegment2D& s);
1237  /**
1238  * Fast default constructor. Initializes to garbage.
1239  */
1240  TLine2D() {}
1241  /**
1242  * Constructor from line's coefficients.
1243  */
1244  inline TLine2D(double A, double B, double C)
1245  {
1246  coefs[0] = A;
1247  coefs[1] = B;
1248  coefs[2] = C;
1249  }
1250  /**
1251  * Construction from 3D object, discarding the Z.
1252  * \throw std::logic_error if the line is normal to the XY plane.
1253  */
1254  explicit TLine2D(const TLine3D& l);
1255 };
1256 
1257 /**
1258  * 3D line, represented by a base point and a director vector.
1259  * \sa TLine2D,TSegment3D,TPlane,TPolygon3D,TPoint3D
1260  */
1261 struct TLine3D
1262 {
1263  public:
1264  /**
1265  * Base point.
1266  */
1268  /**
1269  * Director vector.
1270  */
1271  double director[3];
1272  /**
1273  * Check whether a point is inside the line.
1274  */
1275  bool contains(const TPoint3D& point) const;
1276  /**
1277  * Distance between the line and a point.
1278  */
1279  double distance(const TPoint3D& point) const;
1280  /**
1281  * Unitarize director vector.
1282  */
1283  void unitarize();
1284  /**
1285  * Get director vector.
1286  */
1287  inline void getDirectorVector(double (&vector)[3]) const
1288  {
1289  for (size_t i = 0; i < 3; i++) vector[i] = director[i];
1290  }
1291  /**
1292  * Unitarize and then get director vector.
1293  */
1294  inline void getUnitaryDirectorVector(double (&vector)[3])
1295  {
1296  unitarize();
1297  getDirectorVector(vector);
1298  }
1299  /**
1300  * Project into 2D space, discarding the Z coordinate.
1301  * \throw std::logic_error if the line's director vector is orthogonal to
1302  * the XY plane.
1303  */
1304  inline void generate2DObject(TLine2D& l) const { l = TLine2D(*this); }
1305  /**
1306  * Constructor from two points, through which the line will pass.
1307  * \throw std::logic_error if both points are the same.
1308  */
1309  TLine3D(const TPoint3D& p1, const TPoint3D& p2);
1310  /**
1311  * Constructor from 3D segment.
1312  */
1313  explicit TLine3D(const TSegment3D& s);
1314  /**
1315  * Fast default constructor. Initializes to garbage.
1316  */
1317  TLine3D() {}
1318  /** Constructor from 2D object. Zeroes the z. */
1319  explicit TLine3D(const TLine2D& l);
1320 };
1321 
1322 /**
1323  * 3D Plane, represented by its equation \f$Ax+By+Cz+D=0\f$
1324  * \sa TSegment3D,TLine3D,TPolygon3D,TPoint3D
1325  */
1326 struct TPlane
1327 {
1328  public:
1329  /**
1330  * Plane coefficients, stored as an array: \f$\left[A,B,C,D\right]\f$
1331  */
1332  double coefs[4];
1333  /**
1334  * Evaluate a point in the plane's equation.
1335  */
1336  double evaluatePoint(const TPoint3D& point) const;
1337  /**
1338  * Check whether a point is contained into the plane.
1339  */
1340  bool contains(const TPoint3D& point) const;
1341  /**
1342  * Check whether a segment is fully contained into the plane.
1343  */
1344  inline bool contains(const TSegment3D& segment) const
1345  {
1346  return contains(segment.point1) && contains(segment.point2);
1347  }
1348  /**
1349  * Check whether a line is fully contained into the plane.
1350  */
1351  bool contains(const TLine3D& line) const;
1352  /**
1353  * Distance to 3D point.
1354  */
1355  double distance(const TPoint3D& point) const;
1356  /**
1357  * Distance to 3D line. Will be zero if the line is not parallel to the
1358  * plane.
1359  */
1360  double distance(const TLine3D& line) const;
1361  /**
1362  * Get plane's normal vector.
1363  */
1364  void getNormalVector(double (&vec)[3]) const;
1365  /**
1366  * Unitarize normal vector.
1367  */
1368  void unitarize();
1369  /**
1370  * Unitarize, then get normal vector.
1371  */
1372  inline void getUnitaryNormalVector(double (&vec)[3])
1373  {
1374  unitarize();
1375  getNormalVector(vec);
1376  }
1377  /**
1378  * Gets a pose whose XY plane corresponds to this plane.
1379  */
1380  void getAsPose3D(mrpt::poses::CPose3D& outPose);
1381  /**
1382  * Gets a pose whose XY plane corresponds to this plane.
1383  */
1384  inline void getAsPose3D(mrpt::poses::CPose3D& outPose) const
1385  {
1386  TPlane p = *this;
1387  p.getAsPose3D(outPose);
1388  }
1389  /**
1390  * Gets a pose whose XY plane corresponds to this, forcing an exact point
1391  * as its spatial coordinates.
1392  * \throw std::logic_error if the point is not inside the plane.
1393  */
1395  const TPoint3D& newOrigin, mrpt::poses::CPose3D& pose);
1396  /**
1397  * Gets a pose whose XY plane corresponds to this, forcing an exact point
1398  * as its spatial coordinates.
1399  * \throw std::logic_error if the point is not inside the plane.
1400  */
1402  const TPoint3D& newOrigin, mrpt::poses::CPose3D& pose) const
1403  {
1404  TPlane p = *this;
1405  p.getAsPose3DForcingOrigin(newOrigin, pose);
1406  }
1407  /**
1408  * Gets a plane which contains these three points.
1409  * \throw std::logic_error if the points are linearly dependants.
1410  */
1411  TPlane(const TPoint3D& p1, const TPoint3D& p2, const TPoint3D& p3);
1412  /**
1413  * Gets a plane which contains this point and this line.
1414  * \throw std::logic_error if the point is inside the line.
1415  */
1416  TPlane(const TPoint3D& p1, const TLine3D& r2);
1417  /**
1418  * Gets a plane which contains the two lines.
1419  * \throw std::logic_error if the lines do not cross.
1420  */
1421  TPlane(const TLine3D& r1, const TLine3D& r2);
1422  /**
1423  * Fast default constructor. Initializes to garbage.
1424  */
1425  TPlane() {}
1426  /**
1427  * Constructor from plane coefficients.
1428  */
1429  inline TPlane(double A, double B, double C, double D)
1430  {
1431  coefs[0] = A;
1432  coefs[1] = B;
1433  coefs[2] = C;
1434  coefs[3] = D;
1435  }
1436  /**
1437  * Constructor from an array of coefficients.
1438  */
1439  inline TPlane(const double (&vec)[4])
1440  {
1441  for (size_t i = 0; i < 4; i++) coefs[i] = vec[i];
1442  }
1443 };
1444 
1446 
1447 /**
1448  * 2D polygon, inheriting from std::vector<TPoint2D>.
1449  * \sa TPolygon3D,TSegment2D,TLine2D,TPoint2D, CPolygon
1450  */
1451 class TPolygon2D : public std::vector<TPoint2D>
1452 {
1453  public:
1454  /** Distance to a point (always >=0) */
1455  double distance(const TPoint2D& point) const;
1456  /** Check whether a point is inside (or within geometryEpsilon of a polygon
1457  * edge). This works for concave or convex polygons. */
1458  bool contains(const TPoint2D& point) const;
1459  /** Gets as set of segments, instead of points. */
1460  void getAsSegmentList(std::vector<TSegment2D>& v) const;
1461  /** Projects into 3D space, zeroing the z. */
1462  void generate3DObject(TPolygon3D& p) const;
1463  /** Polygon's central point. */
1464  void getCenter(TPoint2D& p) const;
1465  /** Checks whether is convex. */
1466  bool isConvex() const;
1467  /** Erase repeated vertices. \sa removeRedundantVertices */
1468  void removeRepeatedVertices();
1469  /** Erase every redundant vertex from the polygon, saving space. \sa
1470  * removeRepeatedVertices */
1471  void removeRedundantVertices();
1472  /** Gets plot data, ready to use on a 2D plot. \sa
1473  * mrpt::gui::CDisplayWindowPlots */
1474  void getPlotData(std::vector<double>& x, std::vector<double>& y) const;
1475  /** Get polygon bounding box. \exception On empty polygon */
1476  void getBoundingBox(TPoint2D& min_coords, TPoint2D& max_coords) const;
1477  /** Default constructor */
1478  TPolygon2D() : std::vector<TPoint2D>() {}
1479  /** Constructor for a given number of vertices, intializing them as garbage.
1480  */
1481  explicit TPolygon2D(size_t N) : std::vector<TPoint2D>(N) {}
1482  /** Implicit constructor from a vector of 2D points */
1483  TPolygon2D(const std::vector<TPoint2D>& v) : std::vector<TPoint2D>(v) {}
1484  /** Constructor from a 3D object. */
1485  explicit TPolygon2D(const TPolygon3D& p);
1486  /** Static method to create a regular polygon, given its size and radius.
1487  * \throw std::logic_error if radius is near zero or the number of edges is
1488  * less than three.
1489  */
1490  static void createRegularPolygon(
1491  size_t numEdges, double radius, TPolygon2D& poly);
1492  /** Static method to create a regular polygon from its size and radius. The
1493  * center will correspond to the given pose.
1494  * \throw std::logic_error if radius is near zero or the number of edges is
1495  * less than three.
1496  */
1497  static inline void createRegularPolygon(
1498  size_t numEdges, double radius, TPolygon2D& poly,
1499  const mrpt::poses::CPose2D& pose);
1500 };
1501 
1502 /**
1503  * 3D polygon, inheriting from std::vector<TPoint3D>
1504  * \sa TPolygon2D,TSegment3D,TLine3D,TPlane,TPoint3D
1505  */
1506 class TPolygon3D : public std::vector<TPoint3D>
1507 {
1508  public:
1509  /** Distance to point (always >=0) */
1510  double distance(const TPoint3D& point) const;
1511  /** Check whether a point is inside (or within geometryEpsilon of a polygon
1512  * edge). This works for concave or convex polygons. */
1513  bool contains(const TPoint3D& point) const;
1514  /** Gets as set of segments, instead of set of points. */
1515  void getAsSegmentList(std::vector<TSegment3D>& v) const;
1516  /** Gets a plane which contains the polygon. Returns false if the polygon is
1517  * skew and cannot be fit inside a plane. */
1518  bool getPlane(TPlane& p) const;
1519  /** Gets the best fitting plane, disregarding whether the polygon actually
1520  * fits inside or not. \sa getBestFittingPlane */
1521  void getBestFittingPlane(TPlane& p) const;
1522  /** Projects into a 2D space, discarding the z. \sa getPlane,isSkew */
1523  inline void generate2DObject(TPolygon2D& p) const { p = TPolygon2D(*this); }
1524  /** Get polygon's central point. */
1525  void getCenter(TPoint3D& p) const;
1526  /** Check whether the polygon is skew. Returns true if there doesn't exist a
1527  * plane in which the polygon can fit. \sa getBestFittingPlane */
1528  bool isSkew() const;
1529  /** Remove polygon's repeated vertices. */
1530  void removeRepeatedVertices();
1531  /** Erase every redundant vertex, thus saving space. */
1532  void removeRedundantVertices();
1533  /** Default constructor. Creates a polygon with no vertices. */
1534  TPolygon3D() : std::vector<TPoint3D>() {}
1535  /** Constructor for a given size. Creates a polygon with a fixed number of
1536  * vertices, which are initialized to garbage. */
1537  explicit TPolygon3D(size_t N) : std::vector<TPoint3D>(N) {}
1538  /** Implicit constructor from a 3D points vector. */
1539  TPolygon3D(const std::vector<TPoint3D>& v) : std::vector<TPoint3D>(v) {}
1540  /** Constructor from a 2D object. Zeroes the z. */
1541  explicit TPolygon3D(const TPolygon2D& p);
1542  /** Static method to create a regular polygon, given its size and radius.
1543  * \throw std::logic_error if number of edges is less than three, or radius
1544  * is near zero. */
1545  static void createRegularPolygon(
1546  size_t numEdges, double radius, TPolygon3D& poly);
1547  /** Static method to create a regular polygon, given its size and radius.
1548  * The center will be located on the given pose.
1549  * \throw std::logic_error if number of edges is less than three, or radius
1550  * is near zero.
1551  */
1552  static inline void createRegularPolygon(
1553  size_t numEdges, double radius, TPolygon3D& poly,
1554  const mrpt::poses::CPose3D& pose);
1555 };
1556 
1557 /**
1558  * Object type identifier for TPoint2D or TPoint3D.
1559  * \sa TObject2D,TObject3D
1560  */
1561 const unsigned char GEOMETRIC_TYPE_POINT = 0;
1562 /**
1563  * Object type identifier for TSegment2D or TSegment3D.
1564  * \sa TObject2D,TObject3D
1565  */
1566 const unsigned char GEOMETRIC_TYPE_SEGMENT = 1;
1567 /**
1568  * Object type identifier for TLine2D or TLine3D.
1569  * \sa TObject2D,TObject3D
1570  */
1571 const unsigned char GEOMETRIC_TYPE_LINE = 2;
1572 /**
1573  * Object type identifier for TPolygon2D or TPolygon3D.
1574  * \sa TObject2D,TObject3D
1575  */
1576 const unsigned char GEOMETRIC_TYPE_POLYGON = 3;
1577 /**
1578  * Object type identifier for TPlane.
1579  * \sa TObject3D
1580  */
1581 const unsigned char GEOMETRIC_TYPE_PLANE = 4;
1582 /**
1583  * Object type identifier for empty TObject2D or TObject3D.
1584  * \sa TObject2D,TObject3D
1585  */
1586 const unsigned char GEOMETRIC_TYPE_UNDEFINED = 255;
1587 
1588 /**
1589  * Standard type for storing any lightweight 2D type. Do not inherit from this
1590  * class.
1591  * \sa TPoint2D,TSegment2D,TLine2D,TPolygon2D
1592  */
1594 {
1595  private:
1596  /**
1597  * Object type identifier.
1598  */
1599  unsigned char type;
1600  /**
1601  * Union type storing pointers to every allowed type.
1602  */
1604  {
1609 
1610  tobject2d_data_t() : polygon(nullptr) {}
1611  } data;
1612  /**
1613  * Destroys the object, releasing the pointer to the content (if any).
1614  */
1615  inline void destroy()
1616  {
1617  if (type == GEOMETRIC_TYPE_POLYGON) delete data.polygon;
1619  }
1620 
1621  public:
1622  /**
1623  * Implicit constructor from point.
1624  */
1626  {
1627  data.point = p;
1628  }
1629  /**
1630  * Implicit constructor from segment.
1631  */
1633  {
1634  data.segment = s;
1635  }
1636  /**
1637  * Implicit constructor from line.
1638  */
1640  {
1641  data.line = r;
1642  }
1643  /**
1644  * Implicit constructor from polygon.
1645  */
1647  {
1648  data.polygon = new TPolygon2D(p);
1649  }
1650  /**
1651  * Implicit constructor from polygon.
1652  */
1654  /**
1655  * Object destruction.
1656  */
1658  /**
1659  * Checks whether content is a point.
1660  */
1661  inline bool isPoint() const { return type == GEOMETRIC_TYPE_POINT; }
1662  /**
1663  * Checks whether content is a segment.
1664  */
1665  inline bool isSegment() const { return type == GEOMETRIC_TYPE_SEGMENT; }
1666  /**
1667  * Checks whether content is a line.
1668  */
1669  inline bool isLine() const { return type == GEOMETRIC_TYPE_LINE; }
1670  /**
1671  * Checks whether content is a polygon.
1672  */
1673  inline bool isPolygon() const { return type == GEOMETRIC_TYPE_POLYGON; }
1674  /**
1675  * Gets content type.
1676  */
1677  inline unsigned char getType() const { return type; }
1678  /**
1679  * Gets the content as a point, returning false if the type is inadequate.
1680  */
1681  inline bool getPoint(TPoint2D& p) const
1682  {
1683  if (isPoint())
1684  {
1685  p = data.point;
1686  return true;
1687  }
1688  else
1689  return false;
1690  }
1691  /**
1692  * Gets the content as a segment, returning false if the type is
1693  * inadequate.
1694  */
1695  inline bool getSegment(TSegment2D& s) const
1696  {
1697  if (isSegment())
1698  {
1699  s = data.segment;
1700  return true;
1701  }
1702  else
1703  return false;
1704  }
1705  /**
1706  * Gets the content as a line, returning false if the type is inadequate.
1707  */
1708  inline bool getLine(TLine2D& r) const
1709  {
1710  if (isLine())
1711  {
1712  r = data.line;
1713  return true;
1714  }
1715  else
1716  return false;
1717  }
1718  /**
1719  * Gets the content as a polygon, returning false if the type is
1720  * inadequate.
1721  */
1722  inline bool getPolygon(TPolygon2D& p) const
1723  {
1724  if (isPolygon())
1725  {
1726  p = *(data.polygon);
1727  return true;
1728  }
1729  else
1730  return false;
1731  }
1732  /**
1733  * Assign another TObject2D. Pointers are not shared.
1734  */
1736  {
1737  if (this == &obj) return *this;
1738  destroy();
1739  switch (type = obj.type)
1740  {
1741  case GEOMETRIC_TYPE_POINT:
1742  data.point = obj.data.point;
1743  break;
1745  data.segment = obj.data.segment;
1746  break;
1747  case GEOMETRIC_TYPE_LINE:
1748  data.line = obj.data.line;
1749  break;
1751  data.polygon = new TPolygon2D(*(obj.data.polygon));
1752  break;
1753  }
1754  return *this;
1755  }
1756  /**
1757  * Assign a point to this object.
1758  */
1759  inline void operator=(const TPoint2D& p)
1760  {
1761  destroy();
1763  data.point = p;
1764  }
1765  /**
1766  * Assign a segment to this object.
1767  */
1768  inline void operator=(const TSegment2D& s)
1769  {
1770  destroy();
1772  data.segment = s;
1773  }
1774  /**
1775  * Assign a line to this object.
1776  */
1777  inline void operator=(const TLine2D& l)
1778  {
1779  destroy();
1781  data.line = l;
1782  }
1783  /**
1784  * Assign a polygon to this object.
1785  */
1786  inline void operator=(const TPolygon2D& p)
1787  {
1788  destroy();
1790  data.polygon = new TPolygon2D(p);
1791  }
1792  /**
1793  * Project into 3D space.
1794  */
1795  void generate3DObject(TObject3D& obj) const;
1796  /**
1797  * Constructor from another TObject2D.
1798  */
1800  {
1801  operator=(obj);
1802  }
1803  /**
1804  * Static method to retrieve all the points in a vector of TObject2D.
1805  */
1806  static void getPoints(
1807  const std::vector<TObject2D>& objs, std::vector<TPoint2D>& pnts);
1808  /**
1809  * Static method to retrieve all the segments in a vector of TObject2D.
1810  */
1811  static void getSegments(
1812  const std::vector<TObject2D>& objs, std::vector<TSegment2D>& sgms);
1813  /**
1814  * Static method to retrieve all the lines in a vector of TObject2D.
1815  */
1816  static void getLines(
1817  const std::vector<TObject2D>& objs, std::vector<TLine2D>& lins);
1818  /**
1819  * Static method to retrieve all the polygons in a vector of TObject2D.
1820  */
1821  static void getPolygons(
1822  const std::vector<TObject2D>& objs, std::vector<TPolygon2D>& polys);
1823  /**
1824  * Static method to retrieve all the points in a vector of TObject2D,
1825  * returning the remainder objects in another parameter.
1826  */
1827  static void getPoints(
1828  const std::vector<TObject2D>& objs, std::vector<TPoint2D>& pnts,
1829  std::vector<TObject2D>& remainder);
1830  /**
1831  * Static method to retrieve all the segments in a vector of TObject2D,
1832  * returning the remainder objects in another parameter.
1833  */
1834  static void getSegments(
1835  const std::vector<TObject2D>& objs, std::vector<TSegment2D>& sgms,
1836  std::vector<TObject2D>& remainder);
1837  /**
1838  * Static method to retrieve all the lines in a vector of TObject2D,
1839  * returning the remainder objects in another parameter.
1840  */
1841  static void getLines(
1842  const std::vector<TObject2D>& objs, std::vector<TLine2D>& lins,
1843  std::vector<TObject2D>& remainder);
1844  /**
1845  * Static method to retrieve all the polygons in a vector of TObject2D,
1846  * returning the remainder objects in another parameter.
1847  */
1848  static void getPolygons(
1849  const std::vector<TObject2D>& objs, std::vector<TPolygon2D>& polys,
1850  std::vector<TObject2D>& remainder);
1851 };
1852 /**
1853  * Standard object for storing any 3D lightweight object. Do not inherit from
1854  * this class.
1855  * \sa TPoint3D,TSegment3D,TLine3D,TPlane,TPolygon3D
1856  */
1858 {
1859  private:
1860  /**
1861  * Object type identifier.
1862  */
1863  unsigned char type;
1864  /**
1865  * Union containing pointer to actual data.
1866  */
1868  {
1874 
1875  tobject3d_data_t() : polygon(nullptr) {}
1876  } data;
1877  /**
1878  * Destroys the object and releases the pointer, if any.
1879  */
1880  void destroy()
1881  {
1882  if (type == GEOMETRIC_TYPE_POLYGON) delete data.polygon;
1884  }
1885 
1886  public:
1887  /**
1888  * Constructor from point.
1889  */
1891  {
1892  data.point = p;
1893  }
1894  /**
1895  * Constructor from segment.
1896  */
1898  {
1899  data.segment = s;
1900  }
1901  /**
1902  * Constructor from line.
1903  */
1905  /**
1906  * Constructor from polygon.
1907  */
1909  {
1910  data.polygon = new TPolygon3D(p);
1911  }
1912  /**
1913  * Constructor from plane.
1914  */
1916  /**
1917  * Empty constructor.
1918  */
1920  /**
1921  * Destructor.
1922  */
1924  /**
1925  * Checks whether content is a point.
1926  */
1927  inline bool isPoint() const { return type == GEOMETRIC_TYPE_POINT; }
1928  /**
1929  * Checks whether content is a segment.
1930  */
1931  inline bool isSegment() const { return type == GEOMETRIC_TYPE_SEGMENT; }
1932  /**
1933  * Checks whether content is a line.
1934  */
1935  inline bool isLine() const { return type == GEOMETRIC_TYPE_LINE; }
1936  /**
1937  * Checks whether content is a polygon.
1938  */
1939  inline bool isPolygon() const { return type == GEOMETRIC_TYPE_POLYGON; }
1940  /**
1941  * Checks whether content is a plane.
1942  */
1943  inline bool isPlane() const { return type == GEOMETRIC_TYPE_PLANE; }
1944  /**
1945  * Gets object type.
1946  */
1947  inline unsigned char getType() const { return type; }
1948  /**
1949  * Gets the content as a point, returning false if the type is not
1950  * adequate.
1951  */
1952  inline bool getPoint(TPoint3D& p) const
1953  {
1954  if (isPoint())
1955  {
1956  p = data.point;
1957  return true;
1958  }
1959  else
1960  return false;
1961  }
1962  /**
1963  * Gets the content as a segment, returning false if the type is not
1964  * adequate.
1965  */
1966  inline bool getSegment(TSegment3D& s) const
1967  {
1968  if (isSegment())
1969  {
1970  s = data.segment;
1971  return true;
1972  }
1973  else
1974  return false;
1975  }
1976  /**
1977  * Gets the content as a line, returning false if the type is not adequate.
1978  */
1979  inline bool getLine(TLine3D& r) const
1980  {
1981  if (isLine())
1982  {
1983  r = data.line;
1984  return true;
1985  }
1986  else
1987  return false;
1988  }
1989  /**
1990  * Gets the content as a polygon, returning false if the type is not
1991  * adequate.
1992  */
1993  inline bool getPolygon(TPolygon3D& p) const
1994  {
1995  if (isPolygon())
1996  {
1997  p = *(data.polygon);
1998  return true;
1999  }
2000  else
2001  return false;
2002  }
2003  /**
2004  * Gets the content as a plane, returning false if the type is not
2005  * adequate.
2006  */
2007  inline bool getPlane(TPlane& p) const
2008  {
2009  if (isPlane())
2010  {
2011  p = data.plane;
2012  return true;
2013  }
2014  else
2015  return false;
2016  }
2017  /**
2018  * Assigns another object, creating a new pointer if needed.
2019  */
2021  {
2022  if (this == &obj) return *this;
2023  destroy();
2024  switch (type = obj.type)
2025  {
2026  case GEOMETRIC_TYPE_POINT:
2027  data.point = obj.data.point;
2028  break;
2030  data.segment = obj.data.segment;
2031  break;
2032  case GEOMETRIC_TYPE_LINE:
2033  data.line = obj.data.line;
2034  break;
2036  data.polygon = new TPolygon3D(*(obj.data.polygon));
2037  break;
2038  case GEOMETRIC_TYPE_PLANE:
2039  data.plane = obj.data.plane;
2040  break;
2042  break;
2043  default:
2044  THROW_EXCEPTION("Invalid TObject3D object");
2045  }
2046  return *this;
2047  }
2048  /**
2049  * Assigns a point to this object.
2050  */
2051  inline void operator=(const TPoint3D& p)
2052  {
2053  destroy();
2055  data.point = p;
2056  }
2057  /**
2058  * Assigns a segment to this object.
2059  */
2060  inline void operator=(const TSegment3D& s)
2061  {
2062  destroy();
2064  data.segment = s;
2065  }
2066  /**
2067  * Assigns a line to this object.
2068  */
2069  inline void operator=(const TLine3D& l)
2070  {
2071  destroy();
2073  data.line = l;
2074  }
2075  /**
2076  * Assigns a polygon to this object.
2077  */
2078  inline void operator=(const TPolygon3D& p)
2079  {
2080  destroy();
2082  data.polygon = new TPolygon3D(p);
2083  }
2084  /**
2085  * Assigns a plane to this object.
2086  */
2087  inline void operator=(const TPlane& p)
2088  {
2089  destroy();
2091  data.plane = p;
2092  }
2093  /**
2094  * Projects into 2D space.
2095  * \throw std::logic_error if the 3D object loses its properties when
2096  * projecting into 2D space (for example, it's a plane or a vertical line).
2097  */
2098  inline void generate2DObject(TObject2D& obj) const
2099  {
2100  switch (type)
2101  {
2102  case GEOMETRIC_TYPE_POINT:
2103  obj = TPoint2D(data.point);
2104  break;
2106  obj = TSegment2D(data.segment);
2107  break;
2108  case GEOMETRIC_TYPE_LINE:
2109  obj = TLine2D(data.line);
2110  break;
2112  obj = TPolygon2D(*(data.polygon));
2113  break;
2114  case GEOMETRIC_TYPE_PLANE:
2115  throw std::logic_error("Too many dimensions");
2116  default:
2117  obj = TObject2D();
2118  break;
2119  }
2120  }
2121  /**
2122  * Constructs from another object.
2123  */
2125  {
2126  operator=(obj);
2127  }
2128  /**
2129  * Static method to retrieve every point included in a vector of objects.
2130  */
2131  static void getPoints(
2132  const std::vector<TObject3D>& objs, std::vector<TPoint3D>& pnts);
2133  /**
2134  * Static method to retrieve every segment included in a vector of objects.
2135  */
2136  static void getSegments(
2137  const std::vector<TObject3D>& objs, std::vector<TSegment3D>& sgms);
2138  /**
2139  * Static method to retrieve every line included in a vector of objects.
2140  */
2141  static void getLines(
2142  const std::vector<TObject3D>& objs, std::vector<TLine3D>& lins);
2143  /**
2144  * Static method to retrieve every plane included in a vector of objects.
2145  */
2146  static void getPlanes(
2147  const std::vector<TObject3D>& objs, std::vector<TPlane>& plns);
2148  /**
2149  * Static method to retrieve every polygon included in a vector of objects.
2150  */
2151  static void getPolygons(
2152  const std::vector<TObject3D>& objs, std::vector<TPolygon3D>& polys);
2153  /**
2154  * Static method to retrieve every point included in a vector of objects,
2155  * returning the remaining objects in another argument.
2156  */
2157  static void getPoints(
2158  const std::vector<TObject3D>& objs, std::vector<TPoint3D>& pnts,
2159  std::vector<TObject3D>& remainder);
2160  /**
2161  * Static method to retrieve every segment included in a vector of objects,
2162  * returning the remaining objects in another argument.
2163  */
2164  static void getSegments(
2165  const std::vector<TObject3D>& objs, std::vector<TSegment3D>& sgms,
2166  std::vector<TObject3D>& remainder);
2167  /**
2168  * Static method to retrieve every line included in a vector of objects,
2169  * returning the remaining objects in another argument.
2170  */
2171  static void getLines(
2172  const std::vector<TObject3D>& objs, std::vector<TLine3D>& lins,
2173  std::vector<TObject3D>& remainder);
2174  /**
2175  * Static method to retrieve every plane included in a vector of objects,
2176  * returning the remaining objects in another argument.
2177  */
2178  static void getPlanes(
2179  const std::vector<TObject3D>& objs, std::vector<TPlane>& plns,
2180  std::vector<TObject3D>& remainder);
2181  /**
2182  * Static method to retrieve every polygon included in a vector of objects,
2183  * returning the remaining objects in another argument.
2184  */
2185  static void getPolygons(
2186  const std::vector<TObject3D>& objs, std::vector<TPolygon3D>& polys,
2187  std::vector<TObject3D>& remainder);
2188 };
2189 
2190 /** 2D twist: 2D velocity vector (vx,vy) + planar angular velocity (omega)
2191  * \sa mrpt::math::TTwist3D, mrpt::math::TPose2D
2192  */
2193 struct TTwist2D
2194 {
2195  enum
2196  {
2198  };
2199  /** Velocity components: X,Y (m/s) */
2200  double vx, vy;
2201  /** Angular velocity (rad/s) */
2202  double omega;
2203 
2204  /** Constructor from components */
2205  inline TTwist2D(double vx_, double vy_, double omega_)
2206  : vx(vx_), vy(vy_), omega(omega_)
2207  {
2208  }
2209  /** Default fast constructor. Initializes to garbage */
2210  inline TTwist2D() {}
2211  /** Coordinate access using operator[]. Order: vx,vy,vphi */
2212  inline double& operator[](size_t i)
2213  {
2214  switch (i)
2215  {
2216  case 0:
2217  return vx;
2218  case 1:
2219  return vy;
2220  case 2:
2221  return omega;
2222  default:
2223  throw std::out_of_range("index out of range");
2224  }
2225  }
2226  /** Coordinate access using operator[]. Order: vx,vy,vphi */
2227  inline const double& operator[](size_t i) const
2228  {
2229  switch (i)
2230  {
2231  case 0:
2232  return vx;
2233  case 1:
2234  return vy;
2235  case 2:
2236  return omega;
2237  default:
2238  throw std::out_of_range("index out of range");
2239  }
2240  }
2241  /** Transformation into vector */
2242  inline void getAsVector(std::vector<double>& v) const
2243  {
2244  v.resize(3);
2245  v[0] = vx;
2246  v[1] = vy;
2247  v[2] = omega;
2248  }
2249  /** Transform the (vx,vy) components for a counterclockwise rotation of
2250  * `ang` radians. */
2251  void rotate(const double ang);
2252  bool operator==(const TTwist2D& o) const;
2253  bool operator!=(const TTwist2D& o) const;
2254  /** Returns the pose increment of multiplying each twist component times
2255  * "dt" seconds. */
2256  mrpt::math::TPose2D operator*(const double dt) const;
2257  /** Returns a human-readable textual representation of the object (eg: "[vx
2258  * vy omega]", omega in deg/s)
2259  * \sa fromString
2260  */
2261  void asString(std::string& s) const;
2262  inline std::string asString() const
2263  {
2264  std::string s;
2265  asString(s);
2266  return s;
2267  }
2268 
2269  /** Set the current object value from a string generated by 'asString' (eg:
2270  * "[0.02 1.04 -45.0]" )
2271  * \sa asString
2272  * \exception std::exception On invalid format
2273  */
2274  void fromString(const std::string& s);
2275  static size_t size() { return 3; }
2276 };
2277 
2278 /** 3D twist: 3D velocity vector (vx,vy,vz) + angular velocity (wx,wy,wz)
2279  * \sa mrpt::math::TTwist2D, mrpt::math::TPose3D
2280  */
2281 struct TTwist3D
2282 {
2283  enum
2284  {
2286  };
2287  /** Velocity components: X,Y (m/s) */
2288  double vx, vy, vz;
2289  /** Angular velocity (rad/s) */
2290  double wx, wy, wz;
2291 
2292  /** Constructor from components */
2293  inline TTwist3D(
2294  double vx_, double vy_, double vz_, double wx_, double wy_, double wz_)
2295  : vx(vx_), vy(vy_), vz(vz_), wx(wx_), wy(wy_), wz(wz_)
2296  {
2297  }
2298  /** Default fast constructor. Initializes to garbage */
2299  inline TTwist3D() {}
2300  /** Coordinate access using operator[]. Order: vx,vy,vphi */
2301  inline double& operator[](size_t i)
2302  {
2303  switch (i)
2304  {
2305  case 0:
2306  return vx;
2307  case 1:
2308  return vy;
2309  case 2:
2310  return vz;
2311  case 3:
2312  return wx;
2313  case 4:
2314  return wy;
2315  case 5:
2316  return wz;
2317  default:
2318  throw std::out_of_range("index out of range");
2319  }
2320  }
2321  /** Coordinate access using operator[]. Order: vx,vy,vphi */
2322  inline const double& operator[](size_t i) const
2323  {
2324  switch (i)
2325  {
2326  case 0:
2327  return vx;
2328  case 1:
2329  return vy;
2330  case 2:
2331  return vz;
2332  case 3:
2333  return wx;
2334  case 4:
2335  return wy;
2336  case 5:
2337  return wz;
2338  default:
2339  throw std::out_of_range("index out of range");
2340  }
2341  }
2342  /** Transformation into vector */
2343  inline void getAsVector(std::vector<double>& v) const
2344  {
2345  v.resize(6);
2346  for (int i = 0; i < 6; i++) v[i] = (*this)[i];
2347  }
2348  bool operator==(const TTwist3D& o) const;
2349  bool operator!=(const TTwist3D& o) const;
2350  /** Returns a human-readable textual representation of the object (eg: "[vx
2351  * vy vz wx wy wz]", omegas in deg/s)
2352  * \sa fromString
2353  */
2354  void asString(std::string& s) const;
2355  inline std::string asString() const
2356  {
2357  std::string s;
2358  asString(s);
2359  return s;
2360  }
2361 
2362  /** Transform all 6 components for a change of reference frame from "A" to
2363  * another frame "B" whose rotation with respect to "A" is given by `rot`.
2364  * The translational part of the pose is ignored */
2365  void rotate(const mrpt::poses::CPose3D& rot);
2366 
2367  /** Set the current object value from a string generated by 'asString' (eg:
2368  * "[vx vy vz wx wy wz]" )
2369  * \sa asString
2370  * \exception std::exception On invalid format
2371  */
2372  void fromString(const std::string& s);
2373  static size_t size() { return 3; }
2374 };
2375 
2376 // Binary streaming functions
2381 
2386 
2390  mrpt::utils::CStream& out, const mrpt::math::TPose2D& o);
2391 
2395  mrpt::utils::CStream& out, const mrpt::math::TPose3D& o);
2396 
2401 
2405  mrpt::utils::CStream& out, const mrpt::math::TLine2D& l);
2406 
2411 
2416 
2420  mrpt::utils::CStream& out, const mrpt::math::TLine3D& l);
2421 
2426 
2431 
2436 
2441 
2442 /** @} */ // end of grouping
2443 
2444 } // end of namespace math
2445 
2446 namespace utils
2447 {
2448 // Specialization must occur in the same namespace
2464 
2465 } // end of namespace utils
2466 
2467 } // end of namespace
2468 #endif
void getCenter(TPoint2D &p) const
Segment&#39;s central point.
TSegment2D(const TPoint2D &p1, const TPoint2D &p2)
Constructor from both points.
TPoint2D & operator+=(const TPoint2D &p)
const double & operator[](size_t i) const
Coordinate access using operator[].
double & operator[](size_t i)
Coordinate access using operator[].
const unsigned char GEOMETRIC_TYPE_PLANE
Object type identifier for TPlane.
static void getSegments(const std::vector< TObject2D > &objs, std::vector< TSegment2D > &sgms)
Static method to retrieve all the segments in a vector of TObject2D.
bool getPoint(TPoint2D &p) const
Gets the content as a point, returning false if the type is inadequate.
A pair (x,y) of pixel coordinates (subpixel resolution).
Definition: TPixelCoord.h:21
void getUnitaryNormalVector(double(&vector)[2])
Get line&#39;s normal vector after unitarizing line.
static void createRegularPolygon(size_t numEdges, double radius, TPolygon3D &poly)
Static method to create a regular polygon, given its size and radius.
static void createRegularPolygon(size_t numEdges, double radius, TPolygon2D &poly)
Static method to create a regular polygon, given its size and radius.
bool operator==(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04]" ) ...
TPolygon3D(const std::vector< TPoint3D > &v)
Implicit constructor from a 3D points vector.
TPoint2D operator/(double d) const
#define MRPT_DECLARE_TTYPENAME_NAMESPACE(_TYPE, __NS)
Definition: TTypeName.h:79
TPointXYZIu8(double x, double y, double z, uint8_t intensity_val)
bool getPolygon(TPolygon2D &p) const
Gets the content as a polygon, returning false if the type is inadequate.
double norm() const
Returns the norm of the (x,y) vector (phi is not used)
void rotate(const double ang)
Transform the (vx,vy) components for a counterclockwise rotation of ang radians.
TPoint2D & operator*=(double d)
const unsigned char GEOMETRIC_TYPE_LINE
Object type identifier for TLine2D or TLine3D.
void asString(std::string &s) const
Returns a human-readable textual representation of the object (eg: "[0.02 1.04]" ) ...
GLdouble GLdouble z
Definition: glext.h:3872
TPointXYZfIu8(float x, float y, float z, uint8_t intensity_val)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
unsigned char getType() const
Gets content type.
double x
X,Y coordinates.
bool isPolygon() const
Checks whether content is a polygon.
void destroy()
Destroys the object and releases the pointer, if any.
const unsigned char GEOMETRIC_TYPE_POINT
Object type identifier for TPoint2D or TPoint3D.
void composePoint(const TPoint2D l, TPoint2D &g) const
bool operator==(const TTwist3D &o) const
double x
X,Y coordinates.
double distance(const TPoint3D &point) const
Distance between the line and a point.
void getPlotData(std::vector< double > &x, std::vector< double > &y) const
Gets plot data, ready to use on a 2D plot.
TObject2D(const TPoint2D &p)
Implicit constructor from point.
TPoint3Df(const float xx, const float yy, const float zz)
bool isSkew() const
Check whether the polygon is skew.
TLine2D()
Fast default constructor.
bool getSegment(TSegment3D &s) const
Gets the content as a segment, returning false if the type is not adequate.
bool contains(const TPoint2D &point) const
Check whether a point is inside a segment.
bool contains(const TSegment3D &segment) const
Check whether a segment is fully contained into the plane.
double roll
Roll coordinate (rotation angle over X coordinate).
TPoint3D(double xx, double yy, double zz)
Constructor from coordinates.
static void getPolygons(const std::vector< TObject2D > &objs, std::vector< TPolygon2D > &polys)
Static method to retrieve all the polygons in a vector of TObject2D.
TPointXYZfRGBu8(float x, float y, float z, uint8_t R_val, uint8_t G_val, uint8_t B_val)
TPolygon3D()
Default constructor.
#define THROW_EXCEPTION(msg)
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3721
bool operator!=(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
double distance(const TPoint2D &point) const
Distance to point.
static void getLines(const std::vector< TObject2D > &objs, std::vector< TLine2D > &lins)
Static method to retrieve all the lines in a vector of TObject2D.
void operator=(const TPoint2D &p)
Assign a point to this object.
double x
X,Y,Z, coords.
double norm() const
Point norm.
const float & operator[](size_t i) const
Coordinate access using operator[].
void operator=(const TSegment2D &s)
Assign a segment to this object.
std::string asString() const
Union containing pointer to actual data.
TTwist3D()
Default fast constructor.
void getAsQuaternion(mrpt::math::CQuaternion< double > &q, mrpt::math::CMatrixFixedNumeric< double, 4, 3 > *out_dq_dr=NULL) const
Returns the quaternion associated to the rotation of this object (NOTE: XYZ translation is ignored) ...
TPoint3D()
Default fast constructor.
TPoint3D operator+(const TPoint3D &p) const
Points addition.
double signedDistance(const TPoint2D &point) const
Distance with sign from a given point (sign indicates side).
TPoint3Df & operator+=(const TPoint3Df &p)
Standard type for storing any lightweight 2D type.
TLine3D()
Fast default constructor.
bool getLine(TLine2D &r) const
Gets the content as a line, returning false if the type is inadequate.
TObject2D()
Implicit constructor from polygon.
void getAsPose3D(mrpt::poses::CPose3D &outPose)
Gets a pose whose XY plane corresponds to this plane.
void getAsSegmentList(std::vector< TSegment2D > &v) const
Gets as set of segments, instead of points.
bool getPlane(TPlane &p) const
Gets the content as a plane, returning false if the type is not adequate.
TPoint3D pBase
Base point.
Standard object for storing any 3D lightweight object.
STL namespace.
bool getSegment(TSegment2D &s) const
Gets the content as a segment, returning false if the type is inadequate.
bool contains(const TPoint2D &point) const
Check whether a point is inside the line.
TSegment3D()
Fast default constructor.
void removeRepeatedVertices()
Erase repeated vertices.
double signedDistance(const TPoint2D &point) const
Distance with sign to point (sign indicates which side the point is).
void generate3DObject(TLine3D &l) const
Project into 3D space, setting the z to 0.
TObject3D()
Empty constructor.
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04 -45...
double distance(const TPoint3D &point) const
Distance to 3D point.
TPoint3D & operator+=(const TPoint3D &p)
Translation.
double yaw
Yaw coordinate (rotation angle over Z axis).
void operator=(const TSegment3D &s)
Assigns a segment to this object.
TTwist2D(double vx_, double vy_, double omega_)
Constructor from components.
GLdouble s
Definition: glext.h:3676
void operator=(const TPoint3D &p)
Assigns a point to this object.
TPose2D(double xx, double yy, double pphi)
Constructor from coordinates.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
TSegment3D(const TSegment2D &s)
Constructor from 2D object.
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[vx vy vz wx wy wz]" ) ...
double & operator[](size_t i)
Coordinate access using operator[].
double norm() const
Pose&#39;s spatial coordinates norm.
bool operator<(const TSegment3D &s) const
void getUnitaryDirectorVector(double(&vector)[2])
Unitarize line and then get director vector.
double & operator[](size_t i)
Coordinate access using operator[].
3D twist: 3D velocity vector (vx,vy,vz) + angular velocity (wx,wy,wz)
TPoint2D(const mrpt::poses::CPoseOrPoint< DERIVEDCLASS > &p)
Constructor from CPoseOrPoint, perhaps losing 3D information.
TObject3D(const TPlane &p)
Constructor from plane.
TPoint3D point1
Origin point.
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
void getUnitaryDirectorVector(double(&vector)[3])
Unitarize and then get director vector.
static void getSegments(const std::vector< TObject3D > &objs, std::vector< TSegment3D > &sgms)
Static method to retrieve every segment included in a vector of objects.
unsigned char uint8_t
Definition: rptypes.h:41
void generate2DObject(TLine2D &l) const
Project into 2D space, discarding the Z coordinate.
TPose3D()
Default fast constructor.
2D twist: 2D velocity vector (vx,vy) + planar angular velocity (omega)
void getNormalVector(double(&vector)[2]) const
Get line&#39;s normal vector.
void generate3DObject(TPolygon3D &p) const
Projects into 3D space, zeroing the z.
bool contains(const TPoint3D &point) const
Check whether a point is inside the line.
TPointXYZRGBu8(double x, double y, double z, uint8_t R_val, uint8_t G_val, uint8_t B_val)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
void unitarize()
Unitarize line&#39;s normal vector.
std::string asString() const
void getBoundingBox(TPoint2D &min_coords, TPoint2D &max_coords) const
Get polygon bounding box.
A numeric matrix of compile-time fixed size.
bool getPolygon(TPolygon3D &p) const
Gets the content as a polygon, returning false if the type is not adequate.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
2D segment, consisting of two points.
bool isPoint() const
Checks whether content is a point.
double distance(const TPoint3D &point) const
Distance to point.
struct mrpt::math::TObject2D::tobject2d_data_t data
TPlane(const double(&vec)[4])
Constructor from an array of coefficients.
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04 -45...
3D segment, consisting of two points.
double length() const
Segment length.
TObject2D(const TLine2D &r)
Implicit constructor from line.
Lightweight 3D point (float version).
TObject3D(const TPoint3D &p)
Constructor from point.
TPoint2D(double xx, double yy)
Constructor from coordinates.
const double & operator[](size_t i) const
Coordinate access using operator[].
TPoint3D point2
Destiny point.
bool operator<(const TSegment2D &s) const
float & operator[](size_t i)
Coordinate access using operator[].
std::string asString() const
std::ostream & operator<<(std::ostream &o, const TPoint2D &p)
double vx
Velocity components: X,Y (m/s)
double distanceTo(const TPoint3D &p) const
Point-to-point distance.
TObject3D & operator=(const TObject3D &obj)
Assigns another object, creating a new pointer if needed.
mrpt::math::TPose2D operator+(const mrpt::math::TPose2D &b) const
Operator "oplus" pose composition: "ret=this \oplus b".
TLine2D(double A, double B, double C)
Constructor from line&#39;s coefficients.
void rotate(const mrpt::poses::CPose3D &rot)
Transform all 6 components for a change of reference frame from "A" to another frame "B" whose rotati...
static void getLines(const std::vector< TObject3D > &objs, std::vector< TLine3D > &lins)
Static method to retrieve every line included in a vector of objects.
const double & operator[](size_t i) const
Coordinate access using operator[].
void unitarize()
Unitarize normal vector.
mrpt::math::TPose2D operator*(const double dt) const
Returns the pose increment of multiplying each twist component times "dt" seconds.
double x
Translation in x,y,z.
const unsigned char GEOMETRIC_TYPE_SEGMENT
Object type identifier for TSegment2D or TSegment3D.
const TPoint3D & operator[](size_t i) const
Access to points using operator[0-1].
TPoint3D operator/(double d) const
XYZ point (float) + RGB(u8)
T wrapTo2Pi(T a)
Modifies the given angle to translate it into the [0,2pi[ range.
Definition: wrap2pi.h:41
void generate2DObject(TPolygon2D &p) const
Projects into a 2D space, discarding the z.
static void getPolygons(const std::vector< TObject3D > &objs, std::vector< TPolygon3D > &polys)
Static method to retrieve every polygon included in a vector of objects.
GLubyte g
Definition: glext.h:6279
3D Plane, represented by its equation
void getDirectorVector(double(&vector)[3]) const
Get director vector.
bool getPlane(TPlane &p) const
Gets a plane which contains the polygon.
GLubyte GLubyte b
Definition: glext.h:6279
double coefs[3]
Line coefficients, stored as an array: .
TPoint2D operator*(double d) const
TPolygon2D()
Default constructor.
double x
X,Y,Z coordinates.
void operator=(const TPolygon3D &p)
Assigns a polygon to this object.
void removeRedundantVertices()
Erase every redundant vertex, thus saving space.
void getCenter(TPoint3D &p) const
Segment&#39;s central point.
TPoint3D operator*(double d) const
unsigned char type
Object type identifier.
TPose3DQuat()
Default fast constructor.
TPoint2D point2
Destiny point.
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
Definition: CPose3DQuat.h:48
bool isSegment() const
Checks whether content is a segment.
XYZ point (float) + Intensity(u8)
TObject2D(const TObject2D &obj)
Constructor from another TObject2D.
The base template class for 2D & 3D points and poses.
Definition: CPoseOrPoint.h:125
TPoint2D & operator[](size_t i)
Access to points using operator[0-1].
void getCenter(TPoint2D &p) const
Polygon&#39;s central point.
void getAsVector(std::vector< double > &v) const
Gets the pose as a vector of doubles.
TObject2D & operator=(const TObject2D &obj)
Assign another TObject2D.
bool isPlane() const
Checks whether content is a plane.
GLsizei const GLchar ** string
Definition: glext.h:4101
double qr
Unit quaternion part, qr,qx,qy,qz.
A class used to store a 2D point.
Definition: CPoint2D.h:36
A class used to store a 3D point.
Definition: CPoint3D.h:32
void unitarize()
Unitarize director vector.
void operator=(const TPlane &p)
Assigns a plane to this object.
TObject2D(const TSegment2D &s)
Implicit constructor from segment.
TPolygon2D(size_t N)
Constructor for a given number of vertices, intializing them as garbage.
bool contains(const TPoint3D &point) const
Check whether a point is inside (or within geometryEpsilon of a polygon edge).
double & operator[](size_t i)
Coordinate access using operator[].
bool getPoint(TPoint3D &p) const
Gets the content as a point, returning false if the type is not adequate.
double pitch
Pitch coordinate (rotation angle over Y axis).
Lightweight 3D pose (three spatial coordinates, plus a quaternion ).
double distance(const TPoint3D &point) const
Distance to point (always >=0)
double director[3]
Director vector.
void getAsPose3DForcingOrigin(const TPoint3D &newOrigin, mrpt::poses::CPose3D &pose) const
Gets a pose whose XY plane corresponds to this, forcing an exact point as its spatial coordinates...
TPoint2D point1
Origin point.
double norm() const
Point norm.
bool isConvex() const
Checks whether is convex.
TPoint2D()
Default fast constructor.
mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CMatrix::Ptr &pObj)
void generate3DObject(TSegment3D &s) const
Project into 3D space, setting the z to 0.
bool contains(const TPoint2D &point) const
Check whether a point is inside (or within geometryEpsilon of a polygon edge).
TPoint3D & operator*=(const double f)
Point scale.
double distance(const TPoint2D &point) const
Distance to a point (always >=0)
void getAsPose2D(mrpt::poses::CPose2D &outPose) const
Get a pose2D whose X axis corresponds to the line.
TPoint2D operator+(const TPoint2D &p) const
TObject3D(const TLine3D &r)
Constructor from line.
const GLdouble * v
Definition: glext.h:3678
bool isLine() const
Checks whether content is a line.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const double & operator[](size_t i) const
Coordinate access using operator[].
TPlane()
Fast default constructor.
void asString(std::string &s) const
Returns a human-readable textual representation of the object (eg: "[0.02 1.04 -0.8]" )
double wx
Angular velocity (rad/s)
std::string asString() const
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
double vx
Velocity components: X,Y (m/s)
TPose3DQuat(double _x, double _y, double _z, double _qr, double _qx, double _qy, double _qz)
Constructor from coordinates.
void generate3DObject(TObject3D &obj) const
Project into 3D space.
void operator=(const TPolygon2D &p)
Assign a polygon to this object.
TPoint3D operator-(const TPoint3D &p) const
Points substraction.
XYZ point (double) + RGB(u8)
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
TPoint2D operator-(const TPoint2D &p) const
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
double sqrDistanceTo(const TPoint3D &p) const
Point-to-point distance, squared.
static void getPoints(const std::vector< TObject2D > &objs, std::vector< TPoint2D > &pnts)
Static method to retrieve all the points in a vector of TObject2D.
void getUnitaryNormalVector(double(&vec)[3])
Unitarize, then get normal vector.
double norm() const
Pose&#39;s spatial coordinates norm.
double coefs[4]
Plane coefficients, stored as an array: .
TSegment2D()
Fast default constructor.
unsigned char type
Object type identifier.
~TObject2D()
Object destruction.
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04 -0...
struct mrpt::math::TObject3D::tobject3d_data_t data
bool operator!=(const TTwist3D &o) const
const TPoint2D & operator[](size_t i) const
Access to points using operator[0-1].
TPoint3D & operator[](size_t i)
Access to points using operator[0-1].
T hypot_fast(const T x, const T y)
Faster version of std::hypot(), to use when overflow is not an issue and we prefer fast code...
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
GLuint in
Definition: glext.h:7274
Lightweight 2D pose.
void removeRedundantVertices()
Erase every redundant vertex from the polygon, saving space.
void removeRepeatedVertices()
Remove polygon&#39;s repeated vertices.
bool contains(const TPoint3D &point) const
Check whether a point is inside the segment.
bool getLine(TLine3D &r) const
Gets the content as a line, returning false if the type is not adequate.
void getAsVector(std::vector< double > &v) const
Transformation into vector.
double evaluatePoint(const TPoint3D &point) const
Evaluate a point in the plane&#39;s equation.
const double & operator[](size_t i) const
Coordinate access using operator[].
std::string asString() const
void generate2DObject(TSegment2D &s) const
Projection into 2D space, discarding the z.
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04 -0...
void generate2DObject(TObject2D &obj) const
Projects into 2D space.
const double & operator[](size_t i) const
Coordinate access using operator[].
bool isLine() const
Checks whether content is a line.
bool operator<(const TPoint3D &p) const
double evaluatePoint(const TPoint2D &point) const
Evaluate point in the line&#39;s equation.
bool isSegment() const
Checks whether content is a segment.
GLenum GLint GLint y
Definition: glext.h:3538
TObject2D(const TPolygon2D &p)
Implicit constructor from polygon.
TTwist3D(double vx_, double vy_, double vz_, double wx_, double wy_, double wz_)
Constructor from components.
TSegment3D(const TPoint3D &p1, const TPoint3D &p2)
Constructor from both points.
void getAsPose2DForcingOrigin(const TPoint2D &origin, mrpt::poses::CPose2D &outPose) const
Get a pose2D whose X axis corresponds to the line, forcing the base point to one given.
void getAsPose3D(mrpt::poses::CPose3D &outPose) const
Gets a pose whose XY plane corresponds to this plane.
TObject3D(const TObject3D &obj)
Constructs from another object.
TPose2D()
Default fast constructor.
unsigned char getType() const
Gets object type.
const unsigned char GEOMETRIC_TYPE_UNDEFINED
Object type identifier for empty TObject2D or TObject3D.
TPoint2D & operator-=(const TPoint2D &p)
mrpt::math::TPose2D operator-(const mrpt::math::TPose2D &b) const
Operator "ominus" pose composition: "ret=this \ominus b".
void operator=(const TLine3D &l)
Assigns a line to this object.
double & operator[](size_t i)
Coordinate access using operator[].
GLenum GLint x
Definition: glext.h:3538
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ...
Definition: CQuaternion.h:46
void inverseComposePoint(const TPoint2D g, TPoint2D &l) const
void getAsVector(std::vector< double > &v) const
Transformation into vector.
Lightweight 3D point.
TPolygon3D(size_t N)
Constructor for a given size.
void asString(std::string &s) const
Returns a human-readable textual representation of the object as "[x y z qr qx qy qz]"...
void operator=(const TLine2D &l)
Assign a line to this object.
double distance(const TPoint2D &point) const
Distance from a given point.
void getAsSegmentList(std::vector< TSegment3D > &v) const
Gets as set of segments, instead of set of points.
bool isPolygon() const
Checks whether content is a polygon.
void getBestFittingPlane(TPlane &p) const
Gets the best fitting plane, disregarding whether the polygon actually fits inside or not...
void getDirectorVector(double(&vector)[2]) const
Get line&#39;s director vector.
void destroy()
Destroys the object, releasing the pointer to the content (if any).
TObject3D(const TSegment3D &s)
Constructor from segment.
std::string asString() const
TTwist2D()
Default fast constructor.
Lightweight 2D point.
const unsigned char GEOMETRIC_TYPE_POLYGON
Object type identifier for TPolygon2D or TPolygon3D.
void getAsPose3DForcingOrigin(const TPoint3D &newOrigin, mrpt::poses::CPose3D &pose)
Gets a pose whose XY plane corresponds to this, forcing an exact point as its spatial coordinates...
TPose3D(double _x, double _y, double _z, double _yaw, double _pitch, double _roll)
Constructor from coordinates.
XYZ point (double) + Intensity(u8)
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
std::string asString() const
TPoint3D operator-(const TPoint3D &p1)
Unary minus operator for 3D points.
void getAsVector(std::vector< double > &v) const
Gets the pose as a vector of doubles.
void getAsVector(std::vector< double > &v) const
Transformation into vector.
GLfloat GLfloat p
Definition: glext.h:6305
TPlane(double A, double B, double C, double D)
Constructor from plane coefficients.
static void getPlanes(const std::vector< TObject3D > &objs, std::vector< TPlane > &plns)
Static method to retrieve every plane included in a vector of objects.
TObject3D(const TPolygon3D &p)
Constructor from polygon.
double phi
Orientation (rads)
bool operator<(const TPoint2D &p) const
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528
double & operator[](size_t i)
Coordinate access using operator[].
void getAsVector(VECTORLIKE &v) const
Transformation into vector.
2D polygon, inheriting from std::vector<TPoint2D>.
TPoint3D & operator-=(const TPoint3D &p)
Difference between points.
3D polygon, inheriting from std::vector<TPoint3D>
Union type storing pointers to every allowed type.
void getCenter(TPoint3D &p) const
Get polygon&#39;s central point.
double omega
Angular velocity (rad/s)
bool contains(const TPoint3D &point) const
Check whether a point is contained into the plane.
double length() const
Segment length.
void getAsVector(std::vector< double > &v) const
Transformation into vector.
double & operator[](size_t i)
Coordinate access using operator[].
TPoint3D(const TPoint3Df &p)
Explicit constructor from coordinates.
TPoint2D(const mrpt::utils::TPixelCoordf &p)
Implicit transformation constructor from TPixelCoordf.
bool isPoint() const
Checks whether content is a point.
bool operator!=(const TTwist2D &o) const
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04 -0...
const double & operator[](size_t i) const
Coordinate access using operator[].
bool operator==(const TTwist2D &o) const
TPoint2D & operator/=(double d)
TPoint3Df operator*(const float s)
void getNormalVector(double(&vec)[3]) const
Get plane&#39;s normal vector.
3D line, represented by a base point and a director vector.
TPolygon2D(const std::vector< TPoint2D > &v)
Implicit constructor from a vector of 2D points.
2D line without bounds, represented by its equation .
static void getPoints(const std::vector< TObject3D > &objs, std::vector< TPoint3D > &pnts)
Static method to retrieve every point included in a vector of objects.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019