UNPKG

@maplat/edgeruler

Version:

A small library for dual-constraining a Delaunator triangulation

611 lines (610 loc) 27.9 kB
const at = 11102230246251565e-32, W = 134217729, Yt = (3 + 8 * at) * at; function et(I, o, h, l, m) { let f, r, v, S, a = o[0], d = l[0], e = 0, c = 0; d > a == d > -a ? (f = a, a = o[++e]) : (f = d, d = l[++c]); let g = 0; if (e < I && c < h) for (d > a == d > -a ? (r = a + f, v = f - (r - a), a = o[++e]) : (r = d + f, v = f - (r - d), d = l[++c]), f = r, v !== 0 && (m[g++] = v); e < I && c < h; ) d > a == d > -a ? (r = f + a, S = r - f, v = f - (r - S) + (a - S), a = o[++e]) : (r = f + d, S = r - f, v = f - (r - S) + (d - S), d = l[++c]), f = r, v !== 0 && (m[g++] = v); for (; e < I; ) r = f + a, S = r - f, v = f - (r - S) + (a - S), a = o[++e], f = r, v !== 0 && (m[g++] = v); for (; c < h; ) r = f + d, S = r - f, v = f - (r - S) + (d - S), d = l[++c], f = r, v !== 0 && (m[g++] = v); return (f !== 0 || g === 0) && (m[g++] = f), g; } function bt(I, o, h, l, m, f, r, v) { return et(et(I, o, h, l, r), r, m, f, v); } function u(I, o, h, l) { let m, f, r, v, S, a, d, e, c, g, j; d = W * h, g = d - (d - h), j = h - g; let E = o[0]; m = E * h, d = W * E, e = d - (d - E), c = E - e, r = c * j - (m - e * g - c * g - e * j); let U = 0; r !== 0 && (l[U++] = r); for (let V = 1; V < I; V++) E = o[V], v = E * h, d = W * E, e = d - (d - E), c = E - e, S = c * j - (v - e * g - c * g - e * j), f = m + S, a = f - m, r = m - (f - a) + (S - a), r !== 0 && (l[U++] = r), m = v + f, r = f - (m - v), r !== 0 && (l[U++] = r); return (m !== 0 || U === 0) && (l[U++] = m), U; } function Zt(I, o) { let h = o[0]; for (let l = 1; l < I; l++) h += o[l]; return h; } function L(I) { return new Float64Array(I); } const yt = (3 + 16 * at) * at, xt = (2 + 12 * at) * at, Pt = (9 + 64 * at) * at * at, At = L(4), Gt = L(8), Ht = L(12), Jt = L(16), ht = L(4); function tn(I, o, h, l, m, f, r) { let v, S, a, d, e, c, g, j, E, U, V, H, x, R, P, X, nt, Y; const it = I - m, ct = h - m, lt = o - f, tt = l - f; R = it * tt, c = W * it, g = c - (c - it), j = it - g, c = W * tt, E = c - (c - tt), U = tt - E, P = j * U - (R - g * E - j * E - g * U), X = lt * ct, c = W * lt, g = c - (c - lt), j = lt - g, c = W * ct, E = c - (c - ct), U = ct - E, nt = j * U - (X - g * E - j * E - g * U), V = P - nt, e = P - V, At[0] = P - (V + e) + (e - nt), H = R + V, e = H - R, x = R - (H - e) + (V - e), V = x - X, e = x - V, At[1] = x - (V + e) + (e - X), Y = H + V, e = Y - H, At[2] = H - (Y - e) + (V - e), At[3] = Y; let st = Zt(4, At), t = xt * r; if (st >= t || -st >= t || (e = I - it, v = I - (it + e) + (e - m), e = h - ct, a = h - (ct + e) + (e - m), e = o - lt, S = o - (lt + e) + (e - f), e = l - tt, d = l - (tt + e) + (e - f), v === 0 && S === 0 && a === 0 && d === 0) || (t = Pt * r + Yt * Math.abs(st), st += it * d + tt * v - (lt * a + ct * S), st >= t || -st >= t)) return st; R = v * tt, c = W * v, g = c - (c - v), j = v - g, c = W * tt, E = c - (c - tt), U = tt - E, P = j * U - (R - g * E - j * E - g * U), X = S * ct, c = W * S, g = c - (c - S), j = S - g, c = W * ct, E = c - (c - ct), U = ct - E, nt = j * U - (X - g * E - j * E - g * U), V = P - nt, e = P - V, ht[0] = P - (V + e) + (e - nt), H = R + V, e = H - R, x = R - (H - e) + (V - e), V = x - X, e = x - V, ht[1] = x - (V + e) + (e - X), Y = H + V, e = Y - H, ht[2] = H - (Y - e) + (V - e), ht[3] = Y; const n = et(4, At, 4, ht, Gt); R = it * d, c = W * it, g = c - (c - it), j = it - g, c = W * d, E = c - (c - d), U = d - E, P = j * U - (R - g * E - j * E - g * U), X = lt * a, c = W * lt, g = c - (c - lt), j = lt - g, c = W * a, E = c - (c - a), U = a - E, nt = j * U - (X - g * E - j * E - g * U), V = P - nt, e = P - V, ht[0] = P - (V + e) + (e - nt), H = R + V, e = H - R, x = R - (H - e) + (V - e), V = x - X, e = x - V, ht[1] = x - (V + e) + (e - X), Y = H + V, e = Y - H, ht[2] = H - (Y - e) + (V - e), ht[3] = Y; const i = et(n, Gt, 4, ht, Ht); R = v * d, c = W * v, g = c - (c - v), j = v - g, c = W * d, E = c - (c - d), U = d - E, P = j * U - (R - g * E - j * E - g * U), X = S * a, c = W * S, g = c - (c - S), j = S - g, c = W * a, E = c - (c - a), U = a - E, nt = j * U - (X - g * E - j * E - g * U), V = P - nt, e = P - V, ht[0] = P - (V + e) + (e - nt), H = R + V, e = H - R, x = R - (H - e) + (V - e), V = x - X, e = x - V, ht[1] = x - (V + e) + (e - X), Y = H + V, e = Y - H, ht[2] = H - (Y - e) + (V - e), ht[3] = Y; const b = et(i, Ht, 4, ht, Jt); return Jt[b - 1]; } function Ft(I, o, h, l, m, f) { const r = (o - f) * (h - m), v = (I - m) * (l - f), S = r - v, a = Math.abs(r + v); return Math.abs(S) >= yt * a ? S : -tn(I, o, h, l, m, f, a); } const nn = (10 + 96 * at) * at, sn = (4 + 48 * at) * at, en = (44 + 576 * at) * at * at, Et = L(4), pt = L(4), jt = L(4), wt = L(4), mt = L(4), gt = L(4), ft = L(4), ut = L(4), Ut = L(8), Nt = L(8), Ot = L(8), Tt = L(8), qt = L(8), Lt = L(8), Vt = L(8), It = L(8), Qt = L(8), _t = L(4), Ct = L(4), St = L(4), C = L(8), F = L(16), J = L(16), K = L(16), G = L(32), Wt = L(32), Z = L(48), dt = L(64); let Bt = L(1152), $t = L(1152); function y(I, o, h) { I = et(I, Bt, o, h, $t); const l = Bt; return Bt = $t, $t = l, I; } function on(I, o, h, l, m, f, r, v, S) { let a, d, e, c, g, j, E, U, V, H, x, R, P, X, nt, Y, it, ct, lt, tt, st, t, n, i, b, w, _, s, M, D, p, A, B, Q, k; const N = I - r, O = h - r, T = m - r, $ = o - v, q = l - v, z = f - v; p = O * z, n = W * O, i = n - (n - O), b = O - i, n = W * z, w = n - (n - z), _ = z - w, A = b * _ - (p - i * w - b * w - i * _), B = T * q, n = W * T, i = n - (n - T), b = T - i, n = W * q, w = n - (n - q), _ = q - w, Q = b * _ - (B - i * w - b * w - i * _), s = A - Q, t = A - s, Et[0] = A - (s + t) + (t - Q), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D - B, t = D - s, Et[1] = D - (s + t) + (t - B), k = M + s, t = k - M, Et[2] = M - (k - t) + (s - t), Et[3] = k, p = T * $, n = W * T, i = n - (n - T), b = T - i, n = W * $, w = n - (n - $), _ = $ - w, A = b * _ - (p - i * w - b * w - i * _), B = N * z, n = W * N, i = n - (n - N), b = N - i, n = W * z, w = n - (n - z), _ = z - w, Q = b * _ - (B - i * w - b * w - i * _), s = A - Q, t = A - s, pt[0] = A - (s + t) + (t - Q), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D - B, t = D - s, pt[1] = D - (s + t) + (t - B), k = M + s, t = k - M, pt[2] = M - (k - t) + (s - t), pt[3] = k, p = N * q, n = W * N, i = n - (n - N), b = N - i, n = W * q, w = n - (n - q), _ = q - w, A = b * _ - (p - i * w - b * w - i * _), B = O * $, n = W * O, i = n - (n - O), b = O - i, n = W * $, w = n - (n - $), _ = $ - w, Q = b * _ - (B - i * w - b * w - i * _), s = A - Q, t = A - s, jt[0] = A - (s + t) + (t - Q), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D - B, t = D - s, jt[1] = D - (s + t) + (t - B), k = M + s, t = k - M, jt[2] = M - (k - t) + (s - t), jt[3] = k, a = et( et( et( u(u(4, Et, N, C), C, N, F), F, u(u(4, Et, $, C), C, $, J), J, G ), G, et( u(u(4, pt, O, C), C, O, F), F, u(u(4, pt, q, C), C, q, J), J, Wt ), Wt, dt ), dt, et( u(u(4, jt, T, C), C, T, F), F, u(u(4, jt, z, C), C, z, J), J, G ), G, Bt ); let Mt = Zt(a, Bt), kt = sn * S; if (Mt >= kt || -Mt >= kt || (t = I - N, d = I - (N + t) + (t - r), t = o - $, g = o - ($ + t) + (t - v), t = h - O, e = h - (O + t) + (t - r), t = l - q, j = l - (q + t) + (t - v), t = m - T, c = m - (T + t) + (t - r), t = f - z, E = f - (z + t) + (t - v), d === 0 && e === 0 && c === 0 && g === 0 && j === 0 && E === 0) || (kt = en * S + Yt * Math.abs(Mt), Mt += (N * N + $ * $) * (O * E + z * e - (q * c + T * j)) + 2 * (N * d + $ * g) * (O * z - q * T) + ((O * O + q * q) * (T * g + $ * c - (z * d + N * E)) + 2 * (O * e + q * j) * (T * $ - z * N)) + ((T * T + z * z) * (N * j + q * d - ($ * e + O * g)) + 2 * (T * c + z * E) * (N * q - $ * O)), Mt >= kt || -Mt >= kt)) return Mt; if ((e !== 0 || j !== 0 || c !== 0 || E !== 0) && (p = N * N, n = W * N, i = n - (n - N), b = N - i, A = b * b - (p - i * i - (i + i) * b), B = $ * $, n = W * $, i = n - (n - $), b = $ - i, Q = b * b - (B - i * i - (i + i) * b), s = A + Q, t = s - A, wt[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, wt[1] = D - (s - t) + (B - t), k = M + s, t = k - M, wt[2] = M - (k - t) + (s - t), wt[3] = k), (c !== 0 || E !== 0 || d !== 0 || g !== 0) && (p = O * O, n = W * O, i = n - (n - O), b = O - i, A = b * b - (p - i * i - (i + i) * b), B = q * q, n = W * q, i = n - (n - q), b = q - i, Q = b * b - (B - i * i - (i + i) * b), s = A + Q, t = s - A, mt[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, mt[1] = D - (s - t) + (B - t), k = M + s, t = k - M, mt[2] = M - (k - t) + (s - t), mt[3] = k), (d !== 0 || g !== 0 || e !== 0 || j !== 0) && (p = T * T, n = W * T, i = n - (n - T), b = T - i, A = b * b - (p - i * i - (i + i) * b), B = z * z, n = W * z, i = n - (n - z), b = z - i, Q = b * b - (B - i * i - (i + i) * b), s = A + Q, t = s - A, gt[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, gt[1] = D - (s - t) + (B - t), k = M + s, t = k - M, gt[2] = M - (k - t) + (s - t), gt[3] = k), d !== 0 && (U = u(4, Et, d, Ut), a = y(a, bt( u(U, Ut, 2 * N, F), F, u(u(4, gt, d, C), C, q, J), J, u(u(4, mt, d, C), C, -z, K), K, G, Z ), Z)), g !== 0 && (V = u(4, Et, g, Nt), a = y(a, bt( u(V, Nt, 2 * $, F), F, u(u(4, mt, g, C), C, T, J), J, u(u(4, gt, g, C), C, -O, K), K, G, Z ), Z)), e !== 0 && (H = u(4, pt, e, Ot), a = y(a, bt( u(H, Ot, 2 * O, F), F, u(u(4, wt, e, C), C, z, J), J, u(u(4, gt, e, C), C, -$, K), K, G, Z ), Z)), j !== 0 && (x = u(4, pt, j, Tt), a = y(a, bt( u(x, Tt, 2 * q, F), F, u(u(4, gt, j, C), C, N, J), J, u(u(4, wt, j, C), C, -T, K), K, G, Z ), Z)), c !== 0 && (R = u(4, jt, c, qt), a = y(a, bt( u(R, qt, 2 * T, F), F, u(u(4, mt, c, C), C, $, J), J, u(u(4, wt, c, C), C, -q, K), K, G, Z ), Z)), E !== 0 && (P = u(4, jt, E, Lt), a = y(a, bt( u(P, Lt, 2 * z, F), F, u(u(4, wt, E, C), C, O, J), J, u(u(4, mt, E, C), C, -N, K), K, G, Z ), Z)), d !== 0 || g !== 0) { if (e !== 0 || j !== 0 || c !== 0 || E !== 0 ? (p = e * z, n = W * e, i = n - (n - e), b = e - i, n = W * z, w = n - (n - z), _ = z - w, A = b * _ - (p - i * w - b * w - i * _), B = O * E, n = W * O, i = n - (n - O), b = O - i, n = W * E, w = n - (n - E), _ = E - w, Q = b * _ - (B - i * w - b * w - i * _), s = A + Q, t = s - A, ft[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, ft[1] = D - (s - t) + (B - t), k = M + s, t = k - M, ft[2] = M - (k - t) + (s - t), ft[3] = k, p = c * -q, n = W * c, i = n - (n - c), b = c - i, n = W * -q, w = n - (n - -q), _ = -q - w, A = b * _ - (p - i * w - b * w - i * _), B = T * -j, n = W * T, i = n - (n - T), b = T - i, n = W * -j, w = n - (n - -j), _ = -j - w, Q = b * _ - (B - i * w - b * w - i * _), s = A + Q, t = s - A, ut[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, ut[1] = D - (s - t) + (B - t), k = M + s, t = k - M, ut[2] = M - (k - t) + (s - t), ut[3] = k, nt = et(4, ft, 4, ut, It), p = e * E, n = W * e, i = n - (n - e), b = e - i, n = W * E, w = n - (n - E), _ = E - w, A = b * _ - (p - i * w - b * w - i * _), B = c * j, n = W * c, i = n - (n - c), b = c - i, n = W * j, w = n - (n - j), _ = j - w, Q = b * _ - (B - i * w - b * w - i * _), s = A - Q, t = A - s, Ct[0] = A - (s + t) + (t - Q), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D - B, t = D - s, Ct[1] = D - (s + t) + (t - B), k = M + s, t = k - M, Ct[2] = M - (k - t) + (s - t), Ct[3] = k, ct = 4) : (It[0] = 0, nt = 1, Ct[0] = 0, ct = 1), d !== 0) { const ot = u(nt, It, d, K); a = y(a, et( u(U, Ut, d, F), F, u(ot, K, 2 * N, G), G, Z ), Z); const rt = u(ct, Ct, d, C); a = y(a, bt( u(rt, C, 2 * N, F), F, u(rt, C, d, J), J, u(ot, K, d, G), G, Wt, dt ), dt), j !== 0 && (a = y(a, u(u(4, gt, d, C), C, j, F), F)), E !== 0 && (a = y(a, u(u(4, mt, -d, C), C, E, F), F)); } if (g !== 0) { const ot = u(nt, It, g, K); a = y(a, et( u(V, Nt, g, F), F, u(ot, K, 2 * $, G), G, Z ), Z); const rt = u(ct, Ct, g, C); a = y(a, bt( u(rt, C, 2 * $, F), F, u(rt, C, g, J), J, u(ot, K, g, G), G, Wt, dt ), dt); } } if (e !== 0 || j !== 0) { if (c !== 0 || E !== 0 || d !== 0 || g !== 0 ? (p = c * $, n = W * c, i = n - (n - c), b = c - i, n = W * $, w = n - (n - $), _ = $ - w, A = b * _ - (p - i * w - b * w - i * _), B = T * g, n = W * T, i = n - (n - T), b = T - i, n = W * g, w = n - (n - g), _ = g - w, Q = b * _ - (B - i * w - b * w - i * _), s = A + Q, t = s - A, ft[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, ft[1] = D - (s - t) + (B - t), k = M + s, t = k - M, ft[2] = M - (k - t) + (s - t), ft[3] = k, tt = -z, st = -E, p = d * tt, n = W * d, i = n - (n - d), b = d - i, n = W * tt, w = n - (n - tt), _ = tt - w, A = b * _ - (p - i * w - b * w - i * _), B = N * st, n = W * N, i = n - (n - N), b = N - i, n = W * st, w = n - (n - st), _ = st - w, Q = b * _ - (B - i * w - b * w - i * _), s = A + Q, t = s - A, ut[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, ut[1] = D - (s - t) + (B - t), k = M + s, t = k - M, ut[2] = M - (k - t) + (s - t), ut[3] = k, Y = et(4, ft, 4, ut, Qt), p = c * g, n = W * c, i = n - (n - c), b = c - i, n = W * g, w = n - (n - g), _ = g - w, A = b * _ - (p - i * w - b * w - i * _), B = d * E, n = W * d, i = n - (n - d), b = d - i, n = W * E, w = n - (n - E), _ = E - w, Q = b * _ - (B - i * w - b * w - i * _), s = A - Q, t = A - s, St[0] = A - (s + t) + (t - Q), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D - B, t = D - s, St[1] = D - (s + t) + (t - B), k = M + s, t = k - M, St[2] = M - (k - t) + (s - t), St[3] = k, lt = 4) : (Qt[0] = 0, Y = 1, St[0] = 0, lt = 1), e !== 0) { const ot = u(Y, Qt, e, K); a = y(a, et( u(H, Ot, e, F), F, u(ot, K, 2 * O, G), G, Z ), Z); const rt = u(lt, St, e, C); a = y(a, bt( u(rt, C, 2 * O, F), F, u(rt, C, e, J), J, u(ot, K, e, G), G, Wt, dt ), dt), E !== 0 && (a = y(a, u(u(4, wt, e, C), C, E, F), F)), g !== 0 && (a = y(a, u(u(4, gt, -e, C), C, g, F), F)); } if (j !== 0) { const ot = u(Y, Qt, j, K); a = y(a, et( u(x, Tt, j, F), F, u(ot, K, 2 * q, G), G, Z ), Z); const rt = u(lt, St, j, C); a = y(a, bt( u(rt, C, 2 * q, F), F, u(rt, C, j, J), J, u(ot, K, j, G), G, Wt, dt ), dt); } } if (c !== 0 || E !== 0) { if (d !== 0 || g !== 0 || e !== 0 || j !== 0 ? (p = d * q, n = W * d, i = n - (n - d), b = d - i, n = W * q, w = n - (n - q), _ = q - w, A = b * _ - (p - i * w - b * w - i * _), B = N * j, n = W * N, i = n - (n - N), b = N - i, n = W * j, w = n - (n - j), _ = j - w, Q = b * _ - (B - i * w - b * w - i * _), s = A + Q, t = s - A, ft[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, ft[1] = D - (s - t) + (B - t), k = M + s, t = k - M, ft[2] = M - (k - t) + (s - t), ft[3] = k, tt = -$, st = -g, p = e * tt, n = W * e, i = n - (n - e), b = e - i, n = W * tt, w = n - (n - tt), _ = tt - w, A = b * _ - (p - i * w - b * w - i * _), B = O * st, n = W * O, i = n - (n - O), b = O - i, n = W * st, w = n - (n - st), _ = st - w, Q = b * _ - (B - i * w - b * w - i * _), s = A + Q, t = s - A, ut[0] = A - (s - t) + (Q - t), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D + B, t = s - D, ut[1] = D - (s - t) + (B - t), k = M + s, t = k - M, ut[2] = M - (k - t) + (s - t), ut[3] = k, X = et(4, ft, 4, ut, Vt), p = d * j, n = W * d, i = n - (n - d), b = d - i, n = W * j, w = n - (n - j), _ = j - w, A = b * _ - (p - i * w - b * w - i * _), B = e * g, n = W * e, i = n - (n - e), b = e - i, n = W * g, w = n - (n - g), _ = g - w, Q = b * _ - (B - i * w - b * w - i * _), s = A - Q, t = A - s, _t[0] = A - (s + t) + (t - Q), M = p + s, t = M - p, D = p - (M - t) + (s - t), s = D - B, t = D - s, _t[1] = D - (s + t) + (t - B), k = M + s, t = k - M, _t[2] = M - (k - t) + (s - t), _t[3] = k, it = 4) : (Vt[0] = 0, X = 1, _t[0] = 0, it = 1), c !== 0) { const ot = u(X, Vt, c, K); a = y(a, et( u(R, qt, c, F), F, u(ot, K, 2 * T, G), G, Z ), Z); const rt = u(it, _t, c, C); a = y(a, bt( u(rt, C, 2 * T, F), F, u(rt, C, c, J), J, u(ot, K, c, G), G, Wt, dt ), dt), g !== 0 && (a = y(a, u(u(4, mt, c, C), C, g, F), F)), j !== 0 && (a = y(a, u(u(4, wt, -c, C), C, j, F), F)); } if (E !== 0) { const ot = u(X, Vt, E, K); a = y(a, et( u(P, Lt, E, F), F, u(ot, K, 2 * z, G), G, Z ), Z); const rt = u(it, _t, E, C); a = y(a, bt( u(rt, C, 2 * z, F), F, u(rt, C, E, J), J, u(ot, K, E, G), G, Wt, dt ), dt); } } return Bt[a - 1]; } function rn(I, o, h, l, m, f, r, v) { const S = I - r, a = h - r, d = m - r, e = o - v, c = l - v, g = f - v, j = a * g, E = d * c, U = S * S + e * e, V = d * e, H = S * g, x = a * a + c * c, R = S * c, P = a * e, X = d * d + g * g, nt = U * (j - E) + x * (V - H) + X * (R - P), Y = (Math.abs(j) + Math.abs(E)) * U + (Math.abs(V) + Math.abs(H)) * x + (Math.abs(R) + Math.abs(P)) * X, it = nn * Y; return nt > it || -nt > it ? nt : on(I, o, h, l, m, f, r, v, Y); } class cn { constructor(o, h) { this.W = o, this.bs = h; } /** * Add a number to the set. * * @param idx The number to add. Must be 0 <= idx < len. * @return this. */ add(o) { const h = this.W, l = o / h | 0, m = o % h; return this.bs[l] |= 1 << m, this; } /** * Delete a number from the set. * * @param idx The number to delete. Must be 0 <= idx < len. * @return this. */ delete(o) { const h = this.W, l = o / h | 0, m = o % h; return this.bs[l] &= ~(1 << m), this; } /** * Add or delete a number in the set, depending on the second argument. * * @param idx The number to add or delete. Must be 0 <= idx < len. * @param val If true, add the number, otherwise delete. * @return val. */ set(o, h) { const l = this.W, m = o / l | 0, f = o % l, r = 1 << f; return this.bs[m] ^= (-h ^ this.bs[m]) & r, h; } /** * Whether the number is in the set. * * @param idx The number to test. Must be 0 <= idx < len. * @return True if the number is in the set. */ has(o) { const h = this.W, l = o / h | 0, m = o % h; return !!(this.bs[l] & 1 << m); } /** * Iterate over the numbers that are in the set. The callback is invoked * with each number that is set. It is allowed to change the BitSet during * iteration. If it deletes a number that has not been iterated over, that * number will not show up in a later call. If it adds a number during * iteration, that number may or may not show up in a later call. * * @param fn The function to call for each number. * @return this. */ forEach(o) { const h = this.W, l = this.bs, m = l.length; for (let f = 0; f < m; f++) { let r = 0; for (; l[f] && r < h; ) l[f] & 1 << r && o(f * h + r), r++; } return this; } } class Kt extends cn { /** * Create a bit set. * * @param len The length of the bit set, limiting the maximum value that * can be stored in it to len - 1. */ constructor(o) { const l = new Uint8Array(Math.ceil(o / 8)).fill(0); super(8, l); } } function Dt(I) { return I % 3 === 2 ? I - 2 : I + 1; } function vt(I) { return I % 3 === 0 ? I + 2 : I - 1; } function Rt(I, o, h, l, m, f, r, v) { const S = Ft(I, o, m, f, r, v), a = Ft(h, l, m, f, r, v); if (S > 0 && a > 0 || S < 0 && a < 0) return !1; const d = Ft(m, f, I, o, h, l), e = Ft(r, v, I, o, h, l); return d > 0 && e > 0 || d < 0 && e < 0 ? !1 : S === 0 && a === 0 && d === 0 && e === 0 ? !(Math.max(m, r) < Math.min(I, h) || Math.max(I, h) < Math.min(m, r) || Math.max(f, v) < Math.min(o, l) || Math.max(o, l) < Math.min(f, v)) : !0; } class ln { constructor(o) { this.del = o; } } const zt = class zt extends ln { /** * Make a Constrain. * * @param del The triangulation output from Delaunator. * @param edges If provided, constrain these edges as by constrainAll. */ constructor(o, h) { if (!o || typeof o != "object" || !o.triangles || !o.halfedges || !o.coords) throw new Error("Expected an object with Delaunator output"); if (o.triangles.length % 3 || o.halfedges.length !== o.triangles.length || o.coords.length % 2) throw new Error("Delaunator output appears inconsistent"); if (o.triangles.length < 3) throw new Error("No edges in triangulation"); super(o); const l = 2 ** 32 - 1, m = o.coords.length >> 1, f = o.triangles.length; this.vertMap = new Uint32Array(m).fill(l), this.flips = new Kt(f), this.consd = new Kt(f); for (let r = 0; r < f; r++) { const v = o.triangles[r]; this.vertMap[v] === l && this.updateVert(r); } h && this.constrainAll(h); } /** * Constrain the triangulation such that there is an edge between p1 and p2. */ constrainOne(o, h) { const { triangles: l, halfedges: m } = this.del, f = this.vertMap[o]; let r = f; do { const a = l[r], d = Dt(r); if (a === h) return this.protect(r); const e = vt(r), c = l[e]; if (c === h) return this.protect(d), d; if (this.intersectSegments(o, h, c, a)) { r = e; break; } r = m[d]; } while (r !== -1 && r !== f); let v = r, S = -1; for (; r !== -1; ) { const a = m[r], d = vt(r), e = vt(a), c = Dt(a); if (a === -1) throw new Error("Constraining edge exited the hull"); if (this.consd.has(r)) throw new Error("Edge intersects already constrained edge"); if (this.isCollinear(o, h, l[r]) || this.isCollinear(o, h, l[a])) throw new Error("Constraining edge intersects point"); if (!this.intersectSegments( l[r], l[a], l[d], l[e] )) { if (S === -1 && (S = r), l[e] === h) { if (r === S) throw new Error("Infinite loop: non-convex quadrilateral"); r = S, S = -1; continue; } if (this.intersectSegments(o, h, l[e], l[a])) r = e; else if (this.intersectSegments(o, h, l[c], l[e])) r = c; else if (S === r) throw new Error("Infinite loop: no further intersect after non-convex"); continue; } if (this.flipDiagonal(r), this.intersectSegments(o, h, l[d], l[e]) && (S === -1 && (S = d), S === d)) throw new Error("Infinite loop: flipped diagonal still intersects"); l[e] === h ? (v = e, r = S, S = -1) : this.intersectSegments(o, h, l[c], l[e]) && (r = c); } return this.protect(v), this.delaunify(!0), this.findEdge(o, h); } /** * Fix the Delaunay condition. */ delaunify(o = !1) { const { halfedges: h } = this.del, l = this.flips, m = this.consd, f = h.length; do { var r = 0; for (let v = 0; v < f; v++) { if (m.has(v)) continue; l.delete(v); const S = h[v]; S !== -1 && (l.delete(S), this.isDelaunay(v) || (this.flipDiagonal(v), r++)); } } while (o && r > 0); return this; } /** * Call constrainOne on each edge */ constrainAll(o) { const h = o.length; for (let l = 0; l < h; l++) { const m = o[l]; this.constrainOne(m[0], m[1]); } return this; } /** * Whether an edge is constrained */ isConstrained(o) { return this.consd.has(o); } /** * Find the edge that points from p1 -> p2. If there is only an edge from * p2 -> p1 (i.e. it is on the hull), returns the negative id of it. */ findEdge(o, h) { const l = this.vertMap[h], { triangles: m, halfedges: f } = this.del; let r = l, v = -1; do { if (m[r] === o) return r; v = Dt(r), r = f[v]; } while (r !== -1 && r !== l); return m[Dt(v)] === o ? -v : 1 / 0; } /** * Mark an edge as constrained, i.e. should not be touched by `delaunify`. */ protect(o) { const h = this.del.halfedges[o], l = this.flips, m = this.consd; return l.delete(o), m.add(o), h !== -1 ? (l.delete(h), m.add(h), h) : -o; } /** * Mark an edge as flipped unless constrained. */ markFlip(o) { const h = this.del.halfedges, l = this.flips; if (this.consd.has(o)) return !1; const f = h[o]; return f !== -1 && (l.add(o), l.add(f)), !0; } /** * Flip the edge shared by two triangles. */ flipDiagonal(o) { const { triangles: h, halfedges: l } = this.del, m = this.flips, f = this.consd, r = l[o], v = vt(o), S = Dt(o), a = vt(r), d = Dt(r), e = l[v], c = l[a]; if (f.has(o)) throw new Error("Trying to flip a constrained edge"); return h[o] = h[a], l[o] = c, m.set(o, m.has(a)) || f.set(o, f.has(a)), c !== -1 && (l[c] = o), l[v] = a, h[r] = h[v], l[r] = e, m.set(r, m.has(v)) || f.set(r, f.has(v)), e !== -1 && (l[e] = r), l[a] = v, this.markFlip(o), this.markFlip(S), this.markFlip(r), this.markFlip(d), m.add(v), f.delete(v), m.add(a), f.delete(a), this.updateVert(o), this.updateVert(S), this.updateVert(r), this.updateVert(d), v; } /** * Whether point p1, p2, and p are collinear */ isCollinear(o, h, l) { const m = this.del.coords; return Ft( m[o * 2], m[o * 2 + 1], m[h * 2], m[h * 2 + 1], m[l * 2], m[l * 2 + 1] ) === 0; } /** * Whether px is in the circumcircle of the triangle formed by p1, p2, p3 */ inCircle(o, h, l, m) { const f = this.del.coords; return rn( f[o * 2], f[o * 2 + 1], f[h * 2], f[h * 2 + 1], f[l * 2], f[l * 2 + 1], f[m * 2], f[m * 2 + 1] ) < 0; } /** * Whether the triangles sharing edg conform to the Delaunay condition */ isDelaunay(o) { const { triangles: h, halfedges: l } = this.del, m = l[o]; if (m === -1) return !0; const f = h[vt(o)], r = h[o], v = h[Dt(o)], S = h[vt(m)]; return !this.inCircle(f, r, v, S); } /** * Update the vertex -> incoming edge map */ updateVert(o) { const { triangles: h, halfedges: l } = this.del, m = this.vertMap, f = h[o]; let r = vt(o), v = l[r]; for (; v !== -1 && v !== o; ) r = vt(v), v = l[r]; return m[f] = r, r; } /** * Whether the segments between vertices intersect */ intersectSegments(o, h, l, m) { const f = this.del.coords; return o === l || o === m || h === l || h === m ? !1 : Rt( f[o * 2], f[o * 2 + 1], f[h * 2], f[h * 2 + 1], f[l * 2], f[l * 2 + 1], f[m * 2], f[m * 2 + 1] ); } }; zt.intersectSegments = Rt; let Xt = zt; export { Xt as Constrain, Xt as default };