Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

delaunator.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. const detsum = Math.abs(detleft + detright);
  249. if (Math.abs(det) >= ccwerrboundA * detsum) return det;
  250. return -orient2dadapt(ax, ay, bx, by, cx, cy, detsum);
  251. }
  252. const EPSILON = Math.pow(2, -52);
  253. const EDGE_STACK = new Uint32Array(512);
  254. class Delaunator {
  255. static from(points, getX = defaultGetX, getY = defaultGetY) {
  256. const n = points.length;
  257. const coords = new Float64Array(n * 2);
  258. for (let i = 0; i < n; i++) {
  259. const p = points[i];
  260. coords[2 * i] = getX(p);
  261. coords[2 * i + 1] = getY(p);
  262. }
  263. return new Delaunator(coords);
  264. }
  265. constructor(coords) {
  266. const n = coords.length >> 1;
  267. if (n > 0 && typeof coords[0] !== 'number') throw new Error('Expected coords to contain numbers.');
  268. this.coords = coords;
  269. // arrays that will store the triangulation graph
  270. const maxTriangles = Math.max(2 * n - 5, 0);
  271. this._triangles = new Uint32Array(maxTriangles * 3);
  272. this._halfedges = new Int32Array(maxTriangles * 3);
  273. // temporary arrays for tracking the edges of the advancing convex hull
  274. this._hashSize = Math.ceil(Math.sqrt(n));
  275. this._hullPrev = new Uint32Array(n); // edge to prev edge
  276. this._hullNext = new Uint32Array(n); // edge to next edge
  277. this._hullTri = new Uint32Array(n); // edge to adjacent triangle
  278. this._hullHash = new Int32Array(this._hashSize); // angular edge hash
  279. // temporary arrays for sorting points
  280. this._ids = new Uint32Array(n);
  281. this._dists = new Float64Array(n);
  282. this.update();
  283. }
  284. update() {
  285. const {coords, _hullPrev: hullPrev, _hullNext: hullNext, _hullTri: hullTri, _hullHash: hullHash} = this;
  286. const n = coords.length >> 1;
  287. // populate an array of point indices; calculate input data bbox
  288. let minX = Infinity;
  289. let minY = Infinity;
  290. let maxX = -Infinity;
  291. let maxY = -Infinity;
  292. for (let i = 0; i < n; i++) {
  293. const x = coords[2 * i];
  294. const y = coords[2 * i + 1];
  295. if (x < minX) minX = x;
  296. if (y < minY) minY = y;
  297. if (x > maxX) maxX = x;
  298. if (y > maxY) maxY = y;
  299. this._ids[i] = i;
  300. }
  301. const cx = (minX + maxX) / 2;
  302. const cy = (minY + maxY) / 2;
  303. let i0, i1, i2;
  304. // pick a seed point close to the center
  305. for (let i = 0, minDist = Infinity; i < n; i++) {
  306. const d = dist(cx, cy, coords[2 * i], coords[2 * i + 1]);
  307. if (d < minDist) {
  308. i0 = i;
  309. minDist = d;
  310. }
  311. }
  312. const i0x = coords[2 * i0];
  313. const i0y = coords[2 * i0 + 1];
  314. // find the point closest to the seed
  315. for (let i = 0, minDist = Infinity; i < n; i++) {
  316. if (i === i0) continue;
  317. const d = dist(i0x, i0y, coords[2 * i], coords[2 * i + 1]);
  318. if (d < minDist && d > 0) {
  319. i1 = i;
  320. minDist = d;
  321. }
  322. }
  323. let i1x = coords[2 * i1];
  324. let i1y = coords[2 * i1 + 1];
  325. let minRadius = Infinity;
  326. // find the third point which forms the smallest circumcircle with the first two
  327. for (let i = 0; i < n; i++) {
  328. if (i === i0 || i === i1) continue;
  329. const r = circumradius(i0x, i0y, i1x, i1y, coords[2 * i], coords[2 * i + 1]);
  330. if (r < minRadius) {
  331. i2 = i;
  332. minRadius = r;
  333. }
  334. }
  335. let i2x = coords[2 * i2];
  336. let i2y = coords[2 * i2 + 1];
  337. if (minRadius === Infinity) {
  338. // order collinear points by dx (or dy if all x are identical)
  339. // and return the list as a hull
  340. for (let i = 0; i < n; i++) {
  341. this._dists[i] = (coords[2 * i] - coords[0]) || (coords[2 * i + 1] - coords[1]);
  342. }
  343. quicksort(this._ids, this._dists, 0, n - 1);
  344. const hull = new Uint32Array(n);
  345. let j = 0;
  346. for (let i = 0, d0 = -Infinity; i < n; i++) {
  347. const id = this._ids[i];
  348. const d = this._dists[id];
  349. if (d > d0) {
  350. hull[j++] = id;
  351. d0 = d;
  352. }
  353. }
  354. this.hull = hull.subarray(0, j);
  355. this.triangles = new Uint32Array(0);
  356. this.halfedges = new Uint32Array(0);
  357. return;
  358. }
  359. // swap the order of the seed points for counter-clockwise orientation
  360. if (orient2d(i0x, i0y, i1x, i1y, i2x, i2y) < 0) {
  361. const i = i1;
  362. const x = i1x;
  363. const y = i1y;
  364. i1 = i2;
  365. i1x = i2x;
  366. i1y = i2y;
  367. i2 = i;
  368. i2x = x;
  369. i2y = y;
  370. }
  371. const center = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
  372. this._cx = center.x;
  373. this._cy = center.y;
  374. for (let i = 0; i < n; i++) {
  375. this._dists[i] = dist(coords[2 * i], coords[2 * i + 1], center.x, center.y);
  376. }
  377. // sort the points by distance from the seed triangle circumcenter
  378. quicksort(this._ids, this._dists, 0, n - 1);
  379. // set up the seed triangle as the starting hull
  380. this._hullStart = i0;
  381. let hullSize = 3;
  382. hullNext[i0] = hullPrev[i2] = i1;
  383. hullNext[i1] = hullPrev[i0] = i2;
  384. hullNext[i2] = hullPrev[i1] = i0;
  385. hullTri[i0] = 0;
  386. hullTri[i1] = 1;
  387. hullTri[i2] = 2;
  388. hullHash.fill(-1);
  389. hullHash[this._hashKey(i0x, i0y)] = i0;
  390. hullHash[this._hashKey(i1x, i1y)] = i1;
  391. hullHash[this._hashKey(i2x, i2y)] = i2;
  392. this.trianglesLen = 0;
  393. this._addTriangle(i0, i1, i2, -1, -1, -1);
  394. for (let k = 0, xp, yp; k < this._ids.length; k++) {
  395. const i = this._ids[k];
  396. const x = coords[2 * i];
  397. const y = coords[2 * i + 1];
  398. // skip near-duplicate points
  399. if (k > 0 && Math.abs(x - xp) <= EPSILON && Math.abs(y - yp) <= EPSILON) continue;
  400. xp = x;
  401. yp = y;
  402. // skip seed triangle points
  403. if (i === i0 || i === i1 || i === i2) continue;
  404. // find a visible edge on the convex hull using edge hash
  405. let start = 0;
  406. for (let j = 0, key = this._hashKey(x, y); j < this._hashSize; j++) {
  407. start = hullHash[(key + j) % this._hashSize];
  408. if (start !== -1 && start !== hullNext[start]) break;
  409. }
  410. start = hullPrev[start];
  411. let e = start, q;
  412. while (q = hullNext[e], orient2d(x, y, coords[2 * e], coords[2 * e + 1], coords[2 * q], coords[2 * q + 1]) >= 0) {
  413. e = q;
  414. if (e === start) {
  415. e = -1;
  416. break;
  417. }
  418. }
  419. if (e === -1) continue; // likely a near-duplicate point; skip it
  420. // add the first triangle from the point
  421. let t = this._addTriangle(e, i, hullNext[e], -1, -1, hullTri[e]);
  422. // recursively flip triangles from the point until they satisfy the Delaunay condition
  423. hullTri[i] = this._legalize(t + 2);
  424. hullTri[e] = t; // keep track of boundary triangles on the hull
  425. hullSize++;
  426. // walk forward through the hull, adding more triangles and flipping recursively
  427. let n = hullNext[e];
  428. while (q = hullNext[n], orient2d(x, y, coords[2 * n], coords[2 * n + 1], coords[2 * q], coords[2 * q + 1]) < 0) {
  429. t = this._addTriangle(n, i, q, hullTri[i], -1, hullTri[n]);
  430. hullTri[i] = this._legalize(t + 2);
  431. hullNext[n] = n; // mark as removed
  432. hullSize--;
  433. n = q;
  434. }
  435. // walk backward from the other side, adding more triangles and flipping
  436. if (e === start) {
  437. while (q = hullPrev[e], orient2d(x, y, coords[2 * q], coords[2 * q + 1], coords[2 * e], coords[2 * e + 1]) < 0) {
  438. t = this._addTriangle(q, i, e, -1, hullTri[e], hullTri[q]);
  439. this._legalize(t + 2);
  440. hullTri[q] = t;
  441. hullNext[e] = e; // mark as removed
  442. hullSize--;
  443. e = q;
  444. }
  445. }
  446. // update the hull indices
  447. this._hullStart = hullPrev[i] = e;
  448. hullNext[e] = hullPrev[n] = i;
  449. hullNext[i] = n;
  450. // save the two new edges in the hash table
  451. hullHash[this._hashKey(x, y)] = i;
  452. hullHash[this._hashKey(coords[2 * e], coords[2 * e + 1])] = e;
  453. }
  454. this.hull = new Uint32Array(hullSize);
  455. for (let i = 0, e = this._hullStart; i < hullSize; i++) {
  456. this.hull[i] = e;
  457. e = hullNext[e];
  458. }
  459. // trim typed triangle mesh arrays
  460. this.triangles = this._triangles.subarray(0, this.trianglesLen);
  461. this.halfedges = this._halfedges.subarray(0, this.trianglesLen);
  462. }
  463. _hashKey(x, y) {
  464. return Math.floor(pseudoAngle(x - this._cx, y - this._cy) * this._hashSize) % this._hashSize;
  465. }
  466. _legalize(a) {
  467. const {_triangles: triangles, _halfedges: halfedges, coords} = this;
  468. let i = 0;
  469. let ar = 0;
  470. // recursion eliminated with a fixed-size stack
  471. while (true) {
  472. const b = halfedges[a];
  473. /* if the pair of triangles doesn't satisfy the Delaunay condition
  474. * (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
  475. * then do the same check/flip recursively for the new pair of triangles
  476. *
  477. * pl pl
  478. * /||\ / \
  479. * al/ || \bl al/ \a
  480. * / || \ / \
  481. * / a||b \ flip /___ar___\
  482. * p0\ || /p1 => p0\---bl---/p1
  483. * \ || / \ /
  484. * ar\ || /br b\ /br
  485. * \||/ \ /
  486. * pr pr
  487. */
  488. const a0 = a - a % 3;
  489. ar = a0 + (a + 2) % 3;
  490. if (b === -1) { // convex hull edge
  491. if (i === 0) break;
  492. a = EDGE_STACK[--i];
  493. continue;
  494. }
  495. const b0 = b - b % 3;
  496. const al = a0 + (a + 1) % 3;
  497. const bl = b0 + (b + 2) % 3;
  498. const p0 = triangles[ar];
  499. const pr = triangles[a];
  500. const pl = triangles[al];
  501. const p1 = triangles[bl];
  502. const illegal = inCircle(
  503. coords[2 * p0], coords[2 * p0 + 1],
  504. coords[2 * pr], coords[2 * pr + 1],
  505. coords[2 * pl], coords[2 * pl + 1],
  506. coords[2 * p1], coords[2 * p1 + 1]);
  507. if (illegal) {
  508. triangles[a] = p1;
  509. triangles[b] = p0;
  510. const hbl = halfedges[bl];
  511. // edge swapped on the other side of the hull (rare); fix the halfedge reference
  512. if (hbl === -1) {
  513. let e = this._hullStart;
  514. do {
  515. if (this._hullTri[e] === bl) {
  516. this._hullTri[e] = a;
  517. break;
  518. }
  519. e = this._hullPrev[e];
  520. } while (e !== this._hullStart);
  521. }
  522. this._link(a, hbl);
  523. this._link(b, halfedges[ar]);
  524. this._link(ar, bl);
  525. const br = b0 + (b + 1) % 3;
  526. // don't worry about hitting the cap: it can only happen on extremely degenerate input
  527. if (i < EDGE_STACK.length) {
  528. EDGE_STACK[i++] = br;
  529. }
  530. } else {
  531. if (i === 0) break;
  532. a = EDGE_STACK[--i];
  533. }
  534. }
  535. return ar;
  536. }
  537. _link(a, b) {
  538. this._halfedges[a] = b;
  539. if (b !== -1) this._halfedges[b] = a;
  540. }
  541. // add a new triangle given vertex indices and adjacent half-edge ids
  542. _addTriangle(i0, i1, i2, a, b, c) {
  543. const t = this.trianglesLen;
  544. this._triangles[t] = i0;
  545. this._triangles[t + 1] = i1;
  546. this._triangles[t + 2] = i2;
  547. this._link(t, a);
  548. this._link(t + 1, b);
  549. this._link(t + 2, c);
  550. this.trianglesLen += 3;
  551. return t;
  552. }
  553. }
  554. // monotonically increases with real angle, but doesn't need expensive trigonometry
  555. function pseudoAngle(dx, dy) {
  556. const p = dx / (Math.abs(dx) + Math.abs(dy));
  557. return (dy > 0 ? 3 - p : 1 + p) / 4; // [0..1]
  558. }
  559. function dist(ax, ay, bx, by) {
  560. const dx = ax - bx;
  561. const dy = ay - by;
  562. return dx * dx + dy * dy;
  563. }
  564. function inCircle(ax, ay, bx, by, cx, cy, px, py) {
  565. const dx = ax - px;
  566. const dy = ay - py;
  567. const ex = bx - px;
  568. const ey = by - py;
  569. const fx = cx - px;
  570. const fy = cy - py;
  571. const ap = dx * dx + dy * dy;
  572. const bp = ex * ex + ey * ey;
  573. const cp = fx * fx + fy * fy;
  574. return dx * (ey * cp - bp * fy) -
  575. dy * (ex * cp - bp * fx) +
  576. ap * (ex * fy - ey * fx) < 0;
  577. }
  578. function circumradius(ax, ay, bx, by, cx, cy) {
  579. const dx = bx - ax;
  580. const dy = by - ay;
  581. const ex = cx - ax;
  582. const ey = cy - ay;
  583. const bl = dx * dx + dy * dy;
  584. const cl = ex * ex + ey * ey;
  585. const d = 0.5 / (dx * ey - dy * ex);
  586. const x = (ey * bl - dy * cl) * d;
  587. const y = (dx * cl - ex * bl) * d;
  588. return x * x + y * y;
  589. }
  590. function circumcenter(ax, ay, bx, by, cx, cy) {
  591. const dx = bx - ax;
  592. const dy = by - ay;
  593. const ex = cx - ax;
  594. const ey = cy - ay;
  595. const bl = dx * dx + dy * dy;
  596. const cl = ex * ex + ey * ey;
  597. const d = 0.5 / (dx * ey - dy * ex);
  598. const x = ax + (ey * bl - dy * cl) * d;
  599. const y = ay + (dx * cl - ex * bl) * d;
  600. return {x, y};
  601. }
  602. function quicksort(ids, dists, left, right) {
  603. if (right - left <= 20) {
  604. for (let i = left + 1; i <= right; i++) {
  605. const temp = ids[i];
  606. const tempDist = dists[temp];
  607. let j = i - 1;
  608. while (j >= left && dists[ids[j]] > tempDist) ids[j + 1] = ids[j--];
  609. ids[j + 1] = temp;
  610. }
  611. } else {
  612. const median = (left + right) >> 1;
  613. let i = left + 1;
  614. let j = right;
  615. swap(ids, median, i);
  616. if (dists[ids[left]] > dists[ids[right]]) swap(ids, left, right);
  617. if (dists[ids[i]] > dists[ids[right]]) swap(ids, i, right);
  618. if (dists[ids[left]] > dists[ids[i]]) swap(ids, left, i);
  619. const temp = ids[i];
  620. const tempDist = dists[temp];
  621. while (true) {
  622. do i++; while (dists[ids[i]] < tempDist);
  623. do j--; while (dists[ids[j]] > tempDist);
  624. if (j < i) break;
  625. swap(ids, i, j);
  626. }
  627. ids[left + 1] = ids[j];
  628. ids[j] = temp;
  629. if (right - i + 1 >= j - left) {
  630. quicksort(ids, dists, i, right);
  631. quicksort(ids, dists, left, j - 1);
  632. } else {
  633. quicksort(ids, dists, left, j - 1);
  634. quicksort(ids, dists, i, right);
  635. }
  636. }
  637. }
  638. function swap(arr, i, j) {
  639. const tmp = arr[i];
  640. arr[i] = arr[j];
  641. arr[j] = tmp;
  642. }
  643. function defaultGetX(p) {
  644. return p[0];
  645. }
  646. function defaultGetY(p) {
  647. return p[1];
  648. }
  649. return Delaunator;
  650. }));