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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use std::ops::{Index, IndexMut};
use std::mem;
use std::iter;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use num::Float;
use na;
use na::{Pnt3, Dim, Cross, Orig};
use ncollide_utils::{HashablePartialEq, AsBytes};
use math::{Scalar, Point, Vect};
#[inline]
pub fn push_circle<N: Scalar>(radius: N, nsubdiv: u32, dtheta: N, y: N, out: &mut Vec<Pnt3<N>>) {
let mut curr_theta: N = na::zero();
for _ in 0 .. nsubdiv {
out.push(Pnt3::new(curr_theta.cos() * radius, y.clone(), curr_theta.sin() * radius));
curr_theta = curr_theta + dtheta;
}
}
#[inline]
pub fn push_xy_arc<N, P>(radius: N, nsubdiv: u32, dtheta: N, out: &mut Vec<P>)
where N: Scalar,
P: Dim + Orig + Index<usize, Output = N> + IndexMut<usize, Output = N> {
assert!(na::dim::<P>() >= 2);
let mut curr_theta: N = na::zero();
for _ in 0 .. nsubdiv {
let mut pt = na::orig::<P>();
pt[0] = curr_theta.cos() * radius;
pt[1] = curr_theta.sin() * radius;
out.push(pt);
curr_theta = curr_theta + dtheta;
}
}
#[inline]
pub fn push_ring_indices(base_lower_circle: u32,
base_upper_circle: u32,
nsubdiv: u32,
out: &mut Vec<Pnt3<u32>>) {
push_open_ring_indices(base_lower_circle, base_upper_circle, nsubdiv, out);
push_rectangle_indices(base_upper_circle, base_upper_circle + nsubdiv - 1,
base_lower_circle, base_lower_circle + nsubdiv - 1, out);
}
#[inline]
pub fn push_open_ring_indices(base_lower_circle: u32,
base_upper_circle: u32,
nsubdiv: u32,
out: &mut Vec<Pnt3<u32>>) {
assert!(nsubdiv > 0);
for i in 0 .. nsubdiv - 1 {
let bli = base_lower_circle + i;
let bui = base_upper_circle + i;
push_rectangle_indices(bui + 1, bui,
bli + 1, bli, out);
}
}
#[inline]
pub fn push_degenerate_top_ring_indices(base_circle: u32,
point: u32,
nsubdiv: u32,
out: &mut Vec<Pnt3<u32>>) {
push_degenerate_open_top_ring_indices(base_circle, point, nsubdiv, out);
out.push(Pnt3::new(base_circle + nsubdiv - 1, point, base_circle));
}
#[inline]
pub fn push_degenerate_open_top_ring_indices(base_circle: u32,
point: u32,
nsubdiv: u32,
out: &mut Vec<Pnt3<u32>>) {
assert!(nsubdiv > 0);
for i in 0 .. nsubdiv - 1 {
out.push(Pnt3::new(base_circle + i, point, base_circle + i + 1));
}
}
#[inline]
pub fn push_filled_circle_indices(base_circle: u32, nsubdiv: u32, out: &mut Vec<Pnt3<u32>>) {
for i in base_circle + 1 .. base_circle + nsubdiv - 1 {
out.push(Pnt3::new(base_circle, i, i + 1));
}
}
#[inline]
pub fn push_rectangle_indices<T: Clone>(ul: T, ur: T, dl: T, dr: T, out: &mut Vec<Pnt3<T>>) {
out.push(Pnt3::new(ul.clone(), dl, dr.clone()));
out.push(Pnt3::new(dr , ur, ul));
}
#[inline]
pub fn reverse_clockwising(indices: &mut [Pnt3<u32>]) {
for i in indices.iter_mut() {
mem::swap(&mut i.x, &mut i.y);
}
}
#[inline]
pub fn split_index_buffer(indices: &[Pnt3<u32>]) -> Vec<Pnt3<Pnt3<u32>>> {
let mut resi = Vec::new();
for vertex in indices.iter() {
resi.push(
Pnt3::new(
Pnt3::new(vertex.x, vertex.x, vertex.x),
Pnt3::new(vertex.y, vertex.y, vertex.y),
Pnt3::new(vertex.z, vertex.z, vertex.z)
)
);
}
resi
}
#[inline]
pub fn split_index_buffer_and_recover_topology<P: PartialEq + AsBytes + Clone>(
indices: &[Pnt3<u32>],
coords: &[P])
-> (Vec<Pnt3<Pnt3<u32>>>, Vec<P>) {
let mut vtx_to_id = HashMap::new();
let mut new_coords = Vec::with_capacity(coords.len());
let mut out = Vec::with_capacity(indices.len());
fn resolve_coord_id<P: PartialEq + AsBytes + Clone>(
coord: &P,
vtx_to_id: &mut HashMap<HashablePartialEq<P>, u32>,
new_coords: &mut Vec<P>)
-> u32 {
let key = unsafe { HashablePartialEq::new(coord.clone()) };
let id = match vtx_to_id.entry(key) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(new_coords.len() as u32)
};
if *id == new_coords.len() as u32 {
new_coords.push(coord.clone());
}
*id
}
for t in indices.iter() {
let va = resolve_coord_id(&coords[t.x as usize], &mut vtx_to_id, &mut new_coords);
let oa = t.x;
let vb = resolve_coord_id(&coords[t.y as usize], &mut vtx_to_id, &mut new_coords);
let ob = t.y;
let vc = resolve_coord_id(&coords[t.z as usize], &mut vtx_to_id, &mut new_coords);
let oc = t.z;
out.push(
Pnt3::new(
Pnt3::new(va, oa, oa),
Pnt3::new(vb, ob, ob),
Pnt3::new(vc, oc, oc)
)
);
}
new_coords.shrink_to_fit();
(out, new_coords)
}
#[inline]
pub fn compute_normals<P>(coordinates: &[P], faces: &[Pnt3<u32>], normals: &mut Vec<P::Vect>)
where P: Point,
P::Vect: Vect + Cross<CrossProductType = <P as Point>::Vect> {
let mut divisor: Vec<<P::Vect as Vect>::Scalar> = iter::repeat(na::zero()).take(coordinates.len()).collect();
if normals.len() > coordinates.len() {
normals.truncate(coordinates.len())
}
normals.clear();
normals.extend(iter::repeat(na::zero::<P::Vect>()).take(coordinates.len()));
for f in faces.iter() {
let edge1 = coordinates[f.y as usize] - coordinates[f.x as usize];
let edge2 = coordinates[f.z as usize] - coordinates[f.x as usize];
let cross = na::cross(&edge1, &edge2);
let normal;
if !na::is_zero(&cross) {
normal = na::normalize(&cross)
}
else {
normal = cross
}
normals[f.x as usize] = normals[f.x as usize] + normal;
normals[f.y as usize] = normals[f.y as usize] + normal;
normals[f.z as usize] = normals[f.z as usize] + normal;
divisor[f.x as usize] = divisor[f.x as usize] + na::one();
divisor[f.y as usize] = divisor[f.y as usize] + na::one();
divisor[f.z as usize] = divisor[f.z as usize] + na::one();
}
for (n, divisor) in normals.iter_mut().zip(divisor.iter()) {
*n = *n / *divisor
}
}