fcb_core 0.7.6

FlatCityBuf is a library for reading and writing CityJSON with FlatBuffers. Contains code derived from FlatGeobuf (BSD-2-Clause) for spatial indexing.
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
use cjseq::{
    Boundaries as CjBoundaries, MaterialReference as CjMaterialReference,
    MaterialValues as CjMaterialValues, Semantics as CjSemantics,
    SemanticsSurface as CjSemanticsSurface, SemanticsValues as CjSemanticsValues,
    TextureReference as CjTextureReference, TextureValues as CjTextureValues,
};
use std::collections::HashMap;

#[derive(Debug, Clone, Default)]
pub(crate) struct GMBoundaries {
    pub(crate) solids: Vec<u32>,   // Number of shells per solid
    pub(crate) shells: Vec<u32>,   // Number of surfaces per shell
    pub(crate) surfaces: Vec<u32>, // Number of rings per surface
    pub(crate) strings: Vec<u32>,  // Number of indices per ring
    pub(crate) indices: Vec<u32>,  // Flattened list of all indices
}

#[derive(Debug, Clone, Default)]
pub struct MaterialValues {
    pub(crate) theme: String,
    pub(crate) solids: Vec<u32>, // length of this vector is the same as the number of solids, and the value is the length of the shells vector.
    pub(crate) shells: Vec<u32>, // length of this vector is the same as the number of shells, and the value is the length of the surfaces vector.
    pub(crate) vertices: Vec<u32>, // length of this vector is the same as the number of vertices, and the value is the material index.
}

#[derive(Debug, Clone, Default)]
pub struct MaterialValue {
    pub(crate) theme: String,
    pub(crate) value: u32,
}

#[derive(Debug, Clone)]
pub enum MaterialMapping {
    Value(MaterialValue),
    Values(MaterialValues),
}

#[derive(Debug, Clone, Default)]
pub(crate) struct TextureMapping {
    pub(crate) theme: String,
    pub(crate) solids: Vec<u32>,   // Number of shells per solid
    pub(crate) shells: Vec<u32>,   // Number of surfaces per shell
    pub(crate) surfaces: Vec<u32>, // Number of rings per surface
    pub(crate) strings: Vec<u32>,  // Number of indices per ring
    pub(crate) vertices: Vec<u32>, // Flattened list of all indices
}

#[derive(Debug, Clone, Default)]
pub(crate) struct GMSemantics {
    pub(crate) surfaces: Vec<CjSemanticsSurface>, // List of semantic surfaces
    pub(crate) values: Vec<u32>,                  // Semantic values corresponding to surfaces
}

#[derive(Debug, Clone, Default)]
#[doc(hidden)]
pub(crate) struct EncodedGeometry {
    pub(crate) boundaries: GMBoundaries,
    pub(crate) semantics: Option<GMSemantics>,
    pub(crate) textures: Option<Vec<TextureMapping>>,
    pub(crate) materials: Option<Vec<MaterialMapping>>,
}

/// Encodes the provided CityJSON boundaries and semantics into flattened arrays.
///
/// # Arguments
///
/// * `boundaries` - Reference to the CityJSON boundaries to encode.
/// * `semantics` - Optional reference to the semantics associated with the boundaries.
/// * `textures` - Optional reference to the textures associated with the boundaries.
/// * `materials` - Optional reference to the materials associated with the boundaries.
///
/// # Returns
/// Nothing.
pub(crate) fn encode(
    cj_boundaries: &CjBoundaries,
    semantics: Option<&CjSemantics>,
    textures: Option<&HashMap<String, CjTextureReference>>,
    materials: Option<&HashMap<String, CjMaterialReference>>,
) -> EncodedGeometry {
    let mut boundaries = GMBoundaries {
        solids: vec![],
        shells: vec![],
        surfaces: vec![],
        strings: vec![],
        indices: vec![],
    };
    // Encode the geometric boundaries
    let _ = encode_boundaries(cj_boundaries, &mut boundaries);

    // Encode semantics if provided
    let semantics = semantics.map(encode_semantics);

    // Encode appearance if provided
    let textures = textures.map(encode_texture);
    let materials = materials.map(encode_material);
    EncodedGeometry {
        boundaries,
        semantics,
        materials,
        textures,
    }
}

/// Encodes the CityJSON appearance data into our internal representation
///
/// # Arguments
///
/// * `appearance` - Reference to the CityJSON appearance object
///
/// # Returns
/// A GMAppearance containing the encoded appearance data
pub(crate) fn encode_material(
    materials: &HashMap<String, CjMaterialReference>,
) -> Vec<MaterialMapping> {
    let mut material_mappings = Vec::new();
    for (theme, material) in materials {
        if let Some(value) = material.value {
            // Handle single material value
            let mapping = MaterialMapping::Value(MaterialValue {
                theme: theme.clone(),
                value: value as u32,
            });
            material_mappings.push(mapping);
        } else if let Some(values) = &material.values {
            // Handle material values array
            let mut material_values = MaterialValues {
                theme: theme.clone(),
                solids: Vec::new(),
                shells: Vec::new(),
                vertices: Vec::new(),
            };

            // Process the material values based on their structure
            match values {
                // For MultiSurface/CompositeSurface: values is array of indices
                CjMaterialValues::Indices(indices) => {
                    // Convert indices to u32, replacing None with u32::MAX
                    material_values
                        .vertices
                        .extend(indices.iter().map(|i| i.map_or(u32::MAX, |v| v as u32)));
                }
                // For Solid/MultiSolid/CompositeSolid: values is nested array
                CjMaterialValues::Nested(nested) => {
                    // Check if this is a CompositeSolid (nested contains Nested) or a Solid (nested contains Indices)
                    let is_composite_solid = nested
                        .iter()
                        .any(|v| matches!(v, CjMaterialValues::Nested(_)));

                    if is_composite_solid {
                        // CompositeSolid case (test case 5)
                        // For each solid in the nested array
                        for solid in nested {
                            if let CjMaterialValues::Nested(shells_array) = solid {
                                // Push the number of shells in this solid
                                material_values.solids.push(shells_array.len() as u32);

                                // Process each shell
                                for shell in shells_array {
                                    if let CjMaterialValues::Indices(indices) = shell {
                                        material_values.shells.push(indices.len() as u32);
                                        material_values.vertices.extend(
                                            indices
                                                .iter()
                                                .map(|i| i.map_or(u32::MAX, |v| v as u32)),
                                        );
                                    }
                                }
                            }
                        }
                    } else {
                        // Solid case (test case 3)
                        // This is a single solid with multiple shells
                        material_values.solids.push(nested.len() as u32);

                        // Process each shell
                        for shell in nested {
                            if let CjMaterialValues::Indices(indices) = shell {
                                material_values.shells.push(indices.len() as u32);
                                material_values.vertices.extend(
                                    indices.iter().map(|i| i.map_or(u32::MAX, |v| v as u32)),
                                );
                            }
                        }
                    }
                }
            }

            material_mappings.push(MaterialMapping::Values(material_values));
        }
    }
    material_mappings
}

pub(crate) fn encode_texture(
    texture_map: &HashMap<String, CjTextureReference>,
) -> Vec<TextureMapping> {
    let mut texture_mappings = Vec::new();

    for (theme, texture) in texture_map {
        let mut texture_mapping = TextureMapping {
            theme: theme.clone(),
            solids: Vec::new(),
            shells: Vec::new(),
            surfaces: Vec::new(),
            strings: Vec::new(),
            vertices: Vec::new(),
        };

        // Process the texture values based on their structure
        let _ = encode_texture_values(&texture.values, &mut texture_mapping);

        texture_mappings.push(texture_mapping);
    }

    texture_mappings
}

/// Recursively encodes the texture values into flattened arrays.
///
/// # Arguments
///
/// * `values` - Reference to the texture values to encode.
/// * `mapping` - Mutable reference to the TextureMapping struct to populate.
///
/// # Returns
///
/// The maximum depth encountered during encoding.
fn encode_texture_values(values: &CjTextureValues, mapping: &mut TextureMapping) -> usize {
    match values {
        // Leaf node (indices)
        CjTextureValues::Indices(indices) => {
            // Record the number of indices in this ring
            mapping.strings.push(indices.len() as u32);

            // Convert indices to u32, replacing None with u32::MAX
            mapping
                .vertices
                .extend(indices.iter().map(|i| i.map_or(u32::MAX, |v| v as u32)));

            // Return the current depth level (1 for rings)
            1 // ring-level
        }
        // Nested structure
        CjTextureValues::Nested(nested) => {
            let mut max_depth = 0;

            // Recursively encode each nested value and track the maximum depth
            for sub in nested {
                let d = encode_texture_values(sub, mapping);
                max_depth = max_depth.max(d);
            }

            // Number of nested values at the current level
            let length = nested.len();

            // Interpret the `max_depth` to determine the current geometry type
            match max_depth {
                // max_depth = 1 indicates the children are rings, so this level represents surfaces
                1 => {
                    mapping.surfaces.push(length as u32);
                }
                // max_depth = 2 indicates the children are surfaces, so this level represents shells
                2 => {
                    mapping.shells.push(length as u32);
                }
                // max_depth = 3 indicates the children are shells, so this level represents solids
                3 => {
                    mapping.solids.push(length as u32);
                }
                // Any other depth is invalid and should be ignored
                _ => {}
            }

            // Return the updated depth level
            max_depth + 1
        }
    }
}

/// Recursively encodes the CityJSON boundaries into flattened arrays.
///
/// # Arguments
///
/// * `boundaries` - Reference to the CityJSON boundaries to encode.
///
/// # Returns
///
/// The maximum depth encountered during encoding.
///
/// # Panics
///
/// Panics if the `max_depth` is not 1, 2, or 3, indicating an invalid geometry nesting depth.
fn encode_boundaries(boundaries: &CjBoundaries, wip_boundaries: &mut GMBoundaries) -> usize {
    match boundaries {
        // ------------------
        // (1) Leaf (indices)
        // ------------------
        CjBoundaries::Indices(indices) => {
            // Extend the flat list of indices with the current ring's indices
            wip_boundaries.indices.extend_from_slice(indices);

            // Record the number of indices in the current ring
            wip_boundaries.strings.push(indices.len() as u32);

            // Return the current depth level (1 for rings)
            1 // ring-level
        }
        // ------------------
        // (2) Nested
        // ------------------
        CjBoundaries::Nested(sub_boundaries) => {
            let mut max_depth = 0;

            // Recursively encode each sub-boundary and track the maximum depth
            for sub in sub_boundaries {
                let d = encode_boundaries(sub, wip_boundaries);
                max_depth = max_depth.max(d);
            }

            // Number of sub-boundaries at the current level
            let length = sub_boundaries.len();

            // Interpret the `max_depth` to determine the current geometry type
            match max_depth {
                // max_depth = 1 indicates the children are rings, so this level represents surfaces
                1 => {
                    wip_boundaries.surfaces.push(length as u32);
                }
                // max_depth = 2 indicates the children are surfaces, so this level represents shells
                2 => {
                    // Push the number of surfaces in this shell
                    wip_boundaries.shells.push(length as u32);
                }
                // max_depth = 3 indicates the children are shells, so this level represents solids
                3 => {
                    // Push the number of shells in this solid
                    wip_boundaries.solids.push(length as u32);
                }
                // Any other depth is invalid and should panic
                _ => {}
            }

            // Return the updated depth level
            max_depth + 1
        }
    }
}

/// Encodes the semantic values into the encoder.
///
/// # Arguments
///
/// * `semantics_values` - Reference to the `SemanticsValues` to encode.
/// * `flattened` - Mutable reference to a vector where flattened semantics will be stored.
///
/// # Returns
///
/// The number of semantic values encoded.
fn encode_semantics_values(
    semantics_values: &CjSemanticsValues,
    flattened: &mut Vec<u32>,
) -> usize {
    match semantics_values {
        // ------------------
        // (1) Leaf (Indices)
        // ------------------
        CjSemanticsValues::Indices(indices) => {
            // Flatten the semantic values by converting each index to `Some(u32)`
            flattened.extend_from_slice(
                &indices
                    .iter()
                    .map(|i| if let Some(i) = i { *i } else { u32::MAX })
                    .collect::<Vec<_>>(),
            );

            flattened.len()
        }
        // ------------------
        // (2) Nested
        // ------------------
        CjSemanticsValues::Nested(nested) => {
            // Recursively encode each nested semantics value
            for sub in nested {
                encode_semantics_values(sub, flattened);
            }

            // Return the updated length of the flattened vector
            flattened.len()
        }
    }
}

/// Encodes semantic surfaces and values from a CityJSON Semantics object.
///
/// # Arguments
///
/// * `semantics` - Reference to the CityJSON Semantics object containing surfaces and values
pub(crate) fn encode_semantics(semantics: &CjSemantics) -> GMSemantics {
    let mut values = Vec::new();
    let _ = encode_semantics_values(&semantics.values, &mut values);

    GMSemantics {
        surfaces: semantics.surfaces.to_vec(),
        values,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;
    use cjseq::Geometry as CjGeometry;
    use pretty_assertions::assert_eq;
    use serde_json::json;

    #[test]
    fn test_encode_boundaries() -> Result<()> {
        // MultiPoint
        let boundaries = json!([2, 44, 0, 7]);
        let boundaries: CjBoundaries = serde_json::from_value(boundaries)?;
        let encoded_boundaries = encode(&boundaries, None, None, None);
        assert_eq!(vec![2, 44, 0, 7], encoded_boundaries.boundaries.indices);
        assert_eq!(vec![4], encoded_boundaries.boundaries.strings);
        assert!(encoded_boundaries.boundaries.surfaces.is_empty());
        assert!(encoded_boundaries.boundaries.shells.is_empty());
        assert!(encoded_boundaries.boundaries.solids.is_empty());

        // MultiLineString
        let boundaries = json!([[2, 3, 5], [77, 55, 212]]);
        let boundaries: CjBoundaries = serde_json::from_value(boundaries)?;
        let encoded_boundaries = encode(&boundaries, None, None, None);

        assert_eq!(
            vec![2, 3, 5, 77, 55, 212],
            encoded_boundaries.boundaries.indices
        );
        assert_eq!(vec![3, 3], encoded_boundaries.boundaries.strings);
        assert_eq!(vec![2], encoded_boundaries.boundaries.surfaces);
        assert!(encoded_boundaries.boundaries.shells.is_empty());
        assert!(encoded_boundaries.boundaries.solids.is_empty());

        // MultiSurface
        let boundaries = json!([[[0, 3, 2, 1]], [[4, 5, 6, 7]], [[0, 1, 5, 4]]]);
        let boundaries: CjBoundaries = serde_json::from_value(boundaries)?;
        let encoded_boundaries = encode(&boundaries, None, None, None);

        assert_eq!(
            vec![0, 3, 2, 1, 4, 5, 6, 7, 0, 1, 5, 4],
            encoded_boundaries.boundaries.indices
        );
        assert_eq!(vec![4, 4, 4], encoded_boundaries.boundaries.strings);
        assert_eq!(vec![1, 1, 1], encoded_boundaries.boundaries.surfaces);
        assert_eq!(vec![3], encoded_boundaries.boundaries.shells);
        assert!(encoded_boundaries.boundaries.solids.is_empty());

        // Solid
        let boundaries = json!([
            [
                [[0, 3, 2, 1, 22], [1, 2, 3, 4]],
                [[4, 5, 6, 7]],
                [[0, 1, 5, 4]],
                [[1, 2, 6, 5]]
            ],
            [
                [[240, 243, 124]],
                [[244, 246, 724]],
                [[34, 414, 45]],
                [[111, 246, 5]]
            ]
        ]);
        let boundaries: CjBoundaries = serde_json::from_value(boundaries)?;
        let encoded_boundaries = encode(&boundaries, None, None, None);

        assert_eq!(
            vec![
                0, 3, 2, 1, 22, 1, 2, 3, 4, 4, 5, 6, 7, 0, 1, 5, 4, 1, 2, 6, 5, 240, 243, 124, 244,
                246, 724, 34, 414, 45, 111, 246, 5
            ],
            encoded_boundaries.boundaries.indices
        );
        assert_eq!(
            vec![5, 4, 4, 4, 4, 3, 3, 3, 3],
            encoded_boundaries.boundaries.strings
        );
        assert_eq!(
            vec![2, 1, 1, 1, 1, 1, 1, 1],
            encoded_boundaries.boundaries.surfaces
        );
        assert_eq!(vec![4, 4], encoded_boundaries.boundaries.shells);
        assert_eq!(vec![2], encoded_boundaries.boundaries.solids);

        // CompositeSolid
        let boundaries = json!([
            [
                [
                    [[0, 3, 2, 1, 22]],
                    [[4, 5, 6, 7]],
                    [[0, 1, 5, 4]],
                    [[1, 2, 6, 5]]
                ],
                [
                    [[240, 243, 124]],
                    [[244, 246, 724]],
                    [[34, 414, 45]],
                    [[111, 246, 5]]
                ]
            ],
            [[
                [[666, 667, 668]],
                [[74, 75, 76]],
                [[880, 881, 885]],
                [[111, 122, 226]]
            ]]
        ]);
        let boundaries: CjBoundaries = serde_json::from_value(boundaries)?;
        let encoded_boundaries = encode(&boundaries, None, None, None);
        assert_eq!(
            vec![
                0, 3, 2, 1, 22, 4, 5, 6, 7, 0, 1, 5, 4, 1, 2, 6, 5, 240, 243, 124, 244, 246, 724,
                34, 414, 45, 111, 246, 5, 666, 667, 668, 74, 75, 76, 880, 881, 885, 111, 122, 226
            ],
            encoded_boundaries.boundaries.indices
        );
        assert_eq!(
            encoded_boundaries.boundaries.strings,
            vec![5, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3]
        );
        assert_eq!(
            encoded_boundaries.boundaries.surfaces,
            vec![1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
        );
        assert_eq!(encoded_boundaries.boundaries.shells, vec![4, 4, 4]);
        assert_eq!(encoded_boundaries.boundaries.solids, vec![2, 1]);

        Ok(())
    }

    #[test]
    fn test_encode_semantics() -> Result<()> {
        //MultiSurface
        let multi_surfaces_gem_json = json!({
            "type": "MultiSurface",
            "lod": "2",
            "boundaries": [
              [
                [
                  0,
                  3,
                  2,
                  1
                ]
              ],
              [
                [
                  4,
                  5,
                  6,
                  7
                ]
              ],
              [
                [
                  0,
                  1,
                  5,
                  4
                ]
              ],
              [
                [
                  0,
                  2,
                  3,
                  8
                ]
              ],
              [
                [
                  10,
                  12,
                  23,
                  48
                ]
              ]
            ],
            "semantics": {
              "surfaces": [
                {
                  "type": "WallSurface",
                  "slope": 33.4,
                  "children": [
                    2
                  ]
                },
                {
                  "type": "RoofSurface",
                  "slope": 66.6
                },
                {
                  "type": "OuterCeilingSurface",
                  "parent": 0,
                  "colour": "blue"
                }
              ],
              "values": [
                0,
                0,
                null,
                1,
                2
              ]
            }
        });
        let multi_sufaces_geom: CjGeometry = serde_json::from_value(multi_surfaces_gem_json)?;
        let CjGeometry { semantics, .. } = multi_sufaces_geom;

        let encoded_semantics = encode_semantics(&semantics.unwrap());

        let expected_semantics_surfaces = vec![
            CjSemanticsSurface {
                thetype: "WallSurface".to_string(),
                parent: None,
                children: Some(vec![2]),
                other: Some(json!({
                    "slope": 33.4,
                })),
            },
            CjSemanticsSurface {
                thetype: "RoofSurface".to_string(),
                parent: None,
                children: None,
                other: Some(json!({
                    "slope": 66.6,
                })),
            },
            CjSemanticsSurface {
                thetype: "OuterCeilingSurface".to_string(),
                parent: Some(0),
                children: None,
                other: Some(json!({
                    "colour": "blue",
                })),
            },
        ];

        let expected_semantics_values = vec![0, 0, u32::MAX, 1, 2];
        assert_eq!(expected_semantics_surfaces, encoded_semantics.surfaces);
        assert_eq!(
            expected_semantics_values,
            encoded_semantics.values.as_slice().to_vec()
        );

        //CompositeSolid
        let composite_solid_gem_json = json!({
          "type": "CompositeSolid",
          "lod": "2.2",
          "boundaries": [
            [ //-- 1st Solid
            [
              [
                [
                  0,
                  3,
                  2,
                  1,
                  22
                ]
              ],
              [
                [
                  4,
                  5,
                  6,
                  7
                ]
              ],
              [
                [
                  0,
                  1,
                  5,
                  4
                ]
              ],
              [
                [
                  1,
                  2,
                  6,
                  5
                ]
              ]
            ]
          ],
          [ //-- 2nd Solid
            [
              [
                [
                  666,
                  667,
                  668
                ]
              ],
              [
                [
                  74,
                  75,
                  76
                ]
              ],
              [
                [
                  880,
                  881,
                  885
                ]
              ]
            ]
          ]],
          "semantics": {
            "surfaces" : [
              {
                "type": "RoofSurface"
              },
              {
                "type": "WallSurface"
              }
            ],
            "values": [
              [
                [0, 1, 1, null]
              ],
              [
                [null, null, null]
              ]
            ]
          }
        }  );
        let composite_solid_geom: CjGeometry = serde_json::from_value(composite_solid_gem_json)?;
        let CjGeometry { semantics, .. } = composite_solid_geom;

        let encoded_semantics = encode_semantics(&semantics.unwrap());

        let expected_semantics_surfaces = vec![
            CjSemanticsSurface {
                thetype: "RoofSurface".to_string(),
                parent: None,
                children: None,
                other: Some(json!({})),
            },
            CjSemanticsSurface {
                thetype: "WallSurface".to_string(),
                parent: None,
                children: None,
                other: Some(json!({})),
            },
        ];

        let expected_semantics_values: Vec<u32> =
            vec![0, 1, 1, u32::MAX, u32::MAX, u32::MAX, u32::MAX];
        assert_eq!(expected_semantics_surfaces, encoded_semantics.surfaces);
        assert_eq!(
            expected_semantics_values,
            encoded_semantics.values.as_slice().to_vec()
        );
        Ok(())
    }

    #[test]
    fn test_encode_material() -> Result<()> {
        // Test case 1: Single material value
        let mut materials = HashMap::new();
        materials.insert(
            "theme1".to_string(),
            CjMaterialReference {
                value: Some(5),
                values: None,
            },
        );

        let encoded = encode_material(&materials);
        assert_eq!(encoded.len(), 1);
        match &encoded[0] {
            MaterialMapping::Value(value) => {
                assert_eq!(value.theme, "theme1");
                assert_eq!(value.value, 5);
            }
            _ => panic!("Expected MaterialMapping::Value"),
        }

        // Test case 2: MultiSurface material values
        let mut materials = HashMap::new();
        let multi_surface_values = CjMaterialValues::Indices(vec![Some(0), Some(1), None, Some(2)]);
        materials.insert(
            "theme2".to_string(),
            CjMaterialReference {
                value: None,
                values: Some(multi_surface_values),
            },
        );

        let encoded = encode_material(&materials);
        assert_eq!(encoded.len(), 1);
        match &encoded[0] {
            MaterialMapping::Values(values) => {
                assert_eq!(values.theme, "theme2");
                assert_eq!(values.vertices, vec![0, 1, u32::MAX, 2]);
                assert!(values.shells.is_empty());
                assert!(values.solids.is_empty());
            }
            _ => panic!("Expected MaterialMapping::Values"),
        }

        // Test case 3: Solid material values
        let mut materials = HashMap::new();
        let solid_values = CjMaterialValues::Nested(vec![
            CjMaterialValues::Indices(vec![Some(0), Some(1), None]),
            CjMaterialValues::Indices(vec![Some(2), Some(3), Some(4)]),
        ]);
        materials.insert(
            "theme3".to_string(),
            CjMaterialReference {
                value: None,
                values: Some(solid_values),
            },
        );

        let encoded = encode_material(&materials);
        assert_eq!(encoded.len(), 1);
        match &encoded[0] {
            MaterialMapping::Values(values) => {
                assert_eq!(values.theme, "theme3");
                assert_eq!(values.solids, vec![2]); // 1 solid with 2 shells
                assert_eq!(values.shells, vec![3, 3]); // Each shell has 3 surfaces
                assert_eq!(values.vertices, vec![0, 1, u32::MAX, 2, 3, 4]);
            }
            _ => panic!("Expected MaterialMapping::Values"),
        }

        // Test case 4: Multiple themes
        let mut materials = HashMap::new();
        materials.insert(
            "theme4".to_string(),
            CjMaterialReference {
                value: Some(7),
                values: None,
            },
        );
        materials.insert(
            "theme5".to_string(),
            CjMaterialReference {
                value: None,
                values: Some(CjMaterialValues::Indices(vec![Some(8), Some(9)])),
            },
        );

        let encoded = encode_material(&materials);
        assert_eq!(encoded.len(), 2);

        // Find and verify each mapping by theme name instead of relying on order
        let theme4_mapping = encoded
            .iter()
            .find(|m| match m {
                MaterialMapping::Value(v) => v.theme == "theme4",
                MaterialMapping::Values(v) => v.theme == "theme4",
            })
            .expect("Should have theme4 mapping");

        let theme5_mapping = encoded
            .iter()
            .find(|m| match m {
                MaterialMapping::Value(v) => v.theme == "theme5",
                MaterialMapping::Values(v) => v.theme == "theme5",
            })
            .expect("Should have theme5 mapping");

        // Verify theme4 mapping
        match theme4_mapping {
            MaterialMapping::Value(value) => {
                assert_eq!(value.theme, "theme4");
                assert_eq!(value.value, 7);
            }
            _ => panic!("Expected MaterialMapping::Value for theme4"),
        }

        // Verify theme5 mapping
        match theme5_mapping {
            MaterialMapping::Values(values) => {
                assert_eq!(values.theme, "theme5");
                assert_eq!(values.vertices, vec![8, 9]);
                assert!(values.shells.is_empty());
                assert!(values.solids.is_empty());
            }
            _ => panic!("Expected MaterialMapping::Values for theme5"),
        }

        // Test case 5: CompositeSolid material values
        let mut materials = HashMap::new();
        let composite_solid_values = CjMaterialValues::Nested(vec![
            CjMaterialValues::Nested(vec![
                CjMaterialValues::Indices(vec![Some(0), Some(1), None]),
                CjMaterialValues::Indices(vec![Some(2), None, None]),
            ]),
            CjMaterialValues::Nested(vec![CjMaterialValues::Indices(vec![
                Some(3),
                Some(4),
                None,
            ])]),
        ]);
        materials.insert(
            "theme6".to_string(),
            CjMaterialReference {
                value: None,
                values: Some(composite_solid_values),
            },
        );

        let encoded = encode_material(&materials);
        assert_eq!(encoded.len(), 1);
        match &encoded[0] {
            MaterialMapping::Values(values) => {
                assert_eq!(values.theme, "theme6");
                assert_eq!(values.solids, vec![2, 1]); // Two solids, the first solid has 2 shells, the second solid has 1 shell
                assert_eq!(values.shells, vec![3, 3, 3]); // Each shell has 3 surfaces
                assert_eq!(
                    values.vertices,
                    vec![0, 1, u32::MAX, 2, u32::MAX, u32::MAX, 3, 4, u32::MAX]
                );
            }
            _ => panic!("Expected MaterialMapping::Values"),
        }

        Ok(())
    }

    #[test]
    fn test_encode_texture() -> Result<()> {
        // Create a theme for testing
        let theme = "test-theme".to_string();

        // MultiPoint-like texture values
        let texture_values = json!([0, 10, 20, null]);
        let texture_values: CjTextureValues = serde_json::from_value(texture_values)?;

        let mut textures = HashMap::new();
        textures.insert(
            theme.clone(),
            CjTextureReference {
                values: texture_values,
            },
        );

        let encoded = encode_texture(&textures);
        assert_eq!(encoded.len(), 1);
        assert_eq!(encoded[0].theme, theme);
        assert_eq!(encoded[0].vertices, vec![0, 10, 20, u32::MAX]);
        assert_eq!(encoded[0].strings, vec![4]);
        assert!(encoded[0].surfaces.is_empty());
        assert!(encoded[0].shells.is_empty());
        assert!(encoded[0].solids.is_empty());

        // MultiLineString-like texture values
        let texture_values = json!([[0, 10, 20], [1, 11, null]]);
        let texture_values: CjTextureValues = serde_json::from_value(texture_values)?;

        let mut textures = HashMap::new();
        textures.insert(
            theme.clone(),
            CjTextureReference {
                values: texture_values,
            },
        );

        let encoded = encode_texture(&textures);
        assert_eq!(encoded.len(), 1);
        assert_eq!(encoded[0].theme, theme);
        assert_eq!(encoded[0].vertices, vec![0, 10, 20, 1, 11, u32::MAX]);
        assert_eq!(encoded[0].strings, vec![3, 3]);
        assert_eq!(encoded[0].surfaces, vec![2]);
        assert!(encoded[0].shells.is_empty());
        assert!(encoded[0].solids.is_empty());

        // MultiSurface-like texture values
        let texture_values = json!([[[0, 10, 20, 30]], [[1, 11, 21, null]], [[2, 12, null, 32]]]);
        let texture_values: CjTextureValues = serde_json::from_value(texture_values)?;

        let mut textures = HashMap::new();
        textures.insert(
            theme.clone(),
            CjTextureReference {
                values: texture_values,
            },
        );

        let encoded = encode_texture(&textures);
        assert_eq!(encoded.len(), 1);
        assert_eq!(encoded[0].theme, theme);
        assert_eq!(
            encoded[0].vertices,
            vec![0, 10, 20, 30, 1, 11, 21, u32::MAX, 2, 12, u32::MAX, 32]
        );
        assert_eq!(encoded[0].strings, vec![4, 4, 4]);
        assert_eq!(encoded[0].surfaces, vec![1, 1, 1]);
        assert_eq!(encoded[0].shells, vec![3]);
        assert!(encoded[0].solids.is_empty());

        // Solid-like texture values
        let texture_values = json!([
            [[[0, 10, 20, 30]], [[1, 11, 21, null]], [[2, 12, null, 32]]],
            [[[3, 13, 23, 33]], [[4, 14, 24, null]]]
        ]);
        let texture_values: CjTextureValues = serde_json::from_value(texture_values)?;

        let mut textures = HashMap::new();
        textures.insert(
            theme.clone(),
            CjTextureReference {
                values: texture_values,
            },
        );

        let encoded = encode_texture(&textures);
        assert_eq!(encoded.len(), 1);
        assert_eq!(encoded[0].theme, theme);
        assert_eq!(
            encoded[0].vertices,
            vec![
                0,
                10,
                20,
                30,
                1,
                11,
                21,
                u32::MAX,
                2,
                12,
                u32::MAX,
                32,
                3,
                13,
                23,
                33,
                4,
                14,
                24,
                u32::MAX
            ]
        );
        assert_eq!(encoded[0].strings, vec![4, 4, 4, 4, 4]);
        assert_eq!(encoded[0].surfaces, vec![1, 1, 1, 1, 1]);
        assert_eq!(encoded[0].shells, vec![3, 2]);
        assert_eq!(encoded[0].solids, vec![2]);

        // CompositeSolid-like texture values
        let texture_values = json!([
            [
                [[[0, 10, 20]], [[1, 11, null]]],
                [[[2, 12, 22]], [[3, null, 23]]]
            ],
            [[[[4, 14, 24]], [[5, 15, 25]]]]
        ]);
        let texture_values: CjTextureValues = serde_json::from_value(texture_values)?;

        let mut textures = HashMap::new();
        textures.insert(
            theme.clone(),
            CjTextureReference {
                values: texture_values,
            },
        );

        let encoded = encode_texture(&textures);
        assert_eq!(encoded.len(), 1);
        assert_eq!(encoded[0].theme, theme);
        assert_eq!(
            encoded[0].vertices,
            vec![
                0,
                10,
                20,
                1,
                11,
                u32::MAX,
                2,
                12,
                22,
                3,
                u32::MAX,
                23,
                4,
                14,
                24,
                5,
                15,
                25
            ]
        );
        assert_eq!(encoded[0].strings, vec![3, 3, 3, 3, 3, 3]);
        assert_eq!(encoded[0].surfaces, vec![1, 1, 1, 1, 1, 1]);
        assert_eq!(encoded[0].shells, vec![2, 2, 2]);
        assert_eq!(encoded[0].solids, vec![2, 1]);

        // Multiple themes
        let texture_values1 = json!([0, 10, 20]);
        let texture_values1: CjTextureValues = serde_json::from_value(texture_values1)?;

        let texture_values2 = json!([1, 11, null]);
        let texture_values2: CjTextureValues = serde_json::from_value(texture_values2)?;

        let mut textures = HashMap::new();
        textures.insert(
            "winter".to_string(),
            CjTextureReference {
                values: texture_values1,
            },
        );
        textures.insert(
            "summer".to_string(),
            CjTextureReference {
                values: texture_values2,
            },
        );

        let encoded = encode_texture(&textures);
        assert_eq!(encoded.len(), 2);

        // Find and verify each mapping by theme name instead of relying on order
        let winter_mapping = encoded
            .iter()
            .find(|m| m.theme == "winter")
            .expect("Should have winter mapping");

        let summer_mapping = encoded
            .iter()
            .find(|m| m.theme == "summer")
            .expect("Should have summer mapping");

        assert_eq!(winter_mapping.vertices, vec![0, 10, 20]);
        assert_eq!(winter_mapping.strings, vec![3]);

        assert_eq!(summer_mapping.vertices, vec![1, 11, u32::MAX]);
        assert_eq!(summer_mapping.strings, vec![3]);

        Ok(())
    }
}