Main MRPT website > C++ reference for MRPT 1.9.9
geometry_unittest.cpp
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 
10 #include <mrpt/math/geometry.h>
11 #include <mrpt/math/CPolygon.h>
12 #include <gtest/gtest.h>
13 #include <algorithm>
14 
15 using namespace mrpt;
16 using namespace mrpt::utils;
17 using namespace mrpt::math;
18 using namespace std;
19 
20 TEST(Geometry, Line2DIntersect)
21 {
22  // Two lines that should intersect at (0.5,0.5)
23  const TLine2D l1(TPoint2D(0, 1), TPoint2D(1, 0));
24  const TLine2D l2(TPoint2D(-1, 0.5), TPoint2D(4, 0.5));
25 
26  TObject2D inter;
27  bool do_inter = intersect(l1, l2, inter);
28 
29  EXPECT_TRUE(do_inter);
30  EXPECT_EQ(inter.getType(), GEOMETRIC_TYPE_POINT);
31 
32  TPoint2D i(0, 0);
33  inter.getPoint(i);
34  EXPECT_NEAR(i.x, 0.5, 1e-9);
35  EXPECT_NEAR(i.y, 0.5, 1e-9);
36 }
37 
38 TEST(Geometry, Segment2DIntersect)
39 {
40  {
41  // Two segments that should intersect at (0.5,0.5)
42  const TSegment2D s1(TPoint2D(0, 1), TPoint2D(1, 0));
43  const TSegment2D s2(TPoint2D(-1, 0.5), TPoint2D(4, 0.5));
44 
45  TObject2D inter;
46  bool do_inter = intersect(s1, s2, inter);
47 
48  EXPECT_TRUE(do_inter);
49  EXPECT_EQ(inter.getType(), GEOMETRIC_TYPE_POINT);
50 
51  TPoint2D i(0, 0);
52  inter.getPoint(i);
53  EXPECT_NEAR(i.x, 0.5, 1e-9);
54  EXPECT_NEAR(i.y, 0.5, 1e-9);
55  }
56 
57  {
58  // Two segments that do NOT intersect
59  const TSegment2D s1(TPoint2D(0, 1), TPoint2D(1, 0));
60  const TSegment2D s2(TPoint2D(0.6, 0.5), TPoint2D(4, 0.5));
61 
62  TObject2D inter;
63  bool do_inter = intersect(s1, s2, inter);
64 
65  EXPECT_FALSE(do_inter);
66  }
67 
68 #if 0
69  {
70  // Two parallel segments that do NOT intersect: result is a "segment in the middle".
71  const TSegment2D s1(TPoint2D(-0.05,0.05), TPoint2D(-0.05,-0.05));
72  const TSegment2D s2(TPoint2D(0,0.135), TPoint2D(0,-0.0149999));
73 
74  TObject2D inter;
75  bool do_inter = intersect(s1, s2, inter);
76 
77  EXPECT_TRUE(do_inter && inter.getType()==GEOMETRIC_TYPE_SEGMENT);
78  }
79 #endif
80 }
81 
82 void myTestPolygonContainsPoint(std::vector<TPoint2D>& vs, bool convex)
83 {
84  const mrpt::math::TPolygon2D poly(vs);
85 
86  EXPECT_EQ(poly.isConvex(), convex);
87 
88  EXPECT_TRUE(poly.contains(TPoint2D(0.0, 0.0)));
89  EXPECT_TRUE(poly.contains(TPoint2D(0.0, 0.9)));
90  EXPECT_TRUE(poly.contains(TPoint2D(-0.9, -0.9)));
91  EXPECT_TRUE(poly.contains(TPoint2D(0.9, -0.9)));
92 
93  EXPECT_FALSE(poly.contains(TPoint2D(-4.0, -5.1)));
94  EXPECT_FALSE(poly.contains(TPoint2D(-5.0, -0.1)));
95  EXPECT_FALSE(poly.contains(TPoint2D(1.1, -6.1)));
96  EXPECT_FALSE(poly.contains(TPoint2D(0, 5.1)));
97  EXPECT_FALSE(poly.contains(TPoint2D(0, -1.1)));
98 }
99 
100 TEST(Geometry, PolygonConvexContainsPoint)
101 {
102  // Test with a polygon in one winding order:
103  std::vector<TPoint2D> vs;
104  vs.push_back(TPoint2D(-1.0, -1.0));
105  vs.push_back(TPoint2D(0.0, 1.0));
106  vs.push_back(TPoint2D(1.0, -1.0));
107  myTestPolygonContainsPoint(vs, true);
108 
109  // and the other:
110  std::reverse(vs.begin(), vs.end());
111  myTestPolygonContainsPoint(vs, true);
112 
113  {
115  p.AddVertex(0, -0.322);
116  p.AddVertex(-0.644, -0.322);
117  p.AddVertex(-0.210377, -0.324673);
118  p.AddVertex(0.433623, -0.324673);
119 
120  EXPECT_FALSE(p.contains(TPoint2D(0.73175, -0.325796)));
121  }
122 }
123 
124 TEST(Geometry, PolygonConcaveContainsPoint)
125 {
126  // Test with a polygon in one winding order:
127  std::vector<TPoint2D> vs;
128  vs.push_back(TPoint2D(-2.0, 3.0));
129  vs.push_back(TPoint2D(2.0, 2.0));
130  vs.push_back(TPoint2D(3.0, -4.0));
131  vs.push_back(TPoint2D(0.1, -3.0));
132  vs.push_back(TPoint2D(0.1, -0.1));
133  vs.push_back(TPoint2D(-0.1, -0.1));
134  vs.push_back(TPoint2D(-0.1, -3.0));
135  vs.push_back(TPoint2D(-2.0, -2.0));
136 
137  myTestPolygonContainsPoint(vs, false);
138 
139  // and the other:
140  std::reverse(vs.begin(), vs.end());
141  myTestPolygonContainsPoint(vs, false);
142 }
bool getPoint(TPoint2D &p) const
Gets the content as a point, returning false if the type is inadequate.
void myTestPolygonContainsPoint(std::vector< TPoint2D > &vs, bool convex)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
unsigned char getType() const
Gets content type.
const unsigned char GEOMETRIC_TYPE_POINT
Object type identifier for TPoint2D or TPoint3D.
Standard type for storing any lightweight 2D type.
A wrapper of a TPolygon2D class, implementing CSerializable.
Definition: CPolygon.h:22
STL namespace.
TEST(Geometry, Line2DIntersect)
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
2D segment, consisting of two points.
const unsigned char GEOMETRIC_TYPE_SEGMENT
Object type identifier for TSegment2D or TSegment3D.
bool isConvex() const
Checks whether is convex.
bool contains(const TPoint2D &point) const
Check whether a point is inside (or within geometryEpsilon of a polygon edge).
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Lightweight 2D point.
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>.
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