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
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
//! Mesh **exporters** for the OpenSCAD front end — the counterparts to the
//! importers in `scad::import`.
//!
//! All operate on an evaluated [`BufferGeometry`] (indexed or triangle-soup) and
//! are pure Rust with no dependencies:
//! - `geometry_to_obj` — Wavefront **OBJ** (text; positions, normals, faces).
//! - `geometry_to_off` — Geomview **OFF** (text).
//! - `geometry_to_3mf` — **3MF** (a proper OPC/ZIP package a slicer can open).
//! - `geometry_to_glb` — binary **glTF 2.0** (`.glb`; single self-contained file).
//!
//! [`Solid`](crate::Solid) grows `to_obj`/`to_off`/`to_3mf`/`to_glb` convenience
//! methods that evaluate with the exact-where-confident kernel first.

use crate::core::BufferGeometry;

/// Normalise a geometry to `(vertices, optional per-vertex normals, triangle
/// indices)`. A non-indexed soup gets sequential indices; normals are dropped
/// unless there is exactly one per vertex.
type TriData = (Vec<[f32; 3]>, Option<Vec<[f32; 3]>>, Vec<u32>);

fn tri_data(g: &BufferGeometry) -> Option<TriData> {
    let pos = &g.get_attribute("position")?.array;
    let verts: Vec<[f32; 3]> = pos.chunks_exact(3).map(|c| [c[0], c[1], c[2]]).collect();
    if verts.len() < 3 {
        return None;
    }
    let normals = g
        .get_attribute("normal")
        .map(|a| {
            a.array
                .chunks_exact(3)
                .map(|c| [c[0], c[1], c[2]])
                .collect::<Vec<_>>()
        })
        .filter(|n: &Vec<[f32; 3]>| n.len() == verts.len());
    let indices = g
        .index
        .clone()
        .unwrap_or_else(|| (0..verts.len() as u32).collect());
    Some((verts, normals, indices))
}

/// Export as **Wavefront OBJ** (text). Emits `v`, optional `vn`, and 1-indexed
/// triangle `f` lines (with `v//vn` references when normals are present).
pub fn geometry_to_obj(g: &BufferGeometry) -> String {
    let Some((verts, normals, indices)) = tri_data(g) else {
        return String::new();
    };
    let mut s = String::from("# threers OBJ export\no model\n");
    for v in &verts {
        s.push_str(&format!("v {} {} {}\n", v[0], v[1], v[2]));
    }
    if let Some(n) = &normals {
        for v in n {
            s.push_str(&format!("vn {} {} {}\n", v[0], v[1], v[2]));
        }
    }
    for t in indices.chunks_exact(3) {
        let (a, b, c) = (t[0] + 1, t[1] + 1, t[2] + 1); // OBJ is 1-indexed
        if normals.is_some() {
            s.push_str(&format!("f {a}//{a} {b}//{b} {c}//{c}\n"));
        } else {
            s.push_str(&format!("f {a} {b} {c}\n"));
        }
    }
    s
}

/// Export as **Geomview OFF** (text).
pub fn geometry_to_off(g: &BufferGeometry) -> String {
    let Some((verts, _, indices)) = tri_data(g) else {
        return String::from("OFF\n0 0 0\n");
    };
    let nf = indices.len() / 3;
    let mut s = format!("OFF\n{} {} 0\n", verts.len(), nf);
    for v in &verts {
        s.push_str(&format!("{} {} {}\n", v[0], v[1], v[2]));
    }
    for t in indices.chunks_exact(3) {
        s.push_str(&format!("3 {} {} {}\n", t[0], t[1], t[2]));
    }
    s
}

// --- 3MF (OPC/ZIP package) --------------------------------------------------

/// Export as **3MF** — a spec-shaped OPC package (`[Content_Types].xml`,
/// `_rels/.rels`, `3D/3dmodel.model`) zipped with stored (uncompressed) entries
/// and correct CRC-32s, so real slicers open it.
pub fn geometry_to_3mf(g: &BufferGeometry) -> Vec<u8> {
    let (verts, _, indices) = tri_data(g).unwrap_or_default();
    let mut model = String::from(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
         <model unit=\"millimeter\" xml:lang=\"en-US\" \
         xmlns=\"http://schemas.microsoft.com/3dmanufacturing/core/2015/02\">\n\
         <resources><object id=\"1\" type=\"model\"><mesh><vertices>",
    );
    for v in &verts {
        model.push_str(&format!(
            "<vertex x=\"{}\" y=\"{}\" z=\"{}\"/>",
            v[0], v[1], v[2]
        ));
    }
    model.push_str("</vertices><triangles>");
    for t in indices.chunks_exact(3) {
        model.push_str(&format!(
            "<triangle v1=\"{}\" v2=\"{}\" v3=\"{}\"/>",
            t[0], t[1], t[2]
        ));
    }
    model.push_str(
        "</triangles></mesh></object></resources><build><item objectid=\"1\"/></build></model>",
    );

    let content_types = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
        <Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\
        <Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>\
        <Default Extension=\"model\" ContentType=\"application/vnd.ms-package.3dmanufacturing-3dmodel+xml\"/>\
        </Types>";
    let rels = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
        <Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
        <Relationship Target=\"/3D/3dmodel.model\" Id=\"rel0\" \
        Type=\"http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel\"/></Relationships>";

    let mut zip = Zip::new();
    zip.add("[Content_Types].xml", content_types.as_bytes());
    zip.add("_rels/.rels", rels.as_bytes());
    zip.add("3D/3dmodel.model", model.as_bytes());
    zip.finish()
}

/// Minimal single-purpose ZIP writer: stored (method 0) entries with correct
/// CRC-32s and a central directory. Enough for a valid 3MF package.
struct Zip {
    out: Vec<u8>,
    dir: Vec<u8>,
    count: u16,
}
impl Zip {
    fn new() -> Self {
        Self {
            out: Vec::new(),
            dir: Vec::new(),
            count: 0,
        }
    }
    fn add(&mut self, name: &str, data: &[u8]) {
        let crc = crc32(data);
        let (nl, dl) = (name.len() as u16, data.len() as u32);
        let offset = self.out.len() as u32;
        // local file header
        self.out.extend_from_slice(b"PK\x03\x04");
        self.out.extend_from_slice(&[20, 0, 0, 0, 0, 0]); // version, flags, method=store
        self.out.extend_from_slice(&[0, 0, 0, 0]); // mod time/date
        self.out.extend_from_slice(&crc.to_le_bytes());
        self.out.extend_from_slice(&dl.to_le_bytes());
        self.out.extend_from_slice(&dl.to_le_bytes());
        self.out.extend_from_slice(&nl.to_le_bytes());
        self.out.extend_from_slice(&[0, 0]); // extra len
        self.out.extend_from_slice(name.as_bytes());
        self.out.extend_from_slice(data);
        // central directory record
        self.dir.extend_from_slice(b"PK\x01\x02");
        self.dir.extend_from_slice(&[20, 0, 20, 0, 0, 0, 0, 0]); // versions, flags, method
        self.dir.extend_from_slice(&[0, 0, 0, 0]); // mod time/date
        self.dir.extend_from_slice(&crc.to_le_bytes());
        self.dir.extend_from_slice(&dl.to_le_bytes());
        self.dir.extend_from_slice(&dl.to_le_bytes());
        self.dir.extend_from_slice(&nl.to_le_bytes());
        self.dir.extend_from_slice(&[0, 0, 0, 0, 0, 0]); // extra, comment, disk#
        self.dir.extend_from_slice(&[0, 0, 0, 0, 0, 0]); // int/ext attrs
        self.dir.extend_from_slice(&offset.to_le_bytes());
        self.dir.extend_from_slice(name.as_bytes());
        self.count += 1;
    }
    fn finish(mut self) -> Vec<u8> {
        let cd_off = self.out.len() as u32;
        let cd_size = self.dir.len() as u32;
        self.out.extend_from_slice(&self.dir);
        self.out.extend_from_slice(b"PK\x05\x06");
        self.out.extend_from_slice(&[0, 0, 0, 0]); // disk numbers
        self.out.extend_from_slice(&self.count.to_le_bytes());
        self.out.extend_from_slice(&self.count.to_le_bytes());
        self.out.extend_from_slice(&cd_size.to_le_bytes());
        self.out.extend_from_slice(&cd_off.to_le_bytes());
        self.out.extend_from_slice(&[0, 0]); // comment len
        self.out
    }
}

fn crc32(data: &[u8]) -> u32 {
    let mut crc = 0xFFFF_FFFFu32;
    for &b in data {
        crc ^= b as u32;
        for _ in 0..8 {
            crc = if crc & 1 != 0 {
                (crc >> 1) ^ 0xEDB8_8320
            } else {
                crc >> 1
            };
        }
    }
    !crc
}

// --- glTF 2.0 (binary .glb) -------------------------------------------------

/// Export as binary **glTF 2.0** (`.glb`) — a single self-contained file with a
/// JSON chunk and a BIN chunk (indices + positions + normals). Opens in any glTF
/// viewer (three.js, Blender, Windows 3D Viewer, …).
pub fn geometry_to_glb(g: &BufferGeometry) -> Vec<u8> {
    let Some((verts, normals, indices)) = tri_data(g) else {
        return Vec::new();
    };

    // Binary buffer: [indices u32][positions f32×3][normals f32×3?]. Each section
    // is a multiple of 4 bytes, so bufferView offsets stay 4-aligned automatically.
    let mut bin = Vec::new();
    for &i in &indices {
        bin.extend_from_slice(&i.to_le_bytes());
    }
    let idx_len = bin.len();
    let pos_off = bin.len();
    for v in &verts {
        for c in v {
            bin.extend_from_slice(&c.to_le_bytes());
        }
    }
    let pos_len = bin.len() - pos_off;
    let (nrm_off, nrm_len) = match &normals {
        Some(n) => {
            let off = bin.len();
            for v in n {
                for c in v {
                    bin.extend_from_slice(&c.to_le_bytes());
                }
            }
            (off, bin.len() - off)
        }
        None => (0, 0),
    };
    let buf_len = bin.len();

    // Position accessor requires min/max (glTF validators enforce it).
    let (mut mn, mut mx) = ([f32::MAX; 3], [f32::MIN; 3]);
    for v in &verts {
        for k in 0..3 {
            mn[k] = mn[k].min(v[k]);
            mx[k] = mx[k].max(v[k]);
        }
    }
    let arr = |a: [f32; 3]| format!("[{},{},{}]", a[0], a[1], a[2]);

    // bufferViews / accessors, with NORMAL only when present.
    let mut views = format!(
        "{{\"buffer\":0,\"byteOffset\":0,\"byteLength\":{idx_len},\"target\":34963}},\
         {{\"buffer\":0,\"byteOffset\":{pos_off},\"byteLength\":{pos_len},\"target\":34962}}"
    );
    let mut accessors = format!(
        "{{\"bufferView\":0,\"componentType\":5125,\"count\":{},\"type\":\"SCALAR\"}},\
         {{\"bufferView\":1,\"componentType\":5126,\"count\":{},\"type\":\"VEC3\",\"min\":{},\"max\":{}}}",
        indices.len(),
        verts.len(),
        arr(mn),
        arr(mx),
    );
    let attributes = if normals.is_some() {
        views.push_str(&format!(
            ",{{\"buffer\":0,\"byteOffset\":{nrm_off},\"byteLength\":{nrm_len},\"target\":34962}}"
        ));
        accessors.push_str(&format!(
            ",{{\"bufferView\":2,\"componentType\":5126,\"count\":{},\"type\":\"VEC3\"}}",
            verts.len()
        ));
        "\"POSITION\":1,\"NORMAL\":2"
    } else {
        "\"POSITION\":1"
    };

    let json = format!(
        "{{\"asset\":{{\"version\":\"2.0\",\"generator\":\"threers\"}},\
         \"scene\":0,\"scenes\":[{{\"nodes\":[0]}}],\"nodes\":[{{\"mesh\":0}}],\
         \"meshes\":[{{\"primitives\":[{{\"attributes\":{{{attributes}}},\"indices\":0,\"mode\":4}}]}}],\
         \"buffers\":[{{\"byteLength\":{buf_len}}}],\
         \"bufferViews\":[{views}],\"accessors\":[{accessors}]}}"
    );

    // GLB container: 12-byte header + JSON chunk (space-padded) + BIN chunk (0-padded).
    let mut json_bytes = json.into_bytes();
    while !json_bytes.len().is_multiple_of(4) {
        json_bytes.push(b' ');
    }
    while !bin.len().is_multiple_of(4) {
        bin.push(0);
    }
    let total = 12 + 8 + json_bytes.len() + 8 + bin.len();
    let mut glb = Vec::with_capacity(total);
    glb.extend_from_slice(b"glTF"); // magic
    glb.extend_from_slice(&2u32.to_le_bytes()); // version
    glb.extend_from_slice(&(total as u32).to_le_bytes());
    glb.extend_from_slice(&(json_bytes.len() as u32).to_le_bytes());
    glb.extend_from_slice(b"JSON");
    glb.extend_from_slice(&json_bytes);
    glb.extend_from_slice(&(bin.len() as u32).to_le_bytes());
    glb.extend_from_slice(b"BIN\0");
    glb.extend_from_slice(&bin);
    glb
}

/// Export colored OpenSCAD [`crate::openscad::ScadPart`]s as a multi-mesh **glTF 2.0** `.glb`.
pub fn parts_to_glb(parts: &[crate::openscad::ScadPart]) -> Vec<u8> {
    use crate::openscad::ScadPart;

    let mut bin: Vec<u8> = Vec::new();
    let mut buffer_views: Vec<String> = Vec::new();
    let mut accessors: Vec<String> = Vec::new();
    let mut meshes: Vec<String> = Vec::new();
    let mut nodes: Vec<String> = Vec::new();
    let mut materials: Vec<String> = Vec::new();
    let mut mat_map: Vec<(u32, u32, u32, u32)> = Vec::new();

    let default_rgba = [0.91_f32, 0.93, 0.95, 1.0];
    let pbr = |rgba: [f32; 4]| -> (f32, f32) {
        let (r, g, b) = (rgba[0], rgba[1], rgba[2]);
        // Warm glass tag
        if r > 0.85 && g > 0.75 && b < 0.55 {
            return (0.55, 0.06);
        }
        // Solar cells — blue-black
        if r < 0.08 && g < 0.08 && b < 0.12 {
            return (0.08, 0.32);
        }
        // Dark MLI / logos
        if r < 0.15 && g < 0.16 && b < 0.20 {
            return (0.12, 0.74);
        }
        // Docking marker red
        if r > 0.65 && g < 0.20 && b < 0.25 {
            return (0.55, 0.35);
        }
        // Docking marker blue
        if r < 0.15 && g > 0.22 && b > 0.55 {
            return (0.55, 0.35);
        }
        // Gold EVA
        if r > 0.65 && g > 0.55 && b < 0.45 {
            return (0.90, 0.24);
        }
        // White quilted MLI (warm off-white)
        if r > 0.85 && g > 0.82 && b < 0.92 && r - b > 0.02 {
            return (0.04, 0.86);
        }
        // Near-neutral white hull
        if r > 0.92 && g > 0.92 && b > 0.92 {
            return (0.32, 0.48);
        }
        // Silver trim (blue-shifted)
        if b > r + 0.03 && b > g && r > 0.55 {
            return (0.82, 0.18);
        }
        // Hull fallback
        (0.32, 0.48)
    };

    let mut mat_idx = |rgba: [f32; 4]| -> usize {
        let key = (
            (rgba[0] * 255.0).round() as u32,
            (rgba[1] * 255.0).round() as u32,
            (rgba[2] * 255.0).round() as u32,
            (rgba[3] * 255.0).round() as u32,
        );
        if let Some(i) = mat_map.iter().position(|k| *k == key) {
            return i;
        }
        let i = materials.len();
        mat_map.push(key);
        let (metal, rough) = pbr(rgba);
        materials.push(format!(
            "{{\"pbrMetallicRoughness\":{{\"baseColorFactor\":[{},{},{},{}],\"metallicFactor\":{},\"roughnessFactor\":{}}}}}",
            rgba[0], rgba[1], rgba[2], rgba[3], metal, rough
        ));
        i
    };

    let arr3 = |a: [f32; 3]| format!("[{},{},{}]", a[0], a[1], a[2]);

    for part in parts {
        let ScadPart { geometry, color } = part;
        let Some((verts, normals, indices)) = tri_data(geometry) else {
            continue;
        };
        if indices.is_empty() {
            continue;
        }

        let rgba = color.unwrap_or(default_rgba);
        let mi = mat_idx(rgba);

        let idx_off = bin.len();
        for &i in &indices {
            bin.extend_from_slice(&i.to_le_bytes());
        }
        let idx_len = bin.len() - idx_off;
        let idx_bv = buffer_views.len();
        buffer_views.push(format!(
            "{{\"buffer\":0,\"byteOffset\":{idx_off},\"byteLength\":{idx_len},\"target\":34963}}"
        ));
        let idx_acc = accessors.len();
        accessors.push(format!(
            "{{\"bufferView\":{idx_bv},\"componentType\":5125,\"count\":{},\"type\":\"SCALAR\"}}",
            indices.len()
        ));

        let pos_off = bin.len();
        for v in &verts {
            for c in v {
                bin.extend_from_slice(&c.to_le_bytes());
            }
        }
        let pos_len = bin.len() - pos_off;
        let pos_bv = buffer_views.len();
        buffer_views.push(format!(
            "{{\"buffer\":0,\"byteOffset\":{pos_off},\"byteLength\":{pos_len},\"target\":34962}}"
        ));

        let (mut mn, mut mx) = ([f32::MAX; 3], [f32::MIN; 3]);
        for v in &verts {
            for k in 0..3 {
                mn[k] = mn[k].min(v[k]);
                mx[k] = mx[k].max(v[k]);
            }
        }
        let pos_acc = accessors.len();
        accessors.push(format!(
            "{{\"bufferView\":{pos_bv},\"componentType\":5126,\"count\":{},\"type\":\"VEC3\",\"min\":{},\"max\":{}}}",
            verts.len(),
            arr3(mn),
            arr3(mx)
        ));

        let nrm_acc = if let Some(n) = &normals {
            let nrm_off = bin.len();
            for v in n {
                for c in v {
                    bin.extend_from_slice(&c.to_le_bytes());
                }
            }
            let nrm_len = bin.len() - nrm_off;
            let nrm_bv = buffer_views.len();
            buffer_views.push(format!(
                "{{\"buffer\":0,\"byteOffset\":{nrm_off},\"byteLength\":{nrm_len},\"target\":34962}}"
            ));
            let acc = accessors.len();
            accessors.push(format!(
                "{{\"bufferView\":{nrm_bv},\"componentType\":5126,\"count\":{},\"type\":\"VEC3\"}}",
                n.len()
            ));
            Some(acc)
        } else {
            None
        };

        let attrs = if let Some(na) = nrm_acc {
            format!("\"POSITION\":{pos_acc},\"NORMAL\":{na}")
        } else {
            format!("\"POSITION\":{pos_acc}")
        };
        let mesh_idx = meshes.len();
        meshes.push(format!(
            "{{\"primitives\":[{{\"attributes\":{{{attrs}}},\"indices\":{idx_acc},\"material\":{mi},\"mode\":4}}]}}"
        ));
        nodes.push(format!("{{\"mesh\":{mesh_idx}}}"));
    }

    if meshes.is_empty() {
        return Vec::new();
    }

    let node_list: String = (0..nodes.len()).map(|i| i.to_string()).collect::<Vec<_>>().join(",");
    let json = format!(
        "{{\"asset\":{{\"version\":\"2.0\",\"generator\":\"threers\"}},\
         \"scene\":0,\"scenes\":[{{\"nodes\":[{node_list}]}}],\
         \"nodes\":[{}],\"meshes\":[{}],\"materials\":[{}],\
         \"buffers\":[{{\"byteLength\":{}}}],\"bufferViews\":[{}],\"accessors\":[{}]}}",
        nodes.join(","),
        meshes.join(","),
        materials.join(","),
        bin.len(),
        buffer_views.join(","),
        accessors.join(","),
    );

    let mut json_bytes = json.into_bytes();
    while !json_bytes.len().is_multiple_of(4) {
        json_bytes.push(b' ');
    }
    while !bin.len().is_multiple_of(4) {
        bin.push(0);
    }
    let total = 12 + 8 + json_bytes.len() + 8 + bin.len();
    let mut glb = Vec::with_capacity(total);
    glb.extend_from_slice(b"glTF");
    glb.extend_from_slice(&2u32.to_le_bytes());
    glb.extend_from_slice(&(total as u32).to_le_bytes());
    glb.extend_from_slice(&(json_bytes.len() as u32).to_le_bytes());
    glb.extend_from_slice(b"JSON");
    glb.extend_from_slice(&json_bytes);
    glb.extend_from_slice(&(bin.len() as u32).to_le_bytes());
    glb.extend_from_slice(b"BIN\0");
    glb.extend_from_slice(&bin);
    glb
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{cube, geometry_to_obj as _reexport_check};

    fn tetra() -> BufferGeometry {
        // A unit-ish tetra via the exact kernel (has a normal attribute).
        cube([10.0, 10.0, 10.0]).to_geometry_exact()
    }

    #[test]
    fn obj_roundtrips_through_objloader() {
        let _ = _reexport_check; // ensure the crate re-export exists
        let g = tetra();
        let obj = geometry_to_obj(&g);
        let vlines = obj.lines().filter(|l| l.starts_with("v ")).count();
        let flines = obj.lines().filter(|l| l.starts_with("f ")).count();
        assert!(vlines > 0 && flines > 0, "obj empty");
        // Re-import via the public OBJ loader and compare triangle counts.
        let back = crate::ObjLoader::parse(&obj);
        let n_in = g.index.as_ref().map(|i| i.len() / 3).unwrap_or(vlines / 3);
        let n_out = back.index.as_ref().map(|i| i.len() / 3).unwrap_or_else(|| {
            back.get_attribute("position")
                .map(|a| a.count() / 3)
                .unwrap_or(0)
        });
        assert_eq!(n_out, n_in, "obj triangle count changed on round-trip");
    }

    #[test]
    fn off_has_matching_counts() {
        let g = tetra();
        let off = geometry_to_off(&g);
        let mut lines = off.lines();
        assert_eq!(lines.next(), Some("OFF"));
        let counts: Vec<usize> = lines
            .next()
            .unwrap()
            .split_whitespace()
            .map(|s| s.parse().unwrap())
            .collect();
        let (nv, nf) = (counts[0], counts[1]);
        assert_eq!(
            off.lines().filter(|l| l.starts_with("3 ")).count(),
            nf,
            "face count"
        );
        // vertex lines = total - header(2) - faces
        assert_eq!(off.lines().count() - 2 - nf, nv, "vertex count");
    }

    #[test]
    fn threemf_is_valid_zip_with_the_mesh() {
        let g = tetra();
        let bytes = geometry_to_3mf(&g);
        // Central-directory + local headers present.
        assert!(bytes.starts_with(b"PK\x03\x04"), "not a zip");
        assert!(bytes.windows(4).any(|w| w == b"PK\x05\x06"), "no EOCD");
        // Stored (uncompressed) entries → the model XML is verbatim in the bytes.
        let text = String::from_utf8_lossy(&bytes);
        assert!(text.contains("3dmodel.model"), "missing model part name");
        let n_v = g.get_attribute("position").unwrap().count(); // count() = vertices
        assert_eq!(text.matches("<vertex ").count(), n_v, "3mf vertex count");
    }

    #[test]
    fn glb_header_and_json_are_wellformed() {
        let g = tetra();
        let glb = geometry_to_glb(&g);
        assert!(glb.starts_with(b"glTF"), "bad magic");
        let total = u32::from_le_bytes(glb[8..12].try_into().unwrap()) as usize;
        assert_eq!(total, glb.len(), "declared length != actual");
        // JSON chunk.
        let jlen = u32::from_le_bytes(glb[12..16].try_into().unwrap()) as usize;
        assert_eq!(&glb[16..20], b"JSON");
        let json = std::str::from_utf8(&glb[20..20 + jlen]).unwrap();
        assert!(json.contains("\"version\":\"2.0\""));
        assert!(json.contains("\"POSITION\":1"));
        let n_v = g.get_attribute("position").unwrap().count(); // count() = vertices
        assert!(
            json.contains(&format!("\"count\":{n_v}")),
            "position accessor count"
        );
        // Alignment: both chunks 4-byte aligned.
        assert_eq!(jlen % 4, 0);
        // BIN chunk present after the JSON chunk.
        let bin_hdr = 20 + jlen;
        assert_eq!(&glb[bin_hdr + 4..bin_hdr + 8], b"BIN\0");
    }
}