MRPT  1.9.9
TSegment2D.cpp
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 
10 #include "math-precomp.h" // Precompiled headers
11 
12 #include <mrpt/math/TSegment2D.h>
13 #include <mrpt/math/geometry.h> // distance()
14 #include <mrpt/serialization/CArchive.h> // impl of << operator
15 
16 using namespace mrpt::math;
17 
18 double TSegment2D::length() const { return math::distance(point1, point2); }
19 double TSegment2D::distance(const TPoint2D& point) const
20 {
21  return std::abs(signedDistance(point));
22 }
23 double TSegment2D::signedDistance(const TPoint2D& point) const
24 {
25  // It is reckoned whether the perpendicular line to the TSegment2D which
26  // passes through point crosses or not the referred segment,
27  // or what is the same, whether point makes an obtuse triangle with the
28  // segment or not (being the longest segment one between the point and
29  // either end of TSegment2D).
30  const double d1 = math::distance(point, point1);
31  if (point1 == point2) return d1;
32 
33  const double d2 = math::distance(point, point2);
34  const double d3 = length();
35  const double ds1 = square(d1);
36  const double ds2 = square(d2);
37  const double ds3 = square(d3);
38  if (ds1 > (ds2 + ds3) || ds2 > (ds1 + ds3))
39  // Fix sign:
40  return std::min(d1, d2) *
41  (TLine2D(*this).signedDistance(point) < 0 ? -1 : 1);
42  else
43  return TLine2D(*this).signedDistance(point);
44 }
45 bool TSegment2D::contains(const TPoint2D& point) const
46 {
47  return std::abs(
48  math::distance(point1, point) + math::distance(point2, point) -
50 }
52 {
53  s = TSegment3D(*this);
54 }
56 {
57  point1 = TPoint2D(s.point1);
58  point2 = TPoint2D(s.point2);
59  if (point1 == point2)
60  throw std::logic_error("Segment is normal to projection plane");
61 }
62 
63 bool TSegment2D::operator<(const TSegment2D& s) const
64 {
65  if (point1 < s.point1)
66  return true;
67  else if (s.point1 < point1)
68  return false;
69  else
70  return point2 < s.point2;
71 }
72 
75 {
76  return in >> s.point1 >> s.point2;
77 }
80 {
81  return out << s.point1 << s.point2;
82 }
double distance(const TPoint2D &point) const
Distance to point.
Definition: TSegment2D.cpp:19
TPoint2D_< double > TPoint2D
Lightweight 2D point.
Definition: TPoint2D.h:213
double length() const
Segment length.
Definition: TSegment2D.cpp:18
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrixD::Ptr &pObj)
TPoint3D point1
origin point
Definition: TSegment3D.h:23
double signedDistance(const TPoint2D &point) const
Distance with sign to point (sign indicates which side the point is).
Definition: TSegment2D.cpp:23
This base provides a set of functions for maths stuff.
2D segment, consisting of two points.
Definition: TSegment2D.h:20
3D segment, consisting of two points.
Definition: TSegment3D.h:20
TSegment2D()=default
Fast default constructor.
TPoint3D point2
final point
Definition: TSegment3D.h:24
bool operator<(const TSegment2D &s) const
Definition: TSegment2D.cpp:63
TPoint2D point2
Destiny point.
Definition: TSegment2D.h:30
bool contains(const TPoint2D &point) const
Check whether a point is inside a segment.
Definition: TSegment2D.cpp:45
TPoint2D point1
Origin point.
Definition: TSegment2D.h:26
return_t square(const num_t x)
Inline function for the square of a number.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
double getEpsilon()
Gets the value of the geometric epsilon (default = 1e-5)
Definition: geometry.cpp:34
mrpt::vision::TStereoCalibResults out
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &s, const CVectorFloat &a)
Definition: math.cpp:626
void generate3DObject(TSegment3D &s) const
Project into 3D space, setting the z to 0.
Definition: TSegment2D.cpp:51
double distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
Definition: geometry.cpp:1807
double signedDistance(const TPoint2D &point) const
Distance with sign from a given point (sign indicates side).
Definition: TLine2D.cpp:38
2D line without bounds, represented by its equation .
Definition: TLine2D.h:19



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020