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