@bitbybit-dev/manifold 1.4.1 → 1.5.0-rc.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.
- package/lib/api/dto-registry.js +13 -4
- package/lib/api/inputs/manifold-inputs.d.ts +249 -8
- package/lib/api/inputs/manifold-inputs.js +237 -10
- package/lib/api/manifold-service.d.ts +16 -9
- package/lib/api/manifold-service.js +21 -8
- package/lib/api/resolved-inputs/index.d.ts +9 -2
- package/lib/api/services/cross-section/cross-section-booleans.d.ts +2 -2
- package/lib/api/services/cross-section/cross-section-evaluate.d.ts +2 -2
- package/lib/api/services/cross-section/cross-section-operations.d.ts +41 -3
- package/lib/api/services/cross-section/cross-section-operations.js +85 -3
- package/lib/api/services/cross-section/cross-section-shapes.d.ts +1 -1
- package/lib/api/services/cross-section/cross-section-transforms.d.ts +1 -1
- package/lib/api/services/cross-section/cross-section.constants.d.ts +6 -0
- package/lib/api/services/cross-section/cross-section.constants.js +5 -0
- package/lib/api/services/cross-section/cross-section.d.ts +2 -2
- package/lib/api/services/manifold/manifold-booleans.d.ts +1 -1
- package/lib/api/services/manifold/manifold-evaluate.d.ts +1 -1
- package/lib/api/services/manifold/manifold-operations.d.ts +1 -1
- package/lib/api/services/manifold/manifold-shapes.d.ts +1 -1
- package/lib/api/services/manifold/manifold-shapes.js +2 -17
- package/lib/api/services/manifold/manifold-transforms.d.ts +1 -1
- package/lib/api/services/manifold/manifold.d.ts +2 -2
- package/lib/api/services/mesh/mesh-evaluate.d.ts +1 -1
- package/lib/api/services/mesh/mesh-operations.d.ts +2 -2
- package/lib/api/services/mesh/mesh.d.ts +1 -1
- package/lib/api/services/mesh-data.d.ts +4 -0
- package/lib/api/services/mesh-data.js +23 -0
- package/lib/api/services/recipes/recipe-executor.d.ts +5 -0
- package/lib/api/services/recipes/recipe-executor.js +339 -0
- package/lib/api/services/recipes/recipe-loft.d.ts +3 -0
- package/lib/api/services/recipes/recipe-loft.js +85 -0
- package/lib/api/services/recipes/recipe-meshes.d.ts +5 -0
- package/lib/api/services/recipes/recipe-meshes.js +46 -0
- package/lib/api/services/recipes/recipe-types.d.ts +4 -0
- package/lib/api/services/recipes/recipe-types.js +1 -0
- package/lib/api/services/recipes/recipes.constants.d.ts +3 -0
- package/lib/api/services/recipes/recipes.constants.js +3 -0
- package/lib/api/services/recipes/recipes.d.ts +71 -0
- package/lib/api/services/recipes/recipes.js +104 -0
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs/manifold-inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Building Manifold cross-sections, the flat outlines that become solids: squares, rectangles,
|
|
5
5
|
* circles and outlines from raw polygons. They lie in the XY plane; `center` places a shape on the
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs/manifold-inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Moving, turning, scaling, mirroring and warping Manifold cross-sections in the XY plane. Angles
|
|
5
5
|
* are in degrees and rotations turn about the origin; every method returns a new cross-section.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Base } from "@bitbybit-dev/base";
|
|
2
|
+
export declare const MIN_LOFT_FRAMES = 2;
|
|
3
|
+
export declare const MIN_CIRCLE_SIDES = 3;
|
|
4
|
+
export declare const FULL_TURN: number;
|
|
5
|
+
export declare const DEGENERATE_DETERMINANT = 1e-12;
|
|
6
|
+
export declare const NO_DIRECTION: Base.Vector3;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
import { CrossSectionShapes } from "./cross-section-shapes";
|
|
4
4
|
import { CrossSectionOperations } from "./cross-section-operations";
|
|
5
5
|
import { CrossSectionTransforms } from "./cross-section-transforms";
|
|
6
6
|
import { CrossSectionBooleans } from "./cross-section-booleans";
|
|
7
7
|
import { CrossSectionEvaluate } from "./cross-section-evaluate";
|
|
8
|
-
import { BaseBitByBit } from "../../../base";
|
|
8
|
+
import type { BaseBitByBit } from "../../../base";
|
|
9
9
|
/**
|
|
10
10
|
* Flat outlines in the Manifold kernel, the 2D shapes that `operations.extrude` and
|
|
11
11
|
* `operations.revolve` turn into solids and that `slice` and `project` cut out of them. A
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs/manifold-inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Combining Manifold solids: fusing, cutting and intersecting two or many at once, and splitting a
|
|
5
5
|
* solid with another solid or a plane. Because the kernel works on closed triangle meshes, these
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Measuring Manifold solids and reading their bookkeeping: surface area and volume, vertex,
|
|
5
5
|
* triangle and edge counts, the bounding box, the tolerance, the genus, the gap to another solid,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Changing Manifold solids beyond booleans: wrapping them in a convex hull, slicing and projecting
|
|
5
5
|
* them into cross-sections, refining and smoothing their meshes, simplifying them, composing and
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs/manifold-inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Building Manifold solids: the cube, sphere, cylinder and tetrahedron primitives, and solids from
|
|
5
5
|
* triangle meshes or lists of triangles. The kernel keeps Z as its up axis, so a cylinder stands
|
|
@@ -1,22 +1,7 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs/manifold-inputs";
|
|
2
2
|
import { resolveDto } from "@bitbybit-dev/base";
|
|
3
|
-
/**
|
|
4
|
-
* The grid a vertex is snapped onto before it is used as a de-duplication key.
|
|
5
|
-
*
|
|
6
|
-
* Vertices arriving from separate triangles of the same surface differ in the last bits after any
|
|
7
|
-
* floating-point arithmetic has touched them, and keying on the exact coordinates left every such
|
|
8
|
-
* pair as two vertices - which is what made a mesh built from polygon points non-watertight along a
|
|
9
|
-
* seam that looks closed. Snapping to a grid merges them.
|
|
10
|
-
*
|
|
11
|
-
* The value is absolute, not relative: it is chosen to be far above the noise of double arithmetic on
|
|
12
|
-
* ordinary CAD magnitudes and far below any distance a model means to express. Two vertices that
|
|
13
|
-
* genuinely sit closer together than this are merged, and two that straddle a grid boundary are not -
|
|
14
|
-
* that is inherent to snapping, and the alternative, a neighborhood search, costs more than it is
|
|
15
|
-
* worth here.
|
|
16
|
-
*/
|
|
17
3
|
const VERTEX_MERGE_TOLERANCE = 1e-7;
|
|
18
|
-
|
|
19
|
-
const quantize = (coordinate) => Math.round(coordinate / VERTEX_MERGE_TOLERANCE) || 0;
|
|
4
|
+
const snapToMergeGrid = (coordinate) => Math.round(coordinate / VERTEX_MERGE_TOLERANCE) || 0;
|
|
20
5
|
/**
|
|
21
6
|
* Building Manifold solids: the cube, sphere, cylinder and tetrahedron primitives, and solids from
|
|
22
7
|
* triangle meshes or lists of triangles. The kernel keeps Z as its up axis, so a cylinder stands
|
|
@@ -80,7 +65,7 @@ export class ManifoldShapes {
|
|
|
80
65
|
console.warn(`Skipping invalid point data in triangle: ${JSON.stringify(point)}`);
|
|
81
66
|
throw new Error(`Invalid point data encountered: ${JSON.stringify(point)} in triangle ${JSON.stringify(triangle)}`);
|
|
82
67
|
}
|
|
83
|
-
const vertexKey = `${
|
|
68
|
+
const vertexKey = `${snapToMergeGrid(point[0])},${snapToMergeGrid(point[1])},${snapToMergeGrid(point[2])}`;
|
|
84
69
|
let index;
|
|
85
70
|
if (vertexMap.has(vertexKey)) {
|
|
86
71
|
index = vertexMap.get(vertexKey);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs/manifold-inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Moving, turning, scaling, mirroring and warping Manifold solids. The kernel combines transforms
|
|
5
5
|
* lazily, so chaining them is cheap; angles are in degrees, and rotations turn about the origin, X
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import * as Manifold3D from "manifold-3d";
|
|
1
|
+
import type * as Manifold3D from "manifold-3d";
|
|
2
2
|
import { ManifoldShapes } from "./manifold-shapes";
|
|
3
3
|
import { ManifoldBooleans } from "./manifold-booleans";
|
|
4
4
|
import { ManifoldOperations } from "./manifold-operations";
|
|
5
5
|
import { ManifoldTransforms } from "./manifold-transforms";
|
|
6
6
|
import { ManifoldEvaluate } from "./manifold-evaluate";
|
|
7
|
-
import * as Inputs from "../../inputs";
|
|
7
|
+
import type * as Inputs from "../../inputs";
|
|
8
8
|
/**
|
|
9
9
|
* Solids in the Manifold kernel: `shapes` builds cubes, spheres, cylinders and solids from meshes,
|
|
10
10
|
* `booleans` fuses, cuts and splits them, `operations` hulls, slices, refines and smooths them,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Inputs from "../../inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Reading Manifold mesh data: the position and extra properties of a vertex, the vertices of a
|
|
5
5
|
* triangle, the tangent of a half-edge, the transform of a triangle run, and the counts of
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as Inputs from "../../inputs";
|
|
2
|
-
import * as Manifold3D from "manifold-3d";
|
|
1
|
+
import type * as Inputs from "../../inputs";
|
|
2
|
+
import type * as Manifold3D from "manifold-3d";
|
|
3
3
|
/**
|
|
4
4
|
* Repairs on Manifold mesh data before it is turned back into a solid; today that is `merge`, which
|
|
5
5
|
* restores the merge information a file round trip loses.
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type * as Inputs from "../inputs";
|
|
3
|
+
export declare function positionsOf(mesh: Pick<Manifold3D.Mesh, "numProp" | "vertProperties">): Float32Array;
|
|
4
|
+
export declare function creasedMeshData(positions: Float32Array, triVerts: Uint32Array, minSharpAngle: number): Pick<Inputs.Manifold.DecomposedManifoldMeshDto, "numProp" | "vertProperties" | "triVerts">;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { creasedMesh } from "@bitbybit-dev/base/lib/api/services/helpers/mesh-normals";
|
|
2
|
+
const POSITION_SIZE = 3;
|
|
3
|
+
const WITH_NORMALS = 6;
|
|
4
|
+
export function positionsOf(mesh) {
|
|
5
|
+
if (mesh.numProp === POSITION_SIZE) {
|
|
6
|
+
return mesh.vertProperties;
|
|
7
|
+
}
|
|
8
|
+
const count = mesh.vertProperties.length / mesh.numProp;
|
|
9
|
+
const positions = new Float32Array(count * POSITION_SIZE);
|
|
10
|
+
for (let vertex = 0; vertex < count; vertex++) {
|
|
11
|
+
positions.set(mesh.vertProperties.subarray(vertex * mesh.numProp, vertex * mesh.numProp + POSITION_SIZE), vertex * POSITION_SIZE);
|
|
12
|
+
}
|
|
13
|
+
return positions;
|
|
14
|
+
}
|
|
15
|
+
export function creasedMeshData(positions, triVerts, minSharpAngle) {
|
|
16
|
+
const creased = creasedMesh(positions, triVerts, minSharpAngle);
|
|
17
|
+
const vertProperties = new Float32Array(creased.positions.length / POSITION_SIZE * WITH_NORMALS);
|
|
18
|
+
for (let vertex = 0; vertex * POSITION_SIZE < creased.positions.length; vertex++) {
|
|
19
|
+
vertProperties.set(creased.positions.subarray(vertex * POSITION_SIZE, (vertex + 1) * POSITION_SIZE), vertex * WITH_NORMALS);
|
|
20
|
+
vertProperties.set(creased.normals.subarray(vertex * POSITION_SIZE, (vertex + 1) * POSITION_SIZE), vertex * WITH_NORMALS + POSITION_SIZE);
|
|
21
|
+
}
|
|
22
|
+
return { numProp: WITH_NORMALS, vertProperties, triVerts: creased.indices };
|
|
23
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type { Base } from "@bitbybit-dev/base";
|
|
3
|
+
export declare function turnedZtoY(matrix: Base.TransformMatrix): Manifold3D.Mat4;
|
|
4
|
+
export declare function executeRecipe(wasm: Manifold3D.ManifoldToplevel, recipe: Base.Recipe, circularSegments: number, adjustZtoY: boolean, emptyWhenFailed: boolean): Manifold3D.Manifold[];
|
|
5
|
+
export declare function executeRecipeNodes(wasm: Manifold3D.ManifoldToplevel, recipe: Base.Recipe, circularSegments: number, emptyWhenFailed: boolean, nodes: readonly number[]): Map<number, Manifold3D.Manifold | undefined>;
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { loftMesh } from "./recipe-loft";
|
|
2
|
+
import { DEGREES_PER_RADIAN } from "./recipes.constants";
|
|
3
|
+
const UNIT_HEIGHT = 1;
|
|
4
|
+
const FULL_TURN = 2 * Math.PI;
|
|
5
|
+
const MIN_REVOLVE_SEGMENTS = 3;
|
|
6
|
+
const POSITION_PROPERTIES = 3;
|
|
7
|
+
const FILL_RULE = "NonZero";
|
|
8
|
+
function isHalfSpace(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && "op" in value;
|
|
10
|
+
}
|
|
11
|
+
function release(value) {
|
|
12
|
+
if (value && !isHalfSpace(value)) {
|
|
13
|
+
try {
|
|
14
|
+
value.delete();
|
|
15
|
+
}
|
|
16
|
+
catch (_a) {
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function asError(thrown, subject) {
|
|
21
|
+
return thrown instanceof Error ? thrown : new Error(`${subject} could not be built: the kernel stopped on an error it did not describe`, { cause: thrown });
|
|
22
|
+
}
|
|
23
|
+
function rootSubject(root, index) {
|
|
24
|
+
return `Recipe node ${root.node}, placed by root ${index},`;
|
|
25
|
+
}
|
|
26
|
+
function expectNoError(status, root, index) {
|
|
27
|
+
if (status !== "NoError") {
|
|
28
|
+
throw new Error(`${rootSubject(root, index)} could not be built: the kernel reports '${status}'`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function requiredInputsOf(node) {
|
|
32
|
+
switch (node.op) {
|
|
33
|
+
case "extrude":
|
|
34
|
+
return [node.profile];
|
|
35
|
+
case "difference":
|
|
36
|
+
return [node.of, ...node.tools];
|
|
37
|
+
case "transform":
|
|
38
|
+
return [node.of];
|
|
39
|
+
case "voids":
|
|
40
|
+
return [node.host];
|
|
41
|
+
case "compound":
|
|
42
|
+
case "union":
|
|
43
|
+
case "intersection":
|
|
44
|
+
return node.of;
|
|
45
|
+
case "revolve":
|
|
46
|
+
return [node.profile];
|
|
47
|
+
case "loft":
|
|
48
|
+
return node.profiles;
|
|
49
|
+
default:
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function negatedWithoutNegativeZero(value) {
|
|
54
|
+
return 0 - value;
|
|
55
|
+
}
|
|
56
|
+
export function turnedZtoY(matrix) {
|
|
57
|
+
const [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] = matrix;
|
|
58
|
+
return [
|
|
59
|
+
a, c, negatedWithoutNegativeZero(b), d,
|
|
60
|
+
e, g, negatedWithoutNegativeZero(f), h,
|
|
61
|
+
i, k, negatedWithoutNegativeZero(j), l,
|
|
62
|
+
m, o, negatedWithoutNegativeZero(n), p,
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
function isClockwise(ring) {
|
|
66
|
+
let twiceArea = 0;
|
|
67
|
+
for (let at = 0, previous = ring.length - 1; at < ring.length; previous = at++) {
|
|
68
|
+
twiceArea += ring[previous][0] * ring[at][1] - ring[at][0] * ring[previous][1];
|
|
69
|
+
}
|
|
70
|
+
return twiceArea < 0;
|
|
71
|
+
}
|
|
72
|
+
class RecipeExecutor {
|
|
73
|
+
constructor(wasm, recipe, circularSegments, adjustZtoY, emptyWhenFailed) {
|
|
74
|
+
this.wasm = wasm;
|
|
75
|
+
this.recipe = recipe;
|
|
76
|
+
this.circularSegments = circularSegments;
|
|
77
|
+
this.adjustZtoY = adjustZtoY;
|
|
78
|
+
this.emptyWhenFailed = emptyWhenFailed;
|
|
79
|
+
this.built = [];
|
|
80
|
+
this.failed = new Set();
|
|
81
|
+
}
|
|
82
|
+
run() {
|
|
83
|
+
const results = [];
|
|
84
|
+
try {
|
|
85
|
+
this.buildNodes();
|
|
86
|
+
this.recipe.roots.forEach((root, index) => {
|
|
87
|
+
results.push(this.failed.has(root.node) ? this.wasm.Manifold.compose([]) : this.placed(root, index));
|
|
88
|
+
});
|
|
89
|
+
return results;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
results.forEach(release);
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
this.built.forEach(release);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
solidsOf(nodes) {
|
|
100
|
+
const solids = new Map();
|
|
101
|
+
try {
|
|
102
|
+
this.buildNodes();
|
|
103
|
+
for (const node of nodes) {
|
|
104
|
+
solids.set(node, this.failed.has(node) ? undefined : this.solid(node).translate([0, 0, 0]));
|
|
105
|
+
}
|
|
106
|
+
return solids;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
solids.forEach(release);
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
this.built.forEach(release);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
buildNodes() {
|
|
117
|
+
this.recipe.nodes.forEach((node, index) => {
|
|
118
|
+
if (this.failed.size && requiredInputsOf(node).some((input) => this.failed.has(input))) {
|
|
119
|
+
this.skip(index);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
this.built.push(this.build(node, index));
|
|
124
|
+
}
|
|
125
|
+
catch (thrown) {
|
|
126
|
+
if (!this.emptyWhenFailed) {
|
|
127
|
+
throw asError(thrown, `Recipe node ${index}`);
|
|
128
|
+
}
|
|
129
|
+
this.skip(index);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
skip(index) {
|
|
134
|
+
this.built.push(undefined);
|
|
135
|
+
this.failed.add(index);
|
|
136
|
+
}
|
|
137
|
+
placed(root, index) {
|
|
138
|
+
let placed;
|
|
139
|
+
try {
|
|
140
|
+
placed = this.solid(root.node).transform(this.adjustZtoY ? turnedZtoY(root.matrix) : root.matrix);
|
|
141
|
+
expectNoError(placed.status(), root, index);
|
|
142
|
+
return placed;
|
|
143
|
+
}
|
|
144
|
+
catch (thrown) {
|
|
145
|
+
release(placed);
|
|
146
|
+
if (!this.emptyWhenFailed) {
|
|
147
|
+
throw asError(thrown, rootSubject(root, index));
|
|
148
|
+
}
|
|
149
|
+
return this.wasm.Manifold.compose([]);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
solid(index) {
|
|
153
|
+
return this.built[index];
|
|
154
|
+
}
|
|
155
|
+
ring(range) {
|
|
156
|
+
const values = this.recipe.buffers.f64.subarray(range[0], range[0] + range[1]);
|
|
157
|
+
const points = [];
|
|
158
|
+
for (let at = 0; at + 1 < values.length; at += 2) {
|
|
159
|
+
points.push([values[at], values[at + 1]]);
|
|
160
|
+
}
|
|
161
|
+
return points;
|
|
162
|
+
}
|
|
163
|
+
counterClockwiseRing(range) {
|
|
164
|
+
const points = this.ring(range);
|
|
165
|
+
return isClockwise(points) ? points.reverse() : points;
|
|
166
|
+
}
|
|
167
|
+
build(node, index) {
|
|
168
|
+
switch (node.op) {
|
|
169
|
+
case "polygon":
|
|
170
|
+
return this.polygon(node);
|
|
171
|
+
case "circle": {
|
|
172
|
+
const circle = this.wasm.CrossSection.circle(node.radius, this.circularSegments);
|
|
173
|
+
try {
|
|
174
|
+
return circle.translate(node.center);
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
circle.delete();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
case "extrude":
|
|
181
|
+
return this.extrude(node);
|
|
182
|
+
case "halfSpace":
|
|
183
|
+
return node;
|
|
184
|
+
case "difference":
|
|
185
|
+
return this.difference(node);
|
|
186
|
+
case "transform":
|
|
187
|
+
return this.solid(node.of).transform(node.matrix);
|
|
188
|
+
case "voids":
|
|
189
|
+
return this.voids(node);
|
|
190
|
+
case "triangles":
|
|
191
|
+
return this.triangles(node, index);
|
|
192
|
+
case "compound":
|
|
193
|
+
case "union":
|
|
194
|
+
return this.wasm.Manifold.union(node.of.map((child) => this.solid(child)));
|
|
195
|
+
case "intersection":
|
|
196
|
+
return this.wasm.Manifold.intersection(node.of.map((child) => this.solid(child)));
|
|
197
|
+
case "revolve":
|
|
198
|
+
return this.revolve(node, index);
|
|
199
|
+
case "loft":
|
|
200
|
+
return this.loft(node, index);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
revolve(node, index) {
|
|
204
|
+
const [dx, dy] = node.direction;
|
|
205
|
+
const length = Math.hypot(dx, dy);
|
|
206
|
+
const along = [dx / length, dy / length];
|
|
207
|
+
const [ox, oy] = node.origin;
|
|
208
|
+
const tolerance = this.recipe.tolerance;
|
|
209
|
+
const inAxisFrame = (across) => this.built[node.profile].transform([
|
|
210
|
+
across[0], along[0], 0, across[1], along[1], 0, -(across[0] * ox + across[1] * oy), -(along[0] * ox + along[1] * oy), 1,
|
|
211
|
+
]);
|
|
212
|
+
let across = [along[1], -along[0]];
|
|
213
|
+
let profile = inAxisFrame(across);
|
|
214
|
+
const { min, max } = profile.bounds();
|
|
215
|
+
if (min[0] < -tolerance && max[0] > tolerance) {
|
|
216
|
+
profile.delete();
|
|
217
|
+
throw new Error(`Recipe node ${index} turns a profile that lies on both sides of its axis`);
|
|
218
|
+
}
|
|
219
|
+
if (max[0] <= tolerance) {
|
|
220
|
+
profile.delete();
|
|
221
|
+
across = [-across[0], -across[1]];
|
|
222
|
+
profile = inAxisFrame(across);
|
|
223
|
+
}
|
|
224
|
+
const normal = along[0] * across[1] - along[1] * across[0];
|
|
225
|
+
try {
|
|
226
|
+
const turned = this.wasm.Manifold.revolve(profile, Math.max(MIN_REVOLVE_SEGMENTS, Math.ceil((this.circularSegments * node.angle) / FULL_TURN)), node.angle * DEGREES_PER_RADIAN);
|
|
227
|
+
try {
|
|
228
|
+
return turned.transform([across[0], across[1], 0, 0, 0, 0, normal, 0, along[0], along[1], 0, 0, ox, oy, 0, 1]);
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
turned.delete();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
finally {
|
|
235
|
+
profile.delete();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
profileRings(profile) {
|
|
239
|
+
const node = this.recipe.nodes[profile];
|
|
240
|
+
if (node.op === "circle") {
|
|
241
|
+
const [cx, cy] = node.center;
|
|
242
|
+
return [Array.from({ length: this.circularSegments }, (_, at) => {
|
|
243
|
+
const angle = (FULL_TURN * at) / this.circularSegments;
|
|
244
|
+
return [cx + node.radius * Math.cos(angle), cy + node.radius * Math.sin(angle)];
|
|
245
|
+
})];
|
|
246
|
+
}
|
|
247
|
+
const polygon = node;
|
|
248
|
+
return [this.counterClockwiseRing(polygon.points), ...polygon.holes.map((hole) => this.counterClockwiseRing(hole).reverse())];
|
|
249
|
+
}
|
|
250
|
+
loft(node, index) {
|
|
251
|
+
const frames = this.recipe.buffers.f64.subarray(node.frames[0], node.frames[0] + node.frames[1]);
|
|
252
|
+
const sections = node.profiles.map((profile) => this.profileRings(profile));
|
|
253
|
+
return this.wasm.Manifold.ofMesh(loftMesh(this.wasm, sections, frames, this.recipe.tolerance, `Recipe node ${index}`));
|
|
254
|
+
}
|
|
255
|
+
polygon(node) {
|
|
256
|
+
const { CrossSection } = this.wasm;
|
|
257
|
+
const outline = new CrossSection([this.ring(node.points)], FILL_RULE);
|
|
258
|
+
if (node.holes.length === 0) {
|
|
259
|
+
return outline;
|
|
260
|
+
}
|
|
261
|
+
try {
|
|
262
|
+
const holes = new CrossSection(node.holes.map((hole) => this.counterClockwiseRing(hole)), FILL_RULE);
|
|
263
|
+
try {
|
|
264
|
+
return outline.subtract(holes);
|
|
265
|
+
}
|
|
266
|
+
finally {
|
|
267
|
+
holes.delete();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
finally {
|
|
271
|
+
outline.delete();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
extrude(node) {
|
|
275
|
+
const swept = this.built[node.profile].extrude(UNIT_HEIGHT);
|
|
276
|
+
const [dx, dy, dz] = node.direction;
|
|
277
|
+
const scale = node.depth / Math.hypot(dx, dy, dz);
|
|
278
|
+
try {
|
|
279
|
+
return swept.transform([1, 0, 0, 0, 0, 1, 0, 0, dx * scale, dy * scale, dz * scale, 0, 0, 0, 0, 1]);
|
|
280
|
+
}
|
|
281
|
+
finally {
|
|
282
|
+
swept.delete();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
cut(solid, tool) {
|
|
286
|
+
if (isHalfSpace(tool)) {
|
|
287
|
+
const [nx, ny, nz] = tool.normal;
|
|
288
|
+
const length = Math.hypot(nx, ny, nz);
|
|
289
|
+
const offset = (nx * tool.origin[0] + ny * tool.origin[1] + nz * tool.origin[2]) / length;
|
|
290
|
+
return solid.trimByPlane([-nx / length, -ny / length, -nz / length], -offset);
|
|
291
|
+
}
|
|
292
|
+
return solid.subtract(tool);
|
|
293
|
+
}
|
|
294
|
+
difference(node) {
|
|
295
|
+
let current = this.cut(this.solid(node.of), this.built[node.tools[0]]);
|
|
296
|
+
for (let at = 1; at < node.tools.length; at++) {
|
|
297
|
+
const previous = current;
|
|
298
|
+
try {
|
|
299
|
+
current = this.cut(previous, this.built[node.tools[at]]);
|
|
300
|
+
}
|
|
301
|
+
finally {
|
|
302
|
+
previous.delete();
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return current;
|
|
306
|
+
}
|
|
307
|
+
voids(node) {
|
|
308
|
+
const host = this.solid(node.host);
|
|
309
|
+
const built = node.openings.filter((opening) => !this.failed.has(opening));
|
|
310
|
+
if (built.length === 0) {
|
|
311
|
+
return host.translate([0, 0, 0]);
|
|
312
|
+
}
|
|
313
|
+
const openings = this.wasm.Manifold.union(built.map((opening) => this.solid(opening)));
|
|
314
|
+
try {
|
|
315
|
+
return host.subtract(openings);
|
|
316
|
+
}
|
|
317
|
+
finally {
|
|
318
|
+
openings.delete();
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
triangles(node, index) {
|
|
322
|
+
const positions = this.recipe.buffers.f64.subarray(node.positions[0], node.positions[0] + node.positions[1]);
|
|
323
|
+
const indices = this.recipe.buffers.i32.subarray(node.indices[0], node.indices[0] + node.indices[1]);
|
|
324
|
+
const mesh = new this.wasm.Mesh({ numProp: POSITION_PROPERTIES, vertProperties: Float32Array.from(positions), triVerts: Uint32Array.from(indices) });
|
|
325
|
+
mesh.merge();
|
|
326
|
+
try {
|
|
327
|
+
return this.wasm.Manifold.ofMesh(mesh);
|
|
328
|
+
}
|
|
329
|
+
catch (error) {
|
|
330
|
+
throw new Error(`Recipe node ${index} is a triangle mesh that is not closed, so it cannot be built as a solid`, { cause: error });
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
export function executeRecipe(wasm, recipe, circularSegments, adjustZtoY, emptyWhenFailed) {
|
|
335
|
+
return new RecipeExecutor(wasm, recipe, circularSegments, adjustZtoY, emptyWhenFailed).run();
|
|
336
|
+
}
|
|
337
|
+
export function executeRecipeNodes(wasm, recipe, circularSegments, emptyWhenFailed, nodes) {
|
|
338
|
+
return new RecipeExecutor(wasm, recipe, circularSegments, false, emptyWhenFailed).solidsOf(nodes);
|
|
339
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type { ProfileRings } from "./recipe-types";
|
|
3
|
+
export declare function loftMesh(wasm: Manifold3D.ManifoldToplevel, sections: readonly ProfileRings[], frames: Float64Array, tolerance: number | undefined, subject: string): Manifold3D.Mesh;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { signedVolumeOf } from "@bitbybit-dev/base/lib/api/services/helpers/mesh-measures";
|
|
2
|
+
import { signedArea2 } from "@bitbybit-dev/base/lib/api/services/helpers/polygons";
|
|
3
|
+
import { cross3, length3 } from "@bitbybit-dev/base/lib/api/services/helpers/vectors";
|
|
4
|
+
import { FLOAT32_THICKNESS, TRIANGLE_CORNERS } from "./recipes.constants";
|
|
5
|
+
const MATRIX_SIZE = 16;
|
|
6
|
+
const POSITION_PROPERTIES = 3;
|
|
7
|
+
function sameShape(a, b) {
|
|
8
|
+
return a.length === b.length && a.every((ring, at) => ring.length === b[at].length);
|
|
9
|
+
}
|
|
10
|
+
function placedArea(rings, frame) {
|
|
11
|
+
const areaScale = length3(cross3([frame[0], frame[1], frame[2]], [frame[4], frame[5], frame[6]]));
|
|
12
|
+
return Math.abs(rings.reduce((sum, ring) => sum + signedArea2(ring), 0)) * areaScale;
|
|
13
|
+
}
|
|
14
|
+
function farthestCoordinate(positions) {
|
|
15
|
+
let farthest = 0;
|
|
16
|
+
for (const value of positions) {
|
|
17
|
+
farthest = Math.max(farthest, Math.abs(value));
|
|
18
|
+
}
|
|
19
|
+
return farthest;
|
|
20
|
+
}
|
|
21
|
+
function capOf(wasm, rings, tolerance) {
|
|
22
|
+
return wasm.triangulate(rings.map((ring) => [...ring]), tolerance);
|
|
23
|
+
}
|
|
24
|
+
export function loftMesh(wasm, sections, frames, tolerance, subject) {
|
|
25
|
+
const count = frames.length / MATRIX_SIZE;
|
|
26
|
+
const first = sections[0];
|
|
27
|
+
if (!sections.every((section) => sameShape(section, first))) {
|
|
28
|
+
throw new Error(`${subject} lofts profiles whose loops hold different numbers of corners, so their corners cannot be joined`);
|
|
29
|
+
}
|
|
30
|
+
const starts = [];
|
|
31
|
+
let perFrame = 0;
|
|
32
|
+
for (const ring of first) {
|
|
33
|
+
starts.push(perFrame);
|
|
34
|
+
perFrame += ring.length;
|
|
35
|
+
}
|
|
36
|
+
const positions = new Float32Array(count * perFrame * POSITION_PROPERTIES);
|
|
37
|
+
let at = 0;
|
|
38
|
+
for (let frame = 0; frame < count; frame++) {
|
|
39
|
+
const m = frames.subarray(frame * MATRIX_SIZE, (frame + 1) * MATRIX_SIZE);
|
|
40
|
+
for (const ring of sections[sections.length === 1 ? 0 : frame]) {
|
|
41
|
+
for (const [x, y] of ring) {
|
|
42
|
+
positions[at++] = m[0] * x + m[4] * y + m[12];
|
|
43
|
+
positions[at++] = m[1] * x + m[5] * y + m[13];
|
|
44
|
+
positions[at++] = m[2] * x + m[6] * y + m[14];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const triangles = [];
|
|
49
|
+
const corner = (frame, ring, point) => frame * perFrame + starts[ring] + (point % first[ring].length);
|
|
50
|
+
for (let frame = 0; frame + 1 < count; frame++) {
|
|
51
|
+
first.forEach((ring, r) => {
|
|
52
|
+
for (let point = 0; point < ring.length; point++) {
|
|
53
|
+
const a = corner(frame, r, point);
|
|
54
|
+
const b = corner(frame, r, point + 1);
|
|
55
|
+
const c = corner(frame + 1, r, point + 1);
|
|
56
|
+
const d = corner(frame + 1, r, point);
|
|
57
|
+
triangles.push(a, b, c, a, c, d);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
const lastOffset = (count - 1) * perFrame;
|
|
62
|
+
for (const [a, b, c] of capOf(wasm, first, tolerance)) {
|
|
63
|
+
triangles.push(a, c, b);
|
|
64
|
+
}
|
|
65
|
+
for (const [a, b, c] of capOf(wasm, sections[sections.length - 1], tolerance)) {
|
|
66
|
+
triangles.push(lastOffset + a, lastOffset + b, lastOffset + c);
|
|
67
|
+
}
|
|
68
|
+
const indices = Uint32Array.from(triangles);
|
|
69
|
+
const volume = signedVolumeOf(positions, indices);
|
|
70
|
+
let largestArea = 0;
|
|
71
|
+
for (let frame = 0; frame < count; frame++) {
|
|
72
|
+
largestArea = Math.max(largestArea, placedArea(sections[sections.length === 1 ? 0 : frame], frames.subarray(frame * MATRIX_SIZE, (frame + 1) * MATRIX_SIZE)));
|
|
73
|
+
}
|
|
74
|
+
if (!(Math.abs(volume) > farthestCoordinate(positions) * FLOAT32_THICKNESS * largestArea)) {
|
|
75
|
+
throw new Error(`${subject} is no thicker than the precision of its coordinates: its frames leave its sections in one plane`);
|
|
76
|
+
}
|
|
77
|
+
if (volume < 0) {
|
|
78
|
+
for (let corner = 0; corner < indices.length; corner += TRIANGLE_CORNERS) {
|
|
79
|
+
const second = indices[corner + 1];
|
|
80
|
+
indices[corner + 1] = indices[corner + 2];
|
|
81
|
+
indices[corner + 2] = second;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return new wasm.Mesh({ numProp: POSITION_PROPERTIES, vertProperties: positions, triVerts: indices });
|
|
85
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type * as Manifold3D from "manifold-3d";
|
|
2
|
+
import type { Base } from "@bitbybit-dev/base";
|
|
3
|
+
import type * as Inputs from "../../inputs";
|
|
4
|
+
import type * as Resolved from "../../resolved-inputs";
|
|
5
|
+
export declare function recipeMeshes(wasm: Manifold3D.ManifoldToplevel, recipe: Base.Recipe, options: Omit<Resolved.Manifold.BuildRecipeMeshesDto, "recipe">): Inputs.Manifold.RecipeMeshesDto;
|