@rulecad/topopt 0.2.11 → 0.3.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/package.json +5 -1
- package/topopt.mjs +18 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/topopt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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": {
|
|
@@ -29,6 +29,10 @@
|
|
|
29
29
|
"url": "git+https://github.com/JoniBach/rulecad.git",
|
|
30
30
|
"directory": "packages/topopt"
|
|
31
31
|
},
|
|
32
|
+
"homepage": "https://github.com/JoniBach/rulecad/tree/main/packages/topopt#readme",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/JoniBach/rulecad/issues"
|
|
35
|
+
},
|
|
32
36
|
"scripts": {
|
|
33
37
|
"test": "node --test"
|
|
34
38
|
},
|
package/topopt.mjs
CHANGED
|
@@ -85,8 +85,11 @@ export function discretise(p) {
|
|
|
85
85
|
const all = [...space, ...keep].map(bounds), lo = [], hi = [], n = [];
|
|
86
86
|
for (let a = 0; a < dim; a++) {
|
|
87
87
|
lo.push(Math.min(...all.map((b) => b[a]))); hi.push(Math.max(...all.map((b) => b[dim + a])));
|
|
88
|
-
|
|
88
|
+
// (up: rounded down, a 40 mm space on 3 mm cells was 39 mm; but not for a sliver of a cell, a surface poking a few
|
|
89
|
+
// nanometres past the space it was drawn in, which grew a 12 mm part's grid to 16 cells across)
|
|
90
|
+
n.push(Math.max(1, Math.ceil((hi[a] - lo[a]) / p.cell - 1e-3)));
|
|
89
91
|
}
|
|
92
|
+
if (dim === 2) for (let a = 0; a < 2; a++) lo[a] -= (n[a] * p.cell - (hi[a] - lo[a])) / 2; // the padding split evenly round the space
|
|
90
93
|
if (dim === 3) { // whole numbers of 2, 4 or 8 cells along every side, for multigrid
|
|
91
94
|
const q = Math.min(...n) >= 16 ? 8 : Math.min(...n) >= 8 ? 4 : 2, build = /^([+-])([xyz])$/.exec(p.overhang ?? '');
|
|
92
95
|
for (let a = 0; a < 3; a++) {
|
|
@@ -1019,7 +1022,20 @@ export function surface({ nx, ny, nz, h }, density, origin = [0, 0, 0], { smooth
|
|
|
1019
1022
|
// Printable: faces past the limit tipped back, and tips lifted, in turn until neither finds any (each can make work for
|
|
1020
1023
|
// the other: a lifted tip leaves a face sloping more, a tipped face can leave a corner lowest)
|
|
1021
1024
|
if (overhang) settle({ vertices: pts, faces }, { overhang, lo, hi, held });
|
|
1022
|
-
return
|
|
1025
|
+
return weld(pts, faces, h * 1e-7);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// Corners that ended up in one place (crossings at a grid face, clamped to its corners, then smoothed) merged where a
|
|
1029
|
+
// face joins them (a zero-length edge collapsed), and the triangles that leaves with no area dropped. Only joined
|
|
1030
|
+
// corners are merged: two that merely touch are kept apart, so the mesh stays closed and folds nowhere
|
|
1031
|
+
function weld(pts, faces, tol) {
|
|
1032
|
+
const parent = Int32Array.from(pts.keys()), root = (i) => { while (parent[i] !== i) i = parent[i] = parent[parent[i]]; return i; };
|
|
1033
|
+
const same = (a, b) => Math.abs(pts[a][0] - pts[b][0]) <= tol && Math.abs(pts[a][1] - pts[b][1]) <= tol && Math.abs(pts[a][2] - pts[b][2]) <= tol;
|
|
1034
|
+
for (const f of faces) for (let i = 0; i < 3; i++) { const a = f[i], b = f[(i + 1) % 3]; if (same(a, b)) parent[root(a)] = root(b); }
|
|
1035
|
+
const index = new Map(), out = [];
|
|
1036
|
+
const map = (i) => { const r = root(i); if (!index.has(r)) { index.set(r, out.length); out.push(pts[r]); } return index.get(r); };
|
|
1037
|
+
const kept = faces.map((f) => f.map(map)).filter((f) => f[0] !== f[1] && f[1] !== f[2] && f[0] !== f[2]);
|
|
1038
|
+
return { vertices: out, faces: kept };
|
|
1023
1039
|
}
|
|
1024
1040
|
|
|
1025
1041
|
/**
|