threecrate_core/
traits.rs

1//! Core traits for threecrate
2
3use crate::{point::*, point_cloud::*, mesh::*, transform::Transform3D};
4
5/// Trait for nearest neighbor search functionality
6pub trait NearestNeighborSearch {
7    /// Find the k nearest neighbors to a query point
8    fn find_k_nearest(&self, query: &Point3f, k: usize) -> Vec<(usize, f32)>;
9    
10    /// Find all neighbors within a given radius
11    fn find_radius_neighbors(&self, query: &Point3f, radius: f32) -> Vec<(usize, f32)>;
12}
13
14/// Trait for drawable/renderable objects
15pub trait Drawable {
16    /// Get the bounding box of the object
17    fn bounding_box(&self) -> (Point3f, Point3f);
18    
19    /// Get the center point of the object
20    fn center(&self) -> Point3f;
21}
22
23/// Trait for objects that can be transformed
24pub trait Transformable {
25    /// Apply a transformation to the object
26    fn transform(&mut self, transform: &Transform3D);
27}
28
29impl<T> Drawable for PointCloud<T> 
30where 
31    T: Clone + Copy,
32    Point3f: From<T>,
33{
34    fn bounding_box(&self) -> (Point3f, Point3f) {
35        if self.is_empty() {
36            return (Point3f::origin(), Point3f::origin());
37        }
38        
39        let first_point = Point3f::from(self.points[0]);
40        let mut min = first_point;
41        let mut max = first_point;
42        
43        for point in &self.points {
44            let p = Point3f::from(*point);
45            min.x = min.x.min(p.x);
46            min.y = min.y.min(p.y);
47            min.z = min.z.min(p.z);
48            
49            max.x = max.x.max(p.x);
50            max.y = max.y.max(p.y);
51            max.z = max.z.max(p.z);
52        }
53        
54        (min, max)
55    }
56    
57    fn center(&self) -> Point3f {
58        let (min, max) = self.bounding_box();
59        Point3f::new(
60            (min.x + max.x) / 2.0,
61            (min.y + max.y) / 2.0,
62            (min.z + max.z) / 2.0,
63        )
64    }
65}
66
67impl Drawable for TriangleMesh {
68    fn bounding_box(&self) -> (Point3f, Point3f) {
69        if self.vertices.is_empty() {
70            return (Point3f::origin(), Point3f::origin());
71        }
72        
73        let mut min = self.vertices[0];
74        let mut max = self.vertices[0];
75        
76        for vertex in &self.vertices {
77            min.x = min.x.min(vertex.x);
78            min.y = min.y.min(vertex.y);
79            min.z = min.z.min(vertex.z);
80            
81            max.x = max.x.max(vertex.x);
82            max.y = max.y.max(vertex.y);
83            max.z = max.z.max(vertex.z);
84        }
85        
86        (min, max)
87    }
88    
89    fn center(&self) -> Point3f {
90        let (min, max) = self.bounding_box();
91        Point3f::new(
92            (min.x + max.x) / 2.0,
93            (min.y + max.y) / 2.0,
94            (min.z + max.z) / 2.0,
95        )
96    }
97}