Skip to main content

oxiphysics_geometry/convex_hull/
convexhull_traits.rs

1//! # ConvexHull - Trait Implementations
2//!
3//! This module contains trait implementations for `ConvexHull`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Shape`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use crate::shape::{RayHit, Shape};
12use oxiphysics_core::Aabb;
13use oxiphysics_core::math::{Mat3, Real, Vec3};
14
15use super::types::ConvexHull;
16
17impl Shape for ConvexHull {
18    fn bounding_box(&self) -> Aabb {
19        if self.vertices.is_empty() {
20            return Aabb::new(Vec3::zeros(), Vec3::zeros());
21        }
22        let mut min = self.vertices[0];
23        let mut max = self.vertices[0];
24        for v in &self.vertices[1..] {
25            min = min.inf(v);
26            max = max.sup(v);
27        }
28        Aabb::new(min, max)
29    }
30    fn support_point(&self, direction: &Vec3) -> Vec3 {
31        self.vertices
32            .iter()
33            .max_by(|a, b| {
34                a.dot(direction)
35                    .partial_cmp(&b.dot(direction))
36                    .unwrap_or(std::cmp::Ordering::Equal)
37            })
38            .copied()
39            .unwrap_or_else(Vec3::zeros)
40    }
41    fn volume(&self) -> Real {
42        self.tetra_decomposition().0
43    }
44    fn center_of_mass(&self) -> Vec3 {
45        self.tetra_decomposition().1
46    }
47    fn inertia_tensor(&self, mass: Real) -> Mat3 {
48        let bb = self.bounding_box();
49        let size = bb.max - bb.min;
50        let k = mass / 12.0;
51        Mat3::new(
52            k * (size.y * size.y + size.z * size.z),
53            0.0,
54            0.0,
55            0.0,
56            k * (size.x * size.x + size.z * size.z),
57            0.0,
58            0.0,
59            0.0,
60            k * (size.x * size.x + size.y * size.y),
61        )
62    }
63    fn ray_cast(
64        &self,
65        _ray_origin: &Vec3,
66        _ray_direction: &Vec3,
67        _max_toi: Real,
68    ) -> Option<RayHit> {
69        None
70    }
71}