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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::f32::consts::{self, FRAC_1_SQRT_2};
use super::generators::{IndexedPolygon, SharedVertex};
use super::{MapVertex, Triangle, Vertex};
const TWO_PI: f32 = consts::PI * 2.;
#[derive(Debug)]
enum VertexSection {
Tip(usize),
TopRadius(usize),
BottomRadius(usize),
BottomCenter,
}
pub struct Cone {
u: usize,
sub_u: usize,
}
impl Cone {
pub fn new(u: usize) -> Self {
assert!(u >= 2);
Cone { u: 0, sub_u: u }
}
fn vertex(&self, sec: VertexSection) -> Vertex {
let divisions = TWO_PI / self.sub_u as f32;
match sec {
VertexSection::Tip(i) => {
let pos = divisions * i as f32 + divisions / 2.;
Vertex {
pos: [0., 0., 1.].into(),
normal: [
pos.cos() * FRAC_1_SQRT_2,
pos.sin() * FRAC_1_SQRT_2,
-FRAC_1_SQRT_2,
].into(),
}
}
VertexSection::TopRadius(i) => {
let pos = divisions * i as f32;
Vertex {
pos: [pos.cos(), pos.sin(), -1.].into(),
normal: [
pos.cos() * FRAC_1_SQRT_2,
pos.sin() * FRAC_1_SQRT_2,
-FRAC_1_SQRT_2,
].into(),
}
}
VertexSection::BottomRadius(i) => {
let pos = divisions * i as f32;
Vertex {
pos: [pos.cos(), pos.sin(), -1.].into(),
normal: [0., 0., -1.].into(),
}
}
VertexSection::BottomCenter => Vertex {
pos: [0., 0., -1.].into(),
normal: [0., 0., -1.].into(),
},
}
}
fn index(&self, sec: VertexSection) -> usize {
match sec {
VertexSection::Tip(i) => i,
VertexSection::TopRadius(i) => i + self.sub_u,
VertexSection::BottomRadius(i) => i + self.sub_u * 2,
VertexSection::BottomCenter => self.sub_u * 3,
}
}
fn rev_index(&self, idx: usize) -> VertexSection {
if idx < self.sub_u {
VertexSection::Tip(idx)
} else if idx < self.sub_u * 2 {
VertexSection::TopRadius(idx - self.sub_u)
} else if idx < self.sub_u * 3 {
VertexSection::BottomRadius(idx - self.sub_u * 2)
} else {
VertexSection::BottomCenter
}
}
}
impl Iterator for Cone {
type Item = Triangle<Vertex>;
fn next(&mut self) -> Option<Self::Item> {
if self.u < self.sub_u * 2 {
let idx = self.u;
self.u += 1;
Some(
self.indexed_polygon(idx)
.map_vertex(|i| self.shared_vertex(i)),
)
} else {
None
}
}
}
impl SharedVertex<Vertex> for Cone {
fn shared_vertex(&self, idx: usize) -> Vertex {
self.vertex(self.rev_index(idx))
}
fn shared_vertex_count(&self) -> usize {
self.sub_u * 3 + 1
}
}
impl IndexedPolygon<Triangle<usize>> for Cone {
fn indexed_polygon(&self, idx: usize) -> Triangle<usize> {
if idx < self.sub_u {
let next = if idx != self.sub_u - 1 { idx + 1 } else { 0 };
Triangle::new(
self.index(VertexSection::Tip(idx)),
self.index(VertexSection::TopRadius(idx)),
self.index(VertexSection::TopRadius(next)),
)
} else {
let idx = idx - self.sub_u;
let next = if idx != self.sub_u - 1 { idx + 1 } else { 0 };
Triangle::new(
self.index(VertexSection::BottomCenter),
self.index(VertexSection::BottomRadius(next)),
self.index(VertexSection::BottomRadius(idx)),
)
}
}
fn indexed_polygon_count(&self) -> usize {
self.sub_u * 2
}
}