-
Notifications
You must be signed in to change notification settings - Fork 0
/
;
177 lines (167 loc) · 6 KB
/
;
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
use super::{
axis::Axis,
base_slices::BaseSlice,
line::Line3,
slice_path::{SlicePath, SurfacePathIterator},
triangle::Triangle,
FloatValue,
};
use bvh::{
aabb::Aabb,
bvh::{Bvh, BvhNode},
};
#[derive(Debug)]
pub struct Mesh {
pub aabb: Aabb<FloatValue, 3>,
pub bvh: Bvh<FloatValue, 3>,
pub triangles: Vec<Triangle>,
}
impl From<Vec<Triangle>> for Mesh {
fn from(mut triangles: Vec<Triangle>) -> Self {
Self {
aabb: triangles
.get(0)
.map(|triangle| {
let mut aabb = triangle.aabb;
for triangle in triangles.iter().skip(1) {
aabb.join_mut(&triangle.aabb);
}
aabb
})
.unwrap_or_else(|| Aabb::empty()),
bvh: Bvh::build(&mut triangles),
triangles,
}
}
}
impl Mesh {
pub fn slice_paths<'a>(
self: &'a Mesh,
axis: Axis,
slice_height: FloatValue,
) -> impl Iterator<Item = Vec<SlicePath>> + 'a {
self.slice_base_slices(axis, slice_height)
.map(|slice| slice.find_paths())
.filter(|paths| !paths.is_empty())
}
pub fn slice_surface(&self, axis: Axis, nozzle_width: FloatValue) -> SurfacePathIterator {
SurfacePathIterator::new(self, axis, nozzle_width)
}
pub fn slice_base_slices<'a>(
self: &'a Mesh,
axis: Axis,
slice_height: FloatValue,
) -> impl Iterator<Item = BaseSlice> + 'a {
let layer_count = ((self.aabb.max[axis as usize] - self.aabb.min[axis as usize])
/ slice_height)
.floor() as usize;
(0..layer_count).map(move |i| {
let layer = i as FloatValue * slice_height + self.aabb.min[axis as usize];
let mut base_slice = BaseSlice {
i,
d: layer,
axis,
lines: vec![],
};
let mut stack = Vec::<usize>::with_capacity(self.bvh.nodes.len());
stack.push(0);
while let Some(i) = stack.pop() {
match self.bvh.nodes[i] {
BvhNode::Node {
parent_index: _,
child_l_index,
child_l_aabb,
child_r_index,
child_r_aabb,
} => {
assert!(child_l_aabb.min[axis as usize] <= child_l_aabb.max[axis as usize]);
assert!(child_r_aabb.min[axis as usize] <= child_r_aabb.max[axis as usize]);
if layer >= child_l_aabb.min[axis as usize]
&& layer <= child_l_aabb.max[axis as usize]
{
stack.push(child_l_index);
}
if layer >= child_r_aabb.min[axis as usize]
&& layer <= child_r_aabb.max[axis as usize]
{
stack.push(child_r_index);
}
}
BvhNode::Leaf {
parent_index: _,
shape_index,
} => {
for line in self.triangles[shape_index].intersect(layer, axis as usize) {
base_slice.lines.push(line);
}
}
}
}
base_slice
})
}
pub fn outline_base_slice(&self, axis: Axis) -> BaseSlice {
let mut base_slice = BaseSlice {
i: 0,
d: 0.0,
axis,
lines: vec![],
};
'triangle: for (i, triangle) in self.triangles.iter().enumerate() {
let mut stack = Vec::<usize>::with_capacity(self.bvh.nodes.len());
stack.push(0);
let mut ab = false;
let mut ac = false;
let mut bc = false;
while let Some(i) = stack.pop() {
match self.bvh.nodes[i] {
BvhNode::Node {
parent_index: _,
child_l_index,
child_l_aabb,
child_r_index,
child_r_aabb,
} => {
let coords = [triangle.a, triangle.b, triangle.c];
macro_rules! match_aabb {
($side:expr) => {
coords
.iter()
.map(|point| {
$side.approx_contains_eps(point, FloatValue::EPSILON) as i32
})
.sum::<i32>()
>= 2
};
}
if match_aabb!(child_l_aabb) {
stack.push(child_l_index);
}
if match_aabb!(child_r_aabb) {
stack.push(child_r_index);
}
}
BvhNode::Leaf {
parent_index: _,
shape_index,
} => {
if i == shape_index {
continue;
}
let other = &self.triangles[shape_index];
let a = triangle.has_point(other.a);
let b = triangle.has_point(other.b);
let c = triangle.has_point(other.c);
ab = ab || (a && b);
ac = ac || (a && c);
bc = bc || (b && c);
if ab && ac && bc {
continue 'triangle;
}
}
}
}
}
base_slice
}
}