|
@@ -0,0 +1,427 @@
|
|
1
|
+extern crate cgmath;
|
|
2
|
+
|
|
3
|
+use std::collections::HashMap;
|
|
4
|
+use std::fs::File;
|
|
5
|
+use std::io::Write;
|
|
6
|
+use std::path::Path;
|
|
7
|
+
|
|
8
|
+use cgmath::prelude::*;
|
|
9
|
+use cgmath::Vector3;
|
|
10
|
+use serde::ser::{SerializeSeq, Serializer};
|
|
11
|
+use serde::Serialize;
|
|
12
|
+
|
|
13
|
+#[derive(Debug)]
|
|
14
|
+struct Triangle {
|
|
15
|
+ a: usize,
|
|
16
|
+ b: usize,
|
|
17
|
+ c: usize,
|
|
18
|
+}
|
|
19
|
+
|
|
20
|
+impl Triangle {
|
|
21
|
+ fn new(a: usize, b: usize, c: usize) -> Triangle {
|
|
22
|
+ Triangle { a, b, c }
|
|
23
|
+ }
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+impl Serialize for Triangle {
|
|
27
|
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
28
|
+ where
|
|
29
|
+ S: Serializer,
|
|
30
|
+ {
|
|
31
|
+ let vec_indices = vec![self.a, self.b, self.c];
|
|
32
|
+ let mut seq = serializer.serialize_seq(Some(vec_indices.len()))?;
|
|
33
|
+ for index in vec_indices {
|
|
34
|
+ seq.serialize_element(&index)?;
|
|
35
|
+ }
|
|
36
|
+ seq.end()
|
|
37
|
+ }
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+#[derive(Debug)]
|
|
41
|
+struct ArraySerializedVector(Vector3<f32>);
|
|
42
|
+
|
|
43
|
+#[derive(Serialize, Debug)]
|
|
44
|
+struct Polyhedron {
|
|
45
|
+ positions: Vec<ArraySerializedVector>,
|
|
46
|
+ cells: Vec<Triangle>,
|
|
47
|
+}
|
|
48
|
+
|
|
49
|
+impl Serialize for ArraySerializedVector {
|
|
50
|
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
51
|
+ where
|
|
52
|
+ S: Serializer,
|
|
53
|
+ {
|
|
54
|
+ let values = vec![self.0.x, self.0.y, self.0.z];
|
|
55
|
+ let mut seq = serializer.serialize_seq(Some(values.len()))?;
|
|
56
|
+ for value in values {
|
|
57
|
+ seq.serialize_element(&value)?;
|
|
58
|
+ }
|
|
59
|
+ seq.end()
|
|
60
|
+ }
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+impl Polyhedron {
|
|
64
|
+ fn new() -> Polyhedron {
|
|
65
|
+ Polyhedron {
|
|
66
|
+ positions: vec![],
|
|
67
|
+ cells: vec![],
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ fn new_isocahedron(radius: f32, detail: usize) -> Polyhedron {
|
|
72
|
+ let t = (1.0 + (5.0 as f32).sqrt()) / 2.0;
|
|
73
|
+ let base_isocahedron = Polyhedron {
|
|
74
|
+ positions: vec![
|
|
75
|
+ ArraySerializedVector(Vector3::new(-1.0, t, 0.0)),
|
|
76
|
+ ArraySerializedVector(Vector3::new(1.0, t, 0.0)),
|
|
77
|
+ ArraySerializedVector(Vector3::new(-1.0, -t, 0.0)),
|
|
78
|
+ ArraySerializedVector(Vector3::new(1.0, -t, 0.0)),
|
|
79
|
+ ArraySerializedVector(Vector3::new(0.0, -1.0, t)),
|
|
80
|
+ ArraySerializedVector(Vector3::new(0.0, 1.0, t)),
|
|
81
|
+ ArraySerializedVector(Vector3::new(0.0, -1.0, -t)),
|
|
82
|
+ ArraySerializedVector(Vector3::new(0.0, 1.0, -t)),
|
|
83
|
+ ArraySerializedVector(Vector3::new(t, 0.0, -1.0)),
|
|
84
|
+ ArraySerializedVector(Vector3::new(t, 0.0, 1.0)),
|
|
85
|
+ ArraySerializedVector(Vector3::new(-t, 0.0, -1.0)),
|
|
86
|
+ ArraySerializedVector(Vector3::new(-t, 0.0, 1.0)),
|
|
87
|
+ ],
|
|
88
|
+ cells: vec![
|
|
89
|
+ Triangle::new(0, 11, 5),
|
|
90
|
+ Triangle::new(0, 5, 1),
|
|
91
|
+ Triangle::new(0, 1, 7),
|
|
92
|
+ Triangle::new(0, 7, 10),
|
|
93
|
+ Triangle::new(0, 10, 11),
|
|
94
|
+ Triangle::new(1, 5, 9),
|
|
95
|
+ Triangle::new(5, 11, 4),
|
|
96
|
+ Triangle::new(11, 10, 2),
|
|
97
|
+ Triangle::new(10, 7, 6),
|
|
98
|
+ Triangle::new(7, 1, 8),
|
|
99
|
+ Triangle::new(3, 9, 4),
|
|
100
|
+ Triangle::new(3, 4, 2),
|
|
101
|
+ Triangle::new(3, 2, 6),
|
|
102
|
+ Triangle::new(3, 6, 8),
|
|
103
|
+ Triangle::new(3, 8, 9),
|
|
104
|
+ Triangle::new(4, 9, 5),
|
|
105
|
+ Triangle::new(2, 4, 11),
|
|
106
|
+ Triangle::new(6, 2, 10),
|
|
107
|
+ Triangle::new(8, 6, 7),
|
|
108
|
+ Triangle::new(9, 8, 1),
|
|
109
|
+ ],
|
|
110
|
+ };
|
|
111
|
+ let mut subdivided = Polyhedron::new();
|
|
112
|
+ subdivided.subdivide(base_isocahedron, radius, detail);
|
|
113
|
+ subdivided
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ fn new_dual_isocahedron(radius: f32, detail: usize) -> Polyhedron {
|
|
117
|
+ let isocahedron = Polyhedron::new_isocahedron(radius, detail);
|
|
118
|
+ let mut dual_isocahedron = Polyhedron::new();
|
|
119
|
+ dual_isocahedron.dual(isocahedron);
|
|
120
|
+ dual_isocahedron
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ fn subdivide(&mut self, other: Polyhedron, radius: f32, detail: usize) {
|
|
124
|
+ // TODO: maybe make this part of the polygon stuct to avoid having to pass it around
|
|
125
|
+ let mut added_vert_cache: HashMap<(i32, i32, i32), usize> = HashMap::new();
|
|
126
|
+ let precision = 10_f32.powi(4);
|
|
127
|
+ for triangle in other.cells {
|
|
128
|
+ let a = other.positions[triangle.a].0;
|
|
129
|
+ let b = other.positions[triangle.b].0;
|
|
130
|
+ let c = other.positions[triangle.c].0;
|
|
131
|
+ self.subdivide_triangle(a, b, c, radius, detail, precision, &mut added_vert_cache);
|
|
132
|
+ }
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ fn subdivide_triangle(
|
|
136
|
+ &mut self,
|
|
137
|
+ a: Vector3<f32>,
|
|
138
|
+ b: Vector3<f32>,
|
|
139
|
+ c: Vector3<f32>,
|
|
140
|
+ radius: f32,
|
|
141
|
+ detail: usize,
|
|
142
|
+ precision: f32,
|
|
143
|
+ added_vert_cache: &mut HashMap<(i32, i32, i32), usize>,
|
|
144
|
+ ) {
|
|
145
|
+ let cols = 2usize.pow(detail as u32);
|
|
146
|
+ let mut new_vertices: Vec<Vec<Vector3<f32>>> = vec![];
|
|
147
|
+
|
|
148
|
+ for i in 0..=cols {
|
|
149
|
+ new_vertices.push(vec![]);
|
|
150
|
+ let aj = a.clone().lerp(c, i as f32 / cols as f32);
|
|
151
|
+ let bj = b.clone().lerp(c, i as f32 / cols as f32);
|
|
152
|
+ let rows = cols - i;
|
|
153
|
+
|
|
154
|
+ for j in 0..=rows {
|
|
155
|
+ if j == 0 && i == cols {
|
|
156
|
+ new_vertices[i].push(aj.normalize() * radius);
|
|
157
|
+ } else {
|
|
158
|
+ new_vertices[i]
|
|
159
|
+ .push(aj.clone().lerp(bj, j as f32 / rows as f32).normalize() * radius);
|
|
160
|
+ }
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ for i in 0..cols {
|
|
165
|
+ for j in 0..2 * (cols - i) - 1 {
|
|
166
|
+ let k = j / 2;
|
|
167
|
+
|
|
168
|
+ let mut triangle = Triangle { a: 0, b: 0, c: 0 };
|
|
169
|
+ if j % 2 == 0 {
|
|
170
|
+ triangle.a =
|
|
171
|
+ self.add_position(new_vertices[i][k + 1], precision, added_vert_cache);
|
|
172
|
+ triangle.b =
|
|
173
|
+ self.add_position(new_vertices[i + 1][k], precision, added_vert_cache);
|
|
174
|
+ triangle.c = self.add_position(new_vertices[i][k], precision, added_vert_cache);
|
|
175
|
+ } else {
|
|
176
|
+ triangle.a =
|
|
177
|
+ self.add_position(new_vertices[i][k + 1], precision, added_vert_cache);
|
|
178
|
+ triangle.b =
|
|
179
|
+ self.add_position(new_vertices[i + 1][k + 1], precision, added_vert_cache);
|
|
180
|
+ triangle.c =
|
|
181
|
+ self.add_position(new_vertices[i + 1][k], precision, added_vert_cache);
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ self.cells.push(triangle);
|
|
185
|
+ }
|
|
186
|
+ }
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ fn add_position(
|
|
190
|
+ &mut self,
|
|
191
|
+ vertex: Vector3<f32>,
|
|
192
|
+ precision: f32,
|
|
193
|
+ added_vert_cache: &mut HashMap<(i32, i32, i32), usize>,
|
|
194
|
+ ) -> usize {
|
|
195
|
+ let vertex_key = (
|
|
196
|
+ (vertex.x * precision).round() as i32,
|
|
197
|
+ (vertex.y * precision).round() as i32,
|
|
198
|
+ (vertex.z * precision).round() as i32,
|
|
199
|
+ );
|
|
200
|
+ if let Some(added_vert_index) = added_vert_cache.get(&vertex_key) {
|
|
201
|
+ return *added_vert_index;
|
|
202
|
+ } else {
|
|
203
|
+ self.positions.push(ArraySerializedVector(vertex));
|
|
204
|
+ let added_index = self.positions.len() - 1;
|
|
205
|
+ added_vert_cache.insert(vertex_key, added_index);
|
|
206
|
+ return added_index;
|
|
207
|
+ }
|
|
208
|
+ }
|
|
209
|
+
|
|
210
|
+ fn dual(&mut self, other: Polyhedron) {
|
|
211
|
+ let vert_to_faces = other.vert_to_faces();
|
|
212
|
+ let original_vert_count = other.positions.len();
|
|
213
|
+ let triangle_centroids = other.triangle_centroids();
|
|
214
|
+ let mut mid_centroid_cache: HashMap<(usize, usize), Vector3<f32>> = HashMap::new();
|
|
215
|
+ let mut hex_count = 0;
|
|
216
|
+ let mut pent_count = 0;
|
|
217
|
+ for i in 0..original_vert_count {
|
|
218
|
+ let faces = &vert_to_faces[&i];
|
|
219
|
+ if faces.len() == 6 {
|
|
220
|
+ hex_count += 1;
|
|
221
|
+ } else {
|
|
222
|
+ pent_count += 1;
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+ let center_point = find_center_of_triangles(faces, &triangle_centroids);
|
|
226
|
+
|
|
227
|
+ for face_index in faces {
|
|
228
|
+ let triangle = &other.cells[*face_index];
|
|
229
|
+ let other_verts: Vec<usize> = vec![triangle.a, triangle.b, triangle.c]
|
|
230
|
+ .drain(..)
|
|
231
|
+ .filter(|vert| *vert != i)
|
|
232
|
+ .collect();
|
|
233
|
+ let sorted_triangle = Triangle::new(i, other_verts[0], other_verts[1]);
|
|
234
|
+
|
|
235
|
+ let centroid = triangle_centroids[face_index];
|
|
236
|
+ let mid_b_centroid = other.calculate_mid_centroid(
|
|
237
|
+ sorted_triangle.b,
|
|
238
|
+ faces,
|
|
239
|
+ centroid,
|
|
240
|
+ &triangle_centroids,
|
|
241
|
+ &mut mid_centroid_cache,
|
|
242
|
+ );
|
|
243
|
+ let mid_c_centroid = other.calculate_mid_centroid(
|
|
244
|
+ sorted_triangle.c,
|
|
245
|
+ faces,
|
|
246
|
+ centroid,
|
|
247
|
+ &triangle_centroids,
|
|
248
|
+ &mut mid_centroid_cache,
|
|
249
|
+ );
|
|
250
|
+
|
|
251
|
+ let positions_start = self.positions.len();
|
|
252
|
+ // TODO: remove duplication here:
|
|
253
|
+ // push all triangle_centroids at beginning, same index as face_index
|
|
254
|
+ // push center_point once in outer loop and save index
|
|
255
|
+ // (is it okay if vertices show up in positions out of order like that?)
|
|
256
|
+ // -- yes, I think it is okay
|
|
257
|
+ self.positions.push(ArraySerializedVector(center_point));
|
|
258
|
+ self.positions.push(ArraySerializedVector(centroid));
|
|
259
|
+ self.positions.push(ArraySerializedVector(mid_b_centroid));
|
|
260
|
+ self.positions.push(ArraySerializedVector(mid_c_centroid));
|
|
261
|
+
|
|
262
|
+ self.cells.push(Triangle::new(
|
|
263
|
+ positions_start,
|
|
264
|
+ positions_start + 1,
|
|
265
|
+ positions_start + 2,
|
|
266
|
+ ));
|
|
267
|
+ self.cells.push(Triangle::new(
|
|
268
|
+ positions_start,
|
|
269
|
+ positions_start + 1,
|
|
270
|
+ positions_start + 3,
|
|
271
|
+ ));
|
|
272
|
+ }
|
|
273
|
+ }
|
|
274
|
+ println!("hexagons: {}", hex_count);
|
|
275
|
+ println!("pentagons: {}", pent_count);
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+ fn vert_to_faces(&self) -> HashMap<usize, Vec<usize>> {
|
|
279
|
+ let mut vert_to_faces: HashMap<usize, Vec<usize>> = HashMap::new();
|
|
280
|
+ for i in 0..self.cells.len() {
|
|
281
|
+ let triangle = &self.cells[i];
|
|
282
|
+
|
|
283
|
+ if let Some(faces) = vert_to_faces.get_mut(&triangle.a) {
|
|
284
|
+ faces.push(i);
|
|
285
|
+ } else {
|
|
286
|
+ vert_to_faces.insert(triangle.a, vec![i]);
|
|
287
|
+ }
|
|
288
|
+
|
|
289
|
+ if let Some(faces) = vert_to_faces.get_mut(&triangle.b) {
|
|
290
|
+ faces.push(i);
|
|
291
|
+ } else {
|
|
292
|
+ vert_to_faces.insert(triangle.b, vec![i]);
|
|
293
|
+ }
|
|
294
|
+
|
|
295
|
+ if let Some(faces) = vert_to_faces.get_mut(&triangle.c) {
|
|
296
|
+ faces.push(i);
|
|
297
|
+ } else {
|
|
298
|
+ vert_to_faces.insert(triangle.c, vec![i]);
|
|
299
|
+ }
|
|
300
|
+ }
|
|
301
|
+ vert_to_faces
|
|
302
|
+ }
|
|
303
|
+
|
|
304
|
+ fn triangle_centroids(&self) -> HashMap<usize, Vector3<f32>> {
|
|
305
|
+ let mut triangle_centroids: HashMap<usize, Vector3<f32>> = HashMap::new();
|
|
306
|
+ for i in 0..self.cells.len() {
|
|
307
|
+ let a = self.positions[self.cells[i].a].0;
|
|
308
|
+ let b = self.positions[self.cells[i].b].0;
|
|
309
|
+ let c = self.positions[self.cells[i].c].0;
|
|
310
|
+ triangle_centroids.insert(i, calculate_centroid(a, b, c));
|
|
311
|
+ }
|
|
312
|
+ triangle_centroids
|
|
313
|
+ }
|
|
314
|
+
|
|
315
|
+ fn calculate_mid_centroid(
|
|
316
|
+ &self,
|
|
317
|
+ vertex_index: usize,
|
|
318
|
+ faces: &Vec<usize>,
|
|
319
|
+ centroid: Vector3<f32>,
|
|
320
|
+ triangle_centroids: &HashMap<usize, Vector3<f32>>,
|
|
321
|
+ mid_centroid_cache: &mut HashMap<(usize, usize), Vector3<f32>>,
|
|
322
|
+ ) -> Vector3<f32> {
|
|
323
|
+ let adj_face_index = self.find_adjacent_face(vertex_index, faces).unwrap();
|
|
324
|
+ let adj_centroid = triangle_centroids[&adj_face_index];
|
|
325
|
+ if let Some(mid_centroid) = mid_centroid_cache.get(&(vertex_index, adj_face_index)) {
|
|
326
|
+ return *mid_centroid;
|
|
327
|
+ } else {
|
|
328
|
+ let mid_centroid = centroid.clone().lerp(adj_centroid, 0.5);
|
|
329
|
+ mid_centroid_cache.insert((vertex_index, adj_face_index), mid_centroid);
|
|
330
|
+ return mid_centroid;
|
|
331
|
+ }
|
|
332
|
+ }
|
|
333
|
+
|
|
334
|
+ fn find_adjacent_face(&self, vertex_index: usize, faces: &Vec<usize>) -> Option<usize> {
|
|
335
|
+ for face_index in faces {
|
|
336
|
+ let triangle = &self.cells[*face_index];
|
|
337
|
+ if triangle.a == vertex_index
|
|
338
|
+ || triangle.b == vertex_index
|
|
339
|
+ || triangle.c == vertex_index
|
|
340
|
+ {
|
|
341
|
+ return Some(*face_index);
|
|
342
|
+ }
|
|
343
|
+ }
|
|
344
|
+ None
|
|
345
|
+ }
|
|
346
|
+}
|
|
347
|
+
|
|
348
|
+fn calculate_centroid(pa: Vector3<f32>, pb: Vector3<f32>, pc: Vector3<f32>) -> Vector3<f32> {
|
|
349
|
+ let vab_half = (pb.clone() - pa) / 2.0;
|
|
350
|
+ let pab_half = pa.clone() + vab_half;
|
|
351
|
+ ((pc.clone() - pab_half) * (1.0 / 3.0)) + pab_half
|
|
352
|
+}
|
|
353
|
+
|
|
354
|
+fn find_center_of_triangles(
|
|
355
|
+ triangle_indices: &Vec<usize>,
|
|
356
|
+ triangle_centroids: &HashMap<usize, Vector3<f32>>,
|
|
357
|
+) -> Vector3<f32> {
|
|
358
|
+ let mut center_point: Vector3<f32> = Vector3::new(0.0, 0.0, 0.0);
|
|
359
|
+ for triangle_index in triangle_indices.iter() {
|
|
360
|
+ center_point += triangle_centroids[triangle_index];
|
|
361
|
+ }
|
|
362
|
+ center_point /= triangle_indices.len() as f32;
|
|
363
|
+ center_point
|
|
364
|
+}
|
|
365
|
+
|
|
366
|
+fn generate_icosahedron_files(dir: &str, param_list: Vec<(f32, usize)>) {
|
|
367
|
+ for param in param_list {
|
|
368
|
+ println!(
|
|
369
|
+ "Generating icosahedron with radius {} and detail {}...",
|
|
370
|
+ param.0, param.1
|
|
371
|
+ );
|
|
372
|
+ let filename = Path::new(dir).join(format!("icosahedron_r{}_d{}.json", param.0, param.1));
|
|
373
|
+ let mut file = File::create(filename).expect("Can't create file");
|
|
374
|
+ let icosahedron = Polyhedron::new_isocahedron(param.0, param.1);
|
|
375
|
+ println!("triangles: {}", icosahedron.cells.len());
|
|
376
|
+ println!("vertices: {}", icosahedron.positions.len());
|
|
377
|
+ let icosahedron_json = serde_json::to_string(&icosahedron).expect("Problem serializing");
|
|
378
|
+ file.write_all(icosahedron_json.as_bytes())
|
|
379
|
+ .expect("Can't write to file");
|
|
380
|
+ }
|
|
381
|
+}
|
|
382
|
+
|
|
383
|
+fn generate_hexsphere_files(dir: &str, param_list: Vec<(f32, usize)>) {
|
|
384
|
+ for param in param_list {
|
|
385
|
+ println!(
|
|
386
|
+ "Generating hexsphere with radius {} and detail {}...",
|
|
387
|
+ param.0, param.1
|
|
388
|
+ );
|
|
389
|
+ let filename = Path::new(dir).join(format!("hexsphere_r{}_d{}.json", param.0, param.1));
|
|
390
|
+ let mut file = File::create(filename).expect("Can't create file");
|
|
391
|
+ let hexsphere = Polyhedron::new_dual_isocahedron(param.0, param.1);
|
|
392
|
+ println!("triangles: {}", hexsphere.cells.len());
|
|
393
|
+ println!("vertices: {}", hexsphere.positions.len());
|
|
394
|
+ let hexsphere_json = serde_json::to_string(&hexsphere).expect("Problem serializing");
|
|
395
|
+ file.write_all(hexsphere_json.as_bytes())
|
|
396
|
+ .expect("Can't write to file");
|
|
397
|
+ }
|
|
398
|
+}
|
|
399
|
+
|
|
400
|
+fn main() {
|
|
401
|
+ generate_hexsphere_files(
|
|
402
|
+ "output/",
|
|
403
|
+ vec![
|
|
404
|
+ (1.0, 0),
|
|
405
|
+ (1.0, 1),
|
|
406
|
+ (1.0, 2),
|
|
407
|
+ (1.0, 3),
|
|
408
|
+ (1.0, 4),
|
|
409
|
+ (1.0, 5),
|
|
410
|
+ (1.0, 6),
|
|
411
|
+ (1.0, 7),
|
|
412
|
+ ],
|
|
413
|
+ );
|
|
414
|
+ generate_icosahedron_files(
|
|
415
|
+ "output/",
|
|
416
|
+ vec![
|
|
417
|
+ (1.0, 0),
|
|
418
|
+ (1.0, 1),
|
|
419
|
+ (1.0, 2),
|
|
420
|
+ (1.0, 3),
|
|
421
|
+ (1.0, 4),
|
|
422
|
+ (1.0, 5),
|
|
423
|
+ (1.0, 6),
|
|
424
|
+ (1.0, 7),
|
|
425
|
+ ],
|
|
426
|
+ );
|
|
427
|
+}
|