pointrain_core/pc/
xyz_rgb.rs

1#[cfg(feature = "rerun")]
2use rerun::{EntityPath, MsgSender, MsgSenderError};
3
4use crate::{
5    traits::{PointCloud, PointCloudWithColor},
6    types::{Position, Rgb},
7};
8
9#[derive(Debug, Default, Clone, Copy, PartialEq)]
10pub struct PointXYZRgb {
11    pub pos: Position,
12    pub color: Rgb,
13}
14
15#[derive(Debug, Default, Clone)]
16pub struct PointCloudXYZRgb {
17    positions: Vec<Position>,
18    colors: Vec<Rgb>,
19}
20
21impl PointCloudXYZRgb {
22    pub fn new() -> Self {
23        Self::default()
24    }
25
26    #[cfg(feature = "rerun")]
27    pub fn rerun_msg_sender(
28        &self,
29        label: impl Into<EntityPath>,
30    ) -> Result<MsgSender, MsgSenderError> {
31        use rerun::components::{ColorRGBA, Point3D};
32
33        let points: Vec<_> = self
34            .positions
35            .iter()
36            .map(|p| Point3D::new(p.x, p.y, p.z))
37            .collect();
38
39        let colors: Vec<_> = self
40            .colors
41            .iter()
42            .map(|c| ColorRGBA::from_rgb(c.x, c.y, c.z))
43            .collect();
44
45        MsgSender::new(label.into())
46            .with_component(&points)?
47            .with_component(&colors)
48    }
49}
50
51impl PointCloud for PointCloudXYZRgb {
52    type Point = PointXYZRgb;
53
54    fn with_capacity(capacity: usize) -> Self {
55        Self {
56            positions: Vec::with_capacity(capacity),
57            colors: Vec::with_capacity(capacity),
58        }
59    }
60
61    fn positions(&self) -> &[Position] {
62        &self.positions
63    }
64
65    fn add_point(&mut self, p: Self::Point) -> &mut Self {
66        self.positions.push(p.pos);
67        self.colors.push(p.color);
68        self
69    }
70}
71
72impl PointCloudWithColor for PointCloudXYZRgb {
73    fn colors(&self) -> &[Rgb] {
74        &self.colors
75    }
76}