15 EXPECT_THROW((
void) cp(empty), std::domain_error);
24 EXPECT_THROW((
void) cp(one), std::domain_error);
39 EXPECT_EQ(res.distance_squared, 0);
40 EXPECT_EQ(res.first,
Point(7, 7));
41 EXPECT_EQ(res.second,
Point(7, 7));
71 EXPECT_THROW((
void) calipers.
diameter(p), std::domain_error);
72 EXPECT_THROW((
void) calipers.
minimum_width(p), std::domain_error);
105 EXPECT_EQ(hull.
size(), 2u);
107 EXPECT_TRUE(polygon_contains_vertex(hull,
Point(0, 0)));
108 EXPECT_TRUE(polygon_contains_vertex(hull,
Point(5, 5)));
116 EXPECT_EQ(andrew(empty).
size(), 0u);
126 EXPECT_EQ(hull.
size(), 1u);
127 EXPECT_TRUE(polygon_contains_vertex(hull,
Point(42, 17)));
140 EXPECT_EQ(hull.
size(), 1u);
148 EXPECT_EQ(graham(empty).
size(), 0u);
158 EXPECT_EQ(hull.
size(), 1u);
171 EXPECT_EQ(hull.
size(), 2u);
185 EXPECT_EQ(hull.
size(), 1u);
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)
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);
221 Polygon h_andrew = andrew(points);
222 Polygon h_graham = graham(points);
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());
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));
258 EXPECT_EQ(
count,
r.triangles.size());
274 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::NONE);
275 EXPECT_FALSE(result.intersects());
285 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
286 EXPECT_TRUE(result.intersects());
287 EXPECT_EQ(result.point,
Point(2, 2));
297 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
298 EXPECT_EQ(result.point,
Point(4, 0));
307 const auto result = pair_ix(a, b);
315 "case_segment_segment_overlap_o1", scene,
316 "Dedicated O(1) segment-segment overlap");
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));
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));
342 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
343 EXPECT_EQ(result.point,
Point(3, 3));
353 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
354 EXPECT_EQ(result.point,
Point(7, -2));
364 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::NONE);
365 EXPECT_FALSE(result.intersects());
375 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
376 EXPECT_EQ(result.point,
Point(2, 0));
385 EXPECT_EQ(result.kind, SegmentSegmentIntersection::Kind::POINT);
396 auto result = sweep(segs);
397 EXPECT_EQ(result.size(), 0u);
406 auto result = sweep(segs);
407 EXPECT_EQ(result.size(), 0u);
417 auto result = sweep(segs);
418 EXPECT_EQ(result.size(), 0u);
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);
445 auto result = sweep(segs);
448 for (
size_t i = 0; i < segs.
size(); ++i)
450 for (
size_t i = 0; i < result.size(); ++i)
453 "case_sweepline_multiple_intersections", scene,
454 "Sweep-line / multi-intersection degeneracy");
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));
471 auto result = sweep(segs);
472 EXPECT_EQ(result.size(), 0u);
482 auto result = sweep(segs);
483 ASSERT_EQ(result.size(), 1u);
484 EXPECT_EQ(result(0).point,
Point(2, 2));
494 EXPECT_THROW((
void) sweep(segs), std::domain_error);
508 auto result = sweep(segs);
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));
533 EXPECT_EQ(
count, 1u);
552 EXPECT_EQ(
count, 2u);
571 EXPECT_EQ(
count, 2u);
591 EXPECT_EQ(
count, 3u);
612 EXPECT_EQ(
count, 4u);
624 EXPECT_THROW((
void) mt(p), std::domain_error);
668 size_t ear_count = 0;
672 EXPECT_EQ(mt_count, 4u);
673 EXPECT_EQ(mt_count, ear_count);
692 EXPECT_EQ(result.
size(), 4u);
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)));
717 Polygon result = mink(sq, tri);
721 EXPECT_GE(result.
size(), 3u);
722 EXPECT_LE(result.
size(), 7u);
725 EXPECT_TRUE(polygon_contains_vertex(result,
Point(0, 0)));
726 EXPECT_TRUE(polygon_contains_vertex(result,
Point(3, 0)));
727 EXPECT_TRUE(polygon_contains_vertex(result,
Point(0, 3)));
742 Polygon result = mink(sq_cw, sq_cw);
744 EXPECT_EQ(result.
size(), 4u);
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)));
771 EXPECT_THROW((
void) mink(convex, concave), std::domain_error);
772 EXPECT_THROW((
void) mink(concave, convex), std::domain_error);
790 EXPECT_THROW((
void) mink(open, closed), std::domain_error);
812 Polygon result = mink(pent, tri);
814 EXPECT_GE(result.
size(), 3u);
819 rv.
append(it.get_current_vertex());
822 for (
size_t i = 0; i < rv.
size(); ++i)
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);
837 return it.get_current_vertex();
854 const Segment sp = itp.get_current_segment();
867 bool has_best =
false;
870 for (
size_t i = 0; i < pv.
size(); ++i)
871 for (
size_t j = 0; j < qv.
size(); ++j)
875 const Geom_Number d2 = pv(i).distance_squared_to(proj);
876 if (not has_best or d2 < best_d2)
885 for (
size_t i = 0; i < qv.
size(); ++i)
886 for (
size_t j = 0; j < pv.
size(); ++j)
890 const Geom_Number d2 = qv(i).distance_squared_to(proj);
891 if (not has_best or d2 < best_d2)
922 const auto r = gjk(a, b);
924 EXPECT_FALSE(
r.intersects);
926 EXPECT_EQ(
r.closest_on_first.distance_squared_to(
r.closest_on_second),
928 EXPECT_LE(
r.gjk_iterations, 64u);
948 const auto r = gjk(a, b);
950 EXPECT_TRUE(
r.intersects);
972 const auto r = gjk(a, b);
974 EXPECT_TRUE(
r.intersects);
994 const auto ab = gjk(a, b);
995 const auto ba = gjk(b, a);
997 EXPECT_EQ(ab.distance_squared, ba.distance_squared);
998 EXPECT_EQ(ab.distance, ba.distance);
999 EXPECT_EQ(ab.intersects, ba.intersects);
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);
1036 unsigned int seed = 424242;
1037 for (
int tc = 0; tc < 12; ++tc)
1040 while (a.
size() < 3)
1043 for (
int i = 0; i < 16; ++i)
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;
1054 while (b.
size() < 3)
1057 for (
int i = 0; i < 16; ++i)
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;
1068 const auto r = gjk(a, b);
1072 EXPECT_EQ(
r.distance_squared, brute_d2);
1073 EXPECT_EQ(
r.intersects, brute_d2 == 0);
1089 EXPECT_EQ(kd.
size(), 2u);
1104 ASSERT_TRUE(n.has_value());
1105 EXPECT_EQ(*n,
Point(10, 10));
1108 ASSERT_TRUE(n2.has_value());
1109 EXPECT_EQ(*n2,
Point(50, 50));
1117 EXPECT_FALSE(n.has_value());
1124 for (
int x = 0; x < 10; ++x)
1125 for (
int y = 0;
y < 10; ++
y)
1130 EXPECT_EQ(kd.size(), 100u);
1132 for (
int x = 0; x < 10; ++x)
1133 for (
int y = 0;
y < 10; ++
y)
1134 EXPECT_TRUE(kd.contains(
Point(x,
y)));
1136 auto n = kd.nearest(
Point(5, 5));
1137 ASSERT_TRUE(n.has_value());
1138 EXPECT_EQ(*n,
Point(5, 5));
1156 EXPECT_EQ(
count, 2u);
1169 EXPECT_EQ(visited, 3u);
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));
1189 for (
size_t i = 0; i < snap.partitions.size(); ++i)
1190 if (snap.partitions(i).is_leaf)
1192 EXPECT_EQ(leaves, kd.
size());
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)));
1207 for (
size_t i = 0; i < snap.partitions.size(); ++i)
1209 const auto & part = snap.partitions(i);
1210 EXPECT_TRUE(part.region.contains(part.representative));
1214 EXPECT_EQ(leaves, kd.size());
1266 v.
append(it.get_current_vertex());
1293 ASSERT_GE(
r.triangles.size(), 1u);
1295 for (
size_t t = 0; t <
r.triangles.size(); ++t)
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);
1303 const Point cc = circumcenter_of(a, b, c);
1306 for (
size_t s = 0; s <
r.sites.size(); ++s)
1308 if (s == tri.i or s == tri.j or s == tri.k)
1314 <<
"Site " << s <<
" violates empty-circumcircle for triangle "
1325 for (
int x = 0; x < 5; ++x)
1326 for (
int y = 0;
y < 5; ++
y)
1330 auto r = delaunay(pts);
1332 ASSERT_GE(
r.triangles.size(), 1u);
1334 for (
size_t t = 0; t <
r.triangles.size(); ++t)
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);
1343 for (
size_t s = 0; s <
r.sites.size(); ++s)
1345 if (s == tri.i or s == tri.j or s == tri.k)
1347 EXPECT_GE(
dist2(cc,
r.sites(s)), r2);
1363 ASSERT_GE(dt.triangles.size(), 1u);
1365 for (
size_t t = 0; t < dt.triangles.size(); ++t)
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);
1377 EXPECT_EQ(da, db) <<
"Triangle " << t <<
": circumcenter not equidistant";
1378 EXPECT_EQ(db, dc) <<
"Triangle " << t <<
": circumcenter not equidistant";
1392 auto r = voronoi(dt);
1394 for (
size_t e = 0; e <
r.edges.size(); ++e)
1396 const auto & edge =
r.edges(e);
1403 EXPECT_EQ(d_src_u, d_src_v)
1404 <<
"Edge " << e <<
" src not equidistant to sites";
1408 EXPECT_EQ(d_tgt_u, d_tgt_v)
1409 <<
"Edge " << e <<
" tgt not equidistant to sites";
1432 auto r = delaunay(pts);
1437 for (
size_t i = 0; i <
r.triangles.size(); ++i)
1439 const auto & t =
r.triangles(i);
1445 "case_robust_near_collinear_delaunay", scene,
1446 "Delaunay robustness / near-collinear");
1449 EXPECT_GE(
r.triangles.size(), 1u);
1452 for (
size_t t = 0; t <
r.triangles.size(); ++t)
1454 const auto & tri =
r.triangles(t);
1455 const Point cc = circumcenter_of(
r.sites(tri.i),
r.sites(tri.j),
1458 for (
size_t s = 0; s <
r.sites.size(); ++s)
1460 if (s == tri.i or s == tri.j or s == tri.k)
1462 EXPECT_GE(
dist2(cc,
r.sites(s)), r2);
1490 "case_robust_near_collinear_hull", scene,
1491 "Convex hull robustness / near-collinear");
1494 EXPECT_GE(hull.
size(), 3u);
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)));
1517 auto result = sweep(segs);
1518 EXPECT_EQ(result.size(), 0u);
1532 auto result = sweep(segs);
1535 for (
size_t i = 0; i < segs.
size(); ++i)
1537 for (
size_t i = 0; i < result.size(); ++i)
1540 "case_robust_near_parallel_converging", scene,
1541 "Near-parallel segments / converging intersection");
1543 ASSERT_EQ(result.size(), 1u);
1545 EXPECT_EQ(result(0).point.get_y(),
Geom_Number(0));
1564 auto r = delaunay(pts);
1565 EXPECT_GE(
r.triangles.size(), 1u);
1568 for (
size_t t = 0; t <
r.triangles.size(); ++t)
1570 const auto & tri =
r.triangles(t);
1571 const Point cc = circumcenter_of(
r.sites(tri.i),
r.sites(tri.j),
1574 for (
size_t s = 0; s <
r.sites.size(); ++s)
1576 if (s == tri.i or s == tri.j or s == tri.k)
1578 EXPECT_GE(
dist2(cc,
r.sites(s)), r2);
1596 auto r = delaunay(pts);
1597 EXPECT_GE(
r.triangles.size(), 2u);
1626 auto r = delaunay(pts);
1631 for (
size_t i = 0; i <
r.triangles.size(); ++i)
1633 const auto & t =
r.triangles(i);
1639 "case_robust_cocircular_delaunay", scene,
1640 "Delaunay robustness / cocircular points");
1643 EXPECT_GE(
r.triangles.size(), 6u);
1646 EXPECT_EQ(
r.sites.size(), 8u);
1677 auto r1 = delaunay(pts1);
1678 auto r2 = delaunay(pts2);
1679 auto r3 = delaunay(pts3);
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());
1688 auto ct1 = canonical_triangles(r1);
1689 auto ct2 = canonical_triangles(r2);
1690 auto ct3 = canonical_triangles(r3);
1692 ASSERT_EQ(ct1.size(), ct2.size());
1693 ASSERT_EQ(ct1.size(), ct3.size());
1695 for (
size_t i = 0; i < ct1.size(); ++i)
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);
1730 ASSERT_EQ(v1.size(), v2.size());
1731 for (
size_t i = 0; i < v1.size(); ++i)
1732 EXPECT_EQ(v1(i), v2(i));
1754 EXPECT_EQ(r1.distance_squared, r2.distance_squared);
1756 EXPECT_TRUE(matches_unordered_pair(r1.first, r1.second, r2.first, r2.second));
1766 for (
int x = 0; x < 100; ++x)
1767 for (
int y = 0;
y < 100; ++
y)
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)));
1787 for (
int x = 0; x < 50; ++x)
1788 for (
int y = 0;
y < 100; ++
y)
1802 for (
int x = 0; x < 25; ++x)
1803 for (
int y = 0;
y < 20; ++
y)
1807 auto r = delaunay(pts);
1809 EXPECT_GE(
r.triangles.size(), 1u);
1812 const size_t check_limit =
r.triangles.size() < 50 ?
r.triangles.size() : 50;
1813 for (
size_t t = 0; t < check_limit; ++t)
1815 const auto & tri =
r.triangles(t);
1816 const Point cc = circumcenter_of(
r.sites(tri.i),
r.sites(tri.j),
1819 for (
size_t s = 0; s <
r.sites.size(); ++s)
1821 if (s == tri.i or s == tri.j or s == tri.k)
1823 EXPECT_GE(
dist2(cc,
r.sites(s)), r2);
1836 for (
int x = 0; x <= 50; ++x)
1840 for (
int x = 50; x >= 0; --x)
1845 const size_t nv = p.
size();
1855 EXPECT_EQ(
count, nv - 2);
1878 Polygon h_andrew = andrew(pts);
1879 Polygon h_graham = graham(pts);
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";
1901 for (
size_t i = 0; i < v_andrew.size(); ++i)
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;
1919 for (
int x = 0; x <= 10; ++x)
1920 for (
int y = 0;
y <= 10; ++
y)
1934 Polygon h_andrew = andrew(pts);
1935 Polygon h_graham = graham(pts);
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());
1951 for (
size_t i = 0; i < v_andrew.size(); ++i)
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));
1965 for (
int x = 0; x <= 20; ++x)
1977 Polygon h_andrew = andrew(pts);
1978 Polygon h_graham = graham(pts);
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)));
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)));
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)));
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);
2030 for (
size_t i = 0; i < 3; ++i)
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));
2055 auto r = delaunay(pts);
2057 EXPECT_EQ(
r.sites.size(), 4u);
2058 EXPECT_EQ(
r.triangles.size(), 2u);
2073 auto r = delaunay(pts);
2075 EXPECT_GE(
r.triangles.size(), 4u);
2077 for (
size_t t = 0; t <
r.triangles.size(); ++t)
2079 const auto & tri =
r.triangles(t);
2080 const Point cc = circumcenter_of(
r.sites(tri.i),
r.sites(tri.j),
2083 for (
size_t s = 0; s <
r.sites.size(); ++s)
2085 if (s == tri.i || s == tri.j || s == tri.k)
2087 EXPECT_GE(
dist2(cc,
r.sites(s)), r2)
2088 <<
"Delaunay incremental: site " << s
2089 <<
" violates circumcircle of triangle " << t;
2111 auto rinc = inc(pts);
2113 EXPECT_EQ(rbw.sites.size(), rinc.sites.size());
2114 EXPECT_EQ(rbw.triangles.size(), rinc.triangles.size());
2122 EXPECT_EQ(
r.sites.size(), 3u);
2123 EXPECT_EQ(
r.triangles.size(), 1u);
2136 auto r = delaunay(pts);
2138 EXPECT_EQ(
r.triangles.size(), 0u);
2152 auto r = delaunay(pts);
2153 EXPECT_EQ(
r.sites.size(), 3u);
2154 EXPECT_EQ(
r.triangles.size(), 1u);
2161 for (
int x = 0; x <= 4; ++x)
2162 for (
int y = 0;
y <= 4; ++
y)
2166 auto r = delaunay(pts);
2168 EXPECT_EQ(
r.sites.size(), 25u);
2169 EXPECT_GE(
r.triangles.size(), 32u);
2171 for (
size_t t = 0; t <
r.triangles.size(); ++t)
2173 const auto & tri =
r.triangles(t);
2174 const Point cc = circumcenter_of(
r.sites(tri.i),
r.sites(tri.j),
2177 for (
size_t s = 0; s <
r.sites.size(); ++s)
2179 if (s == tri.i || s == tri.j || s == tri.k)
2181 EXPECT_GE(
dist2(cc,
r.sites(s)), cr2);
2191#if defined(_MSC_VER) && !defined(__clang__)
2192# pragma warning(push)
2193# pragma warning(disable : 4996)
2195# pragma GCC diagnostic push
2196# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2204 EXPECT_EQ(
r.sites.size(), 4u);
2205 EXPECT_GE(
r.vertices.size(), 1u);
2206 EXPECT_GE(
r.edges.size(), 1u);
2220 auto r = voronoi(pts);
2222 for (
size_t e = 0; e <
r.edges.size(); ++e)
2224 const auto & edge =
r.edges(e);
2225 if (edge.unbounded)
continue;
2229 EXPECT_EQ(d_u, d_v) <<
"Voronoi edge src not equidistant for edge " << e;
2251 EXPECT_EQ(cells.size(), 3u);
2252 for (
size_t i = 0; i < cells.size(); ++i)
2253 EXPECT_TRUE(cells(i).polygon.is_closed());
2269 const auto dual = dual_voronoi(pts);
2270 const auto fortune = fortune_voronoi(pts);
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());
2284 std::mt19937 gen(99);
2285 std::uniform_int_distribution<int> coord(0, 100000);
2287 for (
int i = 0; i < 300; ++i)
2293 auto sorted_vertices = [] (
const auto &
r)
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());
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);
2307#if defined(_MSC_VER) && !defined(__clang__)
2308# pragma warning(pop)
2310# pragma GCC diagnostic pop
2325 auto parts = decomp(p);
2327 EXPECT_EQ(parts.size(), 1u);
2328 EXPECT_TRUE(parts(0).is_closed());
2343 auto result = sweep(segs);
2346 EXPECT_GE(result.size(), 1u)
2347 <<
"Collinear overlapping segments were not reported";
2356 const size_t N = 10;
2357 for (
size_t i = 0; i <
N; ++i)
2366 auto result = sweep(segs);
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));
2385 auto result = sweep(segs);
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));
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);
2405 const size_t N = 10000;
2406 for (
size_t i = 0; i <
N; ++i)
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;
2415 auto result = sweep(segs);
2419 const size_t check = std::min(result.size(), (
size_t) 100);
2420 for (
size_t i = 0; i <
check; ++i)
2422 const auto & ix = result(i);
2423 const Segment & s1 = segs(ix.seg_i);
2424 const Segment & s2 = segs(ix.seg_j);
2428 <<
"seg " << ix.seg_i <<
" and seg " << ix.seg_j
2429 <<
" reported as intersecting but don't";
size_t size_t int32_t * out
Andrew's monotonic chain convex hull algorithm.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
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.
bool has_curr() const noexcept
Return true if the iterator has current item.
Iterator on the items of list.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
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
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.
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.
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Geom_Number distance_squared_to(const Point &that) const
Calculates the squared Euclidean distance to another point.
Iterator over the edges (segments) of a polygon.
bool has_curr() const
Check if there is a current segment.
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.
const bool & is_closed() const
Check if the polygon is closed.
const size_t & size() const
Get the number of vertices.
QuickHull convex hull algorithm.
An axis-aligned rectangle.
const Geom_Number & get_xmin() const
Gets the minimum x-coordinate.
const Geom_Number & get_ymax() const
Gets the maximum y-coordinate.
const Geom_Number & get_ymin() const
Gets the minimum y-coordinate.
const Geom_Number & get_xmax() const
Gets the maximum x-coordinate.
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.
bool intersects_with(const Segment &s) const
Checks if this segment intersects another one (including endpoints and collinear overlap).
Point project(const Point &p) const
Orthogonal projection of a point onto this segment's infinite line, clamped to the segment's endpoint...
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 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)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sin_function > > sin(const __gmp_expr< T, U > &expr)
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 ¬e="")
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.
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.
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.
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.
::Array<::Polygon > polygons
::Array<::Point > highlighted_points
::Array<::Segment > segments