188#ifndef GEOM_ALGORITHMS_H
189#define GEOM_ALGORITHMS_H
238 verts.
append(it.get_current_vertex());
250 for (
size_t i = 0; i < verts.
size(); ++i)
252 const Point &a = verts(i);
253 const Point &b = verts((i + 1) % verts.
size());
294 if (verts.
size() < 3)
298 const size_t n = verts.
size();
299 for (
size_t i = 0; i < n; ++i)
305 const int curr = turn > 0 ? 1 : -1;
308 else if (sign != curr)
317 for (
size_t i = 0; i < verts.
size() / 2; ++i)
319 const Point tmp = verts(i);
320 verts(i) = verts(verts.
size() - 1 - i);
321 verts(verts.
size() - 1 - i) = tmp;
356 const size_t tmp = a;
365 template <
typename TriangleArray,
typename GroupFn>
369 edges.
reserve(triangles.size() * 3);
370 for (
size_t tri_idx = 0; tri_idx < triangles.size(); ++tri_idx)
372 const auto &tri = triangles(tri_idx);
388 while (first < edges.
size())
390 size_t last = first + 1;
391 while (last < edges.
size()
and edges(last).u == edges(first).u
and
392 edges(last).v == edges(first).v)
395 on_group(edges, first, last);
437 const size_t tmp = u;
443 if (
const auto *ptr = boundary.
search(e); ptr !=
nullptr)
450 template <
typename IndexedTriangle,
typename InCirclePredicate>
453 InCirclePredicate point_in_circumcircle)
464 for (
size_t i = 1; i < n; ++i)
466 if (pts(i).get_x() < minx)
467 minx = pts(i).get_x();
468 if (pts(i).get_x() > maxx)
469 maxx = pts(i).get_x();
470 if (pts(i).get_y() < miny)
471 miny = pts(i).get_y();
472 if (pts(i).get_y() > maxy)
473 maxy = pts(i).get_y();
487 const Point s0(midx - two_span, midy - span);
488 const Point s1(midx + two_span, midy - span);
489 const Point s2(midx, midy + two_span);
496 const size_t i1 = n + 1;
497 const size_t i2 = n + 2;
502 for (
size_t pidx = 0; pidx < n; ++pidx)
507 for (
size_t t = 0; t < work.
size(); ++t)
509 const auto &[a, b, c, alive] = work(t);
513 if (point_in_circumcircle(pts, a, b, c, pidx))
522 for (
size_t bad_idx = 0; bad_idx < bad.
size(); ++bad_idx)
524 auto &[a, b, c, alive] = work(bad(bad_idx));
545 const size_t tmp = u;
555 for (
size_t t = 0; t < work.
size(); ++t)
557 compacted.
append(work(t));
558 work = std::move(compacted);
562 for (
size_t t = 0; t < work.
size(); ++t)
564 const auto &[a, b, c, alive] = work(t);
568 if (a >= n or b >= n or c >= n)
574 out.append(IndexedTriangle{a, b, c});
660 for (
size_t i = verts.
size(); i > 0; --i)
735 Vertex &curr = it.get_current_vertex();
794 if (
diagonal(p, prev, new_next_next))
800 assert(p.
size() == 3);
906 return {a, b,
dist2(a, b)};
925 for (
size_t i =
l; i <
r; ++i)
926 for (
size_t j = i + 1; j <
r; ++j)
951 const size_t n =
r -
l;
955 const size_t mid =
l + n / 2;
956 const Point &mid_point = px(mid);
963 const size_t left_target = mid -
l;
964 for (
size_t i = 0; i < py.
size(); ++i)
966 const Point &p = py(i);
973 if (left_py.
size() < left_target)
986 for (
size_t i = 0; i < py.
size(); ++i)
988 const Point &p = py(i);
993 for (
size_t i = 0; i < strip.
size(); ++i)
994 for (
size_t j = i + 1; j < strip.
size(); ++j)
996 if (
const Geom_Number dy = strip(j).get_y() - strip(i).get_y();
1020 for (
auto it = point_set.
get_it(); it.has_curr(); it.next_ne())
1021 px.
append(it.get_curr());
1028 for (
size_t i = 1; i < px.
size(); ++i)
1029 if (px(i) == px(i - 1))
1030 return {px(i - 1), px(i), 0};
1046 Result r = (*this)(point_set);
1047 return {
r.first,
r.second};
1130 const Geom_Number d = ax * (by - cy) + bx * (cy - ay) + cx * (ay - by);
1138 if (d_ab >= d_bc && d_ab >= d_ac)
1140 if (d_bc >= d_ab && d_bc >= d_ac)
1150 const Geom_Number ux = (a2 * (by - cy) + b2 * (cy - ay) + c2 * (ay - by)) / den;
1151 const Geom_Number uy = (a2 * (cx - bx) + b2 * (ax - cx) + c2 * (bx - ax)) / den;
1153 const Point center{ux, uy};
1154 return {center, center.distance_squared_to(a)};
1167 for (
auto it = points.
get_it(); it.has_curr(); it.next_ne())
1168 pts.
append(it.get_curr());
1174 std::random_device rd;
1175 std::mt19937 gen(rd());
1176 for (
size_t i = pts.
size() - 1; i > 0; --i)
1178 std::uniform_int_distribution<size_t> dist(0, i);
1179 if (
const size_t j = dist(gen); i != j)
1180 std::swap(pts(i), pts(j));
1191 for (
const auto &p : points)
1201 for (
size_t j = 0; j < n; ++j)
1214 for (
size_t k = 0;
k < n; ++
k)
1225 for (
size_t i = 1; i < pts.
size(); ++i)
1328 const size_t n = verts.
size();
1332 auto edge_area = [&verts, n](
const size_t i,
const size_t k) ->
Geom_Number
1342 while (edge_area(0, (j + 1) % n) > edge_area(0, j)
and safety++ < n)
1347 for (
size_t i = 0; i < n; ++i)
1349 const size_t ni = (i + 1) % n;
1351 while (edge_area(i, (j + 1) % n) > edge_area(i, j)
and safety++ < n)
1384 const size_t n = verts.
size();
1386 return {verts(0), verts(1), verts(0), 0};
1388 auto area_to_edge = [&verts, n](
const size_t i,
const size_t k) ->
Geom_Number
1396 auto edge_len2 = [&verts, n](
const size_t i) ->
Geom_Number
1398 return verts(i).distance_squared_to(verts((i + 1) % n));
1410 while (area_to_edge(0, (j + 1) % n) > area_to_edge(0, j)
and safety++ < n)
1413 for (
size_t i = 0; i < n; ++i)
1415 const size_t ni = (i + 1) % n;
1423 while (area_to_edge(i, (j + 1) % n) > area_to_edge(i, j)
and safety++ < n)
1433 best_width_sq = width_sq;
1439 return {verts(0), verts(1), verts(0), 0};
1441 return {verts(best_i), verts(best_ni), verts(best_j), best_width_sq};
1577 if (verts.
size() < 3)
1585 const bool clip_ccw)
1613 const Geom_Number t = (qpx * sy - qpy * sx) / den;
1615 return {s.
get_x() + t * rx, s.
get_y() + t * ry};
1626 if (
out.get_last() == p)
1629 while (
out.size() >= 2)
1639 static_cast<void>(
out.remove_last());
1649 for (
size_t i = 0; i < pts.
size(); ++i)
1663 for (
size_t i = 0; i < clean.
size(); ++i)
1666 if (ret.
size() >= 3)
1693 const bool clip_ccw = clip_area2 > 0;
1697 for (
size_t i = 0; i < clp.
size(); ++i)
1702 const Point &a = clp(i);
1703 const Point &b = clp((i + 1) % clp.
size());
1712 for (
size_t j = 0; j < input.
size(); ++j)
1714 const Point &e = input(j);
1727 s_inside = e_inside;
1799 return h.dy() > 0 or
h.dy() == 0
and h.dx() >= 0;
1804 return a.
dx() * b.
dy() - a.
dy() * b.
dx();
1809 return a.
dx() * b.
dx() + a.
dy() * b.
dy();
1834 const Geom_Number t = (qpx * sy - qpy * sx) / den;
1847 if (
out.get_last() == p)
1850 while (
out.size() >= 2)
1860 static_cast<void>(
out.remove_last());
1870 for (
size_t i = 0; i < pts.
size(); ++i)
1884 for (
size_t i = 0; i < clean.
size(); ++i)
1887 if (ret.
size() >= 3)
1911 verts.
append(it.get_current_vertex());
1915 const bool ccw = area2 > 0;
1919 for (
size_t i = 0; i < verts.
size(); ++i)
1921 const Point &a = verts(i);
1922 const Point &b = verts((i + 1) % verts.
size());
1936 if (halfplanes.
size() < 3)
1945 return ha
and not hb;
1956 for (
size_t i = 0; i < hps.
size(); ++i)
1967 if (hp.
offset() > last.offset())
1969 static_cast<void>(
unique.remove_last());
1981 for (
size_t i = 0; i <
unique.size(); ++i)
2063 return (*
this)(hps);
2145 for (
size_t i = 2; i < pts.
size(); ++i)
2161 const Point &a = pts(ia);
2162 const Point &b = pts(ib);
2163 const Point &c = pts(ic);
2164 const Point &p = pts(ip);
2185 size_t max_idx = ia;
2190 return ip < max_idx;
2199 for (
auto it = point_set.
get_it(); it.has_curr(); it.next_ne())
2209 for (
size_t i = 0; i <
all.size(); ++i)
2210 if (ret.is_empty() or ret.get_last() !=
all(i))
2227 const size_t n = ret.
sites.size();
2233 ret.
triangles = GeomBowyerWatsonUtils::triangulate<IndexedTriangle>(
2235 [](
const Array<Point> &all_pts,
const size_t ia,
const size_t ib,
const size_t ic,
2253 for (
const Point &p : il)
2255 return (*
this)(points);
2267 for (
size_t tria_idx = 0; tria_idx < result.
triangles.size(); ++tria_idx)
2269 const auto &[i, j,
k] = result.
triangles(tria_idx);
2347 for (
size_t i = 2; i < s.
size(); ++i)
2377 const Point &a = pts(ia);
2378 const Point &b = pts(ib);
2379 const Point &c = pts(ic);
2380 const Point &p = pts(ip);
2389 const Geom_Number ad2 = adx * adx + ady * ady - wts(ia) + wts(ip);
2390 const Geom_Number bd2 = bdx * bdx + bdy * bdy - wts(ib) + wts(ip);
2391 const Geom_Number cd2 = cdx * cdx + cdy * cdy - wts(ic) + wts(ip);
2394 ad2 * (bdx * cdy - bdy * cdx) - bd2 * (adx * cdy - ady * cdx) + cd2 * (adx * bdy - ady * bdx);
2413 size_t max_idx = ia;
2418 return ip < max_idx;
2431 for (
size_t i = 0; i < input.
size(); ++i)
2432 all.append(input(i));
2441 for (
size_t i = 0; i <
all.size(); ++i)
2442 if (ret.is_empty() or ret.get_last().position !=
all(i).position)
2459 const size_t n = ret.
sites.size();
2468 for (
size_t i = 0; i < n; ++i)
2476 ret.
triangles = GeomBowyerWatsonUtils::triangulate<IndexedTriangle>(
2478 [&wts](
const Array<Point> &all_pts,
const size_t ia,
const size_t ib,
const size_t ic,
2512 static constexpr size_t NONE = ~static_cast<size_t>(0);
2530 for (
int i = 0; i < 3; ++i)
2532 return static_cast<size_t>(i);
2537 [[nodiscard]]
static size_t adj_of(
const Tri &t,
const size_t n)
2539 for (
int i = 0; i < 3; ++i)
2541 return static_cast<size_t>(i);
2553 return not (has_cw
and has_ccw);
2601 while (not dag(cur).children.
is_empty())
2604 for (
size_t c = 0; c < dag(cur).children.
size(); ++c)
2606 if (
const size_t child = dag(cur).children(c);
point_in_tri(pts, tris(child), pidx))
2622 for (
const unsigned long nb : tris(ot).adj)
2626 if (
const size_t li =
adj_of(tris(nb), ot); li !=
NONE)
2627 tris(nb).adj[li] = nt;
2647 for (
auto it = point_set.
get_it(); it.has_curr(); it.next_ne())
2655 for (
size_t i = 0; i <
all.size(); ++i)
2660 const size_t n = ret.
sites.size();
2666 bool collinear =
true;
2667 for (
size_t i = 2; i < n
and collinear; ++i)
2678 for (
size_t i = 1; i < n; ++i)
2680 if (pts(i).get_x() < mnx)
2681 mnx = pts(i).get_x();
2682 if (pts(i).get_x() > mxx)
2683 mxx = pts(i).get_x();
2684 if (pts(i).get_y() < mny)
2685 mny = pts(i).get_y();
2686 if (pts(i).get_y() > mxy)
2687 mxy = pts(i).get_y();
2689 Geom_Number delta = mxx - mnx > mxy - mny ? mxx - mnx : mxy - mny;
2693 const Geom_Number cx = (mnx + mxx) / 2, cy = (mny + mxy) / 2;
2701 for (
size_t i = 0; i < n; ++i)
2705 std::random_device rd;
2706 std::mt19937 gen(rd());
2707 for (
size_t i = n - 1; i > 0; --i)
2709 std::uniform_int_distribution<size_t> dis(0, i);
2710 const size_t j = dis(gen);
2711 const size_t tmp = order(i);
2712 order(i) = order(j);
2732 for (
size_t oi = 0; oi < n; ++oi)
2734 const size_t pidx = order(oi);
2735 const size_t ti =
locate(pts, tris, dag, pidx, 0);
2738 const size_t ntris_before = tris.
size();
2741 for (
size_t i = 0; i < ntris_before; ++i)
2752 for (
const size_t ct = frontier.
get();
unsigned long nb : tris(ct).adj)
2754 if (nb ==
NONE or nb >= ntris_before or not tris(nb).alive or is_bad(nb))
2756 if (
in_cc(pts, tris(nb).v[0], tris(nb).v[1], tris(nb).v[2], pidx))
2769 size_t u, v, ext, old_ct;
2772 for (
size_t ci = 0; ci < cavity.
size(); ++ci)
2774 const size_t ct = cavity(ci);
2775 for (
int e = 0; e < 3; ++e)
2777 const size_t nb = tris(ct).adj[e];
2778 if (nb !=
NONE and nb < ntris_before
and is_bad(nb))
2781 boundary.
append(BEdge{tris(ct).v[(e + 1) % 3], tris(ct).v[(e + 2) % 3], nb, ct});
2788 const size_t V = pts.
size();
2792 for (
size_t i = 0; i <
V; ++i)
2801 for (
size_t bi = 0; bi < boundary.
size(); ++bi)
2803 size_t u = boundary(bi).u, v = boundary(bi).v;
2804 const size_t ext = boundary(bi).ext;
2805 const size_t old_ct = boundary(bi).old_ct;
2810 const size_t tmp = u;
2815 const size_t nt = tris.
size();
2825 if (
const size_t li =
adj_of(tris(ext), old_ct); li !=
NONE)
2826 tris(ext).adj[li] = nt;
2834 for (
size_t ni = 0; ni < new_tris.
size(); ++ni)
2836 const size_t nt = new_tris(ni);
2837 const size_t u = tris(nt).v[0];
2838 const size_t v = tris(nt).v[1];
2839 tris(nt).adj[0] = as_u(v);
2840 tris(nt).adj[1] = as_v(u);
2844 for (
size_t ci = 0; ci < cavity.
size(); ++ci)
2846 tris(cavity(ci)).alive =
false;
2847 for (
size_t ni = 0; ni < new_tris.
size(); ++ni)
2848 dag(cavity(ci)).children.
append(new_tris(ni));
2854 for (
size_t t = 0; t < tris.
size(); ++t)
2856 const Tri &tr = tris(t);
2859 if (tr.
v[0] >= n or tr.
v[1] >= n or tr.
v[2] >= n)
2878 for (
const Point &p : il)
2880 return (*
this)(points);
2931 static constexpr size_t NONE = ~static_cast<size_t>(0);
2953 size_t lo = 0, hi = pts.
size();
2967 for (
int i = 0; i < 3; ++i)
2969 return static_cast<size_t>(i);
2974 [[nodiscard]]
static size_t adj_of(
const Tri &t,
const size_t n)
2976 for (
int i = 0; i < 3; ++i)
2978 return static_cast<size_t>(i);
2986 for (
int i = 0; i < 3; ++i)
2988 const size_t e0 = t.
v[(i + 1) % 3];
2989 const size_t e1 = t.
v[(i + 2) % 3];
2990 if ((e0 == a
and e1 == b) or (e0 == b
and e1 == a))
2991 return static_cast<size_t>(i);
3000 for (
size_t i = 0; i < dt_tris.
size(); ++i)
3002 const auto &t = dt_tris(i);
3011 if (last - first != 2)
3014 const auto &e0 = edges(first);
3015 const auto &e1 = edges(first + 1);
3017 const size_t t0 = e0.tri;
3018 const size_t t1 = e1.tri;
3023 tris(t0).adj[loc0] = t1;
3025 tris(t1).adj[loc1] = t0;
3036 for (
size_t t = 0; t < tris.
size(); ++t)
3038 if (not tris(t).alive)
3086 Tri &ta = tris(tri_a);
3087 Tri &tb = tris(tri_b);
3090 const size_t la =
adj_of(ta, tri_b);
3091 const size_t lb =
adj_of(tb, tri_a);
3097 const size_t a_opp = ta.
v[la];
3098 const size_t b_opp = tb.
v[lb];
3099 const size_t p = ta.
v[(la + 1) % 3];
3100 const size_t q = ta.
v[(la + 2) % 3];
3103 const size_t ext_a1 = ta.
adj[(la + 1) % 3];
3104 const size_t ext_a2 = ta.
adj[(la + 2) % 3];
3105 const size_t ext_b1 = tb.
adj[(lb + 1) % 3];
3106 const size_t ext_b2 = tb.
adj[(lb + 2) % 3];
3117 const size_t bp = tb.
v[(lb + 1) % 3];
3118 const size_t bq = tb.
v[(lb + 2) % 3];
3153 const size_t new_a_adj1 = ext_a1;
3154 const bool new_a_con1 = con_a1;
3155 const size_t new_a_adj2 = tri_b;
3156 constexpr bool new_a_con2 =
false;
3160 new_a_adj0 = ext_b1;
3161 new_a_con0 = con_b1;
3165 new_a_adj0 = ext_b2;
3166 new_a_con0 = con_b2;
3176 const size_t new_b_adj0 = ext_a2;
3177 const bool new_b_con0 = con_a2;
3178 const size_t new_b_adj2 = tri_a;
3179 constexpr bool new_b_con2 =
false;
3183 new_b_adj1 = ext_b2;
3184 new_b_con1 = con_b2;
3188 new_b_adj1 = ext_b1;
3189 new_b_con1 = con_b1;
3196 ta.
adj[0] = new_a_adj0;
3197 ta.
adj[1] = new_a_adj1;
3198 ta.
adj[2] = new_a_adj2;
3206 tb.
adj[0] = new_b_adj0;
3207 tb.
adj[1] = new_b_adj1;
3208 tb.
adj[2] = new_b_adj2;
3214 if (new_a_adj0 !=
NONE)
3215 if (
const size_t li =
adj_of(tris(new_a_adj0), tri_b); li !=
NONE)
3216 tris(new_a_adj0).adj[li] = tri_a;
3217 if (new_b_adj0 !=
NONE)
3218 if (
const size_t li =
adj_of(tris(new_b_adj0), tri_a); li !=
NONE)
3219 tris(new_b_adj0).adj[li] = tri_b;
3220 if (new_b_adj1 !=
NONE)
3221 if (
const size_t li =
adj_of(tris(new_b_adj1), tri_a); li !=
NONE)
3222 tris(new_b_adj1).adj[li] = tri_b;
3223 if (new_a_adj1 !=
NONE)
3224 if (
const size_t li =
adj_of(tris(new_a_adj1), tri_b); li !=
NONE)
3225 tris(new_a_adj1).adj[li] = tri_a;
3242 const Segment seg_uv(pts(u), pts(v));
3244 for (
size_t t = 0; t < tris.
size(); ++t)
3246 if (not tris(t).alive)
3249 for (
int e = 0; e < 3; ++e)
3251 const size_t w1 = tris(t).v[(e + 1) % 3];
3252 const size_t w2 = tris(t).v[(e + 2) % 3];
3255 if (w1 == u or w1 == v or w2 == u or w2 == v)
3260 const size_t nb = tris(t).adj[e];
3268 if (nb !=
NONE and tris(nb).alive)
3269 if (
const size_t lb =
adj_of(tris(nb), t); lb !=
NONE)
3281 for (
size_t t = 0; t < tris.
size(); ++t)
3283 if (not tris(t).alive)
3286 if (
const size_t loc = edge_opposite(tris(t), u, v); loc != NONE)
3287 tris(t).constrained[loc] =
true;
3296 if (edge_exists(tris, u, v, et, el))
3298 mark_constrained(tris, u, v);
3303 const size_t max_iterations = tris.
size() * 4 + 100;
3304 for (
size_t iter = 0; iter < max_iterations; ++iter)
3307 if (edge_exists(tris, u, v, et, el))
3309 mark_constrained(tris, u, v);
3317 if (edge_exists(tris, u, v, et, el))
3318 mark_constrained(tris, u, v);
3322 bool flipped_any =
false;
3323 for (
size_t ci = 0; ci < crossings.
size(); ++ci)
3325 const size_t tri_idx = crossings(ci).tri;
3326 const size_t loc_idx = crossings(ci).local;
3328 if (not tris(tri_idx).alive)
3331 if (tris(tri_idx).constrained[loc_idx])
3334 const size_t nb = tris(tri_idx).adj[loc_idx];
3335 if (nb == NONE or not tris(nb).alive)
3339 const size_t a = tris(tri_idx).v[loc_idx];
3340 const size_t lb = adj_of(tris(nb), tri_idx);
3343 const size_t d = tris(nb).v[lb];
3344 const size_t b = tris(tri_idx).v[(loc_idx + 1) % 3];
3345 const size_t c = tris(tri_idx).v[(loc_idx + 2) % 3];
3347 if (is_convex_quad(pts, a, b, c, d))
3349 flip_edge(tris, tri_idx, nb);
3355 if (not flipped_any)
3364 if (edge_exists(tris, u, v, et, el))
3365 mark_constrained(tris, u, v);
3373 bool changed =
true;
3374 const size_t max_passes = tris.
size() * 4 + 100;
3377 while (changed
and pass++ < max_passes)
3381 for (
size_t t = 0; t < tris.
size(); ++t)
3383 if (not tris(t).alive)
3386 for (
int e = 0; e < 3; ++e)
3388 if (tris(t).constrained[e])
3391 const size_t nb = tris(t).adj[e];
3392 if (nb == NONE or nb <= t)
3394 if (not tris(nb).alive)
3399 const size_t lb = adj_of(tris(nb), t);
3403 const size_t d = tris(nb).v[lb];
3407 orientation(pts(tris(t).v[0]), pts(tris(t).v[1]), pts(tris(t).v[2]));
3412 pts(tris(t).v[2]), pts(d));
3414 bool violates =
false;
3424 const size_t a = tris(t).v[e];
3425 const size_t b = tris(t).v[(e + 1) % 3];
3426 const size_t c = tris(t).v[(e + 2) % 3];
3428 if (is_convex_quad(pts, a, b, c, d))
3430 flip_edge(tris, t, nb);
3444 for (
auto it = points.
get_it(); it.has_curr(); it.next_ne())
3449 for (
auto it = constraints.
get_it(); it.has_curr(); it.next_ne())
3451 const Segment &s = it.get_curr();
3458 for (
size_t i = 0; i < con_arr.
size(); ++i)
3459 for (
size_t j = i + 1; j < con_arr.
size(); ++j)
3460 if (con_arr(i).intersects_properly_with(con_arr(j)))
3465 return lexicographic_less(a, b);
3470 for (
size_t i = 0; i <
all.size(); ++i)
3471 if (ret.is_empty() or ret.get_last() !=
all(i))
3483 for (
auto it = constraints.
get_it(); it.has_curr(); it.next_ne())
3485 const Segment &seg = it.get_curr();
3486 const size_t u = find_point_index(sites, seg.
get_src_point());
3487 const size_t v = find_point_index(sites, seg.
get_tgt_point());
3489 if (u == NONE or v == NONE or u == v)
3495 for (
size_t i = 0; i < sites.
size(); ++i)
3497 if (i == u or i == v)
3505 if (
chain.size() > 2)
3507 const Point &pu = sites(u);
3511 (sites(a).get_x() - pu.
get_x()) * (sites(a).get_x() - pu.
get_x()) +
3512 (sites(a).get_y() - pu.
get_y()) * (sites(a).get_y() - pu.
get_y());
3514 (sites(b).get_x() - pu.
get_x()) * (sites(b).get_x() - pu.
get_x()) +
3515 (sites(b).get_y() - pu.
get_y()) * (sites(b).get_y() - pu.
get_y());
3520 for (
size_t i = 0; i + 1 <
chain.size(); ++i)
3526 for (
size_t j = 0; j < result.
size(); ++j)
3527 if (result(j).u == a
and result(j).v == b)
3554 ret.
sites = merge_and_deduplicate(points, constraints);
3555 const size_t n = ret.
sites.size();
3562 bool collinear =
true;
3563 for (
size_t i = 2; i < n
and collinear; ++i)
3572 for (
size_t i = 0; i < n; ++i)
3576 auto [sites, triangles] = dt_algo(site_list);
3578 if (triangles.is_empty())
3587 build_adjacency(tris, triangles);
3593 for (
size_t i = 0; i < indexed_constraints.
size(); ++i)
3594 enforce_constraint(ret.
sites, tris, indexed_constraints(i).u, indexed_constraints(i).v);
3597 lawson_flip(ret.
sites, tris);
3601 for (
size_t t = 0; t < tris.
size(); ++t)
3603 if (not tris(t).alive)
3613 for (
size_t i = 0; i < indexed_constraints.
size(); ++i)
3622 [[nodiscard]]
Result operator () (
const std::initializer_list<Point> points,
3623 const std::initializer_list<Segment> constraints)
const
3626 for (
const Point &p : points)
3630 for (
const Segment &s : constraints)
3633 return (*
this)(pts, segs);
3642 for (
size_t i = 0; i < result.
triangles.size(); ++i)
3644 const auto &[ii, j,
k] = result.
triangles(i);
3742 const Geom_Number d = ax * (by - cy) + bx * (cy - ay) + cx * (ay - by);
3746 const Geom_Number ux = (a2 * (by - cy) + b2 * (cy - ay) + c2 * (ay - by)) / den;
3747 const Geom_Number uy = (a2 * (cx - bx) + b2 * (ax - cx) + c2 * (bx - ax)) / den;
3768 if (verts.
size() < 3)
3796 for (
size_t i = 0; i < sites.
size(); ++i)
3800 cell.site = sites(i);
3801 cell.polygon = polys(i);
3825 for (
size_t idx = 0; idx < dt.
triangles.size(); ++idx)
3834 for (
size_t i = 0; i < dt.
sites.size(); ++i)
3842 if (const size_t cnt = last - first; cnt >= 2)
3844 const Point &p1 = centers(edges(first).tri);
3845 const Point &p2 = centers(edges(first + 1).tri);
3846 ret.edges.append(Edge{edges(first).u, edges(first).v, p1, p2, false, Point()});
3850 const auto &edge = edges(first);
3851 const size_t u = edge.u;
3852 const size_t v = edge.v;
3853 const size_t tri = edge.tri;
3854 const size_t third = edge.third;
3873 const Point &src = centers(tri);
3879 incidence.
reserve(dt.sites.size());
3880 for (
size_t s = 0; s < dt.sites.size(); ++s)
3882 for (
size_t t = 0; t < dt.triangles.size(); ++t)
3884 const auto &[ti, tj, tk] = dt.triangles(t);
3885 incidence(ti).append(t);
3886 incidence(tj).append(t);
3887 incidence(tk).append(t);
3890 ret.cells.reserve(dt.sites.size());
3891 for (
size_t s = 0; s < dt.sites.size(); ++s)
3895 for (
size_t idx = 0; idx < incidence(s).size(); ++idx)
3896 verts.
append(centers(incidence(s)(idx)));
3898 const Point site = dt.sites(s);
3899 if (verts.
size() > 1)
3908 const bool au = (ay > 0) or (ay == 0
and ax >= 0);
3909 const bool bu = (by > 0) or (by == 0
and bx >= 0);
3911 return au
and not bu;
3913 if (
const Geom_Number cr = ax * by - ay * bx; cr != 0)
3916 return (ax * ax + ay * ay) < (bx * bx + by * by);
3922 for (
size_t k = 0;
k < verts.
size(); ++
k)
3929 cell.site_index = s;
3931 cell.bounded = on_hull(s) == 0;
3932 cell.vertices = clean;
3947 return (*
this)(delaunay(point_set));
3956 [[nodiscard]]
Result operator () (
const std::initializer_list<Point> il)
const
3958 return (*
this)(delaunay(il));
3975 const Array<Point> clip_verts = extract_vertices(clip);
3981 const size_t n = sites.
size();
3988 for (
size_t i = 0; i < n; ++i)
3989 site_list.
append(sites(i));
3990 auto dt = dt_algo(site_list);
3995 dt_to_input.
reserve(dt.sites.size());
3996 for (
size_t di = 0; di < dt.sites.size(); ++di)
3999 for (
size_t oi = 0; oi < n; ++oi)
4000 if (dt.sites(di) == sites(oi))
4005 dt_to_input.
append(match);
4010 for (
size_t i = 0; i < n; ++i)
4012 for (
size_t t = 0; t < dt.triangles.size(); ++t)
4014 const size_t a = dt_to_input(dt.triangles(t).i);
4015 const size_t b = dt_to_input(dt.triangles(t).j);
4016 const size_t c = dt_to_input(dt.triangles(t).k);
4028 for (
size_t i = 0; i < n; ++i)
4035 if (sites(j) != sites(i))
4036 hps.
append(bisector_halfplane_for_site(sites(i), sites(j)));
4056 return clipped_cells(vor.
sites, clip);
4068 return clipped_cells(delaunay(point_set).sites, clip);
4081 return clipped_cells(delaunay(il).sites, clip);
4093 return indexed_clipped_cells(sites, clipped_cells(sites, clip));
4105 return clipped_cells_indexed(vor.
sites, clip);
4118 return clipped_cells_indexed(delaunay(point_set).sites, clip);
4131 return clipped_cells_indexed(delaunay(il).sites, clip);
4205 for (
const Point &p : il)
4207 return (*
this)(pts);
4218 auto [sites, triangles] =
delaunay_(pts);
4238class [[deprecated(
"VoronoiDiagramFortune goes in 8.0.0: use VoronoiDiagram, which it calls")]]
4257 return voronoi_(pts);
4265 [[nodiscard]]
Result operator () (
const std::initializer_list<Point> il)
const
4267 return voronoi_(il);
4286 for (
const Point &p : il)
4288 return clipped_cells(pts, clip);
4344 for (
auto it = point_set.
get_it(); it.has_curr(); it.next_ne())
4345 points.
append(it.get_curr());
4347 if (points.
size() <= 1)
4354 unique.append(points(0));
4355 for (
size_t i = 1; i < points.
size(); ++i)
4357 unique.append(points(i));
4381 const size_t n = points.
size();
4394 for (
size_t i = 0; i < n; ++i)
4396 while (lower.
size() >= 2 &&
4397 turn(lower(lower.
size() - 2), lower(lower.
size() - 1), points(i)) <= 0)
4404 for (
size_t i = n; i > 0; --i)
4406 const Point &p = points(i - 1);
4407 while (upper.
size() >= 2 &&
turn(upper(upper.
size() - 2), upper(upper.
size() - 1), p) <= 0)
4416 for (
size_t i = 0; i < lower.
size(); ++i)
4419 for (
size_t i = 0; i < upper.
size(); ++i)
4422 if (ret.
size() >= 3)
4478 for (
auto it = point_set.
get_it(); it.has_curr(); it.next_ne())
4479 points.
append(it.get_curr());
4481 if (points.
size() <= 1)
4488 unique.append(points(0));
4489 for (
size_t i = 1; i < points.
size(); ++i)
4491 unique.append(points(i));
4515 const size_t n = points.
size();
4527 size_t pivot_idx = 0;
4528 for (
size_t i = 1; i < n; ++i)
4530 if (points(i).get_y() < points(pivot_idx).get_y())
4536 if (points(i).get_y() == points(pivot_idx).get_y() &&
4537 points(i).get_x() < points(pivot_idx).get_x())
4541 const Point pivot = points(pivot_idx);
4544 const Point tmp = points(0);
4545 points(0) = points(pivot_idx);
4546 points(pivot_idx) = tmp;
4551 for (
size_t i = 1; i < n; ++i)
4569 for (
size_t i = 0; i < polar.
size();)
4574 filtered.append(polar(j));
4578 if (filtered.size() == 1)
4587 hull.
reserve(filtered.size() + 1);
4589 hull.
append(filtered(0));
4591 for (
size_t i = 1; i < filtered.size(); ++i)
4593 const Point &p = filtered(i);
4594 while (hull.
size() >= 2 &&
turn(hull(hull.
size() - 2), hull(hull.
size() - 1), p) <= 0)
4599 for (
size_t i = 0; i < hull.
size(); ++i)
4602 if (ret.
size() >= 3)
4673 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
4690 for (
auto i = point_set.
get_it(); i.has_curr(); i.next_ne())
4692 const Point &p_i = i.get_curr();
4694 for (
auto j = point_set.
get_it(); j.has_curr(); j.next_ne())
4696 const Point &p_j = j.get_curr();
4723 auto check_it = point_set.
get_it();
4724 const Point first = check_it.get_curr();
4725 bool has_distinct =
false;
4727 for (; check_it.has_curr(); check_it.next_ne())
4728 if (check_it.get_curr() != first)
4730 has_distinct =
true;
4734 if (not has_distinct)
4756 <<
"BruteForceConvexHull: broken chain (degenerate input?)";
4766 if (ret.
size() >= 3)
4808 auto it = point_set.
get_it();
4809 const Point *ret = &it.get_curr();
4812 for (; it.has_curr(); it.next_ne())
4814 const Point &p = it.get_curr();
4841 const Point *current = start;
4848 for (
auto it = point_set.
get_it(); it.has_curr(); it.next_ne())
4850 const Point &candidate = it.get_curr();
4852 if (candidate == *current)
4855 if (
next ==
nullptr)
4864 current->distance_squared_to(candidate) > current->distance_squared_to(*
next))
4868 if (
next ==
nullptr)
4872 }
while (current != start);
4874 if (ret.
size() >= 3)
4936 for (
auto it = point_set.
get_it(); it.has_curr(); it.next_ne())
4938 const Point &p = it.get_curr();
4943 if (area > max_area)
4990 ret.first.append(p);
4995 ret.second.append(p);
5040 auto it = point_set.
get_it();
5041 Point leftmost = it.get_curr();
5042 Point rightmost = it.get_curr();
5045 for (; it.has_curr(); it.next_ne())
5047 const Point &p = it.get_curr();
5056 return std::make_pair(leftmost, rightmost);
5075 for (
auto it = point_set.
get_it(); it.has_curr(); it.next_ne())
5079 ret.second.append(p);
5099 if (leftmost == rightmost)
5105 auto [right, left_or_collinear] =
partition(point_set, leftmost, rightmost);
5111 convex_set.
append(leftmost);
5113 convex_set.
append(rightmost);
5116 for (
auto it = convex_set.
get_it(); it.has_curr(); it.next_ne())
5119 if (ret.
size() >= 3)
5245 if (
out.overlap.get_src_point() ==
out.overlap.get_tgt_point())
5248 out.point =
out.overlap.get_src_point();
5311template <
typename Event,
typename CmpEvent>
5359 return not
queue_.is_empty();
5373 return std::move(se.
event);
5377 [[nodiscard]]
const Event &
peek()
const
5379 return queue_.min().event;
5401 template <
typename Handler>
5414 template <
typename Handler>
5484 return (
y1 + y2) / 2;
5486 return y1 + (x - x1) * (y2 -
y1) / (x2 - x1);
5550 return static_cast<int>(a.
type) <
static_cast<int>(b.
type);
5569 if (adx == 0
and bdx == 0)
5643 if (
root ==
nullptr)
5648 Node *left =
nullptr;
5649 Node *right =
nullptr;
5652 node->
right = right;
5665 if (
root ==
nullptr)
5672 if (
root->label < label)
5686 if (
root ==
nullptr)
5689 if (label < root->label)
5695 if (
root->label < label)
5716 size_t pred = SIZE_MAX;
5718 while (cur !=
nullptr)
5731 size_t succ = SIZE_MAX;
5733 while (cur !=
nullptr)
5746 if (
pred != SIZE_MAX
and succ != SIZE_MAX)
5748 if (
pred != SIZE_MAX)
5750 if (succ != SIZE_MAX)
5759 for (
size_t i = 0; i < segs.
size(); ++i)
5770 return nodes_(seg) !=
nullptr;
5781 static_cast<unsigned>(
rng_()),
nullptr,
nullptr};
5791 Node *removed =
nullptr;
5805 while (cur !=
nullptr)
5806 if (cur->
label < label)
5814 return pred ==
nullptr ? SIZE_MAX :
pred->seg;
5823 const Node *succ =
nullptr;
5825 while (cur !=
nullptr)
5826 if (label < cur->label)
5834 return succ ==
nullptr ? SIZE_MAX : succ->
seg;
5862 const size_t lo = i < j ? i : j;
5863 const size_t hi = i < j ? j : i;
5864 const size_t key = lo * n + hi;
5865 if (seen_pairs.
search(key) !=
nullptr)
5871 if (not hit.intersects())
5877 const Point overlap_start = hit.overlap.get_src_point();
5878 const Point overlap_end = hit.overlap.get_tgt_point();
5879 if (not (overlap_start.
get_x() < sx))
5881 if (not (overlap_end.
get_x() < sx))
5886 const Point ip = hit.point;
5887 if (ip.
get_x() < sx)
5905 const size_t n = segments.
size();
5914 for (
size_t i = 0; i < n; ++i)
5917 <<
"Segment " << i <<
" is degenerate (zero length)";
5923 for (
size_t i = 0; i < n; ++i)
5933 auto pair_key = [n](
const size_t i,
const size_t j)
5935 const size_t lo = i < j ? i : j;
5936 const size_t hi = i < j ? j : i;
5940 auto append_intersection =
5941 [&result, &reported_pairs, &pair_key](
const size_t i,
const size_t j,
const Point &p)
5943 const size_t key = pair_key(i, j);
5944 if (reported_pairs.
search(key) !=
nullptr)
5946 reported_pairs.
insert(key);
5948 const size_t lo = i < j ? i : j;
5949 const size_t hi = i < j ? j : i;
5956 while (
eq.has_events())
5958 const Event ev =
eq.dequeue();
5963 const size_t idx = ev.
seg_a;
5968 if (
const size_t succ = status.
successor(idx); succ != SIZE_MAX)
5976 const size_t succ = status.
successor(idx);
5977 if (
pred != SIZE_MAX
and succ != SIZE_MAX)
5984 const size_t a = ev.
seg_a;
5985 const size_t b = ev.
seg_b;
5986 append_intersection(a, b, ev.
pt);
5995 size_t seed = SIZE_MAX;
6001 if (
seed != SIZE_MAX)
6016 if (block.size() > 2)
6018 for (
size_t i = 0; i + 1 < block.size(); ++i)
6019 for (
size_t j = i + 1; j < block.size(); ++j)
6020 append_intersection(block(i), block(j), ev.
pt);
6023 if (block.size() == 0)
6027 if (status.
contains(b)
and (block.size() == 0 or block(0) != b))
6031 for (
size_t i = 0; i < block.size(); ++i)
6032 status.
erase(block(i));
6033 for (
size_t i = 0; i < block.size(); ++i)
6034 status.
insert(block(i), sx);
6036 for (
size_t i = 0; i < block.size(); ++i)
6038 const size_t s = block(i);
6041 if (
const size_t succ = status.
successor(s); succ != SIZE_MAX)
6142 return is_below(verts((edge + 1) % verts.
size()), verts(edge));
6149 const Point &a = verts(edge);
6150 const Point &b = verts((edge + 1) % verts.
size());
6190 if (
root ==
nullptr)
6197 if (
root->label < label)
6226 if (
root ==
nullptr)
6231 Node *left =
nullptr;
6232 Node *right =
nullptr;
6235 node->
right = right;
6248 if (
root ==
nullptr)
6251 if (label < root->label)
6257 if (
root->label < label)
6278 size_t pred = SIZE_MAX;
6280 while (cur !=
nullptr)
6293 size_t succ = SIZE_MAX;
6295 while (cur !=
nullptr)
6308 if (
pred != SIZE_MAX
and succ != SIZE_MAX)
6310 if (
pred != SIZE_MAX)
6312 if (succ != SIZE_MAX)
6321 for (
size_t i = 0; i < verts.
size(); ++i)
6332 return nodes_(edge) !=
nullptr;
6343 static_cast<unsigned>(
rng_()),
nullptr,
nullptr};
6353 Node *removed =
nullptr;
6361 size_t best = SIZE_MAX;
6363 while (cur !=
nullptr)
6382 const size_t n = verts.
size();
6394 for (
size_t i = 0; i < n; ++i)
6397 quicksort_op(sorted, [&verts](
const size_t a,
const size_t b)
6399 if (verts(a).get_y() != verts(b).get_y())
6400 return verts(a).get_y() > verts(b).get_y();
6401 return verts(a).get_x() < verts(b).get_x();
6407 const size_t top = sorted(0);
6408 const size_t bot = sorted(n - 1);
6412 for (
size_t i = 0; i < n; ++i)
6416 for (
size_t i = top; i != bot; i = (i + 1) % n)
6418 on_left(top) =
true;
6422 stack.
push(sorted(0));
6423 stack.
push(sorted(1));
6425 for (
size_t i = 2; i < n - 1; ++i)
6427 const size_t curr = sorted(i);
6428 const size_t stk_top = stack.
top();
6429 if (on_left(curr) != on_left(stk_top))
6432 while (stack.
size() > 1)
6434 const size_t a = stack.
top();
6435 const size_t b = stack.
top(1);
6440 stack.
push(sorted(i - 1));
6446 size_t last_popped = stack.
top();
6451 const size_t peek = stack.
top();
6457 if ((on_left(curr)
and area >= 0) or (not on_left(curr)
and area <= 0))
6460 result.
append(
Triangle(verts(curr), verts(last_popped), verts(peek)));
6465 stack.
push(last_popped);
6471 const size_t last = sorted(n - 1);
6472 while (stack.
size() > 1)
6474 const size_t a = stack.
top();
6475 const size_t b = stack.
top(1);
6485 const size_t n = v.
size();
6486 const Point &prev = v((i + n - 1) % n);
6487 const Point &curr = v(i);
6490 const bool prev_below =
is_below(prev, curr);
6492 const bool prev_above =
is_above(prev, curr);
6496 if (prev_below
and next_below)
6498 if (prev_above
and next_above)
6510 const DynSetTree<std::pair<size_t, size_t>> &diagonals)
6512 const size_t n = verts.
size();
6515 for (
size_t i = 0; i < n; ++i)
6518 auto add_undirected_edge = [&adj](
const size_t a,
const size_t b)
6524 for (
size_t i = 0; i < n; ++i)
6525 add_undirected_edge(i, (i + 1) % n);
6527 for (
const auto &d : diagonals)
6528 add_undirected_edge(d.first, d.second);
6530 for (
size_t v = 0; v < n; ++v)
6532 auto &nbrs = adj(v);
6535 in_place_sort(nbrs, [&verts, v](
const size_t a,
const size_t b)
6537 const Geom_Number dax = verts(a).get_x() - verts(v).get_x();
6538 const Geom_Number day = verts(a).get_y() - verts(v).get_y();
6539 const Geom_Number dbx = verts(b).get_x() - verts(v).get_x();
6540 const Geom_Number dby = verts(b).get_y() - verts(v).get_y();
6542 const bool upper_a = day > 0 or (day == 0
and dax >= 0);
6543 const bool upper_b = dby > 0 or (dby == 0
and dbx >= 0);
6544 if (upper_a != upper_b)
6545 return upper_a > upper_b;
6547 if (
const Geom_Number cross = dax * dby - day * dbx; cross != 0)
6556 using HalfEdge = std::pair<size_t, size_t>;
6557 std::map<HalfEdge, HalfEdge> next_half;
6558 for (
size_t v = 0; v < n; ++v)
6560 const auto &nbrs = adj(v);
6561 if (nbrs.is_empty())
6563 for (
size_t k = 0;
k < nbrs.size(); ++
k)
6565 const size_t u = nbrs(
k);
6566 const size_t pred = nbrs((
k + nbrs.size() - 1) % nbrs.size());
6567 next_half[HalfEdge{u, v}] = HalfEdge{v,
pred};
6574 for (
size_t i = 0; i < face.size(); ++i)
6576 const Point &a = verts(face(i));
6577 const Point &b = verts(face((i + 1) % face.size()));
6585 for (
const auto &[kv, _] : next_half)
6587 const HalfEdge start = kv;
6588 if (visited.
search(start) !=
nullptr)
6591 HalfEdge cur = start;
6593 while (visited.
insert(cur) !=
nullptr)
6596 auto it_next = next_half.find(cur);
6597 if (it_next == next_half.end())
6602 cur = it_next->second;
6607 if (face.
is_empty() or cur != start or face.
size() < 3)
6609 if (face_area2(face) <= 0)
6614 for (
const size_t idx : face)
6624 const size_t n = verts.
size();
6634 for (
size_t i = 0; i < n; ++i)
6642 for (
size_t i = 0; i < n; ++i)
6647 for (
size_t i = 0; i < n; ++i)
6649 quicksort_op(order, [&verts](
const size_t a,
const size_t b)
6651 if (verts(a).get_y() != verts(b).get_y())
6652 return verts(a).get_y() > verts(b).get_y();
6653 return verts(a).get_x() < verts(b).get_x();
6658 for (
size_t i = 0; i < n; ++i)
6659 helper.append(SIZE_MAX);
6664 auto add_diagonal = [&](
const size_t a,
const size_t b)
6666 if (a == b or b == SIZE_MAX)
6668 if ((a + 1) % n == b or (b + 1) % n == a)
6671 const size_t lo = a < b ? a : b;
6672 const size_t hi = a < b ? b : a;
6673 diagonals.
insert({lo, hi});
6676 auto helper_is_merge = [&helper, &vtype](
const size_t edge)
6681 for (
size_t oi = 0; oi < n; ++oi)
6683 const size_t vi = order(oi);
6684 const size_t prev = (vi + n - 1) % n;
6685 const size_t e_prev = prev;
6686 const size_t e_curr = vi;
6694 status.
insert(e_curr, sweep_y);
6695 helper(e_curr) = vi;
6702 if (helper_is_merge(e_prev))
6703 add_diagonal(vi, helper(e_prev));
6704 status.
erase(e_prev);
6710 if (
const size_t left = status.
left_edge_of_point(verts(vi), sweep_y); left != SIZE_MAX)
6712 add_diagonal(vi, helper(left));
6718 status.
insert(e_curr, sweep_y);
6719 helper(e_curr) = vi;
6727 if (helper_is_merge(e_prev))
6728 add_diagonal(vi, helper(e_prev));
6729 status.
erase(e_prev);
6731 if (
const size_t left = status.
left_edge_of_point(verts(vi), sweep_y); left != SIZE_MAX)
6733 if (helper_is_merge(left))
6734 add_diagonal(vi, helper(left));
6744 if (helper_is_merge(e_prev))
6745 add_diagonal(vi, helper(e_prev));
6746 status.
erase(e_prev);
6750 status.
insert(e_curr, sweep_y);
6751 helper(e_curr) = vi;
6757 if (helper_is_merge(left))
6758 add_diagonal(vi, helper(left));
6766 if (faces.
size() == 0)
6770 for (
size_t i = 0; i < n; ++i)
6811 bool produced_any =
false;
6813 for (
size_t fi = 0; fi < faces.
size(); ++fi)
6815 if (faces(fi).
size() < 3)
6820 for (
size_t i = 0; i < faces(fi).
size(); ++i)
6821 mono.
append(verts(faces(fi)(i)));
6834 for (
size_t i = 0; i < mono.
size(); ++i)
6836 if (piece.
size() >= 3)
6844 for (
auto it = tris.
get_it(); it.has_curr(); it.next_ne())
6846 result.
append(it.get_curr());
6847 produced_any =
true;
6863 const size_t n = v.
size();
6870 for (
size_t i = 1; i < n; ++i)
6872 if (v(i).get_y() > v(top_idx).get_y() or
6873 (v(i).get_y() == v(top_idx).get_y()
and v(i).get_x() < v(top_idx).get_x()))
6876 if (v(i).get_y() < v(bot_idx).get_y() or
6877 (v(i).get_y() == v(bot_idx).get_y()
and v(i).get_x() > v(bot_idx).get_x()))
6883 for (
size_t i = top_idx;;)
6885 const size_t next = (i + 1) % n;
6886 if (
next == bot_idx)
6888 if (v(
next).get_y() > v(i).get_y())
6895 for (
size_t i = top_idx;;)
6897 const size_t prev = (i + n - 1) % n;
6898 if (prev == bot_idx)
6900 if (v(prev).get_y() > v(i).get_y())
6960 if (verts.
size() < 3)
6972 for (
size_t i = 1; i < v.
size(); ++i)
6973 if (v(i).get_y() < v(bot).get_y() or
6974 (v(i).get_y() == v(bot).get_y()
and v(i).get_x() < v(bot).get_x()))
6979 for (
size_t i = 0; i < v.
size(); ++i)
6987 const size_t j = (i + 1) % v.
size();
6988 return {v(j).get_x() - v(i).get_x(), v(j).get_y() - v(i).get_y()};
7023 const size_t np = pv.
size();
7024 const size_t nq = qv.
size();
7032 while (ip < np or iq < nq)
7034 result.
append(
Point(pv(ip % np).get_x() + qv(iq % nq).get_x(),
7035 pv(ip % np).get_y() + qv(iq % nq).get_y()));
7065 for (
size_t i = 0; i < result.
size(); ++i)
7074 for (
size_t i = 0; i < clean.
size(); ++i)
7077 if (ret.
size() >= 3)
7144 static constexpr double kEps = 1e-12;
7151 [[nodiscard]]
static double dot2(
const double ax,
7154 const double by)
noexcept
7156 return ax * bx + ay * by;
7159 [[nodiscard]]
static double norm2(
const double x,
const double y)
noexcept
7182 for (
size_t i = 0; i < v.
size(); ++i)
7198 double best_p = -std::numeric_limits<double>::infinity();
7199 double best_q = -std::numeric_limits<double>::infinity();
7201 for (
size_t i = 0; i < p.
size(); ++i)
7212 for (
size_t i = 0; i < q.
size(); ++i)
7222 const Point &pa = p(ip);
7223 const Point &pb = q(iq);
7233 const Segment sp = itp.get_current_segment();
7242 vp = it.get_current_vertex().to_point();
7249 vq = it.get_current_vertex().to_point();
7262 if (simplex.size() == 1)
7265 out.cx = simplex[0].vx;
7266 out.cy = simplex[0].vy;
7267 out.witness_a = simplex[0].a;
7268 out.witness_b = simplex[0].b;
7273 if (simplex.size() == 2)
7277 const double abx = b.
vx - a.
vx;
7278 const double aby = b.
vy - a.
vy;
7279 const double ab2 =
norm2(abx, aby);
7283 out.reduced_simplex = {a};
7286 out.witness_a = a.
a;
7287 out.witness_b = a.
b;
7292 double t = -
dot2(a.
vx, a.
vy, abx, aby) / ab2;
7300 out.reduced_simplex = {a};
7303 out.witness_a = a.
a;
7304 out.witness_b = a.
b;
7306 else if (t >= 1.0 -
kEps)
7308 out.reduced_simplex = {b};
7311 out.witness_a = b.
a;
7312 out.witness_b = b.
b;
7316 out.reduced_simplex = {a, b};
7317 out.cx = a.
vx + t * abx;
7318 out.cy = a.
vy + t * aby;
7332 const double abx = b.
vx - a.
vx;
7333 const double aby = b.
vy - a.
vy;
7334 const double acx = c.
vx - a.
vx;
7335 const double acy = c.
vy - a.
vy;
7336 const double apx = -a.
vx;
7337 const double apy = -a.
vy;
7339 const double d1 =
dot2(abx, aby, apx, apy);
7340 const double d2 =
dot2(acx, acy, apx, apy);
7341 if (d1 <= 0.0
and d2 <= 0.0)
7343 out.reduced_simplex = {a};
7346 out.witness_a = a.
a;
7347 out.witness_b = a.
b;
7352 const double bpx = -b.
vx;
7353 const double bpy = -b.
vy;
7354 const double d3 =
dot2(abx, aby, bpx, bpy);
7355 const double d4 =
dot2(acx, acy, bpx, bpy);
7356 if (d3 >= 0.0
and d4 <= d3)
7358 out.reduced_simplex = {b};
7361 out.witness_a = b.
a;
7362 out.witness_b = b.
b;
7367 const double vc = d1 * d4 - d3 * d2;
7368 if (vc <= 0.0 and d1 >= 0.0
and d3 <= 0.0)
7370 const double t = d1 / (d1 - d3);
7371 out.reduced_simplex = {a, b};
7372 out.cx = a.
vx + t * abx;
7373 out.cy = a.
vy + t * aby;
7380 const double cpx = -c.
vx;
7381 const double cpy = -c.
vy;
7382 const double d5 =
dot2(abx, aby, cpx, cpy);
7383 const double d6 =
dot2(acx, acy, cpx, cpy);
7384 if (d6 >= 0.0
and d5 <= d6)
7386 out.reduced_simplex = {c};
7389 out.witness_a = c.
a;
7390 out.witness_b = c.
b;
7395 const double vb = d5 * d2 - d1 * d6;
7396 if (vb <= 0.0 and d2 >= 0.0
and d6 <= 0.0)
7398 const double t = d2 / (d2 - d6);
7399 out.reduced_simplex = {a, c};
7400 out.cx = a.
vx + t * acx;
7401 out.cy = a.
vy + t * acy;
7408 const double va = d3 * d6 - d5 * d4;
7409 if (va <= 0.0 and d4 - d3 >= 0.0
and d5 - d6 >= 0.0)
7411 const double t = (d4 - d3) / (d4 - d3 + (d5 - d6));
7412 const double bcx = c.
vx - b.
vx;
7413 const double bcy = c.
vy - b.
vy;
7414 out.reduced_simplex = {b, c};
7415 out.cx = b.
vx + t * bcx;
7416 out.cy = b.
vy + t * bcy;
7424 const double denom = va + vb + vc;
7425 if (std::fabs(denom) <=
kEps)
7427 out.reduced_simplex = {a, b, c};
7430 out.witness_a = a.
a;
7431 out.witness_b = a.
b;
7432 out.contains_origin =
true;
7436 const double inv = 1.0 / denom;
7437 const double wa = va * inv;
7438 const double wb = vb * inv;
7439 const double wc = vc * inv;
7440 out.reduced_simplex = {a, b, c};
7449 out.contains_origin =
true;
7463 out.distance_squared = 0;
7465 out.closest_on_first = pv(0);
7466 out.closest_on_second = pv(0);
7467 out.intersects =
true;
7471 bool has_best =
false;
7473 Point best_a, best_b;
7475 for (
size_t i = 0; i < pv.
size(); ++i)
7477 const Point &pnt = pv(i);
7478 for (
size_t j = 0; j < qv.
size(); ++j)
7480 const Point &q0 = qv(j);
7481 const Point &q1 = qv((j + 1) % qv.
size());
7494 for (
size_t i = 0; i < qv.
size(); ++i)
7496 const Point &pnt = qv(i);
7497 for (
size_t j = 0; j < pv.
size(); ++j)
7499 const Point &p0 = pv(j);
7500 const Point &p1 = pv((j + 1) % pv.
size());
7513 ah_domain_error_if(not has_best) <<
"Could not compute distance between convex polygons";
7515 out.distance_squared = best_d2;
7517 out.closest_on_first = best_a;
7518 out.closest_on_second = best_b;
7519 out.intersects = (best_d2 == 0);
7547 r.distance_squared = 0;
7549 r.closest_on_first = pv(0);
7550 r.closest_on_second = pv(0);
7551 r.intersects =
true;
7565 std::vector<SupportPoint> simplex;
7567 simplex.push_back(
support(pv, qv, dirx, diry));
7568 dirx = -simplex[0].vx;
7569 diry = -simplex[0].vy;
7571 double prev_d2 = std::numeric_limits<double>::infinity();
7579 simplex.push_back(
support(pv, qv, dirx, diry));
7580 if (simplex.size() > 3)
7581 simplex.erase(simplex.begin());
7586 const double d2 =
norm2(step.
cx, step.
cy);
7590 r.distance_squared = 0;
7594 r.intersects =
true;
7595 r.gjk_iterations = iters + 1;
7599 if (prev_d2 - d2 <=
kEps * std::max(1.0, prev_d2))
7667 for (
size_t i = 0; i < points.
size(); ++i)
7668 if (uniq.is_empty() or uniq.get_last() != points(i))
7669 uniq.append(points(i));
7679 for (
size_t i = 0; i <
points_.size(); ++i)
7682 if (p.
get_x() < xmin)
7684 if (p.
get_x() > xmax)
7686 if (p.
get_y() < ymin)
7688 if (p.
get_y() > ymax)
7691 return {xmin, ymin, xmax, ymax};
7730 :
tree(xmin, ymin, xmax, ymax),
bounds_(xmin, ymin, xmax, ymax)
7787 [[nodiscard]]
size_t size() const noexcept
7799 template <
typename Op>
7824 const size_t depth,
const bool split_on_x) ->
void
7843 size_t mid = sorted.
size() / 2;
7846 if (mid >= sorted.
size())
7847 mid = sorted.
size() - 1;
7849 const Point pivot = sorted(mid);
7859 if (sorted.
size() == 1)
7866 for (
size_t i = 0; i < mid; ++i)
7867 left_pts.
append(sorted(i));
7868 for (
size_t i = mid; i < sorted.
size(); ++i)
7869 right_pts.
append(sorted(i));
7873 Rectangle left(region.get_xmin(), region.get_ymin(), pivot.
get_x(), region.get_ymax());
7874 Rectangle right(pivot.
get_x(), region.get_ymin(), region.get_xmax(), region.get_ymax());
7875 self(self, left_pts, left, depth + 1,
false);
7876 self(self, right_pts, right, depth + 1,
false);
7880 Rectangle bottom(region.get_xmin(), region.get_ymin(), region.get_xmax(), pivot.
get_y());
7881 Rectangle top(region.get_xmin(), pivot.
get_y(), region.get_xmax(), region.get_ymax());
7882 self(self, left_pts, bottom, depth + 1,
true);
7883 self(self, right_pts, top, depth + 1,
true);
7930 static constexpr size_t NONE = ~static_cast<size_t>(0);
7934 for (
size_t i = 0; i < face.
size(); ++i)
7944 const size_t tmp = u;
7948 return v == u + 1 or (u == 0
and v == n - 1);
7958 const size_t n1 = f1.
size();
7959 const size_t n2 = f2.
size();
7965 if ((pu1 + 1) % n1 != pv1)
7967 const size_t tmp = u;
7974 const size_t prev_u = f1((pu1 + n1 - 1) % n1);
7975 const size_t next_v = f1((pv1 + 1) % n1);
7977 const size_t pu2 =
find_pos(f2, u);
7978 const size_t pv2 =
find_pos(f2, v);
7980 const size_t next_u = f2((pu2 + 1) % n2);
7981 const size_t prev_v = f2((pv2 + n2 - 1) % n2);
7997 const size_t n1 = f1.
size();
7998 const size_t n2 = f2.
size();
8003 if ((pu1 + 1) % n1 != pv1)
8005 const size_t tmp = u;
8011 const size_t pu2 =
find_pos(f2, u);
8017 for (
size_t k = 0;
k < n1 - 1; ++
k)
8018 merged.
append(f1((pv1 + 1 +
k) % n1));
8021 for (
size_t k = 0;
k < n2 - 1; ++
k)
8022 merged.
append(f2((pu2 + 1 +
k) % n2));
8045 const size_t n = pts.
size();
8060 for (
auto it = tri_list.
get_it(); it.has_curr(); it.next_ne())
8064 for (
size_t i = 0; i < n; ++i)
8078 const size_t tmp = i1;
8087 faces.
append(std::move(face));
8091 bool changed =
true;
8095 for (
size_t fi = 0; fi < faces.
size()
and not changed; ++fi)
8097 const auto f1 = faces(fi);
8098 for (
size_t k = 0;
k < f1.size()
and not changed; ++
k)
8100 const size_t u = f1(
k);
8101 const size_t v = f1((
k + 1) % f1.size());
8106 for (
size_t fj = fi + 1; fj < faces.
size()
and not changed; ++fj)
8108 const auto f2 = faces(fj);
8119 for (
size_t i = 0; i < faces.
size(); ++i)
8122 new_faces.
append(std::move(merged));
8124 new_faces.
append(std::move(faces(i)));
8126 faces = std::move(new_faces);
8136 for (
size_t fi = 0; fi < faces.
size(); ++fi)
8139 for (
size_t k = 0;
k < faces(fi).
size(); ++
k)
8144 result.
append(std::move(p));
8191 size_t left = ~static_cast<size_t>(0);
8226 size_t lo = 0, hi = arr.
size();
8228 if (
const size_t mid = lo + (hi - lo) / 2; arr(mid).get_y() < ymin)
8238 size_t lo = 0, hi = arr.
size();
8240 if (
const size_t mid = lo + (hi - lo) / 2; arr(mid).get_y() <= ymax)
8250 size_t lo = 0, hi =
n_;
8252 if (
const size_t mid = lo + (hi - lo) / 2;
pts_(mid).get_x() < xval)
8262 size_t lo = 0, hi =
n_;
8264 if (
const size_t mid = lo + (hi - lo) / 2;
pts_(mid).get_x() <= xval)
8271 void build_node(
const size_t node,
const size_t lo,
const size_t hi)
8279 const size_t mid = lo + (hi - lo) / 2;
8284 const auto &left =
tree_(2 * node).y_sorted;
8285 const auto &right =
tree_(2 * node + 1).y_sorted;
8286 auto &merged =
tree_(node).y_sorted;
8287 merged.reserve(left.size() + right.size());
8289 size_t i = 0, j = 0;
8290 while (i < left.size()
and j < right.size())
8291 if (left(i).get_y() < right(j).get_y() or
8292 (left(i).get_y() == right(j).get_y() && left(i).get_x() <= right(j).get_x()))
8293 merged.append(left(i++));
8295 merged.append(right(j++));
8296 while (i < left.size())
8297 merged.append(left(i++));
8298 while (j < right.size())
8299 merged.append(right(j++));
8311 if (qlo > qhi or lo > qhi or hi < qlo)
8314 if (qlo <= lo
and hi <= qhi)
8316 const auto &ys =
tree_(node).y_sorted;
8319 for (
size_t i = from; i < to; ++i)
8324 const size_t mid = lo + (hi - lo) / 2;
8326 query_range(2 * node + 1, mid + 1, hi, qlo, qhi, ymin, ymax,
out);
8349 :
pts_(std::move(other.pts_)),
tree_(std::move(other.tree_)),
8350 n_(std::exchange(other.n_, 0)),
built_(std::exchange(other.built_,
false))
8378 pts_.swap(other.pts_);
8379 tree_.swap(other.tree_);
8380 std::swap(
n_, other.n_);
8381 std::swap(
built_, other.built_);
8388 for (
auto it = points.
get_it(); it.has_curr(); it.next_ne())
8389 pts_.append(it.get_curr());
8404 const size_t tree_sz = 4 *
n_ + 4;
8405 tree_.reserve(tree_sz);
8406 for (
size_t i = 0; i < tree_sz; ++i)
8432 [[nodiscard]]
size_t size() const noexcept
8452 const size_t max_size = std::numeric_limits<size_t>::max();
8453 if (
n_ <= max_size / 2 + 1)
8456 auto build_debug = [&](
const auto &self,
const size_t tree_idx,
const size_t lo,
8457 const size_t hi) ->
size_t
8459 const size_t out_idx = snap.
nodes.size();
8461 snap.
nodes(out_idx).tree_index = tree_idx;
8462 snap.
nodes(out_idx).lo = lo;
8463 snap.
nodes(out_idx).hi = hi;
8464 snap.
nodes(out_idx).is_leaf = lo == hi;
8465 snap.
nodes(out_idx).xmin =
pts_(lo).get_x();
8466 snap.
nodes(out_idx).xmax =
pts_(hi).get_x();
8467 snap.
nodes(out_idx).y_sorted_size =
tree_(tree_idx).y_sorted.size();
8468 snap.
nodes(out_idx).split_x =
pts_((lo + hi) / 2).get_x();
8472 const size_t mid = lo + (hi - lo) / 2;
8473 const size_t left = self(self, 2 * tree_idx, lo, mid);
8474 const size_t right = self(self, 2 * tree_idx + 1, mid + 1, hi);
8475 snap.
nodes(out_idx).left = left;
8476 snap.
nodes(out_idx).right = right;
8482 build_debug(build_debug, 1, 0,
n_ - 1);
8556 nx = -dy * md / len;
8562 ny = -dx * md / len;
8580 const Geom_Number dax = a2x - a1x, day = a2y - a1y;
8581 const Geom_Number dbx = b2x - b1x, dby = b2y - b1y;
8590 const Geom_Number t = ((b1x - a1x) * dby - (b1y - a1y) * dbx) / denom;
8591 return {a1x + t * dax, a1y + t * day};
8615 const size_t n = v.
size();
8620 for (
size_t i = 0; i < n; ++i)
8622 const size_t j = (i + 1) % n;
8652 const size_t n = v.
size();
8658 for (
size_t i = 0; i < n; ++i)
8661 offset_edge(v(i), v((i + 1) % n), distance,
false, a, b);
8668 for (
size_t i = 0; i < n; ++i)
8670 const size_t j = (i + 1) % n;
8671 const Geom_Number dax = ob(i).get_x() - oa(i).get_x();
8672 const Geom_Number day = ob(i).get_y() - oa(i).get_y();
8673 const Geom_Number dbx = ob(j).get_x() - oa(j).get_x();
8674 const Geom_Number dby = ob(j).get_y() - oa(j).get_y();
8675 if (
const Geom_Number denom = dax * dby - day * dbx; denom == 0)
8682 if (result.
size() >= 3)
8745 [[nodiscard]]
size_t size() const noexcept
8772 r.polygons.append(poly);
8798 Result r = (*this)(poly, distance,
join, miter_limit);
8804 for (
size_t i = 1; i <
r.polygons.size(); ++i)
8810 return r.polygons(best);
8852 return dx * dx + dy * dy;
8860 const size_t n = v.
size();
8861 const bool inward = distance < 0;
8870 for (
size_t i = 0; i < n; ++i)
8873 offset_edge(v(i), v((i + 1) % n), d, inward, a, b);
8879 const Geom_Number miter_sq = miter_limit * miter_limit * d * d;
8889 for (
size_t i = 0; i < n; ++i)
8891 const size_t j = (i + 1) % n;
8894 const Geom_Number dax = ob(i).get_x() - oa(i).get_x();
8895 const Geom_Number day = ob(i).get_y() - oa(i).get_y();
8896 const Geom_Number dbx = ob(j).get_x() - oa(j).get_x();
8897 const Geom_Number dby = ob(j).get_y() - oa(j).get_y();
8899 if (
const Geom_Number denom = dax * dby - day * dbx; denom == 0)
8901 junction_count.
append(0);
8910 junction_count.
append(2);
8915 junction_count.
append(1);
8923 for (
size_t i = 0; i < n; ++i)
8925 const size_t j = (i + 1) % n;
8926 if (junction_count(i) == 0 || junction_count(j) == 0)
8931 const size_t span_start = junction_first(i) + junction_count(i) - 1;
8932 const size_t span_end = junction_first(j);
8934 const Geom_Number sdx = raw(span_end).get_x() - raw(span_start).get_x();
8935 const Geom_Number sdy = raw(span_end).get_y() - raw(span_start).get_y();
8938 const Geom_Number edx = v((j + 1) % n).get_x() - v(j).get_x();
8939 const Geom_Number edy = v((j + 1) % n).get_y() - v(j).get_y();
8941 if (sdx * edx + sdy * edy < 0)
8972 return (I.get_x() -
P.get_x()) / dx;
8973 return (I.get_y() -
P.get_y()) / (Q.
get_y() -
P.get_y());
8979 const size_t n = raw.
size();
8982 for (
size_t i = 0; i < n; ++i)
8984 const Point &a0 = raw(i);
8985 const Point &a1 = raw((i + 1) % n);
8988 for (
size_t j = i + 2; j < n; ++j)
8991 if (j == (i + n - 1) % n)
8993 if (i == 0 && j == n - 1)
8996 const Point &b0 = raw(j);
8997 const Point &b1 = raw((j + 1) % n);
9006 isects.
append({ip, i, j, ai, aj});
9016 for (
size_t i = 1; i < idx.
size(); ++i)
9018 const size_t val = idx(i);
9021 while (j > 0 &&
keys(idx(j - 1)) > key)
9023 idx(j) = idx(j - 1);
9034 const size_t n = raw.
size();
9035 const size_t m = isects.
size();
9042 for (
size_t k = 0;
k <
m; ++
k)
9051 for (
size_t i = 0; i < n; ++i)
9061 for (
size_t k = 0;
k <
m; ++
k)
9063 if (isects(
k).edge_i == i)
9066 edge_alphas.
append(isects(
k).alpha_i);
9068 else if (isects(
k).edge_j == i)
9071 edge_alphas.
append(isects(
k).alpha_j);
9080 for (
size_t p = 0; p < edge_isects.
size(); ++p)
9084 for (
size_t p = 0; p < order.
size(); ++p)
9086 const size_t k = edge_isects(order(p));
9087 const size_t aug_idx = aug.
size();
9090 if (isects(
k).edge_i == i)
9096 iv.
pt = isects(
k).pt;
9098 iv.
alpha = edge_alphas(order(p));
9104 for (
size_t k = 0;
k <
m; ++
k)
9106 aug(pos_i(
k)).partner = pos_j(
k);
9107 aug(pos_j(
k)).partner = pos_i(
k);
9117 for (
size_t i = 0; i < pts.
size(); ++i)
9119 if (pts.
size() >= 3)
9133 const size_t N = aug.
size();
9134 const size_t safety =
N * 2 + 10;
9136 for (
size_t s = 0; s <
N; ++s)
9138 if (not aug(s).is_intersection || aug(s).visited)
9146 aug(s).visited =
true;
9147 if (aug(s).partner != SIZE_MAX)
9148 aug(aug(s).partner).visited =
true;
9151 size_t idx = (aug(s).partner != SIZE_MAX) ? aug(s).partner : s;
9152 const size_t entry = idx;
9157 boundary.
append(aug(idx).pt);
9158 idx = (idx + 1) %
N;
9160 while (not aug(idx).is_intersection)
9162 boundary.
append(aug(idx).pt);
9163 idx = (idx + 1) %
N;
9164 if (++
steps > safety)
9169 aug(idx).visited =
true;
9170 if (aug(idx).partner != SIZE_MAX)
9172 aug(aug(idx).partner).visited =
true;
9173 idx = aug(idx).partner;
9176 if (++
steps > safety)
9178 }
while (idx != entry);
9181 if (boundary.
size() >= 3)
9197 if (isects.is_empty())
9202 if (sa > 0 && raw.
size() >= 3)
9245 if (dx > 0
and dy >= 0)
9247 if (dx <= 0 and dy > 0)
9249 if (dx < 0
and dy <= 0)
9268 if (
const Geom_Number cross = dax * dby - day * dbx; cross != 0)
9272 return dax * dax + day * day < (dbx * dbx + dby * dby);
9292 const Geom_Number t = (dx * edy - dy * edx) / denom;
9293 const Geom_Number s = (dx * rdy - dy * rdx) / denom;
9294 if (s < 0 or s > 1 or t < 0)
9308 return {q.
get_x() + t * rdx, q.
get_y() + t * rdy};
9348 for (
size_t i = 0; i < n; ++i)
9377 [[nodiscard]]
size_t min()
const
9379 if (
tree_.is_empty())
9381 return tree_.min().edge;
9391 return tree_.is_empty();
9408 <<
"Query point must be strictly inside the polygon";
9413 verts.
append(it.get_current_vertex());
9414 const size_t n = verts.
size();
9419 for (
size_t i = 0; i < n; ++i)
9423 return angle_less(query, verts(a), verts(b));
9432 for (
size_t ei = 0; ei < n; ++ei)
9448 for (
size_t i = 0; i < n; ++i)
9450 for (
size_t i = 0; i < n; ++i)
9453 for (
size_t ei = 0; ei < n; ++ei)
9455 const size_t a = ei, b = (ei + 1) % n;
9456 const bool a_less =
angle_less(query, verts(a), verts(b));
9457 const size_t small_v = a_less ? a : b;
9458 const size_t large_v = a_less ? b : a;
9462 ends(small_v).
append(ei);
9463 starts(large_v).
append(ei);
9467 starts(small_v).
append(ei);
9468 ends(large_v).
append(ei);
9474 for (
size_t ei = 0; ei < n; ++ei)
9476 status.
insert(ei, init_dir);
9481 for (
size_t oi = 0; oi < n; ++oi)
9483 const size_t vi = order(oi);
9484 const Point &v = verts(vi);
9485 const Point dir = v;
9488 const size_t prev_near = status.
min();
9491 for (
auto it = ends(vi).get_it(); it.has_curr(); it.next_ne())
9492 status.
erase(it.get_curr());
9495 for (
auto it = starts(vi).get_it(); it.has_curr(); it.next_ne())
9496 status.
insert(it.get_curr(), dir);
9499 const size_t curr_near = status.
min();
9502 bool v_is_on_edge =
false;
9505 const size_t ea = prev_near, eb = (prev_near + 1) % n;
9506 if (vi == ea or vi == eb)
9507 v_is_on_edge =
true;
9509 if (curr_near < n
and not v_is_on_edge)
9511 const size_t ea = curr_near, eb = (curr_near + 1) % n;
9512 if (vi == ea or vi == eb)
9513 v_is_on_edge =
true;
9521 else if (prev_near != curr_near)
9525 vis_pts.
append(
ray_edge_hit(query, dir, verts(prev_near), verts((prev_near + 1) % n)));
9528 vis_pts.
append(
ray_edge_hit(query, dir, verts(curr_near), verts((curr_near + 1) % n)));
9534 for (
size_t i = 0; i < vis_pts.
size(); ++i)
9536 if (i > 0
and vis_pts(i) == vis_pts(i - 1))
9540 if (result.
size() >= 3)
9545 rv.
append(it.get_current_vertex());
9546 if (rv.
size() >= 2
and rv(0) == rv(rv.
size() - 1))
9549 for (
size_t i = 0; i + 1 < rv.
size(); ++i)
9551 if (cleaned.
size() >= 3)
9590 static constexpr size_t NONE = ~static_cast<size_t>(0);
9601 for (
size_t i = 0; i < pts.
size(); ++i)
9612 for (
auto it = tl.
get_it(); it.has_curr(); it.next_ne())
9619 ti.adj[0] = ti.adj[1] = ti.adj[2] =
NONE;
9632 bool operator () (
const EdgeKey &a,
const EdgeKey &b)
const
9652 size_t u, v, tri, local;
9657 for (
size_t ti = 0; ti < tris.
size(); ++ti)
9658 for (
int e = 0; e < 3; ++e)
9660 size_t u = tris(ti).v[(e + 1) % 3];
9661 size_t v = tris(ti).v[(e + 2) % 3];
9664 const size_t tmp = u;
9668 edges.
append(EdgeEntry{u, v, ti, size_t(e)});
9672 quicksort_op(edges, [](
const EdgeEntry &a,
const EdgeEntry &b)
9678 return a.tri < b.tri;
9681 for (
size_t i = 0; i + 1 < edges.
size(); ++i)
9682 if (edges(i).u == edges(i + 1).u
and edges(i).v == edges(i + 1).v)
9684 const size_t t1 = edges(i).tri,
l1 = edges(i).local;
9685 const size_t t2 = edges(i + 1).tri,
l2 = edges(i + 1).local;
9686 tris(t1).adj[
l1] = t2;
9687 tris(t2).adj[
l2] = t1;
9702 return not (has_cw
and has_ccw);
9708 for (
size_t i = 0; i < tris.
size(); ++i)
9728 for (
size_t i = 0; i < tris.
size(); ++i)
9740 for (
unsigned long nb : tris(cur).adj)
9750 for (
size_t cur = dst; cur != src; cur = parent(cur))
9755 for (
size_t i = 0; i < path.
size() / 2; ++i)
9757 const size_t tmp = path(i);
9758 path(i) = path(path.
size() - 1 - i);
9759 path(path.
size() - 1 - i) = tmp;
9781 const Point &source,
9782 const Point &target)
const
9787 <<
"Source must be inside the polygon";
9789 <<
"Target must be inside the polygon";
9792 if (source == target)
9800 const Segment seg(source, target);
9801 bool blocked =
false;
9816 pts.
append(it.get_current_vertex());
9828 const size_t src_t =
find_tri(pts, tris, source);
9829 const size_t dst_t =
find_tri(pts, tris, target);
9837 if (sleeve.
size() <= 1)
9859 portals.
append(Portal{source, source});
9868 for (
size_t i = 0; i + 1 < sleeve.
size(); ++i)
9870 size_t s0 = 0, s1 = 0;
9874 bool found[3] = {
false,
false,
false};
9875 for (
int a = 0; a < 3; ++a)
9876 for (
unsigned long b : tris(sleeve(i + 1)).v)
9877 if (tris(sleeve(i)).v[a] == b)
9883 for (
int a = 0; a < 3; ++a)
9888 s0 = tris(sleeve(i)).v[a];
9890 s1 = tris(sleeve(i)).v[a];
9894 v_prev = tris(sleeve(i)).v[a];
9900 portals.
append(Portal{pts(s0), pts(s1)});
9902 portals.
append(Portal{pts(s1), pts(s0)});
9905 portals.
append(Portal{target, target});
9908 Point apex = source;
9911 size_t ai = 0, li = 0, ri = 0;
9915 for (
size_t i = 1; i < portals.
size(); ++i)
9917 const Point &pr = portals(i).right;
9918 const Point &pl = portals(i).left;
9921 if (
cross(apex, fr, pr) >= 0)
9923 if (apex == fr or
cross(apex, fl, pr) < 0)
9944 if (
cross(apex, fl, pl) <= 0)
9946 if (apex == fl or
cross(apex, fr, pl) > 0)
10035 static constexpr size_t NONE = ~static_cast<size_t>(0);
10048 size_t lo = 0, hi = verts.
size();
10050 if (
const size_t mid = lo + (hi - lo) / 2;
pt_less(verts(mid), p))
10052 else if (
pt_less(p, verts(mid)))
10062 if (dx > 0
and dy >= 0)
10064 if (dx <= 0 and dy > 0)
10066 if (dx < 0
and dy <= 0)
10082 if (
const Geom_Number cross = dx1 * dy2 - dy1 * dx2; cross != 0)
10084 return dx1 * dx1 + dy1 * dy1 < (dx2 * dx2 + dy2 * dy2);
10110 const size_t n = segments.
size();
10117 ret.
faces.append(uf);
10123 auto inters = sweep(segments);
10128 all_pts.
reserve(2 * n + inters.size());
10129 for (
size_t i = 0; i < n; ++i)
10131 all_pts.
append(segments(i).get_src_point());
10132 all_pts.
append(segments(i).get_tgt_point());
10134 for (
size_t i = 0; i < inters.size(); ++i)
10135 all_pts.
append(inters(i).point);
10145 for (
size_t i = 0; i < all_pts.
size(); ++i)
10151 for (
size_t si = 0; si < n; ++si)
10153 const Point &sp = segments(si).get_src_point();
10154 const Point &tp = segments(si).get_tgt_point();
10162 for (
size_t ki = 0; ki < inters.size(); ++ki)
10163 if (inters(ki).seg_i == si or inters(ki).seg_j == si)
10168 quicksort_op(on_seg, [](
const size_t a,
const size_t b)
10174 for (
size_t i = 0; i < on_seg.
size(); ++i)
10185 quicksort_op(on_seg, [&](
const size_t a,
const size_t b)
10187 return verts(a).get_y() < verts(b).get_y();
10190 quicksort_op(on_seg, [&](
const size_t a,
const size_t b)
10192 return verts(a).get_x() < verts(b).get_x();
10196 for (
size_t i = 0; i + 1 < on_seg.
size(); ++i)
10197 ret.
edges.append(
ArrEdge{on_seg(i), on_seg(i + 1), si});
10201 const size_t ne = ret.edges.size();
10202 const size_t nhe = 2 * ne;
10203 const size_t nv = ret.vertices.size();
10209 uf.unbounded =
true;
10210 ret.faces.append(uf);
10218 for (
size_t i = 0; i < ne; ++i)
10220 he.
append(HalfEdge{ret.edges(i).src, ret.edges(i).tgt, 2 * i + 1,
NONE,
NONE, i});
10221 he.
append(HalfEdge{ret.edges(i).tgt, ret.edges(i).src, 2 * i,
NONE,
NONE, i});
10227 for (
size_t i = 0; i < nv; ++i)
10229 for (
size_t h = 0;
h < nhe; ++
h)
10230 inc(he(
h).origin).append(
h);
10233 const auto &verts = ret.vertices;
10234 for (
size_t v = 0; v < nv; ++v)
10236 if (inc(v).
size() <= 1)
10240 const Geom_Number dxa = verts(he(a).target).get_x() - verts(v).get_x();
10241 const Geom_Number dya = verts(he(a).target).get_y() - verts(v).get_y();
10242 const Geom_Number dxb = verts(he(b).target).get_x() - verts(v).get_x();
10243 const Geom_Number dyb = verts(he(b).target).get_y() - verts(v).get_y();
10244 return angle_lt(dxa, dya, dxb, dyb);
10257 for (
size_t v = 0; v < nv; ++v)
10259 const auto &list = inc(v);
10260 const size_t sz = list.size();
10264 for (
size_t i = 0; i < sz; ++i)
10285 const size_t prev = (i == 0) ? sz - 1 : i - 1;
10286 he(he(list(i)).twin).next = list(prev);
10293 for (
size_t i = 0; i < nhe; ++i)
10296 for (
size_t h = 0;
h < nhe; ++
h)
10302 face.unbounded =
false;
10309 visited(cur) =
true;
10310 face.boundary.
append(he(cur).origin);
10313 const Point &p1 = verts(he(cur).origin);
10314 const Point &p2 = verts(he(cur).target);
10317 cur = he(cur).next;
10318 }
while (cur !=
h and cur !=
NONE);
10321 if (signed_area < 0)
10322 face.unbounded =
true;
10332 bool has_ub =
false;
10333 for (
size_t i = 0; i < ret.faces.size(); ++i)
10334 if (ret.faces(i).unbounded)
10339 if (not has_ub
and not ret.faces.is_empty())
10340 ret.faces(0).unbounded =
true;
10410 auto [sites, triangles] = delaunay(points);
10415 if (triangles.size() == 0)
10420 kept.
reserve(triangles.size());
10421 for (
size_t t = 0; t < triangles.size(); ++t)
10423 const auto &tri = triangles(t);
10424 const Point &a = sites(tri.i);
10425 const Point &b = sites(tri.j);
10426 const Point &c = sites(tri.k);
10433 const Geom_Number four_area_sq = area2x * area2x;
10437 const bool pass = ab2 * bc2 * ca2 <= alpha_squared * four_area_sq * 4;
10455 return u == o.u
and v == o.v;
10460 return u < o.u or (u == o.u
and v < o.v);
10464 auto make_key = [](
const size_t a,
const size_t b) -> EdgeKey
10466 return a < b ? EdgeKey{a, b} : EdgeKey{b, a};
10471 for (
size_t t = 0; t < ret.
triangles.size(); ++t)
10474 all_edges.
append(make_key(i, j));
10475 all_edges.
append(make_key(j,
k));
10476 all_edges.
append(make_key(
k, i));
10479 quicksort_op(all_edges, [](
const EdgeKey &a,
const EdgeKey &b)
10484 for (
size_t i = 0; i < all_edges.
size();)
10487 while (j < all_edges.
size()
and all_edges(j) == all_edges(i))
10621 return {(rhs1 * d2y - rhs2 * d1y) / det, (d1x * rhs2 - d2x * rhs1) / det};
10636 const size_t n = sites.
size();
10645 for (
size_t i = 0; i < n; ++i)
10646 rt_input.
append({sites(i).position, sites(i).weight});
10649 auto rt = reg_tri(rt_input);
10651 if (rt.triangles.size() == 0)
10657 rt_to_ours.
reserve(rt.sites.size());
10658 for (
size_t ri = 0; ri < rt.sites.size(); ++ri)
10661 for (
size_t oi = 0; oi < n; ++oi)
10662 if (rt.sites(ri).position == sites(oi).position)
10667 rt_to_ours.
append(match);
10672 pcenters.
reserve(rt.triangles.size());
10673 for (
size_t t = 0; t < rt.triangles.size(); ++t)
10675 const auto &tri = rt.triangles(t);
10677 sites(rt_to_ours(tri.k))));
10691 if (last - first == 1)
10694 hull_sites.insert(rt_to_ours(edge_refs(first).u));
10695 hull_sites.insert(rt_to_ours(edge_refs(first).v));
10700 if (last - first >= 2)
10701 for (
size_t a = first; a + 1 < last; ++a)
10702 for (
size_t b = a + 1; b < last; ++b)
10705 pe.
site_u = rt_to_ours(edge_refs(first).u);
10706 pe.
site_v = rt_to_ours(edge_refs(first).v);
10707 pe.
src = pcenters(edge_refs(a).tri);
10708 pe.
tgt = pcenters(edge_refs(b).tri);
10711 ret.
edges.append(pe);
10716 ret.cells.reserve(n);
10717 for (
size_t s = 0; s < n; ++s)
10720 cell.site_index = s;
10722 cell.weight = sites(s).weight;
10723 cell.bounded = hull_sites.search(s) ==
nullptr;
10726 for (
size_t t = 0; t < rt.triangles.size(); ++t)
10728 const auto &[i, j,
k] = rt.triangles(t);
10729 if (rt_to_ours(i) == s or rt_to_ours(j) == s or rt_to_ours(
k) == s)
10730 cell.vertices.append(pcenters(t));
10733 ret.cells.append(
cell);
10809 for (
size_t i = 0; i <= n; ++i)
10825 for (
size_t i = 0; i <= n; ++i)
10848 return {s * a.
get_x() + t * b.get_x(), s * a.
get_y() + t * b.get_y()};
10851 const Point q0 = lerp(p0, p1);
10852 const Point q1 = lerp(p1, p2);
10853 const Point q2 = lerp(p2, p3);
10854 const Point r0 = lerp(q0, q1);
10855 const Point r1 = lerp(q1, q2);
10856 const Point s0 = lerp(r0, r1);
10878 auto update = [&](
const Point &p)
10880 if (p.get_x() < mnx)
10882 if (p.get_x() > mxx)
10884 if (p.get_y() < mny)
10886 if (p.get_y() > mxy)
10892 return {mnx, mny, mxx, mxy};
10903 for (
size_t i = 0; i < pts.size(); ++i)
10917 for (
size_t i = 0; i < pts.size(); ++i)
11058 for (
size_t i = 0, j = v.
size() - 1; i < j; ++i, --j)
11060 const Point tmp = v(i);
11070 for (
size_t i = 0; i < pts.
size(); ++i)
11072 if (pts.
size() >= 3)
11082 return (I.get_x() -
P.get_x()) / dx;
11083 return (I.get_y() -
P.get_y()) / (Q.
get_y() -
P.get_y());
11090 const size_t n = poly.
size();
11091 for (
size_t i = 0; i < n; ++i)
11093 const Point &a = poly(i);
11094 const Point &b = poly((i + 1) % n);
11109 return winding != 0;
11115 for (
size_t i = 1; i < idx.
size(); ++i)
11117 const size_t val = idx(i);
11120 while (j > 0 &&
keys(idx(j - 1)) > key)
11122 idx(j) = idx(j - 1);
11146 bool start_entering)
11148 const size_t na = va.
size();
11149 const size_t nb = vb.
size();
11155 for (
size_t i = 0; i < na; ++i)
11157 const Point &a0 = va(i);
11158 const Point &a1 = va((i + 1) % na);
11161 for (
size_t j = 0; j < nb; ++j)
11163 const Point &b0 = vb(j);
11164 const Point &b1 = vb((j + 1) % nb);
11173 pairs.
append({ip, i, j, aa, ab});
11185 for (
size_t k = 0;
k < pairs.
size(); ++
k)
11193 for (
size_t i = 0; i < na; ++i)
11202 for (
size_t k = 0;
k < pairs.
size(); ++
k)
11203 if (pairs(
k).edge_a == i)
11206 edge_alphas.
append(pairs(
k).alpha_a);
11211 for (
size_t p = 0; p < edge_pairs.
size(); ++p)
11215 for (
size_t p = 0; p < order.
size(); ++p)
11217 const size_t k = edge_pairs(order(p));
11218 pair_idx_a(
k) = list_a.
size();
11220 in.
pt = pairs(
k).pt;
11222 in.
alpha = pairs(
k).alpha_a;
11228 for (
size_t j = 0; j < nb; ++j)
11236 for (
size_t k = 0;
k < pairs.
size(); ++
k)
11237 if (pairs(
k).edge_b == j)
11240 edge_alphas.
append(pairs(
k).alpha_b);
11244 for (
size_t p = 0; p < edge_pairs.
size(); ++p)
11248 for (
size_t p = 0; p < order.
size(); ++p)
11250 const size_t k = edge_pairs(order(p));
11251 pair_idx_b(
k) = list_b.
size();
11253 in.
pt = pairs(
k).pt;
11255 in.
alpha = pairs(
k).alpha_b;
11261 for (
size_t k = 0;
k < pairs.
size(); ++
k)
11263 list_a(pair_idx_a(
k)).neighbor = pair_idx_b(
k);
11264 list_b(pair_idx_b(
k)).neighbor = pair_idx_a(
k);
11273 for (
size_t i = 0; i < list_a.
size(); ++i)
11274 if (list_a(i).intersect)
11276 list_a(i).entering = !inside;
11284 for (
size_t j = 0; j < list_b.
size(); ++j)
11285 if (list_b(j).intersect)
11287 list_b(j).entering = !inside;
11295 const size_t safety_limit = list_a.
size() + list_b.
size() + pairs.
size() * 2;
11297 for (
size_t s = 0; s < list_a.
size(); ++s)
11299 if (not list_a(s).intersect)
11301 if (list_a(s).visited)
11303 if (list_a(s).entering != start_entering)
11314 auto &cur_list = on_a ? list_a : list_b;
11317 cur_list(idx).visited =
true;
11318 if (cur_list(idx).neighbor != SIZE_MAX)
11320 auto &other = on_a ? list_b : list_a;
11321 other(cur_list(idx).neighbor).visited =
true;
11325 boundary.
append(cur_list(idx).pt);
11328 idx = (idx + 1) % cur_list.size();
11329 while (not cur_list(idx).intersect)
11331 boundary.
append(cur_list(idx).pt);
11332 idx = (idx + 1) % cur_list.size();
11333 if (++
steps > safety_limit)
11338 cur_list(idx).visited =
true;
11340 auto &other = on_a ? list_b : list_a;
11341 const size_t nbr = cur_list(idx).neighbor;
11342 other(nbr).visited =
true;
11347 if (++
steps > safety_limit)
11349 }
while (!(on_a && idx == s));
11352 if (boundary.
size() >= 3)
11516 std::ostringstream os;
11546 auto pt = [](
const Point &p)
11548 return dbl(p.get_x()) +
" " +
dbl(p.get_y());
11550 return "POLYGON ((" + pt(t.
get_p1()) +
", " + pt(t.
get_p2()) +
", " + pt(t.
get_p3()) +
", " +
11560 return "POLYGON ((" +
dbl(
r.get_xmin()) +
" " +
dbl(
r.get_ymin()) +
", " +
dbl(
r.get_xmax()) +
11561 " " +
dbl(
r.get_ymin()) +
", " +
dbl(
r.get_xmax()) +
" " +
dbl(
r.get_ymax()) +
", " +
11562 dbl(
r.get_xmin()) +
" " +
dbl(
r.get_ymax()) +
", " +
dbl(
r.get_xmin()) +
" " +
11563 dbl(
r.get_ymin()) +
"))";
11571 std::ostringstream os;
11572 os << std::setprecision(15) <<
"POLYGON ((";
11577 const Point &v = it.get_current_vertex();
11609 return R
"({"type":"Point","coordinates":[)" + dbl(p.get_x()) + "," +
dbl(p.
get_y()) +
"]}";
11625 auto pt = [](
const Point &p)
11627 return "[" +
dbl(p.get_x()) +
"," +
dbl(p.get_y()) +
"]";
11629 return R
"({"type":"Polygon","coordinates":[[)" + pt(t.get_p1()) + "," + pt(t.
get_p2()) +
"," +
11636 std::ostringstream os;
11637 os << std::setprecision(15);
11638 os << R
"({"type":"Polygon","coordinates":[[)";
11643 const Point &v = it.get_current_vertex();
11664 return R
"({"type":"Point","coordinates":[)" + dbl(p.get_x()) + "," +
dbl(p.
get_y()) +
"," +
11758 return entry_idx != ~static_cast<size_t>(0);
11765 static constexpr size_t NONE = ~static_cast<size_t>(0);
11776 return {xmin, ymin, xmax, ymax};
11794 p.
get_y() <=
r.get_ymax();
11799 return split_x ?
entries_(entry_idx).bbox.get_xmin() +
entries_(entry_idx).bbox.get_xmax()
11800 :
entries_(entry_idx).bbox.get_ymin() +
entries_(entry_idx).bbox.get_ymax();
11806 const bool split_x)
const
11808 for (
size_t i = lo + 1; i < hi; ++i)
11810 const size_t key = idx(i);
11815 idx(j) = idx(j - 1);
11828 const bool split_x)
const
11830 while (hi - lo > 16)
11842 const size_t tmp = idx(lt);
11848 else if (val > pivot)
11851 const size_t tmp = idx(gt);
11861 else if (kth >= gt)
11878 const size_t ni =
nodes_.size();
11885 for (
size_t i = lo + 1; i < hi; ++i)
11891 const bool split_x = dx >= dy;
11893 const size_t mid = lo + (hi - lo) / 2;
11896 const size_t left =
build(idx, lo, mid);
11897 const size_t right =
build(idx, mid, hi);
11903 const size_t ni =
nodes_.size();
11966 :
nodes_(std::move(other.nodes_)),
entries_(std::move(other.entries_)),
11967 root_(std::exchange(other.root_,
NONE))
11995 nodes_.swap(other.nodes_);
11997 std::swap(
root_, other.root_);
12017 for (
size_t i = 0; i <
entries_.size(); ++i)
12079 auto dfs = [&](
const auto &self,
const size_t ni,
const size_t depth) ->
size_t
12082 const size_t out_idx = snap.
nodes.size();
12084 snap.
nodes(out_idx).node_index = ni;
12087 snap.
nodes(out_idx).depth = depth;
12095 const size_t left = self(self, n.
left, depth + 1);
12096 const size_t right = self(self, n.
right, depth + 1);
12097 snap.
nodes(out_idx).left = left;
12098 snap.
nodes(out_idx).right = right;
12130 const size_t first,
12135 if (last <= first + 1)
12138 const Segment seg(pts(first), pts(last));
12140 size_t max_idx = first;
12142 for (
size_t i = first + 1; i < last; ++i)
12152 if (max_dist2 > eps2)
12154 keep(max_idx) =
true;
12155 dp_recurse(pts, first, max_idx, eps2, keep);
12172 const size_t n = polyline.
size();
12178 for (
size_t i = 0; i < n; ++i)
12179 keep.append(
false);
12181 keep(n - 1) =
true;
12186 for (
size_t i = 0; i < n; ++i)
12188 result.
append(polyline(i));
12198 for (
auto it = polyline.
get_it(); it.has_curr(); it.next_ne())
12199 arr.
append(it.get_curr());
12200 return (*
this)(arr, epsilon);
12216 const size_t n = verts.
size();
12221 size_t far_idx = 1;
12223 for (
size_t i = 1; i < n; ++i)
12225 Geom_Number dx = verts(i).get_x() - verts(0).get_x();
12226 Geom_Number dy = verts(i).get_y() - verts(0).get_y();
12227 if (
Geom_Number d2 = dx * dx + dy * dy; d2 > far_dist2)
12236 for (
size_t i = 0; i <= far_idx; ++i)
12237 chain1.
append(verts(i));
12240 for (
size_t i = far_idx; i < n; ++i)
12241 chain2.
append(verts(i));
12242 chain2.
append(verts(0));
12244 auto s1 = (*this)(chain1, epsilon);
12245 auto s2 = (*this)(chain2, epsilon);
12249 for (
size_t i = 0; i < s1.size(); ++i)
12251 for (
size_t i = 1; i + 1 < s2.size(); ++i)
12252 merged.append(s2(i));
12254 if (merged.size() < 3)
12258 for (
size_t i = 0; i < merged.size(); ++i)
12310 const size_t n = polyline.
size();
12318 for (
size_t i = 0; i < n; ++i)
12320 prev.append(i == 0 ? n : i - 1);
12321 next.append(i == n - 1 ? n : i + 1);
12322 alive.append(
true);
12330 for (
size_t i = 0; i < n; ++i)
12333 for (
size_t i = 1; i + 1 < n; ++i)
12336 heap.
put({i, areas(i)});
12341 auto [idx, area] = heap.
get();
12343 if (not alive(idx))
12345 if (area != areas(idx))
12347 if (area >= area_threshold)
12351 alive(idx) =
false;
12352 const size_t p = prev(idx);
12353 const size_t nx =
next(idx);
12361 if (p < n
and p != 0
and prev(p) < n)
12364 heap.
put({p, areas(p)});
12366 if (nx < n
and nx != n - 1
and next(nx) < n)
12369 heap.
put({nx, areas(nx)});
12374 for (
size_t i = 0; i < n; ++i)
12376 result.
append(polyline(i));
12402 for (
auto it = polyline.
get_it(); it.has_curr(); it.next_ne())
12403 arr.
append(it.get_curr());
12419 const size_t n = verts.
size();
12427 for (
size_t i = 0; i < n; ++i)
12429 prev.append(i == 0 ? n - 1 : i - 1);
12430 next.append(i == n - 1 ? 0 : i + 1);
12431 alive.append(
true);
12437 for (
size_t i = 0; i < n; ++i)
12440 heap.
put({i, areas(i)});
12443 size_t remaining = n;
12446 auto [idx, area] = heap.
get();
12448 if (not alive(idx))
12450 if (area != areas(idx))
12452 if (area >= area_threshold)
12455 alive(idx) =
false;
12457 const size_t p = prev(idx);
12458 const size_t nx =
next(idx);
12465 heap.
put({p, areas(p)});
12467 heap.
put({nx, areas(nx)});
12471 for (
size_t i = 0; i < n; ++i)
12473 result.
append(verts(i));
12475 if (result.
size() < 3)
12479 for (
size_t i = 0; i < result.
size(); ++i)
12508 const size_t n = pts.
size();
12511 for (
size_t i = 0; i + 1 < n; ++i)
12513 const auto &p = pts(i);
12514 const auto &q = pts(i + 1);
12516 result.
append(
Point(p.get_x() * one_r + q.get_x() *
r, p.get_y() * one_r + q.get_y() *
r));
12517 result.
append(
Point(p.get_x() *
r + q.get_x() * one_r, p.get_y() *
r + q.get_y() * one_r));
12519 result.
append(pts(n - 1));
12525 const size_t n = pts.
size();
12528 for (
size_t i = 0; i < n; ++i)
12530 const auto &p = pts(i);
12531 const auto &q = pts((i + 1) % n);
12532 result.
append(
Point(p.get_x() * one_r + q.get_x() *
r, p.get_y() * one_r + q.get_y() *
r));
12533 result.
append(
Point(p.get_x() *
r + q.get_x() * one_r, p.get_y() *
r + q.get_y() * one_r));
12555 <<
"ratio must be in (0, 0.5)";
12558 for (
size_t k = 0;
k < iterations; ++
k)
12569 for (
auto it = polyline.
get_it(); it.has_curr(); it.next_ne())
12570 arr.
append(it.get_curr());
12571 return (*
this)(arr, iterations, ratio);
12590 <<
"ratio must be in (0, 0.5)";
12593 for (
size_t k = 0;
k < iterations; ++
k)
12597 for (
size_t i = 0; i < cur.
size(); ++i)
12663 static constexpr size_t NONE = ~static_cast<size_t>(0);
12756 const auto &[type, index, left, right] =
dag(ni);
12765 ni = p < xp ? left : right;
12780 else if (cross > 0)
12812 size_t crossings = 0;
12823 if (seg_y < p.
get_y())
12826 return crossings % 2 == 1;
12847 const auto &[type, index, left, right] = dag(ni);
12853 if (
const Point &xp = pts(index); p == xp)
12856 ni = p < xp ? left : right;
12864 else if (cross < 0)
12876 const size_t bottom,
12877 const size_t leftp,
12878 const size_t rightp)
12880 const size_t ti = traps.
size();
12881 const size_t di = dag.
size();
12889 const size_t leaf_idx,
12891 const size_t index,
12893 const size_t right)
12895 dag(leaf_idx).type = type;
12896 dag(leaf_idx).index = index;
12897 dag(leaf_idx).left = left;
12898 dag(leaf_idx).right = right;
12903 const size_t neighbor_idx,
12904 const size_t old_ti,
12905 const size_t new_ti)
12907 if (neighbor_idx ==
NONE)
12925 const size_t seg_idx,
12926 const size_t start_trap)
12929 crossed.
append(start_trap);
12931 const Segment &seg = segs(seg_idx);
12934 size_t ti = start_trap;
12941 if (rp < rightp or rp == rightp)
12972 const size_t seg_idx,
12973 const size_t trap_idx)
12975 const Trapezoid old_t = traps(trap_idx);
12976 const Segment &seg = segs(seg_idx);
12981 size_t lp_idx =
NONE, rp_idx =
NONE;
12982 for (
size_t i = 0; i < pts.
size(); ++i)
12990 const bool shared_left = (old_t.
leftp == lp_idx);
12991 const bool shared_right = (old_t.
rightp == rp_idx);
12994 traps(trap_idx).active =
false;
13002 const size_t dag_old = old_t.
dag_leaf;
13004 size_t a_idx =
NONE, d_idx =
NONE;
13006 if (not shared_left)
13008 const size_t b_idx =
make_trapezoid(traps, dag, old_t.
top, seg_idx, lp_idx, rp_idx);
13010 if (not shared_right)
13019 traps(a_idx).upper_right = b_idx;
13020 traps(a_idx).lower_right = c_idx;
13026 traps(b_idx).upper_left = shared_left ? old_t.
upper_left : a_idx;
13027 traps(b_idx).lower_left = shared_left ? old_t.
lower_left : a_idx;
13028 traps(b_idx).upper_right = shared_right ? old_t.
upper_right : d_idx;
13029 traps(b_idx).lower_right = shared_right ? old_t.
lower_right : d_idx;
13032 traps(c_idx).upper_left = shared_left ? old_t.
upper_left : a_idx;
13033 traps(c_idx).lower_left = shared_left ? old_t.
lower_left : a_idx;
13034 traps(c_idx).upper_right = shared_right ? old_t.
upper_right : d_idx;
13035 traps(c_idx).lower_right = shared_right ? old_t.
lower_right : d_idx;
13046 traps(d_idx).upper_left = b_idx;
13047 traps(d_idx).lower_left = c_idx;
13062 if (not shared_left
and not shared_right)
13065 const size_t y_node = dag.
size();
13067 const size_t x_rp = dag.
size();
13071 else if (shared_left
and not shared_right)
13074 const size_t y_node = dag.
size();
13078 else if (not shared_left
and shared_right)
13081 const size_t y_node = dag.
size();
13089 traps(c_idx).dag_leaf);
13098 const size_t seg_idx,
13101 const Segment &seg = segs(seg_idx);
13105 size_t lp_idx =
NONE, rp_idx =
NONE;
13106 for (
size_t i = 0; i < pts.
size(); ++i)
13114 const size_t nc = crossed.
size();
13124 size_t prev_above =
NONE, prev_below =
NONE;
13125 size_t left_trap =
NONE;
13127 for (
size_t ci = 0; ci < nc; ++ci)
13129 const size_t ti = crossed(ci);
13131 traps(ti).active =
false;
13133 const bool is_first = (ci == 0);
13134 const bool is_last = (ci == nc - 1);
13135 const bool shared_left = is_first
and (old_t.
leftp == lp_idx);
13136 const bool shared_right = is_last
and (old_t.
rightp == rp_idx);
13139 const size_t new_leftp = is_first ? lp_idx : old_t.
leftp;
13140 const size_t new_rightp = is_last ? rp_idx : old_t.
rightp;
13143 bool merged_above =
false;
13144 if (prev_above !=
NONE and traps(prev_above).top == old_t.
top)
13147 traps(prev_above).rightp = new_rightp;
13148 above_traps.
append(prev_above);
13149 merged_above =
true;
13153 const size_t ai =
make_trapezoid(traps, dag, old_t.
top, seg_idx, new_leftp, new_rightp);
13159 bool merged_below =
false;
13160 if (prev_below !=
NONE and traps(prev_below).bottom == old_t.
bottom)
13162 traps(prev_below).rightp = new_rightp;
13163 below_traps.
append(prev_below);
13164 merged_below =
true;
13174 const size_t cur_above = above_traps(ci);
13175 const size_t cur_below = below_traps(ci);
13178 if (is_first
and not shared_left)
13181 traps(left_trap).upper_left = old_t.
upper_left;
13182 traps(left_trap).lower_left = old_t.
lower_left;
13183 traps(left_trap).upper_right = cur_above;
13184 traps(left_trap).lower_right = cur_below;
13192 if (not merged_above)
13196 traps(cur_above).upper_left = old_t.
upper_left;
13197 traps(cur_above).lower_left = old_t.
lower_left;
13202 traps(cur_above).upper_left = left_trap;
13203 traps(cur_above).lower_left = left_trap;
13206 if (not merged_below)
13210 traps(cur_below).upper_left = old_t.
upper_left;
13211 traps(cur_below).lower_left = old_t.
lower_left;
13216 traps(cur_below).upper_left = left_trap;
13217 traps(cur_below).lower_left = left_trap;
13224 if (not merged_above)
13227 traps(cur_above).upper_left = above_traps(ci - 1);
13228 traps(cur_above).lower_left = above_traps(ci - 1);
13230 if (above_traps(ci - 1) != cur_above)
13232 traps(above_traps(ci - 1)).upper_right = cur_above;
13233 traps(above_traps(ci - 1)).lower_right = cur_above;
13236 if (not merged_below)
13238 traps(cur_below).upper_left = below_traps(ci - 1);
13239 traps(cur_below).lower_left = below_traps(ci - 1);
13240 if (below_traps(ci - 1) != cur_below)
13242 traps(below_traps(ci - 1)).upper_right = cur_below;
13243 traps(below_traps(ci - 1)).lower_right = cur_below;
13248 if (not merged_above)
13251 traps(cur_above).upper_left = old_t.
upper_left;
13254 if (not merged_below)
13257 traps(cur_below).lower_left = old_t.
lower_left;
13265 if (not shared_right)
13267 const size_t right_trap =
13269 traps(right_trap).upper_left = cur_above;
13270 traps(right_trap).lower_left = cur_below;
13271 traps(right_trap).upper_right = old_t.
upper_right;
13272 traps(right_trap).lower_right = old_t.
lower_right;
13275 traps(cur_above).upper_right = right_trap;
13276 traps(cur_above).lower_right = right_trap;
13277 traps(cur_below).upper_right = right_trap;
13278 traps(cur_below).lower_right = right_trap;
13281 const size_t y_node = dag.
size();
13283 traps(cur_below).dag_leaf});
13284 const size_t dag_old = old_t.
dag_leaf;
13286 traps(right_trap).dag_leaf);
13290 traps(cur_above).upper_right = old_t.
upper_right;
13291 traps(cur_above).lower_right = old_t.
lower_right;
13292 traps(cur_below).upper_right = old_t.
upper_right;
13293 traps(cur_below).lower_right = old_t.
lower_right;
13298 const size_t dag_old = old_t.
dag_leaf;
13300 traps(cur_below).dag_leaf);
13306 const size_t dag_old = old_t.
dag_leaf;
13307 const size_t y_node = dag.
size();
13309 traps(cur_below).dag_leaf});
13312 traps(cur_below).dag_leaf);
13319 const size_t dag_old = old_t.
dag_leaf;
13321 traps(cur_below).dag_leaf);
13338 const size_t n = input_segments.
size();
13344 for (
size_t i = 0; i < n; ++i)
13346 const Point &s = input_segments(i).get_src_point();
13347 const Point &t = input_segments(i).get_tgt_point();
13359 for (
size_t i = 0; i < n; ++i)
13367 for (
size_t i = 0; i < all_pts.
size(); ++i)
13370 ret.
points.append(all_pts(i));
13377 if (ret.
points.is_empty())
13390 for (
size_t i = 1; i < ret.
points.size(); ++i)
13392 if (ret.
points(i).get_x() < mnx)
13393 mnx = ret.
points(i).get_x();
13394 if (ret.
points(i).get_x() > mxx)
13395 mxx = ret.
points(i).get_x();
13396 if (ret.
points(i).get_y() < mny)
13397 mny = ret.
points(i).get_y();
13398 if (ret.
points(i).get_y() > mxy)
13399 mxy = ret.
points(i).get_y();
13402 Geom_Number margin = (mxx - mnx > mxy - mny) ? mxx - mnx : mxy - mny;
13405 margin = margin + 1;
13408 const Point bl(mnx - margin, mny - margin);
13409 const Point br(mxx + margin, mny - margin);
13410 const Point tl(mnx - margin, mxy + margin);
13411 const Point tr(mxx + margin, mxy + margin);
13413 const size_t bl_idx = ret.
points.size();
13417 const size_t tr_idx = ret.
points.size();
13421 const size_t top_seg = ret.
segments.size();
13423 const size_t bot_seg = ret.
segments.size();
13437 for (
size_t i = 0; i < n; ++i)
13442 std::random_device rd;
13443 std::mt19937 gen(rd());
13444 for (
size_t i = n - 1; i > 0; --i)
13446 std::uniform_int_distribution<size_t> dis(0, i);
13447 const size_t j = dis(gen);
13448 const size_t tmp = order(i);
13449 order(i) = order(j);
13455 for (
size_t oi = 0; oi < n; ++oi)
13457 const size_t si = order(oi);
13461 const size_t start =
13468 if (crossed.
size() == 1)
13490 segs.
append(it.get_current_segment());
13491 return (*
this)(segs);
13503 for (
size_t i = 0; i < polygons.
size(); ++i)
13505 const Polygon &poly = polygons(i);
13509 segs.
append(it.get_current_segment());
13511 return (*
this)(segs);
13519#if __cplusplus >= 202002L
13544template <
typename T>
13545concept GeomNumberType =
requires(
T a,
T b,
int i) {
13546 {
T(i) } -> std::convertible_to<T>;
13547 { a + b } -> std::convertible_to<T>;
13548 { a - b } -> std::convertible_to<T>;
13549 { a * b } -> std::convertible_to<T>;
13550 { a / b } -> std::convertible_to<T>;
13551 { a == b } -> std::convertible_to<bool>;
13552 { a != b } -> std::convertible_to<bool>;
13553 { a < b } -> std::convertible_to<bool>;
13554 { a <= b } -> std::convertible_to<bool>;
13555 { a > b } -> std::convertible_to<bool>;
13556 { a >= b } -> std::convertible_to<bool>;
13557 { -a } -> std::convertible_to<T>;
13561static_assert(GeomNumberType<Geom_Number>,
"Geom_Number must satisfy GeomNumberType");
13562static_assert(GeomNumberType<double>,
"double must satisfy GeomNumberType");
13563static_assert(GeomNumberType<long long>,
"long long must satisfy GeomNumberType");
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
bool operator==(const Time &l, const Time &r)
bool operator<(const Time &l, const Time &r)
Deduplicate sequential Aleph containers in-place.
static int cell(const aleph_ca_engine_t *e, size_t r, size_t c)
size_t size_t int32_t * out
Axis-aligned bounding box tree for spatial queries.
Rectangle root_bbox() const
Return the root bounding box (union of all entries).
AABBTree(AABBTree &&other) noexcept
Take the entries and the nodes of another tree, which is left empty.
Array< Entry > entries_
Original entries stored in the tree.
size_t build(Array< size_t > &idx, const size_t lo, const size_t hi)
AABBTree(const AABBTree &other)=default
Copy the entries and the nodes of another tree.
void select_kth_by_center(Array< size_t > &idx, size_t lo, size_t hi, const size_t kth, const bool split_x) const
static bool box_contains_point(const Rectangle &r, const Point &p)
Checks if a rectangle contains a point.
static Rectangle union_bbox(const Rectangle &a, const Rectangle &b)
Computes the union of two axis-aligned bounding boxes.
void swap(AABBTree &other) noexcept
Exchange in O(1) the entries and the nodes of this tree and another.
void query_point_impl(const size_t ni, const Point &p, Array< size_t > &results) const
Geom_Number center_coord(const size_t entry_idx, const bool split_x) const
bool is_empty() const
Whether the tree is empty.
size_t size() const
Number of entries.
void insertion_sort_by_center(Array< size_t > &idx, const size_t lo, const size_t hi, const bool split_x) const
static constexpr size_t NONE
void build(const Array< Entry > &entries)
Build the AABB tree from an array of entries.
DebugSnapshot debug_snapshot() const
Return the full tree structure for visualization/debug.
static bool boxes_overlap(const Rectangle &a, const Rectangle &b)
Checks if two rectangles overlap.
Array< size_t > query(const Rectangle &query) const
Find all entries whose bounding box overlaps the query rectangle.
Array< size_t > query_point(const Point &p) const
Find all entries whose bounding box contains the query point.
Array< Node > nodes_
Internal array storing tree nodes.
AABBTree & operator=(AABBTree &&other) noexcept
Exchange this tree with another, which is left with the previous entries of this one.
AABBTree & operator=(const AABBTree &other)=default
Copy the entries and the nodes of another tree.
void query_impl(const size_t ni, const Rectangle &q, Array< size_t > &results) const
Alpha shape of a point set.
Result operator()(const DynList< Point > &points, const Geom_Number &alpha_squared) const
Compute the α-shape.
Andrew's monotonic chain convex hull algorithm.
Polygon operator()(const DynList< Point > &point_set) const
Compute the convex hull of a point set.
static Array< Point > sorted_unique_points(const DynList< Point > &point_set)
Extracts points from a list, sorts them and removes duplicates.
static Geom_Number turn(const Point &a, const Point &b, const Point &c)
Computes the turn orientation of triple (a, b, c).
Simple dynamic array with automatic resizing and functional operations.
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void empty() noexcept
Empties the container.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & insert(const T &data)
insert a copy of data at the beginning of the array.
T & get_last()
Return a modifiable reference to the last element.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Quadratic and cubic Bézier curves with exact rational arithmetic.
static Polygon approximate_quadratic(const Point &p0, const Point &p1, const Point &p2, const size_t n)
Approximate a quadratic Bézier as a polyline (polygon without closing).
static Array< Point > sample_cubic(const Point &p0, const Point &p1, const Point &p2, const Point &p3, const size_t n)
Sample a cubic Bézier into n+1 points.
static Rectangle control_bbox(const Point &p0, const Point &p1, const Point &p2, const Point &p3)
Compute the bounding box of a cubic Bézier's control polygon.
static Array< Point > sample_quadratic(const Point &p0, const Point &p1, const Point &p2, const size_t n)
Sample a quadratic Bézier into n+1 points (t = 0, 1/n, ..., 1).
static SplitResult split_cubic(const Point &p0, const Point &p1, const Point &p2, const Point &p3, const Geom_Number &t)
static Point quadratic(const Point &p0, const Point &p1, const Point &p2, const Geom_Number &t)
Evaluate a quadratic Bézier at parameter t.
static Polygon approximate_cubic(const Point &p0, const Point &p1, const Point &p2, const Point &p3, const size_t n)
Approximate a cubic Bézier as a polyline.
static Point cubic(const Point &p0, const Point &p1, const Point &p2, const Point &p3, const Geom_Number &t)
Evaluate a cubic Bézier at parameter t.
Boolean operations on simple polygons (union, intersection, difference) using the Greiner-Hormann alg...
static void sort_indices_by_alpha(Array< size_t > &idx, const Array< Geom_Number > &keys)
Insertion-sort indices by a key array.
static Array< Point > extract(const Polygon &p)
Extract vertices from a polygon into an Array.
static void reverse_array(Array< Point > &v)
Reverse a vertex array in place.
static Geom_Number edge_param(const Point &P, const Point &Q, const Point &I)
Compute parameter t of point I along directed edge P→Q.
static Array< Polygon > compute_union(const Polygon &a, const Polygon &b)
Computes the union of two polygons.
Array< Polygon > difference(const Polygon &a, const Polygon &b) const
Convenience: difference (a minus b).
Array< Polygon > polygon_union(const Polygon &a, const Polygon &b) const
Convenience: union.
Array< Polygon > intersection(const Polygon &a, const Polygon &b) const
Convenience: intersection.
Array< Polygon > operator()(const Polygon &a, const Polygon &b, Op op) const
Compute a boolean operation on two simple polygons.
static bool point_inside_ccw(const Array< Point > &poly, const Point &p)
Point-in-polygon test on a CCW vertex array (winding number).
static Array< Polygon > greiner_hormann(const Array< Point > &va, const Array< Point > &vb, bool start_entering)
Run Greiner-Hormann and return result polygons.
Op
Type of Boolean operation to perform.
@ INTERSECTION
Find regions common to both polygons.
@ DIFFERENCE
Find regions in the first polygon not covered by the second.
@ UNION
Find regions covered by either polygon.
static Polygon build_poly(const Array< Point > &pts)
Build a polygon from a vertex array (bypasses colinearity merge).
static Array< Polygon > compute_difference(const Polygon &a, const Polygon &b)
Computes the difference (A \ B) of two polygons.
static void ensure_ccw(Array< Point > &v)
Ensure vertices are in CCW order.
static Array< Polygon > compute_intersection(const Polygon &a, const Polygon &b)
Computes the intersection of two polygons.
Brute force convex hull algorithm.
static SegmentSet extreme_edges(const DynList< Point > &point_set)
Finds the extreme edges of a point set (edges that form the convex hull).
Polygon operator()(const DynList< Point > &point_set) const
Compute the convex hull of a point set.
static bool are_all_points_on_left(const DynList< Point > &l, const Segment &s)
Checks if all points in a list lie to the left of a segment.
Chaikin corner-cutting subdivision for polygon smoothing.
static Array< Point > smooth_closed_once(const Array< Point > &pts, const Geom_Number &r)
static Polygon smooth_polygon(const Polygon &poly, size_t iterations, const Geom_Number &ratio=Geom_Number(1, 4))
Smooth a closed polygon.
Array< Point > operator()(const Array< Point > &polyline, size_t iterations, const Geom_Number &ratio=Geom_Number(1, 4)) const
Smooth an open polyline.
static Array< Point > smooth_open_once(const Array< Point > &pts, const Geom_Number &r)
Closest pair of points via divide and conquer.
static Result make_result(const Point &a, const Point &b)
Construct a Result object for a pair (a,b).
static Result brute_force(const Array< Point > &px, const size_t l, const size_t r)
Brute-force closest pair on a small subarray.
static Geom_Number dist2(const Point &a, const Point &b)
Squared Euclidean distance between two points.
Result operator()(const DynList< Point > &point_set) const
Compute the closest pair of points.
Segment closest_segment(DynList< Point > &point_set) const
Convenience wrapper returning the closest segment.
static Result recurse(const Array< Point > &px, const size_t l, const size_t r, Array< Point > &py)
Recursive divide-and-conquer step.
Constrained Delaunay Triangulation via Sloan's flip-based method.
static void flip_edge(Array< Tri > &tris, const size_t tri_a, const size_t tri_b)
Flip the diagonal of the quad formed by tri_a and tri_b.
static bool lexicographic_less(const Point &p1, const Point &p2)
static Array< Point > merge_and_deduplicate(const DynList< Point > &points, const DynList< Segment > &constraints)
Merge input points, constraint endpoints, and constraint-constraint intersection points; then dedupli...
static size_t adj_of(const Tri &t, const size_t n)
Return the local index of the edge shared with neighbor n.
static constexpr size_t NONE
static bool edge_exists(const Array< Tri > &tris, const size_t u, const size_t v, size_t &out_tri, size_t &out_local)
Check if edge (u,v) already exists in the mesh.
static void build_adjacency(Array< Tri > &tris, const Array< IndexedTriangle > &dt_tris)
Build adjacency mesh from DT output.
static Array< IndexedEdge > map_constraints(const Array< Point > &sites, const DynList< Segment > &constraints)
Split constraints at interior collinear points.
static bool is_convex_quad(const Array< Point > &pts, const size_t a, const size_t b, const size_t c, const size_t d)
True if diagonal (a,d) in quad (a,b,d) + (a,d,c) forms a convex quad.
static void lawson_flip(const Array< Point > &pts, Array< Tri > &tris)
Lawson flip pass: restore Delaunay for non-constrained edges.
static void mark_constrained(Array< Tri > &tris, const size_t u, const size_t v)
Mark edge (u,v) as constrained in the mesh.
static void enforce_constraint(Array< Point > &pts, Array< Tri > &tris, const size_t u, const size_t v)
Enforce a single constraint edge (u,v) using Sloan's flip approach.
static DynList< Triangle > as_triangles(const Result &result)
Convert indexed triangulation to geometric triangles.
static size_t find_point_index(const Array< Point > &pts, const Point &p)
Find index of point p in sorted unique array, or NONE.
static size_t local_of(const Tri &t, const size_t id)
Return local index (0,1,2) of vertex id in triangle t, or NONE.
static Array< CrossingEdge > find_crossing_edges(const Array< Point > &pts, const Array< Tri > &tris, const size_t u, const size_t v)
static size_t edge_opposite(const Tri &t, const size_t a, const size_t b)
Return the local edge index for edge (a,b) in triangle t.
Decompose a simple polygon into convex parts using Hertel-Mehlhorn.
static Array< Point > extract_vertices(const Polygon &p)
static size_t find_pos(const Array< size_t > &face, size_t v)
Array< Polygon > operator()(const Polygon &poly) const
Decompose polygon poly into convex parts.
static bool can_merge(const Array< Point > &pts, const Array< size_t > &f1, const Array< size_t > &f2, size_t u, size_t v)
Check whether merging faces f1 and f2 across diagonal (u,v) is convex.
static Array< size_t > merge_faces(const Array< size_t > &f1, const Array< size_t > &f2, size_t u, size_t v)
Merge f1 and f2 by removing their shared edge (u,v).
static constexpr size_t NONE
static bool is_polygon_edge(size_t u, size_t v, size_t n)
Distance between two closed convex polygons using GJK.
Result operator()(const Polygon &p, const Polygon &q) const
Compute the distance between two closed convex polygons.
static Result exact_refine_disjoint(const Polygon &p, const Polygon &q, const Array< Point > &pv, const Array< Point > &qv)
static double norm2(const double x, const double y) noexcept
static ClosestSimplexResult closest_from_simplex(const std::vector< SupportPoint > &simplex)
static constexpr size_t kMaxIters
static double dot2(const double ax, const double ay, const double bx, const double by) noexcept
static Point point_from_double(const double x, const double y)
static bool polygons_intersect_or_contain(const Polygon &p, const Polygon &q)
static SupportPoint support(const Array< Point > &p, const Array< Point > &q, const double dx, const double dy)
static Point lerp_point(const Point &a, const Point &b, const double t)
static double to_double(const Geom_Number &v)
static Point centroid_of(const Array< Point > &v)
static constexpr double kEps
Basic exact intersection for closed convex polygons.
Polygon operator()(const Polygon &subject, const Polygon &clip) const
Intersect two closed convex polygons.
static Array< Point > normalize_vertices(const Array< Point > &pts)
static Point line_intersection(const Point &s, const Point &e, const Point &a, const Point &b)
static Geom_Number signed_double_area(const Array< Point > &verts)
static bool inside_half_plane(const Point &p, const Point &a, const Point &b, const bool clip_ccw)
static Array< Point > extract_vertices(const Polygon &poly)
static void push_clean(Array< Point > &out, const Point &p)
static Polygon build_polygon(const Array< Point > &pts)
static bool is_convex(const Array< Point > &verts)
Inward (erosion) and outward (dilation) offset of convex polygons.
static Point line_intersect(const Point &a1, const Point &a2, const Point &b1, const Point &b2)
Line-line intersection of (a1,a2) and (b1,b2).
static bool is_convex(const Array< Point > &v)
static void ensure_ccw(Array< Point > &v)
static Geom_Number signed_double_area(const Array< Point > &v)
static void offset_edge(const Point &a, const Point &b, const Geom_Number &d, const bool inward, Point &oa, Point &ob)
Compute two points on the offset line of edge (a, b) moved by distance d along its outward (or inward...
static Polygon outward(const Polygon &convex_poly, const Geom_Number &distance)
Outward offset (dilation) of a convex polygon.
static Array< Point > extract_verts(const Polygon &poly)
static Polygon inward(const Polygon &convex_poly, const Geom_Number &distance)
Inward offset (erosion) of a convex polygon.
Polygon triangulation using the ear-cutting algorithm.
static Geom_Number signed_double_area(const Polygon &p)
static Array< Point > extract_vertices(const Polygon &p)
static bool diagonal(const Polygon &p, const Vertex &a, const Vertex &b)
Check if segment (a, b) is a valid internal diagonal.
static EarsSet init_ears(const Polygon &p)
Initialize the set of ear vertices.
static void normalize_to_ccw(Polygon &p)
static bool in_cone(const Polygon &p, const Vertex &a, const Vertex &b)
Check if vertex b is inside the cone formed at vertex a.
static bool diagonalize(const Polygon &p, const Segment &s)
Check if a segment is a valid diagonal of the polygon.
DynList< Triangle > operator()(const Polygon &poly) const
Triangulate the polygon.
Exact Delaunay triangulation using the Bowyer-Watson incremental algorithm.
static bool all_collinear(const Array< Point > &pts)
Check if all points in an array are collinear.
Result operator()(const DynList< Point > &point_set) const
Compute Delaunay triangulation of a point set.
static bool point_in_circumcircle(const Array< Point > &pts, const size_t ia, const size_t ib, const size_t ic, const size_t ip)
Predicate to check if a point lies inside the circumcircle of a triangle.
static DynList< Triangle > as_triangles(const Result &result)
Convert indexed triangulation to geometric triangles.
static Array< Point > unique_points(const DynList< Point > &point_set)
Extracts unique points from a list and sorts them lexicographically.
static bool lexicographic_less(const Point &p1, const Point &p2)
Lexicographical comparison for points.
O(n log n) expected-time Delaunay's triangulation.
static bool in_cc(const Array< Point > &pts, const size_t ia, const size_t ib, const size_t ic, const size_t ip)
Standard in-circumcircle test using exact in_circle_determinant.
static constexpr size_t NONE
static void remap_adj(Array< Tri > &tris, const size_t ot, const size_t nt)
Update neighbor n of the old triangle ot to point to a new triangle nt.
static bool point_in_tri(const Array< Point > &pts, const Tri &t, const size_t pidx)
True if point p is inside or on triangle t (using orientation tests).
static size_t adj_of(const Tri &t, const size_t n)
Return the local index of the edge shared with neighbor n.
static size_t locate(const Array< Point > &pts, const Array< Tri > &tris, const Array< DagNode > &dag, const size_t pidx, const size_t root)
Locate the leaf triangle containing point pidx via a DAG walk.
static size_t local_of(const Tri &t, const size_t id)
Return local index (0,1,2) of vertex id in triangle t, or NONE.
Result operator()(const DynList< Point > &point_set) const
Computes the Delaunay triangulation of a set of points.
bool has_curr() const noexcept
Return true if the iterator has current item.
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
Douglas-Peucker polyline simplification.
static void dp_recurse(const Array< Point > &pts, const size_t first, const size_t last, const Geom_Number &eps2, Array< bool > &keep)
Polygon simplify_polygon(const Polygon &poly, const Geom_Number &epsilon) const
Simplify a closed polygon.
Array< Point > operator()(const Array< Point > &polyline, const Geom_Number &epsilon) const
Simplify an open polyline.
Dynamic heap of elements of type T ordered by a comparison functor.
T get()
Alias for getMin().
T & put(const T &item)
Synonym of insert().
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
T & get_first()
Return a modifiable reference to first item in the list.
T remove_first()
Remove the first item of the list; return a copy of removed item.
T & append(const T &item)
Append a copied item at the end of the list.
T & get_last()
Return a modifiable reference to last item in the list.
T remove_last()
Remove the last item of the list; return a copy of removed item.
Iterator on the items of list.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
void concat(DynList &l) noexcept
Append the items of l to this, in constant time; l becomes empty.
T & get_last()
Return the last item of the list.
T remove_first()
See remove().
Dynamic set backed by balanced binary search trees with automatic memory management.
long position(const Key &key) const
Returns the infix (ordered) position of the key.
Key remove_pos(const size_t i)
Removes the key at the inorder position i.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * search(const Key &key)
Find an element in the set.
bool is_empty() const
returns true if the set is empty
Very simple queue implemented with a contiguous array.
T & put(const T &item) noexcept
Put an item into the queue by copy.
constexpr bool is_empty() const noexcept
Return true if the queue is empty.
T get() noexcept
Remove the oldest item of the queue.
size_t size() const noexcept
Return the number of elements stored in the stack.
bool is_empty() const noexcept
Return true if stack is empty.
T pop() noexcept
Pop by moving the top of stack.
T & top() noexcept
Return a modifiable reference to stack's top.
T & push(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
Push a copy of data
bool is_empty() const noexcept
Shared Bowyer-Watson core parameterized by in-circle predicate.
static Array< IndexedTriangle > triangulate(Array< Point > pts, const size_t n, InCirclePredicate point_in_circumcircle)
static void toggle_edge(EdgeSet &boundary, size_t u, size_t v)
Shared polygon helpers used across multiple geometry algorithms.
static Geom_Number area(const Array< Point > &verts)
Compute the absolute area of a vertex array.
static Geom_Number signed_double_area(const Polygon &poly)
Compute twice the signed area of a polygon.
static Geom_Number signed_area(const Polygon &poly)
Compute the actual signed area of a polygon.
static bool is_convex(const Array< Point > &verts)
Check if a vertex array forms a convex polygon.
static Geom_Number signed_double_area(const Array< Point > &verts)
Compute twice the signed area (shoelace formula without division).
static void ensure_ccw(Array< Point > &verts)
static Geom_Number signed_area(const Array< Point > &verts)
Compute the actual signed area of a vertex array.
static Geom_Number area(const Polygon &poly)
Compute the absolute area of a polygon.
static Array< Point > extract_vertices(const Polygon &poly)
Extract vertices from a polygon into an array for indexed access.
Serialization utilities for geometry objects.
static std::string to_geojson(const Triangle &t)
Triangle → GeoJSON Polygon.
static std::string to_wkt(const Point3D &p)
Converts a Point3D to WKT format: "POINT Z (x y z)".
static std::string to_geojson(const Polygon &poly)
Polygon → GeoJSON Polygon.
static std::string to_wkt(const Polygon &poly)
Converts a Polygon to WKT format: "POLYGON ((x1 y1, x2 y2, ..., x1 y1))".
static std::string to_wkt(const Rectangle &r)
Converts a Rectangle to WKT format: "POLYGON ((xmin ymin, xmax ymin, xmax ymax, xmin ymax,...
static std::string to_wkt(const Triangle &t)
Converts a Triangle to WKT format: "POLYGON ((x1 y1, x2 y2, x3 y3, x1 y1))".
static std::string dbl(const Geom_Number &n)
Formats a Geom_Number as a string with high precision.
static std::string to_geojson(const Segment &s)
Converts a Segment to a GeoJSON LineString.
static std::string to_wkt(const Segment &s)
Converts a Segment to WKT format: "LINESTRING (x1 y1, x2 y2)".
static std::string to_geojson(const Point &p)
Converts a Point to a GeoJSON geometry object.
static std::string to_wkt(const Point &p)
Converts a Point to WKT format: "POINT (x y)".
static std::string to_geojson(const Point3D &p)
Point3D → GeoJSON Point with Z.
Shared edge-group extraction for triangle meshes.
static void append_edge(Array< EdgeRef > &edges, size_t a, size_t b, const size_t tri, const size_t third)
static void for_each_sorted_edge_group(const TriangleArray &triangles, GroupFn on_group)
Gift wrapping (Jarvis march) convex hull algorithm.
static const Point * get_lowest_point(const DynList< Point > &point_set)
Finds the point with the minimum y-coordinate (and minimum x on ties).
Polygon operator()(const DynList< Point > &point_set) const
Compute the convex hull of a point set.
Graham scan convex hull algorithm.
static Array< Point > sorted_unique_points(const DynList< Point > &point_set)
Extracts points from a list, sorts them and removes duplicates.
static Geom_Number turn(const Point &a, const Point &b, const Point &c)
Computes the turn orientation of triple (a, b, c).
Polygon operator()(const DynList< Point > &point_set) const
Compute the convex hull of a point set.
constexpr bool is_empty() const noexcept
Exact bounded intersection of half-planes.
static Point line_intersection(const HalfPlane &a, const HalfPlane &b)
static Geom_Number cross_dir(const HalfPlane &a, const HalfPlane &b)
static Geom_Number dot_dir(const HalfPlane &a, const HalfPlane &b)
static Polygon build_polygon(const Array< Point > &pts)
static bool parallel(const HalfPlane &a, const HalfPlane &b)
static bool upper_half(const HalfPlane &h)
static Geom_Number signed_double_area(const Array< Point > &verts)
static bool same_direction(const HalfPlane &a, const HalfPlane &b)
static void push_clean(Array< Point > &out, const Point &p)
static Array< HalfPlane > from_convex_polygon(const Polygon &poly)
Build half-planes from the edges of a closed convex polygon.
static Array< Point > normalize_vertices(const Array< Point > &pts)
Polygon operator()(const Array< HalfPlane > &halfplanes) const
Intersect half-planes and return bounded feasible polygon.
Spatial point index for O(log n) nearest-neighbor queries.
DebugSnapshot debug_snapshot() const
Build a deterministic geometric partition snapshot for visualization.
Rectangle extended_bounds() const
The bounds, extended to the points outside them, as the tree extends its region to them.
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.
static bool lexicographic_less(const Point &a, const Point &b)
KDTreePointSearch(const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax)
Construct an empty KD-tree for the given bounding region.
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.
static Array< Point > unique_points(Array< Point > points)
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).
Reusable event-driven sweep line framework.
DynSetTree< SeqEvent, Avl_Tree, CmpSeqEvent > queue_
void enqueue(Event &&e)
Enqueue an event (move version).
bool has_events() const noexcept
True if the event queue is non-empty.
Event dequeue()
Remove and return the next (minimum) event.
void enqueue(const Event &e)
Enqueue an event.
void run(Handler &&handler)
Run the sweep: process every event through handler.
size_t pending() const noexcept
Number of pending events.
void run(Handler &&handler, Array< Event > &out)
Run the sweep, collecting every event into out.
const Event & peek() const
Peek at the next event without removing it.
void clear() noexcept
Discard all pending events.
Smallest circle enclosing a point set (Welzl's algorithm).
Circle operator()(const DynList< Point > &points) const
Compute the minimum enclosing circle of a point set.
static Circle from_two_points(const Point &a, const Point &b)
Smallest circle with a and b on its boundary (diameter).
static Circle from_one_point(const Point &a)
Circle through a single point (degenerate, radius = 0).
static Circle mec_with_two_points(const Array< Point > &pts, size_t n, const Point &p1, const Point &p2)
Minimum enclosing circle with boundary points p1 and p2.
static Circle welzl_iterative(const Array< Point > &pts)
Iterative Welzl: three nested loops.
static Circle from_three_points(const Point &a, const Point &b, const Point &c)
Circumscribed circle through three points.
static Circle mec_with_point(const Array< Point > &pts, size_t n, const Point &p1)
Minimum enclosing circle with boundary point p1.
Exact Minkowski sum of two closed convex polygons.
static Array< Point > normalize(Array< Point > v)
Ensure CCW and rotate so that the bottom-most vertex is first.
static Array< Point > extract_vertices(const Polygon &poly)
static Geom_Number signed_double_area(const Array< Point > &v)
Polygon operator()(const Polygon &P, const Polygon &Q) const
Compute the Minkowski sum of two convex polygons.
static Point edge_vec(const Array< Point > &v, const size_t i)
static bool is_convex(const Array< Point > &verts)
static Geom_Number cross(const Point &a, const Point &b)
size_t left_edge_of_point(const Point &p, const Geom_Number &sweep_y) const
static void split_by_label(Node *root, const Geom_Number &label, Node *&left, Node *&right)
size_t successor_for_insert(const size_t edge, const Geom_Number &sweep_y) const
const Array< Point > & verts_
bool contains(const size_t edge) const
static Node * merge(Node *a, Node *b)
static Node * insert_by_label(Node *root, Node *node)
void erase(const size_t edge)
Geom_Number fresh_label_between(const size_t pred, const size_t succ) const
size_t predecessor_for_insert(const size_t edge, const Geom_Number &sweep_y) const
void insert(const size_t edge, const Geom_Number &sweep_y)
static Node * erase_by_label(Node *root, const Geom_Number &label, Node *&removed)
EdgeStatusTree(const Array< Point > &verts)
static void destroy(const Node *n)
O(n log n) triangulation of simple polygons via y-monotone partition + linear-time monotone triangula...
static Geom_Number signed_double_area(const Array< Point > &v)
static void ensure_ccw(Array< Point > &v)
static bool is_below(const Point &a, const Point &b)
Lexicographical "below" comparison.
static bool is_above(const Point &a, const Point &b)
Lexicographical "above" comparison (y primary, x secondary).
static bool edge_status_less(const size_t lhs, const size_t rhs, const Geom_Number &sweep_y, const Array< Point > &verts)
static Array< Array< size_t > > build_faces_from_diagonals(const Array< Point > &verts, const DynSetTree< std::pair< size_t, size_t > > &diagonals)
VertexType
Classification of vertices for y-monotone decomposition.
static bool is_y_monotone(const Array< Point > &v)
Check if a CCW polygon vertex array is y-monotone.
static Array< Array< size_t > > decompose_to_monotone_faces(const Array< Point > &verts)
DynList< Triangle > operator()(Polygon p) const
Triangulate a simple polygon.
static DynList< Triangle > triangulate_monotone(const Array< Point > &verts)
Triangulate a y-monotone polygon given as a vertex array.
static bool edge_goes_down(const Array< Point > &verts, const size_t edge)
Check if the edge starting at edge points downwards.
static VertexType classify_vertex(const Array< Point > &v, const size_t i)
static Array< Point > extract_vertices(const Polygon &p)
static Geom_Number edge_x_at_y(const Array< Point > &verts, const size_t edge, const Geom_Number &y)
static bool regular_interior_right(const Array< Point > &v, const size_t i)
Number Theoretic Transform over Z / MOD Z.
Represents a point in 3D space with exact rational coordinates.
const Geom_Number & get_z() const
Gets the z-coordinate.
const Geom_Number & get_x() const
Gets the x-coordinate.
const Geom_Number & get_y() const
Gets the y-coordinate.
Exact point-in-polygon classification via winding number.
static bool strictly_contains(const Polygon &poly, const Point &p)
Return true only for strict interior points.
static Location locate(const Polygon &poly, const Point &p)
Classify point location with respect to a polygon.
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.
Point midpoint(const Point &other) const
Calculates the midpoint between this point and another.
bool is_to_left_on_from(const Point &p1, const Point &p2) const
Checks if this point is to the left of or on the line from p1 to p2.
bool is_right_of(const Segment &s) const
Checks if this point is to the right of a directed segment.
bool is_left_of(const Point &p1, const Point &p2) const
Checks if this point is to the left of the directed line from p1 to p2.
Geom_Number distance_squared_to(const Point &that) const
Calculates the squared Euclidean distance to another point.
Offset (inflate/deflate) an arbitrary simple polygon.
static Result cleanup(const Array< Point > &raw)
Full cleanup pipeline.
Result operator()(const Polygon &poly, const Geom_Number &distance, const JoinType join=JoinType::Miter, const Geom_Number &miter_limit=Geom_Number(2)) const
Offset a simple polygon by the given distance.
static Array< Polygon > extract_contours(Array< AugVertex > &aug)
Walk the augmented polygon and extract valid CCW contours.
JoinType
Strategy for connecting offset edges at vertices.
@ Bevel
Connect edge endpoints with a straight line.
@ Miter
Extend edges until they intersect.
static void sort_by_alpha(Array< size_t > &idx, const Array< Geom_Number > &keys)
Insertion-sort indices by alpha key.
Polygon offset_polygon(const Polygon &poly, const Geom_Number &distance, JoinType join=JoinType::Miter, const Geom_Number &miter_limit=Geom_Number(2)) const
Convenience: return the single largest-area result polygon.
static Polygon build_poly(const Array< Point > &pts)
Build a polygon from a point array.
static void offset_edge(const Point &a, const Point &b, const Geom_Number &d, const bool inward, Point &oa, Point &ob)
Compute two points on the offset line of edge (a→b) shifted by |d| along its outward or inward normal...
static Array< Point > compute_raw_offset(const Array< Point > &v, const Geom_Number &distance, const JoinType join, const Geom_Number &miter_limit)
static Array< Intersection > find_self_intersections(const Array< Point > &raw)
O(n²) scan for all proper self-intersections.
static Geom_Number abs_area(const Polygon &p)
static Array< AugVertex > build_augmented(const Array< Point > &raw, const Array< Intersection > &isects)
Build the augmented vertex list with crossing links.
static Point line_intersect(const Point &a1, const Point &a2, const Point &b1, const Point &b2)
Line–line intersection of (a1,a2) and (b1,b2).
static Geom_Number sq_dist(const Point &a, const Point &b)
Compute the squared distance between two points.
static Geom_Number edge_param(const Point &P, const Point &Q, const Point &I)
Compute parameter t of point I along directed edge P→Q.
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.
const Vertex & get_next_vertex(const Vertex &v) const
Get the vertex following v in the polygon.
const Vertex & get_prev_vertex(const Vertex &v) const
Get the vertex preceding v in the polygon.
void remove_vertex(const Vertex &v)
Remove a vertex from the polygon.
PointLocation locate_point(const Point &p) const
Classify a point against this closed polygon.
const Vertex & get_first_vertex() const
Get the first vertex of the polygon.
const Vertex & get_last_vertex() const
Get the last vertex of the polygon.
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.
Power diagram (weighted Voronoi diagram).
static Point power_center(const WeightedSite &a, const WeightedSite &b, const WeightedSite &c)
Compute the power center of three weighted sites.
Result operator()(const Array< WeightedSite > &sites) const
Compute the power diagram.
QuickHull convex hull algorithm.
static std::pair< DynList< Point >, DynList< Point > > partition(const DynList< Point > &point_set, const Point &a, const Point &b)
Split points by the line through directed segment (a,b).
Polygon operator()(const DynList< Point > &point_set) const
Compute the convex hull of a point set.
static std::pair< Point, Point > search_extremes(const DynList< Point > &point_set)
Find leftmost and rightmost points by x coordinate.
static DynList< Point > quick_hull(DynList< Point > &point_set, const Point &a, const Point &b)
Recursive QuickHull step for a directed edge (a,b).
static std::pair< DynList< Point >, DynList< Point > > get_right_points(DynList< Point > &point_set, const Point &a, const Point &b, const Point &c)
Partition points to the right of edges (a,c) and (c,b).
static Point get_farthest_point(const DynList< Point > &point_set, const Segment &s)
Return the point in point_set farthest from segment s.
Static 2D range tree for orthogonal range queries.
void build_node(const size_t node, const size_t lo, const size_t hi)
RangeTree2D(const RangeTree2D &other)=default
Copy the points and the tree of another range tree.
void swap(RangeTree2D &other) noexcept
Exchange in O(1) the points and the tree of this range tree and another.
bool is_empty() const noexcept
DynList< Point > query(const Geom_Number &xmin, const Geom_Number &xmax, const Geom_Number &ymin, const Geom_Number &ymax) const
Query: return all points inside [xmin,xmax] × [ymin,ymax].
Array< Point > pts_
points sorted by x (primary key)
Array< Node > tree_
implicit binary tree (1-indexed)
size_t size() const noexcept
RangeTree2D(RangeTree2D &&other) noexcept
Take the points and the tree of another range tree, which is left empty.
size_t lower_bound_x(const Geom_Number &xval) const
Binary search: first index i in pts_ where pts_(i).get_x() >= xval.
static size_t lower_bound_y(const Array< Point > &arr, const Geom_Number &ymin)
Binary search: first index i in arr where arr(i).get_y() >= ymin.
void query_range(const size_t node, const size_t lo, const size_t hi, const size_t qlo, const size_t qhi, const Geom_Number &ymin, const Geom_Number &ymax, DynList< Point > &out) const
size_t upper_bound_x(const Geom_Number &xval) const
Binary search: first index i in pts_ where pts_(i).get_x() > xval.
RangeTree2D & operator=(const RangeTree2D &other)=default
Copy the points and the tree of another range tree.
void build(const DynList< Point > &points)
Build the range tree from a point set.
RangeTree2D & operator=(RangeTree2D &&other) noexcept
Exchange this range tree with another, which is left with the previous points of this one.
static size_t upper_bound_y(const Array< Point > &arr, const Geom_Number &ymax)
Binary search: first index i in arr where arr(i).get_y() > ymax.
DebugSnapshot debug_snapshot() const
Return structural snapshot for visualization/debug.
RangeTree2D()=default
Build an empty range tree; build() fills it.
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.
Exact regular triangulation (weighted Delaunay) via Bowyer-Watson.
static bool all_collinear(const Array< WeightedSite > &s)
static bool point_in_power_circle(const Array< Point > &pts, const Array< Geom_Number > &wts, const size_t ia, const size_t ib, const size_t ic, const size_t ip)
Weighted in-circle (power) predicate.
static bool lex_less(const Point &p1, const Point &p2)
static Array< WeightedSite > unique_sites(const Array< WeightedSite > &input)
Deduplicates and sorts an array of weighted sites.
Result operator()(const Array< WeightedSite > &weighted_sites) const
Compute the regular triangulation of a weighted point set.
Rotating calipers metrics for convex polygons.
static DiameterResult make_diameter(const Point &a, const Point &b)
static Array< Point > extract_vertices(const Polygon &poly)
Extract polygon vertices into an array.
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.
static bool is_convex(const Array< Point > &verts)
Check if a polygon vertex cycle is convex.
Compute the full planar subdivision induced by a set of segments.
static size_t find_vertex(const Array< Point > &verts, const Point &p)
Find the index of point p in a sorted vertex array (binary search).
static bool angle_lt(const Geom_Number &dx1, const Geom_Number &dy1, const Geom_Number &dx2, const Geom_Number &dy2)
Compare two directions from the same origin by angle (exact).
static constexpr size_t NONE
static int angle_quad(const Geom_Number &dx, const Geom_Number &dy)
Angular quadrant of direction (dx, dy).
Result operator()(const Array< Segment > &segments) const
Compute the arrangement of a set of segments.
static bool pt_less(const Point &a, const Point &b)
Lexicographic point comparison.
Dedicated exact intersection for a single pair of segments.
Kind
Types of intersection between two segments.
@ OVERLAP
Intersection over a collinear interval.
@ POINT
Intersection at a single point.
static Segment collinear_overlap(const Segment &s1, const Segment &s2)
static Point point_max_on_axis(const Point &a, const Point &b, const bool vertical_axis)
static bool point_less_on_axis(const Point &a, const Point &b, const bool vertical_axis)
Result operator()(const Segment &s1, const Segment &s2) const
Computes the intersection of two line segments.
static Point point_min_on_axis(const Point &a, const Point &b, const bool vertical_axis)
Represents a line segment between two points.
bool intersects_properly_with(const Segment &s) const
Checks if this segment properly intersects another segment.
Point intersection_with(const Segment &s) const
Computes the intersection point of the infinite lines defined by two segments.
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...
Geom_Number distance_to(const Point &p) const
Calculates the Euclidean distance from a point to this segment.
bool contains(const Point &p) const
Checks if a point lies on this segment.
const Point & get_tgt_point() const noexcept
Gets the target point of the segment.
bool is_parallel_with(const Segment &s) const
Checks if this segment is parallel to another one.
const Point & get_src_point() const noexcept
Gets the source point of the segment.
Compute the shortest Euclidean path between two points inside a simple polygon.
static size_t find_tri(const Array< Point > &pts, const Array< ITri > &tris, const Point &p)
Find a triangle containing point p.
static constexpr size_t NONE
static Array< size_t > find_sleeve(const Array< ITri > &tris, const size_t src, const size_t dst)
BFS on dual graph → sleeve.
static Array< ITri > build_tris(const Array< Point > &pts, const DynList< Triangle > &tl)
Build indexed triangulation with adjacency.
static Geom_Number cross(const Point &a, const Point &b, const Point &c)
Cross product (b-a) x (c-a).
DynList< Point > operator()(const Polygon &polygon, const Point &source, const Point &target) const
Compute the shortest path between two points in a simple polygon.
static bool point_in_triangle(const Array< Point > &pts, const ITri &t, const Point &p)
Point-in-triangle test.
static size_t find_index(const Array< Point > &pts, const Point &p)
Match a Point from a Triangle to an index in pts (exact comparison).
Sweep-line status as a balanced tree with O(log n) updates.
size_t predecessor_for_insert(const size_t seg, const Geom_Number &sx) const
StatusTree(const Array< Segment > &segs)
bool contains(const size_t seg) const
static Node * insert_by_label(Node *root, Node *node)
Geom_Number fresh_label_between(const size_t pred, const size_t succ) const
static void destroy(const Node *n)
size_t predecessor(const size_t seg) const
static Node * merge(Node *a, Node *b)
size_t successor(const size_t seg) const
void insert(const size_t seg, const Geom_Number &sx)
size_t successor_for_insert(const size_t seg, const Geom_Number &sx) const
static void split_by_label(Node *root, const Geom_Number &label, Node *&left, Node *&right)
void erase(const size_t seg)
static Node * erase_by_label(Node *root, const Geom_Number &label, Node *&removed)
const Array< Segment > & segs_
Report all pairwise intersection points among a set of segments.
static bool status_less(const size_t lhs, const size_t rhs, const Geom_Number &sx, const Array< Segment > &segs)
static void check_and_enqueue(const Array< Segment > &segs, const size_t i, const size_t j, const Geom_Number &sx, LineSweepFramework< Event, EventLess > &eq, DynSetTree< size_t, Treap_Rk > &seen_pairs, const size_t n)
Detect an intersection and enqueue it as a future event.
static bool slope_less(const Segment &a, const Segment &b)
static Segment canonicalize(const Segment &s)
Canonicalize a segment so its src is the "left" endpoint.
Array< Intersection > operator()(const Array< Segment > &segments) const
Find all pairwise segment intersection points.
static bool event_less(const Event &a, const Event &b)
Ordering for events in the sweep queue.
static Geom_Number y_at_x(const Segment &s, const Geom_Number &x)
Evaluate the y-coordinate of segment s at x = x.
EventType
Types of events in the Bentley-Ottmann sweep.
O(log n) point location via trapezoidal map with DAG search.
static void split_multiple_trapezoids(Array< Point > &pts, Array< Segment > &segs, Array< Trapezoid > &traps, Array< DagNode > &dag, const size_t seg_idx, const Array< size_t > &crossed)
Handle the case where a segment crosses multiple trapezoids.
LocationType
Classification of a point location query result.
@ TRAPEZOID
The point is strictly inside a trapezoid.
@ ON_POINT
The point coincides with a segment endpoint.
@ ON_SEGMENT
The point lies on a segment boundary.
static constexpr size_t NONE
static void update_neighbor(Array< Trapezoid > &traps, const size_t neighbor_idx, const size_t old_ti, const size_t new_ti)
Update all neighbor pointers that referenced old_ti to point to new_ti.
static size_t dag_locate(const Array< Point > &pts, const Array< Segment > &segs, const Array< DagNode > &dag, const Point &p, size_t root)
Locate the DAG leaf (trapezoid) containing point p.
static Array< size_t > find_crossed_trapezoids(const Array< Point > &pts, const Array< Segment > &segs, const Array< Trapezoid > &traps, const size_t seg_idx, const size_t start_trap)
Find all trapezoids crossed by segment seg_idx, starting from the trapezoid containing the left endpo...
static bool point_above_segment(const Point &p, const Segment &seg)
Test if point p is above segment seg (left of the directed segment).
static size_t make_trapezoid(Array< Trapezoid > &traps, Array< DagNode > &dag, const size_t top, const size_t bottom, const size_t leftp, const size_t rightp)
Create a new trapezoid and a DAG leaf for it.
Result operator()(const Array< Segment > &input_segments) const
Build a trapezoidal map from a set of non-crossing segments.
static void split_single_trapezoid(Array< Point > &pts, Array< Segment > &segs, Array< Trapezoid > &traps, Array< DagNode > &dag, const size_t seg_idx, const size_t trap_idx)
Handle the case where a segment is fully contained in one trapezoid.
static void replace_leaf(Array< DagNode > &dag, const size_t leaf_idx, const NodeType type, const size_t index, const size_t left, const size_t right)
Replace a DAG leaf with an internal node (X or Y).
NodeType
Types of nodes in the search DAG.
@ X_NODE
A node that splits the search space by an X-coordinate.
@ LEAF
A leaf node representing a trapezoid.
@ Y_NODE
A node that splits the search space by a segment (Y-direction).
A non-degenerate triangle defined by three points.
const Point & get_p3() const
Gets the third vertex.
const Point & get_p2() const
Gets the second vertex.
const Point & get_p1() const
Gets the first vertex.
A vertex in a polygon's doubly linked vertex list.
const Vertex & next_vertex() const
Get the next vertex in the polygon.
Point to_point() const
Return this vertex as a plain Point value.
BST-based sweep status for active edges, ordered by ray-intersection distance.
void erase(const size_t edge)
size_t min() const
Return the nearest edge (smallest ray_param).
DynSetTree< EdgeKey, Treap, EdgeKeyCmp > tree_
EdgeStatusTree(const Array< Point > &verts, const size_t n, const Point &query)
bool contains(const size_t edge) const
const Array< Point > & verts_
Array< Geom_Number > params_
void insert(const size_t edge, const Point &dir)
Compute the visibility polygon from a point inside a simple polygon.
static bool angle_less(const Point &q, const Point &a, const Point &b)
True if direction (a - q) has a smaller angle than (b - q).
static Point ray_edge_hit(const Point &q, const Point &dir, const Point &e0, const Point &e1)
Intersection point of ray from q through dir with edge (e0, e1).
Polygon operator()(const Polygon &polygon, const Point &query) const
Compute the visibility polygon.
static int angle_quadrant(const Geom_Number &dx, const Geom_Number &dy)
Quadrant of direction (dx, dy): 0 = +x+y, 1 = -x+y, 2 = -x-y, 3 = +x-y.
static Geom_Number ray_param(const Point &q, const Point &dir, const Point &e0, const Point &e1)
Parametric t along ray q + t*(dir-q) for intersection with edge (e0,e1).
Visvalingam-Whyatt polyline simplification.
static Array< Point > simplify_open_array(const Array< Point > &polyline, const Geom_Number &area_threshold)
Array< Point > operator()(const Array< Point > &polyline, const Geom_Number &area_threshold) const
Simplify an open polyline.
static Geom_Number effective_area(const Point &a, const Point &b, const Point &c)
static Polygon simplify_polygon(const Polygon &poly, const Geom_Number &area_threshold)
Simplify a closed polygon.
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.
VoronoiDiagram voronoi_
The diagram that it computes.
Array< ClippedCell > clipped_cells(const std::initializer_list< Point > il, const Polygon &clip) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Voronoi diagram derived as the dual of a Delaunay triangulation.
static Array< ClippedCell > indexed_clipped_cells(const Array< Point > &sites, const Array< Polygon > &polys)
Array< ClippedCell > clipped_cells_indexed(const std::initializer_list< Point > il, const Polygon &clip) const
Convenience overload with initializer-list input.
static Point circumcenter(const Point &a, const Point &b, const Point &c)
Computes the circumcenter of three points.
Result operator()(const DelaunayTriangulationBowyerWatson::Result &dt) const
Build Voronoi from a precomputed Delaunay triangulation.
static HalfPlaneIntersection::HalfPlane bisector_halfplane_for_site(const Point &s, const Point &t)
Creates a half-plane from a directed edge.
static bool is_convex(const Array< Point > &verts)
Checks if a vertex set forms a convex polygon.
static Array< ClippedCell > clipped_cells_indexed(const Array< Point > &sites, const Polygon &clip)
Clip Voronoi cells and return explicit site-indexed records.
Array< Polygon > clipped_cells(const std::initializer_list< Point > il, const Polygon &clip) const
Convenience overload with initializer-list input.
Array< ClippedCell > clipped_cells_indexed(const DynList< Point > &point_set, const Polygon &clip) const
Compute/clip Voronoi cells and return site-indexed records.
static Array< Polygon > clipped_cells(const Array< Point > &sites, const Polygon &clip)
Clip Voronoi cells against a closed convex polygon.
static Array< ClippedCell > clipped_cells_indexed(const Result &vor, const Polygon &clip)
Clip Voronoi cells (from result) into site-indexed records.
static Array< Point > extract_vertices(const Polygon &p)
Extracts vertices from a closed polygon.
Array< Polygon > clipped_cells(const DynList< Point > &point_set, const Polygon &clip) const
Compute Voronoi and clip its cells against a closed convex polygon.
DelaunayTriangulationBowyerWatson delaunay
Internal Delaunay triangulator.
static Array< Polygon > clipped_cells(const Result &vor, const Polygon &clip)
Clip Voronoi cells against a closed convex polygon.
O(n log n) Voronoi diagram construction.
VoronoiDiagramFromDelaunay voronoi_
Internal dual builder.
VoronoiDiagramFromDelaunay::Result Result
Complete result structure.
VoronoiDiagramFromDelaunay::Cell Cell
Type for Voronoi cells.
Array< ClippedCell > clipped_cells(const DynList< Point > &pts, const Polygon &clip) const
Compute Voronoi cells clipped to a bounding polygon.
DelaunayTriangulationRandomizedIncremental delaunay_
Internal Delaunay triangulator.
VoronoiDiagramFromDelaunay::Edge Edge
Type for Voronoi edges.
Result operator()(const DynList< Point > &pts) const
Compute the Voronoi diagram for a list of points.
VoronoiDiagramFromDelaunay::ClippedCell ClippedCell
Type for clipped Voronoi cells.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
2D k-d tree for efficient spatial point operations.
bool insert(const Point &p)
Insert a point into the tree.
constexpr size_t size() const noexcept
Get the number of points in the tree.
static void range(Node *root, const Rectangle &rect, DynList< Point > *q)
Recursively find points within a rectangle.
void for_each(Op &&op) const
Apply an operation to every point in the tree (inorder).
static K2Tree build(Array< Point > points, const Point &pmin, const Point &pmax)
Build a balanced k-d tree from an array of points.
bool contains(const Point &p) const noexcept
Check if the tree contains a point.
std::optional< Point > nearest(const Point &p) const noexcept
Find the nearest point to a query point.
constexpr bool is_empty() const noexcept
Check if the tree is empty.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, Type > Type * find_ptr(Operation &operation) noexcept(mutable_search_is_noexcept< Operation >())
Find a pointer to an item in the container according to a searching criterion.
bool contains(const Type &item) const
Test if an item is present in the container using equality.
static Tree_Node< int > * chain(size_t n)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_hypot_function > > hypot(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
__gmp_expr< mpfr_t, mpfr_t > mpfr_class
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
bool vertical
If true, use vertical layout (default).
Singly linked list implementations with head-tail access.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
void in_place_unique(Container &c, Compare cmp={})
Remove duplicates in-place preserving first occurrence order.
Orientation
Classification of three-point orientation.
Geom_Number in_circle_determinant(const Point &a, const Point &b, const Point &c, const Point &p)
Return the exact in-circle determinant for (a,b,c,p).
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
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.
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
bool on_segment(const Segment &s, const Point &p)
Return true if p lies on segment s (exact).
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool segments_intersect(const Segment &s1, const Segment &s2)
Return true if segments s1 and s2 intersect (including endpoints).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
std::decay_t< typename HeadC::Item_Type > T
Point segment_intersection_point(const Segment &s1, const Segment &s2)
Compute the exact intersection point of segments s1 and s2.
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
Geom_Number square_root(const Geom_Number &x)
Square root of x (wrapper over mpfr).
double geom_number_to_double(const Geom_Number &n)
Converts a Geom_Number to its double precision representation.
and
Check uniqueness with explicit hash + equality functors.
Orientation orientation(const Point &a, const Point &b, const Point &c)
Return the orientation of the triple (a, b, c).
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.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
void next()
Advance all underlying iterators (bounds-checked).
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
2D polygon representation and geometric operations.
Structure used for debugging and visualizing the tree structure.
size_t user_index
Entry::index.
size_t node_index
Index of the node.
size_t entry_index
Index in entries_ array.
size_t right
Right child index.
size_t left
Left child index.
Rectangle bbox
Bounding box of the node.
size_t depth
Depth in the tree.
bool is_leaf
True if it's a leaf node.
A snapshot of the entire tree for debugging.
size_t root
Index of the root node.
Array< DebugNode > nodes
Array of all nodes in debug format.
An entry in the tree, consisting of a bounding box and a user-defined index.
size_t index
User-defined identifier for the entry.
Rectangle bbox
The axis-aligned bounding box.
Internal tree node structure.
bool is_leaf() const
Checks if this node is a leaf.
Rectangle bbox
Bounding box enclosing all descendants.
size_t entry_idx
Index in entries_ (leaf only).
size_t left
Left child index.
size_t right
Right child index.
Result of an alpha-shape computation.
Array< DelaunayTriangulationBowyerWatson::IndexedTriangle > triangles
Triangles that satisfy the alpha criterion.
Array< Point > sites
Deduplicated and sorted input sites.
Array< Segment > boundary_edges
Segments forming the boundary of the alpha-shape.
Lexicographical comparison for points (x primary, y secondary).
bool operator()(const Point &p1, const Point &p2) const
De Casteljau subdivision: split a cubic Bézier at parameter t into two cubic Béziers (left and right)...
Intersection pair found between edge i of A and edge j of B.
Comparator for segments to ensure strict weak ordering in sets.
bool operator()(const Segment &s1, const Segment &s2) const
Compares two segments lexicographically.
static bool cmp_point(const Point &p1, const Point &p2)
Strict lexicographical comparison for points.
Strict weak order for sorting points by (y,x).
bool operator()(const Point &p1, const Point &p2) const
Strict weak order for sorting points lexicographically by (x,y).
bool operator()(const Point &p1, const Point &p2) const
Geom_Number distance_squared
Collect edges that cross segment (u,v) by scanning all mesh edges.
size_t local
local index in tri (opposite vertex)
Array< IndexedEdge > constrained_edges
Array< IndexedTriangle > triangles
size_t adj[3]
adj[i] = neighbor across edge opposite v[i]
size_t v[3]
CCW vertex indices.
bool constrained[3]
constrained[i] = edge opp v[i] is constrained
std::vector< SupportPoint > reduced_simplex
Holds the results of the distance computation.
Geom_Number distance
The minimum Euclidean distance.
Geom_Number distance_squared
The minimum squared distance.
Point closest_on_second
Witness point on the second polygon.
bool intersects
True if the polygons overlap.
Point closest_on_first
Witness point on the first polygon.
size_t gjk_iterations
Count of iterations performed.
A point in the Minkowski difference and its source components.
Point a
Contributing point from the first polygon.
Point b
Contributing point from the second polygon.
Point v
Resulting point in Minkowski difference (a - b).
double vy
Double-precision cache for faster predicates.
double vx
Double-precision cache for faster predicates.
Represents a triangle by the indices of its three vertices.
size_t j
Index of the second vertex.
size_t i
Index of the first vertex.
size_t k
Index of the third vertex.
The result of a Delaunay triangulation.
Array< IndexedTriangle > triangles
Triangles forming the Delaunay triangulation.
Array< Point > sites
Unique, sorted input points used for triangulation.
size_t v[3]
vertex indices, CCW order
size_t adj[3]
adj[i] = neighbor across edge opposite v[i]
bool operator()(const UndirectedEdge &a, const UndirectedEdge &b) const
Represents a reference to a triangle edge.
size_t v
Index of the second vertex of the edge (u < v).
size_t third
Index of the third vertex in the triangle (not on the edge).
size_t u
Index of the first vertex of the edge (u < v).
size_t tri
Index of the triangle containing this edge.
Lexicographical comparison for points.
bool operator()(const Point &p1, const Point &p2) const
bool outside(const Point &x) const
Geom_Number offset() const
HalfPlane(Point p_, Point q_)
Represents a spatial partition or leaf in the KD-tree.
bool split_on_x
True if split is vertical, false if horizontal.
Rectangle region
Geometric region covered by this node.
Geom_Number split_value
The coordinate value of the split.
Point representative
Point stored in the leaf.
bool is_leaf
True if it's a leaf node.
size_t depth
Depth of the node in the tree.
A complete snapshot of the tree for debugging.
Array< DebugPartition > partitions
List of all internal and leaf partitions.
Array< Point > points
All points stored in the tree.
Rectangle bounds
Bounds of the tree, extended to the points outside them.
Comparator for SeqEvent, ensuring a strict weak ordering.
CmpEvent cmp
The user-provided base comparator.
bool operator()(const SeqEvent &a, const SeqEvent &b) const
Internal event wrapper that adds a sequence number for tie-breaking.
size_t seq
Insertion sequence number.
Event event
The user-defined event payload.
Result type: a circle defined by center and squared radius.
Geom_Number radius() const
Exact radius (square root of radius_squared).
bool contains(const Point &p) const
True if point p lies inside or on the circle boundary.
Geom_Number radius_squared
Augmented vertex in the cleaned polygon.
Intersection found between edge i and edge j of the raw polygon.
The result of an offset operation, which may produce multiple polygons.
Array< Polygon > polygons
Set of resulting simple polygons.
size_t size() const noexcept
bool is_empty() const noexcept
Checks if the result contains no polygons.
Iterator over the vertices of a polygon.
Represents a cell in the power diagram.
size_t site_index
Index of the site this cell belongs to.
bool bounded
True if the cell is completely bounded.
Array< Point > vertices
Vertices of the cell in CCW order.
Point site
Coordinates of the site.
Geom_Number weight
Weight of the site.
Represents an edge in the power diagram.
size_t site_v
Index of the second site sharing this edge.
Point tgt
Target point of the edge (if not unbounded).
size_t site_u
Index of the first site sharing this edge.
Point direction
Direction vector for unbounded rays.
bool unbounded
True if the edge is an unbounded ray.
Point src
Source point of the edge.
Complete result of a power diagram computation.
Array< PowerCell > cells
Cells of the power diagram.
Array< Point > vertices
Vertices of the power diagram.
Array< WeightedSite > sites
Sorted and unique input sites.
Array< PowerEdge > edges
Edges of the power diagram.
A site with an associated weight (squared radius).
Geom_Number weight
Weight of the site.
Point position
Coordinates of the site.
Represents a node in the range tree for debugging.
size_t left
Left child tree index.
size_t y_sorted_size
Number of points in the y-sorted secondary structure.
size_t right
Right child tree index.
Geom_Number split_x
X-coordinate used to split this node.
bool is_leaf
True if this is a leaf node.
Geom_Number xmin
Minimum x-coordinate in this subtree.
size_t tree_index
Implicit tree index (1-based).
size_t lo
Lower index into the x-sorted point array.
Geom_Number xmax
Maximum x-coordinate in this subtree.
size_t hi
Upper index into the x-sorted point array.
A complete snapshot of the range tree for debugging.
Array< DebugNode > nodes
List of all nodes in debug format.
Array< Point > x_sorted_points
The underlying x-sorted point array.
Internal node structure containing the y-sorted secondary array.
Array< Point > y_sorted
Points in this node's x-range, sorted by y.
Array< IndexedTriangle > triangles
Array< WeightedSite > sites
A 2-D site with an associated weight (squared radius).
Geom_Number distance_squared
Geom_Number width_squared
Represents an edge in the planar subdivision.
size_t tgt
Index of the target vertex.
size_t src
Index of the source vertex.
size_t seg_idx
Index of the original segment that generated this edge.
Represents a face (region) in the planar subdivision.
bool unbounded
True if this is the infinite outer face.
DynList< size_t > boundary
Ordered indices of vertices forming the face boundary.
Internal half-edge structure used for face traversal.
size_t edge_idx
Index of the corresponding undirected edge.
size_t twin
Index of the twin half-edge going in the opposite direction.
size_t next
Index of the next half-edge in CCW order around the face.
size_t origin
Index of the vertex where this half-edge starts.
size_t face
Index of the face this half-edge borders.
size_t target
Index of the vertex where this half-edge ends.
The complete result of the segment arrangement computation.
Array< Point > vertices
Unique vertices (endpoints and intersection points).
Array< ArrEdge > edges
Sub-segments forming the arrangement graph.
Array< ArrFace > faces
Faces defined by the arrangement.
Detailed result of a segment-segment intersection test.
bool intersects() const noexcept
Returns true if any intersection exists.
Point point
The intersection point (if kind is POINT).
Segment overlap
The overlap segment (if kind is OVERLAP).
Kind kind
Type of intersection.
Functor wrapper for event_less (used by LineSweepFramework).
bool operator()(const Event &a, const Event &b) const
Represents an event in the sweep-line algorithm.
Point pt
Coordinates of the event.
size_t seg_b
Index of the second segment (only for INTERSECTION).
EventType type
Type of the event.
size_t seg_a
Index of the first segment involved.
A single intersection record.
size_t seg_j
Index of second segment (seg_i < seg_j).
Point point
Intersection point.
size_t seg_i
Index of the first segment.
A node in the search Directed Acyclic Graph (DAG).
NodeType type
The type of the node.
size_t index
Index into points, segments, or trapezoids depending on type.
size_t right
Index of the right child (or above/right child).
size_t left
Index of the left child (or below/left child).
Result of a point location query.
size_t segment_index
Index of the segment the point lies on.
size_t trapezoid_index
Index of the containing trapezoid (if any).
size_t point_index
Index of the point the query point coincides with.
LocationType type
Classification of the point location.
The trapezoidal map and DAG search structure.
Array< Trapezoid > trapezoids
All trapezoids created during construction.
Array< Segment > segments
Input segments.
Array< DagNode > dag
The search directed acyclic graph.
LocationResult locate(const Point &p) const
Locate a point in the trapezoidal map.
size_t num_input_points
Number of unique points derived from segments.
Array< Point > points
Unique endpoints of all segments.
size_t num_input_segments
Number of segments originally provided.
size_t dag_root
Index of the DAG root node.
bool contains(const Point &p) const
Check if point is inside any input polygon.
A trapezoid in the decomposition.
size_t leftp
Index into points array (left vertical wall).
size_t dag_leaf
Back-pointer to the corresponding DAG leaf node.
size_t lower_left
Neighbor trapezoid to the lower left.
size_t rightp
Index into points array (right vertical wall).
size_t upper_right
Neighbor trapezoid to the upper right.
bool active
True if this trapezoid is part of the current map.
size_t lower_right
Neighbor trapezoid to the lower right.
size_t upper_left
Neighbor trapezoid to the upper left.
size_t bottom
Index into segments array (lower boundary).
size_t top
Index into segments array (upper boundary).
bool operator()(const EdgeKey &a, const EdgeKey &b) const
Entry in the priority queue for vertex removal.
size_t idx
Index of the vertex in the polyline.
bool operator<(const VWEntry &o) const
Comparison based on effective area.
Geom_Number area
Effective area of the triangle formed with its neighbors.
bool operator>(const VWEntry &o) const
Comparison based on effective area.
Represents a Voronoi cell.
size_t site_index
Index of the site this cell belongs to.
Array< Point > vertices
Vertices of the cell in CCW order.
Point site
Coordinates of the site.
bool bounded
True if the cell is completely bounded.
Represents a Voronoi cell clipped by a bounding polygon.
size_t site_index
Index of the site.
Polygon polygon
Clipped convex polygon representing the cell.
Point site
Coordinates of the site.
Represents an edge in the Voronoi diagram.
Point tgt
Target point of the edge (only if not unbounded).
Point src
Source point of the edge.
size_t site_v
Index of the second site sharing this edge.
Point direction
Direction vector for unbounded rays.
size_t site_u
Index of the first site sharing this edge.
bool unbounded
True if the edge is an unbounded ray.
The complete result of a Voronoi diagram computation.
Array< Edge > edges
Voronoi edges.
Array< Point > vertices
Voronoi vertices.
Array< Cell > cells
Voronoi cells.
Array< Point > sites
Input sites.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
2D k-d tree implementation for spatial point indexing.
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
Dynamic binary heap with node-based storage.
Dynamic doubly linked list implementation.
Dynamic set implementations based on balanced binary search trees.
Comprehensive sorting algorithms and search utilities for Aleph-w.