@rulecad/shapes 0.3.0 → 0.4.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/optimised.d.mts CHANGED
@@ -28,4 +28,40 @@ export function optimisedOutline(result: {
28
28
  }, { segments }?: {
29
29
  segments?: number;
30
30
  }): jscad.geometries.geom2.Geom2;
31
+ /**
32
+ * A 3D region (a box, a cylinder along x, y or z, a teardrop, or a ball) as a JSCAD solid; a part's mesh region gives
33
+ * null.
34
+ * @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: '-z' | '+z', part?: string }} r
35
+ * @param {number} [segments]
36
+ */
37
+ export function solidRegion(r: {
38
+ at?: number[];
39
+ r?: number;
40
+ size?: number[];
41
+ length?: number;
42
+ axis?: string;
43
+ teardrop?: "-z" | "+z";
44
+ part?: string;
45
+ }, segments?: number): jscad.geometries.geom3.Geom3;
46
+ /**
47
+ * A 3D optimised result's surface as a solid, its keep regions added and avoid regions cut exactly.
48
+ * @param {{ mesh: { vertices: number[][], faces: number[][] }, regions: { keep: object[], avoid: object[] } }} result
49
+ * @param {{ ops?: { union: Function, subtract: Function }, segments?: number }} [options] ops: v.ops (Manifold, ideally)
50
+ */
51
+ export function optimisedSolid(result: {
52
+ mesh: {
53
+ vertices: number[][];
54
+ faces: number[][];
55
+ };
56
+ regions: {
57
+ keep: object[];
58
+ avoid: object[];
59
+ };
60
+ }, { ops, segments }?: {
61
+ ops?: {
62
+ union: Function;
63
+ subtract: Function;
64
+ };
65
+ segments?: number;
66
+ }): jscad.geometries.geom3.Geom3;
31
67
  import jscad from '@jscad/modeling';
package/optimised.mjs CHANGED
@@ -3,10 +3,14 @@
3
3
  // clockwise), so they make one shape with its holes as they are. The problem's keep regions are added back exactly
4
4
  // and its avoid regions cut exactly, so bolt holes and the bosses round them come out true to size, not stepped.
5
5
  // fromOutlines(loops) the loops as one 2D shape
6
- // optimisedOutline(result, { segments }) a result's outline with its keep regions added and avoid regions cut
6
+ // optimisedOutline(result, { segments }) a 2D result's outline with its keep regions added and avoid regions cut
7
+ // optimisedSolid(result, { ops }) a 3D result's surface as a solid, its keep regions added and avoid regions
8
+ // cut (boxes, cylinders and balls; a part kept is the design's to add). The
9
+ // mesh is big: pass v.ops, with the object's engine set to manifold
7
10
  import jscad from '@jscad/modeling';
8
11
 
9
- const { geom2 } = jscad.geometries, { circle, rectangle, polygon } = jscad.primitives, { union, subtract } = jscad.booleans;
12
+ const { geom2 } = jscad.geometries, { circle, rectangle, polygon, polyhedron, cuboid, cylinder, sphere } = jscad.primitives, { union, subtract } = jscad.booleans;
13
+ const { rotateX, rotateY, rotateZ, translate } = jscad.transforms, { hull } = jscad.hulls;
10
14
 
11
15
  /**
12
16
  * Closed loops (solid on the left) as one JSCAD 2D shape.
@@ -40,3 +44,42 @@ export function optimisedOutline(result, { segments = 48 } = {}) {
40
44
  if (result.regions.avoid.length) shape = subtract(shape, ...result.regions.avoid.map((r) => regionShape(r, segments)));
41
45
  return shape;
42
46
  }
47
+
48
+ /**
49
+ * A 3D region (a box, a cylinder along x, y or z, a teardrop, or a ball) as a JSCAD solid; a part's mesh region gives
50
+ * null.
51
+ * @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: '-z' | '+z', part?: string }} r
52
+ * @param {number} [segments]
53
+ */
54
+ export function solidRegion(r, segments = 48) {
55
+ if (r.part || r.triangles) return null;
56
+ if (r.size) return cuboid({ center: r.at, size: r.size });
57
+ if (r.length != null) {
58
+ let c = cylinder({ radius: r.r, height: r.length, segments });
59
+ // A teardrop (along x or y): the cylinder hulled with a thin ridge r√2 from its axis, below it ('-z', a boss) or
60
+ // above it ('+z', a hole), so that side is 45° (turned below, +x becomes -z)
61
+ // The ridge: a thin slice of a square turned 45° at each end, its far corner a little past r√2: a round of
62
+ // segments is a little inside its circle, so the faces from it to a point at exactly r√2 would be a touch flatter
63
+ // than 45°
64
+ const edge = 0.05, slice = 1e-3, reach = (r.teardrop === '+z' ? -1 : 1) * (r.r * Math.SQRT2 / Math.cos(Math.PI / segments) - edge / Math.SQRT2);
65
+ const end = (z) => translate([reach, 0, z], rotateZ(Math.PI / 4, cuboid({ size: [edge, edge, slice] })));
66
+ if (r.teardrop && r.axis !== 'z') c = hull(c, end((slice - r.length) / 2), end((r.length - slice) / 2));
67
+ const turned = r.axis === 'x' ? rotateY(Math.PI / 2, c) : r.axis === 'y' ? rotateZ(Math.PI / 2, rotateY(Math.PI / 2, c)) : c;
68
+ return translate(r.at, turned);
69
+ }
70
+ return sphere({ center: r.at, radius: r.r, segments });
71
+ }
72
+
73
+ /**
74
+ * A 3D optimised result's surface as a solid, its keep regions added and avoid regions cut exactly.
75
+ * @param {{ mesh: { vertices: number[][], faces: number[][] }, regions: { keep: object[], avoid: object[] } }} result
76
+ * @param {{ ops?: { union: Function, subtract: Function }, segments?: number }} [options] ops: v.ops (Manifold, ideally)
77
+ */
78
+ export function optimisedSolid(result, { ops, segments = 48 } = {}) {
79
+ const o = ops || { union, subtract };
80
+ let solid = polyhedron({ points: result.mesh.vertices, faces: result.mesh.faces, orientation: 'outward' });
81
+ const keep = result.regions.keep.map((r) => solidRegion(r, segments)).filter(Boolean), avoid = result.regions.avoid.map((r) => solidRegion(r, segments)).filter(Boolean);
82
+ if (keep.length) solid = o.union(solid, ...keep);
83
+ if (avoid.length) solid = o.subtract(solid, ...avoid);
84
+ return solid;
85
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/shapes",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Shape helpers for printed JSCAD designs: text that prints cleanly, anchors on a face, clean local cuts, and enclosure pieces (shells, lids, rails, print-safe holes).",
5
5
  "type": "module",
6
6
  "exports": {
package/shapes.d.mts CHANGED
@@ -27,4 +27,4 @@ export const scale: typeof jscad.transforms.scale;
27
27
  import jscad from '@jscad/modeling';
28
28
  export { outline, slab, shell, lid, dovetail, gabled, diamond, vents } from "./enclosure.mjs";
29
29
  export { countersink, teardropHole, nutPocket, crushRibs, wallOpening } from "./features.mjs";
30
- export { fromOutlines, regionShape, optimisedOutline } from "./optimised.mjs";
30
+ export { fromOutlines, regionShape, optimisedOutline, solidRegion, optimisedSolid } from "./optimised.mjs";
package/shapes.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // Shape helpers for JSCAD designs that are printed: text that prints, placing things on a face, and the pieces of
2
2
  // enclosures (enclosure.mjs: outlines, slabs, shells, lids, dovetail rails, print-safe holes, vents), and print-aware
3
3
  // features (features.mjs: countersinks, teardrop holes, nut pockets, crush ribs, wall openings), and optimised layouts
4
- // (optimised.mjs: the outlines cad optimise finds, as a 2D shape to extrude).
4
+ // (optimised.mjs: what cad optimise finds, as a 2D shape to extrude or a 3D solid).
5
5
  // text({ text, height, stroke, segments }) -> a 2D outline of the text, centred on 0: JSCAD's single-line
6
6
  // font, scaled so a capital is height tall, each line thickened to stroke wide (whole print lines, so it prints).
7
7
  // The arrows the font lacks (up, down, left, right) are drawn in the same stroke.
@@ -86,4 +86,4 @@ export { scale };
86
86
  export { outline, slab, shell, lid, dovetail, gabled, diamond, vents } from './enclosure.mjs';
87
87
  export { countersink, teardropHole, nutPocket, crushRibs, wallOpening } from './features.mjs';
88
88
  export { ops } from './ops.mjs';
89
- export { fromOutlines, regionShape, optimisedOutline } from './optimised.mjs';
89
+ export { fromOutlines, regionShape, optimisedOutline, solidRegion, optimisedSolid } from './optimised.mjs';