43 for (
size_t i = 0; i < res.
trapezoids.size(); ++i)
88TEST(TrapezoidalMap, SingleSegment)
101 auto loc = res.locate(
Point(2, 4));
102 EXPECT_EQ(loc.type, LT::TRAPEZOID);
105 loc = res.locate(
Point(2, -2));
106 EXPECT_EQ(loc.type, LT::TRAPEZOID);
109 loc = res.locate(
Point(-2, 0));
110 EXPECT_EQ(loc.type, LT::TRAPEZOID);
113 loc = res.locate(
Point(6, 1));
114 EXPECT_EQ(loc.type, LT::TRAPEZOID);
117TEST(TrapezoidalMap, TwoParallelSegments)
127 auto loc = res.locate(
Point(2, 1));
128 EXPECT_EQ(loc.type, LT::TRAPEZOID);
131 loc = res.locate(
Point(2, 4));
132 EXPECT_EQ(loc.type, LT::TRAPEZOID);
135 loc = res.locate(
Point(2, -2));
136 EXPECT_EQ(loc.type, LT::TRAPEZOID);
139TEST(TrapezoidalMap, TwoSegmentsSharedEndpoint)
150 auto loc = res.locate(
Point(2, 0));
151 EXPECT_EQ(loc.type, LT::TRAPEZOID);
154 loc = res.locate(
Point(0, 0));
155 EXPECT_EQ(loc.type, LT::ON_POINT);
158TEST(TrapezoidalMap, SquarePolygon)
166 EXPECT_TRUE(res.contains(
Point(2, 2)));
167 EXPECT_TRUE(res.contains(
Point(1, 1)));
168 EXPECT_TRUE(res.contains(
Point(3, 3)));
171 EXPECT_FALSE(res.contains(
Point(5, 5)));
172 EXPECT_FALSE(res.contains(
Point(-1, -1)));
173 EXPECT_FALSE(res.contains(
Point(2, 5)));
176 EXPECT_TRUE(res.contains(
Point(0, 2)));
179TEST(TrapezoidalMap, TrianglePolygon)
187 EXPECT_TRUE(res.contains(
Point(2, 2)));
188 EXPECT_TRUE(res.contains(
Point(3, 1)));
191 EXPECT_FALSE(res.contains(
Point(0, 6)));
192 EXPECT_FALSE(res.contains(
Point(-1, -1)));
195 auto loc = res.locate(
Point(0, 0));
196 EXPECT_EQ(loc.type, LT::ON_POINT);
201 EXPECT_TRUE(res.contains(
Point(3, 0)));
208TEST(TrapezoidalMap, VerticalSegment)
217 auto loc = res.locate(
Point(0, 2));
218 EXPECT_EQ(loc.type, LT::TRAPEZOID);
221 loc = res.locate(
Point(4, 2));
222 EXPECT_EQ(loc.type, LT::TRAPEZOID);
225TEST(TrapezoidalMap, SharedEndpoints)
228 const Point center(0, 0);
239 auto loc = res.locate(center);
240 EXPECT_EQ(loc.type, LT::ON_POINT);
243 loc = res.locate(
Point(2, 2));
244 EXPECT_EQ(loc.type, LT::TRAPEZOID);
245 loc = res.locate(
Point(-2, 2));
246 EXPECT_EQ(loc.type, LT::TRAPEZOID);
247 loc = res.locate(
Point(-2, -2));
248 EXPECT_EQ(loc.type, LT::TRAPEZOID);
249 loc = res.locate(
Point(2, -2));
250 EXPECT_EQ(loc.type, LT::TRAPEZOID);
253TEST(TrapezoidalMap, PointOnSegment)
262 auto loc = res.locate(
Point(2, 0));
263 EXPECT_EQ(loc.type, LT::ON_SEGMENT);
266TEST(TrapezoidalMap, PointAtEndpoint)
274 auto loc = res.locate(
Point(0, 0));
275 EXPECT_EQ(loc.type, LT::ON_POINT);
277 loc = res.locate(
Point(4, 2));
278 EXPECT_EQ(loc.type, LT::ON_POINT);
281TEST(TrapezoidalMap, HorizontalSegment)
290 auto loc = res.locate(
Point(3, 5));
291 EXPECT_EQ(loc.type, LT::TRAPEZOID);
294 loc = res.locate(
Point(3, 1));
295 EXPECT_EQ(loc.type, LT::TRAPEZOID);
298 loc = res.locate(
Point(3, 3));
299 EXPECT_EQ(loc.type, LT::ON_SEGMENT);
306TEST(TrapezoidalMap, ConvexPolygonQueries)
314 for (
int x = 0; x <= 10; ++x)
315 for (
int y = 0;
y <= 10; ++
y)
318 const bool tm_inside = res.contains(p);
320 const bool winding_inside =
321 winding_loc != PointInPolygonWinding::Location::Outside;
323 EXPECT_EQ(tm_inside, winding_inside)
324 <<
"Mismatch at (" << x <<
", " <<
y <<
"): "
325 <<
"tm=" << tm_inside <<
" winding=" << winding_inside;
329TEST(TrapezoidalMap, MultiplePolygons)
349 auto res = tm(polys);
352 EXPECT_TRUE(res.contains(
Point(2, 1)));
354 EXPECT_TRUE(res.contains(
Point(8, 1)));
356 EXPECT_FALSE(res.contains(
Point(5, 1)));
358 EXPECT_FALSE(res.contains(
Point(5, 5)));
361TEST(TrapezoidalMap, ManySegments)
390 for (
size_t i = 0; i < res.num_input_points; ++i)
392 auto loc = res.locate(res.points(i));
393 EXPECT_TRUE(loc.type == LT::ON_POINT or loc.type == LT::ON_SEGMENT)
394 <<
"Point " << i <<
" not located as ON_POINT or ON_SEGMENT";
398TEST(TrapezoidalMap, Deterministic)
408 for (
int x = -1; x <= 5; ++x)
409 for (
int y = -1;
y <= 5; ++
y)
412 EXPECT_EQ(res1.contains(p), res2.contains(p))
413 <<
"Non-deterministic result at (" << x <<
", " <<
y <<
")";
421TEST(TrapezoidalMap, EmptyInput)
432 auto loc = res.locate(
Point(0, 0));
433 EXPECT_EQ(loc.type, LT::TRAPEZOID);
436TEST(TrapezoidalMap, DegenerateSegment)
442 EXPECT_THROW(tm(segs), std::domain_error);
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
static Location locate(const Polygon &poly, const Point &p)
Classify point location with respect to a polygon.
Represents a point with rectangular coordinates in a 2D plane.
A general (irregular) 2D polygon defined by a sequence of vertices.
void add_vertex(const Point &point)
Add a vertex to the polygon.
void close()
Close the polygon.
Represents a line segment between two points.
O(log n) point location via trapezoidal map with DAG search.
LocationType
Classification of a point location query result.
static Polygon make_square_poly(Geom_Number cx, Geom_Number cy, Geom_Number half)
static size_t count_active_trapezoids(const TM::Result &res)
static Polygon make_hexagon()
static Polygon make_triangle_poly()
Main namespace for Aleph-w library functions.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
The trapezoidal map and DAG search structure.
Array< Trapezoid > trapezoids
All trapezoids created during construction.