Skip to main content

oxiphysics_geometry/compound/
compound_traits.rs

1//! # Compound - Trait Implementations
2//!
3//! This module contains trait implementations for `Compound`.
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::Compound;
18
19impl Shape for Compound {
20    fn bounding_box(&self) -> Aabb {
21        if self.children.is_empty() {
22            return Aabb::new(Vec3::zeros(), Vec3::zeros());
23        }
24        let mut result: Option<Aabb> = None;
25        for (transform, shape) in &self.children {
26            let local_aabb = shape.bounding_box();
27            let corners = [
28                Vec3::new(local_aabb.min.x, local_aabb.min.y, local_aabb.min.z),
29                Vec3::new(local_aabb.max.x, local_aabb.min.y, local_aabb.min.z),
30                Vec3::new(local_aabb.min.x, local_aabb.max.y, local_aabb.min.z),
31                Vec3::new(local_aabb.max.x, local_aabb.max.y, local_aabb.min.z),
32                Vec3::new(local_aabb.min.x, local_aabb.min.y, local_aabb.max.z),
33                Vec3::new(local_aabb.max.x, local_aabb.min.y, local_aabb.max.z),
34                Vec3::new(local_aabb.min.x, local_aabb.max.y, local_aabb.max.z),
35                Vec3::new(local_aabb.max.x, local_aabb.max.y, local_aabb.max.z),
36            ];
37            let mut min = transform.transform_point(&corners[0]);
38            let mut max = min;
39            for corner in &corners[1..] {
40                let p = transform.transform_point(corner);
41                min = min.inf(&p);
42                max = max.sup(&p);
43            }
44            let child_aabb = Aabb::new(min, max);
45            result = Some(match result {
46                Some(r) => r.merge(&child_aabb),
47                None => child_aabb,
48            });
49        }
50        result.unwrap_or_else(|| Aabb::new(Vec3::zeros(), Vec3::zeros()))
51    }
52    fn support_point(&self, direction: &Vec3) -> Vec3 {
53        let mut best_dot = Real::NEG_INFINITY;
54        let mut best_point = Vec3::zeros();
55        for (transform, shape) in &self.children {
56            let local_dir = transform.inverse().transform_vector(direction);
57            let local_support = shape.support_point(&local_dir);
58            let world_support = transform.transform_point(&local_support);
59            let d = world_support.dot(direction);
60            if d > best_dot {
61                best_dot = d;
62                best_point = world_support;
63            }
64        }
65        best_point
66    }
67    fn volume(&self) -> Real {
68        self.children.iter().map(|(_, s)| s.volume()).sum()
69    }
70    fn center_of_mass(&self) -> Vec3 {
71        let total_vol: Real = self.children.iter().map(|(_, s)| s.volume()).sum();
72        if total_vol < 1e-12 {
73            return Vec3::zeros();
74        }
75        let weighted: Vec3 = self
76            .children
77            .iter()
78            .map(|(t, s)| {
79                let local_com = s.center_of_mass();
80                let world_com = t.transform_point(&local_com);
81                world_com * s.volume()
82            })
83            .sum();
84        weighted / total_vol
85    }
86    fn inertia_tensor(&self, mass: Real) -> Mat3 {
87        let total_vol: Real = self.children.iter().map(|(_, s)| s.volume()).sum();
88        if total_vol < 1e-12 {
89            return Mat3::zeros();
90        }
91        let com = self.center_of_mass();
92        let mut total = Mat3::zeros();
93        for (transform, shape) in &self.children {
94            let child_mass = mass * shape.volume() / total_vol;
95            let child_inertia = shape.inertia_tensor(child_mass);
96            let child_com = transform.transform_point(&shape.center_of_mass());
97            let r = child_com - com;
98            let r2 = r.dot(&r);
99            let parallel = Mat3::identity() * r2 - r * r.transpose();
100            total += child_inertia + parallel * child_mass;
101        }
102        total
103    }
104    fn ray_cast(&self, ray_origin: &Vec3, ray_direction: &Vec3, max_toi: Real) -> Option<RayHit> {
105        let mut best: Option<RayHit> = None;
106        for (transform, shape) in &self.children {
107            let inv = transform.inverse();
108            let local_origin = inv.transform_point(ray_origin);
109            let local_dir = inv.transform_vector(ray_direction);
110            if let Some(hit) = shape.ray_cast(&local_origin, &local_dir, max_toi)
111                && best.as_ref().is_none_or(|b| hit.toi < b.toi)
112            {
113                let world_point = transform.transform_point(&hit.point);
114                let world_normal = transform.transform_vector(&hit.normal).normalize();
115                best = Some(RayHit {
116                    point: world_point,
117                    normal: world_normal,
118                    toi: hit.toi,
119                });
120            }
121        }
122        best
123    }
124}