fast-mesh-wasm 0.7.0 → 0.8.0

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 (49) hide show
  1. package/README.md +43 -3
  2. package/dist/client-CJWOKQMM.d.ts +664 -0
  3. package/dist/{client-DFgvktkT.js → client-DQADH6KL.js} +1753 -123
  4. package/dist/{engine-BL5_sWAf.js → engine-D2124Vml.js} +983 -51
  5. package/dist/io.d.ts +7 -2
  6. package/dist/io.js +6 -6
  7. package/dist/{mesh-DIWD2F5-.js → mesh-BtWBMRMO.js} +2 -1
  8. package/dist/{mesh-BHTFxsXc.d.ts → mesh-DTm2Fnqa.d.ts} +3 -2
  9. package/dist/node.d.ts +4 -2
  10. package/dist/node.js +4 -4
  11. package/dist/primitives-6T0eQiSm.d.ts +157 -0
  12. package/dist/{scene-DUsj6uJD.js → primitives-wasm-C30Bk4ut.js} +1359 -64
  13. package/dist/primitives-wasm-d9m2vikb.d.ts +43 -0
  14. package/dist/{protocol-Ci9LLiwO.js → protocol-Bvjmvlb3.js} +38 -3
  15. package/dist/{protocol-DFR0JDAS.d.ts → protocol-Da0kmXuF.d.ts} +417 -3
  16. package/dist/r3f.d.ts +3 -3
  17. package/dist/r3f.js +3 -3
  18. package/dist/{react-DzrtrzZy.js → react-DIuy5dyR.js} +12 -3
  19. package/dist/react.d.ts +6 -3
  20. package/dist/react.js +1 -1
  21. package/dist/scene-like-Bx7lZKif.d.ts +4808 -0
  22. package/dist/{store-BAzHRRkt.js → store-BxzcXy9y.js} +415 -65
  23. package/dist/{store-8HWG90Jg.d.ts → store-LM3iU8E4.d.ts} +98 -10
  24. package/dist/store.d.ts +3 -3
  25. package/dist/store.js +1 -1
  26. package/dist/three.d.ts +13 -11
  27. package/dist/three.js +63 -18
  28. package/dist/views-DPMbo0Sl.js +3147 -0
  29. package/dist/wasm/fast_mesh_wasm.d.ts +541 -8
  30. package/dist/wasm/fast_mesh_wasm.js +2083 -93
  31. package/dist/wasm/fast_mesh_wasm_bg.wasm +0 -0
  32. package/dist/wasm/fast_mesh_wasm_bg.wasm.d.ts +94 -0
  33. package/dist/wasm-threads/fast_mesh_wasm.d.ts +541 -8
  34. package/dist/wasm-threads/fast_mesh_wasm.js +2084 -94
  35. package/dist/wasm-threads/fast_mesh_wasm_bg.wasm +0 -0
  36. package/dist/wasm-threads/fast_mesh_wasm_bg.wasm.d.ts +94 -0
  37. package/dist/wave6-wasm-Bt4vTlKz.js +146 -0
  38. package/dist/web.d.ts +4 -2
  39. package/dist/web.js +4 -4
  40. package/dist/worker-engine.d.ts +20 -2
  41. package/dist/worker-engine.js +1 -1
  42. package/dist/worker-entry.js +1 -1
  43. package/dist/worker.d.ts +1 -1
  44. package/dist/worker.js +1 -1
  45. package/package.json +1 -1
  46. package/dist/api-4kz45-Mq.js +0 -63
  47. package/dist/client-nvmbhtn-.d.ts +0 -398
  48. package/dist/scene-like-CR_Dpohm.d.ts +0 -2511
  49. package/dist/views-BSy-Qeg8.js +0 -584
@@ -1,6 +1,7 @@
1
1
  import { s as setMemory, t as FastMeshError } from "./core-4OcWKu3r.js";
2
- import { i as useWasmScene, n as fieldSteps, r as meshAdds, t as Scene } from "./scene-DUsj6uJD.js";
3
- import { a as supersedes, n as isLastWins, r as keyOf, t as isInit } from "./protocol-Ci9LLiwO.js";
2
+ import { Rt as wrapPackedPointCloud, Tn as decodeHeightMapOptions, wn as decodeHeightMapFrame } from "./views-DPMbo0Sl.js";
3
+ import { a as Scene, c as meshAdds, l as useWasmScene, o as fieldSteps, r as generateCheckedHeightfield, s as geodesicSteps, t as generateEncodedPrimitive } from "./primitives-wasm-C30Bk4ut.js";
4
+ import { a as supersedes, n as isLastWins, r as keyOf, t as isInit } from "./protocol-Bvjmvlb3.js";
4
5
  /**
5
6
  * A module importing a shared memory `e.m` (1 page, at most 2), exporting
6
7
  * `grow(pages)` (`memory.grow`: the old size in pages, or -1),
@@ -337,6 +338,8 @@ const STEP_POINTS = 4096;
337
338
  const BACKGROUND_ADD_TRIANGLES = 65536;
338
339
  /** The default {@link EngineOptions.addStepWork}: a few milliseconds of BVH build. */
339
340
  const ADD_STEP_WORK = 1e5;
341
+ /** The default {@link EngineOptions.geodesicStepWork}: a few milliseconds of a solve. */
342
+ const GEODESIC_STEP_WORK = 1e5;
340
343
  /** A step budget that computes a whole unit at once. */
341
344
  const ALL_POINTS = 4294967295;
342
345
  /** Bytes of a background add's render buffers copied per unit of {@link EngineOptions.addStepWork}. */
@@ -427,9 +430,11 @@ function serveEngine(endpoint, ready, options = {}) {
427
430
  if (isInit(data)) {
428
431
  if (!started && typeof ready === "function") {
429
432
  started = true;
430
- if (data.backgroundAddTriangles !== void 0) engineOptions = {
433
+ const { backgroundAddTriangles, historyBudget } = data;
434
+ engineOptions = {
431
435
  ...options,
432
- backgroundAddTriangles: data.backgroundAddTriangles
436
+ ...backgroundAddTriangles === void 0 ? {} : { backgroundAddTriangles },
437
+ ...historyBudget === void 0 ? {} : { historyBudget }
433
438
  };
434
439
  serve(new Promise((resolve) => resolve(ready(data))));
435
440
  }
@@ -552,6 +557,9 @@ var Engine = class {
552
557
  #steps = fieldSteps(this.#scene);
553
558
  /** The scene's stepped mesh adds (internal API). */
554
559
  #meshAdds = meshAdds(this.#scene);
560
+ /** Geodesic queries run in steps. */
561
+ #geodesics = geodesicSteps(this.#scene);
562
+ #geodesicStepWork;
555
563
  /** Meshes being added in the background, in arrival order. */
556
564
  #adds = [];
557
565
  /**
@@ -609,8 +617,32 @@ var Engine = class {
609
617
  #sectionMeshes = /* @__PURE__ */ new Map();
610
618
  /** Section entries ended by mesh removals this batch, reported as `null`. */
611
619
  #goneEntries = [];
620
+ /**
621
+ * Meshes out of the scene that undo or redo can bring back. Their ids stay
622
+ * bound (requests on them fail in the scene), and the fields on them stay
623
+ * known: they come back with the mesh.
624
+ */
625
+ #parked = /* @__PURE__ */ new Set();
626
+ /** Parked meshes the client removed for good meanwhile: they go when they come back. */
627
+ #doomed = /* @__PURE__ */ new Set();
628
+ /** Meshes parked, restored or dropped by the history in this batch (see `Result.presence`). */
629
+ #moved = /* @__PURE__ */ new Set();
630
+ /** What those moves left of fields (see `Result.fieldTargets`). */
631
+ #fieldTargets = /* @__PURE__ */ new Map();
632
+ /** The sections those moves took meshes from (see `Result.sectionMeshes`). */
633
+ #sectionsLost = /* @__PURE__ */ new Set();
634
+ /** What happened to the client's entries, for the next result. */
635
+ #entryEvents = [];
636
+ /** The revision of the request being run. */
637
+ #rev = 0;
638
+ /** The steps the history could not keep, for the next result. */
639
+ #permanent = [];
612
640
  /** The active drag, by the dragged mesh's client id. */
613
641
  #drag;
642
+ /** Meshes whose authoritative placement must be emitted in the next result even if delta is 0. */
643
+ #forcePlacements = /* @__PURE__ */ new Set();
644
+ /** Mirrors `this.#scene`'s open step depth so step/drag/inSteps invariants stay aligned. */
645
+ #stepDepth = 0;
614
646
  #debug;
615
647
  #onChunk;
616
648
  #tick = messageTick();
@@ -648,9 +680,21 @@ var Engine = class {
648
680
  this.#backgroundAdd = bg === void 0 || Number.isNaN(bg) ? BACKGROUND_ADD_TRIANGLES : Math.max(0, bg);
649
681
  const work = Math.floor(options.addStepWork ?? 1e5);
650
682
  this.#addStepWork = work >= 1 ? Math.min(work, ALL_POINTS) : ADD_STEP_WORK;
683
+ const geodesic = Math.floor(options.geodesicStepWork ?? 1e5);
684
+ this.#geodesicStepWork = Number.isNaN(geodesic) ? GEODESIC_STEP_WORK : Math.min(Math.max(geodesic, 1), ALL_POINTS);
651
685
  this.#threaded = (options.threads ?? 1) > 1;
652
686
  this.#keepCopies = options.keepCopies ?? false;
653
687
  this.#steps.setDefer(true);
688
+ if (options.historyBudget !== void 0) this.#scene.setHistoryBudget(options.historyBudget);
689
+ const ids = (meshes) => meshes.flatMap((h) => this.#meshIds.get(h) ?? []);
690
+ this.#scene.onPermanent(({ lost, meshes, gone, bytes }) => {
691
+ this.#permanent.push({
692
+ lost,
693
+ meshes: ids(meshes),
694
+ gone: ids(gone),
695
+ bytes
696
+ });
697
+ });
654
698
  }
655
699
  /**
656
700
  * {@link Engine.#maybeYield}, first advancing the oldest background add by
@@ -754,8 +798,13 @@ var Engine = class {
754
798
  async #step(req) {
755
799
  const b = this.#batch;
756
800
  b.rev = Math.max(b.rev, req.rev);
801
+ this.#rev = req.rev;
757
802
  const key = keyOf(req);
758
803
  if (this.#superseded(req, key) || this.#abandoned(req, key)) return true;
804
+ if (req.kind === "query" && this.#cancelled(req.qid)) {
805
+ this.#replyCancelled(req.qid, b);
806
+ return true;
807
+ }
759
808
  if (this.#adds.length > 0 && this.#dependsOnAdds(req) && !await this.#finishAdds(req)) return false;
760
809
  this.#unpack(req);
761
810
  try {
@@ -770,7 +819,8 @@ var Engine = class {
770
819
  if (req.kind === "repair" && this.#inSteps(req)) await this.#repairInSteps(req, b);
771
820
  else if (req.kind === "field" && req.op === "add") {
772
821
  if (!await this.#addFieldInSteps(req, key)) return true;
773
- } else this.#run(req, b);
822
+ } else if (req.kind === "query" && (req.name === "geodesicDistances" || req.name === "shortestSurfacePath")) await this.#geodesicInSteps(req, b);
823
+ else this.#run(req, b);
774
824
  b.ready(key, req.rev);
775
825
  this.#onChunk?.({
776
826
  kind: req.kind,
@@ -792,12 +842,78 @@ var Engine = class {
792
842
  }
793
843
  return true;
794
844
  }
845
+ /** Whether a queued request cancels the query `qid`. */
846
+ #cancelled(qid) {
847
+ return this.#queue.some((item) => item !== END && item.kind === "cancel" && item.qid === qid);
848
+ }
849
+ #replyCancelled(qid, b) {
850
+ b.replies.push({
851
+ qid,
852
+ ok: false,
853
+ error: {
854
+ code: "CANCELLED",
855
+ message: "the query was cancelled"
856
+ }
857
+ });
858
+ this.#onChunk?.({ kind: "queryCancel" });
859
+ }
860
+ /**
861
+ * Runs a query of geodesic distances, or of a path (its distances, then
862
+ * the path in one go), in steps of {@link EngineOptions.geodesicStepWork},
863
+ * yielding between them once the time budget is spent: a queued `cancel`
864
+ * of it then ends it. Other requests wait for it, so its mesh does not
865
+ * change meanwhile.
866
+ */
867
+ async #geodesicInSteps(req, b) {
868
+ const args = req.args;
869
+ const mesh = this.#mesh(args.mesh);
870
+ let distances = req.name === "shortestSurfacePath" ? args.options?.distances : void 0;
871
+ if (distances === void 0) {
872
+ const steps = this.#geodesics;
873
+ const token = steps.begin(mesh, args.sources, args.options);
874
+ try {
875
+ while (!steps.step(token, this.#geodesicStepWork)) {
876
+ this.#onChunk?.({
877
+ kind: "geodesicStep",
878
+ id: args.mesh
879
+ });
880
+ await this.#maybeYield();
881
+ if (this.#cancelled(req.qid)) {
882
+ this.#replyCancelled(req.qid, b);
883
+ return;
884
+ }
885
+ }
886
+ distances = steps.finish(token);
887
+ } finally {
888
+ steps.drop(token);
889
+ }
890
+ }
891
+ if (req.name === "geodesicDistances") {
892
+ b.replies.push({
893
+ qid: req.qid,
894
+ ok: true,
895
+ value: distances
896
+ });
897
+ b.transfer.push(distances.buffer);
898
+ } else {
899
+ const value = this.#scene.shortestSurfacePath(mesh, args.sources, args.targetVertex, {
900
+ ...args.options,
901
+ distances
902
+ });
903
+ b.replies.push({
904
+ qid: req.qid,
905
+ ok: true,
906
+ value
907
+ });
908
+ }
909
+ }
795
910
  /**
796
911
  * Whether an add runs in the background: the port can post updates, the
797
- * mesh is large enough, and the add is not asked to run in place.
912
+ * mesh is large enough, and the add is not asked to run in place (nor
913
+ * undoable: it must land in the step it was made in).
798
914
  */
799
915
  #inBackground(req) {
800
- return this.#port.postUpdate !== void 0 && req.inline !== true && req.indices.byteLength / 12 >= this.#backgroundAdd;
916
+ return this.#port.postUpdate !== void 0 && req.inline !== true && req.options.undoable !== true && req.indices.byteLength / 12 >= this.#backgroundAdd;
801
917
  }
802
918
  /**
803
919
  * Starts adding a mesh in the background: its inputs upload into wasm
@@ -877,14 +993,51 @@ var Engine = class {
877
993
  #dependsOnAdds(req) {
878
994
  switch (req.kind) {
879
995
  case "addMesh":
880
- case "removeMesh": return true;
996
+ case "removeMesh":
997
+ case "booleanBatch":
998
+ case "cutByPlane":
999
+ case "extractSelectedFaces":
1000
+ case "splitSelectedFaces":
1001
+ case "reconstructPointCloud":
1002
+ case "addHeightfield":
1003
+ case "convexHull":
1004
+ case "convexHullPoints":
1005
+ case "wrap":
1006
+ case "punchDrainHole":
1007
+ case "placeMesh":
1008
+ case "moveWithCollision":
1009
+ case "commitCollisionInteraction": return true;
881
1010
  case "compact":
882
1011
  case "setPlacement":
883
1012
  case "drag":
884
1013
  case "edit":
885
- case "repair": return this.#pending(req.id);
886
- case "join": return this.#pending(req.target, req.source);
887
- case "register": return this.#pending(req.moving, req.fixed);
1014
+ case "repair":
1015
+ case "trimByCurve":
1016
+ case "fillHoleContinuity":
1017
+ case "fillAllHolesContinuity":
1018
+ case "bridgePartialArcs":
1019
+ case "applyDyntopo":
1020
+ case "dyntopoDab":
1021
+ case "deleteSelectedFaces":
1022
+ case "extrudeFaces":
1023
+ case "extrudeBase":
1024
+ case "applyFfd":
1025
+ case "applyArap":
1026
+ case "applyKelvinlet":
1027
+ case "applyXpbd":
1028
+ case "alignToFittedPrimitive":
1029
+ case "hollowMesh":
1030
+ case "latticeFill":
1031
+ case "denoise":
1032
+ case "delaunayFlips": return this.#pending(req.id);
1033
+ case "join":
1034
+ case "boolean": return this.#pending(req.target, req.source);
1035
+ case "setCollisionRule":
1036
+ case "clearCollisionRule": return this.#pending(req.a, req.b);
1037
+ case "setDefaultCollisionRule": return false;
1038
+ case "register":
1039
+ case "alignByLandmarks":
1040
+ case "registerMultistage": return this.#pending(req.moving, req.fixed);
888
1041
  case "coloring": return this.#pending(req.id) || req.rule !== null && this.#fieldPending(req.rule?.field);
889
1042
  case "fetchFieldValues": return this.#fieldPending(req.field);
890
1043
  case "field": return req.desc !== void 0 ? this.#pending(req.desc.on, ...req.desc.to) : this.#fieldPending(req.field);
@@ -901,7 +1054,10 @@ var Engine = class {
901
1054
  case "fieldTargetStatus": return this.#fieldPending(a.field);
902
1055
  case "closestPoint": return a.options?.mesh === void 0 || this.#pending(a.options.mesh);
903
1056
  case "findContacts": return a.pairs === void 0 || this.#pending(...a.pairs.flat());
904
- default: return this.#pending(a.a, a.b, a.mesh);
1057
+ case "wrapPlan":
1058
+ case "heightMapFrame":
1059
+ case "heightMap": return this.#pending(...req.args.meshes);
1060
+ default: return this.#pending(a.a, a.b, a.mesh, a.source, a.target);
905
1061
  }
906
1062
  }
907
1063
  default: return false;
@@ -959,8 +1115,32 @@ var Engine = class {
959
1115
  case "edit":
960
1116
  case "repair":
961
1117
  case "compact":
1118
+ case "cutByPlane":
1119
+ case "trimByCurve":
1120
+ case "fillHoleContinuity":
1121
+ case "fillAllHolesContinuity":
1122
+ case "bridgePartialArcs":
1123
+ case "applyDyntopo":
1124
+ case "dyntopoDab":
1125
+ case "deleteSelectedFaces":
1126
+ case "extrudeFaces":
1127
+ case "extrudeBase":
1128
+ case "splitSelectedFaces":
1129
+ case "applyFfd":
1130
+ case "applyArap":
1131
+ case "applyKelvinlet":
1132
+ case "applyXpbd":
1133
+ case "hollowMesh":
1134
+ case "latticeFill":
1135
+ case "denoise":
1136
+ case "delaunayFlips":
1137
+ case "punchDrainHole":
962
1138
  case "removeMesh": return req.id === id;
1139
+ case "boolean":
1140
+ case "placeMesh": return req.target === id;
963
1141
  case "join": return req.target === id || req.source === id;
1142
+ case "moveWithCollision":
1143
+ case "commitCollisionInteraction":
964
1144
  case "undo":
965
1145
  case "redo": return true;
966
1146
  default: return false;
@@ -1208,9 +1388,36 @@ var Engine = class {
1208
1388
  case "edit":
1209
1389
  case "repair":
1210
1390
  case "compact":
1391
+ case "cutByPlane":
1392
+ case "trimByCurve":
1393
+ case "fillHoleContinuity":
1394
+ case "fillAllHolesContinuity":
1395
+ case "bridgePartialArcs":
1396
+ case "applyDyntopo":
1397
+ case "dyntopoDab":
1398
+ case "deleteSelectedFaces":
1399
+ case "extrudeFaces":
1400
+ case "extrudeBase":
1401
+ case "splitSelectedFaces":
1402
+ case "applyFfd":
1403
+ case "applyArap":
1404
+ case "applyKelvinlet":
1405
+ case "applyXpbd":
1406
+ case "alignToFittedPrimitive":
1407
+ case "hollowMesh":
1408
+ case "latticeFill":
1409
+ case "denoise":
1410
+ case "delaunayFlips":
1411
+ case "punchDrainHole":
1211
1412
  case "removeMesh": return meshes.has(req.id);
1413
+ case "boolean":
1414
+ case "placeMesh": return meshes.has(req.target);
1212
1415
  case "join": return meshes.has(req.target) || meshes.has(req.source);
1213
- case "register": return meshes.has(req.moving);
1416
+ case "register":
1417
+ case "alignByLandmarks":
1418
+ case "registerMultistage": return meshes.has(req.moving);
1419
+ case "moveWithCollision":
1420
+ case "commitCollisionInteraction":
1214
1421
  case "undo":
1215
1422
  case "redo": return true;
1216
1423
  case "field": {
@@ -1383,7 +1590,7 @@ var Engine = class {
1383
1590
  return Array.isArray(req.payload) && req.payload.length > 1 && !req.payload.some((o) => {
1384
1591
  const name = o?.op;
1385
1592
  return name === "captureRestSizes" || name === "captureStrokeState";
1386
- }) && this.#drag === void 0 && !this.#scene.canRedo();
1593
+ }) && this.#drag === void 0 && this.#stepDepth === 0 && !this.#scene.canRedo();
1387
1594
  }
1388
1595
  async #repairInSteps(req, b) {
1389
1596
  const s = this.#scene;
@@ -1527,17 +1734,28 @@ var Engine = class {
1527
1734
  }
1528
1735
  /**
1529
1736
  * Drops engine-side references of a removed mesh: fields on it (or only
1530
- * to it) and its section entries. The scene did the same with the mesh.
1737
+ * to it) and its section entries. The scene did the same with the mesh. A
1738
+ * `"parked"` mesh keeps the fields on it, and leaves those of other parked
1739
+ * meshes alone: they come back together. What is left of the fields is
1740
+ * reported when the client did not ask for the removal, or may see it in
1741
+ * another order (`"parked"` and `"gone"`).
1531
1742
  */
1532
- #dropMeshRefs(id) {
1533
- for (const [field, [on, ...to]] of this.#fieldMeshes) if (on === id || to.length === 1 && to[0] === id) this.#dropField(field);
1534
- else if (to.includes(id)) {
1535
- this.#fieldMeshes.set(field, [on, ...to.filter((m) => m !== id)]);
1536
- const desc = this.#deferred.get(field);
1537
- if (desc !== void 0) this.#deferred.set(field, {
1538
- ...desc,
1539
- to: desc.to.filter((m) => m !== id)
1540
- });
1743
+ #dropMeshRefs(id, why = "removed") {
1744
+ for (const [field, [on, ...to]] of this.#fieldMeshes) {
1745
+ if (why === "parked" && this.#parked.has(on)) continue;
1746
+ if (on === id || to.length === 1 && to[0] === id) {
1747
+ this.#dropField(field);
1748
+ if (why !== "removed") this.#fieldTargets.set(field, null);
1749
+ } else if (to.includes(id)) {
1750
+ const left = to.filter((m) => m !== id);
1751
+ this.#fieldMeshes.set(field, [on, ...left]);
1752
+ if (why !== "removed") this.#fieldTargets.set(field, left);
1753
+ const desc = this.#deferred.get(field);
1754
+ if (desc !== void 0) this.#deferred.set(field, {
1755
+ ...desc,
1756
+ to: desc.to.filter((m) => m !== id)
1757
+ });
1758
+ }
1541
1759
  }
1542
1760
  for (const [section, meshes] of this.#sectionMeshes) if (meshes.includes(id)) {
1543
1761
  this.#sectionMeshes.set(section, meshes.filter((m) => m !== id));
@@ -1545,6 +1763,54 @@ var Engine = class {
1545
1763
  section,
1546
1764
  mesh: id
1547
1765
  });
1766
+ if (why !== "removed") this.#sectionsLost.add(section);
1767
+ }
1768
+ }
1769
+ /**
1770
+ * After a request that may park or restore meshes (an undoable removal, a
1771
+ * join, an undo, a redo, a cancelled step): brings the engine's books in
1772
+ * line with where the scene has each mesh.
1773
+ */
1774
+ #syncPresence() {
1775
+ const left = [];
1776
+ for (const [id, h] of this.#meshes) {
1777
+ const live = this.#scene.has(h);
1778
+ if (live !== this.#parked.has(id)) continue;
1779
+ this.#moved.add(id);
1780
+ if (live) {
1781
+ this.#parked.delete(id);
1782
+ if (this.#doomed.delete(id)) {
1783
+ this.#scene.removeMesh(h);
1784
+ this.#forgetMesh(id, h);
1785
+ }
1786
+ } else {
1787
+ this.#parked.add(id);
1788
+ left.push(id);
1789
+ }
1790
+ }
1791
+ for (const id of left) {
1792
+ if (this.#drag?.id === id) this.#drag = void 0;
1793
+ this.#dropMeshRefs(id, "parked");
1794
+ }
1795
+ }
1796
+ /** Forgets a mesh removed for good. */
1797
+ #forgetMesh(id, h) {
1798
+ this.#meshes.delete(id);
1799
+ this.#removed.push(h);
1800
+ if (this.#drag?.id === id) this.#drag = void 0;
1801
+ this.#dropMeshRefs(id);
1802
+ }
1803
+ /** Forgets the parked meshes the history has dropped for good. */
1804
+ #dropGone() {
1805
+ for (const id of [...this.#parked]) {
1806
+ const h = this.#meshes.get(id);
1807
+ if (this.#scene.isRestorable(h)) continue;
1808
+ this.#parked.delete(id);
1809
+ this.#doomed.delete(id);
1810
+ this.#moved.add(id);
1811
+ this.#meshes.delete(id);
1812
+ this.#removed.push(h);
1813
+ this.#dropMeshRefs(id, "gone");
1548
1814
  }
1549
1815
  }
1550
1816
  #run(req, b) {
@@ -1563,22 +1829,26 @@ var Engine = class {
1563
1829
  return;
1564
1830
  }
1565
1831
  case "removeMesh": {
1832
+ if (req.undoable === true) this.#moved.add(req.id);
1833
+ if (this.#parked.has(req.id)) {
1834
+ if (req.undoable !== true) this.#doomed.add(req.id);
1835
+ return;
1836
+ }
1566
1837
  const h = this.#mesh(req.id);
1838
+ if (req.undoable === true) {
1839
+ s.removeMesh(h, { undoable: true });
1840
+ this.#syncPresence();
1841
+ return;
1842
+ }
1567
1843
  s.removeMesh(h);
1568
- this.#meshes.delete(req.id);
1569
- this.#removed.push(h);
1570
- if (this.#drag?.id === req.id) this.#drag = void 0;
1571
- this.#dropMeshRefs(req.id);
1844
+ this.#forgetMesh(req.id, h);
1572
1845
  return;
1573
1846
  }
1574
1847
  case "join": {
1575
1848
  const target = this.#mesh(req.target);
1576
1849
  const source = this.#mesh(req.source);
1577
1850
  const value = s.join(target, source, req.payload);
1578
- this.#meshes.delete(req.source);
1579
- this.#removed.push(source);
1580
- if (this.#drag?.id === req.source) this.#drag = void 0;
1581
- this.#dropMeshRefs(req.source);
1851
+ this.#syncPresence();
1582
1852
  if (req.qid !== void 0) b.replies.push({
1583
1853
  qid: req.qid,
1584
1854
  ok: true,
@@ -1595,9 +1865,401 @@ var Engine = class {
1595
1865
  });
1596
1866
  return;
1597
1867
  }
1868
+ case "boolean":
1869
+ s.boolean(this.#mesh(req.target), this.#mesh(req.source), req.payload.op, req.payload);
1870
+ b.replies.push({
1871
+ qid: req.qid,
1872
+ ok: true,
1873
+ value: void 0
1874
+ });
1875
+ return;
1876
+ case "booleanBatch": {
1877
+ if (this.#meshes.has(req.addedId) || this.#adds.some((a) => a.id === req.addedId)) throw invalid(`mesh client id ${req.addedId} is already in use`);
1878
+ const added = s.booleanBatch(this.#meshList(req.meshes), req.tree, req.options);
1879
+ this.#meshes.set(req.addedId, added);
1880
+ this.#meshIds.set(added, req.addedId);
1881
+ b.replies.push({
1882
+ qid: req.qid,
1883
+ ok: true,
1884
+ value: void 0
1885
+ });
1886
+ return;
1887
+ }
1888
+ case "cutByPlane": {
1889
+ if (this.#meshes.has(req.addedId) || this.#adds.some((a) => a.id === req.addedId)) throw invalid(`mesh client id ${req.addedId} is already in use`);
1890
+ const r = s.cutByPlane(this.#mesh(req.id), req.plane, { cap: req.cap });
1891
+ this.#meshes.set(req.addedId, r.removed);
1892
+ this.#meshIds.set(r.removed, req.addedId);
1893
+ b.replies.push({
1894
+ qid: req.qid,
1895
+ ok: true,
1896
+ value: {
1897
+ keptTriangles: r.keptTriangles,
1898
+ removedTriangles: r.removedTriangles,
1899
+ capTriangles: r.capTriangles
1900
+ }
1901
+ });
1902
+ return;
1903
+ }
1904
+ case "trimByCurve": {
1905
+ const r = s.trimByCurve(this.#mesh(req.id), req.payload);
1906
+ b.replies.push({
1907
+ qid: req.qid,
1908
+ ok: true,
1909
+ value: {
1910
+ trianglesAdded: r.trianglesAdded,
1911
+ trianglesRemoved: r.trianglesRemoved,
1912
+ imprintTriangles: r.imprintTriangles,
1913
+ cutTriangles: r.cutTriangles,
1914
+ removedArea: r.removedArea,
1915
+ verticesAdded: r.verticesAdded,
1916
+ seamEdges: r.seamEdges
1917
+ }
1918
+ });
1919
+ return;
1920
+ }
1921
+ case "fillHoleContinuity": {
1922
+ const value = s.fillHoleContinuity(this.#mesh(req.id), req.loopIndex, req.options);
1923
+ b.replies.push({
1924
+ qid: req.qid,
1925
+ ok: true,
1926
+ value
1927
+ });
1928
+ return;
1929
+ }
1930
+ case "fillAllHolesContinuity": {
1931
+ const value = s.fillAllHolesContinuity(this.#mesh(req.id), req.options);
1932
+ b.replies.push({
1933
+ qid: req.qid,
1934
+ ok: true,
1935
+ value
1936
+ });
1937
+ return;
1938
+ }
1939
+ case "bridgePartialArcs": {
1940
+ const value = s.bridgePartialArcs(this.#mesh(req.id), req.loopA, req.loopB, req.options);
1941
+ b.replies.push({
1942
+ qid: req.qid,
1943
+ ok: true,
1944
+ value
1945
+ });
1946
+ return;
1947
+ }
1948
+ case "applyDyntopo": {
1949
+ const value = s.applyDyntopo(this.#mesh(req.id), req.center, req.radius, req.options, req.pinned);
1950
+ b.replies.push({
1951
+ qid: req.qid,
1952
+ ok: true,
1953
+ value
1954
+ });
1955
+ if (value.createdVertices.buffer instanceof ArrayBuffer) b.transfer.push(value.createdVertices.buffer);
1956
+ if (value.removedVertices.buffer instanceof ArrayBuffer) b.transfer.push(value.removedVertices.buffer);
1957
+ return;
1958
+ }
1959
+ case "dyntopoDab": {
1960
+ const value = s.dyntopoDab(this.#mesh(req.id), req.dab, req.options);
1961
+ b.replies.push({
1962
+ qid: req.qid,
1963
+ ok: true,
1964
+ value
1965
+ });
1966
+ if (value.createdVertices.buffer instanceof ArrayBuffer) b.transfer.push(value.createdVertices.buffer);
1967
+ if (value.removedVertices.buffer instanceof ArrayBuffer) b.transfer.push(value.removedVertices.buffer);
1968
+ return;
1969
+ }
1970
+ case "alignByLandmarks": {
1971
+ const value = s.alignByLandmarks(this.#mesh(req.moving), this.#mesh(req.fixed), req.pairs, req.allowScaling);
1972
+ b.replies.push({
1973
+ qid: req.qid,
1974
+ ok: true,
1975
+ value
1976
+ });
1977
+ return;
1978
+ }
1979
+ case "registerMultistage": {
1980
+ const value = s.registerMultistage(this.#mesh(req.moving), this.#mesh(req.fixed), req.payload);
1981
+ b.replies.push({
1982
+ qid: req.qid,
1983
+ ok: true,
1984
+ value
1985
+ });
1986
+ return;
1987
+ }
1988
+ case "deleteSelectedFaces": {
1989
+ const value = s.deleteSelectedFaces(this.#mesh(req.id), req.selectedTris);
1990
+ b.replies.push({
1991
+ qid: req.qid,
1992
+ ok: true,
1993
+ value
1994
+ });
1995
+ return;
1996
+ }
1997
+ case "extrudeFaces": {
1998
+ const value = s.extrudeFaces(this.#mesh(req.id), req.triangles, req.options);
1999
+ b.replies.push({
2000
+ qid: req.qid,
2001
+ ok: true,
2002
+ value
2003
+ });
2004
+ return;
2005
+ }
2006
+ case "extrudeBase": {
2007
+ const value = s.extrudeBase(this.#mesh(req.id), req.options);
2008
+ b.replies.push({
2009
+ qid: req.qid,
2010
+ ok: true,
2011
+ value
2012
+ });
2013
+ return;
2014
+ }
2015
+ case "extractSelectedFaces": {
2016
+ this.#ensureUnused(req.addedId);
2017
+ const added = s.extractSelectedFaces(this.#mesh(req.id), req.selectedTris, { normals: req.normals });
2018
+ this.#bindAdded(req.addedId, added);
2019
+ b.replies.push({
2020
+ qid: req.qid,
2021
+ ok: true,
2022
+ value: void 0
2023
+ });
2024
+ return;
2025
+ }
2026
+ case "splitSelectedFaces": {
2027
+ this.#ensureUnused(req.addedId);
2028
+ const r = s.splitSelectedFaces(this.#mesh(req.id), req.selectedTris, { normals: req.normals });
2029
+ this.#bindAdded(req.addedId, r.detached);
2030
+ const value = {
2031
+ detached: req.addedId,
2032
+ removedTriangles: r.removedTriangles,
2033
+ removedVertices: r.removedVertices
2034
+ };
2035
+ b.replies.push({
2036
+ qid: req.qid,
2037
+ ok: true,
2038
+ value
2039
+ });
2040
+ return;
2041
+ }
2042
+ case "applyFfd": {
2043
+ const value = s.applyFfd(this.#mesh(req.id), req.options);
2044
+ b.replies.push({
2045
+ qid: req.qid,
2046
+ ok: true,
2047
+ value
2048
+ });
2049
+ return;
2050
+ }
2051
+ case "applyArap": {
2052
+ const value = s.applyArap(this.#mesh(req.id), req.options);
2053
+ b.replies.push({
2054
+ qid: req.qid,
2055
+ ok: true,
2056
+ value
2057
+ });
2058
+ return;
2059
+ }
2060
+ case "applyKelvinlet": {
2061
+ const value = s.applyKelvinlet(this.#mesh(req.id), req.options);
2062
+ b.replies.push({
2063
+ qid: req.qid,
2064
+ ok: true,
2065
+ value
2066
+ });
2067
+ return;
2068
+ }
2069
+ case "applyXpbd": {
2070
+ const value = s.applyXpbd(this.#mesh(req.id), req.options);
2071
+ b.replies.push({
2072
+ qid: req.qid,
2073
+ ok: true,
2074
+ value
2075
+ });
2076
+ return;
2077
+ }
2078
+ case "setDefaultCollisionRule":
2079
+ s.setDefaultCollisionRule(req.rule);
2080
+ return;
2081
+ case "setCollisionRule":
2082
+ s.setCollisionRule(this.#mesh(req.a), this.#mesh(req.b), req.rule);
2083
+ return;
2084
+ case "clearCollisionRule":
2085
+ s.clearCollisionRule(this.#mesh(req.a), this.#mesh(req.b));
2086
+ return;
2087
+ case "moveWithCollision": {
2088
+ const r = s.moveWithCollision(this.#mesh(req.id), req.placement, req.isRelease);
2089
+ b.replies.push({
2090
+ qid: req.qid,
2091
+ ok: true,
2092
+ value: this.#mapCollisionReport(r)
2093
+ });
2094
+ return;
2095
+ }
2096
+ case "commitCollisionInteraction": {
2097
+ const r = s.commitCollisionInteraction(this.#mesh(req.id));
2098
+ b.replies.push({
2099
+ qid: req.qid,
2100
+ ok: true,
2101
+ value: this.#mapCollisionReport(r)
2102
+ });
2103
+ return;
2104
+ }
2105
+ case "alignToFittedPrimitive": {
2106
+ const value = s.alignToFittedPrimitive(this.#mesh(req.id), req.primitive, req.targetAxis, req.targetOrigin);
2107
+ b.replies.push({
2108
+ qid: req.qid,
2109
+ ok: true,
2110
+ value
2111
+ });
2112
+ return;
2113
+ }
2114
+ case "reconstructPointCloud": {
2115
+ this.#ensureUnused(req.addedId);
2116
+ const added = s.reconstructPointCloud(wrapPackedPointCloud(req.cloud, req.origin), req.options);
2117
+ this.#bindAdded(req.addedId, added);
2118
+ b.replies.push({
2119
+ qid: req.qid,
2120
+ ok: true,
2121
+ value: void 0
2122
+ });
2123
+ return;
2124
+ }
2125
+ case "addHeightfield": {
2126
+ this.#ensureUnused(req.addedId);
2127
+ const { normals, ...placement } = req.placement;
2128
+ const { mesh, ...value } = s.addHeightfield(req.field, {
2129
+ placement,
2130
+ normals
2131
+ });
2132
+ this.#bindAdded(req.addedId, mesh);
2133
+ b.replies.push({
2134
+ qid: req.qid,
2135
+ ok: true,
2136
+ value
2137
+ });
2138
+ return;
2139
+ }
2140
+ case "convexHull":
2141
+ case "convexHullPoints": {
2142
+ this.#ensureUnused(req.addedId);
2143
+ const options = { normals: req.normals };
2144
+ const r = req.kind === "convexHull" ? s.convexHull(this.#meshList(req.meshes), options) : s.convexHullOfPoints(req.points, options);
2145
+ this.#bindAdded(req.addedId, r.mesh);
2146
+ const value = {
2147
+ inputPoints: r.inputPoints,
2148
+ vertices: r.vertices,
2149
+ triangles: r.triangles
2150
+ };
2151
+ b.replies.push({
2152
+ qid: req.qid,
2153
+ ok: true,
2154
+ value
2155
+ });
2156
+ return;
2157
+ }
2158
+ case "denoise": {
2159
+ const value = s.denoise(this.#mesh(req.id), req.options);
2160
+ b.replies.push({
2161
+ qid: req.qid,
2162
+ ok: true,
2163
+ value
2164
+ });
2165
+ return;
2166
+ }
2167
+ case "delaunayFlips": {
2168
+ const value = s.delaunayFlips(this.#mesh(req.id), req.options);
2169
+ b.replies.push({
2170
+ qid: req.qid,
2171
+ ok: true,
2172
+ value
2173
+ });
2174
+ return;
2175
+ }
2176
+ case "wrap": {
2177
+ this.#ensureUnused(req.addedId);
2178
+ const { mesh, ...value } = s.wrap(this.#meshList(req.meshes), req.options);
2179
+ this.#bindAdded(req.addedId, mesh);
2180
+ b.replies.push({
2181
+ qid: req.qid,
2182
+ ok: true,
2183
+ value
2184
+ });
2185
+ return;
2186
+ }
2187
+ case "hollowMesh": {
2188
+ const value = s.hollowMesh(this.#mesh(req.id), req.options);
2189
+ b.replies.push({
2190
+ qid: req.qid,
2191
+ ok: true,
2192
+ value
2193
+ });
2194
+ return;
2195
+ }
2196
+ case "latticeFill": {
2197
+ const value = s.latticeFill(this.#mesh(req.id), req.options);
2198
+ b.replies.push({
2199
+ qid: req.qid,
2200
+ ok: true,
2201
+ value
2202
+ });
2203
+ return;
2204
+ }
2205
+ case "punchDrainHole": {
2206
+ if (req.plugId !== void 0) this.#ensureUnused(req.plugId);
2207
+ const r = s.punchDrainHole(this.#mesh(req.id), req.options);
2208
+ const plugIds = [];
2209
+ if (r.plugs.length > 0) {
2210
+ if (r.plugs.length > 1 || req.plugId === void 0) {
2211
+ for (const h of r.plugs) s.removeMesh(h);
2212
+ throw invalid("punchDrainHole produced unexpected plug count");
2213
+ }
2214
+ this.#bindAdded(req.plugId, r.plugs[0]);
2215
+ plugIds.push(req.plugId);
2216
+ }
2217
+ const value = {
2218
+ holesPunched: r.holesPunched,
2219
+ keptTriangles: r.keptTriangles,
2220
+ usedFallback: r.usedFallback,
2221
+ plugs: plugIds,
2222
+ methodUsed: r.methodUsed,
2223
+ uvsDropped: r.uvsDropped
2224
+ };
2225
+ b.replies.push({
2226
+ qid: req.qid,
2227
+ ok: true,
2228
+ value
2229
+ });
2230
+ return;
2231
+ }
2232
+ case "placeMesh": {
2233
+ if (req.addedId !== void 0) this.#ensureUnused(req.addedId);
2234
+ const r = s.placeMesh(this.#mesh(req.target), this.#mesh(req.stamp), req.options);
2235
+ let addedId = null;
2236
+ if (r.added !== null) {
2237
+ if (req.addedId === void 0) {
2238
+ s.removeMesh(r.added);
2239
+ throw invalid("placeMesh produced an unexpected detached mesh");
2240
+ }
2241
+ this.#bindAdded(req.addedId, r.added);
2242
+ addedId = req.addedId;
2243
+ }
2244
+ const value = {
2245
+ transform: r.transform,
2246
+ keptTriangles: r.keptTriangles,
2247
+ usedFallback: r.usedFallback,
2248
+ conformMaxDisplacement: r.conformMaxDisplacement,
2249
+ added: addedId,
2250
+ uvsDropped: r.uvsDropped
2251
+ };
2252
+ b.replies.push({
2253
+ qid: req.qid,
2254
+ ok: true,
2255
+ value
2256
+ });
2257
+ return;
2258
+ }
1598
2259
  case "fieldInteraction":
1599
2260
  s.setFieldInteraction(req.points);
1600
2261
  return;
2262
+ case "cancel": return;
1601
2263
  case "compact":
1602
2264
  s.compact(this.#mesh(req.id));
1603
2265
  return;
@@ -1631,9 +2293,15 @@ var Engine = class {
1631
2293
  }
1632
2294
  case "undo":
1633
2295
  s.undo();
2296
+ this.#stepDepth = 0;
2297
+ if (this.#drag !== void 0 && !this.#drag.session.active) this.#drag = void 0;
2298
+ this.#syncPresence();
1634
2299
  return;
1635
2300
  case "redo":
1636
2301
  s.redo();
2302
+ this.#stepDepth = 0;
2303
+ if (this.#drag !== void 0 && !this.#drag.session.active) this.#drag = void 0;
2304
+ this.#syncPresence();
1637
2305
  return;
1638
2306
  case "clearHistory":
1639
2307
  if (this.#drag !== void 0) {
@@ -1641,7 +2309,54 @@ var Engine = class {
1641
2309
  this.#drag = void 0;
1642
2310
  }
1643
2311
  s.clearHistory();
2312
+ this.#stepDepth = 0;
1644
2313
  return;
2314
+ case "historyBudget":
2315
+ s.setHistoryBudget(req.bytes);
2316
+ return;
2317
+ case "entry": {
2318
+ const { id, bytes, coalesce } = req;
2319
+ const note = (what) => () => {
2320
+ this.#entryEvents.push({
2321
+ id,
2322
+ what,
2323
+ rev: this.#rev
2324
+ });
2325
+ };
2326
+ const entry = {
2327
+ undo: note("undo"),
2328
+ redo: note("redo"),
2329
+ drop: note("drop"),
2330
+ bytes,
2331
+ coalesce
2332
+ };
2333
+ try {
2334
+ s.recordEntry(entry);
2335
+ } catch (e) {
2336
+ entry.drop();
2337
+ throw e;
2338
+ }
2339
+ return;
2340
+ }
2341
+ case "step":
2342
+ switch (req.phase) {
2343
+ case "begin":
2344
+ s.beginStep();
2345
+ this.#stepDepth++;
2346
+ return;
2347
+ case "end":
2348
+ s.endStep();
2349
+ this.#stepDepth = Math.max(0, this.#stepDepth - 1);
2350
+ if (this.#drag !== void 0 && !this.#drag.session.active) this.#drag = void 0;
2351
+ return;
2352
+ case "cancel":
2353
+ s.cancelStep();
2354
+ this.#stepDepth = 0;
2355
+ if (this.#drag !== void 0 && !this.#drag.session.active) this.#drag = void 0;
2356
+ this.#syncPresence();
2357
+ return;
2358
+ }
2359
+ throw invalid(`unknown step phase ${req.phase}`);
1645
2360
  case "coloring":
1646
2361
  s.setColoring(this.#mesh(req.id), req.rule === null ? null : this.#rule(req.rule));
1647
2362
  b.colorings.push({
@@ -1667,7 +2382,35 @@ var Engine = class {
1667
2382
  ok: true,
1668
2383
  value
1669
2384
  });
1670
- if (req.name === "verticesInSphere" && value instanceof Uint32Array) b.transfer.push(value.buffer);
2385
+ const pushOwned = (view) => {
2386
+ if (ArrayBuffer.isView(view) && view.byteOffset === 0 && view.byteLength === view.buffer.byteLength && view.buffer instanceof ArrayBuffer) b.transfer.push(view.buffer);
2387
+ };
2388
+ if (req.name === "verticesInSphere" || req.name === "selfIntersectingTriangles" || req.name === "selectFacesScreen" || req.name === "selectFacesInSphere" || req.name === "selectByCreaseAngle" || req.name === "selectConnectedShell" || req.name === "selectSmallFaceIslands" || req.name === "growFaceSelection" || req.name === "shrinkFaceSelection" || req.name === "fillSelectionHoles" || req.name === "smoothSelectionBoundary" || req.name === "faceSelectionToVertexMask" || req.name === "vertexMaskToFaceSelection") pushOwned(value);
2389
+ else if (req.name === "latticePreview") {
2390
+ const preview = value;
2391
+ pushOwned(preview.positions);
2392
+ pushOwned(preview.indices);
2393
+ } else if (req.name === "buildFfdLattice") pushOwned(value.offsets);
2394
+ else if (req.name === "fitPrimitive") pushOwned(value.residuals);
2395
+ else if (req.name === "sliceContours") for (const loop of value.loops) {
2396
+ pushOwned(loop.points2d);
2397
+ pushOwned(loop.points3d);
2398
+ pushOwned(loop.points3d64);
2399
+ }
2400
+ else if (req.name === "sliceContourStack") for (const slice of value) for (const loop of slice.loops) {
2401
+ pushOwned(loop.points2d);
2402
+ pushOwned(loop.points3d);
2403
+ pushOwned(loop.points3d64);
2404
+ }
2405
+ else if (req.name === "overhangs") pushOwned(value.vertexAnglesDeg);
2406
+ else if (req.name === "triangleQuality") pushOwned(value.vertexQuality);
2407
+ else if (req.name === "meshDeviation") pushOwned(value.vertexDeviations);
2408
+ else if (req.name === "heightMap") pushOwned(value.heights);
2409
+ else if (req.name === "generatePrimitive" || req.name === "generateHeightfield") {
2410
+ const mesh = value;
2411
+ pushOwned(mesh.positions);
2412
+ pushOwned(mesh.indices);
2413
+ }
1671
2414
  return;
1672
2415
  }
1673
2416
  case "fetchFieldValues": {
@@ -1694,10 +2437,13 @@ var Engine = class {
1694
2437
  };
1695
2438
  };
1696
2439
  if (req.phase === "begin") {
1697
- const session = this.#scene.drag(this.#mesh(req.id));
2440
+ const handle = this.#mesh(req.id);
2441
+ const session = this.#scene.drag(handle);
2442
+ const collisionActive = this.#scene.hasActiveCollisionResponse(handle);
1698
2443
  this.#drag = {
1699
2444
  id: req.id,
1700
- session
2445
+ session,
2446
+ collisionActive
1701
2447
  };
1702
2448
  return;
1703
2449
  }
@@ -1707,18 +2453,17 @@ var Engine = class {
1707
2453
  switch (req.phase) {
1708
2454
  case "move":
1709
2455
  drag.session.move(pose());
2456
+ if (drag.collisionActive) this.#forcePlacements.add(req.id);
1710
2457
  return;
1711
2458
  case "end":
1712
2459
  this.#drag = void 0;
1713
- if (req.rotation !== void 0) {
1714
- try {
1715
- drag.session.move(pose());
1716
- } finally {
1717
- drag.session.end();
1718
- }
1719
- return;
2460
+ if (req.rotation !== void 0) try {
2461
+ drag.session.move(pose());
2462
+ } finally {
2463
+ drag.session.end();
1720
2464
  }
1721
- drag.session.end();
2465
+ else drag.session.end();
2466
+ if (drag.collisionActive) this.#forcePlacements.add(req.id);
1722
2467
  return;
1723
2468
  case "cancel":
1724
2469
  this.#drag = void 0;
@@ -1817,6 +2562,23 @@ var Engine = class {
1817
2562
  const a = args;
1818
2563
  return s.intersects(this.#mesh(a.a), this.#mesh(a.b));
1819
2564
  }
2565
+ case "selfIntersectingTriangles": {
2566
+ const a = args;
2567
+ return s.selfIntersectingTriangles(this.#mesh(a.mesh));
2568
+ }
2569
+ case "wrapPlan": {
2570
+ const a = args;
2571
+ return s.wrapPlan(this.#meshList(a.meshes), a.options);
2572
+ }
2573
+ case "removalBytes": return s.removalBytes(this.#mesh(args.mesh));
2574
+ case "latticePlan": {
2575
+ const a = args;
2576
+ return s.latticePlan(this.#mesh(a.mesh), a.options);
2577
+ }
2578
+ case "latticePreview": {
2579
+ const a = args;
2580
+ return s.latticePreview(this.#mesh(a.mesh), a.options);
2581
+ }
1820
2582
  case "findContacts": {
1821
2583
  const a = args;
1822
2584
  const pairs = a.pairs?.map(([x, y]) => [this.#mesh(x), this.#mesh(y)]);
@@ -1857,6 +2619,96 @@ var Engine = class {
1857
2619
  const a = args;
1858
2620
  return s.sectionGap(this.#section(a.section), this.#mesh(a.a), this.#mesh(a.b));
1859
2621
  }
2622
+ case "overhangs": {
2623
+ const a = args;
2624
+ return s.overhangs(this.#mesh(a.mesh), a.options);
2625
+ }
2626
+ case "triangleQuality": {
2627
+ const a = args;
2628
+ return s.triangleQuality(this.#mesh(a.mesh), a.metric, a.sliverThreshold);
2629
+ }
2630
+ case "meshDeviation": {
2631
+ const a = args;
2632
+ return s.meshDeviation(this.#mesh(a.source), this.#mesh(a.target), a.options);
2633
+ }
2634
+ case "releaseGeodesics":
2635
+ s.releaseGeodesics();
2636
+ return null;
2637
+ case "detectSymmetryPlane": {
2638
+ const a = args;
2639
+ return s.detectSymmetryPlane(this.#mesh(a.mesh));
2640
+ }
2641
+ case "selectFacesScreen": {
2642
+ const a = args;
2643
+ return s.selectFacesScreen(this.#mesh(a.mesh), a.shape, a.options);
2644
+ }
2645
+ case "selectFacesInSphere": {
2646
+ const a = args;
2647
+ return s.selectFacesInSphere(this.#mesh(a.mesh), a.center, a.radius, a.normalCone);
2648
+ }
2649
+ case "selectByCreaseAngle": {
2650
+ const a = args;
2651
+ return s.selectByCreaseAngle(this.#mesh(a.mesh), a.seedTri, a.maxDihedralRad);
2652
+ }
2653
+ case "selectConnectedShell": {
2654
+ const a = args;
2655
+ return s.selectConnectedShell(this.#mesh(a.mesh), a.seedTri);
2656
+ }
2657
+ case "selectSmallFaceIslands": {
2658
+ const a = args;
2659
+ return s.selectSmallFaceIslands(this.#mesh(a.mesh), a.maxFaceCount, a.maxArea);
2660
+ }
2661
+ case "growFaceSelection": {
2662
+ const a = args;
2663
+ return s.growFaceSelection(this.#mesh(a.mesh), a.selectedTris, a.rings, a.adjacency);
2664
+ }
2665
+ case "shrinkFaceSelection": {
2666
+ const a = args;
2667
+ return s.shrinkFaceSelection(this.#mesh(a.mesh), a.selectedTris, a.rings, a.adjacency);
2668
+ }
2669
+ case "fillSelectionHoles": {
2670
+ const a = args;
2671
+ return s.fillSelectionHoles(this.#mesh(a.mesh), a.selectedTris, a.maxHoleTris);
2672
+ }
2673
+ case "smoothSelectionBoundary": {
2674
+ const a = args;
2675
+ return s.smoothSelectionBoundary(this.#mesh(a.mesh), a.selectedTris, a.iterations);
2676
+ }
2677
+ case "faceSelectionToVertexMask": {
2678
+ const a = args;
2679
+ return s.faceSelectionToVertexMask(this.#mesh(a.mesh), a.selectedTris);
2680
+ }
2681
+ case "vertexMaskToFaceSelection": {
2682
+ const a = args;
2683
+ return s.vertexMaskToFaceSelection(this.#mesh(a.mesh), a.mask, a.threshold, a.requireAllVertices);
2684
+ }
2685
+ case "buildFfdLattice": {
2686
+ const a = args;
2687
+ return s.buildFfdLattice(this.#mesh(a.mesh), a.dims, a.basis, a.padding);
2688
+ }
2689
+ case "fitPrimitive": {
2690
+ const a = args;
2691
+ return s.fitPrimitive(this.#mesh(a.mesh), a.options);
2692
+ }
2693
+ case "sliceContours": {
2694
+ const a = args;
2695
+ return s.sliceContours(this.#mesh(a.mesh), a.origin, a.normal);
2696
+ }
2697
+ case "sliceContourStack": {
2698
+ const a = args;
2699
+ return s.sliceContourStack(this.#mesh(a.mesh), a.origin, a.normal, a.spacing, a.count);
2700
+ }
2701
+ case "heightMapFrame": {
2702
+ const a = args;
2703
+ return s.heightMapFrame(this.#meshList(a.meshes), decodeHeightMapOptions(a.options));
2704
+ }
2705
+ case "heightMap": {
2706
+ const a = args;
2707
+ const frame = a.frame === void 0 ? decodeHeightMapOptions(a.options ?? []) : decodeHeightMapFrame(a.frame);
2708
+ return s.heightMap(this.#meshList(a.meshes), frame, a.rows);
2709
+ }
2710
+ case "generateHeightfield": return generateCheckedHeightfield(args);
2711
+ case "generatePrimitive": return generateEncodedPrimitive(args);
1860
2712
  case "debugDump": {
1861
2713
  if (!this.#debug) throw invalid(`unknown query ${name}`);
1862
2714
  const h = this.#mesh(args.mesh);
@@ -1878,6 +2730,7 @@ var Engine = class {
1878
2730
  /** Commits the frame and packs everything that changed. */
1879
2731
  #result(b) {
1880
2732
  const s = this.#scene;
2733
+ this.#dropGone();
1881
2734
  let frame = s.commitFrame();
1882
2735
  if (this.#valuesFull) {
1883
2736
  this.#valuesFull = false;
@@ -1886,26 +2739,40 @@ var Engine = class {
1886
2739
  const changes = [];
1887
2740
  const placements = [];
1888
2741
  const sections = [];
2742
+ const forced = new Set(this.#forcePlacements);
2743
+ this.#forcePlacements.clear();
1889
2744
  for (const c of frame) {
1890
2745
  const id = this.#meshId(c.mesh);
1891
2746
  changes.push(this.#change(c, id, b));
1892
- if (c.removed) continue;
1893
- if (c.added || c.placementChanged) {
2747
+ if (c.removed) {
2748
+ forced.delete(id);
2749
+ continue;
2750
+ }
2751
+ const isForced = forced.delete(id);
2752
+ if (c.added || c.placementChanged || isForced) {
1894
2753
  const p = s.placement(c.mesh);
1895
2754
  placements.push({
1896
2755
  id,
1897
2756
  rotation: p.rotation,
1898
2757
  translation: p.translation,
1899
2758
  scale: p.scale,
1900
- matrix4: Array.from(s.matrix4(c.mesh))
2759
+ matrix4: Array.from(s.matrix4(c.mesh)),
2760
+ ...isForced ? { authoritative: true } : {}
1901
2761
  });
1902
2762
  }
1903
2763
  for (const sh of c.sectionsChanged) {
1904
2764
  const section = this.#sectionIds.get(sh);
1905
2765
  const result = this.#sectionResult(sh, c.mesh);
1906
2766
  if (result !== null) {
1907
- for (const contour of result.contours) b.transfer.push(contour.points.buffer);
1908
- if (result.fill !== void 0) b.transfer.push(result.fill.positions.buffer, result.fill.indices.buffer);
2767
+ for (const { points, points64 } of result.contours) {
2768
+ b.transfer.push(points.buffer);
2769
+ if (points64 !== void 0) b.transfer.push(points64.buffer);
2770
+ }
2771
+ if (result.fill !== void 0) {
2772
+ const { positions, positions64, indices } = result.fill;
2773
+ b.transfer.push(positions.buffer, indices.buffer);
2774
+ if (positions64 !== void 0) b.transfer.push(positions64.buffer);
2775
+ }
1909
2776
  }
1910
2777
  sections.push({
1911
2778
  section,
@@ -1914,15 +2781,48 @@ var Engine = class {
1914
2781
  });
1915
2782
  }
1916
2783
  }
2784
+ for (const id of forced) {
2785
+ const h = this.#meshes.get(id);
2786
+ if (h === void 0 || this.#parked.has(id)) continue;
2787
+ const p = s.placement(h);
2788
+ placements.push({
2789
+ id,
2790
+ rotation: p.rotation,
2791
+ translation: p.translation,
2792
+ scale: p.scale,
2793
+ matrix4: Array.from(s.matrix4(h)),
2794
+ authoritative: true
2795
+ });
2796
+ }
1917
2797
  for (const { section, mesh } of this.#goneEntries.splice(0)) sections.push({
1918
2798
  section,
1919
2799
  mesh,
1920
2800
  result: null
1921
2801
  });
2802
+ const presence = [...this.#moved].map((id) => ({
2803
+ id,
2804
+ state: this.#doomed.has(id) ? "gone" : this.#parked.has(id) ? "parked" : this.#meshes.has(id) ? "live" : "gone"
2805
+ }));
2806
+ this.#moved.clear();
2807
+ const fieldTargets = [...this.#fieldTargets].map(([field, to]) => ({
2808
+ field,
2809
+ to
2810
+ }));
2811
+ this.#fieldTargets.clear();
2812
+ const sectionMeshes = [...this.#sectionsLost].flatMap((section) => {
2813
+ const meshes = this.#sectionMeshes.get(section);
2814
+ return meshes === void 0 ? [] : [{
2815
+ section,
2816
+ meshes
2817
+ }];
2818
+ });
2819
+ this.#sectionsLost.clear();
1922
2820
  const history = {
1923
2821
  canUndo: s.canUndo(),
1924
2822
  canRedo: s.canRedo()
1925
2823
  };
2824
+ const entries = this.#entryEvents.splice(0);
2825
+ const permanent = this.#permanent.splice(0);
1926
2826
  this.#packed.clear();
1927
2827
  for (const h of this.#removed.splice(0)) this.#meshIds.delete(h);
1928
2828
  for (const h of this.#removedFields.splice(0)) this.#fieldIds.delete(h);
@@ -1933,6 +2833,12 @@ var Engine = class {
1933
2833
  placements,
1934
2834
  sections,
1935
2835
  history,
2836
+ historyBytes: s.historyBytes(),
2837
+ ...presence.length === 0 ? {} : { presence },
2838
+ ...fieldTargets.length === 0 ? {} : { fieldTargets },
2839
+ ...sectionMeshes.length === 0 ? {} : { sectionMeshes },
2840
+ ...entries.length === 0 ? {} : { entries },
2841
+ ...permanent.length === 0 ? {} : { permanent },
1936
2842
  status: b.status,
1937
2843
  replies: b.replies,
1938
2844
  colorings: b.colorings
@@ -1944,8 +2850,8 @@ var Engine = class {
1944
2850
  */
1945
2851
  #withValues(frame) {
1946
2852
  const out = [...frame];
1947
- for (const h of this.#meshes.values()) {
1948
- if ((this.#scene.views(h).values?.() ?? null) === null) continue;
2853
+ for (const [id, h] of this.#meshes) {
2854
+ if (this.#parked.has(id) || (this.#scene.views(h).values?.() ?? null) === null) continue;
1949
2855
  const at = out.findIndex((c) => c.mesh === h);
1950
2856
  if (at === -1) out.push({
1951
2857
  mesh: h,
@@ -1981,6 +2887,7 @@ var Engine = class {
1981
2887
  id,
1982
2888
  added: c.added,
1983
2889
  removed: c.removed,
2890
+ ...c.restorable === true && !this.#doomed.has(id) ? { restorable: true } : {},
1984
2891
  placementChanged: c.placementChanged,
1985
2892
  capacityChanged: c.capacityChanged,
1986
2893
  positions: c.positions,
@@ -2001,6 +2908,7 @@ var Engine = class {
2001
2908
  }
2002
2909
  };
2003
2910
  if (c.removed) return wire;
2911
+ if (c.added) wire.origin = this.#scene.origin(c.mesh);
2004
2912
  wire.vertexSlots = this.#scene.vertexSlots(c.mesh);
2005
2913
  wire.triangleSlots = this.#scene.triangleSlots(c.mesh);
2006
2914
  if (c.added || c.capacityChanged || c.positions === "all" || c.positions.length > 0 || c.indices === "all" || c.indices.length > 0) wire.bounds = this.#scene.bounds(c.mesh);
@@ -2019,5 +2927,29 @@ var Engine = class {
2019
2927
  }
2020
2928
  return wire;
2021
2929
  }
2930
+ #ensureUnused(id) {
2931
+ if (this.#meshes.has(id) || this.#adds.some((a) => a.id === id)) throw invalid(`mesh client id ${id} is already in use`);
2932
+ }
2933
+ #bindAdded(id, handle) {
2934
+ this.#ensureUnused(id);
2935
+ this.#meshes.set(id, handle);
2936
+ this.#meshIds.set(handle, id);
2937
+ }
2938
+ #mapCollisionReport(r) {
2939
+ return {
2940
+ appliedPlacement: r.appliedPlacement,
2941
+ appliedQuaternion: r.appliedQuaternion,
2942
+ activePairs: r.activePairs.map((p) => ({
2943
+ ...p,
2944
+ other: this.#meshId(p.other)
2945
+ })),
2946
+ modifiedMeshes: r.modifiedMeshes.map((m) => this.#meshId(m)),
2947
+ minClearance: r.minClearance,
2948
+ cutsExecuted: r.cutsExecuted,
2949
+ softVerticesDeformed: r.softVerticesDeformed,
2950
+ elasticRecoveryCount: r.elasticRecoveryCount,
2951
+ selfIntersectionGuardTriggered: r.selfIntersectionGuardTriggered
2952
+ };
2953
+ }
2022
2954
  };
2023
- export { YIELD_MS as a, loadEngine as c, STEP_POINTS as i, startEngine as l, BACKGROUND_ADD_TRIANGLES as n, runEngine as o, STALE_GUARD_MS as r, serveEngine as s, ADD_STEP_WORK as t, probeSharedGrowth as u };
2955
+ export { STEP_POINTS as a, serveEngine as c, probeSharedGrowth as d, STALE_GUARD_MS as i, loadEngine as l, BACKGROUND_ADD_TRIANGLES as n, YIELD_MS as o, GEODESIC_STEP_WORK as r, runEngine as s, ADD_STEP_WORK as t, startEngine as u };