ncollide3d/procedural/
cone.rs

1use super::utils;
2use super::{IndexBuffer, TriMesh};
3use na;
4use na::{Point3, Vector3};
5use simba::scalar::RealField;
6
7/// Generates a cone with a given height and diameter.
8pub fn cone<N: RealField + Copy>(diameter: N, height: N, nsubdiv: u32) -> TriMesh<N> {
9    let mut cone = unit_cone(nsubdiv);
10
11    cone.scale_by(&Vector3::new(diameter, height, diameter));
12
13    cone
14}
15
16/// Generates a cone with unit height and diameter.
17pub fn unit_cone<N: RealField + Copy>(nsubdiv: u32) -> TriMesh<N> {
18    let two_pi = N::two_pi();
19    let dtheta = two_pi / na::convert(nsubdiv as f64);
20    let mut coords = Vec::new();
21    let mut indices = Vec::new();
22    let mut normals: Vec<Vector3<N>>;
23
24    utils::push_circle(
25        na::convert(0.5),
26        nsubdiv,
27        dtheta,
28        na::convert(-0.5),
29        &mut coords,
30    );
31
32    normals = coords.iter().map(|p| p.coords).collect();
33
34    coords.push(Point3::new(na::zero(), na::convert(0.5), na::zero()));
35
36    utils::push_degenerate_top_ring_indices(0, coords.len() as u32 - 1, nsubdiv, &mut indices);
37    utils::push_filled_circle_indices(0, nsubdiv, &mut indices);
38
39    /*
40     * Normals.
41     */
42    let mut indices = utils::split_index_buffer(&indices[..]);
43
44    // Adjust the normals:
45    let shift: N = na::convert(0.05 / 0.475);
46    let div = (shift * shift + na::convert(0.25)).sqrt();
47    for n in normals.iter_mut() {
48        n.y = n.y + shift;
49        // FIXME: n / div does not work?
50        n.x = n.x / div;
51        n.y = n.y / div;
52        n.z = n.z / div;
53    }
54
55    // Normal for the basis.
56    normals.push(Vector3::new(na::zero(), -na::one::<N>(), na::zero()));
57
58    let ilen = indices.len();
59    let nlen = normals.len() as u32;
60    for (id, i) in indices[..ilen - (nsubdiv as usize - 2)]
61        .iter_mut()
62        .enumerate()
63    {
64        i.y.y = id as u32;
65    }
66
67    for i in indices[ilen - (nsubdiv as usize - 2)..].iter_mut() {
68        i.x.y = nlen - 1;
69        i.y.y = nlen - 1;
70        i.z.y = nlen - 1;
71    }
72
73    // Normal for the body.
74
75    TriMesh::new(
76        coords,
77        Some(normals),
78        None,
79        Some(IndexBuffer::Split(indices)),
80    )
81
82    // XXX: uvs
83}