rvt 0.4.0

Apache-2.0 clean-room reader for Autodesk Revit files (.rvt, .rfa, .rte, .rft) without Revit: metadata, previews and the embedded schema of 2016-2026 files, and typed elements with measured geometry from Revit 2024 and 2025 projects, exported as IFC4, glTF and plan SVG.
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
//! glTF 2.0 exporter (VW1-04) — dep-free binary GLB emission.
//!
//! Produces a `.glb` file ready to load into Three.js, BabylonJS,
//! Blender, or any glTF 2.0 viewer. Each `BuildingElement` is drawn as the
//! body the STEP writer gives it ([`super::body_geometry`]), at its
//! `location_feet` turned by its `rotation_radians`. A plain rectangular
//! extrusion shares one unit cube, scaled by its node's matrix; every
//! other body (a sketched profile, a steel section, a swept or revolved
//! solid, a brep) has its own mesh. An element with no body gets a node
//! with no mesh. Each element gets a material drawn from
//! `PbrMaterial::from_material_info` (VW1-06).
//!
//! Element nodes are in the model's frame, feet with +Z up. glTF is
//! metres with +Y up, so they hang under one root node whose matrix
//! ([`Z_UP_FEET_TO_Y_UP_METRES`]) converts; without it every model lay on
//! its side, ten times too large, in any glTF viewer.
//!
//! GLB file layout (per the glTF 2.0 spec):
//!
//! ```text
//! [u32 magic=0x46546C67 "glTF"] [u32 version=2] [u32 total_length]
//! [u32 json_chunk_length] [u32 chunk_type=0x4E4F534A "JSON"]
//! [json_chunk_length bytes UTF-8 JSON, padded to 4-byte boundary with 0x20]
//! [u32 bin_chunk_length] [u32 chunk_type=0x004E4942 "BIN\0"]
//! [bin_chunk_length bytes binary buffer, padded to 4-byte with 0x00]
//! ```

use super::IfcModel;
use super::body_geometry::{self, Body, Placement, body_mesh, element_body};
use super::entities::IfcEntity;
use super::pbr::PbrMaterial;
use serde::{Deserialize, Serialize};

/// Top-level glTF 2.0 JSON document (VW1-04).
///
/// Matches the glTF spec field names exactly (camelCase) so the
/// serialized JSON is a valid glTF file.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GltfDocument {
    pub asset: Asset,
    pub scene: Option<usize>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub scenes: Vec<Scene>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub nodes: Vec<Node>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub meshes: Vec<Mesh>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub materials: Vec<Material>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub buffers: Vec<Buffer>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    #[serde(rename = "bufferViews")]
    pub buffer_views: Vec<BufferView>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub accessors: Vec<Accessor>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Asset {
    pub version: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub generator: Option<String>,
}

impl Default for Asset {
    fn default() -> Self {
        Self {
            version: "2.0".into(),
            generator: Some("rvt-rs VW1-04".into()),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Scene {
    pub nodes: Vec<usize>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Node {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mesh: Option<usize>,
    /// 16-element column-major transformation matrix. `None` =
    /// identity.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub matrix: Option<[f32; 16]>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub children: Vec<usize>,
    /// Application-specific payload. glTF loaders surface this as
    /// the object's `userData`, which is how the viewer maps a
    /// picked mesh back to its entity.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub extras: Option<NodeExtras>,
}

/// Identity the viewer needs on every drawable node: the
/// `model.entities` index behind it and its IFC type. The viewer
/// reads these as `userData.entityIndex` / `userData.ifcType` to
/// drive picking, selection highlight, and category visibility —
/// all three of which were inert while the nodes carried no
/// extras at all.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct NodeExtras {
    #[serde(rename = "entityIndex")]
    pub entity_index: usize,
    #[serde(rename = "ifcType")]
    pub ifc_type: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Mesh {
    pub primitives: Vec<Primitive>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Primitive {
    pub attributes: std::collections::BTreeMap<String, usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub indices: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub material: Option<usize>,
    /// 4 = triangle list (the default per the glTF spec).
    #[serde(default)]
    pub mode: u32,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Material {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(rename = "pbrMetallicRoughness")]
    pub pbr_metallic_roughness: PbrMetallicRoughness,
    #[serde(default, rename = "doubleSided")]
    pub double_sided: bool,
    #[serde(default, rename = "alphaMode", skip_serializing_if = "Option::is_none")]
    pub alpha_mode: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PbrMetallicRoughness {
    /// `[r, g, b, a]` linear 0-1.
    #[serde(rename = "baseColorFactor")]
    pub base_color_factor: [f32; 4],
    #[serde(rename = "metallicFactor")]
    pub metallic_factor: f32,
    #[serde(rename = "roughnessFactor")]
    pub roughness_factor: f32,
}

impl Default for PbrMetallicRoughness {
    fn default() -> Self {
        Self {
            base_color_factor: [0.75, 0.75, 0.75, 1.0],
            metallic_factor: 0.0,
            roughness_factor: 0.6,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Buffer {
    #[serde(rename = "byteLength")]
    pub byte_length: usize,
    /// `None` = embedded in the GLB binary chunk (the common case
    /// for single-file .glb output).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BufferView {
    pub buffer: usize,
    #[serde(rename = "byteOffset", default)]
    pub byte_offset: usize,
    #[serde(rename = "byteLength")]
    pub byte_length: usize,
    /// glTF target: 34962 = ARRAY_BUFFER (vertex attrs),
    /// 34963 = ELEMENT_ARRAY_BUFFER (indices). `None` for
    /// unspecified.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Accessor {
    #[serde(rename = "bufferView")]
    pub buffer_view: usize,
    #[serde(rename = "byteOffset", default)]
    pub byte_offset: usize,
    /// 5120..5126 per glTF component-type enum:
    /// 5120=i8, 5121=u8, 5122=i16, 5123=u16, 5125=u32, 5126=f32.
    #[serde(rename = "componentType")]
    pub component_type: u32,
    pub count: usize,
    /// `"SCALAR"`, `"VEC2"`, `"VEC3"`, `"VEC4"`, `"MAT4"`, etc.
    #[serde(rename = "type")]
    pub type_: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub max: Vec<f32>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub min: Vec<f32>,
}

/// Unit-cube vertex positions ([-0.5, 0.5]³, 24 verts — 4 per
/// face for correct per-face normals). Shared across every
/// element in the first-pass VW1-04 export.
fn unit_cube_vertices() -> [f32; 24 * 3] {
    // 6 faces × 4 verts × 3 coords.
    let h = 0.5_f32;
    [
        // +X face
        h, -h, -h, h, h, -h, h, h, h, h, -h, h, // -X face
        -h, -h, h, -h, h, h, -h, h, -h, -h, -h, -h, // +Y face
        -h, h, -h, -h, h, h, h, h, h, h, h, -h, // -Y face
        h, -h, -h, h, -h, h, -h, -h, h, -h, -h, -h, // +Z face
        -h, -h, h, h, -h, h, h, h, h, -h, h, h, // -Z face
        -h, -h, -h, -h, h, -h, h, h, -h, h, -h, -h,
    ]
}

fn unit_cube_indices() -> [u16; 36] {
    // 2 triangles per face.
    [
        0, 1, 2, 0, 2, 3, // +X
        4, 5, 6, 4, 6, 7, // -X
        8, 9, 10, 8, 10, 11, // +Y
        12, 13, 14, 12, 14, 15, // -Y
        16, 17, 18, 16, 18, 19, // +Z
        20, 21, 22, 20, 22, 23, // -Z
    ]
}

/// Column-major matrix taking the model's frame (feet, +Z up) to glTF's
/// (metres, +Y up): model `(x, y, z)` feet is glTF `(x, z, -y)` metres.
pub const Z_UP_FEET_TO_Y_UP_METRES: [f32; 16] = [
    0.3048, 0.0, 0.0, 0.0, // model X stays X
    0.0, 0.0, -0.3048, 0.0, // model Y is glTF -Z
    0.0, 0.3048, 0.0, 0.0, // model Z (up) is glTF Y (up)
    0.0, 0.0, 0.0, 1.0,
];

/// Build a glTF document + binary buffer from an `IfcModel`
/// (VW1-04). Returns `(document, binary_buffer)` — wire into
/// [`write_glb`] for a single-file `.glb` output.
///
/// Every `BuildingElement` becomes a node carrying its entity index and
/// IFC type, a child of the root node that converts the model's frame to
/// glTF's. Its mesh is its body in element-local feet, and its matrix
/// its placement: the shared unit cube scaled, turned and moved for a
/// plain rectangular extrusion, a per-element mesh turned and moved for
/// any other body, and no mesh for an element with none.
pub fn build_gltf(model: &IfcModel) -> (GltfDocument, Vec<u8>) {
    let mut bin = Vec::<u8>::new();
    let mut doc = GltfDocument::default();

    // Binary layout:
    //   [positions: 24 * VEC3 f32 = 288 bytes]
    //   [indices: 36 * u16 = 72 bytes → pad to 4-aligned = 72]
    let positions = unit_cube_vertices();
    let pos_bytes = bytemuck_cast_f32(&positions);
    let pos_offset = 0;
    let pos_len = pos_bytes.len();
    bin.extend_from_slice(&pos_bytes);
    pad_to_4(&mut bin);

    let indices = unit_cube_indices();
    let idx_bytes = bytemuck_cast_u16(&indices);
    let idx_offset = bin.len();
    let idx_len = idx_bytes.len();
    bin.extend_from_slice(&idx_bytes);
    pad_to_4(&mut bin);

    // BufferView 0: positions.
    doc.buffer_views.push(BufferView {
        buffer: 0,
        byte_offset: pos_offset,
        byte_length: pos_len,
        target: Some(34962), // ARRAY_BUFFER
    });
    // BufferView 1: indices.
    doc.buffer_views.push(BufferView {
        buffer: 0,
        byte_offset: idx_offset,
        byte_length: idx_len,
        target: Some(34963), // ELEMENT_ARRAY_BUFFER
    });

    // Accessor 0: POSITION (VEC3 f32, 24 verts).
    doc.accessors.push(Accessor {
        buffer_view: 0,
        byte_offset: 0,
        component_type: 5126, // f32
        count: 24,
        type_: "VEC3".into(),
        min: vec![-0.5, -0.5, -0.5],
        max: vec![0.5, 0.5, 0.5],
    });
    // Accessor 1: indices (SCALAR u16, 36 indices).
    doc.accessors.push(Accessor {
        buffer_view: 1,
        byte_offset: 0,
        component_type: 5123, // u16
        count: 36,
        type_: "SCALAR".into(),
        ..Accessor {
            buffer_view: 0,
            byte_offset: 0,
            component_type: 0,
            count: 0,
            type_: String::new(),
            max: Vec::new(),
            min: Vec::new(),
        }
    });

    // Materials — one per model.materials entry via PbrMaterial.
    for m in &model.materials {
        let pbr = PbrMaterial::from_material_info(m);
        let mat = Material {
            name: Some(m.name.clone()),
            pbr_metallic_roughness: PbrMetallicRoughness {
                base_color_factor: [
                    pbr.base_color_rgb[0],
                    pbr.base_color_rgb[1],
                    pbr.base_color_rgb[2],
                    pbr.alpha,
                ],
                metallic_factor: pbr.metallic,
                roughness_factor: pbr.roughness,
            },
            double_sided: pbr.double_sided,
            alpha_mode: if pbr.alpha < 1.0 {
                Some("BLEND".into())
            } else {
                None
            },
        };
        doc.materials.push(mat);
    }

    // An element with no material of its own takes its category's colour,
    // one material per category, after the model's own.
    let mut category_materials: std::collections::BTreeMap<String, usize> = Default::default();
    // Layer colours (RE-53), one material per colour and alpha.
    let mut layer_materials: std::collections::BTreeMap<(u32, u32), usize> = Default::default();
    // A family instance whose type draws in one material (RE-82) takes that
    // material's colour, when the colour was read (RE-53). One drawn in
    // several keeps its category's: a single body cannot show which part is
    // which.
    let single_material: std::collections::BTreeMap<usize, usize> = model
        .material_constituent_sets
        .iter()
        .filter_map(|set| match set.material_indices.as_slice() {
            [material]
                if model
                    .materials
                    .get(*material)
                    .is_some_and(|info| info.color_packed.is_some()) =>
            {
                Some(
                    set.elements
                        .iter()
                        .map(move |&element| (element, *material)),
                )
            }
            _ => None,
        })
        .flatten()
        // A member written with a material profile set of one material
        // (RE-105) takes it the same way.
        .chain(
            model
                .entities
                .iter()
                .enumerate()
                .filter_map(|(index, entity)| match entity {
                    IfcEntity::BuildingElement {
                        material_profile_set_index: Some(set),
                        ..
                    } => match model.material_profile_sets.get(*set)?.profiles.as_slice() {
                        [profile]
                            if model
                                .materials
                                .get(profile.material_index)
                                .is_some_and(|info| info.color_packed.is_some()) =>
                        {
                            Some((index, profile.material_index))
                        }
                        _ => None,
                    },
                    _ => None,
                }),
        )
        .collect();

    // Per-element: a node carrying the entity's identity, with the
    // shared cube or its own mesh.
    let mut scene_nodes: Vec<usize> = Vec::new();
    for (entity_index, ent) in model.entities.iter().enumerate() {
        let IfcEntity::BuildingElement {
            ifc_type,
            name,
            type_guid,
            material_index,
            location_feet,
            rotation_radians,
            ..
        } = ent
        else {
            continue;
        };
        // An opening is a void in its host, not a solid (RE-84).
        if ifc_type == "IFCOPENINGELEMENT" {
            continue;
        }
        let placement = Placement::new(*location_feet, *rotation_radians);
        let body = element_body(ent, &model.representation_maps);
        let (c, s) = (placement.cos as f32, placement.sin as f32);
        let [tx, ty, tz] = placement.origin.map(|v| v as f32);
        let body_matrix = [
            c,
            s,
            0.0,
            0.0,
            if s == 0.0 { 0.0 } else { -s },
            c,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            tx,
            ty,
            tz,
            1.0,
        ];
        // RE-53: a layered element is one node per layer, each in its
        // material's colour, all carrying the element's identity.
        let layered = type_guid
            .as_deref()
            .and_then(|tag| tag.parse::<u32>().ok())
            .and_then(|id| model.element_layers.get(&id));
        if let (Some(layers), Some(Body::Extrusion(extrusion))) = (layered, body) {
            let [nx, ny] = layers.exterior_normal;
            let local = [
                nx * placement.cos + ny * placement.sin,
                -nx * placement.sin + ny * placement.cos,
            ];
            let widths: Vec<f64> = layers.layers.iter().map(|l| l.width_feet).collect();
            let meshes = if layers.stacked {
                body_geometry::stacked_extrusion_meshes(extrusion, &widths)
            } else {
                body_geometry::layered_extrusion_meshes(extrusion, local, &widths)
            };
            if let Some(meshes) = meshes {
                for (mesh, band) in meshes.iter().zip(&layers.layers) {
                    let Some((position, indices)) = push_mesh(&mut doc, &mut bin, mesh) else {
                        continue;
                    };
                    let material = match band.color_packed {
                        Some(packed) => {
                            let rgb = ((packed & 0xff) << 16)
                                | (packed & 0xff00)
                                | ((packed >> 16) & 0xff);
                            let alpha = (1.0 - band.transparency.clamp(0.0, 1.0)) as f32;
                            *layer_materials
                                .entry((rgb, alpha.to_bits()))
                                .or_insert_with(|| {
                                    doc.materials.push(colour_material(
                                        format!("Layer {rgb:06x}"),
                                        rgb,
                                        alpha,
                                    ));
                                    doc.materials.len() - 1
                                })
                        }
                        None => *category_materials
                            .entry(ifc_type.clone())
                            .or_insert_with(|| {
                                doc.materials.push(category_material(ifc_type));
                                doc.materials.len() - 1
                            }),
                    };
                    let mut attributes = std::collections::BTreeMap::new();
                    attributes.insert("POSITION".into(), position);
                    let mesh_idx = doc.meshes.len();
                    doc.meshes.push(Mesh {
                        primitives: vec![Primitive {
                            attributes,
                            indices: Some(indices),
                            material: Some(material),
                            mode: 4,
                        }],
                        name: Some(name.clone()),
                    });
                    let node_idx = doc.nodes.len();
                    doc.nodes.push(Node {
                        name: Some(name.clone()),
                        mesh: Some(mesh_idx),
                        matrix: Some(body_matrix),
                        children: Vec::new(),
                        extras: Some(NodeExtras {
                            entity_index,
                            ifc_type: ifc_type.clone(),
                        }),
                    });
                    scene_nodes.push(node_idx);
                }
                continue;
            }
        }
        // (accessor pair, matrix)
        let drawn: Option<((usize, usize), [f32; 16])> = match body {
            Some(Body::Extrusion(ex))
                if ex.profile_override.is_none()
                    && [ex.width_feet, ex.depth_feet, ex.height_feet]
                        .iter()
                        .all(|v| v.is_finite() && *v > 0.0) =>
            {
                let (sx, sy, sz) = (
                    ex.width_feet as f32,
                    ex.depth_feet as f32,
                    ex.height_feet as f32,
                );
                // Translate · rotate about Z · scale the unit cube, whose
                // base sits half its height below its centre.
                Some((
                    (0, 1),
                    [
                        c * sx,
                        s * sx,
                        0.0,
                        0.0,
                        if s == 0.0 { 0.0 } else { -s * sy },
                        c * sy,
                        0.0,
                        0.0,
                        0.0,
                        0.0,
                        sz,
                        0.0,
                        tx,
                        ty,
                        tz + sz * 0.5,
                        1.0,
                    ],
                ))
            }
            Some(body) => body_mesh(body).and_then(|mesh| {
                let accessors = push_mesh(&mut doc, &mut bin, &mesh)?;
                Some((accessors, body_matrix))
            }),
            None => None,
        };
        let (mesh, matrix) = match drawn {
            Some(((position, indices), matrix)) => {
                let material = material_index
                    .or_else(|| single_material.get(&entity_index).copied())
                    .or_else(|| {
                        Some(
                            *category_materials
                                .entry(ifc_type.clone())
                                .or_insert_with(|| {
                                    doc.materials.push(category_material(ifc_type));
                                    doc.materials.len() - 1
                                }),
                        )
                    });
                let mut attributes = std::collections::BTreeMap::new();
                attributes.insert("POSITION".into(), position);
                let mesh_idx = doc.meshes.len();
                doc.meshes.push(Mesh {
                    primitives: vec![Primitive {
                        attributes,
                        indices: Some(indices),
                        material,
                        mode: 4, // TRIANGLES
                    }],
                    name: Some(name.clone()),
                });
                (Some(mesh_idx), Some(matrix))
            }
            None => (None, None),
        };
        let node_idx = doc.nodes.len();
        doc.nodes.push(Node {
            name: Some(name.clone()),
            mesh,
            matrix,
            children: Vec::new(),
            extras: Some(NodeExtras {
                entity_index,
                ifc_type: ifc_type.clone(),
            }),
        });
        scene_nodes.push(node_idx);
    }

    // Buffer (singular — GLB embeds the whole bin chunk as buffer 0).
    doc.buffers.push(Buffer {
        byte_length: bin.len(),
        uri: None,
    });
    let root = doc.nodes.len();
    doc.nodes.push(Node {
        name: Some("model".into()),
        mesh: None,
        matrix: Some(Z_UP_FEET_TO_Y_UP_METRES),
        children: scene_nodes,
        extras: None,
    });
    doc.scenes.push(Scene { nodes: vec![root] });
    doc.scene = Some(0);

    (doc, bin)
}

/// The sRGB colour, and the alpha, an element with no material of its own
/// is drawn in: its category's, from the viewer design system's gray, blue,
/// red, green and amber ramps, in the hues of the plan's palette
/// ([`super::sheet`]). Blue-6 is left to the viewer's selection highlight.
/// Glass-like categories are translucent, and spaces faint enough not to
/// hide the building they fill.
pub fn category_colour(ifc_type: &str) -> (u32, f32) {
    match ifc_type {
        "IFCWALL" | "IFCWALLSTANDARDCASE" | "IFCCURTAINWALL" => (0xCDCED0, 1.0),
        "IFCSLAB" | "IFCROOF" | "IFCCOVERING" => (0x909398, 1.0),
        "IFCDOOR" => (0xF59E0B, 1.0),
        "IFCWINDOW" | "IFCPLATE" => (0x8FC1FF, 0.45),
        "IFCCOLUMN" => (0xDC2626, 1.0),
        "IFCBEAM" | "IFCMEMBER" => (0xF87171, 1.0),
        "IFCSTAIR" | "IFCSTAIRFLIGHT" | "IFCRAILING" | "IFCRAMP" | "IFCRAMPFLIGHT" => {
            (0x92400E, 1.0)
        }
        "IFCFURNITURE" | "IFCFURNISHINGELEMENT" => (0x16A34A, 1.0),
        "IFCSPACE" => (0x3D94FF, 0.12),
        _ => (0xB2B4B8, 1.0),
    }
}

/// The material a category's colour makes, named for the category.
fn category_material(ifc_type: &str) -> Material {
    let (rgb, alpha) = category_colour(ifc_type);
    colour_material(format!("Category {ifc_type}"), rgb, alpha)
}

/// A material of one sRGB colour (`0xRRGGBB`) and alpha. glTF base colours
/// are linear, so the colour is linearised.
fn colour_material(name: String, rgb: u32, alpha: f32) -> Material {
    let linear = |shift: u32| {
        let c = ((rgb >> shift) & 0xff) as f32 / 255.0;
        if c <= 0.04045 {
            c / 12.92
        } else {
            ((c + 0.055) / 1.055).powf(2.4)
        }
    };
    Material {
        name: Some(name),
        pbr_metallic_roughness: PbrMetallicRoughness {
            base_color_factor: [linear(16), linear(8), linear(0), alpha],
            metallic_factor: 0.0,
            roughness_factor: 0.8,
        },
        double_sided: alpha < 1.0,
        alpha_mode: (alpha < 1.0).then(|| "BLEND".into()),
    }
}

/// Append `mesh` to the binary buffer as f32 positions and u32 indices,
/// with a buffer view and an accessor for each. Returns the
/// `(POSITION, indices)` accessor indices, or `None` for an empty mesh or
/// one past `u32` indexing.
fn push_mesh(
    doc: &mut GltfDocument,
    bin: &mut Vec<u8>,
    mesh: &body_geometry::Mesh,
) -> Option<(usize, usize)> {
    if mesh.is_empty() || u32::try_from(mesh.vertices.len()).is_err() {
        return None;
    }
    let mut min = [f32::INFINITY; 3];
    let mut max = [f32::NEG_INFINITY; 3];
    let positions: Vec<f32> = mesh
        .vertices
        .iter()
        .flat_map(|v| {
            let p = v.map(|c| c as f32);
            for axis in 0..3 {
                min[axis] = min[axis].min(p[axis]);
                max[axis] = max[axis].max(p[axis]);
            }
            p
        })
        .collect();
    if !positions.iter().all(|v| v.is_finite()) {
        return None;
    }
    let indices: Vec<u8> = mesh
        .triangles
        .iter()
        .flatten()
        .flat_map(|i| i.to_le_bytes())
        .collect();

    let position_offset = bin.len();
    bin.extend_from_slice(&bytemuck_cast_f32(&positions));
    let position_view = doc.buffer_views.len();
    doc.buffer_views.push(BufferView {
        buffer: 0,
        byte_offset: position_offset,
        byte_length: bin.len() - position_offset,
        target: Some(34962), // ARRAY_BUFFER
    });
    pad_to_4(bin);
    let index_offset = bin.len();
    bin.extend_from_slice(&indices);
    let index_view = doc.buffer_views.len();
    doc.buffer_views.push(BufferView {
        buffer: 0,
        byte_offset: index_offset,
        byte_length: indices.len(),
        target: Some(34963), // ELEMENT_ARRAY_BUFFER
    });
    pad_to_4(bin);

    let position = doc.accessors.len();
    doc.accessors.push(Accessor {
        buffer_view: position_view,
        byte_offset: 0,
        component_type: 5126, // f32
        count: mesh.vertices.len(),
        type_: "VEC3".into(),
        min: min.to_vec(),
        max: max.to_vec(),
    });
    let index = doc.accessors.len();
    doc.accessors.push(Accessor {
        buffer_view: index_view,
        byte_offset: 0,
        component_type: 5125, // u32
        count: mesh.triangles.len() * 3,
        type_: "SCALAR".into(),
        min: Vec::new(),
        max: Vec::new(),
    });
    Some((position, index))
}

/// Write a GLB (binary glTF) file to `out` given a JSON document
/// + binary buffer (VW1-04).
///
/// Spec reference: <https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#glb-file-format-specification>
pub fn write_glb(doc: &GltfDocument, bin: &[u8], out: &mut Vec<u8>) {
    let json_text = serde_json::to_string(doc).expect("serialize glTF doc");
    let mut json_bytes = json_text.into_bytes();
    // Pad JSON chunk to 4-byte boundary with ASCII space (per spec).
    while json_bytes.len() % 4 != 0 {
        json_bytes.push(b' ');
    }
    let mut bin_padded = bin.to_vec();
    while bin_padded.len() % 4 != 0 {
        bin_padded.push(0);
    }

    // Header: 12 bytes (magic + version + total length).
    let total_len = 12 + 8 + json_bytes.len() + 8 + bin_padded.len();
    out.extend_from_slice(&0x4654_6C67_u32.to_le_bytes()); // "glTF"
    out.extend_from_slice(&2_u32.to_le_bytes()); // version
    out.extend_from_slice(&(total_len as u32).to_le_bytes());

    // JSON chunk.
    out.extend_from_slice(&(json_bytes.len() as u32).to_le_bytes());
    out.extend_from_slice(&0x4E4F_534A_u32.to_le_bytes()); // "JSON"
    out.extend_from_slice(&json_bytes);

    // BIN chunk.
    out.extend_from_slice(&(bin_padded.len() as u32).to_le_bytes());
    out.extend_from_slice(&0x004E_4942_u32.to_le_bytes()); // "BIN\0"
    out.extend_from_slice(&bin_padded);
}

/// One-call convenience: `IfcModel` → GLB bytes.
pub fn model_to_glb(model: &IfcModel) -> Vec<u8> {
    let (doc, bin) = build_gltf(model);
    let mut out = Vec::new();
    write_glb(&doc, &bin, &mut out);
    out
}

// ---- Internal: dep-free f32/u16 slice-to-bytes casts ----

fn bytemuck_cast_f32(src: &[f32]) -> Vec<u8> {
    let mut out = Vec::with_capacity(src.len() * 4);
    for &v in src {
        out.extend_from_slice(&v.to_le_bytes());
    }
    out
}

fn bytemuck_cast_u16(src: &[u16]) -> Vec<u8> {
    let mut out = Vec::with_capacity(src.len() * 2);
    for &v in src {
        out.extend_from_slice(&v.to_le_bytes());
    }
    out
}

fn pad_to_4(buf: &mut Vec<u8>) {
    while buf.len() % 4 != 0 {
        buf.push(0);
    }
}

#[cfg(test)]
mod tests {
    use super::super::Storey;
    use super::super::entities::{Extrusion, IfcEntity};
    use super::*;

    fn mk_wall(name: &str, loc: Option<[f64; 3]>, extrusion: Option<Extrusion>) -> IfcEntity {
        IfcEntity::BuildingElement {
            ifc_type: "IFCWALL".into(),
            name: name.into(),
            type_guid: None,
            predefined_type: None,
            storey_index: None,
            material_index: None,
            property_set: None,
            location_feet: loc,
            rotation_radians: None,
            extrusion,
            host_element_index: None,
            material_layer_set_index: None,
            material_profile_set_index: None,
            solid_shape: None,
            representation_map_index: None,
        }
    }

    #[test]
    fn every_element_node_carries_its_entity_index_and_ifc_type() {
        // The viewer maps a picked mesh back to its entity through
        // glTF node extras. Without them, picking, highlight and
        // category visibility have nothing to match on.
        let model = IfcModel {
            entities: vec![
                mk_wall("W1", Some([0.0, 0.0, 0.0]), None),
                IfcEntity::Project {
                    name: Some("P".into()),
                    description: None,
                    long_name: None,
                },
                mk_wall("W2", Some([5.0, 0.0, 0.0]), None),
            ],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        let extras: Vec<(usize, &str)> = element_nodes(&doc)
            .map(|n| {
                let e = n.extras.as_ref().expect("node carries extras");
                (e.entity_index, e.ifc_type.as_str())
            })
            .collect();
        // Indices are into model.entities, so the skipped Project
        // entity at index 1 leaves a gap rather than renumbering.
        assert_eq!(extras, vec![(0, "IFCWALL"), (2, "IFCWALL")]);
    }

    #[test]
    fn node_extras_serialize_as_the_camel_case_keys_the_viewer_reads() {
        let model = IfcModel {
            entities: vec![mk_wall("W1", None, None)],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        let json = serde_json::to_string(&doc).unwrap();
        assert!(json.contains("\"extras\""));
        assert!(json.contains("\"entityIndex\""));
        assert!(json.contains("\"ifcType\""));
    }

    #[test]
    fn empty_model_produces_valid_glb_framing() {
        let glb = model_to_glb(&IfcModel::default());
        // 12-byte header + 8-byte JSON chunk header + JSON body
        // padded to 4 + 8-byte BIN chunk header + BIN body
        // padded to 4. Minimum realistic size ~200 bytes.
        assert!(glb.len() >= 100);
        // Magic "glTF"
        assert_eq!(&glb[..4], b"glTF");
        // Version 2
        let ver = u32::from_le_bytes([glb[4], glb[5], glb[6], glb[7]]);
        assert_eq!(ver, 2);
        // Total length matches
        let total = u32::from_le_bytes([glb[8], glb[9], glb[10], glb[11]]) as usize;
        assert_eq!(total, glb.len());
    }

    #[test]
    fn glb_has_json_and_bin_chunks() {
        let glb = model_to_glb(&IfcModel::default());
        // JSON chunk type at offset 16 = "JSON"
        assert_eq!(&glb[16..20], b"JSON");
        let json_len = u32::from_le_bytes([glb[12], glb[13], glb[14], glb[15]]) as usize;
        // BIN chunk type at 20 + json_len + 4
        let bin_type_offset = 20 + json_len + 4;
        assert_eq!(&glb[bin_type_offset..bin_type_offset + 4], b"BIN\0");
    }

    #[test]
    fn build_gltf_includes_asset_version_2() {
        let (doc, _) = build_gltf(&IfcModel::default());
        assert_eq!(doc.asset.version, "2.0");
        assert!(doc.asset.generator.is_some());
    }

    #[test]
    fn build_gltf_shares_mesh_accessors() {
        let (doc, _) = build_gltf(&IfcModel::default());
        // Shared cube: 1 buffer, 2 bufferViews, 2 accessors.
        assert_eq!(doc.buffers.len(), 1);
        assert_eq!(doc.buffer_views.len(), 2);
        assert_eq!(doc.accessors.len(), 2);
    }

    #[test]
    fn each_building_element_becomes_a_node_with_mesh() {
        let model = IfcModel {
            entities: vec![
                mk_wall(
                    "Wall-1",
                    Some([10.0, 5.0, 0.0]),
                    Some(Extrusion {
                        width_feet: 20.0,
                        depth_feet: 0.5,
                        height_feet: 10.0,
                        profile_override: None,
                    }),
                ),
                mk_wall("Wall-2", None, None),
            ],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        // Wall-2 has no body: it keeps its node, with no mesh, rather than
        // being drawn as a unit cube it does not have.
        assert_eq!(doc.meshes.len(), 1);
        assert_eq!(element_nodes(&doc).count(), 2);
        assert_eq!(doc.scenes[0].nodes.len(), 1, "the root");
        assert!(doc.nodes[0].mesh.is_some());
        assert!(doc.nodes[1].mesh.is_none() && doc.nodes[1].matrix.is_none());
    }

    fn with_rotation(mut entity: IfcEntity, rotation: f64) -> IfcEntity {
        if let IfcEntity::BuildingElement {
            rotation_radians, ..
        } = &mut entity
        {
            *rotation_radians = Some(rotation);
        }
        entity
    }

    #[test]
    fn a_rotated_element_is_drawn_turned() {
        let wall = with_rotation(
            mk_wall(
                "W",
                Some([3.0, 7.0, 1.0]),
                Some(Extrusion::rectangle(20.0, 0.5, 10.0)),
            ),
            std::f64::consts::FRAC_PI_2,
        );
        let model = IfcModel {
            entities: vec![wall],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        let m = doc.nodes[0].matrix.unwrap();
        // The cube's X (the wall's length) now runs along +Y.
        assert!(m[0].abs() < 1e-6 && (m[1] - 20.0).abs() < 1e-5);
        assert!((m[4] + 0.5).abs() < 1e-6 && m[5].abs() < 1e-6);
        assert_eq!((m[12], m[13], m[14]), (3.0, 7.0, 6.0));
    }

    /// Read back a mesh's world-space vertices from the GLB buffer.
    fn world_vertices(doc: &GltfDocument, bin: &[u8], node: usize) -> Vec<[f32; 3]> {
        let n = &doc.nodes[node];
        let mesh = &doc.meshes[n.mesh.expect("mesh")];
        let accessor = &doc.accessors[mesh.primitives[0].attributes["POSITION"]];
        let view = &doc.buffer_views[accessor.buffer_view];
        let m = n.matrix.expect("matrix");
        bin[view.byte_offset..view.byte_offset + view.byte_length]
            .chunks_exact(12)
            .map(|c| {
                let v: Vec<f32> = c
                    .chunks_exact(4)
                    .map(|b| f32::from_le_bytes(b.try_into().unwrap()))
                    .collect();
                [
                    m[0] * v[0] + m[4] * v[1] + m[8] * v[2] + m[12],
                    m[1] * v[0] + m[5] * v[1] + m[9] * v[2] + m[13],
                    m[2] * v[0] + m[6] * v[1] + m[10] * v[2] + m[14],
                ]
            })
            .collect()
    }

    #[test]
    fn a_sketched_profile_is_drawn_as_its_own_mesh() {
        // An L-shaped slab, 0.5 ft thick, placed at (10, 0, 2).
        let slab = mk_wall(
            "S",
            Some([10.0, 0.0, 2.0]),
            Some(Extrusion::arbitrary_closed(
                vec![
                    (0.0, 0.0),
                    (4.0, 0.0),
                    (4.0, 2.0),
                    (2.0, 2.0),
                    (2.0, 4.0),
                    (0.0, 4.0),
                ],
                0.5,
            )),
        );
        let model = IfcModel {
            entities: vec![slab],
            ..Default::default()
        };
        let (doc, bin) = build_gltf(&model);
        assert_eq!(doc.buffers[0].byte_length, bin.len());
        let vertices = world_vertices(&doc, &bin, 0);
        let xs: Vec<f32> = vertices.iter().map(|v| v[0]).collect();
        let zs: Vec<f32> = vertices.iter().map(|v| v[2]).collect();
        assert_eq!(xs.iter().copied().fold(f32::INFINITY, f32::min), 10.0);
        assert_eq!(xs.iter().copied().fold(f32::NEG_INFINITY, f32::max), 14.0);
        assert_eq!(zs.iter().copied().fold(f32::INFINITY, f32::min), 2.0);
        assert_eq!(zs.iter().copied().fold(f32::NEG_INFINITY, f32::max), 2.5);
        // No vertex falls in the L's missing corner.
        assert!(
            !vertices
                .iter()
                .any(|v| v[0] > 12.0 + 1e-4 && v[1] > 2.0 + 1e-4)
        );
        let accessor = &doc.accessors[doc.meshes[0].primitives[0].attributes["POSITION"]];
        assert_eq!(accessor.min, vec![0.0, 0.0, 0.0]);
        assert_eq!(accessor.max, vec![4.0, 4.0, 0.5]);
    }

    #[test]
    fn a_swept_solid_is_drawn_along_its_directrix() {
        let mut beam = mk_wall("B", Some([0.0, 0.0, 0.0]), None);
        if let IfcEntity::BuildingElement {
            ifc_type,
            solid_shape,
            extrusion,
            ..
        } = &mut beam
        {
            *ifc_type = "IFCBEAM".into();
            // The record box the swept solid takes precedence over.
            *extrusion = Some(Extrusion::rectangle(1.0, 30.0, 2.0));
            *solid_shape = Some(super::super::entities::SolidShape::SweptPath {
                profile: super::super::entities::ProfileDef::Rectangle {
                    width_feet: 1.0,
                    depth_feet: 0.5,
                },
                directrix_points_feet: vec![[0.0, -10.0, 1.0], [0.0, 10.0, 1.5]],
                fixed_reference: [0.0, 0.0, 1.0],
            });
        }
        let model = IfcModel {
            entities: vec![beam],
            ..Default::default()
        };
        let (doc, bin) = build_gltf(&model);
        let vertices = world_vertices(&doc, &bin, 0);
        let span = |axis: usize| {
            let values: Vec<f32> = vertices.iter().map(|v| v[axis]).collect();
            values.iter().copied().fold(f32::NEG_INFINITY, f32::max)
                - values.iter().copied().fold(f32::INFINITY, f32::min)
        };
        assert!((span(0) - 0.5).abs() < 1e-4, "the section's width");
        assert!((span(1) - 20.0).abs() < 0.05, "the directrix, not the box");
    }

    #[test]
    fn node_transform_reflects_extrusion_dims() {
        let ext = Extrusion {
            width_feet: 20.0,
            depth_feet: 0.5,
            height_feet: 10.0,
            profile_override: None,
        };
        let model = IfcModel {
            entities: vec![mk_wall("W", Some([3.0, 7.0, 0.0]), Some(ext))],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        let m = doc.nodes[0].matrix.unwrap();
        assert_eq!(m[0], 20.0); // scale X
        assert_eq!(m[5], 0.5); // scale Y
        assert_eq!(m[10], 10.0); // scale Z
        assert_eq!(m[12], 3.0); // translate X
        assert_eq!(m[13], 7.0); // translate Y
        // translate Z = loc.z + height/2 = 0 + 5 = 5
        assert_eq!(m[14], 5.0);
    }

    #[test]
    fn elements_without_a_material_take_their_categorys_colour() {
        let mut door = mk_wall(
            "D",
            Some([0.0; 3]),
            Some(Extrusion::rectangle(3.0, 0.2, 7.0)),
        );
        let mut room = mk_wall(
            "R",
            Some([0.0; 3]),
            Some(Extrusion::rectangle(10.0, 10.0, 8.0)),
        );
        for (entity, kind) in [(&mut door, "IFCDOOR"), (&mut room, "IFCSPACE")] {
            if let IfcEntity::BuildingElement { ifc_type, .. } = entity {
                *ifc_type = kind.into();
            }
        }
        let wall = mk_wall(
            "W",
            Some([0.0; 3]),
            Some(Extrusion::rectangle(10.0, 0.5, 9.0)),
        );
        let wall2 = mk_wall(
            "W2",
            Some([0.0; 3]),
            Some(Extrusion::rectangle(5.0, 0.5, 9.0)),
        );
        let model = IfcModel {
            entities: vec![wall, door, room, wall2],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        // One material per category, shared by its elements.
        assert_eq!(doc.materials.len(), 3);
        let material_of = |mesh: usize| doc.meshes[mesh].primitives[0].material.unwrap();
        assert_eq!(material_of(0), material_of(3));
        let door_material = &doc.materials[material_of(1)];
        assert_eq!(door_material.name.as_deref(), Some("Category IFCDOOR"));
        let [r, g, b, a] = door_material.pbr_metallic_roughness.base_color_factor;
        assert!(r > g && g > b && a == 1.0, "a linear amber");
        let room_material = &doc.materials[material_of(2)];
        assert_eq!(room_material.alpha_mode.as_deref(), Some("BLEND"));
        assert!(room_material.pbr_metallic_roughness.base_color_factor[3] < 0.2);
    }

    #[test]
    fn an_elements_own_material_wins_over_its_category() {
        let mut wall = mk_wall(
            "W",
            Some([0.0; 3]),
            Some(Extrusion::rectangle(10.0, 0.5, 9.0)),
        );
        if let IfcEntity::BuildingElement { material_index, .. } = &mut wall {
            *material_index = Some(0);
        }
        let model = IfcModel {
            entities: vec![wall],
            materials: vec![super::super::MaterialInfo {
                name: "Concrete".into(),
                color_packed: Some(0x00808080),
                transparency: None,
            }],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        assert_eq!(doc.materials.len(), 1);
        assert_eq!(doc.meshes[0].primitives[0].material, Some(0));
    }

    #[test]
    fn materials_mirror_model_materials() {
        let model = IfcModel {
            materials: vec![
                super::super::MaterialInfo {
                    name: "Concrete".into(),
                    color_packed: Some(0x00808080),
                    transparency: None,
                },
                super::super::MaterialInfo {
                    name: "Glass".into(),
                    color_packed: None,
                    transparency: Some(0.6),
                },
            ],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        assert_eq!(doc.materials.len(), 2);
        assert_eq!(doc.materials[0].name.as_deref(), Some("Concrete"));
        assert_eq!(doc.materials[1].name.as_deref(), Some("Glass"));
        assert!(doc.materials[1].double_sided); // glass is double-sided
        assert_eq!(doc.materials[1].alpha_mode.as_deref(), Some("BLEND"));
    }

    #[test]
    fn json_chunk_is_valid_json() {
        let model = IfcModel {
            project_name: Some("Test".into()),
            entities: vec![mk_wall("W1", None, None)],
            ..Default::default()
        };
        let glb = model_to_glb(&model);
        let json_len = u32::from_le_bytes([glb[12], glb[13], glb[14], glb[15]]) as usize;
        let json_bytes = &glb[20..20 + json_len];
        let json_text = std::str::from_utf8(json_bytes)
            .unwrap()
            .trim_end_matches(' ');
        let _: serde_json::Value =
            serde_json::from_str(json_text).expect("emitted JSON must parse");
    }

    #[test]
    fn unit_cube_has_24_vertices_and_36_indices() {
        assert_eq!(unit_cube_vertices().len(), 24 * 3);
        assert_eq!(unit_cube_indices().len(), 36);
    }

    #[test]
    fn pad_to_4_rounds_up_only() {
        let mut v = vec![1u8, 2, 3];
        pad_to_4(&mut v);
        assert_eq!(v.len(), 4);
        pad_to_4(&mut v);
        assert_eq!(v.len(), 4); // already aligned
    }

    #[test]
    fn glb_json_and_bin_chunks_are_4_byte_aligned() {
        let model = IfcModel {
            entities: vec![
                mk_wall("A", None, None),
                mk_wall("B", None, None),
                mk_wall("C", None, None),
            ],
            ..Default::default()
        };
        let glb = model_to_glb(&model);
        let json_len = u32::from_le_bytes([glb[12], glb[13], glb[14], glb[15]]) as usize;
        assert_eq!(json_len % 4, 0, "JSON chunk length not 4-aligned");
        let bin_len_offset = 20 + json_len;
        let bin_len = u32::from_le_bytes([
            glb[bin_len_offset],
            glb[bin_len_offset + 1],
            glb[bin_len_offset + 2],
            glb[bin_len_offset + 3],
        ]) as usize;
        assert_eq!(bin_len % 4, 0, "BIN chunk length not 4-aligned");
    }

    #[test]
    fn scene_zero_references_all_nodes() {
        let model = IfcModel {
            entities: vec![
                mk_wall("a", None, None),
                mk_wall("b", None, None),
                mk_wall("c", None, None),
            ],
            building_storeys: vec![Storey {
                name: "Ground".into(),
                elevation_feet: 0.0,
            }],
            ..Default::default()
        };
        let (doc, _) = build_gltf(&model);
        assert_eq!(doc.scene, Some(0));
        let root = &doc.nodes[doc.scenes[0].nodes[0]];
        assert_eq!(root.children.len(), 3);
        assert_eq!(root.matrix, Some(Z_UP_FEET_TO_Y_UP_METRES));
    }

    /// The nodes of building elements, without the root.
    fn element_nodes(doc: &GltfDocument) -> impl Iterator<Item = &Node> {
        doc.nodes.iter().filter(|n| n.extras.is_some())
    }

    #[test]
    fn the_root_turns_z_up_feet_into_y_up_metres() {
        let m = Z_UP_FEET_TO_Y_UP_METRES;
        let apply = |p: [f32; 3]| {
            [
                m[0] * p[0] + m[4] * p[1] + m[8] * p[2] + m[12],
                m[1] * p[0] + m[5] * p[1] + m[9] * p[2] + m[13],
                m[2] * p[0] + m[6] * p[1] + m[10] * p[2] + m[14],
            ]
        };
        // 10 ft up is 3.048 m along glTF's +Y; 10 ft north is -Z.
        assert_eq!(apply([0.0, 0.0, 10.0]), [0.0, 3.048, 0.0]);
        assert_eq!(apply([0.0, 10.0, 0.0]), [0.0, 0.0, -3.048]);
        assert_eq!(apply([10.0, 0.0, 0.0]), [3.048, 0.0, 0.0]);
    }
}