delaunator.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Delaunator = factory());
  5. }(this, (function () { 'use strict';
  6. const epsilon = 1.1102230246251565e-16;
  7. const splitter = 134217729;
  8. const resulterrbound = (3 + 8 * epsilon) * epsilon;
  9. // fast_expansion_sum_zeroelim routine from oritinal code
  10. function sum(elen, e, flen, f, h) {
  11. let Q, Qnew, hh, bvirt;
  12. let enow = e[0];
  13. let fnow = f[0];
  14. let eindex = 0;
  15. let findex = 0;
  16. if ((fnow > enow) === (fnow > -enow)) {
  17. Q = enow;
  18. enow = e[++eindex];
  19. } else {
  20. Q = fnow;
  21. fnow = f[++findex];
  22. }
  23. let hindex = 0;
  24. if (eindex < elen && findex < flen) {
  25. if ((fnow > enow) === (fnow > -enow)) {
  26. Qnew = enow + Q;
  27. hh = Q - (Qnew - enow);
  28. enow = e[++eindex];
  29. } else {
  30. Qnew = fnow + Q;
  31. hh = Q - (Qnew - fnow);
  32. fnow = f[++findex];
  33. }
  34. Q = Qnew;
  35. if (hh !== 0) {
  36. h[hindex++] = hh;
  37. }
  38. while (eindex < elen && findex < flen) {
  39. if ((fnow > enow) === (fnow > -enow)) {
  40. Qnew = Q + enow;
  41. bvirt = Qnew - Q;
  42. hh = Q - (Qnew - bvirt) + (enow - bvirt);
  43. enow = e[++eindex];
  44. } else {
  45. Qnew = Q + fnow;
  46. bvirt = Qnew - Q;
  47. hh = Q - (Qnew - bvirt) + (fnow - bvirt);
  48. fnow = f[++findex];
  49. }
  50. Q = Qnew;
  51. if (hh !== 0) {
  52. h[hindex++] = hh;
  53. }
  54. }
  55. }
  56. while (eindex < elen) {
  57. Qnew = Q + enow;
  58. bvirt = Qnew - Q;
  59. hh = Q - (Qnew - bvirt) + (enow - bvirt);
  60. enow = e[++eindex];
  61. Q = Qnew;
  62. if (hh !== 0) {
  63. h[hindex++] = hh;
  64. }
  65. }
  66. while (findex < flen) {
  67. Qnew = Q + fnow;
  68. bvirt = Qnew - Q;
  69. hh = Q - (Qnew - bvirt) + (fnow - bvirt);
  70. fnow = f[++findex];
  71. Q = Qnew;
  72. if (hh !== 0) {
  73. h[hindex++] = hh;
  74. }
  75. }
  76. if (Q !== 0 || hindex === 0) {
  77. h[hindex++] = Q;
  78. }
  79. return hindex;
  80. }
  81. function estimate(elen, e) {
  82. let Q = e[0];
  83. for (let i = 1; i < elen; i++) Q += e[i];
  84. return Q;
  85. }
  86. function vec(n) {
  87. return new Float64Array(n);
  88. }
  89. const ccwerrboundA = (3 + 16 * epsilon) * epsilon;
  90. const ccwerrboundB = (2 + 12 * epsilon) * epsilon;
  91. const ccwerrboundC = (9 + 64 * epsilon) * epsilon * epsilon;
  92. const B = vec(4);
  93. const C1 = vec(8);
  94. const C2 = vec(12);
  95. const D = vec(16);
  96. const u = vec(4);
  97. function orient2dadapt(ax, ay, bx, by, cx, cy, detsum) {
  98. let acxtail, acytail, bcxtail, bcytail;
  99. let bvirt, c, ahi, alo, bhi, blo, _i, _j, _0, s1, s0, t1, t0, u3;
  100. const acx = ax - cx;
  101. const bcx = bx - cx;
  102. const acy = ay - cy;
  103. const bcy = by - cy;
  104. s1 = acx * bcy;
  105. c = splitter * acx;
  106. ahi = c - (c - acx);
  107. alo = acx - ahi;
  108. c = splitter * bcy;
  109. bhi = c - (c - bcy);
  110. blo = bcy - bhi;
  111. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  112. t1 = acy * bcx;
  113. c = splitter * acy;
  114. ahi = c - (c - acy);
  115. alo = acy - ahi;
  116. c = splitter * bcx;
  117. bhi = c - (c - bcx);
  118. blo = bcx - bhi;
  119. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  120. _i = s0 - t0;
  121. bvirt = s0 - _i;
  122. B[0] = s0 - (_i + bvirt) + (bvirt - t0);
  123. _j = s1 + _i;
  124. bvirt = _j - s1;
  125. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  126. _i = _0 - t1;
  127. bvirt = _0 - _i;
  128. B[1] = _0 - (_i + bvirt) + (bvirt - t1);
  129. u3 = _j + _i;
  130. bvirt = u3 - _j;
  131. B[2] = _j - (u3 - bvirt) + (_i - bvirt);
  132. B[3] = u3;
  133. let det = estimate(4, B);
  134. let errbound = ccwerrboundB * detsum;
  135. if (det >= errbound || -det >= errbound) {
  136. return det;
  137. }
  138. bvirt = ax - acx;
  139. acxtail = ax - (acx + bvirt) + (bvirt - cx);
  140. bvirt = bx - bcx;
  141. bcxtail = bx - (bcx + bvirt) + (bvirt - cx);
  142. bvirt = ay - acy;
  143. acytail = ay - (acy + bvirt) + (bvirt - cy);
  144. bvirt = by - bcy;
  145. bcytail = by - (bcy + bvirt) + (bvirt - cy);
  146. if (acxtail === 0 && acytail === 0 && bcxtail === 0 && bcytail === 0) {
  147. return det;
  148. }
  149. errbound = ccwerrboundC * detsum + resulterrbound * Math.abs(det);
  150. det += (acx * bcytail + bcy * acxtail) - (acy * bcxtail + bcx * acytail);
  151. if (det >= errbound || -det >= errbound) return det;
  152. s1 = acxtail * bcy;
  153. c = splitter * acxtail;
  154. ahi = c - (c - acxtail);
  155. alo = acxtail - ahi;
  156. c = splitter * bcy;
  157. bhi = c - (c - bcy);
  158. blo = bcy - bhi;
  159. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  160. t1 = acytail * bcx;
  161. c = splitter * acytail;
  162. ahi = c - (c - acytail);
  163. alo = acytail - ahi;
  164. c = splitter * bcx;
  165. bhi = c - (c - bcx);
  166. blo = bcx - bhi;
  167. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  168. _i = s0 - t0;
  169. bvirt = s0 - _i;
  170. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  171. _j = s1 + _i;
  172. bvirt = _j - s1;
  173. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  174. _i = _0 - t1;
  175. bvirt = _0 - _i;
  176. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  177. u3 = _j + _i;
  178. bvirt = u3 - _j;
  179. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  180. u[3] = u3;
  181. const C1len = sum(4, B, 4, u, C1);
  182. s1 = acx * bcytail;
  183. c = splitter * acx;
  184. ahi = c - (c - acx);
  185. alo = acx - ahi;
  186. c = splitter * bcytail;
  187. bhi = c - (c - bcytail);
  188. blo = bcytail - bhi;
  189. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  190. t1 = acy * bcxtail;
  191. c = splitter * acy;
  192. ahi = c - (c - acy);
  193. alo = acy - ahi;
  194. c = splitter * bcxtail;
  195. bhi = c - (c - bcxtail);
  196. blo = bcxtail - bhi;
  197. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  198. _i = s0 - t0;
  199. bvirt = s0 - _i;
  200. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  201. _j = s1 + _i;
  202. bvirt = _j - s1;
  203. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  204. _i = _0 - t1;
  205. bvirt = _0 - _i;
  206. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  207. u3 = _j + _i;
  208. bvirt = u3 - _j;
  209. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  210. u[3] = u3;
  211. const C2len = sum(C1len, C1, 4, u, C2);
  212. s1 = acxtail * bcytail;
  213. c = splitter * acxtail;
  214. ahi = c - (c - acxtail);
  215. alo = acxtail - ahi;
  216. c = splitter * bcytail;
  217. bhi = c - (c - bcytail);
  218. blo = bcytail - bhi;
  219. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  220. t1 = acytail * bcxtail;
  221. c = splitter * acytail;
  222. ahi = c - (c - acytail);
  223. alo = acytail - ahi;
  224. c = splitter * bcxtail;
  225. bhi = c - (c - bcxtail);
  226. blo = bcxtail - bhi;
  227. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  228. _i = s0 - t0;
  229. bvirt = s0 - _i;
  230. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  231. _j = s1 + _i;
  232. bvirt = _j - s1;
  233. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  234. _i = _0 - t1;
  235. bvirt = _0 - _i;
  236. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  237. u3 = _j + _i;
  238. bvirt = u3 - _j;
  239. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  240. u[3] = u3;
  241. const Dlen = sum(C2len, C2, 4, u, D);
  242. return D[Dlen - 1];
  243. }
  244. function orient2d(ax, ay, bx, by, cx, cy) {
  245. const detleft = (ay - cy) * (bx - cx);
  246. const detright = (ax - cx) * (by - cy);
  247. const det = detleft - detright;
  248. if (detleft === 0 || detright === 0 || (detleft > 0) !== (detright > 0)) return det;
  249. const detsum = Math.abs(detleft + detright);
  250. if (Math.abs(det) >= ccwerrboundA * detsum) return det;
  251. return -orient2dadapt(ax, ay, bx, by, cx, cy, detsum);
  252. }
  253. const EPSILON = Math.pow(2, -52);
  254. const EDGE_STACK = new Uint32Array(512);
  255. class Delaunator {
  256. static from(points, getX = defaultGetX, getY = defaultGetY) {
  257. const n = points.length;
  258. const coords = new Float64Array(n * 2);
  259. for (let i = 0; i < n; i++) {
  260. const p = points[i];
  261. coords[2 * i] = getX(p);
  262. coords[2 * i + 1] = getY(p);
  263. }
  264. return new Delaunator(coords);
  265. }
  266. constructor(coords) {
  267. const n = coords.length >> 1;
  268. if (n > 0 && typeof coords[0] !== 'number') throw new Error('Expected coords to contain numbers.');
  269. this.coords = coords;
  270. // arrays that will store the triangulation graph
  271. const maxTriangles = Math.max(2 * n - 5, 0);
  272. this._triangles = new Uint32Array(maxTriangles * 3);
  273. this._halfedges = new Int32Array(maxTriangles * 3);
  274. // temporary arrays for tracking the edges of the advancing convex hull
  275. this._hashSize = Math.ceil(Math.sqrt(n));
  276. this._hullPrev = new Uint32Array(n); // edge to prev edge
  277. this._hullNext = new Uint32Array(n); // edge to next edge
  278. this._hullTri = new Uint32Array(n); // edge to adjacent triangle
  279. this._hullHash = new Int32Array(this._hashSize).fill(-1); // angular edge hash
  280. // temporary arrays for sorting points
  281. this._ids = new Uint32Array(n);
  282. this._dists = new Float64Array(n);
  283. this.update();
  284. }
  285. update() {
  286. const {coords, _hullPrev: hullPrev, _hullNext: hullNext, _hullTri: hullTri, _hullHash: hullHash} = this;
  287. const n = coords.length >> 1;
  288. // populate an array of point indices; calculate input data bbox
  289. let minX = Infinity;
  290. let minY = Infinity;
  291. let maxX = -Infinity;
  292. let maxY = -Infinity;
  293. for (let i = 0; i < n; i++) {
  294. const x = coords[2 * i];
  295. const y = coords[2 * i + 1];
  296. if (x < minX) minX = x;
  297. if (y < minY) minY = y;
  298. if (x > maxX) maxX = x;
  299. if (y > maxY) maxY = y;
  300. this._ids[i] = i;
  301. }
  302. const cx = (minX + maxX) / 2;
  303. const cy = (minY + maxY) / 2;
  304. let minDist = Infinity;
  305. let i0, i1, i2;
  306. // pick a seed point close to the center
  307. for (let i = 0; i < n; i++) {
  308. const d = dist(cx, cy, coords[2 * i], coords[2 * i + 1]);
  309. if (d < minDist) {
  310. i0 = i;
  311. minDist = d;
  312. }
  313. }
  314. const i0x = coords[2 * i0];
  315. const i0y = coords[2 * i0 + 1];
  316. minDist = Infinity;
  317. // find the point closest to the seed
  318. for (let i = 0; i < n; i++) {
  319. if (i === i0) continue;
  320. const d = dist(i0x, i0y, coords[2 * i], coords[2 * i + 1]);
  321. if (d < minDist && d > 0) {
  322. i1 = i;
  323. minDist = d;
  324. }
  325. }
  326. let i1x = coords[2 * i1];
  327. let i1y = coords[2 * i1 + 1];
  328. let minRadius = Infinity;
  329. // find the third point which forms the smallest circumcircle with the first two
  330. for (let i = 0; i < n; i++) {
  331. if (i === i0 || i === i1) continue;
  332. const r = circumradius(i0x, i0y, i1x, i1y, coords[2 * i], coords[2 * i + 1]);
  333. if (r < minRadius) {
  334. i2 = i;
  335. minRadius = r;
  336. }
  337. }
  338. let i2x = coords[2 * i2];
  339. let i2y = coords[2 * i2 + 1];
  340. if (minRadius === Infinity) {
  341. // order collinear points by dx (or dy if all x are identical)
  342. // and return the list as a hull
  343. for (let i = 0; i < n; i++) {
  344. this._dists[i] = (coords[2 * i] - coords[0]) || (coords[2 * i + 1] - coords[1]);
  345. }
  346. quicksort(this._ids, this._dists, 0, n - 1);
  347. const hull = new Uint32Array(n);
  348. let j = 0;
  349. for (let i = 0, d0 = -Infinity; i < n; i++) {
  350. const id = this._ids[i];
  351. if (this._dists[id] > d0) {
  352. hull[j++] = id;
  353. d0 = this._dists[id];
  354. }
  355. }
  356. this.hull = hull.subarray(0, j);
  357. this.triangles = new Uint32Array(0);
  358. this.halfedges = new Uint32Array(0);
  359. return;
  360. }
  361. // swap the order of the seed points for counter-clockwise orientation
  362. if (orient2d(i0x, i0y, i1x, i1y, i2x, i2y) < 0) {
  363. const i = i1;
  364. const x = i1x;
  365. const y = i1y;
  366. i1 = i2;
  367. i1x = i2x;
  368. i1y = i2y;
  369. i2 = i;
  370. i2x = x;
  371. i2y = y;
  372. }
  373. const center = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
  374. this._cx = center.x;
  375. this._cy = center.y;
  376. for (let i = 0; i < n; i++) {
  377. this._dists[i] = dist(coords[2 * i], coords[2 * i + 1], center.x, center.y);
  378. }
  379. // sort the points by distance from the seed triangle circumcenter
  380. quicksort(this._ids, this._dists, 0, n - 1);
  381. // set up the seed triangle as the starting hull
  382. this._hullStart = i0;
  383. let hullSize = 3;
  384. hullNext[i0] = hullPrev[i2] = i1;
  385. hullNext[i1] = hullPrev[i0] = i2;
  386. hullNext[i2] = hullPrev[i1] = i0;
  387. hullTri[i0] = 0;
  388. hullTri[i1] = 1;
  389. hullTri[i2] = 2;
  390. hullHash.fill(-1);
  391. hullHash[this._hashKey(i0x, i0y)] = i0;
  392. hullHash[this._hashKey(i1x, i1y)] = i1;
  393. hullHash[this._hashKey(i2x, i2y)] = i2;
  394. this.trianglesLen = 0;
  395. this._addTriangle(i0, i1, i2, -1, -1, -1);
  396. for (let k = 0, xp, yp; k < this._ids.length; k++) {
  397. const i = this._ids[k];
  398. const x = coords[2 * i];
  399. const y = coords[2 * i + 1];
  400. // skip near-duplicate points
  401. if (k > 0 && Math.abs(x - xp) <= EPSILON && Math.abs(y - yp) <= EPSILON) continue;
  402. xp = x;
  403. yp = y;
  404. // skip seed triangle points
  405. if (i === i0 || i === i1 || i === i2) continue;
  406. // find a visible edge on the convex hull using edge hash
  407. let start = 0;
  408. for (let j = 0, key = this._hashKey(x, y); j < this._hashSize; j++) {
  409. start = hullHash[(key + j) % this._hashSize];
  410. if (start !== -1 && start !== hullNext[start]) break;
  411. }
  412. start = hullPrev[start];
  413. let e = start, q;
  414. while (q = hullNext[e], orient2d(x, y, coords[2 * e], coords[2 * e + 1], coords[2 * q], coords[2 * q + 1]) >= 0) {
  415. e = q;
  416. if (e === start) {
  417. e = -1;
  418. break;
  419. }
  420. }
  421. if (e === -1) continue; // likely a near-duplicate point; skip it
  422. // add the first triangle from the point
  423. let t = this._addTriangle(e, i, hullNext[e], -1, -1, hullTri[e]);
  424. // recursively flip triangles from the point until they satisfy the Delaunay condition
  425. hullTri[i] = this._legalize(t + 2);
  426. hullTri[e] = t; // keep track of boundary triangles on the hull
  427. hullSize++;
  428. // walk forward through the hull, adding more triangles and flipping recursively
  429. let n = hullNext[e];
  430. while (q = hullNext[n], orient2d(x, y, coords[2 * n], coords[2 * n + 1], coords[2 * q], coords[2 * q + 1]) < 0) {
  431. t = this._addTriangle(n, i, q, hullTri[i], -1, hullTri[n]);
  432. hullTri[i] = this._legalize(t + 2);
  433. hullNext[n] = n; // mark as removed
  434. hullSize--;
  435. n = q;
  436. }
  437. // walk backward from the other side, adding more triangles and flipping
  438. if (e === start) {
  439. while (q = hullPrev[e], orient2d(x, y, coords[2 * q], coords[2 * q + 1], coords[2 * e], coords[2 * e + 1]) < 0) {
  440. t = this._addTriangle(q, i, e, -1, hullTri[e], hullTri[q]);
  441. this._legalize(t + 2);
  442. hullTri[q] = t;
  443. hullNext[e] = e; // mark as removed
  444. hullSize--;
  445. e = q;
  446. }
  447. }
  448. // update the hull indices
  449. this._hullStart = hullPrev[i] = e;
  450. hullNext[e] = hullPrev[n] = i;
  451. hullNext[i] = n;
  452. // save the two new edges in the hash table
  453. hullHash[this._hashKey(x, y)] = i;
  454. hullHash[this._hashKey(coords[2 * e], coords[2 * e + 1])] = e;
  455. }
  456. this.hull = new Uint32Array(hullSize);
  457. for (let i = 0, e = this._hullStart; i < hullSize; i++) {
  458. this.hull[i] = e;
  459. e = hullNext[e];
  460. }
  461. // trim typed triangle mesh arrays
  462. this.triangles = this._triangles.subarray(0, this.trianglesLen);
  463. this.halfedges = this._halfedges.subarray(0, this.trianglesLen);
  464. }
  465. _hashKey(x, y) {
  466. return Math.floor(pseudoAngle(x - this._cx, y - this._cy) * this._hashSize) % this._hashSize;
  467. }
  468. _legalize(a) {
  469. const {_triangles: triangles, _halfedges: halfedges, coords} = this;
  470. let i = 0;
  471. let ar = 0;
  472. // recursion eliminated with a fixed-size stack
  473. while (true) {
  474. const b = halfedges[a];
  475. /* if the pair of triangles doesn't satisfy the Delaunay condition
  476. * (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
  477. * then do the same check/flip recursively for the new pair of triangles
  478. *
  479. * pl pl
  480. * /||\ / \
  481. * al/ || \bl al/ \a
  482. * / || \ / \
  483. * / a||b \ flip /___ar___\
  484. * p0\ || /p1 => p0\---bl---/p1
  485. * \ || / \ /
  486. * ar\ || /br b\ /br
  487. * \||/ \ /
  488. * pr pr
  489. */
  490. const a0 = a - a % 3;
  491. ar = a0 + (a + 2) % 3;
  492. if (b === -1) { // convex hull edge
  493. if (i === 0) break;
  494. a = EDGE_STACK[--i];
  495. continue;
  496. }
  497. const b0 = b - b % 3;
  498. const al = a0 + (a + 1) % 3;
  499. const bl = b0 + (b + 2) % 3;
  500. const p0 = triangles[ar];
  501. const pr = triangles[a];
  502. const pl = triangles[al];
  503. const p1 = triangles[bl];
  504. const illegal = inCircle(
  505. coords[2 * p0], coords[2 * p0 + 1],
  506. coords[2 * pr], coords[2 * pr + 1],
  507. coords[2 * pl], coords[2 * pl + 1],
  508. coords[2 * p1], coords[2 * p1 + 1]);
  509. if (illegal) {
  510. triangles[a] = p1;
  511. triangles[b] = p0;
  512. const hbl = halfedges[bl];
  513. // edge swapped on the other side of the hull (rare); fix the halfedge reference
  514. if (hbl === -1) {
  515. let e = this._hullStart;
  516. do {
  517. if (this._hullTri[e] === bl) {
  518. this._hullTri[e] = a;
  519. break;
  520. }
  521. e = this._hullPrev[e];
  522. } while (e !== this._hullStart);
  523. }
  524. this._link(a, hbl);
  525. this._link(b, halfedges[ar]);
  526. this._link(ar, bl);
  527. const br = b0 + (b + 1) % 3;
  528. // don't worry about hitting the cap: it can only happen on extremely degenerate input
  529. if (i < EDGE_STACK.length) {
  530. EDGE_STACK[i++] = br;
  531. }
  532. } else {
  533. if (i === 0) break;
  534. a = EDGE_STACK[--i];
  535. }
  536. }
  537. return ar;
  538. }
  539. _link(a, b) {
  540. this._halfedges[a] = b;
  541. if (b !== -1) this._halfedges[b] = a;
  542. }
  543. // add a new triangle given vertex indices and adjacent half-edge ids
  544. _addTriangle(i0, i1, i2, a, b, c) {
  545. const t = this.trianglesLen;
  546. this._triangles[t] = i0;
  547. this._triangles[t + 1] = i1;
  548. this._triangles[t + 2] = i2;
  549. this._link(t, a);
  550. this._link(t + 1, b);
  551. this._link(t + 2, c);
  552. this.trianglesLen += 3;
  553. return t;
  554. }
  555. }
  556. // monotonically increases with real angle, but doesn't need expensive trigonometry
  557. function pseudoAngle(dx, dy) {
  558. const p = dx / (Math.abs(dx) + Math.abs(dy));
  559. return (dy > 0 ? 3 - p : 1 + p) / 4; // [0..1]
  560. }
  561. function dist(ax, ay, bx, by) {
  562. const dx = ax - bx;
  563. const dy = ay - by;
  564. return dx * dx + dy * dy;
  565. }
  566. function inCircle(ax, ay, bx, by, cx, cy, px, py) {
  567. const dx = ax - px;
  568. const dy = ay - py;
  569. const ex = bx - px;
  570. const ey = by - py;
  571. const fx = cx - px;
  572. const fy = cy - py;
  573. const ap = dx * dx + dy * dy;
  574. const bp = ex * ex + ey * ey;
  575. const cp = fx * fx + fy * fy;
  576. return dx * (ey * cp - bp * fy) -
  577. dy * (ex * cp - bp * fx) +
  578. ap * (ex * fy - ey * fx) < 0;
  579. }
  580. function circumradius(ax, ay, bx, by, cx, cy) {
  581. const dx = bx - ax;
  582. const dy = by - ay;
  583. const ex = cx - ax;
  584. const ey = cy - ay;
  585. const bl = dx * dx + dy * dy;
  586. const cl = ex * ex + ey * ey;
  587. const d = 0.5 / (dx * ey - dy * ex);
  588. const x = (ey * bl - dy * cl) * d;
  589. const y = (dx * cl - ex * bl) * d;
  590. return x * x + y * y;
  591. }
  592. function circumcenter(ax, ay, bx, by, cx, cy) {
  593. const dx = bx - ax;
  594. const dy = by - ay;
  595. const ex = cx - ax;
  596. const ey = cy - ay;
  597. const bl = dx * dx + dy * dy;
  598. const cl = ex * ex + ey * ey;
  599. const d = 0.5 / (dx * ey - dy * ex);
  600. const x = ax + (ey * bl - dy * cl) * d;
  601. const y = ay + (dx * cl - ex * bl) * d;
  602. return {x, y};
  603. }
  604. function quicksort(ids, dists, left, right) {
  605. if (right - left <= 20) {
  606. for (let i = left + 1; i <= right; i++) {
  607. const temp = ids[i];
  608. const tempDist = dists[temp];
  609. let j = i - 1;
  610. while (j >= left && dists[ids[j]] > tempDist) ids[j + 1] = ids[j--];
  611. ids[j + 1] = temp;
  612. }
  613. } else {
  614. const median = (left + right) >> 1;
  615. let i = left + 1;
  616. let j = right;
  617. swap(ids, median, i);
  618. if (dists[ids[left]] > dists[ids[right]]) swap(ids, left, right);
  619. if (dists[ids[i]] > dists[ids[right]]) swap(ids, i, right);
  620. if (dists[ids[left]] > dists[ids[i]]) swap(ids, left, i);
  621. const temp = ids[i];
  622. const tempDist = dists[temp];
  623. while (true) {
  624. do i++; while (dists[ids[i]] < tempDist);
  625. do j--; while (dists[ids[j]] > tempDist);
  626. if (j < i) break;
  627. swap(ids, i, j);
  628. }
  629. ids[left + 1] = ids[j];
  630. ids[j] = temp;
  631. if (right - i + 1 >= j - left) {
  632. quicksort(ids, dists, i, right);
  633. quicksort(ids, dists, left, j - 1);
  634. } else {
  635. quicksort(ids, dists, left, j - 1);
  636. quicksort(ids, dists, i, right);
  637. }
  638. }
  639. }
  640. function swap(arr, i, j) {
  641. const tmp = arr[i];
  642. arr[i] = arr[j];
  643. arr[j] = tmp;
  644. }
  645. function defaultGetX(p) {
  646. return p[0];
  647. }
  648. function defaultGetY(p) {
  649. return p[1];
  650. }
  651. return Delaunator;
  652. })));