@rulecad/topopt 0.3.0 → 0.3.4
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/CHANGELOG.md +22 -0
- package/README.md +35 -0
- package/package.json +2 -1
- package/topopt.d.mts +127 -43
- package/topopt.mjs +225 -6
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog: @rulecad/topopt
|
|
2
|
+
|
|
3
|
+
What changed in each release, for someone upgrading.
|
|
4
|
+
|
|
5
|
+
## 0.3.4: process filters
|
|
6
|
+
### Added
|
|
7
|
+
|
|
8
|
+
- `symmetry: { mirror, turns, axis }`: the same either side of the grid's middle planes (mirror `'x'`, `'xy'`, ...)
|
|
9
|
+
and each half or quarter turn about its middle (turns 2 or 4; a quarter turn needs a square grid across its axis).
|
|
10
|
+
- `extrude: 'x' | 'y' | 'z'`: one section all along an axis, a part cut from a bar or plate of the profile.
|
|
11
|
+
- `mill: '+z'`: milled from that side, so no undercuts: a cell is as solid as every cell between it and the tool.
|
|
12
|
+
- `cast: '+z'` or `cast: { draw: 'z', parting }`: cast in a mould drawn off one way, or in two halves drawn off both
|
|
13
|
+
ways from a parting plane (mm along the axis; the space's middle by default). The spec holds the plane's layer.
|
|
14
|
+
- `tieFilter`, `drawFilter`: the filters themselves. `undercuts(grid, density, { from, parting })`: the empty cells a
|
|
15
|
+
tool or mould half could not reach. `voids(grid, density)`: enclosed empty pockets (powder a powder bed print can't
|
|
16
|
+
lose, a casting's trapped core), each with its volume and centre.
|
|
17
|
+
- `grey(density, passive)`: the share of the design cells neither empty nor solid (between a tenth and nine tenths).
|
|
18
|
+
- A problem with two of overhang, mill and cast is refused, as is a mirror or turn that swaps the tool's side, the
|
|
19
|
+
draw or the build direction for its opposite, or a quarter turn that moves it onto another axis (a mould in two
|
|
20
|
+
halves parted at the grid's middle may be mirrored or half turned across its draw).
|
|
21
|
+
|
|
22
|
+
No existing result changes: a problem with none of the new fields is optimised exactly as before.
|
package/README.md
CHANGED
|
@@ -71,6 +71,35 @@ Two more steps shape the result:
|
|
|
71
71
|
what holds it up; `result.unsupported` says how much of the kept material is still left hanging, which means there
|
|
72
72
|
wasn't material enough to stand it on.
|
|
73
73
|
|
|
74
|
+
- **Process filters** (optional) hold the result to what another way of making it can make. `mill: '+z'` (the tool
|
|
75
|
+
cuts from that side) and `cast: '+z'` (a mould drawn off that way) make each cell as solid as every cell between
|
|
76
|
+
it and that side, by a smooth running maximum along each line (`drawFilter`, never below the larger of the two
|
|
77
|
+
densities it joins and never above 1), so nothing is undercut. With
|
|
78
|
+
`cast: { draw: 'z', parting }` the mould is in two halves, drawn off both ways from the parting plane (mm; the
|
|
79
|
+
space's middle by default). `symmetry: { mirror: 'xy', turns: 4, axis: 'z' }` and `extrude: 'x'` tie cells
|
|
80
|
+
together (`tieFilter`): each design density is the average over its mirror images, its copies turned about the
|
|
81
|
+
middle, or its line along the axis, so the part is the same in each. The mirror planes are the grid's middle
|
|
82
|
+
planes, which are the middle of the space and keep regions (in 3D, except along an overhang's build direction).
|
|
83
|
+
A mirror or turn that swaps the tool's side, the draw or the build direction for its opposite, or a quarter turn
|
|
84
|
+
that moves it onto another axis, is refused by name: the part would have to be made from both sides at once. A
|
|
85
|
+
mould in two halves parted at the grid's middle maps onto itself, so it may be mirrored or half turned across its
|
|
86
|
+
draw. A process filter goes after the density filter and before the projection, in
|
|
87
|
+
place of the overhang filter: a part is printed, milled or cast, one of them.
|
|
88
|
+
`undercuts(grid, density, { from, parting })` counts the cells a tool or a mould half could not reach (none, by
|
|
89
|
+
construction, unless an avoid region or the space's own edge sits behind material); `grey(density, passive)` is
|
|
90
|
+
the share of the design cells between a tenth and nine tenths solid. A clear result has a few per cent, on its
|
|
91
|
+
surfaces. A section milled, cast or extruded across its own plane in 2D often comes out grey (a grey column bends as
|
|
92
|
+
stiffly as a solid one of its share's height, so the optimiser has no reason to choose), and its outline at half
|
|
93
|
+
density is then a guess; `voids(grid, density)` finds enclosed empty pockets,
|
|
94
|
+
which a powder bed print can't empty of powder, with each one's volume and centre. Draft angles are a degree or
|
|
95
|
+
two, less than a cell, so they are left to the drawing.
|
|
96
|
+
|
|
97
|
+
Milled from above, the 3D cantilever (24 × 8 × 8 cubes, volume 0.3, 200 steps) has no undercuts from the tool's
|
|
98
|
+
side and a compliance of 3227 against 2989 optimised freely, 8% less stiff, with 5.3% of its cells grey; the free
|
|
99
|
+
design has over 100 cells hidden under material from above. A 2D cantilever (30 × 20 squares, 200 steps) milled from
|
|
100
|
+
its top edge has no undercuts either, but over 30% of it is grey. Mirrored across its middle, a cantilever loaded at one corner comes out the same both
|
|
101
|
+
sides to the last digit.
|
|
102
|
+
|
|
74
103
|
Several load cases are optimised together by weight. A case weighted 0 is solved only at the end, for its figures.
|
|
75
104
|
`model(spec)` exposes the analysis and its gradient, for other update methods.
|
|
76
105
|
|
|
@@ -177,6 +206,10 @@ Its gradients agree with finite differences through the filter and the projectio
|
|
|
177
206
|
- **Compliance:** the load times how far it moves (N·mm); the inverse of stiffness.
|
|
178
207
|
- **Volume:** the share of the space the result fills, 0 to 1.
|
|
179
208
|
- **Filter radius:** about the thinnest member, in mm.
|
|
209
|
+
- **Undercut:** material the tool or mould can't reach, because other material lies between it and the side the tool
|
|
210
|
+
comes from or the mould is drawn off to.
|
|
211
|
+
- **Parting plane:** where a two-part mould's halves meet.
|
|
212
|
+
- **Draft:** a slight taper on faces along the draw, so a part slides out of its mould.
|
|
180
213
|
- **Von Mises stress:** one number for how hard a point of material is worked, from all its stresses together; it's
|
|
181
214
|
compared with the material's strength.
|
|
182
215
|
|
|
@@ -194,6 +227,8 @@ Its gradients agree with finite differences through the filter and the projectio
|
|
|
194
227
|
Optimization* 50, 1175–1196.
|
|
195
228
|
- Langelaar (2017). An additive manufacturing filter for topology optimization of print-ready designs. *Structural and
|
|
196
229
|
Multidisciplinary Optimization* 55, 871–883.
|
|
230
|
+
- Gersborg, Andreassen (2011). An explicit parameterization for casting constraints in gradient driven topology
|
|
231
|
+
optimization. *Structural and Multidisciplinary Optimization* 44, 875–881.
|
|
197
232
|
- Taubin (1995). A signal processing approach to fair surface design. *SIGGRAPH '95*, 351–358.
|
|
198
233
|
- Senhora, Giraldo-Londoño, Menezes, Paulino (2020). Topology optimization with local stress constraints: a stress
|
|
199
234
|
aggregation-free approach. *Structural and Multidisciplinary Optimization* 62, 1639–1668.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/topopt",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
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": {
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"topopt.mjs",
|
|
12
12
|
"topopt.d.mts",
|
|
13
13
|
"README.md",
|
|
14
|
+
"CHANGELOG.md",
|
|
14
15
|
"LICENSE"
|
|
15
16
|
],
|
|
16
17
|
"license": "MIT",
|
package/topopt.d.mts
CHANGED
|
@@ -28,6 +28,14 @@
|
|
|
28
28
|
* @property {number} [bridge] with overhang, the widest gap the printer bridges (mm): material may also be held up by a run
|
|
29
29
|
* of solid cells along its own layer, half that long, ending on a held cell
|
|
30
30
|
* @property {number} [steps] at most this many steps (default 300)
|
|
31
|
+
* @property {{ mirror?: string, turns?: 2 | 4, axis?: 'x' | 'y' | 'z' }} [symmetry] the same either side of the grid's
|
|
32
|
+
* middle planes across each axis in mirror ('x', 'xz', …), and the same each half or quarter turn about its middle
|
|
33
|
+
* (about axis in 3D, z by default); keep and avoid regions stay as given
|
|
34
|
+
* @property {'x' | 'y' | 'z'} [extrude] one section all along this axis: cut from a bar or plate of the profile
|
|
35
|
+
* @property {string} [mill] milled from this side ('+z': the tool cuts down from above): no undercuts, so a cell is
|
|
36
|
+
* as solid as every cell between it and that side
|
|
37
|
+
* @property {string | { draw: 'x' | 'y' | 'z', parting?: number }} [cast] cast in a mould drawn off this way ('+z'),
|
|
38
|
+
* or in two halves drawn off both ways along draw from a parting plane (mm along it, default the space's middle)
|
|
31
39
|
*/
|
|
32
40
|
/**
|
|
33
41
|
* A problem described in shapes, on a grid: each cell inside the space (or a keep region) is free, kept or avoided;
|
|
@@ -49,49 +57,7 @@ export function resultant(loads: {
|
|
|
49
57
|
size: number;
|
|
50
58
|
moment: number;
|
|
51
59
|
};
|
|
52
|
-
export function discretise(p: any):
|
|
53
|
-
regions: {
|
|
54
|
-
keep: any;
|
|
55
|
-
avoid: any;
|
|
56
|
-
};
|
|
57
|
-
stress?: any;
|
|
58
|
-
bridge?: any;
|
|
59
|
-
overhang?: any;
|
|
60
|
-
material: any;
|
|
61
|
-
thickness: any;
|
|
62
|
-
volume: any;
|
|
63
|
-
filter: any;
|
|
64
|
-
penal: any;
|
|
65
|
-
projection: any;
|
|
66
|
-
steps: any;
|
|
67
|
-
output?: {
|
|
68
|
-
force: Float64Array<ArrayBuffer>;
|
|
69
|
-
spring: any;
|
|
70
|
-
input: Float64Array<ArrayBuffer>;
|
|
71
|
-
};
|
|
72
|
-
springs?: Float64Array<ArrayBuffer>;
|
|
73
|
-
grid: {
|
|
74
|
-
nx: number;
|
|
75
|
-
ny: number;
|
|
76
|
-
nz: number;
|
|
77
|
-
h: any;
|
|
78
|
-
} | {
|
|
79
|
-
nx: number;
|
|
80
|
-
ny: number;
|
|
81
|
-
h: any;
|
|
82
|
-
nz?: undefined;
|
|
83
|
-
};
|
|
84
|
-
origin: number[];
|
|
85
|
-
passive: Int8Array<ArrayBuffer>;
|
|
86
|
-
fixed: Uint8Array<ArrayBuffer>;
|
|
87
|
-
loads: {
|
|
88
|
-
moment?: number;
|
|
89
|
-
name: any;
|
|
90
|
-
force: Float64Array<ArrayBuffer>;
|
|
91
|
-
weight: any;
|
|
92
|
-
size: number;
|
|
93
|
-
}[];
|
|
94
|
-
};
|
|
60
|
+
export function discretise(p: any): any;
|
|
95
61
|
/**
|
|
96
62
|
* The density filter: each cell's filtered density is the weighted average of the cells within radius (weights
|
|
97
63
|
* falling linearly to 0 at the radius). apply filters densities; adjoint carries sensitivities back through it.
|
|
@@ -124,6 +90,48 @@ export function overhangFilter({ nx, ny, nz }: {
|
|
|
124
90
|
apply(x: any, out: any): any;
|
|
125
91
|
adjoint(g: any, out: any): any;
|
|
126
92
|
};
|
|
93
|
+
/**
|
|
94
|
+
* Ties cells that must be alike: mirror images across the grid's middle planes (mirror: 'x', 'xy', …), the copies of
|
|
95
|
+
* a turn about its middle (turns 2 or 4, about axis: z by default, the 2D grid's own), and every cell along an axis
|
|
96
|
+
* (extrude: 'x', 'y' or 'z', a constant section along it). Each design density is replaced by the average over its
|
|
97
|
+
* group's free cells (passive marks the cells that are not free: nonzero), so the design is the same in every copy.
|
|
98
|
+
* The average is its own adjoint (the matrix is symmetric), so adjoint is apply. Null when nothing is tied.
|
|
99
|
+
* @param {{ nx: number, ny: number, nz?: number }} size
|
|
100
|
+
* @param {{ mirror?: string, turns?: number, axis?: string, extrude?: string }} o @param {Int8Array} [passive]
|
|
101
|
+
*/
|
|
102
|
+
export function tieFilter({ nx, ny, nz }: {
|
|
103
|
+
nx: number;
|
|
104
|
+
ny: number;
|
|
105
|
+
nz?: number;
|
|
106
|
+
}, { mirror, turns, axis, extrude }?: {
|
|
107
|
+
mirror?: string;
|
|
108
|
+
turns?: number;
|
|
109
|
+
axis?: string;
|
|
110
|
+
extrude?: string;
|
|
111
|
+
}, passive?: Int8Array): {
|
|
112
|
+
apply: (x: any, out: any) => any;
|
|
113
|
+
adjoint: (x: any, out: any) => any;
|
|
114
|
+
groups: number;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* No undercuts from a direction: a tool coming in from one side (milling, from up: '+z' cuts down from above) or a
|
|
118
|
+
* mould drawn off that way (casting) reaches a cell only through the cells between it and that side, so a cell is
|
|
119
|
+
* at least as solid as every cell on its way out. Each cell's density is the smooth maximum of its own and the one
|
|
120
|
+
* before it on that way (a chain along each line, smooth so it has a gradient: Gersborg and Andreassen 2011 ask the
|
|
121
|
+
* same of a casting by a sum along the line). With parting, a mould in two halves: the cells from the parting layer
|
|
122
|
+
* on (towards +) are drawn off one way, those before it the other. apply gives the densities the process can make;
|
|
123
|
+
* adjoint carries sensitivities back through it.
|
|
124
|
+
* @param {{ nx: number, ny: number, nz?: number }} size @param {string} from the side the tool comes from, or (with
|
|
125
|
+
* parting) the draw axis, 'z' @param {number} [parting] with a two-part mould, the first layer of the + half (0 to n)
|
|
126
|
+
*/
|
|
127
|
+
export function drawFilter({ nx, ny, nz }: {
|
|
128
|
+
nx: number;
|
|
129
|
+
ny: number;
|
|
130
|
+
nz?: number;
|
|
131
|
+
}, from: string, parting?: number): {
|
|
132
|
+
apply(x: any, out: any): any;
|
|
133
|
+
adjoint(g: any, out: any): any;
|
|
134
|
+
};
|
|
127
135
|
export function model(spec: any, { method, backend }?: {
|
|
128
136
|
backend?: {
|
|
129
137
|
name: string;
|
|
@@ -227,6 +235,55 @@ export function dimension(r: Region): 2 | 3;
|
|
|
227
235
|
* @returns {number[]}
|
|
228
236
|
*/
|
|
229
237
|
export function bounds(r: Region): number[];
|
|
238
|
+
/**
|
|
239
|
+
* Where a result has undercuts: empty cells (below iso) with a solid one between them and the tool's side, along the
|
|
240
|
+
* line to it (or, with parting, to their half's side). What drawFilter prevents, measured on densities as made.
|
|
241
|
+
* @param {{ nx: number, ny: number, nz?: number }} size @param {ArrayLike<number>} density
|
|
242
|
+
* @param {{ from: string, parting?: number, iso?: number }} o
|
|
243
|
+
* @returns {{ count: number, cells: number[] }} the hidden cells (indices)
|
|
244
|
+
*/
|
|
245
|
+
export function undercuts({ nx, ny, nz }: {
|
|
246
|
+
nx: number;
|
|
247
|
+
ny: number;
|
|
248
|
+
nz?: number;
|
|
249
|
+
}, density: ArrayLike<number>, { from, parting, iso }: {
|
|
250
|
+
from: string;
|
|
251
|
+
parting?: number;
|
|
252
|
+
iso?: number;
|
|
253
|
+
}): {
|
|
254
|
+
count: number;
|
|
255
|
+
cells: number[];
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* How grey a result is: the share of its design cells (free, inside the space) that are neither empty nor solid,
|
|
259
|
+
* between a tenth and nine tenths. A clear design has a few per cent, on its edges; much more and the outline drawn
|
|
260
|
+
* at half density is a guess at a part, not the part. A process or a tie that leaves no clear part (a 2D section
|
|
261
|
+
* milled from its side: a grey column bends as stiffly as a solid one of its share's height) shows as grey.
|
|
262
|
+
* @param {ArrayLike<number>} density @param {ArrayLike<number>} [passive] nonzero for cells that are not free
|
|
263
|
+
* @returns {number} 0 to 1
|
|
264
|
+
*/
|
|
265
|
+
export function grey(density: ArrayLike<number>, passive?: ArrayLike<number>): number;
|
|
266
|
+
/**
|
|
267
|
+
* Enclosed voids: empty cells (below iso) that no path of empty cells, face to face, joins to the grid's edge. Powder
|
|
268
|
+
* left in them can't be got out of a powder bed print, nor a core out of a casting. Each void's cells, its volume
|
|
269
|
+
* (mm³, or mm² in 2D) and its centre (mm, from origin).
|
|
270
|
+
* @param {{ nx: number, ny: number, nz?: number, h: number }} size @param {ArrayLike<number>} density
|
|
271
|
+
* @param {{ iso?: number, origin?: number[] }} [o]
|
|
272
|
+
* @returns {{ cells: number[], volume: number, at: number[] }[]}
|
|
273
|
+
*/
|
|
274
|
+
export function voids({ nx, ny, nz, h }: {
|
|
275
|
+
nx: number;
|
|
276
|
+
ny: number;
|
|
277
|
+
nz?: number;
|
|
278
|
+
h: number;
|
|
279
|
+
}, density: ArrayLike<number>, { iso, origin }?: {
|
|
280
|
+
iso?: number;
|
|
281
|
+
origin?: number[];
|
|
282
|
+
}): {
|
|
283
|
+
cells: number[];
|
|
284
|
+
volume: number;
|
|
285
|
+
at: number[];
|
|
286
|
+
}[];
|
|
230
287
|
/**
|
|
231
288
|
* The edges of a result at density 0.5 as closed loops in mm (marching squares on the densities averaged to the
|
|
232
289
|
* cells' corners), solid on the left: outer edges counter-clockwise, holes clockwise. Smoothed (Taubin, which keeps
|
|
@@ -496,5 +553,32 @@ export type Problem = {
|
|
|
496
553
|
* at most this many steps (default 300)
|
|
497
554
|
*/
|
|
498
555
|
steps?: number;
|
|
556
|
+
/**
|
|
557
|
+
* the same either side of the grid's
|
|
558
|
+
* middle planes across each axis in mirror ('x', 'xz', …), and the same each half or quarter turn about its middle
|
|
559
|
+
* (about axis in 3D, z by default); keep and avoid regions stay as given
|
|
560
|
+
*/
|
|
561
|
+
symmetry?: {
|
|
562
|
+
mirror?: string;
|
|
563
|
+
turns?: 2 | 4;
|
|
564
|
+
axis?: "x" | "y" | "z";
|
|
565
|
+
};
|
|
566
|
+
/**
|
|
567
|
+
* one section all along this axis: cut from a bar or plate of the profile
|
|
568
|
+
*/
|
|
569
|
+
extrude?: "x" | "y" | "z";
|
|
570
|
+
/**
|
|
571
|
+
* milled from this side ('+z': the tool cuts down from above): no undercuts, so a cell is
|
|
572
|
+
* as solid as every cell between it and that side
|
|
573
|
+
*/
|
|
574
|
+
mill?: string;
|
|
575
|
+
/**
|
|
576
|
+
* cast in a mould drawn off this way ('+z'),
|
|
577
|
+
* or in two halves drawn off both ways along draw from a parting plane (mm along it, default the space's middle)
|
|
578
|
+
*/
|
|
579
|
+
cast?: string | {
|
|
580
|
+
draw: "x" | "y" | "z";
|
|
581
|
+
parting?: number;
|
|
582
|
+
};
|
|
499
583
|
};
|
|
500
584
|
import { jsBackend } from '@rulecad/fea';
|
package/topopt.mjs
CHANGED
|
@@ -50,6 +50,14 @@ export { jsBackend };
|
|
|
50
50
|
* @property {number} [bridge] with overhang, the widest gap the printer bridges (mm): material may also be held up by a run
|
|
51
51
|
* of solid cells along its own layer, half that long, ending on a held cell
|
|
52
52
|
* @property {number} [steps] at most this many steps (default 300)
|
|
53
|
+
* @property {{ mirror?: string, turns?: 2 | 4, axis?: 'x' | 'y' | 'z' }} [symmetry] the same either side of the grid's
|
|
54
|
+
* middle planes across each axis in mirror ('x', 'xz', …), and the same each half or quarter turn about its middle
|
|
55
|
+
* (about axis in 3D, z by default); keep and avoid regions stay as given
|
|
56
|
+
* @property {'x' | 'y' | 'z'} [extrude] one section all along this axis: cut from a bar or plate of the profile
|
|
57
|
+
* @property {string} [mill] milled from this side ('+z': the tool cuts down from above): no undercuts, so a cell is
|
|
58
|
+
* as solid as every cell between it and that side
|
|
59
|
+
* @property {string | { draw: 'x' | 'y' | 'z', parting?: number }} [cast] cast in a mould drawn off this way ('+z'),
|
|
60
|
+
* or in two halves drawn off both ways along draw from a parting plane (mm along it, default the space's middle)
|
|
53
61
|
*/
|
|
54
62
|
|
|
55
63
|
/**
|
|
@@ -81,11 +89,16 @@ export function resultant(loads) {
|
|
|
81
89
|
export function discretise(p) {
|
|
82
90
|
const space = [p.space].flat(), keep = p.keep || [], avoid = p.avoid || [], dim = dimension(space[0]);
|
|
83
91
|
if (!(p.cell > 0)) throw new Error('the cell size must be more than 0 mm');
|
|
92
|
+
// One process at a time: printed (overhang), milled or cast
|
|
93
|
+
const made = ['overhang', 'mill', 'cast'].filter((k) => p[k]);
|
|
94
|
+
if (made.length > 1) throw new Error(`a part is printed (overhang), milled (mill) or cast (cast), one of them: this problem gives ${made.join(' and ')}`);
|
|
84
95
|
for (const r of [...space, ...keep, ...avoid]) if (dimension(r) !== dim) throw new Error(`a ${dimension(r)}D region in a ${dim}D problem: give every region ${dim === 3 ? 'x, y and z' : 'x and y'}`);
|
|
85
96
|
const all = [...space, ...keep].map(bounds), lo = [], hi = [], n = [];
|
|
86
97
|
for (let a = 0; a < dim; a++) {
|
|
87
98
|
lo.push(Math.min(...all.map((b) => b[a]))); hi.push(Math.max(...all.map((b) => b[dim + a])));
|
|
88
|
-
|
|
99
|
+
// (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
|
|
100
|
+
// nanometres past the space it was drawn in, which grew a 12 mm part's grid to 16 cells across)
|
|
101
|
+
n.push(Math.max(1, Math.ceil((hi[a] - lo[a]) / p.cell - 1e-3)));
|
|
89
102
|
}
|
|
90
103
|
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
|
|
91
104
|
if (dim === 3) { // whole numbers of 2, 4 or 8 cells along every side, for multigrid
|
|
@@ -150,15 +163,47 @@ export function discretise(p) {
|
|
|
150
163
|
if (p.input_spring) for (const m of ins) springs[dim * m + inDir] += p.input_spring / ins.length;
|
|
151
164
|
output = { force: L, spring: o.spring || 0, input: loads[0].force };
|
|
152
165
|
}
|
|
153
|
-
return {
|
|
166
|
+
return clash({
|
|
154
167
|
grid: size, origin, passive, fixed, loads, ...(output ? { output, springs } : {}),
|
|
155
168
|
material: p.material, thickness: p.thickness, volume: p.volume,
|
|
156
169
|
// (a solid's default is more: the projection reaches full sharpness about step 200, and every 3D showcase part was
|
|
157
170
|
// still improving at 300: the bin's compliance fell 388 → 265 → 195 over steps 100, 200 and 300)
|
|
158
171
|
filter: p.filter ?? p.cell * 1.5, penal: p.penal ?? 3, projection: p.projection ?? true, steps: p.steps ?? (p.stress ? 1000 : dim === 3 ? 500 : 300),
|
|
159
172
|
...(p.overhang ? { overhang: p.overhang } : {}), ...(p.bridge ? { bridge: p.bridge } : {}), ...(p.stress ? { stress: p.stress } : {}),
|
|
173
|
+
...(p.symmetry ? { symmetry: p.symmetry } : {}), ...(p.extrude ? { extrude: p.extrude } : {}), ...(p.mill ? { mill: p.mill } : {}), ...(p.cast ? { cast: castOn(p.cast, lo, n, h, space) } : {}),
|
|
160
174
|
regions: { keep, avoid },
|
|
161
|
-
};
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// A symmetry the process can't keep: a mirror or turn that swaps the tool's side, the mould's draw or the build
|
|
179
|
+
// direction for its opposite (or a quarter turn that moves it to another axis) would ask the part to be made from both
|
|
180
|
+
// sides at once, and the tie and the process would pull it lopsided. Refused by name; a mould in two halves parted at
|
|
181
|
+
// the grid's middle maps onto itself under a mirror or half turn across its draw, so that one stands.
|
|
182
|
+
function clash(spec) {
|
|
183
|
+
const sym = spec.symmetry, way = spec.overhang || spec.mill || spec.cast;
|
|
184
|
+
if (!sym || !way || way === 'none') return spec;
|
|
185
|
+
const g = spec.grid, n = g.nz > 1 ? [g.nx, g.ny, g.nz] : [g.nx, g.ny], dim = n.length, names = 'xyz'.slice(0, dim);
|
|
186
|
+
const two = typeof way === 'object', axis = two ? way.draw : String(way).at(-1), ax = names.indexOf(axis);
|
|
187
|
+
if (ax < 0) return spec; // (a bad side is refused by its own filter, by name)
|
|
188
|
+
const what = spec.overhang ? `printed ${way}` : spec.mill ? `milled from ${way}` : two ? `cast in two halves along ${axis}` : `cast, drawn off ${way}`;
|
|
189
|
+
const role = spec.overhang ? 'the build direction' : spec.mill ? "the tool's side" : 'the draw';
|
|
190
|
+
const middle = two && 2 * way.parting === n[ax], about = dim === 2 ? 'z' : sym.axis || 'z', across = [...'xyz'].filter((a) => a !== about && names.includes(a)).includes(axis);
|
|
191
|
+
const fix = two ? `, or part it at the grid's middle (layer ${n[ax] / 2} of ${n[ax]})` : '';
|
|
192
|
+
if ((sym.mirror || '').includes(axis) && !middle) throw new Error(`symmetry: ${what}, a part can't be the same mirrored across ${axis} (${role}): mirror across the other axes${fix}`);
|
|
193
|
+
const instead = dim === 2 ? `mirror across ${names.replace(axis, '')} instead` : `turn about ${axis}`;
|
|
194
|
+
if (sym.turns === 2 && across && !middle) throw new Error(`symmetry: ${what}, a half turn about ${about} turns ${role} round to face the other way: ${instead}${fix}`);
|
|
195
|
+
if (sym.turns === 4 && across) throw new Error(`symmetry: ${what}, a quarter turn about ${about} moves ${role} onto another axis: ${instead}`);
|
|
196
|
+
return spec;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// A casting's draw on the grid: one way ('+z'), or two halves from a parting plane, its mm as a layer
|
|
200
|
+
function castOn(c, lo, n, h, space) {
|
|
201
|
+
if (typeof c === 'string') return c;
|
|
202
|
+
const ax = 'xyz'.indexOf(c.draw), b = space.map(bounds), dim = n.length;
|
|
203
|
+
if (ax < 0 || ax >= dim || c.draw.length !== 1) throw new Error(`cast: the draw axis is ${'xyz'.slice(0, dim).split('').join(', ')} (got ${c.draw})`);
|
|
204
|
+
const at = c.parting ?? (Math.min(...b.map((x) => x[ax])) + Math.max(...b.map((x) => x[dim + ax]))) / 2, layer = Math.round((at - lo[ax]) / h);
|
|
205
|
+
if (!(layer >= 0 && layer <= n[ax])) throw new Error(`cast: the parting plane at ${fmt(at)} mm is off the grid along ${c.draw} (${fmt(lo[ax])} to ${fmt(lo[ax] + n[ax] * h)} mm)`);
|
|
206
|
+
return { draw: c.draw, parting: layer, at: lo[ax] + layer * h };
|
|
162
207
|
}
|
|
163
208
|
|
|
164
209
|
/**
|
|
@@ -321,6 +366,112 @@ export function overhangFilter({ nx, ny, nz = 1 }, up, reach = 0) {
|
|
|
321
366
|
function x_of(e) { return lastX[e]; }
|
|
322
367
|
}
|
|
323
368
|
|
|
369
|
+
/**
|
|
370
|
+
* Ties cells that must be alike: mirror images across the grid's middle planes (mirror: 'x', 'xy', …), the copies of
|
|
371
|
+
* a turn about its middle (turns 2 or 4, about axis: z by default, the 2D grid's own), and every cell along an axis
|
|
372
|
+
* (extrude: 'x', 'y' or 'z', a constant section along it). Each design density is replaced by the average over its
|
|
373
|
+
* group's free cells (passive marks the cells that are not free: nonzero), so the design is the same in every copy.
|
|
374
|
+
* The average is its own adjoint (the matrix is symmetric), so adjoint is apply. Null when nothing is tied.
|
|
375
|
+
* @param {{ nx: number, ny: number, nz?: number }} size
|
|
376
|
+
* @param {{ mirror?: string, turns?: number, axis?: string, extrude?: string }} o @param {Int8Array} [passive]
|
|
377
|
+
*/
|
|
378
|
+
export function tieFilter({ nx, ny, nz = 1 }, { mirror = '', turns = 1, axis, extrude } = {}, passive) {
|
|
379
|
+
const n = nz > 1 ? [nx, ny, nz] : [nx, ny], dim = n.length, cells = n.reduce((p, c) => p * c, 1), names = 'xyz'.slice(0, dim);
|
|
380
|
+
const bad = [...mirror].find((a) => !names.includes(a));
|
|
381
|
+
if (bad) throw new Error(`symmetry: mirror across ${names.split('').join(', ')} (got "${mirror}")`);
|
|
382
|
+
if (![1, 2, 4].includes(turns)) throw new Error(`symmetry: turns is 2 or 4, the turns a grid of cubes maps onto itself by (got ${turns})`);
|
|
383
|
+
if (extrude != null && !names.includes(extrude)) throw new Error(`extrude: along ${names.split('').join(', ')} (got "${extrude}")`);
|
|
384
|
+
const about = dim === 2 ? 'z' : axis || 'z';
|
|
385
|
+
if (!'xyz'.includes(about) || about.length !== 1) throw new Error(`symmetry: turns about x, y or z (got "${axis}")`);
|
|
386
|
+
// The plane a turn is in, and a quarter turn needs it square
|
|
387
|
+
const [p, q] = [0, 1, 2].filter((a) => a < dim && a !== 'xyz'.indexOf(about));
|
|
388
|
+
if (turns === 4 && n[p] !== n[q]) throw new Error(`symmetry: a quarter turn about ${about} needs a square grid across it (${n[p]} by ${n[q]} cells): make the space as wide both ways`);
|
|
389
|
+
if (!mirror && turns === 1 && extrude == null) return null;
|
|
390
|
+
const parent = Int32Array.from({ length: cells }, (_, e) => e), find = (e) => { while (parent[e] !== e) e = parent[e] = parent[parent[e]]; return e; };
|
|
391
|
+
const join = (a, b) => { const ra = find(a), rb = find(b); if (ra !== rb) parent[Math.max(ra, rb)] = Math.min(ra, rb); };
|
|
392
|
+
const stride = [1, nx, nx * ny], ix = (c) => c.reduce((s, v, a) => s + v * stride[a], 0);
|
|
393
|
+
for (let e = 0; e < cells; e++) {
|
|
394
|
+
const c = n.map((_, a) => Math.floor(e / stride[a]) % n[a]);
|
|
395
|
+
for (const a of mirror) { const m = [...c]; m[names.indexOf(a)] = n[names.indexOf(a)] - 1 - c[names.indexOf(a)]; join(e, ix(m)); }
|
|
396
|
+
if (turns === 2) { const m = [...c]; m[p] = n[p] - 1 - c[p]; m[q] = n[q] - 1 - c[q]; join(e, ix(m)); }
|
|
397
|
+
if (turns === 4) { const m = [...c]; m[p] = n[q] - 1 - c[q]; m[q] = c[p]; join(e, ix(m)); }
|
|
398
|
+
if (extrude != null) { const a = names.indexOf(extrude); if (c[a] > 0) { const m = [...c]; m[a] = 0; join(e, ix(m)); } }
|
|
399
|
+
}
|
|
400
|
+
// Each group's free cells, as flat arrays (group g's are at start[g] to start[g + 1])
|
|
401
|
+
const groups = new Map();
|
|
402
|
+
for (let e = 0; e < cells; e++) if (!passive || !passive[e]) { const r = find(e); if (!groups.has(r)) groups.set(r, []); groups.get(r).push(e); }
|
|
403
|
+
const lists = [...groups.values()], start = new Int32Array(lists.length + 1), members = Int32Array.from(lists.flat());
|
|
404
|
+
lists.forEach((l, g) => { start[g + 1] = start[g] + l.length; });
|
|
405
|
+
const apply = (x, out) => {
|
|
406
|
+
if (out !== x) out.set(x);
|
|
407
|
+
for (let g = 0; g < lists.length; g++) {
|
|
408
|
+
let s = 0;
|
|
409
|
+
for (let k = start[g]; k < start[g + 1]; k++) s += x[members[k]];
|
|
410
|
+
s /= start[g + 1] - start[g];
|
|
411
|
+
for (let k = start[g]; k < start[g + 1]; k++) out[members[k]] = s;
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
};
|
|
415
|
+
return { apply, adjoint: apply, groups: lists.length };
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* No undercuts from a direction: a tool coming in from one side (milling, from up: '+z' cuts down from above) or a
|
|
420
|
+
* mould drawn off that way (casting) reaches a cell only through the cells between it and that side, so a cell is
|
|
421
|
+
* at least as solid as every cell on its way out. Each cell's density is the smooth maximum of its own and the one
|
|
422
|
+
* before it on that way (a chain along each line, smooth so it has a gradient: Gersborg and Andreassen 2011 ask the
|
|
423
|
+
* same of a casting by a sum along the line). With parting, a mould in two halves: the cells from the parting layer
|
|
424
|
+
* on (towards +) are drawn off one way, those before it the other. apply gives the densities the process can make;
|
|
425
|
+
* adjoint carries sensitivities back through it.
|
|
426
|
+
* @param {{ nx: number, ny: number, nz?: number }} size @param {string} from the side the tool comes from, or (with
|
|
427
|
+
* parting) the draw axis, 'z' @param {number} [parting] with a two-part mould, the first layer of the + half (0 to n)
|
|
428
|
+
*/
|
|
429
|
+
export function drawFilter({ nx, ny, nz = 1 }, from, parting) {
|
|
430
|
+
const n = nz > 1 ? [nx, ny, nz] : [nx, ny], dim = n.length, ax = 'xyz'.indexOf(String(from).at(-1)), cells = n.reduce((p, c) => p * c, 1);
|
|
431
|
+
const two = parting != null, sign = from[0] === '-' ? -1 : 1;
|
|
432
|
+
if (ax < 0 || ax >= dim || (!two && !/^[+-]/.test(from))) throw new Error(`the tool's side is ${dim === 3 ? '+x, -x, +y, -y, +z or -z' : '+x, -x, +y or -y'}${two ? ', or with a parting plane the draw axis, x, y or z' : ''} (got ${from})`);
|
|
433
|
+
if (two && !(Number.isInteger(parting) && parting >= 0 && parting <= n[ax])) throw new Error(`the parting plane is a layer from 0 to ${n[ax]} (got ${parting})`);
|
|
434
|
+
const stride = [1, nx, nx * ny][ax], eps = 1e-6;
|
|
435
|
+
// Each line along the axis as a list of chains, each from the side its tool or mould half comes from inwards
|
|
436
|
+
const chains = [];
|
|
437
|
+
for (let e = 0; e < cells; e++) {
|
|
438
|
+
if (Math.floor(e / stride) % n[ax]) continue;
|
|
439
|
+
const line = Array.from({ length: n[ax] }, (_, k) => e + k * stride);
|
|
440
|
+
if (two) { chains.push(Int32Array.from(line.slice(parting).reverse()), Int32Array.from(line.slice(0, parting))); }
|
|
441
|
+
else chains.push(Int32Array.from(sign > 0 ? [...line].reverse() : line));
|
|
442
|
+
}
|
|
443
|
+
const slopeIn = new Float64Array(cells), slopePrev = new Float64Array(cells);
|
|
444
|
+
return {
|
|
445
|
+
apply(x, out) {
|
|
446
|
+
for (const c of chains) {
|
|
447
|
+
let v = 0;
|
|
448
|
+
for (let k = 0; k < c.length; k++) {
|
|
449
|
+
const e = c[k], t = x[e];
|
|
450
|
+
if (k === 0) { v = t; slopeIn[e] = 1; slopePrev[e] = 0; out[e] = v; continue; }
|
|
451
|
+
const d = v - t, root = Math.sqrt(d * d + eps);
|
|
452
|
+
slopePrev[e] = 0.5 * (1 + d / root); slopeIn[e] = 0.5 * (1 - d / root);
|
|
453
|
+
// (never below the larger of the two, so a cell is never less solid than one on its way out, and at most
|
|
454
|
+
// 1: a full cell stays full; past it the chain is solid and nothing before it changes that)
|
|
455
|
+
v = 0.5 * (v + t + root);
|
|
456
|
+
if (v >= 1) { v = 1; slopePrev[e] = slopeIn[e] = 0; }
|
|
457
|
+
out[e] = v;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return out;
|
|
461
|
+
},
|
|
462
|
+
adjoint(g, out) {
|
|
463
|
+
for (const c of chains) {
|
|
464
|
+
let carry = 0; // what the cells further in ask of this one's output
|
|
465
|
+
for (let k = c.length - 1; k >= 0; k--) {
|
|
466
|
+
const e = c[k], total = g[e] + carry;
|
|
467
|
+
out[e] = total * slopeIn[e]; carry = total * slopePrev[e];
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return out;
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
|
|
324
475
|
/**
|
|
325
476
|
* The problem as a function of the design densities x (one per cell, 0 to 1), at projection sharpness beta:
|
|
326
477
|
* physical(x, beta) gives the densities the part is made of (filtered, printable, projected, kept and avoided cells
|
|
@@ -334,10 +485,14 @@ const SUPPORT = 10; // what a kept cell nothing holds up costs, in the part's co
|
|
|
334
485
|
const ERODE = 0.7, DILATE = 0.3, DILATE_EVERY = 10; // a mechanism's robust formulation: the eroded and dilated copies' thresholds, and how often the dilated volume target follows
|
|
335
486
|
|
|
336
487
|
export function model(spec, { method, backend = jsBackend } = {}) {
|
|
488
|
+
clash(spec);
|
|
337
489
|
const g = makeGrid(spec.grid), dim = g.dim, cells = g.cells, m = g.cellDofs, ke = cellStiffness(spec.material.nu, dim);
|
|
338
490
|
const { passive, penal } = spec, E = spec.material.E, Emin = E * 1e-9, ETA = 0.5, size = dim === 3 ? g.h : spec.thickness;
|
|
339
491
|
const solver = method || (dim === 3 ? 'mg' : 'direct'), tol = solver === 'direct' ? undefined : 1e-6;
|
|
340
492
|
const filter = densityFilter(spec.grid, spec.filter / spec.grid.h), print = spec.overhang ? overhangFilter(spec.grid, spec.overhang, spec.bridge ? Math.floor(spec.bridge / 2 / spec.grid.h) : 0) : null;
|
|
493
|
+
// Milled or cast: no undercuts from the tool's side or the mould's draw; symmetric or extruded: cells tied together
|
|
494
|
+
const draw = spec.mill ? drawFilter(spec.grid, spec.mill) : spec.cast ? (typeof spec.cast === 'string' ? drawFilter(spec.grid, spec.cast) : drawFilter(spec.grid, spec.cast.draw, spec.cast.parting)) : null;
|
|
495
|
+
const tie = spec.symmetry || spec.extrude ? tieFilter(spec.grid, { ...spec.symmetry, extrude: spec.extrude }, passive) : null, tied = new Float64Array(cells);
|
|
341
496
|
const tilde = new Float64Array(cells), made = new Float64Array(cells), phys = new Float64Array(cells), scale = new Float64Array(cells), back = new Float64Array(cells);
|
|
342
497
|
const fixedValue = (p) => (p === 1 ? 1 : 0);
|
|
343
498
|
// Heaviside projection about eta: steeper as beta grows, so densities are pushed to 0 or 1 (Wang, Lazarov, Sigmund 2011)
|
|
@@ -349,9 +504,9 @@ export function model(spec, { method, backend = jsBackend } = {}) {
|
|
|
349
504
|
// it, an avoided cell holds up nothing, and a kept cell is printed (made) only as far as what's below holds it up,
|
|
350
505
|
// which is what the kept regions' support cost (below) is measured on. The part is analysed with every kept cell solid.
|
|
351
506
|
const physical = (x, beta, eta = ETA) => {
|
|
352
|
-
filter.apply(x, tilde);
|
|
507
|
+
filter.apply(tie ? tie.apply(x, tied) : x, tilde);
|
|
353
508
|
for (let e = 0; e < cells; e++) if (passive[e]) tilde[e] = fixedValue(passive[e]);
|
|
354
|
-
if (print) print.apply(tilde, made); else made.set(tilde);
|
|
509
|
+
if (print) print.apply(tilde, made); else if (draw) draw.apply(tilde, made); else made.set(tilde);
|
|
355
510
|
for (let e = 0; e < cells; e++) phys[e] = passive[e] ? fixedValue(passive[e]) : project(made[e], beta, eta);
|
|
356
511
|
return phys;
|
|
357
512
|
};
|
|
@@ -372,9 +527,10 @@ export function model(spec, { method, backend = jsBackend } = {}) {
|
|
|
372
527
|
// Back through the overhang filter and the density filter to the design densities (kept and avoided cells don't
|
|
373
528
|
// change: their own densities are fixed). Right after the physical() it belongs to.
|
|
374
529
|
const toDesign = (d) => {
|
|
375
|
-
if (print) { print.adjoint(d, back); d.set(back); }
|
|
530
|
+
if (print) { print.adjoint(d, back); d.set(back); } else if (draw) { draw.adjoint(d, back); d.set(back); }
|
|
376
531
|
for (let e = 0; e < cells; e++) if (passive[e]) d[e] = 0;
|
|
377
532
|
filter.adjoint(d, back); d.set(back);
|
|
533
|
+
if (tie) { tie.adjoint(d, back); d.set(back); }
|
|
378
534
|
};
|
|
379
535
|
let last = null;
|
|
380
536
|
// Each step solves only the load cases it optimises for (weight above 0); every case is solved when all is set
|
|
@@ -846,6 +1002,69 @@ export function bounds(r) {
|
|
|
846
1002
|
}
|
|
847
1003
|
|
|
848
1004
|
// ---------- shapes from results ----------
|
|
1005
|
+
|
|
1006
|
+
/**
|
|
1007
|
+
* Where a result has undercuts: empty cells (below iso) with a solid one between them and the tool's side, along the
|
|
1008
|
+
* line to it (or, with parting, to their half's side). What drawFilter prevents, measured on densities as made.
|
|
1009
|
+
* @param {{ nx: number, ny: number, nz?: number }} size @param {ArrayLike<number>} density
|
|
1010
|
+
* @param {{ from: string, parting?: number, iso?: number }} o
|
|
1011
|
+
* @returns {{ count: number, cells: number[] }} the hidden cells (indices)
|
|
1012
|
+
*/
|
|
1013
|
+
export function undercuts({ nx, ny, nz = 1 }, density, { from, parting, iso = 0.5 }) {
|
|
1014
|
+
const f = drawFilter({ nx, ny, nz }, from, parting), solid = Float64Array.from(density, (d) => (d >= iso ? 1 : 0)), made = f.apply(solid, new Float64Array(solid.length));
|
|
1015
|
+
const cells = [];
|
|
1016
|
+
for (let e = 0; e < solid.length; e++) if (!solid[e] && made[e] >= 0.5) cells.push(e);
|
|
1017
|
+
return { count: cells.length, cells };
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* How grey a result is: the share of its design cells (free, inside the space) that are neither empty nor solid,
|
|
1022
|
+
* between a tenth and nine tenths. A clear design has a few per cent, on its edges; much more and the outline drawn
|
|
1023
|
+
* at half density is a guess at a part, not the part. A process or a tie that leaves no clear part (a 2D section
|
|
1024
|
+
* milled from its side: a grey column bends as stiffly as a solid one of its share's height) shows as grey.
|
|
1025
|
+
* @param {ArrayLike<number>} density @param {ArrayLike<number>} [passive] nonzero for cells that are not free
|
|
1026
|
+
* @returns {number} 0 to 1
|
|
1027
|
+
*/
|
|
1028
|
+
export function grey(density, passive) {
|
|
1029
|
+
let free = 0, mid = 0;
|
|
1030
|
+
for (let e = 0; e < density.length; e++) if (!passive || !passive[e]) { free++; if (density[e] > 0.1 && density[e] < 0.9) mid++; }
|
|
1031
|
+
return free ? mid / free : 0;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* Enclosed voids: empty cells (below iso) that no path of empty cells, face to face, joins to the grid's edge. Powder
|
|
1036
|
+
* left in them can't be got out of a powder bed print, nor a core out of a casting. Each void's cells, its volume
|
|
1037
|
+
* (mm³, or mm² in 2D) and its centre (mm, from origin).
|
|
1038
|
+
* @param {{ nx: number, ny: number, nz?: number, h: number }} size @param {ArrayLike<number>} density
|
|
1039
|
+
* @param {{ iso?: number, origin?: number[] }} [o]
|
|
1040
|
+
* @returns {{ cells: number[], volume: number, at: number[] }[]}
|
|
1041
|
+
*/
|
|
1042
|
+
export function voids({ nx, ny, nz = 1, h }, density, { iso = 0.5, origin } = {}) {
|
|
1043
|
+
const n = nz > 1 ? [nx, ny, nz] : [nx, ny], dim = n.length, cells = density.length, stride = [1, nx, nx * ny];
|
|
1044
|
+
const seen = new Uint8Array(cells), coord = (e, a) => Math.floor(e / stride[a]) % n[a], empty = (e) => density[e] < iso;
|
|
1045
|
+
const flood = (seed) => {
|
|
1046
|
+
const list = [seed]; seen[seed] = 1;
|
|
1047
|
+
for (let i = 0; i < list.length; i++) {
|
|
1048
|
+
const e = list[i];
|
|
1049
|
+
for (let a = 0; a < dim; a++) for (const s of [-1, 1]) {
|
|
1050
|
+
const c = coord(e, a) + s;
|
|
1051
|
+
if (c < 0 || c >= n[a]) continue;
|
|
1052
|
+
const f = e + s * stride[a];
|
|
1053
|
+
if (!seen[f] && empty(f)) { seen[f] = 1; list.push(f); }
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
return list;
|
|
1057
|
+
};
|
|
1058
|
+
// Everything empty that reaches the edge first; what's left empty is enclosed
|
|
1059
|
+
for (let e = 0; e < cells; e++) if (!seen[e] && empty(e) && n.some((m, a) => coord(e, a) === 0 || coord(e, a) === m - 1)) flood(e);
|
|
1060
|
+
const out = [], o = origin || new Array(dim).fill(0);
|
|
1061
|
+
for (let e = 0; e < cells; e++) if (!seen[e] && empty(e)) {
|
|
1062
|
+
const list = flood(e), at = n.map((_, a) => o[a] + h * (list.reduce((s, c) => s + coord(c, a), 0) / list.length + 0.5));
|
|
1063
|
+
out.push({ cells: list, volume: list.length * h ** dim, at });
|
|
1064
|
+
}
|
|
1065
|
+
return out;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
849
1068
|
// (Everything above decides the densities; everything below only draws them, so results can be kept by the code above)
|
|
850
1069
|
|
|
851
1070
|
/**
|