@math.gl/geometry 5.0.0-beta.10 → 5.0.0-beta.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/dist/arrow/index.cjs +926 -0
  2. package/dist/arrow/index.cjs.map +6 -0
  3. package/dist/arrow/index.d.ts +3 -0
  4. package/dist/arrow/index.d.ts.map +1 -0
  5. package/dist/arrow/index.js +6 -0
  6. package/dist/arrow/index.js.map +1 -0
  7. package/dist/arrow/stream-tessellate.d.ts +34 -0
  8. package/dist/arrow/stream-tessellate.d.ts.map +1 -0
  9. package/dist/arrow/stream-tessellate.js +171 -0
  10. package/dist/arrow/stream-tessellate.js.map +1 -0
  11. package/dist/arrow/tessellate.d.ts +31 -0
  12. package/dist/arrow/tessellate.d.ts.map +1 -0
  13. package/dist/arrow/tessellate.js +102 -0
  14. package/dist/arrow/tessellate.js.map +1 -0
  15. package/dist/color/index.cjs +3 -108
  16. package/dist/color/index.cjs.map +3 -3
  17. package/dist/color/index.d.ts +2 -3
  18. package/dist/color/index.d.ts.map +1 -1
  19. package/dist/color/index.js +1 -1
  20. package/dist/color/index.js.map +1 -1
  21. package/dist/culling/index.cjs +3 -1
  22. package/dist/culling/index.cjs.map +1 -1
  23. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts +4 -1
  24. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts.map +1 -1
  25. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js +9 -2
  26. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js.map +1 -1
  27. package/dist/index.cjs +735 -684
  28. package/dist/index.cjs.map +3 -3
  29. package/dist/index.d.ts +3 -3
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/polygon/cut-by-grid.d.ts.map +1 -1
  34. package/dist/polygon/cut-by-grid.js +18 -0
  35. package/dist/polygon/cut-by-grid.js.map +1 -1
  36. package/dist/polygon/earcut.d.ts +12 -1
  37. package/dist/polygon/earcut.d.ts.map +1 -1
  38. package/dist/polygon/earcut.js +855 -630
  39. package/dist/polygon/earcut.js.map +1 -1
  40. package/dist/polygon/index.d.ts +1 -0
  41. package/dist/polygon/index.d.ts.map +1 -1
  42. package/dist/polygon/index.js.map +1 -1
  43. package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -1
  44. package/dist/polygon/subdivide-triangle-mesh.js +55 -17
  45. package/dist/polygon/subdivide-triangle-mesh.js.map +1 -1
  46. package/dist/utils/compute-vertex-normals.d.ts +2 -2
  47. package/dist/utils/compute-vertex-normals.d.ts.map +1 -1
  48. package/dist/utils/compute-vertex-normals.js +3 -4
  49. package/dist/utils/compute-vertex-normals.js.map +1 -1
  50. package/dist/utils/geometry.d.ts +11 -4
  51. package/dist/utils/geometry.d.ts.map +1 -1
  52. package/dist/utils/geometry.js +19 -2
  53. package/dist/utils/geometry.js.map +1 -1
  54. package/dist/utils/index.d.ts +1 -3
  55. package/dist/utils/index.d.ts.map +1 -1
  56. package/dist/utils/index.js +0 -2
  57. package/dist/utils/index.js.map +1 -1
  58. package/dist/utils/mesh-processing.d.ts +16 -6
  59. package/dist/utils/mesh-processing.d.ts.map +1 -1
  60. package/dist/utils/mesh-processing.js +9 -6
  61. package/dist/utils/mesh-processing.js.map +1 -1
  62. package/dist/utils/primitive-iterator.d.ts +16 -8
  63. package/dist/utils/primitive-iterator.d.ts.map +1 -1
  64. package/dist/utils/primitive-iterator.js +32 -35
  65. package/dist/utils/primitive-iterator.js.map +1 -1
  66. package/package.json +16 -3
  67. package/{dist/color/color-lut-types.js → src/arrow/index.ts} +3 -2
  68. package/src/arrow/stream-tessellate.ts +209 -0
  69. package/src/arrow/tessellate.ts +133 -0
  70. package/src/color/index.ts +3 -3
  71. package/src/culling/lib/algorithms/oriented-bounding-box-quaternion.ts +8 -2
  72. package/src/index.ts +34 -22
  73. package/src/polygon/cut-by-grid.ts +18 -0
  74. package/src/polygon/earcut.ts +946 -668
  75. package/src/polygon/index.ts +1 -0
  76. package/src/polygon/subdivide-triangle-mesh.ts +56 -16
  77. package/src/utils/compute-vertex-normals.ts +5 -6
  78. package/src/utils/geometry.ts +32 -6
  79. package/src/utils/index.ts +5 -5
  80. package/src/utils/mesh-processing.ts +30 -14
  81. package/src/utils/primitive-iterator.ts +45 -43
  82. package/dist/color/color-lut-types.d.ts +0 -16
  83. package/dist/color/color-lut-types.d.ts.map +0 -1
  84. package/dist/color/color-lut-types.js.map +0 -1
  85. package/dist/color/sample-lut.d.ts +0 -13
  86. package/dist/color/sample-lut.d.ts.map +0 -1
  87. package/dist/color/sample-lut.js +0 -132
  88. package/dist/color/sample-lut.js.map +0 -1
  89. package/dist/utils/constants.d.ts +0 -52
  90. package/dist/utils/constants.d.ts.map +0 -1
  91. package/dist/utils/constants.js +0 -39
  92. package/dist/utils/constants.js.map +0 -1
  93. package/dist/utils/gl-type.d.ts +0 -17
  94. package/dist/utils/gl-type.d.ts.map +0 -1
  95. package/dist/utils/gl-type.js +0 -69
  96. package/dist/utils/gl-type.js.map +0 -1
  97. package/src/color/color-lut-types.ts +0 -19
  98. package/src/color/sample-lut.ts +0 -175
  99. package/src/utils/constants.ts +0 -42
  100. package/src/utils/gl-type.ts +0 -87
package/dist/index.cjs CHANGED
@@ -36,11 +36,6 @@ __export(index_exports, {
36
36
  CylinderGeometry: () => CylinderGeometry,
37
37
  CylinderShape: () => CylinderShape,
38
38
  DEFAULT_POLE_SEARCH_SEGMENT_TESTS: () => DEFAULT_POLE_SEARCH_SEGMENT_TESTS,
39
- GL: () => GL,
40
- GLType: () => GLType,
41
- GL_PRIMITIVE: () => GL_PRIMITIVE,
42
- GL_PRIMITIVE_MODE: () => GL_PRIMITIVE_MODE,
43
- GL_TYPE: () => GL_TYPE,
44
39
  Geometry: () => Geometry,
45
40
  HermiteCurve: () => HermiteCurve,
46
41
  HingeTree: () => HingeTree,
@@ -1205,111 +1200,27 @@ var LatheGeometry = class extends ParametricGeometry {
1205
1200
  }
1206
1201
  };
1207
1202
 
1208
- // dist/utils/constants.js
1209
- var GL_PRIMITIVE = {
1210
- POINTS: 0,
1211
- LINES: 1,
1212
- TRIANGLES: 4
1213
- };
1214
- var GL_PRIMITIVE_MODE = {
1215
- POINTS: 0,
1216
- LINES: 1,
1217
- LINE_LOOP: 2,
1218
- LINE_STRIP: 3,
1219
- TRIANGLES: 4,
1220
- TRIANGLE_STRIP: 5,
1221
- TRIANGLE_FAN: 6
1222
- };
1223
- var GL_TYPE = {
1224
- BYTE: 5120,
1225
- UNSIGNED_BYTE: 5121,
1226
- SHORT: 5122,
1227
- UNSIGNED_SHORT: 5123,
1228
- INT: 5124,
1229
- UNSIGNED_INT: 5125,
1230
- FLOAT: 5126,
1231
- DOUBLE: 5130,
1232
- UNSIGNED_SHORT_4_4_4_4: 32819,
1233
- UNSIGNED_SHORT_5_5_5_1: 32820,
1234
- UNSIGNED_SHORT_5_6_5: 33635
1235
- };
1236
- var GL = {
1237
- ...GL_PRIMITIVE_MODE,
1238
- ...GL_TYPE
1239
- };
1240
-
1241
- // dist/utils/gl-type.js
1242
- var GL_TYPE_TO_ARRAY_TYPE = {
1243
- [GL_TYPE.DOUBLE]: Float64Array,
1244
- [GL_TYPE.FLOAT]: Float32Array,
1245
- [GL_TYPE.UNSIGNED_SHORT]: Uint16Array,
1246
- [GL_TYPE.UNSIGNED_INT]: Uint32Array,
1247
- [GL_TYPE.UNSIGNED_BYTE]: Uint8Array,
1248
- [GL_TYPE.BYTE]: Int8Array,
1249
- [GL_TYPE.SHORT]: Int16Array,
1250
- [GL_TYPE.INT]: Int32Array,
1251
- [GL_TYPE.UNSIGNED_SHORT_4_4_4_4]: Uint16Array,
1252
- [GL_TYPE.UNSIGNED_SHORT_5_5_5_1]: Uint16Array,
1253
- [GL_TYPE.UNSIGNED_SHORT_5_6_5]: Uint16Array
1254
- };
1255
- var NAME_TO_GL_TYPE = Object.fromEntries(Object.entries(GL_TYPE).map(([name, glType]) => [name, glType]));
1256
- var TYPE_CONVERSION_ERROR = "Failed to convert GL type";
1257
- var GLType = class _GLType {
1258
- /** Returns the WebGL component constant for a typed array or typed-array constructor. */
1259
- static fromTypedArray(arrayOrType) {
1260
- const arrayType = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType;
1261
- if (arrayType === Uint8ClampedArray) {
1262
- return GL_TYPE.UNSIGNED_BYTE;
1263
- }
1264
- for (const [glType, candidateArrayType] of Object.entries(GL_TYPE_TO_ARRAY_TYPE)) {
1265
- if (candidateArrayType === arrayType) {
1266
- return Number(glType);
1267
- }
1268
- }
1269
- throw new Error(TYPE_CONVERSION_ERROR);
1270
- }
1271
- /** Returns a WebGL component constant from its uppercase name. */
1272
- static fromName(name) {
1273
- const glType = NAME_TO_GL_TYPE[name];
1274
- if (glType === void 0) {
1275
- throw new Error(TYPE_CONVERSION_ERROR);
1276
- }
1277
- return glType;
1278
- }
1279
- /** Returns the typed-array constructor for a WebGL component constant. */
1280
- static getArrayType(glType) {
1281
- const ArrayType = GL_TYPE_TO_ARRAY_TYPE[glType];
1282
- if (!ArrayType) {
1283
- throw new Error(TYPE_CONVERSION_ERROR);
1284
- }
1285
- return ArrayType;
1286
- }
1287
- /** Returns the size in bytes of one element of a WebGL component type. */
1288
- static getByteSize(glType) {
1289
- return _GLType.getArrayType(glType).BYTES_PER_ELEMENT;
1290
- }
1291
- /** Returns whether a value is a supported WebGL component type. */
1292
- static validate(glType) {
1293
- return Boolean(GL_TYPE_TO_ARRAY_TYPE[glType]);
1294
- }
1295
- /** Creates a typed view over an ArrayBuffer or an existing ArrayBuffer view. */
1296
- static createTypedArray(glType, buffer, byteOffset = 0, length) {
1297
- const ArrayType = _GLType.getArrayType(glType);
1298
- const arrayBuffer = ArrayBuffer.isView(buffer) ? buffer.buffer : buffer;
1299
- const viewByteOffset = ArrayBuffer.isView(buffer) ? buffer.byteOffset : 0;
1300
- const resolvedByteOffset = viewByteOffset + byteOffset;
1301
- const resolvedLength = length ?? Math.floor((buffer.byteLength - byteOffset) / ArrayType.BYTES_PER_ELEMENT);
1302
- return new ArrayType(arrayBuffer, resolvedByteOffset, resolvedLength);
1303
- }
1304
- };
1305
-
1306
1203
  // dist/utils/geometry.js
1307
1204
  function isGeometry(value) {
1308
1205
  if (!value || typeof value !== "object") {
1309
1206
  return false;
1310
1207
  }
1311
- const geometry = value;
1312
- return typeof geometry.mode === "number" && Boolean(geometry.attributes);
1208
+ const { topology, attributes } = value;
1209
+ if (!attributes || typeof attributes !== "object" || Array.isArray(attributes) || ArrayBuffer.isView(attributes)) {
1210
+ return false;
1211
+ }
1212
+ switch (topology) {
1213
+ case "point-list":
1214
+ case "line-list":
1215
+ case "line-strip":
1216
+ case "line-loop":
1217
+ case "triangle-list":
1218
+ case "triangle-strip":
1219
+ case "triangle-fan":
1220
+ return true;
1221
+ default:
1222
+ return false;
1223
+ }
1313
1224
  }
1314
1225
  function getAttributeValues(attribute) {
1315
1226
  if (ArrayBuffer.isView(attribute)) {
@@ -1348,75 +1259,73 @@ function* makeAttributeIterator(values, size) {
1348
1259
  }
1349
1260
 
1350
1261
  // dist/utils/primitive-iterator.js
1351
- function* makePrimitiveIterator(geometryOrIndices, attributes = {}, mode, start = 0, end) {
1352
- let indices;
1353
- if (isGeometry(geometryOrIndices)) {
1354
- const geometry = geometryOrIndices;
1355
- indices = geometry.indices ? getAttributeValues(geometry.indices) : void 0;
1356
- attributes = geometry.attributes;
1357
- mode = geometry.mode;
1358
- if (end === void 0) {
1262
+ function* makePrimitiveIterator(geometry, options = {}) {
1263
+ if (!isGeometry(geometry)) {
1264
+ throw new TypeError("Geometry with a supported topology and attributes is required");
1265
+ }
1266
+ const { start = 0 } = options;
1267
+ let end = options.end;
1268
+ const { attributes, topology } = geometry;
1269
+ const indices = geometry.indices ? getAttributeValues(geometry.indices) : void 0;
1270
+ if (end === void 0) {
1271
+ if (indices) {
1272
+ end = indices.length;
1273
+ } else {
1359
1274
  const position = getPositionAttribute(geometry);
1360
- end = (indices == null ? void 0 : indices.length) ?? getAttributeValues(position).length / (position.size || 3);
1275
+ end = getAttributeValues(position).length / (position.size || 3);
1361
1276
  }
1362
- } else if (geometryOrIndices) {
1363
- indices = getAttributeValues(geometryOrIndices);
1364
- }
1365
- if (mode === void 0) {
1366
- throw new Error("Primitive mode is required");
1367
1277
  }
1368
- end ?? (end = (indices == null ? void 0 : indices.length) ?? start);
1369
1278
  const dereference = (index) => Number(indices ? indices[index] : index);
1370
1279
  let primitiveIndex = 0;
1371
- switch (mode) {
1372
- case GL.POINTS:
1280
+ switch (topology) {
1281
+ case "point-list":
1373
1282
  for (let index = start; index < end; index++) {
1374
1283
  yield {
1375
1284
  attributes,
1376
- type: GL.POINTS,
1285
+ type: "point-list",
1377
1286
  i1: dereference(index),
1378
1287
  primitiveIndex: primitiveIndex++
1379
1288
  };
1380
1289
  }
1381
1290
  return;
1382
- case GL.LINES:
1291
+ case "line-list":
1383
1292
  for (let index = start; index + 1 < end; index += 2) {
1384
1293
  yield {
1385
1294
  attributes,
1386
- type: GL.LINES,
1295
+ type: "line-list",
1387
1296
  i1: dereference(index),
1388
1297
  i2: dereference(index + 1),
1389
1298
  primitiveIndex: primitiveIndex++
1390
1299
  };
1391
1300
  }
1392
1301
  return;
1393
- case GL.LINE_STRIP:
1302
+ case "line-strip":
1394
1303
  for (let index = start; index + 1 < end; index++) {
1395
1304
  yield {
1396
1305
  attributes,
1397
- type: GL.LINES,
1306
+ type: "line-list",
1398
1307
  i1: dereference(index),
1399
1308
  i2: dereference(index + 1),
1400
1309
  primitiveIndex: primitiveIndex++
1401
1310
  };
1402
1311
  }
1403
1312
  return;
1404
- case GL.LINE_LOOP:
1313
+ case "line-loop":
1405
1314
  for (let index = start; index < end; index++) {
1406
1315
  yield {
1407
1316
  attributes,
1408
- type: GL.LINES,
1317
+ type: "line-list",
1409
1318
  i1: dereference(index),
1410
1319
  i2: dereference(index + 1 < end ? index + 1 : start),
1411
1320
  primitiveIndex: primitiveIndex++
1412
1321
  };
1413
1322
  }
1414
1323
  return;
1415
- case GL.TRIANGLES:
1324
+ case "triangle-list":
1416
1325
  for (let index = start; index + 2 < end; index += 3) {
1417
1326
  yield {
1418
1327
  attributes,
1419
- type: GL.TRIANGLES,
1328
+ type: "triangle-list",
1420
1329
  i1: dereference(index),
1421
1330
  i2: dereference(index + 1),
1422
1331
  i3: dereference(index + 2),
@@ -1424,12 +1333,12 @@ function* makePrimitiveIterator(geometryOrIndices, attributes = {}, mode, start
1424
1333
  };
1425
1334
  }
1426
1335
  return;
1427
- case GL.TRIANGLE_STRIP:
1336
+ case "triangle-strip":
1428
1337
  for (let index = start; index + 2 < end; index++) {
1429
1338
  const odd = (index - start) % 2 === 1;
1430
1339
  yield {
1431
1340
  attributes,
1432
- type: GL.TRIANGLES,
1341
+ type: "triangle-list",
1433
1342
  i1: dereference(odd ? index + 1 : index),
1434
1343
  i2: dereference(odd ? index : index + 1),
1435
1344
  i3: dereference(index + 2),
@@ -1437,11 +1346,11 @@ function* makePrimitiveIterator(geometryOrIndices, attributes = {}, mode, start
1437
1346
  };
1438
1347
  }
1439
1348
  return;
1440
- case GL.TRIANGLE_FAN:
1349
+ case "triangle-fan":
1441
1350
  for (let index = start + 1; index + 1 < end; index++) {
1442
1351
  yield {
1443
1352
  attributes,
1444
- type: GL.TRIANGLES,
1353
+ type: "triangle-list",
1445
1354
  i1: dereference(start),
1446
1355
  i2: dereference(index),
1447
1356
  i3: dereference(index + 1),
@@ -1449,15 +1358,13 @@ function* makePrimitiveIterator(geometryOrIndices, attributes = {}, mode, start
1449
1358
  };
1450
1359
  }
1451
1360
  return;
1452
- default:
1453
- throw new Error(`Unknown primitive mode ${mode}`);
1454
1361
  }
1455
1362
  }
1456
1363
 
1457
1364
  // dist/utils/compute-vertex-normals.js
1458
1365
  var import_core2 = require("@math.gl/core");
1459
1366
  function computeVertexNormals(geometry) {
1460
- if (geometry.mode !== GL.TRIANGLES && geometry.mode !== GL.TRIANGLE_STRIP && geometry.mode !== GL.TRIANGLE_FAN) {
1367
+ if (geometry.topology !== "triangle-list" && geometry.topology !== "triangle-strip" && geometry.topology !== "triangle-fan") {
1461
1368
  throw new Error("Triangle geometry is required");
1462
1369
  }
1463
1370
  const position = getPositionAttribute(geometry);
@@ -1752,7 +1659,7 @@ function transformGeometry(geometry, matrix) {
1752
1659
  if (determinant < 0)
1753
1660
  for (let i = 0; i < indices.length; i += 3)
1754
1661
  [indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
1755
- return { mode: GL.TRIANGLES, attributes, indices };
1662
+ return { topology: "triangle-list", attributes, indices };
1756
1663
  }
1757
1664
  function mergeGeometries(geometries) {
1758
1665
  if (!geometries.length)
@@ -1790,7 +1697,7 @@ function mergeGeometries(geometries) {
1790
1697
  indices[indexOffset++] = index + vertexOffset;
1791
1698
  vertexOffset += mesh.count;
1792
1699
  }
1793
- return { mode: GL.TRIANGLES, attributes, indices };
1700
+ return { topology: "triangle-list", attributes, indices };
1794
1701
  }
1795
1702
  function weldGeometry(geometry, options = {}) {
1796
1703
  const mesh = readMesh(geometry), grid = options.positionGridSize ?? 0;
@@ -1827,7 +1734,7 @@ function weldGeometry(geometry, options = {}) {
1827
1734
  }
1828
1735
  return {
1829
1736
  geometry: {
1830
- mode: GL.TRIANGLES,
1737
+ topology: "triangle-list",
1831
1738
  attributes,
1832
1739
  indices: Uint32Array.from(mesh.indices, (index) => vertexMap[index])
1833
1740
  },
@@ -1869,7 +1776,7 @@ function getDegenerateTriangles(geometry, areaEpsilon = 0) {
1869
1776
  return Uint32Array.from(result);
1870
1777
  }
1871
1778
  function readMesh(geometry) {
1872
- if (geometry.mode !== GL.TRIANGLES)
1779
+ if (geometry.topology !== "triangle-list")
1873
1780
  throw new RangeError("Packed triangle-list geometry is required");
1874
1781
  const position = geometry.attributes["POSITION"] ? "POSITION" : geometry.attributes["positions"] ? "positions" : null;
1875
1782
  if (!position)
@@ -1948,7 +1855,10 @@ function toDyadic(value) {
1948
1855
  const exponentBits = high >>> 20 & 2047;
1949
1856
  const fraction = BigInt(high & 1048575) << 32n | BigInt(low);
1950
1857
  const n = exponentBits ? 1n << 52n | fraction : fraction;
1951
- return { n: high >>> 31 ? -n : n, exponent: exponentBits ? exponentBits - 1075 : -1074 };
1858
+ return {
1859
+ n: high >>> 31 ? -n : n,
1860
+ exponent: exponentBits ? exponentBits - 1075 : -1074
1861
+ };
1952
1862
  }
1953
1863
  function subtractDyadic(a, b) {
1954
1864
  const exponent = Math.min(a.exponent, b.exponent);
@@ -2521,14 +2431,14 @@ var DimIndex = {
2521
2431
  function getPolygonSignedArea(points, options = {}) {
2522
2432
  const { start = 0, end = points.length, plane = "xy" } = options;
2523
2433
  const dim = options.size || 2;
2524
- let area2 = 0;
2434
+ let area = 0;
2525
2435
  const i0 = DimIndex[plane[0]];
2526
2436
  const i1 = DimIndex[plane[1]];
2527
2437
  for (let i = start, j = end - dim; i < end; i += dim) {
2528
- area2 += (points[i + i0] - points[j + i0]) * (points[i + i1] + points[j + i1]);
2438
+ area += (points[i + i0] - points[j + i0]) * (points[i + i1] + points[j + i1]);
2529
2439
  j = i;
2530
2440
  }
2531
- return area2 / 2;
2441
+ return area / 2;
2532
2442
  }
2533
2443
  function forEachSegmentInPolygon(points, visitor, options = {}) {
2534
2444
  const { start = 0, end = points.length, size = 2, isClosed } = options;
@@ -2576,14 +2486,14 @@ function getWindingDirection(signedArea) {
2576
2486
  }
2577
2487
  function getPolygonSignedAreaPoints(points, options = {}) {
2578
2488
  const { start = 0, end = points.length, plane = "xy" } = options;
2579
- let area2 = 0;
2489
+ let area = 0;
2580
2490
  const i0 = DimIndex[plane[0]];
2581
2491
  const i1 = DimIndex[plane[1]];
2582
2492
  for (let i = start, j = end - 1; i < end; ++i) {
2583
- area2 += (points[i][i0] - points[j][i0]) * (points[i][i1] + points[j][i1]);
2493
+ area += (points[i][i0] - points[j][i0]) * (points[i][i1] + points[j][i1]);
2584
2494
  j = i;
2585
2495
  }
2586
- return area2 / 2;
2496
+ return area / 2;
2587
2497
  }
2588
2498
  function forEachSegmentInPolygonPoints(points, visitor, options = {}) {
2589
2499
  const { start = 0, end = points.length, isClosed } = options;
@@ -2669,546 +2579,646 @@ var Polygon = class {
2669
2579
  };
2670
2580
 
2671
2581
  // dist/polygon/earcut.js
2672
- function earcut(positions, holeIndices, dim = 2, areas, plane = "xy") {
2673
- const savedIndex = vertexPoolIndex;
2674
- const savedQueue = activeHoleQueue;
2675
- if (!holeQueues[callDepth])
2676
- holeQueues[callDepth] = [];
2677
- activeHoleQueue = holeQueues[callDepth];
2582
+ function earcut(positions, holeIndices, dim = 2, areas, plane = "xy", options = {}) {
2583
+ if (!triangulators[callDepth])
2584
+ triangulators[callDepth] = createTriangulator();
2585
+ const triangulate = triangulators[callDepth];
2678
2586
  callDepth++;
2679
2587
  try {
2680
- return triangulate(positions, holeIndices, dim, areas, plane);
2588
+ return triangulate(positions, holeIndices, dim, areas, plane, options);
2681
2589
  } finally {
2682
- activeHoleQueue.length = 0;
2683
- activeHoleQueue = savedQueue;
2684
- vertexPoolIndex = savedIndex;
2685
2590
  callDepth--;
2686
- if (callDepth === 0 && vertexPool.length > 65536)
2687
- vertexPool.length = 0;
2688
2591
  }
2689
2592
  }
2690
- function triangulate(positions, holeIndices, dim, areas, plane) {
2691
- const hasHoles = holeIndices && holeIndices.length;
2692
- const outerLen = hasHoles ? holeIndices[0] * dim : positions.length;
2693
- let outerNode = linkedList(positions, 0, outerLen, dim, true, areas && areas[0], plane);
2694
- const triangles = [];
2695
- if (!outerNode || outerNode.next === outerNode.prev)
2593
+ var triangulators = [];
2594
+ var callDepth = 0;
2595
+ function createTriangulator() {
2596
+ let balanceTriangles = false;
2597
+ const steiners = /* @__PURE__ */ new Set();
2598
+ const splitStack = [];
2599
+ let filteredOut = false;
2600
+ function triangulate(data, holeIndices, dim, areas, plane) {
2601
+ const hasHoles = holeIndices && holeIndices.length;
2602
+ const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
2603
+ if (steiners.size)
2604
+ steiners.clear();
2605
+ let outerNode = linkedList(data, 0, outerLen, dim, true, areas && areas[0], plane);
2606
+ const triangles = [];
2607
+ if (!outerNode || outerNode.next === outerNode.prev)
2608
+ return triangles;
2609
+ let minX = 0, minY = 0, invSize = 0;
2610
+ if (hasHoles)
2611
+ outerNode = eliminateHoles(data, holeIndices, outerNode, dim, areas, plane);
2612
+ if (data.length > 80 * dim) {
2613
+ const i0 = DimIndex[plane[0]], i1 = DimIndex[plane[1]];
2614
+ minX = data[i0];
2615
+ minY = data[i1];
2616
+ let maxX = minX;
2617
+ let maxY = minY;
2618
+ for (let i = dim; i < outerLen; i += dim) {
2619
+ const x = data[i + i0];
2620
+ const y = data[i + i1];
2621
+ if (x < minX)
2622
+ minX = x;
2623
+ if (y < minY)
2624
+ minY = y;
2625
+ if (x > maxX)
2626
+ maxX = x;
2627
+ if (y > maxY)
2628
+ maxY = y;
2629
+ }
2630
+ invSize = Math.max(maxX - minX, maxY - minY);
2631
+ invSize = invSize !== 0 ? 32767 / invSize : 0;
2632
+ }
2633
+ earcutLinked(outerNode, triangles, minX, minY, invSize);
2634
+ while (splitStack.length)
2635
+ earcutLinked(
2636
+ /** @type {Node} */
2637
+ splitStack.pop(),
2638
+ triangles,
2639
+ minX,
2640
+ minY,
2641
+ invSize
2642
+ );
2696
2643
  return triangles;
2697
- let invSize;
2698
- let maxX;
2699
- let maxY;
2700
- let minX;
2701
- let minY;
2702
- let x;
2703
- let y;
2704
- if (hasHoles)
2705
- outerNode = eliminateHoles(positions, holeIndices, outerNode, dim, areas, plane);
2706
- if (positions.length > 80 * dim) {
2644
+ }
2645
+ function linkedList(data, start, end, dim, clockwise, area2, plane) {
2646
+ if (start >= end)
2647
+ return null;
2648
+ let i;
2649
+ let last;
2707
2650
  const i0 = DimIndex[plane[0]];
2708
2651
  const i1 = DimIndex[plane[1]];
2709
- minX = maxX = positions[i0];
2710
- minY = maxY = positions[i1];
2711
- for (let i = dim; i < outerLen; i += dim) {
2712
- x = positions[i + i0];
2713
- y = positions[i + i1];
2714
- if (x < minX)
2715
- minX = x;
2716
- if (y < minY)
2717
- minY = y;
2718
- if (x > maxX)
2719
- maxX = x;
2720
- if (y > maxY)
2721
- maxY = y;
2722
- }
2723
- invSize = Math.max(maxX - minX, maxY - minY);
2724
- invSize = invSize !== 0 ? 32767 / invSize : 0;
2725
- }
2726
- earcutLinked(outerNode, triangles, minX, minY, invSize, 0);
2727
- return triangles;
2728
- }
2729
- function linkedList(data, start, end, dim, clockwise, area2, plane) {
2730
- if (start >= end)
2731
- return null;
2732
- let i;
2733
- let last;
2734
- const i0 = DimIndex[plane[0]];
2735
- const i1 = DimIndex[plane[1]];
2736
- if (area2 === void 0) {
2737
- let sum = 0;
2738
- let previousX = data[end - dim + i0];
2739
- let previousY = data[end - dim + i1];
2740
- for (i = start; i < end; i += dim) {
2741
- const x = data[i + i0];
2742
- const y = data[i + i1];
2743
- sum += (x - previousX) * (y + previousY);
2744
- last = insertNode(i / dim, x, y, last);
2745
- previousX = x;
2746
- previousY = y;
2747
- }
2748
- if (last && clockwise !== sum / 2 < 0) {
2749
- let node = last;
2750
- do {
2751
- const next = node.next;
2752
- node.next = node.prev;
2753
- node.prev = next;
2754
- node = next;
2755
- } while (node !== last);
2756
- last = last.prev;
2757
- }
2758
- } else if (clockwise === area2 < 0) {
2759
- for (i = start; i < end; i += dim)
2760
- last = insertNode(i / dim, data[i + i0], data[i + i1], last);
2761
- } else {
2762
- for (i = end - dim; i >= start; i -= dim)
2763
- last = insertNode(i / dim, data[i + i0], data[i + i1], last);
2764
- }
2765
- if (last && equals2(last, last.next)) {
2766
- removeNode(last);
2767
- last = last.next;
2768
- }
2769
- return last;
2770
- }
2771
- function filterPoints(start, end) {
2772
- if (!start)
2773
- return start;
2774
- if (!end)
2775
- end = start;
2776
- let p = start;
2777
- let again;
2778
- do {
2779
- again = false;
2780
- if (!p.steiner && (equals2(p, p.next) || area(p.prev, p, p.next) === 0)) {
2781
- removeNode(p);
2782
- p = end = p.prev;
2783
- if (p === p.next)
2784
- break;
2785
- again = true;
2652
+ if (area2 === void 0) {
2653
+ let sum = 0;
2654
+ let previousX = data[end - dim + i0];
2655
+ let previousY = data[end - dim + i1];
2656
+ for (i = start; i < end; i += dim) {
2657
+ const x = data[i + i0];
2658
+ const y = data[i + i1];
2659
+ sum += (x - previousX) * (y + previousY);
2660
+ last = insertNode(i / dim, x, y, last);
2661
+ previousX = x;
2662
+ previousY = y;
2663
+ }
2664
+ if (last && clockwise !== sum / 2 < 0) {
2665
+ let node = last;
2666
+ do {
2667
+ const next = node.next;
2668
+ node.next = node.prev;
2669
+ node.prev = next;
2670
+ node = next;
2671
+ } while (node !== last);
2672
+ last = last.prev;
2673
+ }
2674
+ } else if (clockwise === area2 < 0) {
2675
+ for (i = start; i < end; i += dim)
2676
+ last = insertNode(i / dim, data[i + i0], data[i + i1], last);
2786
2677
  } else {
2787
- p = p.next;
2788
- }
2789
- } while (again || p !== end);
2790
- return end;
2791
- }
2792
- function earcutLinked(ear, triangles, minX, minY, invSize, pass) {
2793
- if (!ear)
2794
- return;
2795
- if (!pass && invSize)
2796
- indexCurve(ear, minX, minY, invSize);
2797
- let stop = ear;
2798
- let prev;
2799
- let next;
2800
- while (ear.prev !== ear.next) {
2801
- prev = ear.prev;
2802
- next = ear.next;
2803
- if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
2804
- triangles.push(prev.i);
2805
- triangles.push(ear.i);
2806
- triangles.push(next.i);
2807
- removeNode(ear);
2808
- ear = next.next;
2809
- stop = next.next;
2810
- continue;
2811
- }
2812
- ear = next;
2813
- if (ear === stop) {
2814
- if (!pass) {
2815
- earcutLinked(filterPoints(ear), triangles, minX, minY, invSize, 1);
2816
- } else if (pass === 1) {
2817
- ear = cureLocalIntersections(filterPoints(ear), triangles);
2818
- earcutLinked(ear, triangles, minX, minY, invSize, 2);
2819
- } else if (pass === 2) {
2820
- splitEarcut(ear, triangles, minX, minY, invSize);
2678
+ for (i = end - dim; i >= start; i -= dim)
2679
+ last = insertNode(i / dim, data[i + i0], data[i + i1], last);
2680
+ }
2681
+ if (last && equals3(last, last.next)) {
2682
+ removeNode(last);
2683
+ last = last.next;
2684
+ }
2685
+ return last;
2686
+ }
2687
+ function filterPoints(start, end = start) {
2688
+ const full = end === start;
2689
+ let p = start, again;
2690
+ do {
2691
+ again = false;
2692
+ if (p !== p.next && (steiners.size === 0 || !steiners.has(p)) && (equals3(p, p.next) || area(p.prev, p, p.next) === 0)) {
2693
+ if (full || p === end)
2694
+ end = p.prev;
2695
+ filteredOut = true;
2696
+ removeNode(p);
2697
+ p = p.prev;
2698
+ again = true;
2699
+ } else if (full || p !== end) {
2700
+ p = p.next;
2701
+ again = !full;
2702
+ }
2703
+ } while (again || p !== end);
2704
+ return end;
2705
+ }
2706
+ function earcutLinked(ear, triangles, minX, minY, invSize) {
2707
+ if (invSize)
2708
+ indexCurve(ear, minX, minY, invSize);
2709
+ let stop = ear, cured = false, convex = invSize ? isConvex(ear) : false;
2710
+ while (ear.prev !== ear.next) {
2711
+ const prev = ear.prev;
2712
+ const next = ear.next;
2713
+ if (area(prev, ear, next) < 0 && (convex || (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)))) {
2714
+ triangles.push(prev.i, ear.i, next.i);
2715
+ removeNode(ear);
2716
+ ear = balanceTriangles ? next.next : next;
2717
+ stop = ear;
2718
+ continue;
2719
+ }
2720
+ convex = false;
2721
+ ear = next;
2722
+ if (ear === stop) {
2723
+ filteredOut = false;
2724
+ ear = filterPoints(ear);
2725
+ if (filteredOut) {
2726
+ stop = ear;
2727
+ continue;
2728
+ }
2729
+ if (!cured) {
2730
+ ear = cureLocalIntersections(ear, triangles);
2731
+ stop = ear;
2732
+ cured = true;
2733
+ continue;
2734
+ }
2735
+ splitEarcut(ear);
2736
+ break;
2821
2737
  }
2822
- break;
2823
2738
  }
2824
2739
  }
2825
- }
2826
- function isEar(ear) {
2827
- const a = ear.prev;
2828
- const b = ear;
2829
- const c = ear.next;
2830
- if (area(a, b, c) >= 0)
2831
- return false;
2832
- const ax = a.x;
2833
- const bx = b.x;
2834
- const cx = c.x;
2835
- const ay = a.y;
2836
- const by = b.y;
2837
- const cy = c.y;
2838
- const x0 = ax < bx ? ax < cx ? ax : cx : bx < cx ? bx : cx;
2839
- const y0 = ay < by ? ay < cy ? ay : cy : by < cy ? by : cy;
2840
- const x1 = ax > bx ? ax > cx ? ax : cx : bx > cx ? bx : cx;
2841
- const y1 = ay > by ? ay > cy ? ay : cy : by > cy ? by : cy;
2842
- let p = c.next;
2843
- while (p !== a) {
2844
- if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0)
2845
- return false;
2846
- p = p.next;
2847
- }
2848
- return true;
2849
- }
2850
- function isEarHashed(ear, minX, minY, invSize) {
2851
- const a = ear.prev;
2852
- const b = ear;
2853
- const c = ear.next;
2854
- if (area(a, b, c) >= 0)
2855
- return false;
2856
- const ax = a.x;
2857
- const bx = b.x;
2858
- const cx = c.x;
2859
- const ay = a.y;
2860
- const by = b.y;
2861
- const cy = c.y;
2862
- const x0 = ax < bx ? ax < cx ? ax : cx : bx < cx ? bx : cx;
2863
- const y0 = ay < by ? ay < cy ? ay : cy : by < cy ? by : cy;
2864
- const x1 = ax > bx ? ax > cx ? ax : cx : bx > cx ? bx : cx;
2865
- const y1 = ay > by ? ay > cy ? ay : cy : by > cy ? by : cy;
2866
- const minZ = zOrder(x0, y0, minX, minY, invSize);
2867
- const maxZ = zOrder(x1, y1, minX, minY, invSize);
2868
- let p = ear.prevZ;
2869
- let n = ear.nextZ;
2870
- while (p && p.z >= minZ && n && n.z <= maxZ) {
2871
- if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c && pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0)
2872
- return false;
2873
- p = p.prevZ;
2874
- if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c && pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0)
2875
- return false;
2876
- n = n.nextZ;
2740
+ function isConvex(start) {
2741
+ let p = start;
2742
+ do {
2743
+ if (area(p.prev, p, p.next) >= 0)
2744
+ return false;
2745
+ p = p.next;
2746
+ } while (p !== start);
2747
+ return true;
2877
2748
  }
2878
- while (p && p.z >= minZ) {
2879
- if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c && pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0)
2880
- return false;
2881
- p = p.prevZ;
2749
+ function isEar(ear) {
2750
+ const a = ear.prev, b = ear, c = ear.next, ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y, x0 = Math.min(ax, bx, cx), y0 = Math.min(ay, by, cy), x1 = Math.max(ax, bx, cx), y1 = Math.max(ay, by, cy);
2751
+ let p = c.next;
2752
+ while (p !== a) {
2753
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && !(ax === p.x && ay === p.y) && pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0)
2754
+ return false;
2755
+ p = p.next;
2756
+ }
2757
+ return true;
2882
2758
  }
2883
- while (n && n.z <= maxZ) {
2884
- if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c && pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0)
2885
- return false;
2886
- n = n.nextZ;
2759
+ function isEarHashed(ear, minX, minY, invSize) {
2760
+ const a = ear.prev, b = ear, c = ear.next, ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y, x0 = Math.min(ax, bx, cx), y0 = Math.min(ay, by, cy), x1 = Math.max(ax, bx, cx), y1 = Math.max(ay, by, cy), minZ = zOrder(x0, y0, minX, minY, invSize), maxZ = zOrder(x1, y1, minX, minY, invSize);
2761
+ let p = ear.prevZ;
2762
+ while (p && p.z >= minZ) {
2763
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== c && !(ax === p.x && ay === p.y) && pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0)
2764
+ return false;
2765
+ p = p.prevZ;
2766
+ }
2767
+ let n = ear.nextZ;
2768
+ while (n && n.z <= maxZ) {
2769
+ if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== c && !(ax === n.x && ay === n.y) && pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0)
2770
+ return false;
2771
+ n = n.nextZ;
2772
+ }
2773
+ return true;
2887
2774
  }
2888
- return true;
2889
- }
2890
- function cureLocalIntersections(start, triangles) {
2891
- let p = start;
2892
- do {
2893
- const a = p.prev;
2894
- const b = p.next.next;
2895
- if (!equals2(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
2896
- triangles.push(a.i);
2897
- triangles.push(p.i);
2898
- triangles.push(b.i);
2899
- removeNode(p);
2900
- removeNode(p.next);
2901
- p = start = b;
2902
- }
2903
- p = p.next;
2904
- } while (p !== start);
2905
- return filterPoints(p);
2906
- }
2907
- function splitEarcut(start, triangles, minX, minY, invSize) {
2908
- let a = start;
2909
- do {
2910
- let b = a.next.next;
2911
- while (b !== a.prev) {
2912
- if (a.i !== b.i && isValidDiagonal(a, b)) {
2913
- let c = splitPolygon(a, b);
2914
- a = filterPoints(a, a.next);
2915
- c = filterPoints(c, c.next);
2916
- earcutLinked(a, triangles, minX, minY, invSize, 0);
2917
- earcutLinked(c, triangles, minX, minY, invSize, 0);
2918
- return;
2775
+ function cureLocalIntersections(start, triangles) {
2776
+ let p = start;
2777
+ let cured = false;
2778
+ do {
2779
+ const a = p.prev, b = p.next.next;
2780
+ if (intersects(a, p, p.next, b, false) && locallyInside(a, b) && locallyInside(b, a)) {
2781
+ triangles.push(a.i, p.i, b.i);
2782
+ removeNode(p);
2783
+ removeNode(p.next);
2784
+ p = start = b;
2785
+ cured = true;
2919
2786
  }
2920
- b = b.next;
2921
- }
2922
- a = a.next;
2923
- } while (a !== start);
2924
- }
2925
- function eliminateHoles(data, holeIndices, outerNode, dim, areas, plane) {
2926
- const queue = activeHoleQueue;
2927
- queue.length = 0;
2928
- let i;
2929
- let len;
2930
- let start;
2931
- let end;
2932
- let list;
2933
- for (i = 0, len = holeIndices.length; i < len; i++) {
2934
- start = holeIndices[i] * dim;
2935
- end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
2936
- list = linkedList(data, start, end, dim, false, areas && areas[i + 1], plane);
2937
- if (!list)
2938
- continue;
2939
- if (list === list.next)
2940
- list.steiner = true;
2941
- queue.push(getLeftmost(list));
2942
- }
2943
- queue.sort(compareX);
2944
- for (i = 0; i < queue.length; i++) {
2945
- outerNode = eliminateHole(queue[i], outerNode);
2946
- }
2947
- return outerNode;
2948
- }
2949
- function compareX(a, b) {
2950
- return a.x - b.x;
2951
- }
2952
- function eliminateHole(hole, outerNode) {
2953
- const bridge = findHoleBridge(hole, outerNode);
2954
- if (!bridge) {
2955
- return outerNode;
2956
- }
2957
- const bridgeReverse = splitPolygon(bridge, hole);
2958
- filterPoints(bridgeReverse, bridgeReverse.next);
2959
- return filterPoints(bridge, bridge.next);
2960
- }
2961
- function findHoleBridge(hole, outerNode) {
2962
- let p = outerNode;
2963
- const hx = hole.x;
2964
- const hy = hole.y;
2965
- let qx = -Infinity;
2966
- let m;
2967
- do {
2968
- if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
2969
- const x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
2970
- if (x <= hx && x > qx) {
2971
- qx = x;
2972
- m = p.x < p.next.x ? p : p.next;
2973
- if (x === hx)
2974
- return m;
2975
- }
2976
- }
2977
- p = p.next;
2978
- } while (p !== outerNode);
2979
- if (!m)
2980
- return null;
2981
- const stop = m;
2982
- const mx = m.x;
2983
- const my = m.y;
2984
- let tanMin = Infinity;
2985
- let tan;
2986
- p = m;
2987
- do {
2988
- if (hx >= p.x && p.x >= mx && hx !== p.x && pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
2989
- tan = Math.abs(hy - p.y) / (hx - p.x);
2990
- if (locallyInside(p, hole) && (tan < tanMin || tan === tanMin && (p.x > m.x || p.x === m.x && sectorContainsSector(m, p)))) {
2991
- m = p;
2992
- tanMin = tan;
2787
+ p = p.next;
2788
+ } while (p !== start);
2789
+ return cured ? filterPoints(p) : p;
2790
+ }
2791
+ function splitEarcut(start) {
2792
+ let a = start;
2793
+ do {
2794
+ let b = a.next.next;
2795
+ while (b !== a.prev) {
2796
+ if (a.i !== b.i && isValidDiagonal(a, b)) {
2797
+ let c = splitPolygon(a, b);
2798
+ a = filterPoints(a, a.next);
2799
+ c = filterPoints(c, c.next);
2800
+ splitStack.push(c, a);
2801
+ return;
2802
+ }
2803
+ b = b.next;
2993
2804
  }
2805
+ a = a.next;
2806
+ } while (a !== start);
2807
+ }
2808
+ let indexActive = false;
2809
+ function eliminateHoles(data, holeIndices, outerNode, dim, areas, plane) {
2810
+ const queue = holeQueue;
2811
+ for (let i = 0, len = holeIndices.length; i < len; i++) {
2812
+ const start = holeIndices[i] * dim;
2813
+ const end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
2814
+ const list = (
2815
+ /** @type {Node} */
2816
+ linkedList(data, start, end, dim, false, areas && areas[i + 1], plane)
2817
+ );
2818
+ if (!list)
2819
+ continue;
2820
+ if (list === list.next)
2821
+ steiners.add(list);
2822
+ queue.push(getLeftmost(list));
2823
+ }
2824
+ queue.sort(compareXYSlope);
2825
+ buildBlockIndex(data.length / dim, holeIndices.length);
2826
+ indexSegment(outerNode, outerNode);
2827
+ indexActive = true;
2828
+ for (let i = 0; i < queue.length; i++) {
2829
+ outerNode = eliminateHole(queue[i], outerNode);
2830
+ }
2831
+ indexActive = false;
2832
+ return filterPoints(outerNode);
2833
+ }
2834
+ function compareXYSlope(a, b) {
2835
+ return a.x - b.x || a.y - b.y || (a.next.y - a.y) / (a.next.x - a.x) - (b.next.y - b.y) / (b.next.x - b.x);
2836
+ }
2837
+ function eliminateHole(hole, outerNode) {
2838
+ const bridge = findHoleBridge(hole, outerNode);
2839
+ if (!bridge) {
2840
+ return outerNode;
2841
+ }
2842
+ const bridgeReverse = splitPolygon(bridge, hole);
2843
+ const bridge2 = bridgeReverse.next;
2844
+ indexSegment(bridge, bridge2.next);
2845
+ filterPoints(bridgeReverse.prev, bridgeReverse);
2846
+ const a = bridge.prev;
2847
+ if (area(a, bridge, hole) === 0 && (a.x - bridge.x) * (hole.x - bridge.x) + (a.y - bridge.y) * (hole.y - bridge.y) < 0)
2848
+ return bridge;
2849
+ return filterPoints(bridge, bridge.next);
2850
+ }
2851
+ const K = 16;
2852
+ let blockBBox = new Float64Array(0);
2853
+ let numBlocks = 0;
2854
+ const blockHead = [];
2855
+ const blockStop = [];
2856
+ function buildBlockIndex(maxNodes, numHoles) {
2857
+ const maxBlocks = Math.ceil((maxNodes + 2 * numHoles) / K) + numHoles + 2;
2858
+ if (blockBBox.length < maxBlocks * 4)
2859
+ blockBBox = new Float64Array(maxBlocks * 4);
2860
+ numBlocks = 0;
2861
+ }
2862
+ function indexSegment(head, stop) {
2863
+ let p = head;
2864
+ do {
2865
+ const b = numBlocks++;
2866
+ blockHead[b] = p;
2867
+ let minX = p.x, minY = p.y, maxX = p.x, maxY = p.y;
2868
+ let k = 0;
2869
+ do {
2870
+ const c = p.next;
2871
+ p.z = b;
2872
+ if (c.x < minX)
2873
+ minX = c.x;
2874
+ if (c.x > maxX)
2875
+ maxX = c.x;
2876
+ if (c.y < minY)
2877
+ minY = c.y;
2878
+ if (c.y > maxY)
2879
+ maxY = c.y;
2880
+ p = c;
2881
+ } while (++k < K && p !== stop);
2882
+ blockStop[b] = p;
2883
+ const g = b * 4;
2884
+ blockBBox[g] = minX;
2885
+ blockBBox[g + 1] = minY;
2886
+ blockBBox[g + 2] = maxX;
2887
+ blockBBox[g + 3] = maxY;
2888
+ } while (p !== stop);
2889
+ }
2890
+ function growBlock(head, tail) {
2891
+ const g = head.z * 4;
2892
+ if (tail.x < blockBBox[g])
2893
+ blockBBox[g] = tail.x;
2894
+ if (tail.y < blockBBox[g + 1])
2895
+ blockBBox[g + 1] = tail.y;
2896
+ if (tail.x > blockBBox[g + 2])
2897
+ blockBBox[g + 2] = tail.x;
2898
+ if (tail.y > blockBBox[g + 3])
2899
+ blockBBox[g + 3] = tail.y;
2900
+ }
2901
+ function liveBlockStop(b) {
2902
+ let stop = blockStop[b];
2903
+ while (stop.prev.next !== stop)
2904
+ stop = stop.next;
2905
+ blockStop[b] = stop;
2906
+ return stop;
2907
+ }
2908
+ function liveBlockHead(b) {
2909
+ let head = blockHead[b];
2910
+ while (head.prev.next !== head)
2911
+ head = head.next;
2912
+ blockHead[b] = head;
2913
+ return head;
2914
+ }
2915
+ function findHoleBridge(hole, outerNode) {
2916
+ let p = outerNode;
2917
+ const hx = hole.x;
2918
+ const hy = hole.y;
2919
+ let qx = -Infinity;
2920
+ let m;
2921
+ if (equals3(hole, p))
2922
+ return p;
2923
+ for (let b = 0, g = 0; b < numBlocks; b++, g += 4) {
2924
+ if (hy < blockBBox[g + 1] || hy > blockBBox[g + 3] || blockBBox[g] > hx || blockBBox[g + 2] <= qx)
2925
+ continue;
2926
+ const stop = liveBlockStop(b);
2927
+ p = liveBlockHead(b);
2928
+ do {
2929
+ if (equals3(hole, p.next))
2930
+ return p.next;
2931
+ else if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
2932
+ const x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
2933
+ if (x <= hx && x > qx) {
2934
+ qx = x;
2935
+ m = p.x < p.next.x ? p : p.next;
2936
+ if (x === hx)
2937
+ return m;
2938
+ }
2939
+ }
2940
+ p = p.next;
2941
+ } while (p !== stop);
2994
2942
  }
2995
- p = p.next;
2996
- } while (p !== stop);
2997
- return m;
2998
- }
2999
- function sectorContainsSector(m, p) {
3000
- return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
3001
- }
3002
- function indexCurve(start, minX, minY, invSize) {
3003
- let p = start;
3004
- do {
3005
- if (p.z === 0)
2943
+ if (!m)
2944
+ return null;
2945
+ const mx = m.x;
2946
+ const my = m.y;
2947
+ const tminY = Math.min(hy, my);
2948
+ const tmaxY = Math.max(hy, my);
2949
+ let tanMin = Infinity;
2950
+ for (let b = 0, g = 0; b < numBlocks; b++, g += 4) {
2951
+ if (blockBBox[g + 2] < mx || blockBBox[g] > hx || blockBBox[g + 3] < tminY || blockBBox[g + 1] > tmaxY)
2952
+ continue;
2953
+ const stop = liveBlockStop(b);
2954
+ p = liveBlockHead(b);
2955
+ do {
2956
+ if (hx >= p.x && p.x >= mx && hx !== p.x && pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
2957
+ const tan = Math.abs(hy - p.y) / (hx - p.x);
2958
+ if ((locallyInside(p, hole) || p.y === hy && p.next.y === hy && p.next.x > hx) && (tan < tanMin || tan === tanMin && (p.x > m.x || p.x === m.x && sectorContainsSector(m, p)))) {
2959
+ m = p;
2960
+ tanMin = tan;
2961
+ }
2962
+ }
2963
+ p = p.next;
2964
+ } while (p !== stop);
2965
+ }
2966
+ return m;
2967
+ }
2968
+ function sectorContainsSector(m, p) {
2969
+ return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
2970
+ }
2971
+ const sortArr = [];
2972
+ let sortBuf = [];
2973
+ let zArr = new Uint32Array(0);
2974
+ let zBuf = new Uint32Array(0);
2975
+ const counts = new Uint32Array(256);
2976
+ function indexCurve(start, minX, minY, invSize) {
2977
+ let p = start;
2978
+ let n = 0;
2979
+ do {
3006
2980
  p.z = zOrder(p.x, p.y, minX, minY, invSize);
3007
- p.prevZ = p.prev;
3008
- p.nextZ = p.next;
3009
- p = p.next;
3010
- } while (p !== start);
3011
- p.prevZ.nextZ = null;
3012
- p.prevZ = null;
3013
- sortLinked(p);
3014
- }
3015
- function sortLinked(list) {
3016
- let e;
3017
- let i;
3018
- let inSize = 1;
3019
- let numMerges;
3020
- let p;
3021
- let pSize;
3022
- let q;
3023
- let qSize;
3024
- let tail;
3025
- do {
3026
- p = list;
3027
- list = null;
3028
- tail = null;
3029
- numMerges = 0;
3030
- while (p) {
3031
- numMerges++;
3032
- q = p;
3033
- pSize = 0;
3034
- for (i = 0; i < inSize; i++) {
3035
- pSize++;
3036
- q = q.nextZ;
3037
- if (!q)
3038
- break;
3039
- }
3040
- qSize = inSize;
3041
- while (pSize > 0 || qSize > 0 && q) {
3042
- if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
3043
- e = p;
3044
- p = p.nextZ;
3045
- pSize--;
3046
- } else {
3047
- e = q;
3048
- q = q.nextZ;
3049
- qSize--;
2981
+ sortArr[n++] = p;
2982
+ p = p.next;
2983
+ } while (p !== start);
2984
+ sortNodes(n);
2985
+ let prev = null;
2986
+ for (let i = 0; i < n; i++) {
2987
+ const node = sortArr[i];
2988
+ node.prevZ = prev;
2989
+ if (prev)
2990
+ prev.nextZ = node;
2991
+ prev = node;
2992
+ }
2993
+ prev.nextZ = null;
2994
+ sortArr.fill(
2995
+ /** @type {Node} */
2996
+ /** @type {unknown} */
2997
+ null,
2998
+ 0,
2999
+ n
3000
+ );
3001
+ sortBuf.fill(
3002
+ /** @type {Node} */
3003
+ /** @type {unknown} */
3004
+ null,
3005
+ 0,
3006
+ n
3007
+ );
3008
+ }
3009
+ function sortNodes(n) {
3010
+ if (n <= 32) {
3011
+ for (let i = 1; i < n; i++) {
3012
+ const node = sortArr[i], z = node.z;
3013
+ let j = i - 1;
3014
+ while (j >= 0 && sortArr[j].z > z) {
3015
+ sortArr[j + 1] = sortArr[j];
3016
+ j--;
3050
3017
  }
3051
- if (tail)
3052
- tail.nextZ = e;
3053
- else
3054
- list = e;
3055
- e.prevZ = tail;
3056
- tail = e;
3018
+ sortArr[j + 1] = node;
3057
3019
  }
3058
- p = q;
3059
- }
3060
- tail.nextZ = null;
3061
- inSize *= 2;
3062
- } while (numMerges > 1);
3063
- return list;
3064
- }
3065
- function zOrder(x, y, minX, minY, invSize) {
3066
- x = (x - minX) * invSize | 0;
3067
- y = (y - minY) * invSize | 0;
3068
- x = (x | x << 8) & 16711935;
3069
- x = (x | x << 4) & 252645135;
3070
- x = (x | x << 2) & 858993459;
3071
- x = (x | x << 1) & 1431655765;
3072
- y = (y | y << 8) & 16711935;
3073
- y = (y | y << 4) & 252645135;
3074
- y = (y | y << 2) & 858993459;
3075
- y = (y | y << 1) & 1431655765;
3076
- return x | y << 1;
3077
- }
3078
- function getLeftmost(start) {
3079
- let p = start;
3080
- let leftmost = start;
3081
- do {
3082
- if (p.x < leftmost.x || p.x === leftmost.x && p.y < leftmost.y)
3083
- leftmost = p;
3084
- p = p.next;
3085
- } while (p !== start);
3086
- return leftmost;
3087
- }
3088
- function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
3089
- return (cx - px) * (ay - py) >= (ax - px) * (cy - py) && (ax - px) * (by - py) >= (bx - px) * (ay - py) && (bx - px) * (cy - py) >= (cx - px) * (by - py);
3090
- }
3091
- function isValidDiagonal(a, b) {
3092
- return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
3093
- (locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
3094
- (area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
3095
- equals2(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0);
3096
- }
3097
- function area(p, q, r) {
3098
- return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
3099
- }
3100
- function equals2(p1, p2) {
3101
- return p1.x === p2.x && p1.y === p2.y;
3102
- }
3103
- function intersects(p1, q1, p2, q2) {
3104
- const o1 = sign(area(p1, q1, p2));
3105
- const o2 = sign(area(p1, q1, q2));
3106
- const o3 = sign(area(p2, q2, p1));
3107
- const o4 = sign(area(p2, q2, q1));
3108
- if (o1 !== o2 && o3 !== o4)
3109
- return true;
3110
- if (o1 === 0 && onSegment(p1, p2, q1))
3111
- return true;
3112
- if (o2 === 0 && onSegment(p1, q2, q1))
3113
- return true;
3114
- if (o3 === 0 && onSegment(p2, p1, q2))
3115
- return true;
3116
- if (o4 === 0 && onSegment(p2, q1, q2))
3117
- return true;
3118
- return false;
3119
- }
3120
- function onSegment(p, q, r) {
3121
- return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
3122
- }
3123
- function sign(num) {
3124
- return num > 0 ? 1 : num < 0 ? -1 : 0;
3125
- }
3126
- function intersectsPolygon(a, b) {
3127
- let p = a;
3128
- do {
3129
- if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && intersects(p, p.next, a, b))
3020
+ return;
3021
+ }
3022
+ if (zArr.length < n) {
3023
+ zArr = new Uint32Array(n);
3024
+ zBuf = new Uint32Array(n);
3025
+ sortBuf = new Array(n);
3026
+ }
3027
+ for (let i = 0; i < n; i++)
3028
+ zArr[i] = sortArr[i].z;
3029
+ radixPass(n, sortArr, zArr, sortBuf, zBuf, 0);
3030
+ radixPass(n, sortBuf, zBuf, sortArr, zArr, 8);
3031
+ radixPass(n, sortArr, zArr, sortBuf, zBuf, 16);
3032
+ radixPass(n, sortBuf, zBuf, sortArr, zArr, 24);
3033
+ }
3034
+ function radixPass(n, src, srcZ, dst, dstZ, shift) {
3035
+ counts.fill(0);
3036
+ for (let i = 0; i < n; i++)
3037
+ counts[srcZ[i] >>> shift & 255]++;
3038
+ let sum = 0;
3039
+ for (let b = 0; b < 256; b++) {
3040
+ const c = counts[b];
3041
+ counts[b] = sum;
3042
+ sum += c;
3043
+ }
3044
+ for (let i = 0; i < n; i++) {
3045
+ const z = srcZ[i];
3046
+ const pos = counts[z >>> shift & 255]++;
3047
+ dst[pos] = src[i];
3048
+ dstZ[pos] = z;
3049
+ }
3050
+ }
3051
+ function zOrder(x, y, minX, minY, invSize) {
3052
+ x = (x - minX) * invSize | 0;
3053
+ y = (y - minY) * invSize | 0;
3054
+ x = (x | x << 8) & 16711935;
3055
+ x = (x | x << 4) & 252645135;
3056
+ x = (x | x << 2) & 858993459;
3057
+ x = (x | x << 1) & 1431655765;
3058
+ y = (y | y << 8) & 16711935;
3059
+ y = (y | y << 4) & 252645135;
3060
+ y = (y | y << 2) & 858993459;
3061
+ y = (y | y << 1) & 1431655765;
3062
+ return x | y << 1;
3063
+ }
3064
+ function getLeftmost(start) {
3065
+ let p = start, leftmost = start;
3066
+ do {
3067
+ if (p.x < leftmost.x || p.x === leftmost.x && p.y < leftmost.y)
3068
+ leftmost = p;
3069
+ p = p.next;
3070
+ } while (p !== start);
3071
+ return leftmost;
3072
+ }
3073
+ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
3074
+ return (cx - px) * (ay - py) >= (ax - px) * (cy - py) && (ax - px) * (by - py) >= (bx - px) * (ay - py) && (bx - px) * (cy - py) >= (cx - px) * (by - py);
3075
+ }
3076
+ function isValidDiagonal(a, b) {
3077
+ const zeroLength = equals3(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0;
3078
+ return a.next.i !== b.i && (zeroLength || locallyInside(a, b) && locallyInside(b, a) && // // locally visible
3079
+ (area(a.prev, a, b.prev) !== 0 || area(a, b.prev, b) !== 0)) && // no opposite-facing sectors
3080
+ !intersectsPolygon(a, b) && (zeroLength || middleInside(a, b));
3081
+ }
3082
+ function area(p, q, r) {
3083
+ return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
3084
+ }
3085
+ function equals3(p1, p2) {
3086
+ return p1.x === p2.x && p1.y === p2.y;
3087
+ }
3088
+ function intersects(p1, q1, p2, q2, includeBoundary = true) {
3089
+ const o1 = area(p1, q1, p2);
3090
+ const o2 = area(p1, q1, q2);
3091
+ const o3 = area(p2, q2, p1);
3092
+ const o4 = area(p2, q2, q1);
3093
+ if ((o1 > 0 && o2 < 0 || o1 < 0 && o2 > 0) && (o3 > 0 && o4 < 0 || o3 < 0 && o4 > 0))
3130
3094
  return true;
3131
- p = p.next;
3132
- } while (p !== a);
3133
- return false;
3134
- }
3135
- function locallyInside(a, b) {
3136
- return area(a.prev, a, a.next) < 0 ? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 : area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
3137
- }
3138
- function middleInside(a, b) {
3139
- let p = a;
3140
- let inside = false;
3141
- const px = (a.x + b.x) / 2;
3142
- const py = (a.y + b.y) / 2;
3143
- do {
3144
- if (p.y > py !== p.next.y > py && p.next.y !== p.y && px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x)
3145
- inside = !inside;
3146
- p = p.next;
3147
- } while (p !== a);
3148
- return inside;
3149
- }
3150
- function splitPolygon(a, b) {
3151
- const a2 = allocateVertex(a.i, a.x, a.y);
3152
- const b2 = allocateVertex(b.i, b.x, b.y);
3153
- const an = a.next;
3154
- const bp = b.prev;
3155
- a.next = b;
3156
- b.prev = a;
3157
- a2.next = an;
3158
- an.prev = a2;
3159
- b2.next = a2;
3160
- a2.prev = b2;
3161
- bp.next = b2;
3162
- b2.prev = bp;
3163
- return b2;
3164
- }
3165
- function insertNode(i, x, y, last) {
3166
- const p = allocateVertex(i, x, y);
3167
- if (!last) {
3168
- p.prev = p;
3169
- p.next = p;
3170
- } else {
3171
- p.next = last.next;
3172
- p.prev = last;
3173
- last.next.prev = p;
3174
- last.next = p;
3175
- }
3176
- return p;
3177
- }
3178
- function removeNode(p) {
3179
- p.next.prev = p.prev;
3180
- p.prev.next = p.next;
3181
- if (p.prevZ)
3182
- p.prevZ.nextZ = p.nextZ;
3183
- if (p.nextZ)
3184
- p.nextZ.prevZ = p.prevZ;
3185
- }
3186
- var vertexPool = [];
3187
- var vertexPoolIndex = 0;
3188
- var holeQueues = [];
3189
- var activeHoleQueue = [];
3190
- var callDepth = 0;
3191
- function allocateVertex(i, x, y) {
3192
- let vertex2 = vertexPool[vertexPoolIndex];
3193
- if (!vertex2)
3194
- vertexPool[vertexPoolIndex] = vertex2 = new Vertex(i, x, y);
3195
- vertexPoolIndex++;
3196
- vertex2.i = i;
3197
- vertex2.x = x;
3198
- vertex2.y = y;
3199
- vertex2.prev = vertex2.next = vertex2.prevZ = vertex2.nextZ = null;
3200
- vertex2.z = 0;
3201
- vertex2.steiner = false;
3202
- return vertex2;
3095
+ if (!includeBoundary)
3096
+ return false;
3097
+ if (o1 === 0 && onSegment(p1, p2, q1))
3098
+ return true;
3099
+ if (o2 === 0 && onSegment(p1, q2, q1))
3100
+ return true;
3101
+ if (o3 === 0 && onSegment(p2, p1, q2))
3102
+ return true;
3103
+ if (o4 === 0 && onSegment(p2, q1, q2))
3104
+ return true;
3105
+ return false;
3106
+ }
3107
+ function onSegment(p, q, r) {
3108
+ return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
3109
+ }
3110
+ function intersectsPolygon(a, b) {
3111
+ const minX = Math.min(a.x, b.x);
3112
+ const maxX = Math.max(a.x, b.x);
3113
+ const minY = Math.min(a.y, b.y);
3114
+ const maxY = Math.max(a.y, b.y);
3115
+ let p = a;
3116
+ do {
3117
+ const n = p.next;
3118
+ if (p.x > maxX && n.x > maxX || p.x < minX && n.x < minX || p.y > maxY && n.y > maxY || p.y < minY && n.y < minY) {
3119
+ p = n;
3120
+ continue;
3121
+ }
3122
+ if (p.i !== a.i && n.i !== a.i && p.i !== b.i && n.i !== b.i && intersects(p, n, a, b))
3123
+ return true;
3124
+ p = n;
3125
+ } while (p !== a);
3126
+ return false;
3127
+ }
3128
+ function locallyInside(a, b) {
3129
+ return area(a.prev, a, a.next) < 0 ? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 : area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
3130
+ }
3131
+ function middleInside(a, b) {
3132
+ let p = a;
3133
+ let inside = false;
3134
+ const px = (a.x + b.x) / 2;
3135
+ const py = (a.y + b.y) / 2;
3136
+ do {
3137
+ const n = p.next;
3138
+ if (p.y > py !== n.y > py && px < (n.x - p.x) * (py - p.y) / (n.y - p.y) + p.x)
3139
+ inside = !inside;
3140
+ p = n;
3141
+ } while (p !== a);
3142
+ return inside;
3143
+ }
3144
+ function splitPolygon(a, b) {
3145
+ const a2 = createNode(a.i, a.x, a.y), b2 = createNode(b.i, b.x, b.y), an = a.next, bp = b.prev;
3146
+ a.next = b;
3147
+ b.prev = a;
3148
+ a2.next = an;
3149
+ an.prev = a2;
3150
+ b2.next = a2;
3151
+ a2.prev = b2;
3152
+ bp.next = b2;
3153
+ b2.prev = bp;
3154
+ return b2;
3155
+ }
3156
+ function insertNode(i, x, y, last) {
3157
+ const p = createNode(i, x, y);
3158
+ if (!last) {
3159
+ p.prev = p;
3160
+ p.next = p;
3161
+ } else {
3162
+ p.next = last.next;
3163
+ p.prev = last;
3164
+ last.next.prev = p;
3165
+ last.next = p;
3166
+ }
3167
+ return p;
3168
+ }
3169
+ function removeNode(p) {
3170
+ p.next.prev = p.prev;
3171
+ p.prev.next = p.next;
3172
+ if (p.prevZ)
3173
+ p.prevZ.nextZ = p.nextZ;
3174
+ if (p.nextZ)
3175
+ p.nextZ.prevZ = p.prevZ;
3176
+ if (indexActive)
3177
+ growBlock(p.prev, p.next);
3178
+ }
3179
+ function createNode(i, x, y) {
3180
+ let vertex2 = vertexPool[vertexPoolIndex];
3181
+ if (!vertex2)
3182
+ vertexPool[vertexPoolIndex] = vertex2 = new Vertex(i, x, y);
3183
+ vertexPoolIndex++;
3184
+ vertex2.i = i;
3185
+ vertex2.x = x;
3186
+ vertex2.y = y;
3187
+ vertex2.prev = vertex2.next = vertex2.prevZ = vertex2.nextZ = null;
3188
+ vertex2.z = 0;
3189
+ return vertex2;
3190
+ }
3191
+ const vertexPool = [];
3192
+ let vertexPoolIndex = 0;
3193
+ const holeQueue = [];
3194
+ return (positions, holeIndices, dim, areas, plane, options) => {
3195
+ try {
3196
+ balanceTriangles = options.triangulation === "balanced";
3197
+ return triangulate(positions, holeIndices, dim, areas, plane);
3198
+ } finally {
3199
+ vertexPoolIndex = 0;
3200
+ indexActive = false;
3201
+ filteredOut = false;
3202
+ steiners.clear();
3203
+ splitStack.length = holeQueue.length = blockHead.length = blockStop.length = 0;
3204
+ numBlocks = 0;
3205
+ if (vertexPool.length > 65536 || blockBBox.length > 65536 * 4 || zArr.length > 65536) {
3206
+ vertexPool.length = sortArr.length = sortBuf.length = 0;
3207
+ blockBBox = new Float64Array(0);
3208
+ zArr = new Uint32Array(0);
3209
+ zBuf = new Uint32Array(0);
3210
+ }
3211
+ }
3212
+ };
3203
3213
  }
3204
3214
  var Vertex = class {
3215
+ /** Initialize a node; links are assigned before the first read. */
3205
3216
  constructor(i, x, y) {
3206
3217
  this.prev = null;
3207
3218
  this.next = null;
3208
3219
  this.z = 0;
3209
3220
  this.prevZ = null;
3210
3221
  this.nextZ = null;
3211
- this.steiner = false;
3212
3222
  this.i = i;
3213
3223
  this.x = x;
3214
3224
  this.y = y;
@@ -3650,6 +3660,7 @@ function cutPolygonByGrid(positions, holeIndices = null, options) {
3650
3660
  const { pos, types, holes } = queue.shift();
3651
3661
  getBoundingBox(pos, size, holes[0] || pos.length, bbox);
3652
3662
  cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
3663
+ containPoint(cell, bbox[0], gridResolution);
3653
3664
  const code = bitCode(bbox[1], cell);
3654
3665
  if (code) {
3655
3666
  let parts = bisectPolygon(pos, types, size, 0, holes[0] || pos.length, cell, code);
@@ -3740,6 +3751,17 @@ function getGridCell(p, gridResolution, gridOffset, out) {
3740
3751
  out[3] = bottom + gridResolution;
3741
3752
  return out;
3742
3753
  }
3754
+ function containPoint(cell, p, gridResolution) {
3755
+ for (let axis = 0; axis < 2; axis++) {
3756
+ if (p[axis] < cell[axis]) {
3757
+ cell[axis + 2] = cell[axis];
3758
+ cell[axis] -= gridResolution;
3759
+ } else if (p[axis] >= cell[axis + 2]) {
3760
+ cell[axis] = cell[axis + 2];
3761
+ cell[axis + 2] += gridResolution;
3762
+ }
3763
+ }
3764
+ }
3743
3765
  function moveToNeighborCell(cell, gridResolution, edge) {
3744
3766
  if (edge & 8) {
3745
3767
  cell[1] += gridResolution;
@@ -3999,6 +4021,7 @@ function subdividePolyline(input, options) {
3999
4021
 
4000
4022
  // dist/polygon/subdivide-triangle-mesh.js
4001
4023
  var EDGE_FRACTIONS = [0.25, 0.5, 0.75];
4024
+ var EDGE_SAMPLE_ORDER = [1, 0, 2];
4002
4025
  var FACE_WEIGHTS = [
4003
4026
  [1 / 3, 1 / 3, 1 / 3],
4004
4027
  [0.5, 0.25, 0.25],
@@ -4010,6 +4033,8 @@ function subdivideTriangleMesh(mesh, options) {
4010
4033
  validateOptions();
4011
4034
  const vertices = [];
4012
4035
  let faces2 = [];
4036
+ const edgeErrors = /* @__PURE__ */ new Map();
4037
+ const edgeMidpointTargets = /* @__PURE__ */ new Map();
4013
4038
  for (let i = 0; i < mesh.positions.length; i += size) {
4014
4039
  const source = Array.from({ length: size }, (_, j) => mesh.positions[i + j]);
4015
4040
  if (!source.every(Number.isFinite))
@@ -4034,11 +4059,14 @@ function subdivideTriangleMesh(mesh, options) {
4034
4059
  splitEdges.add(edgeKey(a, b));
4035
4060
  }
4036
4061
  }
4062
+ } else if (face.accepted) {
4037
4063
  } else if (needsRefinement(face)) {
4038
4064
  const [a, b, c] = face.vertices;
4039
4065
  splitEdges.add(edgeKey(a, b));
4040
4066
  splitEdges.add(edgeKey(b, c));
4041
4067
  splitEdges.add(edgeKey(c, a));
4068
+ } else {
4069
+ face.accepted = true;
4042
4070
  }
4043
4071
  }
4044
4072
  if (!splitEdges.size)
@@ -4069,37 +4097,56 @@ function subdivideTriangleMesh(mesh, options) {
4069
4097
  throw new RangeError("Transform returned nonfinite coordinates");
4070
4098
  return target;
4071
4099
  }
4072
- function error(points, weights) {
4100
+ function error(points, weights, keepExact) {
4073
4101
  const source = blend(points.map((p) => p.source), weights);
4074
4102
  const linearTarget = blend(points.map((p) => p.target), weights);
4075
4103
  const exact = project(source);
4104
+ keepExact == null ? void 0 : keepExact.push(exact);
4076
4105
  if (!errorMetric)
4077
4106
  return Math.hypot(...exact.map((value2, i) => value2 - linearTarget[i]));
4078
- const value = errorMetric({ source, exact, interpolated: linearTarget });
4107
+ const value = errorMetric({ source, exact: exact.slice(), interpolated: linearTarget });
4079
4108
  if (!Number.isFinite(value) || value < 0) {
4080
4109
  throw new RangeError("errorMetric must return a finite nonnegative value");
4081
4110
  }
4082
4111
  return value;
4083
4112
  }
4113
+ function edgeError(a, b, sample) {
4114
+ const key = edgeKey(a, b);
4115
+ let errors = edgeErrors.get(key);
4116
+ if (!errors) {
4117
+ errors = [];
4118
+ edgeErrors.set(key, errors);
4119
+ }
4120
+ const slot = a <= b ? sample : EDGE_FRACTIONS.length - 1 - sample;
4121
+ let value = errors[slot];
4122
+ if (value === void 0) {
4123
+ const fraction = EDGE_FRACTIONS[sample];
4124
+ const keepExact = fraction === 0.5 ? [] : void 0;
4125
+ value = error([vertices[a], vertices[b]], [1 - fraction, fraction], keepExact);
4126
+ errors[slot] = value;
4127
+ if (keepExact)
4128
+ edgeMidpointTargets.set(key, keepExact[0]);
4129
+ }
4130
+ return value;
4131
+ }
4084
4132
  function needsRefinement(face) {
4085
4133
  const points = face.vertices.map((index) => vertices[index]);
4086
- let exceedsTolerance = false;
4134
+ const limit = tolerance ?? Infinity;
4087
4135
  for (let i = 0; i < 3; i++) {
4088
- const a = points[i];
4089
- const b = points[(i + 1) % 3];
4090
- const length = sourceEdgeLength(a, b);
4091
- if (length > maxEdgeLength)
4092
- exceedsTolerance = true;
4093
- for (const fraction of EDGE_FRACTIONS) {
4094
- if (error([a, b], [1 - fraction, fraction]) > (tolerance ?? Infinity))
4095
- exceedsTolerance = true;
4136
+ if (sourceEdgeLength(points[i], points[(i + 1) % 3]) > maxEdgeLength)
4137
+ return true;
4138
+ }
4139
+ for (const sample of EDGE_SAMPLE_ORDER) {
4140
+ for (let i = 0; i < 3; i++) {
4141
+ if (edgeError(face.vertices[i], face.vertices[(i + 1) % 3], sample) > limit)
4142
+ return true;
4096
4143
  }
4097
4144
  }
4098
4145
  for (const weights of FACE_WEIGHTS) {
4099
- if (error(points, weights) > (tolerance ?? Infinity))
4100
- exceedsTolerance = true;
4146
+ if (error(points, weights) > limit)
4147
+ return true;
4101
4148
  }
4102
- return exceedsTolerance;
4149
+ return false;
4103
4150
  }
4104
4151
  function midpoint(a, b, midpoints) {
4105
4152
  const key = edgeKey(a, b);
@@ -4118,7 +4165,7 @@ function subdivideTriangleMesh(mesh, options) {
4118
4165
  if (weights.size > 3)
4119
4166
  throw new Error("Inconsistent mesh provenance");
4120
4167
  const index = vertices.length;
4121
- vertices.push({ source, target: project(source), weights });
4168
+ vertices.push({ source, target: edgeMidpointTargets.get(key) ?? project(source), weights });
4122
4169
  midpoints.set(key, index);
4123
4170
  return index;
4124
4171
  }
@@ -4128,9 +4175,11 @@ function subdivideTriangleMesh(mesh, options) {
4128
4175
  const bc = splitEdges.has(edgeKey(b, c)) ? midpoint(b, c, midpoints) : -1;
4129
4176
  const ca = splitEdges.has(edgeKey(c, a)) ? midpoint(c, a, midpoints) : -1;
4130
4177
  const count = Number(ab >= 0) + Number(bc >= 0) + Number(ca >= 0);
4131
- if (count === 0)
4132
- emit(a, b, c);
4133
- else if (count === 3) {
4178
+ if (count === 0) {
4179
+ if (output2.length >= maxTriangles)
4180
+ throw new RangeError("Triangle mesh subdivision exceeded maxTriangles");
4181
+ output2.push(face);
4182
+ } else if (count === 3) {
4134
4183
  emit(a, ab, ca);
4135
4184
  emit(ab, b, bc);
4136
4185
  emit(ca, bc, c);
@@ -4303,12 +4352,12 @@ function cross3(a, b, p) {
4303
4352
  function validateRegion(region) {
4304
4353
  if (!Array.isArray(region) || region.length < 3 || region.length > 1024 || Array.from(region).some((p) => !Array.isArray(p) || p.length !== 2 || !Array.from(p).every(Number.isFinite)))
4305
4354
  throw new RangeError("Invalid convex clipping region");
4306
- let area2 = 0;
4355
+ let area = 0;
4307
4356
  for (let i = 1; i + 1 < region.length; i++)
4308
- area2 += cross3(region[0], region[i], region[i + 1]);
4309
- if (!Number.isFinite(area2) || area2 === 0)
4357
+ area += cross3(region[0], region[i], region[i + 1]);
4358
+ if (!Number.isFinite(area) || area === 0)
4310
4359
  throw new RangeError("Degenerate clipping region");
4311
- const result = area2 > 0 ? Array.from(region) : Array.from(region).reverse();
4360
+ const result = area > 0 ? Array.from(region) : Array.from(region).reverse();
4312
4361
  for (let i = 0; i < result.length; i++) {
4313
4362
  const a = result[i], b = result[(i + 1) % result.length];
4314
4363
  if (a[0] === b[0] && a[1] === b[1] || result.some((p) => cross3(a, b, p) < 0))
@@ -6640,7 +6689,9 @@ function getOrientedBoundingBoxQuaternion(box) {
6640
6689
  const x = new import_core22.Vector3(box.halfAxes.getColumn(0)).normalize();
6641
6690
  const y = new import_core22.Vector3(box.halfAxes.getColumn(1)).normalize();
6642
6691
  const z = new import_core22.Vector3(box.halfAxes.getColumn(2)).normalize();
6643
- return new import_core22.Quaternion().fromMatrix3(new import_core22.Matrix3([...x, ...y, ...z]));
6692
+ if (box.halfAxes.determinant() < 0)
6693
+ z.negate();
6694
+ return new import_core22.Quaternion().fromMatrix3(new import_core22.Matrix3([...x, ...y, ...z])).normalize();
6644
6695
  }
6645
6696
  function makeOrientedBoundingBoxFromQuaternion(center, halfSize, quaternion, result = new OrientedBoundingBox()) {
6646
6697
  const halfAxes = new import_core22.Matrix3().fromQuaternion(quaternion);
@@ -7364,10 +7415,10 @@ function qualifySurfaceTransition(mesh, targetPositions, options = {}) {
7364
7415
  };
7365
7416
  for (let i = 0; i < indices.length; i += 3) {
7366
7417
  const tri = indices.slice(i, i + 3);
7367
- const area2 = minimumOrientation(tri[0], tri[1], tri[2]);
7368
- if (area2 < margin)
7418
+ const area = minimumOrientation(tri[0], tri[1], tri[2]);
7419
+ if (area < margin)
7369
7420
  throw new RangeError("Triangle collapses or reverses during transition");
7370
- minimum = Math.min(minimum, area2);
7421
+ minimum = Math.min(minimum, area);
7371
7422
  for (let j = 0; j < 3; j++) {
7372
7423
  const a = tri[j], b = tri[(j + 1) % 3], key = a < b ? `${a}:${b}` : `${b}:${a}`, edge = edges.get(key);
7373
7424
  if (edge) {