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
use crate::{is_zero, Intersection, Plane, Polygon, Splitter};
use binary_space_partition::{BspNode, Plane as BspPlane, PlaneCut};
use euclid::{approxeq::ApproxEq, Point3D, Vector3D};
use num_traits::{Float, One, Zero};
use std::{fmt, iter, ops};
impl<T, U, A> BspPlane for Polygon<T, U, A>
where
T: Copy
+ fmt::Debug
+ ApproxEq<T>
+ ops::Sub<T, Output = T>
+ ops::Add<T, Output = T>
+ ops::Mul<T, Output = T>
+ ops::Div<T, Output = T>
+ Zero
+ Float,
U: fmt::Debug,
A: Copy + fmt::Debug,
{
fn cut(&self, mut poly: Self) -> PlaneCut<Self> {
log::debug!("\tCutting anchor {:?} by {:?}", poly.anchor, self.anchor);
log::trace!("\t\tbase {:?}", self.plane);
let (intersection, dist) = match self.plane.intersect(&poly.plane) {
None => {
let ndot = self.plane.normal.dot(poly.plane.normal);
log::debug!("\t\tNormals are aligned with {:?}", ndot);
let dist = self.plane.offset - ndot * poly.plane.offset;
(Intersection::Coplanar, dist)
}
Some(_) if self.plane.are_outside(&poly.points) => {
let dist = self.plane.signed_distance_sum_to(&poly);
(Intersection::Outside, dist)
}
Some(line) => {
(Intersection::Inside(line), T::zero())
}
};
match intersection {
Intersection::Coplanar if is_zero(dist) => {
log::debug!("\t\tCoplanar at {:?}", dist);
PlaneCut::Sibling(poly)
}
Intersection::Coplanar | Intersection::Outside => {
log::debug!("\t\tOutside at {:?}", dist);
if dist > T::zero() {
PlaneCut::Cut {
front: vec![poly],
back: vec![],
}
} else {
PlaneCut::Cut {
front: vec![],
back: vec![poly],
}
}
}
Intersection::Inside(line) => {
log::debug!("\t\tCut across {:?}", line);
let (res_add1, res_add2) = poly.split_with_normal(&line, &self.plane.normal);
let mut front = Vec::new();
let mut back = Vec::new();
for sub in iter::once(poly)
.chain(res_add1)
.chain(res_add2)
.filter(|p| !p.is_empty())
{
let dist = self.plane.signed_distance_sum_to(&sub);
if dist > T::zero() {
log::trace!("\t\t\tdist {:?} -> front: {:?}", dist, sub);
front.push(sub)
} else {
log::trace!("\t\t\tdist {:?} -> back: {:?}", dist, sub);
back.push(sub)
}
}
PlaneCut::Cut { front, back }
}
}
}
fn is_aligned(&self, other: &Self) -> bool {
self.plane.normal.dot(other.plane.normal) > T::zero()
}
}
pub struct BspSplitter<T, U, A> {
tree: BspNode<Polygon<T, U, A>>,
result: Vec<Polygon<T, U, A>>,
}
impl<T, U, A> BspSplitter<T, U, A> {
pub fn new() -> Self {
BspSplitter {
tree: BspNode::new(),
result: Vec::new(),
}
}
}
impl<T, U, A> Splitter<T, U, A> for BspSplitter<T, U, A>
where
T: Copy
+ fmt::Debug
+ ApproxEq<T>
+ ops::Sub<T, Output = T>
+ ops::Add<T, Output = T>
+ ops::Mul<T, Output = T>
+ ops::Div<T, Output = T>
+ Zero
+ One
+ Float,
U: fmt::Debug,
A: Copy + fmt::Debug + Default,
{
fn reset(&mut self) {
self.tree = BspNode::new();
}
fn add(&mut self, poly: Polygon<T, U, A>) {
self.tree.insert(poly);
}
fn sort(&mut self, view: Vector3D<T, U>) -> &[Polygon<T, U, A>] {
let poly = Polygon {
points: [Point3D::origin(); 4],
plane: Plane {
normal: -view,
offset: T::zero(),
},
anchor: A::default(),
};
self.result.clear();
self.tree.order(&poly, &mut self.result);
&self.result
}
}