threers 0.1.0

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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! Marching cubes over a sampled scalar field, welded and watertight.
//!
//! The textbook implementation drives a 256-entry triangle table transcribed by
//! hand. This one derives that table on first use by contouring each cube face
//! and chaining the segments into loops, which buys two things the transcribed
//! table does not:
//!
//! - **Ambiguous faces cannot crack.** A face with four sign changes can be
//!   contoured two ways. The rule here reads only the face's four corner signs,
//!   and the two cells sharing that face see the same four signs, so they always
//!   pick the same pairing and their surfaces meet edge to edge.
//! - **Winding falls out of the construction.** Face segments are directed —
//!   walking a face counter-clockwise from outside, each crossing *into* the
//!   solid runs to the next crossing *out* of it — so chaining them yields
//!   loops that are already wound counter-clockwise seen from outside the
//!   surface, and no triangle needs a normal test to orient it.
//!
//! Vertices are keyed by the grid edge they sit on, so neighbouring cells share
//! them: the output is indexed and, for a field that is negative all around the
//! sampled box, closed.

use crate::core::{BufferAttribute, BufferGeometry};
use crate::extras::marching_cubes::CROSSING_CLEARANCE;
use crate::math::Vector3;
use std::sync::OnceLock;

/// Corner `c` of a cell sits at `(c & 1, (c >> 1) & 1, (c >> 2) & 1)`.
/// Edges are grouped by axis, so `edge / 4` is 0 for x, 1 for y, 2 for z.
const EDGES: [(u8, u8); 12] = [
    (0, 1),
    (2, 3),
    (4, 5),
    (6, 7),
    (0, 2),
    (1, 3),
    (4, 6),
    (5, 7),
    (0, 4),
    (1, 5),
    (2, 6),
    (3, 7),
];

/// The six faces, corners counter-clockwise as seen from *outside* the cell.
/// Adjacent faces therefore traverse a shared edge in opposite directions,
/// which is what makes every cut edge exactly one segment start and one end.
const FACES: [[u8; 4]; 6] = [
    [0, 4, 6, 2], // -x
    [1, 3, 7, 5], // +x
    [0, 1, 5, 4], // -y
    [2, 6, 7, 3], // +y
    [0, 2, 3, 1], // -z
    [4, 5, 7, 6], // +z
];

const fn edge_index(a: u8, b: u8) -> u8 {
    let (lo, hi) = if a < b { (a, b) } else { (b, a) };
    let mut e = 0;
    while e < 12 {
        if EDGES[e].0 == lo && EDGES[e].1 == hi {
            return e as u8;
        }
        e += 1;
    }
    panic!("corners are not a cube edge");
}

/// The oriented contour loops for one of the 256 corner sign patterns, as cell
/// edge indices. Bit `c` of `case` is set when corner `c` is inside.
fn loops_for(case: u8) -> Vec<Vec<u8>> {
    let inside = |c: u8| case & (1 << c) != 0;

    // `next[e]` — the cut edge the contour leaves for after entering on `e`.
    // Each cut edge starts a segment on one of its two faces and ends one on
    // the other, so this is a permutation of the cut edges.
    let mut next = [u8::MAX; 12];
    for face in FACES {
        let mut kind = [0i8; 4];
        for i in 0..4 {
            let (a, b) = (face[i], face[(i + 1) % 4]);
            kind[i] = match (inside(a), inside(b)) {
                (true, false) => 1,  // leaving the solid
                (false, true) => -1, // entering it
                _ => 0,
            };
        }
        // Signs alternate around a closed face, so each "entering" crossing is
        // matched with the next "leaving" one. On a four-crossing face that
        // choice is the ambiguity, and it depends only on the corner signs.
        //
        // Entering-to-leaving, rather than the other way round, is what puts
        // the solid on the right of the contour and so makes the loops wind
        // counter-clockwise seen from outside the surface.
        for i in 0..4 {
            if kind[i] != -1 {
                continue;
            }
            for d in 1..4 {
                let j = (i + d) % 4;
                if kind[j] == 1 {
                    let from = edge_index(face[i], face[(i + 1) % 4]);
                    let to = edge_index(face[j], face[(j + 1) % 4]);
                    next[from as usize] = to;
                    break;
                }
            }
        }
    }

    let mut loops = Vec::new();
    let mut seen = [false; 12];
    for start in 0..12u8 {
        if next[start as usize] == u8::MAX || seen[start as usize] {
            continue;
        }
        let mut cycle = Vec::new();
        let mut e = start;
        while !seen[e as usize] {
            seen[e as usize] = true;
            cycle.push(e);
            e = next[e as usize];
        }
        if cycle.len() >= 3 {
            loops.push(cycle);
        }
    }
    loops
}

fn case_table() -> &'static [Vec<Vec<u8>>; 256] {
    static TABLE: OnceLock<[Vec<Vec<u8>>; 256]> = OnceLock::new();
    TABLE.get_or_init(|| std::array::from_fn(|case| loops_for(case as u8)))
}

/// A scalar field sampled on a regular grid, positive inside the solid.
pub(crate) struct IsoGrid {
    /// Sample counts per axis; at least 2 each.
    pub dims: [usize; 3],
    /// World position of sample `(0, 0, 0)`.
    pub origin: Vector3,
    /// World spacing between samples.
    pub step: Vector3,
    /// `dims.x * dims.y * dims.z` samples, x fastest.
    pub values: Vec<f32>,
}

/// One slab's share of a contour, before its seams are welded.
struct Slab {
    positions: Vec<f32>,
    normals: Vec<f32>,
    indices: Vec<u32>,
    /// Vertex per x- and y-edge of the slab's bottom plane, and of its top.
    bottom: Vec<u32>,
    top: Vec<u32>,
}

impl IsoGrid {
    fn at(&self, i: usize, j: usize, k: usize) -> f32 {
        self.values[(k * self.dims[1] + j) * self.dims[0] + i]
    }

    /// Central difference at a sample, one-sided on the boundary. Points into
    /// the solid, so the outward normal is its negation.
    fn gradient(&self, i: usize, j: usize, k: usize) -> Vector3 {
        let [nx, ny, nz] = self.dims;
        let (x0, x1) = (i.saturating_sub(1), (i + 1).min(nx - 1));
        let (y0, y1) = (j.saturating_sub(1), (j + 1).min(ny - 1));
        let (z0, z1) = (k.saturating_sub(1), (k + 1).min(nz - 1));
        Vector3::new(
            (self.at(x1, j, k) - self.at(x0, j, k)) / ((x1 - x0) as f32 * self.step.x),
            (self.at(i, y1, k) - self.at(i, y0, k)) / ((y1 - y0) as f32 * self.step.y),
            (self.at(i, j, z1) - self.at(i, j, z0)) / ((z1 - z0) as f32 * self.step.z),
        )
    }

    /// Triangulate the zero level set. UVs are a planar projection onto the
    /// plane the vertex normal faces most directly, normalised over the grid —
    /// enough for tiling detail maps, with a seam wherever that axis flips.
    /// Contour the cells of layers `k0..k1`, with a vertex table of their own.
    ///
    /// Besides the slab's triangles this hands back, per x- and y-edge of its
    /// bottom and top planes, the vertex it put there (or `u32::MAX`): the
    /// seams a neighbouring slab shares, which [`triangulate`](Self::triangulate)
    /// welds on.
    fn sweep(&self, k0: usize, k1: usize) -> Slab {
        let [nx, ny, _] = self.dims;
        let table = case_table();

        let mut positions: Vec<f32> = Vec::new();
        let mut normals: Vec<f32> = Vec::new();
        let mut indices: Vec<u32> = Vec::new();

        // Which vertex sits on each grid edge, so neighbouring cells share it.
        //
        // A cell only ever touches edges on its own z-layer and the one above,
        // so two layers of `nx × ny × 3` are enough and the table rolls forward
        // as the sweep does. Keyed by arithmetic rather than hashed: a hash map
        // over every crossed edge was the single largest cost in here, and it
        // was also the largest allocation — tens of megabytes of table for a
        // mesh whose *live* front is a couple of hundred kilobytes.
        let plane = nx * ny;
        let mut rows: [Vec<u32>; 2] = [vec![u32::MAX; plane * 3], vec![u32::MAX; plane * 3]];

        let mut corner = [0.0f32; 8];
        let mut cell_edge = [u32::MAX; 12];
        // Which of the four samples at (i, j|j+1, k|k+1) are inside, one
        // nibble per i: bit 0 is (j, k), 1 is (j+1, k), 2 is (j, k+1) and 3 is
        // (j+1, k+1). Two neighbouring nibbles are a cell's whole case, so each
        // sample is read once per row of cells instead of once per cell
        // touching it — and most cells, empty, are done with at that.
        let mut quad = vec![0u8; nx];
        // A nibble's bits spread to the even corners, where `(c >> 1) & 1` is y
        // and `c >> 2` is z; the odd corners are the same at i + 1.
        const SPREAD: [u8; 16] = {
            let mut t = [0u8; 16];
            let mut q = 0;
            while q < 16 {
                t[q] = ((q & 1) | (q & 2) << 1 | (q & 4) << 2 | (q & 8) << 3) as u8;
                q += 1;
            }
            t
        };
        let mut bottom = Vec::new();
        let mut top = Vec::new();
        for k in k0..k1 {
            for j in 0..ny - 1 {
                let row = |jj: usize, kk: usize| {
                    let start = (kk * ny + jj) * nx;
                    &self.values[start..start + nx]
                };
                let (r0, r1, r2, r3) = (row(j, k), row(j + 1, k), row(j, k + 1), row(j + 1, k + 1));
                let (mut any_in, mut any_out) = (false, false);
                for i in 0..nx {
                    let q = (r0[i] > 0.0) as u8
                        | ((r1[i] > 0.0) as u8) << 1
                        | ((r2[i] > 0.0) as u8) << 2
                        | ((r3[i] > 0.0) as u8) << 3;
                    any_in |= q != 0;
                    any_out |= q != 15;
                    quad[i] = q;
                }
                // All inside or all outside: no cell of the row is crossed.
                if !(any_in && any_out) {
                    continue;
                }
                for i in 0..nx - 1 {
                    let case = SPREAD[quad[i] as usize] | SPREAD[quad[i + 1] as usize] << 1;
                    let loops = &table[case as usize];
                    if loops.is_empty() {
                        continue;
                    }
                    for (c, slot) in corner.iter_mut().enumerate() {
                        *slot = self.at(i + (c & 1), j + ((c >> 1) & 1), k + ((c >> 2) & 1));
                    }

                    cell_edge.fill(u32::MAX);
                    for cycle in loops {
                        for &e in cycle {
                            let slot = &mut cell_edge[e as usize];
                            if *slot != u32::MAX {
                                continue;
                            }
                            let (a, b) = EDGES[e as usize];
                            let axis = (e / 4) as usize;
                            let (ai, aj, ak) = (
                                i + (a as usize & 1),
                                j + ((a as usize >> 1) & 1),
                                k + ((a as usize >> 2) & 1),
                            );
                            // x- and y-parallel edges sit on layer `k` or `k+1`;
                            // z-parallel ones start on `k`. Either way the
                            // difference is 0 or 1.
                            let row = &mut rows[ak - k][axis * plane + aj * nx + ai];
                            if *row != u32::MAX {
                                *slot = *row;
                                continue;
                            }
                            let (va, vb) = (corner[a as usize], corner[b as usize]);
                            let denom = va - vb;
                            // Off the ends, for the reason given on
                            // `CROSSING_CLEARANCE`: a clip or a trim can put a
                            // sample exactly on the surface.
                            let t = if denom.abs() > f32::EPSILON {
                                (va / denom).clamp(CROSSING_CLEARANCE, 1.0 - CROSSING_CLEARANCE)
                            } else {
                                0.5
                            };
                            let (bi, bj, bk) = (
                                i + (b as usize & 1),
                                j + ((b as usize >> 1) & 1),
                                k + ((b as usize >> 2) & 1),
                            );
                            let mut p = Vector3::new(
                                self.origin.x + ai as f32 * self.step.x,
                                self.origin.y + aj as f32 * self.step.y,
                                self.origin.z + ak as f32 * self.step.z,
                            );
                            match axis {
                                0 => p.x += t * self.step.x,
                                1 => p.y += t * self.step.y,
                                _ => p.z += t * self.step.z,
                            }
                            let ga = self.gradient(ai, aj, ak);
                            let gb = self.gradient(bi, bj, bk);
                            let n = (ga * (1.0 - t) + gb * t) * -1.0;
                            let n = if n.length_sq() > 0.0 {
                                n.normalize()
                            } else {
                                Vector3::new(0.0, 0.0, 1.0)
                            };
                            let index = (positions.len() / 3) as u32;
                            positions.extend_from_slice(&[p.x, p.y, p.z]);
                            normals.extend_from_slice(&[n.x, n.y, n.z]);
                            *row = index;
                            *slot = index;
                        }
                        if cycle.len() == 3 {
                            indices.extend_from_slice(&[
                                cell_edge[cycle[0] as usize],
                                cell_edge[cycle[1] as usize],
                                cell_edge[cycle[2] as usize],
                            ]);
                            continue;
                        }
                        // Fanning a bigger loop from one of its own vertices
                        // folds the triangles back on themselves when the loop
                        // saddles, which it does whenever the cell straddles a
                        // neck. A centre vertex follows the loop instead: the
                        // triangles stay on the same side and stay well shaped,
                        // and the loop's own edges — the ones the neighbouring
                        // cells have to meet — are untouched.
                        let hub = {
                            let inv = 1.0 / cycle.len() as f32;
                            let (mut p, mut n) = (Vector3::ZERO, Vector3::ZERO);
                            for &e in cycle {
                                let v = cell_edge[e as usize] as usize * 3;
                                p = p + Vector3::new(
                                    positions[v],
                                    positions[v + 1],
                                    positions[v + 2],
                                );
                                n = n + Vector3::new(normals[v], normals[v + 1], normals[v + 2]);
                            }
                            p = p * inv;
                            let n = if n.length_sq() > 0.0 {
                                n.normalize()
                            } else {
                                Vector3::new(0.0, 0.0, 1.0)
                            };
                            let index = (positions.len() / 3) as u32;
                            positions.extend_from_slice(&[p.x, p.y, p.z]);
                            normals.extend_from_slice(&[n.x, n.y, n.z]);
                            index
                        };
                        for w in 0..cycle.len() {
                            indices.extend_from_slice(&[
                                hub,
                                cell_edge[cycle[w] as usize],
                                cell_edge[cycle[(w + 1) % cycle.len()] as usize],
                            ]);
                        }
                    }
                }
            }
            // Roll the table forward. The upper layer's x- and y-edges become
            // the lower layer's, which is exactly what the next sweep needs;
            // its z-edges were never written, and the layer being retired is
            // cleared to take the new upper one.
            // The seams: this slab's first plane, as soon as its first layer
            // of cells has written it, and its last, before it rolls away.
            if k == k0 {
                bottom = rows[0][..2 * plane].to_vec();
            }
            if k + 1 == k1 {
                top = rows[1][..2 * plane].to_vec();
            }
            rows.swap(0, 1);
            rows[1].fill(u32::MAX);
        }
        Slab {
            positions,
            normals,
            indices,
            bottom,
            top,
        }
    }

    /// Triangulate the zero level set. UVs are a planar projection onto the
    /// plane the vertex normal faces most directly, normalised over the grid —
    /// enough for tiling detail maps, with a seam wherever that axis flips.
    ///
    /// With the `parallel` feature the grid is contoured in slabs of layers at
    /// once and welded back on their seams. The result is the same, to the
    /// byte, as one sweep: a seam vertex is made by the slab below before the
    /// slab above would make it, which is when the single sweep makes it too,
    /// so concatenating the slabs in order and dropping the upper copies
    /// reproduces its vertex order exactly.
    pub(crate) fn triangulate(&self) -> BufferGeometry {
        let layers = self.dims[2] - 1;
        let slabs = if cfg!(feature = "parallel") {
            (layers / 16).clamp(1, 32)
        } else {
            1
        };
        self.triangulate_with(slabs)
    }

    /// [`triangulate`](Self::triangulate) in exactly `slabs` slabs.
    fn triangulate_with(&self, slabs: usize) -> BufferGeometry {
        let [nx, ny, nz] = self.dims;
        let layers = nz - 1;
        let slabs = slabs.clamp(1, layers.max(1));
        let bounds = |c: usize| c * layers / slabs;
        let parts = crate::utils::parallel::par_map_range_coarse(slabs, |c| {
            self.sweep(bounds(c), bounds(c + 1))
        });

        let total = |f: fn(&Slab) -> usize| parts.iter().map(f).sum::<usize>();
        let mut positions = Vec::with_capacity(total(|s| s.positions.len()));
        let mut normals = Vec::with_capacity(total(|s| s.normals.len()));
        let mut indices = Vec::with_capacity(total(|s| s.indices.len()));
        let mut seam: Vec<u32> = Vec::new();
        for (c, part) in parts.iter().enumerate() {
            // Local vertex → global, the upper copies of the seam folded onto
            // the lower slab's.
            let count = part.positions.len() / 3;
            let mut global = vec![u32::MAX; count];
            if c > 0 {
                for (slot, &local) in part.bottom.iter().enumerate() {
                    if local != u32::MAX {
                        global[local as usize] = seam[slot];
                    }
                }
            }
            for (v, slot) in global.iter_mut().enumerate() {
                if *slot == u32::MAX {
                    *slot = (positions.len() / 3) as u32;
                    positions.extend_from_slice(&part.positions[v * 3..v * 3 + 3]);
                    normals.extend_from_slice(&part.normals[v * 3..v * 3 + 3]);
                }
            }
            indices.extend(part.indices.iter().map(|&i| global[i as usize]));
            seam = part
                .top
                .iter()
                .map(|&local| {
                    if local == u32::MAX {
                        u32::MAX
                    } else {
                        global[local as usize]
                    }
                })
                .collect();
        }
        let extent = Vector3::new(
            (nx - 1) as f32 * self.step.x,
            (ny - 1) as f32 * self.step.y,
            (nz - 1) as f32 * self.step.z,
        );

        let mut geom = BufferGeometry::new();
        if positions.is_empty() {
            return geom;
        }
        let mut uvs = Vec::with_capacity(positions.len() / 3 * 2);
        for v in 0..positions.len() / 3 {
            let p = Vector3::new(
                positions[v * 3] - self.origin.x,
                positions[v * 3 + 1] - self.origin.y,
                positions[v * 3 + 2] - self.origin.z,
            );
            let n = [
                normals[v * 3].abs(),
                normals[v * 3 + 1].abs(),
                normals[v * 3 + 2].abs(),
            ];
            let (u, w) = if n[0] >= n[1] && n[0] >= n[2] {
                (p.z / extent.z, p.y / extent.y)
            } else if n[1] >= n[2] {
                (p.x / extent.x, p.z / extent.z)
            } else {
                (p.x / extent.x, p.y / extent.y)
            };
            uvs.extend_from_slice(&[u, w]);
        }

        geom.set_attribute("position", BufferAttribute::new(positions, 3));
        geom.set_attribute("normal", BufferAttribute::new(normals, 3));
        geom.set_attribute("uv", BufferAttribute::new(uvs, 2));
        geom.set_index(indices);
        geom
    }
}

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

    /// Sample a field over a box padded far enough that it is negative all the
    /// way round, so the surface is closed.
    fn grid_of(res: usize, half: f32, field: impl Fn(Vector3) -> f32) -> IsoGrid {
        let step = 2.0 * half / (res - 1) as f32;
        let origin = Vector3::new(-half, -half, -half);
        let mut values = Vec::with_capacity(res * res * res);
        for k in 0..res {
            for j in 0..res {
                for i in 0..res {
                    values.push(field(Vector3::new(
                        origin.x + i as f32 * step,
                        origin.y + j as f32 * step,
                        origin.z + k as f32 * step,
                    )));
                }
            }
        }
        IsoGrid {
            dims: [res; 3],
            origin,
            step: Vector3::new(step, step, step),
            values,
        }
    }

    fn triangles(geom: &BufferGeometry) -> Vec<[u32; 3]> {
        let idx = geom.index.as_ref().expect("indexed");
        idx.as_chunks::<3>()
            .0
            .iter()
            .map(|t| [t[0], t[1], t[2]])
            .collect()
    }

    #[test]
    fn every_case_yields_closed_loops() {
        // A loop must visit each cut edge once and close: the permutation the
        // face rule builds has no fixed points and no stragglers.
        for case in 0..256u16 {
            let case = case as u8;
            let loops = loops_for(case);
            let mut visited: Vec<u8> = loops.iter().flatten().copied().collect();
            visited.sort_unstable();
            let before = visited.len();
            visited.dedup();
            assert_eq!(before, visited.len(), "case {case} repeats an edge");

            let inside = |c: u8| case & (1 << c) != 0;
            let mut cut: Vec<u8> = (0..12u8)
                .filter(|&e| {
                    let (a, b) = EDGES[e as usize];
                    inside(a) != inside(b)
                })
                .collect();
            cut.sort_unstable();
            assert_eq!(cut, visited, "case {case} misses a cut edge");
        }
    }

    #[test]
    fn slabs_weld_back_to_the_single_sweep() {
        // Any number of slabs, the same bytes: positions, normals, UVs, index.
        let g = grid_of(37, 1.0, |p| {
            let s = 4.0;
            0.3 - ((p.x * s).sin() * (p.y * s).cos()
                + (p.y * s).sin() * (p.z * s).cos()
                + (p.z * s).sin() * (p.x * s).cos())
            .abs()
        });
        let one = g.triangulate_with(1);
        for slabs in [2, 3, 7, 40] {
            let many = g.triangulate_with(slabs);
            assert_eq!(one.index, many.index, "{slabs} slabs");
            for name in ["position", "normal", "uv"] {
                assert_eq!(
                    one.get_attribute(name).unwrap().array,
                    many.get_attribute(name).unwrap().array,
                    "{slabs} slabs: {name}"
                );
            }
        }
    }

    #[test]
    fn sphere_is_watertight_and_outward_facing() {
        let r = 0.6f32;
        let geom = grid_of(24, 1.0, |p| r - p.length()).triangulate();
        let tris = triangles(&geom);
        assert!(!tris.is_empty());

        // Watertight and consistently wound: every directed edge appears once,
        // and its reverse appears once — the signature of an oriented manifold.
        let mut edges: HashMap<(u32, u32), i32> = HashMap::new();
        for t in &tris {
            for e in [(t[0], t[1]), (t[1], t[2]), (t[2], t[0])] {
                let (key, dir) = if e.0 < e.1 {
                    ((e.0, e.1), 1)
                } else {
                    ((e.1, e.0), -1)
                };
                *edges.entry(key).or_insert(0) += dir;
            }
        }
        assert!(
            edges.values().all(|&v| v == 0),
            "open or inconsistently wound edges"
        );

        // Outward: the winding normal agrees with the radial direction.
        let pos = geom.get_attribute("position").unwrap();
        let vert = |i: u32| {
            let i = i as usize * 3;
            Vector3::new(pos.array[i], pos.array[i + 1], pos.array[i + 2])
        };
        let inward = tris
            .iter()
            .filter(|t| {
                let (a, b, c) = (vert(t[0]), vert(t[1]), vert(t[2]));
                let n = (b - a).cross(c - a);
                n.dot(a + b + c) < 0.0
            })
            .count();
        assert_eq!(inward, 0, "of {} triangles", tris.len());

        // And it is the right sphere: every vertex sits on it.
        for i in 0..pos.count() as u32 {
            assert!((vert(i).length() - r).abs() < 0.05);
        }
    }

    #[test]
    fn normals_point_out() {
        let geom = grid_of(20, 1.0, |p| 0.6 - p.length()).triangulate();
        let pos = geom.get_attribute("position").unwrap();
        let nrm = geom.get_attribute("normal").unwrap();
        for i in 0..pos.count() {
            let p = Vector3::new(pos.array[i * 3], pos.array[i * 3 + 1], pos.array[i * 3 + 2]);
            let n = Vector3::new(nrm.array[i * 3], nrm.array[i * 3 + 1], nrm.array[i * 3 + 2]);
            assert!(
                n.dot(p.normalize()) > 0.9,
                "normal off the radial by too far"
            );
        }
    }

    #[test]
    fn saddle_cells_do_not_fold_back() {
        // A gyroid puts a saddle through most cells, so its loops are the long
        // non-planar ones — exactly the case a fan from a loop vertex inverts.
        let geom = grid_of(28, 2.0, |p| {
            let (x, y, z) = (p.x * 2.0, p.y * 2.0, p.z * 2.0);
            0.4 - (x.sin() * y.cos() + y.sin() * z.cos() + z.sin() * x.cos()).abs()
        })
        .triangulate();
        let pos = geom.get_attribute("position").unwrap();
        let nrm = geom.get_attribute("normal").unwrap();
        let vec = |attr: &BufferAttribute, i: u32| {
            let i = i as usize * 3;
            Vector3::new(attr.array[i], attr.array[i + 1], attr.array[i + 2])
        };
        let mut inverted = 0;
        let tris = triangles(&geom);
        for t in &tris {
            let (a, b, c) = (vec(pos, t[0]), vec(pos, t[1]), vec(pos, t[2]));
            let face = (b - a).cross(c - a);
            let shading = vec(nrm, t[0]) + vec(nrm, t[1]) + vec(nrm, t[2]);
            if face.dot(shading) < 0.0 {
                inverted += 1;
            }
        }
        assert_eq!(inverted, 0, "of {} triangles", tris.len());
    }

    #[test]
    fn empty_field_makes_empty_geometry() {
        let geom = grid_of(8, 1.0, |_| -1.0).triangulate();
        assert!(geom.get_attribute("position").is_none());
    }

    #[test]
    fn two_disjoint_blobs_both_survive() {
        // Two spheres in one grid — the cell-local table must not merge them.
        let geom = grid_of(32, 2.0, |p| {
            let a = 0.5 - (p - Vector3::new(-0.9, 0.0, 0.0)).length();
            let b = 0.5 - (p - Vector3::new(0.9, 0.0, 0.0)).length();
            a.max(b)
        })
        .triangulate();
        let pos = geom.get_attribute("position").unwrap();
        let (mut left, mut right) = (0, 0);
        for i in 0..pos.count() {
            if pos.array[i * 3] < 0.0 {
                left += 1;
            } else {
                right += 1;
            }
        }
        assert!(left > 100 && right > 100, "lost a blob: {left}/{right}");
    }
}