three_d_asset/geometry/
point_cloud.rs

1use super::Positions;
2use crate::prelude::*;
3
4///
5/// Represents a set of points in 3D space, usually created with a scanner.
6///
7#[derive(Default, Clone)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct PointCloud {
10    /// The positions of the points.
11    pub positions: Positions,
12    /// The colors of the points.
13    pub colors: Option<Vec<Srgba>>,
14}
15
16impl std::fmt::Debug for PointCloud {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        let mut d = f.debug_struct("PointCloud");
19        d.field("positions", &self.positions.len());
20        d.field("colors", &self.colors.as_ref().map(|v| v.len()));
21        d.finish()
22    }
23}
24
25impl PointCloud {
26    ///
27    /// Returns a point cloud whose points lie on the corners of an axis aligned unconnected cube with positions in the range `[-1..1]` in all axes.
28    ///
29    pub fn cube() -> Self {
30        let positions = vec![
31            vec3(-1.0, -1.0, -1.0),
32            vec3(-1.0, -1.0, 1.0),
33            vec3(-1.0, 1.0, -1.0),
34            vec3(-1.0, 1.0, 1.0),
35            vec3(1.0, -1.0, -1.0),
36            vec3(1.0, -1.0, 1.0),
37            vec3(1.0, 1.0, -1.0),
38            vec3(1.0, 1.0, 1.0),
39        ];
40        Self {
41            positions: Positions::F32(positions),
42            ..Default::default()
43        }
44    }
45
46    ///
47    /// Computes the [AxisAlignedBoundingBox] for this point cloud.
48    ///
49    pub fn compute_aabb(&self) -> AxisAlignedBoundingBox {
50        self.positions.compute_aabb()
51    }
52}