use crate::point::*;
use crate::transform::Transform3D;
use serde::{Deserialize, Serialize};
use std::ops::{Index, IndexMut};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PointCloud<T> {
pub points: Vec<T>,
}
pub type PointCloud3f = PointCloud<Point3f>;
pub type ColoredPointCloud3f = PointCloud<ColoredPoint3f>;
pub type NormalPointCloud3f = PointCloud<NormalPoint3f>;
pub type ColoredNormalPointCloud3f = PointCloud<ColoredNormalPoint3f>;
impl<T> PointCloud<T> {
pub fn new() -> Self {
Self {
points: Vec::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
points: Vec::with_capacity(capacity),
}
}
pub fn from_points(points: Vec<T>) -> Self {
Self { points }
}
pub fn len(&self) -> usize {
self.points.len()
}
pub fn is_empty(&self) -> bool {
self.points.is_empty()
}
pub fn push(&mut self, point: T) {
self.points.push(point);
}
pub fn iter(&self) -> std::slice::Iter<T> {
self.points.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<T> {
self.points.iter_mut()
}
pub fn clear(&mut self) {
self.points.clear();
}
pub fn reserve(&mut self, additional: usize) {
self.points.reserve(additional);
}
}
impl<T> Default for PointCloud<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> Index<usize> for PointCloud<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
&self.points[index]
}
}
impl<T> IndexMut<usize> for PointCloud<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.points[index]
}
}
impl<T> IntoIterator for PointCloud<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.points.into_iter()
}
}
impl<'a, T> IntoIterator for &'a PointCloud<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.points.iter()
}
}
impl<'a, T> IntoIterator for &'a mut PointCloud<T> {
type Item = &'a mut T;
type IntoIter = std::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.points.iter_mut()
}
}
impl<T> Extend<T> for PointCloud<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
self.points.extend(iter);
}
}
impl<T> FromIterator<T> for PointCloud<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self {
points: Vec::from_iter(iter),
}
}
}
impl PointCloud<Point3f> {
pub fn transform(&mut self, transform: &Transform3D) {
for point in &mut self.points {
*point = transform.transform_point(point);
}
}
}