-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbah.cpp
executable file
·208 lines (188 loc) · 6.03 KB
/
bah.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
/*
Copyright ©2013 The Regents of the University of California
(Regents). All Rights Reserved. Permission to use, copy, modify, and
distribute this software and its documentation for educational,
research, and not-for-profit purposes, without fee and without a
signed licensing agreement, is hereby granted, provided that the
above copyright notice, this paragraph and the following two
paragraphs appear in all copies, modifications, and
distributions. Contact The Office of Technology Licensing, UC
Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620,
(510) 643-7201, for commercial licensing opportunities.
IN NO EVENT SHALL REGENTS 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 REGENTS HAS BEEN ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "bah.hpp"
Box &Box::operator+= (const Vec2 &u) {
umin = vec_min(umin, u);
umax = vec_max(umax, u);
return *this;
}
Box &Box::operator+= (const Box &box) {
umin = vec_min(umin, box.umin);
umax = vec_max(umax, box.umax);
return *this;
}
bool Box::overlaps (const Box &box) const {
for (int i = 0; i < 2; i++) {
if (umin[i] > box.umax[i]) return false;
if (umax[i] < box.umin[i]) return false;
}
return true;
}
Vec2 Box::size () const {
return umax - umin;
}
Vec2 Box::center () const {
return (umin + umax)/2.;
}
Box vert_box (const Vert *vert) {
return Box(vert->u);
}
Box face_box (const Face *face) {
Box box;
for (int v = 0; v < 3; v++)
box += face->v[v]->u;
return box;
}
struct Aap {
int xy;
float p;
Aap (const Box &total) {
Vec2 center = total.center();
Vec2 size = total.size();
xy = (size[0]>=size[1]) ? 0 : 1;
p = center[xy];
}
bool inside (const Vec2 &mid) const {
return mid[xy] > p;
}
};
BahNode *new_bah_tree (const Mesh &mesh) {
Box total;
int count;
int num_vtx = mesh.verts.size(),
num_tri = mesh.faces.size();
for (unsigned int i=0; i<num_vtx; i++)
total += mesh.verts[i]->u;
count = num_tri;
Box *tri_boxes = new Box[count];
Vec2 *tri_centers = new Vec2[count];
Aap pln(total);
Face **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++) {
Vec2 &p1 = mesh.faces[i]->v[0]->u;
Vec2 &p2 = mesh.faces[i]->v[1]->u;
Vec2 &p3 = mesh.faces[i]->v[2]->u;
tri_centers[i] = (p1 + p2 + p3)/3.;
if (pln.inside(tri_centers[i]))
face_buffer[left_idx++] = mesh.faces[i];
else
face_buffer[--right_idx] = mesh.faces[i];
tri_boxes[i] += p1;
tri_boxes[i] += p2;
tri_boxes[i] += p3;
}
BahNode *root = new BahNode;
root->box = total;
if (count == 1) {
root->face = mesh.faces[0];
root->left = root->right = NULL;
} else {
if (left_idx == 0 || left_idx == count)
left_idx = count/2;
root->left = new BahNode(root, face_buffer, left_idx,
tri_boxes, tri_centers);
root->right = new BahNode(root, face_buffer+left_idx, count-left_idx,
tri_boxes, tri_centers);
}
delete [] tri_boxes;
delete [] tri_centers;
delete [] face_buffer;
return root;
}
BahNode::BahNode ():
face(NULL), parent(NULL), left(NULL), right(NULL) {
}
BahNode::~BahNode () {
if (left) delete left;
if (right) delete right;
}
BahNode::BahNode (BahNode *parent, Face *face, const Box &box):
box(box), face(face), parent(parent), left(NULL), right(NULL) {
}
BahNode::BahNode (BahNode *parent, Face **lst, unsigned int lst_num,
Box *tri_boxes, Vec2 *tri_centers) {
// BahNode *node = new BahNode;
assert(lst_num > 0);
left = right = NULL;
parent = parent;
face = NULL;
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 BahNode(this, lst[0], tri_boxes[lst[0]->index]);
right = new BahNode(this, lst[1], tri_boxes[lst[1]->index]);
} 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 BahNode(this, lst, hal, tri_boxes, tri_centers);
right = new BahNode(this, lst+hal, lst_num-hal,
tri_boxes, tri_centers);
} else {
left = new BahNode(this, lst, left_idx, tri_boxes, tri_centers);
right = new BahNode(this, lst+left_idx, lst_num-left_idx,
tri_boxes, tri_centers);
}
}
}
}
void delete_bah_tree (BahNode *root) {
delete root;
}
void for_overlapping_faces (Face *face, const Box &box, const BahNode *node,
BahCallback callback);
void for_overlapping_faces (Face *face, const BahNode *node,
BahCallback callback) {
for_overlapping_faces(face, face_box(face), node, callback);
}
void for_overlapping_faces (Face *face, const Box &box, const BahNode *node,
BahCallback callback) {
if (!box.overlaps(node->box))
return;
if (node->face) {
callback(face, node->face);
} else {
for_overlapping_faces(face, box, node->left, callback);
for_overlapping_faces(face, box, node->right, callback);
}
}