MRPT  2.0.4
TLine2D.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/TLine2D.h>
13 #include <mrpt/math/TLine3D.h>
14 #include <mrpt/math/TPoint2D.h>
15 #include <mrpt/math/TPose2D.h>
16 #include <mrpt/math/TSegment2D.h>
17 #include <mrpt/math/epsilon.h>
18 #include <mrpt/serialization/CArchive.h> // impl of << operator
19 #include <cmath>
20 
21 using namespace mrpt::math;
22 
23 static_assert(std::is_trivially_copyable_v<TLine2D>);
24 
25 TLine2D TLine2D::FromCoefficientsABC(double A, double B, double C)
26 {
27  return TLine2D(A, B, C);
28 }
29 
31 {
32  return TLine2D(p1, p2);
33 }
34 
35 double TLine2D::evaluatePoint(const TPoint2D& point) const
36 {
37  return coefs[0] * point.x + coefs[1] * point.y + coefs[2];
38 }
39 bool TLine2D::contains(const TPoint2D& point) const
40 {
41  return std::abs(distance(point)) < getEpsilon();
42 }
43 double TLine2D::distance(const TPoint2D& point) const
44 {
45  return std::abs(evaluatePoint(point)) /
46  sqrt(coefs[0] * coefs[0] + coefs[1] * coefs[1]);
47 }
48 double TLine2D::signedDistance(const TPoint2D& point) const
49 {
50  return evaluatePoint(point) /
51  sqrt(coefs[0] * coefs[0] + coefs[1] * coefs[1]);
52 }
53 void TLine2D::getNormalVector(double (&vector)[2]) const
54 {
55  vector[0] = coefs[0];
56  vector[1] = coefs[1];
57 }
59 {
60  double s = sqrt(coefs[0] * coefs[0] + coefs[1] * coefs[1]);
61  for (double& coef : coefs) coef /= s;
62 }
63 void TLine2D::getDirectorVector(double (&vector)[2]) const
64 {
65  vector[0] = -coefs[1];
66  vector[1] = coefs[0];
67 }
68 void TLine2D::generate3DObject(TLine3D& l) const { l = TLine3D(*this); }
69 void TLine2D::getAsPose2D(TPose2D& outPose) const
70 {
71  // Line's director vector is (-coefs[1],coefs[0]).
72  // If line is horizontal, force x=0. Else, force y=0. In both cases, we'll
73  // find a suitable point.
74  outPose.phi = atan2(coefs[0], -coefs[1]);
75  if (std::abs(coefs[0]) < getEpsilon())
76  {
77  outPose.x = 0;
78  outPose.y = -coefs[2] / coefs[1];
79  }
80  else
81  {
82  outPose.x = -coefs[2] / coefs[0];
83  outPose.y = 0;
84  }
85 }
87  const TPoint2D& origin, TPose2D& outPose) const
88 {
89  if (!contains(origin))
90  throw std::logic_error("Base point is not contained in the line");
91  outPose = origin;
92  // Line's director vector is (-coefs[1],coefs[0]).
93  outPose.phi = atan2(coefs[0], -coefs[1]);
94 }
95 TLine2D::TLine2D(const TPoint2D& p1, const TPoint2D& p2)
96 {
97  if (p1 == p2) throw std::logic_error("Both points are the same");
98  coefs[0] = p2.y - p1.y;
99  coefs[1] = p1.x - p2.x;
100  coefs[2] = p2.x * p1.y - p2.y * p1.x;
101 }
103 {
104  coefs[0] = s.point2.y - s.point1.y;
105  coefs[1] = s.point1.x - s.point2.x;
106  coefs[2] = s.point2.x * s.point1.y - s.point2.y * s.point1.x;
107  // unitarize(); //¿?
108 }
110 {
111  // Line's projection to Z plane may be a point.
112  if (hypot(l.director[0], l.director[1]) < getEpsilon())
113  throw std::logic_error("Line is normal to projection plane");
114  coefs[0] = -l.director[1];
115  coefs[1] = l.director[0];
116  coefs[2] = l.pBase.x * l.director[1] - l.pBase.y * l.director[0];
117 }
118 
121 {
122  return in >> l.coefs[0] >> l.coefs[1] >> l.coefs[2];
123 }
126 {
127  return out << l.coefs[0] << l.coefs[1] << l.coefs[2];
128 }
void getDirectorVector(double(&vector)[2]) const
Get line&#39;s director vector.
Definition: TLine2D.cpp:63
double x
X,Y coordinates.
Definition: TPose2D.h:30
T x
X,Y coordinates.
Definition: TPoint2D.h:25
TPoint3D pBase
Base point.
Definition: TLine3D.h:47
void unitarize()
Unitarize line&#39;s normal vector.
Definition: TLine2D.cpp:58
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
double evaluatePoint(const TPoint2D &point) const
Evaluate point in the line&#39;s equation.
Definition: TLine2D.cpp:35
void generate3DObject(TLine3D &l) const
Project into 3D space, setting the z to 0.
Definition: TLine2D.cpp:68
void getAsPose2D(TPose2D &outPose) const
Definition: TLine2D.cpp:69
double distance(const TPoint2D &point) const
Distance from a given point.
Definition: TLine2D.cpp:43
TVector3D director
Director vector.
Definition: TLine3D.h:49
static TLine2D FromCoefficientsABC(double A, double B, double C)
Static constructor from Ax+By+C=0 coefficients.
Definition: TLine2D.cpp:25
void getAsPose2DForcingOrigin(const TPoint2D &origin, TPose2D &outPose) const
Definition: TLine2D.cpp:86
TPoint2D point2
Destiny point.
Definition: TSegment2D.h:30
TPoint2D point1
Origin point.
Definition: TSegment2D.h:26
T x
X,Y,Z coordinates.
Definition: TPoint3D.h:29
std::array< double, 3 > coefs
Line coefficients, stored as an array: .
Definition: TLine2D.h:52
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
Lightweight 2D pose.
Definition: TPose2D.h:22
bool contains(const TPoint2D &point) const
Check whether a point is inside the line.
Definition: TLine2D.cpp:39
static TLine2D FromTwoPoints(const TPoint2D &p1, const TPoint2D &p2)
Static constructor from two points.
Definition: TLine2D.cpp:30
TLine2D()=default
Fast default constructor.
double phi
Orientation (rads)
Definition: TPose2D.h:32
void getNormalVector(double(&vector)[2]) const
Get line&#39;s normal vector.
Definition: TLine2D.cpp:53
double signedDistance(const TPoint2D &point) const
Distance with sign from a given point (sign indicates side).
Definition: TLine2D.cpp:48
3D line, represented by a base point and a director vector.
Definition: TLine3D.h:19
2D line without bounds, represented by its equation .
Definition: TLine2D.h:19



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020