-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsg.rs
222 lines (199 loc) · 7.7 KB
/
csg.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use std::{cmp::Ordering, collections::VecDeque};
use glam::Affine3A;
use crate::{
hit::Hit,
object::{
plane::Plane, polymesh::PolyMesh, quadratic::Quadratic, sphere::Sphere, triangle::Triangle,
Object,
},
ray::Ray,
};
#[derive(Debug)]
pub enum Csg {
Branch {
child1: Box<Csg>,
child2: Box<Csg>,
operation: Op,
},
Leaf {
object: Box<dyn Object + Send + Sync + 'static>,
},
}
#[derive(Debug)]
pub enum Op {
Union,
Intersection,
Difference,
}
impl Csg {
pub fn new_branch<T1, T2>(child1: T1, child2: T2, operation: Op) -> Self
where
T1: Into<Csg>,
T2: Into<Csg>,
{
Csg::Branch {
child1: Box::new(child1.into()),
child2: Box::new(child2.into()),
operation,
}
}
pub fn new_leaf(object: Box<dyn Object + Send + Sync + 'static>) -> Self {
Csg::Leaf { object }
}
}
pub trait FromCsg: Object {}
impl FromCsg for Plane {}
impl FromCsg for PolyMesh {}
impl FromCsg for Quadratic {}
impl FromCsg for Sphere {}
impl FromCsg for Triangle {}
impl<T> From<T> for Csg
where
T: FromCsg + Send + Sync + 'static,
{
fn from(object: T) -> Self {
Self::new_leaf(Box::new(object))
}
}
impl Object for Csg {
fn intersection(&self, ray: &Ray) -> Vec<Hit> {
match self {
Csg::Branch {
child1,
child2,
operation,
} => {
let int_a = VecDeque::from(child1.intersection(ray));
let int_b = VecDeque::from(child2.intersection(ray));
// perform operation to merge intersections
match operation {
Op::Union => Op::union(int_a, int_b),
Op::Intersection => Op::intersection(int_a, int_b),
Op::Difference => Op::difference(int_a, int_b),
}
}
Csg::Leaf { object } => object.intersection(ray),
}
}
fn apply_transform(&mut self, t: Affine3A) {
match self {
Csg::Branch { child1, child2, .. } => {
child1.apply_transform(t);
child2.apply_transform(t);
}
Csg::Leaf { object } => object.apply_transform(t),
}
}
}
impl Op {
pub fn union<'a>(mut int_a: VecDeque<Hit<'a>>, mut int_b: VecDeque<Hit<'a>>) -> Vec<Hit<'a>> {
use Ordering::*;
let mut int_result = Vec::new();
let mut inside_a = false;
let mut inside_b = false;
while !(int_a.is_empty() || int_b.is_empty()) {
match (
inside_a,
inside_b,
int_a[0].t.partial_cmp(&int_b[0].t).unwrap(),
) {
(false, false, Less) => keep(&mut int_a, &mut int_result, &mut inside_a),
(false, false, Greater) => keep(&mut int_b, &mut int_result, &mut inside_b),
(false, false, Equal) => keep(&mut int_a, &mut int_result, &mut inside_a),
(true, false, Less) => keep(&mut int_a, &mut int_result, &mut inside_a),
(true, false, Greater) => discard(&mut int_b, &mut inside_b),
(true, false, Equal) => discard(&mut int_a, &mut inside_a),
(true, true, Less) => discard(&mut int_a, &mut inside_a),
(true, true, Greater) => discard(&mut int_b, &mut inside_b),
(true, true, Equal) => discard(&mut int_a, &mut inside_a),
(false, true, Less) => discard(&mut int_a, &mut inside_a),
(false, true, Greater) => keep(&mut int_b, &mut int_result, &mut inside_b),
(false, true, Equal) => discard(&mut int_b, &mut inside_b),
}
}
while !int_a.is_empty() {
keep(&mut int_a, &mut int_result, &mut inside_a);
}
while !int_b.is_empty() {
keep(&mut int_b, &mut int_result, &mut inside_b);
}
int_result
}
pub fn intersection<'a>(
mut int_a: VecDeque<Hit<'a>>,
mut int_b: VecDeque<Hit<'a>>,
) -> Vec<Hit<'a>> {
use Ordering::*;
let mut int_result = Vec::new();
let mut inside_a = false;
let mut inside_b = false;
while !(int_a.is_empty() || int_b.is_empty()) {
match (
inside_a,
inside_b,
int_a[0].t.partial_cmp(&int_b[0].t).unwrap(),
) {
(false, false, Less) => discard(&mut int_a, &mut inside_a),
(false, false, Greater) => discard(&mut int_b, &mut inside_b),
(false, false, Equal) => keep(&mut int_a, &mut int_result, &mut inside_a),
(true, false, Less) => discard(&mut int_a, &mut inside_a),
(true, false, Greater) => keep(&mut int_b, &mut int_result, &mut inside_b),
(true, false, Equal) => discard(&mut int_a, &mut inside_a),
(true, true, Less) => keep(&mut int_a, &mut int_result, &mut inside_a),
(true, true, Greater) => keep(&mut int_b, &mut int_result, &mut inside_b),
(true, true, Equal) => keep(&mut int_a, &mut int_result, &mut inside_a),
(false, true, Less) => keep(&mut int_a, &mut int_result, &mut inside_a),
(false, true, Greater) => discard(&mut int_b, &mut inside_b),
(false, true, Equal) => discard(&mut int_b, &mut inside_b),
}
}
drop(int_a);
drop(int_b);
int_result
}
pub fn difference<'a>(
mut int_a: VecDeque<Hit<'a>>,
mut int_b: VecDeque<Hit<'a>>,
) -> Vec<Hit<'a>> {
use Ordering::*;
let mut int_result = Vec::new();
let mut inside_a = false;
let mut inside_b = false;
while !(int_a.is_empty() || int_b.is_empty()) {
match (
inside_a,
inside_b,
int_a[0].t.partial_cmp(&int_b[0].t).unwrap(),
) {
(false, false, Less) => keep(&mut int_a, &mut int_result, &mut inside_a),
(false, false, Greater) => discard(&mut int_b, &mut inside_b),
(false, false, Equal) => discard(&mut int_b, &mut inside_b),
(true, false, Less) => keep(&mut int_a, &mut int_result, &mut inside_a),
(true, false, Greater) => keep(&mut int_b, &mut int_result, &mut inside_b),
(true, false, Equal) => keep(&mut int_a, &mut int_result, &mut inside_a),
(true, true, Less) => discard(&mut int_a, &mut inside_a),
(true, true, Greater) => keep(&mut int_b, &mut int_result, &mut inside_b),
(true, true, Equal) => discard(&mut int_a, &mut inside_a),
(false, true, Less) => discard(&mut int_a, &mut inside_a),
(false, true, Greater) => discard(&mut int_b, &mut inside_b),
(false, true, Equal) => discard(&mut int_b, &mut inside_b),
}
}
while !int_a.is_empty() {
keep(&mut int_a, &mut int_result, &mut inside_a);
}
drop(int_b);
int_result
}
}
fn keep<'a>(source: &mut VecDeque<Hit<'a>>, dest: &mut Vec<Hit<'a>>, inside: &mut bool) {
// keep the remaining intersection points in source
*inside = !*inside;
let val = source.pop_front().unwrap();
dest.push(val);
}
fn discard(source: &mut VecDeque<Hit>, inside: &mut bool) {
*inside = !*inside;
// pop and discard unneeded intersection points with _
let _ = source.pop_front().unwrap();
}