parry3d/mass_properties/
mass_properties_cuboid.rs1use crate::mass_properties::MassProperties;
2use crate::math::{Point, PrincipalAngularInertia, Real, Vector};
3
4impl MassProperties {
5 pub(crate) fn cuboid_volume_unit_inertia(
6 half_extents: Vector<Real>,
7 ) -> (Real, PrincipalAngularInertia<Real>) {
8 #[cfg(feature = "dim2")]
9 {
10 let volume = half_extents.x * half_extents.y * 4.0;
11 let ix = (half_extents.x * half_extents.x) / 3.0;
12 let iy = (half_extents.y * half_extents.y) / 3.0;
13
14 (volume, ix + iy)
15 }
16
17 #[cfg(feature = "dim3")]
18 {
19 let volume = half_extents.x * half_extents.y * half_extents.z * 8.0;
20 let ix = (half_extents.x * half_extents.x) / 3.0;
21 let iy = (half_extents.y * half_extents.y) / 3.0;
22 let iz = (half_extents.z * half_extents.z) / 3.0;
23
24 (volume, Vector::new(iy + iz, ix + iz, ix + iy))
25 }
26 }
27
28 pub fn from_cuboid(density: Real, half_extents: Vector<Real>) -> Self {
30 let (vol, unit_i) = Self::cuboid_volume_unit_inertia(half_extents);
31 let mass = vol * density;
32 Self::new(Point::origin(), mass, unit_i * mass)
33 }
34}