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