Main MRPT website > C++ reference for MRPT 1.9.9
geometry.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 GEO_H
10 #define GEO_H
11 
12 #include <mrpt/utils/utils_defs.h>
14 #include <mrpt/poses/CPose3D.h>
16 
17 #include <mrpt/math/math_frwds.h> // forward declarations
18 #include <mrpt/math/wrap2pi.h>
19 
20 namespace mrpt
21 {
22 namespace math
23 {
24 /** \addtogroup geometry_grp Geometry: lines, planes, intersections, SLERP,
25  * "lightweight" point & pose classes
26  * \ingroup mrpt_base_grp
27  * @{ */
28 
29 /** Slightly heavyweight type to speed-up calculations with polygons in 3D
30  * \sa TPolygon3D,TPlane
31  */
33 {
34  public:
35  /** Actual polygon. */
37  /** Plane containing the polygon. */
39  /** Plane's pose. \sa inversePose */
41  /** Plane's inverse pose. \sa pose */
43  /** Polygon, after being projected to the plane using inversePose. \sa
44  * inversePose */
46  /** Constructor. Takes a polygon and computes each parameter. */
48  /** Basic constructor. Needed to create containers \sa
49  * TPolygonWithPlane(const TPolygon3D &) */
51  /** Static method for vectors. Takes a set of polygons and creates every
52  * TPolygonWithPlane */
53  static void getPlanes(
54  const std::vector<TPolygon3D>& oldPolys,
55  std::vector<TPolygonWithPlane>& newPolys);
56 };
57 
58 /** @name Simple intersection operations, relying basically on geometrical
59  operations.
60  @{
61  */
62 /** Gets the intersection between two 3D segments. Possible outcomes:
63  * - Segments intersect: Return=true, obj.getType()=GEOMETRIC_TYPE_POINT
64  * - Segments don't intersect & are parallel: Return=true,
65  *obj.getType()=GEOMETRIC_TYPE_SEGMENT, obj is the segment "in between" both
66  *segments.
67  * - Segments don't intersect & aren't parallel: Return=false.
68  * \sa TObject3D
69  */
70 bool intersect(const TSegment3D& s1, const TSegment3D& s2, TObject3D& obj);
71 
72 /** Gets the intersection between a 3D segment and a plane. Possible outcomes:
73  * - Don't intersect: Return=false
74  * - s1 is within the plane: Return=true,
75  *obj.getType()=GEOMETRIC_TYPE_SEGMENT
76  * - s1 intersects the plane at one point: Return=true,
77  *obj.getType()=GEOMETRIC_TYPE_POINT
78  * \sa TObject3D
79  */
80 bool intersect(const TSegment3D& s1, const TPlane& p2, TObject3D& obj);
81 
82 /** Gets the intersection between a 3D segment and a 3D line. Possible outcomes:
83  * - They don't intersect : Return=false
84  * - s1 lies within the line: Return=true,
85  *obj.getType()=GEOMETRIC_TYPE_SEGMENT
86  * - s1 intersects the line at a point: Return=true,
87  *obj.getType()=GEOMETRIC_TYPE_POINT
88  * \sa TObject3D
89  */
90 bool intersect(const TSegment3D& s1, const TLine3D& r2, TObject3D& obj);
91 
92 /** Gets the intersection between a plane and a 3D segment. Possible outcomes:
93  * - Don't intersect: Return=false
94  * - s2 is within the plane: Return=true,
95  *obj.getType()=GEOMETRIC_TYPE_SEGMENT
96  * - s2 intersects the plane at one point: Return=true,
97  *obj.getType()=GEOMETRIC_TYPE_POINT
98  * \sa TObject3D
99  */
100 inline bool intersect(const TPlane& p1, const TSegment3D& s2, TObject3D& obj)
101 {
102  return intersect(s2, p1, obj);
103 }
104 
105 /** Gets the intersection between two planes. Possible outcomes:
106  * - Planes are parallel: Return=false
107  * - Planes intersect into a line: Return=true,
108  *obj.getType()=GEOMETRIC_TYPE_LINE
109  * \sa TObject3D
110  */
111 bool intersect(const TPlane& p1, const TPlane& p2, TObject3D& obj);
112 
113 /** Gets the intersection between a plane and a 3D line. Possible outcomes:
114  * - Line is parallel to plane but not within it: Return=false
115  * - Line is contained in the plane: Return=true,
116  *obj.getType()=GEOMETRIC_TYPE_LINE
117  * - Line intersects the plane at one point: Return=true,
118  *obj.getType()=GEOMETRIC_TYPE_POINT
119  * \sa TObject3D
120  */
121 bool intersect(const TPlane& p1, const TLine3D& p2, TObject3D& obj);
122 
123 /** Gets the intersection between a 3D line and a 3D segment. Possible outcomes:
124  * - They don't intersect : Return=false
125  * - s2 lies within the line: Return=true,
126  *obj.getType()=GEOMETRIC_TYPE_SEGMENT
127  * - s2 intersects the line at a point: Return=true,
128  *obj.getType()=GEOMETRIC_TYPE_POINT
129  * \sa TObject3D
130  */
131 inline bool intersect(const TLine3D& r1, const TSegment3D& s2, TObject3D& obj)
132 {
133  return intersect(s2, r1, obj);
134 }
135 
136 /** Gets the intersection between a 3D line and a plane. Possible outcomes:
137  * - Line is parallel to plane but not within it: Return=false
138  * - Line is contained in the plane: Return=true,
139  *obj.getType()=GEOMETRIC_TYPE_LINE
140  * - Line intersects the plane at one point: Return=true,
141  *obj.getType()=GEOMETRIC_TYPE_POINT
142  * \sa TObject3D
143  */
144 inline bool intersect(const TLine3D& r1, const TPlane& p2, TObject3D& obj)
145 {
146  return intersect(p2, r1, obj);
147 }
148 
149 /** Gets the intersection between two 3D lines. Possible outcomes:
150  * - Lines do not intersect: Return=false
151  * - Lines are parallel and do not coincide: Return=false
152  * - Lines coincide (are the same): Return=true,
153  *obj.getType()=GEOMETRIC_TYPE_LINE
154  * - Lines intesect in a point: Return=true,
155  *obj.getType()=GEOMETRIC_TYPE_POINT
156  * \sa TObject3D
157  */
158 bool intersect(const TLine3D& r1, const TLine3D& r2, TObject3D& obj);
159 
160 /** Gets the intersection between two 2D lines. Possible outcomes:
161  * - Lines do not intersect: Return=false
162  * - Lines are parallel and do not coincide: Return=false
163  * - Lines coincide (are the same): Return=true,
164  *obj.getType()=GEOMETRIC_TYPE_LINE
165  * - Lines intesect in a point: Return=true,
166  *obj.getType()=GEOMETRIC_TYPE_POINT
167  * \sa TObject2D
168  */
169 bool intersect(const TLine2D& r1, const TLine2D& r2, TObject2D& obj);
170 
171 /** Gets the intersection between a 2D line and a 2D segment. Possible outcomes:
172  * - They don't intersect: Return=false
173  * - s2 lies within the line: Return=true,
174  *obj.getType()=GEOMETRIC_TYPE_SEGMENT
175  * - Both intersects in one point: Return=true,
176  *obj.getType()=GEOMETRIC_TYPE_POINT
177  * \sa TObject2D
178  */
179 bool intersect(const TLine2D& r1, const TSegment2D& s2, TObject2D& obj);
180 
181 /** Gets the intersection between a 2D line and a 2D segment. Possible outcomes:
182  * - They don't intersect: Return=false
183  * - s1 lies within the line: Return=true,
184  *obj.getType()=GEOMETRIC_TYPE_SEGMENT
185  * - Both intersects in one point: Return=true,
186  *obj.getType()=GEOMETRIC_TYPE_POINT
187  * \sa TObject2D
188  */
189 inline bool intersect(const TSegment2D& s1, const TLine2D& r2, TObject2D& obj)
190 {
191  return intersect(r2, s1, obj);
192 }
193 
194 /** Gets the intersection between two 2D segments. Possible outcomes:
195  * - Segments intersect: Return=true, obj.getType()=GEOMETRIC_TYPE_POINT
196  * - Segments don't intersect & are parallel: Return=true,
197  *obj.getType()=GEOMETRIC_TYPE_SEGMENT, obj is the segment "in between" both
198  *segments.
199  * - Segments don't intersect & aren't parallel: Return=false.
200  * \sa TObject2D
201  */
202 bool intersect(const TSegment2D& s1, const TSegment2D& s2, TObject2D& obj);
203 
204 /** @}
205  */
206 
207 /** @name Angle retrieval methods. Methods which use TSegments will
208  automatically use TLines' implicit constructors.
209  @{
210  */
211 /**
212  * Computes the angle between two planes.
213  */
214 double getAngle(const TPlane& p1, const TPlane& p2);
215 /**
216  * Computes the angle between a plane and a 3D line or segment (implicit
217  * constructor will be used if passing a segment instead of a line).
218  */
219 double getAngle(const TPlane& p1, const TLine3D& r2);
220 /**
221  * Computes the angle between a 3D line or segment and a plane (implicit
222  * constructor will be used if passing a segment instead of a line).
223  */
224 inline double getAngle(const TLine3D& r1, const TPlane& p2)
225 {
226  return getAngle(p2, r1);
227 }
228 /**
229  * Computes the angle between two 3D lines or segments (implicit constructor
230  * will be used if passing a segment instead of a line).
231  */
232 double getAngle(const TLine3D& r1, const TLine3D& r2);
233 /**
234  * Computes the angle between two 2D lines or segments (implicit constructor
235  * will be used if passing a segment instead of a line).
236  */
237 double getAngle(const TLine2D& r1, const TLine2D& r2);
238 /** @}
239  */
240 
241 /** @name Creation of lines from poses.
242  @{
243  */
244 /**
245  * Gets a 3D line corresponding to the X axis in a given pose. An implicit
246  * constructor is used if a TPose3D is given.
247  * \sa createFromPoseY,createFromPoseZ,createFromPoseAndVector
248  */
249 void createFromPoseX(const mrpt::poses::CPose3D& p, TLine3D& r);
250 /**
251  * Gets a 3D line corresponding to the Y axis in a given pose. An implicit
252  * constructor is used if a TPose3D is given.
253  * \sa createFromPoseX,createFromPoseZ,createFromPoseAndVector
254  */
255 void createFromPoseY(const mrpt::poses::CPose3D& p, TLine3D& r);
256 /**
257  * Gets a 3D line corresponding to the Z axis in a given pose. An implicit
258  * constructor is used if a TPose3D is given.
259  * \sa createFromPoseX,createFromPoseY,createFromPoseAndVector
260  */
261 void createFromPoseZ(const mrpt::poses::CPose3D& p, TLine3D& r);
262 /**
263  * Gets a 3D line corresponding to any arbitrary vector, in the base given by
264  * the pose. An implicit constructor is used if a TPose3D is given.
265  * \sa createFromPoseX,createFromPoseY,createFromPoseZ
266  */
268  const mrpt::poses::CPose3D& p, const double (&vector)[3], TLine3D& r);
269 /**
270  * Gets a 2D line corresponding to the X axis in a given pose. An implicit
271  * constructor is used if a CPose2D is given.
272  * \sa createFromPoseY,createFromPoseAndVector
273  */
274 void createFromPoseX(const TPose2D& p, TLine2D& r);
275 /**
276  * Gets a 2D line corresponding to the Y axis in a given pose. An implicit
277  * constructor is used if a CPose2D is given.
278  * \sa createFromPoseX,createFromPoseAndVector
279  */
280 void createFromPoseY(const TPose2D& p, TLine2D& r);
281 /**
282  * Gets a 2D line corresponding to any arbitrary vector, in the base given the
283  * given pose. An implicit constructor is used if a CPose2D is given.
284  * \sa createFromPoseY,createFromPoseAndVector
285  */
287  const TPose2D& p, const double (&vector)[2], TLine2D& r);
288 /** @}
289  */
290 
291 /** @name Other line or plane related methods.
292  @{
293  */
294 /**
295  * Checks whether this polygon or set of points acceptably fits a plane.
296  * \sa TPolygon3D,getEpsilon
297  */
298 bool conformAPlane(const std::vector<TPoint3D>& points);
299 /**
300  * Checks whether this polygon or set of points acceptably fits a plane, and if
301  * it's the case returns it in the second argument.
302  * \sa TPolygon3D,getEpsilon
303  */
304 bool conformAPlane(const std::vector<TPoint3D>& points, TPlane& p);
305 /**
306  * Checks whether this set of points acceptably fits a 2D line.
307  * \sa getEpsilon
308  */
309 bool areAligned(const std::vector<TPoint2D>& points);
310 /**
311  * Checks whether this set of points acceptably fits a 2D line, and if it's the
312  * case returns it in the second argument.
313  * \sa getEpsilon
314  */
315 bool areAligned(const std::vector<TPoint2D>& points, TLine2D& r);
316 /**
317  * Checks whether this set of points acceptably fits a 3D line.
318  * \sa getEpsilon
319  */
320 bool areAligned(const std::vector<TPoint3D>& points);
321 /**
322  * Checks whether this set of points acceptably fits a 3D line, and if it's the
323  * case returns it in the second argument.
324  */
325 bool areAligned(const std::vector<TPoint3D>& points, TLine3D& r);
326 /** @}
327  */
328 
329 /** @name Projections
330  @{
331  */
332 /** Uses the given pose 3D to project a point into a new base */
333 inline void project3D(
334  const TPoint3D& point, const mrpt::poses::CPose3D& newXYpose,
335  TPoint3D& newPoint)
336 {
337  newXYpose.composePoint(
338  point.x, point.y, point.z, newPoint.x, newPoint.y, newPoint.z);
339 }
340 /** Uses the given pose 3D to project a segment into a new base */
341 inline void project3D(
342  const TSegment3D& segment, const mrpt::poses::CPose3D& newXYpose,
343  TSegment3D& newSegment)
344 {
345  project3D(segment.point1, newXYpose, newSegment.point1);
346  project3D(segment.point2, newXYpose, newSegment.point2);
347 }
348 
349 /** Uses the given pose 3D to project a line into a new base */
350 void project3D(
351  const TLine3D& line, const mrpt::poses::CPose3D& newXYpose,
352  TLine3D& newLine);
353 /** Uses the given pose 3D to project a plane into a new base */
354 void project3D(
355  const TPlane& plane, const mrpt::poses::CPose3D& newXYpose,
356  TPlane& newPlane);
357 /** Uses the given pose 3D to project a polygon into a new base */
358 void project3D(
359  const TPolygon3D& polygon, const mrpt::poses::CPose3D& newXYpose,
360  TPolygon3D& newPolygon);
361 /** Uses the given pose 3D to project any 3D object into a new base. */
362 void project3D(
363  const TObject3D& object, const mrpt::poses::CPose3D& newXYPose,
364  TObject3D& newObject);
365 
366 /** Projects any 3D object into the plane's base, using its inverse pose. If the
367  * object is exactly inside the plane, this projection will zero its Z
368  * coordinates */
369 template <class T>
370 void project3D(const T& obj, const TPlane& newXYPlane, T& newObj)
371 {
373  TPlane(newXYPlane).getAsPose3D(pose);
374  project3D(obj, -pose, newObj);
375 }
376 
377 /** Projects any 3D object into the plane's base, using its inverse pose and
378  * forcing the position of the new coordinates origin. If the object is exactly
379  * inside the plane, this projection will zero its Z coordinates */
380 template <class T>
381 void project3D(
382  const T& obj, const TPlane& newXYPlane, const TPoint3D& newOrigin,
383  T& newObj)
384 {
386  // TPlane(newXYPlane).getAsPose3DForcingOrigin(newOrigin,pose);
387  TPlane(newXYPlane).getAsPose3D(pose);
388  project3D(obj, -pose, newObj);
389 }
390 
391 /** Projects a set of 3D objects into the plane's base. */
392 template <class T>
393 void project3D(
394  const std::vector<T>& objs, const mrpt::poses::CPose3D& newXYpose,
395  std::vector<T>& newObjs)
396 {
397  size_t N = objs.size();
398  newObjs.resize(N);
399  for (size_t i = 0; i < N; i++) project3D(objs[i], newXYpose, newObjs[i]);
400 }
401 
402 /** Uses the given pose 2D to project a point into a new base. */
403 void project2D(
404  const TPoint2D& point, const mrpt::poses::CPose2D& newXpose,
405  TPoint2D& newPoint);
406 
407 /** Uses the given pose 2D to project a segment into a new base */
408 inline void project2D(
409  const TSegment2D& segment, const mrpt::poses::CPose2D& newXpose,
410  TSegment2D& newSegment)
411 {
412  project2D(segment.point1, newXpose, newSegment.point1);
413  project2D(segment.point2, newXpose, newSegment.point2);
414 }
415 
416 /** Uses the given pose 2D to project a line into a new base */
417 void project2D(
418  const TLine2D& line, const mrpt::poses::CPose2D& newXpose,
419  TLine2D& newLine);
420 /** Uses the given pose 2D to project a polygon into a new base. */
421 void project2D(
422  const TPolygon2D& polygon, const mrpt::poses::CPose2D& newXpose,
423  TPolygon2D& newPolygon);
424 /** Uses the given pose 2D to project any 2D object into a new base */
425 void project2D(
426  const TObject2D& object, const mrpt::poses::CPose2D& newXpose,
427  TObject2D& newObject);
428 
429 /** Projects any 2D object into the line's base, using its inverse pose. If the
430  * object is exactly inside the line, this projection will zero its Y
431  * coordinate.
432  * \tparam CPOSE2D set to mrpt::poses::CPose2D
433  */
434 template <class T, class CPOSE2D>
435 void project2D(const T& obj, const TLine2D& newXLine, T& newObj)
436 {
437  CPOSE2D pose;
438  newXLine.getAsPose2D(pose);
439  project2D(obj, CPOSE2D(0, 0, 0) - pose, newObj);
440 }
441 
442 /** Projects any 2D object into the line's base, using its inverse pose and
443  * forcing the position of the new coordinate origin. If the object is exactly
444  * inside the line, this projection will zero its Y coordinate.
445  * \tparam CPOSE2D set to mrpt::poses::CPose2D
446  */
447 template <class T, class CPOSE2D>
448 void project2D(
449  const T& obj, const TLine2D& newXLine, const TPoint2D& newOrigin, T& newObj)
450 {
451  CPOSE2D pose;
452  newXLine.getAsPose2DForcingOrigin(newOrigin, pose);
453  project2D(obj, CPOSE2D(0, 0, 0) - pose, newObj);
454 }
455 
456 /** Projects a set of 2D objects into the line's base */
457 template <class T>
458 void project2D(
459  const std::vector<T>& objs, const mrpt::poses::CPose2D& newXpose,
460  std::vector<T>& newObjs)
461 {
462  size_t N = objs.size();
463  newObjs.resize(N);
464  for (size_t i = 0; i < N; i++) project2D(objs[i], newXpose, newObjs[i]);
465 }
466 /** @}
467  */
468 
469 /** @name Polygon intersections. These operations rely more on spatial reasoning
470  than in raw numerical operations.
471  @{
472  */
473 /** Gets the intersection between a 2D polygon and a 2D segment. \sa TObject2D
474  */
475 bool intersect(const TPolygon2D& p1, const TSegment2D& s2, TObject2D& obj);
476 /** Gets the intersection between a 2D polygon and a 2D line. \sa TObject2D */
477 bool intersect(const TPolygon2D& p1, const TLine2D& r2, TObject2D& obj);
478 /** Gets the intersection between two 2D polygons. \sa TObject2D */
479 bool intersect(const TPolygon2D& p1, const TPolygon2D& p2, TObject2D& obj);
480 /** Gets the intersection between a 2D segment and a 2D polygon. \sa TObject2D
481  */
482 inline bool intersect(
483  const TSegment2D& s1, const TPolygon2D& p2, TObject2D& obj)
484 {
485  return intersect(p2, s1, obj);
486 }
487 /** Gets the intersection between a 2D line and a 2D polygon.\sa TObject2D */
488 inline bool intersect(const TLine2D& r1, const TPolygon2D& p2, TObject2D& obj)
489 {
490  return intersect(p2, r1, obj);
491 }
492 /** Gets the intersection between a 3D polygon and a 3D segment. \sa TObject3D
493  */
494 bool intersect(const TPolygon3D& p1, const TSegment3D& s2, TObject3D& obj);
495 /** Gets the intersection between a 3D polygon and a 3D line. \sa TObject3D */
496 bool intersect(const TPolygon3D& p1, const TLine3D& r2, TObject3D& obj);
497 /** Gets the intersection between a 3D polygon and a plane. \sa TObject3D */
498 bool intersect(const TPolygon3D& p1, const TPlane& p2, TObject3D& obj);
499 /** Gets the intersection between two 3D polygons. \sa TObject3D */
500 bool intersect(const TPolygon3D& p1, const TPolygon3D& p2, TObject3D& obj);
501 /** Gets the intersection between a 3D segment and a 3D polygon. \sa TObject3D
502  */
503 inline bool intersect(
504  const TSegment3D& s1, const TPolygon3D& p2, TObject3D& obj)
505 {
506  return intersect(p2, s1, obj);
507 }
508 /** Gets the intersection between a 3D line and a 3D polygon.\sa TObject3D */
509 inline bool intersect(const TLine3D& r1, const TPolygon3D& p2, TObject3D& obj)
510 {
511  return intersect(p2, r1, obj);
512 }
513 /** Gets the intersection between a plane and a 3D polygon. \sa TObject3D */
514 inline bool intersect(const TPlane& p1, const TPolygon3D& p2, TObject3D& obj)
515 {
516  return intersect(p2, p1, obj);
517 }
518 
519 /** Gets the intersection between two sets of 3D polygons. The intersection is
520  * returned as an sparse matrix with each pair of polygons' intersections, and
521  * the return value is the amount of intersections found.
522  * \sa TObject3D,CSparseMatrixTemplate */
523 size_t intersect(
524  const std::vector<TPolygon3D>& v1, const std::vector<TPolygon3D>& v2,
525  CSparseMatrixTemplate<TObject3D>& objs);
526 /** Gets the intersection between two sets of 3D polygons. The intersection is
527  * returned as a vector with every intersection found, and the return value is
528  * the amount of intersections found.
529  * \sa TObject3D */
530 size_t intersect(
531  const std::vector<TPolygon3D>& v1, const std::vector<TPolygon3D>& v2,
532  std::vector<TObject3D>& objs);
533 /** @}
534  */
535 
536 /** @name Other intersections
537  @{
538  */
539 /** Gets the intersection between vectors of geometric objects and returns it in
540  * a sparse matrix of either TObject2D or TObject3D.
541  * \sa TObject2D,TObject3D,CSparseMatrix */
542 template <class T, class U, class O>
543 size_t intersect(
544  const std::vector<T>& v1, const std::vector<U>& v2,
546 {
547  size_t M = v1.size(), N = v2.size();
548  O obj;
549  objs.clear();
550  objs.resize(M, N);
551  for (size_t i = 0; i < M; i++)
552  for (size_t j = 0; j < M; j++)
553  if (intersect(v1[i], v2[j], obj)) objs(i, j) = obj;
554  return objs.getNonNullElements();
555 }
556 
557 /** Gets the intersection between vectors of geometric objects and returns it in
558  * a vector of either TObject2D or TObject3D.
559  * \sa TObject2D,TObject3D */
560 template <class T, class U, class O>
561 size_t intersect(
562  const std::vector<T>& v1, const std::vector<U>& v2, std::vector<O> objs)
563 {
564  objs.resize(0);
565  O obj;
566  for (typename std::vector<T>::const_iterator it1 = v1.begin();
567  it1 != v1.end(); ++it1)
568  {
569  const T& elem1 = *it1;
570  for (typename std::vector<U>::const_iterator it2 = v2.begin();
571  it2 != v2.end(); ++it2)
572  if (intersect(elem1, *it2, obj)) objs.push_back(obj);
573  }
574  return objs.size();
575 }
576 
577 /** Gets the intersection between any pair of 2D objects.*/
578 bool intersect(const TObject2D& o1, const TObject2D& o2, TObject2D& obj);
579 /** Gets the intersection between any pair of 3D objects.*/
580 bool intersect(const TObject3D& o1, const TObject3D& o2, TObject3D& obj);
581 /** @}
582  */
583 
584 /** @name Distances
585  @{
586  */
587 /** Gets the distance between two points in a 2D space. */
588 double distance(const TPoint2D& p1, const TPoint2D& p2);
589 /** Gets the distance between two points in a 3D space. */
590 double distance(const TPoint3D& p1, const TPoint3D& p2);
591 /** Gets the distance between two lines in a 2D space. */
592 double distance(const TLine2D& r1, const TLine2D& r2);
593 /** Gets the distance between two lines in a 3D space. */
594 double distance(const TLine3D& r1, const TLine3D& r2);
595 /** Gets the distance between two planes. It will be zero if the planes are not
596  * parallel. */
597 double distance(const TPlane& p1, const TPlane& p2);
598 /** Gets the distance between two polygons in a 2D space. */
599 double distance(const TPolygon2D& p1, const TPolygon2D& p2);
600 /** Gets the distance between a polygon and a segment in a 2D space. */
601 double distance(const TPolygon2D& p1, const TSegment2D& s2);
602 /** Gets the distance between a segment and a polygon in a 2D space. */
603 inline double distance(const TSegment2D& s1, const TPolygon2D& p2)
604 {
605  return distance(p2, s1);
606 }
607 /** Gets the distance between a polygon and a line in a 2D space. */
608 double distance(const TPolygon2D& p1, const TLine2D& l2);
609 inline double distance(const TLine2D& l1, const TPolygon2D& p2)
610 {
611  return distance(p2, l1);
612 }
613 /** Gets the distance between two polygons in a 3D space. */
614 double distance(const TPolygon3D& p1, const TPolygon3D& p2);
615 /** Gets the distance between a polygon and a segment in a 3D space. */
616 double distance(const TPolygon3D& p1, const TSegment3D& s2);
617 /** Gets the distance between a segment and a polygon in a 3D space.*/
618 inline double distance(const TSegment3D& s1, const TPolygon3D& p2)
619 {
620  return distance(p2, s1);
621 }
622 /** Gets the distance between a polygon and a line in a 3D space. */
623 double distance(const TPolygon3D& p1, const TLine3D& l2);
624 /** Gets the distance between a line and a polygon in a 3D space */
625 inline double distance(const TLine3D& l1, const TPolygon3D& p2)
626 {
627  return distance(p2, l1);
628 }
629 /** Gets the distance between a polygon and a plane. */
630 double distance(const TPolygon3D& po, const TPlane& pl);
631 /** Gets the distance between a plane and a polygon.*/
632 inline double distance(const TPlane& pl, const TPolygon3D& po)
633 {
634  return distance(po, pl);
635 }
636 /** @}
637  */
638 
639 /** @name Bound checkers
640  @{
641  */
642 /** Gets the rectangular bounds of a 2D polygon or set of 2D points */
643 void getRectangleBounds(
644  const std::vector<TPoint2D>& poly, TPoint2D& pMin, TPoint2D& pMax);
645 /** Gets the prism bounds of a 3D polygon or set of 3D points. */
646 void getPrismBounds(
647  const std::vector<TPoint3D>& poly, TPoint3D& pMin, TPoint3D& pMax);
648 /** @}
649  */
650 
651 /** @name Creation of planes from poses
652  @{
653  */
654 /**
655  * Given a pose, creates a plane orthogonal to its Z vector.
656  * \sa createPlaneFromPoseXZ,createPlaneFromPoseYZ,createPlaneFromPoseAndNormal
657  */
658 void createPlaneFromPoseXY(const mrpt::poses::CPose3D& pose, TPlane& plane);
659 /**
660  * Given a pose, creates a plane orthogonal to its Y vector.
661  * \sa createPlaneFromPoseXY,createPlaneFromPoseYZ,createPlaneFromPoseAndNormal
662  */
663 void createPlaneFromPoseXZ(const mrpt::poses::CPose3D& pose, TPlane& plane);
664 /**
665  * Given a pose, creates a plane orthogonal to its X vector.
666  * \sa createPlaneFromPoseXY,createPlaneFromPoseXZ,createPlaneFromPoseAndNormal
667  */
668 void createPlaneFromPoseYZ(const mrpt::poses::CPose3D& pose, TPlane& plane);
669 /**
670  * Given a pose and any vector, creates a plane orthogonal to that vector in
671  * the pose's coordinates.
672  * \sa createPlaneFromPoseXY,createPlaneFromPoseXZ,createPlaneFromPoseYZ
673  */
675  const mrpt::poses::CPose3D& pose, const double (&normal)[3], TPlane& plane);
676 /**
677  * Creates a homogeneus matrix (4x4) such that the coordinate given (0 for x, 1
678  * for y, 2 for z) corresponds to the provided vector.
679  * \param[in] vec must be a *unitary* vector
680  * \sa generateAxisBaseFromDirectionAndAxis()
681  */
683  const double (&vec)[3], char coord, CMatrixDouble& matrix);
684 /** @}
685  */
686 
687 /** @name Linear regression methods
688  @{
689  */
690 /**
691  * Using eigenvalues, gets the best fitting line for a set of 2D points.
692  * Returns an estimation of the error.
693  * \sa spline, leastSquareLinearFit
694  */
695 double getRegressionLine(const std::vector<TPoint2D>& points, TLine2D& line);
696 /**
697  * Using eigenvalues, gets the best fitting line for a set of 3D points.
698  * Returns an estimation of the error.
699  * \sa spline, leastSquareLinearFit
700  */
701 double getRegressionLine(const std::vector<TPoint3D>& points, TLine3D& line);
702 /**
703  * Using eigenvalues, gets the best fitting plane for a set of 3D points.
704  * Returns an estimation of the error.
705  * \sa spline, leastSquareLinearFit
706  */
707 double getRegressionPlane(const std::vector<TPoint3D>& points, TPlane& plane);
708 /** @}
709  */
710 
711 /** @name Miscellaneous Geometry methods
712  @{
713  */
714 /**
715  * Tries to assemble a set of segments into a set of closed polygons.
716  */
717 void assemblePolygons(
718  const std::vector<TSegment3D>& segms, std::vector<TPolygon3D>& polys);
719 /**
720  * Tries to assemble a set of segments into a set of closed polygons, returning
721  * the unused segments as another out parameter.
722  */
723 void assemblePolygons(
724  const std::vector<TSegment3D>& segms, std::vector<TPolygon3D>& polys,
725  std::vector<TSegment3D>& remainder);
726 /**
727  * Extracts all the polygons, including those formed from segments, from the
728  * set of objects.
729  */
730 void assemblePolygons(
731  const std::vector<TObject3D>& objs, std::vector<TPolygon3D>& polys);
732 /**
733  * Extracts all the polygons, including those formed from segments, from the
734  * set of objects.
735  */
736 void assemblePolygons(
737  const std::vector<TObject3D>& objs, std::vector<TPolygon3D>& polys,
738  std::vector<TObject3D>& remainder);
739 /**
740  * Extracts all the polygons, including those formed from segments, from the
741  * set of objects.
742  */
743 void assemblePolygons(
744  const std::vector<TObject3D>& objs, std::vector<TPolygon3D>& polys,
745  std::vector<TSegment3D>& remainder1, std::vector<TObject3D>& remainder2);
746 
747 /**
748  * Changes the value of the geometric epsilon (default = 1e-5)
749  * \sa geometryEpsilon,getEpsilon
750  */
751 void setEpsilon(double nE);
752 /**
753  * Gets the value of the geometric epsilon (default = 1e-5)
754  * \sa setEpsilon
755  */
756 double getEpsilon();
757 /**
758  * Splits a 2D polygon into convex components.
759  */
761  const TPolygon2D& poly, std::vector<TPolygon2D>& components);
762 /**
763  * Splits a 3D polygon into convex components.
764  * \throw std::logic_error if the polygon can't be fit into a plane.
765  */
767  const TPolygon3D& poly, std::vector<TPolygon3D>& components);
768 
769 /**
770  * Gets the bisector of a 2D segment.
771  */
772 void getSegmentBisector(const TSegment2D& sgm, TLine2D& bis);
773 /**
774  * Gets the bisector of a 3D segment.
775  */
776 void getSegmentBisector(const TSegment3D& sgm, TPlane& bis);
777 /**
778  * Gets the bisector of two lines or segments (implicit constructor will be
779  * used if necessary)
780  */
781 void getAngleBisector(const TLine2D& l1, const TLine2D& l2, TLine2D& bis);
782 /**
783  * Gets the bisector of two lines or segments (implicit constructor will be
784  * used if necessary)
785  * \throw std::logic_error if the lines do not fit in a single plane.
786  */
787 void getAngleBisector(const TLine3D& l1, const TLine3D& l2, TLine3D& bis);
788 
789 /**
790  * Fast ray tracing method using polygons' properties.
791  * \sa CRenderizable::rayTrace
792  */
793 bool traceRay(
794  const std::vector<TPolygonWithPlane>& vec, const mrpt::poses::CPose3D& pose,
795  double& dist);
796 /**
797  * Fast ray tracing method using polygons' properties.
798  * \sa CRenderizable::rayTrace
799  */
800 inline bool traceRay(
801  const std::vector<TPolygon3D>& vec, const mrpt::poses::CPose3D& pose,
802  double& dist)
803 {
804  std::vector<TPolygonWithPlane> pwp;
806  return traceRay(pwp, pose, dist);
807 }
808 
809 /** Computes the cross product of two 3D vectors, returning a vector normal to
810  both.
811  * It uses the simple implementation:
812 
813  \f[ v_out = \left(
814  \begin{array}{c c c}
815  \hat{i} ~ \hat{j} ~ \hat{k} \\
816  x0 ~ y0 ~ z0 \\
817  x1 ~ y1 ~ z1 \\
818  \end{array} \right)
819  \f]
820  */
821 template <class T, class U, class V>
822 inline void crossProduct3D(const T& v0, const U& v1, V& vOut)
823 {
824  vOut[0] = v0[1] * v1[2] - v0[2] * v1[1];
825  vOut[1] = v0[2] * v1[0] - v0[0] * v1[2];
826  vOut[2] = v0[0] * v1[1] - v0[1] * v1[0];
827 }
828 
829 //! \overload
830 template <class T>
831 inline void crossProduct3D(
832  const std::vector<T>& v0, const std::vector<T>& v1, std::vector<T>& v_out)
833 {
834  ASSERT_(v0.size() == 3)
835  ASSERT_(v1.size() == 3);
836  v_out.resize(3);
837  v_out[0] = v0[1] * v1[2] - v0[2] * v1[1];
838  v_out[1] = -v0[0] * v1[2] + v0[2] * v1[0];
839  v_out[2] = v0[0] * v1[1] - v0[1] * v1[0];
840 }
841 
842 //! overload (returning a vector of size 3 by value).
843 template <class VEC1, class VEC2>
844 inline Eigen::Matrix<double, 3, 1> crossProduct3D(
845  const VEC1& v0, const VEC2& v1)
846 {
847  Eigen::Matrix<double, 3, 1> vOut;
848  vOut[0] = v0[1] * v1[2] - v0[2] * v1[1];
849  vOut[1] = v0[2] * v1[0] - v0[0] * v1[2];
850  vOut[2] = v0[0] * v1[1] - v0[1] * v1[0];
851  return vOut;
852 }
854 /** Computes the 3x3 skew symmetric matrix from a 3-vector or 3-array:
855  * \f[ M([x ~ y ~ z]^\top) = \left(
856  * \begin{array}{c c c}
857  * 0 & -z & y \\
858  * z & 0 & -x \\
859  * -y & x & 0
860  * \end{array} \right)
861  * \f]
862  */
863 template <class VECTOR, class MATRIX>
864 inline void skew_symmetric3(const VECTOR& v, MATRIX& M)
865 {
866  ASSERT_(v.size() == 3)
867  M.setSize(3, 3);
868  M.set_unsafe(0, 0, 0);
869  M.set_unsafe(0, 1, -v[2]);
870  M.set_unsafe(0, 2, v[1]);
871  M.set_unsafe(1, 0, v[2]);
872  M.set_unsafe(1, 1, 0);
873  M.set_unsafe(1, 2, -v[0]);
874  M.set_unsafe(2, 0, -v[1]);
875  M.set_unsafe(2, 1, v[0]);
876  M.set_unsafe(2, 2, 0);
877 }
878 //! \overload
879 template <class VECTOR>
880 inline mrpt::math::CMatrixDouble33 skew_symmetric3(const VECTOR& v)
881 {
883  skew_symmetric3(v, M);
884  return M;
885 }
886 
887 /** Computes the negative version of a 3x3 skew symmetric matrix from a 3-vector
888  * or 3-array:
889  * \f[ -M([x ~ y ~ z]^\top) = \left(
890  * \begin{array}{c c c}
891  * 0 & z & -y \\
892  * -z & 0 & x \\
893  * y & -x & 0
894  * \end{array} \right)
895  * \f]
896  */
897 template <class VECTOR, class MATRIX>
898 inline void skew_symmetric3_neg(const VECTOR& v, MATRIX& M)
899 {
900  ASSERT_(v.size() == 3)
901  M.setSize(3, 3);
902  M.set_unsafe(0, 0, 0);
903  M.set_unsafe(0, 1, v[2]);
904  M.set_unsafe(0, 2, -v[1]);
905  M.set_unsafe(1, 0, -v[2]);
906  M.set_unsafe(1, 1, 0);
907  M.set_unsafe(1, 2, v[0]);
908  M.set_unsafe(2, 0, v[1]);
909  M.set_unsafe(2, 1, -v[0]);
910  M.set_unsafe(2, 2, 0);
911 }
912 //! \overload
913 template <class VECTOR>
915 {
918  return M;
919 }
920 
921 /**
922  * Returns true if two 2D vectors are parallel. The arguments may be points,
923  * arrays, etc.
924  */
925 template <class T, class U>
926 inline bool vectorsAreParallel2D(const T& v1, const U& v2)
927 {
928  return abs(v1[0] * v2[1] - v2[0] * v1[1]) < getEpsilon();
929 }
930 
931 /**
932  * Returns true if two 3D vectors are parallel. The arguments may be points,
933  * arrays, etc.
934  */
935 template <class T, class U>
936 inline bool vectorsAreParallel3D(const T& v1, const U& v2)
937 {
938  if (abs(v1[0] * v2[1] - v2[0] * v1[1]) >= getEpsilon()) return false;
939  if (abs(v1[1] * v2[2] - v2[1] * v1[2]) >= getEpsilon()) return false;
940  return abs(v1[2] * v2[0] - v2[2] * v1[0]) < getEpsilon();
941 }
942 
943 /** Computes the closest point from a given point to a segment.
944  * \sa closestFromPointToLine
945  */
947  const double& Px, const double& Py, const double& x1, const double& y1,
948  const double& x2, const double& y2, double& out_x, double& out_y);
949 
950 /** Computes the closest point from a given point to a (infinite) line.
951  * \sa closestFromPointToSegment
952  */
954  const double& Px, const double& Py, const double& x1, const double& y1,
955  const double& x2, const double& y2, double& out_x, double& out_y);
956 
957 /** Returns the square distance from a point to a line.
958  */
960  const double& Px, const double& Py, const double& x1, const double& y1,
961  const double& x2, const double& y2);
962 
963 /** Returns the distance between 2 points in 2D. */
964 template <typename T>
965 T distanceBetweenPoints(const T x1, const T y1, const T x2, const T y2)
966 {
967  return std::sqrt(square(x1 - x2) + square(y1 - y2));
968 }
970 /** Returns the distance between 2 points in 3D. */
971 template <typename T>
973  const T x1, const T y1, const T z1, const T x2, const T y2, const T z2)
974 {
975  return std::sqrt(square(x1 - x2) + square(y1 - y2) + square(z1 - z2));
976 }
977 
978 /** Returns the square distance between 2 points in 2D. */
979 template <typename T>
980 T distanceSqrBetweenPoints(const T x1, const T y1, const T x2, const T y2)
981 {
982  return square(x1 - x2) + square(y1 - y2);
983 }
984 
985 /** Returns the square distance between 2 points in 3D. */
986 template <typename T>
988  const T x1, const T y1, const T z1, const T x2, const T y2, const T z2)
989 {
990  return square(x1 - x2) + square(y1 - y2) + square(z1 - z2);
991 }
992 
993 /** Computes the closest point from a given point to a segment, and returns that
994  * minimum distance.
995  */
996 template <typename T>
998  const double Px, const double Py, const double x1, const double y1,
999  const double x2, const double y2, T& out_x, T& out_y)
1000 {
1001  double ox, oy;
1002  closestFromPointToSegment(Px, Py, x1, y1, x2, y2, ox, oy);
1003  out_x = static_cast<T>(ox);
1004  out_y = static_cast<T>(oy);
1005  return distanceBetweenPoints(Px, Py, ox, oy);
1006 }
1007 
1008 /** Returns the intersection point, and if it exists, between two segments.
1009  */
1011  const double x1, const double y1, const double x2, const double y2,
1012  const double x3, const double y3, const double x4, const double y4,
1013  double& ix, double& iy);
1014 
1015 /** Returns the intersection point, and if it exists, between two segments.
1016  */
1018  const double x1, const double y1, const double x2, const double y2,
1019  const double x3, const double y3, const double x4, const double y4,
1020  float& ix, float& iy);
1021 
1022 /** Returns true if the 2D point (px,py) falls INTO the given polygon.
1023  * \sa pointIntoQuadrangle
1024  */
1025 bool pointIntoPolygon2D(
1026  const double& px, const double& py, unsigned int polyEdges,
1027  const double* poly_xs, const double* poly_ys);
1028 
1029 /** Specialized method to check whether a point (x,y) falls into a quadrangle.
1030  * \sa pointIntoPolygon2D
1031  */
1032 template <typename T>
1033 bool pointIntoQuadrangle(
1034  T x, T y, T v1x, T v1y, T v2x, T v2y, T v3x, T v3y, T v4x, T v4y)
1035 {
1036  using mrpt::utils::sign;
1037 
1038  const T a1 = atan2(v1y - y, v1x - x);
1039  const T a2 = atan2(v2y - y, v2x - x);
1040  const T a3 = atan2(v3y - y, v3x - x);
1041  const T a4 = atan2(v4y - y, v4x - x);
1042 
1043  // The point is INSIDE iff all the signs of the angles between each vertex
1044  // and the next one are equal.
1045  const T da1 = mrpt::math::wrapToPi(a2 - a1);
1046  const T da2 = mrpt::math::wrapToPi(a3 - a2);
1047  if (sign(da1) != sign(da2)) return false;
1048 
1049  const T da3 = mrpt::math::wrapToPi(a4 - a3);
1050  if (sign(da2) != sign(da3)) return false;
1051 
1052  const T da4 = mrpt::math::wrapToPi(a1 - a4);
1053  return (sign(da3) == sign(da4) && (sign(da4) == sign(da1)));
1054 }
1055 
1056 /** Returns the closest distance of a given 2D point to a polygon, or "0" if the
1057  * point is INTO the polygon or its perimeter.
1058  */
1060  const double& px, const double& py, unsigned int polyEdges,
1061  const double* poly_xs, const double* poly_ys);
1062 
1063 /** Calculates the minimum distance between a pair of lines.
1064  The lines are given by:
1065  - Line 1 = P1 + f (P2-P1)
1066  - Line 2 = P3 + f (P4-P3)
1067  The Euclidean distance is returned in "dist", and the mid point between the
1068  lines in (x,y,z)
1069  \return It returns false if there is no solution, i.e. lines are (almost, up
1070  to EPS) parallel.
1071  */
1072 bool minDistBetweenLines(
1073  const double& p1_x, const double& p1_y, const double& p1_z,
1074  const double& p2_x, const double& p2_y, const double& p2_z,
1075  const double& p3_x, const double& p3_y, const double& p3_z,
1076  const double& p4_x, const double& p4_y, const double& p4_z, double& x,
1077  double& y, double& z, double& dist);
1078 
1079 /** Returns whether two rotated rectangles intersect.
1080  * The first rectangle is not rotated and given by
1081  * (R1_x_min,R1_x_max)-(R1_y_min,R1_y_max).
1082  * The second rectangle is given is a similar way, but it is internally rotated
1083  * according
1084  * to the given coordinates translation
1085  * (R2_pose_x,R2_pose_y,R2_pose_phi(radians)), relative
1086  * to the coordinates system of rectangle 1.
1087  */
1089  const double& R1_x_min, const double& R1_x_max, const double& R1_y_min,
1090  const double& R1_y_max, const double& R2_x_min, const double& R2_x_max,
1091  const double& R2_y_min, const double& R2_y_max, const double& R2_pose_x,
1092  const double& R2_pose_y, const double& R2_pose_phi);
1093 
1094 /** Computes an axis base (a set of three 3D normal vectors) with the given
1095  vector being the first of them ("X")
1096  * NOTE: Make sure of passing all floats or doubles and that the template of
1097  the receiving matrix is of the same type!
1098  *
1099  * If \f$ d = [ dx ~ dy ~ dz ] \f$ is the input vector, then this function
1100  returns a matrix \f$ M \f$ such as:
1101  *
1102  \f[ M = \left(
1103  \begin{array}{c c c}
1104  v^1_x ~ v^2_x ~ v^3_x \\
1105  v^1_y ~ v^2_y ~ v^3_y \\
1106  v^1_z ~ v^2_z ~ v^3_z
1107  \end{array} \right)
1108  \f]
1109  *
1110  * And the three normal vectors are computed as:
1111  *
1112  * \f[ v^1 = \frac{d}{|d|} \f]
1113  *
1114  * If (dx!=0 or dy!=0):
1115  * \f[ v^2 = \frac{[-dy ~ dx ~ 0 ]}{\sqrt{dx^2+dy^2}} \f]
1116  * otherwise (the direction vector is vertical):
1117  * \f[ v^2 = [1 ~ 0 ~ 0] \f]
1118  *
1119  * And finally, the third vector is the cross product of the others:
1120  *
1121  * \f[ v^3 = v^1 \times v^2 \f]
1122  *
1123  * \return The 3x3 matrix (CMatrixTemplateNumeric<T>), containing one vector
1124  per column.
1125  * \except Throws an std::exception on invalid input (i.e. null direction
1126  vector)
1127  * \sa generateAxisBaseFromDirectionAndAxis()
1128  *
1129  * (JLB @ 18-SEP-2007)
1130  */
1131 template <class T>
1133 {
1134  MRPT_START
1135 
1136  if (dx == 0 && dy == 0 && dz == 0)
1137  THROW_EXCEPTION("Invalid input: Direction vector is (0,0,0)!");
1138 
1139  CMatrixTemplateNumeric<T> P(3, 3);
1140 
1141  // 1st vector:
1142  T n_xy = square(dx) + square(dy);
1143  T n = sqrt(n_xy + square(dz));
1144  n_xy = sqrt(n_xy);
1145  P(0, 0) = dx / n;
1146  P(1, 0) = dy / n;
1147  P(2, 0) = dz / n;
1148 
1149  // 2nd perpendicular vector:
1150  if (fabs(dx) > 1e-4 || fabs(dy) > 1e-4)
1151  {
1152  P(0, 1) = -dy / n_xy;
1153  P(1, 1) = dx / n_xy;
1154  P(2, 1) = 0;
1155  }
1156  else
1157  {
1158  // Any vector in the XY plane will work:
1159  P(0, 1) = 1;
1160  P(1, 1) = 0;
1161  P(2, 1) = 0;
1162  }
1163 
1164  // 3rd perpendicular vector: cross product of the two last vectors:
1165  P.col(2) = crossProduct3D(P.col(0), P.col(1));
1166 
1167  return P;
1168  MRPT_END
1169 }
1170 
1171 /** @} */ // end of misc. geom. methods
1172 
1173 /** @} */ // end of grouping
1174 
1175 } // End of namespace
1176 
1177 } // End of namespace
1178 #endif
void createPlaneFromPoseAndNormal(const mrpt::poses::CPose3D &pose, const double(&normal)[3], TPlane &plane)
Given a pose and any vector, creates a plane orthogonal to that vector in the pose&#39;s coordinates...
Definition: geometry.cpp:2059
void setEpsilon(double nE)
Changes the value of the geometric epsilon (default = 1e-5)
Definition: geometry.cpp:34
bool RectanglesIntersection(const double &R1_x_min, const double &R1_x_max, const double &R1_y_min, const double &R1_y_max, const double &R2_x_min, const double &R2_x_max, const double &R2_y_min, const double &R2_y_max, const double &R2_pose_x, const double &R2_pose_y, const double &R2_pose_phi)
Returns whether two rotated rectangles intersect.
Definition: geometry.cpp:373
bool splitInConvexComponents(const TPolygon2D &poly, std::vector< TPolygon2D > &components)
Splits a 2D polygon into convex components.
Definition: geometry.cpp:2389
GLdouble GLdouble z
Definition: glext.h:3872
void generateAxisBaseFromDirectionAndAxis(const double(&vec)[3], char coord, CMatrixDouble &matrix)
Creates a homogeneus matrix (4x4) such that the coordinate given (0 for x, 1 for y, 2 for z) corresponds to the provided vector.
Definition: geometry.cpp:2073
void closestFromPointToLine(const double &Px, const double &Py, const double &x1, const double &y1, const double &x2, const double &y2, double &out_x, double &out_y)
Computes the closest point from a given point to a (infinite) line.
Definition: geometry.cpp:80
void closestFromPointToSegment(const double &Px, const double &Py, const double &x1, const double &y1, const double &x2, const double &y2, double &out_x, double &out_y)
Computes the closest point from a given point to a segment.
Definition: geometry.cpp:42
bool minDistBetweenLines(const double &p1_x, const double &p1_y, const double &p1_z, const double &p2_x, const double &p2_y, const double &p2_z, const double &p3_x, const double &p3_y, const double &p3_z, const double &p4_x, const double &p4_y, const double &p4_z, double &x, double &y, double &z, double &dist)
Calculates the minimum distance between a pair of lines.
Definition: geometry.cpp:302
GLuint GLenum matrix
Definition: glext.h:6975
void skew_symmetric3(const VECTOR &v, MATRIX &M)
Computes the 3x3 skew symmetric matrix from a 3-vector or 3-array: .
Definition: geometry.h:853
double closestSquareDistanceFromPointToLine(const double &Px, const double &Py, const double &x1, const double &y1, const double &x2, const double &y2)
Returns the square distance from a point to a line.
Definition: geometry.cpp:103
bool pointIntoPolygon2D(const double &px, const double &py, unsigned int polyEdges, const double *poly_xs, const double *poly_ys)
Returns true if the 2D point (px,py) falls INTO the given polygon.
Definition: geometry.cpp:237
INT32 z2
Definition: jidctint.cpp:130
size_t getNonNullElements() const
Gets the amount of non-null elements inside the matrix.
bool traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::poses::CPose3D &pose, double &dist)
Fast ray tracing method using polygons&#39; properties.
Definition: geometry.cpp:2582
void assemblePolygons(const std::vector< TSegment3D > &segms, std::vector< TPolygon3D > &polys)
Tries to assemble a set of segments into a set of closed polygons.
Definition: geometry.cpp:2183
void getSegmentBisector(const TSegment2D &sgm, TLine2D &bis)
Gets the bisector of a 2D segment.
Definition: geometry.cpp:2508
#define THROW_EXCEPTION(msg)
GLenum GLsizei n
Definition: glext.h:5074
Slightly heavyweight type to speed-up calculations with polygons in 3D.
Definition: geometry.h:32
bool vectorsAreParallel2D(const T &v1, const U &v2)
Returns true if two 2D vectors are parallel.
Definition: geometry.h:915
int sign(T x)
Returns the sign of X as "1" or "-1".
Standard type for storing any lightweight 2D type.
void createFromPoseAndVector(const mrpt::poses::CPose3D &p, const double(&vector)[3], TLine3D &r)
Gets a 3D line corresponding to any arbitrary vector, in the base given by the pose.
Definition: geometry.cpp:958
void getAsPose3D(mrpt::poses::CPose3D &outPose)
Gets a pose whose XY plane corresponds to this plane.
Standard object for storing any 3D lightweight object.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
GLenum GLenum GLuint components
Definition: glext.h:7282
bool vectorsAreParallel3D(const T &v1, const U &v2)
Returns true if two 3D vectors are parallel.
Definition: geometry.h:925
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void getAngleBisector(const TLine2D &l1, const TLine2D &l2, TLine2D &bis)
Gets the bisector of two lines or segments (implicit constructor will be used if necessary) ...
Definition: geometry.cpp:2530
GLuint coord
Definition: glext.h:7131
void project2D(const TPoint2D &point, const mrpt::poses::CPose2D &newXpose, TPoint2D &newPoint)
Uses the given pose 2D to project a point into a new base.
Definition: geometry.cpp:1172
A sparse matrix container (with cells of any type), with iterators.
void createFromPoseY(const mrpt::poses::CPose3D &p, TLine3D &r)
Gets a 3D line corresponding to the Y axis in a given pose.
Definition: geometry.cpp:948
void crossProduct3D(const T &v0, const U &v1, V &vOut)
Computes the cross product of two 3D vectors, returning a vector normal to both.
Definition: geometry.h:811
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
bool conformAPlane(const std::vector< TPoint3D > &points)
Checks whether this polygon or set of points acceptably fits a plane.
Definition: geometry.cpp:995
GLsizei const GLfloat * points
Definition: glext.h:5339
void createFromPoseX(const mrpt::poses::CPose3D &p, TLine3D &r)
Gets a 3D line corresponding to the X axis in a given pose.
Definition: geometry.cpp:943
TPolygonWithPlane()
Basic constructor.
Definition: geometry.h:50
void skew_symmetric3_neg(const VECTOR &v, MATRIX &M)
Computes the negative version of a 3x3 skew symmetric matrix from a 3-vector or 3-array: ...
Definition: geometry.h:887
bool pointIntoQuadrangle(T x, T y, T v1x, T v1y, T v2x, T v2y, T v3x, T v3y, T v4x, T v4y)
Specialized method to check whether a point (x,y) falls into a quadrangle.
Definition: geometry.h:1022
2D segment, consisting of two points.
3D segment, consisting of two points.
void createPlaneFromPoseXY(const mrpt::poses::CPose3D &pose, TPlane &plane)
Given a pose, creates a plane orthogonal to its Z vector.
Definition: geometry.cpp:2044
#define MRPT_END
double distancePointToPolygon2D(const double &px, const double &py, unsigned int polyEdges, const double *poly_xs, const double *poly_ys)
Returns the closest distance of a given 2D point to a polygon, or "0" if the point is INTO the polygo...
Definition: geometry.cpp:268
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
void createPlaneFromPoseXZ(const mrpt::poses::CPose3D &pose, TPlane &plane)
Given a pose, creates a plane orthogonal to its Y vector.
Definition: geometry.cpp:2049
void getRectangleBounds(const std::vector< TPoint2D > &poly, TPoint2D &pMin, TPoint2D &pMax)
Gets the rectangular bounds of a 2D polygon or set of 2D points.
Definition: geometry.cpp:1900
TPolygon3D poly
Actual polygon.
Definition: geometry.h:36
3D Plane, represented by its equation
double getRegressionPlane(const std::vector< TPoint3D > &points, TPlane &plane)
Using eigenvalues, gets the best fitting plane for a set of 3D points.
Definition: geometry.cpp:2157
double x
X,Y,Z coordinates.
TPoint2D point2
Destiny point.
TPolygon2D poly2D
Polygon, after being projected to the plane using inversePose.
Definition: geometry.h:45
double getAngle(const TPlane &p1, const TPlane &p2)
Computes the angle between two planes.
Definition: geometry.cpp:863
static void getPlanes(const std::vector< TPolygon3D > &oldPolys, std::vector< TPolygonWithPlane > &newPolys)
Static method for vectors.
Definition: geometry.cpp:627
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Definition: wrap2pi.h:53
int sign(T x)
Returns the sign of X as "1" or "-1".
Definition: bits.h:121
mrpt::poses::CPose3D pose
Plane&#39;s pose.
Definition: geometry.h:40
TPoint2D point1
Origin point.
bool SegmentsIntersection(const double x1, const double y1, const double x2, const double y2, const double x3, const double y3, const double x4, const double y4, double &ix, double &iy)
Returns the intersection point, and if it exists, between two segments.
Definition: geometry.cpp:124
void clear()
Completely removes all elements, although maintaining the matrix&#39;s size.
#define MRPT_START
void getAsPose2D(mrpt::poses::CPose2D &outPose) const
Get a pose2D whose X axis corresponds to the line.
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CMatrixTemplateNumeric< T > generateAxisBaseFromDirection(T dx, T dy, T dz)
Computes an axis base (a set of three 3D normal vectors) with the given vector being the first of the...
Definition: geometry.h:1121
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
GLfloat GLfloat v1
Definition: glext.h:4105
double getRegressionLine(const std::vector< TPoint2D > &points, TLine2D &line)
Using eigenvalues, gets the best fitting line for a set of 2D points.
Definition: geometry.cpp:2109
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
GLfloat v0
Definition: glext.h:4103
void getPrismBounds(const std::vector< TPoint3D > &poly, TPoint3D &pMin, TPoint3D &pMax)
Gets the prism bounds of a 3D polygon or set of 3D points.
Definition: geometry.cpp:2015
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=nullptr, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
Definition: CPose3D.cpp:453
void resize(size_t nRows, size_t nCols)
Changes the size of the matrix.
void project3D(const TPoint3D &point, const mrpt::poses::CPose3D &newXYpose, TPoint3D &newPoint)
Uses the given pose 3D to project a point into a new base.
Definition: geometry.h:329
#define ASSERT_(f)
mrpt::poses::CPose3D inversePose
Plane&#39;s inverse pose.
Definition: geometry.h:42
GLenum GLint GLint y
Definition: glext.h:3538
void createPlaneFromPoseYZ(const mrpt::poses::CPose3D &pose, TPlane &plane)
Given a pose, creates a plane orthogonal to its X vector.
Definition: geometry.cpp:2054
bool areAligned(const std::vector< TPoint2D > &points)
Checks whether this set of points acceptably fits a 2D line.
Definition: geometry.cpp:1016
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.
T distanceBetweenPoints(const T x1, const T y1, const T x2, const T y2)
Returns the distance between 2 points in 2D.
Definition: geometry.h:954
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
void createFromPoseZ(const mrpt::poses::CPose3D &p, TLine3D &r)
Gets a 3D line corresponding to the Z axis in a given pose.
Definition: geometry.cpp:953
T distanceSqrBetweenPoints(const T x1, const T y1, const T x2, const T y2)
Returns the square distance between 2 points in 2D.
Definition: geometry.h:969
GLenum GLint x
Definition: glext.h:3538
Lightweight 3D point.
TPlane plane
Plane containing the polygon.
Definition: geometry.h:38
double getEpsilon()
Gets the value of the geometric epsilon (default = 1e-5)
Definition: geometry.cpp:30
Lightweight 2D point.
double minimumDistanceFromPointToSegment(const double Px, const double Py, const double x1, const double y1, const double x2, const double y2, T &out_x, T &out_y)
Computes the closest point from a given point to a segment, and returns that minimum distance...
Definition: geometry.h:986
INT32 z1
Definition: jidctint.cpp:130
GLfloat GLfloat p
Definition: glext.h:6305
bool intersect(const TSegment3D &s1, const TSegment3D &s2, TObject3D &obj)
Gets the intersection between two 3D segments.
Definition: geometry.cpp:635
2D polygon, inheriting from std::vector<TPoint2D>.
3D polygon, inheriting from std::vector<TPoint3D>
double distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
Definition: geometry.cpp:1885
3D line, represented by a base point and a director vector.
2D line without bounds, represented by its equation .



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