rev |
line source |
John@15
|
1 #include <math.h>
|
nuclear@25
|
2 #include <float.h>
|
nuclear@26
|
3 #include <assert.h>
|
nuclear@22
|
4 #include "scene.h"
|
nuclear@27
|
5 #include "ogl.h"
|
nuclear@6
|
6
|
nuclear@26
|
7
|
nuclear@27
|
8 static void draw_kdtree(const KDNode *node, int level = 0);
|
nuclear@27
|
9 static bool build_kdtree(KDNode *kd, int level = 0);
|
nuclear@27
|
10 static float eval_cost(const std::list<const Face*> &faces, const AABBox &aabb, int axis, float par_sarea = 1.0);
|
nuclear@26
|
11 static void free_kdtree(KDNode *node);
|
nuclear@28
|
12 static void kdtree_gpu_flatten(KDNodeGPU *kdbuf, int idx, const KDNode *node, const Face *facebuf);
|
nuclear@27
|
13 static void print_item_counts(const KDNode *node, int level);
|
nuclear@26
|
14
|
nuclear@26
|
15
|
nuclear@26
|
16 static int accel_param[NUM_ACCEL_PARAMS] = {
|
nuclear@28
|
17 40, // max tree depth
|
nuclear@26
|
18 0, // max items per node (0 means ignore limit)
|
nuclear@26
|
19 5, // estimated traversal cost
|
nuclear@26
|
20 15 // estimated interseciton cost
|
nuclear@26
|
21 };
|
nuclear@26
|
22
|
nuclear@26
|
23
|
nuclear@26
|
24 void set_accel_param(int p, int v)
|
nuclear@26
|
25 {
|
nuclear@26
|
26 assert(p >= 0 && p < NUM_ACCEL_PARAMS);
|
nuclear@26
|
27 accel_param[p] = v;
|
nuclear@26
|
28 }
|
nuclear@26
|
29
|
nuclear@26
|
30
|
John@15
|
31 #define FEQ(a, b) (fabs((a) - (b)) < 1e-8)
|
John@15
|
32 bool Face::operator ==(const Face &f) const
|
John@15
|
33 {
|
John@15
|
34 for(int i=0; i<3; i++) {
|
John@15
|
35 for(int j=0; j<3; j++) {
|
John@15
|
36 if(!FEQ(v[i].pos[j], f.v[i].pos[j])) {
|
John@15
|
37 return false;
|
John@15
|
38 }
|
John@15
|
39 if(!FEQ(v[i].normal[j], f.v[i].normal[j])) {
|
John@15
|
40 return false;
|
John@15
|
41 }
|
John@15
|
42 }
|
John@15
|
43 if(!FEQ(normal[i], f.normal[i])) {
|
John@15
|
44 return false;
|
John@15
|
45 }
|
John@15
|
46 }
|
John@15
|
47 return true;
|
John@15
|
48 }
|
John@15
|
49
|
nuclear@25
|
50 float AABBox::calc_surface_area() const
|
nuclear@25
|
51 {
|
nuclear@25
|
52 float area1 = (max[0] - min[0]) * (max[1] - min[1]);
|
nuclear@25
|
53 float area2 = (max[3] - min[3]) * (max[1] - min[1]);
|
nuclear@25
|
54 float area3 = (max[0] - min[0]) * (max[3] - min[3]);
|
nuclear@25
|
55
|
nuclear@25
|
56 return 2.0f * (area1 + area2 + area3);
|
nuclear@25
|
57 }
|
nuclear@25
|
58
|
nuclear@26
|
59 KDNode::KDNode()
|
nuclear@26
|
60 {
|
nuclear@26
|
61 axis = 0;
|
nuclear@26
|
62 pt = 0.0;
|
nuclear@26
|
63 left = right = 0;
|
nuclear@26
|
64 num_faces = 0;
|
nuclear@26
|
65 }
|
nuclear@26
|
66
|
nuclear@25
|
67
|
nuclear@24
|
68 Scene::Scene()
|
nuclear@24
|
69 {
|
nuclear@24
|
70 facebuf = 0;
|
nuclear@24
|
71 num_faces = -1;
|
nuclear@24
|
72 kdtree = 0;
|
nuclear@28
|
73 kdbuf = 0;
|
nuclear@24
|
74 }
|
nuclear@24
|
75
|
nuclear@24
|
76 Scene::~Scene()
|
nuclear@24
|
77 {
|
nuclear@24
|
78 delete [] facebuf;
|
nuclear@28
|
79 delete [] kdbuf;
|
nuclear@28
|
80 free_kdtree(kdtree);
|
nuclear@24
|
81 }
|
nuclear@24
|
82
|
nuclear@13
|
83 bool Scene::add_mesh(Mesh *m)
|
nuclear@13
|
84 {
|
nuclear@13
|
85 // make sure triangles have material ids
|
nuclear@13
|
86 for(size_t i=0; i<m->faces.size(); i++) {
|
nuclear@13
|
87 m->faces[i].matid = m->matid;
|
nuclear@13
|
88 }
|
nuclear@24
|
89
|
nuclear@24
|
90 try {
|
nuclear@24
|
91 meshes.push_back(m);
|
nuclear@24
|
92 }
|
nuclear@24
|
93 catch(...) {
|
nuclear@24
|
94 return false;
|
nuclear@24
|
95 }
|
nuclear@24
|
96
|
nuclear@24
|
97 // invalidate facebuffer and count
|
nuclear@24
|
98 delete [] facebuf;
|
nuclear@24
|
99 facebuf = 0;
|
nuclear@24
|
100 num_faces = -1;
|
nuclear@24
|
101
|
nuclear@13
|
102 return true;
|
nuclear@13
|
103 }
|
nuclear@13
|
104
|
John@14
|
105 int Scene::get_num_meshes() const
|
John@14
|
106 {
|
John@14
|
107 return (int)meshes.size();
|
John@14
|
108 }
|
John@14
|
109
|
nuclear@13
|
110 int Scene::get_num_faces() const
|
nuclear@13
|
111 {
|
nuclear@24
|
112 if(num_faces >= 0) {
|
nuclear@24
|
113 return num_faces;
|
nuclear@24
|
114 }
|
nuclear@24
|
115
|
nuclear@24
|
116 num_faces = 0;
|
nuclear@13
|
117 for(size_t i=0; i<meshes.size(); i++) {
|
nuclear@13
|
118 num_faces += meshes[i]->faces.size();
|
nuclear@13
|
119 }
|
nuclear@13
|
120 return num_faces;
|
nuclear@13
|
121 }
|
nuclear@13
|
122
|
John@14
|
123 int Scene::get_num_materials() const
|
John@14
|
124 {
|
John@14
|
125 return (int)matlib.size();
|
John@14
|
126 }
|
John@14
|
127
|
John@14
|
128 Material *Scene::get_materials()
|
John@14
|
129 {
|
John@14
|
130 if(matlib.empty()) {
|
John@14
|
131 return 0;
|
John@14
|
132 }
|
John@14
|
133 return &matlib[0];
|
John@14
|
134 }
|
John@14
|
135
|
John@14
|
136 const Material *Scene::get_materials() const
|
John@14
|
137 {
|
John@14
|
138 if(matlib.empty()) {
|
John@14
|
139 return 0;
|
John@14
|
140 }
|
John@14
|
141 return &matlib[0];
|
John@14
|
142 }
|
nuclear@24
|
143
|
nuclear@24
|
144 const Face *Scene::get_face_buffer() const
|
nuclear@24
|
145 {
|
nuclear@24
|
146 if(facebuf) {
|
nuclear@24
|
147 return facebuf;
|
nuclear@24
|
148 }
|
nuclear@24
|
149
|
nuclear@24
|
150 int num_meshes = get_num_meshes();
|
nuclear@24
|
151
|
nuclear@24
|
152 printf("constructing face buffer with %d faces (out of %d meshes)\n", get_num_faces(), num_meshes);
|
nuclear@24
|
153 facebuf = new Face[num_faces];
|
nuclear@24
|
154 Face *fptr = facebuf;
|
nuclear@24
|
155
|
nuclear@24
|
156 for(int i=0; i<num_meshes; i++) {
|
nuclear@24
|
157 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
|
nuclear@24
|
158 *fptr++ = meshes[i]->faces[j];
|
nuclear@24
|
159 }
|
nuclear@24
|
160 }
|
nuclear@24
|
161 return facebuf;
|
nuclear@24
|
162 }
|
nuclear@24
|
163
|
nuclear@28
|
164 const KDNodeGPU *Scene::get_kdtree_buffer() const
|
nuclear@28
|
165 {
|
nuclear@28
|
166 if(kdbuf) {
|
nuclear@28
|
167 return kdbuf;
|
nuclear@28
|
168 }
|
nuclear@28
|
169
|
nuclear@28
|
170 if(!kdtree) {
|
nuclear@28
|
171 ((Scene*)this)->build_kdtree();
|
nuclear@28
|
172 }
|
nuclear@28
|
173
|
nuclear@29
|
174 int max_nodes = (int)pow(2, kdtree_depth(kdtree)) - 1;
|
nuclear@29
|
175 printf("allocating storage for the complete tree (%d)\n", max_nodes);
|
nuclear@28
|
176
|
nuclear@29
|
177 kdbuf = new KDNodeGPU[max_nodes + 1];
|
nuclear@28
|
178 kdtree_gpu_flatten(kdbuf, 1, kdtree, get_face_buffer());
|
nuclear@28
|
179 return kdbuf;
|
nuclear@28
|
180 }
|
nuclear@28
|
181
|
nuclear@29
|
182 static int ipow(int x, int n)
|
nuclear@28
|
183 {
|
nuclear@29
|
184 assert(n >= 0);
|
nuclear@29
|
185
|
nuclear@29
|
186 int res = 1;
|
nuclear@29
|
187 for(int i=0; i<n; i++) {
|
nuclear@29
|
188 res *= x;
|
nuclear@28
|
189 }
|
nuclear@29
|
190 return res;
|
nuclear@28
|
191 }
|
nuclear@28
|
192
|
nuclear@29
|
193 int Scene::get_kdtree_buffer_size() const
|
nuclear@29
|
194 {
|
nuclear@29
|
195 // 2**depth - 1 nodes for the complete tree + 1 for the unused heap item 0.
|
nuclear@29
|
196 return ipow(2, kdtree_depth(kdtree)) * sizeof(KDNodeGPU);
|
nuclear@29
|
197 }
|
nuclear@24
|
198
|
nuclear@27
|
199 void Scene::draw_kdtree() const
|
nuclear@27
|
200 {
|
nuclear@27
|
201 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@27
|
202 glDisable(GL_LIGHTING);
|
nuclear@27
|
203 glDepthMask(0);
|
nuclear@27
|
204
|
nuclear@27
|
205 glBegin(GL_LINES);
|
nuclear@27
|
206 ::draw_kdtree(kdtree, 0);
|
nuclear@27
|
207 glEnd();
|
nuclear@27
|
208
|
nuclear@27
|
209 glDepthMask(1);
|
nuclear@27
|
210 glPopAttrib();
|
nuclear@27
|
211 }
|
nuclear@27
|
212
|
nuclear@27
|
213 static float palette[][3] = {
|
nuclear@27
|
214 {0, 1, 0},
|
nuclear@27
|
215 {1, 0, 0},
|
nuclear@27
|
216 {0, 0, 1},
|
nuclear@27
|
217 {1, 1, 0},
|
nuclear@27
|
218 {0, 0, 1},
|
nuclear@27
|
219 {1, 0, 1}
|
nuclear@27
|
220 };
|
nuclear@27
|
221 static int pal_size = sizeof palette / sizeof *palette;
|
nuclear@27
|
222
|
nuclear@27
|
223 static void draw_kdtree(const KDNode *node, int level)
|
nuclear@27
|
224 {
|
nuclear@27
|
225 if(!node) return;
|
nuclear@27
|
226
|
nuclear@27
|
227 draw_kdtree(node->left, level + 1);
|
nuclear@27
|
228 draw_kdtree(node->right, level + 1);
|
nuclear@27
|
229
|
nuclear@27
|
230 glColor3fv(palette[level % pal_size]);
|
nuclear@27
|
231
|
nuclear@27
|
232 glVertex3fv(node->aabb.min);
|
nuclear@27
|
233 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.min[2]);
|
nuclear@27
|
234 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.min[2]);
|
nuclear@27
|
235 glVertex3f(node->aabb.max[0], node->aabb.max[1], node->aabb.min[2]);
|
nuclear@27
|
236 glVertex3f(node->aabb.max[0], node->aabb.max[1], node->aabb.min[2]);
|
nuclear@27
|
237 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.min[2]);
|
nuclear@27
|
238 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.min[2]);
|
nuclear@27
|
239 glVertex3fv(node->aabb.min);
|
nuclear@27
|
240
|
nuclear@27
|
241 glVertex3f(node->aabb.min[0], node->aabb.min[1], node->aabb.max[2]);
|
nuclear@27
|
242 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.max[2]);
|
nuclear@27
|
243 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.max[2]);
|
nuclear@27
|
244 glVertex3fv(node->aabb.max);
|
nuclear@27
|
245 glVertex3fv(node->aabb.max);
|
nuclear@27
|
246 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.max[2]);
|
nuclear@27
|
247 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.max[2]);
|
nuclear@27
|
248 glVertex3f(node->aabb.min[0], node->aabb.min[1], node->aabb.max[2]);
|
nuclear@27
|
249
|
nuclear@27
|
250 glVertex3fv(node->aabb.min);
|
nuclear@27
|
251 glVertex3f(node->aabb.min[0], node->aabb.min[1], node->aabb.max[2]);
|
nuclear@27
|
252 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.min[2]);
|
nuclear@27
|
253 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.max[2]);
|
nuclear@27
|
254 glVertex3f(node->aabb.max[0], node->aabb.max[1], node->aabb.min[2]);
|
nuclear@27
|
255 glVertex3fv(node->aabb.max);
|
nuclear@27
|
256 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.min[2]);
|
nuclear@27
|
257 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.max[2]);
|
nuclear@27
|
258 }
|
nuclear@27
|
259
|
nuclear@27
|
260 bool Scene::build_kdtree()
|
nuclear@24
|
261 {
|
nuclear@29
|
262 assert(kdtree == 0);
|
nuclear@29
|
263
|
nuclear@24
|
264 const Face *faces = get_face_buffer();
|
nuclear@24
|
265 int num_faces = get_num_faces();
|
nuclear@24
|
266
|
nuclear@25
|
267 printf("Constructing kd-tree out of %d faces ...\n", num_faces);
|
nuclear@25
|
268
|
nuclear@27
|
269 int icost = accel_param[ACCEL_PARAM_COST_INTERSECT];
|
nuclear@27
|
270 int tcost = accel_param[ACCEL_PARAM_COST_TRAVERSE];
|
nuclear@27
|
271 printf(" max items per leaf: %d\n", accel_param[ACCEL_PARAM_MAX_NODE_ITEMS]);
|
nuclear@27
|
272 printf(" SAH parameters - tcost: %d - icost: %d\n", tcost, icost);
|
nuclear@27
|
273
|
nuclear@25
|
274 free_kdtree(kdtree);
|
nuclear@25
|
275 kdtree = new KDNode;
|
nuclear@25
|
276
|
nuclear@25
|
277 /* Start the construction of the kdtree by adding all faces of the scene
|
nuclear@25
|
278 * to the new root node. At the same time calculate the root's AABB.
|
nuclear@25
|
279 */
|
nuclear@25
|
280 kdtree->aabb.min[0] = kdtree->aabb.min[1] = kdtree->aabb.min[2] = FLT_MAX;
|
nuclear@25
|
281 kdtree->aabb.max[0] = kdtree->aabb.max[1] = kdtree->aabb.max[2] = -FLT_MAX;
|
nuclear@25
|
282
|
nuclear@24
|
283 for(int i=0; i<num_faces; i++) {
|
nuclear@25
|
284 const Face *face = faces + i;
|
nuclear@25
|
285
|
nuclear@25
|
286 // for each vertex of the face ...
|
nuclear@25
|
287 for(int j=0; j<3; j++) {
|
nuclear@25
|
288 const float *pos = face->v[j].pos;
|
nuclear@25
|
289
|
nuclear@25
|
290 // for each element (xyz) of the position vector ...
|
nuclear@25
|
291 for(int k=0; k<3; k++) {
|
nuclear@25
|
292 if(pos[k] < kdtree->aabb.min[k]) {
|
nuclear@25
|
293 kdtree->aabb.min[k] = pos[k];
|
nuclear@25
|
294 }
|
nuclear@25
|
295 if(pos[k] > kdtree->aabb.max[k]) {
|
nuclear@25
|
296 kdtree->aabb.max[k] = pos[k];
|
nuclear@25
|
297 }
|
nuclear@25
|
298 }
|
nuclear@25
|
299 }
|
nuclear@25
|
300
|
nuclear@25
|
301 kdtree->faces.push_back(face); // add the face
|
nuclear@26
|
302 kdtree->num_faces++;
|
nuclear@24
|
303 }
|
nuclear@24
|
304
|
nuclear@26
|
305 // calculate the heuristic for the root
|
nuclear@26
|
306 kdtree->cost = eval_cost(kdtree->faces, kdtree->aabb, kdtree->axis);
|
nuclear@26
|
307
|
nuclear@25
|
308 // now proceed splitting the root recursively
|
nuclear@27
|
309 if(!::build_kdtree(kdtree)) {
|
nuclear@27
|
310 fprintf(stderr, "failed to build kdtree\n");
|
nuclear@27
|
311 return false;
|
nuclear@27
|
312 }
|
nuclear@27
|
313
|
nuclear@27
|
314 printf(" tree depth: %d\n", kdtree_depth(kdtree));
|
nuclear@27
|
315 print_item_counts(kdtree, 0);
|
nuclear@27
|
316 return true;
|
nuclear@24
|
317 }
|
nuclear@24
|
318
|
nuclear@27
|
319 static bool build_kdtree(KDNode *kd, int level)
|
nuclear@24
|
320 {
|
nuclear@28
|
321 int opt_max_depth = accel_param[ACCEL_PARAM_MAX_TREE_DEPTH];
|
nuclear@26
|
322 int opt_max_items = accel_param[ACCEL_PARAM_MAX_NODE_ITEMS];
|
nuclear@27
|
323 int tcost = accel_param[ACCEL_PARAM_COST_TRAVERSE];
|
nuclear@27
|
324
|
nuclear@28
|
325 if(kd->num_faces == 0 || level >= opt_max_depth) {
|
nuclear@27
|
326 return true;
|
nuclear@25
|
327 }
|
nuclear@25
|
328
|
nuclear@27
|
329 int axis = level % 3;
|
nuclear@27
|
330 //float parent_sa = kd->aabb.calc_surface_area();
|
nuclear@26
|
331
|
nuclear@26
|
332 float best_cost[2], best_sum_cost = FLT_MAX;
|
nuclear@26
|
333 float best_split;
|
nuclear@26
|
334
|
nuclear@26
|
335 std::list<const Face*>::iterator it = kd->faces.begin();
|
nuclear@26
|
336 while(it != kd->faces.end()) {
|
nuclear@26
|
337 const Face *face = *it++;
|
nuclear@26
|
338
|
nuclear@26
|
339 for(int i=0; i<3; i++) {
|
nuclear@26
|
340 AABBox aabb_left, aabb_right;
|
nuclear@26
|
341 const float *split = face->v[i].pos;
|
nuclear@26
|
342
|
nuclear@26
|
343 aabb_left = aabb_right = kd->aabb;
|
nuclear@26
|
344 aabb_left.max[axis] = split[axis];
|
nuclear@26
|
345 aabb_right.min[axis] = split[axis];
|
nuclear@26
|
346
|
nuclear@26
|
347 float left_cost = eval_cost(kd->faces, aabb_left, axis);
|
nuclear@26
|
348 float right_cost = eval_cost(kd->faces, aabb_right, axis);
|
nuclear@27
|
349 float sum_cost = left_cost + right_cost - tcost; // tcost is added twice
|
nuclear@26
|
350
|
nuclear@26
|
351 if(sum_cost < best_sum_cost) {
|
nuclear@26
|
352 best_cost[0] = left_cost;
|
nuclear@26
|
353 best_cost[1] = right_cost;
|
nuclear@26
|
354 best_sum_cost = sum_cost;
|
nuclear@26
|
355 best_split = split[axis];
|
nuclear@26
|
356 }
|
nuclear@26
|
357 }
|
nuclear@26
|
358 }
|
nuclear@26
|
359
|
nuclear@29
|
360 //printf("current cost: %f, best_cost: %f\n", kd->cost, best_sum_cost);
|
nuclear@27
|
361 if(best_sum_cost > kd->cost && (opt_max_items == 0 || kd->num_faces <= opt_max_items)) {
|
nuclear@27
|
362 return true; // stop splitting if it doesn't reduce the cost
|
nuclear@26
|
363 }
|
nuclear@26
|
364 kd->pt = best_split;
|
nuclear@26
|
365
|
nuclear@26
|
366 // create the two children
|
nuclear@26
|
367 KDNode *kdleft, *kdright;
|
nuclear@26
|
368 kdleft = new KDNode;
|
nuclear@26
|
369 kdright = new KDNode;
|
nuclear@26
|
370
|
nuclear@26
|
371 kdleft->aabb = kdright->aabb = kd->aabb;
|
nuclear@26
|
372
|
nuclear@26
|
373 kdleft->aabb.max[axis] = best_split;
|
nuclear@26
|
374 kdright->aabb.min[axis] = best_split;
|
nuclear@26
|
375
|
nuclear@26
|
376 kdleft->cost = best_cost[0];
|
nuclear@26
|
377 kdright->cost = best_cost[1];
|
nuclear@26
|
378
|
nuclear@27
|
379 //kdleft->axis = kdright->axis = (axis + 1) % 3;
|
nuclear@27
|
380
|
nuclear@26
|
381 it = kd->faces.begin();
|
nuclear@26
|
382 while(it != kd->faces.end()) {
|
nuclear@26
|
383 const Face *face = *it++;
|
nuclear@26
|
384
|
nuclear@26
|
385 if(face->v[0].pos[axis] < best_split ||
|
nuclear@26
|
386 face->v[1].pos[axis] < best_split ||
|
nuclear@26
|
387 face->v[2].pos[axis] < best_split) {
|
nuclear@26
|
388 kdleft->faces.push_back(face);
|
nuclear@26
|
389 kdleft->num_faces++;
|
nuclear@26
|
390 }
|
nuclear@26
|
391 if(face->v[0].pos[axis] >= best_split ||
|
nuclear@26
|
392 face->v[1].pos[axis] >= best_split ||
|
nuclear@26
|
393 face->v[2].pos[axis] >= best_split) {
|
nuclear@26
|
394 kdright->faces.push_back(face);
|
nuclear@26
|
395 kdright->num_faces++;
|
nuclear@26
|
396 }
|
nuclear@26
|
397 }
|
nuclear@27
|
398 kd->faces.clear(); // only leaves have faces
|
nuclear@26
|
399
|
nuclear@26
|
400 kd->left = kdleft;
|
nuclear@26
|
401 kd->right = kdright;
|
nuclear@27
|
402
|
nuclear@27
|
403 return build_kdtree(kd->left, level + 1) && build_kdtree(kd->right, level + 1);
|
nuclear@26
|
404 }
|
nuclear@26
|
405
|
nuclear@27
|
406 static float eval_cost(const std::list<const Face*> &faces, const AABBox &aabb, int axis, float par_sarea)
|
nuclear@26
|
407 {
|
nuclear@26
|
408 int num_inside = 0;
|
nuclear@26
|
409 int tcost = accel_param[ACCEL_PARAM_COST_TRAVERSE];
|
nuclear@26
|
410 int icost = accel_param[ACCEL_PARAM_COST_INTERSECT];
|
nuclear@26
|
411
|
nuclear@26
|
412 std::list<const Face*>::const_iterator it = faces.begin();
|
nuclear@26
|
413 while(it != faces.end()) {
|
nuclear@26
|
414 const Face *face = *it++;
|
nuclear@26
|
415
|
nuclear@26
|
416 for(int i=0; i<3; i++) {
|
nuclear@26
|
417 if(face->v[i].pos[axis] >= aabb.min[axis] && face->v[i].pos[axis] < aabb.max[axis]) {
|
nuclear@26
|
418 num_inside++;
|
nuclear@26
|
419 break;
|
nuclear@26
|
420 }
|
nuclear@26
|
421 }
|
nuclear@26
|
422 }
|
nuclear@26
|
423
|
nuclear@27
|
424 float sarea = aabb.calc_surface_area();
|
nuclear@27
|
425 if(sarea < 1e-8) {
|
nuclear@27
|
426 return FLT_MAX; // heavily penalize 0-area voxels
|
nuclear@27
|
427 }
|
nuclear@27
|
428
|
nuclear@27
|
429 return tcost + (sarea / par_sarea) * num_inside * icost;
|
nuclear@24
|
430 }
|
nuclear@25
|
431
|
nuclear@25
|
432 static void free_kdtree(KDNode *node)
|
nuclear@25
|
433 {
|
nuclear@25
|
434 if(node) {
|
nuclear@25
|
435 free_kdtree(node->left);
|
nuclear@25
|
436 free_kdtree(node->right);
|
nuclear@25
|
437 delete node;
|
nuclear@25
|
438 }
|
nuclear@25
|
439 }
|
nuclear@27
|
440
|
nuclear@28
|
441 int kdtree_depth(const KDNode *node)
|
nuclear@27
|
442 {
|
nuclear@27
|
443 if(!node) return 0;
|
nuclear@27
|
444
|
nuclear@27
|
445 int left = kdtree_depth(node->left);
|
nuclear@27
|
446 int right = kdtree_depth(node->right);
|
nuclear@27
|
447 return (left > right ? left : right) + 1;
|
nuclear@27
|
448 }
|
nuclear@27
|
449
|
nuclear@28
|
450 int kdtree_nodes(const KDNode *node)
|
nuclear@28
|
451 {
|
nuclear@28
|
452 if(!node) return 0;
|
nuclear@28
|
453 return kdtree_nodes(node->left) + kdtree_nodes(node->right) + 1;
|
nuclear@28
|
454 }
|
nuclear@28
|
455
|
nuclear@28
|
456 #define MAX_FACES (sizeof dest->face_idx / sizeof *dest->face_idx)
|
nuclear@28
|
457 static void kdtree_gpu_flatten(KDNodeGPU *kdbuf, int idx, const KDNode *node, const Face *facebuf)
|
nuclear@28
|
458 {
|
nuclear@28
|
459 KDNodeGPU *dest = kdbuf + idx;
|
nuclear@28
|
460
|
nuclear@28
|
461 dest->aabb = node->aabb;
|
nuclear@28
|
462 dest->num_faces = 0;
|
nuclear@28
|
463
|
nuclear@28
|
464 std::list<const Face*>::const_iterator it = node->faces.begin();
|
nuclear@28
|
465 while(it != node->faces.end()) {
|
nuclear@28
|
466 if(dest->num_faces >= (int)MAX_FACES) {
|
nuclear@28
|
467 fprintf(stderr, "kdtree_gpu_flatten WARNING: more than %d faces in node, skipping!\n", (int)MAX_FACES);
|
nuclear@28
|
468 break;
|
nuclear@28
|
469 }
|
nuclear@29
|
470 dest->face_idx[dest->num_faces++] = *it++ - facebuf;
|
nuclear@28
|
471 }
|
nuclear@28
|
472
|
nuclear@28
|
473 if(node->left) {
|
nuclear@28
|
474 assert(node->right);
|
nuclear@31
|
475 assert(!dest->num_faces);
|
nuclear@31
|
476
|
nuclear@31
|
477 dest->num_faces = -1;
|
nuclear@31
|
478
|
nuclear@28
|
479 kdtree_gpu_flatten(kdbuf, idx * 2, node->left, facebuf);
|
nuclear@28
|
480 kdtree_gpu_flatten(kdbuf, idx * 2 + 1, node->right, facebuf);
|
nuclear@28
|
481 }
|
nuclear@28
|
482 }
|
nuclear@28
|
483
|
nuclear@27
|
484 static void print_item_counts(const KDNode *node, int level)
|
nuclear@27
|
485 {
|
nuclear@27
|
486 if(!node) return;
|
nuclear@27
|
487
|
nuclear@30
|
488 for(int i=0; i<level; i++) {
|
nuclear@27
|
489 fputs(" ", stdout);
|
nuclear@27
|
490 }
|
nuclear@30
|
491 printf("- %d (cost: %f)\n", node->num_faces, node->cost);
|
nuclear@27
|
492
|
nuclear@27
|
493 print_item_counts(node->left, level + 1);
|
nuclear@27
|
494 print_item_counts(node->right, level + 1);
|
nuclear@27
|
495 }
|