@rulecad/shapes 0.4.0 → 0.4.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/optimised.d.mts CHANGED
@@ -31,7 +31,7 @@ export function optimisedOutline(result: {
31
31
  /**
32
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
33
  * null.
34
- * @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: '-z' | '+z', part?: string }} r
34
+ * @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: string, part?: string }} r
35
35
  * @param {number} [segments]
36
36
  */
37
37
  export function solidRegion(r: {
@@ -40,7 +40,7 @@ export function solidRegion(r: {
40
40
  size?: number[];
41
41
  length?: number;
42
42
  axis?: string;
43
- teardrop?: "-z" | "+z";
43
+ teardrop?: string;
44
44
  part?: string;
45
45
  }, segments?: number): jscad.geometries.geom3.Geom3;
46
46
  /**
package/optimised.mjs CHANGED
@@ -48,24 +48,26 @@ export function optimisedOutline(result, { segments = 48 } = {}) {
48
48
  /**
49
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
50
  * null.
51
- * @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: '-z' | '+z', part?: string }} r
51
+ * @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: string, part?: string }} r
52
52
  * @param {number} [segments]
53
53
  */
54
54
  export function solidRegion(r, segments = 48) {
55
55
  if (r.part || r.triangles) return null;
56
56
  if (r.size) return cuboid({ center: r.at, size: r.size });
57
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);
58
+ const c = cylinder({ radius: r.r, height: r.length, segments }), axis = r.axis || 'z', ax = 'xyz'.indexOf(axis);
59
+ const turned = translate(r.at, axis === 'x' ? rotateY(Math.PI / 2, c) : axis === 'y' ? rotateZ(Math.PI / 2, rotateY(Math.PI / 2, c)) : c);
60
+ // A teardrop (across the axis): the cylinder hulled with a thin ridge r√2 from its axis, below it (a boss) or
61
+ // above it (a hole), so that side is 45°
62
+ // The ridge: a thin slice of a square turned 45° about the axis at each end, its far corner a little past r√2: a
63
+ // round of segments is a little inside its circle, so the faces from it to a point at exactly r√2 would be a touch
64
+ // flatter than 45°
65
+ const pa = r.teardrop ? 'xyz'.indexOf(r.teardrop[1]) : -1;
66
+ if (pa < 0 || pa === ax) return turned;
67
+ const edge = 0.05, slice = 1e-3, reach = (r.teardrop[0] === '-' ? -1 : 1) * (r.r * Math.SQRT2 / Math.cos(Math.PI / segments) - edge / Math.SQRT2);
68
+ const square = [0, 1, 2].map((a) => (a === ax ? slice : edge)), spin = [rotateX, rotateY, rotateZ][ax];
69
+ const end = (along) => translate(r.at.map((x, a) => x + (a === pa ? reach : 0) + (a === ax ? along : 0)), spin(Math.PI / 4, cuboid({ size: square })));
70
+ return hull(turned, end((slice - r.length) / 2), end((r.length - slice) / 2));
69
71
  }
70
72
  return sphere({ center: r.at, radius: r.r, segments });
71
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/shapes",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
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": {