threecrate_core/
point_cloud.rs1use crate::point::*;
4use crate::transform::Transform3D;
5use serde::{Deserialize, Serialize};
7use std::ops::{Index, IndexMut};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct PointCloud<T> {
12 pub points: Vec<T>,
13}
14
15pub type PointCloud3f = PointCloud<Point3f>;
17
18pub type ColoredPointCloud3f = PointCloud<ColoredPoint3f>;
20
21pub type NormalPointCloud3f = PointCloud<NormalPoint3f>;
23
24pub type ColoredNormalPointCloud3f = PointCloud<ColoredNormalPoint3f>;
26
27impl<T> PointCloud<T> {
28 pub fn new() -> Self {
30 Self { points: Vec::new() }
31 }
32
33 pub fn with_capacity(capacity: usize) -> Self {
35 Self {
36 points: Vec::with_capacity(capacity),
37 }
38 }
39
40 pub fn from_points(points: Vec<T>) -> Self {
42 Self { points }
43 }
44
45 pub fn len(&self) -> usize {
47 self.points.len()
48 }
49
50 pub fn is_empty(&self) -> bool {
52 self.points.is_empty()
53 }
54
55 pub fn push(&mut self, point: T) {
57 self.points.push(point);
58 }
59
60 pub fn iter(&self) -> std::slice::Iter<'_, T> {
62 self.points.iter()
63 }
64
65 pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> {
67 self.points.iter_mut()
68 }
69
70 pub fn clear(&mut self) {
72 self.points.clear();
73 }
74
75 pub fn reserve(&mut self, additional: usize) {
77 self.points.reserve(additional);
78 }
79}
80
81impl<T> Default for PointCloud<T> {
82 fn default() -> Self {
83 Self::new()
84 }
85}
86
87impl<T> Index<usize> for PointCloud<T> {
88 type Output = T;
89
90 fn index(&self, index: usize) -> &Self::Output {
91 &self.points[index]
92 }
93}
94
95impl<T> IndexMut<usize> for PointCloud<T> {
96 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
97 &mut self.points[index]
98 }
99}
100
101impl<T> IntoIterator for PointCloud<T> {
102 type Item = T;
103 type IntoIter = std::vec::IntoIter<T>;
104
105 fn into_iter(self) -> Self::IntoIter {
106 self.points.into_iter()
107 }
108}
109
110impl<'a, T> IntoIterator for &'a PointCloud<T> {
111 type Item = &'a T;
112 type IntoIter = std::slice::Iter<'a, T>;
113
114 fn into_iter(self) -> Self::IntoIter {
115 self.points.iter()
116 }
117}
118
119impl<'a, T> IntoIterator for &'a mut PointCloud<T> {
120 type Item = &'a mut T;
121 type IntoIter = std::slice::IterMut<'a, T>;
122
123 fn into_iter(self) -> Self::IntoIter {
124 self.points.iter_mut()
125 }
126}
127
128impl<T> Extend<T> for PointCloud<T> {
129 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
130 self.points.extend(iter);
131 }
132}
133
134impl<T> FromIterator<T> for PointCloud<T> {
135 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
136 Self {
137 points: Vec::from_iter(iter),
138 }
139 }
140}
141
142impl PointCloud<Point3f> {
143 pub fn transform(&mut self, transform: &Transform3D) {
145 for point in &mut self.points {
146 *point = transform.transform_point(point);
147 }
148 }
149}