fbx-dom 0.1.0

Document Model for Reading in FBX
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
//! FBX `Geometry` / `Mesh` — Assimp [`MeshGeometry`](https://github.com/assimp/assimp/blob/master/code/AssetLib/FBX/FBXMeshGeometry.h) / [`FBXMeshGeometry.cpp`](https://github.com/assimp/assimp/blob/master/code/AssetLib/FBX/FBXMeshGeometry.cpp).
//!
//! ## Layout
//!
//! - **Control points** — `Vertices` / `PolygonVertexIndex` on the geometry root; ASCII stores large
//!   arrays as `Name: *N { a: … }`; `parse_f32_array` / `parse_i32_array` read the optional **`a`**
//!   child or fall back to tokens on the node (unit tests).
//! - **Layers** — `LayerElementNormal`, `LayerElementUV`, … each hold `MappingInformationType`,
//!   `ReferenceInformationType`, and channel arrays. `expand_mesh_polygon_vertices` duplicates
//!   positions per corner and builds `mapping_*` tables for `resolve_flat_f32_channel`.

use std::collections::HashMap;
use std::convert::TryFrom;
use std::num::ParseFloatError;
use std::num::ParseIntError;

use crate::OwnedObject;
use fbxscii::ElementAttribute;

use super::AttrExtractor;
use super::AttrExtractorExt;
use super::{FbxObjectTag, FbxTryFromReason, FbxTypeMismatch, fbx_object_tag};

const MAX_UV_CHANNELS: usize = 8;
const MAX_COLOR_SETS: usize = 8;

const ATTR_MAPPING_INFORMATION_TYPE: &str = "MappingInformationType";
const ATTR_REFERENCE_INFORMATION_TYPE: &str = "ReferenceInformationType";

/// FBX SDK spelling (not "ByVertex").
const MAPPING_BY_VERTICE: &str = "ByVertice";
const MAPPING_BY_POLYGON_VERTEX: &str = "ByPolygonVertex";
const MAPPING_BY_POLYGON: &str = "ByPolygon";
const MAPPING_ALL_SAME: &str = "AllSame";

const REFERENCE_DIRECT: &str = "Direct";
const REFERENCE_INDEX_TO_DIRECT: &str = "IndexToDirect";

const ATTR_VERTICES: &str = "Vertices";
const ATTR_POLYGON_VERTEX_INDEX: &str = "PolygonVertexIndex";
const ATTR_LAYER_ELEMENT_NORMAL: &str = "LayerElementNormal";
const ATTR_LAYER_ELEMENT_TANGENT: &str = "LayerElementTangent";
const ATTR_LAYER_ELEMENT_BINORMAL: &str = "LayerElementBinormal";
const ATTR_LAYER_ELEMENT_UV: &str = "LayerElementUV";
const ATTR_MATERIALS: &str = "Materials";

const ACCESSOR_KEY: &str = "a";

#[derive(Debug, PartialEq)]
pub struct MeshGeometry {
    pub object: OwnedObject,
    pub vertices: Vec<[f32; 3]>,
    pub face_vertex_counts: Vec<u32>,
    pub normals: Vec<[f32; 3]>,
    pub tangents: Vec<[f32; 3]>,
    pub binormals: Vec<[f32; 3]>,
    pub texture_coords: [Vec<[f32; 2]>; MAX_UV_CHANNELS],
    pub texture_coord_names: [String; MAX_UV_CHANNELS],
    pub vertex_colors: [Vec<[f32; 4]>; MAX_COLOR_SETS],
    pub material_indices: Vec<i32>,
}

impl MeshGeometry {
    pub fn inner(&self) -> &OwnedObject {
        &self.object
    }

    pub fn into_inner(self) -> OwnedObject {
        self.object
    }
}

impl TryFrom<OwnedObject> for MeshGeometry {
    type Error = FbxTypeMismatch;

    fn try_from(o: OwnedObject) -> Result<Self, Self::Error> {
        // Check if tagged as MeshGeometry
        match fbx_object_tag(&o) {
            FbxObjectTag::MeshGeometry => {}
            _ => {
                return Err(FbxTypeMismatch::wrong_object_kind(
                    o,
                    "MeshGeometry".to_string(),
                ));
            }
        }

        let attrs = &o.attributes;

        // Extract Vertices (ASCII: `Vertices: *N { a: ... }`; tests may use a leaf with tokens only).
        let verts_attr = match attrs.extract_case_insensitive(ATTR_VERTICES) {
            Some(a) => a,
            None => {
                return Err(FbxTypeMismatch::new(
                    o,
                    FbxTryFromReason::MissingAttribute {
                        name: ATTR_VERTICES.to_string(),
                    },
                ));
            }
        };
        // Parse Vertices
        let vertices_result = parse_f32_array(verts_attr);
        let Ok(vertices) = vertices_result else {
            return Err(FbxTypeMismatch::new(
                o,
                FbxTryFromReason::InvalidAttributeFormat {
                    name: ATTR_VERTICES.to_string(),
                    detail: format!("invalid float token: {}", vertices_result.unwrap_err()),
                },
            ));
        };
        let vertices = vertices
            .chunks_exact(3)
            .map(|c| [c[0], c[1], c[2]])
            .collect::<Vec<[f32; 3]>>();

        // Extract Face Indices
        let poly_attr = match attrs.extract_case_insensitive(ATTR_POLYGON_VERTEX_INDEX) {
            Some(a) => a,
            None => {
                return Err(FbxTypeMismatch::new(
                    o,
                    FbxTryFromReason::MissingAttribute {
                        name: ATTR_POLYGON_VERTEX_INDEX.to_string(),
                    },
                ));
            }
        };
        let temp_faces_result = parse_i32_array(poly_attr);
        let Ok(temp_faces) = temp_faces_result else {
            return Err(FbxTypeMismatch::new(
                o,
                FbxTryFromReason::InvalidAttributeFormat {
                    name: ATTR_POLYGON_VERTEX_INDEX.to_string(),
                    detail: format!("invalid int token: {}", temp_faces_result.unwrap_err()),
                },
            ));
        };

        let (vertices, face_vertex_counts, mapping_counts, mapping_offsets, mappings) =
            match expand_mesh_polygon_vertices(&vertices, &temp_faces) {
                Ok(v) => v,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
        let vertex_count = vertices.len();

        let mut normals = Vec::new();
        if let Some(el) = attrs.extract_case_insensitive(ATTR_LAYER_ELEMENT_NORMAL) {
            let map = el.get_children_distinct();
            let mapping_ty = match map
                .require_token_case_insensitive(ATTR_MAPPING_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let reference_ty = match map
                .require_token_case_insensitive(ATTR_REFERENCE_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };

            let normals_flat = match resolve_flat_f32_channel(
                &map,
                ResolveFlatF32ChannelParams {
                    data_name: "Normals",
                    index_name: "NormalsIndex",
                    vertex_count,
                    components: 3,
                    mapping_counts: &mapping_counts,
                    mapping_offsets: &mapping_offsets,
                    mappings: &mappings,
                    mapping_ty: mapping_ty,
                    reference_ty: reference_ty,
                },
            ) {
                Ok(v) => v,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            normals = normals_flat
                .chunks_exact(3)
                .map(|c| [c[0], c[1], c[2]])
                .collect();
        }

        let mut tangents = Vec::new();
        if let Some(el) = attrs.extract_case_insensitive(ATTR_LAYER_ELEMENT_TANGENT) {
            let map = el.get_children_distinct();
            let (data_name, index_name) = if map.extract_case_insensitive("Tangents").is_some() {
                ("Tangents", "TangentsIndex")
            } else {
                ("Tangent", "TangentIndex")
            };
            let mapping_ty = match map
                .require_token_case_insensitive(ATTR_MAPPING_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let reference_ty = match map
                .require_token_case_insensitive(ATTR_REFERENCE_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let tangents_flat = match resolve_flat_f32_channel(
                &map,
                ResolveFlatF32ChannelParams {
                    data_name,
                    index_name,
                    vertex_count,
                    components: 3,
                    mapping_counts: &mapping_counts,
                    mapping_offsets: &mapping_offsets,
                    mappings: &mappings,
                    mapping_ty: mapping_ty,
                    reference_ty: reference_ty,
                },
            ) {
                Ok(v) => v,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            tangents = tangents_flat
                .chunks_exact(3)
                .map(|c| [c[0], c[1], c[2]])
                .collect();
        }

        let mut binormals = Vec::new();
        if let Some(el) = attrs.extract_case_insensitive(ATTR_LAYER_ELEMENT_BINORMAL) {
            let map = el.get_children_distinct();
            let (data_name, index_name) = if map.extract_case_insensitive("Binormals").is_some() {
                ("Binormals", "BinormalsIndex")
            } else {
                ("Binormal", "BinormalIndex")
            };
            let mapping_ty = match map
                .require_token_case_insensitive(ATTR_MAPPING_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let reference_ty = match map
                .require_token_case_insensitive(ATTR_REFERENCE_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let binormals_flat = match resolve_flat_f32_channel(
                &map,
                ResolveFlatF32ChannelParams {
                    data_name,
                    index_name,
                    vertex_count,
                    components: 3,
                    mapping_counts: &mapping_counts,
                    mapping_offsets: &mapping_offsets,
                    mappings: &mappings,
                    mapping_ty: mapping_ty,
                    reference_ty: reference_ty,
                },
            ) {
                Ok(v) => v,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            binormals = binormals_flat
                .chunks_exact(3)
                .map(|c| [c[0], c[1], c[2]])
                .collect();
        }

        let mut texture_coords: [Vec<[f32; 2]>; MAX_UV_CHANNELS] = Default::default();
        let mut texture_coord_names: [String; MAX_UV_CHANNELS] = Default::default();

        if let Some(el) = attrs.extract_case_insensitive(ATTR_LAYER_ELEMENT_UV) {
            let map = el.get_children_distinct();
            if let Ok(Some(name)) = map.optional_token_case_insensitive("Name") {
                texture_coord_names[0] = name
                    .trim()
                    .trim_matches(|c| c == '"' || c == '\'')
                    .to_string();
            }
            let mapping_ty = match map
                .require_token_case_insensitive(ATTR_MAPPING_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let reference_ty = match map
                .require_token_case_insensitive(ATTR_REFERENCE_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let uv_flat = match resolve_flat_f32_channel(
                &map,
                ResolveFlatF32ChannelParams {
                    data_name: "UV",
                    index_name: "UVIndex",
                    vertex_count,
                    components: 2,
                    mapping_counts: &mapping_counts,
                    mapping_offsets: &mapping_offsets,
                    mappings: &mappings,
                    mapping_ty: mapping_ty,
                    reference_ty: reference_ty,
                },
            ) {
                Ok(v) => v,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            texture_coords[0] = uv_flat.chunks_exact(2).map(|c| [c[0], c[1]]).collect();
        }

        let mut vertex_colors: [Vec<[f32; 4]>; MAX_COLOR_SETS] = Default::default();
        if let Some(el) = attrs.extract_case_insensitive("LayerElementColor") {
            let map = el.get_children_distinct();
            let mapping_ty = match map
                .require_token_case_insensitive(ATTR_MAPPING_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let reference_ty = match map
                .require_token_case_insensitive(ATTR_REFERENCE_INFORMATION_TYPE)
                .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
            {
                Ok(s) => s,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            let colors_flat = match resolve_flat_f32_channel(
                &map,
                ResolveFlatF32ChannelParams {
                    data_name: "Colors",
                    index_name: "ColorIndex",
                    vertex_count,
                    components: 4,
                    mapping_counts: &mapping_counts,
                    mapping_offsets: &mapping_offsets,
                    mappings: &mappings,
                    mapping_ty: mapping_ty,
                    reference_ty: reference_ty,
                },
            ) {
                Ok(v) => v,
                Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
            };
            vertex_colors[0] = colors_flat
                .chunks_exact(4)
                .map(|c| [c[0], c[1], c[2], c[3]])
                .collect();
        }

        let material_indices =
            if let Some(el) = attrs.extract_case_insensitive("LayerElementMaterial") {
                let map = el.get_children_distinct();
                let mapping_ty = match map
                    .require_token_case_insensitive(ATTR_MAPPING_INFORMATION_TYPE)
                    .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
                {
                    Ok(s) => s,
                    Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
                };
                let reference_ty = match map
                    .require_token_case_insensitive(ATTR_REFERENCE_INFORMATION_TYPE)
                    .map(|s| s.trim().trim_matches(|c| c == '"' || c == '\''))
                {
                    Ok(s) => s,
                    Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
                };
                match read_vertex_data_materials(
                    &map,
                    &face_vertex_counts,
                    vertex_count,
                    &mapping_ty,
                    &reference_ty,
                ) {
                    Ok(v) => v,
                    Err(reason) => return Err(FbxTypeMismatch::new(o, reason)),
                }
            } else {
                Vec::new()
            };

        Ok(MeshGeometry {
            object: o,
            vertices,
            face_vertex_counts,
            normals,
            tangents,
            binormals,
            texture_coords,
            texture_coord_names,
            vertex_colors,
            material_indices,
        })
    }
}

/// Comma-separated float list from `attr` tokens, after optional ASCII **`a:`** child (see `ACCESSOR_KEY`).
fn parse_f32_array(attr: &ElementAttribute) -> Result<Vec<f32>, ParseFloatError> {
    let children = attr.get_children_distinct();
    let payload = children.get(ACCESSOR_KEY).unwrap_or(attr);
    let tokens = payload.get_tokens();
    tokens
        .iter()
        .flat_map(|t| t.split(','))
        .map(|t| t.trim())
        .filter(|t| !t.is_empty())
        .map(|t| t.parse::<f32>())
        .collect()
}

/// Comma-separated `i32` list; same `a:` drill-down as [`parse_f32_array`].
fn parse_i32_array(attr: &ElementAttribute) -> Result<Vec<i32>, ParseIntError> {
    let children = attr.get_children_distinct();
    let payload = children.get(ACCESSOR_KEY).unwrap_or(attr);
    let tokens = payload.get_tokens();
    tokens
        .iter()
        .flat_map(|t| t.split(','))
        .map(|t| t.trim())
        .filter(|t| !t.is_empty())
        .map(|t| t.parse::<i32>())
        .collect()
}

/// Expand FBX mesh indices into a **per-polygon-vertex** (“corner”) linear layout and build tables to
/// remap **per-corner** data from FBX’s indexed vertex pool.
///
/// Mirrors Assimp [`FBXMeshGeometry`](https://github.com/assimp/assimp/blob/master/code/AssetLib/FBX/FBXMeshGeometry.cpp)
/// (`MeshGeometry` ctor after `ParseVectorDataArray`).
///
/// ## FBX encoding (`PolygonVertexIndex`)
///
/// Indices reference rows in `Vertices` (the control-point / vertex pool). The **last** index of each
/// polygon is **negative**; its absolute value is still `absi = (-index - 1)`, marking the end of that
/// face. Non-final corners use non-negative indices. Example triangle `0, 1, -3` uses pool vertices
/// 0, 1, and 2 (`-3` → abs index 2).
///
/// ## Why expand?
///
/// Layer data (normals, UVs, …) is often stored **ByPolygonVertex** or **ByVertice** with a different
/// indexing than the raw pool. Assimp duplicates pool positions so each **corner** gets its own row in
/// `expanded_vertices` (length = number of polygon corners = `temp_faces.len()` in well-formed files).
/// We must also know, for each pool index `i`, which contiguous slice of “corner slots” belongs to
/// that pool vertex so channels can be scattered into the expanded order.
///
/// ## Outputs
///
/// - **`expanded_vertices`**: `temp_verts[absi]` appended once per corner, in `temp_faces` order
///   (ignoring sign on the last index of each face).
/// - **`face_vertex_counts`**: number of corners per polygon (from running `count` reset on each
///   negative index).
/// - **`mapping_offsets[i]`**: start offset in corner space for pool vertex `i` (prefix sum of how
///   many corners reference pool vertex `i`).
/// - **`mapping_counts`**: after this function returns, again the number of corners referencing each
///   pool vertex `i` (same as after pass 1; rebuilt during pass 3).
/// - **`mappings`**: length = corner count. For each corner in `temp_faces` order (global corner
///   index `cursor`), `mappings[slot] = cursor` where `slot` is the next free slot in the slice
///   `[mapping_offsets[absi] ..)` reserved for pool vertex `absi`. So `mappings` ties pool-vertex
///   corner slots to the global expanded corner index for channel gather/scatter in
///   [`resolve_flat_f32_channel`].
///
/// ## Three passes (same as Assimp)
///
/// 1. **Walk `temp_faces` once**: append expanded positions; increment `mapping_counts[absi]` per
///    reference; accumulate polygon sizes into `face_vertex_counts` when hitting a negative index.
/// 2. **Prefix-sum `mapping_counts` → `mapping_offsets`**, then zero `mapping_counts` (slots will be
///    filled in pass 3).
/// 3. **Walk `temp_faces` again**: for each corner, assign `mappings[mapping_offsets[absi] +
///    mapping_counts[absi]++] = global_corner_index` so each pool vertex’s corners occupy a stable,
///    contiguous block in corner index space.
fn expand_mesh_polygon_vertices(
    temp_verts: &[[f32; 3]],
    temp_faces: &[i32],
) -> Result<(Vec<[f32; 3]>, Vec<u32>, Vec<u32>, Vec<u32>, Vec<u32>), FbxTryFromReason> {
    let vertex_count = temp_verts.len();
    let mut mapping_counts = vec![0u32; vertex_count];
    let mut expanded_vertices = Vec::new();
    let mut face_vertex_counts = Vec::new();
    let mut count = 0u32;

    // Pass 1 — see module-level docs on [`expand_mesh_polygon_vertices`].
    for &index in temp_faces {
        // Get absolute index
        let absi = if index < 0 {
            (-index - 1) as usize
        } else {
            index as usize
        };
        if absi >= vertex_count {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: ATTR_POLYGON_VERTEX_INDEX.to_string(),
                detail: format!("index {absi} out of range (vertex count {vertex_count})"),
            });
        }
        // Add vertex to expanded vertices
        expanded_vertices.push(temp_verts[absi]);
        count += 1;
        // Update the running count of how many vertices are expanded.
        mapping_counts[absi] = mapping_counts[absi].saturating_add(1);
        if index < 0 {
            // Count the index difference between this and the last negative index.
            face_vertex_counts.push(count);
            count = 0;
        }
    }

    let polygon_vertex_count = expanded_vertices.len();
    // Pass 2 — prefix sums into mapping_offsets; clear mapping_counts for pass 3 slot filling.
    let mut mapping_offsets = vec![0u32; vertex_count];
    let mut cursor = 0u32;
    for i in 0..vertex_count {
        mapping_offsets[i] = cursor;
        cursor += mapping_counts[i];
        mapping_counts[i] = 0;
    }

    // Pass 3 — for each corner in order, assign stable slot in per-pool-vertex ranges (see doc above).
    let mut mappings = vec![0u32; polygon_vertex_count];
    cursor = 0;
    for &index in temp_faces {
        let absi = if index < 0 {
            (-index - 1) as usize
        } else {
            index as usize
        };
        let slot = mapping_offsets[absi] + mapping_counts[absi];
        mapping_counts[absi] += 1;
        mappings[slot as usize] = cursor;
        cursor += 1;
    }

    Ok((
        expanded_vertices,
        face_vertex_counts,
        mapping_counts,
        mapping_offsets,
        mappings,
    ))
}

pub struct ResolveFlatF32ChannelParams<'a> {
    data_name: &'a str,
    index_name: &'a str,
    vertex_count: usize,
    components: usize,
    mapping_counts: &'a [u32],
    mapping_offsets: &'a [u32],
    mappings: &'a [u32],
    mapping_ty: &'a str,
    reference_ty: &'a str,
}

/// Turn one mesh layer attribute (e.g. normals, UVs) into a flat `f32` slice **one scalar wide per
/// expanded corner × `components`**, matching Assimp-style expanded mesh layout.
///
/// FBX combines **mapping** and **reference** (see `mapping_ty` / `reference_ty`):
/// - **ByVertice** — one logical value per **pool** vertex; we scatter it to every expanded corner
///   that references that pool vertex using `mapping_offsets` / `mapping_counts` / `mappings`.
/// - **ByPolygonVertex** — one value per polygon corner; output order is already corner-linear.
/// - **Direct** — floats live in the data array in mapping order.
/// - **IndexToDirect** — a parallel `i32` index array picks **which element** (group of `components`
///   floats) to copy from the data array; index `-1` means “no value” (zeros for that corner’s group).
///
/// All copies use `src..src+components` and `dst..dst+components` so multi-component channels stay aligned.
fn resolve_flat_f32_channel(
    source: &HashMap<String, ElementAttribute>,
    params: ResolveFlatF32ChannelParams<'_>,
) -> Result<Vec<f32>, FbxTryFromReason> {
    let mut is_direct = params.reference_ty.eq_ignore_ascii_case(REFERENCE_DIRECT);
    let mut is_index_to_direct = params
        .reference_ty
        .eq_ignore_ascii_case(REFERENCE_INDEX_TO_DIRECT);
    let has_data = source.extract_case_insensitive(params.data_name).is_some();
    let has_index = source.extract_case_insensitive(params.index_name).is_some();
    // Some files declare IndexToDirect but omit the index channel; treat as Direct (Assimp-style).
    if is_index_to_direct && !has_index {
        is_direct = true;
        is_index_to_direct = false;
    }

    if params.components == 0 {
        return Ok(Vec::new());
    }
    // Linear layout: corner `k` occupies `vertex_out[k*components .. (k+1)*components)`.
    let mut vertex_out = vec![0f32; params.vertex_count * params.components];

    let by_vertice = params.mapping_ty.eq_ignore_ascii_case(MAPPING_BY_VERTICE);
    let by_polygon_vertex = params
        .mapping_ty
        .eq_ignore_ascii_case(MAPPING_BY_POLYGON_VERTEX);

    if by_vertice && is_direct {
        // Case 1: ByVertice + Direct — `channel_data` is one vector per pool vertex; broadcast each
        // to every expanded corner slot that references that pool vertex.
        if !has_data {
            return Ok(Vec::new());
        }
        let channel_attribute = source.extract_case_insensitive(params.data_name).ok_or(
            FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: "data channel not found".to_string(),
            },
        )?;
        let channel_data_result = parse_f32_array(&channel_attribute);
        let Ok(channel_data) = channel_data_result else {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: format!("invalid float token: {}", channel_data_result.unwrap_err()),
            });
        };
        if channel_data.len() != params.mapping_offsets.len() * params.components {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: format!(
                    "{} {}: expected {} floats, got {}",
                    MAPPING_BY_VERTICE,
                    REFERENCE_DIRECT,
                    params.mapping_offsets.len() * params.components,
                    channel_data.len()
                ),
            });
        }
        for i in 0..params.mapping_offsets.len() {
            // Pool vertex `i` owns `channel_data[i*components ..)`; copy that block to each corner.
            let src = i * params.components;
            let istart = params.mapping_offsets[i] as usize;
            let iend = istart + params.mapping_counts[i] as usize;
            for j in istart..iend {
                // `mappings[j]` is the global expanded-corner index for this use of pool vertex `i`.
                let dst = params.mappings[j] as usize * params.components;
                if src + params.components > channel_data.len()
                    || dst + params.components > vertex_out.len()
                {
                    return Err(FbxTryFromReason::InvalidAttributeFormat {
                        name: params.data_name.to_string(),
                        detail: format!("length mismatch for {MAPPING_BY_VERTICE}"),
                    });
                }
                vertex_out[dst..dst + params.components]
                    .copy_from_slice(&channel_data[src..src + params.components]);
            }
        }
    } else if by_vertice && is_index_to_direct {
        // Case 2: ByVertice + IndexToDirect — same scatter as case 1, but each pool vertex picks a
        // data element index first (`channel_index_data[i]`); that element is a `components`-wide slice.
        if !has_data || !has_index {
            return Ok(Vec::new());
        }

        let channel_attribute = source.extract_case_insensitive(params.data_name).ok_or(
            FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: "data channel not found".to_string(),
            },
        )?;
        let channel_data_result = parse_f32_array(&channel_attribute);
        let Ok(channel_data) = channel_data_result else {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: format!("invalid float token: {}", channel_data_result.unwrap_err()),
            });
        };
        let channel_index_attribute = source.extract_case_insensitive(params.index_name).ok_or(
            FbxTryFromReason::InvalidAttributeFormat {
                name: params.index_name.to_string(),
                detail: "index channel not found".to_string(),
            },
        )?;
        let channel_index_data_result = parse_i32_array(&channel_index_attribute);
        let Ok(channel_index_data) = channel_index_data_result else {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.index_name.to_string(),
                detail: format!(
                    "invalid int token: {}",
                    channel_index_data_result.unwrap_err()
                ),
            });
        };
        if channel_index_data.len() != params.mapping_offsets.len() {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.index_name.to_string(),
                detail: format!("length mismatch for {MAPPING_BY_VERTICE}"),
            });
        }
        for i in 0..params.mapping_offsets.len() {
            let idx = channel_index_data[i] as usize;
            let src = idx * params.components; // element `idx` in the direct table
            let istart = params.mapping_offsets[i] as usize;
            let iend = istart + params.mapping_counts[i] as usize;
            for j in istart..iend {
                let dst = params.mappings[j] as usize * params.components;
                if src + params.components > channel_data.len()
                    || dst + params.components > vertex_out.len()
                {
                    return Err(FbxTryFromReason::InvalidAttributeFormat {
                        name: params.data_name.to_string(),
                        detail: format!("length mismatch for {MAPPING_BY_VERTICE}"),
                    });
                }
                vertex_out[dst..dst + params.components]
                    .copy_from_slice(&channel_data[src..src + params.components]);
            }
        }
    } else if by_polygon_vertex && is_direct {
        // Case 3: ByPolygonVertex + Direct — floats are already in expanded-corner order; one memcpy.
        if !has_data {
            return Ok(Vec::new());
        }
        let channel_attribute = source.extract_case_insensitive(params.data_name).ok_or(
            FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: "data channel not found".to_string(),
            },
        )?;
        let channel_data_result = parse_f32_array(&channel_attribute);
        let Ok(channel_data) = channel_data_result else {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: format!("invalid float token: {}", channel_data_result.unwrap_err()),
            });
        };
        if channel_data.len() != params.vertex_count * params.components {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: format!(
                    "{} {}: expected {} floats, got {}",
                    MAPPING_BY_POLYGON_VERTEX,
                    REFERENCE_DIRECT,
                    params.vertex_count * params.components,
                    channel_data.len()
                ),
            });
        }
        vertex_out = channel_data;
    } else if by_polygon_vertex && is_index_to_direct {
        // Case 4: ByPolygonVertex + IndexToDirect — one index per corner; each index selects a
        // `components`-wide slice in `channel_data`, laid out at `slot * components` in `vertex_out`.
        if !has_data || !has_index {
            return Ok(Vec::new());
        }
        // Get data and attributes for the data and index keys.
        let channel_attribute = source.extract_case_insensitive(params.data_name).ok_or(
            FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: "data channel not found".to_string(),
            },
        )?;
        let channel_data_result = parse_f32_array(&channel_attribute);
        let Ok(channel_data) = channel_data_result else {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.data_name.to_string(),
                detail: format!("invalid float token: {}", channel_data_result.unwrap_err()),
            });
        };
        let channel_index_attribute = source.extract_case_insensitive(params.index_name).ok_or(
            FbxTryFromReason::InvalidAttributeFormat {
                name: params.index_name.to_string(),
                detail: "index channel not found".to_string(),
            },
        )?;
        let channel_index_data_result = parse_i32_array(&channel_index_attribute);
        let Ok(mut channel_index_data) = channel_index_data_result else {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.index_name.to_string(),
                detail: format!(
                    "invalid int token: {}",
                    channel_index_data_result.unwrap_err()
                ),
            });
        };
        if channel_index_data.len() > params.vertex_count {
            channel_index_data.truncate(params.vertex_count);
        }
        // After optional truncation, require exactly one index per expanded corner.
        if channel_index_data.len() != params.vertex_count {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: params.index_name.to_string(),
                detail: format!(
                    "{} {}: expected {} indices, got {}",
                    MAPPING_BY_POLYGON_VERTEX,
                    REFERENCE_INDEX_TO_DIRECT,
                    params.vertex_count,
                    channel_index_data.len()
                ),
            });
        }
        for (slot, &i) in channel_index_data.iter().enumerate() {
            let dst = slot * params.components; // corner `slot` in expanded order
            if dst + params.components > vertex_out.len() {
                return Err(FbxTryFromReason::InvalidAttributeFormat {
                    name: params.data_name.to_string(),
                    detail: format!("length mismatch for {MAPPING_BY_POLYGON_VERTEX}"),
                });
            }
            if i == -1 {
                vertex_out[dst..dst + params.components].fill(0.0);
                continue;
            }
            let src = i as usize * params.components; // direct-table element `i`
            if src + params.components > channel_data.len() {
                return Err(FbxTryFromReason::InvalidAttributeFormat {
                    name: params.data_name.to_string(),
                    detail: format!("length mismatch for {MAPPING_BY_POLYGON_VERTEX}"),
                });
            }
            vertex_out[dst..dst + params.components]
                .copy_from_slice(&channel_data[src..src + params.components]);
        }
    } else {
        // Case 5: Unsupported mapping/reference pair — caller gets empty channel (Assimp skips similarly).
        return Ok(Vec::new());
    }
    Ok(vertex_out)
}

/// Assimp `ReadVertexDataMaterials` (subset): `AllSame` and `ByPolygon` + `IndexToDirect`.
fn read_vertex_data_materials(
    source: &HashMap<String, ElementAttribute>,
    face_vertex_counts: &[u32],
    polygon_vertex_count: usize,
    mapping_ty: &str,
    reference_ty: &str,
) -> Result<Vec<i32>, FbxTryFromReason> {
    // Face count guard
    let face_count = face_vertex_counts.len();
    if face_count == 0 {
        return Ok(Vec::new());
    }

    // Extract Materials
    let Some(mat_el) = source.extract_case_insensitive("Materials") else {
        return Ok(Vec::new());
    };
    let materials_out_result = parse_i32_array(mat_el);
    let Ok(materials_out) = materials_out_result else {
        return Err(FbxTryFromReason::InvalidAttributeFormat {
            name: ATTR_MATERIALS.to_string(),
            detail: format!("invalid int token: {}", materials_out_result.unwrap_err()),
        });
    };

    if mapping_ty.eq_ignore_ascii_case(MAPPING_ALL_SAME) {
        // Case 1: Map type is AllSame
        // All materials are the same, so return a mapping of all polygons to the same material.
        if materials_out.is_empty() {
            return Ok(Vec::new());
        }
        let count_neg = materials_out.iter().filter(|&&n| n < 0).count();
        if count_neg == materials_out.len() {
            return Ok(Vec::new());
        }
        let v = materials_out[0];
        Ok(vec![v; polygon_vertex_count])
    } else if mapping_ty.eq_ignore_ascii_case(MAPPING_BY_POLYGON)
        && reference_ty.eq_ignore_ascii_case(REFERENCE_INDEX_TO_DIRECT)
    {
        // Case 2: Map type is ByPolygon and reference type is IndexToDirect
        // The materials are indexed by the face index, so we need to map the materials to the vertices.
        if materials_out.len() != face_count {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: ATTR_MATERIALS.to_string(),
                detail: format!(
                    "{}: expected {} material indices, got {}",
                    MAPPING_BY_POLYGON,
                    face_count,
                    materials_out.len()
                ),
            });
        }
        let count_neg = materials_out.iter().filter(|&&n| n < 0).count();
        if count_neg == materials_out.len() {
            return Ok(Vec::new());
        }
        let mut per_corner = Vec::with_capacity(polygon_vertex_count);
        for (&m, &n) in materials_out.iter().zip(face_vertex_counts.iter()) {
            for _ in 0..n {
                per_corner.push(m);
            }
        }
        if per_corner.len() != polygon_vertex_count {
            return Err(FbxTryFromReason::InvalidAttributeFormat {
                name: ATTR_MATERIALS.to_string(),
                detail: format!(
                    "expanded material indices length {} != polygon vertex count {}",
                    per_corner.len(),
                    polygon_vertex_count
                ),
            });
        }
        Ok(per_corner)
    } else {
        Ok(Vec::new())
    }
}

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

    use fbxscii::{
        Element, ElementAmphitheatre, ElementAttribute, LeafAttribute, SubTreeAttribute,
    };

    use crate::OwnedObject;

    use super::super::{
        FbxTryFromReason, GEOMETRY_LINE_CLASS_NAME, GEOMETRY_MESH_CLASS_NAME, GEOMETRY_TYPE_NAME,
    };
    use super::MeshGeometry;

    fn leaf(tokens: &[&str]) -> ElementAttribute {
        ElementAttribute::Leaf(Box::new(LeafAttribute {
            key: String::new(),
            tokens: tokens.iter().map(|s| (*s).to_string()).collect(),
        }))
    }

    fn append_layer_string_child(
        arena: &mut ElementAmphitheatre,
        root_idx: usize,
        key: &str,
        token: &str,
    ) {
        let mut el = Element::new(key.to_string());
        el.tokens = vec![token.to_string()];
        el.parent_index = Some(root_idx);
        let idx = arena.insert(el);
        arena.get_mut(root_idx).unwrap().children.push(idx);
    }

    /// Same nesting as ASCII FBX (e.g. `duck.fbx`): mapping + reference under the layer root, then data.
    fn layer_element_normal(mapping: &str, reference: &str, normals_csv: &str) -> ElementAttribute {
        let mut arena = ElementAmphitheatre::new();
        let root_idx = arena.insert(Element::new("LayerElementNormal".into()));
        append_layer_string_child(
            &mut arena,
            root_idx,
            super::ATTR_MAPPING_INFORMATION_TYPE,
            mapping,
        );
        append_layer_string_child(
            &mut arena,
            root_idx,
            super::ATTR_REFERENCE_INFORMATION_TYPE,
            reference,
        );
        let mut normals_el = Element::new("Normals".into());
        normals_el.tokens = vec![normals_csv.to_string()];
        normals_el.parent_index = Some(root_idx);
        let normals_idx = arena.insert(normals_el);
        arena.get_mut(root_idx).unwrap().children.push(normals_idx);

        ElementAttribute::SubTree(Box::new(SubTreeAttribute {
            amphitheatre: arena,
            root_element_index: root_idx,
        }))
    }

    fn layer_element_material(
        mapping: &str,
        reference: &str,
        materials_csv: &str,
    ) -> ElementAttribute {
        let mut arena = ElementAmphitheatre::new();
        let root_idx = arena.insert(Element::new("LayerElementMaterial".into()));
        append_layer_string_child(
            &mut arena,
            root_idx,
            super::ATTR_MAPPING_INFORMATION_TYPE,
            mapping,
        );
        append_layer_string_child(
            &mut arena,
            root_idx,
            super::ATTR_REFERENCE_INFORMATION_TYPE,
            reference,
        );
        let mut mats_el = Element::new(super::ATTR_MATERIALS.into());
        mats_el.tokens = vec![materials_csv.to_string()];
        mats_el.parent_index = Some(root_idx);
        let mats_idx = arena.insert(mats_el);
        arena.get_mut(root_idx).unwrap().children.push(mats_idx);

        ElementAttribute::SubTree(Box::new(SubTreeAttribute {
            amphitheatre: arena,
            root_element_index: root_idx,
        }))
    }

    fn owned_mesh(attrs: HashMap<String, ElementAttribute>) -> OwnedObject {
        OwnedObject {
            object_index: 11,
            name: "Geometry::TestMesh".into(),
            type_name: GEOMETRY_TYPE_NAME.into(),
            class_name: GEOMETRY_MESH_CLASS_NAME.into(),
            properties: HashMap::new(),
            attributes: attrs,
            connected_object_ids: vec![],
            object_property_targets: vec![],
            pp_property_targets: HashMap::new(),
        }
    }

    fn minimal_base_attrs() -> HashMap<String, ElementAttribute> {
        let mut attrs = HashMap::new();
        attrs.insert("Vertices".into(), leaf(&["0,0,0,1,0,0,0,1,0"]));
        attrs.insert("PolygonVertexIndex".into(), leaf(&["0,1,-3"]));
        attrs
    }

    #[test]
    fn minimal_triangle_expands_corners() {
        let attrs = minimal_base_attrs();
        let mesh = MeshGeometry::try_from(owned_mesh(attrs)).unwrap();
        assert_eq!(mesh.vertices.len(), 3);
        assert_eq!(mesh.face_vertex_counts, vec![3u32]);
        assert!(mesh.normals.is_empty());
        assert!(mesh.material_indices.is_empty());
    }

    #[test]
    fn mapping_and_reference_trim_quotes_and_lowercase_keys() {
        let mut attrs = minimal_base_attrs();
        let mut arena = ElementAmphitheatre::new();
        let root_idx = arena.insert(Element::new("LayerElementNormal".into()));
        append_layer_string_child(
            &mut arena,
            root_idx,
            "mappinginformationtype",
            "  \"ByPolygonVertex\"  ",
        );
        append_layer_string_child(&mut arena, root_idx, "referenceinformationtype", "'Direct'");
        let mut normals_el = Element::new("Normals".into());
        normals_el.tokens = vec!["0,0,1,0,0,1,0,0,1".to_string()];
        normals_el.parent_index = Some(root_idx);
        let normals_idx = arena.insert(normals_el);
        arena.get_mut(root_idx).unwrap().children.push(normals_idx);
        attrs.insert(
            "LayerElementNormal".into(),
            ElementAttribute::SubTree(Box::new(SubTreeAttribute {
                amphitheatre: arena,
                root_element_index: root_idx,
            })),
        );
        let mesh = MeshGeometry::try_from(owned_mesh(attrs)).unwrap();
        assert_eq!(mesh.vertices.len(), 3);
        assert_eq!(mesh.normals.len(), 3);
    }

    #[test]
    fn normals_by_polygon_vertex_direct() {
        let mut attrs = minimal_base_attrs();
        attrs.insert(
            "LayerElementNormal".into(),
            layer_element_normal("ByPolygonVertex", "Direct", "0,0,1,0,0,1,0,0,1"),
        );
        let mesh = MeshGeometry::try_from(owned_mesh(attrs)).unwrap();
        assert_eq!(
            mesh.normals,
            vec![[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0],]
        );
    }

    #[test]
    fn materials_all_same_replicates_first_index() {
        let mut attrs = minimal_base_attrs();
        attrs.insert(
            "LayerElementMaterial".into(),
            layer_element_material("AllSame", "IndexToDirect", "5"),
        );
        let mesh = MeshGeometry::try_from(owned_mesh(attrs)).unwrap();
        assert_eq!(mesh.material_indices, vec![5, 5, 5]);
    }

    #[test]
    fn wrong_object_kind_line_geometry() {
        let o = OwnedObject {
            object_index: 1,
            name: "G".into(),
            type_name: GEOMETRY_TYPE_NAME.into(),
            class_name: GEOMETRY_LINE_CLASS_NAME.into(),
            properties: HashMap::new(),
            attributes: HashMap::new(),
            connected_object_ids: vec![],
            object_property_targets: vec![],
            pp_property_targets: HashMap::new(),
        };
        let err = MeshGeometry::try_from(o).unwrap_err();
        assert!(matches!(
            err.reason,
            FbxTryFromReason::WrongObjectKind { ref expected, .. } if expected == "MeshGeometry"
        ));
    }

    #[test]
    fn missing_mapping_information_type() {
        let mut attrs = minimal_base_attrs();
        let mut arena = ElementAmphitheatre::new();
        let root_idx = arena.insert(Element::new("LayerElementNormal".into()));
        append_layer_string_child(
            &mut arena,
            root_idx,
            super::ATTR_REFERENCE_INFORMATION_TYPE,
            "Direct",
        );
        let mut normals_el = Element::new("Normals".into());
        normals_el.tokens = vec!["0,0,1,0,0,1,0,0,1".to_string()];
        normals_el.parent_index = Some(root_idx);
        let normals_idx = arena.insert(normals_el);
        arena.get_mut(root_idx).unwrap().children.push(normals_idx);
        attrs.insert(
            "LayerElementNormal".into(),
            ElementAttribute::SubTree(Box::new(SubTreeAttribute {
                amphitheatre: arena,
                root_element_index: root_idx,
            })),
        );
        let err = MeshGeometry::try_from(owned_mesh(attrs)).unwrap_err();
        assert!(matches!(
            err.reason,
            FbxTryFromReason::MissingAttribute { ref name } if name == "MappingInformationType"
        ));
    }

    #[test]
    fn missing_reference_information_type() {
        let mut attrs = minimal_base_attrs();
        let mut arena = ElementAmphitheatre::new();
        let root_idx = arena.insert(Element::new("LayerElementNormal".into()));
        append_layer_string_child(
            &mut arena,
            root_idx,
            super::ATTR_MAPPING_INFORMATION_TYPE,
            "ByPolygonVertex",
        );
        let mut normals_el = Element::new("Normals".into());
        normals_el.tokens = vec!["0,0,1,0,0,1,0,0,1".to_string()];
        normals_el.parent_index = Some(root_idx);
        let normals_idx = arena.insert(normals_el);
        arena.get_mut(root_idx).unwrap().children.push(normals_idx);
        attrs.insert(
            "LayerElementNormal".into(),
            ElementAttribute::SubTree(Box::new(SubTreeAttribute {
                amphitheatre: arena,
                root_element_index: root_idx,
            })),
        );
        let err = MeshGeometry::try_from(owned_mesh(attrs)).unwrap_err();
        assert!(matches!(
            err.reason,
            FbxTryFromReason::MissingAttribute { ref name } if name == "ReferenceInformationType"
        ));
    }
}