MRPT  2.0.1
TObject2D.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/math/TLine2D.h>
12 #include <mrpt/math/TPoint2D.h>
13 #include <mrpt/math/TPolygon2D.h>
14 #include <mrpt/math/TPoseOrPoint.h>
15 #include <mrpt/math/TSegment2D.h>
16 
17 namespace mrpt::math
18 {
19 /**
20  * Standard type for storing any lightweight 2D type. Do not inherit from this
21  * class.
22  * \sa TPoint2D,TSegment2D,TLine2D,TPolygon2D
23  */
24 struct TObject2D
25 {
26  private:
27  /**
28  * Object type identifier.
29  */
31  /**
32  * Union type storing pointers to every allowed type.
33  */
35  {
39  TPolygon2D* polygon{nullptr};
40 
41  tobject2d_data_t() = default;
42  } data;
43  /**
44  * Destroys the object, releasing the pointer to the content (if any).
45  */
46  void destroy()
47  {
48  if (type == GEOMETRIC_TYPE_POLYGON) delete data.polygon;
50  }
51 
52  public:
53  /**
54  * Implicit constructor from point.
55  */
57  {
58  data.point = p;
59  }
60  /**
61  * Implicit constructor from segment.
62  */
64  {
65  data.segment = s;
66  }
67  /**
68  * Implicit constructor from line.
69  */
71  /**
72  * Implicit constructor from polygon.
73  */
75  {
76  data.polygon = new TPolygon2D(p);
77  }
78  /**
79  * Implicit constructor from polygon.
80  */
82  /**
83  * Object destruction.
84  */
86  /**
87  * Checks whether content is a point.
88  */
89  bool isPoint() const { return type == GEOMETRIC_TYPE_POINT; }
90  /**
91  * Checks whether content is a segment.
92  */
93  bool isSegment() const { return type == GEOMETRIC_TYPE_SEGMENT; }
94  /**
95  * Checks whether content is a line.
96  */
97  bool isLine() const { return type == GEOMETRIC_TYPE_LINE; }
98  /**
99  * Checks whether content is a polygon.
100  */
101  bool isPolygon() const { return type == GEOMETRIC_TYPE_POLYGON; }
102  /**
103  * Gets content type.
104  */
105  unsigned char getType() const { return type; }
106  /**
107  * Gets the content as a point, returning false if the type is inadequate.
108  */
109  bool getPoint(TPoint2D& p) const
110  {
111  if (isPoint())
112  {
113  p = data.point;
114  return true;
115  }
116  else
117  return false;
118  }
119  /**
120  * Gets the content as a segment, returning false if the type is
121  * inadequate.
122  */
123  bool getSegment(TSegment2D& s) const
124  {
125  if (isSegment())
126  {
127  s = data.segment;
128  return true;
129  }
130  else
131  return false;
132  }
133  /**
134  * Gets the content as a line, returning false if the type is inadequate.
135  */
136  bool getLine(TLine2D& r) const
137  {
138  if (isLine())
139  {
140  r = data.line;
141  return true;
142  }
143  else
144  return false;
145  }
146  /**
147  * Gets the content as a polygon, returning false if the type is
148  * inadequate.
149  */
150  bool getPolygon(TPolygon2D& p) const
151  {
152  if (isPolygon())
153  {
154  p = *(data.polygon);
155  return true;
156  }
157  else
158  return false;
159  }
160  /**
161  * Assign another TObject2D. Pointers are not shared.
162  */
164  {
165  if (this == &obj) return *this;
166  destroy();
167  switch (type = obj.type)
168  {
170  data.point = obj.data.point;
171  break;
173  data.segment = obj.data.segment;
174  break;
175  case GEOMETRIC_TYPE_LINE:
176  data.line = obj.data.line;
177  break;
179  data.polygon = new TPolygon2D(*(obj.data.polygon));
180  break;
181  }
182  return *this;
183  }
184  /**
185  * Assign a point to this object.
186  */
187  void operator=(const TPoint2D& p)
188  {
189  destroy();
191  data.point = p;
192  }
193  /**
194  * Assign a segment to this object.
195  */
196  void operator=(const TSegment2D& s)
197  {
198  destroy();
200  data.segment = s;
201  }
202  /**
203  * Assign a line to this object.
204  */
205  void operator=(const TLine2D& l)
206  {
207  destroy();
209  data.line = l;
210  }
211  /**
212  * Assign a polygon to this object.
213  */
214  void operator=(const TPolygon2D& p)
215  {
216  destroy();
218  data.polygon = new TPolygon2D(p);
219  }
220  /**
221  * Project into 3D space.
222  */
223  void generate3DObject(TObject3D& obj) const;
224  /**
225  * Constructor from another TObject2D.
226  */
228  {
229  operator=(obj);
230  }
231  /**
232  * Static method to retrieve all the points in a vector of TObject2D.
233  */
234  static void getPoints(
235  const std::vector<TObject2D>& objs, std::vector<TPoint2D>& pnts);
236  /**
237  * Static method to retrieve all the segments in a vector of TObject2D.
238  */
239  static void getSegments(
240  const std::vector<TObject2D>& objs, std::vector<TSegment2D>& sgms);
241  /**
242  * Static method to retrieve all the lines in a vector of TObject2D.
243  */
244  static void getLines(
245  const std::vector<TObject2D>& objs, std::vector<TLine2D>& lins);
246  /**
247  * Static method to retrieve all the polygons in a vector of TObject2D.
248  */
249  static void getPolygons(
250  const std::vector<TObject2D>& objs, std::vector<TPolygon2D>& polys);
251  /**
252  * Static method to retrieve all the points in a vector of TObject2D,
253  * returning the remainder objects in another parameter.
254  */
255  static void getPoints(
256  const std::vector<TObject2D>& objs, std::vector<TPoint2D>& pnts,
257  std::vector<TObject2D>& remainder);
258  /**
259  * Static method to retrieve all the segments in a vector of TObject2D,
260  * returning the remainder objects in another parameter.
261  */
262  static void getSegments(
263  const std::vector<TObject2D>& objs, std::vector<TSegment2D>& sgms,
264  std::vector<TObject2D>& remainder);
265  /**
266  * Static method to retrieve all the lines in a vector of TObject2D,
267  * returning the remainder objects in another parameter.
268  */
269  static void getLines(
270  const std::vector<TObject2D>& objs, std::vector<TLine2D>& lins,
271  std::vector<TObject2D>& remainder);
272  /**
273  * Static method to retrieve all the polygons in a vector of TObject2D,
274  * returning the remainder objects in another parameter.
275  */
276  static void getPolygons(
277  const std::vector<TObject2D>& objs, std::vector<TPolygon2D>& polys,
278  std::vector<TObject2D>& remainder);
279 };
280 
285 
286 } // namespace mrpt::math
287 
288 namespace mrpt::typemeta
289 {
290 // Specialization must occur in the same namespace
292 } // namespace mrpt::typemeta
bool getPoint(TPoint2D &p) const
Gets the content as a point, returning false if the type is inadequate.
Definition: TObject2D.h:109
bool getPolygon(TPolygon2D &p) const
Gets the content as a polygon, returning false if the type is inadequate.
Definition: TObject2D.h:150
unsigned char getType() const
Gets content type.
Definition: TObject2D.h:105
bool isPolygon() const
Checks whether content is a polygon.
Definition: TObject2D.h:101
static constexpr unsigned char GEOMETRIC_TYPE_POLYGON
Object type identifier for TPolygon2D or TPolygon3D.
Definition: TPoseOrPoint.h:125
TObject2D(const TPoint2D &p)
Implicit constructor from point.
Definition: TObject2D.h:56
static void getLines(const std::vector< TObject2D > &objs, std::vector< TLine2D > &lins)
Static method to retrieve all the lines in a vector of TObject2D.
Definition: TObject2D.cpp:57
void operator=(const TPoint2D &p)
Assign a point to this object.
Definition: TObject2D.h:187
void operator=(const TSegment2D &s)
Assign a segment to this object.
Definition: TObject2D.h:196
Standard type for storing any lightweight 2D type.
Definition: TObject2D.h:24
bool getLine(TLine2D &r) const
Gets the content as a line, returning false if the type is inadequate.
Definition: TObject2D.h:136
TObject2D()
Implicit constructor from polygon.
Definition: TObject2D.h:81
static constexpr unsigned char GEOMETRIC_TYPE_POINT
Object type identifier for TPoint2D or TPoint3D.
Definition: TPoseOrPoint.h:110
Standard object for storing any 3D lightweight object.
Definition: TObject3D.h:25
bool getSegment(TSegment2D &s) const
Gets the content as a segment, returning false if the type is inadequate.
Definition: TObject2D.h:123
static void getSegments(const std::vector< TObject2D > &objs, std::vector< TSegment2D > &sgms)
Static method to retrieve all the segments in a vector of TObject2D.
Definition: TObject2D.cpp:51
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrixD::Ptr &pObj)
This base provides a set of functions for maths stuff.
2D segment, consisting of two points.
Definition: TSegment2D.h:20
struct mrpt::math::TObject2D::tobject2d_data_t data
TObject2D(const TLine2D &r)
Implicit constructor from line.
Definition: TObject2D.h:70
void generate3DObject(TObject3D &obj) const
Project into 3D space.
Definition: TObject2D.cpp:24
#define MRPT_DECLARE_TTYPENAME_NO_NAMESPACE(_TYPE, __NS)
Declares a typename to be "type" (without the NS prefix)
Definition: TTypeName.h:128
unsigned char type
Object type identifier.
Definition: TObject2D.h:30
bool isSegment() const
Checks whether content is a segment.
Definition: TObject2D.h:93
TObject2D(const TObject2D &obj)
Constructor from another TObject2D.
Definition: TObject2D.h:227
TObject2D & operator=(const TObject2D &obj)
Assign another TObject2D.
Definition: TObject2D.h:163
static constexpr unsigned char GEOMETRIC_TYPE_UNDEFINED
Object type identifier for empty TObject2D or TObject3D.
Definition: TPoseOrPoint.h:135
TObject2D(const TSegment2D &s)
Implicit constructor from segment.
Definition: TObject2D.h:63
bool isLine() const
Checks whether content is a line.
Definition: TObject2D.h:97
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
static void getPoints(const std::vector< TObject2D > &objs, std::vector< TPoint2D > &pnts)
Static method to retrieve all the points in a vector of TObject2D.
Definition: TObject2D.cpp:45
void operator=(const TPolygon2D &p)
Assign a polygon to this object.
Definition: TObject2D.h:214
mrpt::vision::TStereoCalibResults out
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &s, const CVectorFloat &a)
Definition: math.cpp:626
~TObject2D()
Object destruction.
Definition: TObject2D.h:85
static void getPolygons(const std::vector< TObject2D > &objs, std::vector< TPolygon2D > &polys)
Static method to retrieve all the polygons in a vector of TObject2D.
Definition: TObject2D.cpp:63
TObject2D(const TPolygon2D &p)
Implicit constructor from polygon.
Definition: TObject2D.h:74
static constexpr unsigned char GEOMETRIC_TYPE_SEGMENT
Object type identifier for TSegment2D or TSegment3D.
Definition: TPoseOrPoint.h:115
void operator=(const TLine2D &l)
Assign a line to this object.
Definition: TObject2D.h:205
void destroy()
Destroys the object, releasing the pointer to the content (if any).
Definition: TObject2D.h:46
static constexpr unsigned char GEOMETRIC_TYPE_LINE
Object type identifier for TLine2D or TLine3D.
Definition: TPoseOrPoint.h:120
2D polygon, inheriting from std::vector<TPoint2D>.
Definition: TPolygon2D.h:21
Union type storing pointers to every allowed type.
Definition: TObject2D.h:34
bool isPoint() const
Checks whether content is a point.
Definition: TObject2D.h:89
2D line without bounds, represented by its equation .
Definition: TLine2D.h:19



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020