12 #include <gtest/gtest.h> 17 using Eigen::MatrixXd;
18 using Eigen::Vector2d;
19 using Eigen::VectorXd;
33 expectedCentroid.x() = 1.0 / 3.0 * (1.0 + 0.5);
34 expectedCentroid.y() = 1.0 / 3.0;
36 EXPECT_DOUBLE_EQ(expectedCentroid.x(), centroid.x());
37 EXPECT_DOUBLE_EQ(expectedCentroid.y(), centroid.y());
43 rectangle.
addVertex(Vector2d(-2.0, -1.0));
48 Position expectedCentroid(-0.5, 0.5);
50 EXPECT_DOUBLE_EQ(expectedCentroid.x(), centroid.x());
51 EXPECT_DOUBLE_EQ(expectedCentroid.y(), centroid.y());
62 Length expectedLength(1.0, 1.2);
67 EXPECT_DOUBLE_EQ(expectedCenter.x(), center.x());
68 EXPECT_DOUBLE_EQ(expectedCenter.y(), center.y());
69 EXPECT_DOUBLE_EQ(expectedLength.x(), length.x());
70 EXPECT_DOUBLE_EQ(expectedLength.y(), length.y());
76 std::vector<Position> points1;
77 points1.emplace_back(0.0, 0.0);
78 points1.emplace_back(1.0, 0.0);
79 points1.emplace_back(1.0, 1.0);
80 points1.emplace_back(0.0, 1.0);
81 Polygon polygon1 = Polygon::monotoneChainConvexHullOfPoints(points1);
83 EXPECT_TRUE(polygon1.
isInside(Vector2d(0.5, 0.5)));
84 EXPECT_FALSE(polygon1.
isInside(Vector2d(-0.01, 0.5)));
87 std::vector<Position> points2;
88 points2.emplace_back(0.0, 0.0);
89 points2.emplace_back(1.0, 0.0);
90 points2.emplace_back(2.0, 1.0);
91 points2.emplace_back(1.0, 2.0);
92 points2.emplace_back(-1.0, 2.0);
93 points2.emplace_back(-1.0, -2.0);
94 points2.emplace_back(0.0, 1.0);
95 points2.emplace_back(1.0, 1.0);
96 Polygon polygon2 = Polygon::monotoneChainConvexHullOfPoints(points2);
98 EXPECT_TRUE(polygon2.
isInside(Vector2d(0.5, 0.5)));
99 EXPECT_TRUE(polygon2.
isInside(Vector2d(0.0, 1.0)));
100 EXPECT_TRUE(polygon2.
isInside(Vector2d(-0.5, -0.5)));
101 EXPECT_FALSE(polygon2.
isInside(Vector2d(2.0, 0.0)));
102 EXPECT_FALSE(polygon2.
isInside(Vector2d(-0.5, -2)));
103 EXPECT_FALSE(polygon2.
isInside(Vector2d(1.75, 1.75)));
120 Polygon hull = Polygon::convexHull(polygon1, polygon2);
123 EXPECT_TRUE(hull.
isInside(Vector2d(0.5, 0.5)));
124 EXPECT_FALSE(hull.
isInside(Vector2d(0.01, 1.49)));
132 const int nVertices = 15;
134 Polygon hull = Polygon::convexHullOfTwoCircles(center1, center2, radius);
136 EXPECT_TRUE(hull.
isInside(Vector2d(-0.25, 0.0)));
137 EXPECT_TRUE(hull.
isInside(Vector2d(0.5, 0.0)));
138 EXPECT_TRUE(hull.
isInside(Vector2d(0.5, 0.4)));
139 EXPECT_FALSE(hull.
isInside(Vector2d(0.5, 0.6)));
140 EXPECT_FALSE(hull.
isInside(Vector2d(1.5, 0.2)));
142 hull = Polygon::convexHullOfTwoCircles(center1, center2, radius, nVertices);
143 EXPECT_EQ(nVertices + 1, hull.nVertices());
144 EXPECT_TRUE(hull.isInside(Vector2d(-0.25, 0.0)));
145 EXPECT_TRUE(hull.isInside(Vector2d(0.5, 0.0)));
146 EXPECT_TRUE(hull.isInside(Vector2d(0.5, 0.4)));
147 EXPECT_FALSE(hull.isInside(Vector2d(0.5, 0.6)));
148 EXPECT_FALSE(hull.isInside(Vector2d(1.5, 0.2)));
150 hull = Polygon::convexHullOfTwoCircles(center1, center1, radius);
151 EXPECT_EQ(20, hull.nVertices());
152 EXPECT_TRUE(hull.isInside(Vector2d(-0.25, 0.0)));
153 EXPECT_TRUE(hull.isInside(Vector2d(0.25, 0.0)));
154 EXPECT_TRUE(hull.isInside(Vector2d(0.0, 0.25)));
155 EXPECT_TRUE(hull.isInside(Vector2d(0.0, -0.25)));
156 EXPECT_FALSE(hull.isInside(Vector2d(0.5, 0.5)));
157 EXPECT_FALSE(hull.isInside(Vector2d(0.6, 0.0)));
158 EXPECT_FALSE(hull.isInside(Vector2d(-0.6, 0.0)));
159 EXPECT_FALSE(hull.isInside(Vector2d(0.0, 0.6)));
160 EXPECT_FALSE(hull.isInside(Vector2d(0.0, -0.6)));
162 hull = Polygon::convexHullOfTwoCircles(center1, center1, radius, nVertices);
163 EXPECT_EQ(nVertices, hull.nVertices());
164 EXPECT_TRUE(hull.isInside(Vector2d(-0.25, 0.0)));
165 EXPECT_TRUE(hull.isInside(Vector2d(0.25, 0.0)));
166 EXPECT_TRUE(hull.isInside(Vector2d(0.0, 0.25)));
167 EXPECT_TRUE(hull.isInside(Vector2d(0.0, -0.25)));
168 EXPECT_FALSE(hull.isInside(Vector2d(0.5, 0.5)));
169 EXPECT_FALSE(hull.isInside(Vector2d(0.6, 0.0)));
170 EXPECT_FALSE(hull.isInside(Vector2d(-0.6, 0.0)));
171 EXPECT_FALSE(hull.isInside(Vector2d(0.0, 0.6)));
172 EXPECT_FALSE(hull.isInside(Vector2d(0.0, -0.6)));
179 const int nVertices = 15;
181 Polygon hull = Polygon::fromCircle(center, radius);
184 EXPECT_TRUE(hull.
isInside(Vector2d(-0.25, 0.0)));
185 EXPECT_TRUE(hull.
isInside(Vector2d(0.49, 0.0)));
186 EXPECT_FALSE(hull.
isInside(Vector2d(0.5, 0.4)));
187 EXPECT_FALSE(hull.
isInside(Vector2d(1.0, 0.0)));
189 hull = Polygon::fromCircle(center, radius, nVertices);
190 EXPECT_EQ(nVertices, hull.nVertices());
191 EXPECT_TRUE(hull.isInside(Vector2d(-0.25, 0.0)));
192 EXPECT_TRUE(hull.isInside(Vector2d(0.49, 0.0)));
193 EXPECT_FALSE(hull.isInside(Vector2d(0.5, 0.4)));
194 EXPECT_FALSE(hull.isInside(Vector2d(1.0, 0.0)));
197 TEST(convertToInequalityConstraints, triangle1)
202 ASSERT_TRUE(polygon.convertToInequalityConstraints(A, b));
203 EXPECT_NEAR(-1.3636, A(0, 0), 1e-4);
204 EXPECT_NEAR( 1.3636, A(0, 1), 1e-4);
205 EXPECT_NEAR(-1.5000, A(1, 0), 1e-4);
206 EXPECT_NEAR(-1.5000, A(1, 1), 1e-4);
207 EXPECT_NEAR( 2.8636, A(2, 0), 1e-4);
208 EXPECT_NEAR( 0.1364, A(2, 1), 1e-4);
209 EXPECT_NEAR( 0.0000, b(0), 1e-4);
210 EXPECT_NEAR( 0.0000, b(1), 1e-4);
211 EXPECT_NEAR( 3.0000, b(2), 1e-4);
214 TEST(convertToInequalityConstraints, triangle2)
219 ASSERT_TRUE(polygon.convertToInequalityConstraints(A, b));
220 EXPECT_NEAR(-1.5000, A(0, 0), 1e-4);
221 EXPECT_NEAR( 0.0000, A(0, 1), 1e-4);
222 EXPECT_NEAR( 0.0000, A(1, 0), 1e-4);
223 EXPECT_NEAR(-3.0000, A(1, 1), 1e-4);
224 EXPECT_NEAR( 1.5000, A(2, 0), 1e-4);
225 EXPECT_NEAR( 3.0000, A(2, 1), 1e-4);
226 EXPECT_NEAR( 1.5000, b(0), 1e-4);
227 EXPECT_NEAR( 1.5000, b(1), 1e-4);
228 EXPECT_NEAR( 0.0000, b(2), 1e-4);
235 EXPECT_NEAR(0.9, polygon.getVertex(0)(0), 1e-4);
236 EXPECT_NEAR(0.758579, polygon.getVertex(0)(1), 1e-4);
237 EXPECT_NEAR(0.141421, polygon.getVertex(1)(0), 1e-4);
238 EXPECT_NEAR(0.0, polygon.getVertex(1)(1), 1e-4);
239 EXPECT_NEAR(0.9, polygon.getVertex(2)(0), 1e-4);
240 EXPECT_NEAR(-0.758579, polygon.getVertex(2)(1), 1e-4);
246 std::vector<Polygon> polygons;
248 ASSERT_EQ(1, polygons.size());
249 EXPECT_EQ(polygon.getVertex(0).x(), polygons[0].getVertex(0).x());
250 EXPECT_EQ(polygon.getVertex(0).y(), polygons[0].getVertex(0).y());
251 EXPECT_EQ(polygon.getVertex(1).x(), polygons[0].getVertex(1).x());
252 EXPECT_EQ(polygon.getVertex(1).y(), polygons[0].getVertex(1).y());
253 EXPECT_EQ(polygon.getVertex(2).x(), polygons[0].getVertex(2).x());
254 EXPECT_EQ(polygon.getVertex(2).y(), polygons[0].getVertex(2).y());
260 rectangle.
addVertex(Vector2d(-2.0, -1.0));
261 rectangle.
addVertex(Vector2d(-2.0, 2.0));
263 rectangle.
addVertex(Vector2d(1.0, -1.0));
264 std::vector<Polygon> polygons;
266 ASSERT_EQ(2, polygons.size());
Position getCentroid() const
TEST(Polygon, getCentroidTriangle)
void addVertex(const Position &vertex)
bool offsetInward(double margin)
bool isInside(const Position &point) const
std::vector< Polygon > triangulate(const TriangulationMethods &method=TriangulationMethods::FAN) const
void getBoundingBox(Position ¢er, Length &length) const