threers 0.0.4

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Timoshenko-style 3D frame model for cuboct lattices (Jenett et al. 2020).
//!
//! Builds a beam network from the same segment generator as [`super::cuboct`],
//! assembles axial + weak bending stiffness, and runs quasi-static compression
//! tests for effective modulus, Poisson ratio, and chiral twist per strain.

use super::cuboct::{cell_segments, ChiralRule, Cuboct};
use std::collections::HashMap;

/// Material and section properties for the reduced-order frame model.
#[derive(Clone, Copy, Debug)]
pub struct FrameMaterial {
    /// Young's modulus (same units as stress output).
    pub e: f64,
    /// Beam cross-section area.
    pub a: f64,
    /// Second moment (used for weak bending regularisation).
    pub i: f64,
}

impl Default for FrameMaterial {
    fn default() -> Self {
        Self {
            e: 1.0,
            a: 1.0,
            i: 0.01,
        }
    }
}

/// Quasi-static test results for a cuboct specimen.
#[derive(Clone, Copy, Debug, Default)]
pub struct FrameResponse {
    /// Effective axial stiffness F/δ.
    pub stiffness: f64,
    /// Effective modulus E* = stiffness * H / A.
    pub effective_modulus: f64,
    /// ν = ε_trans / ε_axial (negative ⇒ auxetic).
    pub poisson: f64,
    /// Degrees twist per unit axial strain (chiral columns).
    pub twist_per_strain: f64,
}

/// Frame mesh extracted from a periodic cuboct lattice block.
pub struct CuboctFrame {
    nodes: Vec<[f64; 3]>,
    /// `(node_a, node_b, length)`.
    elements: Vec<(usize, usize, f64)>,
    pitch: f64,
    cells: [usize; 3],
    mat: FrameMaterial,
}

impl CuboctFrame {
    /// Build a frame model for `cells` voxels of `kind` at `pitch`.
    pub fn new(cells: [usize; 3], kind: Cuboct, pitch: f32, shape: f32) -> Self {
        Self::with_program(cells, pitch, shape, None, |_, _, _| kind)
    }

    /// Per-cell programming, same convention as [`super::CuboctAssembly::program`].
    pub fn with_program(
        cells: [usize; 3],
        pitch: f32,
        shape: f32,
        rule: Option<ChiralRule>,
        mut kind_at: impl FnMut(i32, i32, i32) -> Cuboct,
    ) -> Self {
        let pitch = pitch as f64;
        let [nx, ny, nz] = cells;
        let n = nx as i32;
        let weld = pitch * 1e-4;
        let mut nodes: Vec<[f64; 3]> = Vec::new();
        let mut elements = Vec::new();
        let mut index = HashMap::new();

        let mut node_id = |p: [f64; 3]| -> usize {
            for (i, q) in nodes.iter().enumerate() {
                let d = [(p[0] - q[0]).abs(), (p[1] - q[1]).abs(), (p[2] - q[2]).abs()];
                if d[0] < weld && d[1] < weld && d[2] < weld {
                    return i;
                }
            }
            let id = nodes.len();
            nodes.push(p);
            index.insert(
                (
                    (p[0] / weld).round() as i64,
                    (p[1] / weld).round() as i64,
                    (p[2] / weld).round() as i64,
                ),
                id,
            );
            id
        };

        let origin = |ix: i32, iy: i32, iz: i32| {
            [
                (ix as f64 - nx as f64 * 0.5) * pitch,
                (iy as f64 - ny as f64 * 0.5) * pitch,
                (iz as f64 - nz as f64 * 0.5) * pitch,
            ]
        };

        for iz in 0..nz as i32 {
            for iy in 0..ny as i32 {
                for ix in 0..nx as i32 {
                    let kind = kind_at(ix, iy, iz);
                    let o = origin(ix, iy, iz);
                    let segs = cell_segments(kind, shape, ix, iy, iz, rule, n);
                    for seg in segs {
                        let a = [
                            o[0] + seg[0][0] as f64 * pitch,
                            o[1] + seg[0][1] as f64 * pitch,
                            o[2] + seg[0][2] as f64 * pitch,
                        ];
                        let b = [
                            o[0] + seg[1][0] as f64 * pitch,
                            o[1] + seg[1][1] as f64 * pitch,
                            o[2] + seg[1][2] as f64 * pitch,
                        ];
                        let ia = node_id(a);
                        let ib = node_id(b);
                        if ia == ib {
                            continue;
                        }
                        let len = ((b[0] - a[0]).powi(2) + (b[1] - a[1]).powi(2) + (b[2] - a[2]).powi(2))
                            .sqrt()
                            .max(weld);
                        elements.push((ia, ib, len));
                    }
                }
            }
        }

        Self {
            nodes,
            elements,
            pitch,
            cells,
            mat: FrameMaterial::default(),
        }
    }

    pub fn material(mut self, mat: FrameMaterial) -> Self {
        self.mat = mat;
        self
    }

    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    pub fn element_count(&self) -> usize {
        self.elements.len()
    }

    /// Uniaxial compression along +Z with strain `epsilon` (positive = compress).
    pub fn compress_z(&self, epsilon: f64) -> FrameResponse {
        let ndof = self.nodes.len() * 3;
        if ndof == 0 {
            return FrameResponse::default();
        }
        let mut k = vec![0.0f64; ndof * ndof];
        for &(a, b, len) in &self.elements {
            add_truss(&mut k, ndof, a, b, self.mat.e * self.mat.a / len, &self.nodes);
            let bend = self.mat.e * self.mat.i / len.powi(3);
            add_bending(&mut k, ndof, a, b, bend, &self.nodes);
        }

        let zmin = self
            .nodes
            .iter()
            .map(|p| p[2])
            .fold(f64::INFINITY, f64::min);
        let zmax = self
            .nodes
            .iter()
            .map(|p| p[2])
            .fold(f64::NEG_INFINITY, f64::max);
        let tol = self.pitch * 0.05;

        let mut fixed = vec![false; ndof];
        for (i, p) in self.nodes.iter().enumerate() {
            if (p[2] - zmin).abs() < tol {
                fixed[i * 3] = true;
                fixed[i * 3 + 1] = true;
                fixed[i * 3 + 2] = true;
            }
        }

        let height = zmax - zmin;
        let dz = -epsilon * height;
        let mut prescribed = vec![0.0f64; ndof];
        for (i, p) in self.nodes.iter().enumerate() {
            if (p[2] - zmax).abs() < tol {
                prescribed[i * 3 + 2] = dz;
                fixed[i * 3 + 2] = true;
            }
        }

        let u = solve_reduced(&k, &prescribed, &fixed);

        let area = (self.cells[0] as f64 * self.pitch) * (self.cells[1] as f64 * self.pitch);
        let reaction = reaction_z(&k, &u, ndof, zmin, tol, &self.nodes);
        let stiffness = reaction.abs() / dz.abs().max(1e-12);
        let effective_modulus = stiffness * height / area.max(1e-12);

        let cx = self.nodes.iter().map(|p| p[0]).sum::<f64>() / self.nodes.len() as f64;
        let cy = self.nodes.iter().map(|p| p[1]).sum::<f64>() / self.nodes.len() as f64;
        let mut lat0 = 0.0;
        let mut lat1 = 0.0;
        let mut n0 = 0usize;
        let mut n1 = 0usize;
        for (i, p) in self.nodes.iter().enumerate() {
            let ux = u[i * 3];
            let uy = u[i * 3 + 1];
            let r = ((p[0] + ux - cx).powi(2) + (p[1] + uy - cy).powi(2)).sqrt();
            if (p[2] - zmin).abs() < tol {
                lat0 += r;
                n0 += 1;
            } else if (p[2] - zmax).abs() < tol {
                lat1 += r;
                n1 += 1;
            }
        }
        let eps_ax = dz / height;
        let r0 = lat0 / n0.max(1) as f64;
        let r1 = lat1 / n1.max(1) as f64;
        let eps_lat = (r1 - r0) / (self.pitch * 0.5).max(1e-12);
        let poisson = if eps_ax.abs() > 1e-9 {
            eps_lat / eps_ax
        } else {
            0.0
        };

        let mut twist = 0.0;
        let mut twist_n = 0usize;
        let cx = self.nodes.iter().map(|p| p[0]).sum::<f64>() / self.nodes.len() as f64;
        let cy = self.nodes.iter().map(|p| p[1]).sum::<f64>() / self.nodes.len() as f64;
        for (i, p) in self.nodes.iter().enumerate() {
            if (p[2] - zmax).abs() < tol {
                let rx = p[0] - cx;
                let ry = p[1] - cy;
                let r = (rx * rx + ry * ry).sqrt();
                if r > self.pitch * 0.05 {
                    let ux = u[i * 3];
                    let uy = u[i * 3 + 1];
                    twist += (rx * uy - ry * ux) / (r * r);
                    twist_n += 1;
                }
            }
        }
        let twist_per_strain = if twist_n > 0 && epsilon.abs() > 1e-9 {
            twist / twist_n as f64 / epsilon
        } else {
            0.0
        };

        FrameResponse {
            stiffness,
            effective_modulus,
            poisson,
            twist_per_strain,
        }
    }
}

fn add_truss(k: &mut [f64], ndof: usize, a: usize, b: usize, ea_l: f64, nodes: &[[f64; 3]]) {
    let mut c = [0.0; 3];
    let pa = nodes[a];
    let pb = nodes[b];
    let len = ((pb[0] - pa[0]).powi(2) + (pb[1] - pa[1]).powi(2) + (pb[2] - pa[2]).powi(2))
        .sqrt()
        .max(1e-12);
    for d in 0..3 {
        c[d] = (pb[d] - pa[d]) / len;
    }
    let idx = [a * 3, a * 3 + 1, a * 3 + 2, b * 3, b * 3 + 1, b * 3 + 2];
    for i in 0..6 {
        for j in 0..6 {
            let ci = c[i % 3];
            let cj = c[j % 3];
            let sign = if (i < 3) == (j < 3) { 1.0 } else { -1.0 };
            add_k(k, ndof, idx[i], idx[j], ea_l * ci * cj * sign);
        }
    }
}

fn add_bending(k: &mut [f64], ndof: usize, a: usize, b: usize, ei_l3: f64, _nodes: &[[f64; 3]]) {
    for d in 0..3 {
        let ia = a * 3 + d;
        let ib = b * 3 + d;
        add_k(k, ndof, ia, ia, ei_l3);
        add_k(k, ndof, ib, ib, ei_l3);
        add_k(k, ndof, ia, ib, -ei_l3);
        add_k(k, ndof, ib, ia, -ei_l3);
    }
}

fn add_k(k: &mut [f64], n: usize, i: usize, j: usize, v: f64) {
    k[i * n + j] += v;
}

fn solve_reduced(k: &[f64], prescribed: &[f64], fixed: &[bool]) -> Vec<f64> {
    let n = prescribed.len();
    let free: Vec<usize> = (0..n).filter(|&i| !fixed[i]).collect();
    let m = free.len();
    let mut u = vec![0.0; n];
    if m == 0 {
        for i in 0..n {
            if fixed[i] {
                u[i] = prescribed[i];
            }
        }
        return u;
    }
    let mut kff = vec![0.0; m * m];
    let mut ff = vec![0.0; m];
    for (ii, &i) in free.iter().enumerate() {
        for j in 0..n {
            if fixed[j] {
                ff[ii] -= k[i * n + j] * prescribed[j];
            }
        }
        for (jj, &j) in free.iter().enumerate() {
            kff[ii * m + jj] = k[i * n + j];
        }
    }
    gauss_solve(&mut kff, &mut ff, m);
    for (ii, &i) in free.iter().enumerate() {
        u[i] = ff[ii];
    }
    for i in 0..n {
        if fixed[i] {
            u[i] = prescribed[i];
        }
    }
    u
}

fn gauss_solve(a: &mut [f64], b: &mut [f64], n: usize) {
    for col in 0..n {
        let mut piv = col;
        let mut best = a[col * n + col].abs();
        for row in col + 1..n {
            let v = a[row * n + col].abs();
            if v > best {
                best = v;
                piv = row;
            }
        }
        if best < 1e-14 {
            continue;
        }
        if piv != col {
            for k in 0..n {
                a.swap(col * n + k, piv * n + k);
            }
            b.swap(col, piv);
        }
        let diag = a[col * n + col];
        for k in col..n {
            a[col * n + k] /= diag;
        }
        b[col] /= diag;
        for row in 0..n {
            if row == col {
                continue;
            }
            let f = a[row * n + col];
            if f.abs() < 1e-18 {
                continue;
            }
            for k in col..n {
                a[row * n + k] -= f * a[col * n + k];
            }
            b[row] -= f * b[col];
        }
    }
}

fn reaction_z(
    k: &[f64],
    u: &[f64],
    ndof: usize,
    zmin: f64,
    tol: f64,
    nodes: &[[f64; 3]],
) -> f64 {
    let mut fz = 0.0;
    for (i, p) in nodes.iter().enumerate() {
        if (p[2] - zmin).abs() < tol {
            for j in 0..ndof {
                fz += k[(i * 3 + 2) * ndof + j] * u[j];
            }
        }
    }
    fz
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rigid_modulus_rises_with_cell_count() {
        let e1 = CuboctFrame::new([1, 1, 1], Cuboct::Rigid, 1.0, 0.0)
            .compress_z(0.01)
            .effective_modulus;
        let e2 = CuboctFrame::new([2, 2, 2], Cuboct::Rigid, 1.0, 0.0)
            .compress_z(0.01)
            .effective_modulus;
        assert!(e1 > 0.0 && e2 > e1, "E*1={e1} E*2={e2}");
    }

    #[test]
    fn compliant_modulus_near_constant_across_n() {
        let e1 = CuboctFrame::new([1, 1, 1], Cuboct::Compliant, 1.0, 0.15)
            .compress_z(0.02)
            .effective_modulus;
        let e2 = CuboctFrame::new([2, 2, 2], Cuboct::Compliant, 1.0, 0.15)
            .compress_z(0.02)
            .effective_modulus;
        assert!(
            (e1 - e2).abs() / e1.max(1e-9) < 0.5,
            "compliant E* drifted: {e1} vs {e2}"
        );
    }

    #[test]
    fn auxetic_negative_poisson() {
        let rigid = CuboctFrame::new([2, 2, 2], Cuboct::Rigid, 1.0, 0.0).compress_z(0.05);
        let aux = CuboctFrame::new([2, 2, 2], Cuboct::Auxetic, 1.0, 0.2).compress_z(0.05);
        assert!(
            aux.poisson < rigid.poisson,
            "auxetic nu={} should be below rigid nu={}",
            aux.poisson,
            rigid.poisson
        );
    }

    #[test]
    fn chiral_column_twists() {
        let r = CuboctFrame::with_program([1, 1, 4], 1.0, 0.12, Some(ChiralRule::R1), |_, _, k| {
            Cuboct::column_half(k, 4)
        })
        .compress_z(0.05);
        assert!(r.twist_per_strain.abs() > 5e-4, "twist = {}", r.twist_per_strain);
    }
}