-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbvh.cpp
executable file
·355 lines (288 loc) · 9.17 KB
/
bvh.cpp
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Originally from the SELF-CCD library by Min Tang and Dinesh Manocha
// (http://gamma.cs.unc.edu/SELFCD/).
// Modified by Rahul Narain.
/*************************************************************************\
Copyright 2010 The University of North Carolina at Chapel Hill.
All Rights Reserved.
Permission to use, copy, modify and distribute this software and its
documentation for educational, research and non-profit purposes, without
fee, and without a written agreement is hereby granted, provided that the
above copyright notice and the following three paragraphs appear in all
copies.
IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL BE
LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE
USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY
OF NORTH CAROLINA HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
THE UNIVERSITY OF NORTH CAROLINA SPECIFICALLY DISCLAIM ANY
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
NORTH CAROLINA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
The authors may be contacted via:
US Mail: GAMMA Research Group at UNC
Department of Computer Science
Sitterson Hall, CB #3175
University of N. Carolina
Chapel Hill, NC 27599-3175
Phone: (919)962-1749
EMail: [email protected]; [email protected]
\**************************************************************************/
#include <assert.h>
#include <stdlib.h>
#include "bvh.hpp"
#include "collision.hpp"
#include "mesh.hpp"
#include <climits>
#include <utility>
using namespace std;
BOX node_box(const Node *node, bool ccd) {
BOX box;
box += node->x;
if (ccd)
box += node->x0;
return box;
}
BOX vert_box(const Vert *vert, bool ccd) { return node_box(vert->node, ccd); }
BOX edge_box(const Edge *edge, bool ccd) {
BOX box;
box += node_box(edge->n[0], ccd);
box += node_box(edge->n[1], ccd);
return box;
}
BOX face_box(const Face *face, bool ccd) {
BOX box;
for (int v = 0; v < 3; v++)
box += vert_box(face->v[v], ccd);
return box;
}
BOX dilate(const BOX &box, double d) {
static double sqrt2 = sqrt(2);
BOX dbox = box;
for (int i = 0; i < 3; i++) {
dbox._dist[i] -= d;
dbox._dist[i + 9] += d;
}
for (int i = 0; i < 6; i++) {
dbox._dist[3 + i] -= sqrt2 * d;
dbox._dist[3 + i + 9] += sqrt2 * d;
}
return dbox;
}
bool overlap(const BOX &box0, const BOX &box1, float thickness) {
return box0.overlaps(dilate(box1, thickness));
}
float DeformBVHTree::refit() {
getRoot()->refit(_ccd);
return 0.f;
}
BOX DeformBVHTree::box() { return getRoot()->_box; }
inline vec3f norm(vec3f &p1, vec3f &p2, vec3f &p3) {
vec3f s = p2 - p1;
vec3f t = p3 - p1;
vec3f n = cross(s, t);
return n;
}
void DeformBVHNode::refit(bool ccd) {
if (isLeaf()) {
_box = face_box(getFace(), ccd);
} else {
getLeftChild()->refit(ccd);
getRightChild()->refit(ccd);
_box = getLeftChild()->_box + getRightChild()->_box;
}
}
bool DeformBVHNode::find(Face *face) {
if (isLeaf())
return getFace() == face;
if (getLeftChild()->find(face))
return true;
if (getRightChild()->find(face))
return true;
return false;
}
inline float middle_xyz(char xyz, const vec3f &p1, const vec3f &p2,
const vec3f &p3) {
float t0, t1;
t0 = MIN(p1[xyz], p2[xyz]);
t0 = MIN(t0, p3[xyz]);
t1 = MAX(p1[xyz], p2[xyz]);
t1 = MAX(t1, p3[xyz]);
return (t0 + t1) * 0.5f;
}
class aap {
public:
char _xyz;
float _p;
FORCEINLINE aap(const BOX &total) {
vec3f center = total.center();
char xyz = 2;
if (total.width() >= total.height() && total.width() >= total.depth()) {
xyz = 0;
} else if (total.height() >= total.width() &&
total.height() >= total.depth()) {
xyz = 1;
}
_xyz = xyz;
_p = center[xyz];
}
FORCEINLINE bool inside(const vec3f &mid) const { return mid[_xyz] > _p; }
};
extern float middle_xyz(char xyz, const vec3f &p1, const vec3f &p2,
const vec3f &p3);
DeformBVHTree::DeformBVHTree(DeformModel &mdl, bool ccd) {
_mdl = &mdl;
_ccd = ccd;
if (!mdl.verts.empty())
Construct();
else
_root = NULL;
}
void DeformBVHTree::Construct() {
BOX total;
int count;
int num_vtx = _mdl->verts.size(), num_tri = _mdl->faces.size();
for (unsigned int i = 0; i < num_vtx; i++) {
if (_mdl->verts[i]->node == nullptr) {
std::cout<<"Warning: (remesh bug?) null ptr vert "<< i << "\n";
continue;
}
total += _mdl->verts[i]->node->x;
if (_ccd)
total += _mdl->verts[i]->node->x0;
}
count = num_tri;
BOX *tri_boxes = new BOX[count];
vec3f *tri_centers = new vec3f[count];
aap pln(total);
face_buffer = new Face *[count];
unsigned int left_idx = 0, right_idx = count;
unsigned int tri_idx = 0;
for (unsigned int i = 0; i < num_tri; i++) {
tri_idx++;
vec3f &p1 = _mdl->faces[i]->v[0]->node->x;
vec3f &p2 = _mdl->faces[i]->v[1]->node->x;
vec3f &p3 = _mdl->faces[i]->v[2]->node->x;
vec3f &pp1 = _mdl->faces[i]->v[0]->node->x0;
vec3f &pp2 = _mdl->faces[i]->v[1]->node->x0;
vec3f &pp3 = _mdl->faces[i]->v[2]->node->x0;
if (_ccd) {
tri_centers[tri_idx - 1] = vec3f(
(middle_xyz(0, p1, p2, p3) + middle_xyz(0, pp1, pp2, pp3)) * 0.5f,
(middle_xyz(1, p1, p2, p3) + middle_xyz(1, pp1, pp2, pp3)) * 0.5f,
(middle_xyz(2, p1, p2, p3) + middle_xyz(2, pp1, pp2, pp3)) * 0.5f);
} else {
tri_centers[tri_idx - 1] =
vec3f(middle_xyz(0, p1, p2, p3), middle_xyz(1, p1, p2, p3),
middle_xyz(2, p1, p2, p3));
}
if (pln.inside(tri_centers[tri_idx - 1]))
face_buffer[left_idx++] = _mdl->faces[i];
else
face_buffer[--right_idx] = _mdl->faces[i];
tri_boxes[tri_idx - 1] += p1;
tri_boxes[tri_idx - 1] += p2;
tri_boxes[tri_idx - 1] += p3;
if (_ccd) {
tri_boxes[tri_idx - 1] += pp1;
tri_boxes[tri_idx - 1] += pp2;
tri_boxes[tri_idx - 1] += pp3;
}
}
_root = new DeformBVHNode();
_root->_box = total;
//_root->_count = count;
if (count == 1) {
_root->_face = _mdl->faces[0];
_root->_left = _root->_right = NULL;
} else {
if (left_idx == 0 || left_idx == count)
left_idx = count / 2;
_root->_left =
new DeformBVHNode(_root, face_buffer, left_idx, tri_boxes, tri_centers);
_root->_right = new DeformBVHNode(_root, face_buffer + left_idx,
count - left_idx, tri_boxes, tri_centers);
}
delete[] tri_boxes;
delete[] tri_centers;
}
DeformBVHTree::~DeformBVHTree() {
if (!_root)
return;
delete _root;
delete[] face_buffer;
}
//#################################################################
// called by root
DeformBVHNode::DeformBVHNode() {
_face = NULL;
_left = _right = NULL;
_parent = NULL;
//_count = 0;
_active = true;
}
DeformBVHNode::~DeformBVHNode() {
if (_left)
delete _left;
if (_right)
delete _right;
}
// called by leaf
DeformBVHNode::DeformBVHNode(DeformBVHNode *parent, Face *face, BOX *tri_boxes,
vec3f *tri_centers) {
_left = _right = NULL;
_parent = parent;
_face = face;
_box = tri_boxes[face->index];
//_count = 1;
_active = true;
}
// called by nodes
DeformBVHNode::DeformBVHNode(DeformBVHNode *parent, Face **lst,
unsigned int lst_num, BOX *tri_boxes,
vec3f *tri_centers) {
assert(lst_num > 0);
_left = _right = NULL;
_parent = parent;
_face = NULL;
//_count = lst_num;
_active = true;
if (lst_num == 1) {
_face = lst[0];
_box = tri_boxes[lst[0]->index];
} else { // try to split them
for (unsigned int t = 0; t < lst_num; t++) {
int i = lst[t]->index;
_box += tri_boxes[i];
}
if (lst_num == 2) { // must split it!
_left = new DeformBVHNode(this, lst[0], tri_boxes, tri_centers);
_right = new DeformBVHNode(this, lst[1], tri_boxes, tri_centers);
} else {
aap pln(_box);
unsigned int left_idx = 0, right_idx = lst_num - 1;
for (unsigned int t = 0; t < lst_num; t++) {
int i = lst[left_idx]->index;
if (pln.inside(tri_centers[i]))
left_idx++;
else { // swap it
Face *tmp = lst[left_idx];
lst[left_idx] = lst[right_idx];
lst[right_idx--] = tmp;
}
}
int hal = lst_num / 2;
if (left_idx == 0 || left_idx == lst_num) {
_left = new DeformBVHNode(this, lst, hal, tri_boxes, tri_centers);
_right = new DeformBVHNode(this, lst + hal, lst_num - hal, tri_boxes,
tri_centers);
} else {
_left = new DeformBVHNode(this, lst, left_idx, tri_boxes, tri_centers);
_right = new DeformBVHNode(this, lst + left_idx, lst_num - left_idx,
tri_boxes, tri_centers);
}
}
}
}