12 #include <gtest/gtest.h> 16 #include <Eigen/Dense> 19 using namespace Eigen;
30 expectedCentroid.x() = 1.0 / 3.0 * (1.0 + 0.5);
31 expectedCentroid.y() = 1.0 / 3.0;
33 EXPECT_DOUBLE_EQ(expectedCentroid.x(), centroid.x());
34 EXPECT_DOUBLE_EQ(expectedCentroid.y(), centroid.y());
40 rectangle.
addVertex(Vector2d(-2.0, -1.0));
45 Position expectedCentroid(-0.5, 0.5);
47 EXPECT_DOUBLE_EQ(expectedCentroid.x(), centroid.x());
48 EXPECT_DOUBLE_EQ(expectedCentroid.y(), centroid.y());
59 Length expectedLength(1.0, 1.2);
64 EXPECT_DOUBLE_EQ(expectedCenter.x(), center.x());
65 EXPECT_DOUBLE_EQ(expectedCenter.y(), center.y());
66 EXPECT_DOUBLE_EQ(expectedLength.x(), length.x());
67 EXPECT_DOUBLE_EQ(expectedLength.y(), length.y());
73 std::vector<Position> points1;
74 points1.push_back(Vector2d(0.0, 0.0));
75 points1.push_back(Vector2d(1.0, 0.0));
76 points1.push_back(Vector2d(1.0, 1.0));
77 points1.push_back(Vector2d(0.0, 1.0));
78 Polygon polygon1 = Polygon::monotoneChainConvexHullOfPoints(points1);
80 EXPECT_TRUE(polygon1.
isInside(Vector2d(0.5, 0.5)));
81 EXPECT_FALSE(polygon1.
isInside(Vector2d(-0.01, 0.5)));
84 std::vector<Position> points2;
85 points2.push_back(Vector2d(0.0, 0.0));
86 points2.push_back(Vector2d(1.0, 0.0));
87 points2.push_back(Vector2d(2.0, 1.0));
88 points2.push_back(Vector2d(1.0, 2.0));
89 points2.push_back(Vector2d(-1.0, 2.0));
90 points2.push_back(Vector2d(-1.0, -2.0));
91 points2.push_back(Vector2d(0.0, 1.0));
92 points2.push_back(Vector2d(1.0, 1.0));
93 Polygon polygon2 = Polygon::monotoneChainConvexHullOfPoints(points2);
95 EXPECT_TRUE(polygon2.
isInside(Vector2d(0.5, 0.5)));
96 EXPECT_TRUE(polygon2.
isInside(Vector2d(0.0, 1.0)));
97 EXPECT_TRUE(polygon2.
isInside(Vector2d(-0.5, -0.5)));
98 EXPECT_FALSE(polygon2.
isInside(Vector2d(2.0, 0.0)));
99 EXPECT_FALSE(polygon2.
isInside(Vector2d(-0.5, -2)));
100 EXPECT_FALSE(polygon2.
isInside(Vector2d(1.75, 1.75)));
117 Polygon hull = Polygon::convexHull(polygon1, polygon2);
120 EXPECT_TRUE(hull.
isInside(Vector2d(0.5, 0.5)));
121 EXPECT_FALSE(hull.
isInside(Vector2d(0.01, 1.49)));
129 const int nVertices = 15;
131 Polygon hull = Polygon::convexHullOfTwoCircles(center1, center2, radius);
133 EXPECT_TRUE(hull.
isInside(Vector2d(-0.25, 0.0)));
134 EXPECT_TRUE(hull.
isInside(Vector2d(0.5, 0.0)));
135 EXPECT_TRUE(hull.
isInside(Vector2d(0.5, 0.4)));
136 EXPECT_FALSE(hull.
isInside(Vector2d(0.5, 0.6)));
137 EXPECT_FALSE(hull.
isInside(Vector2d(1.5, 0.2)));
139 hull = Polygon::convexHullOfTwoCircles(center1, center2, radius, nVertices);
140 EXPECT_EQ(nVertices + 1, hull.nVertices());
141 EXPECT_TRUE(hull.isInside(Vector2d(-0.25, 0.0)));
142 EXPECT_TRUE(hull.isInside(Vector2d(0.5, 0.0)));
143 EXPECT_TRUE(hull.isInside(Vector2d(0.5, 0.4)));
144 EXPECT_FALSE(hull.isInside(Vector2d(0.5, 0.6)));
145 EXPECT_FALSE(hull.isInside(Vector2d(1.5, 0.2)));
147 hull = Polygon::convexHullOfTwoCircles(center1, center1, radius);
148 EXPECT_EQ(20, hull.nVertices());
149 EXPECT_TRUE(hull.isInside(Vector2d(-0.25, 0.0)));
150 EXPECT_TRUE(hull.isInside(Vector2d(0.25, 0.0)));
151 EXPECT_TRUE(hull.isInside(Vector2d(0.0, 0.25)));
152 EXPECT_TRUE(hull.isInside(Vector2d(0.0, -0.25)));
153 EXPECT_FALSE(hull.isInside(Vector2d(0.5, 0.5)));
154 EXPECT_FALSE(hull.isInside(Vector2d(0.6, 0.0)));
155 EXPECT_FALSE(hull.isInside(Vector2d(-0.6, 0.0)));
156 EXPECT_FALSE(hull.isInside(Vector2d(0.0, 0.6)));
157 EXPECT_FALSE(hull.isInside(Vector2d(0.0, -0.6)));
159 hull = Polygon::convexHullOfTwoCircles(center1, center1, radius, nVertices);
160 EXPECT_EQ(nVertices, hull.nVertices());
161 EXPECT_TRUE(hull.isInside(Vector2d(-0.25, 0.0)));
162 EXPECT_TRUE(hull.isInside(Vector2d(0.25, 0.0)));
163 EXPECT_TRUE(hull.isInside(Vector2d(0.0, 0.25)));
164 EXPECT_TRUE(hull.isInside(Vector2d(0.0, -0.25)));
165 EXPECT_FALSE(hull.isInside(Vector2d(0.5, 0.5)));
166 EXPECT_FALSE(hull.isInside(Vector2d(0.6, 0.0)));
167 EXPECT_FALSE(hull.isInside(Vector2d(-0.6, 0.0)));
168 EXPECT_FALSE(hull.isInside(Vector2d(0.0, 0.6)));
169 EXPECT_FALSE(hull.isInside(Vector2d(0.0, -0.6)));
176 const int nVertices = 15;
178 Polygon hull = Polygon::fromCircle(center, radius);
181 EXPECT_TRUE(hull.
isInside(Vector2d(-0.25, 0.0)));
182 EXPECT_TRUE(hull.
isInside(Vector2d(0.49, 0.0)));
183 EXPECT_FALSE(hull.
isInside(Vector2d(0.5, 0.4)));
184 EXPECT_FALSE(hull.
isInside(Vector2d(1.0, 0.0)));
186 hull = Polygon::fromCircle(center, radius, nVertices);
187 EXPECT_EQ(nVertices, hull.nVertices());
188 EXPECT_TRUE(hull.isInside(Vector2d(-0.25, 0.0)));
189 EXPECT_TRUE(hull.isInside(Vector2d(0.49, 0.0)));
190 EXPECT_FALSE(hull.isInside(Vector2d(0.5, 0.4)));
191 EXPECT_FALSE(hull.isInside(Vector2d(1.0, 0.0)));
194 TEST(convertToInequalityConstraints, triangle1)
199 ASSERT_TRUE(polygon.convertToInequalityConstraints(A, b));
200 EXPECT_NEAR(-1.3636, A(0, 0), 1e-4);
201 EXPECT_NEAR( 1.3636, A(0, 1), 1e-4);
202 EXPECT_NEAR(-1.5000, A(1, 0), 1e-4);
203 EXPECT_NEAR(-1.5000, A(1, 1), 1e-4);
204 EXPECT_NEAR( 2.8636, A(2, 0), 1e-4);
205 EXPECT_NEAR( 0.1364, A(2, 1), 1e-4);
206 EXPECT_NEAR( 0.0000, b(0), 1e-4);
207 EXPECT_NEAR( 0.0000, b(1), 1e-4);
208 EXPECT_NEAR( 3.0000, b(2), 1e-4);
211 TEST(convertToInequalityConstraints, triangle2)
216 ASSERT_TRUE(polygon.convertToInequalityConstraints(A, b));
217 EXPECT_NEAR(-1.5000, A(0, 0), 1e-4);
218 EXPECT_NEAR( 0.0000, A(0, 1), 1e-4);
219 EXPECT_NEAR( 0.0000, A(1, 0), 1e-4);
220 EXPECT_NEAR(-3.0000, A(1, 1), 1e-4);
221 EXPECT_NEAR( 1.5000, A(2, 0), 1e-4);
222 EXPECT_NEAR( 3.0000, A(2, 1), 1e-4);
223 EXPECT_NEAR( 1.5000, b(0), 1e-4);
224 EXPECT_NEAR( 1.5000, b(1), 1e-4);
225 EXPECT_NEAR( 0.0000, b(2), 1e-4);
232 EXPECT_NEAR(0.9, polygon.getVertex(0)(0), 1e-4);
233 EXPECT_NEAR(0.758579, polygon.getVertex(0)(1), 1e-4);
234 EXPECT_NEAR(0.141421, polygon.getVertex(1)(0), 1e-4);
235 EXPECT_NEAR(0.0, polygon.getVertex(1)(1), 1e-4);
236 EXPECT_NEAR(0.9, polygon.getVertex(2)(0), 1e-4);
237 EXPECT_NEAR(-0.758579, polygon.getVertex(2)(1), 1e-4);
243 std::vector<Polygon> polygons;
245 ASSERT_EQ(1, polygons.size());
246 EXPECT_EQ(polygon.getVertex(0).x(), polygons[0].getVertex(0).x());
247 EXPECT_EQ(polygon.getVertex(0).y(), polygons[0].getVertex(0).y());
248 EXPECT_EQ(polygon.getVertex(1).x(), polygons[0].getVertex(1).x());
249 EXPECT_EQ(polygon.getVertex(1).y(), polygons[0].getVertex(1).y());
250 EXPECT_EQ(polygon.getVertex(2).x(), polygons[0].getVertex(2).x());
251 EXPECT_EQ(polygon.getVertex(2).y(), polygons[0].getVertex(2).y());
257 rectangle.
addVertex(Vector2d(-2.0, -1.0));
258 rectangle.
addVertex(Vector2d(-2.0, 2.0));
260 rectangle.
addVertex(Vector2d(1.0, -1.0));
261 std::vector<Polygon> polygons;
263 ASSERT_EQ(2, polygons.size());
std::vector< Polygon > triangulate(const TriangulationMethods &method=TriangulationMethods::FAN) const
Position getCentroid() const
TEST(Polygon, getCentroidTriangle)
bool isInside(const Position &point) const
void addVertex(const Position &vertex)
void getBoundingBox(Position ¢er, Length &length) const
bool offsetInward(const double margin)