clray

annotate src/scene.cc @ 55:df239a52a091

extensive render stats for the CPU raytracer
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Sep 2010 03:00:21 +0100
parents 54a96b738afe
children 3d13924b22e6
rev   line source
nuclear@35 1 #include <stdlib.h>
John@15 2 #include <math.h>
nuclear@25 3 #include <float.h>
nuclear@26 4 #include <assert.h>
nuclear@35 5 #include <map>
nuclear@22 6 #include "scene.h"
nuclear@27 7 #include "ogl.h"
nuclear@6 8
nuclear@38 9 #define CHECK_AABB(aabb) \
nuclear@38 10 assert(aabb.max[0] >= aabb.min[0] && aabb.max[1] >= aabb.min[1] && aabb.max[2] >= aabb.min[2])
nuclear@38 11
nuclear@26 12
nuclear@37 13 #define MIN(a, b) ((a) < (b) ? (a) : (b))
nuclear@37 14 #define MAX(a, b) ((a) > (b) ? (a) : (b))
nuclear@37 15
nuclear@37 16
nuclear@53 17 static int flatten_kdtree(const KDNode *node, KDNodeGPU *kdbuf, int *count);
nuclear@27 18 static void draw_kdtree(const KDNode *node, int level = 0);
nuclear@32 19 static bool build_kdtree(KDNode *kd, const Face *faces, int level = 0);
nuclear@32 20 static float eval_cost(const Face *faces, const int *face_idx, int num_faces, const AABBox &aabb, int axis);
nuclear@26 21 static void free_kdtree(KDNode *node);
nuclear@27 22 static void print_item_counts(const KDNode *node, int level);
nuclear@26 23
nuclear@26 24
nuclear@26 25 static int accel_param[NUM_ACCEL_PARAMS] = {
nuclear@44 26 64, // max tree depth
nuclear@44 27 MAX_NODE_FACES, // max items per node (0 means ignore limit)
nuclear@26 28 5, // estimated traversal cost
nuclear@26 29 15 // estimated interseciton cost
nuclear@26 30 };
nuclear@26 31
nuclear@26 32
nuclear@26 33 void set_accel_param(int p, int v)
nuclear@26 34 {
nuclear@26 35 assert(p >= 0 && p < NUM_ACCEL_PARAMS);
nuclear@26 36 accel_param[p] = v;
nuclear@26 37 }
nuclear@26 38
nuclear@26 39
John@15 40 #define FEQ(a, b) (fabs((a) - (b)) < 1e-8)
John@15 41 bool Face::operator ==(const Face &f) const
John@15 42 {
John@15 43 for(int i=0; i<3; i++) {
John@15 44 for(int j=0; j<3; j++) {
John@15 45 if(!FEQ(v[i].pos[j], f.v[i].pos[j])) {
John@15 46 return false;
John@15 47 }
John@15 48 if(!FEQ(v[i].normal[j], f.v[i].normal[j])) {
John@15 49 return false;
John@15 50 }
John@15 51 }
John@15 52 if(!FEQ(normal[i], f.normal[i])) {
John@15 53 return false;
John@15 54 }
John@15 55 }
John@15 56 return true;
John@15 57 }
John@15 58
nuclear@25 59 float AABBox::calc_surface_area() const
nuclear@25 60 {
nuclear@25 61 float area1 = (max[0] - min[0]) * (max[1] - min[1]);
nuclear@25 62 float area2 = (max[3] - min[3]) * (max[1] - min[1]);
nuclear@25 63 float area3 = (max[0] - min[0]) * (max[3] - min[3]);
nuclear@25 64
nuclear@25 65 return 2.0f * (area1 + area2 + area3);
nuclear@25 66 }
nuclear@25 67
nuclear@26 68 KDNode::KDNode()
nuclear@26 69 {
nuclear@26 70 left = right = 0;
nuclear@32 71 cost = 0.0;
nuclear@26 72 }
nuclear@26 73
nuclear@25 74
nuclear@24 75 Scene::Scene()
nuclear@24 76 {
nuclear@24 77 facebuf = 0;
nuclear@24 78 num_faces = -1;
nuclear@24 79 kdtree = 0;
nuclear@28 80 kdbuf = 0;
nuclear@24 81 }
nuclear@24 82
nuclear@24 83 Scene::~Scene()
nuclear@24 84 {
nuclear@24 85 delete [] facebuf;
nuclear@28 86 delete [] kdbuf;
nuclear@28 87 free_kdtree(kdtree);
nuclear@24 88 }
nuclear@24 89
nuclear@13 90 bool Scene::add_mesh(Mesh *m)
nuclear@13 91 {
nuclear@13 92 // make sure triangles have material ids
nuclear@13 93 for(size_t i=0; i<m->faces.size(); i++) {
nuclear@13 94 m->faces[i].matid = m->matid;
nuclear@13 95 }
nuclear@24 96
nuclear@24 97 try {
nuclear@24 98 meshes.push_back(m);
nuclear@24 99 }
nuclear@24 100 catch(...) {
nuclear@24 101 return false;
nuclear@24 102 }
nuclear@24 103
nuclear@24 104 // invalidate facebuffer and count
nuclear@24 105 delete [] facebuf;
nuclear@24 106 facebuf = 0;
nuclear@24 107 num_faces = -1;
nuclear@24 108
nuclear@13 109 return true;
nuclear@13 110 }
nuclear@13 111
nuclear@54 112 bool Scene::add_light(const Light &lt)
nuclear@54 113 {
nuclear@54 114 try {
nuclear@54 115 lights.push_back(lt);
nuclear@54 116 }
nuclear@54 117 catch(...) {
nuclear@54 118 return false;
nuclear@54 119 }
nuclear@54 120 return true;
nuclear@54 121 }
nuclear@54 122
John@14 123 int Scene::get_num_meshes() const
John@14 124 {
John@14 125 return (int)meshes.size();
John@14 126 }
John@14 127
nuclear@54 128 int Scene::get_num_lights() const
nuclear@54 129 {
nuclear@54 130 return (int)lights.size();
nuclear@54 131 }
nuclear@54 132
nuclear@13 133 int Scene::get_num_faces() const
nuclear@13 134 {
nuclear@24 135 if(num_faces >= 0) {
nuclear@24 136 return num_faces;
nuclear@24 137 }
nuclear@24 138
nuclear@24 139 num_faces = 0;
nuclear@13 140 for(size_t i=0; i<meshes.size(); i++) {
nuclear@13 141 num_faces += meshes[i]->faces.size();
nuclear@13 142 }
nuclear@13 143 return num_faces;
nuclear@13 144 }
nuclear@13 145
John@14 146 int Scene::get_num_materials() const
John@14 147 {
John@14 148 return (int)matlib.size();
John@14 149 }
John@14 150
nuclear@35 151 int Scene::get_num_kdnodes() const
nuclear@35 152 {
nuclear@35 153 return kdtree_nodes(kdtree);
nuclear@35 154 }
nuclear@35 155
nuclear@54 156 Mesh **Scene::get_meshes()
nuclear@54 157 {
nuclear@54 158 if(meshes.empty()) {
nuclear@54 159 return 0;
nuclear@54 160 }
nuclear@54 161 return &meshes[0];
nuclear@54 162 }
nuclear@54 163
nuclear@54 164 const Mesh * const *Scene::get_meshes() const
nuclear@54 165 {
nuclear@54 166 if(meshes.empty()) {
nuclear@54 167 return 0;
nuclear@54 168 }
nuclear@54 169 return &meshes[0];
nuclear@54 170 }
nuclear@54 171
nuclear@54 172 Light *Scene::get_lights()
nuclear@54 173 {
nuclear@54 174 if(lights.empty()) {
nuclear@54 175 return 0;
nuclear@54 176 }
nuclear@54 177 return &lights[0];
nuclear@54 178 }
nuclear@54 179
nuclear@54 180 const Light *Scene::get_lights() const
nuclear@54 181 {
nuclear@54 182 if(lights.empty()) {
nuclear@54 183 return 0;
nuclear@54 184 }
nuclear@54 185 return &lights[0];
nuclear@54 186 }
nuclear@54 187
John@14 188 Material *Scene::get_materials()
John@14 189 {
John@14 190 if(matlib.empty()) {
John@14 191 return 0;
John@14 192 }
John@14 193 return &matlib[0];
John@14 194 }
John@14 195
John@14 196 const Material *Scene::get_materials() const
John@14 197 {
John@14 198 if(matlib.empty()) {
John@14 199 return 0;
John@14 200 }
John@14 201 return &matlib[0];
John@14 202 }
nuclear@24 203
nuclear@24 204 const Face *Scene::get_face_buffer() const
nuclear@24 205 {
nuclear@24 206 if(facebuf) {
nuclear@24 207 return facebuf;
nuclear@24 208 }
nuclear@24 209
nuclear@24 210 int num_meshes = get_num_meshes();
nuclear@24 211
nuclear@24 212 printf("constructing face buffer with %d faces (out of %d meshes)\n", get_num_faces(), num_meshes);
nuclear@24 213 facebuf = new Face[num_faces];
nuclear@24 214 Face *fptr = facebuf;
nuclear@24 215
nuclear@24 216 for(int i=0; i<num_meshes; i++) {
nuclear@24 217 for(size_t j=0; j<meshes[i]->faces.size(); j++) {
nuclear@24 218 *fptr++ = meshes[i]->faces[j];
nuclear@24 219 }
nuclear@24 220 }
nuclear@24 221 return facebuf;
nuclear@24 222 }
nuclear@24 223
nuclear@28 224 const KDNodeGPU *Scene::get_kdtree_buffer() const
nuclear@28 225 {
nuclear@28 226 if(kdbuf) {
nuclear@28 227 return kdbuf;
nuclear@28 228 }
nuclear@28 229
nuclear@28 230 if(!kdtree) {
nuclear@28 231 ((Scene*)this)->build_kdtree();
nuclear@28 232 }
nuclear@28 233
nuclear@35 234 int num_nodes = get_num_kdnodes();
nuclear@35 235 kdbuf = new KDNodeGPU[num_nodes];
nuclear@35 236
nuclear@35 237 int count = 0;
nuclear@35 238
nuclear@35 239 // first arrange the kdnodes into an array (flatten)
nuclear@53 240 flatten_kdtree(kdtree, kdbuf, &count);
nuclear@35 241
nuclear@28 242 return kdbuf;
nuclear@28 243 }
nuclear@28 244
nuclear@53 245 static int flatten_kdtree(const KDNode *node, KDNodeGPU *kdbuf, int *count)
nuclear@28 246 {
nuclear@38 247 const size_t max_node_items = sizeof kdbuf[0].face_idx / sizeof kdbuf[0].face_idx[0];
nuclear@35 248 int idx = (*count)++;
nuclear@29 249
nuclear@35 250 // copy the node
nuclear@35 251 kdbuf[idx].aabb = node->aabb;
nuclear@38 252 kdbuf[idx].num_faces = 0;
nuclear@38 253
nuclear@35 254 for(size_t i=0; i<node->face_idx.size(); i++) {
nuclear@38 255 if(i >= max_node_items) {
nuclear@38 256 fprintf(stderr, "WARNING too many faces per leaf node!\n");
nuclear@38 257 break;
nuclear@38 258 }
nuclear@35 259 kdbuf[idx].face_idx[i] = node->face_idx[i];
nuclear@38 260 kdbuf[idx].num_faces++;
nuclear@28 261 }
nuclear@35 262
nuclear@35 263 // recurse to the left/right (if we're not in a leaf node)
nuclear@35 264 if(node->left) {
nuclear@35 265 assert(node->right);
nuclear@35 266
nuclear@53 267 kdbuf[idx].left = flatten_kdtree(node->left, kdbuf, count);
nuclear@53 268 kdbuf[idx].right = flatten_kdtree(node->right, kdbuf, count);
nuclear@53 269 } else {
nuclear@53 270 kdbuf[idx].left = kdbuf[idx].right = -1;
nuclear@35 271 }
nuclear@28 272
nuclear@53 273 return idx;
nuclear@29 274 }
nuclear@24 275
nuclear@27 276 void Scene::draw_kdtree() const
nuclear@27 277 {
nuclear@27 278 glPushAttrib(GL_ENABLE_BIT);
nuclear@27 279 glDisable(GL_LIGHTING);
nuclear@27 280 glDepthMask(0);
nuclear@27 281
nuclear@27 282 glBegin(GL_LINES);
nuclear@27 283 ::draw_kdtree(kdtree, 0);
nuclear@27 284 glEnd();
nuclear@27 285
nuclear@27 286 glDepthMask(1);
nuclear@27 287 glPopAttrib();
nuclear@27 288 }
nuclear@27 289
nuclear@27 290 static float palette[][3] = {
nuclear@27 291 {0, 1, 0},
nuclear@27 292 {1, 0, 0},
nuclear@27 293 {0, 0, 1},
nuclear@27 294 {1, 1, 0},
nuclear@27 295 {0, 0, 1},
nuclear@27 296 {1, 0, 1}
nuclear@27 297 };
nuclear@27 298 static int pal_size = sizeof palette / sizeof *palette;
nuclear@27 299
nuclear@27 300 static void draw_kdtree(const KDNode *node, int level)
nuclear@27 301 {
nuclear@27 302 if(!node) return;
nuclear@27 303
nuclear@27 304 draw_kdtree(node->left, level + 1);
nuclear@27 305 draw_kdtree(node->right, level + 1);
nuclear@27 306
nuclear@27 307 glColor3fv(palette[level % pal_size]);
nuclear@27 308
nuclear@27 309 glVertex3fv(node->aabb.min);
nuclear@27 310 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.min[2]);
nuclear@27 311 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.min[2]);
nuclear@27 312 glVertex3f(node->aabb.max[0], node->aabb.max[1], node->aabb.min[2]);
nuclear@27 313 glVertex3f(node->aabb.max[0], node->aabb.max[1], node->aabb.min[2]);
nuclear@27 314 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.min[2]);
nuclear@27 315 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.min[2]);
nuclear@27 316 glVertex3fv(node->aabb.min);
nuclear@27 317
nuclear@27 318 glVertex3f(node->aabb.min[0], node->aabb.min[1], node->aabb.max[2]);
nuclear@27 319 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.max[2]);
nuclear@27 320 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.max[2]);
nuclear@27 321 glVertex3fv(node->aabb.max);
nuclear@27 322 glVertex3fv(node->aabb.max);
nuclear@27 323 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.max[2]);
nuclear@27 324 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.max[2]);
nuclear@27 325 glVertex3f(node->aabb.min[0], node->aabb.min[1], node->aabb.max[2]);
nuclear@27 326
nuclear@27 327 glVertex3fv(node->aabb.min);
nuclear@27 328 glVertex3f(node->aabb.min[0], node->aabb.min[1], node->aabb.max[2]);
nuclear@27 329 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.min[2]);
nuclear@27 330 glVertex3f(node->aabb.max[0], node->aabb.min[1], node->aabb.max[2]);
nuclear@27 331 glVertex3f(node->aabb.max[0], node->aabb.max[1], node->aabb.min[2]);
nuclear@27 332 glVertex3fv(node->aabb.max);
nuclear@27 333 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.min[2]);
nuclear@27 334 glVertex3f(node->aabb.min[0], node->aabb.max[1], node->aabb.max[2]);
nuclear@27 335 }
nuclear@27 336
nuclear@27 337 bool Scene::build_kdtree()
nuclear@24 338 {
nuclear@29 339 assert(kdtree == 0);
nuclear@29 340
nuclear@24 341 const Face *faces = get_face_buffer();
nuclear@24 342 int num_faces = get_num_faces();
nuclear@24 343
nuclear@25 344 printf("Constructing kd-tree out of %d faces ...\n", num_faces);
nuclear@25 345
nuclear@27 346 int icost = accel_param[ACCEL_PARAM_COST_INTERSECT];
nuclear@27 347 int tcost = accel_param[ACCEL_PARAM_COST_TRAVERSE];
nuclear@27 348 printf(" max items per leaf: %d\n", accel_param[ACCEL_PARAM_MAX_NODE_ITEMS]);
nuclear@27 349 printf(" SAH parameters - tcost: %d - icost: %d\n", tcost, icost);
nuclear@27 350
nuclear@25 351 free_kdtree(kdtree);
nuclear@25 352 kdtree = new KDNode;
nuclear@25 353
nuclear@25 354 /* Start the construction of the kdtree by adding all faces of the scene
nuclear@25 355 * to the new root node. At the same time calculate the root's AABB.
nuclear@25 356 */
nuclear@25 357 kdtree->aabb.min[0] = kdtree->aabb.min[1] = kdtree->aabb.min[2] = FLT_MAX;
nuclear@25 358 kdtree->aabb.max[0] = kdtree->aabb.max[1] = kdtree->aabb.max[2] = -FLT_MAX;
nuclear@25 359
nuclear@24 360 for(int i=0; i<num_faces; i++) {
nuclear@25 361 const Face *face = faces + i;
nuclear@25 362
nuclear@25 363 // for each vertex of the face ...
nuclear@25 364 for(int j=0; j<3; j++) {
nuclear@25 365 const float *pos = face->v[j].pos;
nuclear@25 366
nuclear@25 367 // for each element (xyz) of the position vector ...
nuclear@25 368 for(int k=0; k<3; k++) {
nuclear@25 369 if(pos[k] < kdtree->aabb.min[k]) {
nuclear@25 370 kdtree->aabb.min[k] = pos[k];
nuclear@25 371 }
nuclear@25 372 if(pos[k] > kdtree->aabb.max[k]) {
nuclear@25 373 kdtree->aabb.max[k] = pos[k];
nuclear@25 374 }
nuclear@25 375 }
nuclear@25 376 }
nuclear@25 377
nuclear@32 378 kdtree->face_idx.push_back(i); // add the face
nuclear@24 379 }
nuclear@24 380
nuclear@38 381 CHECK_AABB(kdtree->aabb);
nuclear@38 382
nuclear@26 383 // calculate the heuristic for the root
nuclear@32 384 kdtree->cost = eval_cost(faces, &kdtree->face_idx[0], kdtree->face_idx.size(), kdtree->aabb, 0);
nuclear@26 385
nuclear@25 386 // now proceed splitting the root recursively
nuclear@32 387 if(!::build_kdtree(kdtree, faces)) {
nuclear@27 388 fprintf(stderr, "failed to build kdtree\n");
nuclear@27 389 return false;
nuclear@27 390 }
nuclear@27 391
nuclear@27 392 printf(" tree depth: %d\n", kdtree_depth(kdtree));
nuclear@27 393 print_item_counts(kdtree, 0);
nuclear@27 394 return true;
nuclear@24 395 }
nuclear@24 396
nuclear@37 397 struct Split {
nuclear@37 398 int axis;
nuclear@37 399 float pos;
nuclear@37 400 float sum_cost;
nuclear@37 401 float cost_left, cost_right;
nuclear@37 402 };
nuclear@37 403
nuclear@37 404 static void find_best_split(const KDNode *node, int axis, const Face *faces, Split *split)
nuclear@37 405 {
nuclear@37 406 Split best_split;
nuclear@37 407 best_split.sum_cost = FLT_MAX;
nuclear@37 408
nuclear@37 409 for(size_t i=0; i<node->face_idx.size(); i++) {
nuclear@37 410 const Face *face = faces + node->face_idx[i];
nuclear@37 411
nuclear@37 412 float splitpt[2];
nuclear@37 413 splitpt[0] = MIN(face->v[0].pos[axis], MIN(face->v[1].pos[axis], face->v[2].pos[axis]));
nuclear@37 414 splitpt[1] = MAX(face->v[0].pos[axis], MAX(face->v[1].pos[axis], face->v[2].pos[axis]));
nuclear@37 415
nuclear@37 416 for(int j=0; j<2; j++) {
nuclear@38 417 if(splitpt[j] <= node->aabb.min[axis] || splitpt[j] >= node->aabb.max[axis]) {
nuclear@38 418 continue;
nuclear@38 419 }
nuclear@38 420
nuclear@37 421 AABBox aabb_left, aabb_right;
nuclear@37 422 aabb_left = aabb_right = node->aabb;
nuclear@37 423 aabb_left.max[axis] = splitpt[j];
nuclear@37 424 aabb_right.min[axis] = splitpt[j];
nuclear@37 425
nuclear@37 426 float left_cost = eval_cost(faces, &node->face_idx[0], node->face_idx.size(), aabb_left, axis);
nuclear@37 427 float right_cost = eval_cost(faces, &node->face_idx[0], node->face_idx.size(), aabb_right, axis);
nuclear@37 428 float sum_cost = left_cost + right_cost - accel_param[ACCEL_PARAM_COST_TRAVERSE]; // tcost is added twice
nuclear@37 429
nuclear@37 430 if(sum_cost < best_split.sum_cost) {
nuclear@37 431 best_split.cost_left = left_cost;
nuclear@37 432 best_split.cost_right = right_cost;
nuclear@37 433 best_split.sum_cost = sum_cost;
nuclear@37 434 best_split.pos = splitpt[j];
nuclear@37 435 }
nuclear@37 436 }
nuclear@37 437 }
nuclear@37 438
nuclear@37 439 assert(split);
nuclear@37 440 *split = best_split;
nuclear@37 441 split->axis = axis;
nuclear@37 442 }
nuclear@37 443
nuclear@32 444 static bool build_kdtree(KDNode *kd, const Face *faces, int level)
nuclear@24 445 {
nuclear@28 446 int opt_max_depth = accel_param[ACCEL_PARAM_MAX_TREE_DEPTH];
nuclear@26 447 int opt_max_items = accel_param[ACCEL_PARAM_MAX_NODE_ITEMS];
nuclear@27 448
nuclear@32 449 if(kd->face_idx.empty() || level >= opt_max_depth) {
nuclear@27 450 return true;
nuclear@25 451 }
nuclear@25 452
nuclear@37 453 Split best_split;
nuclear@38 454 best_split.axis = -1;
nuclear@37 455 best_split.sum_cost = FLT_MAX;
nuclear@26 456
nuclear@38 457 for(int i=0; i<3; i++) {
nuclear@37 458 Split split;
nuclear@37 459 find_best_split(kd, i, faces, &split);
nuclear@26 460
nuclear@37 461 if(split.sum_cost < best_split.sum_cost) {
nuclear@37 462 best_split = split;
nuclear@26 463 }
nuclear@26 464 }
nuclear@26 465
nuclear@38 466 if(best_split.axis == -1) {
nuclear@38 467 return true; // can't split any more, only 0-area splits available
nuclear@38 468 }
nuclear@37 469
nuclear@29 470 //printf("current cost: %f, best_cost: %f\n", kd->cost, best_sum_cost);
nuclear@37 471 if(best_split.sum_cost > kd->cost && (opt_max_items == 0 || (int)kd->face_idx.size() <= opt_max_items)) {
nuclear@27 472 return true; // stop splitting if it doesn't reduce the cost
nuclear@26 473 }
nuclear@26 474
nuclear@38 475 kd->axis = best_split.axis;
nuclear@38 476
nuclear@26 477 // create the two children
nuclear@26 478 KDNode *kdleft, *kdright;
nuclear@26 479 kdleft = new KDNode;
nuclear@26 480 kdright = new KDNode;
nuclear@26 481
nuclear@26 482 kdleft->aabb = kdright->aabb = kd->aabb;
nuclear@26 483
nuclear@37 484 kdleft->aabb.max[kd->axis] = best_split.pos;
nuclear@37 485 kdright->aabb.min[kd->axis] = best_split.pos;
nuclear@26 486
nuclear@37 487 kdleft->cost = best_split.cost_left;
nuclear@37 488 kdright->cost = best_split.cost_right;
nuclear@26 489
nuclear@34 490 // TODO would it be much better if we actually split faces that straddle the splitting plane?
nuclear@32 491 for(size_t i=0; i<kd->face_idx.size(); i++) {
nuclear@32 492 int fidx = kd->face_idx[i];
nuclear@32 493 const Face *face = faces + fidx;
nuclear@26 494
nuclear@37 495 if(face->v[0].pos[kd->axis] < best_split.pos ||
nuclear@37 496 face->v[1].pos[kd->axis] < best_split.pos ||
nuclear@37 497 face->v[2].pos[kd->axis] < best_split.pos) {
nuclear@32 498 kdleft->face_idx.push_back(fidx);
nuclear@26 499 }
nuclear@37 500 if(face->v[0].pos[kd->axis] >= best_split.pos ||
nuclear@37 501 face->v[1].pos[kd->axis] >= best_split.pos ||
nuclear@37 502 face->v[2].pos[kd->axis] >= best_split.pos) {
nuclear@32 503 kdright->face_idx.push_back(fidx);
nuclear@26 504 }
nuclear@26 505 }
nuclear@32 506 kd->face_idx.clear(); // only leaves have faces
nuclear@26 507
nuclear@26 508 kd->left = kdleft;
nuclear@26 509 kd->right = kdright;
nuclear@27 510
nuclear@32 511 return build_kdtree(kd->left, faces, level + 1) && build_kdtree(kd->right, faces, level + 1);
nuclear@26 512 }
nuclear@26 513
nuclear@32 514 static float eval_cost(const Face *faces, const int *face_idx, int num_faces, const AABBox &aabb, int axis)
nuclear@26 515 {
nuclear@26 516 int num_inside = 0;
nuclear@26 517 int tcost = accel_param[ACCEL_PARAM_COST_TRAVERSE];
nuclear@26 518 int icost = accel_param[ACCEL_PARAM_COST_INTERSECT];
nuclear@26 519
nuclear@32 520 for(int i=0; i<num_faces; i++) {
nuclear@32 521 const Face *face = faces + face_idx[i];
nuclear@26 522
nuclear@32 523 for(int j=0; j<3; j++) {
nuclear@32 524 if(face->v[j].pos[axis] >= aabb.min[axis] && face->v[j].pos[axis] < aabb.max[axis]) {
nuclear@26 525 num_inside++;
nuclear@26 526 break;
nuclear@26 527 }
nuclear@26 528 }
nuclear@26 529 }
nuclear@26 530
nuclear@38 531 float dx = aabb.max[0] - aabb.min[0];
nuclear@38 532 float dy = aabb.max[1] - aabb.min[1];
nuclear@38 533 float dz = aabb.max[2] - aabb.min[2];
nuclear@38 534
nuclear@38 535 if(dx < 0.0) {
nuclear@38 536 fprintf(stderr, "FOO DX = %f\n", dx);
nuclear@38 537 abort();
nuclear@38 538 }
nuclear@38 539 if(dy < 0.0) {
nuclear@38 540 fprintf(stderr, "FOO DX = %f\n", dy);
nuclear@38 541 abort();
nuclear@38 542 }
nuclear@38 543 if(dz < 0.0) {
nuclear@38 544 fprintf(stderr, "FOO DX = %f\n", dz);
nuclear@38 545 abort();
nuclear@38 546 }
nuclear@38 547
nuclear@38 548 if(dx < 1e-6 || dy < 1e-6 || dz < 1e-6) {
nuclear@27 549 return FLT_MAX; // heavily penalize 0-area voxels
nuclear@27 550 }
nuclear@27 551
nuclear@38 552 float sarea = 2.0 * (dx + dy + dz);//aabb.calc_surface_area();
nuclear@32 553 return tcost + sarea * num_inside * icost;
nuclear@24 554 }
nuclear@25 555
nuclear@25 556 static void free_kdtree(KDNode *node)
nuclear@25 557 {
nuclear@25 558 if(node) {
nuclear@25 559 free_kdtree(node->left);
nuclear@25 560 free_kdtree(node->right);
nuclear@25 561 delete node;
nuclear@25 562 }
nuclear@25 563 }
nuclear@27 564
nuclear@28 565 int kdtree_depth(const KDNode *node)
nuclear@27 566 {
nuclear@27 567 if(!node) return 0;
nuclear@27 568
nuclear@27 569 int left = kdtree_depth(node->left);
nuclear@27 570 int right = kdtree_depth(node->right);
nuclear@27 571 return (left > right ? left : right) + 1;
nuclear@27 572 }
nuclear@27 573
nuclear@28 574 int kdtree_nodes(const KDNode *node)
nuclear@28 575 {
nuclear@28 576 if(!node) return 0;
nuclear@28 577 return kdtree_nodes(node->left) + kdtree_nodes(node->right) + 1;
nuclear@28 578 }
nuclear@28 579
nuclear@27 580 static void print_item_counts(const KDNode *node, int level)
nuclear@27 581 {
nuclear@27 582 if(!node) return;
nuclear@27 583
nuclear@30 584 for(int i=0; i<level; i++) {
nuclear@27 585 fputs(" ", stdout);
nuclear@27 586 }
nuclear@32 587 printf("- %d (cost: %f)\n", (int)node->face_idx.size(), node->cost);
nuclear@27 588
nuclear@27 589 print_item_counts(node->left, level + 1);
nuclear@27 590 print_item_counts(node->right, level + 1);
nuclear@27 591 }