parry3d/transformation/to_trimesh/
ball_to_trimesh.rs

1use crate::math::{Point, Real, Vector, DIM};
2use crate::shape::Ball;
3use crate::transformation::utils;
4use alloc::vec::Vec;
5use na::{self, ComplexField, Point3, RealField};
6
7impl Ball {
8    /// Discretize the boundary of this ball as a triangle-mesh.
9    pub fn to_trimesh(
10        &self,
11        ntheta_subdiv: u32,
12        nphi_subdiv: u32,
13    ) -> (Vec<Point3<Real>>, Vec<[u32; 3]>) {
14        let diameter = self.radius * 2.0;
15        let (vtx, idx) = unit_sphere(ntheta_subdiv, nphi_subdiv);
16        (utils::scaled(vtx, Vector::repeat(diameter)), idx)
17    }
18}
19
20fn unit_sphere(ntheta_subdiv: u32, nphi_subdiv: u32) -> (Vec<Point3<Real>>, Vec<[u32; 3]>) {
21    let dtheta = Real::two_pi() / (ntheta_subdiv as Real);
22    let dphi = Real::pi() / (nphi_subdiv as Real);
23
24    let mut coords = Vec::new();
25    let mut curr_phi: Real = -Real::frac_pi_2() + dphi;
26
27    coords.push(Point::new(0.0, -1.0, 0.0));
28
29    for _ in 1..nphi_subdiv {
30        utils::push_circle(
31            ComplexField::cos(curr_phi),
32            ntheta_subdiv,
33            dtheta,
34            ComplexField::sin(curr_phi),
35            &mut coords,
36        );
37        curr_phi += dphi;
38    }
39
40    coords.push(Point::new(0.0, 1.0, 0.0));
41
42    let mut idx = Vec::new();
43
44    utils::push_degenerate_top_ring_indices(1, 0, ntheta_subdiv, &mut idx);
45    utils::reverse_clockwising(&mut idx);
46
47    for i in 0..nphi_subdiv - 2 {
48        utils::push_ring_indices(
49            1 + i * ntheta_subdiv,
50            1 + (i + 1) * ntheta_subdiv,
51            ntheta_subdiv,
52            &mut idx,
53        );
54    }
55
56    utils::push_degenerate_top_ring_indices(
57        coords.len() as u32 - 1 - ntheta_subdiv,
58        coords.len() as u32 - 1,
59        ntheta_subdiv,
60        &mut idx,
61    );
62
63    (utils::scaled(coords, Vector::repeat(0.5)), idx)
64}
65
66/// Creates an hemisphere with a diameter of 1.
67pub(crate) fn unit_hemisphere(
68    ntheta_subdiv: u32,
69    nphi_subdiv: u32,
70) -> (Vec<Point<Real>>, Vec<[u32; DIM]>) {
71    let two_pi = Real::two_pi();
72    let pi_two = Real::frac_pi_2();
73    let dtheta = two_pi / (ntheta_subdiv as Real);
74    let dphi = pi_two / (nphi_subdiv as Real);
75
76    let mut coords = Vec::new();
77    let mut curr_phi: Real = 0.0;
78
79    for _ in 0..nphi_subdiv {
80        utils::push_circle(
81            ComplexField::cos(curr_phi),
82            ntheta_subdiv,
83            dtheta,
84            ComplexField::sin(curr_phi),
85            &mut coords,
86        );
87        curr_phi += dphi;
88    }
89
90    coords.push(Point::new(0.0, 1.0, 0.0));
91
92    let mut idx = Vec::new();
93
94    for i in 0..nphi_subdiv - 1 {
95        utils::push_ring_indices(
96            i * ntheta_subdiv,
97            (i + 1) * ntheta_subdiv,
98            ntheta_subdiv,
99            &mut idx,
100        );
101    }
102
103    utils::push_degenerate_top_ring_indices(
104        (nphi_subdiv - 1) * ntheta_subdiv,
105        coords.len() as u32 - 1,
106        ntheta_subdiv,
107        &mut idx,
108    );
109
110    (utils::scaled(coords, Vector::repeat(0.5)), idx)
111}