nphysics3d/volumetric/
volumetric_cylinder.rs

1use num::Zero;
2
3use crate::math::{AngularInertia, Point, DIM};
4use na::{self, RealField};
5
6/// The volume of a cylinder.
7#[inline]
8pub fn cylinder_volume<N: RealField + Copy>(half_height: N, radius: N) -> N {
9    if DIM == 2 {
10        half_height * radius * na::convert(4.0f64)
11    } else {
12        half_height * radius * radius * N::pi() * na::convert(2.0f64)
13    }
14}
15
16/// The area of a cylinder.
17#[inline]
18pub fn cylinder_area<N: RealField + Copy>(half_height: N, radius: N) -> N {
19    if DIM == 2 {
20        (half_height + radius) * na::convert(2.0f64)
21    } else {
22        let _pi = N::pi();
23        let basis = radius * radius * _pi;
24        let side = _pi * radius * (half_height + half_height) * na::convert(2.0f64);
25
26        side + basis + basis
27    }
28}
29
30/// The center of mass of a cylinder.
31#[inline]
32pub fn cylinder_center_of_mass<N: RealField + Copy>() -> Point<N> {
33    Point::origin()
34}
35
36/// The unit angular inertia of a cylinder.
37#[inline]
38pub fn cylinder_unit_angular_inertia<N: RealField + Copy>(half_height: N, radius: N) -> AngularInertia<N> {
39    if DIM == 2 {
40        // Same a the rectangle.
41        let _2: N = na::convert(2.0f64);
42        let _i12: N = na::convert(1.0f64 / 12.0);
43        let w = _i12 * _2 * _2;
44        let ix = w * half_height * half_height;
45        let iy = w * radius * radius;
46
47        let mut res = AngularInertia::zero();
48
49        res[(0, 0)] = ix + iy;
50
51        res
52    } else {
53        let sq_radius = radius * radius;
54        let sq_height = half_height * half_height * na::convert(4.0f64);
55        let off_principal = (sq_radius * na::convert(3.0f64) + sq_height) / na::convert(12.0f64);
56
57        let mut res = AngularInertia::zero();
58
59        res[(0, 0)] = off_principal.clone();
60        res[(1, 1)] = sq_radius / na::convert(2.0f64);
61        res[(2, 2)] = off_principal;
62
63        res
64    }
65}
66
67//impl<N: RealField + Copy> Volumetric<N> for Cylinder<N> {
68//    fn area(&self) -> N {
69//        cylinder_area(self.half_height(), self.radius())
70//    }
71//
72//    fn volume(&self) -> N {
73//        cylinder_volume(self.half_height(), self.radius())
74//    }
75//
76//    fn center_of_mass(&self) -> Point<N> {
77//        cylinder_center_of_mass()
78//    }
79//
80//    fn unit_angular_inertia(&self) -> AngularInertia<N> {
81//        cylinder_unit_angular_inertia(self.half_height(), self.radius())
82//    }
83//}