[408] | 1 |
|
---|
| 2 | // ================================================================
|
---|
| 3 | // $Id: lsds_kdtree.cpp,v 1.18 2005/04/16 09:34:21 bittner Exp $
|
---|
| 4 | // ****************************************************************
|
---|
| 5 | /**
|
---|
| 6 | The KD tree based LSDS
|
---|
| 7 | */
|
---|
| 8 | // Initial coding by
|
---|
| 9 | /**
|
---|
| 10 | @author Jiri Bittner
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | // Standard headers
|
---|
[406] | 14 | #include <stack>
|
---|
[408] | 15 | #include <queue>
|
---|
[406] | 16 | #include <algorithm>
|
---|
[408] | 17 | #include <fstream>
|
---|
| 18 | #include <string>
|
---|
| 19 |
|
---|
| 20 | #include "VspKdTree.h"
|
---|
| 21 |
|
---|
[406] | 22 | #include "Environment.h"
|
---|
[408] | 23 | #include "VssRay.h"
|
---|
| 24 | #include "Intersectable.h"
|
---|
| 25 | #include "Ray.h"
|
---|
[420] | 26 | #include "RayInfo.h"
|
---|
[462] | 27 | #include "ViewCell.h"
|
---|
[453] | 28 | #include "ViewCellsManager.h"
|
---|
[462] | 29 | #include "ViewCellBsp.h"
|
---|
[406] | 30 |
|
---|
[418] | 31 | // Static variables
|
---|
[462] | 32 | int VspKdLeaf::sMailId = 0;
|
---|
| 33 | int MergeCandidate::sMaxPvsSize = 150;
|
---|
[406] | 34 |
|
---|
[462] | 35 |
|
---|
[428] | 36 | #define USE_FIXEDPOINT_T 0
|
---|
| 37 |
|
---|
[420] | 38 | /// Adds object to the pvs of the front and back node
|
---|
[409] | 39 | inline void AddObject2Pvs(Intersectable *object,
|
---|
| 40 | const int side,
|
---|
| 41 | int &pvsBack,
|
---|
| 42 | int &pvsFront)
|
---|
[406] | 43 | {
|
---|
[408] | 44 | if (!object)
|
---|
| 45 | return;
|
---|
| 46 |
|
---|
[409] | 47 | if (side <= 0)
|
---|
| 48 | {
|
---|
| 49 | if (!object->Mailed() && !object->Mailed(2))
|
---|
| 50 | {
|
---|
| 51 | ++ pvsBack;
|
---|
| 52 |
|
---|
[408] | 53 | if (object->Mailed(1))
|
---|
| 54 | object->Mail(2);
|
---|
| 55 | else
|
---|
| 56 | object->Mail();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[428] | 60 | if (side >= 0)
|
---|
[409] | 61 | {
|
---|
| 62 | if (!object->Mailed(1) && !object->Mailed(2))
|
---|
| 63 | {
|
---|
| 64 | ++ pvsFront;
|
---|
[428] | 65 |
|
---|
[408] | 66 | if (object->Mailed())
|
---|
| 67 | object->Mail(2);
|
---|
| 68 | else
|
---|
| 69 | object->Mail(1);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
[406] | 72 | }
|
---|
| 73 |
|
---|
[418] | 74 | /**************************************************************/
|
---|
[462] | 75 | /* class VspKdNode implementation */
|
---|
[420] | 76 | /**************************************************************/
|
---|
| 77 |
|
---|
| 78 | // Inline constructor
|
---|
[462] | 79 | VspKdNode::VspKdNode(VspKdInterior *p):
|
---|
[420] | 80 | mParent(p), mAxis(-1), mDepth(p ? p->mDepth + 1 : 0)
|
---|
| 81 | {}
|
---|
| 82 |
|
---|
[462] | 83 | VspKdNode::~VspKdNode()
|
---|
[420] | 84 | {};
|
---|
| 85 |
|
---|
[462] | 86 | inline VspKdInterior *VspKdNode::GetParent() const
|
---|
[420] | 87 | {
|
---|
| 88 | return mParent;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[462] | 91 | inline void VspKdNode::SetParent(VspKdInterior *p)
|
---|
[420] | 92 | {
|
---|
| 93 | mParent = p;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[462] | 96 | bool VspKdNode::IsLeaf() const
|
---|
[420] | 97 | {
|
---|
| 98 | return mAxis == -1;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[462] | 101 | int VspKdNode::GetAccessTime()
|
---|
[420] | 102 | {
|
---|
| 103 | return 0x7FFFFFF;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**************************************************************/
|
---|
[462] | 107 | /* VspKdInterior implementation */
|
---|
[418] | 108 | /**************************************************************/
|
---|
[408] | 109 |
|
---|
[462] | 110 | VspKdInterior::VspKdInterior(VspKdInterior *p):
|
---|
| 111 | VspKdNode(p), mBack(NULL), mFront(NULL), mAccesses(0), mLastAccessTime(-1)
|
---|
[418] | 112 | {
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[462] | 115 | int VspKdInterior::GetAccessTime()
|
---|
[418] | 116 | {
|
---|
[419] | 117 | return mLastAccessTime;
|
---|
[418] | 118 | }
|
---|
| 119 |
|
---|
[462] | 120 | void VspKdInterior::SetupChildLinks(VspKdNode *b, VspKdNode *f)
|
---|
[418] | 121 | {
|
---|
| 122 | mBack = b;
|
---|
| 123 | mFront = f;
|
---|
[420] | 124 | b->SetParent(this);
|
---|
| 125 | f->SetParent(this);
|
---|
[418] | 126 | }
|
---|
| 127 |
|
---|
[462] | 128 | void VspKdInterior::ReplaceChildLink(VspKdNode *oldChild,
|
---|
| 129 | VspKdNode *newChild)
|
---|
[418] | 130 | {
|
---|
| 131 | if (mBack == oldChild)
|
---|
| 132 | mBack = newChild;
|
---|
| 133 | else
|
---|
| 134 | mFront = newChild;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[462] | 137 | int VspKdInterior::Type() const
|
---|
[418] | 138 | {
|
---|
[419] | 139 | return EInterior;
|
---|
[418] | 140 | }
|
---|
| 141 |
|
---|
[462] | 142 | VspKdInterior::~VspKdInterior()
|
---|
[418] | 143 | {
|
---|
| 144 | DEL_PTR(mBack);
|
---|
| 145 | DEL_PTR(mFront);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[462] | 148 | void VspKdInterior::Print(ostream &s) const
|
---|
[418] | 149 | {
|
---|
[423] | 150 | switch (mAxis)
|
---|
| 151 | {
|
---|
| 152 | case 0:
|
---|
| 153 | s << "x "; break;
|
---|
| 154 | case 1:
|
---|
| 155 | s << "y "; break;
|
---|
| 156 | case 2:
|
---|
| 157 | s << "z "; break;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[419] | 160 | s << mPosition << " ";
|
---|
[418] | 161 |
|
---|
| 162 | mBack->Print(s);
|
---|
| 163 | mFront->Print(s);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[462] | 166 | int VspKdInterior::ComputeRayIntersection(const RayInfo &rayData, float &t)
|
---|
[418] | 167 | {
|
---|
[419] | 168 | return rayData.ComputeRayIntersection(mAxis, mPosition, t);
|
---|
[418] | 169 | }
|
---|
| 170 |
|
---|
[462] | 171 | VspKdNode *VspKdInterior::GetBack() const
|
---|
[419] | 172 | {
|
---|
| 173 | return mBack;
|
---|
| 174 | }
|
---|
[418] | 175 |
|
---|
[462] | 176 | VspKdNode *VspKdInterior::GetFront() const
|
---|
[406] | 177 | {
|
---|
[419] | 178 | return mFront;
|
---|
| 179 | }
|
---|
[406] | 180 |
|
---|
[420] | 181 |
|
---|
[425] | 182 | /**************************************************************/
|
---|
[462] | 183 | /* class VspKdLeaf implementation */
|
---|
[425] | 184 | /**************************************************************/
|
---|
[462] | 185 |
|
---|
| 186 |
|
---|
| 187 | VspKdLeaf::VspKdLeaf(VspKdInterior *p, const int nRays):
|
---|
| 188 | VspKdNode(p), mRays(), mPvsSize(0), mValidPvs(false), mViewCell(NULL)
|
---|
[420] | 189 | {
|
---|
| 190 | mRays.reserve(nRays);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[462] | 193 | VspKdLeaf::~VspKdLeaf()
|
---|
| 194 | {
|
---|
| 195 | }
|
---|
[420] | 196 |
|
---|
[462] | 197 | int VspKdLeaf::Type() const
|
---|
[420] | 198 | {
|
---|
| 199 | return ELeaf;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[462] | 202 | void VspKdLeaf::Print(ostream &s) const
|
---|
[420] | 203 | {
|
---|
| 204 | s << endl << "L: r = " << (int)mRays.size() << endl;
|
---|
| 205 | };
|
---|
| 206 |
|
---|
[462] | 207 | void VspKdLeaf::AddRay(const RayInfo &data)
|
---|
[420] | 208 | {
|
---|
| 209 | mValidPvs = false;
|
---|
| 210 | mRays.push_back(data);
|
---|
| 211 | data.mRay->Ref();
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[462] | 214 | int VspKdLeaf::GetPvsSize() const
|
---|
[420] | 215 | {
|
---|
| 216 | return mPvsSize;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[462] | 219 | void VspKdLeaf::SetPvsSize(const int s)
|
---|
[420] | 220 | {
|
---|
| 221 | mPvsSize = s;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[462] | 224 | void VspKdLeaf::Mail()
|
---|
[420] | 225 | {
|
---|
[462] | 226 | mMailbox = sMailId;
|
---|
[420] | 227 | }
|
---|
| 228 |
|
---|
[462] | 229 | void VspKdLeaf::NewMail()
|
---|
[420] | 230 | {
|
---|
[462] | 231 | ++ sMailId;
|
---|
[420] | 232 | }
|
---|
| 233 |
|
---|
[462] | 234 | bool VspKdLeaf::Mailed() const
|
---|
[420] | 235 | {
|
---|
[462] | 236 | return mMailbox == sMailId;
|
---|
[420] | 237 | }
|
---|
| 238 |
|
---|
[462] | 239 | bool VspKdLeaf::Mailed(const int mail) const
|
---|
[420] | 240 | {
|
---|
[462] | 241 | return mMailbox == mail;
|
---|
[420] | 242 | }
|
---|
| 243 |
|
---|
[462] | 244 | int VspKdLeaf::GetMailbox() const
|
---|
[420] | 245 | {
|
---|
[462] | 246 | return mMailbox;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | float VspKdLeaf::GetAvgRayContribution() const
|
---|
| 250 | {
|
---|
[420] | 251 | return GetPvsSize() / ((float)mRays.size() + Limits::Small);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[462] | 254 | float VspKdLeaf::GetSqrRayContribution() const
|
---|
[420] | 255 | {
|
---|
| 256 | return sqr(GetAvgRayContribution());
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[462] | 259 | RayInfoContainer &VspKdLeaf::GetRays()
|
---|
[426] | 260 | {
|
---|
| 261 | return mRays;
|
---|
| 262 | }
|
---|
[428] | 263 |
|
---|
[462] | 264 | void VspKdLeaf::ExtractPvs(ObjectContainer &objects) const
|
---|
[428] | 265 | {
|
---|
| 266 | RayInfoContainer::const_iterator it, it_end = mRays.end();
|
---|
| 267 |
|
---|
| 268 | for (it = mRays.begin(); it != it_end; ++ it)
|
---|
| 269 | {
|
---|
| 270 | if ((*it).mRay->mTerminationObject)
|
---|
| 271 | objects.push_back((*it).mRay->mTerminationObject);
|
---|
| 272 | if ((*it).mRay->mOriginObject)
|
---|
| 273 | objects.push_back((*it).mRay->mOriginObject);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[462] | 277 | void VspKdLeaf::GetRays(VssRayContainer &rays)
|
---|
[428] | 278 | {
|
---|
| 279 | RayInfoContainer::const_iterator it, it_end = mRays.end();
|
---|
| 280 |
|
---|
| 281 | for (it = mRays.begin(); it != mRays.end(); ++ it)
|
---|
| 282 | rays.push_back((*it).mRay);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[462] | 285 | VspKdViewCell *VspKdLeaf::GetViewCell()
|
---|
[453] | 286 | {
|
---|
| 287 | return mViewCell;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[462] | 290 | void VspKdLeaf::SetViewCell(VspKdViewCell *viewCell)
|
---|
| 291 | {
|
---|
| 292 | mViewCell = viewCell;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 |
|
---|
| 296 | void VspKdLeaf::UpdatePvsSize()
|
---|
| 297 | {
|
---|
| 298 | if (!mValidPvs)
|
---|
| 299 | {
|
---|
| 300 | Intersectable::NewMail();
|
---|
| 301 | int pvsSize = 0;
|
---|
| 302 | for(RayInfoContainer::iterator ri = mRays.begin();
|
---|
| 303 | ri != mRays.end(); ++ ri)
|
---|
| 304 | {
|
---|
| 305 | if ((*ri).mRay->IsActive())
|
---|
| 306 | {
|
---|
| 307 | Intersectable *object;
|
---|
| 308 | #if BIDIRECTIONAL_RAY
|
---|
| 309 | object = (*ri).mRay->mOriginObject;
|
---|
| 310 |
|
---|
| 311 | if (object && !object->Mailed())
|
---|
| 312 | {
|
---|
| 313 | ++ pvsSize;
|
---|
| 314 | object->Mail();
|
---|
| 315 | }
|
---|
| 316 | #endif
|
---|
| 317 | object = (*ri).mRay->mTerminationObject;
|
---|
| 318 | if (object && !object->Mailed())
|
---|
| 319 | {
|
---|
| 320 | ++ pvsSize;
|
---|
| 321 | object->Mail();
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | mPvsSize = pvsSize;
|
---|
| 327 | mValidPvs = true;
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[420] | 331 | /*********************************************************/
|
---|
| 332 | /* class VspKdTree implementation */
|
---|
| 333 | /*********************************************************/
|
---|
| 334 |
|
---|
| 335 |
|
---|
[462] | 336 |
|
---|
[428] | 337 | VspKdTree::VspKdTree(): mOnlyDrivingAxis(true)
|
---|
[419] | 338 | {
|
---|
[421] | 339 | environment->GetIntValue("VspKdTree.Termination.maxDepth", mTermMaxDepth);
|
---|
| 340 | environment->GetIntValue("VspKdTree.Termination.minPvs", mTermMinPvs);
|
---|
| 341 | environment->GetIntValue("VspKdTree.Termination.minRays", mTermMinRays);
|
---|
| 342 | environment->GetFloatValue("VspKdTree.Termination.maxRayContribution", mTermMaxRayContribution);
|
---|
| 343 | environment->GetFloatValue("VspKdTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
[422] | 344 | environment->GetFloatValue("VspKdTree.Termination.minSize", mTermMinSize);
|
---|
[421] | 345 |
|
---|
| 346 | mTermMinSize = sqr(mTermMinSize);
|
---|
| 347 |
|
---|
[422] | 348 | environment->GetFloatValue("VspKdTree.epsilon", mEpsilon);
|
---|
| 349 | environment->GetFloatValue("VspKdTree.ct_div_ci", mCtDivCi);
|
---|
[408] | 350 |
|
---|
[422] | 351 | environment->GetFloatValue("VspKdTree.maxTotalMemory", mMaxTotalMemory);
|
---|
| 352 | environment->GetFloatValue("VspKdTree.maxStaticMemory", mMaxStaticMemory);
|
---|
| 353 |
|
---|
| 354 | environment->GetIntValue("VspKdTree.accessTimeThreshold", mAccessTimeThreshold);
|
---|
| 355 | environment->GetIntValue("VspKdTree.minCollapseDepth", mMinCollapseDepth);
|
---|
[408] | 356 |
|
---|
[462] | 357 | environment->GetFloatValue("VspKdTree.maxCostRatio", mMaxCostRatio);
|
---|
| 358 | environment->GetIntValue("ViewCells.maxViewCells", mMaxViewCells);
|
---|
| 359 |
|
---|
| 360 | MergeCandidate::sMaxPvsSize = 300; // TODO: add option
|
---|
| 361 |
|
---|
[409] | 362 | // split type
|
---|
[408] | 363 | char sname[128];
|
---|
| 364 | environment->GetStringValue("VspKdTree.splitType", sname);
|
---|
[409] | 365 | string name(sname);
|
---|
[423] | 366 |
|
---|
| 367 | Debug << "======= vsp kd tree options ========" << endl;
|
---|
| 368 | Debug << "max depth: "<< mTermMaxDepth << endl;
|
---|
| 369 | Debug << "min pvs: "<< mTermMinPvs << endl;
|
---|
| 370 | Debug << "min rays: "<< mTermMinRays << endl;
|
---|
| 371 | Debug << "max ray contribution: "<< mTermMaxRayContribution << endl;
|
---|
| 372 | Debug << "max cost ratio: "<< mTermMaxCostRatio << endl;
|
---|
| 373 | Debug << "min size: "<<mTermMinSize << endl;
|
---|
| 374 |
|
---|
[409] | 375 | if (name.compare("regular") == 0)
|
---|
[423] | 376 | {
|
---|
| 377 | Debug << "using regular split" << endl;
|
---|
[409] | 378 | splitType = ESplitRegular;
|
---|
[423] | 379 | }
|
---|
[409] | 380 | else
|
---|
| 381 | {
|
---|
| 382 | if (name.compare("heuristic") == 0)
|
---|
[423] | 383 | {
|
---|
| 384 | Debug << "using heuristic split" << endl;
|
---|
[409] | 385 | splitType = ESplitHeuristic;
|
---|
[423] | 386 | }
|
---|
[409] | 387 | else
|
---|
| 388 | {
|
---|
| 389 | cerr << "Invalid VspKdTree split type " << name << endl;
|
---|
[408] | 390 | exit(1);
|
---|
| 391 | }
|
---|
[409] | 392 | }
|
---|
[408] | 393 |
|
---|
[418] | 394 | mRoot = NULL;
|
---|
[409] | 395 |
|
---|
[422] | 396 | mSplitCandidates = new vector<SortableEntry>;
|
---|
[406] | 397 | }
|
---|
| 398 |
|
---|
[408] | 399 |
|
---|
| 400 | VspKdTree::~VspKdTree()
|
---|
[406] | 401 | {
|
---|
[419] | 402 | DEL_PTR(mRoot);
|
---|
[449] | 403 | DEL_PTR(mSplitCandidates);
|
---|
[408] | 404 | }
|
---|
[406] | 405 |
|
---|
[418] | 406 | void VspKdStatistics::Print(ostream &app) const
|
---|
[408] | 407 | {
|
---|
[426] | 408 | app << "===== VspKdTree statistics ===============\n";
|
---|
[408] | 409 |
|
---|
[426] | 410 | app << "#N_RAYS ( Number of rays )\n"
|
---|
| 411 | << rays << endl;
|
---|
[410] | 412 |
|
---|
[426] | 413 | app << "#N_INITPVS ( Initial PVS size )\n"
|
---|
| 414 | << initialPvsSize << endl;
|
---|
[406] | 415 |
|
---|
[426] | 416 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
|
---|
[406] | 417 |
|
---|
[426] | 418 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
[406] | 419 |
|
---|
[426] | 420 | app << "#N_SPLITS ( Number of splits in axes x y z dx dy dz \n";
|
---|
| 421 |
|
---|
| 422 | for (int i=0; i<7; i++)
|
---|
| 423 | app << splits[i] <<" ";
|
---|
| 424 | app << endl;
|
---|
[406] | 425 |
|
---|
[426] | 426 | app << "#N_RAYREFS ( Number of rayRefs )\n" <<
|
---|
| 427 | rayRefs << "\n";
|
---|
[408] | 428 |
|
---|
[426] | 429 | app << "#N_RAYRAYREFS ( Number of rayRefs / ray )\n" <<
|
---|
| 430 | rayRefs / (double)rays << "\n";
|
---|
[408] | 431 |
|
---|
[426] | 432 | app << "#N_LEAFRAYREFS ( Number of rayRefs / leaf )\n" <<
|
---|
| 433 | rayRefs / (double)Leaves() << "\n";
|
---|
[408] | 434 |
|
---|
[426] | 435 | app << "#N_MAXRAYREFS ( Max number of rayRefs / leaf )\n" <<
|
---|
| 436 | maxRayRefs << "\n";
|
---|
[408] | 437 |
|
---|
| 438 | // app << setprecision(4);
|
---|
| 439 |
|
---|
[426] | 440 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n" <<
|
---|
| 441 | maxDepthNodes * 100 / (double)Leaves() << endl;
|
---|
[408] | 442 |
|
---|
[426] | 443 | app << "#N_PMINPVSLEAVES ( Percentage of leaves with mininimal PVS )\n" <<
|
---|
| 444 | minPvsNodes * 100 / (double)Leaves() << endl;
|
---|
[408] | 445 |
|
---|
[426] | 446 | app << "#N_PMINRAYSLEAVES ( Percentage of leaves with minimal number of rays)\n" <<
|
---|
| 447 | minRaysNodes * 100 / (double)Leaves() << endl;
|
---|
| 448 |
|
---|
[408] | 449 | app << "#N_PMINSIZELEAVES ( Percentage of leaves with minSize )\n"<<
|
---|
[426] | 450 | minSizeNodes * 100 / (double)Leaves() << endl;
|
---|
[408] | 451 |
|
---|
[426] | 452 | app << "#N_PMAXRAYCONTRIBLEAVES ( Percentage of leaves with maximal ray contribution )\n" <<
|
---|
| 453 | maxRayContribNodes * 100 / (double)Leaves() << endl;
|
---|
[408] | 454 |
|
---|
[426] | 455 | app << "#N_ADDED_RAYREFS ( Number of dynamically added ray references )\n"<<
|
---|
| 456 | addedRayRefs << endl;
|
---|
[408] | 457 |
|
---|
[426] | 458 | app << "#N_REMOVED_RAYREFS ( Number of dynamically removed ray references )\n"<<
|
---|
| 459 | removedRayRefs << endl;
|
---|
[408] | 460 |
|
---|
[426] | 461 | // app << setprecision(4);
|
---|
[408] | 462 |
|
---|
[462] | 463 | app << "#N_( Maximal PVS size / leaf)\n"
|
---|
[426] | 464 | << maxPvsSize << endl;
|
---|
[408] | 465 |
|
---|
[426] | 466 | app << "#N_CTIME ( Construction time [s] )\n"
|
---|
| 467 | << Time() << " \n";
|
---|
[408] | 468 |
|
---|
[426] | 469 | app << "===== END OF VspKdTree statistics ==========\n";
|
---|
[406] | 470 | }
|
---|
| 471 |
|
---|
[408] | 472 |
|
---|
[462] | 473 | void VspKdTree::CollectViewCells(ViewCellContainer &viewCells) const |
---|
| 474 | { |
---|
| 475 | stack<VspKdNode *> nodeStack; |
---|
| 476 | nodeStack.push(mRoot); |
---|
| 477 | |
---|
| 478 | ViewCell::NewMail(); |
---|
| 479 | |
---|
| 480 | while (!nodeStack.empty()) |
---|
| 481 | { |
---|
| 482 | VspKdNode *node = nodeStack.top(); |
---|
| 483 | nodeStack.pop(); |
---|
| 484 | |
---|
| 485 | if (node->IsLeaf()) |
---|
| 486 | { |
---|
| 487 | ViewCell *viewCell = dynamic_cast<VspKdLeaf *>(node)->mViewCell; |
---|
| 488 | |
---|
| 489 | if (!viewCell->Mailed()) |
---|
| 490 | { |
---|
| 491 | viewCell->Mail(); |
---|
| 492 | viewCells.push_back(viewCell); |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | else |
---|
| 496 | { |
---|
| 497 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); |
---|
| 498 | |
---|
| 499 | nodeStack.push(interior->GetFront()); |
---|
| 500 | nodeStack.push(interior->GetBack()); |
---|
| 501 | } |
---|
| 502 | } |
---|
[408] | 503 | }
|
---|
[406] | 504 |
|
---|
[462] | 505 | void VspKdTree::Construct(const VssRayContainer &rays,
|
---|
| 506 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
[452] | 507 | {
|
---|
[414] | 508 | mStat.Start();
|
---|
[452] | 509 |
|
---|
[422] | 510 | mMaxMemory = mMaxStaticMemory;
|
---|
[419] | 511 | DEL_PTR(mRoot);
|
---|
[408] | 512 |
|
---|
[462] | 513 | mRoot = new VspKdLeaf(NULL, (int)rays.size());
|
---|
[408] | 514 |
|
---|
[423] | 515 | // first construct a leaf that will get subdivided
|
---|
[462] | 516 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(mRoot);
|
---|
[409] | 517 |
|
---|
[414] | 518 | mStat.nodes = 1;
|
---|
[448] | 519 | mBox.Initialize();
|
---|
[452] | 520 |
|
---|
[448] | 521 | //-- compute bounding box
|
---|
[409] | 522 | if (forcedBoundingBox)
|
---|
[419] | 523 | mBox = *forcedBoundingBox;
|
---|
[448] | 524 | else
|
---|
| 525 | for (VssRayContainer::const_iterator ri = rays.begin(); ri != rays.end(); ++ ri)
|
---|
| 526 | {
|
---|
[452] | 527 | if ((*ri)->mOriginObject)
|
---|
| 528 | mBox.Include((*ri)->GetOrigin());
|
---|
| 529 | if ((*ri)->mTerminationObject)
|
---|
| 530 | mBox.Include((*ri)->GetTermination());
|
---|
[448] | 531 | }
|
---|
[408] | 532 |
|
---|
[422] | 533 | Debug << "bbox = " << mBox << endl;
|
---|
[448] | 534 |
|
---|
| 535 | for (VssRayContainer::const_iterator ri = rays.begin(); ri != rays.end(); ++ ri)
|
---|
| 536 | {
|
---|
| 537 | //leaf->AddRay(RayInfo(*ri));
|
---|
| 538 | VssRay *ray = *ri; |
---|
| 539 | |
---|
| 540 | float minT, maxT; |
---|
| 541 | |
---|
| 542 | // TODO: not very efficient to implictly cast between rays types ... |
---|
| 543 | if (mBox.GetRaySegment(*ray, minT, maxT)) |
---|
| 544 | { |
---|
| 545 | float len = ray->Length(); |
---|
| 546 | |
---|
| 547 | if (!len) |
---|
| 548 | len = Limits::Small; |
---|
| 549 | |
---|
| 550 | leaf->AddRay(RayInfo(ray, minT / len, maxT / len));
|
---|
| 551 | }
|
---|
| 552 | }
|
---|
[419] | 553 |
|
---|
[420] | 554 | mStat.rays = (int)leaf->mRays.size();
|
---|
[408] | 555 | leaf->UpdatePvsSize();
|
---|
[409] | 556 |
|
---|
[414] | 557 | mStat.initialPvsSize = leaf->GetPvsSize();
|
---|
[409] | 558 |
|
---|
| 559 | // Subdivide();
|
---|
[419] | 560 | mRoot = Subdivide(TraversalData(leaf, mBox, 0));
|
---|
[408] | 561 |
|
---|
[422] | 562 | if (mSplitCandidates)
|
---|
[409] | 563 | {
|
---|
[422] | 564 | // force release of this vector
|
---|
| 565 | delete mSplitCandidates;
|
---|
| 566 | mSplitCandidates = new vector<SortableEntry>;
|
---|
[409] | 567 | }
|
---|
[408] | 568 |
|
---|
[414] | 569 | mStat.Stop();
|
---|
[408] | 570 |
|
---|
[414] | 571 | mStat.Print(cout);
|
---|
[423] | 572 | Debug << "#Total memory=" << GetMemUsage() << endl;
|
---|
[408] | 573 | }
|
---|
| 574 |
|
---|
| 575 |
|
---|
| 576 |
|
---|
[462] | 577 | VspKdNode *VspKdTree::Subdivide(const TraversalData &tdata)
|
---|
[408] | 578 | {
|
---|
[462] | 579 | VspKdNode *result = NULL;
|
---|
[408] | 580 |
|
---|
[409] | 581 | priority_queue<TraversalData> tStack;
|
---|
[423] | 582 | //stack<TraversalData> tStack;
|
---|
[406] | 583 |
|
---|
[409] | 584 | tStack.push(tdata);
|
---|
[406] | 585 |
|
---|
[409] | 586 | AxisAlignedBox3 backBox;
|
---|
| 587 | AxisAlignedBox3 frontBox;
|
---|
[406] | 588 |
|
---|
[408] | 589 | int lastMem = 0;
|
---|
[423] | 590 |
|
---|
[409] | 591 | while (!tStack.empty())
|
---|
| 592 | {
|
---|
[408] | 593 | float mem = GetMemUsage();
|
---|
| 594 |
|
---|
[423] | 595 | if (lastMem / 10 != ((int)mem) / 10)
|
---|
[409] | 596 | {
|
---|
[425] | 597 | Debug << mem << " MB" << endl;
|
---|
[408] | 598 | }
|
---|
| 599 | lastMem = (int)mem;
|
---|
| 600 |
|
---|
[423] | 601 | if (1 && mem > mMaxMemory)
|
---|
[409] | 602 | {
|
---|
[423] | 603 | Debug << "memory limit reached: " << mem << endl;
|
---|
[409] | 604 | // count statistics on unprocessed leafs
|
---|
| 605 | while (!tStack.empty())
|
---|
| 606 | {
|
---|
[408] | 607 | EvaluateLeafStats(tStack.top());
|
---|
| 608 | tStack.pop();
|
---|
[409] | 609 | }
|
---|
| 610 | break;
|
---|
| 611 | }
|
---|
[408] | 612 |
|
---|
[409] | 613 | TraversalData data = tStack.top();
|
---|
[423] | 614 | tStack.pop();
|
---|
[409] | 615 |
|
---|
[462] | 616 | VspKdNode *node = SubdivideNode((VspKdLeaf *) data.mNode,
|
---|
[419] | 617 | data.mBox, backBox, frontBox);
|
---|
[409] | 618 | if (result == NULL)
|
---|
| 619 | result = node;
|
---|
[406] | 620 |
|
---|
[409] | 621 | if (!node->IsLeaf())
|
---|
| 622 | {
|
---|
[462] | 623 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
[408] | 624 |
|
---|
[409] | 625 | // push the children on the stack
|
---|
[419] | 626 | tStack.push(TraversalData(interior->GetBack(), backBox, data.mDepth + 1));
|
---|
| 627 | tStack.push(TraversalData(interior->GetFront(), frontBox, data.mDepth + 1));
|
---|
[409] | 628 | }
|
---|
| 629 | else
|
---|
| 630 | {
|
---|
| 631 | EvaluateLeafStats(data);
|
---|
| 632 | }
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | return result;
|
---|
[408] | 636 | }
|
---|
[406] | 637 |
|
---|
[408] | 638 |
|
---|
| 639 | // returns selected plane for subdivision
|
---|
[462] | 640 | int VspKdTree::SelectPlane(VspKdLeaf *leaf,
|
---|
[424] | 641 | const AxisAlignedBox3 &box,
|
---|
| 642 | float &position,
|
---|
| 643 | int &raysBack,
|
---|
| 644 | int &raysFront,
|
---|
| 645 | int &pvsBack,
|
---|
| 646 | int &pvsFront)
|
---|
[408] | 647 | {
|
---|
[410] | 648 | int minDirDepth = 6;
|
---|
[428] | 649 | int axis = 0;
|
---|
| 650 | float costRatio = 0;
|
---|
[408] | 651 |
|
---|
[422] | 652 | if (splitType == ESplitRegular)
|
---|
| 653 | {
|
---|
[408] | 654 | costRatio = BestCostRatioRegular(leaf,
|
---|
[410] | 655 | axis,
|
---|
| 656 | position,
|
---|
| 657 | raysBack,
|
---|
| 658 | raysFront,
|
---|
| 659 | pvsBack,
|
---|
| 660 | pvsFront);
|
---|
| 661 | }
|
---|
[423] | 662 | else if (splitType == ESplitHeuristic)
|
---|
[410] | 663 | {
|
---|
| 664 | costRatio = BestCostRatioHeuristic(leaf,
|
---|
| 665 | axis,
|
---|
| 666 | position,
|
---|
| 667 | raysBack,
|
---|
| 668 | raysFront,
|
---|
| 669 | pvsBack,
|
---|
| 670 | pvsFront);
|
---|
[423] | 671 | }
|
---|
| 672 | else
|
---|
| 673 | {
|
---|
| 674 | cerr << "VspKdTree: Unknown split heuristics\n";
|
---|
| 675 | exit(1);
|
---|
| 676 | }
|
---|
[408] | 677 |
|
---|
[428] | 678 | if (costRatio > mTermMaxCostRatio)
|
---|
[410] | 679 | {
|
---|
[425] | 680 | Debug << "Too big cost ratio " << costRatio << endl;
|
---|
[408] | 681 | return -1;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
[428] | 684 | if (0)
|
---|
| 685 | Debug << "pvs=" << leaf->mPvsSize
|
---|
| 686 | << " rays=" << (int)leaf->mRays.size()
|
---|
| 687 | << " rc=" << leaf->GetAvgRayContribution()
|
---|
| 688 | << " axis=" << axis << endl;
|
---|
[408] | 689 |
|
---|
| 690 | return axis;
|
---|
[406] | 691 | }
|
---|
| 692 |
|
---|
| 693 |
|
---|
[408] | 694 |
|
---|
[462] | 695 | float VspKdTree::EvalCostRatio(VspKdLeaf *leaf,
|
---|
[425] | 696 | const int axis,
|
---|
| 697 | const float position,
|
---|
| 698 | int &raysBack,
|
---|
| 699 | int &raysFront,
|
---|
| 700 | int &pvsBack,
|
---|
| 701 | int &pvsFront)
|
---|
[406] | 702 | {
|
---|
[408] | 703 | raysBack = 0;
|
---|
| 704 | raysFront = 0;
|
---|
| 705 | pvsFront = 0;
|
---|
| 706 | pvsBack = 0;
|
---|
| 707 |
|
---|
| 708 | float newCost;
|
---|
| 709 |
|
---|
| 710 | Intersectable::NewMail(3);
|
---|
| 711 |
|
---|
| 712 | // eval pvs size
|
---|
| 713 | int pvsSize = leaf->GetPvsSize();
|
---|
| 714 |
|
---|
| 715 | Intersectable::NewMail(3);
|
---|
| 716 |
|
---|
[419] | 717 | // this is the main ray classification loop!
|
---|
[420] | 718 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
[424] | 719 | ri != leaf->mRays.end(); ++ ri)
|
---|
[419] | 720 | {
|
---|
| 721 | if (!(*ri).mRay->IsActive())
|
---|
| 722 | continue;
|
---|
[410] | 723 |
|
---|
[419] | 724 | // determine the side of this ray with respect to the plane
|
---|
| 725 | int side = (*ri).ComputeRayIntersection(axis, position, (*ri).mRay->mT);
|
---|
| 726 | // (*ri).mRay->mSide = side;
|
---|
| 727 |
|
---|
| 728 | if (side <= 0)
|
---|
| 729 | ++ raysBack;
|
---|
[408] | 730 |
|
---|
[419] | 731 | if (side >= 0)
|
---|
| 732 | ++ raysFront;
|
---|
[408] | 733 |
|
---|
[419] | 734 | AddObject2Pvs((*ri).mRay->mTerminationObject, side, pvsBack, pvsFront);
|
---|
| 735 | }
|
---|
[408] | 736 |
|
---|
[428] | 737 | if (0)
|
---|
[425] | 738 | {
|
---|
[428] | 739 | const float sum = float(pvsBack + pvsFront);
|
---|
[425] | 740 | const float oldCost = (float)pvsSize * 2;
|
---|
| 741 |
|
---|
| 742 | return sum / oldCost;
|
---|
| 743 | }
|
---|
[428] | 744 | else
|
---|
| 745 | {
|
---|
[425] | 746 | AxisAlignedBox3 box = GetBBox(leaf);
|
---|
| 747 |
|
---|
| 748 | float minBox = box.Min(axis);
|
---|
| 749 | float maxBox = box.Max(axis);
|
---|
| 750 | float sizeBox = maxBox - minBox;
|
---|
[408] | 751 |
|
---|
[425] | 752 | // float sum = raysBack*(position - minBox) + raysFront*(maxBox - position);
|
---|
[428] | 753 | const float sum = pvsBack * (position - minBox) + pvsFront * (maxBox - position);
|
---|
[408] | 754 |
|
---|
[425] | 755 | newCost = mCtDivCi + sum / sizeBox;
|
---|
[419] | 756 |
|
---|
[425] | 757 | //Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl;
|
---|
| 758 | // float oldCost = leaf->mRays.size();
|
---|
[428] | 759 | const float oldCost = (float)pvsSize;
|
---|
[425] | 760 |
|
---|
[452] | 761 | return newCost / oldCost;
|
---|
[425] | 762 | }
|
---|
[406] | 763 | }
|
---|
| 764 |
|
---|
[462] | 765 | float VspKdTree::BestCostRatioRegular(VspKdLeaf *leaf,
|
---|
[422] | 766 | int &axis,
|
---|
| 767 | float &position,
|
---|
| 768 | int &raysBack,
|
---|
| 769 | int &raysFront,
|
---|
| 770 | int &pvsBack,
|
---|
| 771 | int &pvsFront)
|
---|
[408] | 772 | {
|
---|
[422] | 773 | int nRaysBack[3], nRaysFront[3];
|
---|
| 774 | int nPvsBack[3], nPvsFront[3];
|
---|
| 775 |
|
---|
| 776 | float nPosition[3];
|
---|
| 777 | float nCostRatio[3];
|
---|
[408] | 778 | int bestAxis = -1;
|
---|
| 779 |
|
---|
| 780 | AxisAlignedBox3 sBox = GetBBox(leaf);
|
---|
[410] | 781 |
|
---|
[428] | 782 | const int sAxis = sBox.Size().DrivingAxis();
|
---|
[408] | 783 |
|
---|
[422] | 784 | for (axis = 0; axis < 3; ++ axis)
|
---|
[410] | 785 | {
|
---|
[422] | 786 | if (!mOnlyDrivingAxis || axis == sAxis)
|
---|
[410] | 787 | {
|
---|
[452] | 788 |
|
---|
[424] | 789 | nPosition[axis] = (sBox.Min()[axis] + sBox.Max()[axis]) * 0.5f;
|
---|
[419] | 790 |
|
---|
[408] | 791 | nCostRatio[axis] = EvalCostRatio(leaf,
|
---|
[410] | 792 | axis,
|
---|
| 793 | nPosition[axis],
|
---|
| 794 | nRaysBack[axis],
|
---|
| 795 | nRaysFront[axis],
|
---|
| 796 | nPvsBack[axis],
|
---|
| 797 | nPvsFront[axis]);
|
---|
| 798 | if (bestAxis == -1)
|
---|
[452] | 799 | {
|
---|
[408] | 800 | bestAxis = axis;
|
---|
[452] | 801 | }
|
---|
| 802 | else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
| 803 | {
|
---|
| 804 | /*Debug << "pvs front " << nPvsBack[axis]
|
---|
| 805 | << " pvs back " << nPvsFront[axis]
|
---|
| 806 | << " overall pvs " << leaf->GetPvsSize() << endl;*/
|
---|
| 807 | bestAxis = axis;
|
---|
| 808 | }
|
---|
| 809 |
|
---|
[408] | 810 | }
|
---|
| 811 | }
|
---|
| 812 |
|
---|
[428] | 813 | //-- assign best axis
|
---|
[408] | 814 | axis = bestAxis;
|
---|
| 815 | position = nPosition[bestAxis];
|
---|
| 816 |
|
---|
| 817 | raysBack = nRaysBack[bestAxis];
|
---|
| 818 | raysFront = nRaysFront[bestAxis];
|
---|
| 819 |
|
---|
| 820 | pvsBack = nPvsBack[bestAxis];
|
---|
| 821 | pvsFront = nPvsFront[bestAxis];
|
---|
| 822 |
|
---|
| 823 | return nCostRatio[bestAxis];
|
---|
| 824 | }
|
---|
| 825 |
|
---|
[462] | 826 | float VspKdTree::BestCostRatioHeuristic(VspKdLeaf *leaf,
|
---|
[410] | 827 | int &axis,
|
---|
| 828 | float &position,
|
---|
| 829 | int &raysBack,
|
---|
| 830 | int &raysFront,
|
---|
| 831 | int &pvsBack,
|
---|
| 832 | int &pvsFront)
|
---|
[406] | 833 | {
|
---|
[408] | 834 | AxisAlignedBox3 box = GetBBox(leaf);
|
---|
| 835 | // AxisAlignedBox3 dirBox = GetDirBBox(node);
|
---|
| 836 |
|
---|
| 837 | axis = box.Size().DrivingAxis();
|
---|
| 838 |
|
---|
| 839 | SortSplitCandidates(leaf, axis);
|
---|
[406] | 840 |
|
---|
[408] | 841 | // go through the lists, count the number of objects left and right
|
---|
| 842 | // and evaluate the following cost funcion:
|
---|
| 843 | // C = ct_div_ci + (ql*rl + qr*rr)/queries
|
---|
| 844 |
|
---|
[420] | 845 | int rl = 0, rr = (int)leaf->mRays.size();
|
---|
| 846 | int pl = 0, pr = leaf->GetPvsSize();
|
---|
[424] | 847 |
|
---|
[408] | 848 | float minBox = box.Min(axis);
|
---|
| 849 | float maxBox = box.Max(axis);
|
---|
| 850 | float sizeBox = maxBox - minBox;
|
---|
| 851 |
|
---|
[419] | 852 | float minBand = minBox + 0.1f*(maxBox - minBox);
|
---|
| 853 | float maxBand = minBox + 0.9f*(maxBox - minBox);
|
---|
[408] | 854 |
|
---|
| 855 | float sum = rr*sizeBox;
|
---|
[411] | 856 | float minSum = 1e20f;
|
---|
[408] | 857 |
|
---|
| 858 | Intersectable::NewMail();
|
---|
[424] | 859 |
|
---|
[408] | 860 | // set all object as belonging to the fron pvs
|
---|
[420] | 861 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
[424] | 862 | ri != leaf->mRays.end(); ++ ri)
|
---|
[410] | 863 | {
|
---|
| 864 | if ((*ri).mRay->IsActive())
|
---|
| 865 | {
|
---|
[408] | 866 | Intersectable *object = (*ri).mRay->mTerminationObject;
|
---|
[410] | 867 |
|
---|
[408] | 868 | if (object)
|
---|
[410] | 869 | if (!object->Mailed())
|
---|
| 870 | {
|
---|
[408] | 871 | object->Mail();
|
---|
| 872 | object->mCounter = 1;
|
---|
[410] | 873 | }
|
---|
| 874 | else
|
---|
| 875 | ++ object->mCounter;
|
---|
[408] | 876 | }
|
---|
[410] | 877 | }
|
---|
[408] | 878 |
|
---|
| 879 | Intersectable::NewMail();
|
---|
| 880 |
|
---|
[422] | 881 | for (vector<SortableEntry>::const_iterator ci = mSplitCandidates->begin();
|
---|
| 882 | ci < mSplitCandidates->end(); ++ ci)
|
---|
[410] | 883 | {
|
---|
[408] | 884 | VssRay *ray;
|
---|
[410] | 885 |
|
---|
| 886 | switch ((*ci).type)
|
---|
| 887 | {
|
---|
| 888 | case SortableEntry::ERayMin:
|
---|
| 889 | {
|
---|
| 890 | ++ rl;
|
---|
| 891 | ray = (VssRay *) (*ci).data;
|
---|
| 892 | Intersectable *object = ray->mTerminationObject;
|
---|
| 893 | if (object && !object->Mailed())
|
---|
| 894 | {
|
---|
| 895 | object->Mail();
|
---|
| 896 | ++ pl;
|
---|
| 897 | }
|
---|
| 898 | break;
|
---|
| 899 | }
|
---|
| 900 | case SortableEntry::ERayMax:
|
---|
| 901 | {
|
---|
| 902 | -- rr;
|
---|
| 903 |
|
---|
| 904 | ray = (VssRay *) (*ci).data;
|
---|
| 905 | Intersectable *object = ray->mTerminationObject;
|
---|
| 906 |
|
---|
| 907 | if (object)
|
---|
| 908 | {
|
---|
| 909 | if (-- object->mCounter == 0)
|
---|
| 910 | -- pr;
|
---|
| 911 | }
|
---|
| 912 |
|
---|
| 913 | break;
|
---|
| 914 | }
|
---|
[408] | 915 | }
|
---|
| 916 |
|
---|
[410] | 917 | if ((*ci).value > minBand && (*ci).value < maxBand)
|
---|
| 918 | {
|
---|
[408] | 919 | sum = pl*((*ci).value - minBox) + pr*(maxBox - (*ci).value);
|
---|
[410] | 920 |
|
---|
| 921 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
| 922 | // cout<<"cost= "<<sum<<endl;
|
---|
[408] | 923 |
|
---|
[410] | 924 | if (sum < minSum)
|
---|
| 925 | {
|
---|
[408] | 926 | minSum = sum;
|
---|
| 927 | position = (*ci).value;
|
---|
[410] | 928 |
|
---|
[408] | 929 | raysBack = rl;
|
---|
| 930 | raysFront = rr;
|
---|
[410] | 931 |
|
---|
[408] | 932 | pvsBack = pl;
|
---|
| 933 | pvsFront = pr;
|
---|
| 934 |
|
---|
[410] | 935 | }
|
---|
| 936 | }
|
---|
| 937 | }
|
---|
| 938 |
|
---|
[419] | 939 | float oldCost = (float)leaf->GetPvsSize();
|
---|
[422] | 940 | float newCost = mCtDivCi + minSum / sizeBox;
|
---|
[410] | 941 | float ratio = newCost / oldCost;
|
---|
[408] | 942 |
|
---|
[422] | 943 | //Debug << "costRatio=" << ratio << " pos=" << position << " t=" << (position - minBox) / (maxBox - minBox)
|
---|
| 944 | // <<"\t q=(" << queriesBack << "," << queriesFront << ")\t r=(" << raysBack << "," << raysFront << ")" << endl;
|
---|
[410] | 945 |
|
---|
[408] | 946 | return ratio;
|
---|
| 947 | }
|
---|
| 948 |
|
---|
[462] | 949 | void VspKdTree::SortSplitCandidates(VspKdLeaf *node,
|
---|
[410] | 950 | const int axis)
|
---|
[408] | 951 | {
|
---|
[422] | 952 | mSplitCandidates->clear();
|
---|
[408] | 953 |
|
---|
[420] | 954 | int requestedSize = 2 * (int)(node->mRays.size());
|
---|
[410] | 955 | // creates a sorted split candidates array
|
---|
[422] | 956 | if (mSplitCandidates->capacity() > 500000 &&
|
---|
| 957 | requestedSize < (int)(mSplitCandidates->capacity()/10) )
|
---|
[410] | 958 | {
|
---|
[449] | 959 | DEL_PTR(mSplitCandidates);
|
---|
[422] | 960 | mSplitCandidates = new vector<SortableEntry>;
|
---|
[410] | 961 | }
|
---|
[408] | 962 |
|
---|
[422] | 963 | mSplitCandidates->reserve(requestedSize);
|
---|
[408] | 964 |
|
---|
[410] | 965 | // insert all queries
|
---|
[420] | 966 | for(RayInfoContainer::const_iterator ri = node->mRays.begin();
|
---|
| 967 | ri < node->mRays.end(); ++ ri)
|
---|
[410] | 968 | {
|
---|
| 969 | bool positive = (*ri).mRay->HasPosDir(axis);
|
---|
[422] | 970 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax,
|
---|
| 971 | (*ri).ExtrapOrigin(axis), (void *)&*ri));
|
---|
[408] | 972 |
|
---|
[422] | 973 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,
|
---|
| 974 | (*ri).ExtrapTermination(axis), (void *)&*ri));
|
---|
[410] | 975 | }
|
---|
[408] | 976 |
|
---|
[422] | 977 | stable_sort(mSplitCandidates->begin(), mSplitCandidates->end());
|
---|
[406] | 978 | }
|
---|
| 979 |
|
---|
[408] | 980 |
|
---|
[410] | 981 | void VspKdTree::EvaluateLeafStats(const TraversalData &data)
|
---|
[406] | 982 | {
|
---|
[410] | 983 | // the node became a leaf -> evaluate stats for leafs
|
---|
[462] | 984 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(data.mNode);
|
---|
[408] | 985 |
|
---|
[426] | 986 | if (leaf->GetPvsSize() > mStat.maxPvsSize)
|
---|
| 987 | mStat.maxPvsSize = leaf->GetPvsSize();
|
---|
| 988 |
|
---|
| 989 | if ((int)(leaf->mRays.size()) > mStat.maxRayRefs)
|
---|
| 990 | mStat.maxRayRefs = (int)leaf->mRays.size();
|
---|
| 991 |
|
---|
| 992 |
|
---|
[421] | 993 | if (data.mDepth >= mTermMaxDepth)
|
---|
[413] | 994 | ++ mStat.maxDepthNodes;
|
---|
[406] | 995 |
|
---|
[421] | 996 | if (leaf->GetPvsSize() < mTermMinPvs)
|
---|
[413] | 997 | ++ mStat.minPvsNodes;
|
---|
[408] | 998 |
|
---|
[426] | 999 | if ((int)leaf->GetRays().size() < mTermMinRays)
|
---|
[413] | 1000 | ++ mStat.minRaysNodes;
|
---|
[408] | 1001 |
|
---|
[426] | 1002 | if (leaf->GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
[413] | 1003 | ++ mStat.maxRayContribNodes;
|
---|
[408] | 1004 |
|
---|
[421] | 1005 | if (SqrMagnitude(data.mBox.Size()) <= mTermMinSize)
|
---|
[413] | 1006 | ++ mStat.minSizeNodes;
|
---|
[408] | 1007 | }
|
---|
| 1008 |
|
---|
| 1009 |
|
---|
[462] | 1010 | inline bool VspKdTree::TerminationCriteriaMet(const VspKdLeaf *leaf,
|
---|
[421] | 1011 | const AxisAlignedBox3 &box) const
|
---|
| 1012 | {
|
---|
[423] | 1013 | return ((leaf->GetPvsSize() < mTermMinPvs) ||
|
---|
[426] | 1014 | (leaf->mRays.size() < mTermMinRays) ||
|
---|
[462] | 1015 | //(leaf->GetAvgRayContribution() > mTermMaxRayContribution ) ||
|
---|
[423] | 1016 | (leaf->mDepth >= mTermMaxDepth) ||
|
---|
| 1017 | (SqrMagnitude(box.Size()) <= mTermMinSize));
|
---|
[421] | 1018 | }
|
---|
[408] | 1019 |
|
---|
[421] | 1020 |
|
---|
[462] | 1021 | VspKdNode *VspKdTree::SubdivideNode(VspKdLeaf *leaf,
|
---|
[410] | 1022 | const AxisAlignedBox3 &box,
|
---|
| 1023 | AxisAlignedBox3 &backBBox,
|
---|
| 1024 | AxisAlignedBox3 &frontBBox)
|
---|
[408] | 1025 | {
|
---|
[421] | 1026 | if (TerminationCriteriaMet(leaf, box))
|
---|
[410] | 1027 | {
|
---|
[428] | 1028 | if (1 && leaf->mDepth >= mTermMaxDepth)
|
---|
[422] | 1029 | {
|
---|
[428] | 1030 | Debug << "Warning: max depth reached depth=" << (int)leaf->mDepth<<" rays=" << (int)leaf->mRays.size() << endl;
|
---|
| 1031 | Debug << "Bbox: " << GetBBox(leaf) << endl;
|
---|
[408] | 1032 | }
|
---|
[428] | 1033 | //Debug << "depth: " << (int)leaf->mDepth << " pvs: " << leaf->GetPvsSize() << " rays: " << leaf->mRays.size() << endl;
|
---|
| 1034 |
|
---|
[408] | 1035 | return leaf;
|
---|
| 1036 | }
|
---|
| 1037 |
|
---|
| 1038 | float position;
|
---|
| 1039 | // first count ray sides
|
---|
[410] | 1040 | int raysBack;
|
---|
| 1041 | int raysFront;
|
---|
[408] | 1042 | int pvsBack;
|
---|
| 1043 | int pvsFront;
|
---|
| 1044 |
|
---|
[410] | 1045 | // select subdivision axis
|
---|
[425] | 1046 | const int axis = SelectPlane(leaf, box, position, raysBack, raysFront, pvsBack, pvsFront);
|
---|
[406] | 1047 |
|
---|
[426] | 1048 | //Debug << "rays back=" << raysBack << " rays front=" << raysFront << " pvs back=" << pvsBack << " pvs front=" << pvsFront << endl;
|
---|
[408] | 1049 |
|
---|
[410] | 1050 | if (axis == -1)
|
---|
| 1051 | {
|
---|
| 1052 | return leaf;
|
---|
| 1053 | }
|
---|
[406] | 1054 |
|
---|
[413] | 1055 | mStat.nodes += 2;
|
---|
| 1056 | //++ mStat.splits[axis];
|
---|
[408] | 1057 |
|
---|
[410] | 1058 | // add the new nodes to the tree
|
---|
[462] | 1059 | VspKdInterior *node = new VspKdInterior(leaf->mParent);
|
---|
[406] | 1060 |
|
---|
[419] | 1061 | node->mAxis = axis;
|
---|
| 1062 | node->mPosition = position;
|
---|
| 1063 | node->mBox = box;
|
---|
| 1064 |
|
---|
[410] | 1065 | backBBox = box;
|
---|
| 1066 | frontBBox = box;
|
---|
[406] | 1067 |
|
---|
[462] | 1068 | VspKdLeaf *back = new VspKdLeaf(node, raysBack);
|
---|
[408] | 1069 | back->SetPvsSize(pvsBack);
|
---|
[462] | 1070 | VspKdLeaf *front = new VspKdLeaf(node, raysFront);
|
---|
[408] | 1071 | front->SetPvsSize(pvsFront);
|
---|
[406] | 1072 |
|
---|
[410] | 1073 | // replace a link from node's parent
|
---|
[419] | 1074 | if (leaf->mParent)
|
---|
| 1075 | leaf->mParent->ReplaceChildLink(leaf, node);
|
---|
[410] | 1076 | // and setup child links
|
---|
| 1077 | node->SetupChildLinks(back, front);
|
---|
[408] | 1078 |
|
---|
[462] | 1079 | if (axis <= VspKdNode::SPLIT_Z)
|
---|
[410] | 1080 | {
|
---|
[408] | 1081 | backBBox.SetMax(axis, position);
|
---|
[410] | 1082 | frontBBox.SetMin(axis, position);
|
---|
[408] | 1083 |
|
---|
[420] | 1084 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
| 1085 | ri != leaf->mRays.end(); ++ ri)
|
---|
[410] | 1086 | {
|
---|
| 1087 | if ((*ri).mRay->IsActive())
|
---|
| 1088 | {
|
---|
[408] | 1089 | // first unref ray from the former leaf
|
---|
| 1090 | (*ri).mRay->Unref();
|
---|
[406] | 1091 |
|
---|
[408] | 1092 | // determine the side of this ray with respect to the plane
|
---|
| 1093 | int side = node->ComputeRayIntersection(*ri, (*ri).mRay->mT);
|
---|
| 1094 |
|
---|
[410] | 1095 | if (side == 0)
|
---|
| 1096 | {
|
---|
| 1097 | if ((*ri).mRay->HasPosDir(axis))
|
---|
| 1098 | {
|
---|
[420] | 1099 | back->AddRay(RayInfo((*ri).mRay,
|
---|
| 1100 | (*ri).mMinT,
|
---|
| 1101 | (*ri).mRay->mT));
|
---|
| 1102 | front->AddRay(RayInfo((*ri).mRay,
|
---|
| 1103 | (*ri).mRay->mT,
|
---|
| 1104 | (*ri).mMaxT));
|
---|
[410] | 1105 | }
|
---|
| 1106 | else
|
---|
| 1107 | {
|
---|
[420] | 1108 | back->AddRay(RayInfo((*ri).mRay,
|
---|
| 1109 | (*ri).mRay->mT,
|
---|
| 1110 | (*ri).mMaxT));
|
---|
| 1111 | front->AddRay(RayInfo((*ri).mRay,
|
---|
| 1112 | (*ri).mMinT,
|
---|
| 1113 | (*ri).mRay->mT));
|
---|
[408] | 1114 | }
|
---|
[410] | 1115 | }
|
---|
| 1116 | else
|
---|
[408] | 1117 | if (side == 1)
|
---|
| 1118 | front->AddRay(*ri);
|
---|
| 1119 | else
|
---|
| 1120 | back->AddRay(*ri);
|
---|
[410] | 1121 | } else
|
---|
[408] | 1122 | (*ri).mRay->Unref();
|
---|
[410] | 1123 | }
|
---|
| 1124 | }
|
---|
| 1125 | else
|
---|
| 1126 | {
|
---|
| 1127 | // rays front/back
|
---|
[406] | 1128 |
|
---|
[420] | 1129 | for (RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
| 1130 | ri != leaf->mRays.end(); ++ ri)
|
---|
[410] | 1131 | {
|
---|
| 1132 | if ((*ri).mRay->IsActive())
|
---|
| 1133 | {
|
---|
[408] | 1134 | // first unref ray from the former leaf
|
---|
| 1135 | (*ri).mRay->Unref();
|
---|
| 1136 |
|
---|
| 1137 | int side;
|
---|
| 1138 | if ((*ri).mRay->GetDirParametrization(axis - 3) > position)
|
---|
| 1139 | side = 1;
|
---|
| 1140 | else
|
---|
| 1141 | side = -1;
|
---|
| 1142 |
|
---|
| 1143 | if (side == 1)
|
---|
| 1144 | front->AddRay(*ri);
|
---|
| 1145 | else
|
---|
[410] | 1146 | back->AddRay(*ri);
|
---|
| 1147 | }
|
---|
| 1148 | else
|
---|
[408] | 1149 | (*ri).mRay->Unref();
|
---|
[410] | 1150 | }
|
---|
| 1151 | }
|
---|
[408] | 1152 |
|
---|
[410] | 1153 | // update stats
|
---|
[420] | 1154 | mStat.rayRefs -= (int)leaf->mRays.size();
|
---|
[414] | 1155 | mStat.rayRefs += raysBack + raysFront;
|
---|
[408] | 1156 |
|
---|
[420] | 1157 | DEL_PTR(leaf);
|
---|
| 1158 |
|
---|
[410] | 1159 | return node;
|
---|
[406] | 1160 | }
|
---|
| 1161 |
|
---|
[410] | 1162 | int VspKdTree::ReleaseMemory(const int time)
|
---|
| 1163 | {
|
---|
[462] | 1164 | stack<VspKdNode *> tstack;
|
---|
[406] | 1165 |
|
---|
[410] | 1166 | // find a node in the tree which subtree will be collapsed
|
---|
[422] | 1167 | int maxAccessTime = time - mAccessTimeThreshold;
|
---|
[420] | 1168 | int released = 0;
|
---|
[406] | 1169 |
|
---|
[419] | 1170 | tstack.push(mRoot);
|
---|
[406] | 1171 |
|
---|
[423] | 1172 | while (!tstack.empty())
|
---|
[410] | 1173 | {
|
---|
[462] | 1174 | VspKdNode *node = tstack.top();
|
---|
[410] | 1175 | tstack.pop();
|
---|
[408] | 1176 |
|
---|
[410] | 1177 | if (!node->IsLeaf())
|
---|
| 1178 | {
|
---|
[462] | 1179 | VspKdInterior *in = dynamic_cast<VspKdInterior *>(node);
|
---|
[410] | 1180 | // cout<<"depth="<<(int)in->depth<<" time="<<in->lastAccessTime<<endl;
|
---|
[422] | 1181 | if (in->mDepth >= mMinCollapseDepth && in->mLastAccessTime <= maxAccessTime)
|
---|
[410] | 1182 | {
|
---|
| 1183 | released = CollapseSubtree(node, time);
|
---|
| 1184 | break;
|
---|
| 1185 | }
|
---|
[420] | 1186 | if (in->GetBack()->GetAccessTime() < in->GetFront()->GetAccessTime())
|
---|
[410] | 1187 | {
|
---|
[419] | 1188 | tstack.push(in->GetFront());
|
---|
| 1189 | tstack.push(in->GetBack());
|
---|
[410] | 1190 | }
|
---|
| 1191 | else
|
---|
| 1192 | {
|
---|
[419] | 1193 | tstack.push(in->GetBack());
|
---|
| 1194 | tstack.push(in->GetFront());
|
---|
[410] | 1195 | }
|
---|
| 1196 | }
|
---|
| 1197 | }
|
---|
[406] | 1198 |
|
---|
[410] | 1199 | while (tstack.empty())
|
---|
| 1200 | {
|
---|
| 1201 | // could find node to collaps...
|
---|
| 1202 | // cout<<"Could not find a node to release "<<endl;
|
---|
| 1203 | break;
|
---|
| 1204 | }
|
---|
[408] | 1205 |
|
---|
[410] | 1206 | return released;
|
---|
[408] | 1207 | }
|
---|
[406] | 1208 |
|
---|
| 1209 |
|
---|
[462] | 1210 | VspKdNode *VspKdTree::SubdivideLeaf(VspKdLeaf *leaf,
|
---|
[410] | 1211 | const float sizeThreshold)
|
---|
[408] | 1212 | {
|
---|
[462] | 1213 | VspKdNode *node = leaf;
|
---|
[406] | 1214 |
|
---|
[410] | 1215 | AxisAlignedBox3 leafBBox = GetBBox(leaf);
|
---|
[406] | 1216 |
|
---|
[408] | 1217 | static int pass = 0;
|
---|
[410] | 1218 | ++ pass;
|
---|
[408] | 1219 |
|
---|
[410] | 1220 | // check if we should perform a dynamic subdivision of the leaf
|
---|
[420] | 1221 | if (// leaf->mRays.size() > (unsigned)termMinCost &&
|
---|
[421] | 1222 | (leaf->GetPvsSize() >= mTermMinPvs) &&
|
---|
[410] | 1223 | (SqrMagnitude(leafBBox.Size()) > sizeThreshold) )
|
---|
| 1224 | {
|
---|
[423] | 1225 | // memory check and release
|
---|
[422] | 1226 | if (GetMemUsage() > mMaxTotalMemory)
|
---|
[410] | 1227 | ReleaseMemory(pass);
|
---|
| 1228 |
|
---|
| 1229 | AxisAlignedBox3 backBBox, frontBBox;
|
---|
[406] | 1230 |
|
---|
[410] | 1231 | // subdivide the node
|
---|
| 1232 | node = SubdivideNode(leaf, leafBBox, backBBox, frontBBox);
|
---|
| 1233 | }
|
---|
| 1234 | return node;
|
---|
[406] | 1235 | }
|
---|
| 1236 |
|
---|
| 1237 |
|
---|
| 1238 |
|
---|
[423] | 1239 | void VspKdTree::UpdateRays(VssRayContainer &remove,
|
---|
| 1240 | VssRayContainer &add)
|
---|
[406] | 1241 | {
|
---|
[462] | 1242 | VspKdLeaf::NewMail();
|
---|
[406] | 1243 |
|
---|
[410] | 1244 | // schedule rays for removal
|
---|
| 1245 | for(VssRayContainer::const_iterator ri = remove.begin();
|
---|
| 1246 | ri != remove.end(); ++ ri)
|
---|
| 1247 | {
|
---|
| 1248 | (*ri)->ScheduleForRemoval();
|
---|
| 1249 | }
|
---|
[406] | 1250 |
|
---|
[410] | 1251 | int inactive = 0;
|
---|
[406] | 1252 |
|
---|
[410] | 1253 | for (VssRayContainer::const_iterator ri = remove.begin(); ri != remove.end(); ++ ri)
|
---|
| 1254 | {
|
---|
| 1255 | if ((*ri)->ScheduledForRemoval())
|
---|
| 1256 | // RemoveRay(*ri, NULL, false);
|
---|
| 1257 | // !!! BUG - with true it does not work correctly - aggreated delete
|
---|
| 1258 | RemoveRay(*ri, NULL, true);
|
---|
| 1259 | else
|
---|
| 1260 | ++ inactive;
|
---|
| 1261 | }
|
---|
[406] | 1262 |
|
---|
[410] | 1263 | // cout<<"all/inactive"<<remove.size()<<"/"<<inactive<<endl;
|
---|
[423] | 1264 | for (VssRayContainer::const_iterator ri = add.begin(); ri != add.end(); ++ ri)
|
---|
[410] | 1265 | {
|
---|
| 1266 | AddRay(*ri);
|
---|
| 1267 | }
|
---|
[406] | 1268 | }
|
---|
| 1269 |
|
---|
| 1270 |
|
---|
[410] | 1271 | void VspKdTree::RemoveRay(VssRay *ray,
|
---|
[462] | 1272 | vector<VspKdLeaf *> *affectedLeaves,
|
---|
[410] | 1273 | const bool removeAllScheduledRays)
|
---|
[406] | 1274 | {
|
---|
[410] | 1275 | stack<RayTraversalData> tstack;
|
---|
[406] | 1276 |
|
---|
[420] | 1277 | tstack.push(RayTraversalData(mRoot, RayInfo(ray)));
|
---|
[406] | 1278 |
|
---|
[410] | 1279 | RayTraversalData data;
|
---|
[406] | 1280 |
|
---|
[410] | 1281 | // cout<<"Number of ray refs = "<<ray->RefCount()<<endl;
|
---|
| 1282 | while (!tstack.empty())
|
---|
| 1283 | {
|
---|
| 1284 | data = tstack.top();
|
---|
| 1285 | tstack.pop();
|
---|
[406] | 1286 |
|
---|
[419] | 1287 | if (!data.mNode->IsLeaf())
|
---|
[410] | 1288 | {
|
---|
| 1289 | // split the set of rays in two groups intersecting the
|
---|
| 1290 | // two subtrees
|
---|
| 1291 | TraverseInternalNode(data, tstack);
|
---|
| 1292 | }
|
---|
| 1293 | else
|
---|
| 1294 | {
|
---|
| 1295 | // remove the ray from the leaf
|
---|
| 1296 | // find the ray in the leaf and swap it with the last ray...
|
---|
[462] | 1297 | VspKdLeaf *leaf = (VspKdLeaf *)data.mNode;
|
---|
[408] | 1298 |
|
---|
[410] | 1299 | if (!leaf->Mailed())
|
---|
| 1300 | {
|
---|
| 1301 | leaf->Mail();
|
---|
| 1302 |
|
---|
| 1303 | if (affectedLeaves)
|
---|
| 1304 | affectedLeaves->push_back(leaf);
|
---|
[406] | 1305 |
|
---|
[410] | 1306 | if (removeAllScheduledRays)
|
---|
| 1307 | {
|
---|
[420] | 1308 | int tail = (int)leaf->mRays.size() - 1;
|
---|
[406] | 1309 |
|
---|
[420] | 1310 | for (int i=0; i < (int)(leaf->mRays.size()); ++ i)
|
---|
[410] | 1311 | {
|
---|
[420] | 1312 | if (leaf->mRays[i].mRay->ScheduledForRemoval())
|
---|
[410] | 1313 | {
|
---|
| 1314 | // find a ray to replace it with
|
---|
[420] | 1315 | while (tail >= i && leaf->mRays[tail].mRay->ScheduledForRemoval())
|
---|
[410] | 1316 | {
|
---|
[414] | 1317 | ++ mStat.removedRayRefs;
|
---|
[420] | 1318 | leaf->mRays[tail].mRay->Unref();
|
---|
| 1319 | leaf->mRays.pop_back();
|
---|
[410] | 1320 |
|
---|
| 1321 | -- tail;
|
---|
| 1322 | }
|
---|
| 1323 |
|
---|
| 1324 | if (tail < i)
|
---|
| 1325 | break;
|
---|
[408] | 1326 |
|
---|
[414] | 1327 | ++ mStat.removedRayRefs;
|
---|
[410] | 1328 |
|
---|
[420] | 1329 | leaf->mRays[i].mRay->Unref();
|
---|
| 1330 | leaf->mRays[i] = leaf->mRays[tail];
|
---|
| 1331 | leaf->mRays.pop_back();
|
---|
[410] | 1332 | -- tail;
|
---|
| 1333 | }
|
---|
| 1334 | }
|
---|
| 1335 | }
|
---|
| 1336 | }
|
---|
[406] | 1337 |
|
---|
[410] | 1338 | if (!removeAllScheduledRays)
|
---|
[420] | 1339 | for (int i=0; i < (int)leaf->mRays.size(); i++)
|
---|
[410] | 1340 | {
|
---|
[420] | 1341 | if (leaf->mRays[i].mRay == ray)
|
---|
[410] | 1342 | {
|
---|
[414] | 1343 | ++ mStat.removedRayRefs;
|
---|
[410] | 1344 | ray->Unref();
|
---|
[420] | 1345 | leaf->mRays[i] = leaf->mRays[leaf->mRays.size() - 1];
|
---|
| 1346 | leaf->mRays.pop_back();
|
---|
[410] | 1347 | // check this ray again
|
---|
| 1348 | break;
|
---|
| 1349 | }
|
---|
| 1350 | }
|
---|
| 1351 | }
|
---|
[408] | 1352 | }
|
---|
| 1353 |
|
---|
[410] | 1354 | if (ray->RefCount() != 0)
|
---|
| 1355 | {
|
---|
| 1356 | cerr << "Error: Number of remaining refs = " << ray->RefCount() << endl;
|
---|
| 1357 | exit(1);
|
---|
| 1358 | }
|
---|
| 1359 |
|
---|
[406] | 1360 | }
|
---|
| 1361 |
|
---|
[410] | 1362 | void VspKdTree::AddRay(VssRay *ray)
|
---|
[406] | 1363 | {
|
---|
[410] | 1364 | stack<RayTraversalData> tstack;
|
---|
[406] | 1365 |
|
---|
[420] | 1366 | tstack.push(RayTraversalData(mRoot, RayInfo(ray)));
|
---|
[406] | 1367 |
|
---|
[410] | 1368 | RayTraversalData data;
|
---|
[406] | 1369 |
|
---|
[410] | 1370 | while (!tstack.empty())
|
---|
| 1371 | {
|
---|
| 1372 | data = tstack.top();
|
---|
| 1373 | tstack.pop();
|
---|
[406] | 1374 |
|
---|
[419] | 1375 | if (!data.mNode->IsLeaf())
|
---|
[410] | 1376 | {
|
---|
| 1377 | TraverseInternalNode(data, tstack);
|
---|
| 1378 | }
|
---|
| 1379 | else
|
---|
| 1380 | {
|
---|
| 1381 | // remove the ray from the leaf
|
---|
[420] | 1382 | // find the ray in the leaf and swap it with the last ray
|
---|
[462] | 1383 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(data.mNode);
|
---|
[423] | 1384 |
|
---|
[419] | 1385 | leaf->AddRay(data.mRayData);
|
---|
[413] | 1386 | ++ mStat.addedRayRefs;
|
---|
[410] | 1387 | }
|
---|
| 1388 | }
|
---|
[406] | 1389 | }
|
---|
| 1390 |
|
---|
[410] | 1391 | void VspKdTree::TraverseInternalNode(RayTraversalData &data,
|
---|
| 1392 | stack<RayTraversalData> &tstack)
|
---|
[406] | 1393 | {
|
---|
[462] | 1394 | VspKdInterior *in = dynamic_cast<VspKdInterior *>(data.mNode);
|
---|
[408] | 1395 |
|
---|
[462] | 1396 | if (in->mAxis <= VspKdNode::SPLIT_Z)
|
---|
[410] | 1397 | {
|
---|
| 1398 | // determine the side of this ray with respect to the plane
|
---|
[419] | 1399 | int side = in->ComputeRayIntersection(data.mRayData, data.mRayData.mRay->mT);
|
---|
[408] | 1400 |
|
---|
[433] | 1401 | if (side == 0)
|
---|
[410] | 1402 | {
|
---|
[433] | 1403 | if (data.mRayData.mRay->HasPosDir(in->mAxis))
|
---|
| 1404 | {
|
---|
| 1405 | tstack.push(RayTraversalData(in->GetBack(),
|
---|
| 1406 | RayInfo(data.mRayData.mRay, data.mRayData.mMinT, data.mRayData.mRay->mT)));
|
---|
[408] | 1407 |
|
---|
[433] | 1408 | tstack.push(RayTraversalData(in->GetFront(),
|
---|
| 1409 | RayInfo(data.mRayData.mRay, data.mRayData.mRay->mT, data.mRayData.mMaxT)));
|
---|
[408] | 1410 |
|
---|
[433] | 1411 | }
|
---|
| 1412 | else
|
---|
| 1413 | {
|
---|
| 1414 | tstack.push(RayTraversalData(in->GetBack(),
|
---|
| 1415 | RayInfo(data.mRayData.mRay,
|
---|
| 1416 | data.mRayData.mRay->mT,
|
---|
| 1417 | data.mRayData.mMaxT)));
|
---|
| 1418 | tstack.push(RayTraversalData(in->GetFront(),
|
---|
| 1419 | RayInfo(data.mRayData.mRay,
|
---|
| 1420 | data.mRayData.mMinT,
|
---|
| 1421 | data.mRayData.mRay->mT)));
|
---|
| 1422 | }
|
---|
[410] | 1423 | }
|
---|
[433] | 1424 | else
|
---|
| 1425 | if (side == 1)
|
---|
| 1426 | tstack.push(RayTraversalData(in->GetFront(), data.mRayData));
|
---|
| 1427 | else
|
---|
| 1428 | tstack.push(RayTraversalData(in->GetBack(), data.mRayData));
|
---|
[410] | 1429 | }
|
---|
| 1430 | else
|
---|
| 1431 | {
|
---|
| 1432 | // directional split
|
---|
[419] | 1433 | if (data.mRayData.mRay->GetDirParametrization(in->mAxis - 3) > in->mPosition)
|
---|
| 1434 | tstack.push(RayTraversalData(in->GetFront(), data.mRayData));
|
---|
[408] | 1435 | else
|
---|
[419] | 1436 | tstack.push(RayTraversalData(in->GetBack(), data.mRayData));
|
---|
[410] | 1437 | }
|
---|
[406] | 1438 | }
|
---|
| 1439 |
|
---|
[408] | 1440 |
|
---|
[462] | 1441 | int VspKdTree::CollapseSubtree(VspKdNode *sroot, const int time)
|
---|
[406] | 1442 | {
|
---|
[410] | 1443 | // first count all rays in the subtree
|
---|
| 1444 | // use mail 1 for this purpose
|
---|
[462] | 1445 | stack<VspKdNode *> tstack;
|
---|
[410] | 1446 |
|
---|
| 1447 | int rayCount = 0;
|
---|
| 1448 | int totalRayCount = 0;
|
---|
| 1449 | int collapsedNodes = 0;
|
---|
[408] | 1450 |
|
---|
| 1451 | #if DEBUG_COLLAPSE
|
---|
[420] | 1452 | cout << "Collapsing subtree" << endl;
|
---|
[428] | 1453 | cout << "accessTime=" << sroot->GetAccessTime() << endl;
|
---|
[420] | 1454 | cout << "depth=" << (int)sroot->depth << endl;
|
---|
[408] | 1455 | #endif
|
---|
| 1456 |
|
---|
[410] | 1457 | // tstat.collapsedSubtrees++;
|
---|
| 1458 | // tstat.collapseDepths += (int)sroot->depth;
|
---|
| 1459 | // tstat.collapseAccessTimes += time - sroot->GetAccessTime();
|
---|
| 1460 | tstack.push(sroot);
|
---|
| 1461 | VssRay::NewMail();
|
---|
[408] | 1462 |
|
---|
[410] | 1463 | while (!tstack.empty())
|
---|
| 1464 | {
|
---|
| 1465 | ++ collapsedNodes;
|
---|
| 1466 |
|
---|
[462] | 1467 | VspKdNode *node = tstack.top();
|
---|
[410] | 1468 | tstack.pop();
|
---|
| 1469 | if (node->IsLeaf())
|
---|
| 1470 | {
|
---|
[462] | 1471 | VspKdLeaf *leaf = (VspKdLeaf *) node;
|
---|
[410] | 1472 |
|
---|
[420] | 1473 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
| 1474 | ri != leaf->mRays.end(); ++ ri)
|
---|
[410] | 1475 | {
|
---|
| 1476 | ++ totalRayCount;
|
---|
[408] | 1477 |
|
---|
[410] | 1478 | if ((*ri).mRay->IsActive() && !(*ri).mRay->Mailed())
|
---|
| 1479 | {
|
---|
[408] | 1480 | (*ri).mRay->Mail();
|
---|
[410] | 1481 | ++ rayCount;
|
---|
[408] | 1482 | }
|
---|
[410] | 1483 | }
|
---|
| 1484 | }
|
---|
| 1485 | else
|
---|
| 1486 | {
|
---|
[462] | 1487 | tstack.push(((VspKdInterior *)node)->GetFront());
|
---|
| 1488 | tstack.push(((VspKdInterior *)node)->GetBack());
|
---|
[410] | 1489 | }
|
---|
| 1490 | }
|
---|
[406] | 1491 |
|
---|
[410] | 1492 | VssRay::NewMail();
|
---|
[406] | 1493 |
|
---|
[410] | 1494 | // create a new node that will hold the rays
|
---|
[462] | 1495 | VspKdLeaf *newLeaf = new VspKdLeaf(sroot->mParent, rayCount);
|
---|
[410] | 1496 |
|
---|
[419] | 1497 | if (newLeaf->mParent)
|
---|
| 1498 | newLeaf->mParent->ReplaceChildLink(sroot, newLeaf);
|
---|
[406] | 1499 |
|
---|
[410] | 1500 | tstack.push(sroot);
|
---|
[406] | 1501 |
|
---|
[410] | 1502 | while (!tstack.empty())
|
---|
| 1503 | {
|
---|
[462] | 1504 | VspKdNode *node = tstack.top();
|
---|
[410] | 1505 | tstack.pop();
|
---|
[408] | 1506 |
|
---|
[410] | 1507 | if (node->IsLeaf())
|
---|
| 1508 | {
|
---|
[462] | 1509 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
[408] | 1510 |
|
---|
[420] | 1511 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
| 1512 | ri != leaf->mRays.end(); ++ ri)
|
---|
[410] | 1513 | {
|
---|
| 1514 | // unref this ray from the old node
|
---|
| 1515 | if ((*ri).mRay->IsActive())
|
---|
| 1516 | {
|
---|
[408] | 1517 | (*ri).mRay->Unref();
|
---|
[410] | 1518 | if (!(*ri).mRay->Mailed())
|
---|
| 1519 | {
|
---|
[408] | 1520 | (*ri).mRay->Mail();
|
---|
| 1521 | newLeaf->AddRay(*ri);
|
---|
| 1522 | }
|
---|
[410] | 1523 | }
|
---|
| 1524 | else
|
---|
[408] | 1525 | (*ri).mRay->Unref();
|
---|
[410] | 1526 | }
|
---|
| 1527 | }
|
---|
| 1528 | else
|
---|
| 1529 | {
|
---|
[462] | 1530 | VspKdInterior *interior =
|
---|
| 1531 | dynamic_cast<VspKdInterior *>(node);
|
---|
[419] | 1532 | tstack.push(interior->GetBack());
|
---|
| 1533 | tstack.push(interior->GetFront());
|
---|
[410] | 1534 | }
|
---|
| 1535 | }
|
---|
[406] | 1536 |
|
---|
[410] | 1537 | // delete the node and all its children
|
---|
[420] | 1538 | DEL_PTR(sroot);
|
---|
[408] | 1539 |
|
---|
[462] | 1540 | // for(VspKdNode::SRayContainer::iterator ri = newleaf->mRays.begin();
|
---|
[420] | 1541 | // ri != newleaf->mRays.end(); ++ ri)
|
---|
[410] | 1542 | // (*ri).ray->UnMail(2);
|
---|
[408] | 1543 |
|
---|
| 1544 | #if DEBUG_COLLAPSE
|
---|
[423] | 1545 | cout << "Total memory before=" << GetMemUsage() << endl;
|
---|
[408] | 1546 | #endif
|
---|
| 1547 |
|
---|
[414] | 1548 | mStat.nodes -= collapsedNodes - 1;
|
---|
| 1549 | mStat.rayRefs -= totalRayCount - rayCount;
|
---|
[408] | 1550 |
|
---|
| 1551 | #if DEBUG_COLLAPSE
|
---|
[410] | 1552 | cout << "collapsed nodes" << collapsedNodes << endl;
|
---|
| 1553 | cout << "collapsed rays" << totalRayCount - rayCount << endl;
|
---|
| 1554 | cout << "Total memory after=" << GetMemUsage() << endl;
|
---|
| 1555 | cout << "================================" << endl;
|
---|
[408] | 1556 | #endif
|
---|
| 1557 |
|
---|
| 1558 | // tstat.collapsedNodes += collapsedNodes;
|
---|
| 1559 | // tstat.collapsedRays += totalRayCount - rayCount;
|
---|
[410] | 1560 | return totalRayCount - rayCount;
|
---|
[406] | 1561 | }
|
---|
| 1562 |
|
---|
[408] | 1563 |
|
---|
[462] | 1564 | int VspKdTree::GetPvsSize(VspKdNode *node, const AxisAlignedBox3 &box) const
|
---|
[406] | 1565 | {
|
---|
[462] | 1566 | stack<VspKdNode *> tstack;
|
---|
[419] | 1567 | tstack.push(mRoot);
|
---|
[408] | 1568 |
|
---|
| 1569 | Intersectable::NewMail();
|
---|
| 1570 | int pvsSize = 0;
|
---|
| 1571 |
|
---|
[410] | 1572 | while (!tstack.empty())
|
---|
| 1573 | {
|
---|
[462] | 1574 | VspKdNode *node = tstack.top();
|
---|
[410] | 1575 | tstack.pop();
|
---|
[408] | 1576 |
|
---|
[410] | 1577 | if (node->IsLeaf())
|
---|
| 1578 | {
|
---|
[462] | 1579 | VspKdLeaf *leaf = (VspKdLeaf *)node;
|
---|
[420] | 1580 | for (RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
| 1581 | ri != leaf->mRays.end(); ++ ri)
|
---|
[410] | 1582 | {
|
---|
| 1583 | if ((*ri).mRay->IsActive())
|
---|
| 1584 | {
|
---|
[408] | 1585 | Intersectable *object;
|
---|
| 1586 | #if BIDIRECTIONAL_RAY
|
---|
| 1587 | object = (*ri).mRay->mOriginObject;
|
---|
[410] | 1588 |
|
---|
| 1589 | if (object && !object->Mailed())
|
---|
| 1590 | {
|
---|
| 1591 | ++ pvsSize;
|
---|
[408] | 1592 | object->Mail();
|
---|
| 1593 | }
|
---|
| 1594 | #endif
|
---|
| 1595 | object = (*ri).mRay->mTerminationObject;
|
---|
[410] | 1596 | if (object && !object->Mailed())
|
---|
| 1597 | {
|
---|
| 1598 | ++ pvsSize;
|
---|
[408] | 1599 | object->Mail();
|
---|
| 1600 | }
|
---|
| 1601 | }
|
---|
[410] | 1602 | }
|
---|
| 1603 | }
|
---|
| 1604 | else
|
---|
| 1605 | {
|
---|
[462] | 1606 | VspKdInterior *in = dynamic_cast<VspKdInterior *>(node);
|
---|
[410] | 1607 |
|
---|
[419] | 1608 | if (in->mAxis < 3)
|
---|
[410] | 1609 | {
|
---|
[419] | 1610 | if (box.Max(in->mAxis) >= in->mPosition)
|
---|
| 1611 | tstack.push(in->GetFront());
|
---|
[408] | 1612 |
|
---|
[419] | 1613 | if (box.Min(in->mAxis) <= in->mPosition)
|
---|
| 1614 | tstack.push(in->GetBack());
|
---|
[410] | 1615 | }
|
---|
| 1616 | else
|
---|
| 1617 | {
|
---|
[408] | 1618 | // both nodes for directional splits
|
---|
[419] | 1619 | tstack.push(in->GetFront());
|
---|
| 1620 | tstack.push(in->GetBack());
|
---|
[408] | 1621 | }
|
---|
| 1622 | }
|
---|
| 1623 | }
|
---|
[410] | 1624 |
|
---|
[408] | 1625 | return pvsSize;
|
---|
[406] | 1626 | }
|
---|
| 1627 |
|
---|
[410] | 1628 | void VspKdTree::GetRayContributionStatistics(float &minRayContribution,
|
---|
| 1629 | float &maxRayContribution,
|
---|
| 1630 | float &avgRayContribution)
|
---|
[406] | 1631 | {
|
---|
[462] | 1632 | stack<VspKdNode *> tstack;
|
---|
[419] | 1633 | tstack.push(mRoot);
|
---|
[408] | 1634 |
|
---|
| 1635 | minRayContribution = 1.0f;
|
---|
| 1636 | maxRayContribution = 0.0f;
|
---|
[410] | 1637 |
|
---|
[408] | 1638 | float sumRayContribution = 0.0f;
|
---|
| 1639 | int leaves = 0;
|
---|
[406] | 1640 |
|
---|
[410] | 1641 | while (!tstack.empty())
|
---|
| 1642 | {
|
---|
[462] | 1643 | VspKdNode *node = tstack.top();
|
---|
[410] | 1644 | tstack.pop();
|
---|
| 1645 | if (node->IsLeaf())
|
---|
| 1646 | {
|
---|
[408] | 1647 | leaves++;
|
---|
[462] | 1648 | VspKdLeaf *leaf = (VspKdLeaf *)node;
|
---|
[408] | 1649 | float c = leaf->GetAvgRayContribution();
|
---|
[410] | 1650 |
|
---|
[408] | 1651 | if (c > maxRayContribution)
|
---|
| 1652 | maxRayContribution = c;
|
---|
| 1653 | if (c < minRayContribution)
|
---|
| 1654 | minRayContribution = c;
|
---|
| 1655 | sumRayContribution += c;
|
---|
| 1656 |
|
---|
[410] | 1657 | }
|
---|
| 1658 | else {
|
---|
[462] | 1659 | VspKdInterior *in = (VspKdInterior *)node;
|
---|
[408] | 1660 | // both nodes for directional splits
|
---|
[419] | 1661 | tstack.push(in->GetFront());
|
---|
| 1662 | tstack.push(in->GetBack());
|
---|
[408] | 1663 | }
|
---|
| 1664 | }
|
---|
| 1665 |
|
---|
[423] | 1666 | Debug << "sum=" << sumRayContribution << endl;
|
---|
| 1667 | Debug << "leaves=" << leaves << endl;
|
---|
| 1668 | avgRayContribution = sumRayContribution / (float)leaves;
|
---|
[406] | 1669 | }
|
---|
| 1670 |
|
---|
| 1671 |
|
---|
[410] | 1672 | int VspKdTree::GenerateRays(const float ratioPerLeaf,
|
---|
| 1673 | SimpleRayContainer &rays)
|
---|
[406] | 1674 | {
|
---|
[462] | 1675 | stack<VspKdNode *> tstack;
|
---|
[419] | 1676 | tstack.push(mRoot);
|
---|
[408] | 1677 |
|
---|
[410] | 1678 | while (!tstack.empty())
|
---|
| 1679 | {
|
---|
[462] | 1680 | VspKdNode *node = tstack.top();
|
---|
[410] | 1681 | tstack.pop();
|
---|
[408] | 1682 |
|
---|
[410] | 1683 | if (node->IsLeaf())
|
---|
| 1684 | {
|
---|
[462] | 1685 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
[423] | 1686 |
|
---|
[408] | 1687 | float c = leaf->GetAvgRayContribution();
|
---|
[411] | 1688 | int num = (int)(c*ratioPerLeaf + 0.5);
|
---|
[408] | 1689 | // cout<<num<<" ";
|
---|
| 1690 |
|
---|
[410] | 1691 | for (int i=0; i < num; i++)
|
---|
| 1692 | {
|
---|
[408] | 1693 | Vector3 origin = GetBBox(leaf).GetRandomPoint();
|
---|
[419] | 1694 | /*Vector3 dirVector = GetDirBBox(leaf).GetRandomPoint();
|
---|
[408] | 1695 | Vector3 direction = Vector3(sin(dirVector.x), sin(dirVector.y), cos(dirVector.x));
|
---|
| 1696 | //cout<<"dir vector.x="<<dirVector.x<<"direction'.x="<<atan2(direction.x, direction.y)<<endl;
|
---|
[419] | 1697 | rays.push_back(SimpleRay(origin, direction));*/
|
---|
[408] | 1698 | }
|
---|
| 1699 |
|
---|
[410] | 1700 | }
|
---|
| 1701 | else
|
---|
| 1702 | {
|
---|
[462] | 1703 | VspKdInterior *in =
|
---|
| 1704 | dynamic_cast<VspKdInterior *>(node);
|
---|
[408] | 1705 | // both nodes for directional splits
|
---|
[419] | 1706 | tstack.push(in->GetFront());
|
---|
| 1707 | tstack.push(in->GetBack());
|
---|
[408] | 1708 | }
|
---|
[406] | 1709 | }
|
---|
| 1710 |
|
---|
[411] | 1711 | return (int)rays.size();
|
---|
[406] | 1712 | }
|
---|
| 1713 |
|
---|
| 1714 |
|
---|
[410] | 1715 | float VspKdTree::GetAvgPvsSize()
|
---|
[406] | 1716 | {
|
---|
[462] | 1717 | stack<VspKdNode *> tstack;
|
---|
[419] | 1718 | tstack.push(mRoot);
|
---|
[406] | 1719 |
|
---|
[408] | 1720 | int sumPvs = 0;
|
---|
| 1721 | int leaves = 0;
|
---|
[410] | 1722 |
|
---|
| 1723 | while (!tstack.empty())
|
---|
| 1724 | {
|
---|
[462] | 1725 | VspKdNode *node = tstack.top();
|
---|
[410] | 1726 | tstack.pop();
|
---|
[408] | 1727 |
|
---|
[410] | 1728 | if (node->IsLeaf())
|
---|
| 1729 | {
|
---|
[462] | 1730 | VspKdLeaf *leaf = (VspKdLeaf *)node;
|
---|
[408] | 1731 | // update pvs size
|
---|
| 1732 | leaf->UpdatePvsSize();
|
---|
| 1733 | sumPvs += leaf->GetPvsSize();
|
---|
| 1734 | leaves++;
|
---|
[410] | 1735 | }
|
---|
| 1736 | else
|
---|
| 1737 | {
|
---|
[462] | 1738 | VspKdInterior *in = (VspKdInterior *)node;
|
---|
[408] | 1739 | // both nodes for directional splits
|
---|
[419] | 1740 | tstack.push(in->GetFront());
|
---|
| 1741 | tstack.push(in->GetBack());
|
---|
[406] | 1742 | }
|
---|
| 1743 | }
|
---|
[408] | 1744 |
|
---|
[410] | 1745 | return sumPvs / (float)leaves;
|
---|
[418] | 1746 | }
|
---|
| 1747 |
|
---|
[462] | 1748 | VspKdNode *VspKdTree::GetRoot() const
|
---|
[418] | 1749 | {
|
---|
| 1750 | return mRoot;
|
---|
| 1751 | }
|
---|
| 1752 |
|
---|
[462] | 1753 | AxisAlignedBox3 VspKdTree::GetBBox(VspKdNode *node) const
|
---|
[418] | 1754 | {
|
---|
[419] | 1755 | if (node->mParent == NULL)
|
---|
| 1756 | return mBox;
|
---|
[418] | 1757 |
|
---|
| 1758 | if (!node->IsLeaf())
|
---|
[462] | 1759 | return (dynamic_cast<VspKdInterior *>(node))->mBox;
|
---|
[418] | 1760 |
|
---|
[419] | 1761 | if (node->mParent->mAxis >= 3)
|
---|
| 1762 | return node->mParent->mBox;
|
---|
[418] | 1763 |
|
---|
[419] | 1764 | AxisAlignedBox3 box(node->mParent->mBox);
|
---|
[448] | 1765 | if (node->mParent->GetFront() == node)
|
---|
[419] | 1766 | box.SetMin(node->mParent->mAxis, node->mParent->mPosition);
|
---|
[418] | 1767 | else
|
---|
[419] | 1768 | box.SetMax(node->mParent->mAxis, node->mParent->mPosition);
|
---|
| 1769 |
|
---|
| 1770 | return box;
|
---|
[418] | 1771 | }
|
---|
| 1772 |
|
---|
| 1773 | int VspKdTree::GetRootPvsSize() const
|
---|
| 1774 | {
|
---|
| 1775 | return GetPvsSize(mRoot, mBox);
|
---|
| 1776 | }
|
---|
[419] | 1777 |
|
---|
| 1778 | const VspKdStatistics &VspKdTree::GetStatistics() const
|
---|
| 1779 | {
|
---|
| 1780 | return mStat;
|
---|
[420] | 1781 | }
|
---|
| 1782 |
|
---|
| 1783 | void VspKdTree::AddRays(VssRayContainer &add)
|
---|
| 1784 | {
|
---|
| 1785 | VssRayContainer remove;
|
---|
| 1786 | UpdateRays(remove, add);
|
---|
| 1787 | }
|
---|
| 1788 |
|
---|
| 1789 | // return memory usage in MB
|
---|
| 1790 | float VspKdTree::GetMemUsage() const
|
---|
| 1791 | {
|
---|
| 1792 | return
|
---|
| 1793 | (sizeof(VspKdTree) +
|
---|
[462] | 1794 | mStat.Leaves() * sizeof(VspKdLeaf) +
|
---|
| 1795 | mStat.Interior() * sizeof(VspKdInterior) +
|
---|
[420] | 1796 | mStat.rayRefs * sizeof(RayInfo)) / (1024.0f * 1024.0f);
|
---|
| 1797 | }
|
---|
| 1798 |
|
---|
| 1799 | float VspKdTree::GetRayMemUsage() const
|
---|
| 1800 | {
|
---|
| 1801 | return mStat.rays * (sizeof(VssRay))/(1024.0f * 1024.0f);
|
---|
[425] | 1802 | }
|
---|
| 1803 |
|
---|
[462] | 1804 | int VspKdTree::ComputePvsSize(VspKdNode *node,
|
---|
[426] | 1805 | const RayInfoContainer &globalRays) const
|
---|
[425] | 1806 | {
|
---|
| 1807 | int pvsSize = 0;
|
---|
| 1808 |
|
---|
| 1809 | const AxisAlignedBox3 box = GetBBox(node);
|
---|
| 1810 |
|
---|
| 1811 | RayInfoContainer::const_iterator it, it_end = globalRays.end();
|
---|
| 1812 |
|
---|
[426] | 1813 | // warning: implicit conversion from VssRay to Ray
|
---|
[425] | 1814 | for (it = globalRays.begin(); it != globalRays.end(); ++ it)
|
---|
[426] | 1815 | pvsSize += box.GetMinMaxT(*(*it).mRay, NULL, NULL);
|
---|
| 1816 |
|
---|
[425] | 1817 | return pvsSize;
|
---|
[428] | 1818 | }
|
---|
| 1819 |
|
---|
[462] | 1820 | void VspKdTree::CollectLeaves(vector<VspKdLeaf *> &leaves) const
|
---|
[428] | 1821 | {
|
---|
[462] | 1822 | stack<VspKdNode *> nodeStack;
|
---|
[428] | 1823 | nodeStack.push(mRoot);
|
---|
| 1824 |
|
---|
| 1825 | while (!nodeStack.empty())
|
---|
| 1826 | {
|
---|
[462] | 1827 | VspKdNode *node = nodeStack.top();
|
---|
[428] | 1828 |
|
---|
| 1829 | nodeStack.pop();
|
---|
| 1830 |
|
---|
| 1831 | if (node->IsLeaf())
|
---|
| 1832 | {
|
---|
[462] | 1833 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
[428] | 1834 | leaves.push_back(leaf);
|
---|
| 1835 | }
|
---|
| 1836 | else
|
---|
| 1837 | {
|
---|
[462] | 1838 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
[428] | 1839 |
|
---|
[448] | 1840 | nodeStack.push(interior->GetBack());
|
---|
| 1841 | nodeStack.push(interior->GetFront());
|
---|
[428] | 1842 | }
|
---|
| 1843 | }
|
---|
[452] | 1844 | }
|
---|
| 1845 |
|
---|
[462] | 1846 | int VspKdTree::FindNeighbors(VspKdLeaf *n,
|
---|
| 1847 | vector<VspKdLeaf *> &neighbors,
|
---|
[452] | 1848 | bool onlyUnmailed)
|
---|
| 1849 | {
|
---|
[462] | 1850 | stack<VspKdNode *> nodeStack;
|
---|
[452] | 1851 |
|
---|
| 1852 | nodeStack.push(mRoot);
|
---|
| 1853 |
|
---|
| 1854 | AxisAlignedBox3 box = GetBBox(n);
|
---|
| 1855 |
|
---|
| 1856 | while (!nodeStack.empty())
|
---|
| 1857 | {
|
---|
[462] | 1858 | VspKdNode *node = nodeStack.top();
|
---|
[452] | 1859 | nodeStack.pop();
|
---|
| 1860 |
|
---|
| 1861 | if (node->IsLeaf())
|
---|
| 1862 | {
|
---|
[462] | 1863 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
[452] | 1864 |
|
---|
| 1865 | if (leaf != n && (!onlyUnmailed || !leaf->Mailed()))
|
---|
| 1866 | neighbors.push_back(leaf);
|
---|
| 1867 | }
|
---|
| 1868 | else
|
---|
| 1869 | {
|
---|
[462] | 1870 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
[452] | 1871 |
|
---|
| 1872 | if (interior->mPosition > box.Max(interior->mAxis))
|
---|
| 1873 | nodeStack.push(interior->mBack);
|
---|
| 1874 | else
|
---|
| 1875 | {
|
---|
| 1876 | if (interior->mPosition < box.Min(interior->mAxis))
|
---|
| 1877 | nodeStack.push(interior->mFront);
|
---|
| 1878 | else
|
---|
| 1879 | {
|
---|
| 1880 | // random decision
|
---|
| 1881 | nodeStack.push(interior->mBack);
|
---|
| 1882 | nodeStack.push(interior->mFront);
|
---|
| 1883 | }
|
---|
| 1884 | }
|
---|
| 1885 | }
|
---|
| 1886 | }
|
---|
| 1887 |
|
---|
| 1888 | return (int)neighbors.size();
|
---|
| 1889 | }
|
---|
[462] | 1890 | |
---|
[452] | 1891 |
|
---|
[462] | 1892 |
|
---|
| 1893 | void VspKdTree::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
[452] | 1894 | {
|
---|
[462] | 1895 | mViewCellsManager = vcm;
|
---|
| 1896 | }
|
---|
[452] | 1897 |
|
---|
[462] | 1898 |
|
---|
| 1899 | int VspKdTree::CastRay(Ray &ray)
|
---|
| 1900 | {
|
---|
| 1901 | int hits = 0;
|
---|
| 1902 |
|
---|
| 1903 | stack<RayTraversalData> tStack;
|
---|
| 1904 |
|
---|
| 1905 | float maxt = 1e6;
|
---|
| 1906 | float mint = 0;
|
---|
| 1907 |
|
---|
| 1908 | Intersectable::NewMail();
|
---|
| 1909 |
|
---|
| 1910 | if (!mBox.GetMinMaxT(ray, &mint, &maxt))
|
---|
| 1911 | return 0;
|
---|
| 1912 |
|
---|
| 1913 | if (mint < 0)
|
---|
| 1914 | mint = 0;
|
---|
| 1915 |
|
---|
| 1916 | maxt += Limits::Threshold;
|
---|
| 1917 |
|
---|
| 1918 | Vector3 entp = ray.Extrap(mint);
|
---|
| 1919 | Vector3 extp = ray.Extrap(maxt);
|
---|
| 1920 |
|
---|
| 1921 | VspKdNode *node = mRoot;
|
---|
| 1922 | VspKdNode *farChild;
|
---|
| 1923 |
|
---|
| 1924 | float position;
|
---|
| 1925 | int axis;
|
---|
| 1926 |
|
---|
| 1927 | while (1)
|
---|
| 1928 | {
|
---|
| 1929 | if (!node->IsLeaf())
|
---|
| 1930 | {
|
---|
| 1931 | VspKdInterior *in = dynamic_cast<VspKdInterior *>(node);
|
---|
| 1932 | position = in->mPosition;
|
---|
| 1933 | axis = in->mAxis;
|
---|
| 1934 |
|
---|
| 1935 | if (entp[axis] <= position)
|
---|
| 1936 | {
|
---|
| 1937 | if (extp[axis] <= position)
|
---|
| 1938 | {
|
---|
| 1939 | node = in->mBack;
|
---|
| 1940 | // cases N1,N2,N3,P5,Z2,Z3
|
---|
| 1941 | continue;
|
---|
| 1942 | } else
|
---|
| 1943 | {
|
---|
| 1944 | // case N4
|
---|
| 1945 | node = in->mBack;
|
---|
| 1946 | farChild = in->mFront;
|
---|
| 1947 | }
|
---|
| 1948 | }
|
---|
| 1949 | else
|
---|
| 1950 | {
|
---|
| 1951 | if (position <= extp[axis])
|
---|
| 1952 | {
|
---|
| 1953 | node = in->mFront;
|
---|
| 1954 | // cases P1,P2,P3,N5,Z1
|
---|
| 1955 | continue;
|
---|
| 1956 | }
|
---|
| 1957 | else
|
---|
| 1958 | {
|
---|
| 1959 | node = in->mFront;
|
---|
| 1960 | farChild = in->mBack;
|
---|
| 1961 | // case P4
|
---|
| 1962 | }
|
---|
| 1963 | }
|
---|
| 1964 |
|
---|
| 1965 | // $$ modification 3.5.2004 - hints from Kamil Ghais
|
---|
| 1966 | // case N4 or P4
|
---|
| 1967 | float tdist = (position - ray.GetLoc(axis)) / ray.GetDir(axis);
|
---|
| 1968 | //tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
|
---|
| 1969 | extp = ray.GetLoc() + ray.GetDir()*tdist;
|
---|
| 1970 | maxt = tdist;
|
---|
| 1971 | }
|
---|
| 1972 | else
|
---|
| 1973 | {
|
---|
| 1974 | // compute intersection with all objects in this leaf
|
---|
| 1975 | /*KdLeaf *leaf = (KdLeaf *) node;
|
---|
| 1976 | if (ray.mFlags & Ray::STORE_KDLEAVES)
|
---|
| 1977 | ray.kdLeaves.push_back(leaf);
|
---|
| 1978 |
|
---|
| 1979 | ObjectContainer::const_iterator mi;
|
---|
| 1980 | for ( mi = leaf->mObjects.begin(); mi != leaf->mObjects.end(); mi++)
|
---|
| 1981 | {
|
---|
| 1982 | Intersectable *object = *mi;
|
---|
| 1983 | if (!object->Mailed() )
|
---|
| 1984 | {
|
---|
| 1985 | object->Mail();
|
---|
| 1986 | if (ray.mFlags & Ray::STORE_TESTED_OBJECTS)
|
---|
| 1987 | ray.testedObjects.push_back(object);
|
---|
| 1988 | hits += object->CastRay(ray);
|
---|
| 1989 | }
|
---|
| 1990 | }
|
---|
| 1991 |
|
---|
| 1992 | if (hits && ray.GetType() == Ray::LOCAL_RAY)
|
---|
| 1993 | if (ray.intersections[0].mT <= maxt)
|
---|
| 1994 | break;
|
---|
| 1995 |
|
---|
| 1996 | // get the next node from the stack
|
---|
| 1997 | if (tStack.empty())
|
---|
| 1998 | break;
|
---|
| 1999 |
|
---|
| 2000 | entp = extp;
|
---|
| 2001 | mint = maxt;
|
---|
| 2002 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
|
---|
| 2003 | break;
|
---|
| 2004 |
|
---|
| 2005 | RayTraversalData &s = tStack.top();
|
---|
| 2006 | node = s.mNode;
|
---|
| 2007 | extp = s.mExitPoint;
|
---|
| 2008 | maxt = s.mMaxT;
|
---|
| 2009 | tStack.pop();
|
---|
| 2010 | */
|
---|
| 2011 | }
|
---|
| 2012 | }
|
---|
| 2013 |
|
---|
| 2014 | return hits;
|
---|
| 2015 | }
|
---|
| 2016 |
|
---|
| 2017 |
|
---|
| 2018 | void VspKdTree::GenerateViewCell(VspKdLeaf *leaf)
|
---|
| 2019 | {
|
---|
| 2020 | RayInfoContainer::const_iterator it, it_end = leaf->GetRays().end();
|
---|
| 2021 |
|
---|
| 2022 | VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>(mViewCellsManager->GenerateViewCell());
|
---|
| 2023 | leaf->SetViewCell(vc);
|
---|
| 2024 |
|
---|
| 2025 | vc->SetSize(GetBBox(leaf).GetVolume());
|
---|
| 2026 | vc->mVspKdLeaves.push_back(leaf);
|
---|
| 2027 |
|
---|
| 2028 | for (it = leaf->GetRays().begin(); it != it_end; ++ it)
|
---|
| 2029 | {
|
---|
| 2030 | VssRay *ray = (*it).mRay;
|
---|
| 2031 |
|
---|
| 2032 | if (ray->mTerminationObject) |
---|
| 2033 | vc->GetPvs().AddSample(ray->mTerminationObject); |
---|
| 2034 | |
---|
| 2035 | if (ray->mOriginObject) |
---|
| 2036 | vc->GetPvs().AddSample(ray->mOriginObject);
|
---|
| 2037 | }
|
---|
| 2038 | }
|
---|
| 2039 |
|
---|
| 2040 |
|
---|
| 2041 | void VspKdTree::EvaluateViewCellsStats(ViewCellsStatistics &vcStat) const |
---|
| 2042 | { |
---|
| 2043 | vcStat.Reset(); |
---|
| 2044 | |
---|
| 2045 | stack<VspKdNode *> nodeStack; |
---|
| 2046 | nodeStack.push(mRoot); |
---|
| 2047 | |
---|
| 2048 | ViewCell::NewMail(); |
---|
| 2049 | |
---|
| 2050 | // exclude root cell |
---|
| 2051 | //mRootCell->Mail(); |
---|
| 2052 | |
---|
| 2053 | while (!nodeStack.empty()) |
---|
| 2054 | { |
---|
| 2055 | VspKdNode *node = nodeStack.top(); |
---|
| 2056 | nodeStack.pop(); |
---|
| 2057 | |
---|
| 2058 | if (node->IsLeaf()) |
---|
| 2059 | { |
---|
| 2060 | ++ vcStat.leaves; |
---|
| 2061 | |
---|
| 2062 | VspKdViewCell *viewCell = dynamic_cast<VspKdLeaf *>(node)->mViewCell; |
---|
| 2063 | |
---|
| 2064 | if (!viewCell->Mailed()) |
---|
| 2065 | { |
---|
| 2066 | viewCell->Mail(); |
---|
| 2067 | |
---|
| 2068 | ++ vcStat.viewCells; |
---|
| 2069 | const int pvsSize = viewCell->GetPvs().GetSize(); |
---|
| 2070 | |
---|
| 2071 | vcStat.pvs += pvsSize; |
---|
| 2072 | |
---|
| 2073 | if (pvsSize < 1) |
---|
| 2074 | ++ vcStat.emptyPvs; |
---|
| 2075 | |
---|
| 2076 | if (pvsSize > vcStat.maxPvs) |
---|
| 2077 | vcStat.maxPvs = pvsSize; |
---|
| 2078 | |
---|
| 2079 | if (pvsSize < vcStat.minPvs) |
---|
| 2080 | vcStat.minPvs = pvsSize; |
---|
| 2081 | |
---|
| 2082 | if ((int)viewCell->mVspKdLeaves.size() > vcStat.maxLeaves) |
---|
| 2083 | vcStat.maxLeaves = (int)viewCell->mVspKdLeaves.size(); |
---|
| 2084 | } |
---|
| 2085 | } |
---|
| 2086 | else |
---|
| 2087 | { |
---|
| 2088 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); |
---|
| 2089 | |
---|
| 2090 | nodeStack.push(interior->GetFront()); |
---|
| 2091 | nodeStack.push(interior->GetBack()); |
---|
| 2092 | } |
---|
| 2093 | } |
---|
| 2094 | }
|
---|
| 2095 |
|
---|
| 2096 |
|
---|
| 2097 | bool VspKdTree::MergeViewCells(VspKdLeaf *l1, VspKdLeaf *l2) |
---|
| 2098 | { |
---|
| 2099 | //-- change pointer to view cells of all leaves associated with the previous view cells |
---|
| 2100 | VspKdViewCell *fVc = l1->GetViewCell(); |
---|
| 2101 | VspKdViewCell *bVc = l2->GetViewCell(); |
---|
| 2102 | |
---|
| 2103 | VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>( |
---|
| 2104 | mViewCellsManager->MergeViewCells(*fVc, *bVc)); |
---|
| 2105 | |
---|
| 2106 | // if merge was unsuccessful |
---|
| 2107 | if (!vc) return false; |
---|
| 2108 | |
---|
| 2109 | // set new size of view cell |
---|
| 2110 | vc->SetSize(fVc->GetSize() + bVc->GetSize()); |
---|
| 2111 | |
---|
| 2112 | vector<VspKdLeaf *> fLeaves = fVc->mVspKdLeaves; |
---|
| 2113 | vector<VspKdLeaf *> bLeaves = bVc->mVspKdLeaves; |
---|
| 2114 | |
---|
| 2115 | vector<VspKdLeaf *>::const_iterator it; |
---|
| 2116 | |
---|
| 2117 | //-- change view cells of all the other leaves the view cell belongs to |
---|
| 2118 | for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) |
---|
| 2119 | { |
---|
| 2120 | (*it)->SetViewCell(vc); |
---|
| 2121 | vc->mVspKdLeaves.push_back(*it); |
---|
| 2122 | } |
---|
| 2123 | |
---|
| 2124 | for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) |
---|
| 2125 | { |
---|
| 2126 | (*it)->SetViewCell(vc); |
---|
| 2127 | vc->mVspKdLeaves.push_back(*it); |
---|
| 2128 | } |
---|
| 2129 | |
---|
| 2130 | // clean up old view cells |
---|
| 2131 | DEL_PTR(fVc); |
---|
| 2132 | DEL_PTR(bVc); |
---|
| 2133 | |
---|
| 2134 | return true; |
---|
| 2135 | }
|
---|
| 2136 |
|
---|
| 2137 |
|
---|
| 2138 |
|
---|
| 2139 | int VspKdTree::MergeLeaves()
|
---|
| 2140 | {
|
---|
| 2141 | vector<VspKdLeaf *> leaves;
|
---|
| 2142 | priority_queue<MergeCandidate> mergeQueue;
|
---|
| 2143 |
|
---|
| 2144 | // collect the leaves, e.g., the "voxels" that will build the view cells
|
---|
[452] | 2145 | CollectLeaves(leaves);
|
---|
| 2146 |
|
---|
[462] | 2147 | int vcSize = (int)leaves.size();
|
---|
[452] | 2148 |
|
---|
[462] | 2149 | VspKdLeaf::NewMail();
|
---|
[452] | 2150 |
|
---|
[462] | 2151 | vector<VspKdLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
[452] | 2152 |
|
---|
[462] | 2153 | // generate view cells
|
---|
[452] | 2154 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[462] | 2155 | GenerateViewCell(*it);
|
---|
| 2156 |
|
---|
| 2157 | // find merge candidates and push them into queue
|
---|
| 2158 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[452] | 2159 | {
|
---|
[462] | 2160 | VspKdLeaf *leaf = *it;
|
---|
| 2161 |
|
---|
| 2162 | // no leaf is part of two merge candidates
|
---|
[452] | 2163 | if (!leaf->Mailed())
|
---|
| 2164 | {
|
---|
[462] | 2165 | leaf->Mail();
|
---|
| 2166 |
|
---|
| 2167 | vector<VspKdLeaf *> neighbors;
|
---|
[452] | 2168 | FindNeighbors(leaf, neighbors, true);
|
---|
| 2169 |
|
---|
[462] | 2170 | vector<VspKdLeaf *>::const_iterator nit,
|
---|
| 2171 | nit_end = neighbors.end();
|
---|
[452] | 2172 |
|
---|
[462] | 2173 | for (nit = neighbors.begin(); nit != nit_end; ++ nit)
|
---|
| 2174 | mergeQueue.push(MergeCandidate(leaf, *nit));
|
---|
| 2175 | }
|
---|
| 2176 | }
|
---|
| 2177 |
|
---|
| 2178 | int merged = 0;
|
---|
| 2179 |
|
---|
| 2180 | float savedMaxCostRatio = 0;
|
---|
| 2181 |
|
---|
| 2182 | Debug << "merge cost " << mergeQueue.top().GetMergeCost() << " of " << mMaxCostRatio << endl;
|
---|
| 2183 |
|
---|
| 2184 | // use priority queue to merge leaves
|
---|
| 2185 | while (!mergeQueue.empty() &&
|
---|
| 2186 | ((mMaxViewCells < vcSize) ||
|
---|
| 2187 | (mergeQueue.top().GetMergeCost() < mMaxCostRatio)))
|
---|
| 2188 | {
|
---|
| 2189 | ViewCell::NewMail();
|
---|
| 2190 |
|
---|
| 2191 | Debug << "=====================" << endl;
|
---|
| 2192 | MergeCandidate mc = mergeQueue.top();
|
---|
| 2193 | mergeQueue.pop();
|
---|
| 2194 |
|
---|
| 2195 | if (mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell())
|
---|
| 2196 | // both view cells the same!
|
---|
| 2197 | {
|
---|
| 2198 | Debug << "same view cell!!" << endl;
|
---|
| 2199 | -- vcSize;
|
---|
| 2200 |
|
---|
| 2201 | continue;
|
---|
| 2202 | }
|
---|
| 2203 |
|
---|
| 2204 | if (mc.Valid())
|
---|
| 2205 | {
|
---|
| 2206 | mc.GetLeaf1()->GetViewCell()->Mail();
|
---|
| 2207 | mc.GetLeaf2()->GetViewCell()->Mail();
|
---|
[452] | 2208 |
|
---|
[462] | 2209 | Debug << "merging cells with cost " << mc.GetMergeCost() << " of " << mMaxCostRatio << endl;
|
---|
| 2210 | savedMaxCostRatio = mc.GetMergeCost();
|
---|
| 2211 |
|
---|
| 2212 | MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2());
|
---|
| 2213 |
|
---|
| 2214 | ++ merged;
|
---|
| 2215 | -- vcSize;
|
---|
[452] | 2216 | }
|
---|
[462] | 2217 | // merge candidate not valid, because one of the leaves was already
|
---|
| 2218 | // merged with another one
|
---|
| 2219 | else
|
---|
| 2220 | {
|
---|
| 2221 | Debug << "not valid " << mc.GetMergeCost() << endl;
|
---|
| 2222 |
|
---|
| 2223 | // validate and reinsert into queue
|
---|
| 2224 | mc.SetValid();
|
---|
| 2225 | Debug << "ag. valid " << mc.GetMergeCost() << endl;
|
---|
| 2226 |
|
---|
| 2227 | mergeQueue.push(mc);
|
---|
| 2228 | }
|
---|
[452] | 2229 | }
|
---|
[462] | 2230 |
|
---|
| 2231 | if (mergeQueue.empty())
|
---|
| 2232 | Debug << "empty queue" << endl;
|
---|
| 2233 |
|
---|
| 2234 | Debug << "max cost ratio: " << savedMaxCostRatio << endl;
|
---|
| 2235 |
|
---|
| 2236 | return merged;
|
---|
[452] | 2237 | }
|
---|
| 2238 |
|
---|
| 2239 |
|
---|
| 2240 | /*********************************************************************/
|
---|
| 2241 | /* MergeCandidate implementation */
|
---|
| 2242 | /*********************************************************************/
|
---|
| 2243 |
|
---|
| 2244 |
|
---|
[462] | 2245 | MergeCandidate::MergeCandidate(VspKdLeaf *l1, VspKdLeaf *l2):
|
---|
| 2246 | mMergeCost(0),
|
---|
| 2247 | mLeaf1(l1),
|
---|
| 2248 | mLeaf2(l2),
|
---|
| 2249 | mLeaf1Id(l1->GetViewCell()->mMailbox),
|
---|
| 2250 | mLeaf2Id(l2->GetViewCell()->mMailbox)
|
---|
| 2251 | {
|
---|
| 2252 | EvalMergeCost();
|
---|
| 2253 | }
|
---|
[452] | 2254 |
|
---|
[462] | 2255 |
|
---|
| 2256 | void MergeCandidate::EvalMergeCost()
|
---|
[452] | 2257 | {
|
---|
[462] | 2258 | // compute pvs differences
|
---|
| 2259 | VspKdViewCell *vc1 = mLeaf1->GetViewCell(); |
---|
| 2260 | VspKdViewCell *vc2 = mLeaf2->GetViewCell();
|
---|
| 2261 | |
---|
| 2262 | //VspKdViewCell *vc1 = dynamic_cast<VspKdViewCell *>(mLeaf1->GetViewCell()); |
---|
| 2263 | //VspKdViewCell *vc2 = dynamic_cast<VspKdViewCell *>(mLeaf2->GetViewCell());
|
---|
[452] | 2264 |
|
---|
[453] | 2265 | const int diff1 = vc1->GetPvs().Diff(vc2->GetPvs()); |
---|
[462] | 2266 | const int vcPvs = diff1 + vc1->GetPvs().GetSize();
|
---|
[453] | 2267 |
|
---|
[462] | 2268 | //-- compute ratio of old cost
|
---|
| 2269 | //-- (added size of left and right view cell times pvs size)
|
---|
| 2270 | //-- to new rendering cost (size of merged view cell times pvs size)
|
---|
[453] | 2271 | const float oldCost =
|
---|
[462] | 2272 | (float)vc1->GetPvs().GetSize() * vc1->GetSize() +
|
---|
| 2273 | (float)vc2->GetPvs().GetSize() * vc2->GetSize();
|
---|
[453] | 2274 |
|
---|
[462] | 2275 | const float newCost =
|
---|
| 2276 | (float)vcPvs * (vc1->GetSize() + vc2->GetSize());
|
---|
[453] | 2277 |
|
---|
[462] | 2278 | mMergeCost = newCost / oldCost;
|
---|
| 2279 |
|
---|
| 2280 | Debug << "******************************" << endl;
|
---|
| 2281 | Debug << "pvs size: " << vcPvs << " max: " << sMaxPvsSize << endl;
|
---|
| 2282 | if (vcPvs > sMaxPvsSize) // strong penalty if pvs size too large
|
---|
| 2283 | mMergeCost += 1.0;
|
---|
| 2284 |
|
---|
| 2285 | Debug << "old cost: " << oldCost << endl;
|
---|
| 2286 | Debug << "new cost: " << newCost << endl;
|
---|
| 2287 | }
|
---|
| 2288 |
|
---|
| 2289 | void MergeCandidate::SetLeaf1(VspKdLeaf *l)
|
---|
| 2290 | {
|
---|
| 2291 | mLeaf1 = l;
|
---|
| 2292 | }
|
---|
| 2293 |
|
---|
| 2294 | void MergeCandidate::SetLeaf2(VspKdLeaf *l)
|
---|
| 2295 | {
|
---|
| 2296 | mLeaf2 = l;
|
---|
| 2297 | }
|
---|
| 2298 |
|
---|
| 2299 | VspKdLeaf *MergeCandidate::GetLeaf1()
|
---|
| 2300 | {
|
---|
| 2301 | return mLeaf1;
|
---|
| 2302 | }
|
---|
| 2303 |
|
---|
| 2304 | VspKdLeaf *MergeCandidate::GetLeaf2()
|
---|
| 2305 | {
|
---|
| 2306 | return mLeaf2;
|
---|
| 2307 | }
|
---|
| 2308 |
|
---|
| 2309 | bool MergeCandidate::Valid() const
|
---|
| 2310 | {
|
---|
| 2311 | //Debug << mLeaf1->GetViewCell()->mMailbox << " " << mLeaf1Id << endl;
|
---|
| 2312 | //Debug << mLeaf2->GetViewCell()->mMailbox << " " << mLeaf2Id << endl;
|
---|
| 2313 |
|
---|
| 2314 | return
|
---|
| 2315 | (mLeaf1->GetViewCell()->mMailbox == mLeaf1Id) &&
|
---|
| 2316 | (mLeaf2->GetViewCell()->mMailbox == mLeaf2Id);
|
---|
| 2317 | }
|
---|
| 2318 |
|
---|
| 2319 | float MergeCandidate::GetMergeCost() const
|
---|
| 2320 | {
|
---|
| 2321 | return mMergeCost;
|
---|
| 2322 | }
|
---|
| 2323 |
|
---|
| 2324 | void MergeCandidate::SetValid()
|
---|
| 2325 | {
|
---|
| 2326 | mLeaf1Id = mLeaf1->GetViewCell()->mMailbox;
|
---|
| 2327 | mLeaf2Id = mLeaf2->GetViewCell()->mMailbox;
|
---|
| 2328 |
|
---|
| 2329 | EvalMergeCost();
|
---|
[419] | 2330 | } |
---|