okgeometry-api 1.2.0 → 1.2.1
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.
- package/dist/Line.d.ts +10 -1
- package/dist/Line.d.ts.map +1 -1
- package/dist/Line.js +11 -0
- package/dist/Line.js.map +1 -1
- package/dist/Mesh.d.ts +82 -9
- package/dist/Mesh.d.ts.map +1 -1
- package/dist/Mesh.js +329 -26
- package/dist/Mesh.js.map +1 -1
- package/dist/MeshSurface.d.ts +32 -0
- package/dist/MeshSurface.d.ts.map +1 -0
- package/dist/MeshSurface.js +51 -0
- package/dist/MeshSurface.js.map +1 -0
- package/dist/NurbsCurve.d.ts +24 -2
- package/dist/NurbsCurve.d.ts.map +1 -1
- package/dist/NurbsCurve.js +34 -2
- package/dist/NurbsCurve.js.map +1 -1
- package/dist/NurbsSurface.d.ts +9 -1
- package/dist/NurbsSurface.d.ts.map +1 -1
- package/dist/NurbsSurface.js +12 -3
- package/dist/NurbsSurface.js.map +1 -1
- package/dist/PolyCurve.d.ts +21 -3
- package/dist/PolyCurve.d.ts.map +1 -1
- package/dist/PolyCurve.js +82 -38
- package/dist/PolyCurve.js.map +1 -1
- package/dist/Polygon.d.ts +13 -2
- package/dist/Polygon.d.ts.map +1 -1
- package/dist/Polygon.js +21 -3
- package/dist/Polygon.js.map +1 -1
- package/dist/Polyline.d.ts +19 -2
- package/dist/Polyline.d.ts.map +1 -1
- package/dist/Polyline.js +38 -6
- package/dist/Polyline.js.map +1 -1
- package/dist/Surface.d.ts +17 -0
- package/dist/Surface.d.ts.map +1 -0
- package/dist/Surface.js +2 -0
- package/dist/Surface.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +13 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/wasm-base64.d.ts +1 -1
- package/dist/wasm-base64.d.ts.map +1 -1
- package/dist/wasm-base64.js +1 -1
- package/dist/wasm-base64.js.map +1 -1
- package/dist/wasm-bindings.d.ts +65 -2
- package/dist/wasm-bindings.d.ts.map +1 -1
- package/dist/wasm-bindings.js +100 -2
- package/dist/wasm-bindings.js.map +1 -1
- package/package.json +1 -1
- package/src/Line.ts +38 -20
- package/src/Mesh.ts +538 -184
- package/src/MeshSurface.ts +72 -0
- package/src/NurbsCurve.ts +80 -26
- package/src/NurbsSurface.ts +28 -13
- package/src/PolyCurve.ts +157 -85
- package/src/Polygon.ts +34 -4
- package/src/Polyline.ts +74 -24
- package/src/Surface.ts +18 -0
- package/src/index.ts +5 -0
- package/src/types.ts +15 -0
- package/src/wasm-base64.ts +1 -1
- package/src/wasm-bindings.d.ts +43 -2
- package/src/wasm-bindings.js +105 -2
package/dist/Mesh.js
CHANGED
|
@@ -7,6 +7,7 @@ import { Line } from "./Line.js";
|
|
|
7
7
|
import { Circle } from "./Circle.js";
|
|
8
8
|
import { Arc } from "./Arc.js";
|
|
9
9
|
import { Polygon } from "./Polygon.js";
|
|
10
|
+
import { MeshSurface } from "./MeshSurface.js";
|
|
10
11
|
import { NurbsCurve } from "./NurbsCurve.js";
|
|
11
12
|
import { PolyCurve } from "./PolyCurve.js";
|
|
12
13
|
import { pointsToCoords, parsePolylineBuffer as parsePolylineBuf } from "./BufferCodec.js";
|
|
@@ -34,6 +35,10 @@ export class Mesh {
|
|
|
34
35
|
this._indexBuffer = null;
|
|
35
36
|
this._vertices = null;
|
|
36
37
|
this._faces = null;
|
|
38
|
+
this._planarFaces = null;
|
|
39
|
+
this._planarFacesByTriangleIndex = null;
|
|
40
|
+
this._surfaces = null;
|
|
41
|
+
this._surfacesByTriangleIndex = null;
|
|
37
42
|
this._edgeVertexPairs = null;
|
|
38
43
|
this._topologyMetricsCache = null;
|
|
39
44
|
this._isClosedVolumeCache = null;
|
|
@@ -117,6 +122,55 @@ export class Mesh {
|
|
|
117
122
|
const diag = Math.hypot(dx, dy, dz);
|
|
118
123
|
return Number.isFinite(diag) && diag > 0 ? diag : 1;
|
|
119
124
|
}
|
|
125
|
+
static computeSignedVolumeFromBuffer(buffer) {
|
|
126
|
+
const vertexCount = Math.max(0, Math.floor(buffer[0] ?? 0));
|
|
127
|
+
if (vertexCount <= 0)
|
|
128
|
+
return 0;
|
|
129
|
+
const indexStart = 1 + vertexCount * 3;
|
|
130
|
+
let signedVolume = 0;
|
|
131
|
+
for (let i = indexStart; i + 2 < buffer.length; i += 3) {
|
|
132
|
+
const ia = Math.floor(buffer[i] ?? -1);
|
|
133
|
+
const ib = Math.floor(buffer[i + 1] ?? -1);
|
|
134
|
+
const ic = Math.floor(buffer[i + 2] ?? -1);
|
|
135
|
+
if (ia < 0 || ib < 0 || ic < 0 || ia >= vertexCount || ib >= vertexCount || ic >= vertexCount)
|
|
136
|
+
continue;
|
|
137
|
+
const ax = buffer[1 + ia * 3];
|
|
138
|
+
const ay = buffer[1 + ia * 3 + 1];
|
|
139
|
+
const az = buffer[1 + ia * 3 + 2];
|
|
140
|
+
const bx = buffer[1 + ib * 3];
|
|
141
|
+
const by = buffer[1 + ib * 3 + 1];
|
|
142
|
+
const bz = buffer[1 + ib * 3 + 2];
|
|
143
|
+
const cx = buffer[1 + ic * 3];
|
|
144
|
+
const cy = buffer[1 + ic * 3 + 1];
|
|
145
|
+
const cz = buffer[1 + ic * 3 + 2];
|
|
146
|
+
signedVolume += ax * (by * cz - bz * cy)
|
|
147
|
+
- ay * (bx * cz - bz * cx)
|
|
148
|
+
+ az * (bx * cy - by * cx);
|
|
149
|
+
}
|
|
150
|
+
return signedVolume / 6;
|
|
151
|
+
}
|
|
152
|
+
static reverseTriangleWinding(buffer) {
|
|
153
|
+
const flipped = new Float64Array(buffer);
|
|
154
|
+
const vertexCount = Math.max(0, Math.floor(flipped[0] ?? 0));
|
|
155
|
+
const indexStart = 1 + vertexCount * 3;
|
|
156
|
+
for (let i = indexStart; i + 2 < flipped.length; i += 3) {
|
|
157
|
+
const tmp = flipped[i + 1];
|
|
158
|
+
flipped[i + 1] = flipped[i + 2];
|
|
159
|
+
flipped[i + 2] = tmp;
|
|
160
|
+
}
|
|
161
|
+
return flipped;
|
|
162
|
+
}
|
|
163
|
+
static normalizeClosedVolumeOrientation(mesh) {
|
|
164
|
+
if (mesh.faceCount === 0 || mesh._vertexCount <= 0)
|
|
165
|
+
return mesh;
|
|
166
|
+
const topology = mesh.topologyMetrics();
|
|
167
|
+
if (topology.boundaryEdges !== 0 || topology.nonManifoldEdges !== 0)
|
|
168
|
+
return mesh;
|
|
169
|
+
const signedVolume = Mesh.computeSignedVolumeFromBuffer(mesh._buffer);
|
|
170
|
+
if (!Number.isFinite(signedVolume) || signedVolume >= 0)
|
|
171
|
+
return mesh;
|
|
172
|
+
return new Mesh(Mesh.reverseTriangleWinding(mesh._buffer), mesh._trustedBooleanInput);
|
|
173
|
+
}
|
|
120
174
|
static booleanContactTolerance(a, b) {
|
|
121
175
|
const scale = Math.max(Mesh.boundsDiag(a), Mesh.boundsDiag(b));
|
|
122
176
|
return Math.min(1e-4, Math.max(1e-9, scale * 1e-6));
|
|
@@ -532,7 +586,7 @@ export class Mesh {
|
|
|
532
586
|
get vertexCount() {
|
|
533
587
|
return this._vertexCount;
|
|
534
588
|
}
|
|
535
|
-
/** Number of triangular faces in this mesh */
|
|
589
|
+
/** Number of triangular faces in this mesh. Use `planarFaceCount` for logical planar faces. */
|
|
536
590
|
get faceCount() {
|
|
537
591
|
const start = 1 + this._vertexCount * 3;
|
|
538
592
|
if (this._buffer.length <= start)
|
|
@@ -558,6 +612,7 @@ export class Mesh {
|
|
|
558
612
|
/**
|
|
559
613
|
* Get all faces as arrays of vertex indices.
|
|
560
614
|
* Each face is [i0, i1, i2] for the three triangle vertices.
|
|
615
|
+
* For coplanar edge-connected logical faces, use `planarFaces`.
|
|
561
616
|
* Lazy-computed and cached.
|
|
562
617
|
*/
|
|
563
618
|
get faces() {
|
|
@@ -571,6 +626,42 @@ export class Mesh {
|
|
|
571
626
|
}
|
|
572
627
|
return this._faces;
|
|
573
628
|
}
|
|
629
|
+
/**
|
|
630
|
+
* Alias for triangle faces, mirroring `faces`.
|
|
631
|
+
* Use `planarFaces` when you want logical mesh faces such as the 6 faces of a box.
|
|
632
|
+
*/
|
|
633
|
+
get triangles() {
|
|
634
|
+
return this.faces;
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Number of coplanar edge-connected planar faces in this mesh.
|
|
638
|
+
* Examples: a box has 6, an L-shaped extrusion has 8.
|
|
639
|
+
*/
|
|
640
|
+
get planarFaceCount() {
|
|
641
|
+
return this.planarFaces.length;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Logical mesh faces built by grouping coplanar edge-connected triangles.
|
|
645
|
+
* Cached after the first query to keep repeated face access cheap.
|
|
646
|
+
*/
|
|
647
|
+
get planarFaces() {
|
|
648
|
+
this.ensurePlanarFaceCache();
|
|
649
|
+
return this._planarFaces ?? [];
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Number of logical planar surfaces in this mesh.
|
|
653
|
+
* Mirrors `planarFaceCount`, but returns wrapper objects with surface queries.
|
|
654
|
+
*/
|
|
655
|
+
get surfaceCount() {
|
|
656
|
+
return this.surfaces.length;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Logical planar mesh surfaces with `evaluate(u, v)` and `normal(u, v)` methods.
|
|
660
|
+
*/
|
|
661
|
+
get surfaces() {
|
|
662
|
+
this.ensureSurfaceCache();
|
|
663
|
+
return this._surfaces ?? [];
|
|
664
|
+
}
|
|
574
665
|
/** Raw WASM buffer (for advanced use / re-passing to WASM) */
|
|
575
666
|
get rawBuffer() {
|
|
576
667
|
return this._buffer;
|
|
@@ -585,11 +676,111 @@ export class Mesh {
|
|
|
585
676
|
return new Mesh(buffer, options?.trustedBooleanInput ?? false);
|
|
586
677
|
}
|
|
587
678
|
static fromTrustedBuffer(buffer) {
|
|
588
|
-
return new Mesh(buffer, true);
|
|
679
|
+
return Mesh.normalizeClosedVolumeOrientation(new Mesh(buffer, true));
|
|
589
680
|
}
|
|
590
681
|
get trustedBooleanInput() {
|
|
591
682
|
return this._trustedBooleanInput;
|
|
592
683
|
}
|
|
684
|
+
getTriangleVertexIndices(faceIndex) {
|
|
685
|
+
if (!Number.isFinite(faceIndex) || faceIndex < 0 || faceIndex >= this.faceCount)
|
|
686
|
+
return null;
|
|
687
|
+
const indexBuffer = this.indexBuffer;
|
|
688
|
+
const offset = Math.floor(faceIndex) * 3;
|
|
689
|
+
if (offset + 2 >= indexBuffer.length)
|
|
690
|
+
return null;
|
|
691
|
+
return [indexBuffer[offset], indexBuffer[offset + 1], indexBuffer[offset + 2]];
|
|
692
|
+
}
|
|
693
|
+
computeTriangleAreaByIndex(faceIndex) {
|
|
694
|
+
const tri = this.getTriangleVertexIndices(faceIndex);
|
|
695
|
+
if (!tri)
|
|
696
|
+
return 0;
|
|
697
|
+
const [i0, i1, i2] = tri;
|
|
698
|
+
const base = 1;
|
|
699
|
+
const off0 = base + i0 * 3;
|
|
700
|
+
const off1 = base + i1 * 3;
|
|
701
|
+
const off2 = base + i2 * 3;
|
|
702
|
+
const ax = this._buffer[off0] ?? 0;
|
|
703
|
+
const ay = this._buffer[off0 + 1] ?? 0;
|
|
704
|
+
const az = this._buffer[off0 + 2] ?? 0;
|
|
705
|
+
const bx = this._buffer[off1] ?? 0;
|
|
706
|
+
const by = this._buffer[off1 + 1] ?? 0;
|
|
707
|
+
const bz = this._buffer[off1 + 2] ?? 0;
|
|
708
|
+
const cx = this._buffer[off2] ?? 0;
|
|
709
|
+
const cy = this._buffer[off2 + 1] ?? 0;
|
|
710
|
+
const cz = this._buffer[off2 + 2] ?? 0;
|
|
711
|
+
const abx = bx - ax;
|
|
712
|
+
const aby = by - ay;
|
|
713
|
+
const abz = bz - az;
|
|
714
|
+
const acx = cx - ax;
|
|
715
|
+
const acy = cy - ay;
|
|
716
|
+
const acz = cz - az;
|
|
717
|
+
const crossX = aby * acz - abz * acy;
|
|
718
|
+
const crossY = abz * acx - abx * acz;
|
|
719
|
+
const crossZ = abx * acy - aby * acx;
|
|
720
|
+
return 0.5 * Math.sqrt(crossX * crossX + crossY * crossY + crossZ * crossZ);
|
|
721
|
+
}
|
|
722
|
+
ensurePlanarFaceCache() {
|
|
723
|
+
if (this._planarFaces && this._planarFacesByTriangleIndex)
|
|
724
|
+
return;
|
|
725
|
+
ensureInit();
|
|
726
|
+
const raw = wasm.mesh_build_coplanar_connected_face_groups(this._vertexCount, this._buffer);
|
|
727
|
+
const declaredGroupCount = Math.max(0, Math.floor(raw[0] ?? 0));
|
|
728
|
+
const planarFaces = [];
|
|
729
|
+
const planarFacesByTriangleIndex = new Array(this.faceCount).fill(null);
|
|
730
|
+
let offset = 1;
|
|
731
|
+
for (let groupIndex = 0; groupIndex < declaredGroupCount && offset < raw.length; groupIndex++) {
|
|
732
|
+
const declaredTriangleCount = Math.max(0, Math.floor(raw[offset] ?? 0));
|
|
733
|
+
offset += 1;
|
|
734
|
+
const triangleIndices = [];
|
|
735
|
+
let area = 0;
|
|
736
|
+
for (let i = 0; i < declaredTriangleCount && offset < raw.length; i++, offset++) {
|
|
737
|
+
const triangleIndex = Math.floor(raw[offset] ?? -1);
|
|
738
|
+
if (triangleIndex < 0 || triangleIndex >= this.faceCount)
|
|
739
|
+
continue;
|
|
740
|
+
triangleIndices.push(triangleIndex);
|
|
741
|
+
area += this.computeTriangleAreaByIndex(triangleIndex);
|
|
742
|
+
}
|
|
743
|
+
if (offset + 5 >= raw.length)
|
|
744
|
+
break;
|
|
745
|
+
const centroid = new Point(raw[offset] ?? 0, raw[offset + 1] ?? 0, raw[offset + 2] ?? 0);
|
|
746
|
+
const normal = new Vec3(raw[offset + 3] ?? 0, raw[offset + 4] ?? 0, raw[offset + 5] ?? 0).normalize();
|
|
747
|
+
offset += 6;
|
|
748
|
+
if (triangleIndices.length === 0)
|
|
749
|
+
continue;
|
|
750
|
+
const planarFace = {
|
|
751
|
+
index: planarFaces.length,
|
|
752
|
+
seedTriangleIndex: triangleIndices[0],
|
|
753
|
+
triangleIndices,
|
|
754
|
+
triangleCount: triangleIndices.length,
|
|
755
|
+
centroid,
|
|
756
|
+
normal,
|
|
757
|
+
area,
|
|
758
|
+
};
|
|
759
|
+
planarFaces.push(planarFace);
|
|
760
|
+
for (const triangleIndex of triangleIndices) {
|
|
761
|
+
planarFacesByTriangleIndex[triangleIndex] = planarFace;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
this._planarFaces = planarFaces;
|
|
765
|
+
this._planarFacesByTriangleIndex = planarFacesByTriangleIndex;
|
|
766
|
+
this._surfaces = null;
|
|
767
|
+
this._surfacesByTriangleIndex = null;
|
|
768
|
+
}
|
|
769
|
+
ensureSurfaceCache() {
|
|
770
|
+
if (this._surfaces && this._surfacesByTriangleIndex)
|
|
771
|
+
return;
|
|
772
|
+
const surfaces = this.planarFaces.map(face => new MeshSurface(this, face));
|
|
773
|
+
const surfacesByTriangleIndex = new Array(this.faceCount).fill(null);
|
|
774
|
+
for (const surface of surfaces) {
|
|
775
|
+
for (const triangleIndex of surface.triangleIndices) {
|
|
776
|
+
if (triangleIndex >= 0 && triangleIndex < surfacesByTriangleIndex.length) {
|
|
777
|
+
surfacesByTriangleIndex[triangleIndex] = surface;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
this._surfaces = surfaces;
|
|
782
|
+
this._surfacesByTriangleIndex = surfacesByTriangleIndex;
|
|
783
|
+
}
|
|
593
784
|
debugSummary() {
|
|
594
785
|
const bounds = Mesh.toDebugBounds(this.getBounds());
|
|
595
786
|
const topology = this.topologyMetrics();
|
|
@@ -1298,6 +1489,34 @@ export class Mesh {
|
|
|
1298
1489
|
}
|
|
1299
1490
|
return Mesh.fromTrustedBuffer(result);
|
|
1300
1491
|
}
|
|
1492
|
+
static mergeMeshComponents(meshes) {
|
|
1493
|
+
if (meshes.length === 0)
|
|
1494
|
+
return Mesh.emptyMesh();
|
|
1495
|
+
if (meshes.length === 1)
|
|
1496
|
+
return Mesh.cloneMesh(meshes[0]);
|
|
1497
|
+
return Mesh.mergeMeshes(meshes);
|
|
1498
|
+
}
|
|
1499
|
+
static createUnsupportedOpenMeshBooleanError(operation, aClosed, bClosed) {
|
|
1500
|
+
const topology = `A=${aClosed ? "closed" : "open"}, B=${bClosed ? "closed" : "open"}`;
|
|
1501
|
+
if (operation === "union") {
|
|
1502
|
+
return new Error(`Boolean union requires both inputs to be closed volumes (${topology}). `
|
|
1503
|
+
+ "Close or cap the open mesh before calling union().");
|
|
1504
|
+
}
|
|
1505
|
+
return new Error(`Boolean ${operation} with open meshes is only supported when A is open and B is closed `
|
|
1506
|
+
+ `(surface trim mode). Got ${topology}. Use Mesh.split(...) and choose outside/inside explicitly `
|
|
1507
|
+
+ "for other open-mesh combinations.");
|
|
1508
|
+
}
|
|
1509
|
+
resolveOpenMeshBooleanFallback(other, operation, options) {
|
|
1510
|
+
const aClosed = this.isClosedVolume();
|
|
1511
|
+
const bClosed = other.isClosedVolume();
|
|
1512
|
+
if (aClosed && bClosed)
|
|
1513
|
+
return null;
|
|
1514
|
+
if (!aClosed && bClosed && (operation === "subtraction" || operation === "intersection")) {
|
|
1515
|
+
const split = this.splitWithMesh(other, options);
|
|
1516
|
+
return Mesh.mergeMeshComponents(operation === "subtraction" ? split.outside : split.inside);
|
|
1517
|
+
}
|
|
1518
|
+
throw Mesh.createUnsupportedOpenMeshBooleanError(operation, aClosed, bClosed);
|
|
1519
|
+
}
|
|
1301
1520
|
static encodeBooleanOperationToken(operation, a, b, options) {
|
|
1302
1521
|
const tokens = [operation];
|
|
1303
1522
|
if (a._trustedBooleanInput && b._trustedBooleanInput) {
|
|
@@ -1321,36 +1540,54 @@ export class Mesh {
|
|
|
1321
1540
|
// ── Booleans ───────────────────────────────────────────────────
|
|
1322
1541
|
/**
|
|
1323
1542
|
* Compute boolean union with another mesh.
|
|
1543
|
+
* Requires both inputs to be closed volumes.
|
|
1324
1544
|
* @param other - Mesh to union with
|
|
1325
1545
|
* @param options - Optional safety overrides
|
|
1326
1546
|
* @returns New mesh containing volume of both inputs
|
|
1327
1547
|
*/
|
|
1328
1548
|
union(other, options) {
|
|
1329
1549
|
ensureInit();
|
|
1330
|
-
const
|
|
1331
|
-
|
|
1550
|
+
const lhs = Mesh.normalizeClosedVolumeOrientation(this);
|
|
1551
|
+
const rhs = Mesh.normalizeClosedVolumeOrientation(other);
|
|
1552
|
+
lhs.resolveOpenMeshBooleanFallback(rhs, "union", options);
|
|
1553
|
+
const operationToken = Mesh.encodeBooleanOperationToken("union", lhs, rhs, options);
|
|
1554
|
+
return lhs.runBoolean(rhs, "union", () => wasm.mesh_boolean_operation(lhs._vertexCount, lhs._buffer, rhs._vertexCount, rhs._buffer, operationToken), options);
|
|
1332
1555
|
}
|
|
1333
1556
|
/**
|
|
1334
1557
|
* Compute boolean subtraction with another mesh.
|
|
1558
|
+
* If this mesh is open and `other` is a closed cutter, this trims the open
|
|
1559
|
+
* host via `split()` and returns the outside surface pieces merged together.
|
|
1335
1560
|
* @param other - Mesh to subtract
|
|
1336
1561
|
* @param options - Optional safety overrides
|
|
1337
1562
|
* @returns New mesh with other's volume removed from this
|
|
1338
1563
|
*/
|
|
1339
1564
|
subtract(other, options) {
|
|
1340
1565
|
ensureInit();
|
|
1341
|
-
const
|
|
1342
|
-
|
|
1566
|
+
const lhs = Mesh.normalizeClosedVolumeOrientation(this);
|
|
1567
|
+
const rhs = Mesh.normalizeClosedVolumeOrientation(other);
|
|
1568
|
+
const fallback = lhs.resolveOpenMeshBooleanFallback(rhs, "subtraction", options);
|
|
1569
|
+
if (fallback)
|
|
1570
|
+
return fallback;
|
|
1571
|
+
const operationToken = Mesh.encodeBooleanOperationToken("subtraction", lhs, rhs, options);
|
|
1572
|
+
return lhs.runBoolean(rhs, "subtraction", () => wasm.mesh_boolean_operation(lhs._vertexCount, lhs._buffer, rhs._vertexCount, rhs._buffer, operationToken), options);
|
|
1343
1573
|
}
|
|
1344
1574
|
/**
|
|
1345
1575
|
* Compute boolean intersection with another mesh.
|
|
1576
|
+
* If this mesh is open and `other` is a closed cutter, this trims the open
|
|
1577
|
+
* host via `split()` and returns the inside surface pieces merged together.
|
|
1346
1578
|
* @param other - Mesh to intersect with
|
|
1347
1579
|
* @param options - Optional safety overrides
|
|
1348
1580
|
* @returns New mesh containing only the overlapping volume
|
|
1349
1581
|
*/
|
|
1350
1582
|
intersect(other, options) {
|
|
1351
1583
|
ensureInit();
|
|
1352
|
-
const
|
|
1353
|
-
|
|
1584
|
+
const lhs = Mesh.normalizeClosedVolumeOrientation(this);
|
|
1585
|
+
const rhs = Mesh.normalizeClosedVolumeOrientation(other);
|
|
1586
|
+
const fallback = lhs.resolveOpenMeshBooleanFallback(rhs, "intersection", options);
|
|
1587
|
+
if (fallback)
|
|
1588
|
+
return fallback;
|
|
1589
|
+
const operationToken = Mesh.encodeBooleanOperationToken("intersection", lhs, rhs, options);
|
|
1590
|
+
return lhs.runBoolean(rhs, "intersection", () => wasm.mesh_boolean_operation(lhs._vertexCount, lhs._buffer, rhs._vertexCount, rhs._buffer, operationToken), options);
|
|
1354
1591
|
}
|
|
1355
1592
|
splitWithMesh(other, options) {
|
|
1356
1593
|
ensureInit();
|
|
@@ -1841,6 +2078,88 @@ export class Mesh {
|
|
|
1841
2078
|
nonManifoldEdges,
|
|
1842
2079
|
};
|
|
1843
2080
|
}
|
|
2081
|
+
/**
|
|
2082
|
+
* Get a logical planar face by its planar-face index.
|
|
2083
|
+
*/
|
|
2084
|
+
getPlanarFace(index) {
|
|
2085
|
+
if (!Number.isFinite(index) || index < 0)
|
|
2086
|
+
return null;
|
|
2087
|
+
const planarFaces = this.planarFaces;
|
|
2088
|
+
return planarFaces[Math.floor(index)] ?? null;
|
|
2089
|
+
}
|
|
2090
|
+
/**
|
|
2091
|
+
* Get a logical planar surface wrapper by its planar-face index.
|
|
2092
|
+
*/
|
|
2093
|
+
getSurface(index) {
|
|
2094
|
+
if (!Number.isFinite(index) || index < 0)
|
|
2095
|
+
return null;
|
|
2096
|
+
const surfaces = this.surfaces;
|
|
2097
|
+
return surfaces[Math.floor(index)] ?? null;
|
|
2098
|
+
}
|
|
2099
|
+
/**
|
|
2100
|
+
* Get the logical planar face that contains a triangle face index.
|
|
2101
|
+
*/
|
|
2102
|
+
getPlanarFaceByTriangleIndex(triangleIndex) {
|
|
2103
|
+
if (!Number.isFinite(triangleIndex) || triangleIndex < 0)
|
|
2104
|
+
return null;
|
|
2105
|
+
this.ensurePlanarFaceCache();
|
|
2106
|
+
return this._planarFacesByTriangleIndex?.[Math.floor(triangleIndex)] ?? null;
|
|
2107
|
+
}
|
|
2108
|
+
/**
|
|
2109
|
+
* Get the logical planar surface wrapper that contains a triangle face index.
|
|
2110
|
+
*/
|
|
2111
|
+
getSurfaceByTriangleIndex(triangleIndex) {
|
|
2112
|
+
if (!Number.isFinite(triangleIndex) || triangleIndex < 0)
|
|
2113
|
+
return null;
|
|
2114
|
+
this.ensureSurfaceCache();
|
|
2115
|
+
return this._surfacesByTriangleIndex?.[Math.floor(triangleIndex)] ?? null;
|
|
2116
|
+
}
|
|
2117
|
+
/**
|
|
2118
|
+
* Find the best matching logical planar face by normal and optional centroid proximity.
|
|
2119
|
+
*/
|
|
2120
|
+
findPlanarFaceByNormal(targetNormal, nearPoint) {
|
|
2121
|
+
const planarFaces = this.planarFaces;
|
|
2122
|
+
if (planarFaces.length === 0)
|
|
2123
|
+
return null;
|
|
2124
|
+
const normalizedTarget = targetNormal.normalize();
|
|
2125
|
+
if (normalizedTarget.length() < 1e-12)
|
|
2126
|
+
return null;
|
|
2127
|
+
const alignmentEps = 1e-9;
|
|
2128
|
+
const distanceEps = 1e-9;
|
|
2129
|
+
let best = null;
|
|
2130
|
+
let bestAlignment = -Infinity;
|
|
2131
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
2132
|
+
let bestArea = -Infinity;
|
|
2133
|
+
for (const planarFace of planarFaces) {
|
|
2134
|
+
const alignment = planarFace.normal.dot(normalizedTarget);
|
|
2135
|
+
const distance = nearPoint ? planarFace.centroid.distanceTo(nearPoint) : 0;
|
|
2136
|
+
const betterAlignment = alignment > bestAlignment + alignmentEps;
|
|
2137
|
+
const similarAlignment = Math.abs(alignment - bestAlignment) <= alignmentEps;
|
|
2138
|
+
const betterDistance = nearPoint !== undefined && distance < bestDistance - distanceEps;
|
|
2139
|
+
const similarDistance = Math.abs(distance - bestDistance) <= distanceEps;
|
|
2140
|
+
const betterArea = planarFace.area > bestArea + distanceEps;
|
|
2141
|
+
if (best === null
|
|
2142
|
+
|| betterAlignment
|
|
2143
|
+
|| (similarAlignment && betterDistance)
|
|
2144
|
+
|| (similarAlignment && (!nearPoint || similarDistance) && betterArea)) {
|
|
2145
|
+
best = planarFace;
|
|
2146
|
+
bestAlignment = alignment;
|
|
2147
|
+
bestDistance = distance;
|
|
2148
|
+
bestArea = planarFace.area;
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
return best;
|
|
2152
|
+
}
|
|
2153
|
+
/**
|
|
2154
|
+
* Find the best matching logical planar surface wrapper by normal
|
|
2155
|
+
* similarity and optional point proximity.
|
|
2156
|
+
*/
|
|
2157
|
+
findSurfaceByNormal(targetNormal, nearPoint) {
|
|
2158
|
+
const face = this.findPlanarFaceByNormal(targetNormal, nearPoint);
|
|
2159
|
+
if (!face)
|
|
2160
|
+
return null;
|
|
2161
|
+
return this.getSurface(face.index);
|
|
2162
|
+
}
|
|
1844
2163
|
/**
|
|
1845
2164
|
* Odd/even point containment test against a closed mesh.
|
|
1846
2165
|
* Uses majority vote across multiple ray directions for robustness.
|
|
@@ -1853,30 +2172,14 @@ export class Mesh {
|
|
|
1853
2172
|
* Find the coplanar + edge-connected face group containing a triangle.
|
|
1854
2173
|
*/
|
|
1855
2174
|
findFaceByTriangleIndex(triangleIndex) {
|
|
1856
|
-
|
|
1857
|
-
return null;
|
|
1858
|
-
ensureInit();
|
|
1859
|
-
const r = wasm.mesh_find_face_group_by_triangle_index(this._vertexCount, this._buffer, Math.floor(triangleIndex));
|
|
1860
|
-
if (!r || r.length < 6)
|
|
1861
|
-
return null;
|
|
1862
|
-
return {
|
|
1863
|
-
centroid: new Point(r[0], r[1], r[2]),
|
|
1864
|
-
normal: new Vec3(r[3], r[4], r[5]),
|
|
1865
|
-
};
|
|
2175
|
+
return this.getPlanarFaceByTriangleIndex(triangleIndex);
|
|
1866
2176
|
}
|
|
1867
2177
|
/**
|
|
1868
2178
|
* Find the best matching coplanar + edge-connected face group by normal
|
|
1869
2179
|
* similarity and optional point proximity.
|
|
1870
2180
|
*/
|
|
1871
2181
|
findFaceByNormal(targetNormal, nearPoint) {
|
|
1872
|
-
|
|
1873
|
-
const r = wasm.mesh_find_face_group_by_normal(this._vertexCount, this._buffer, targetNormal.x, targetNormal.y, targetNormal.z, nearPoint?.x ?? 0, nearPoint?.y ?? 0, nearPoint?.z ?? 0, nearPoint !== undefined);
|
|
1874
|
-
if (!r || r.length < 6)
|
|
1875
|
-
return null;
|
|
1876
|
-
return {
|
|
1877
|
-
centroid: new Point(r[0], r[1], r[2]),
|
|
1878
|
-
normal: new Vec3(r[3], r[4], r[5]),
|
|
1879
|
-
};
|
|
2182
|
+
return this.findPlanarFaceByNormal(targetNormal, nearPoint);
|
|
1880
2183
|
}
|
|
1881
2184
|
// ── Export ──────────────────────────────────────────────────────
|
|
1882
2185
|
/**
|