[372] | 1 | #include <stack>
|
---|
| 2 | #include <algorithm>
|
---|
| 3 | #include <queue>
|
---|
| 4 | #include "Environment.h"
|
---|
| 5 | #include "Mesh.h"
|
---|
| 6 | #include "KdTree.h"
|
---|
[469] | 7 | #include "ViewCell.h"
|
---|
[505] | 8 | #include "Beam.h"
|
---|
[2116] | 9 | #include "ViewCell.h"
|
---|
| 10 | #include "IntersectableWrapper.h"
|
---|
[372] | 11 |
|
---|
| 12 |
|
---|
[2176] | 13 | using namespace std;
|
---|
| 14 |
|
---|
[372] | 15 |
|
---|
[2332] | 16 | #define TYPE_INTERIOR -2
|
---|
| 17 | #define TYPE_LEAF -3
|
---|
| 18 |
|
---|
| 19 |
|
---|
[863] | 20 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 21 |
|
---|
[1176] | 22 | int KdNode::sMailId = 1;
|
---|
| 23 | int KdNode::sReservedMailboxes = 1;
|
---|
[863] | 24 |
|
---|
[2066] | 25 |
|
---|
[1197] | 26 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 27 | {
|
---|
[2575] | 28 | return obj1->mId < obj2->mId;
|
---|
[1197] | 29 | }
|
---|
| 30 |
|
---|
| 31 |
|
---|
[2539] | 32 | KdNode::KdNode(KdInterior *parent):
|
---|
| 33 | mParent(parent), mMailbox(0), mIntersectable(NULL)
|
---|
[372] | 34 | {
|
---|
[2575] | 35 | if (parent)
|
---|
| 36 | mDepth = parent->mDepth+1;
|
---|
| 37 | else
|
---|
| 38 | mDepth = 0;
|
---|
[372] | 39 | }
|
---|
| 40 |
|
---|
[1002] | 41 |
|
---|
| 42 | KdInterior::~KdInterior()
|
---|
| 43 | {
|
---|
[2575] | 44 | // recursivly destroy children
|
---|
| 45 | DEL_PTR(mFront);
|
---|
| 46 | DEL_PTR(mBack);
|
---|
[1002] | 47 | }
|
---|
| 48 |
|
---|
| 49 |
|
---|
[1999] | 50 | KdLeaf::~KdLeaf()
|
---|
| 51 | {
|
---|
[2575] | 52 | DEL_PTR(mViewCell);
|
---|
[1999] | 53 | }
|
---|
| 54 |
|
---|
| 55 |
|
---|
[372] | 56 | KdTree::KdTree()
|
---|
[2539] | 57 | {
|
---|
[372] | 58 | mRoot = new KdLeaf(NULL, 0);
|
---|
[2575] | 59 | Environment::GetSingleton()->GetIntValue("KdTree.Termination.maxNodes",
|
---|
| 60 | mTermMaxNodes);
|
---|
| 61 | Environment::GetSingleton()->GetIntValue("KdTree.Termination.maxDepth",
|
---|
| 62 | mTermMaxDepth);
|
---|
| 63 | Environment::GetSingleton()->GetIntValue("KdTree.Termination.minCost",
|
---|
| 64 | mTermMinCost);
|
---|
| 65 | Environment::GetSingleton()->GetFloatValue("KdTree.Termination.maxCostRatio",
|
---|
| 66 | mMaxCostRatio);
|
---|
| 67 | Environment::GetSingleton()->GetFloatValue("KdTree.Termination.ct_div_ci",
|
---|
| 68 | mCt_div_ci);
|
---|
| 69 | Environment::GetSingleton()->GetFloatValue("KdTree.splitBorder",
|
---|
| 70 | mSplitBorder);
|
---|
| 71 | Environment::GetSingleton()->GetFloatValue("KdTree.pvsArea",
|
---|
| 72 | mKdPvsArea);
|
---|
[372] | 73 |
|
---|
[2575] | 74 | Environment::GetSingleton()->GetBoolValue("KdTree.sahUseFaces",
|
---|
| 75 | mSahUseFaces);
|
---|
[372] | 76 |
|
---|
| 77 | char splitType[64];
|
---|
[1004] | 78 | Environment::GetSingleton()->GetStringValue("KdTree.splitMethod", splitType);
|
---|
[372] | 79 |
|
---|
| 80 | mSplitMethod = SPLIT_SPATIAL_MEDIAN;
|
---|
| 81 | if (strcmp(splitType, "spatialMedian") == 0)
|
---|
| 82 | mSplitMethod = SPLIT_SPATIAL_MEDIAN;
|
---|
| 83 | else
|
---|
| 84 | if (strcmp(splitType, "objectMedian") == 0)
|
---|
| 85 | mSplitMethod = SPLIT_OBJECT_MEDIAN;
|
---|
| 86 | else
|
---|
| 87 | if (strcmp(splitType, "SAH") == 0)
|
---|
| 88 | mSplitMethod = SPLIT_SAH;
|
---|
| 89 | else {
|
---|
| 90 | cerr<<"Wrong kd split type "<<splitType<<endl;
|
---|
| 91 | exit(1);
|
---|
| 92 | }
|
---|
| 93 | splitCandidates = NULL;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[1002] | 96 |
|
---|
| 97 | KdTree::~KdTree()
|
---|
| 98 | {
|
---|
[1999] | 99 | DEL_PTR(mRoot);
|
---|
| 100 | CLEAR_CONTAINER(mKdIntersectables);
|
---|
[1002] | 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
[372] | 104 | bool
|
---|
| 105 | KdTree::Construct()
|
---|
| 106 | {
|
---|
| 107 | if (!splitCandidates)
|
---|
[2003] | 108 | splitCandidates = new vector<SortableEntry *>;
|
---|
[372] | 109 |
|
---|
| 110 | // first construct a leaf that will get subdivide
|
---|
| 111 | KdLeaf *leaf = (KdLeaf *) mRoot;
|
---|
| 112 |
|
---|
| 113 | mStat.nodes = 1;
|
---|
| 114 |
|
---|
| 115 | mBox.Initialize();
|
---|
| 116 | ObjectContainer::const_iterator mi;
|
---|
| 117 | for ( mi = leaf->mObjects.begin();
|
---|
| 118 | mi != leaf->mObjects.end();
|
---|
| 119 | mi++) {
|
---|
[752] | 120 | // cout<<(*mi)->GetBox()<<endl;
|
---|
| 121 | mBox.Include((*mi)->GetBox());
|
---|
[372] | 122 | }
|
---|
| 123 |
|
---|
[752] | 124 | cout <<"KdTree Root Box:"<<mBox<<endl;
|
---|
[372] | 125 | mRoot = Subdivide(TraversalData(leaf, mBox, 0));
|
---|
| 126 |
|
---|
| 127 | // remove the allocated array
|
---|
[2003] | 128 | CLEAR_CONTAINER(*splitCandidates);
|
---|
[372] | 129 | delete splitCandidates;
|
---|
| 130 |
|
---|
[2569] | 131 | float area = GetBox().SurfaceArea() * mKdPvsArea;
|
---|
[1989] | 132 |
|
---|
| 133 | SetPvsTerminationNodes(area);
|
---|
| 134 |
|
---|
[372] | 135 | return true;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | KdNode *
|
---|
| 139 | KdTree::Subdivide(const TraversalData &tdata)
|
---|
| 140 | {
|
---|
| 141 |
|
---|
| 142 | KdNode *result = NULL;
|
---|
| 143 |
|
---|
| 144 | priority_queue<TraversalData> tStack;
|
---|
| 145 | // stack<STraversalData> tStack;
|
---|
| 146 |
|
---|
| 147 | tStack.push(tdata);
|
---|
| 148 | AxisAlignedBox3 backBox, frontBox;
|
---|
| 149 |
|
---|
| 150 | while (!tStack.empty()) {
|
---|
[752] | 151 | // cout<<mStat.Nodes()<<" "<<mTermMaxNodes<<endl;
|
---|
| 152 | if (mStat.Nodes() > mTermMaxNodes) {
|
---|
| 153 | // if ( GetMemUsage() > maxMemory ) {
|
---|
[372] | 154 | // count statistics on unprocessed leafs
|
---|
| 155 | while (!tStack.empty()) {
|
---|
[752] | 156 | EvaluateLeafStats(tStack.top());
|
---|
| 157 | tStack.pop();
|
---|
[372] | 158 | }
|
---|
| 159 | break;
|
---|
| 160 | }
|
---|
[752] | 161 |
|
---|
| 162 |
|
---|
[372] | 163 | TraversalData data = tStack.top();
|
---|
| 164 | tStack.pop();
|
---|
| 165 |
|
---|
| 166 | KdNode *node = SubdivideNode((KdLeaf *) data.mNode,
|
---|
[2575] | 167 | data.mBox,
|
---|
| 168 | backBox,
|
---|
| 169 | frontBox
|
---|
| 170 | );
|
---|
[752] | 171 |
|
---|
[2575] | 172 | if (result == NULL)
|
---|
[372] | 173 | result = node;
|
---|
| 174 |
|
---|
| 175 | if (!node->IsLeaf()) {
|
---|
| 176 | KdInterior *interior = (KdInterior *) node;
|
---|
| 177 | // push the children on the stack
|
---|
| 178 | tStack.push(TraversalData(interior->mBack, backBox, data.mDepth+1));
|
---|
| 179 | tStack.push(TraversalData(interior->mFront, frontBox, data.mDepth+1));
|
---|
| 180 |
|
---|
[2575] | 181 | }
|
---|
| 182 | else {
|
---|
[372] | 183 | EvaluateLeafStats(data);
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | return result;
|
---|
| 188 |
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | bool
|
---|
| 193 | KdTree::TerminationCriteriaMet(const KdLeaf *leaf)
|
---|
| 194 | {
|
---|
[2575] | 195 | const bool criteriaMet =
|
---|
| 196 | ((int)leaf->mObjects.size() <= mTermMinCost) ||
|
---|
| 197 | (leaf->mDepth >= mTermMaxDepth);
|
---|
| 198 |
|
---|
| 199 | if (0 && criteriaMet)
|
---|
| 200 | cerr<<"\n OBJECTS="<<(int)leaf->mObjects.size()<<endl;
|
---|
| 201 |
|
---|
| 202 | return criteriaMet;
|
---|
[372] | 203 | }
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | int
|
---|
| 207 | KdTree::SelectPlane(KdLeaf *leaf,
|
---|
[2575] | 208 | const AxisAlignedBox3 &box,
|
---|
| 209 | float &position
|
---|
| 210 | )
|
---|
[372] | 211 | {
|
---|
| 212 | int axis = -1;
|
---|
| 213 |
|
---|
| 214 | switch (mSplitMethod)
|
---|
| 215 | {
|
---|
| 216 | case SPLIT_SPATIAL_MEDIAN: {
|
---|
| 217 | axis = box.Size().DrivingAxis();
|
---|
| 218 | position = (box.Min()[axis] + box.Max()[axis])*0.5f;
|
---|
| 219 | break;
|
---|
| 220 | }
|
---|
| 221 | case SPLIT_SAH: {
|
---|
| 222 | int objectsBack, objectsFront;
|
---|
| 223 | float costRatio;
|
---|
[1584] | 224 | bool mOnlyDrivingAxis = true;
|
---|
[1387] | 225 |
|
---|
[2575] | 226 | if (mOnlyDrivingAxis) {
|
---|
| 227 | axis = box.Size().DrivingAxis();
|
---|
| 228 | costRatio = BestCostRatio(leaf,
|
---|
| 229 | box,
|
---|
| 230 | axis,
|
---|
| 231 | position,
|
---|
| 232 | objectsBack,
|
---|
| 233 | objectsFront);
|
---|
[372] | 234 | }
|
---|
[2575] | 235 | else {
|
---|
| 236 | // for all 3 axes
|
---|
| 237 | costRatio = MAX_FLOAT;
|
---|
| 238 | for (int i=0; i < 3; i++) {
|
---|
| 239 | float p;
|
---|
| 240 | float r = BestCostRatio(leaf,
|
---|
| 241 | box,
|
---|
| 242 | i,
|
---|
| 243 | p,
|
---|
| 244 | objectsBack,
|
---|
| 245 | objectsFront);
|
---|
| 246 | if (r < costRatio) {
|
---|
| 247 | costRatio = r;
|
---|
| 248 | axis = i;
|
---|
| 249 | position = p;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
[372] | 253 |
|
---|
| 254 | if (costRatio > mMaxCostRatio) {
|
---|
[2575] | 255 | //cout<<"Too big cost ratio "<<costRatio<<endl;
|
---|
| 256 | axis = -1;
|
---|
[372] | 257 | }
|
---|
| 258 | break;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | }
|
---|
| 262 | return axis;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[2575] | 265 | KdNode*
|
---|
[372] | 266 | KdTree::SubdivideNode(
|
---|
| 267 | KdLeaf *leaf,
|
---|
| 268 | const AxisAlignedBox3 &box,
|
---|
| 269 | AxisAlignedBox3 &backBBox,
|
---|
| 270 | AxisAlignedBox3 &frontBBox
|
---|
| 271 | )
|
---|
[2575] | 272 | {
|
---|
[372] | 273 | if (TerminationCriteriaMet(leaf))
|
---|
[2575] | 274 | return leaf;
|
---|
[469] | 275 |
|
---|
[372] | 276 | float position;
|
---|
| 277 |
|
---|
| 278 | // select subdivision axis
|
---|
| 279 | int axis = SelectPlane( leaf, box, position );
|
---|
| 280 |
|
---|
| 281 | if (axis == -1) {
|
---|
| 282 | return leaf;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[2575] | 285 | mStat.nodes += 2;
|
---|
[372] | 286 | mStat.splits[axis]++;
|
---|
| 287 |
|
---|
| 288 | // add the new nodes to the tree
|
---|
| 289 | KdInterior *node = new KdInterior(leaf->mParent);
|
---|
| 290 |
|
---|
| 291 | node->mAxis = axis;
|
---|
| 292 | node->mPosition = position;
|
---|
| 293 | node->mBox = box;
|
---|
| 294 |
|
---|
| 295 | backBBox = box;
|
---|
| 296 | frontBBox = box;
|
---|
| 297 |
|
---|
| 298 | // first count ray sides
|
---|
| 299 | int objectsBack = 0;
|
---|
| 300 | int objectsFront = 0;
|
---|
| 301 |
|
---|
| 302 | backBBox.SetMax(axis, position);
|
---|
| 303 | frontBBox.SetMin(axis, position);
|
---|
| 304 |
|
---|
| 305 | ObjectContainer::const_iterator mi;
|
---|
| 306 |
|
---|
| 307 | for ( mi = leaf->mObjects.begin();
|
---|
| 308 | mi != leaf->mObjects.end();
|
---|
| 309 | mi++) {
|
---|
| 310 | // determine the side of this ray with respect to the plane
|
---|
| 311 | AxisAlignedBox3 box = (*mi)->GetBox();
|
---|
| 312 | if (box.Max(axis) > position )
|
---|
| 313 | objectsFront++;
|
---|
| 314 |
|
---|
| 315 | if (box.Min(axis) < position )
|
---|
| 316 | objectsBack++;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | KdLeaf *back = new KdLeaf(node, objectsBack);
|
---|
| 320 | KdLeaf *front = new KdLeaf(node, objectsFront);
|
---|
[1074] | 321 |
|
---|
[372] | 322 | // replace a link from node's parent
|
---|
| 323 | if ( leaf->mParent )
|
---|
| 324 | leaf->mParent->ReplaceChildLink(leaf, node);
|
---|
| 325 |
|
---|
| 326 | // and setup child links
|
---|
| 327 | node->SetupChildLinks(back, front);
|
---|
| 328 |
|
---|
| 329 | for (mi = leaf->mObjects.begin();
|
---|
| 330 | mi != leaf->mObjects.end();
|
---|
| 331 | mi++) {
|
---|
| 332 | // determine the side of this ray with respect to the plane
|
---|
| 333 | AxisAlignedBox3 box = (*mi)->GetBox();
|
---|
[1736] | 334 |
|
---|
| 335 | // matt: no more ref
|
---|
[1143] | 336 | // for handling multiple objects: keep track of references
|
---|
[1736] | 337 | //if (leaf->IsRoot())
|
---|
| 338 | // (*mi)->mReferences = 1; // initialise references at root
|
---|
[1143] | 339 |
|
---|
[1736] | 340 | // matt: no more ref
|
---|
| 341 | //-- (*mi)->mReferences; // remove parent ref
|
---|
[372] | 342 |
|
---|
[1143] | 343 |
|
---|
[2575] | 344 | if (box.Max(axis) >= position )
|
---|
| 345 | {
|
---|
[372] | 346 | front->mObjects.push_back(*mi);
|
---|
[2575] | 347 | //++ (*mi)->mReferences;
|
---|
| 348 | }
|
---|
[1143] | 349 |
|
---|
[372] | 350 | if (box.Min(axis) < position )
|
---|
[2575] | 351 | {
|
---|
[372] | 352 | back->mObjects.push_back(*mi);
|
---|
[2575] | 353 | // matt: no more ref
|
---|
| 354 | // ++ (*mi)->mReferences;
|
---|
| 355 | }
|
---|
[1143] | 356 |
|
---|
[1144] | 357 | mStat.objectRefs -= (int)leaf->mObjects.size();
|
---|
| 358 | mStat.objectRefs += objectsBack + objectsFront;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[2575] | 361 | // store objects referenced in more than one leaf
|
---|
| 362 | // for easy access
|
---|
| 363 | ProcessMultipleRefs(back);
|
---|
| 364 | ProcessMultipleRefs(front);
|
---|
| 365 |
|
---|
| 366 | delete leaf;
|
---|
| 367 | return node;
|
---|
[372] | 368 | }
|
---|
| 369 |
|
---|
| 370 |
|
---|
[1143] | 371 | void KdTree::ProcessMultipleRefs(KdLeaf *leaf) const
|
---|
| 372 | {
|
---|
[2575] | 373 | // find objects from multiple kd-leaves
|
---|
| 374 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
[1143] | 375 |
|
---|
[2575] | 376 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 377 | {
|
---|
| 378 | Intersectable *object = *oit;
|
---|
[1143] | 379 |
|
---|
[2575] | 380 | // matt: no more ref
|
---|
| 381 | /*
|
---|
| 382 | if (object->mReferences > 1) {
|
---|
| 383 | leaf->mMultipleObjects.push_back(object);
|
---|
| 384 | }
|
---|
| 385 | */
|
---|
| 386 | }
|
---|
[1143] | 387 | }
|
---|
| 388 |
|
---|
| 389 |
|
---|
[372] | 390 | void
|
---|
| 391 | KdTreeStatistics::Print(ostream &app) const
|
---|
| 392 | {
|
---|
| 393 | app << "===== KdTree statistics ===============\n";
|
---|
| 394 |
|
---|
| 395 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
|
---|
| 396 |
|
---|
| 397 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
| 398 |
|
---|
[667] | 399 | app << "#N_SPLITS ( Number of splits in axes x y z dx dy dz)\n";
|
---|
[372] | 400 | for (int i=0; i<7; i++)
|
---|
| 401 | app << splits[i] <<" ";
|
---|
| 402 | app <<endl;
|
---|
| 403 |
|
---|
| 404 | app << "#N_RAYREFS ( Number of rayRefs )\n" <<
|
---|
| 405 | rayRefs << "\n";
|
---|
| 406 |
|
---|
| 407 | app << "#N_RAYRAYREFS ( Number of rayRefs / ray )\n" <<
|
---|
| 408 | rayRefs/(double)rays << "\n";
|
---|
| 409 |
|
---|
| 410 | app << "#N_LEAFRAYREFS ( Number of rayRefs / leaf )\n" <<
|
---|
| 411 | rayRefs/(double)Leaves() << "\n";
|
---|
| 412 |
|
---|
[1184] | 413 | app << "#N_MAXOBJECTREFS ( Max number of object refs / leaf )\n" <<
|
---|
[372] | 414 | maxObjectRefs << "\n";
|
---|
| 415 |
|
---|
| 416 | app << "#N_NONEMPTYRAYREFS ( Number of rayRefs in nonEmpty leaves / non empty leaf )\n" <<
|
---|
| 417 | rayRefsNonZeroQuery/(double)(Leaves() - zeroQueryNodes) << "\n";
|
---|
| 418 |
|
---|
| 419 | app << "#N_LEAFDOMAINREFS ( Number of query domain Refs / leaf )\n" <<
|
---|
| 420 | objectRefs/(double)Leaves() << "\n";
|
---|
| 421 |
|
---|
| 422 | // app << setprecision(4);
|
---|
| 423 |
|
---|
| 424 | app << "#N_PEMPTYLEAVES ( Percentage of leaves with zero query domains )\n"<<
|
---|
| 425 | zeroQueryNodes*100/(double)Leaves()<<endl;
|
---|
| 426 |
|
---|
| 427 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n"<<
|
---|
| 428 | maxDepthNodes*100/(double)Leaves()<<endl;
|
---|
| 429 |
|
---|
| 430 | app << "#N_PMINCOSTLEAVES ( Percentage of leaves with minCost )\n"<<
|
---|
| 431 | minCostNodes*100/(double)Leaves()<<endl;
|
---|
| 432 |
|
---|
| 433 | app << "#N_ADDED_RAYREFS (Number of dynamically added ray references )\n"<<
|
---|
| 434 | addedRayRefs<<endl;
|
---|
| 435 |
|
---|
| 436 | app << "#N_REMOVED_RAYREFS (Number of dynamically removed ray references )\n"<<
|
---|
| 437 | removedRayRefs<<endl;
|
---|
| 438 |
|
---|
| 439 | // app << setprecision(4);
|
---|
| 440 |
|
---|
| 441 | // app << "#N_CTIME ( Construction time [s] )\n"
|
---|
| 442 | // << Time() << " \n";
|
---|
| 443 |
|
---|
| 444 | app << "===== END OF KdTree statistics ==========\n";
|
---|
| 445 |
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 |
|
---|
| 449 |
|
---|
| 450 | void
|
---|
| 451 | KdTree::EvaluateLeafStats(const TraversalData &data)
|
---|
| 452 | {
|
---|
| 453 |
|
---|
| 454 | // the node became a leaf -> evaluate stats for leafs
|
---|
| 455 | KdLeaf *leaf = (KdLeaf *)data.mNode;
|
---|
| 456 |
|
---|
| 457 | if (data.mDepth > mTermMaxDepth)
|
---|
| 458 | mStat.maxDepthNodes++;
|
---|
| 459 |
|
---|
| 460 | if ( (int)(leaf->mObjects.size()) < mTermMinCost)
|
---|
| 461 | mStat.minCostNodes++;
|
---|
| 462 |
|
---|
| 463 |
|
---|
| 464 | if ( (int)(leaf->mObjects.size()) > mStat.maxObjectRefs)
|
---|
[469] | 465 | mStat.maxObjectRefs = (int)leaf->mObjects.size();
|
---|
[372] | 466 |
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 |
|
---|
| 470 |
|
---|
| 471 | void
|
---|
[1233] | 472 | KdTree::SortSubdivisionCandidates(
|
---|
[2575] | 473 | KdLeaf *node,
|
---|
| 474 | const int axis
|
---|
| 475 | )
|
---|
[372] | 476 | {
|
---|
[2575] | 477 | CLEAR_CONTAINER(*splitCandidates);
|
---|
| 478 | //splitCandidates->clear();
|
---|
| 479 |
|
---|
| 480 | int requestedSize = 2*(int)node->mObjects.size();
|
---|
| 481 |
|
---|
| 482 | // creates a sorted split candidates array
|
---|
| 483 | if (splitCandidates->capacity() > 500000 &&
|
---|
| 484 | requestedSize < (int)(splitCandidates->capacity()/10) ) {
|
---|
| 485 | delete splitCandidates;
|
---|
| 486 | splitCandidates = new vector<SortableEntry *>;
|
---|
[372] | 487 | }
|
---|
| 488 |
|
---|
| 489 | splitCandidates->reserve(requestedSize);
|
---|
| 490 |
|
---|
| 491 | // insert all queries
|
---|
| 492 | for(ObjectContainer::const_iterator mi = node->mObjects.begin();
|
---|
| 493 | mi != node->mObjects.end();
|
---|
[2066] | 494 | mi++)
|
---|
| 495 | {
|
---|
[2575] | 496 | AxisAlignedBox3 box = (*mi)->GetBox();
|
---|
[372] | 497 |
|
---|
[2575] | 498 | splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MIN,
|
---|
| 499 | box.Min(axis),
|
---|
| 500 | *mi)
|
---|
| 501 | );
|
---|
[372] | 502 |
|
---|
[2575] | 503 | splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX,
|
---|
| 504 | box.Max(axis),
|
---|
| 505 | *mi)
|
---|
| 506 | );
|
---|
[372] | 507 | }
|
---|
| 508 |
|
---|
[2066] | 509 | stable_sort(splitCandidates->begin(), splitCandidates->end(), iltS);
|
---|
[372] | 510 | }
|
---|
| 511 |
|
---|
| 512 |
|
---|
| 513 | float
|
---|
| 514 | KdTree::BestCostRatio(
|
---|
[2575] | 515 | KdLeaf *node,
|
---|
| 516 | const AxisAlignedBox3 &box,
|
---|
| 517 | const int axis,
|
---|
| 518 | float &position,
|
---|
| 519 | int &objectsBack,
|
---|
| 520 | int &objectsFront
|
---|
| 521 | )
|
---|
[372] | 522 | {
|
---|
| 523 |
|
---|
[1387] | 524 | #define DEBUG_COST 0
|
---|
| 525 |
|
---|
| 526 | #if DEBUG_COST
|
---|
| 527 | static int nodeId = -1;
|
---|
| 528 | char filename[256];
|
---|
| 529 |
|
---|
| 530 | static int lastAxis = 100;
|
---|
| 531 | if (axis <= lastAxis)
|
---|
| 532 | nodeId++;
|
---|
| 533 |
|
---|
| 534 | lastAxis = axis;
|
---|
| 535 |
|
---|
| 536 | sprintf(filename, "sah-cost%d-%d.log", nodeId, axis);
|
---|
| 537 | ofstream costStream;
|
---|
| 538 |
|
---|
| 539 | if (nodeId < 100)
|
---|
[2575] | 540 | costStream.open(filename);
|
---|
[1387] | 541 |
|
---|
| 542 | #endif
|
---|
| 543 |
|
---|
[1233] | 544 | SortSubdivisionCandidates(node, axis);
|
---|
[372] | 545 |
|
---|
| 546 | // go through the lists, count the number of objects left and right
|
---|
| 547 | // and evaluate the following cost funcion:
|
---|
| 548 | // C = ct_div_ci + (ol + or)/queries
|
---|
| 549 |
|
---|
| 550 | float totalIntersections = 0.0f;
|
---|
[2003] | 551 | vector<SortableEntry *>::const_iterator ci;
|
---|
[372] | 552 |
|
---|
| 553 | for(ci = splitCandidates->begin();
|
---|
| 554 | ci < splitCandidates->end();
|
---|
| 555 | ci++)
|
---|
[2003] | 556 | if ((*ci)->type == SortableEntry::BOX_MIN) {
|
---|
| 557 | totalIntersections += (*ci)->intersectable->IntersectionComplexity();
|
---|
[372] | 558 | }
|
---|
| 559 |
|
---|
| 560 | float intersectionsLeft = 0;
|
---|
| 561 | float intersectionsRight = totalIntersections;
|
---|
| 562 |
|
---|
[469] | 563 | int objectsLeft = 0, objectsRight = (int)node->mObjects.size();
|
---|
[372] | 564 |
|
---|
| 565 | float minBox = box.Min(axis);
|
---|
| 566 | float maxBox = box.Max(axis);
|
---|
| 567 | float boxArea = box.SurfaceArea();
|
---|
| 568 |
|
---|
| 569 | float minBand = minBox + mSplitBorder*(maxBox - minBox);
|
---|
| 570 | float maxBand = minBox + (1.0f - mSplitBorder)*(maxBox - minBox);
|
---|
| 571 |
|
---|
[469] | 572 | float minSum = 1e20f;
|
---|
[372] | 573 |
|
---|
| 574 | for(ci = splitCandidates->begin();
|
---|
| 575 | ci < splitCandidates->end();
|
---|
| 576 | ci++) {
|
---|
[2003] | 577 | switch ((*ci)->type) {
|
---|
[372] | 578 | case SortableEntry::BOX_MIN:
|
---|
| 579 | objectsLeft++;
|
---|
[2003] | 580 | intersectionsLeft += (*ci)->intersectable->IntersectionComplexity();
|
---|
[372] | 581 | break;
|
---|
| 582 | case SortableEntry::BOX_MAX:
|
---|
| 583 | objectsRight--;
|
---|
[2003] | 584 | intersectionsRight -= (*ci)->intersectable->IntersectionComplexity();
|
---|
[372] | 585 | break;
|
---|
[2575] | 586 | } // switch
|
---|
[372] | 587 |
|
---|
[2003] | 588 | if ((*ci)->value > minBand && (*ci)->value < maxBand) {
|
---|
[372] | 589 | AxisAlignedBox3 lbox = box;
|
---|
| 590 | AxisAlignedBox3 rbox = box;
|
---|
[2003] | 591 | lbox.SetMax(axis, (*ci)->value);
|
---|
| 592 | rbox.SetMin(axis, (*ci)->value);
|
---|
[372] | 593 |
|
---|
| 594 | float sum;
|
---|
| 595 | if (mSahUseFaces)
|
---|
[2575] | 596 | sum = intersectionsLeft*lbox.SurfaceArea() + intersectionsRight*rbox.SurfaceArea();
|
---|
[372] | 597 | else
|
---|
[2575] | 598 | sum = objectsLeft*lbox.SurfaceArea() + objectsRight*rbox.SurfaceArea();
|
---|
[372] | 599 |
|
---|
| 600 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
| 601 | // cout<<"cost= "<<sum<<endl;
|
---|
[1387] | 602 |
|
---|
| 603 | #if DEBUG_COST
|
---|
[2575] | 604 | if (nodeId < 100) {
|
---|
[1387] | 605 | float oldCost = mSahUseFaces ? totalIntersections : node->mObjects.size();
|
---|
| 606 | float newCost = mCt_div_ci + sum/boxArea;
|
---|
| 607 | float ratio = newCost/oldCost;
|
---|
[2003] | 608 | costStream<<(*ci)->value<<" "<<ratio<<endl;
|
---|
[2575] | 609 | }
|
---|
[1387] | 610 | #endif
|
---|
[2575] | 611 |
|
---|
[372] | 612 | if (sum < minSum) {
|
---|
[2575] | 613 | minSum = sum;
|
---|
| 614 | position = (*ci)->value;
|
---|
| 615 |
|
---|
| 616 | objectsBack = objectsLeft;
|
---|
| 617 | objectsFront = objectsRight;
|
---|
[372] | 618 | }
|
---|
| 619 | }
|
---|
[2575] | 620 | } // for ci
|
---|
[372] | 621 |
|
---|
| 622 | float oldCost = mSahUseFaces ? totalIntersections : node->mObjects.size();
|
---|
| 623 | float newCost = mCt_div_ci + minSum/boxArea;
|
---|
| 624 | float ratio = newCost/oldCost;
|
---|
| 625 |
|
---|
| 626 | #if 0
|
---|
| 627 | cout<<"===================="<<endl;
|
---|
| 628 | cout<<"costRatio="<<ratio<<" pos="<<position<<" t="<<(position - minBox)/(maxBox - minBox)
|
---|
| 629 | <<"\t o=("<<objectsBack<<","<<objectsFront<<")"<<endl;
|
---|
| 630 | #endif
|
---|
| 631 | return ratio;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | int
|
---|
| 635 | KdTree::CastRay(
|
---|
[2575] | 636 | Ray &ray
|
---|
| 637 | )
|
---|
[372] | 638 | {
|
---|
[1984] | 639 |
|
---|
[372] | 640 | int hits = 0;
|
---|
| 641 |
|
---|
| 642 | stack<RayTraversalData> tStack;
|
---|
| 643 |
|
---|
| 644 | float maxt = 1e6;
|
---|
| 645 | float mint = 0;
|
---|
[1583] | 646 |
|
---|
[1584] | 647 | // ray.ComputeInvertedDir();
|
---|
[372] | 648 | Intersectable::NewMail();
|
---|
| 649 |
|
---|
| 650 | if (!mBox.GetMinMaxT(ray, &mint, &maxt))
|
---|
| 651 | return 0;
|
---|
| 652 |
|
---|
| 653 | if (mint < 0)
|
---|
| 654 | mint = 0;
|
---|
[1584] | 655 |
|
---|
[372] | 656 |
|
---|
| 657 | maxt += Limits::Threshold;
|
---|
| 658 |
|
---|
| 659 | Vector3 entp = ray.Extrap(mint);
|
---|
| 660 | Vector3 extp = ray.Extrap(maxt);
|
---|
| 661 |
|
---|
| 662 | KdNode *node = mRoot;
|
---|
| 663 | KdNode *farChild;
|
---|
| 664 | float position;
|
---|
| 665 | int axis;
|
---|
[752] | 666 |
|
---|
[372] | 667 |
|
---|
| 668 | while (1) {
|
---|
| 669 | if (!node->IsLeaf()) {
|
---|
| 670 | KdInterior *in = (KdInterior *) node;
|
---|
| 671 | position = in->mPosition;
|
---|
| 672 | axis = in->mAxis;
|
---|
| 673 |
|
---|
| 674 | if (entp[axis] <= position) {
|
---|
[2575] | 675 | if (extp[axis] <= position) {
|
---|
| 676 | node = in->mBack;
|
---|
| 677 | // cases N1,N2,N3,P5,Z2,Z3
|
---|
| 678 | continue;
|
---|
| 679 | }
|
---|
| 680 | else {
|
---|
| 681 | // case N4
|
---|
| 682 | node = in->mBack;
|
---|
| 683 | farChild = in->mFront;
|
---|
| 684 | }
|
---|
[372] | 685 | }
|
---|
| 686 | else {
|
---|
[2575] | 687 | if (position <= extp[axis]) {
|
---|
| 688 | node = in->mFront;
|
---|
| 689 | // cases P1,P2,P3,N5,Z1
|
---|
| 690 | continue;
|
---|
| 691 | }
|
---|
| 692 | else {
|
---|
| 693 | node = in->mFront;
|
---|
| 694 | farChild = in->mBack;
|
---|
| 695 | // case P4
|
---|
| 696 | }
|
---|
| 697 | }
|
---|
[372] | 698 | // $$ modification 3.5.2004 - hints from Kamil Ghais
|
---|
| 699 | // case N4 or P4
|
---|
| 700 | float tdist = (position - ray.GetLoc(axis)) / ray.GetDir(axis);
|
---|
| 701 | tStack.push(RayTraversalData(farChild, extp, maxt));
|
---|
| 702 | extp = ray.GetLoc() + ray.GetDir()*tdist;
|
---|
| 703 | maxt = tdist;
|
---|
[2575] | 704 | }
|
---|
| 705 | else {
|
---|
| 706 | // compute intersection with all objects in this leaf
|
---|
| 707 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
| 708 | if (ray.mFlags & Ray::STORE_KDLEAVES)
|
---|
| 709 | ray.kdLeaves.push_back(leaf);
|
---|
[752] | 710 |
|
---|
[2575] | 711 | ObjectContainer::const_iterator mi;
|
---|
| 712 | for ( mi = leaf->mObjects.begin();
|
---|
| 713 | mi != leaf->mObjects.end();
|
---|
| 714 | mi++) {
|
---|
| 715 | Intersectable *object = *mi;
|
---|
| 716 | if (!object->Mailed() ) {
|
---|
| 717 | object->Mail();
|
---|
| 718 | if (ray.mFlags & Ray::STORE_TESTED_OBJECTS)
|
---|
[1867] | 719 | ray.testedObjects.push_back(object);
|
---|
| 720 |
|
---|
[2575] | 721 | static int oi=1;
|
---|
| 722 | if (MeshDebug)
|
---|
| 723 | cout<<"Object "<<oi++;
|
---|
[752] | 724 |
|
---|
[2575] | 725 | hits += object->CastRay(ray);
|
---|
[752] | 726 |
|
---|
[2575] | 727 | if (MeshDebug) {
|
---|
| 728 | if (!ray.intersections.empty())
|
---|
| 729 | cout<<"nearest t="<<ray.intersections[0].mT<<endl;
|
---|
| 730 | else
|
---|
| 731 | cout<<"nearest t=-INF"<<endl;
|
---|
| 732 | }
|
---|
| 733 | }
|
---|
| 734 | }
|
---|
[752] | 735 |
|
---|
[2575] | 736 | if (hits && ray.GetType() == Ray::LOCAL_RAY)
|
---|
| 737 | if (ray.intersections[0].mT <= maxt)
|
---|
| 738 | break;
|
---|
| 739 |
|
---|
| 740 | // get the next node from the stack
|
---|
| 741 | if (tStack.empty())
|
---|
| 742 | break;
|
---|
| 743 |
|
---|
| 744 | entp = extp;
|
---|
| 745 | mint = maxt;
|
---|
| 746 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
|
---|
| 747 | break;
|
---|
| 748 |
|
---|
| 749 | RayTraversalData &s = tStack.top();
|
---|
| 750 | node = s.mNode;
|
---|
| 751 | extp = s.mExitPoint;
|
---|
| 752 | maxt = s.mMaxT;
|
---|
| 753 | tStack.pop();
|
---|
| 754 | }
|
---|
[372] | 755 | }
|
---|
| 756 | return hits;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
[469] | 759 | int KdTree::CastLineSegment(const Vector3 &origin,
|
---|
[2575] | 760 | const Vector3 &termination,
|
---|
| 761 | ViewCellContainer &viewcells)
|
---|
[469] | 762 | {
|
---|
[2575] | 763 | int hits = 0;
|
---|
[2017] | 764 |
|
---|
[2575] | 765 | float mint = 0.0f, maxt = 1.0f;
|
---|
| 766 | const Vector3 dir = termination - origin;
|
---|
| 767 |
|
---|
| 768 | stack<RayTraversalData> tStack;
|
---|
| 769 |
|
---|
| 770 | Intersectable::NewMail();
|
---|
| 771 |
|
---|
| 772 | //maxt += Limits::Threshold;
|
---|
| 773 |
|
---|
| 774 | Vector3 entp = origin;
|
---|
| 775 | Vector3 extp = termination;
|
---|
| 776 |
|
---|
| 777 | KdNode *node = mRoot;
|
---|
| 778 | KdNode *farChild;
|
---|
| 779 |
|
---|
| 780 | float position;
|
---|
| 781 | int axis;
|
---|
[469] | 782 |
|
---|
[2575] | 783 | while (1)
|
---|
| 784 | {
|
---|
| 785 | if (!node->IsLeaf())
|
---|
| 786 | {
|
---|
| 787 | KdInterior *in = static_cast<KdInterior *>(node);
|
---|
| 788 | position = in->mPosition;
|
---|
| 789 | axis = in->mAxis;
|
---|
| 790 |
|
---|
| 791 | if (entp[axis] <= position)
|
---|
| 792 | {
|
---|
| 793 | if (extp[axis] <= position)
|
---|
[469] | 794 | {
|
---|
[2575] | 795 | node = in->mBack;
|
---|
| 796 | // cases N1,N2,N3,P5,Z2,Z3
|
---|
| 797 | continue;
|
---|
| 798 | }
|
---|
| 799 | else
|
---|
| 800 | {
|
---|
| 801 | // case N4
|
---|
| 802 | node = in->mBack;
|
---|
| 803 | farChild = in->mFront;
|
---|
[469] | 804 | }
|
---|
[2575] | 805 | }
|
---|
| 806 | else
|
---|
| 807 | {
|
---|
| 808 | if (position <= extp[axis])
|
---|
| 809 | {
|
---|
| 810 | node = in->mFront;
|
---|
| 811 | // cases P1,P2,P3,N5,Z1
|
---|
| 812 | continue;
|
---|
| 813 | }
|
---|
| 814 | else
|
---|
| 815 | {
|
---|
| 816 | node = in->mFront;
|
---|
| 817 | farChild = in->mBack;
|
---|
| 818 | // case P4
|
---|
| 819 | }
|
---|
| 820 | }
|
---|
| 821 |
|
---|
| 822 | // $$ modification 3.5.2004 - hints from Kamil Ghais
|
---|
| 823 | // case N4 or P4
|
---|
| 824 | float tdist = (position - origin[axis]) / dir[axis];
|
---|
| 825 | //tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
|
---|
| 826 | extp = origin + dir * tdist;
|
---|
| 827 | maxt = tdist;
|
---|
| 828 | }
|
---|
| 829 | else
|
---|
| 830 | {
|
---|
| 831 | // compute intersection with all objects in this leaf
|
---|
| 832 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
| 833 |
|
---|
| 834 | // add view cell to intersections
|
---|
| 835 | ViewCell *vc = leaf->mViewCell;
|
---|
| 836 |
|
---|
| 837 | if (!vc->Mailed())
|
---|
| 838 | {
|
---|
| 839 | vc->Mail();
|
---|
| 840 | viewcells.push_back(vc);
|
---|
| 841 | ++ hits;
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 | // get the next node from the stack
|
---|
| 845 | if (tStack.empty())
|
---|
| 846 | break;
|
---|
| 847 |
|
---|
| 848 | entp = extp;
|
---|
| 849 | mint = maxt;
|
---|
| 850 |
|
---|
| 851 | RayTraversalData &s = tStack.top();
|
---|
| 852 | node = s.mNode;
|
---|
| 853 | extp = s.mExitPoint;
|
---|
| 854 | maxt = s.mMaxT;
|
---|
| 855 | tStack.pop();
|
---|
| 856 | }
|
---|
| 857 | }
|
---|
| 858 | return hits;
|
---|
[469] | 859 | }
|
---|
| 860 |
|
---|
[1974] | 861 | void
|
---|
| 862 | KdTree::CollectKdObjects(const AxisAlignedBox3 &box,
|
---|
[2575] | 863 | ObjectContainer &objects
|
---|
| 864 | )
|
---|
[1974] | 865 | {
|
---|
| 866 | stack<KdNode *> nodeStack;
|
---|
| 867 |
|
---|
| 868 | nodeStack.push(mRoot);
|
---|
[469] | 869 |
|
---|
[1974] | 870 | while (!nodeStack.empty()) {
|
---|
| 871 | KdNode *node = nodeStack.top();
|
---|
| 872 | nodeStack.pop();
|
---|
[2575] | 873 | if (node->IsLeaf() || node->mPvsTermination == 1) {
|
---|
| 874 | Intersectable *object = GetOrCreateKdIntersectable(node);
|
---|
| 875 | if (!node->Mailed()) {
|
---|
| 876 | node->Mail();
|
---|
| 877 | objects.push_back(object);
|
---|
| 878 | }
|
---|
| 879 | }
|
---|
| 880 | else {
|
---|
[1974] | 881 | KdInterior *interior = (KdInterior *)node;
|
---|
| 882 |
|
---|
[2575] | 883 | if ( box.Max()[interior->mAxis] > interior->mPosition )
|
---|
| 884 | nodeStack.push(interior->mFront);
|
---|
| 885 |
|
---|
| 886 | if (box.Min()[interior->mAxis] < interior->mPosition)
|
---|
| 887 | nodeStack.push(interior->mBack);
|
---|
[1974] | 888 | }
|
---|
[2575] | 889 | } // while
|
---|
[1974] | 890 | }
|
---|
| 891 |
|
---|
[372] | 892 | void
|
---|
[859] | 893 | KdTree::CollectObjects(const AxisAlignedBox3 &box,
|
---|
[2575] | 894 | ObjectContainer &objects)
|
---|
[859] | 895 | {
|
---|
| 896 | stack<KdNode *> nodeStack;
|
---|
| 897 |
|
---|
| 898 | nodeStack.push(mRoot);
|
---|
| 899 |
|
---|
| 900 | while (!nodeStack.empty()) {
|
---|
| 901 | KdNode *node = nodeStack.top();
|
---|
| 902 | nodeStack.pop();
|
---|
| 903 | if (node->IsLeaf()) {
|
---|
| 904 | KdLeaf *leaf = (KdLeaf *)node;
|
---|
| 905 | for (int j=0; j < leaf->mObjects.size(); j++) {
|
---|
[2575] | 906 | Intersectable *object = leaf->mObjects[j];
|
---|
| 907 | if (!object->Mailed() && Overlap(box, object->GetBox())) {
|
---|
| 908 | object->Mail();
|
---|
| 909 | objects.push_back(object);
|
---|
| 910 | }
|
---|
[859] | 911 | }
|
---|
| 912 | } else {
|
---|
| 913 | KdInterior *interior = (KdInterior *)node;
|
---|
| 914 |
|
---|
[2575] | 915 | if ( box.Max()[interior->mAxis] > interior->mPosition )
|
---|
| 916 | nodeStack.push(interior->mFront);
|
---|
| 917 |
|
---|
| 918 | if (box.Min()[interior->mAxis] < interior->mPosition)
|
---|
| 919 | nodeStack.push(interior->mBack);
|
---|
[859] | 920 | }
|
---|
| 921 | }
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | void
|
---|
[372] | 925 | KdTree::CollectObjects(KdNode *n, ObjectContainer &objects)
|
---|
| 926 | {
|
---|
| 927 | stack<KdNode *> nodeStack;
|
---|
| 928 |
|
---|
| 929 | nodeStack.push(n);
|
---|
| 930 |
|
---|
| 931 | while (!nodeStack.empty()) {
|
---|
| 932 | KdNode *node = nodeStack.top();
|
---|
| 933 | nodeStack.pop();
|
---|
| 934 | if (node->IsLeaf()) {
|
---|
| 935 | KdLeaf *leaf = (KdLeaf *)node;
|
---|
| 936 | for (int j=0; j < leaf->mObjects.size(); j++) {
|
---|
[2575] | 937 | Intersectable *object = leaf->mObjects[j];
|
---|
| 938 | if (!object->Mailed()) {
|
---|
| 939 | object->Mail();
|
---|
| 940 | objects.push_back(object);
|
---|
| 941 | }
|
---|
[372] | 942 | }
|
---|
| 943 | } else {
|
---|
| 944 | KdInterior *interior = (KdInterior *)node;
|
---|
| 945 | nodeStack.push(interior->mFront);
|
---|
| 946 | nodeStack.push(interior->mBack);
|
---|
| 947 | }
|
---|
| 948 | }
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | // Find random neighbor which was not mailed
|
---|
| 952 | KdNode *
|
---|
| 953 | KdTree::FindRandomNeighbor(KdNode *n,
|
---|
[2575] | 954 | bool onlyUnmailed
|
---|
| 955 | )
|
---|
[372] | 956 | {
|
---|
| 957 | stack<KdNode *> nodeStack;
|
---|
| 958 |
|
---|
| 959 | nodeStack.push(mRoot);
|
---|
| 960 |
|
---|
| 961 | AxisAlignedBox3 box = GetBox(n);
|
---|
| 962 | int mask = rand();
|
---|
| 963 |
|
---|
| 964 | while (!nodeStack.empty()) {
|
---|
| 965 | KdNode *node = nodeStack.top();
|
---|
| 966 | nodeStack.pop();
|
---|
| 967 | if (node->IsLeaf()) {
|
---|
| 968 | if ( node != n && (!onlyUnmailed || !node->Mailed()) )
|
---|
| 969 | return node;
|
---|
| 970 | } else {
|
---|
| 971 | KdInterior *interior = (KdInterior *)node;
|
---|
| 972 | if (interior->mPosition > box.Max(interior->mAxis))
|
---|
| 973 | nodeStack.push(interior->mBack);
|
---|
| 974 | else
|
---|
| 975 | if (interior->mPosition < box.Min(interior->mAxis))
|
---|
| 976 | nodeStack.push(interior->mFront);
|
---|
| 977 | else {
|
---|
| 978 | // random decision
|
---|
| 979 | if (mask&1)
|
---|
| 980 | nodeStack.push(interior->mBack);
|
---|
| 981 | else
|
---|
| 982 | nodeStack.push(interior->mFront);
|
---|
| 983 | mask = mask>>1;
|
---|
| 984 | }
|
---|
| 985 | }
|
---|
| 986 | }
|
---|
| 987 |
|
---|
| 988 | return NULL;
|
---|
| 989 | }
|
---|
| 990 |
|
---|
| 991 | int
|
---|
| 992 | KdTree::FindNeighbors(KdNode *n,
|
---|
| 993 | vector<KdNode *> &neighbors,
|
---|
| 994 | bool onlyUnmailed
|
---|
| 995 | )
|
---|
| 996 | {
|
---|
| 997 | stack<KdNode *> nodeStack;
|
---|
| 998 |
|
---|
| 999 | nodeStack.push(mRoot);
|
---|
| 1000 |
|
---|
| 1001 | AxisAlignedBox3 box = GetBox(n);
|
---|
| 1002 |
|
---|
| 1003 | while (!nodeStack.empty()) {
|
---|
| 1004 | KdNode *node = nodeStack.top();
|
---|
| 1005 | nodeStack.pop();
|
---|
| 1006 | if (node->IsLeaf()) {
|
---|
| 1007 | if ( node != n && (!onlyUnmailed || !node->Mailed()) )
|
---|
| 1008 | neighbors.push_back(node);
|
---|
[2575] | 1009 | }
|
---|
| 1010 | else {
|
---|
[372] | 1011 | KdInterior *interior = (KdInterior *)node;
|
---|
| 1012 | if (interior->mPosition > box.Max(interior->mAxis))
|
---|
[2575] | 1013 | nodeStack.push(interior->mBack);
|
---|
| 1014 | else {
|
---|
| 1015 | if (interior->mPosition < box.Min(interior->mAxis))
|
---|
| 1016 | nodeStack.push(interior->mFront);
|
---|
| 1017 | else {
|
---|
| 1018 | // random decision
|
---|
| 1019 | nodeStack.push(interior->mBack);
|
---|
| 1020 | nodeStack.push(interior->mFront);
|
---|
| 1021 | }
|
---|
| 1022 | }
|
---|
[372] | 1023 | }
|
---|
| 1024 | }
|
---|
| 1025 |
|
---|
[469] | 1026 | return (int)neighbors.size();
|
---|
[372] | 1027 | }
|
---|
| 1028 |
|
---|
| 1029 | // Find random neighbor which was not mailed
|
---|
| 1030 | KdNode *
|
---|
| 1031 | KdTree::GetRandomLeaf(const Plane3 &plane)
|
---|
| 1032 | {
|
---|
| 1033 | stack<KdNode *> nodeStack;
|
---|
| 1034 |
|
---|
| 1035 | nodeStack.push(mRoot);
|
---|
| 1036 |
|
---|
| 1037 | int mask = rand();
|
---|
| 1038 |
|
---|
| 1039 | while (!nodeStack.empty()) {
|
---|
| 1040 | KdNode *node = nodeStack.top();
|
---|
| 1041 | nodeStack.pop();
|
---|
| 1042 | if (node->IsLeaf()) {
|
---|
| 1043 | return node;
|
---|
| 1044 | } else {
|
---|
| 1045 | KdInterior *interior = (KdInterior *)node;
|
---|
| 1046 | KdNode *next;
|
---|
| 1047 | if (GetBox(interior->mBack).Side(plane) < 0)
|
---|
| 1048 | next = interior->mFront;
|
---|
| 1049 | else
|
---|
| 1050 | if (GetBox(interior->mFront).Side(plane) < 0)
|
---|
| 1051 | next = interior->mBack;
|
---|
| 1052 | else {
|
---|
| 1053 | // random decision
|
---|
| 1054 | if (mask&1)
|
---|
| 1055 | next = interior->mBack;
|
---|
| 1056 | else
|
---|
| 1057 | next = interior->mFront;
|
---|
| 1058 | mask = mask>>1;
|
---|
| 1059 | }
|
---|
| 1060 | nodeStack.push(next);
|
---|
| 1061 | }
|
---|
| 1062 | }
|
---|
| 1063 |
|
---|
| 1064 |
|
---|
| 1065 | return NULL;
|
---|
| 1066 | }
|
---|
| 1067 |
|
---|
| 1068 | void
|
---|
| 1069 | KdTree::CollectLeaves(vector<KdLeaf *> &leaves)
|
---|
| 1070 | {
|
---|
| 1071 | stack<KdNode *> nodeStack;
|
---|
| 1072 | nodeStack.push(mRoot);
|
---|
| 1073 |
|
---|
| 1074 | while (!nodeStack.empty()) {
|
---|
| 1075 | KdNode *node = nodeStack.top();
|
---|
| 1076 | nodeStack.pop();
|
---|
| 1077 | if (node->IsLeaf()) {
|
---|
| 1078 | KdLeaf *leaf = (KdLeaf *)node;
|
---|
| 1079 | leaves.push_back(leaf);
|
---|
| 1080 | } else {
|
---|
| 1081 | KdInterior *interior = (KdInterior *)node;
|
---|
| 1082 | nodeStack.push(interior->mBack);
|
---|
| 1083 | nodeStack.push(interior->mFront);
|
---|
| 1084 | }
|
---|
| 1085 | }
|
---|
| 1086 | }
|
---|
| 1087 |
|
---|
[469] | 1088 | void
|
---|
| 1089 | KdTree::CreateAndCollectViewCells(ViewCellContainer &vc) const
|
---|
| 1090 | {
|
---|
| 1091 | stack<KdNode *> nodeStack;
|
---|
| 1092 | nodeStack.push(mRoot);
|
---|
[372] | 1093 |
|
---|
[469] | 1094 | while (!nodeStack.empty()) {
|
---|
| 1095 | KdNode *node = nodeStack.top();
|
---|
| 1096 | nodeStack.pop();
|
---|
| 1097 | if (node->IsLeaf()) {
|
---|
| 1098 | KdLeaf *leaf = (KdLeaf *)node;
|
---|
| 1099 | // kdtree used as view cell container => create view cell
|
---|
[471] | 1100 | KdViewCell *viewCell = new KdViewCell();
|
---|
| 1101 | leaf->mViewCell = viewCell;
|
---|
| 1102 | // push back pointer to this leaf
|
---|
[1551] | 1103 | viewCell->mLeaves[0] = leaf;
|
---|
[471] | 1104 | vc.push_back(viewCell);
|
---|
[469] | 1105 | } else {
|
---|
| 1106 | KdInterior *interior = (KdInterior *)node;
|
---|
| 1107 | nodeStack.push(interior->mBack);
|
---|
| 1108 | nodeStack.push(interior->mFront);
|
---|
| 1109 | }
|
---|
| 1110 | }
|
---|
| 1111 | }
|
---|
| 1112 |
|
---|
[1736] | 1113 |
|
---|
[372] | 1114 | KdNode *
|
---|
| 1115 | KdTree::GetRandomLeaf(const bool onlyUnmailed)
|
---|
| 1116 | {
|
---|
[507] | 1117 | stack<KdNode *> nodeStack;
|
---|
[372] | 1118 | nodeStack.push(mRoot);
|
---|
[507] | 1119 |
|
---|
| 1120 | int mask = rand();
|
---|
[372] | 1121 |
|
---|
| 1122 | while (!nodeStack.empty()) {
|
---|
| 1123 | KdNode *node = nodeStack.top();
|
---|
| 1124 | nodeStack.pop();
|
---|
| 1125 | if (node->IsLeaf()) {
|
---|
| 1126 | if ( (!onlyUnmailed || !node->Mailed()) )
|
---|
| 1127 | return node;
|
---|
| 1128 | } else {
|
---|
| 1129 | KdInterior *interior = (KdInterior *)node;
|
---|
| 1130 | // random decision
|
---|
| 1131 | if (mask&1)
|
---|
| 1132 | nodeStack.push(interior->mBack);
|
---|
| 1133 | else
|
---|
| 1134 | nodeStack.push(interior->mFront);
|
---|
| 1135 | mask = mask>>1;
|
---|
| 1136 | }
|
---|
| 1137 | }
|
---|
| 1138 | return NULL;
|
---|
| 1139 | }
|
---|
[504] | 1140 |
|
---|
| 1141 |
|
---|
[2539] | 1142 | int KdTree::CastBeam(Beam &beam)
|
---|
[504] | 1143 | {
|
---|
[507] | 1144 | stack<KdNode *> nodeStack;
|
---|
| 1145 | nodeStack.push(mRoot);
|
---|
[504] | 1146 |
|
---|
[507] | 1147 | while (!nodeStack.empty()) {
|
---|
| 1148 | KdNode *node = nodeStack.top();
|
---|
| 1149 | nodeStack.pop();
|
---|
| 1150 |
|
---|
| 1151 | int side = beam.ComputeIntersection(GetBox(node));
|
---|
| 1152 | switch (side) {
|
---|
| 1153 | case -1:
|
---|
| 1154 | beam.mKdNodes.push_back(node);
|
---|
| 1155 | break;
|
---|
| 1156 | case 0:
|
---|
| 1157 | if (node->IsLeaf())
|
---|
| 1158 | beam.mKdNodes.push_back(node);
|
---|
| 1159 | else {
|
---|
| 1160 | KdInterior *interior = (KdInterior *)node;
|
---|
| 1161 | KdNode *first = interior->mBack;
|
---|
| 1162 | KdNode *second = interior->mFront;
|
---|
| 1163 |
|
---|
| 1164 | if (interior->mAxis < 3) {
|
---|
| 1165 | // spatial split -> decide on the order of the nodes
|
---|
| 1166 | if (beam.mPlanes[0].mNormal[interior->mAxis] > 0)
|
---|
| 1167 | swap(first, second);
|
---|
| 1168 | }
|
---|
[504] | 1169 |
|
---|
[507] | 1170 | nodeStack.push(first);
|
---|
| 1171 | nodeStack.push(second);
|
---|
| 1172 | }
|
---|
| 1173 | break;
|
---|
| 1174 | // default: cull
|
---|
| 1175 | }
|
---|
| 1176 | }
|
---|
| 1177 |
|
---|
[532] | 1178 | if (beam.mFlags & Beam::STORE_OBJECTS)
|
---|
| 1179 | {
|
---|
| 1180 | vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
[535] | 1181 |
|
---|
[532] | 1182 | Intersectable::NewMail();
|
---|
| 1183 | for (it = beam.mKdNodes.begin(); it != it_end; ++ it)
|
---|
| 1184 | {
|
---|
| 1185 | CollectObjects(*it, beam.mObjects);
|
---|
| 1186 | }
|
---|
| 1187 | }
|
---|
| 1188 |
|
---|
[837] | 1189 | return (int)beam.mKdNodes.size();
|
---|
[504] | 1190 | }
|
---|
[860] | 1191 |
|
---|
[1074] | 1192 |
|
---|
[1201] | 1193 | void KdTree::ExportBinLeaf(OUT_STREAM &stream, KdLeaf *leaf)
|
---|
[1194] | 1194 | {
|
---|
[2575] | 1195 | ObjectContainer::const_iterator it, it_end = leaf->mObjects.end();
|
---|
| 1196 |
|
---|
| 1197 | int type = TYPE_LEAF;
|
---|
| 1198 | int size = (int)leaf->mObjects.size();
|
---|
| 1199 |
|
---|
| 1200 | stream.write(reinterpret_cast<char *>(&type), sizeof(int));
|
---|
| 1201 | stream.write(reinterpret_cast<char *>(&size), sizeof(int));
|
---|
| 1202 |
|
---|
| 1203 | for (it = leaf->mObjects.begin(); it != it_end; ++ it)
|
---|
| 1204 | {
|
---|
| 1205 | Intersectable *obj = *it;
|
---|
| 1206 | int id = obj->mId;
|
---|
| 1207 |
|
---|
| 1208 | //stream.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 1209 | stream.write(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
[1194] | 1210 | }
|
---|
[878] | 1211 | }
|
---|
[1194] | 1212 |
|
---|
| 1213 |
|
---|
[1201] | 1214 | KdLeaf *KdTree::ImportBinLeaf(IN_STREAM &stream,
|
---|
[2575] | 1215 | KdInterior *parent,
|
---|
| 1216 | const ObjectContainer &objects)
|
---|
[1194] | 1217 | {
|
---|
[2575] | 1218 | int leafId = TYPE_LEAF;
|
---|
| 1219 | int objId = leafId;
|
---|
| 1220 | int size;
|
---|
| 1221 |
|
---|
| 1222 | stream.read(reinterpret_cast<char *>(&size), sizeof(int));
|
---|
| 1223 | KdLeaf *leaf = new KdLeaf(parent, size);
|
---|
[1196] | 1224 |
|
---|
[2575] | 1225 | MeshInstance dummyInst(NULL);
|
---|
[1633] | 1226 |
|
---|
[2575] | 1227 | // read object ids
|
---|
| 1228 | // note: this can also be done geometrically
|
---|
| 1229 | for (int i = 0; i < size; ++ i)
|
---|
| 1230 | {
|
---|
| 1231 | stream.read(reinterpret_cast<char *>(&objId), sizeof(int));
|
---|
| 1232 | dummyInst.SetId(objId);
|
---|
| 1233 |
|
---|
| 1234 | ObjectContainer::const_iterator oit =
|
---|
| 1235 | lower_bound(objects.begin(), objects.end(), (Intersectable *)&dummyInst, ilt);
|
---|
| 1236 |
|
---|
| 1237 | if ((oit != objects.end()) && ((*oit)->GetId() == objId))
|
---|
| 1238 | leaf->mObjects.push_back(*oit);
|
---|
| 1239 | else
|
---|
| 1240 | Debug << "error: object with id " << objId << " does not exist" << endl;
|
---|
| 1241 | }
|
---|
| 1242 |
|
---|
| 1243 | return leaf;
|
---|
[1194] | 1244 | }
|
---|
| 1245 |
|
---|
| 1246 |
|
---|
[1201] | 1247 | void KdTree::ExportBinInterior(OUT_STREAM &stream, KdInterior *interior)
|
---|
[1194] | 1248 | {
|
---|
[1196] | 1249 | int interiorid = TYPE_INTERIOR;
|
---|
[1194] | 1250 | stream.write(reinterpret_cast<char *>(&interiorid), sizeof(int));
|
---|
| 1251 |
|
---|
| 1252 | int axis = interior->mAxis;
|
---|
[1196] | 1253 | float pos = interior->mPosition;
|
---|
[1194] | 1254 |
|
---|
| 1255 | stream.write(reinterpret_cast<char *>(&axis), sizeof(int));
|
---|
[1196] | 1256 | stream.write(reinterpret_cast<char *>(&pos), sizeof(float));
|
---|
[1194] | 1257 | }
|
---|
| 1258 |
|
---|
| 1259 |
|
---|
[1201] | 1260 | KdInterior *KdTree::ImportBinInterior(IN_STREAM &stream, KdInterior *parent)
|
---|
[1194] | 1261 | {
|
---|
[1196] | 1262 | KdInterior *interior = new KdInterior(parent);
|
---|
[1194] | 1263 |
|
---|
[2539] | 1264 | int axis;
|
---|
| 1265 | float pos;
|
---|
[1194] | 1266 |
|
---|
| 1267 | stream.read(reinterpret_cast<char *>(&axis), sizeof(int));
|
---|
[1196] | 1268 | stream.read(reinterpret_cast<char *>(&pos), sizeof(float));
|
---|
| 1269 |
|
---|
| 1270 | interior->mAxis = axis;
|
---|
| 1271 | interior->mPosition = pos;
|
---|
| 1272 |
|
---|
| 1273 | return interior;
|
---|
[1194] | 1274 | }
|
---|
| 1275 |
|
---|
| 1276 |
|
---|
| 1277 | bool KdTree::ExportBinTree(const string &filename)
|
---|
| 1278 | {
|
---|
[1201] | 1279 | OUT_STREAM stream(filename.c_str(), OUT_BIN_MODE);
|
---|
[1194] | 1280 |
|
---|
[2332] | 1281 | if (!stream.is_open()) return false;
|
---|
[1194] | 1282 |
|
---|
| 1283 | // export binary version of mesh
|
---|
[1197] | 1284 | queue<KdNode *> tStack;
|
---|
[1194] | 1285 | tStack.push(mRoot);
|
---|
| 1286 |
|
---|
| 1287 | while(!tStack.empty())
|
---|
| 1288 | {
|
---|
[1197] | 1289 | KdNode *node = tStack.front();
|
---|
[1196] | 1290 | tStack.pop();
|
---|
| 1291 |
|
---|
| 1292 | if (node->IsLeaf())
|
---|
| 1293 | {
|
---|
[1197] | 1294 | //Debug << "l";
|
---|
[2017] | 1295 | ExportBinLeaf(stream, static_cast<KdLeaf *>(node));
|
---|
[1196] | 1296 | }
|
---|
| 1297 | else
|
---|
| 1298 | {
|
---|
[1197] | 1299 | //Debug << "i";
|
---|
[2017] | 1300 | KdInterior *interior = static_cast<KdInterior *>(node);
|
---|
[1194] | 1301 |
|
---|
[1196] | 1302 | ExportBinInterior(stream, interior);
|
---|
| 1303 |
|
---|
| 1304 | tStack.push(interior->mFront);
|
---|
| 1305 | tStack.push(interior->mBack);
|
---|
[1194] | 1306 | }
|
---|
| 1307 | }
|
---|
| 1308 |
|
---|
| 1309 | return true;
|
---|
| 1310 | }
|
---|
| 1311 |
|
---|
| 1312 |
|
---|
[2539] | 1313 | KdNode *KdTree::ImportNextNode(IN_STREAM &stream,
|
---|
| 1314 | KdInterior *parent,
|
---|
| 1315 | const ObjectContainer &objects)
|
---|
[1194] | 1316 | {
|
---|
[1196] | 1317 | int nodeType;
|
---|
| 1318 | stream.read(reinterpret_cast<char *>(&nodeType), sizeof(int));
|
---|
| 1319 |
|
---|
| 1320 | if (nodeType == TYPE_LEAF)
|
---|
[2017] | 1321 | return ImportBinLeaf(stream, static_cast<KdInterior *>(parent), objects);
|
---|
[1196] | 1322 |
|
---|
| 1323 | if (nodeType == TYPE_INTERIOR)
|
---|
[2017] | 1324 | return ImportBinInterior(stream, static_cast<KdInterior *>(parent));
|
---|
[1196] | 1325 |
|
---|
| 1326 | Debug << "error! loading failed!" << endl;
|
---|
| 1327 | return NULL;
|
---|
| 1328 | }
|
---|
| 1329 |
|
---|
| 1330 |
|
---|
[2539] | 1331 | bool KdTree::ImportBinTree(const string &filename, ObjectContainer &objects)
|
---|
[1196] | 1332 | {
|
---|
[2575] | 1333 | // export binary version of mesh
|
---|
| 1334 | queue<TraversalData> tStack;
|
---|
| 1335 | IN_STREAM stream(filename.c_str(), IN_BIN_MODE);
|
---|
| 1336 |
|
---|
| 1337 | if (!stream.is_open()) return false;
|
---|
| 1338 |
|
---|
| 1339 | // sort objects by their id
|
---|
| 1340 | // if (!is_sorted(objects.begin(), objects.end(), ilt))
|
---|
| 1341 | sort(objects.begin(), objects.end(), ilt);
|
---|
| 1342 |
|
---|
| 1343 | mBox.Initialize();
|
---|
| 1344 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 1345 |
|
---|
| 1346 | ///////////////////////////
|
---|
| 1347 | //-- compute bounding box of object space
|
---|
| 1348 |
|
---|
| 1349 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 1350 | {
|
---|
| 1351 | const AxisAlignedBox3 box = (*oit)->GetBox();
|
---|
| 1352 | mBox.Include(box);
|
---|
| 1353 | }
|
---|
| 1354 |
|
---|
| 1355 | // hack: we make a new root
|
---|
| 1356 | DEL_PTR(mRoot);
|
---|
| 1357 |
|
---|
| 1358 | mRoot = ImportNextNode(stream, NULL, objects);
|
---|
| 1359 |
|
---|
| 1360 | tStack.push(TraversalData(mRoot, mBox, 0));
|
---|
| 1361 | mStat.Reset();
|
---|
| 1362 | mStat.nodes = 1;
|
---|
| 1363 |
|
---|
| 1364 | while(!tStack.empty())
|
---|
| 1365 | {
|
---|
| 1366 | TraversalData tData = tStack.front();
|
---|
| 1367 | tStack.pop();
|
---|
| 1368 |
|
---|
| 1369 | KdNode *node = tData.mNode;
|
---|
| 1370 |
|
---|
| 1371 | if (!node->IsLeaf())
|
---|
| 1372 | {
|
---|
| 1373 | mStat.nodes += 2;
|
---|
| 1374 |
|
---|
| 1375 | //Debug << "i" ;
|
---|
| 1376 | KdInterior *interior = static_cast<KdInterior *>(node);
|
---|
| 1377 | interior->mBox = tData.mBox;
|
---|
| 1378 |
|
---|
| 1379 | KdNode *front = ImportNextNode(stream, interior, objects);
|
---|
| 1380 | KdNode *back = ImportNextNode(stream, interior, objects);
|
---|
| 1381 |
|
---|
| 1382 | interior->SetupChildLinks(back, front);
|
---|
| 1383 |
|
---|
| 1384 | ++ mStat.splits[interior->mAxis];
|
---|
| 1385 |
|
---|
| 1386 | // compute new bounding box
|
---|
| 1387 | AxisAlignedBox3 frontBox, backBox;
|
---|
| 1388 |
|
---|
| 1389 | tData.mBox.Split(interior->mAxis,
|
---|
| 1390 | interior->mPosition,
|
---|
| 1391 | frontBox,
|
---|
| 1392 | backBox);
|
---|
| 1393 |
|
---|
| 1394 | tStack.push(TraversalData(front, frontBox, tData.mDepth + 1));
|
---|
| 1395 | tStack.push(TraversalData(back, backBox, tData.mDepth + 1));
|
---|
| 1396 | }
|
---|
| 1397 | else
|
---|
| 1398 | {
|
---|
| 1399 | EvaluateLeafStats(tData);
|
---|
| 1400 | //cout << "l";
|
---|
| 1401 | }
|
---|
| 1402 | }
|
---|
[1197] | 1403 |
|
---|
[2575] | 1404 | float area = GetBox().SurfaceArea()*mKdPvsArea;
|
---|
[1197] | 1405 |
|
---|
[2575] | 1406 | SetPvsTerminationNodes(area);
|
---|
| 1407 |
|
---|
| 1408 | Debug << mStat << endl;
|
---|
| 1409 |
|
---|
| 1410 | return true;
|
---|
[1194] | 1411 | }
|
---|
| 1412 |
|
---|
[1999] | 1413 |
|
---|
[1594] | 1414 | KdIntersectable *
|
---|
| 1415 | KdTree::GetOrCreateKdIntersectable(KdNode *node)
|
---|
| 1416 | {
|
---|
[2575] | 1417 | if (node == NULL)
|
---|
| 1418 | return NULL;
|
---|
[1194] | 1419 |
|
---|
[2116] | 1420 | if (node->mIntersectable == NULL)
|
---|
| 1421 | {
|
---|
| 1422 | // not in map => create new entry
|
---|
[2117] | 1423 | const int id = (int)mKdIntersectables.size();
|
---|
[2571] | 1424 | KdIntersectable *kdObj = new KdIntersectable(node, GetBox(node));
|
---|
[2117] | 1425 | node->mIntersectable = kdObj;
|
---|
| 1426 |
|
---|
| 1427 | mKdIntersectables.push_back(kdObj);
|
---|
| 1428 | kdObj->SetId(id);
|
---|
| 1429 | #ifdef USE_BIT_PVS
|
---|
[2575] | 1430 | // hack: for kd pvs the kd intersecables are the pvs objects
|
---|
| 1431 | ObjectPvsIterator::sObjects.push_back(kdObj);
|
---|
[2117] | 1432 | #endif
|
---|
[2575] | 1433 | }
|
---|
[1761] | 1434 |
|
---|
[2575] | 1435 | return node->mIntersectable;
|
---|
[1387] | 1436 | }
|
---|
[1594] | 1437 |
|
---|
[1989] | 1438 |
|
---|
| 1439 | void
|
---|
[2575] | 1440 | KdTree::SetPvsTerminationNodes(const float maxArea)
|
---|
[1989] | 1441 | {
|
---|
| 1442 | stack<KdNode *> nodeStack;
|
---|
| 1443 |
|
---|
| 1444 | nodeStack.push(mRoot);
|
---|
| 1445 |
|
---|
[1995] | 1446 | float area = 0.0f;
|
---|
| 1447 | float radius = 0.0f;
|
---|
| 1448 | int nodes = 0;
|
---|
| 1449 |
|
---|
[1989] | 1450 | while (!nodeStack.empty()) {
|
---|
| 1451 | KdNode *node = nodeStack.top();
|
---|
[2575] | 1452 | nodeStack.pop();
|
---|
[1989] | 1453 |
|
---|
[2575] | 1454 | node->mPvsTermination = 0;
|
---|
| 1455 | if (node->IsLeaf() || (GetSurfaceArea(node) <= maxArea) ) {
|
---|
| 1456 | area += GetSurfaceArea(node);
|
---|
| 1457 | radius += GetBox(node).Radius();
|
---|
| 1458 | nodes++;
|
---|
| 1459 | node->mPvsTermination = 1;
|
---|
| 1460 | // create dummy kd intersectable
|
---|
| 1461 | Intersectable *object = GetOrCreateKdIntersectable(node);
|
---|
| 1462 | } else {
|
---|
| 1463 | KdInterior *interior = (KdInterior *)node;
|
---|
| 1464 | nodeStack.push(interior->mFront);
|
---|
| 1465 | nodeStack.push(interior->mBack);
|
---|
[1989] | 1466 | }
|
---|
| 1467 | }
|
---|
[2575] | 1468 |
|
---|
[1995] | 1469 | if (nodes) {
|
---|
[2575] | 1470 | area /= nodes;
|
---|
| 1471 | radius /= nodes;
|
---|
| 1472 | cout<<"Number of nodes for storing in the PVS = "<<nodes<<endl;
|
---|
| 1473 | cout<<"Average rel. node area = "<<area/GetSurfaceArea(mRoot)<<endl;
|
---|
| 1474 | cout<<"Average rel. node radius = "<<radius/GetBox(mRoot).Radius()<<endl;
|
---|
| 1475 | cout<<"Avg node radius = "<<radius<<endl;
|
---|
[1995] | 1476 | }
|
---|
| 1477 |
|
---|
[1989] | 1478 | }
|
---|
| 1479 |
|
---|
[1594] | 1480 | KdNode *
|
---|
[1989] | 1481 | KdTree::GetPvsNode(const Vector3 &point) const
|
---|
| 1482 | {
|
---|
| 1483 | KdNode *node = mRoot;
|
---|
| 1484 |
|
---|
| 1485 | while (node->mPvsTermination == 0 ) {
|
---|
[2575] | 1486 | KdInterior *inter = (KdInterior *)node;
|
---|
| 1487 | if (point[inter->mAxis] < inter->mPosition)
|
---|
| 1488 | node = inter->mBack;
|
---|
| 1489 | else
|
---|
| 1490 | node = inter->mFront;
|
---|
[1989] | 1491 | }
|
---|
| 1492 |
|
---|
| 1493 | return node;
|
---|
| 1494 | }
|
---|
| 1495 |
|
---|
| 1496 | KdNode *
|
---|
[1594] | 1497 | KdTree::GetNode(const Vector3 &point,
|
---|
| 1498 | const float maxArea) const
|
---|
| 1499 | {
|
---|
| 1500 | KdNode *node = mRoot;
|
---|
| 1501 |
|
---|
| 1502 | while (!node->IsLeaf() && (GetSurfaceArea(node) > maxArea) ) {
|
---|
[2575] | 1503 | KdInterior *inter = (KdInterior *)node;
|
---|
| 1504 | if (point[inter->mAxis] < inter->mPosition)
|
---|
| 1505 | node = inter->mBack;
|
---|
| 1506 | else
|
---|
| 1507 | node = inter->mFront;
|
---|
[1594] | 1508 | }
|
---|
| 1509 |
|
---|
| 1510 | return node;
|
---|
| 1511 | }
|
---|
| 1512 |
|
---|
| 1513 |
|
---|
[1999] | 1514 | void KdTree::GetBoxIntersections(const AxisAlignedBox3 &box,
|
---|
[2575] | 1515 | vector<KdLeaf *> &leaves)
|
---|
[1999] | 1516 | {
|
---|
[2575] | 1517 | stack<KdNode *> tStack;
|
---|
| 1518 |
|
---|
| 1519 | tStack.push(mRoot);
|
---|
| 1520 |
|
---|
| 1521 | while (!tStack.empty())
|
---|
| 1522 | {
|
---|
| 1523 | KdNode *node = tStack.top();
|
---|
| 1524 | tStack.pop();
|
---|
| 1525 |
|
---|
| 1526 | if (node->IsLeaf())
|
---|
[1633] | 1527 | {
|
---|
[2575] | 1528 | leaves.push_back(static_cast<KdLeaf *>(node));
|
---|
[1999] | 1529 | }
|
---|
[2575] | 1530 | else // interior
|
---|
| 1531 | {
|
---|
| 1532 | KdInterior *interior = static_cast<KdInterior *>(node);
|
---|
| 1533 |
|
---|
| 1534 | if (box.Max(interior->mAxis) >= interior->mPosition)
|
---|
| 1535 | {
|
---|
| 1536 | tStack.push(interior->mFront);
|
---|
| 1537 | }
|
---|
| 1538 |
|
---|
| 1539 | if (box.Min(interior->mAxis) < interior->mPosition)
|
---|
| 1540 | {
|
---|
| 1541 | tStack.push(interior->mBack);
|
---|
| 1542 | }
|
---|
| 1543 | }
|
---|
| 1544 | }
|
---|
[1594] | 1545 | }
|
---|
[1633] | 1546 |
|
---|
[1999] | 1547 |
|
---|
| 1548 |
|
---|
[1633] | 1549 | }
|
---|