1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use na;
use na::{Pnt3, Vec3, BaseFloat};
use super::{TriMesh, IndexBuffer};
use super::utils;
use math::Scalar;
pub fn cone<N>(diameter: N, height: N, nsubdiv: u32) -> TriMesh<Pnt3<N>>
where N: Scalar {
let mut cone = unit_cone(nsubdiv);
cone.scale_by(&Vec3::new(diameter, height, diameter));
cone
}
pub fn unit_cone<N>(nsubdiv: u32) -> TriMesh<Pnt3<N>>
where N: Scalar {
let two_pi: N = BaseFloat::two_pi();
let dtheta = two_pi / na::cast(nsubdiv as f64);
let mut coords = Vec::new();
let mut indices = Vec::new();
let mut normals: Vec<Vec3<N>>;
utils::push_circle(na::cast(0.5), nsubdiv, dtheta, na::cast(-0.5), &mut coords);
normals = coords.iter().map(|p| p.as_vec().clone()).collect();
coords.push(Pnt3::new(na::zero(), na::cast(0.5), na::zero()));
utils::push_degenerate_top_ring_indices(0, coords.len() as u32 - 1, nsubdiv, &mut indices);
utils::push_filled_circle_indices(0, nsubdiv, &mut indices);
let mut indices = utils::split_index_buffer(&indices[..]);
let shift: N = na::cast(0.05 / 0.475);
let div = (shift * shift + na::cast(0.25)).sqrt();
for n in normals.iter_mut() {
n.y = n.y + shift;
n.x = n.x / div;
n.y = n.y / div;
n.z = n.z / div;
}
normals.push(Vec3::new(na::zero(), -na::one::<N>(), na::zero()));
let ilen = indices.len();
let nlen = normals.len() as u32;
for (id, i) in indices[.. ilen - (nsubdiv as usize - 2)].iter_mut().enumerate() {
i.y.y = id as u32;
}
for i in indices[ilen - (nsubdiv as usize - 2) ..].iter_mut() {
i.x.y = nlen - 1;
i.y.y = nlen - 1;
i.z.y = nlen - 1;
}
TriMesh::new(coords, Some(normals), None, Some(IndexBuffer::Split(indices)))
}