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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use super::{PolyMesh, TetMesh, TriMesh};
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum AxisPlaneOrientation {
XY,
YZ,
ZX,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct GridBuilder {
pub rows: usize,
pub cols: usize,
pub orientation: AxisPlaneOrientation,
}
impl GridBuilder {
pub fn build(self) -> PolyMesh<f64> {
let GridBuilder {
rows,
cols,
orientation,
} = self;
let mut positions = Vec::new();
for j in 0..=cols {
for i in 0..=rows {
let r = -1.0 + 2.0 * (i as f64) / rows as f64;
let c = -1.0 + 2.0 * (j as f64) / cols as f64;
let node_pos = match orientation {
AxisPlaneOrientation::XY => [r, c, 0.0],
AxisPlaneOrientation::YZ => [0.0, r, c],
AxisPlaneOrientation::ZX => [c, 0.0, r],
};
positions.push(node_pos);
}
}
let mut indices = Vec::new();
for i in 0..rows {
for j in 0..cols {
indices.push(4);
indices.push((rows + 1) * j + i);
indices.push((rows + 1) * j + i + 1);
indices.push((rows + 1) * (j + 1) + i + 1);
indices.push((rows + 1) * (j + 1) + i);
}
}
PolyMesh::new(positions, &indices)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct SolidBoxBuilder {
pub res: [usize; 3],
}
impl Default for SolidBoxBuilder {
fn default() -> Self {
SolidBoxBuilder { res: [1, 1, 1] }
}
}
impl SolidBoxBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn build(self) -> TetMesh<f64> {
let mut positions = Vec::new();
let [nx, ny, nz] = self.res;
for ix in 0..=nx {
for iy in 0..=ny {
for iz in 0..=nz {
let x = -1.0 + 2.0 * (ix as f64) / nx as f64;
let y = -1.0 + 2.0 * (iy as f64) / ny as f64;
let z = -1.0 + 2.0 * (iz as f64) / nz as f64;
positions.push([x, y, z]);
}
}
}
let mut indices = Vec::new();
for ix in 0..nx {
for iy in 0..ny {
for iz in 0..nz {
let index = |x, y, z| ((ix + x) * (ny + 1) + (iy + y)) * (nz + 1) + (iz + z);
let first = index(0, 0, 0);
let second = index(1, 1, 1);
indices.push([first, second, index(0, 1, 1), index(0, 1, 0)]);
indices.push([first, second, index(0, 1, 0), index(1, 1, 0)]);
indices.push([first, second, index(1, 1, 0), index(1, 0, 0)]);
indices.push([first, second, index(1, 0, 0), index(1, 0, 1)]);
indices.push([first, second, index(1, 0, 1), index(0, 0, 1)]);
indices.push([first, second, index(0, 0, 1), index(0, 1, 1)]);
}
}
}
TetMesh::new(positions, indices)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct PlatonicSolidBuilder {}
impl PlatonicSolidBuilder {
pub fn build_octahedron() -> TriMesh<f64> {
let vertices = vec![
[-0.5, 0.0, 0.0],
[0.5, 0.0, 0.0],
[0.0, -0.5, 0.0],
[0.0, 0.5, 0.0],
[0.0, 0.0, -0.5],
[0.0, 0.0, 0.5],
];
#[rustfmt::skip]
let indices = vec![
[0, 5, 3],
[4, 0, 3],
[1, 4, 3],
[5, 1, 3],
[5, 0, 2],
[0, 4, 2],
[4, 1, 2],
[1, 5, 2],
];
TriMesh::new(vertices, indices)
}
pub fn build_tetrahedron() -> TetMesh<f64> {
let sqrt_8_by_9 = f64::sqrt(8.0 / 9.0);
let sqrt_2_by_9 = f64::sqrt(2.0 / 9.0);
let sqrt_2_by_3 = f64::sqrt(2.0 / 3.0);
let vertices = vec![
[0.0, 1.0, 0.0],
[-sqrt_8_by_9, -1.0 / 3.0, 0.0],
[sqrt_2_by_9, -1.0 / 3.0, sqrt_2_by_3],
[sqrt_2_by_9, -1.0 / 3.0, -sqrt_2_by_3],
];
let indices = vec![[3, 1, 0, 2]];
TetMesh::new(vertices, indices)
}
pub fn build_icosahedron() -> TriMesh<f64> {
let sqrt5 = 5.0_f64.sqrt();
let a = 1.0 / sqrt5;
let w1 = 0.25 * (sqrt5 - 1.0);
let h1 = (0.125 * (5.0 + sqrt5)).sqrt();
let w2 = 0.25 * (sqrt5 + 1.0);
let h2 = (0.125 * (5.0 - sqrt5)).sqrt();
let vertices = vec![
[0.0, 0.0, 1.0],
[0.0, 2.0 * a, a],
[2.0 * a * h2, 2.0 * a * w2, -a],
[2.0 * a * h1, 2.0 * a * w1, a],
[2.0 * a * h1, -2.0 * a * w1, -a],
[2.0 * a * h2, -2.0 * a * w2, a],
[0.0, -2.0 * a, -a],
[-2.0 * a * h2, -2.0 * a * w2, a],
[-2.0 * a * h1, -2.0 * a * w1, -a],
[-2.0 * a * h1, 2.0 * a * w1, a],
[-2.0 * a * h2, 2.0 * a * w2, -a],
[0.0, 0.0, -1.0],
];
#[rustfmt::skip]
let indices = vec![
[0, 1, 3],
[0, 3, 5],
[0, 5, 7],
[0, 7, 9],
[0, 9, 1],
[1, 2, 3],
[2, 4, 3],
[3, 4, 5],
[4, 6, 5],
[5, 6, 7],
[6, 8, 7],
[7, 8, 9],
[8, 10, 9],
[9, 10, 1],
[10, 2, 1],
[11, 2, 10],
[11, 4, 2],
[11, 6, 4],
[11, 8, 6],
[11, 10, 8],
];
TriMesh::new(vertices, indices)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct TorusBuilder {
pub outer_radius: f32,
pub inner_radius: f32,
pub outer_divs: usize,
pub inner_divs: usize,
}
impl Default for TorusBuilder {
fn default() -> Self {
TorusBuilder {
outer_radius: 0.5,
inner_radius: 0.25,
outer_divs: 24,
inner_divs: 12,
}
}
}
impl TorusBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn build(self) -> PolyMesh<f64> {
let TorusBuilder {
outer_radius,
inner_radius,
outer_divs,
inner_divs,
} = self;
let mut vertices = Vec::with_capacity(outer_divs * inner_divs);
let mut indices = Vec::with_capacity(5 * outer_divs * inner_divs);
let outer_step = 2.0 * std::f64::consts::PI / outer_divs as f64;
let inner_step = 2.0 * std::f64::consts::PI / inner_divs as f64;
for i in 0..outer_divs {
let theta = outer_step * i as f64;
for j in 0..inner_divs {
let phi = inner_step * j as f64;
let idx = vertices.len();
vertices.push([
theta.cos() * (outer_radius as f64 + phi.cos() * inner_radius as f64),
phi.sin() * inner_radius as f64,
theta.sin() * (outer_radius as f64 + phi.cos() * inner_radius as f64),
]);
indices.extend_from_slice(&[
4,
idx,
(((idx + 1) % inner_divs) + inner_divs * (idx / inner_divs))
% (inner_divs * outer_divs),
((1 + idx) % inner_divs + (1 + idx / inner_divs) * inner_divs)
% (inner_divs * outer_divs),
(idx % inner_divs + (1 + idx / inner_divs) * inner_divs)
% (inner_divs * outer_divs),
]);
}
}
PolyMesh::new(vertices, &indices)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn icosahedron_unity_test() {
use crate::mesh::VertexPositions;
use approx::assert_relative_eq;
use math::Vector3;
let icosa = PlatonicSolidBuilder::build_icosahedron();
for &v in icosa.vertex_positions() {
assert_relative_eq!(Vector3::from(v).norm(), 1.0);
}
}
#[test]
fn grid_test() {
use crate::ops::*;
let grid = GridBuilder {
rows: 1,
cols: 1,
orientation: AxisPlaneOrientation::ZX,
}
.build();
let bbox = grid.bounding_box();
assert_eq!(bbox.min_corner(), [-1.0, 0.0, -1.0]);
assert_eq!(bbox.max_corner(), [1.0, 0.0, 1.0]);
}
}