@rulecad/topopt 0.2.0 → 0.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/README.md +3 -3
- package/package.json +1 -1
- package/topopt.d.mts +12 -6
- package/topopt.mjs +44 -9
package/README.md
CHANGED
|
@@ -25,8 +25,8 @@ const loops = outlines(spec.grid, result.density, spec.origin); // closed loop
|
|
|
25
25
|
### In 3D
|
|
26
26
|
|
|
27
27
|
Regions take a z. A region can be a box (`size`), a cylinder (`r`, `length`, `axis`), a ball (`r`), or a closed
|
|
28
|
-
triangle mesh (`triangles`). A cylinder
|
|
29
|
-
|
|
28
|
+
triangle mesh (`triangles`). A cylinder can be a teardrop: `teardrop: '-z'` for a boss, `'+z'` for a hole, or any
|
|
29
|
+
direction across its axis. It gets a 45° point that way, so it prints without support. Supports and loads can be any region.
|
|
30
30
|
|
|
31
31
|
```js
|
|
32
32
|
const spec = discretise({
|
|
@@ -38,7 +38,7 @@ const spec = discretise({
|
|
|
38
38
|
overhang: '+z', // printable standing this way up: no supports
|
|
39
39
|
});
|
|
40
40
|
const result = optimise(spec); // multigrid solves each step
|
|
41
|
-
const { vertices, faces } = surface(spec.grid, result.density, spec.origin);
|
|
41
|
+
const { vertices, faces } = surface(spec.grid, result.density, spec.origin, { overhang: { up: '+z', angle: 45 } });
|
|
42
42
|
```
|
|
43
43
|
|
|
44
44
|
## How it works
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/topopt",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Topology optimisation for flat and solid parts: the stiffest layout of a fixed amount of material, printable without supports if asked, as outlines or a surface ready to print.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
package/topopt.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @typedef {{ at: number[], r: number } | { at: number[], size: number[] } | { at: number[], r: number, length: number, axis?: 'x' | 'y' | 'z', teardrop?:
|
|
2
|
+
* @typedef {{ at: number[], r: number } | { at: number[], size: number[] } | { at: number[], r: number, length: number, axis?: 'x' | 'y' | 'z', teardrop?: string } | { points: [number, number][] } | { triangles: number[][][] }} Region
|
|
3
3
|
* in mm: a circle (2D) or sphere (3D) at, r; a box at (its centre), size; a cylinder at (its centre), r, length,
|
|
4
|
-
* along axis (default z; teardrop '
|
|
4
|
+
* along axis (default z; teardrop '+z', '-y', …: a 45° point that way, across the axis, so it prints); a polygon (2D);
|
|
5
5
|
* or a closed triangle mesh (3D: a design's part)
|
|
6
6
|
*/
|
|
7
7
|
/** @typedef {Region & { fix?: string }} Support holds the nodes in the region (on material): fix the directions, 'xy' or 'xyz' (default all) */
|
|
@@ -165,7 +165,9 @@ export function area(loop: [number, number][]): number;
|
|
|
165
165
|
* @param {{ nx: number, ny: number, nz: number, h: number }} size @param {ArrayLike<number>} density @param {number[]} origin
|
|
166
166
|
* Vertices on the grid's outer faces stay where they are as it's smoothed, so a part that fills its space to a face (a
|
|
167
167
|
* base on the bed) keeps that face flat, and the walls rising from it don't lean out.
|
|
168
|
-
* @param {{ smooth?: number, iso?: number }} [options] iso: the density the
|
|
168
|
+
* @param {{ smooth?: number, iso?: number, overhang?: { up: string, angle: number } }} [options] iso: the density the
|
|
169
|
+
* surface is drawn at (0.5). overhang: the build direction ('+z') and the printer's overhang limit (degrees from
|
|
170
|
+
* vertical): faces smoothing left past it are tipped back to it.
|
|
169
171
|
* @returns {{ vertices: number[][], faces: number[][] }}
|
|
170
172
|
*/
|
|
171
173
|
export function surface({ nx, ny, nz, h }: {
|
|
@@ -173,9 +175,13 @@ export function surface({ nx, ny, nz, h }: {
|
|
|
173
175
|
ny: number;
|
|
174
176
|
nz: number;
|
|
175
177
|
h: number;
|
|
176
|
-
}, density: ArrayLike<number>, origin?: number[], { smooth, iso }?: {
|
|
178
|
+
}, density: ArrayLike<number>, origin?: number[], { smooth, iso, overhang }?: {
|
|
177
179
|
smooth?: number;
|
|
178
180
|
iso?: number;
|
|
181
|
+
overhang?: {
|
|
182
|
+
up: string;
|
|
183
|
+
angle: number;
|
|
184
|
+
};
|
|
179
185
|
}): {
|
|
180
186
|
vertices: number[][];
|
|
181
187
|
faces: number[][];
|
|
@@ -184,7 +190,7 @@ export { jsBackend };
|
|
|
184
190
|
export function inside(r: Region, pt: number[]): boolean;
|
|
185
191
|
/**
|
|
186
192
|
* in mm: a circle (2D) or sphere (3D) at, r; a box at (its centre), size; a cylinder at (its centre), r, length,
|
|
187
|
-
* along axis (default z; teardrop '
|
|
193
|
+
* along axis (default z; teardrop '+z', '-y', …: a 45° point that way, across the axis, so it prints); a polygon (2D);
|
|
188
194
|
* or a closed triangle mesh (3D: a design's part)
|
|
189
195
|
*/
|
|
190
196
|
export type Region = {
|
|
@@ -198,7 +204,7 @@ export type Region = {
|
|
|
198
204
|
r: number;
|
|
199
205
|
length: number;
|
|
200
206
|
axis?: "x" | "y" | "z";
|
|
201
|
-
teardrop?:
|
|
207
|
+
teardrop?: string;
|
|
202
208
|
} | {
|
|
203
209
|
points: [number, number][];
|
|
204
210
|
} | {
|
package/topopt.mjs
CHANGED
|
@@ -17,9 +17,9 @@ import { grid as makeGrid, cellStiffness, solve, positions, jsBackend } from '@r
|
|
|
17
17
|
export { jsBackend };
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
* @typedef {{ at: number[], r: number } | { at: number[], size: number[] } | { at: number[], r: number, length: number, axis?: 'x' | 'y' | 'z', teardrop?:
|
|
20
|
+
* @typedef {{ at: number[], r: number } | { at: number[], size: number[] } | { at: number[], r: number, length: number, axis?: 'x' | 'y' | 'z', teardrop?: string } | { points: [number, number][] } | { triangles: number[][][] }} Region
|
|
21
21
|
* in mm: a circle (2D) or sphere (3D) at, r; a box at (its centre), size; a cylinder at (its centre), r, length,
|
|
22
|
-
* along axis (default z; teardrop '
|
|
22
|
+
* along axis (default z; teardrop '+z', '-y', …: a 45° point that way, across the axis, so it prints); a polygon (2D);
|
|
23
23
|
* or a closed triangle mesh (3D: a design's part)
|
|
24
24
|
*/
|
|
25
25
|
/** @typedef {Region & { fix?: string }} Support holds the nodes in the region (on material): fix the directions, 'xy' or 'xyz' (default all) */
|
|
@@ -360,13 +360,13 @@ function insider(r) {
|
|
|
360
360
|
if (r.size) return (pt) => r.size.every((s, a) => Math.abs(pt[a] - r.at[a]) <= s / 2);
|
|
361
361
|
if (r.length != null) {
|
|
362
362
|
const ax = 'xyz'.indexOf(r.axis || 'z'), others = [0, 1, 2].filter((a) => a !== ax);
|
|
363
|
-
// A teardrop: a cylinder across the build
|
|
364
|
-
//
|
|
365
|
-
const
|
|
363
|
+
// A teardrop: a cylinder across the build with a 45° point below it (a boss) or above it (a hole), so it prints
|
|
364
|
+
// unsupported. Along the build, it's a plain cylinder.
|
|
365
|
+
const pa = r.teardrop ? 'xyz'.indexOf(r.teardrop[1]) : -1, point = pa >= 0 && pa !== ax ? (r.teardrop[0] === '-' ? -1 : 1) : 0, side = others.find((a) => a !== pa);
|
|
366
366
|
return (pt) => {
|
|
367
367
|
if (Math.abs(pt[ax] - r.at[ax]) > r.length / 2) return false;
|
|
368
368
|
if (Math.hypot(pt[others[0]] - r.at[others[0]], pt[others[1]] - r.at[others[1]]) <= r.r) return true;
|
|
369
|
-
const out = point * (pt[
|
|
369
|
+
const out = point * (pt[pa] - r.at[pa]);
|
|
370
370
|
return point !== 0 && out > 0 && Math.abs(pt[side] - r.at[side]) + out <= r.r * Math.SQRT2;
|
|
371
371
|
};
|
|
372
372
|
}
|
|
@@ -421,7 +421,8 @@ export function bounds(r) {
|
|
|
421
421
|
const half = r.size ? r.size.map((s) => s / 2) : r.length != null ? [0, 1, 2].map((a) => (a === 'xyz'.indexOf(r.axis || 'z') ? r.length / 2 : r.r)) : new Array(dim).fill(r.r);
|
|
422
422
|
const lo = r.at.map((c, a) => c - half[a]);
|
|
423
423
|
const hi = r.at.map((c, a) => c + half[a]);
|
|
424
|
-
|
|
424
|
+
const pa = r.teardrop && r.length != null ? 'xyz'.indexOf(r.teardrop[1]) : -1;
|
|
425
|
+
if (pa >= 0 && pa !== 'xyz'.indexOf(r.axis || 'z')) { if (r.teardrop[0] === '-') lo[pa] = r.at[pa] - r.r * Math.SQRT2; else hi[pa] = r.at[pa] + r.r * Math.SQRT2; } // its point
|
|
425
426
|
return [...lo, ...hi];
|
|
426
427
|
}
|
|
427
428
|
|
|
@@ -525,10 +526,12 @@ function simplify(loop, tolerance) {
|
|
|
525
526
|
* @param {{ nx: number, ny: number, nz: number, h: number }} size @param {ArrayLike<number>} density @param {number[]} origin
|
|
526
527
|
* Vertices on the grid's outer faces stay where they are as it's smoothed, so a part that fills its space to a face (a
|
|
527
528
|
* base on the bed) keeps that face flat, and the walls rising from it don't lean out.
|
|
528
|
-
* @param {{ smooth?: number, iso?: number }} [options] iso: the density the
|
|
529
|
+
* @param {{ smooth?: number, iso?: number, overhang?: { up: string, angle: number } }} [options] iso: the density the
|
|
530
|
+
* surface is drawn at (0.5). overhang: the build direction ('+z') and the printer's overhang limit (degrees from
|
|
531
|
+
* vertical): faces smoothing left past it are tipped back to it.
|
|
529
532
|
* @returns {{ vertices: number[][], faces: number[][] }}
|
|
530
533
|
*/
|
|
531
|
-
export function surface({ nx, ny, nz, h }, density, origin = [0, 0, 0], { smooth = 4, iso = 0.5 } = {}) {
|
|
534
|
+
export function surface({ nx, ny, nz, h }, density, origin = [0, 0, 0], { smooth = 4, iso = 0.5, overhang } = {}) {
|
|
532
535
|
const W = nx + 3, H = ny + 3, D = nz + 3, v = new Float64Array(W * H * D), id = (i, j, k) => (k * H + j) * W + i;
|
|
533
536
|
// Each corner: the average of the cubes round it that are in the grid (on the grid's faces, only those inside it, so
|
|
534
537
|
// material reaching a face reads as solid there, not half)
|
|
@@ -593,7 +596,39 @@ export function surface({ nx, ny, nz, h }, density, origin = [0, 0, 0], { smooth
|
|
|
593
596
|
return held[n] ? p : p.map((x, q) => Math.min(hi[q], Math.max(lo[q], x + f * (avg[q] - x))));
|
|
594
597
|
});
|
|
595
598
|
}
|
|
599
|
+
if (overhang) steepen(pts, faces, held, overhang, lo, hi);
|
|
596
600
|
return { vertices: pts, faces };
|
|
597
601
|
}
|
|
598
602
|
|
|
603
|
+
// Faces past the overhang limit, tipped back to it. Optimised in cubes, a printable slope is a staircase of 45°, and
|
|
604
|
+
// smoothing leaves small faces either side of 45°. Each face facing down more steeply than the limit is turned about
|
|
605
|
+
// its centre, moving its corners along the build direction only, until it's a little inside the limit: about as much
|
|
606
|
+
// material is added as taken away. A corner shared by several such faces moves by their average; it's repeated until
|
|
607
|
+
// none are left. Flat faces (a bridge) and faces held on the grid's sides stay as they are.
|
|
608
|
+
function steepen(pts, faces, held, { up, angle }, lo, hi, passes = 200) {
|
|
609
|
+
const q = 'xyz'.indexOf(up[1]), s = up[0] === '-' ? -1 : 1, [a, b] = [0, 1, 2].filter((x) => x !== q);
|
|
610
|
+
const limit = Math.sin((angle * Math.PI) / 180), slope = 1.03 / Math.tan((angle * Math.PI) / 180);
|
|
611
|
+
for (let pass = 0; pass < passes; pass++) {
|
|
612
|
+
const move = new Float64Array(pts.length), count = new Uint16Array(pts.length);
|
|
613
|
+
let left = 0;
|
|
614
|
+
for (const f of faces) {
|
|
615
|
+
if (held[f[0]] && held[f[1]] && held[f[2]]) continue;
|
|
616
|
+
const [p, r, t] = f.map((n) => pts[n]), u = [r[0] - p[0], r[1] - p[1], r[2] - p[2]], w = [t[0] - p[0], t[1] - p[1], t[2] - p[2]];
|
|
617
|
+
const n = [u[1] * w[2] - u[2] * w[1], u[2] * w[0] - u[0] * w[2], u[0] * w[1] - u[1] * w[0]], len = Math.hypot(...n);
|
|
618
|
+
if (!len || (s * n[q]) / len >= -limit) continue;
|
|
619
|
+
// The face's plane as a height along the build direction over the other two: x_q = c + ga x_a + gb x_b
|
|
620
|
+
const ga = -n[a] / n[q], gb = -n[b] / n[q], g = Math.hypot(ga, gb);
|
|
621
|
+
if (g < 0.05) continue; // flat: a bridge, which turning won't help
|
|
622
|
+
left++;
|
|
623
|
+
const c = [0, 1, 2].map((x) => (p[x] + r[x] + t[x]) / 3), k = slope / g;
|
|
624
|
+
for (const m of f) {
|
|
625
|
+
const v = pts[m];
|
|
626
|
+
move[m] += c[q] + k * (ga * (v[a] - c[a]) + gb * (v[b] - c[b])) - v[q]; count[m]++;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (!left) break;
|
|
630
|
+
for (let m = 0; m < pts.length; m++) if (count[m] && !held[m]) pts[m][q] = Math.min(hi[q], Math.max(lo[q], pts[m][q] + move[m] / count[m]));
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
599
634
|
const fmt = (n) => String(Number(n.toFixed(2)));
|