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
use crate::{
clone,
geom::{Collide, Cube, Mesh, SmoothTriangle, Tree},
math::Pos3,
ord::Set,
tools::ProgressBar,
};
use arctk_attr::load;
use std::fmt::Display;
#[load]
pub struct TreeBuilder {
tar_tris: usize,
max_depth: u32,
padding: f64,
}
impl TreeBuilder {
clone!(tar_tris, usize);
clone!(max_depth, u32);
clone!(padding, f64);
#[inline]
#[must_use]
pub fn build<'a, T: Display + Clone + Ord>(&self, surfs: &'a Set<T, Mesh>) -> Tree<'a, &'a T> {
let mut boundary = Self::init_boundary(surfs);
boundary.expand(self.padding);
let mut tris = Vec::new();
for (key, mesh) in surfs.map() {
tris.reserve(mesh.tris().len());
for tri in mesh.tris() {
tris.push((key, tri));
}
}
let mut pb = ProgressBar::new("Growing tree", 8_u64.pow(self.max_depth));
let children = self.init_children(&boundary, 1, tris.as_slice(), &mut pb);
pb.finish_with_message("Tree grown.");
Tree::Root { boundary, children }
}
#[inline]
#[must_use]
fn init_boundary<T: Display + Clone + Ord>(surfs: &Set<T, Mesh>) -> Cube {
let mut mins = None;
let mut maxs = None;
for mesh in surfs.map().values() {
let (mesh_mins, mesh_maxs) = mesh.boundary().mins_maxs();
if mins.is_none() {
mins = Some(mesh_mins);
} else {
for (grid_min, mesh_min) in mins.as_mut().unwrap().iter_mut().zip(mesh_mins.iter())
{
if mesh_min < grid_min {
*grid_min = *mesh_min;
}
}
}
if maxs.is_none() {
maxs = Some(mesh_maxs);
} else {
for (grid_max, mesh_max) in maxs.as_mut().unwrap().iter_mut().zip(mesh_maxs.iter())
{
if mesh_max > grid_max {
*grid_max = *mesh_max;
}
}
}
}
Cube::new(mins.unwrap(), maxs.unwrap())
}
#[allow(clippy::similar_names)]
#[inline]
#[must_use]
fn init_children<'a, T: Clone>(
&self,
parent_boundary: &Cube,
depth: u32,
potential_tris: &[(&'a T, &'a SmoothTriangle)],
mut pb: &mut ProgressBar,
) -> [Box<Tree<'a, &'a T>>; 8] {
debug_assert!(depth <= self.max_depth);
debug_assert!(!potential_tris.is_empty());
let hws = parent_boundary.half_widths();
let mut make_child = |min_x: f64, min_y: f64, min_z: f64| {
let min = Pos3::new(min_x, min_y, min_z);
Box::new(self.init_child(Cube::new(min, min + hws), depth, potential_tris, &mut pb))
};
let mins = parent_boundary.mins();
let min_x = mins.x;
let min_y = mins.y;
let min_z = mins.z;
let nnn = make_child(min_x, min_y, min_z);
let pnn = make_child(min_x + hws.x, min_y, min_z);
let npn = make_child(min_x, min_y + hws.y, min_z);
let ppn = make_child(min_x + hws.x, min_y + hws.y, min_z);
let nnp = make_child(min_x, min_y, min_z + hws.z);
let pnp = make_child(min_x + hws.x, min_y, min_z + hws.z);
let npp = make_child(min_x, min_y + hws.y, min_z + hws.z);
let ppp = make_child(min_x + hws.x, min_y + hws.y, min_z + hws.z);
[nnn, pnn, npn, ppn, nnp, pnp, npp, ppp]
}
#[inline]
#[must_use]
fn init_child<'a, T: Clone>(
&self,
boundary: Cube,
depth: u32,
potential_tris: &[(&'a T, &'a SmoothTriangle)],
mut pb: &mut ProgressBar,
) -> Tree<'a, &'a T> {
debug_assert!(depth <= self.max_depth);
let mut detection_vol = boundary.clone();
detection_vol.expand(self.padding);
let mut tris = Vec::new();
for (key, tri) in potential_tris {
if tri.overlap(&detection_vol) {
tris.push((*key, *tri));
}
}
if tris.is_empty() {
pb.block(8_u64.pow((self.max_depth - depth) as u32));
return Tree::Empty { boundary };
}
if (tris.len() <= self.tar_tris) || (depth >= self.max_depth) {
pb.block(8_u64.pow((self.max_depth - depth) as u32));
return Tree::Leaf { boundary, tris };
}
let children = self.init_children(&boundary, depth + 1, &tris, &mut pb);
Tree::Branch { boundary, children }
}
}