Node-Red configuration
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

d3-delaunay.js 44KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. // https://github.com/d3/d3-delaunay v6.0.4 Copyright 2018-2021 Observable, Inc., 2021 Mapbox
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, (function (exports) { 'use strict';
  7. const epsilon$1 = 1.1102230246251565e-16;
  8. const splitter = 134217729;
  9. const resulterrbound = (3 + 8 * epsilon$1) * epsilon$1;
  10. // fast_expansion_sum_zeroelim routine from oritinal code
  11. function sum(elen, e, flen, f, h) {
  12. let Q, Qnew, hh, bvirt;
  13. let enow = e[0];
  14. let fnow = f[0];
  15. let eindex = 0;
  16. let findex = 0;
  17. if ((fnow > enow) === (fnow > -enow)) {
  18. Q = enow;
  19. enow = e[++eindex];
  20. } else {
  21. Q = fnow;
  22. fnow = f[++findex];
  23. }
  24. let hindex = 0;
  25. if (eindex < elen && findex < flen) {
  26. if ((fnow > enow) === (fnow > -enow)) {
  27. Qnew = enow + Q;
  28. hh = Q - (Qnew - enow);
  29. enow = e[++eindex];
  30. } else {
  31. Qnew = fnow + Q;
  32. hh = Q - (Qnew - fnow);
  33. fnow = f[++findex];
  34. }
  35. Q = Qnew;
  36. if (hh !== 0) {
  37. h[hindex++] = hh;
  38. }
  39. while (eindex < elen && findex < flen) {
  40. if ((fnow > enow) === (fnow > -enow)) {
  41. Qnew = Q + enow;
  42. bvirt = Qnew - Q;
  43. hh = Q - (Qnew - bvirt) + (enow - bvirt);
  44. enow = e[++eindex];
  45. } else {
  46. Qnew = Q + fnow;
  47. bvirt = Qnew - Q;
  48. hh = Q - (Qnew - bvirt) + (fnow - bvirt);
  49. fnow = f[++findex];
  50. }
  51. Q = Qnew;
  52. if (hh !== 0) {
  53. h[hindex++] = hh;
  54. }
  55. }
  56. }
  57. while (eindex < elen) {
  58. Qnew = Q + enow;
  59. bvirt = Qnew - Q;
  60. hh = Q - (Qnew - bvirt) + (enow - bvirt);
  61. enow = e[++eindex];
  62. Q = Qnew;
  63. if (hh !== 0) {
  64. h[hindex++] = hh;
  65. }
  66. }
  67. while (findex < flen) {
  68. Qnew = Q + fnow;
  69. bvirt = Qnew - Q;
  70. hh = Q - (Qnew - bvirt) + (fnow - bvirt);
  71. fnow = f[++findex];
  72. Q = Qnew;
  73. if (hh !== 0) {
  74. h[hindex++] = hh;
  75. }
  76. }
  77. if (Q !== 0 || hindex === 0) {
  78. h[hindex++] = Q;
  79. }
  80. return hindex;
  81. }
  82. function estimate(elen, e) {
  83. let Q = e[0];
  84. for (let i = 1; i < elen; i++) Q += e[i];
  85. return Q;
  86. }
  87. function vec(n) {
  88. return new Float64Array(n);
  89. }
  90. const ccwerrboundA = (3 + 16 * epsilon$1) * epsilon$1;
  91. const ccwerrboundB = (2 + 12 * epsilon$1) * epsilon$1;
  92. const ccwerrboundC = (9 + 64 * epsilon$1) * epsilon$1 * epsilon$1;
  93. const B = vec(4);
  94. const C1 = vec(8);
  95. const C2 = vec(12);
  96. const D = vec(16);
  97. const u = vec(4);
  98. function orient2dadapt(ax, ay, bx, by, cx, cy, detsum) {
  99. let acxtail, acytail, bcxtail, bcytail;
  100. let bvirt, c, ahi, alo, bhi, blo, _i, _j, _0, s1, s0, t1, t0, u3;
  101. const acx = ax - cx;
  102. const bcx = bx - cx;
  103. const acy = ay - cy;
  104. const bcy = by - cy;
  105. s1 = acx * bcy;
  106. c = splitter * acx;
  107. ahi = c - (c - acx);
  108. alo = acx - ahi;
  109. c = splitter * bcy;
  110. bhi = c - (c - bcy);
  111. blo = bcy - bhi;
  112. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  113. t1 = acy * bcx;
  114. c = splitter * acy;
  115. ahi = c - (c - acy);
  116. alo = acy - ahi;
  117. c = splitter * bcx;
  118. bhi = c - (c - bcx);
  119. blo = bcx - bhi;
  120. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  121. _i = s0 - t0;
  122. bvirt = s0 - _i;
  123. B[0] = s0 - (_i + bvirt) + (bvirt - t0);
  124. _j = s1 + _i;
  125. bvirt = _j - s1;
  126. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  127. _i = _0 - t1;
  128. bvirt = _0 - _i;
  129. B[1] = _0 - (_i + bvirt) + (bvirt - t1);
  130. u3 = _j + _i;
  131. bvirt = u3 - _j;
  132. B[2] = _j - (u3 - bvirt) + (_i - bvirt);
  133. B[3] = u3;
  134. let det = estimate(4, B);
  135. let errbound = ccwerrboundB * detsum;
  136. if (det >= errbound || -det >= errbound) {
  137. return det;
  138. }
  139. bvirt = ax - acx;
  140. acxtail = ax - (acx + bvirt) + (bvirt - cx);
  141. bvirt = bx - bcx;
  142. bcxtail = bx - (bcx + bvirt) + (bvirt - cx);
  143. bvirt = ay - acy;
  144. acytail = ay - (acy + bvirt) + (bvirt - cy);
  145. bvirt = by - bcy;
  146. bcytail = by - (bcy + bvirt) + (bvirt - cy);
  147. if (acxtail === 0 && acytail === 0 && bcxtail === 0 && bcytail === 0) {
  148. return det;
  149. }
  150. errbound = ccwerrboundC * detsum + resulterrbound * Math.abs(det);
  151. det += (acx * bcytail + bcy * acxtail) - (acy * bcxtail + bcx * acytail);
  152. if (det >= errbound || -det >= errbound) return det;
  153. s1 = acxtail * bcy;
  154. c = splitter * acxtail;
  155. ahi = c - (c - acxtail);
  156. alo = acxtail - ahi;
  157. c = splitter * bcy;
  158. bhi = c - (c - bcy);
  159. blo = bcy - bhi;
  160. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  161. t1 = acytail * bcx;
  162. c = splitter * acytail;
  163. ahi = c - (c - acytail);
  164. alo = acytail - ahi;
  165. c = splitter * bcx;
  166. bhi = c - (c - bcx);
  167. blo = bcx - bhi;
  168. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  169. _i = s0 - t0;
  170. bvirt = s0 - _i;
  171. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  172. _j = s1 + _i;
  173. bvirt = _j - s1;
  174. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  175. _i = _0 - t1;
  176. bvirt = _0 - _i;
  177. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  178. u3 = _j + _i;
  179. bvirt = u3 - _j;
  180. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  181. u[3] = u3;
  182. const C1len = sum(4, B, 4, u, C1);
  183. s1 = acx * bcytail;
  184. c = splitter * acx;
  185. ahi = c - (c - acx);
  186. alo = acx - ahi;
  187. c = splitter * bcytail;
  188. bhi = c - (c - bcytail);
  189. blo = bcytail - bhi;
  190. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  191. t1 = acy * bcxtail;
  192. c = splitter * acy;
  193. ahi = c - (c - acy);
  194. alo = acy - ahi;
  195. c = splitter * bcxtail;
  196. bhi = c - (c - bcxtail);
  197. blo = bcxtail - bhi;
  198. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  199. _i = s0 - t0;
  200. bvirt = s0 - _i;
  201. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  202. _j = s1 + _i;
  203. bvirt = _j - s1;
  204. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  205. _i = _0 - t1;
  206. bvirt = _0 - _i;
  207. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  208. u3 = _j + _i;
  209. bvirt = u3 - _j;
  210. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  211. u[3] = u3;
  212. const C2len = sum(C1len, C1, 4, u, C2);
  213. s1 = acxtail * bcytail;
  214. c = splitter * acxtail;
  215. ahi = c - (c - acxtail);
  216. alo = acxtail - ahi;
  217. c = splitter * bcytail;
  218. bhi = c - (c - bcytail);
  219. blo = bcytail - bhi;
  220. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  221. t1 = acytail * bcxtail;
  222. c = splitter * acytail;
  223. ahi = c - (c - acytail);
  224. alo = acytail - ahi;
  225. c = splitter * bcxtail;
  226. bhi = c - (c - bcxtail);
  227. blo = bcxtail - bhi;
  228. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  229. _i = s0 - t0;
  230. bvirt = s0 - _i;
  231. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  232. _j = s1 + _i;
  233. bvirt = _j - s1;
  234. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  235. _i = _0 - t1;
  236. bvirt = _0 - _i;
  237. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  238. u3 = _j + _i;
  239. bvirt = u3 - _j;
  240. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  241. u[3] = u3;
  242. const Dlen = sum(C2len, C2, 4, u, D);
  243. return D[Dlen - 1];
  244. }
  245. function orient2d(ax, ay, bx, by, cx, cy) {
  246. const detleft = (ay - cy) * (bx - cx);
  247. const detright = (ax - cx) * (by - cy);
  248. const det = detleft - detright;
  249. if (detleft === 0 || detright === 0 || (detleft > 0) !== (detright > 0)) return det;
  250. const detsum = Math.abs(detleft + detright);
  251. if (Math.abs(det) >= ccwerrboundA * detsum) return det;
  252. return -orient2dadapt(ax, ay, bx, by, cx, cy, detsum);
  253. }
  254. const EPSILON = Math.pow(2, -52);
  255. const EDGE_STACK = new Uint32Array(512);
  256. class Delaunator {
  257. static from(points, getX = defaultGetX, getY = defaultGetY) {
  258. const n = points.length;
  259. const coords = new Float64Array(n * 2);
  260. for (let i = 0; i < n; i++) {
  261. const p = points[i];
  262. coords[2 * i] = getX(p);
  263. coords[2 * i + 1] = getY(p);
  264. }
  265. return new Delaunator(coords);
  266. }
  267. constructor(coords) {
  268. const n = coords.length >> 1;
  269. if (n > 0 && typeof coords[0] !== 'number') throw new Error('Expected coords to contain numbers.');
  270. this.coords = coords;
  271. // arrays that will store the triangulation graph
  272. const maxTriangles = Math.max(2 * n - 5, 0);
  273. this._triangles = new Uint32Array(maxTriangles * 3);
  274. this._halfedges = new Int32Array(maxTriangles * 3);
  275. // temporary arrays for tracking the edges of the advancing convex hull
  276. this._hashSize = Math.ceil(Math.sqrt(n));
  277. this._hullPrev = new Uint32Array(n); // edge to prev edge
  278. this._hullNext = new Uint32Array(n); // edge to next edge
  279. this._hullTri = new Uint32Array(n); // edge to adjacent triangle
  280. this._hullHash = new Int32Array(this._hashSize).fill(-1); // angular edge hash
  281. // temporary arrays for sorting points
  282. this._ids = new Uint32Array(n);
  283. this._dists = new Float64Array(n);
  284. this.update();
  285. }
  286. update() {
  287. const {coords, _hullPrev: hullPrev, _hullNext: hullNext, _hullTri: hullTri, _hullHash: hullHash} = this;
  288. const n = coords.length >> 1;
  289. // populate an array of point indices; calculate input data bbox
  290. let minX = Infinity;
  291. let minY = Infinity;
  292. let maxX = -Infinity;
  293. let maxY = -Infinity;
  294. for (let i = 0; i < n; i++) {
  295. const x = coords[2 * i];
  296. const y = coords[2 * i + 1];
  297. if (x < minX) minX = x;
  298. if (y < minY) minY = y;
  299. if (x > maxX) maxX = x;
  300. if (y > maxY) maxY = y;
  301. this._ids[i] = i;
  302. }
  303. const cx = (minX + maxX) / 2;
  304. const cy = (minY + maxY) / 2;
  305. let minDist = Infinity;
  306. let i0, i1, i2;
  307. // pick a seed point close to the center
  308. for (let i = 0; i < n; i++) {
  309. const d = dist(cx, cy, coords[2 * i], coords[2 * i + 1]);
  310. if (d < minDist) {
  311. i0 = i;
  312. minDist = d;
  313. }
  314. }
  315. const i0x = coords[2 * i0];
  316. const i0y = coords[2 * i0 + 1];
  317. minDist = Infinity;
  318. // find the point closest to the seed
  319. for (let i = 0; i < n; i++) {
  320. if (i === i0) continue;
  321. const d = dist(i0x, i0y, coords[2 * i], coords[2 * i + 1]);
  322. if (d < minDist && d > 0) {
  323. i1 = i;
  324. minDist = d;
  325. }
  326. }
  327. let i1x = coords[2 * i1];
  328. let i1y = coords[2 * i1 + 1];
  329. let minRadius = Infinity;
  330. // find the third point which forms the smallest circumcircle with the first two
  331. for (let i = 0; i < n; i++) {
  332. if (i === i0 || i === i1) continue;
  333. const r = circumradius(i0x, i0y, i1x, i1y, coords[2 * i], coords[2 * i + 1]);
  334. if (r < minRadius) {
  335. i2 = i;
  336. minRadius = r;
  337. }
  338. }
  339. let i2x = coords[2 * i2];
  340. let i2y = coords[2 * i2 + 1];
  341. if (minRadius === Infinity) {
  342. // order collinear points by dx (or dy if all x are identical)
  343. // and return the list as a hull
  344. for (let i = 0; i < n; i++) {
  345. this._dists[i] = (coords[2 * i] - coords[0]) || (coords[2 * i + 1] - coords[1]);
  346. }
  347. quicksort(this._ids, this._dists, 0, n - 1);
  348. const hull = new Uint32Array(n);
  349. let j = 0;
  350. for (let i = 0, d0 = -Infinity; i < n; i++) {
  351. const id = this._ids[i];
  352. if (this._dists[id] > d0) {
  353. hull[j++] = id;
  354. d0 = this._dists[id];
  355. }
  356. }
  357. this.hull = hull.subarray(0, j);
  358. this.triangles = new Uint32Array(0);
  359. this.halfedges = new Uint32Array(0);
  360. return;
  361. }
  362. // swap the order of the seed points for counter-clockwise orientation
  363. if (orient2d(i0x, i0y, i1x, i1y, i2x, i2y) < 0) {
  364. const i = i1;
  365. const x = i1x;
  366. const y = i1y;
  367. i1 = i2;
  368. i1x = i2x;
  369. i1y = i2y;
  370. i2 = i;
  371. i2x = x;
  372. i2y = y;
  373. }
  374. const center = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
  375. this._cx = center.x;
  376. this._cy = center.y;
  377. for (let i = 0; i < n; i++) {
  378. this._dists[i] = dist(coords[2 * i], coords[2 * i + 1], center.x, center.y);
  379. }
  380. // sort the points by distance from the seed triangle circumcenter
  381. quicksort(this._ids, this._dists, 0, n - 1);
  382. // set up the seed triangle as the starting hull
  383. this._hullStart = i0;
  384. let hullSize = 3;
  385. hullNext[i0] = hullPrev[i2] = i1;
  386. hullNext[i1] = hullPrev[i0] = i2;
  387. hullNext[i2] = hullPrev[i1] = i0;
  388. hullTri[i0] = 0;
  389. hullTri[i1] = 1;
  390. hullTri[i2] = 2;
  391. hullHash.fill(-1);
  392. hullHash[this._hashKey(i0x, i0y)] = i0;
  393. hullHash[this._hashKey(i1x, i1y)] = i1;
  394. hullHash[this._hashKey(i2x, i2y)] = i2;
  395. this.trianglesLen = 0;
  396. this._addTriangle(i0, i1, i2, -1, -1, -1);
  397. for (let k = 0, xp, yp; k < this._ids.length; k++) {
  398. const i = this._ids[k];
  399. const x = coords[2 * i];
  400. const y = coords[2 * i + 1];
  401. // skip near-duplicate points
  402. if (k > 0 && Math.abs(x - xp) <= EPSILON && Math.abs(y - yp) <= EPSILON) continue;
  403. xp = x;
  404. yp = y;
  405. // skip seed triangle points
  406. if (i === i0 || i === i1 || i === i2) continue;
  407. // find a visible edge on the convex hull using edge hash
  408. let start = 0;
  409. for (let j = 0, key = this._hashKey(x, y); j < this._hashSize; j++) {
  410. start = hullHash[(key + j) % this._hashSize];
  411. if (start !== -1 && start !== hullNext[start]) break;
  412. }
  413. start = hullPrev[start];
  414. let e = start, q;
  415. while (q = hullNext[e], orient2d(x, y, coords[2 * e], coords[2 * e + 1], coords[2 * q], coords[2 * q + 1]) >= 0) {
  416. e = q;
  417. if (e === start) {
  418. e = -1;
  419. break;
  420. }
  421. }
  422. if (e === -1) continue; // likely a near-duplicate point; skip it
  423. // add the first triangle from the point
  424. let t = this._addTriangle(e, i, hullNext[e], -1, -1, hullTri[e]);
  425. // recursively flip triangles from the point until they satisfy the Delaunay condition
  426. hullTri[i] = this._legalize(t + 2);
  427. hullTri[e] = t; // keep track of boundary triangles on the hull
  428. hullSize++;
  429. // walk forward through the hull, adding more triangles and flipping recursively
  430. let n = hullNext[e];
  431. while (q = hullNext[n], orient2d(x, y, coords[2 * n], coords[2 * n + 1], coords[2 * q], coords[2 * q + 1]) < 0) {
  432. t = this._addTriangle(n, i, q, hullTri[i], -1, hullTri[n]);
  433. hullTri[i] = this._legalize(t + 2);
  434. hullNext[n] = n; // mark as removed
  435. hullSize--;
  436. n = q;
  437. }
  438. // walk backward from the other side, adding more triangles and flipping
  439. if (e === start) {
  440. while (q = hullPrev[e], orient2d(x, y, coords[2 * q], coords[2 * q + 1], coords[2 * e], coords[2 * e + 1]) < 0) {
  441. t = this._addTriangle(q, i, e, -1, hullTri[e], hullTri[q]);
  442. this._legalize(t + 2);
  443. hullTri[q] = t;
  444. hullNext[e] = e; // mark as removed
  445. hullSize--;
  446. e = q;
  447. }
  448. }
  449. // update the hull indices
  450. this._hullStart = hullPrev[i] = e;
  451. hullNext[e] = hullPrev[n] = i;
  452. hullNext[i] = n;
  453. // save the two new edges in the hash table
  454. hullHash[this._hashKey(x, y)] = i;
  455. hullHash[this._hashKey(coords[2 * e], coords[2 * e + 1])] = e;
  456. }
  457. this.hull = new Uint32Array(hullSize);
  458. for (let i = 0, e = this._hullStart; i < hullSize; i++) {
  459. this.hull[i] = e;
  460. e = hullNext[e];
  461. }
  462. // trim typed triangle mesh arrays
  463. this.triangles = this._triangles.subarray(0, this.trianglesLen);
  464. this.halfedges = this._halfedges.subarray(0, this.trianglesLen);
  465. }
  466. _hashKey(x, y) {
  467. return Math.floor(pseudoAngle(x - this._cx, y - this._cy) * this._hashSize) % this._hashSize;
  468. }
  469. _legalize(a) {
  470. const {_triangles: triangles, _halfedges: halfedges, coords} = this;
  471. let i = 0;
  472. let ar = 0;
  473. // recursion eliminated with a fixed-size stack
  474. while (true) {
  475. const b = halfedges[a];
  476. /* if the pair of triangles doesn't satisfy the Delaunay condition
  477. * (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
  478. * then do the same check/flip recursively for the new pair of triangles
  479. *
  480. * pl pl
  481. * /||\ / \
  482. * al/ || \bl al/ \a
  483. * / || \ / \
  484. * / a||b \ flip /___ar___\
  485. * p0\ || /p1 => p0\---bl---/p1
  486. * \ || / \ /
  487. * ar\ || /br b\ /br
  488. * \||/ \ /
  489. * pr pr
  490. */
  491. const a0 = a - a % 3;
  492. ar = a0 + (a + 2) % 3;
  493. if (b === -1) { // convex hull edge
  494. if (i === 0) break;
  495. a = EDGE_STACK[--i];
  496. continue;
  497. }
  498. const b0 = b - b % 3;
  499. const al = a0 + (a + 1) % 3;
  500. const bl = b0 + (b + 2) % 3;
  501. const p0 = triangles[ar];
  502. const pr = triangles[a];
  503. const pl = triangles[al];
  504. const p1 = triangles[bl];
  505. const illegal = inCircle(
  506. coords[2 * p0], coords[2 * p0 + 1],
  507. coords[2 * pr], coords[2 * pr + 1],
  508. coords[2 * pl], coords[2 * pl + 1],
  509. coords[2 * p1], coords[2 * p1 + 1]);
  510. if (illegal) {
  511. triangles[a] = p1;
  512. triangles[b] = p0;
  513. const hbl = halfedges[bl];
  514. // edge swapped on the other side of the hull (rare); fix the halfedge reference
  515. if (hbl === -1) {
  516. let e = this._hullStart;
  517. do {
  518. if (this._hullTri[e] === bl) {
  519. this._hullTri[e] = a;
  520. break;
  521. }
  522. e = this._hullPrev[e];
  523. } while (e !== this._hullStart);
  524. }
  525. this._link(a, hbl);
  526. this._link(b, halfedges[ar]);
  527. this._link(ar, bl);
  528. const br = b0 + (b + 1) % 3;
  529. // don't worry about hitting the cap: it can only happen on extremely degenerate input
  530. if (i < EDGE_STACK.length) {
  531. EDGE_STACK[i++] = br;
  532. }
  533. } else {
  534. if (i === 0) break;
  535. a = EDGE_STACK[--i];
  536. }
  537. }
  538. return ar;
  539. }
  540. _link(a, b) {
  541. this._halfedges[a] = b;
  542. if (b !== -1) this._halfedges[b] = a;
  543. }
  544. // add a new triangle given vertex indices and adjacent half-edge ids
  545. _addTriangle(i0, i1, i2, a, b, c) {
  546. const t = this.trianglesLen;
  547. this._triangles[t] = i0;
  548. this._triangles[t + 1] = i1;
  549. this._triangles[t + 2] = i2;
  550. this._link(t, a);
  551. this._link(t + 1, b);
  552. this._link(t + 2, c);
  553. this.trianglesLen += 3;
  554. return t;
  555. }
  556. }
  557. // monotonically increases with real angle, but doesn't need expensive trigonometry
  558. function pseudoAngle(dx, dy) {
  559. const p = dx / (Math.abs(dx) + Math.abs(dy));
  560. return (dy > 0 ? 3 - p : 1 + p) / 4; // [0..1]
  561. }
  562. function dist(ax, ay, bx, by) {
  563. const dx = ax - bx;
  564. const dy = ay - by;
  565. return dx * dx + dy * dy;
  566. }
  567. function inCircle(ax, ay, bx, by, cx, cy, px, py) {
  568. const dx = ax - px;
  569. const dy = ay - py;
  570. const ex = bx - px;
  571. const ey = by - py;
  572. const fx = cx - px;
  573. const fy = cy - py;
  574. const ap = dx * dx + dy * dy;
  575. const bp = ex * ex + ey * ey;
  576. const cp = fx * fx + fy * fy;
  577. return dx * (ey * cp - bp * fy) -
  578. dy * (ex * cp - bp * fx) +
  579. ap * (ex * fy - ey * fx) < 0;
  580. }
  581. function circumradius(ax, ay, bx, by, cx, cy) {
  582. const dx = bx - ax;
  583. const dy = by - ay;
  584. const ex = cx - ax;
  585. const ey = cy - ay;
  586. const bl = dx * dx + dy * dy;
  587. const cl = ex * ex + ey * ey;
  588. const d = 0.5 / (dx * ey - dy * ex);
  589. const x = (ey * bl - dy * cl) * d;
  590. const y = (dx * cl - ex * bl) * d;
  591. return x * x + y * y;
  592. }
  593. function circumcenter(ax, ay, bx, by, cx, cy) {
  594. const dx = bx - ax;
  595. const dy = by - ay;
  596. const ex = cx - ax;
  597. const ey = cy - ay;
  598. const bl = dx * dx + dy * dy;
  599. const cl = ex * ex + ey * ey;
  600. const d = 0.5 / (dx * ey - dy * ex);
  601. const x = ax + (ey * bl - dy * cl) * d;
  602. const y = ay + (dx * cl - ex * bl) * d;
  603. return {x, y};
  604. }
  605. function quicksort(ids, dists, left, right) {
  606. if (right - left <= 20) {
  607. for (let i = left + 1; i <= right; i++) {
  608. const temp = ids[i];
  609. const tempDist = dists[temp];
  610. let j = i - 1;
  611. while (j >= left && dists[ids[j]] > tempDist) ids[j + 1] = ids[j--];
  612. ids[j + 1] = temp;
  613. }
  614. } else {
  615. const median = (left + right) >> 1;
  616. let i = left + 1;
  617. let j = right;
  618. swap(ids, median, i);
  619. if (dists[ids[left]] > dists[ids[right]]) swap(ids, left, right);
  620. if (dists[ids[i]] > dists[ids[right]]) swap(ids, i, right);
  621. if (dists[ids[left]] > dists[ids[i]]) swap(ids, left, i);
  622. const temp = ids[i];
  623. const tempDist = dists[temp];
  624. while (true) {
  625. do i++; while (dists[ids[i]] < tempDist);
  626. do j--; while (dists[ids[j]] > tempDist);
  627. if (j < i) break;
  628. swap(ids, i, j);
  629. }
  630. ids[left + 1] = ids[j];
  631. ids[j] = temp;
  632. if (right - i + 1 >= j - left) {
  633. quicksort(ids, dists, i, right);
  634. quicksort(ids, dists, left, j - 1);
  635. } else {
  636. quicksort(ids, dists, left, j - 1);
  637. quicksort(ids, dists, i, right);
  638. }
  639. }
  640. }
  641. function swap(arr, i, j) {
  642. const tmp = arr[i];
  643. arr[i] = arr[j];
  644. arr[j] = tmp;
  645. }
  646. function defaultGetX(p) {
  647. return p[0];
  648. }
  649. function defaultGetY(p) {
  650. return p[1];
  651. }
  652. const epsilon = 1e-6;
  653. class Path {
  654. constructor() {
  655. this._x0 = this._y0 = // start of current subpath
  656. this._x1 = this._y1 = null; // end of current subpath
  657. this._ = "";
  658. }
  659. moveTo(x, y) {
  660. this._ += `M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`;
  661. }
  662. closePath() {
  663. if (this._x1 !== null) {
  664. this._x1 = this._x0, this._y1 = this._y0;
  665. this._ += "Z";
  666. }
  667. }
  668. lineTo(x, y) {
  669. this._ += `L${this._x1 = +x},${this._y1 = +y}`;
  670. }
  671. arc(x, y, r) {
  672. x = +x, y = +y, r = +r;
  673. const x0 = x + r;
  674. const y0 = y;
  675. if (r < 0) throw new Error("negative radius");
  676. if (this._x1 === null) this._ += `M${x0},${y0}`;
  677. else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) this._ += "L" + x0 + "," + y0;
  678. if (!r) return;
  679. this._ += `A${r},${r},0,1,1,${x - r},${y}A${r},${r},0,1,1,${this._x1 = x0},${this._y1 = y0}`;
  680. }
  681. rect(x, y, w, h) {
  682. this._ += `M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${+w}v${+h}h${-w}Z`;
  683. }
  684. value() {
  685. return this._ || null;
  686. }
  687. }
  688. class Polygon {
  689. constructor() {
  690. this._ = [];
  691. }
  692. moveTo(x, y) {
  693. this._.push([x, y]);
  694. }
  695. closePath() {
  696. this._.push(this._[0].slice());
  697. }
  698. lineTo(x, y) {
  699. this._.push([x, y]);
  700. }
  701. value() {
  702. return this._.length ? this._ : null;
  703. }
  704. }
  705. class Voronoi {
  706. constructor(delaunay, [xmin, ymin, xmax, ymax] = [0, 0, 960, 500]) {
  707. if (!((xmax = +xmax) >= (xmin = +xmin)) || !((ymax = +ymax) >= (ymin = +ymin))) throw new Error("invalid bounds");
  708. this.delaunay = delaunay;
  709. this._circumcenters = new Float64Array(delaunay.points.length * 2);
  710. this.vectors = new Float64Array(delaunay.points.length * 2);
  711. this.xmax = xmax, this.xmin = xmin;
  712. this.ymax = ymax, this.ymin = ymin;
  713. this._init();
  714. }
  715. update() {
  716. this.delaunay.update();
  717. this._init();
  718. return this;
  719. }
  720. _init() {
  721. const {delaunay: {points, hull, triangles}, vectors} = this;
  722. let bx, by; // lazily computed barycenter of the hull
  723. // Compute circumcenters.
  724. const circumcenters = this.circumcenters = this._circumcenters.subarray(0, triangles.length / 3 * 2);
  725. for (let i = 0, j = 0, n = triangles.length, x, y; i < n; i += 3, j += 2) {
  726. const t1 = triangles[i] * 2;
  727. const t2 = triangles[i + 1] * 2;
  728. const t3 = triangles[i + 2] * 2;
  729. const x1 = points[t1];
  730. const y1 = points[t1 + 1];
  731. const x2 = points[t2];
  732. const y2 = points[t2 + 1];
  733. const x3 = points[t3];
  734. const y3 = points[t3 + 1];
  735. const dx = x2 - x1;
  736. const dy = y2 - y1;
  737. const ex = x3 - x1;
  738. const ey = y3 - y1;
  739. const ab = (dx * ey - dy * ex) * 2;
  740. if (Math.abs(ab) < 1e-9) {
  741. // For a degenerate triangle, the circumcenter is at the infinity, in a
  742. // direction orthogonal to the halfedge and away from the “center” of
  743. // the diagram <bx, by>, defined as the hull’s barycenter.
  744. if (bx === undefined) {
  745. bx = by = 0;
  746. for (const i of hull) bx += points[i * 2], by += points[i * 2 + 1];
  747. bx /= hull.length, by /= hull.length;
  748. }
  749. const a = 1e9 * Math.sign((bx - x1) * ey - (by - y1) * ex);
  750. x = (x1 + x3) / 2 - a * ey;
  751. y = (y1 + y3) / 2 + a * ex;
  752. } else {
  753. const d = 1 / ab;
  754. const bl = dx * dx + dy * dy;
  755. const cl = ex * ex + ey * ey;
  756. x = x1 + (ey * bl - dy * cl) * d;
  757. y = y1 + (dx * cl - ex * bl) * d;
  758. }
  759. circumcenters[j] = x;
  760. circumcenters[j + 1] = y;
  761. }
  762. // Compute exterior cell rays.
  763. let h = hull[hull.length - 1];
  764. let p0, p1 = h * 4;
  765. let x0, x1 = points[2 * h];
  766. let y0, y1 = points[2 * h + 1];
  767. vectors.fill(0);
  768. for (let i = 0; i < hull.length; ++i) {
  769. h = hull[i];
  770. p0 = p1, x0 = x1, y0 = y1;
  771. p1 = h * 4, x1 = points[2 * h], y1 = points[2 * h + 1];
  772. vectors[p0 + 2] = vectors[p1] = y0 - y1;
  773. vectors[p0 + 3] = vectors[p1 + 1] = x1 - x0;
  774. }
  775. }
  776. render(context) {
  777. const buffer = context == null ? context = new Path : undefined;
  778. const {delaunay: {halfedges, inedges, hull}, circumcenters, vectors} = this;
  779. if (hull.length <= 1) return null;
  780. for (let i = 0, n = halfedges.length; i < n; ++i) {
  781. const j = halfedges[i];
  782. if (j < i) continue;
  783. const ti = Math.floor(i / 3) * 2;
  784. const tj = Math.floor(j / 3) * 2;
  785. const xi = circumcenters[ti];
  786. const yi = circumcenters[ti + 1];
  787. const xj = circumcenters[tj];
  788. const yj = circumcenters[tj + 1];
  789. this._renderSegment(xi, yi, xj, yj, context);
  790. }
  791. let h0, h1 = hull[hull.length - 1];
  792. for (let i = 0; i < hull.length; ++i) {
  793. h0 = h1, h1 = hull[i];
  794. const t = Math.floor(inedges[h1] / 3) * 2;
  795. const x = circumcenters[t];
  796. const y = circumcenters[t + 1];
  797. const v = h0 * 4;
  798. const p = this._project(x, y, vectors[v + 2], vectors[v + 3]);
  799. if (p) this._renderSegment(x, y, p[0], p[1], context);
  800. }
  801. return buffer && buffer.value();
  802. }
  803. renderBounds(context) {
  804. const buffer = context == null ? context = new Path : undefined;
  805. context.rect(this.xmin, this.ymin, this.xmax - this.xmin, this.ymax - this.ymin);
  806. return buffer && buffer.value();
  807. }
  808. renderCell(i, context) {
  809. const buffer = context == null ? context = new Path : undefined;
  810. const points = this._clip(i);
  811. if (points === null || !points.length) return;
  812. context.moveTo(points[0], points[1]);
  813. let n = points.length;
  814. while (points[0] === points[n-2] && points[1] === points[n-1] && n > 1) n -= 2;
  815. for (let i = 2; i < n; i += 2) {
  816. if (points[i] !== points[i-2] || points[i+1] !== points[i-1])
  817. context.lineTo(points[i], points[i + 1]);
  818. }
  819. context.closePath();
  820. return buffer && buffer.value();
  821. }
  822. *cellPolygons() {
  823. const {delaunay: {points}} = this;
  824. for (let i = 0, n = points.length / 2; i < n; ++i) {
  825. const cell = this.cellPolygon(i);
  826. if (cell) cell.index = i, yield cell;
  827. }
  828. }
  829. cellPolygon(i) {
  830. const polygon = new Polygon;
  831. this.renderCell(i, polygon);
  832. return polygon.value();
  833. }
  834. _renderSegment(x0, y0, x1, y1, context) {
  835. let S;
  836. const c0 = this._regioncode(x0, y0);
  837. const c1 = this._regioncode(x1, y1);
  838. if (c0 === 0 && c1 === 0) {
  839. context.moveTo(x0, y0);
  840. context.lineTo(x1, y1);
  841. } else if (S = this._clipSegment(x0, y0, x1, y1, c0, c1)) {
  842. context.moveTo(S[0], S[1]);
  843. context.lineTo(S[2], S[3]);
  844. }
  845. }
  846. contains(i, x, y) {
  847. if ((x = +x, x !== x) || (y = +y, y !== y)) return false;
  848. return this.delaunay._step(i, x, y) === i;
  849. }
  850. *neighbors(i) {
  851. const ci = this._clip(i);
  852. if (ci) for (const j of this.delaunay.neighbors(i)) {
  853. const cj = this._clip(j);
  854. // find the common edge
  855. if (cj) loop: for (let ai = 0, li = ci.length; ai < li; ai += 2) {
  856. for (let aj = 0, lj = cj.length; aj < lj; aj += 2) {
  857. if (ci[ai] === cj[aj]
  858. && ci[ai + 1] === cj[aj + 1]
  859. && ci[(ai + 2) % li] === cj[(aj + lj - 2) % lj]
  860. && ci[(ai + 3) % li] === cj[(aj + lj - 1) % lj]) {
  861. yield j;
  862. break loop;
  863. }
  864. }
  865. }
  866. }
  867. }
  868. _cell(i) {
  869. const {circumcenters, delaunay: {inedges, halfedges, triangles}} = this;
  870. const e0 = inedges[i];
  871. if (e0 === -1) return null; // coincident point
  872. const points = [];
  873. let e = e0;
  874. do {
  875. const t = Math.floor(e / 3);
  876. points.push(circumcenters[t * 2], circumcenters[t * 2 + 1]);
  877. e = e % 3 === 2 ? e - 2 : e + 1;
  878. if (triangles[e] !== i) break; // bad triangulation
  879. e = halfedges[e];
  880. } while (e !== e0 && e !== -1);
  881. return points;
  882. }
  883. _clip(i) {
  884. // degenerate case (1 valid point: return the box)
  885. if (i === 0 && this.delaunay.hull.length === 1) {
  886. return [this.xmax, this.ymin, this.xmax, this.ymax, this.xmin, this.ymax, this.xmin, this.ymin];
  887. }
  888. const points = this._cell(i);
  889. if (points === null) return null;
  890. const {vectors: V} = this;
  891. const v = i * 4;
  892. return this._simplify(V[v] || V[v + 1]
  893. ? this._clipInfinite(i, points, V[v], V[v + 1], V[v + 2], V[v + 3])
  894. : this._clipFinite(i, points));
  895. }
  896. _clipFinite(i, points) {
  897. const n = points.length;
  898. let P = null;
  899. let x0, y0, x1 = points[n - 2], y1 = points[n - 1];
  900. let c0, c1 = this._regioncode(x1, y1);
  901. let e0, e1 = 0;
  902. for (let j = 0; j < n; j += 2) {
  903. x0 = x1, y0 = y1, x1 = points[j], y1 = points[j + 1];
  904. c0 = c1, c1 = this._regioncode(x1, y1);
  905. if (c0 === 0 && c1 === 0) {
  906. e0 = e1, e1 = 0;
  907. if (P) P.push(x1, y1);
  908. else P = [x1, y1];
  909. } else {
  910. let S, sx0, sy0, sx1, sy1;
  911. if (c0 === 0) {
  912. if ((S = this._clipSegment(x0, y0, x1, y1, c0, c1)) === null) continue;
  913. [sx0, sy0, sx1, sy1] = S;
  914. } else {
  915. if ((S = this._clipSegment(x1, y1, x0, y0, c1, c0)) === null) continue;
  916. [sx1, sy1, sx0, sy0] = S;
  917. e0 = e1, e1 = this._edgecode(sx0, sy0);
  918. if (e0 && e1) this._edge(i, e0, e1, P, P.length);
  919. if (P) P.push(sx0, sy0);
  920. else P = [sx0, sy0];
  921. }
  922. e0 = e1, e1 = this._edgecode(sx1, sy1);
  923. if (e0 && e1) this._edge(i, e0, e1, P, P.length);
  924. if (P) P.push(sx1, sy1);
  925. else P = [sx1, sy1];
  926. }
  927. }
  928. if (P) {
  929. e0 = e1, e1 = this._edgecode(P[0], P[1]);
  930. if (e0 && e1) this._edge(i, e0, e1, P, P.length);
  931. } else if (this.contains(i, (this.xmin + this.xmax) / 2, (this.ymin + this.ymax) / 2)) {
  932. return [this.xmax, this.ymin, this.xmax, this.ymax, this.xmin, this.ymax, this.xmin, this.ymin];
  933. }
  934. return P;
  935. }
  936. _clipSegment(x0, y0, x1, y1, c0, c1) {
  937. // for more robustness, always consider the segment in the same order
  938. const flip = c0 < c1;
  939. if (flip) [x0, y0, x1, y1, c0, c1] = [x1, y1, x0, y0, c1, c0];
  940. while (true) {
  941. if (c0 === 0 && c1 === 0) return flip ? [x1, y1, x0, y0] : [x0, y0, x1, y1];
  942. if (c0 & c1) return null;
  943. let x, y, c = c0 || c1;
  944. if (c & 0b1000) x = x0 + (x1 - x0) * (this.ymax - y0) / (y1 - y0), y = this.ymax;
  945. else if (c & 0b0100) x = x0 + (x1 - x0) * (this.ymin - y0) / (y1 - y0), y = this.ymin;
  946. else if (c & 0b0010) y = y0 + (y1 - y0) * (this.xmax - x0) / (x1 - x0), x = this.xmax;
  947. else y = y0 + (y1 - y0) * (this.xmin - x0) / (x1 - x0), x = this.xmin;
  948. if (c0) x0 = x, y0 = y, c0 = this._regioncode(x0, y0);
  949. else x1 = x, y1 = y, c1 = this._regioncode(x1, y1);
  950. }
  951. }
  952. _clipInfinite(i, points, vx0, vy0, vxn, vyn) {
  953. let P = Array.from(points), p;
  954. if (p = this._project(P[0], P[1], vx0, vy0)) P.unshift(p[0], p[1]);
  955. if (p = this._project(P[P.length - 2], P[P.length - 1], vxn, vyn)) P.push(p[0], p[1]);
  956. if (P = this._clipFinite(i, P)) {
  957. for (let j = 0, n = P.length, c0, c1 = this._edgecode(P[n - 2], P[n - 1]); j < n; j += 2) {
  958. c0 = c1, c1 = this._edgecode(P[j], P[j + 1]);
  959. if (c0 && c1) j = this._edge(i, c0, c1, P, j), n = P.length;
  960. }
  961. } else if (this.contains(i, (this.xmin + this.xmax) / 2, (this.ymin + this.ymax) / 2)) {
  962. P = [this.xmin, this.ymin, this.xmax, this.ymin, this.xmax, this.ymax, this.xmin, this.ymax];
  963. }
  964. return P;
  965. }
  966. _edge(i, e0, e1, P, j) {
  967. while (e0 !== e1) {
  968. let x, y;
  969. switch (e0) {
  970. case 0b0101: e0 = 0b0100; continue; // top-left
  971. case 0b0100: e0 = 0b0110, x = this.xmax, y = this.ymin; break; // top
  972. case 0b0110: e0 = 0b0010; continue; // top-right
  973. case 0b0010: e0 = 0b1010, x = this.xmax, y = this.ymax; break; // right
  974. case 0b1010: e0 = 0b1000; continue; // bottom-right
  975. case 0b1000: e0 = 0b1001, x = this.xmin, y = this.ymax; break; // bottom
  976. case 0b1001: e0 = 0b0001; continue; // bottom-left
  977. case 0b0001: e0 = 0b0101, x = this.xmin, y = this.ymin; break; // left
  978. }
  979. // Note: this implicitly checks for out of bounds: if P[j] or P[j+1] are
  980. // undefined, the conditional statement will be executed.
  981. if ((P[j] !== x || P[j + 1] !== y) && this.contains(i, x, y)) {
  982. P.splice(j, 0, x, y), j += 2;
  983. }
  984. }
  985. return j;
  986. }
  987. _project(x0, y0, vx, vy) {
  988. let t = Infinity, c, x, y;
  989. if (vy < 0) { // top
  990. if (y0 <= this.ymin) return null;
  991. if ((c = (this.ymin - y0) / vy) < t) y = this.ymin, x = x0 + (t = c) * vx;
  992. } else if (vy > 0) { // bottom
  993. if (y0 >= this.ymax) return null;
  994. if ((c = (this.ymax - y0) / vy) < t) y = this.ymax, x = x0 + (t = c) * vx;
  995. }
  996. if (vx > 0) { // right
  997. if (x0 >= this.xmax) return null;
  998. if ((c = (this.xmax - x0) / vx) < t) x = this.xmax, y = y0 + (t = c) * vy;
  999. } else if (vx < 0) { // left
  1000. if (x0 <= this.xmin) return null;
  1001. if ((c = (this.xmin - x0) / vx) < t) x = this.xmin, y = y0 + (t = c) * vy;
  1002. }
  1003. return [x, y];
  1004. }
  1005. _edgecode(x, y) {
  1006. return (x === this.xmin ? 0b0001
  1007. : x === this.xmax ? 0b0010 : 0b0000)
  1008. | (y === this.ymin ? 0b0100
  1009. : y === this.ymax ? 0b1000 : 0b0000);
  1010. }
  1011. _regioncode(x, y) {
  1012. return (x < this.xmin ? 0b0001
  1013. : x > this.xmax ? 0b0010 : 0b0000)
  1014. | (y < this.ymin ? 0b0100
  1015. : y > this.ymax ? 0b1000 : 0b0000);
  1016. }
  1017. _simplify(P) {
  1018. if (P && P.length > 4) {
  1019. for (let i = 0; i < P.length; i+= 2) {
  1020. const j = (i + 2) % P.length, k = (i + 4) % P.length;
  1021. if (P[i] === P[j] && P[j] === P[k] || P[i + 1] === P[j + 1] && P[j + 1] === P[k + 1]) {
  1022. P.splice(j, 2), i -= 2;
  1023. }
  1024. }
  1025. if (!P.length) P = null;
  1026. }
  1027. return P;
  1028. }
  1029. }
  1030. const tau = 2 * Math.PI, pow = Math.pow;
  1031. function pointX(p) {
  1032. return p[0];
  1033. }
  1034. function pointY(p) {
  1035. return p[1];
  1036. }
  1037. // A triangulation is collinear if all its triangles have a non-null area
  1038. function collinear(d) {
  1039. const {triangles, coords} = d;
  1040. for (let i = 0; i < triangles.length; i += 3) {
  1041. const a = 2 * triangles[i],
  1042. b = 2 * triangles[i + 1],
  1043. c = 2 * triangles[i + 2],
  1044. cross = (coords[c] - coords[a]) * (coords[b + 1] - coords[a + 1])
  1045. - (coords[b] - coords[a]) * (coords[c + 1] - coords[a + 1]);
  1046. if (cross > 1e-10) return false;
  1047. }
  1048. return true;
  1049. }
  1050. function jitter(x, y, r) {
  1051. return [x + Math.sin(x + y) * r, y + Math.cos(x - y) * r];
  1052. }
  1053. class Delaunay {
  1054. static from(points, fx = pointX, fy = pointY, that) {
  1055. return new Delaunay("length" in points
  1056. ? flatArray(points, fx, fy, that)
  1057. : Float64Array.from(flatIterable(points, fx, fy, that)));
  1058. }
  1059. constructor(points) {
  1060. this._delaunator = new Delaunator(points);
  1061. this.inedges = new Int32Array(points.length / 2);
  1062. this._hullIndex = new Int32Array(points.length / 2);
  1063. this.points = this._delaunator.coords;
  1064. this._init();
  1065. }
  1066. update() {
  1067. this._delaunator.update();
  1068. this._init();
  1069. return this;
  1070. }
  1071. _init() {
  1072. const d = this._delaunator, points = this.points;
  1073. // check for collinear
  1074. if (d.hull && d.hull.length > 2 && collinear(d)) {
  1075. this.collinear = Int32Array.from({length: points.length/2}, (_,i) => i)
  1076. .sort((i, j) => points[2 * i] - points[2 * j] || points[2 * i + 1] - points[2 * j + 1]); // for exact neighbors
  1077. const e = this.collinear[0], f = this.collinear[this.collinear.length - 1],
  1078. bounds = [ points[2 * e], points[2 * e + 1], points[2 * f], points[2 * f + 1] ],
  1079. r = 1e-8 * Math.hypot(bounds[3] - bounds[1], bounds[2] - bounds[0]);
  1080. for (let i = 0, n = points.length / 2; i < n; ++i) {
  1081. const p = jitter(points[2 * i], points[2 * i + 1], r);
  1082. points[2 * i] = p[0];
  1083. points[2 * i + 1] = p[1];
  1084. }
  1085. this._delaunator = new Delaunator(points);
  1086. } else {
  1087. delete this.collinear;
  1088. }
  1089. const halfedges = this.halfedges = this._delaunator.halfedges;
  1090. const hull = this.hull = this._delaunator.hull;
  1091. const triangles = this.triangles = this._delaunator.triangles;
  1092. const inedges = this.inedges.fill(-1);
  1093. const hullIndex = this._hullIndex.fill(-1);
  1094. // Compute an index from each point to an (arbitrary) incoming halfedge
  1095. // Used to give the first neighbor of each point; for this reason,
  1096. // on the hull we give priority to exterior halfedges
  1097. for (let e = 0, n = halfedges.length; e < n; ++e) {
  1098. const p = triangles[e % 3 === 2 ? e - 2 : e + 1];
  1099. if (halfedges[e] === -1 || inedges[p] === -1) inedges[p] = e;
  1100. }
  1101. for (let i = 0, n = hull.length; i < n; ++i) {
  1102. hullIndex[hull[i]] = i;
  1103. }
  1104. // degenerate case: 1 or 2 (distinct) points
  1105. if (hull.length <= 2 && hull.length > 0) {
  1106. this.triangles = new Int32Array(3).fill(-1);
  1107. this.halfedges = new Int32Array(3).fill(-1);
  1108. this.triangles[0] = hull[0];
  1109. inedges[hull[0]] = 1;
  1110. if (hull.length === 2) {
  1111. inedges[hull[1]] = 0;
  1112. this.triangles[1] = hull[1];
  1113. this.triangles[2] = hull[1];
  1114. }
  1115. }
  1116. }
  1117. voronoi(bounds) {
  1118. return new Voronoi(this, bounds);
  1119. }
  1120. *neighbors(i) {
  1121. const {inedges, hull, _hullIndex, halfedges, triangles, collinear} = this;
  1122. // degenerate case with several collinear points
  1123. if (collinear) {
  1124. const l = collinear.indexOf(i);
  1125. if (l > 0) yield collinear[l - 1];
  1126. if (l < collinear.length - 1) yield collinear[l + 1];
  1127. return;
  1128. }
  1129. const e0 = inedges[i];
  1130. if (e0 === -1) return; // coincident point
  1131. let e = e0, p0 = -1;
  1132. do {
  1133. yield p0 = triangles[e];
  1134. e = e % 3 === 2 ? e - 2 : e + 1;
  1135. if (triangles[e] !== i) return; // bad triangulation
  1136. e = halfedges[e];
  1137. if (e === -1) {
  1138. const p = hull[(_hullIndex[i] + 1) % hull.length];
  1139. if (p !== p0) yield p;
  1140. return;
  1141. }
  1142. } while (e !== e0);
  1143. }
  1144. find(x, y, i = 0) {
  1145. if ((x = +x, x !== x) || (y = +y, y !== y)) return -1;
  1146. const i0 = i;
  1147. let c;
  1148. while ((c = this._step(i, x, y)) >= 0 && c !== i && c !== i0) i = c;
  1149. return c;
  1150. }
  1151. _step(i, x, y) {
  1152. const {inedges, hull, _hullIndex, halfedges, triangles, points} = this;
  1153. if (inedges[i] === -1 || !points.length) return (i + 1) % (points.length >> 1);
  1154. let c = i;
  1155. let dc = pow(x - points[i * 2], 2) + pow(y - points[i * 2 + 1], 2);
  1156. const e0 = inedges[i];
  1157. let e = e0;
  1158. do {
  1159. let t = triangles[e];
  1160. const dt = pow(x - points[t * 2], 2) + pow(y - points[t * 2 + 1], 2);
  1161. if (dt < dc) dc = dt, c = t;
  1162. e = e % 3 === 2 ? e - 2 : e + 1;
  1163. if (triangles[e] !== i) break; // bad triangulation
  1164. e = halfedges[e];
  1165. if (e === -1) {
  1166. e = hull[(_hullIndex[i] + 1) % hull.length];
  1167. if (e !== t) {
  1168. if (pow(x - points[e * 2], 2) + pow(y - points[e * 2 + 1], 2) < dc) return e;
  1169. }
  1170. break;
  1171. }
  1172. } while (e !== e0);
  1173. return c;
  1174. }
  1175. render(context) {
  1176. const buffer = context == null ? context = new Path : undefined;
  1177. const {points, halfedges, triangles} = this;
  1178. for (let i = 0, n = halfedges.length; i < n; ++i) {
  1179. const j = halfedges[i];
  1180. if (j < i) continue;
  1181. const ti = triangles[i] * 2;
  1182. const tj = triangles[j] * 2;
  1183. context.moveTo(points[ti], points[ti + 1]);
  1184. context.lineTo(points[tj], points[tj + 1]);
  1185. }
  1186. this.renderHull(context);
  1187. return buffer && buffer.value();
  1188. }
  1189. renderPoints(context, r) {
  1190. if (r === undefined && (!context || typeof context.moveTo !== "function")) r = context, context = null;
  1191. r = r == undefined ? 2 : +r;
  1192. const buffer = context == null ? context = new Path : undefined;
  1193. const {points} = this;
  1194. for (let i = 0, n = points.length; i < n; i += 2) {
  1195. const x = points[i], y = points[i + 1];
  1196. context.moveTo(x + r, y);
  1197. context.arc(x, y, r, 0, tau);
  1198. }
  1199. return buffer && buffer.value();
  1200. }
  1201. renderHull(context) {
  1202. const buffer = context == null ? context = new Path : undefined;
  1203. const {hull, points} = this;
  1204. const h = hull[0] * 2, n = hull.length;
  1205. context.moveTo(points[h], points[h + 1]);
  1206. for (let i = 1; i < n; ++i) {
  1207. const h = 2 * hull[i];
  1208. context.lineTo(points[h], points[h + 1]);
  1209. }
  1210. context.closePath();
  1211. return buffer && buffer.value();
  1212. }
  1213. hullPolygon() {
  1214. const polygon = new Polygon;
  1215. this.renderHull(polygon);
  1216. return polygon.value();
  1217. }
  1218. renderTriangle(i, context) {
  1219. const buffer = context == null ? context = new Path : undefined;
  1220. const {points, triangles} = this;
  1221. const t0 = triangles[i *= 3] * 2;
  1222. const t1 = triangles[i + 1] * 2;
  1223. const t2 = triangles[i + 2] * 2;
  1224. context.moveTo(points[t0], points[t0 + 1]);
  1225. context.lineTo(points[t1], points[t1 + 1]);
  1226. context.lineTo(points[t2], points[t2 + 1]);
  1227. context.closePath();
  1228. return buffer && buffer.value();
  1229. }
  1230. *trianglePolygons() {
  1231. const {triangles} = this;
  1232. for (let i = 0, n = triangles.length / 3; i < n; ++i) {
  1233. yield this.trianglePolygon(i);
  1234. }
  1235. }
  1236. trianglePolygon(i) {
  1237. const polygon = new Polygon;
  1238. this.renderTriangle(i, polygon);
  1239. return polygon.value();
  1240. }
  1241. }
  1242. function flatArray(points, fx, fy, that) {
  1243. const n = points.length;
  1244. const array = new Float64Array(n * 2);
  1245. for (let i = 0; i < n; ++i) {
  1246. const p = points[i];
  1247. array[i * 2] = fx.call(that, p, i, points);
  1248. array[i * 2 + 1] = fy.call(that, p, i, points);
  1249. }
  1250. return array;
  1251. }
  1252. function* flatIterable(points, fx, fy, that) {
  1253. let i = 0;
  1254. for (const p of points) {
  1255. yield fx.call(that, p, i, points);
  1256. yield fy.call(that, p, i, points);
  1257. ++i;
  1258. }
  1259. }
  1260. exports.Delaunay = Delaunay;
  1261. exports.Voronoi = Voronoi;
  1262. Object.defineProperty(exports, '__esModule', { value: true });
  1263. })));