pointrain_core/pc/
xyz_normal.rs

1#[cfg(feature = "rerun")]
2use rerun::{EntityPath, MsgSender, MsgSenderError};
3
4use crate::{
5    traits::{PointCloud, PointCloudWithNormal},
6    types::{Float, Normal, Position},
7};
8
9#[derive(Debug, Default, Clone, Copy, PartialEq)]
10pub struct PointXYZNormal {
11    pub pos: Position,
12    pub normal: Normal,
13    pub curvature: Float,
14}
15
16#[derive(Debug, Default, Clone)]
17pub struct PointCloudXYZNormal {
18    positions: Vec<Position>,
19    normals: Vec<Normal>,
20    curvatures: Vec<Float>,
21}
22
23impl PointCloudXYZNormal {
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    #[cfg(feature = "rerun")]
29    pub fn rerun_msg_sender(
30        &self,
31        label: impl Into<EntityPath>,
32        scale: Option<f32>,
33    ) -> Result<MsgSender, MsgSenderError> {
34        use rerun::components::{Arrow3D, Vec3D};
35
36        let scale = scale.unwrap_or(0.005);
37
38        let arrows: Vec<_> = self
39            .positions
40            .iter()
41            .zip(self.normals.iter())
42            .map(|(p, n)| Arrow3D {
43                origin: Vec3D::new(p.x, p.y, p.z),
44                vector: Vec3D::new(n.x * scale, n.y * scale, n.z * scale),
45            })
46            .collect();
47
48        MsgSender::new(label.into()).with_component(&arrows)
49    }
50}
51
52impl PointCloud for PointCloudXYZNormal {
53    type Point = PointXYZNormal;
54
55    fn with_capacity(capacity: usize) -> Self {
56        Self {
57            positions: Vec::with_capacity(capacity),
58            normals: Vec::with_capacity(capacity),
59            curvatures: Vec::with_capacity(capacity),
60        }
61    }
62
63    fn positions(&self) -> &[Position] {
64        &self.positions
65    }
66
67    fn add_point(&mut self, p: Self::Point) -> &mut Self {
68        self.positions.push(p.pos);
69        self.normals.push(p.normal);
70        self.curvatures.push(p.curvature);
71        self
72    }
73}
74
75impl PointCloudWithNormal for PointCloudXYZNormal {
76    fn normals(&self) -> &[Normal] {
77        &self.normals
78    }
79
80    fn curvatures(&self) -> &[Float] {
81        &self.curvatures
82    }
83}