draco-oxide 0.1.0-alpha.5

draco-oxide is a rust rewrite of Google's draco mesh compression library.
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
use std::{ops, vec};

use crate::core::attribute::Attribute;
use crate::core::attribute::{AttributeDomain, ComponentDataType};
use crate::core::corner_table::GenericCornerTable;
use crate::core::shared::{CornerIdx, DataValue, NdVector};
use crate::encode::connectivity::ConnectivityEncoderOutput;
use crate::encode::entropy::symbol_coding::encode_symbols;
use crate::prelude::{AttributeType, ByteWriter, ConfigType};
use crate::shared::attribute::sequence::Traverser;
use crate::shared::attribute::Portable;
use crate::shared::entropy::SymbolEncodingMethod;
use thiserror::Error;

#[cfg(feature = "evaluation")]
#[allow(unused_imports)]
use crate::eval;

#[derive(Error, Debug)]
pub enum Err {
    #[error("Entropy Symbol Encoding Error: {0}")]
    EntropyEncodingError(#[from] crate::encode::entropy::symbol_coding::Err),
    #[error("Invalid attribute id: {0}")]
    InvalidAttributeId(usize),
    #[error("Invalid prediction scheme id: {0}")]
    InvalidPredictionSchemeId(usize),
    #[error("Attribute Encoder has too many encoding groups: {0}")]
    TooManyEncodingGroups(usize),
    #[error("An attribute has too many parents: {0}")]
    TooManyParents(usize),
    #[error("Unsupported data type.")]
    UnsupportedDataType,
    #[error("Attribute data has too many components; it must be less than {}, but it is {}.", 5, .0)]
    // ToDo: Change 5 to the build config
    UnsupportedNumComponents(usize),
    #[error("Prediction Error: {0}")]
    PredictionError(#[from] crate::shared::attribute::prediction_scheme::Err),
}

#[derive(Clone, Debug)]
pub struct GroupConfig {
    #[allow(unused)]
    range: Vec<ops::Range<usize>>,

    pub prediction_scheme: prediction_scheme::Config,
    pub prediction_transform: prediction_transform::Config,
}

impl GroupConfig {
    #[allow(clippy::single_range_in_vec_init)]
    fn default_with_size(size: usize) -> Self {
        Self {
            range: vec![0..size],
            prediction_scheme: prediction_scheme::Config::default(),
            prediction_transform: prediction_transform::Config::default(),
        }
    }

    #[allow(clippy::single_range_in_vec_init, clippy::needless_update)]
    fn default_for(att_ty: AttributeType, size: usize) -> Self {
        match att_ty {
            AttributeType::Position => Self {
                range: vec![0..size],
                prediction_scheme: prediction_scheme::Config {
                    ty: prediction_scheme::PredictionSchemeType::MeshParallelogramPrediction,
                    ..prediction_scheme::Config::default()
                },
                prediction_transform: prediction_transform::Config {
                    ty: prediction_transform::PredictionTransformType::WrappedDifference,
                    portabilization: portabilization::Config::default(),
                },
            },
            AttributeType::Normal => Self {
                range: vec![0..size],
                prediction_scheme: prediction_scheme::Config {
                    ty: prediction_scheme::PredictionSchemeType::MeshNormalPrediction,
                    ..prediction_scheme::Config::default()
                },
                prediction_transform: prediction_transform::Config {
                    ty: prediction_transform::PredictionTransformType::OctahedralOrthogonal,
                    portabilization: portabilization::Config::default(),
                },
            },
            AttributeType::TextureCoordinate => Self {
                range: vec![0..size],
                prediction_scheme: prediction_scheme::Config {
                    ty:
                        prediction_scheme::PredictionSchemeType::MeshPredictionForTextureCoordinates,
                    ..prediction_scheme::Config::default()
                },
                prediction_transform: prediction_transform::Config {
                    ty: prediction_transform::PredictionTransformType::WrappedDifference,
                    portabilization: portabilization::Config::default(),
                },
            },
            AttributeType::Custom => Self {
                range: vec![0..size],
                prediction_scheme: prediction_scheme::Config {
                    ty: prediction_scheme::PredictionSchemeType::DeltaPrediction,
                    ..prediction_scheme::Config::default()
                },
                prediction_transform: prediction_transform::Config {
                    ty: prediction_transform::PredictionTransformType::WrappedDifference,
                    portabilization: portabilization::Config::default_for(AttributeType::Custom),
                },
            },
            _ => Self::default_with_size(size),
        }
    }
}

#[derive(Clone, Debug)]
pub struct Config {
    group_cfgs: Vec<GroupConfig>,
    rans_encoding: bool,
}

// ToDo: THIS IMPLEMENTATION IS NOT FINAL
impl ConfigType for Config {
    fn default() -> Self {
        Self {
            group_cfgs: Vec::new(),
            rans_encoding: true,
        }
    }
}

impl Config {
    pub fn default_for(att_ty: AttributeType, size: usize) -> Self {
        Self {
            group_cfgs: vec![GroupConfig::default_for(att_ty, size)],
            rans_encoding: true,
        }
    }
}

pub(super) struct AttributeEncoder<'parents, 'encoder, 'writer, 'co, 'mesh, W> {
    att: Attribute,
    att_data_id: usize,
    #[allow(unused)]
    cfg: Config,
    writer: &'writer mut W,
    parents: &'encoder [&'parents Attribute],
    conn_out: &'co ConnectivityEncoderOutput<'mesh>,
}

impl<'parents, 'encoder, 'writer, 'co, 'mesh, W>
    AttributeEncoder<'parents, 'encoder, 'writer, 'co, 'mesh, W>
where
    W: ByteWriter,
    'parents: 'encoder,
{
    pub(super) fn new(
        att: Attribute,
        att_data_id: usize,
        parents: &'encoder [&'parents Attribute],
        conn_out: &'co ConnectivityEncoderOutput<'mesh>,
        writer: &'writer mut W,
        cfg: Config,
    ) -> Self {
        AttributeEncoder {
            att,
            att_data_id,
            cfg,
            writer,
            parents,
            conn_out,
        }
    }

    pub(super) fn encode<const WRITE_NOW: bool, const BOOST: bool>(self) -> Result<Attribute, Err> {
        self.cfg.group_cfgs[0]
            .prediction_scheme
            .ty
            .write_to(self.writer);
        self.cfg.group_cfgs[0]
            .prediction_transform
            .ty
            .write_to(self.writer);

        let component_type = self.att.get_component_type();
        match component_type {
            ComponentDataType::F32 => self.unpack_num_components::<WRITE_NOW, BOOST, f32>(),
            ComponentDataType::F64 => self.unpack_num_components::<WRITE_NOW, BOOST, f64>(),
            ComponentDataType::U8 => self.unpack_num_components::<WRITE_NOW, BOOST, u8>(),
            ComponentDataType::U16 => self.unpack_num_components::<WRITE_NOW, BOOST, u16>(),
            ComponentDataType::U32 => self.unpack_num_components::<WRITE_NOW, BOOST, u32>(),
            ComponentDataType::U64 => self.unpack_num_components::<WRITE_NOW, BOOST, u64>(),
            ComponentDataType::I8 => self.unpack_num_components::<WRITE_NOW, BOOST, i8>(),
            ComponentDataType::I16 => self.unpack_num_components::<WRITE_NOW, BOOST, i16>(),
            ComponentDataType::I32 => self.unpack_num_components::<WRITE_NOW, BOOST, i32>(),
            ComponentDataType::I64 => self.unpack_num_components::<WRITE_NOW, BOOST, i64>(),
            ComponentDataType::Invalid => Err(Err::UnsupportedDataType),
        }
    }

    fn unpack_num_components<const WRITE_NOW: bool, const BOOST: bool, T>(
        self,
    ) -> Result<Attribute, Err>
    where
        T: DataValue + Copy,
        NdVector<1, T>: Vector<1>,
        NdVector<2, T>: Vector<2>,
        NdVector<3, T>: Vector<3>,
        NdVector<4, T>: Vector<4>,
    {
        let num_components = self.att.get_num_components();
        match num_components {
            0 => unreachable!("Vector of dimension 0 is not allowed"),
            1 => self.encode_typed::<WRITE_NOW, BOOST, 1, _>(),
            2 => self.encode_typed::<WRITE_NOW, BOOST, 2, _>(),
            3 => self.encode_typed::<WRITE_NOW, BOOST, 3, _>(),
            4 => self.encode_typed::<WRITE_NOW, BOOST, 4, _>(),
            _ => Err(Err::UnsupportedNumComponents(num_components)),
        }
    }

    fn encode_typed<const WRITE_NOW: bool, const BOOST: bool, const N: usize, T>(
        self,
    ) -> Result<Attribute, Err>
    where
        T: DataValue + Copy,
        NdVector<N, T>: Vector<N> + Portable,
        NdVector<N, i32>: Vector<N, Component = i32>,
        NdVector<N, f32>: Vector<N, Component = f32> + Portable,
    {
        if !BOOST {
            match self.conn_out {
                ConnectivityEncoderOutput::Edgebreaker(edgebreaker_out) => {
                    if let Some(corner_table) = edgebreaker_out
                        .corner_table
                        .attribute_corner_table(self.att_data_id)
                    {
                        let sequence = Traverser::new(
                            &corner_table,
                            edgebreaker_out.corners_of_edgebreaker.clone(), // ToDo: take this value
                        )
                        .compute_seqeunce();
                        self.encode_impl_edgebreaker::<WRITE_NOW, _, _, NdVector<N, T>, N>(
                            &corner_table,
                            sequence.into_iter(),
                        )
                    } else {
                        let corner_table = edgebreaker_out.corner_table.universal_corner_table();
                        let sequence = Traverser::new(
                            corner_table,
                            edgebreaker_out.corners_of_edgebreaker.clone(), // ToDo: take this value
                        )
                        .compute_seqeunce();
                        self.encode_impl_edgebreaker::<WRITE_NOW, _, _, NdVector<N, T>, N>(
                            corner_table,
                            sequence.into_iter(),
                        )
                    }
                }
                ConnectivityEncoderOutput::Sequential(_) => {
                    unimplemented!("Sequential connectivity encoding is not implemented yet");
                }
            }
        } else {
            unimplemented!("BOOST is not implemented yet");
            // let corner_table = match self.conn_out {
            //     ConnectivityEncoderOutput::Edgebreaker(edgebreaker_out) => {
            //         edgebreaker_out.corner_table.attribute_corner_table(self.att.get_id().as_usize())
            //     },
            //     ConnectivityEncoderOutput::Sequential(_) => {
            //         unimplemented!("Sequential connectivity encoding is not implemented yet");
            //     },
            // };
            // let mut gm: GroupManager<'encoder, NdVector<N, T>,_> = GroupManager::compose_groups(&self.parents, &corner_table, cfg);
            // gm.split_unpredicted_values();
            // gm.compress::<WRITE_NOW,_>(&self.att, self.writer)?;
        }
    }

    fn encode_impl_edgebreaker<const WRITE_NOW: bool, CT, S, Data, const N: usize>(
        mut self,
        corner_table: &CT,
        sequence: S,
    ) -> Result<Attribute, Err>
    where
        CT: GenericCornerTable,
        S: Iterator<Item = CornerIdx> + Clone,
        Data: Vector<N> + Portable,
        NdVector<N, i32>: Vector<N, Component = i32>,
        NdVector<N, f32>: Vector<N, Component = f32> + Portable,
    {
        let por_cfg = portabilization::Config::default_for(self.att.get_attribute_type());

        let mut att = Attribute::new(
            Vec::<Data>::new(),
            AttributeType::Position,
            AttributeDomain::Position,
            Vec::new(),
        );
        std::mem::swap(&mut att, &mut self.att);
        let mut port_info_buffer = Vec::new();
        let portabilization: portabilization::Portabilization<Data, N> =
            portabilization::Portabilization::new(att, por_cfg, &mut port_info_buffer);
        let port_att = portabilization.portabilize();

        match port_att.get_num_components() {
            1 => self.encode_portabilized::<CT, S, 1>(
                corner_table,
                sequence,
                port_att,
                port_info_buffer,
            ),
            2 => self.encode_portabilized::<CT, S, 2>(
                corner_table,
                sequence,
                port_att,
                port_info_buffer,
            ),
            3 => self.encode_portabilized::<CT, S, 3>(
                corner_table,
                sequence,
                port_att,
                port_info_buffer,
            ),
            4 => self.encode_portabilized::<CT, S, 4>(
                corner_table,
                sequence,
                port_att,
                port_info_buffer,
            ),
            _ => Err(Err::UnsupportedNumComponents(port_att.get_num_components())),
        }
    }

    fn encode_portabilized<CT, S, const N: usize>(
        &mut self,
        corner_table: &CT,
        sequence: S,
        port_att: Attribute,
        port_info_buffer: Vec<u8>,
    ) -> Result<Attribute, Err>
    where
        CT: GenericCornerTable,
        S: Iterator<Item = CornerIdx>,
        NdVector<N, i32>: Vector<N, Component = i32> + Portable,
    {
        let mut prediction_scheme = prediction_scheme::PredictionScheme::new(
            self.cfg.group_cfgs[0].prediction_scheme.ty.clone(),
            self.parents,
            corner_table,
        );

        // Transform the predicted values
        let mut transform = PredictionTransform::new(self.cfg.group_cfgs[0].prediction_transform);

        // Predict and transform the values
        let mut sequence_record = Vec::new();

        for c in sequence {
            let val = prediction_scheme.predict(c, &sequence_record, &port_att);
            let v = corner_table.vertex_idx(c);
            sequence_record.push(v);
            let p = corner_table.point_idx(c);
            transform.map_with_tentative_metadata(port_att.get(p), val);
        }

        // Write the output
        let mut transform_info_buffer = Vec::new();
        let output = transform.squeeze(&mut transform_info_buffer);

        self.writer.write_u8(self.cfg.rans_encoding as u8);
        if self.cfg.rans_encoding {
            // ToDo: This can be a lot smarter.
            let symbols = output
                .iter()
                .flat_map(|v| (0..N).map(|i| *v.get(i) as u64))
                .collect::<Vec<_>>();
            encode_symbols(symbols, N, SymbolEncodingMethod::DirectCoded, self.writer)?;
        } else {
            // If RANS encoding is not used, we write the output directly
            for value in output {
                value.write_to(self.writer);
            }
        }

        // We need to write the metadata for the prediction, prediction scheme, and transform.
        // This part is a bit tricky, as we need to swap the order of transform and prediction metadata
        // depending on the prediction type, in order to be compatible with the draco decoder.
        if prediction_scheme.get_type()
            == prediction_scheme::PredictionSchemeType::MeshNormalPrediction
        {
            for byte in transform_info_buffer {
                self.writer.write_u8(byte);
            }
            prediction_scheme.encode_prediction_metadtata(self.writer)?;
        } else if prediction_scheme.get_type()
            == prediction_scheme::PredictionSchemeType::MeshPredictionForTextureCoordinates
        {
            prediction_scheme.encode_prediction_metadtata(self.writer)?;
            for byte in transform_info_buffer {
                self.writer.write_u8(byte);
            }
        } else {
            // otherwise, the prediction scheme does not have metadata
            assert!({
                let mut buffer = Vec::new();
                prediction_scheme.encode_prediction_metadtata(&mut buffer)?;
                buffer.is_empty()
            });
            for byte in transform_info_buffer {
                self.writer.write_u8(byte);
            }
        }

        for byte in port_info_buffer {
            self.writer.write_u8(byte);
        }

        Ok(port_att)
    }
}

use super::prediction_transform::{self, PredictionTransform};
use crate::core::shared::Vector;
use crate::encode::attribute::portabilization;
use crate::encode::attribute::prediction_transform::PredictionTransformImpl;
use crate::shared::attribute::prediction_scheme;

// struct Group<'encoder, C, const N: usize>
// {
// 	/// Prediction
// 	prediction: PredictionScheme<'encoder, C, N>,
//     transform: PredictionTransform<N>,
// }

// impl<'encoder, C, const N: usize> Group<'encoder, C, N>
//     where
//         C: GenericCornerTable,
//         NdVector<N, i32>: Vector<N, Component = i32>,
// {

//     fn from<'parents>(parents: &'encoder[&'parents Attribute], corner_table: &'parents C, cfg: GroupConfig) -> Self
//         where 'parents: 'encoder
//     {

//         let prediction_scheme = prediction_scheme::PredictionScheme::new(cfg.prediction_scheme.ty, parents, corner_table);

//         let prediction_transform = PredictionTransform::new(cfg.prediction_transform);

//         Self {
//             prediction: prediction_scheme,
//             transform: prediction_transform
//         }
//     }

//     fn split_unpredicted_values(&mut self, values_indices: &mut Vec<std::ops::Range<usize>>) -> Vec<std::ops::Range<usize>> {
//         let impossible_to_predict = self.prediction
//             .get_values_impossible_to_predict(values_indices);
//         impossible_to_predict
//     }

//     // fn predict_and_transform(&mut self, ranges: &Vec<ops::Range<usize>>, attribute: &Attribute) {
//     //     for i in ranges.iter().cloned().flatten() {
//     //         let prediction = self.prediction.predict(
//     //             unsafe { &attribute.as_slice_unchecked()[0..i] }
//     //         );
//     //         self.transform.map_with_tentative_metadata(
//     //             attribute.get::<Data>(i),
//     //             prediction
//     //         );
//     //     }
//     // }

//     fn squeeze_transformed_data<W>(&mut self, writer: &mut W)
//         where W: ByteWriter
//     {
//         self.transform.squeeze(writer)
//     }

//     fn take_output<W>(self, writer: &mut W) -> Vec<u64>
//         where W: ByteWriter
//     {
//         self.transform.out(writer)
//     }
// }

// struct GroupManager<'encoder, Data, C, const N: usize>
//     where
//         Data: Vector<N> + Portable,
//         Data::Component: DataValue,
// {
// 	partition: Vec<Vec<ops::Range<usize>>>,
// 	groups: Vec<Group<'encoder, Data, C, N>>,
//     corner_table: &'encoder C,
// }

// impl <'parents, 'encoder, Data, C, const N: usize> GroupManager<'encoder, Data, C, N>
//     where
//         'parents: 'encoder,
//         Data: Vector<N> + Portable,
//         Data::Component: DataValue,
//         C: GenericCornerTable,
// {
//     fn compose_groups(parents: &'encoder [&'parents Attribute], corner_table: &'parents C, cfg: Config) -> Self {
//         let mut groups = Vec::new();
//         for cfg in cfg.group_cfgs.clone() {
//             groups.push( Group::from(parents, corner_table, cfg));
//         }
//         Self {
//             partition: cfg.group_cfgs.iter().map(|cfg| {
//                 cfg.range.clone()
//             }).collect(),
//             groups,
//             corner_table,
//         }
//     }

//     fn split_unpredicted_values(&mut self) {
//         let mut set_of_value_impossible_to_predict = Vec::new();
//         for (group, indices) in &mut self.groups.iter_mut().zip(self.partition.iter_mut()) {
//             let values = group.split_unpredicted_values(indices);
//             set_of_value_impossible_to_predict.push(values);
//         }
//         let unpredicted_values = splice_disjoint_indices(set_of_value_impossible_to_predict);

//         let cfg = prediction_transform::Config{
//             ty: prediction_transform::PredictionTransformType::NoTransform,
//             portabilization: portabilization::Config{
//                 type_: portabilization::PortabilizationType::ToBits,
//                 ..portabilization::Config::default()
//             },
//             ..prediction_transform::Config::default()
//         };
//         let group = Group {
//             prediction: PredictionScheme::new(prediction_scheme::PredictionSchemeType::NoPrediction, &[], self.corner_table),
//             transform: PredictionTransform::new(cfg),
//         };
//         self.partition.push(unpredicted_values);
//         self.groups.push(group);
//     }

//     #[allow(dead_code)]
//     fn partition_iter(&self) -> impl Iterator<Item = (ops::Range<usize>, &Group<'encoder, Data, C, N>)> {
//         PartitionGroupIter::new(&self.groups, &self.partition)
//     }

//     #[allow(dead_code)]
//     fn partition_iter_mut(&mut self) -> impl Iterator<Item = (ops::Range<usize>, &mut Group<'encoder, Data, C, N>)> {
//         PartitionGroupIterMut::new(&mut self.groups, &self.partition)
//     }

//     fn partition_group_idx_iter<'a>(&'a self) -> PartitionGroupIdxIter<'a> {
//         PartitionGroupIdxIter::new(&self.partition)
//     }

//     fn compress<const WRITE_NOW: bool, W>(&mut self, attribute: &Attribute, writer: &mut W) -> Result<(), Err>
//         where W: ByteWriter
//     {
//         debug_write!("Start of Attribute Metadata", writer);
//         // write id
//         let id = attribute.get_id().as_usize();
//         if id >= 1 << 16 {
//             return Err(Err::InvalidAttributeId(id));
//         } else {
//             writer.write_u16(id as u16);
//         };

//         // write att type
//         let att_type = attribute.get_attribute_type().get_id() as u64;
//         writer.write_u8(att_type as u8);
//         #[cfg(feature = "evaluation")]
//         eval::write_json_pair(
//             "attribute type",
//             serde_json::to_value(attribute.get_attribute_type()).unwrap(),
//             writer
//         );

//         // write the attribbute length
//         let length = attribute.len() as u64;
//         writer.write_u64(length);
//         // for evaluation, write the data size in bytes
//         #[cfg(feature = "evaluation")]
//         eval::write_json_pair(
//             "data size in bytes",
//             // data size in bytes
//             serde_json::to_value(length * std::mem::size_of::<Data>() as u64).unwrap(),
//             writer
//         );

//         // write component type
//         let component_type = attribute.get_component_type().get_id() as u8;
//         writer.write_u8(component_type);
//         #[cfg(feature = "evaluation")]
//         eval::write_json_pair(
//             "component type",
//             serde_json::to_value(attribute.get_component_type()).unwrap(),
//             writer
//         );

//         // write number of components
//         let num_components = attribute.get_num_components();
//         if num_components >= 1 << 8 {
//             return Err(Err::UnsupportedNumComponents(num_components as usize));
//         }
//         writer.write_u8(num_components as u8);
//         #[cfg(feature = "evaluation")]
//         eval::write_json_pair(
//             "number of components",
//             serde_json::to_value(num_components).unwrap(),
//             writer
//         );

//         // write parents
//         let num_parents = attribute.get_parents().len();
//         if num_parents >= 1 << 8 {
//             return Err(Err::TooManyParents(num_parents as usize));
//         }
//         writer.write_u8(num_parents as u8);
//         #[cfg(feature = "evaluation")]
//         eval::write_json_pair(
//             "number of parents",
//             serde_json::to_value(num_parents).unwrap(),
//             writer
//         );

//         for parent in attribute.get_parents() {
//             let parent_id = parent.as_usize();
//             if parent_id >= 1 << 16 {
//                 return Err(Err::InvalidAttributeId(parent_id));
//             } else {
//                 writer.write_u16(parent_id as u16);
//             }
//         }
//         #[cfg(feature = "evaluation")]
//         {
//             let parents = attribute.get_parents();
//             eval::write_json_pair(
//                 "parents",
//                 serde_json::to_value(parents).unwrap(),
//                 writer
//             );
//         }

//         debug_write!("End of Attribute Metadata", writer);

//         // Prediction
//         for (_ranges, _group) in self.partition.iter().zip(self.groups.iter_mut()) {
//             // group.predict_and_transform(ranges, attribute);
//         }

//         debug_write!("Start of Transform Metadata", writer);
//         // write number of groups
//         let num_groups = self.groups.len();
//         if num_groups >= 1 << 8 {
//             return Err(Err::TooManyEncodingGroups(num_groups));
//         }
//         writer.write_u8(num_groups as u8);
//         // Squeeze the transformed data and write it
//         let mut transform_outputs = Vec::new();
//         transform_outputs.reserve(self.groups.len());

//         #[cfg(feature = "evaluation")]
//         eval::array_scope_begin("groups", writer);

//         for (mut group, _ranges) in std::mem::take(&mut self.groups).into_iter().zip(self.partition.iter()) {
//             #[cfg(feature = "evaluation")]
//             {
//                 eval::scope_begin("group", writer);
//                 eval::write_json_pair("prediction", group.prediction.get_type().to_string().into(), writer);
//                 eval::write_json_pair("indices", format!("{:?}", _ranges).into(), writer);
//             }

//             // write prediction id
//             let prediction_id = group.prediction.get_type().get_id();
//             if prediction_id >= 1 << 4 {
//                 return Err(Err::InvalidPredictionSchemeId(prediction_id as usize));
//             }
//             writer.write_u8(prediction_id);

//             debug_write!("Start of Prediction Transform Metadata", writer);
//             // write transform id
//             let transform_id = group.transform.get_type().get_id();
//             if transform_id >= 1 << 4 {
//                 return Err(Err::InvalidPredictionSchemeId(transform_id as usize));
//             }
//             writer.write_u8(transform_id);

//             #[cfg(feature = "evaluation")]
//             eval::scope_begin("transform", writer);
//             group.squeeze_transformed_data(writer);
//             #[cfg(feature = "evaluation")]
//             eval::scope_end(writer);

//             #[cfg(feature = "evaluation")]
//             eval::scope_begin("portabilization", writer);
//             transform_outputs.push(group.take_output(writer).into_iter());
//             #[cfg(feature = "evaluation")]
//             eval::scope_end(writer);

//             #[cfg(feature = "evaluation")]
//             eval::scope_end(writer);

//             debug_write!("End of Prediction Transform Metadata", writer);
//         }

//         #[cfg(feature = "evaluation")]
//         eval::array_scope_end(writer);

//         debug_write!("End of Transform Metadata", writer);

//         for (range, gp_idx) in self.partition_group_idx_iter() {
//             debug_write!("Start of a Range", writer);
//             writer.write_u8(gp_idx as u8);
//             let range_size = range.end - range.start;
//             // ToDo: Reduce the size by realizing the fact that range size is always less than the attrubute size.
//             writer.write_u64(range_size as u64);
//             for _ in range {
//                 transform_outputs[gp_idx].next().unwrap();
//             }
//         }
//         Ok(())
//     }
// }

// struct PartitionGroupIdxIter<'groups> {
//     curr_pos: usize,
//     ranges: &'groups Vec<Vec<ops::Range<usize>>>,
//     is_done: bool,
// }

// impl<'groups> PartitionGroupIdxIter<'groups> {
//     fn new(ranges: &'groups Vec<Vec<ops::Range<usize>>>) -> Self {
//         Self {
//             curr_pos: 0,
//             ranges,
//             is_done: false,
//         }
//     }
// }

// impl<'groups> Iterator for PartitionGroupIdxIter<'groups> {
//     type Item = (ops::Range<usize>, usize);

//     fn next(&mut self) -> Option<Self::Item> {
//         if self.is_done {
//             return None;
//         }

//         let mut out = None;
//         for (gp_idx, ranges) in self.ranges.iter().enumerate() {
//             if let Some(range) = ranges.iter().find(|r| r.start == self.curr_pos) {
//                 out = Some(
//                     (gp_idx, range.clone())
//                 );
//             }
//         }

//         match out {
//             Some((gp_idx, range)) => {
//                 self.curr_pos = range.end;
//                 Some((range, gp_idx))
//             },
//             None => {
//                 self.is_done = true;
//                 None
//             }
//         }
//     }
// }

// struct PartitionGroupIter<'encoder, 'groups, Data, C, const N: usize>
//     where Data: Vector<N> + Portable
// {
//     curr_pos: usize,
//     groups: &'groups [Group<'encoder, Data, C, N>],
//     ranges: &'groups Vec<Vec<ops::Range<usize>>>,
//     is_done: bool,
// }

// impl<'encoder, 'groups, Data, C, const N: usize> PartitionGroupIter<'encoder, 'groups, Data, C, N>
//     where
//         Data: Vector<N> + Portable,
//         C: GenericCornerTable,
//         'encoder: 'groups,
// {
//     fn new(groups: &'groups [Group<'encoder, Data, C, N>], ranges: &'groups Vec<Vec<ops::Range<usize>>>) -> Self {
//         Self {
//             curr_pos: 0,
//             groups,
//             ranges,
//             is_done: false,
//         }
//     }
// }

// impl<'encoder, 'groups, Data, C, const N: usize> Iterator for PartitionGroupIter<'encoder, 'groups, Data, C, N>
//     where Data: Vector<N> + Portable,
// {
//     type Item = (ops::Range<usize>, &'groups Group<'encoder, Data, C, N>);

//     fn next(&mut self) -> Option<Self::Item> {
//         if self.is_done {
//             return None;
//         }

//         let mut out = None;
//         for (gp_idx, ranges) in self.ranges.iter().enumerate() {
//             if let Some(range) = ranges.iter().find(|r| r.start == self.curr_pos) {
//                 out = Some(
//                     (gp_idx, range.clone())
//                 );
//             }
//         }

//         match out {
//             Some((gp_idx, range)) => {
//                 self.curr_pos = range.end;
//                 Some((range, &self.groups[gp_idx]))
//             },
//             None => {
//                 self.is_done = true;
//                 None
//             }
//         }
//     }
// }

// struct PartitionGroupIterMut<'encoder, 'groups, Data, C, const N: usize>
//     where Data: Vector<N> + Portable
// {
//     curr_pos: usize,
//     groups: &'groups mut [Group<'encoder, Data, C, N>],
//     ranges: &'groups Vec<Vec<ops::Range<usize>>>,
//     is_done: bool,
// }

// impl<'encoder, 'groups, Data, C, const N: usize> PartitionGroupIterMut<'encoder, 'groups, Data, C, N>
//     where
//         Data: Vector<N> + Portable,
//         'encoder: 'groups,
// {
//     fn new(groups: &'groups mut [Group<'encoder, Data, C, N>], ranges: &'groups Vec<Vec<ops::Range<usize>>>) -> Self {
//         Self {
//             curr_pos: 0,
//             groups,
//             ranges,
//             is_done: false,
//         }
//     }
// }

// impl<'encoder, 'groups, Data, C, const N: usize> Iterator for PartitionGroupIterMut<'encoder, 'groups, Data, C, N>
//     where
//         Data: Vector<N> + Portable,
//         'encoder: 'groups,
// {
//     type Item = (ops::Range<usize>, &'groups mut Group<'encoder, Data, C, N>);

//     fn next(&mut self) -> Option<Self::Item> {
//         if self.is_done {
//             return None;
//         }

//         let mut out = None;
//         for (gp_idx, ranges) in self.ranges.iter().enumerate() {
//             if let Some(range) = ranges.iter().find(|r| r.start == self.curr_pos) {
//                 out = Some(
//                     (gp_idx, range.clone())
//                 );
//             }
//         }

//         match out {
//             Some((gp_idx, range)) => {
//                 self.curr_pos = range.end;
//                 let group = &mut self.groups[gp_idx] as *mut Group<'encoder, Data, C, N>;
//                 // SAFETY: We ensure that the mutable reference is not used elsewhere.
//                 Some((range, unsafe { &mut *group }))
//             },
//             None => {
//                 self.is_done = true;
//                 None
//             }
//         }
//     }
// }