-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsp_tree.h
340 lines (332 loc) · 8.73 KB
/
bsp_tree.h
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifndef BSP_TREE_H_INCLUDED
#define BSP_TREE_H_INCLUDED
#include "triangle.h"
#include <utility>
#include <random>
struct BSPNode final
{
vector<Triangle> triangles;
VectorF normal;
float d;
BSPNode *front;
BSPNode *back;
BSPNode(Triangle tri)
: triangles(1, tri), normal(tri.normal()), front(nullptr), back(nullptr)
{
d = -dot(normal, tri.p1);
}
BSPNode(Triangle tri, VectorF normal, float d)
: triangles(1, tri), normal(tri.empty() ? VectorF(0) : normal), d(d), front(nullptr), back(nullptr)
{
}
BSPNode(vector<Triangle> triangles, VectorF normal, float d)
: triangles(std::move(triangles)), normal(normal), d(d), front(nullptr), back(nullptr)
{
}
bool good() const
{
return normal != VectorF(0);
}
void invert()
{
for(Triangle &tri : triangles)
{
tri = reverse(tri);
}
normal = -normal;
d = -d;
if(front)
front->invert();
if(back)
back->invert();
auto temp = front;
front = back;
back = temp;
}
};
class BSPTree final
{
BSPNode *root = nullptr;
static void freeNode(BSPNode *node)
{
if(node != nullptr)
{
freeNode(node->front);
freeNode(node->back);
delete node;
}
}
static BSPNode *dupNode(const BSPNode *node)
{
if(node == nullptr)
return nullptr;
BSPNode *retval = new BSPNode(*node);
retval->front = dupNode(node->front);
retval->back = dupNode(node->back);
return retval;
}
static void insertTriangle(BSPNode *&tree, Triangle tri)
{
if(tri.empty())
{
return;
}
if(tree == nullptr)
{
tree = new BSPNode(tri);
return;
}
CutTriangle ct = cut(tri, tree->normal, tree->d);
for(size_t i = 0; i < ct.coplanarTriangleCount; i++)
{
tree->triangles.push_back(ct.coplanarTriangles[i]);
}
for(size_t i = 0; i < ct.backTriangleCount; i++)
{
insertTriangle(tree->back, ct.backTriangles[i]);
}
for(size_t i = 0; i < ct.frontTriangleCount; i++)
{
insertTriangle(tree->front, ct.frontTriangles[i]);
}
}
static void getTriangles(vector<Triangle> &triangles, const BSPNode *tree)
{
if(tree == nullptr)
return;
getTriangles(triangles, tree->front);
triangles.insert(triangles.end(), tree->triangles.begin(), tree->triangles.end());
getTriangles(triangles, tree->back);
}
static void getTriangles(vector<Triangle> &triangles, const BSPNode *tree, VectorF viewPoint)
{
if(tree == nullptr)
return;
if(dot(viewPoint, tree->normal) > -tree->d)
{
getTriangles(triangles, tree->back, viewPoint);
triangles.insert(triangles.end(), tree->triangles.begin(), tree->triangles.end());
getTriangles(triangles, tree->front, viewPoint);
}
else
{
getTriangles(triangles, tree->front, viewPoint);
triangles.insert(triangles.end(), tree->triangles.begin(), tree->triangles.end());
getTriangles(triangles, tree->back, viewPoint);
}
}
static vector<Triangle> clipTriangles(vector<Triangle> triangles, const BSPNode *tree)
{
if(tree == nullptr)
return std::move(triangles);
vector<Triangle> front, back;
VectorF tn = tree->normal;
float td = tree->d;
for(Triangle tri : triangles)
{
CutTriangle ct = cut(tri, tn, td);
for(size_t i = 0; i < ct.coplanarTriangleCount; i++)
{
if(dot(ct.coplanarTriangles[i].point_cross(), tn) >= 0)
{
front.push_back(ct.coplanarTriangles[i]);
}
else if(tree->back != nullptr)
{
back.push_back(ct.coplanarTriangles[i]);
}
}
for(size_t i = 0; i < ct.frontTriangleCount; i++)
{
front.push_back(ct.frontTriangles[i]);
}
if(tree->back != nullptr)
{
for(size_t i = 0; i < ct.backTriangleCount; i++)
{
back.push_back(ct.backTriangles[i]);
}
}
}
triangles = vector<Triangle>();
front = clipTriangles(std::move(front), tree->front);
if(tree->back != nullptr)
back = clipTriangles(std::move(back), tree->back);
else
back.clear();
front.insert(front.begin(), back.begin(), back.end());
return std::move(front);
}
static void clipTo(BSPNode *clipTree, const BSPNode *clipToTree)
{
if(clipTree == nullptr)
return;
clipTree->triangles = clipTriangles(std::move(clipTree->triangles), clipToTree);
clipTo(clipTree->front, clipToTree);
clipTo(clipTree->back, clipToTree);
}
void merge(const BSPNode *tree)
{
if(tree == nullptr)
return;
merge(tree->front);
insert(tree->triangles);
merge(tree->back);
}
static void transform_helper(Transform tform, BSPNode *tree)
{
if(tree == nullptr)
return;
transform_helper(tform, tree->front);
transform_helper(tform, tree->back);
for(Triangle &tri : tree->triangles)
{
tri = transform(tform, tri);
}
VectorF p = transform(tform, tree->normal * -tree->d);
tree->normal = transformNormal(tform, tree->normal);
tree->d = -dot(tree->normal, p);
}
public:
BSPTree()
{
}
BSPTree(Triangle tri)
{
insert(tri);
}
BSPTree(const vector<Triangle> &triangles)
{
insert(triangles);
}
BSPTree(const BSPTree &rt)
: root(dupNode(rt.root))
{
}
BSPTree(BSPTree &&rt)
: root(rt.root)
{
rt.root = nullptr;
}
void swap(BSPTree &rt)
{
auto temp = root;
root = rt.root;
rt.root = temp;
}
const BSPTree &operator =(const BSPTree &rt)
{
if(rt.root == root)
return *this;
freeNode(root);
root = dupNode(rt.root);
return *this;
}
const BSPTree &operator =(BSPTree &&rt)
{
swap(rt);
return *this;
}
~BSPTree()
{
freeNode(root);
}
void clear()
{
freeNode(root);
root = nullptr;
}
void insert(Triangle tri)
{
insertTriangle(root, tri);
}
void insert(const vector<Triangle> &triangles)
{
vector<size_t> order;
order.resize(triangles.size());
for(size_t i = 0; i < order.size(); i++)
order[i] = i;
default_random_engine rg;
uniform_int_distribution<size_t> d(0, order.size() - 1);
for(size_t &v : order)
{
std::swap(v, order[d(rg)]);
}
for(size_t index : order)
{
insert(triangles[index]);
}
}
vector<Triangle> getTriangles(vector<Triangle> buffer = vector<Triangle>()) const
{
buffer.clear();
getTriangles(buffer, root);
return std::move(buffer);
}
vector<Triangle> getTriangles(VectorF viewPoint, vector<Triangle> buffer = vector<Triangle>()) const
{
buffer.clear();
getTriangles(buffer, root, viewPoint);
return std::move(buffer);
}
void clipTo(const BSPTree &tree)
{
clipTo(root, tree.root);
}
void invert()
{
if(root)
root->invert();
}
void insert(const BSPTree &tree)
{
merge(tree.root);
}
void transform_helper(Transform tform)
{
transform_helper(tform, root);
}
vector<Triangle> clipTriangles(vector<Triangle> triangles) const
{
return clipTriangles(std::move(triangles), root);
}
};
inline BSPTree csgUnion(BSPTree a, BSPTree b)
{
a.clipTo(b);
b.clipTo(a);
b.invert();
b.clipTo(a);
b.invert();
a.insert(b);
return std::move(a);
}
inline BSPTree csgIntersection(BSPTree a, BSPTree b)
{
a.invert();
b.clipTo(a);
b.invert();
a.clipTo(b);
b.clipTo(a);
a.insert(b);
a.invert();
return std::move(a);
}
inline BSPTree csgDifference(BSPTree a, BSPTree b)
{
a.invert();
a.clipTo(b);
b.clipTo(a);
b.invert();
b.clipTo(a);
b.invert();
a.insert(b);
a.invert();
return std::move(a);
}
inline BSPTree transform(Transform tform, BSPTree tree)
{
tree.transform_helper(tform);
return std::move(tree);
}
#endif // BSP_TREE_H_INCLUDED