Main MRPT website > C++ reference for MRPT 1.5.5
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 
11 #include <mrpt/math/geometry.h>
12 #include <mrpt/math/CPolygon.h>
13 #include <gtest/gtest.h>
14 #include <algorithm>
15 
16 using namespace mrpt;
17 using namespace mrpt::utils;
18 using namespace mrpt::math;
19 using namespace std;
20 
21 
22 TEST(Geometry, Line2DIntersect)
23 {
24  // Two lines that should intersect at (0.5,0.5)
25  const TLine2D l1(TPoint2D(0,1), TPoint2D(1,0));
26  const TLine2D l2(TPoint2D(-1,0.5), TPoint2D(4,0.5));
27 
28  TObject2D inter;
29  bool do_inter = intersect(l1, l2, inter);
30 
31  EXPECT_TRUE(do_inter);
32  EXPECT_EQ( inter.getType(), GEOMETRIC_TYPE_POINT );
33 
34  TPoint2D i(0,0);
35  inter.getPoint(i);
36  EXPECT_NEAR(i.x, 0.5, 1e-9);
37  EXPECT_NEAR(i.y, 0.5, 1e-9);
38 }
39 
40 TEST(Geometry, Segment2DIntersect)
41 {
42  {
43  // Two segments that should intersect at (0.5,0.5)
44  const TSegment2D s1(TPoint2D(0,1), TPoint2D(1,0));
45  const TSegment2D s2(TPoint2D(-1,0.5), TPoint2D(4,0.5));
46 
47  TObject2D inter;
48  bool do_inter = intersect(s1, s2, inter);
49 
50  EXPECT_TRUE(do_inter);
51  EXPECT_EQ( inter.getType(), GEOMETRIC_TYPE_POINT );
52 
53  TPoint2D i(0,0);
54  inter.getPoint(i);
55  EXPECT_NEAR(i.x, 0.5, 1e-9);
56  EXPECT_NEAR(i.y, 0.5, 1e-9);
57  }
58 
59  {
60  // Two segments that do NOT intersect
61  const TSegment2D s1(TPoint2D(0,1), TPoint2D(1,0));
62  const TSegment2D s2(TPoint2D(0.6,0.5), TPoint2D(4,0.5));
63 
64  TObject2D inter;
65  bool do_inter = intersect(s1, s2, inter);
66 
67  EXPECT_FALSE(do_inter);
68  }
69 
70 #if 0
71  {
72  // Two parallel segments that do NOT intersect: result is a "segment in the middle".
73  const TSegment2D s1(TPoint2D(-0.05,0.05), TPoint2D(-0.05,-0.05));
74  const TSegment2D s2(TPoint2D(0,0.135), TPoint2D(0,-0.0149999));
75 
76  TObject2D inter;
77  bool do_inter = intersect(s1, s2, inter);
78 
79  EXPECT_TRUE(do_inter && inter.getType()==GEOMETRIC_TYPE_SEGMENT);
80  }
81 #endif
82 }
83 
84 void myTestPolygonContainsPoint(std::vector<TPoint2D> &vs, bool convex)
85 {
86  const mrpt::math::TPolygon2D poly(vs);
87 
88  EXPECT_EQ(poly.isConvex(),convex);
89 
90  EXPECT_TRUE( poly.contains( TPoint2D(0.0, 0.0) ) );
91  EXPECT_TRUE( poly.contains( TPoint2D(0.0, 0.9) ) );
92  EXPECT_TRUE( poly.contains( TPoint2D(-0.9, -0.9) ) );
93  EXPECT_TRUE( poly.contains( TPoint2D(0.9, -0.9) ) );
94 
95  EXPECT_FALSE( poly.contains( TPoint2D(-4.0, -5.1) ) );
96  EXPECT_FALSE( poly.contains( TPoint2D(-5.0, -0.1) ) );
97  EXPECT_FALSE( poly.contains( TPoint2D( 1.1, -6.1) ) );
98  EXPECT_FALSE( poly.contains( TPoint2D( 0, 5.1) ) );
99  EXPECT_FALSE( poly.contains( TPoint2D( 0, -1.1) ) );
100 }
101 
102 TEST(Geometry, PolygonConvexContainsPoint)
103 {
104  // Test with a polygon in one winding order:
105  std::vector<TPoint2D> vs;
106  vs.push_back(TPoint2D(-1.0, -1.0));
107  vs.push_back(TPoint2D( 0.0, 1.0));
108  vs.push_back(TPoint2D( 1.0, -1.0));
109  myTestPolygonContainsPoint(vs, true);
110 
111  // and the other:
112  std::reverse(vs.begin(),vs.end());
113  myTestPolygonContainsPoint(vs, true);
114 
115  {
117  p.AddVertex(0, -0.322);
118  p.AddVertex(-0.644, -0.322);
119  p.AddVertex(-0.210377, -0.324673);
120  p.AddVertex(0.433623, -0.324673);
121 
122  EXPECT_FALSE (p.contains(TPoint2D(0.73175, -0.325796)));
123  }
124 }
125 
126 TEST(Geometry, PolygonConcaveContainsPoint)
127 {
128  // Test with a polygon in one winding order:
129  std::vector<TPoint2D> vs;
130  vs.push_back(TPoint2D(-2.0, 3.0));
131  vs.push_back(TPoint2D( 2.0, 2.0));
132  vs.push_back(TPoint2D( 3.0, -4.0));
133  vs.push_back(TPoint2D( 0.1, -3.0));
134  vs.push_back(TPoint2D( 0.1, -0.1));
135  vs.push_back(TPoint2D(-0.1, -0.1));
136  vs.push_back(TPoint2D(-0.1, -3.0));
137  vs.push_back(TPoint2D(-2.0, -2.0));
138 
139  myTestPolygonContainsPoint(vs,false);
140 
141  // and the other:
142  std::reverse(vs.begin(),vs.end());
143  myTestPolygonContainsPoint(vs,false);
144 }
145 
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.
Definition: zip.h:16
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:25
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 works for concave...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Lightweight 2D point.
GLfloat GLfloat p
Definition: glext.h:5587
bool BASE_IMPEXP intersect(const TSegment3D &s1, const TSegment3D &s2, TObject3D &obj)
Gets the intersection between two 3D segments.
Definition: geometry.cpp:568
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.5.5 Git: e06b63dbf Fri Dec 1 14:41:11 2017 +0100 at lun oct 28 01:31:35 CET 2019