Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
geom_algorithms_test_edgecases_sweepline_minkowski_kdtree.cc
Go to the documentation of this file.
2#include <algorithm>
3#include <random>
4#include <cmath>
5#include <utility>
6#include <vector>
7
8
9// ---------- Edge cases: ClosestPair ----------
10
11TEST_F(GeomAlgorithmsTest, ClosestPairEmptyInputThrows)
12{
13 DynList<Point> empty;
15 EXPECT_THROW((void) cp(empty), std::domain_error);
16}
17
18
19TEST_F(GeomAlgorithmsTest, ClosestPairSinglePointThrows)
20{
22 one.append(Point(1, 1));
24 EXPECT_THROW((void) cp(one), std::domain_error);
25}
26
27
28TEST_F(GeomAlgorithmsTest, ClosestPairAllDuplicates)
29{
30 DynList<Point> dups;
31 dups.append(Point(7, 7));
32 dups.append(Point(7, 7));
33 dups.append(Point(7, 7));
34 dups.append(Point(7, 7));
35
37 auto res = cp(dups);
38
39 EXPECT_EQ(res.distance_squared, 0);
40 EXPECT_EQ(res.first, Point(7, 7));
41 EXPECT_EQ(res.second, Point(7, 7));
42}
43
44
45// ---------- Edge cases: CuttingEarsTriangulation ----------
46
47TEST_F(GeomAlgorithmsTest, TriangulateTwoVerticesThrows)
48{
49 // A polygon with only 2 vertices cannot be closed (requires >= 3)
50 // so the exception is thrown at close() time, not at triangulation time
51 EXPECT_THROW(
52 {
53 Polygon p;
54 p.add_vertex(Point(0, 0));
55 p.add_vertex(Point(1, 0));
56 p.close();
57 },
58 std::domain_error);
59}
60
61
62// ---------- Edge cases: RotatingCalipers ----------
63
64TEST_F(GeomAlgorithmsTest, RotatingCalipersOpenSingleVertexThrows)
65{
66 Polygon p;
67 p.add_vertex(Point(1, 1));
68 // Not closed — should throw.
69
71 EXPECT_THROW((void) calipers.diameter(p), std::domain_error);
72 EXPECT_THROW((void) calipers.minimum_width(p), std::domain_error);
73}
74
75
76// ---------- Edge cases: PointInPolygon ----------
77
78TEST_F(GeomAlgorithmsTest, PointInPolygonTwoVerticesThrows)
79{
80 // A polygon with only 2 vertices cannot be closed (requires >= 3)
81 // so the exception is thrown at close() time
82 EXPECT_THROW(
83 {
84 Polygon p;
85 p.add_vertex(Point(0, 0));
86 p.add_vertex(Point(5, 5));
87 p.close();
88 },
89 std::domain_error);
90}
91
92
93// ---------- Edge cases: Convex hull algorithms with 2 collinear points ----------
94
95TEST_F(GeomAlgorithmsTest, AndrewMonotonicChainTwoPoints)
96{
97 DynList<Point> points;
98 points.append(Point(0, 0));
99 points.append(Point(5, 5));
100
102 Polygon hull = andrew(points);
103
104 // Degenerate 2-point hull cannot be closed (requires >= 3 vertices)
105 EXPECT_EQ(hull.size(), 2u);
106 EXPECT_FALSE(hull.is_closed());
107 EXPECT_TRUE(polygon_contains_vertex(hull, Point(0, 0)));
108 EXPECT_TRUE(polygon_contains_vertex(hull, Point(5, 5)));
109}
110
111
112TEST_F(GeomAlgorithmsTest, AndrewMonotonicChainEmptyInput)
113{
114 DynList<Point> empty;
116 EXPECT_EQ(andrew(empty).size(), 0u);
117}
118
119
120TEST_F(GeomAlgorithmsTest, AndrewMonotonicChainSinglePoint)
121{
122 DynList<Point> one;
123 one.append(Point(42, 17));
125 Polygon hull = andrew(one);
126 EXPECT_EQ(hull.size(), 1u);
127 EXPECT_TRUE(polygon_contains_vertex(hull, Point(42, 17)));
128}
129
130
131TEST_F(GeomAlgorithmsTest, AndrewMonotonicChainAllDuplicates)
132{
133 DynList<Point> dups;
134 dups.append(Point(3, 3));
135 dups.append(Point(3, 3));
136 dups.append(Point(3, 3));
137
139 Polygon hull = andrew(dups);
140 EXPECT_EQ(hull.size(), 1u);
141}
142
143
144TEST_F(GeomAlgorithmsTest, GrahamScanEmptyInput)
145{
146 DynList<Point> empty;
148 EXPECT_EQ(graham(empty).size(), 0u);
149}
150
151
152TEST_F(GeomAlgorithmsTest, GrahamScanSinglePoint)
153{
154 DynList<Point> one;
155 one.append(Point(42, 17));
157 Polygon hull = graham(one);
158 EXPECT_EQ(hull.size(), 1u);
159}
160
161
162TEST_F(GeomAlgorithmsTest, GrahamScanTwoPoints)
163{
164 DynList<Point> points;
165 points.append(Point(0, 0));
166 points.append(Point(5, 5));
167
169 Polygon hull = graham(points);
170 // Degenerate 2-point hull cannot be closed (requires >= 3 vertices)
171 EXPECT_EQ(hull.size(), 2u);
172 EXPECT_FALSE(hull.is_closed());
173}
174
175
176TEST_F(GeomAlgorithmsTest, GrahamScanAllDuplicates)
177{
178 DynList<Point> dups;
179 dups.append(Point(7, 7));
180 dups.append(Point(7, 7));
181 dups.append(Point(7, 7));
182
184 Polygon hull = graham(dups);
185 EXPECT_EQ(hull.size(), 1u);
186}
187
188
189// ---------- Cross-algorithm consistency ----------
190
191TEST_F(GeomAlgorithmsTest, AllHullAlgorithmsAgreeOnRandomInput)
192{
193 // All five hull algorithms should produce the same vertex set.
194 DynList<Point> points;
195 // Deterministic "random" set avoiding cocircular degeneracies.
196 // The constants below are the canonical glibc `rand()` LCG parameters
197 // (a, c, m) = (1103515245, 12345, 2^31). We use a hand-rolled LCG
198 // instead of <random> to keep the generated sequence portable across
199 // standard libraries — the test asserts that the five hull
200 // implementations agree on the same input set, so any well-defined
201 // deterministic generator works.
202 constexpr unsigned int LCG_MULT = 1103515245u;
203 constexpr unsigned int LCG_INCR = 12345u;
204 constexpr unsigned int LCG_MASK = 0x7fffffffu;
205 unsigned int seed = 12345;
206 for (int i = 0; i < 50; ++i)
207 {
208 seed = (seed * LCG_MULT + LCG_INCR) & LCG_MASK;
209 int x = static_cast<int>(seed % 1000);
210 seed = (seed * LCG_MULT + LCG_INCR) & LCG_MASK;
211 int y = static_cast<int>(seed % 1000);
212 points.append(Point(x, y));
213 }
214
217 QuickHull qh;
220
221 Polygon h_andrew = andrew(points);
222 Polygon h_graham = graham(points);
223 Polygon h_qh = qh(points);
224 Polygon h_gw = gw(points);
225 Polygon h_bf = bf(points);
226
227 EXPECT_EQ(h_andrew.size(), h_graham.size());
228 EXPECT_EQ(h_andrew.size(), h_qh.size());
229 EXPECT_EQ(h_andrew.size(), h_gw.size());
230 EXPECT_EQ(h_andrew.size(), h_bf.size());
231
232 // Every vertex of Andrew's hull should appear in every other hull.
233 for (Polygon::Vertex_Iterator it(h_andrew); it.has_curr(); it.next_ne())
234 {
235 const Point p = it.get_current_vertex();
236 EXPECT_TRUE(polygon_contains_vertex(h_graham, p));
237 EXPECT_TRUE(polygon_contains_vertex(h_qh, p));
238 EXPECT_TRUE(polygon_contains_vertex(h_gw, p));
239 EXPECT_TRUE(polygon_contains_vertex(h_bf, p));
240 }
241}
242
243
244// ---------- Delaunay: as_triangles helper ----------
245
246TEST_F(GeomAlgorithmsTest, DelaunayAsTrianglesProducesValidTriangles)
247{
249 auto r = delaunay({Point(0, 0), Point(6, 0), Point(3, 5), Point(6, 5),
250 Point(0, 5)});
251
252 DynList<Triangle> tris = delaunay.as_triangles(r);
253
254 size_t count = 0;
255 for (DynList<Triangle>::Iterator it(tris); it.has_curr(); it.next_ne())
256 ++count;
257
258 EXPECT_EQ(count, r.triangles.size());
259}
260
261
262// ============================================================================
263// Phase 4 — New Algorithms Tests
264// ============================================================================
265
266// ---------- SegmentSegmentIntersection (Dedicated O(1)) ----------
267
268TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionNoIntersection)
269{
271 const auto result = pair_ix(Segment(Point(0, 0), Point(2, 0)),
272 Segment(Point(0, 2), Point(2, 2)));
273
274 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::NONE);
275 EXPECT_FALSE(result.intersects());
276}
277
278
279TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionProperCross)
280{
282 const auto result = pair_ix(Segment(Point(0, 0), Point(4, 4)),
283 Segment(Point(0, 4), Point(4, 0)));
284
285 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
286 EXPECT_TRUE(result.intersects());
287 EXPECT_EQ(result.point, Point(2, 2));
288}
289
290
291TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionEndpointTouch)
292{
294 const auto result = pair_ix(Segment(Point(0, 0), Point(4, 0)),
295 Segment(Point(4, 0), Point(4, 3)));
296
297 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
298 EXPECT_EQ(result.point, Point(4, 0));
299}
300
301
302TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionCollinearOverlap)
303{
305 const Segment a(Point(0, 0), Point(6, 0));
306 const Segment b(Point(2, 0), Point(4, 0));
307 const auto result = pair_ix(a, b);
308
310 scene.segments.append(a);
311 scene.segments.append(b);
312 scene.highlighted_points.append(Point(2, 0));
313 scene.highlighted_points.append(Point(4, 0));
315 "case_segment_segment_overlap_o1", scene,
316 "Dedicated O(1) segment-segment overlap");
317
318 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::OVERLAP);
319 EXPECT_EQ(result.overlap.get_src_point(), Point(2, 0));
320 EXPECT_EQ(result.overlap.get_tgt_point(), Point(4, 0));
321}
322
323
324TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionVerticalOverlap)
325{
327 const auto result = pair_ix(Segment(Point(2, 5), Point(2, 1)),
328 Segment(Point(2, 3), Point(2, 7)));
329
330 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::OVERLAP);
331 EXPECT_EQ(result.overlap.get_src_point(), Point(2, 3));
332 EXPECT_EQ(result.overlap.get_tgt_point(), Point(2, 5));
333}
334
335
336TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionDegeneratePointOnSegment)
337{
339 const auto result = pair_ix(Segment(Point(3, 3), Point(3, 3)),
340 Segment(Point(0, 0), Point(6, 6)));
341
342 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
343 EXPECT_EQ(result.point, Point(3, 3));
344}
345
346
347TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionDegenerateIdenticalPoints)
348{
350 const auto result = pair_ix(Segment(Point(7, -2), Point(7, -2)),
351 Segment(Point(7, -2), Point(7, -2)));
352
353 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
354 EXPECT_EQ(result.point, Point(7, -2));
355}
356
357
358TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionDegenerateDisjointPoints)
359{
361 const auto result = pair_ix(Segment(Point(1, 1), Point(1, 1)),
362 Segment(Point(2, 2), Point(2, 2)));
363
364 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::NONE);
365 EXPECT_FALSE(result.intersects());
366}
367
368
369TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionCollinearTouchPoint)
370{
372 const auto result = pair_ix(Segment(Point(0, 0), Point(2, 0)),
373 Segment(Point(2, 0), Point(5, 0)));
374
375 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
376 EXPECT_EQ(result.point, Point(2, 0));
377}
378
379
380TEST_F(GeomAlgorithmsTest, SegmentSegmentIntersectionFreeFunction)
381{
382 const auto result = segment_segment_intersection(Segment(Point(0, 0), Point(3, 3)),
383 Segment(Point(0, 3), Point(3, 0)));
384
385 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
386 EXPECT_EQ(result.point, Point(Geom_Number(3, 2), Geom_Number(3, 2)));
387}
388
389
390// ---------- SweepLineSegmentIntersection ----------
391
392TEST_F(GeomAlgorithmsTest, SweepLineNoSegments)
393{
395 Array<Segment> segs;
396 auto result = sweep(segs);
397 EXPECT_EQ(result.size(), 0u);
398}
399
400
401TEST_F(GeomAlgorithmsTest, SweepLineSingleSegment)
402{
404 Array<Segment> segs;
405 segs.append(Segment(Point(0, 0), Point(5, 5)));
406 auto result = sweep(segs);
407 EXPECT_EQ(result.size(), 0u);
408}
409
410
411TEST_F(GeomAlgorithmsTest, SweepLineParallelNoIntersection)
412{
414 Array<Segment> segs;
415 segs.append(Segment(Point(0, 0), Point(5, 0)));
416 segs.append(Segment(Point(0, 1), Point(5, 1)));
417 auto result = sweep(segs);
418 EXPECT_EQ(result.size(), 0u);
419}
420
421
422TEST_F(GeomAlgorithmsTest, SweepLineSimpleCross)
423{
425 Array<Segment> segs;
426 segs.append(Segment(Point(0, 0), Point(4, 4)));
427 segs.append(Segment(Point(0, 4), Point(4, 0)));
428 auto result = sweep(segs);
429 ASSERT_EQ(result.size(), 1u);
430 EXPECT_EQ(result(0).point, Point(2, 2));
431 EXPECT_EQ(result(0).seg_i, 0u);
432 EXPECT_EQ(result(0).seg_j, 1u);
433}
434
435
436TEST_F(GeomAlgorithmsTest, SweepLineMultipleIntersections)
437{
438 // Three segments forming a triangle of intersections.
440 Array<Segment> segs;
441 segs.append(Segment(Point(0, 0), Point(6, 6))); // s0: diagonal up
442 segs.append(Segment(Point(0, 6), Point(6, 0))); // s1: diagonal down
443 segs.append(Segment(Point(0, 3), Point(6, 3))); // s2: horizontal
444
445 auto result = sweep(segs);
446
448 for (size_t i = 0; i < segs.size(); ++i)
449 scene.segments.append(segs(i));
450 for (size_t i = 0; i < result.size(); ++i)
451 scene.highlighted_points.append(result(i).point);
453 "case_sweepline_multiple_intersections", scene,
454 "Sweep-line / multi-intersection degeneracy");
455
456 // s0 x s1 at (3,3), s0 x s2 at (3,3), s1 x s2 at (3,3)
457 // Actually: s0 x s2 at (3,3), s1 x s2 at (3,3), s0 x s1 at (3,3)
458 // All three intersect at (3,3).
459 EXPECT_EQ(result.size(), 3u);
460 for (size_t i = 0; i < result.size(); ++i)
461 EXPECT_EQ(result(i).point, Point(3, 3));
462}
463
464
465TEST_F(GeomAlgorithmsTest, SweepLineDisjointSegments)
466{
468 Array<Segment> segs;
469 segs.append(Segment(Point(0, 0), Point(1, 0)));
470 segs.append(Segment(Point(3, 3), Point(4, 3)));
471 auto result = sweep(segs);
472 EXPECT_EQ(result.size(), 0u);
473}
474
475
476TEST_F(GeomAlgorithmsTest, SweepLineTShapedIntersection)
477{
479 Array<Segment> segs;
480 segs.append(Segment(Point(0, 2), Point(4, 2))); // horizontal
481 segs.append(Segment(Point(2, 0), Point(2, 2))); // vertical, touching
482 auto result = sweep(segs);
483 ASSERT_EQ(result.size(), 1u);
484 EXPECT_EQ(result(0).point, Point(2, 2));
485}
486
487
488TEST_F(GeomAlgorithmsTest, SweepLineDegenerateSegmentThrows)
489{
491 Array<Segment> segs;
492 segs.append(Segment(Point(1, 1), Point(1, 1))); // zero length
493 segs.append(Segment(Point(0, 0), Point(2, 2)));
494 EXPECT_THROW((void) sweep(segs), std::domain_error);
495}
496
497
498TEST_F(GeomAlgorithmsTest, SweepLineFourSegmentsStar)
499{
500 // Four segments through center (2,2).
502 Array<Segment> segs;
503 segs.append(Segment(Point(0, 2), Point(4, 2))); // horizontal
504 segs.append(Segment(Point(2, 0), Point(2, 4))); // vertical
505 segs.append(Segment(Point(0, 0), Point(4, 4))); // diagonal up
506 segs.append(Segment(Point(0, 4), Point(4, 0))); // diagonal down
507
508 auto result = sweep(segs);
509
510 // C(4,2) = 6 pairs, all intersecting at (2,2).
511 EXPECT_EQ(result.size(), 6u);
512 for (size_t i = 0; i < result.size(); ++i)
513 EXPECT_EQ(result(i).point, Point(2, 2));
514}
515
516
517// ---------- MonotonePolygonTriangulation ----------
518
519TEST_F(GeomAlgorithmsTest, MonotoneTriangulateTriangle)
520{
521 Polygon p;
522 p.add_vertex(Point(0, 0));
523 p.add_vertex(Point(4, 0));
524 p.add_vertex(Point(2, 3));
525 p.close();
526
528 DynList<Triangle> tris = mt(p);
529
530 size_t count = 0;
531 for (DynList<Triangle>::Iterator it(tris); it.has_curr(); it.next_ne())
532 ++count;
533 EXPECT_EQ(count, 1u);
534}
535
536
537TEST_F(GeomAlgorithmsTest, MonotoneTriangulateSquare)
538{
539 Polygon p;
540 p.add_vertex(Point(0, 0));
541 p.add_vertex(Point(4, 0));
542 p.add_vertex(Point(4, 4));
543 p.add_vertex(Point(0, 4));
544 p.close();
545
547 DynList<Triangle> tris = mt(p);
548
549 size_t count = 0;
550 for (DynList<Triangle>::Iterator it(tris); it.has_curr(); it.next_ne())
551 ++count;
552 EXPECT_EQ(count, 2u);
553}
554
555
556TEST_F(GeomAlgorithmsTest, MonotoneTriangulateSquareCW)
557{
558 Polygon p;
559 p.add_vertex(Point(0, 0));
560 p.add_vertex(Point(0, 4));
561 p.add_vertex(Point(4, 4));
562 p.add_vertex(Point(4, 0));
563 p.close();
564
566 DynList<Triangle> tris = mt(p);
567
568 size_t count = 0;
569 for (DynList<Triangle>::Iterator it(tris); it.has_curr(); it.next_ne())
570 ++count;
571 EXPECT_EQ(count, 2u);
572}
573
574
575TEST_F(GeomAlgorithmsTest, MonotoneTriangulatePentagon)
576{
577 Polygon p;
578 p.add_vertex(Point(2, 0));
579 p.add_vertex(Point(4, 1.5));
580 p.add_vertex(Point(3, 4));
581 p.add_vertex(Point(1, 4));
582 p.add_vertex(Point(0, 1.5));
583 p.close();
584
586 DynList<Triangle> tris = mt(p);
587
588 size_t count = 0;
589 for (DynList<Triangle>::Iterator it(tris); it.has_curr(); it.next_ne())
590 ++count;
591 EXPECT_EQ(count, 3u);
592}
593
594
595TEST_F(GeomAlgorithmsTest, MonotoneTriangulateHexagon)
596{
597 Polygon p;
598 p.add_vertex(Point(1, 0));
599 p.add_vertex(Point(2, 0));
600 p.add_vertex(Point(3, 1));
601 p.add_vertex(Point(2, 2));
602 p.add_vertex(Point(1, 2));
603 p.add_vertex(Point(0, 1));
604 p.close();
605
607 DynList<Triangle> tris = mt(p);
608
609 size_t count = 0;
610 for (DynList<Triangle>::Iterator it(tris); it.has_curr(); it.next_ne())
611 ++count;
612 EXPECT_EQ(count, 4u);
613}
614
615
616TEST_F(GeomAlgorithmsTest, MonotoneTriangulateOpenThrows)
617{
618 Polygon p;
619 p.add_vertex(Point(0, 0));
620 p.add_vertex(Point(4, 0));
621 p.add_vertex(Point(2, 3));
622
624 EXPECT_THROW((void) mt(p), std::domain_error);
625}
626
627
628TEST_F(GeomAlgorithmsTest, MonotoneTriangulateDegenerateThrows)
629{
630 // Degenerate (collinear) polygon should throw - either at close() time
631 // if vertices are reduced, or at triangulation time for zero area
632 EXPECT_THROW(
633 {
634 Polygon p;
635 p.add_vertex(Point(0, 0));
636 p.add_vertex(Point(1, 0));
637 p.add_vertex(Point(2, 0));
638 p.close();
640 (void) mt(p);
641 },
642 std::domain_error);
643}
644
645
646TEST_F(GeomAlgorithmsTest, MonotoneTriangulateCountMatchesCuttingEars)
647{
648 // L-shaped polygon (non-monotone): both methods should produce n-2 triangles.
649 Polygon p;
650 p.add_vertex(Point(0, 0));
651 p.add_vertex(Point(4, 0));
652 p.add_vertex(Point(4, 2));
653 p.add_vertex(Point(2, 2));
654 p.add_vertex(Point(2, 4));
655 p.add_vertex(Point(0, 4));
656 p.close();
657
660
661 DynList<Triangle> mt_tris = mt(p);
662 DynList<Triangle> ear_tris = ears(p);
663
664 size_t mt_count = 0;
665 for (DynList<Triangle>::Iterator it(mt_tris); it.has_curr(); it.next_ne())
666 ++mt_count;
667
668 size_t ear_count = 0;
669 for (DynList<Triangle>::Iterator it(ear_tris); it.has_curr(); it.next_ne())
670 ++ear_count;
671
672 EXPECT_EQ(mt_count, 4u);
673 EXPECT_EQ(mt_count, ear_count);
674}
675
676
677// ---------- MinkowskiSumConvex ----------
678
679TEST_F(GeomAlgorithmsTest, MinkowskiSumTwoSquares)
680{
681 // Square [0,1]^2 ⊕ Square [0,1]^2 = Square [0,2]^2.
682 Polygon sq;
683 sq.add_vertex(Point(0, 0));
684 sq.add_vertex(Point(1, 0));
685 sq.add_vertex(Point(1, 1));
686 sq.add_vertex(Point(0, 1));
687 sq.close();
688
690 Polygon result = mink(sq, sq);
691
692 EXPECT_EQ(result.size(), 4u);
693 EXPECT_TRUE(result.is_closed());
694 EXPECT_TRUE(polygon_contains_vertex(result, Point(0, 0)));
695 EXPECT_TRUE(polygon_contains_vertex(result, Point(2, 0)));
696 EXPECT_TRUE(polygon_contains_vertex(result, Point(2, 2)));
697 EXPECT_TRUE(polygon_contains_vertex(result, Point(0, 2)));
698}
699
700
701TEST_F(GeomAlgorithmsTest, MinkowskiSumSquareAndTriangle)
702{
703 Polygon sq;
704 sq.add_vertex(Point(0, 0));
705 sq.add_vertex(Point(2, 0));
706 sq.add_vertex(Point(2, 2));
707 sq.add_vertex(Point(0, 2));
708 sq.close();
709
710 Polygon tri;
711 tri.add_vertex(Point(0, 0));
712 tri.add_vertex(Point(1, 0));
713 tri.add_vertex(Point(0, 1));
714 tri.close();
715
717 Polygon result = mink(sq, tri);
718
719 // Square (4 edges) + Triangle (3 edges) = up to 7 vertices.
720 EXPECT_TRUE(result.is_closed());
721 EXPECT_GE(result.size(), 3u);
722 EXPECT_LE(result.size(), 7u);
723
724 // The sum must contain the extreme vertices.
725 EXPECT_TRUE(polygon_contains_vertex(result, Point(0, 0))); // (0,0)+(0,0)
726 EXPECT_TRUE(polygon_contains_vertex(result, Point(3, 0))); // (2,0)+(1,0)
727 EXPECT_TRUE(polygon_contains_vertex(result, Point(0, 3))); // (0,2)+(0,1)
728}
729
730
731TEST_F(GeomAlgorithmsTest, MinkowskiSumCWInputsNormalized)
732{
733 // CW square ⊕ CW square should still work.
734 Polygon sq_cw;
735 sq_cw.add_vertex(Point(0, 0));
736 sq_cw.add_vertex(Point(0, 1));
737 sq_cw.add_vertex(Point(1, 1));
738 sq_cw.add_vertex(Point(1, 0));
739 sq_cw.close();
740
742 Polygon result = mink(sq_cw, sq_cw);
743
744 EXPECT_EQ(result.size(), 4u);
745 EXPECT_TRUE(result.is_closed());
746 EXPECT_TRUE(polygon_contains_vertex(result, Point(0, 0)));
747 EXPECT_TRUE(polygon_contains_vertex(result, Point(2, 0)));
748 EXPECT_TRUE(polygon_contains_vertex(result, Point(2, 2)));
749 EXPECT_TRUE(polygon_contains_vertex(result, Point(0, 2)));
750}
751
752
753TEST_F(GeomAlgorithmsTest, MinkowskiSumNonConvexThrows)
754{
755 Polygon convex;
756 convex.add_vertex(Point(0, 0));
757 convex.add_vertex(Point(2, 0));
758 convex.add_vertex(Point(2, 2));
759 convex.add_vertex(Point(0, 2));
760 convex.close();
761
762 Polygon concave;
763 concave.add_vertex(Point(0, 0));
764 concave.add_vertex(Point(4, 0));
765 concave.add_vertex(Point(2, 1));
766 concave.add_vertex(Point(4, 4));
767 concave.add_vertex(Point(0, 4));
768 concave.close();
769
771 EXPECT_THROW((void) mink(convex, concave), std::domain_error);
772 EXPECT_THROW((void) mink(concave, convex), std::domain_error);
773}
774
775
776TEST_F(GeomAlgorithmsTest, MinkowskiSumOpenPolygonThrows)
777{
778 Polygon open;
779 open.add_vertex(Point(0, 0));
780 open.add_vertex(Point(1, 0));
781 open.add_vertex(Point(1, 1));
782
783 Polygon closed;
784 closed.add_vertex(Point(0, 0));
785 closed.add_vertex(Point(1, 0));
786 closed.add_vertex(Point(0, 1));
787 closed.close();
788
790 EXPECT_THROW((void) mink(open, closed), std::domain_error);
791}
792
793
794TEST_F(GeomAlgorithmsTest, MinkowskiSumIsConvex)
795{
796 // Pentagon ⊕ Triangle — result must be convex.
797 Polygon pent;
798 pent.add_vertex(Point(2, 0));
799 pent.add_vertex(Point(4, 1));
800 pent.add_vertex(Point(3, 3));
801 pent.add_vertex(Point(1, 3));
802 pent.add_vertex(Point(0, 1));
803 pent.close();
804
805 Polygon tri;
806 tri.add_vertex(Point(0, 0));
807 tri.add_vertex(Point(1, 0));
808 tri.add_vertex(Point(0, 1));
809 tri.close();
810
812 Polygon result = mink(pent, tri);
813 EXPECT_TRUE(result.is_closed());
814 EXPECT_GE(result.size(), 3u);
815
816 // Verify convexity: all turns should be consistent.
817 Array<Point> rv;
818 for (Polygon::Vertex_Iterator it(result); it.has_curr(); it.next_ne())
819 rv.append(it.get_current_vertex());
820
821 int sign = 0;
822 for (size_t i = 0; i < rv.size(); ++i)
823 {
824 Geom_Number turn =
825 area_of_parallelogram(rv(i), rv((i + 1) % rv.size()),
826 rv((i + 2) % rv.size()));
827 if (turn == 0) continue;
828 int s = turn > 0 ? 1 : -1;
829 if (sign == 0) sign = s;
830 else EXPECT_EQ(sign, s);
831 }
832}
833
834static Point first_vertex_of(const Polygon & poly)
835{
836 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
837 return it.get_current_vertex();
838 return Point(0, 0);
839}
840
842 Point & out_p, Point & out_q)
843{
846 {
847 out_p = first_vertex_of(p);
848 out_q = out_p;
849 return 0;
850 }
851
852 for (Polygon::Segment_Iterator itp(p); itp.has_curr(); itp.next_ne())
853 {
854 const Segment sp = itp.get_current_segment();
855 for (Polygon::Segment_Iterator itq(q); itq.has_curr(); itq.next_ne())
856 if (sp.intersects_with(itq.get_current_segment()))
857 {
858 out_p = first_vertex_of(p);
859 out_q = out_p;
860 return 0;
861 }
862 }
863
866
867 bool has_best = false;
868 Geom_Number best_d2 = 0;
869
870 for (size_t i = 0; i < pv.size(); ++i)
871 for (size_t j = 0; j < qv.size(); ++j)
872 {
873 const Segment e(qv(j), qv((j + 1) % qv.size()));
874 const Point proj = e.project(pv(i));
875 const Geom_Number d2 = pv(i).distance_squared_to(proj);
876 if (not has_best or d2 < best_d2)
877 {
878 has_best = true;
879 best_d2 = d2;
880 out_p = pv(i);
881 out_q = proj;
882 }
883 }
884
885 for (size_t i = 0; i < qv.size(); ++i)
886 for (size_t j = 0; j < pv.size(); ++j)
887 {
888 const Segment e(pv(j), pv((j + 1) % pv.size()));
889 const Point proj = e.project(qv(i));
890 const Geom_Number d2 = qv(i).distance_squared_to(proj);
891 if (not has_best or d2 < best_d2)
892 {
893 has_best = true;
894 best_d2 = d2;
895 out_p = proj;
896 out_q = qv(i);
897 }
898 }
899
900 return best_d2;
901}
902
903// ---------- ConvexPolygonDistanceGJK ----------
904
905TEST_F(GeomAlgorithmsTest, ConvexPolygonDistanceGJKSeparatedSquares)
906{
907 Polygon a;
908 a.add_vertex(Point(0, 0));
909 a.add_vertex(Point(1, 0));
910 a.add_vertex(Point(1, 1));
911 a.add_vertex(Point(0, 1));
912 a.close();
913
914 Polygon b;
915 b.add_vertex(Point(2, 0));
916 b.add_vertex(Point(3, 0));
917 b.add_vertex(Point(3, 1));
918 b.add_vertex(Point(2, 1));
919 b.close();
920
922 const auto r = gjk(a, b);
923
924 EXPECT_FALSE(r.intersects);
925 EXPECT_EQ(r.distance_squared, Geom_Number(1));
926 EXPECT_EQ(r.closest_on_first.distance_squared_to(r.closest_on_second),
927 r.distance_squared);
928 EXPECT_LE(r.gjk_iterations, 64u);
929}
930
931TEST_F(GeomAlgorithmsTest, ConvexPolygonDistanceGJKOverlapping)
932{
933 Polygon a;
934 a.add_vertex(Point(0, 0));
935 a.add_vertex(Point(3, 0));
936 a.add_vertex(Point(3, 3));
937 a.add_vertex(Point(0, 3));
938 a.close();
939
940 Polygon b;
941 b.add_vertex(Point(2, 2));
942 b.add_vertex(Point(4, 2));
943 b.add_vertex(Point(4, 4));
944 b.add_vertex(Point(2, 4));
945 b.close();
946
948 const auto r = gjk(a, b);
949
950 EXPECT_TRUE(r.intersects);
951 EXPECT_EQ(r.distance_squared, Geom_Number(0));
952 EXPECT_EQ(r.distance, Geom_Number(0));
953}
954
955TEST_F(GeomAlgorithmsTest, ConvexPolygonDistanceGJKTouchingEdge)
956{
957 Polygon a;
958 a.add_vertex(Point(0, 0));
959 a.add_vertex(Point(1, 0));
960 a.add_vertex(Point(1, 1));
961 a.add_vertex(Point(0, 1));
962 a.close();
963
964 Polygon b;
965 b.add_vertex(Point(1, 0));
966 b.add_vertex(Point(2, 0));
967 b.add_vertex(Point(2, 1));
968 b.add_vertex(Point(1, 1));
969 b.close();
970
972 const auto r = gjk(a, b);
973
974 EXPECT_TRUE(r.intersects);
975 EXPECT_EQ(r.distance_squared, Geom_Number(0));
976}
977
978TEST_F(GeomAlgorithmsTest, ConvexPolygonDistanceGJKSymmetry)
979{
980 Polygon a;
981 a.add_vertex(Point(0, 0));
982 a.add_vertex(Point(4, 0));
983 a.add_vertex(Point(2, 2));
984 a.close();
985
986 Polygon b;
987 b.add_vertex(Point(6, 1));
988 b.add_vertex(Point(9, 1));
989 b.add_vertex(Point(9, 4));
990 b.add_vertex(Point(6, 4));
991 b.close();
992
994 const auto ab = gjk(a, b);
995 const auto ba = gjk(b, a);
996
997 EXPECT_EQ(ab.distance_squared, ba.distance_squared);
998 EXPECT_EQ(ab.distance, ba.distance);
999 EXPECT_EQ(ab.intersects, ba.intersects);
1000}
1001
1002TEST_F(GeomAlgorithmsTest, ConvexPolygonDistanceGJKInvalidInputThrows)
1003{
1004 Polygon convex;
1005 convex.add_vertex(Point(0, 0));
1006 convex.add_vertex(Point(2, 0));
1007 convex.add_vertex(Point(2, 2));
1008 convex.add_vertex(Point(0, 2));
1009 convex.close();
1010
1011 Polygon concave;
1012 concave.add_vertex(Point(0, 0));
1013 concave.add_vertex(Point(4, 0));
1014 concave.add_vertex(Point(2, 1));
1015 concave.add_vertex(Point(4, 4));
1016 concave.add_vertex(Point(0, 4));
1017 concave.close();
1018
1019 Polygon open;
1020 open.add_vertex(Point(0, 0));
1021 open.add_vertex(Point(1, 0));
1022 open.add_vertex(Point(1, 1));
1023
1025 EXPECT_THROW((void) gjk(open, convex), std::domain_error);
1026 EXPECT_THROW((void) gjk(convex, open), std::domain_error);
1027 EXPECT_THROW((void) gjk(concave, convex), std::domain_error);
1028 EXPECT_THROW((void) gjk(convex, concave), std::domain_error);
1029}
1030
1031TEST_F(GeomAlgorithmsTest, ConvexPolygonDistanceGJKMatchesBruteBaseline)
1032{
1035
1036 unsigned int seed = 424242;
1037 for (int tc = 0; tc < 12; ++tc)
1038 {
1039 Polygon a, b;
1040 while (a.size() < 3)
1041 {
1042 DynList<Point> pts;
1043 for (int i = 0; i < 16; ++i)
1044 {
1045 seed = (seed * 1103515245 + 12345) & 0x7fffffff;
1046 const int x = static_cast<int>(seed % 51) - 25;
1047 seed = (seed * 1103515245 + 12345) & 0x7fffffff;
1048 const int y = static_cast<int>(seed % 51) - 25;
1049 pts.append(Point(x, y));
1050 }
1051 a = hull(pts);
1052 }
1053
1054 while (b.size() < 3)
1055 {
1056 DynList<Point> pts;
1057 for (int i = 0; i < 16; ++i)
1058 {
1059 seed = (seed * 1103515245 + 12345) & 0x7fffffff;
1060 const int x = static_cast<int>(seed % 51) - 25;
1061 seed = (seed * 1103515245 + 12345) & 0x7fffffff;
1062 const int y = static_cast<int>(seed % 51) - 25;
1063 pts.append(Point(x + 10, y + 7));
1064 }
1065 b = hull(pts);
1066 }
1067
1068 const auto r = gjk(a, b);
1069 Point bp, bq;
1070 const Geom_Number brute_d2 = brute_convex_distance_squared(a, b, bp, bq);
1071
1072 EXPECT_EQ(r.distance_squared, brute_d2);
1073 EXPECT_EQ(r.intersects, brute_d2 == 0);
1074 }
1075}
1076
1077
1078// ---------- KDTreePointSearch ----------
1079
1080TEST_F(GeomAlgorithmsTest, KDTreeInsertAndContains)
1081{
1082 KDTreePointSearch kd(0, 0, 100, 100);
1083 EXPECT_TRUE(kd.is_empty());
1084
1085 EXPECT_TRUE(kd.insert(Point(10, 20)));
1086 EXPECT_TRUE(kd.insert(Point(50, 50)));
1087 EXPECT_FALSE(kd.insert(Point(10, 20))); // duplicate
1088
1089 EXPECT_EQ(kd.size(), 2u);
1090 EXPECT_TRUE(kd.contains(Point(10, 20)));
1091 EXPECT_TRUE(kd.contains(Point(50, 50)));
1092 EXPECT_FALSE(kd.contains(Point(30, 30)));
1093}
1094
1095
1097{
1098 KDTreePointSearch kd(0, 0, 100, 100);
1099 kd.insert(Point(10, 10));
1100 kd.insert(Point(50, 50));
1101 kd.insert(Point(90, 90));
1102
1103 auto n = kd.nearest(Point(12, 12));
1104 ASSERT_TRUE(n.has_value());
1105 EXPECT_EQ(*n, Point(10, 10));
1106
1107 auto n2 = kd.nearest(Point(48, 52));
1108 ASSERT_TRUE(n2.has_value());
1109 EXPECT_EQ(*n2, Point(50, 50));
1110}
1111
1112
1113TEST_F(GeomAlgorithmsTest, KDTreeNearestEmpty)
1114{
1115 KDTreePointSearch kd(0, 0, 100, 100);
1116 auto n = kd.nearest(Point(50, 50));
1117 EXPECT_FALSE(n.has_value());
1118}
1119
1120
1121TEST_F(GeomAlgorithmsTest, KDTreeBuildBalanced)
1122{
1123 Array<Point> points;
1124 for (int x = 0; x < 10; ++x)
1125 for (int y = 0; y < 10; ++y)
1126 points.append(Point(x, y));
1127
1128 auto kd = KDTreePointSearch::build(points, 0, 0, 10, 10);
1129
1130 EXPECT_EQ(kd.size(), 100u);
1131
1132 for (int x = 0; x < 10; ++x)
1133 for (int y = 0; y < 10; ++y)
1134 EXPECT_TRUE(kd.contains(Point(x, y)));
1135
1136 auto n = kd.nearest(Point(5, 5));
1137 ASSERT_TRUE(n.has_value());
1138 EXPECT_EQ(*n, Point(5, 5));
1139}
1140
1141
1143{
1144 KDTreePointSearch kd(0, 0, 100, 100);
1145 kd.insert(Point(10, 10));
1146 kd.insert(Point(20, 20));
1147 kd.insert(Point(50, 50));
1148 kd.insert(Point(80, 80));
1149
1151 kd.range(5, 5, 25, 25, &out);
1152
1153 size_t count = 0;
1154 for (DynList<Point>::Iterator it(out); it.has_curr(); it.next_ne())
1155 ++count;
1156 EXPECT_EQ(count, 2u); // (10,10) and (20,20)
1157}
1158
1159
1161{
1162 KDTreePointSearch kd(0, 0, 100, 100);
1163 kd.insert(Point(1, 1));
1164 kd.insert(Point(2, 2));
1165 kd.insert(Point(3, 3));
1166
1167 size_t visited = 0;
1168 kd.for_each([&visited](const Point &) { ++visited; });
1169 EXPECT_EQ(visited, 3u);
1170}
1171
1172TEST_F(GeomAlgorithmsTest, KDTreeDebugSnapshotHasPartitions)
1173{
1174 KDTreePointSearch kd(0, 0, 100, 100);
1175 kd.insert(Point(10, 10));
1176 kd.insert(Point(25, 40));
1177 kd.insert(Point(70, 20));
1178 kd.insert(Point(80, 90));
1179
1180 const auto snap = kd.debug_snapshot();
1181 EXPECT_EQ(snap.points.size(), kd.size());
1182 EXPECT_GT(snap.partitions.size(), 0u);
1183 EXPECT_EQ(snap.bounds.get_xmin(), Geom_Number(0));
1184 EXPECT_EQ(snap.bounds.get_ymin(), Geom_Number(0));
1185 EXPECT_EQ(snap.bounds.get_xmax(), Geom_Number(100));
1186 EXPECT_EQ(snap.bounds.get_ymax(), Geom_Number(100));
1187
1188 size_t leaves = 0;
1189 for (size_t i = 0; i < snap.partitions.size(); ++i)
1190 if (snap.partitions(i).is_leaf)
1191 ++leaves;
1192 EXPECT_EQ(leaves, kd.size());
1193}
1194
1195TEST_F(GeomAlgorithmsTest, KDTreeDebugSnapshotCoversThePointsOutsideItsBounds)
1196{
1197 // The tree takes the points outside its bounds, and extends its region
1198 // to them; the snapshot split its partitions from the bounds given, and
1199 // such a point made one whose corners were swapped, which threw
1200 auto check = [] (const KDTreePointSearch & kd)
1201 {
1202 const auto snap = kd.debug_snapshot();
1203 EXPECT_EQ(snap.points.size(), kd.size());
1204 for (size_t i = 0; i < snap.points.size(); ++i)
1205 EXPECT_TRUE(snap.bounds.contains(snap.points(i)));
1206 size_t leaves = 0;
1207 for (size_t i = 0; i < snap.partitions.size(); ++i)
1208 {
1209 const auto & part = snap.partitions(i);
1210 EXPECT_TRUE(part.region.contains(part.representative));
1211 if (part.is_leaf)
1212 ++leaves;
1213 }
1214 EXPECT_EQ(leaves, kd.size());
1215 return snap.bounds;
1216 };
1217
1218 KDTreePointSearch kd(0, 0, 10, 10);
1219 kd.insert(Point(5, 5));
1220 kd.insert(Point(20, 5));
1221 kd.insert(Point(-4, 12));
1222 const Rectangle b = check(kd);
1223 EXPECT_EQ(b.get_xmin(), Geom_Number(-4));
1224 EXPECT_EQ(b.get_ymin(), Geom_Number(0));
1225 EXPECT_EQ(b.get_xmax(), Geom_Number(20));
1226 EXPECT_EQ(b.get_ymax(), Geom_Number(12));
1227
1228 Array<Point> points;
1229 points.append(Point(5, 5));
1230 points.append(Point(-3, 2));
1231 points.append(Point(8, 30));
1232 points.append(Point(1, 1));
1233 const Rectangle c = check(KDTreePointSearch::build(points, 0, 0, 10, 10));
1234 EXPECT_EQ(c.get_xmin(), Geom_Number(-3));
1235 EXPECT_EQ(c.get_ymin(), Geom_Number(0));
1236 EXPECT_EQ(c.get_xmax(), Geom_Number(10));
1237 EXPECT_EQ(c.get_ymax(), Geom_Number(30));
1238}
1239
1240
1241// ============================================================================
1242// Phase 5 — Rigorous Tests
1243// ============================================================================
1244
1245// ---------- 5.1 Property tests: Delaunay empty-circumcircle ----------
1246
1247// Helper: squared distance between two points (exact).
1248static Geom_Number dist2(const Point & a, const Point & b)
1249
1250{
1251
1252 return a.distance_squared_to(b);
1253
1254}
1255
1256
1257// Helper: extract sorted vertex set from polygon for comparison.
1259
1260{
1261
1262 Array<Point> v;
1263
1264 for (Polygon::Vertex_Iterator it(p); it.has_curr(); it.next_ne())
1265
1266 v.append(it.get_current_vertex());
1267
1268 quicksort_op(v, [](const Point & a, const Point & b)
1269
1270 {
1271
1272 if (a.get_x() != b.get_x())
1273
1274 return a.get_x() < b.get_x();
1275
1276 return a.get_y() < b.get_y();
1277
1278 });
1279
1280 return v;
1281
1282}
1283
1284
1285TEST_F(GeomAlgorithmsTest, DelaunayEmptyCircumcircleProperty)
1286{
1287 // The Delaunay property: for every triangle, no other site is strictly
1288 // inside its circumcircle.
1290 auto r = delaunay({Point(0, 0), Point(6, 0), Point(3, 5), Point(6, 5),
1291 Point(0, 5), Point(3, 2), Point(1, 3), Point(5, 1)});
1292
1293 ASSERT_GE(r.triangles.size(), 1u);
1294
1295 for (size_t t = 0; t < r.triangles.size(); ++t)
1296 {
1297 const auto & tri = r.triangles(t);
1298 const Point & a = r.sites(tri.i);
1299 const Point & b = r.sites(tri.j);
1300 const Point & c = r.sites(tri.k);
1301
1302 // Compute circumcenter and squared circumradius.
1303 const Point cc = circumcenter_of(a, b, c);
1304 const Geom_Number r2 = dist2(cc, a);
1305
1306 for (size_t s = 0; s < r.sites.size(); ++s)
1307 {
1308 if (s == tri.i or s == tri.j or s == tri.k)
1309 continue;
1310
1311 const Geom_Number d2 = dist2(cc, r.sites(s));
1312 // d2 must be >= r2 (no site strictly inside the circumcircle).
1313 EXPECT_GE(d2, r2)
1314 << "Site " << s << " violates empty-circumcircle for triangle "
1315 << t;
1316 }
1317 }
1318}
1319
1320
1321TEST_F(GeomAlgorithmsTest, DelaunayEmptyCircumcircleGridPoints)
1322{
1323 // Grid of 5x5 points — a stress test of the circumcircle property.
1324 DynList<Point> pts;
1325 for (int x = 0; x < 5; ++x)
1326 for (int y = 0; y < 5; ++y)
1327 pts.append(Point(x, y));
1328
1330 auto r = delaunay(pts);
1331
1332 ASSERT_GE(r.triangles.size(), 1u);
1333
1334 for (size_t t = 0; t < r.triangles.size(); ++t)
1335 {
1336 const auto & tri = r.triangles(t);
1337 const Point & a = r.sites(tri.i);
1338 const Point & b = r.sites(tri.j);
1339 const Point & c = r.sites(tri.k);
1340 const Point cc = circumcenter_of(a, b, c);
1341 const Geom_Number r2 = dist2(cc, a);
1342
1343 for (size_t s = 0; s < r.sites.size(); ++s)
1344 {
1345 if (s == tri.i or s == tri.j or s == tri.k)
1346 continue;
1347 EXPECT_GE(dist2(cc, r.sites(s)), r2);
1348 }
1349 }
1350}
1351
1352
1353// ---------- 5.1 Property tests: Voronoi equidistance ----------
1354
1355TEST_F(GeomAlgorithmsTest, VoronoiVerticesEquidistantToSites)
1356{
1357 // Each bounded Voronoi edge connects two circumcenters.
1358 // Each circumcenter (Voronoi vertex) is equidistant to the 3 sites
1359 // of its Delaunay triangle.
1361 auto dt = delaunay({Point(0, 0), Point(5, 0), Point(6, 3), Point(0, 4),
1362 Point(2, 2), Point(4, 4)});
1363 ASSERT_GE(dt.triangles.size(), 1u);
1364
1365 for (size_t t = 0; t < dt.triangles.size(); ++t)
1366 {
1367 const auto & tri = dt.triangles(t);
1368 const Point & a = dt.sites(tri.i);
1369 const Point & b = dt.sites(tri.j);
1370 const Point & c = dt.sites(tri.k);
1371 const Point cc = circumcenter_of(a, b, c);
1372
1373 const Geom_Number da = dist2(cc, a);
1374 const Geom_Number db = dist2(cc, b);
1375 const Geom_Number dc = dist2(cc, c);
1376
1377 EXPECT_EQ(da, db) << "Triangle " << t << ": circumcenter not equidistant";
1378 EXPECT_EQ(db, dc) << "Triangle " << t << ": circumcenter not equidistant";
1379 }
1380}
1381
1382
1383TEST_F(GeomAlgorithmsTest, VoronoiBoundedEdgeSitesAreEquidistantToEndpoints)
1384{
1385 // For each bounded Voronoi edge (connecting two circumcenters c0 and c1),
1386 // the two adjacent sites u,v should be equidistant from the edge midpoint.
1388 auto dt = delaunay({Point(0, 0), Point(5, 0), Point(6, 3), Point(0, 4),
1389 Point(2, 2), Point(4, 4)});
1390
1392 auto r = voronoi(dt);
1393
1394 for (size_t e = 0; e < r.edges.size(); ++e)
1395 {
1396 const auto & edge = r.edges(e);
1397 if (edge.unbounded)
1398 continue;
1399
1400 // Both endpoints are circumcenters equidistant to sites u and v.
1401 const Geom_Number d_src_u = dist2(edge.src, r.sites(edge.site_u));
1402 const Geom_Number d_src_v = dist2(edge.src, r.sites(edge.site_v));
1403 EXPECT_EQ(d_src_u, d_src_v)
1404 << "Edge " << e << " src not equidistant to sites";
1405
1406 const Geom_Number d_tgt_u = dist2(edge.tgt, r.sites(edge.site_u));
1407 const Geom_Number d_tgt_v = dist2(edge.tgt, r.sites(edge.site_v));
1408 EXPECT_EQ(d_tgt_u, d_tgt_v)
1409 << "Edge " << e << " tgt not equidistant to sites";
1410 }
1411}
1412
1413
1414// ---------- 5.2 Numerical robustness: near-collinear ----------
1415
1416TEST_F(GeomAlgorithmsTest, RobustnessNearCollinearDelaunay)
1417{
1418 // Points almost collinear but with tiny deviation — exact arithmetic
1419 // should handle this correctly.
1420 // Using rational offsets like 1/1000000 instead of floating-point.
1421 Geom_Number tiny(1, 1000000); // 10^-6 as exact rational
1422
1423 DynList<Point> pts;
1424 pts.append(Point(0, 0));
1425 pts.append(Point(1, tiny));
1426 pts.append(Point(2, -tiny));
1427 pts.append(Point(3, tiny));
1428 pts.append(Point(4, 0));
1429 pts.append(Point(2, 1)); // clearly off-axis to guarantee non-collinear set
1430
1432 auto r = delaunay(pts);
1433
1435 for (DynList<Point>::Iterator it(pts); it.has_curr(); it.next_ne())
1436 scene.points.append(it.get_curr());
1437 for (size_t i = 0; i < r.triangles.size(); ++i)
1438 {
1439 const auto & t = r.triangles(i);
1440 scene.segments.append(Segment(r.sites(t.i), r.sites(t.j)));
1441 scene.segments.append(Segment(r.sites(t.j), r.sites(t.k)));
1442 scene.segments.append(Segment(r.sites(t.k), r.sites(t.i)));
1443 }
1445 "case_robust_near_collinear_delaunay", scene,
1446 "Delaunay robustness / near-collinear");
1447
1448 // Should produce a valid triangulation.
1449 EXPECT_GE(r.triangles.size(), 1u);
1450
1451 // Verify circumcircle property.
1452 for (size_t t = 0; t < r.triangles.size(); ++t)
1453 {
1454 const auto & tri = r.triangles(t);
1455 const Point cc = circumcenter_of(r.sites(tri.i), r.sites(tri.j),
1456 r.sites(tri.k));
1457 const Geom_Number r2 = dist2(cc, r.sites(tri.i));
1458 for (size_t s = 0; s < r.sites.size(); ++s)
1459 {
1460 if (s == tri.i or s == tri.j or s == tri.k)
1461 continue;
1462 EXPECT_GE(dist2(cc, r.sites(s)), r2);
1463 }
1464 }
1465}
1466
1467
1468TEST_F(GeomAlgorithmsTest, RobustnessNearCollinearConvexHull)
1469{
1470 // Near-collinear points should still produce a valid hull.
1471 Geom_Number tiny(1, 10000000); // 10^-7
1472
1473 DynList<Point> pts;
1474 pts.append(Point(0, 0));
1475 pts.append(Point(1, tiny));
1476 pts.append(Point(2, 0));
1477 pts.append(Point(3, -tiny));
1478 pts.append(Point(4, 0));
1479 pts.append(Point(2, 1)); // off-line to make non-degenerate
1480
1482 Polygon hull = andrew(pts);
1483
1485 for (DynList<Point>::Iterator it(pts); it.has_curr(); it.next_ne())
1486 scene.points.append(it.get_curr());
1487 scene.polygons.append(hull);
1488 Aleph::TestVisual::add_polygon_vertices(scene, hull, true);
1490 "case_robust_near_collinear_hull", scene,
1491 "Convex hull robustness / near-collinear");
1492
1493 EXPECT_TRUE(hull.is_closed());
1494 EXPECT_GE(hull.size(), 3u);
1495
1496 // Hull must contain the extremes.
1497 EXPECT_TRUE(polygon_contains_vertex(hull, Point(0, 0)));
1498 EXPECT_TRUE(polygon_contains_vertex(hull, Point(4, 0)));
1499 EXPECT_TRUE(polygon_contains_vertex(hull, Point(2, 1)));
1500}
1501
1502
1503// ---------- 5.2 Numerical robustness: near-parallel segments ----------
1504
1505TEST_F(GeomAlgorithmsTest, RobustnessNearParallelSegments)
1506{
1507 // Two segments that are nearly parallel — they intersect at a very
1508 // distant point. The sweep line should either find 0 or 1 intersection
1509 // depending on whether the segments actually overlap.
1510 Geom_Number tiny(1, 100000000); // 10^-8
1511
1513 Array<Segment> segs;
1514 segs.append(Segment(Point(0, 0), Point(10, 0)));
1515 segs.append(Segment(Point(0, tiny), Point(10, tiny))); // almost parallel
1516
1517 auto result = sweep(segs);
1518 EXPECT_EQ(result.size(), 0u); // truly parallel, no intersection
1519}
1520
1521
1522TEST_F(GeomAlgorithmsTest, RobustnessNearParallelSegmentsConverging)
1523{
1524 // Two segments that converge at a nearly-parallel angle.
1525 Geom_Number tiny(1, 1000000);
1526
1528 Array<Segment> segs;
1529 segs.append(Segment(Point(0, 0), Point(10, 0)));
1530 segs.append(Segment(Point(0, tiny), Point(10, -tiny))); // slight converge
1531
1532 auto result = sweep(segs);
1533
1535 for (size_t i = 0; i < segs.size(); ++i)
1536 scene.segments.append(segs(i));
1537 for (size_t i = 0; i < result.size(); ++i)
1538 scene.highlighted_points.append(result(i).point);
1540 "case_robust_near_parallel_converging", scene,
1541 "Near-parallel segments / converging intersection");
1542
1543 ASSERT_EQ(result.size(), 1u);
1544 // Intersection must be exact.
1545 EXPECT_EQ(result(0).point.get_y(), Geom_Number(0)); // should be on y=0 plane
1546}
1547
1548
1549// ---------- 5.2 Numerical robustness: extreme coordinates ----------
1550
1551TEST_F(GeomAlgorithmsTest, RobustnessExtremeCoordinates)
1552{
1553 // Points with very large coordinates — exact arithmetic handles this.
1554 Geom_Number big(1000000000); // 10^9
1555
1556 DynList<Point> pts;
1557 pts.append(Point(big, big));
1558 pts.append(Point(-big, big));
1559 pts.append(Point(-big, -big));
1560 pts.append(Point(big, -big));
1561 pts.append(Point(0, 0));
1562
1564 auto r = delaunay(pts);
1565 EXPECT_GE(r.triangles.size(), 1u);
1566
1567 // Verify circumcircle property with big coords.
1568 for (size_t t = 0; t < r.triangles.size(); ++t)
1569 {
1570 const auto & tri = r.triangles(t);
1571 const Point cc = circumcenter_of(r.sites(tri.i), r.sites(tri.j),
1572 r.sites(tri.k));
1573 const Geom_Number r2 = dist2(cc, r.sites(tri.i));
1574 for (size_t s = 0; s < r.sites.size(); ++s)
1575 {
1576 if (s == tri.i or s == tri.j or s == tri.k)
1577 continue;
1578 EXPECT_GE(dist2(cc, r.sites(s)), r2);
1579 }
1580 }
1581}
1582
1583
1584TEST_F(GeomAlgorithmsTest, RobustnessVerySmallCoordinates)
1585{
1586 // Points with very small coordinates.
1587 Geom_Number eps(1, 1000000000); // 10^-9
1588
1589 DynList<Point> pts;
1590 pts.append(Point(0, 0));
1591 pts.append(Point(eps, 0));
1592 pts.append(Point(0, eps));
1593 pts.append(Point(eps, eps));
1594
1596 auto r = delaunay(pts);
1597 EXPECT_GE(r.triangles.size(), 2u);
1598}
1599
1600
1601// ---------- 5.2 Numerical robustness: cocircular points ----------
1602
1603TEST_F(GeomAlgorithmsTest, RobustnessCocircularPoints)
1604{
1605 // 8 points on a circle — a degenerate case for Delaunay.
1606 // The triangulation should still be valid and complete.
1607 // Use rational approximations of circle points:
1608 // (2,0), (0,2), (-2,0), (0,-2), and 4 diagonal points.
1609 DynList<Point> pts;
1610 pts.append(Point(2, 0));
1611 pts.append(Point(0, 2));
1612 pts.append(Point(-2, 0));
1613 pts.append(Point(0, -2));
1614
1615 // Points at 45-degree offsets (rational approximation on circle r=2).
1616 // Use exact: sqrt(2) ≈ 1414/1000 (close enough for testing cocircularity).
1617 // Actually, for true cocircularity use x^2+y^2 = 4.
1618 // (1, sqrt(3)) is on circle r=2: 1+3=4. Use Geom_Number for sqrt(3).
1619 // Instead, let's use: (8/5, 6/5) since (8/5)^2+(6/5)^2 = 64/25+36/25 = 100/25 = 4.
1620 pts.append(Point(Geom_Number(8, 5), Geom_Number(6, 5)));
1621 pts.append(Point(Geom_Number(-8, 5), Geom_Number(6, 5)));
1622 pts.append(Point(Geom_Number(-8, 5), Geom_Number(-6, 5)));
1623 pts.append(Point(Geom_Number(8, 5), Geom_Number(-6, 5)));
1624
1626 auto r = delaunay(pts);
1627
1629 for (DynList<Point>::Iterator it(pts); it.has_curr(); it.next_ne())
1630 scene.points.append(it.get_curr());
1631 for (size_t i = 0; i < r.triangles.size(); ++i)
1632 {
1633 const auto & t = r.triangles(i);
1634 scene.segments.append(Segment(r.sites(t.i), r.sites(t.j)));
1635 scene.segments.append(Segment(r.sites(t.j), r.sites(t.k)));
1636 scene.segments.append(Segment(r.sites(t.k), r.sites(t.i)));
1637 }
1639 "case_robust_cocircular_delaunay", scene,
1640 "Delaunay robustness / cocircular points");
1641
1642 // Must produce a triangulation.
1643 EXPECT_GE(r.triangles.size(), 6u); // at least 6 triangles for 8 cocircular pts
1644
1645 // All sites should participate.
1646 EXPECT_EQ(r.sites.size(), 8u);
1647}
1648
1649
1650// ---------- 5.3 Determinism: permuted inputs produce same results ----------
1651
1652TEST_F(GeomAlgorithmsTest, DeterminismDelaunayPermutedInputs)
1653{
1654 // The Delaunay output should be the same regardless of input order.
1655 DynList<Point> pts1;
1656 pts1.append(Point(0, 0));
1657 pts1.append(Point(5, 0));
1658 pts1.append(Point(6, 3));
1659 pts1.append(Point(0, 4));
1660 pts1.append(Point(3, 2));
1661
1662 DynList<Point> pts2; // reverse order
1663 pts2.append(Point(3, 2));
1664 pts2.append(Point(0, 4));
1665 pts2.append(Point(6, 3));
1666 pts2.append(Point(5, 0));
1667 pts2.append(Point(0, 0));
1668
1669 DynList<Point> pts3; // shuffled
1670 pts3.append(Point(6, 3));
1671 pts3.append(Point(0, 0));
1672 pts3.append(Point(3, 2));
1673 pts3.append(Point(5, 0));
1674 pts3.append(Point(0, 4));
1675
1677 auto r1 = delaunay(pts1);
1678 auto r2 = delaunay(pts2);
1679 auto r3 = delaunay(pts3);
1680
1681 // Same number of sites and triangles.
1682 EXPECT_EQ(r1.sites.size(), r2.sites.size());
1683 EXPECT_EQ(r1.sites.size(), r3.sites.size());
1684 EXPECT_EQ(r1.triangles.size(), r2.triangles.size());
1685 EXPECT_EQ(r1.triangles.size(), r3.triangles.size());
1686
1687 // Canonical triangle sets should match.
1688 auto ct1 = canonical_triangles(r1);
1689 auto ct2 = canonical_triangles(r2);
1690 auto ct3 = canonical_triangles(r3);
1691
1692 ASSERT_EQ(ct1.size(), ct2.size());
1693 ASSERT_EQ(ct1.size(), ct3.size());
1694
1695 for (size_t i = 0; i < ct1.size(); ++i)
1696 {
1697 EXPECT_EQ(ct1(i).a, ct2(i).a);
1698 EXPECT_EQ(ct1(i).b, ct2(i).b);
1699 EXPECT_EQ(ct1(i).c, ct2(i).c);
1700 EXPECT_EQ(ct1(i).a, ct3(i).a);
1701 EXPECT_EQ(ct1(i).b, ct3(i).b);
1702 EXPECT_EQ(ct1(i).c, ct3(i).c);
1703 }
1704}
1705
1706
1707TEST_F(GeomAlgorithmsTest, DeterminismConvexHullPermutedInputs)
1708{
1709 DynList<Point> pts1;
1710 pts1.append(Point(0, 0));
1711 pts1.append(Point(5, 0));
1712 pts1.append(Point(6, 3));
1713 pts1.append(Point(0, 4));
1714 pts1.append(Point(3, 1)); // interior point
1715
1716 DynList<Point> pts2;
1717 pts2.append(Point(3, 1));
1718 pts2.append(Point(0, 4));
1719 pts2.append(Point(6, 3));
1720 pts2.append(Point(5, 0));
1721 pts2.append(Point(0, 0));
1722
1724 Polygon h1 = andrew(pts1);
1725 Polygon h2 = andrew(pts2);
1726
1727 auto v1 = sorted_hull_vertices(h1);
1728 auto v2 = sorted_hull_vertices(h2);
1729
1730 ASSERT_EQ(v1.size(), v2.size());
1731 for (size_t i = 0; i < v1.size(); ++i)
1732 EXPECT_EQ(v1(i), v2(i));
1733}
1734
1735
1736TEST_F(GeomAlgorithmsTest, DeterminismClosestPairPermutedInputs)
1737{
1738 DynList<Point> pts1;
1739 pts1.append(Point(0, 0));
1740 pts1.append(Point(10, 10));
1741 pts1.append(Point(1, 0)); // closest pair: (0,0)-(1,0)
1742 pts1.append(Point(5, 5));
1743
1744 DynList<Point> pts2;
1745 pts2.append(Point(5, 5));
1746 pts2.append(Point(1, 0));
1747 pts2.append(Point(0, 0));
1748 pts2.append(Point(10, 10));
1749
1751 auto r1 = cp(pts1);
1752 auto r2 = cp(pts2);
1753
1754 EXPECT_EQ(r1.distance_squared, r2.distance_squared);
1755 // Same pair (possibly swapped).
1756 EXPECT_TRUE(matches_unordered_pair(r1.first, r1.second, r2.first, r2.second));
1757}
1758
1759
1760// ---------- 5.4 Performance: large datasets ----------
1761
1762TEST_F(GeomAlgorithmsTest, PerformanceConvexHull10KPoints)
1763{
1764 // 10000 points on a grid — convex hull should return the boundary.
1765 DynList<Point> pts;
1766 for (int x = 0; x < 100; ++x)
1767 for (int y = 0; y < 100; ++y)
1768 pts.append(Point(x, y));
1769
1771 Polygon hull = andrew(pts);
1772
1773 EXPECT_TRUE(hull.is_closed());
1774 // The hull of a grid is the bounding rectangle.
1775 EXPECT_EQ(hull.size(), 4u);
1776 EXPECT_TRUE(polygon_contains_vertex(hull, Point(0, 0)));
1777 EXPECT_TRUE(polygon_contains_vertex(hull, Point(99, 0)));
1778 EXPECT_TRUE(polygon_contains_vertex(hull, Point(99, 99)));
1779 EXPECT_TRUE(polygon_contains_vertex(hull, Point(0, 99)));
1780}
1781
1782
1783TEST_F(GeomAlgorithmsTest, PerformanceClosestPair5KPoints)
1784{
1785 // 5000 points on a grid; minimum distance = 1.
1786 DynList<Point> pts;
1787 for (int x = 0; x < 50; ++x)
1788 for (int y = 0; y < 100; ++y)
1789 pts.append(Point(x, y));
1790
1792 auto r = cp(pts);
1793
1794 EXPECT_EQ(r.distance_squared, Geom_Number(1));
1795}
1796
1797
1798TEST_F(GeomAlgorithmsTest, PerformanceDelaunay500Points)
1799{
1800 // 500 points on a grid — verify valid Delaunay.
1801 DynList<Point> pts;
1802 for (int x = 0; x < 25; ++x)
1803 for (int y = 0; y < 20; ++y)
1804 pts.append(Point(x, y));
1805
1807 auto r = delaunay(pts);
1808
1809 EXPECT_GE(r.triangles.size(), 1u);
1810
1811 // Spot-check a few triangles for circumcircle property.
1812 const size_t check_limit = r.triangles.size() < 50 ? r.triangles.size() : 50;
1813 for (size_t t = 0; t < check_limit; ++t)
1814 {
1815 const auto & tri = r.triangles(t);
1816 const Point cc = circumcenter_of(r.sites(tri.i), r.sites(tri.j),
1817 r.sites(tri.k));
1818 const Geom_Number r2 = dist2(cc, r.sites(tri.i));
1819 for (size_t s = 0; s < r.sites.size(); ++s)
1820 {
1821 if (s == tri.i or s == tri.j or s == tri.k)
1822 continue;
1823 EXPECT_GE(dist2(cc, r.sites(s)), r2);
1824 }
1825 }
1826}
1827
1828
1829TEST_F(GeomAlgorithmsTest, PerformanceTriangulation100Vertices)
1830{
1831 // Build a simple polygon with ~100 vertices (zigzag) — no collinear edges.
1832 // Triangulation should produce n-2 triangles.
1833 Polygon p;
1834
1835 // Bottom zigzag: (0,0), (1,1), (2,0), (3,1), ..., (48,0), (49,1), (50,0)
1836 for (int x = 0; x <= 50; ++x)
1837 p.add_vertex(Point(x, (x % 2 == 0) ? 0 : 1));
1838
1839 // Top zigzag going back: (50,10), (49,9), (48,10), ..., (1,9), (0,10)
1840 for (int x = 50; x >= 0; --x)
1841 p.add_vertex(Point(x, (x % 2 == 0) ? 10 : 9));
1842
1843 p.close();
1844
1845 const size_t nv = p.size();
1846 ASSERT_GE(nv, 50u);
1847
1849 DynList<Triangle> tris = ears(p);
1850
1851 size_t count = 0;
1852 for (DynList<Triangle>::Iterator it(tris); it.has_curr(); it.next_ne())
1853 ++count;
1854
1855 EXPECT_EQ(count, nv - 2);
1856}
1857
1858
1859// ---------- 5.5 Cross-algorithm comparison: 5 convex hulls ----------
1860
1861TEST_F(GeomAlgorithmsTest, CrossAlgorithmConvexHullSimple)
1862{
1863 DynList<Point> pts;
1864 pts.append(Point(0, 0));
1865 pts.append(Point(5, 0));
1866 pts.append(Point(6, 3));
1867 pts.append(Point(3, 6));
1868 pts.append(Point(0, 4));
1869 pts.append(Point(2, 1)); // interior
1870 pts.append(Point(3, 2)); // interior
1871
1873 GrahamScanConvexHull graham;
1876 QuickHull quick;
1877
1878 Polygon h_andrew = andrew(pts);
1879 Polygon h_graham = graham(pts);
1880 Polygon h_brute = brute(pts);
1881 Polygon h_gift = gift(pts);
1882 Polygon h_quick = quick(pts);
1883
1884 auto v_andrew = sorted_hull_vertices(h_andrew);
1885 auto v_graham = sorted_hull_vertices(h_graham);
1886 auto v_brute = sorted_hull_vertices(h_brute);
1887 auto v_gift = sorted_hull_vertices(h_gift);
1888 auto v_quick = sorted_hull_vertices(h_quick);
1889
1890 // All must have the same vertex count.
1891 ASSERT_EQ(v_andrew.size(), v_graham.size())
1892 << "Andrew vs Graham vertex count mismatch";
1893 ASSERT_EQ(v_andrew.size(), v_brute.size())
1894 << "Andrew vs BruteForce vertex count mismatch";
1895 ASSERT_EQ(v_andrew.size(), v_gift.size())
1896 << "Andrew vs GiftWrapping vertex count mismatch";
1897 ASSERT_EQ(v_andrew.size(), v_quick.size())
1898 << "Andrew vs QuickHull vertex count mismatch";
1899
1900 // All must have the same vertices.
1901 for (size_t i = 0; i < v_andrew.size(); ++i)
1902 {
1903 EXPECT_EQ(v_andrew(i), v_graham(i))
1904 << "Andrew vs Graham mismatch at index " << i;
1905 EXPECT_EQ(v_andrew(i), v_brute(i))
1906 << "Andrew vs BruteForce mismatch at index " << i;
1907 EXPECT_EQ(v_andrew(i), v_gift(i))
1908 << "Andrew vs GiftWrapping mismatch at index " << i;
1909 EXPECT_EQ(v_andrew(i), v_quick(i))
1910 << "Andrew vs QuickHull mismatch at index " << i;
1911 }
1912}
1913
1914
1915TEST_F(GeomAlgorithmsTest, CrossAlgorithmConvexHullLargerSet)
1916{
1917 // 100 points, mix of grid + interior + boundary.
1918 DynList<Point> pts;
1919 for (int x = 0; x <= 10; ++x)
1920 for (int y = 0; y <= 10; ++y)
1921 pts.append(Point(x, y));
1922
1923 // Add some extra interior points.
1924 pts.append(Point(5, 5));
1925 pts.append(Point(3, 7));
1926 pts.append(Point(8, 2));
1927
1929 GrahamScanConvexHull graham;
1932 QuickHull quick;
1933
1934 Polygon h_andrew = andrew(pts);
1935 Polygon h_graham = graham(pts);
1936 Polygon h_brute = brute(pts);
1937 Polygon h_gift = gift(pts);
1938 Polygon h_quick = quick(pts);
1939
1940 auto v_andrew = sorted_hull_vertices(h_andrew);
1941 auto v_graham = sorted_hull_vertices(h_graham);
1942 auto v_brute = sorted_hull_vertices(h_brute);
1943 auto v_gift = sorted_hull_vertices(h_gift);
1944 auto v_quick = sorted_hull_vertices(h_quick);
1945
1946 ASSERT_EQ(v_andrew.size(), v_graham.size());
1947 ASSERT_EQ(v_andrew.size(), v_brute.size());
1948 ASSERT_EQ(v_andrew.size(), v_gift.size());
1949 ASSERT_EQ(v_andrew.size(), v_quick.size());
1950
1951 for (size_t i = 0; i < v_andrew.size(); ++i)
1952 {
1953 EXPECT_EQ(v_andrew(i), v_graham(i));
1954 EXPECT_EQ(v_andrew(i), v_brute(i));
1955 EXPECT_EQ(v_andrew(i), v_gift(i));
1956 EXPECT_EQ(v_andrew(i), v_quick(i));
1957 }
1958}
1959
1960
1961TEST_F(GeomAlgorithmsTest, CrossAlgorithmConvexHullCollinearBoundary)
1962{
1963 // Many collinear points on the hull boundary.
1964 DynList<Point> pts;
1965 for (int x = 0; x <= 20; ++x)
1966 {
1967 pts.append(Point(x, 0)); // bottom
1968 pts.append(Point(x, 10)); // top
1969 }
1970 pts.append(Point(0, 5)); // left
1971 pts.append(Point(20, 5)); // right
1972
1974 GrahamScanConvexHull graham;
1975 QuickHull quick;
1976
1977 Polygon h_andrew = andrew(pts);
1978 Polygon h_graham = graham(pts);
1979 Polygon h_quick = quick(pts);
1980
1981 auto v_andrew = sorted_hull_vertices(h_andrew);
1982 auto v_graham = sorted_hull_vertices(h_graham);
1983 auto v_quick = sorted_hull_vertices(h_quick);
1984
1985 // For collinear points, algorithms may differ on whether they include
1986 // intermediate points. Compare just the extreme corners.
1987 EXPECT_TRUE(polygon_contains_vertex(h_andrew, Point(0, 0)));
1988 EXPECT_TRUE(polygon_contains_vertex(h_andrew, Point(20, 0)));
1989 EXPECT_TRUE(polygon_contains_vertex(h_andrew, Point(20, 10)));
1990 EXPECT_TRUE(polygon_contains_vertex(h_andrew, Point(0, 10)));
1991
1992 EXPECT_TRUE(polygon_contains_vertex(h_graham, Point(0, 0)));
1993 EXPECT_TRUE(polygon_contains_vertex(h_graham, Point(20, 0)));
1994 EXPECT_TRUE(polygon_contains_vertex(h_graham, Point(20, 10)));
1995 EXPECT_TRUE(polygon_contains_vertex(h_graham, Point(0, 10)));
1996
1997 EXPECT_TRUE(polygon_contains_vertex(h_quick, Point(0, 0)));
1998 EXPECT_TRUE(polygon_contains_vertex(h_quick, Point(20, 0)));
1999 EXPECT_TRUE(polygon_contains_vertex(h_quick, Point(20, 10)));
2000 EXPECT_TRUE(polygon_contains_vertex(h_quick, Point(0, 10)));
2001}
2002
2003
2004TEST_F(GeomAlgorithmsTest, CrossAlgorithmConvexHullTrianglePoints)
2005{
2006 // All points on hull (triangle) — all algorithms must agree.
2007 DynList<Point> pts;
2008 pts.append(Point(0, 0));
2009 pts.append(Point(10, 0));
2010 pts.append(Point(5, 8));
2011
2013 GrahamScanConvexHull graham;
2016 QuickHull quick;
2017
2018 auto v_andrew = sorted_hull_vertices(andrew(pts));
2019 auto v_graham = sorted_hull_vertices(graham(pts));
2020 auto v_brute = sorted_hull_vertices(brute(pts));
2021 auto v_gift = sorted_hull_vertices(gift(pts));
2022 auto v_quick = sorted_hull_vertices(quick(pts));
2023
2024 ASSERT_EQ(v_andrew.size(), 3u);
2025 ASSERT_EQ(v_graham.size(), 3u);
2026 ASSERT_EQ(v_brute.size(), 3u);
2027 ASSERT_EQ(v_gift.size(), 3u);
2028 ASSERT_EQ(v_quick.size(), 3u);
2029
2030 for (size_t i = 0; i < 3; ++i)
2031 {
2032 EXPECT_EQ(v_andrew(i), v_graham(i));
2033 EXPECT_EQ(v_andrew(i), v_brute(i));
2034 EXPECT_EQ(v_andrew(i), v_gift(i));
2035 EXPECT_EQ(v_andrew(i), v_quick(i));
2036 }
2037}
2038
2039
2040// ============================================================================
2041// Section 5.1 — Tests for new algorithms
2042// ============================================================================
2043
2044// ---------- Delaunay O(n log n) — randomized incremental ----------
2045
2046TEST_F(GeomAlgorithmsTest, DelaunayIncrementalBasicSquare)
2047{
2048 DynList<Point> pts;
2049 pts.append(Point(0, 0));
2050 pts.append(Point(4, 0));
2051 pts.append(Point(4, 4));
2052 pts.append(Point(0, 4));
2053
2055 auto r = delaunay(pts);
2056
2057 EXPECT_EQ(r.sites.size(), 4u);
2058 EXPECT_EQ(r.triangles.size(), 2u);
2059}
2060
2061
2062TEST_F(GeomAlgorithmsTest, DelaunayIncrementalEmptyCircumcircle)
2063{
2064 DynList<Point> pts;
2065 pts.append(Point(0, 0));
2066 pts.append(Point(5, 0));
2067 pts.append(Point(5, 5));
2068 pts.append(Point(0, 5));
2069 pts.append(Point(2, 3));
2070 pts.append(Point(3, 1));
2071
2073 auto r = delaunay(pts);
2074
2075 EXPECT_GE(r.triangles.size(), 4u);
2076
2077 for (size_t t = 0; t < r.triangles.size(); ++t)
2078 {
2079 const auto & tri = r.triangles(t);
2080 const Point cc = circumcenter_of(r.sites(tri.i), r.sites(tri.j),
2081 r.sites(tri.k));
2082 const Geom_Number r2 = dist2(cc, r.sites(tri.i));
2083 for (size_t s = 0; s < r.sites.size(); ++s)
2084 {
2085 if (s == tri.i || s == tri.j || s == tri.k)
2086 continue;
2087 EXPECT_GE(dist2(cc, r.sites(s)), r2)
2088 << "Delaunay incremental: site " << s
2089 << " violates circumcircle of triangle " << t;
2090 }
2091 }
2092}
2093
2094
2095TEST_F(GeomAlgorithmsTest, DelaunayIncrementalMatchesBowyerWatson)
2096{
2097 DynList<Point> pts;
2098 pts.append(Point(0, 0));
2099 pts.append(Point(10, 0));
2100 pts.append(Point(10, 10));
2101 pts.append(Point(0, 10));
2102 pts.append(Point(5, 5));
2103 pts.append(Point(3, 7));
2104 pts.append(Point(7, 2));
2105 pts.append(Point(1, 3));
2106
2108 auto rbw = bw(pts);
2109
2111 auto rinc = inc(pts);
2112
2113 EXPECT_EQ(rbw.sites.size(), rinc.sites.size());
2114 EXPECT_EQ(rbw.triangles.size(), rinc.triangles.size());
2115}
2116
2117
2118TEST_F(GeomAlgorithmsTest, DelaunayIncrementalSingleTriangle)
2119{
2121 auto r = delaunay({Point(0, 0), Point(1, 0), Point(0, 1)});
2122 EXPECT_EQ(r.sites.size(), 3u);
2123 EXPECT_EQ(r.triangles.size(), 1u);
2124}
2125
2126
2127TEST_F(GeomAlgorithmsTest, DelaunayIncrementalCollinear)
2128{
2129 DynList<Point> pts;
2130 pts.append(Point(0, 0));
2131 pts.append(Point(1, 0));
2132 pts.append(Point(2, 0));
2133 pts.append(Point(3, 0));
2134
2136 auto r = delaunay(pts);
2137
2138 EXPECT_EQ(r.triangles.size(), 0u);
2139}
2140
2141
2142TEST_F(GeomAlgorithmsTest, DelaunayIncrementalDuplicates)
2143{
2144 DynList<Point> pts;
2145 pts.append(Point(0, 0));
2146 pts.append(Point(1, 0));
2147 pts.append(Point(0, 1));
2148 pts.append(Point(0, 0));
2149 pts.append(Point(1, 0));
2150
2152 auto r = delaunay(pts);
2153 EXPECT_EQ(r.sites.size(), 3u);
2154 EXPECT_EQ(r.triangles.size(), 1u);
2155}
2156
2157
2158TEST_F(GeomAlgorithmsTest, DelaunayIncrementalGrid)
2159{
2160 DynList<Point> pts;
2161 for (int x = 0; x <= 4; ++x)
2162 for (int y = 0; y <= 4; ++y)
2163 pts.append(Point(x, y));
2164
2166 auto r = delaunay(pts);
2167
2168 EXPECT_EQ(r.sites.size(), 25u);
2169 EXPECT_GE(r.triangles.size(), 32u);
2170
2171 for (size_t t = 0; t < r.triangles.size(); ++t)
2172 {
2173 const auto & tri = r.triangles(t);
2174 const Point cc = circumcenter_of(r.sites(tri.i), r.sites(tri.j),
2175 r.sites(tri.k));
2176 const Geom_Number cr2 = dist2(cc, r.sites(tri.i));
2177 for (size_t s = 0; s < r.sites.size(); ++s)
2178 {
2179 if (s == tri.i || s == tri.j || s == tri.k)
2180 continue;
2181 EXPECT_GE(dist2(cc, r.sites(s)), cr2);
2182 }
2183 }
2184}
2185
2186
2187// ---------- VoronoiDiagramFortune ----------
2188
2189// VoronoiDiagramFortune is deprecated, but supported until 8.0.0: these
2190// tests are its coverage
2191#if defined(_MSC_VER) && !defined(__clang__)
2192# pragma warning(push)
2193# pragma warning(disable : 4996)
2194#else
2195# pragma GCC diagnostic push
2196# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2197#endif
2198
2199TEST_F(GeomAlgorithmsTest, VoronoiFortuneFourPoints)
2200{
2201 VoronoiDiagramFortune voronoi;
2202 auto r = voronoi({Point(0, 0), Point(4, 0), Point(4, 4), Point(0, 4)});
2203
2204 EXPECT_EQ(r.sites.size(), 4u);
2205 EXPECT_GE(r.vertices.size(), 1u);
2206 EXPECT_GE(r.edges.size(), 1u);
2207}
2208
2209
2210TEST_F(GeomAlgorithmsTest, VoronoiFortuneEquidistance)
2211{
2212 DynList<Point> pts;
2213 pts.append(Point(0, 0));
2214 pts.append(Point(6, 0));
2215 pts.append(Point(3, 5));
2216 pts.append(Point(6, 5));
2217 pts.append(Point(0, 5));
2218
2219 VoronoiDiagramFortune voronoi;
2220 auto r = voronoi(pts);
2221
2222 for (size_t e = 0; e < r.edges.size(); ++e)
2223 {
2224 const auto & edge = r.edges(e);
2225 if (edge.unbounded) continue;
2226
2227 const Geom_Number d_u = dist2(edge.src, r.sites(edge.site_u));
2228 const Geom_Number d_v = dist2(edge.src, r.sites(edge.site_v));
2229 EXPECT_EQ(d_u, d_v) << "Voronoi edge src not equidistant for edge " << e;
2230 }
2231}
2232
2233
2234TEST_F(GeomAlgorithmsTest, VoronoiFortuneClippedCells)
2235{
2236 DynList<Point> pts;
2237 pts.append(Point(1, 1));
2238 pts.append(Point(3, 1));
2239 pts.append(Point(2, 3));
2240
2241 Polygon clip;
2242 clip.add_vertex(Point(0, 0));
2243 clip.add_vertex(Point(4, 0));
2244 clip.add_vertex(Point(4, 4));
2245 clip.add_vertex(Point(0, 4));
2246 clip.close();
2247
2248 VoronoiDiagramFortune voronoi;
2249 auto cells = voronoi.clipped_cells(pts, clip);
2250
2251 EXPECT_EQ(cells.size(), 3u);
2252 for (size_t i = 0; i < cells.size(); ++i)
2253 EXPECT_TRUE(cells(i).polygon.is_closed());
2254}
2255
2256
2257TEST_F(GeomAlgorithmsTest, VoronoiFortuneKeepsDualImplementationAvailable)
2258{
2259 DynList<Point> pts;
2260 pts.append(Point(0, 0));
2261 pts.append(Point(6, 0));
2262 pts.append(Point(7, 3));
2263 pts.append(Point(2, 7));
2264 pts.append(Point(-1, 4));
2265
2266 VoronoiDiagram dual_voronoi;
2267 VoronoiDiagramFortune fortune_voronoi;
2268
2269 const auto dual = dual_voronoi(pts);
2270 const auto fortune = fortune_voronoi(pts);
2271
2272 EXPECT_EQ(fortune.sites.size(), dual.sites.size());
2273 EXPECT_EQ(fortune.vertices.size(), dual.vertices.size());
2274 EXPECT_EQ(fortune.edges.size(), dual.edges.size());
2275 EXPECT_EQ(fortune.cells.size(), dual.cells.size());
2276}
2277
2278TEST_F(GeomAlgorithmsTest, VoronoiFortuneGivesTheVerticesOfVoronoiDiagram)
2279{
2280 // It ran Bowyer-Watson, quadratic, and a sweep of Fortune; it computes as
2281 // VoronoiDiagram now, whose order of insertion is random: the vertices
2282 // are the same, as sets. VoronoiDiagramFromDelaunay, which triangulates
2283 // with Bowyer-Watson, gives them apart from VoronoiDiagram
2284 std::mt19937 gen(99);
2285 std::uniform_int_distribution<int> coord(0, 100000);
2286 DynList<Point> pts;
2287 for (int i = 0; i < 300; ++i)
2288 pts.append(Point(coord(gen), coord(gen)));
2289
2290 const auto fortune = VoronoiDiagramFortune()(pts);
2291 const auto dual = VoronoiDiagram()(pts);
2292 const auto bowyer_watson = VoronoiDiagramFromDelaunay()(pts);
2293 auto sorted_vertices = [] (const auto & r)
2294 {
2295 std::vector<std::pair<Geom_Number, Geom_Number>> v;
2296 for (size_t i = 0; i < r.vertices.size(); ++i)
2297 v.emplace_back(r.vertices(i).get_x(), r.vertices(i).get_y());
2298 std::sort(v.begin(), v.end());
2299 return v;
2300 };
2301 EXPECT_EQ(sorted_vertices(fortune), sorted_vertices(dual));
2302 EXPECT_EQ(sorted_vertices(fortune), sorted_vertices(bowyer_watson));
2303 EXPECT_FALSE(sorted_vertices(fortune).empty());
2304 EXPECT_EQ(fortune.sites.size(), 300u);
2305}
2306
2307#if defined(_MSC_VER) && !defined(__clang__)
2308# pragma warning(pop)
2309#else
2310# pragma GCC diagnostic pop
2311#endif
2312
2313
2314// ---------- ConvexPolygonDecomposition ----------
2315
2316TEST_F(GeomAlgorithmsTest, ConvexDecompTriangle)
2317{
2318 Polygon p;
2319 p.add_vertex(Point(0, 0));
2320 p.add_vertex(Point(4, 0));
2321 p.add_vertex(Point(2, 3));
2322 p.close();
2323
2325 auto parts = decomp(p);
2326
2327 EXPECT_EQ(parts.size(), 1u);
2328 EXPECT_TRUE(parts(0).is_closed());
2329}
2330
2331
2332// ---------- SweepLineSegmentIntersection: new critical tests ----------
2333
2334TEST_F(GeomAlgorithmsTest, SweepLineCollinearOverlapping)
2335{
2336 // Two collinear overlapping segments on the x-axis.
2337 // Segments [0,4] and [2,6] overlap on [2,4].
2339 Array<Segment> segs;
2340 segs.append(Segment(Point(0, 0), Point(4, 0)));
2341 segs.append(Segment(Point(2, 0), Point(6, 0)));
2342
2343 auto result = sweep(segs);
2344
2345 // Overlap must be reported at least at one overlap boundary point.
2346 EXPECT_GE(result.size(), 1u)
2347 << "Collinear overlapping segments were not reported";
2348}
2349
2350
2351TEST_F(GeomAlgorithmsTest, SweepLineManySegmentsAtOnePoint)
2352{
2353 // 10 segments all sharing endpoint (5,5), fanning out.
2355 Array<Segment> segs;
2356 const size_t N = 10;
2357 for (size_t i = 0; i < N; ++i)
2358 {
2359 Geom_Number angle = Geom_Number(2) * Geom_Number(M_PI) * Geom_Number(static_cast<long>(i))
2360 / Geom_Number(static_cast<long>(N));
2361 Point far(Geom_Number(5) + Geom_Number(10) * Geom_Number(cos(angle.get_d())),
2362 Geom_Number(5) + Geom_Number(10) * Geom_Number(sin(angle.get_d())));
2363 segs.append(Segment(Point(5, 5), far));
2364 }
2365
2366 auto result = sweep(segs);
2367
2368 // C(10,2) = 45 pairwise intersections, all at (5,5).
2369 EXPECT_EQ(result.size(), N * (N - 1) / 2);
2370 for (size_t i = 0; i < result.size(); ++i)
2371 EXPECT_EQ(result(i).point, Point(5, 5));
2372}
2373
2374
2375TEST_F(GeomAlgorithmsTest, SweepLineVerticalSegments)
2376{
2377 // Mix of vertical, horizontal, and diagonal segments.
2379 Array<Segment> segs;
2380
2381 segs.append(Segment(Point(3, 0), Point(3, 6))); // vertical
2382 segs.append(Segment(Point(0, 3), Point(6, 3))); // horizontal
2383 segs.append(Segment(Point(0, 0), Point(6, 6))); // diagonal
2384
2385 auto result = sweep(segs);
2386
2387 // vertical x horizontal at (3,3), vertical x diagonal at (3,3),
2388 // horizontal x diagonal at (3,3).
2389 EXPECT_EQ(result.size(), 3u);
2390 for (size_t i = 0; i < result.size(); ++i)
2391 EXPECT_EQ(result(i).point, Point(3, 3));
2392}
2393
2394
2395TEST_F(GeomAlgorithmsTest, SweepLineStress10K)
2396{
2397 // 10K random short segments. Verify each reported intersection is real.
2399 Array<Segment> segs;
2400
2401 std::mt19937 rng(999);
2402 std::uniform_real_distribution<double> coord(-100.0, 100.0);
2403 std::uniform_real_distribution<double> delta(-5.0, 5.0);
2404
2405 const size_t N = 10000;
2406 for (size_t i = 0; i < N; ++i)
2407 {
2408 double x = coord(rng), y = coord(rng);
2409 double dx = delta(rng), dy = delta(rng);
2410 if (dx == 0 and dy == 0) dx = 1;
2412 Point(Geom_Number(x + dx), Geom_Number(y + dy))));
2413 }
2414
2415 auto result = sweep(segs);
2416
2417 // Spot-check first 100 intersections: the reported point must lie on
2418 // both participating segments (within tolerance).
2419 const size_t check = std::min(result.size(), (size_t) 100);
2420 for (size_t i = 0; i < check; ++i)
2421 {
2422 const auto & ix = result(i);
2423 const Segment & s1 = segs(ix.seg_i);
2424 const Segment & s2 = segs(ix.seg_j);
2425
2426 // Verify the two segments actually intersect.
2427 EXPECT_TRUE(s1.intersects_with(s2))
2428 << "seg " << ix.seg_i << " and seg " << ix.seg_j
2429 << " reported as intersecting but don't";
2430 }
2431}
size_t size_t int32_t * out
Definition ca-c-api.h:120
Andrew's monotonic chain convex hull algorithm.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Brute force convex hull algorithm.
Closest pair of points via divide and conquer.
Decompose a simple polygon into convex parts using Hertel-Mehlhorn.
Distance between two closed convex polygons using GJK.
Polygon triangulation using the ear-cutting algorithm.
Exact Delaunay triangulation using the Bowyer-Watson incremental algorithm.
static DynList< Triangle > as_triangles(const Result &result)
Convert indexed triangulation to geometric triangles.
O(n log n) expected-time Delaunay's triangulation.
Iterator on the items of list.
Definition htlist.H:1614
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
static Array< Point > extract_vertices(const Polygon &poly)
Extract vertices from a polygon into an array for indexed access.
Gift wrapping (Jarvis march) convex hull algorithm.
Graham scan convex hull algorithm.
bool has_curr() const noexcept
Definition htlist.H:969
Spatial point index for O(log n) nearest-neighbor queries.
DebugSnapshot debug_snapshot() const
Build a deterministic geometric partition snapshot for visualization.
bool is_empty() const noexcept
Return true if the tree is empty.
static KDTreePointSearch build(const Array< Point > &points, const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax)
Build a balanced KD-tree from a point array.
size_t size() const noexcept
Return the number of points in the tree.
bool contains(const Point &p) const
Check if a point exists in the tree.
std::optional< Point > nearest(const Point &p) const
Find the nearest neighbor to query point p.
void range(const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax, DynList< Point > *out) const
Collect all points inside the given rectangle.
bool insert(const Point &p)
Insert a point. Returns true if inserted, false if duplicate.
void for_each(Op &&op) const
Apply an operation to every point (inorder traversal).
Exact Minkowski sum of two closed convex polygons.
O(n log n) triangulation of simple polygons via y-monotone partition + linear-time monotone triangula...
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
static bool contains(const Polygon &poly, const Point &p)
Return true if the point is inside or on the boundary.
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:448
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:457
Geom_Number distance_squared_to(const Point &that) const
Calculates the squared Euclidean distance to another point.
Definition point.H:1490
Iterator over the edges (segments) of a polygon.
Definition polygon.H:529
bool has_curr() const
Check if there is a current segment.
Definition polygon.H:546
A general (irregular) 2D polygon defined by a sequence of vertices.
Definition polygon.H:247
void add_vertex(const Point &point)
Add a vertex to the polygon.
Definition polygon.H:686
void close()
Close the polygon.
Definition polygon.H:851
const bool & is_closed() const
Check if the polygon is closed.
Definition polygon.H:482
const size_t & size() const
Get the number of vertices.
Definition polygon.H:486
QuickHull convex hull algorithm.
An axis-aligned rectangle.
Definition point.H:1789
const Geom_Number & get_xmin() const
Gets the minimum x-coordinate.
Definition point.H:1815
const Geom_Number & get_ymax() const
Gets the maximum y-coordinate.
Definition point.H:1830
const Geom_Number & get_ymin() const
Gets the minimum y-coordinate.
Definition point.H:1820
const Geom_Number & get_xmax() const
Gets the maximum x-coordinate.
Definition point.H:1825
Rotating calipers metrics for convex polygons.
static DiameterResult diameter(const Polygon &poly)
Compute convex polygon diameter (farthest vertex pair).
static WidthResult minimum_width(const Polygon &poly)
Compute the minimum width of a closed convex polygon.
Dedicated exact intersection for a single pair of segments.
Represents a line segment between two points.
Definition point.H:837
bool intersects_with(const Segment &s) const
Checks if this segment intersects another one (including endpoints and collinear overlap).
Definition point.H:1291
Point project(const Point &p) const
Orthogonal projection of a point onto this segment's infinite line, clamped to the segment's endpoint...
Definition point.H:1161
Report all pairwise intersection points among a set of segments.
The Voronoi diagram, as VoronoiDiagram computes it.
Array< ClippedCell > clipped_cells(const DynList< Point > &pts, const Polygon &clip) const
Compute Voronoi cells clipped to a bounding polygon.
Voronoi diagram derived as the dual of a Delaunay triangulation.
O(n log n) Voronoi diagram construction.
static mt19937 rng
#define N
Definition fib.C:294
static Array< Point > sorted_hull_vertices(const Polygon &p)
static Point first_vertex_of(const Polygon &poly)
static Geom_Number brute_convex_distance_squared(const Polygon &p, const Polygon &q, Point &out_p, Point &out_q)
static Geom_Number dist2(const Point &a, const Point &b)
TEST_F(GeomAlgorithmsTest, ClosestPairEmptyInputThrows)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_cos_function > > cos(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4147
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sin_function > > sin(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4148
static mpfr_t y
Definition mpfr_mul_d.c:3
void add_polygon_vertices(SvgScene &scene, const ::Polygon &poly, const bool as_highlight=false)
std::filesystem::path emit_case_svg(const std::string &case_id, const SvgScene &scene, const std::string &note="")
Geom_Number area_of_parallelogram(const Point &a, const Point &b, const Point &c)
Compute the signed area of the parallelogram defined by vectors a->b and a->c.
Definition point.H:2886
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:113
SegmentSegmentIntersection::Result segment_segment_intersection(const Segment &s1, const Segment &s2)
Convenience free-function wrapper for SegmentSegmentIntersection.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
Iterator over the vertices of a polygon.
Definition polygon.H:498
bool check(GT &g)
ValueArg< size_t > seed
Definition testHash.C:53
Gsl_Compatible_Rng r