-
Notifications
You must be signed in to change notification settings - Fork 0
/
subdivision.rs
130 lines (110 loc) · 3.61 KB
/
subdivision.rs
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
use dcel::{ptr_t, Dcel, ObjExport, ObjImport, Shell};
use std::array::from_fn;
use std::collections::HashMap;
fn avg(iter: impl Iterator<Item = [f32; 3]>) -> Option<[f32; 3]> {
let mut count = 0usize;
let mut accum = [0.0; 3];
for x in iter {
count += 1;
accum = from_fn(|i| accum[i] + x[i])
}
(count > 0).then(|| accum.map(|x| x / (count as f32)))
}
fn catmull_clark_subdivision<'brand, 'arena>(
dcel: &mut Dcel<'brand, 'arena, [f32; 3]>,
shell: ptr_t!(Shell<'brand, 'arena, [f32; 3]>),
) {
let face_points = shell
.iter_faces(dcel)
.map(|f| {
(
f.id(),
avg(f.outer_loop().iter_half_edges().map(|x| *x.origin().data())).unwrap(),
)
})
.collect::<HashMap<_, _>>();
let vert_points = shell
.iter_vertices(dcel)
.map(|vert| {
let p = vert.data();
let mut n = 0.0;
let mut f = [0.0; 3];
let mut r = [0.0; 3];
for h in vert.iter_outgoing() {
let fp = face_points[&h.loop_().face().id()];
let op = *h.origin().data();
let tp = *h.target().data();
f = from_fn(|i| f[i] + fp[i]);
r = from_fn(|i| r[i] + op[i] + tp[i]);
n += 1.0;
}
(
vert.id(),
(
vert.item,
from_fn(|i| (f[i] / n + r[i] / n + (n - 3.0) * p[i]) / n),
),
)
})
.collect::<HashMap<_, _>>();
for edge in shell.iter_edges(dcel).map(|x| x.item).collect::<Vec<_>>() {
let data = avg(edge
.lens(dcel)
.half_edges()
.into_iter()
.flat_map(|h| [face_points[&h.loop_().face().id()], *h.origin().data()]))
.unwrap();
dcel.mve(edge, data).unwrap();
}
for &(vert, data) in vert_points.values() {
*vert.mut_data(dcel) = data;
}
for face in shell.iter_faces(dcel).map(|x| x.item).collect::<Vec<_>>() {
let loop_ = face.outer_loop(dcel);
let mut verts = loop_
.iter_half_edges(dcel)
.skip_while(|h| vert_points.contains_key(&h.origin().id()))
.map(|h| h.origin().item)
.step_by(2)
.collect::<Vec<_>>()
.into_iter();
let inner = *dcel
.mev(loop_, verts.next().unwrap(), face_points[&face.id(dcel)])
.unwrap()
.vertex;
verts.fold([loop_; 2], |loops, vert| {
let l = loops
.into_iter()
.find(|&l| vert.find_outgoing(l, dcel).is_some())
.unwrap();
[l, *dcel.melf([vert, inner], l).unwrap().loop_]
});
}
}
fn main() {
Dcel::new(|mut dcel| {
let body = ObjImport::import(
&mut dcel,
&obj::raw::object::parse_obj(&mut std::io::BufReader::new(
std::fs::File::open("bunny.obj").unwrap(),
))
.unwrap(),
|(x, y, z, _)| [x, y, z],
)
.unwrap();
let shell = body.iter_shells(&dcel).next().unwrap().item;
for i in 0..5 {
println!("subdivision level {i}...");
catmull_clark_subdivision(&mut dcel, shell);
}
println!("exporting...");
ObjExport::export(
&mut std::io::BufWriter::new(std::fs::File::create("bunny2.obj").unwrap()),
&dcel,
|&[x, y, z]| (x as _, y as _, z as _, None),
|_, _| None,
|_, _| None,
)
.unwrap();
});
}