@bitbybit-dev/manifold 1.6.0 → 1.6.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.
@@ -6,6 +6,8 @@ const FULL_TURN = 2 * Math.PI;
6
6
  const MIN_REVOLVE_SEGMENTS = 3;
7
7
  const POSITION_PROPERTIES = 3;
8
8
  const FILL_RULE = "NonZero";
9
+ const AXES = 3;
10
+ const MAX_TOOL_PARTS = 64;
9
11
  function isHalfSpace(value) {
10
12
  return typeof value === "object" && value !== null && "op" in value;
11
13
  }
@@ -290,24 +292,130 @@ class RecipeExecutor {
290
292
  swept.delete();
291
293
  }
292
294
  }
293
- cut(solid, tool) {
295
+ reaching(tool, outside) {
296
+ const reach = this.recipe.tolerance;
297
+ const ofTool = tool.boundingBox();
298
+ const below = ofTool.min.map((side, axis) => Math.abs(side - outside.min[axis]) <= reach);
299
+ const above = ofTool.max.map((side, axis) => Math.abs(side - outside.max[axis]) <= reach);
300
+ if (!below.includes(true) && !above.includes(true)) {
301
+ return tool.translate([0, 0, 0]);
302
+ }
303
+ return tool.warp((vertex) => {
304
+ for (let axis = 0; axis < AXES; axis++) {
305
+ const drawn = vertex[axis];
306
+ const fromBelow = drawn - ofTool.min[axis];
307
+ const fromAbove = ofTool.max[axis] - drawn;
308
+ const down = below[axis] && fromBelow <= reach;
309
+ const up = above[axis] && fromAbove <= reach;
310
+ if (down && (!up || fromBelow <= fromAbove)) {
311
+ vertex[axis] = drawn - reach;
312
+ }
313
+ else if (up) {
314
+ vertex[axis] = drawn + reach;
315
+ }
316
+ }
317
+ });
318
+ }
319
+ joinsParts(index) {
320
+ const node = this.recipe.nodes[index];
321
+ return node.op === "compound" || node.op === "union" || (node.op === "transform" && this.joinsParts(node.of));
322
+ }
323
+ partCount(index, counted) {
324
+ const known = counted.get(index);
325
+ if (known !== undefined) {
326
+ return known;
327
+ }
328
+ const node = this.recipe.nodes[index];
329
+ const count = node.op === "compound" || node.op === "union"
330
+ ? node.of.reduce((sum, child) => sum + this.partCount(child, counted), 0)
331
+ : node.op === "transform" ? this.partCount(node.of, counted) : 1;
332
+ counted.set(index, count);
333
+ return count;
334
+ }
335
+ toolParts(index) {
336
+ const parts = [];
337
+ try {
338
+ if (this.partCount(index, new Map()) > MAX_TOOL_PARTS) {
339
+ parts.push(this.solid(index).translate([0, 0, 0]));
340
+ }
341
+ else {
342
+ this.collectParts(index, [], parts);
343
+ }
344
+ return parts;
345
+ }
346
+ catch (thrown) {
347
+ parts.forEach(release);
348
+ throw thrown;
349
+ }
350
+ }
351
+ collectParts(index, above, into) {
352
+ const node = this.recipe.nodes[index];
353
+ if (node.op === "compound" || node.op === "union") {
354
+ node.of.forEach((child) => this.collectParts(child, above, into));
355
+ return;
356
+ }
357
+ if (node.op === "transform" && this.joinsParts(node.of)) {
358
+ this.collectParts(node.of, [node.matrix, ...above], into);
359
+ return;
360
+ }
361
+ let part = this.solid(index).translate([0, 0, 0]);
362
+ try {
363
+ for (const matrix of above) {
364
+ const moved = part.transform(matrix);
365
+ part.delete();
366
+ part = moved;
367
+ }
368
+ }
369
+ catch (thrown) {
370
+ part.delete();
371
+ throw thrown;
372
+ }
373
+ into.push(part);
374
+ }
375
+ reachedParts(index, outside, into) {
376
+ const parts = this.toolParts(index);
377
+ try {
378
+ for (const part of parts) {
379
+ into.push(this.reaching(part, outside));
380
+ }
381
+ }
382
+ finally {
383
+ parts.forEach(release);
384
+ }
385
+ }
386
+ cut(solid, toolIndex, outside) {
387
+ const tool = this.built[toolIndex];
294
388
  if (isHalfSpace(tool)) {
295
389
  const [nx, ny, nz] = tool.normal;
296
390
  const length = Math.hypot(nx, ny, nz);
297
391
  const offset = (nx * tool.origin[0] + ny * tool.origin[1] + nz * tool.origin[2]) / length;
298
392
  return solid.trimByPlane([-nx / length, -ny / length, -nz / length], -offset);
299
393
  }
300
- return solid.subtract(tool);
394
+ const tools = [];
395
+ let reaching;
396
+ try {
397
+ this.reachedParts(toolIndex, outside, tools);
398
+ reaching = this.wasm.Manifold.union(tools);
399
+ return solid.subtract(reaching);
400
+ }
401
+ finally {
402
+ tools.forEach(release);
403
+ release(reaching);
404
+ }
301
405
  }
302
406
  difference(node) {
303
- let current = this.cut(this.solid(node.of), this.built[node.tools[0]]);
304
- for (let at = 1; at < node.tools.length; at++) {
407
+ let outside = this.solid(node.of).boundingBox();
408
+ let current;
409
+ for (const toolIndex of node.tools) {
305
410
  const previous = current;
306
411
  try {
307
- current = this.cut(previous, this.built[node.tools[at]]);
412
+ current = this.cut(previous !== null && previous !== void 0 ? previous : this.solid(node.of), toolIndex, outside);
308
413
  }
309
414
  finally {
310
- previous.delete();
415
+ release(previous);
416
+ }
417
+ if (isHalfSpace(this.built[toolIndex])) {
418
+ outside = current.boundingBox();
311
419
  }
312
420
  }
313
421
  return current;
@@ -318,12 +426,19 @@ class RecipeExecutor {
318
426
  if (built.length === 0) {
319
427
  return host.translate([0, 0, 0]);
320
428
  }
321
- const openings = this.wasm.Manifold.union(built.map((opening) => this.solid(opening)));
429
+ const outside = host.boundingBox();
430
+ const tools = [];
431
+ let openings;
322
432
  try {
433
+ for (const opening of built) {
434
+ this.reachedParts(opening, outside, tools);
435
+ }
436
+ openings = this.wasm.Manifold.union(tools);
323
437
  return host.subtract(openings);
324
438
  }
325
439
  finally {
326
- openings.delete();
440
+ tools.forEach(release);
441
+ release(openings);
327
442
  }
328
443
  }
329
444
  triangles(node, index) {
@@ -17,9 +17,9 @@ export declare class ManifoldRecipes {
17
17
  /**
18
18
  * Builds each root of a recipe as a solid placed by its matrix, in root order.
19
19
  *
20
- * Shared nodes are built once. An invalid recipe throws before anything is built; a root the
21
- * kernel cannot build, such as an open mesh, throws naming its node unless `emptyWhenFailed` is
22
- * set. Circles get `circularSegments` sides; `tolerance` and `millimetresPerUnit` are not applied.
20
+ * Shared nodes are built once. An invalid recipe throws; a root the kernel cannot build, such as
21
+ * an open mesh, throws unless `emptyWhenFailed` is set. A cut within `tolerance` of a side of the
22
+ * box of what it cuts goes through it.
23
23
  * @param inputs - The recipe, how finely circles are divided, and whether Z turns into Y
24
24
  * @returns One solid per root, in root order
25
25
  * @group build
@@ -22,9 +22,9 @@ export class ManifoldRecipes {
22
22
  /**
23
23
  * Builds each root of a recipe as a solid placed by its matrix, in root order.
24
24
  *
25
- * Shared nodes are built once. An invalid recipe throws before anything is built; a root the
26
- * kernel cannot build, such as an open mesh, throws naming its node unless `emptyWhenFailed` is
27
- * set. Circles get `circularSegments` sides; `tolerance` and `millimetresPerUnit` are not applied.
25
+ * Shared nodes are built once. An invalid recipe throws; a root the kernel cannot build, such as
26
+ * an open mesh, throws unless `emptyWhenFailed` is set. A cut within `tolerance` of a side of the
27
+ * box of what it cuts goes through it.
28
28
  * @param inputs - The recipe, how finely circles are divided, and whether Z turns into Y
29
29
  * @returns One solid per root, in root order
30
30
  * @group build
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitbybit-dev/manifold",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
4
4
  "description": "Bit By Bit Developers Manifold based CAD Library to Program Geometry",
5
5
  "homepage": "https://docs.bitbybit.dev/classes/Bit.Manifold",
6
6
  "main": "index.js",
@@ -55,6 +55,6 @@
55
55
  "type": "module",
56
56
  "dependencies": {
57
57
  "manifold-3d": "3.5.4",
58
- "@bitbybit-dev/base": "1.6.0"
58
+ "@bitbybit-dev/base": "1.6.1"
59
59
  }
60
60
  }