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
#![doc = include_str!("../README.md")]
pub mod pcl_utils;
pub mod ros_types;

use crate::pcl_utils::*;
use crate::ros_types::{PointCloud2Msg, PointFieldMsg};
use num_traits::Zero;

#[macro_use]
pub extern crate mem_macros;

pub extern crate fallible_iterator;

/// Errors that can occur when converting between PointCloud2 and a custom type.
#[derive(Debug)]
pub enum ConversionError {
    InvalidFieldFormat,
    NotEnoughFields,
    TooManyDimensions,
    UnsupportedFieldType,
    NoPoints,
    DataLengthMismatch,
    MetaIndexLengthMismatch,
    EndOfBuffer,
    PointConversionError,
    MetaDatatypeMismatch,
}

/// Trait to convert a point to a tuple of coordinates and meta data.
/// Implement this trait for your point type to use the conversion.
pub trait PointConvertible<T, const SIZE: usize, const DIM: usize, const METADIM: usize>:
    TryInto<([T; DIM], [PointMeta; METADIM])>
    + TryFrom<([T; DIM], [PointMeta; METADIM])>
    + MetaNames<METADIM>
    + Clone
where
    T: FromBytes,
{
}

/// Datatypes from the [PointField message](http://docs.ros.org/en/melodic/api/sensor_msgs/html/msg/PointField.html).
#[derive(Clone, Debug, PartialEq, Copy)]
pub enum FieldDatatype {
    F32,
    F64,
    I32,
    U8,
    U16,
    U32,
    I8,
    I16,
}

/// Getter trait for the datatype of a field value.
pub trait GetFieldDatatype {
    fn field_datatype() -> FieldDatatype;
}

impl GetFieldDatatype for f32 {
    fn field_datatype() -> FieldDatatype {
        FieldDatatype::F32
    }
}

impl GetFieldDatatype for f64 {
    fn field_datatype() -> FieldDatatype {
        FieldDatatype::F64
    }
}

impl GetFieldDatatype for i32 {
    fn field_datatype() -> FieldDatatype {
        FieldDatatype::I32
    }
}

impl GetFieldDatatype for u8 {
    fn field_datatype() -> FieldDatatype {
        FieldDatatype::U8
    }
}

impl GetFieldDatatype for u16 {
    fn field_datatype() -> FieldDatatype {
        FieldDatatype::U16
    }
}

impl GetFieldDatatype for u32 {
    fn field_datatype() -> FieldDatatype {
        FieldDatatype::U32
    }
}

impl GetFieldDatatype for i8 {
    fn field_datatype() -> FieldDatatype {
        FieldDatatype::I8
    }
}

impl GetFieldDatatype for i16 {
    fn field_datatype() -> FieldDatatype {
        FieldDatatype::I16
    }
}

fn convert_to_msg_code(geo_type: &FieldDatatype) -> u8 {
    match geo_type {
        FieldDatatype::I8 => 1,
        FieldDatatype::U8 => 2,
        FieldDatatype::I16 => 3,
        FieldDatatype::U16 => 4,
        FieldDatatype::I32 => 5,
        FieldDatatype::U32 => 6,
        FieldDatatype::F32 => 7,
        FieldDatatype::F64 => 8,
    }
}

fn convert_msg_code_to_type(code: u8) -> Result<FieldDatatype, ConversionError> {
    match code {
        7 => Ok(FieldDatatype::F32),
        8 => Ok(FieldDatatype::F64),
        5 => Ok(FieldDatatype::I32),
        2 => Ok(FieldDatatype::U8),
        4 => Ok(FieldDatatype::U16),
        6 => Ok(FieldDatatype::U32),
        1 => Ok(FieldDatatype::I8),
        3 => Ok(FieldDatatype::I16),
        _ => Err(ConversionError::UnsupportedFieldType),
    }
}

fn check_coord(
    coord: Option<usize>,
    fields: &[PointFieldMsg],
    xyz_field_type: &FieldDatatype,
) -> Result<PointFieldMsg, ConversionError> {
    match coord {
        Some(y_idx) => {
            let field = &fields[y_idx];
            if field.datatype != convert_to_msg_code(xyz_field_type) {
                return Err(ConversionError::InvalidFieldFormat);
            }
            Ok(field.clone())
        }
        None => Err(ConversionError::NotEnoughFields),
    }
}

/// Matching field names from each meta data per point to the PointField name.
/// Always make sure to use the same order as in your Into<> implementation to have a correct mapping.
pub trait MetaNames<const METADIM: usize> {
    fn meta_names() -> [String; METADIM];
}

/// The Convert struct is used to convert between a PointCloud2 message and a custom type.
/// A custom type must implement the FromBytes trait and the Into trait.
/// The Into trait is used to convert the custom type into a tuple of the coordinates and the meta data.
/// The FromBytes trait is used to convert the bytes from the PointCloud2 message into the custom type.
///
/// # Example
/// ```
/// use ros_pointcloud2::mem_macros::size_of;
/// use ros_pointcloud2::{Convert, PointMeta};
/// use ros_pointcloud2::ros_types::PointCloud2Msg;
/// const DIM : usize = 3; // how many dimensions your point has (e.g. x, y, z)
/// const METADIM : usize = 4; // how many meta fields you have (e.g. r, g, b, a)
/// type MyConverter = Convert<f32, { size_of!(f32) }, DIM, METADIM, ([f32; DIM], [PointMeta; METADIM])>;
/// ```
pub struct Convert<T: FromBytes, const SIZE: usize, const DIM: usize, const METADIM: usize, C>
where
    T: FromBytes,
    C: PointConvertible<T, SIZE, DIM, METADIM>,
{
    iteration: usize,
    coordinates: Vec<C>,
    phantom_t: std::marker::PhantomData<T>,
    data: Vec<u8>,
    point_step_size: usize,
    cloud_length: usize,
    offsets: Vec<usize>,
    big_endian: Endianness,
    xyz_field_type: FieldDatatype,
    meta: Vec<(String, FieldDatatype)>,
}

pub type ConvertXYZ = Convert<f32, { size_of!(f32) }, 3, 0, PointXYZ>;
pub type ConvertXYZI = Convert<f32, { size_of!(f32) }, 3, 1, PointXYZI>;
pub type ConvertXYZNormal = Convert<f32, { size_of!(f32) }, 3, 3, PointXYZNormal>;
pub type ConvertXYZRGB = Convert<f32, { size_of!(f32) }, 3, 1, PointXYZRGB>;
pub type ConvertXYZRGBL = Convert<f32, { size_of!(f32) }, 3, 2, PointXYZRGBL>;
pub type ConvertXYZRGBA = Convert<f32, { size_of!(f32) }, 3, 2, PointXYZRGBA>;
pub type ConvertXYZRGBNormal = Convert<f32, { size_of!(f32) }, 3, 4, PointXYZRGBNormal>;
pub type ConvertXYZINormal = Convert<f32, { size_of!(f32) }, 3, 4, PointXYZINormal>;
pub type ConvertXYZL = Convert<f32, { size_of!(f32) }, 3, 1, PointXYZL>;

impl<T, const SIZE: usize, const DIM: usize, const METADIM: usize, C> TryFrom<Vec<C>>
    for Convert<T, SIZE, DIM, METADIM, C>
where
    T: FromBytes,
    C: PointConvertible<T, SIZE, DIM, METADIM>,
{
    type Error = ConversionError;

    /// Converts a vector of custom types into a Convert struct that implements the Iterator trait.
    ///
    /// # Example
    /// ```
    /// use ros_pointcloud2::{ConvertXYZ, ConversionError};
    /// use ros_pointcloud2::pcl_utils::PointXYZ;
    ///
    /// let cloud_points: Vec<PointXYZ> = vec![
    ///     PointXYZ { x: 1.0, y: 2.0, z: 3.0 },
    ///     PointXYZ { x: 4.0, y: 5.0, z: 6.0 },
    /// ];
    /// let convert: Result<ConvertXYZ, ConversionError> = ConvertXYZ::try_from(cloud_points);
    /// ```
    fn try_from(cloud: Vec<C>) -> Result<Self, Self::Error> {
        let length = cloud.len();

        let mut meta: Vec<(String, FieldDatatype)> = vec![];
        let first_point = cloud.first().ok_or(ConversionError::NoPoints)?;
        let point_meta: ([T; DIM], [PointMeta; METADIM]) = match first_point.clone().try_into() {
            Ok(point_meta) => point_meta,
            Err(_) => {
                return Err(ConversionError::PointConversionError);
            }
        };
        let meta_names = C::meta_names();
        let mut point_step_size = DIM * SIZE;
        for (idx, value) in point_meta.1.iter().enumerate() {
            meta.push((
                meta_names
                    .get(idx)
                    .ok_or(ConversionError::MetaIndexLengthMismatch)?
                    .clone(),
                value.datatype,
            ));
            point_step_size += datatype_size(&value.datatype);
        }

        Ok(Self {
            phantom_t: std::marker::PhantomData,
            iteration: usize::zero(),
            coordinates: cloud,
            data: Vec::new(),
            point_step_size,
            cloud_length: length,
            offsets: Vec::new(),
            big_endian: Endianness::Little,
            xyz_field_type: T::field_datatype(),
            meta,
        })
    }
}

/// Meta data representation for a point
///
/// This struct is used to store meta data in a fixed size byte buffer along the with the
/// datatype that is encoded so that it can be decoded later.
///
/// # Example
/// ```
/// use ros_pointcloud2::PointMeta;
///
/// let original_data: f64 = 1.0;
/// let meta = PointMeta::new(original_data);
/// let my_data: f64 = meta.get().unwrap();
/// ```
#[derive(Debug, Clone, Copy)]
pub struct PointMeta {
    bytes: [u8; size_of!(f64)],
    datatype: FieldDatatype,
}

impl Default for PointMeta {
    fn default() -> Self {
        Self {
            bytes: [0; size_of!(f64)],
            datatype: FieldDatatype::F32,
        }
    }
}

impl PointMeta {
    /// Create a new PointMeta from a value
    ///
    /// # Example
    /// ```
    /// let meta = ros_pointcloud2::PointMeta::new(1.0);
    /// ```
    pub fn new<T: FromBytes>(value: T) -> Self {
        let raw_bytes = T::bytes(&value);
        let mut bytes = [0; size_of!(f64)];
        for (idx, byte) in raw_bytes.iter().enumerate() {
            bytes[idx] = *byte;
        }
        Self {
            bytes,
            datatype: T::field_datatype(),
        }
    }

    fn new_from_buffer(
        data: &[u8],
        offset: usize,
        datatype: &FieldDatatype,
    ) -> Result<Self, ConversionError> {
        let size = datatype_size(datatype);
        let bytes = data
            .get(offset..offset + size)
            .ok_or(ConversionError::DataLengthMismatch)?;
        let mut bytes_array = [0; size_of!(f64)]; // 8 bytes as f64 is the largest type
        for (idx, byte) in bytes.iter().enumerate() {
            bytes_array[idx] = *byte;
        }
        Ok(Self {
            bytes: bytes_array,
            datatype: *datatype,
        })
    }

    /// Get the value from the PointMeta. It will return None if the datatype does not match.
    ///
    /// # Example
    /// ```
    /// let original_data: f64 = 1.0;
    /// let meta = ros_pointcloud2::PointMeta::new(original_data);
    /// let my_data: f64 = meta.get().unwrap();
    /// ```
    pub fn get<T: FromBytes>(&self) -> Result<T, ConversionError> {
        if self.datatype != T::field_datatype() {
            return Err(ConversionError::MetaDatatypeMismatch);
        }
        let size = datatype_size(&T::field_datatype());
        if let Some(bytes) = self.bytes.get(0..size) {
            Ok(T::from_le_bytes(bytes))
        } else {
            Err(ConversionError::DataLengthMismatch) // self.bytes are not long enough, already handled while parsing
        }
    }
}

impl<T, const SIZE: usize, const DIM: usize, const METADIM: usize, C> TryFrom<PointCloud2Msg>
    for Convert<T, SIZE, DIM, METADIM, C>
where
    T: FromBytes,
    C: PointConvertible<T, SIZE, DIM, METADIM>,
{
    type Error = ConversionError;

    /// Converts a ROS PointCloud2 message into a Convert struct that implements the Iterator trait.
    /// Iterate over the struct to get or use the points.
    ///
    /// # Example
    /// Since we do not have a ROS node here, we first create a PointCloud2 message and then convert back to a Convert struct.
    /// ```
    /// use ros_pointcloud2::ros_types::PointCloud2Msg;
    /// use ros_pointcloud2::ConvertXYZ;
    /// use ros_pointcloud2::pcl_utils::PointXYZ;
    ///
    /// let cloud_points: Vec<PointXYZ> = vec![
    ///     PointXYZ { x: 1.0, y: 2.0, z: 3.0 },
    ///     PointXYZ { x: 4.0, y: 5.0, z: 6.0 },
    /// ];
    /// let msg: PointCloud2Msg = ConvertXYZ::try_from(cloud_points).unwrap().try_into().unwrap();
    ///
    /// let convert: ConvertXYZ = ConvertXYZ::try_from(msg).unwrap(); // parse message
    /// ```
    fn try_from(cloud: PointCloud2Msg) -> Result<Self, Self::Error> {
        if cloud.fields.len() < DIM {
            return Err(ConversionError::NotEnoughFields);
        }

        let xyz_field_type = T::field_datatype();

        let mut has_x: Option<usize> = None;
        let mut has_y: Option<usize> = None;
        let mut has_z: Option<usize> = None;

        let mut meta_with_offsets: Vec<(String, FieldDatatype, usize)> =
            Vec::with_capacity(METADIM);

        let lower_meta_names = C::meta_names()
            .iter()
            .map(|x| x.to_lowercase())
            .collect::<Vec<String>>();
        for (idx, field) in cloud.fields.iter().enumerate() {
            let lower_field_name = field.name.to_lowercase();
            match lower_field_name.as_str() {
                "x" => has_x = Some(idx),
                "y" => has_y = Some(idx),
                "z" => has_z = Some(idx),
                _ => {
                    if lower_meta_names.contains(&lower_field_name) {
                        meta_with_offsets.push((
                            field.name.clone(),
                            convert_msg_code_to_type(field.datatype)?,
                            field.offset as usize,
                        ));
                    }
                }
            }
        }

        meta_with_offsets.sort_by(|a, b| a.2.cmp(&b.2));
        let meta_offsets: Vec<usize> = meta_with_offsets.iter().map(|x| x.2).collect();
        let meta: Vec<(String, FieldDatatype)> = meta_with_offsets
            .iter()
            .map(|x| (x.0.clone(), x.1))
            .collect();

        let x_field = check_coord(has_x, &cloud.fields, &xyz_field_type)?;
        let y_field = check_coord(has_y, &cloud.fields, &xyz_field_type)?;

        let mut offsets = vec![x_field.offset as usize, y_field.offset as usize];

        let z_field = check_coord(has_z, &cloud.fields, &xyz_field_type);
        match z_field {
            Ok(z_field) => {
                offsets.push(z_field.offset as usize);
            }
            Err(err) => match err {
                ConversionError::NotEnoughFields => {
                    if DIM == 3 {
                        return Err(ConversionError::NotEnoughFields);
                    }
                }
                _ => return Err(err),
            },
        }

        let endian = if cloud.is_bigendian {
            Endianness::Big
        } else {
            Endianness::Little
        };

        if offsets.len() != DIM {
            return Err(ConversionError::NotEnoughFields);
        }

        offsets.extend(meta_offsets);

        if offsets.len() != DIM + METADIM {
            return Err(ConversionError::NotEnoughFields);
        }

        let point_step_size = cloud.point_step as usize;
        if point_step_size * cloud.width as usize * cloud.height as usize != cloud.data.len() {
            return Err(ConversionError::DataLengthMismatch);
        }

        if let Some(max_offset) = offsets.last() {
            if let Some(last_meta) = meta.last() {
                let size_with_last_meta = max_offset + datatype_size(&last_meta.1);
                if size_with_last_meta > point_step_size {
                    return Err(ConversionError::DataLengthMismatch);
                }
            }
        }

        Ok(Self {
            phantom_t: std::marker::PhantomData,
            iteration: usize::zero(),
            coordinates: Vec::new(),
            data: cloud.data,
            point_step_size,
            cloud_length: cloud.width as usize * cloud.height as usize,
            offsets,
            big_endian: endian,
            xyz_field_type: T::field_datatype(),
            meta,
        })
    }
}

fn datatype_size(datatype: &FieldDatatype) -> usize {
    match datatype {
        FieldDatatype::U8 => 1,
        FieldDatatype::U16 => 2,
        FieldDatatype::U32 => 4,
        FieldDatatype::I8 => 1,
        FieldDatatype::I16 => 2,
        FieldDatatype::I32 => 4,
        FieldDatatype::F32 => 4,
        FieldDatatype::F64 => 8,
    }
}

impl<T, const SIZE: usize, const DIM: usize, const METADIM: usize, C> TryInto<PointCloud2Msg>
    for Convert<T, SIZE, DIM, METADIM, C>
where
    T: FromBytes,
    C: PointConvertible<T, SIZE, DIM, METADIM>,
{
    type Error = ConversionError;

    /// Convert the point cloud to a ROS message.
    /// First use the `try_from` method for initializing the conversion and parsing meta data.
    /// Then use the `try_into` method to do the actual conversion.
    ///
    /// # Example
    /// ```
    /// use ros_pointcloud2::ros_types::PointCloud2Msg;
    /// use ros_pointcloud2::ConvertXYZ;
    /// use ros_pointcloud2::pcl_utils::PointXYZ;
    ///
    /// let cloud_points: Vec<PointXYZ> = vec![
    ///     PointXYZ { x: 1.0, y: 2.0, z: 3.0 },
    ///     PointXYZ { x: 4.0, y: 5.0, z: 6.0 },
    /// ];
    /// let msg_out: PointCloud2Msg = ConvertXYZ::try_from(cloud_points).unwrap().try_into().unwrap();
    /// ```
    fn try_into(self) -> Result<PointCloud2Msg, Self::Error> {
        let mut cloud = PointCloud2Msg::default();

        // Define the message fields
        let mut fields = Vec::new();
        if DIM > 3 {
            return Err(ConversionError::TooManyDimensions);
        }

        let datatype: u8 = convert_to_msg_code(&self.xyz_field_type);

        if DIM > 1 {
            fields.push(PointFieldMsg {
                name: "x".to_string(),
                offset: 0,
                datatype,
                count: 1,
            });
            fields.push(PointFieldMsg {
                name: "y".to_string(),
                offset: SIZE as u32,
                datatype,
                count: 1,
            });
        }

        if DIM == 3 {
            fields.push(PointFieldMsg {
                name: "z".to_string(),
                offset: 2 * SIZE as u32,
                datatype,
                count: 1,
            });
        }

        // meta data fields
        let first_point = self.coordinates.first().ok_or(ConversionError::NoPoints)?;
        let meta: ([T; DIM], [PointMeta; METADIM]) = match first_point.clone().try_into() {
            Ok(meta) => meta,
            Err(_) => return Err(ConversionError::PointConversionError),
        };

        let meta_names = C::meta_names();

        let sized_dim = DIM as u32 * SIZE as u32;

        for (idx, value) in meta.1.iter().enumerate() {
            let datatype: u8 = convert_to_msg_code(&value.datatype);
            let mut offset = sized_dim;
            for i in 0..idx {
                let datatype: u8 = convert_to_msg_code(&meta.1[i].datatype);
                let field_type = convert_msg_code_to_type(datatype)?;
                let field_size = datatype_size(&field_type);
                offset += field_size as u32;
            }
            fields.push(PointFieldMsg {
                name: meta_names[idx].to_string(),
                offset,
                datatype,
                count: 1,
            });
        }

        // calc all meta data points step size
        let mut step_size = 0;
        for field in fields.iter() {
            let field_type = convert_msg_code_to_type(field.datatype)?;
            let field_size = datatype_size(&field_type);
            step_size += field.count * field_size as u32;
        }

        cloud.fields = fields;
        cloud.point_step = step_size;
        cloud.row_step = self.coordinates.len() as u32 * cloud.point_step;
        cloud.height = 1;
        cloud.width = self.coordinates.len() as u32;
        cloud.is_bigendian = false;
        cloud.is_dense = true;

        for coords in self.coordinates {
            let coords_data: ([T; DIM], [PointMeta; METADIM]) = match coords.try_into() {
                Ok(meta) => meta,
                Err(_) => return Err(ConversionError::PointConversionError),
            };
            coords_data
                .0
                .iter()
                .for_each(|x| cloud.data.extend_from_slice(T::bytes(x).as_slice()));
            coords_data.1.iter().for_each(|meta| {
                let truncated_bytes = &meta.bytes[0..datatype_size(&meta.datatype)];
                cloud.data.extend_from_slice(truncated_bytes);
            });
        }

        Ok(cloud)
    }
}

impl<T, const SIZE: usize, const DIM: usize, const METADIM: usize, C>
    Convert<T, SIZE, DIM, METADIM, C>
where
    T: FromBytes,
    C: PointConvertible<T, SIZE, DIM, METADIM>,
{
    /// Convenience getter for the number of points in the cloud.
    pub fn len(&self) -> usize {
        self.cloud_length
    }

    pub fn is_empty(&self) -> bool {
        self.cloud_length == 0
    }
}

impl<T, const SIZE: usize, const DIM: usize, const METADIM: usize, C>
    fallible_iterator::FallibleIterator for Convert<T, SIZE, DIM, METADIM, C>
where
    T: FromBytes,
    C: PointConvertible<T, SIZE, DIM, METADIM>,
{
    type Item = C;
    type Error = ConversionError;

    /// Iterate over the data buffer and interpret the data as a point.
    fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> {
        if self.iteration >= self.cloud_length {
            return Ok(None); // iteration finished
        }

        let mut xyz: [T; DIM] = [T::zero(); DIM];
        let mut meta: [PointMeta; METADIM] = [PointMeta::default(); METADIM];
        for (idx, in_point_offset) in self.offsets.iter().enumerate() {
            if idx < DIM {
                match load_loadable::<T, SIZE>(
                    (self.iteration * self.point_step_size) + in_point_offset,
                    &self.data,
                    self.big_endian.clone(),
                ) {
                    Some(c) => xyz[idx] = c,
                    None => return Err(ConversionError::EndOfBuffer),
                }
            } else {
                let meta_idx = idx - DIM;
                let meta_type = self.meta[meta_idx].1;
                let start = (self.iteration * self.point_step_size) + in_point_offset;
                if let Ok(m) = PointMeta::new_from_buffer(&self.data, start, &meta_type) {
                    meta[meta_idx] = m;
                } else {
                    return Err(ConversionError::MetaIndexLengthMismatch); // since we can not return an error in the iterator, we finish the iteration here early since the last point is not valid. This case is not expected since we catch it while parsing the file.
                }
            }
        }

        self.iteration += 1;
        let conv = C::try_from((xyz, meta)); // try convert the point
        match conv {
            Err(_) => Err(ConversionError::PointConversionError),
            Ok(tuple) => Ok(Some(tuple)),
        }
    }
}

/// This trait is used to convert a byte slice to a primitive type.
/// All PointField types are supported.
pub trait FromBytes: Zero + Sized + Copy + GetFieldDatatype {
    fn from_be_bytes(bytes: &[u8]) -> Self;
    fn from_le_bytes(bytes: &[u8]) -> Self;

    fn bytes(_: &Self) -> Vec<u8>;
}

impl FromBytes for i8 {
    fn from_be_bytes(bytes: &[u8]) -> Self {
        Self::from_be_bytes([bytes[0]])
    }
    fn from_le_bytes(bytes: &[u8]) -> Self {
        Self::from_le_bytes([bytes[0]])
    }

    fn bytes(x: &i8) -> Vec<u8> {
        Vec::from(x.to_le_bytes())
    }
}

impl FromBytes for i16 {
    fn from_be_bytes(bytes: &[u8]) -> Self {
        Self::from_be_bytes([bytes[0], bytes[1]])
    }
    fn from_le_bytes(bytes: &[u8]) -> Self {
        Self::from_le_bytes([bytes[0], bytes[1]])
    }

    fn bytes(x: &i16) -> Vec<u8> {
        Vec::from(x.to_le_bytes())
    }
}

impl FromBytes for u16 {
    fn from_be_bytes(bytes: &[u8]) -> Self {
        Self::from_be_bytes([bytes[0], bytes[1]])
    }
    fn from_le_bytes(bytes: &[u8]) -> Self {
        Self::from_le_bytes([bytes[0], bytes[1]])
    }

    fn bytes(x: &u16) -> Vec<u8> {
        Vec::from(x.to_le_bytes())
    }
}

impl FromBytes for u32 {
    fn from_be_bytes(bytes: &[u8]) -> Self {
        Self::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }
    fn from_le_bytes(bytes: &[u8]) -> Self {
        Self::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }

    fn bytes(x: &u32) -> Vec<u8> {
        Vec::from(x.to_le_bytes())
    }
}

impl FromBytes for f32 {
    fn from_be_bytes(bytes: &[u8]) -> Self {
        Self::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }
    fn from_le_bytes(bytes: &[u8]) -> Self {
        Self::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }

    fn bytes(x: &f32) -> Vec<u8> {
        Vec::from(x.to_le_bytes())
    }
}

impl FromBytes for i32 {
    fn from_be_bytes(bytes: &[u8]) -> Self {
        Self::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }
    fn from_le_bytes(bytes: &[u8]) -> Self {
        Self::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
    }

    fn bytes(x: &i32) -> Vec<u8> {
        Vec::from(x.to_le_bytes())
    }
}

impl FromBytes for f64 {
    fn from_be_bytes(bytes: &[u8]) -> Self {
        Self::from_be_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
        ])
    }
    fn from_le_bytes(bytes: &[u8]) -> Self {
        Self::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
        ])
    }

    fn bytes(x: &f64) -> Vec<u8> {
        Vec::from(x.to_le_bytes())
    }
}

impl FromBytes for u8 {
    fn from_be_bytes(bytes: &[u8]) -> Self {
        Self::from_be_bytes([bytes[0]])
    }
    fn from_le_bytes(bytes: &[u8]) -> Self {
        Self::from_le_bytes([bytes[0]])
    }

    fn bytes(x: &u8) -> Vec<u8> {
        Vec::from(x.to_le_bytes())
    }
}

#[derive(Clone, Debug, PartialEq)]
enum Endianness {
    Big,
    Little,
}

fn load_loadable<T: FromBytes, const SIZE: usize>(
    start_idx: usize,
    data: &[u8],
    endian: Endianness,
) -> Option<T> {
    match endian {
        Endianness::Big => Some(T::from_be_bytes(
            load_bytes::<SIZE>(start_idx, data)?.as_slice(),
        )),
        Endianness::Little => Some(T::from_le_bytes(
            load_bytes::<SIZE>(start_idx, data)?.as_slice(),
        )),
    }
}

fn load_bytes<const S: usize>(start_idx: usize, data: &[u8]) -> Option<[u8; S]> {
    if start_idx + S > data.len() {
        return None;
    }
    let mut buff: [u8; S] = [u8::zero(); S];
    for (byte, buff_val) in buff.iter_mut().enumerate().take(S) {
        let raw_byte = data.get(start_idx + byte);
        match raw_byte {
            None => {
                return None;
            }
            Some(some_byte) => {
                *buff_val = *some_byte;
            }
        }
    }

    Some(buff)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_bytes() {
        i8::bytes(&1);
        u8::bytes(&1);
        i16::bytes(&1);
        u16::bytes(&1);
        i32::bytes(&1);
        u32::bytes(&1);
        f32::bytes(&1.0);
        f64::bytes(&1.0);
    }
}