parry3d/mass_properties/
mass_properties_cylinder.rs

1use crate::mass_properties::MassProperties;
2use crate::math::{PrincipalAngularInertia, Real, Vector};
3#[cfg(feature = "dim3")]
4use {
5    crate::math::{Point, Rotation},
6    na::RealField,
7};
8
9impl MassProperties {
10    pub(crate) fn cylinder_y_volume_unit_inertia(
11        half_height: Real,
12        radius: Real,
13    ) -> (Real, PrincipalAngularInertia<Real>) {
14        #[cfg(feature = "dim2")]
15        {
16            Self::cuboid_volume_unit_inertia(Vector::new(radius, half_height))
17        }
18
19        #[cfg(feature = "dim3")]
20        {
21            let volume = half_height * radius * radius * Real::pi() * 2.0;
22            let sq_radius = radius * radius;
23            let sq_height = half_height * half_height * 4.0;
24            let off_principal = (sq_radius * 3.0 + sq_height) / 12.0;
25
26            let inertia = Vector::new(off_principal, sq_radius / 2.0, off_principal);
27            (volume, inertia)
28        }
29    }
30
31    /// Computes the mass properties of a cylinder.
32    #[cfg(feature = "dim3")]
33    pub fn from_cylinder(density: Real, half_height: Real, radius: Real) -> Self {
34        let (cyl_vol, cyl_unit_i) = Self::cylinder_y_volume_unit_inertia(half_height, radius);
35        let cyl_mass = cyl_vol * density;
36
37        Self::with_principal_inertia_frame(
38            Point::origin(),
39            cyl_mass,
40            cyl_unit_i * cyl_mass,
41            Rotation::identity(),
42        )
43    }
44}