MRPT  1.9.9
TObject3D.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-2019, 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/core/exceptions.h>
12 #include <mrpt/math/TLine3D.h>
13 #include <mrpt/math/TPlane.h>
14 #include <mrpt/math/TPoint3D.h>
15 #include <mrpt/math/TPolygon3D.h>
16 #include <mrpt/math/TPoseOrPoint.h>
17 
18 namespace mrpt::math
19 {
20 /**
21  * Standard object for storing any 3D lightweight object. Do not inherit from
22  * this class.
23  * \sa TPoint3D,TSegment3D,TLine3D,TPlane,TPolygon3D
24  */
25 struct TObject3D
26 {
27  private:
28  /**
29  * Object type identifier.
30  */
32  /**
33  * Union containing pointer to actual data.
34  */
36  {
40  TPolygon3D* polygon{nullptr};
42 
43  tobject3d_data_t() = default;
44  } data;
45  /**
46  * Destroys the object and releases the pointer, if any.
47  */
48  void destroy()
49  {
50  if (type == GEOMETRIC_TYPE_POLYGON) delete data.polygon;
52  }
53 
54  public:
55  /**
56  * Constructor from point.
57  */
59  {
60  data.point = p;
61  }
62  /**
63  * Constructor from segment.
64  */
66  {
67  data.segment = s;
68  }
69  /**
70  * Constructor from line.
71  */
72  TObject3D(const TLine3D& r) : type(GEOMETRIC_TYPE_LINE) { data.line = r; }
73  /**
74  * Constructor from polygon.
75  */
77  {
78  data.polygon = new TPolygon3D(p);
79  }
80  /**
81  * Constructor from plane.
82  */
83  TObject3D(const TPlane& p) : type(GEOMETRIC_TYPE_PLANE) { data.plane = p; }
84  /**
85  * Empty constructor.
86  */
88  /**
89  * Destructor.
90  */
92  /**
93  * Checks whether content is a point.
94  */
95  bool isPoint() const { return type == GEOMETRIC_TYPE_POINT; }
96  /**
97  * Checks whether content is a segment.
98  */
99  bool isSegment() const { return type == GEOMETRIC_TYPE_SEGMENT; }
100  /**
101  * Checks whether content is a line.
102  */
103  bool isLine() const { return type == GEOMETRIC_TYPE_LINE; }
104  /**
105  * Checks whether content is a polygon.
106  */
107  bool isPolygon() const { return type == GEOMETRIC_TYPE_POLYGON; }
108  /**
109  * Checks whether content is a plane.
110  */
111  bool isPlane() const { return type == GEOMETRIC_TYPE_PLANE; }
112  /**
113  * Gets object type.
114  */
115  unsigned char getType() const { return type; }
116  /**
117  * Gets the content as a point, returning false if the type is not
118  * adequate.
119  */
120  bool getPoint(TPoint3D& p) const
121  {
122  if (isPoint())
123  {
124  p = data.point;
125  return true;
126  }
127  else
128  return false;
129  }
130  /**
131  * Gets the content as a segment, returning false if the type is not
132  * adequate.
133  */
134  bool getSegment(TSegment3D& s) const
135  {
136  if (isSegment())
137  {
138  s = data.segment;
139  return true;
140  }
141  else
142  return false;
143  }
144  /**
145  * Gets the content as a line, returning false if the type is not adequate.
146  */
147  bool getLine(TLine3D& r) const
148  {
149  if (isLine())
150  {
151  r = data.line;
152  return true;
153  }
154  else
155  return false;
156  }
157  /**
158  * Gets the content as a polygon, returning false if the type is not
159  * adequate.
160  */
161  bool getPolygon(TPolygon3D& p) const
162  {
163  if (isPolygon())
164  {
165  p = *(data.polygon);
166  return true;
167  }
168  else
169  return false;
170  }
171  /**
172  * Gets the content as a plane, returning false if the type is not
173  * adequate.
174  */
175  bool getPlane(TPlane& p) const
176  {
177  if (isPlane())
178  {
179  p = data.plane;
180  return true;
181  }
182  else
183  return false;
184  }
185  /**
186  * Assigns another object, creating a new pointer if needed.
187  */
189  {
190  if (this == &obj) return *this;
191  destroy();
192  switch (type = obj.type)
193  {
195  data.point = obj.data.point;
196  break;
198  data.segment = obj.data.segment;
199  break;
200  case GEOMETRIC_TYPE_LINE:
201  data.line = obj.data.line;
202  break;
204  data.polygon = new TPolygon3D(*(obj.data.polygon));
205  break;
207  data.plane = obj.data.plane;
208  break;
210  break;
211  default:
212  THROW_EXCEPTION("Invalid TObject3D object");
213  }
214  return *this;
215  }
216  /**
217  * Assigns a point to this object.
218  */
219  void operator=(const TPoint3D& p)
220  {
221  destroy();
223  data.point = p;
224  }
225  /**
226  * Assigns a segment to this object.
227  */
228  void operator=(const TSegment3D& s)
229  {
230  destroy();
232  data.segment = s;
233  }
234  /**
235  * Assigns a line to this object.
236  */
237  void operator=(const TLine3D& l)
238  {
239  destroy();
241  data.line = l;
242  }
243  /**
244  * Assigns a polygon to this object.
245  */
246  void operator=(const TPolygon3D& p)
247  {
248  destroy();
250  data.polygon = new TPolygon3D(p);
251  }
252  /**
253  * Assigns a plane to this object.
254  */
255  void operator=(const TPlane& p)
256  {
257  destroy();
259  data.plane = p;
260  }
261  /**
262  * Projects into 2D space.
263  * \throw std::logic_error if the 3D object loses its properties when
264  * projecting into 2D space (for example, it's a plane or a vertical line).
265  */
266  void generate2DObject(TObject2D& obj) const;
267  /**
268  * Constructs from another object.
269  */
271  {
272  operator=(obj);
273  }
274  /**
275  * Static method to retrieve every point included in a vector of objects.
276  */
277  static void getPoints(
278  const std::vector<TObject3D>& objs, std::vector<TPoint3D>& pnts);
279  /**
280  * Static method to retrieve every segment included in a vector of objects.
281  */
282  static void getSegments(
283  const std::vector<TObject3D>& objs, std::vector<TSegment3D>& sgms);
284  /**
285  * Static method to retrieve every line included in a vector of objects.
286  */
287  static void getLines(
288  const std::vector<TObject3D>& objs, std::vector<TLine3D>& lins);
289  /**
290  * Static method to retrieve every plane included in a vector of objects.
291  */
292  static void getPlanes(
293  const std::vector<TObject3D>& objs, std::vector<TPlane>& plns);
294  /**
295  * Static method to retrieve every polygon included in a vector of objects.
296  */
297  static void getPolygons(
298  const std::vector<TObject3D>& objs, std::vector<TPolygon3D>& polys);
299  /**
300  * Static method to retrieve every point included in a vector of objects,
301  * returning the remaining objects in another argument.
302  */
303  static void getPoints(
304  const std::vector<TObject3D>& objs, std::vector<TPoint3D>& pnts,
305  std::vector<TObject3D>& remainder);
306  /**
307  * Static method to retrieve every segment included in a vector of objects,
308  * returning the remaining objects in another argument.
309  */
310  static void getSegments(
311  const std::vector<TObject3D>& objs, std::vector<TSegment3D>& sgms,
312  std::vector<TObject3D>& remainder);
313  /**
314  * Static method to retrieve every line included in a vector of objects,
315  * returning the remaining objects in another argument.
316  */
317  static void getLines(
318  const std::vector<TObject3D>& objs, std::vector<TLine3D>& lins,
319  std::vector<TObject3D>& remainder);
320  /**
321  * Static method to retrieve every plane included in a vector of objects,
322  * returning the remaining objects in another argument.
323  */
324  static void getPlanes(
325  const std::vector<TObject3D>& objs, std::vector<TPlane>& plns,
326  std::vector<TObject3D>& remainder);
327  /**
328  * Static method to retrieve every polygon included in a vector of objects,
329  * returning the remaining objects in another argument.
330  */
331  static void getPolygons(
332  const std::vector<TObject3D>& objs, std::vector<TPolygon3D>& polys,
333  std::vector<TObject3D>& remainder);
334 };
335 
340 
341 } // namespace mrpt::math
342 
343 namespace mrpt::typemeta
344 {
345 // Specialization must occur in the same namespace
347 
348 } // namespace mrpt::typemeta
void destroy()
Destroys the object and releases the pointer, if any.
Definition: TObject3D.h:48
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
static constexpr unsigned char GEOMETRIC_TYPE_POLYGON
Object type identifier for TPolygon2D or TPolygon3D.
Definition: TPoseOrPoint.h:116
bool getSegment(TSegment3D &s) const
Gets the content as a segment, returning false if the type is not adequate.
Definition: TObject3D.h:134
Union containing pointer to actual data.
Definition: TObject3D.h:35
Standard type for storing any lightweight 2D type.
Definition: TObject2D.h:24
static constexpr unsigned char GEOMETRIC_TYPE_POINT
Object type identifier for TPoint2D or TPoint3D.
Definition: TPoseOrPoint.h:101
bool getPlane(TPlane &p) const
Gets the content as a plane, returning false if the type is not adequate.
Definition: TObject3D.h:175
Standard object for storing any 3D lightweight object.
Definition: TObject3D.h:25
TObject3D()
Empty constructor.
Definition: TObject3D.h:87
void operator=(const TSegment3D &s)
Assigns a segment to this object.
Definition: TObject3D.h:228
GLdouble s
Definition: glext.h:3682
void operator=(const TPoint3D &p)
Assigns a point to this object.
Definition: TObject3D.h:219
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrixD::Ptr &pObj)
TObject3D(const TPlane &p)
Constructor from plane.
Definition: TObject3D.h:83
static void getSegments(const std::vector< TObject3D > &objs, std::vector< TSegment3D > &sgms)
Static method to retrieve every segment included in a vector of objects.
static constexpr unsigned char GEOMETRIC_TYPE_PLANE
Object type identifier for TPlane.
Definition: TPoseOrPoint.h:121
bool getPolygon(TPolygon3D &p) const
Gets the content as a polygon, returning false if the type is not adequate.
Definition: TObject3D.h:161
This base provides a set of functions for maths stuff.
bool isPoint() const
Checks whether content is a point.
Definition: TObject3D.h:95
3D segment, consisting of two points.
Definition: TSegment3D.h:21
TObject3D(const TPoint3D &p)
Constructor from point.
Definition: TObject3D.h:58
TObject3D & operator=(const TObject3D &obj)
Assigns another object, creating a new pointer if needed.
Definition: TObject3D.h:188
static void getLines(const std::vector< TObject3D > &objs, std::vector< TLine3D > &lins)
Static method to retrieve every line included in a vector of objects.
static void getPolygons(const std::vector< TObject3D > &objs, std::vector< TPolygon3D > &polys)
Static method to retrieve every polygon included in a vector of objects.
3D Plane, represented by its equation
Definition: TPlane.h:22
void operator=(const TPolygon3D &p)
Assigns a polygon to this object.
Definition: TObject3D.h:246
#define MRPT_DECLARE_TTYPENAME_NO_NAMESPACE(_TYPE, __NS)
Declares a typename to be "type" (without the NS prefix)
Definition: TTypeName.h:128
bool isPlane() const
Checks whether content is a plane.
Definition: TObject3D.h:111
static constexpr unsigned char GEOMETRIC_TYPE_UNDEFINED
Object type identifier for empty TObject2D or TObject3D.
Definition: TPoseOrPoint.h:126
void operator=(const TPlane &p)
Assigns a plane to this object.
Definition: TObject3D.h:255
bool getPoint(TPoint3D &p) const
Gets the content as a point, returning false if the type is not adequate.
Definition: TObject3D.h:120
TObject3D(const TLine3D &r)
Constructor from line.
Definition: TObject3D.h:72
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
mrpt::vision::TStereoCalibResults out
~TObject3D()
Destructor.
Definition: TObject3D.h:91
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &s, const CVectorFloat &a)
Definition: math.cpp:627
unsigned char type
Object type identifier.
Definition: TObject3D.h:31
struct mrpt::math::TObject3D::tobject3d_data_t data
GLuint in
Definition: glext.h:7391
bool getLine(TLine3D &r) const
Gets the content as a line, returning false if the type is not adequate.
Definition: TObject3D.h:147
void generate2DObject(TObject2D &obj) const
Projects into 2D space.
bool isLine() const
Checks whether content is a line.
Definition: TObject3D.h:103
bool isSegment() const
Checks whether content is a segment.
Definition: TObject3D.h:99
TObject3D(const TObject3D &obj)
Constructs from another object.
Definition: TObject3D.h:270
unsigned char getType() const
Gets object type.
Definition: TObject3D.h:115
void operator=(const TLine3D &l)
Assigns a line to this object.
Definition: TObject3D.h:237
static constexpr unsigned char GEOMETRIC_TYPE_SEGMENT
Object type identifier for TSegment2D or TSegment3D.
Definition: TPoseOrPoint.h:106
Lightweight 3D point.
Definition: TPoint3D.h:90
bool isPolygon() const
Checks whether content is a polygon.
Definition: TObject3D.h:107
TObject3D(const TSegment3D &s)
Constructor from segment.
Definition: TObject3D.h:65
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
GLfloat GLfloat p
Definition: glext.h:6398
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.
Definition: TObject3D.h:76
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3532
static constexpr unsigned char GEOMETRIC_TYPE_LINE
Object type identifier for TLine2D or TLine3D.
Definition: TPoseOrPoint.h:111
3D polygon, inheriting from std::vector<TPoint3D>
Definition: TPolygon3D.h:18
3D line, represented by a base point and a director vector.
Definition: TLine3D.h:19
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: 9b18308f3 Mon Nov 18 23:39:25 2019 +0100 at lun nov 18 23:45:12 CET 2019