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

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 (51) hide show
  1. package/dist/index.cjs +110 -161
  2. package/dist/index.cjs.map +2 -2
  3. package/dist/index.d.ts +2 -2
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/polygon/cut-by-grid.d.ts.map +1 -1
  8. package/dist/polygon/cut-by-grid.js +18 -0
  9. package/dist/polygon/cut-by-grid.js.map +1 -1
  10. package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -1
  11. package/dist/polygon/subdivide-triangle-mesh.js +55 -17
  12. package/dist/polygon/subdivide-triangle-mesh.js.map +1 -1
  13. package/dist/utils/compute-vertex-normals.d.ts +2 -2
  14. package/dist/utils/compute-vertex-normals.d.ts.map +1 -1
  15. package/dist/utils/compute-vertex-normals.js +3 -4
  16. package/dist/utils/compute-vertex-normals.js.map +1 -1
  17. package/dist/utils/geometry.d.ts +11 -4
  18. package/dist/utils/geometry.d.ts.map +1 -1
  19. package/dist/utils/geometry.js +19 -2
  20. package/dist/utils/geometry.js.map +1 -1
  21. package/dist/utils/index.d.ts +1 -3
  22. package/dist/utils/index.d.ts.map +1 -1
  23. package/dist/utils/index.js +0 -2
  24. package/dist/utils/index.js.map +1 -1
  25. package/dist/utils/mesh-processing.d.ts +16 -6
  26. package/dist/utils/mesh-processing.d.ts.map +1 -1
  27. package/dist/utils/mesh-processing.js +9 -6
  28. package/dist/utils/mesh-processing.js.map +1 -1
  29. package/dist/utils/primitive-iterator.d.ts +16 -8
  30. package/dist/utils/primitive-iterator.d.ts.map +1 -1
  31. package/dist/utils/primitive-iterator.js +32 -35
  32. package/dist/utils/primitive-iterator.js.map +1 -1
  33. package/package.json +3 -3
  34. package/src/index.ts +22 -11
  35. package/src/polygon/cut-by-grid.ts +18 -0
  36. package/src/polygon/subdivide-triangle-mesh.ts +56 -16
  37. package/src/utils/compute-vertex-normals.ts +5 -6
  38. package/src/utils/geometry.ts +32 -6
  39. package/src/utils/index.ts +5 -5
  40. package/src/utils/mesh-processing.ts +30 -14
  41. package/src/utils/primitive-iterator.ts +45 -43
  42. package/dist/utils/constants.d.ts +0 -52
  43. package/dist/utils/constants.d.ts.map +0 -1
  44. package/dist/utils/constants.js +0 -39
  45. package/dist/utils/constants.js.map +0 -1
  46. package/dist/utils/gl-type.d.ts +0 -17
  47. package/dist/utils/gl-type.d.ts.map +0 -1
  48. package/dist/utils/gl-type.js +0 -69
  49. package/dist/utils/gl-type.js.map +0 -1
  50. package/src/utils/constants.ts +0 -42
  51. 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
1277
  }
1365
- if (mode === void 0) {
1366
- throw new Error("Primitive mode is required");
1367
- }
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);
@@ -3650,6 +3560,7 @@ function cutPolygonByGrid(positions, holeIndices = null, options) {
3650
3560
  const { pos, types, holes } = queue.shift();
3651
3561
  getBoundingBox(pos, size, holes[0] || pos.length, bbox);
3652
3562
  cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
3563
+ containPoint(cell, bbox[0], gridResolution);
3653
3564
  const code = bitCode(bbox[1], cell);
3654
3565
  if (code) {
3655
3566
  let parts = bisectPolygon(pos, types, size, 0, holes[0] || pos.length, cell, code);
@@ -3740,6 +3651,17 @@ function getGridCell(p, gridResolution, gridOffset, out) {
3740
3651
  out[3] = bottom + gridResolution;
3741
3652
  return out;
3742
3653
  }
3654
+ function containPoint(cell, p, gridResolution) {
3655
+ for (let axis = 0; axis < 2; axis++) {
3656
+ if (p[axis] < cell[axis]) {
3657
+ cell[axis + 2] = cell[axis];
3658
+ cell[axis] -= gridResolution;
3659
+ } else if (p[axis] >= cell[axis + 2]) {
3660
+ cell[axis] = cell[axis + 2];
3661
+ cell[axis + 2] += gridResolution;
3662
+ }
3663
+ }
3664
+ }
3743
3665
  function moveToNeighborCell(cell, gridResolution, edge) {
3744
3666
  if (edge & 8) {
3745
3667
  cell[1] += gridResolution;
@@ -3999,6 +3921,7 @@ function subdividePolyline(input, options) {
3999
3921
 
4000
3922
  // dist/polygon/subdivide-triangle-mesh.js
4001
3923
  var EDGE_FRACTIONS = [0.25, 0.5, 0.75];
3924
+ var EDGE_SAMPLE_ORDER = [1, 0, 2];
4002
3925
  var FACE_WEIGHTS = [
4003
3926
  [1 / 3, 1 / 3, 1 / 3],
4004
3927
  [0.5, 0.25, 0.25],
@@ -4010,6 +3933,8 @@ function subdivideTriangleMesh(mesh, options) {
4010
3933
  validateOptions();
4011
3934
  const vertices = [];
4012
3935
  let faces2 = [];
3936
+ const edgeErrors = /* @__PURE__ */ new Map();
3937
+ const edgeMidpointTargets = /* @__PURE__ */ new Map();
4013
3938
  for (let i = 0; i < mesh.positions.length; i += size) {
4014
3939
  const source = Array.from({ length: size }, (_, j) => mesh.positions[i + j]);
4015
3940
  if (!source.every(Number.isFinite))
@@ -4034,11 +3959,14 @@ function subdivideTriangleMesh(mesh, options) {
4034
3959
  splitEdges.add(edgeKey(a, b));
4035
3960
  }
4036
3961
  }
3962
+ } else if (face.accepted) {
4037
3963
  } else if (needsRefinement(face)) {
4038
3964
  const [a, b, c] = face.vertices;
4039
3965
  splitEdges.add(edgeKey(a, b));
4040
3966
  splitEdges.add(edgeKey(b, c));
4041
3967
  splitEdges.add(edgeKey(c, a));
3968
+ } else {
3969
+ face.accepted = true;
4042
3970
  }
4043
3971
  }
4044
3972
  if (!splitEdges.size)
@@ -4069,37 +3997,56 @@ function subdivideTriangleMesh(mesh, options) {
4069
3997
  throw new RangeError("Transform returned nonfinite coordinates");
4070
3998
  return target;
4071
3999
  }
4072
- function error(points, weights) {
4000
+ function error(points, weights, keepExact) {
4073
4001
  const source = blend(points.map((p) => p.source), weights);
4074
4002
  const linearTarget = blend(points.map((p) => p.target), weights);
4075
4003
  const exact = project(source);
4004
+ keepExact == null ? void 0 : keepExact.push(exact);
4076
4005
  if (!errorMetric)
4077
4006
  return Math.hypot(...exact.map((value2, i) => value2 - linearTarget[i]));
4078
- const value = errorMetric({ source, exact, interpolated: linearTarget });
4007
+ const value = errorMetric({ source, exact: exact.slice(), interpolated: linearTarget });
4079
4008
  if (!Number.isFinite(value) || value < 0) {
4080
4009
  throw new RangeError("errorMetric must return a finite nonnegative value");
4081
4010
  }
4082
4011
  return value;
4083
4012
  }
4013
+ function edgeError(a, b, sample) {
4014
+ const key = edgeKey(a, b);
4015
+ let errors = edgeErrors.get(key);
4016
+ if (!errors) {
4017
+ errors = [];
4018
+ edgeErrors.set(key, errors);
4019
+ }
4020
+ const slot = a <= b ? sample : EDGE_FRACTIONS.length - 1 - sample;
4021
+ let value = errors[slot];
4022
+ if (value === void 0) {
4023
+ const fraction = EDGE_FRACTIONS[sample];
4024
+ const keepExact = fraction === 0.5 ? [] : void 0;
4025
+ value = error([vertices[a], vertices[b]], [1 - fraction, fraction], keepExact);
4026
+ errors[slot] = value;
4027
+ if (keepExact)
4028
+ edgeMidpointTargets.set(key, keepExact[0]);
4029
+ }
4030
+ return value;
4031
+ }
4084
4032
  function needsRefinement(face) {
4085
4033
  const points = face.vertices.map((index) => vertices[index]);
4086
- let exceedsTolerance = false;
4034
+ const limit = tolerance ?? Infinity;
4087
4035
  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;
4036
+ if (sourceEdgeLength(points[i], points[(i + 1) % 3]) > maxEdgeLength)
4037
+ return true;
4038
+ }
4039
+ for (const sample of EDGE_SAMPLE_ORDER) {
4040
+ for (let i = 0; i < 3; i++) {
4041
+ if (edgeError(face.vertices[i], face.vertices[(i + 1) % 3], sample) > limit)
4042
+ return true;
4096
4043
  }
4097
4044
  }
4098
4045
  for (const weights of FACE_WEIGHTS) {
4099
- if (error(points, weights) > (tolerance ?? Infinity))
4100
- exceedsTolerance = true;
4046
+ if (error(points, weights) > limit)
4047
+ return true;
4101
4048
  }
4102
- return exceedsTolerance;
4049
+ return false;
4103
4050
  }
4104
4051
  function midpoint(a, b, midpoints) {
4105
4052
  const key = edgeKey(a, b);
@@ -4118,7 +4065,7 @@ function subdivideTriangleMesh(mesh, options) {
4118
4065
  if (weights.size > 3)
4119
4066
  throw new Error("Inconsistent mesh provenance");
4120
4067
  const index = vertices.length;
4121
- vertices.push({ source, target: project(source), weights });
4068
+ vertices.push({ source, target: edgeMidpointTargets.get(key) ?? project(source), weights });
4122
4069
  midpoints.set(key, index);
4123
4070
  return index;
4124
4071
  }
@@ -4128,9 +4075,11 @@ function subdivideTriangleMesh(mesh, options) {
4128
4075
  const bc = splitEdges.has(edgeKey(b, c)) ? midpoint(b, c, midpoints) : -1;
4129
4076
  const ca = splitEdges.has(edgeKey(c, a)) ? midpoint(c, a, midpoints) : -1;
4130
4077
  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) {
4078
+ if (count === 0) {
4079
+ if (output2.length >= maxTriangles)
4080
+ throw new RangeError("Triangle mesh subdivision exceeded maxTriangles");
4081
+ output2.push(face);
4082
+ } else if (count === 3) {
4134
4083
  emit(a, ab, ca);
4135
4084
  emit(ab, b, bc);
4136
4085
  emit(ca, bc, c);