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
use crate::Error::{NoData, ShapeMisMatch};
use crate::{Error, PointDataColumnType};
use chrono::{DateTime, Timelike, Utc};
use nalgebra::Point3;
use palette::Srgb;
use polars::datatypes::DataType;
use polars::frame::DataFrame;
use polars::prelude::NamedFrom;
use polars::series::Series;

pub struct PointDataColumns {
    pub point: Vec<Point3<f64>>,
    pub id: Option<Vec<u64>>,
    pub frame_id: Option<Vec<String>>,
    pub timestamp: Option<Vec<DateTime<Utc>>>,
    pub intensity: Option<Vec<f32>>,
    pub beam_origin: Option<Vec<Point3<f64>>>,
    pub color: Option<Vec<Srgb<u16>>>,
}

impl PointDataColumns {
    pub fn new(
        point: Vec<Point3<f64>>,
        id: Option<Vec<u64>>,
        frame_id: Option<Vec<String>>,
        timestamp: Option<Vec<DateTime<Utc>>>,
        intensity: Option<Vec<f32>>,
        beam_origin: Option<Vec<Point3<f64>>>,
        color: Option<Vec<Srgb<u16>>>,
    ) -> Result<Self, Error> {
        if point.is_empty() {
            return Err(NoData("point"));
        }
        let total_length = point.len();

        if let Some(id) = &id {
            if id.len() != total_length {
                return Err(ShapeMisMatch(
                    "id vector has a different length than the point vector",
                ));
            }
        }
        if let Some(frame_id_entries) = &frame_id {
            if frame_id_entries.len() != total_length {
                return Err(ShapeMisMatch(
                    "frame_id vector has a different length than the point vector",
                ));
            }
        }
        if let Some(timestamp_entries) = &timestamp {
            if timestamp_entries.len() != total_length {
                return Err(ShapeMisMatch(
                    "frame_id vector has a different length than the point vector",
                ));
            }
        }
        if let Some(intensity_entries) = &intensity {
            if intensity_entries.len() != total_length {
                return Err(ShapeMisMatch(
                    "intensity vector has a different length than the point vector",
                ));
            }
        }
        if let Some(beam_origin_entries) = &beam_origin {
            if beam_origin_entries.len() != total_length {
                return Err(ShapeMisMatch(
                    "beam_origin vector has a different length than the point vector",
                ));
            }
        }
        if let Some(color_entries) = &color {
            if color_entries.len() != total_length {
                return Err(ShapeMisMatch(
                    "color vector has a different length than the point vector",
                ));
            }
        }

        Ok(Self {
            point,
            id,
            frame_id,
            timestamp,
            intensity,
            beam_origin,
            color,
        })
    }

    pub fn is_empty(&self) -> bool {
        self.point.is_empty()
    }

    pub fn len(&self) -> usize {
        self.point.len()
    }

    pub fn get_as_data_frame(&self) -> DataFrame {
        let mut columns = self.get_xyz_series();

        if let Some(id) = &self.id {
            let id_series = Series::new(PointDataColumnType::Id.as_str(), id);
            columns.push(id_series);
        }

        if let Some(frame_id) = &self.frame_id {
            let frame_id_series = Series::new(PointDataColumnType::FrameId.as_str(), frame_id);
            let frame_id_series = frame_id_series
                .cast(&DataType::Categorical(None, Default::default()))
                .unwrap();
            columns.push(frame_id_series);
        }

        if let Some(timestamp_entries) = &self.timestamp {
            let timestamp_seconds_series = Series::new(
                PointDataColumnType::TimestampSeconds.as_str(),
                timestamp_entries
                    .iter()
                    .map(|t| t.timestamp())
                    .collect::<Vec<i64>>(),
            );
            columns.push(timestamp_seconds_series);

            let timestamp_nanoseconds_series = Series::new(
                PointDataColumnType::TimestampNanoSeconds.as_str(),
                timestamp_entries
                    .iter()
                    .map(|t| t.nanosecond())
                    .collect::<Vec<u32>>(),
            );
            columns.push(timestamp_nanoseconds_series);
        }

        if let Some(intensity) = &self.intensity {
            let intensity_series = Series::new(PointDataColumnType::Intensity.as_str(), intensity);
            columns.push(intensity_series);
        }

        if self.beam_origin.is_some() {
            columns.append(&mut self.get_beam_origin_xyz_series().unwrap());
        }

        if self.color.is_some() {
            columns.append(&mut self.get_color_series().unwrap());
        }

        DataFrame::new(columns).unwrap()
    }

    pub fn get_xyz_series(&self) -> Vec<Series> {
        vec![
            Series::new(
                PointDataColumnType::X.as_str(),
                self.point.iter().map(|p| p.x).collect::<Vec<f64>>(),
            ),
            Series::new(
                PointDataColumnType::Y.as_str(),
                self.point.iter().map(|p| p.y).collect::<Vec<f64>>(),
            ),
            Series::new(
                PointDataColumnType::Z.as_str(),
                self.point.iter().map(|p| p.z).collect::<Vec<f64>>(),
            ),
        ]
    }

    pub fn get_beam_origin_xyz_series(&self) -> Option<Vec<Series>> {
        let beam_origin = self.beam_origin.as_ref()?;

        Some(vec![
            Series::new(
                PointDataColumnType::BeamOriginX.as_str(),
                beam_origin.iter().map(|p| p.x).collect::<Vec<f64>>(),
            ),
            Series::new(
                PointDataColumnType::BeamOriginY.as_str(),
                beam_origin.iter().map(|p| p.y).collect::<Vec<f64>>(),
            ),
            Series::new(
                PointDataColumnType::BeamOriginZ.as_str(),
                beam_origin.iter().map(|p| p.z).collect::<Vec<f64>>(),
            ),
        ])
    }

    pub fn get_color_series(&self) -> Option<Vec<Series>> {
        let color = self.color.as_ref()?;

        Some(vec![
            Series::new(
                PointDataColumnType::ColorRed.as_str(),
                color.iter().map(|c| c.red).collect::<Vec<u16>>(),
            ),
            Series::new(
                PointDataColumnType::ColorGreen.as_str(),
                color.iter().map(|c| c.green).collect::<Vec<u16>>(),
            ),
            Series::new(
                PointDataColumnType::ColorBlue.as_str(),
                color.iter().map(|c| c.blue).collect::<Vec<u16>>(),
            ),
        ])
    }
}