[478] | 1 | #include <stack>
|
---|
| 2 | #include <time.h>
|
---|
| 3 | #include <iomanip>
|
---|
| 4 |
|
---|
[463] | 5 | #include "Plane3.h"
|
---|
| 6 | #include "VspBspTree.h"
|
---|
| 7 | #include "Mesh.h"
|
---|
| 8 | #include "common.h"
|
---|
| 9 | #include "ViewCell.h"
|
---|
| 10 | #include "Environment.h"
|
---|
| 11 | #include "Polygon3.h"
|
---|
| 12 | #include "Ray.h"
|
---|
| 13 | #include "AxisAlignedBox3.h"
|
---|
| 14 | #include "Exporter.h"
|
---|
| 15 | #include "Plane3.h"
|
---|
| 16 | #include "ViewCellBsp.h"
|
---|
[478] | 17 | #include "ViewCellsManager.h"
|
---|
[532] | 18 | #include "Beam.h"
|
---|
[463] | 19 |
|
---|
[639] | 20 | #define USE_FIXEDPOINT_T 0
|
---|
[590] | 21 |
|
---|
[639] | 22 |
|
---|
[463] | 23 | //-- static members
|
---|
[508] | 24 |
|
---|
[463] | 25 |
|
---|
[482] | 26 | int VspBspTree::sFrontId = 0;
|
---|
[463] | 27 | int VspBspTree::sBackId = 0;
|
---|
| 28 | int VspBspTree::sFrontAndBackId = 0;
|
---|
| 29 |
|
---|
| 30 |
|
---|
[557] | 31 |
|
---|
[579] | 32 | // pvs penalty can be different from pvs size
|
---|
| 33 | inline float EvalPvsPenalty(const int pvs,
|
---|
| 34 | const int lower,
|
---|
| 35 | const int upper)
|
---|
| 36 | {
|
---|
| 37 | // clamp to minmax values
|
---|
| 38 | if (pvs < lower)
|
---|
| 39 | return (float)lower;
|
---|
| 40 | if (pvs > upper)
|
---|
| 41 | return (float)upper;
|
---|
[578] | 42 |
|
---|
[579] | 43 | return (float)pvs;
|
---|
| 44 | }
|
---|
[463] | 45 |
|
---|
[557] | 46 |
|
---|
[579] | 47 |
|
---|
| 48 |
|
---|
[601] | 49 | /******************************************************************************/
|
---|
| 50 | /* class VspBspTree implementation */
|
---|
| 51 | /******************************************************************************/
|
---|
[579] | 52 |
|
---|
| 53 |
|
---|
[482] | 54 | VspBspTree::VspBspTree():
|
---|
[463] | 55 | mRoot(NULL),
|
---|
[547] | 56 | mUseAreaForPvs(false),
|
---|
[478] | 57 | mCostNormalizer(Limits::Small),
|
---|
| 58 | mViewCellsManager(NULL),
|
---|
[497] | 59 | mOutOfBoundsCell(NULL),
|
---|
[643] | 60 | mStoreRays(false),
|
---|
[605] | 61 | mRenderCostWeight(0.5),
|
---|
[610] | 62 | mUseRandomAxis(false),
|
---|
[650] | 63 | mTimeStamp(1)
|
---|
[463] | 64 | {
|
---|
[486] | 65 | bool randomize = false;
|
---|
| 66 | environment->GetBoolValue("VspBspTree.Construction.randomize", randomize);
|
---|
| 67 | if (randomize)
|
---|
| 68 | Randomize(); // initialise random generator for heuristics
|
---|
[463] | 69 |
|
---|
| 70 | //-- termination criteria for autopartition
|
---|
| 71 | environment->GetIntValue("VspBspTree.Termination.maxDepth", mTermMaxDepth);
|
---|
| 72 | environment->GetIntValue("VspBspTree.Termination.minPvs", mTermMinPvs);
|
---|
| 73 | environment->GetIntValue("VspBspTree.Termination.minRays", mTermMinRays);
|
---|
[547] | 74 | environment->GetFloatValue("VspBspTree.Termination.minProbability", mTermMinProbability);
|
---|
[463] | 75 | environment->GetFloatValue("VspBspTree.Termination.maxRayContribution", mTermMaxRayContribution);
|
---|
| 76 | environment->GetFloatValue("VspBspTree.Termination.minAccRayLenght", mTermMinAccRayLength);
|
---|
[472] | 77 | environment->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
| 78 | environment->GetIntValue("VspBspTree.Termination.missTolerance", mTermMissTolerance);
|
---|
[564] | 79 | environment->GetIntValue("VspBspTree.Termination.maxViewCells", mMaxViewCells);
|
---|
[666] | 80 |
|
---|
[564] | 81 | //-- max cost ratio for early tree termination
|
---|
| 82 | environment->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
[610] | 83 |
|
---|
[654] | 84 | environment->GetFloatValue("VspBspTree.Termination.minGlobalCostRatio", mTermMinGlobalCostRatio);
|
---|
| 85 | environment->GetIntValue("VspBspTree.Termination.globalCostMissTolerance", mTermGlobalCostMissTolerance);
|
---|
[610] | 86 |
|
---|
[654] | 87 | // HACK//mTermMinPolygons = 25;
|
---|
| 88 |
|
---|
[463] | 89 | //-- factors for bsp tree split plane heuristics
|
---|
| 90 | environment->GetFloatValue("VspBspTree.Factor.pvs", mPvsFactor);
|
---|
| 91 | environment->GetFloatValue("VspBspTree.Termination.ct_div_ci", mCtDivCi);
|
---|
| 92 |
|
---|
[482] | 93 |
|
---|
[463] | 94 | //-- partition criteria
|
---|
| 95 | environment->GetIntValue("VspBspTree.maxPolyCandidates", mMaxPolyCandidates);
|
---|
| 96 | environment->GetIntValue("VspBspTree.maxRayCandidates", mMaxRayCandidates);
|
---|
| 97 | environment->GetIntValue("VspBspTree.splitPlaneStrategy", mSplitPlaneStrategy);
|
---|
| 98 |
|
---|
| 99 | environment->GetFloatValue("VspBspTree.Construction.epsilon", mEpsilon);
|
---|
| 100 | environment->GetIntValue("VspBspTree.maxTests", mMaxTests);
|
---|
| 101 |
|
---|
[564] | 102 | // if only the driving axis is used for axis aligned split
|
---|
[551] | 103 | environment->GetBoolValue("VspBspTree.splitUseOnlyDrivingAxis", mOnlyDrivingAxis);
|
---|
[580] | 104 |
|
---|
[508] | 105 | //-- termination criteria for axis aligned split
|
---|
| 106 | environment->GetFloatValue("VspBspTree.Termination.AxisAligned.maxRayContribution",
|
---|
[665] | 107 | mTermMaxRayContriForAxisAligned);
|
---|
[508] | 108 | environment->GetIntValue("VspBspTree.Termination.AxisAligned.minRays",
|
---|
[665] | 109 | mTermMinRaysForAxisAligned);
|
---|
[487] | 110 |
|
---|
[508] | 111 | //environment->GetFloatValue("VspBspTree.maxTotalMemory", mMaxTotalMemory);
|
---|
| 112 | environment->GetFloatValue("VspBspTree.maxStaticMemory", mMaxMemory);
|
---|
| 113 |
|
---|
[580] | 114 | environment->GetFloatValue("VspBspTree.Construction.renderCostWeight", mRenderCostWeight);
|
---|
[607] | 115 | environment->GetBoolValue("VspBspTree.usePolygonSplitIfAvailable", mUsePolygonSplitIfAvailable);
|
---|
[547] | 116 |
|
---|
[612] | 117 | environment->GetBoolValue("VspBspTree.useCostHeuristics", mUseCostHeuristics);
|
---|
[655] | 118 | environment->GetBoolValue("VspBspTree.useSplitCostQueue", mUseSplitCostQueue);
|
---|
[663] | 119 | environment->GetBoolValue("VspBspTree.simulateOctree", mSimulateOctree);
|
---|
[664] | 120 | environment->GetBoolValue("VspBspTree.useRandomAxis", mUseRandomAxis);
|
---|
[666] | 121 | environment->GetBoolValue("VspBspTree.useBreathFirstSplits", mBreathFirstSplits);
|
---|
[580] | 122 |
|
---|
[676] | 123 | environment->GetBoolValue("ViewCells.PostProcess.emptyViewCellsMergeAllowed", mEmptyViewCellsMergeAllowed);
|
---|
| 124 |
|
---|
[660] | 125 | char subdivisionStatsLog[100];
|
---|
[666] | 126 | environment->GetStringValue("VspBspTree.subdivisionStats", subdivisionStatsLog);
|
---|
[660] | 127 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
[612] | 128 |
|
---|
[478] | 129 | //-- debug output
|
---|
[580] | 130 |
|
---|
[473] | 131 | Debug << "******* VSP BSP options ******** " << endl;
|
---|
| 132 | Debug << "max depth: " << mTermMaxDepth << endl;
|
---|
| 133 | Debug << "min PVS: " << mTermMinPvs << endl;
|
---|
[547] | 134 | Debug << "min probabiliy: " << mTermMinProbability << endl;
|
---|
[473] | 135 | Debug << "min rays: " << mTermMinRays << endl;
|
---|
| 136 | Debug << "max ray contri: " << mTermMaxRayContribution << endl;
|
---|
| 137 | Debug << "max cost ratio: " << mTermMaxCostRatio << endl;
|
---|
| 138 | Debug << "miss tolerance: " << mTermMissTolerance << endl;
|
---|
| 139 | Debug << "max view cells: " << mMaxViewCells << endl;
|
---|
| 140 | Debug << "max polygon candidates: " << mMaxPolyCandidates << endl;
|
---|
[663] | 141 | //Debug << "max plane candidates: " << mMaxRayCandidates << endl;
|
---|
[486] | 142 | Debug << "randomize: " << randomize << endl;
|
---|
[582] | 143 |
|
---|
[551] | 144 | Debug << "using area for pvs: " << mUseAreaForPvs << endl;
|
---|
[580] | 145 | Debug << "render cost weight: " << mRenderCostWeight << endl;
|
---|
[663] | 146 | Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
|
---|
| 147 | Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
|
---|
| 148 | Debug << "only driving axis: " << mOnlyDrivingAxis << endl;
|
---|
| 149 | Debug << "max memory: " << mMaxMemory << endl;
|
---|
| 150 | Debug << "use poly split if available: " << mUsePolygonSplitIfAvailable << endl;
|
---|
| 151 | Debug << "use cost heuristics: " << mUseCostHeuristics << endl;
|
---|
| 152 | Debug << "use split cost queue: " << mUseSplitCostQueue << endl;
|
---|
| 153 | Debug << "subdivision stats log: " << subdivisionStatsLog << endl;
|
---|
[664] | 154 | Debug << "use random axis: " << mUseRandomAxis << endl;
|
---|
| 155 | Debug << "breath first splits: " << mBreathFirstSplits << endl;
|
---|
[667] | 156 |
|
---|
[665] | 157 | Debug << "octree: " << mSimulateOctree << endl;
|
---|
[663] | 158 |
|
---|
[463] | 159 | Debug << "Split plane strategy: ";
|
---|
[564] | 160 |
|
---|
[463] | 161 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
[474] | 162 | {
|
---|
[463] | 163 | Debug << "random polygon ";
|
---|
[474] | 164 | }
|
---|
[463] | 165 | if (mSplitPlaneStrategy & AXIS_ALIGNED)
|
---|
[472] | 166 | {
|
---|
[463] | 167 | Debug << "axis aligned ";
|
---|
[472] | 168 | }
|
---|
[665] | 169 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
[472] | 170 | {
|
---|
[474] | 171 | mCostNormalizer += mLeastRaySplitsFactor;
|
---|
[463] | 172 | Debug << "least ray splits ";
|
---|
[472] | 173 | }
|
---|
[463] | 174 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
[472] | 175 | {
|
---|
[474] | 176 | mCostNormalizer += mBalancedRaysFactor;
|
---|
[463] | 177 | Debug << "balanced rays ";
|
---|
[472] | 178 | }
|
---|
[463] | 179 | if (mSplitPlaneStrategy & PVS)
|
---|
[472] | 180 | {
|
---|
[474] | 181 | mCostNormalizer += mPvsFactor;
|
---|
[463] | 182 | Debug << "pvs";
|
---|
[665] | 183 | }
|
---|
[482] | 184 |
|
---|
[489] | 185 |
|
---|
[480] | 186 | mSplitCandidates = new vector<SortableEntry>;
|
---|
[463] | 187 |
|
---|
| 188 | Debug << endl;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[580] | 191 |
|
---|
[508] | 192 | BspViewCell *VspBspTree::GetOutOfBoundsCell()
|
---|
| 193 | {
|
---|
| 194 | return mOutOfBoundsCell;
|
---|
| 195 | }
|
---|
[463] | 196 |
|
---|
[508] | 197 |
|
---|
[489] | 198 | BspViewCell *VspBspTree::GetOrCreateOutOfBoundsCell()
|
---|
| 199 | {
|
---|
| 200 | if (!mOutOfBoundsCell)
|
---|
[508] | 201 | {
|
---|
[489] | 202 | mOutOfBoundsCell = new BspViewCell();
|
---|
[508] | 203 | mOutOfBoundsCell->SetId(-1);
|
---|
[547] | 204 | mOutOfBoundsCell->SetValid(false);
|
---|
[508] | 205 | }
|
---|
[547] | 206 |
|
---|
[489] | 207 | return mOutOfBoundsCell;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 |
|
---|
[482] | 211 | const BspTreeStatistics &VspBspTree::GetStatistics() const
|
---|
[463] | 212 | {
|
---|
[574] | 213 | return mBspStats;
|
---|
[463] | 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | VspBspTree::~VspBspTree()
|
---|
| 218 | {
|
---|
| 219 | DEL_PTR(mRoot);
|
---|
[480] | 220 | DEL_PTR(mSplitCandidates);
|
---|
[463] | 221 | }
|
---|
| 222 |
|
---|
[579] | 223 |
|
---|
[482] | 224 | int VspBspTree::AddMeshToPolygons(Mesh *mesh,
|
---|
| 225 | PolygonContainer &polys,
|
---|
[463] | 226 | MeshInstance *parent)
|
---|
| 227 | {
|
---|
| 228 | FaceContainer::const_iterator fi;
|
---|
[482] | 229 |
|
---|
[463] | 230 | // copy the face data to polygons
|
---|
| 231 | for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi)
|
---|
| 232 | {
|
---|
| 233 | Polygon3 *poly = new Polygon3((*fi), mesh);
|
---|
[482] | 234 |
|
---|
[463] | 235 | if (poly->Valid(mEpsilon))
|
---|
| 236 | {
|
---|
| 237 | poly->mParent = parent; // set parent intersectable
|
---|
| 238 | polys.push_back(poly);
|
---|
| 239 | }
|
---|
| 240 | else
|
---|
| 241 | DEL_PTR(poly);
|
---|
| 242 | }
|
---|
| 243 | return (int)mesh->mFaces.size();
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[580] | 246 |
|
---|
[482] | 247 | int VspBspTree::AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
| 248 | PolygonContainer &polys,
|
---|
[463] | 249 | int maxObjects)
|
---|
| 250 | {
|
---|
[482] | 251 | int limit = (maxObjects > 0) ?
|
---|
[463] | 252 | Min((int)viewCells.size(), maxObjects) : (int)viewCells.size();
|
---|
[482] | 253 |
|
---|
[463] | 254 | int polysSize = 0;
|
---|
| 255 |
|
---|
| 256 | for (int i = 0; i < limit; ++ i)
|
---|
| 257 | {
|
---|
| 258 | if (viewCells[i]->GetMesh()) // copy the mesh data to polygons
|
---|
| 259 | {
|
---|
| 260 | mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb
|
---|
[482] | 261 | polysSize +=
|
---|
| 262 | AddMeshToPolygons(viewCells[i]->GetMesh(),
|
---|
| 263 | polys,
|
---|
[463] | 264 | viewCells[i]);
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | return polysSize;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[580] | 271 |
|
---|
[482] | 272 | int VspBspTree::AddToPolygonSoup(const ObjectContainer &objects,
|
---|
| 273 | PolygonContainer &polys,
|
---|
[463] | 274 | int maxObjects)
|
---|
| 275 | {
|
---|
[482] | 276 | int limit = (maxObjects > 0) ?
|
---|
[463] | 277 | Min((int)objects.size(), maxObjects) : (int)objects.size();
|
---|
[482] | 278 |
|
---|
[463] | 279 | for (int i = 0; i < limit; ++i)
|
---|
| 280 | {
|
---|
| 281 | Intersectable *object = objects[i];//*it;
|
---|
| 282 | Mesh *mesh = NULL;
|
---|
| 283 |
|
---|
| 284 | switch (object->Type()) // extract the meshes
|
---|
| 285 | {
|
---|
| 286 | case Intersectable::MESH_INSTANCE:
|
---|
| 287 | mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
|
---|
| 288 | break;
|
---|
| 289 | case Intersectable::VIEW_CELL:
|
---|
| 290 | mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
|
---|
| 291 | break;
|
---|
| 292 | // TODO: handle transformed mesh instances
|
---|
| 293 | default:
|
---|
| 294 | Debug << "intersectable type not supported" << endl;
|
---|
| 295 | break;
|
---|
| 296 | }
|
---|
[482] | 297 |
|
---|
[463] | 298 | if (mesh) // copy the mesh data to polygons
|
---|
| 299 | {
|
---|
| 300 | mBox.Include(object->GetBox()); // add to BSP tree aabb
|
---|
[485] | 301 | AddMeshToPolygons(mesh, polys, NULL);
|
---|
[463] | 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | return (int)polys.size();
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[580] | 308 |
|
---|
[483] | 309 | void VspBspTree::Construct(const VssRayContainer &sampleRays,
|
---|
| 310 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
[463] | 311 | {
|
---|
[574] | 312 | mBspStats.nodes = 1;
|
---|
[463] | 313 | mBox.Initialize(); // initialise BSP tree bounding box
|
---|
[482] | 314 |
|
---|
[484] | 315 | if (forcedBoundingBox)
|
---|
| 316 | mBox = *forcedBoundingBox;
|
---|
| 317 |
|
---|
[463] | 318 | PolygonContainer polys;
|
---|
| 319 | RayInfoContainer *rays = new RayInfoContainer();
|
---|
| 320 |
|
---|
| 321 | VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
| 322 |
|
---|
| 323 | long startTime = GetTime();
|
---|
| 324 |
|
---|
[480] | 325 | cout << "Extracting polygons from rays ... ";
|
---|
[463] | 326 |
|
---|
| 327 | Intersectable::NewMail();
|
---|
| 328 |
|
---|
[511] | 329 | int numObj = 0;
|
---|
[542] | 330 |
|
---|
[463] | 331 | //-- extract polygons intersected by the rays
|
---|
| 332 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
| 333 | {
|
---|
| 334 | VssRay *ray = *rit;
|
---|
[482] | 335 |
|
---|
[484] | 336 | if ((mBox.IsInside(ray->mTermination) || !forcedBoundingBox) &&
|
---|
| 337 | ray->mTerminationObject &&
|
---|
| 338 | !ray->mTerminationObject->Mailed())
|
---|
[463] | 339 | {
|
---|
| 340 | ray->mTerminationObject->Mail();
|
---|
| 341 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mTerminationObject);
|
---|
| 342 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
[511] | 343 | ++ numObj;
|
---|
[648] | 344 |
|
---|
[484] | 345 | //-- compute bounding box
|
---|
| 346 | if (!forcedBoundingBox)
|
---|
| 347 | mBox.Include(ray->mTermination);
|
---|
[463] | 348 | }
|
---|
| 349 |
|
---|
[484] | 350 | if ((mBox.IsInside(ray->mOrigin) || !forcedBoundingBox) &&
|
---|
| 351 | ray->mOriginObject &&
|
---|
| 352 | !ray->mOriginObject->Mailed())
|
---|
[463] | 353 | {
|
---|
| 354 | ray->mOriginObject->Mail();
|
---|
| 355 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mOriginObject);
|
---|
| 356 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
[511] | 357 | ++ numObj;
|
---|
| 358 |
|
---|
[484] | 359 | //-- compute bounding box
|
---|
| 360 | if (!forcedBoundingBox)
|
---|
| 361 | mBox.Include(ray->mOrigin);
|
---|
[463] | 362 | }
|
---|
| 363 | }
|
---|
[535] | 364 |
|
---|
[653] | 365 | Debug << "maximal pvs (i.e., pvs still considered as valid) : "
|
---|
| 366 | << mViewCellsManager->GetMaxPvsSize() << endl;
|
---|
[580] | 367 |
|
---|
[463] | 368 | //-- store rays
|
---|
| 369 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
| 370 | {
|
---|
| 371 | VssRay *ray = *rit;
|
---|
[482] | 372 |
|
---|
[463] | 373 | float minT, maxT;
|
---|
| 374 |
|
---|
[564] | 375 | static Ray hray;
|
---|
| 376 | hray.Init(*ray);
|
---|
[562] | 377 |
|
---|
[483] | 378 | // TODO: not very efficient to implictly cast between rays types
|
---|
[564] | 379 | if (mBox.GetRaySegment(hray, minT, maxT))
|
---|
[463] | 380 | {
|
---|
| 381 | float len = ray->Length();
|
---|
[482] | 382 |
|
---|
| 383 | if (!len)
|
---|
[463] | 384 | len = Limits::Small;
|
---|
[482] | 385 |
|
---|
[463] | 386 | rays->push_back(RayInfo(ray, minT / len, maxT / len));
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[587] | 390 | // normalize
|
---|
[547] | 391 | if (mUseAreaForPvs)
|
---|
[587] | 392 | mTermMinProbability *= mBox.SurfaceArea();
|
---|
[547] | 393 | else
|
---|
| 394 | mTermMinProbability *= mBox.GetVolume();
|
---|
| 395 |
|
---|
[648] | 396 | // throw out unnecessary polygons
|
---|
| 397 | PreprocessPolygons(polys);
|
---|
| 398 |
|
---|
[574] | 399 | mBspStats.polys = (int)polys.size();
|
---|
[654] | 400 | mGlobalCostMisses = 0;
|
---|
| 401 |
|
---|
[475] | 402 | cout << "finished" << endl;
|
---|
[463] | 403 |
|
---|
[482] | 404 | Debug << "\nPolygon extraction: " << (int)polys.size() << " polys extracted from "
|
---|
[475] | 405 | << (int)sampleRays.size() << " rays in "
|
---|
| 406 | << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl << endl;
|
---|
| 407 |
|
---|
[655] | 408 | // use split cost priority queue
|
---|
| 409 | if (mUseSplitCostQueue)
|
---|
| 410 | {
|
---|
[654] | 411 | ConstructWithSplitQueue(polys, rays);
|
---|
[655] | 412 | }
|
---|
[654] | 413 | else
|
---|
[655] | 414 | {
|
---|
[654] | 415 | Construct(polys, rays);
|
---|
[655] | 416 | }
|
---|
[463] | 417 |
|
---|
| 418 | // clean up polygons
|
---|
| 419 | CLEAR_CONTAINER(polys);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[508] | 422 |
|
---|
[612] | 423 | // TODO: return memory usage in MB
|
---|
[656] | 424 | float VspBspTree::GetMemUsage() const
|
---|
[508] | 425 | {
|
---|
[656] | 426 | return (float)
|
---|
| 427 | (sizeof(VspBspTree) +
|
---|
| 428 | mBspStats.Leaves() * sizeof(BspLeaf) +
|
---|
| 429 | mCreatedViewCells * sizeof(BspViewCell) +
|
---|
| 430 | mBspStats.pvs * sizeof(ObjectPvsData) +
|
---|
| 431 | mBspStats.Interior() * sizeof(BspInterior) +
|
---|
| 432 | mBspStats.accumRays * sizeof(RayInfo)) / (1024.0f * 1024.0f);
|
---|
[508] | 433 | }
|
---|
| 434 |
|
---|
| 435 |
|
---|
| 436 |
|
---|
[463] | 437 | void VspBspTree::Construct(const PolygonContainer &polys, RayInfoContainer *rays)
|
---|
| 438 | {
|
---|
[600] | 439 | VspBspTraversalQueue tQueue;
|
---|
[463] | 440 |
|
---|
| 441 | mRoot = new BspLeaf();
|
---|
| 442 |
|
---|
| 443 | // constrruct root node geometry
|
---|
| 444 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
| 445 | ConstructGeometry(mRoot, *geom);
|
---|
| 446 |
|
---|
[547] | 447 | const float prop = mUseAreaForPvs ? geom->GetArea() : geom->GetVolume();
|
---|
| 448 |
|
---|
[482] | 449 | VspBspTraversalData tData(mRoot,
|
---|
| 450 | new PolygonContainer(polys),
|
---|
[463] | 451 | 0,
|
---|
[482] | 452 | rays,
|
---|
| 453 | ComputePvsSize(*rays),
|
---|
[547] | 454 | prop,
|
---|
[463] | 455 | geom);
|
---|
[663] | 456 |
|
---|
[664] | 457 | EvalPriority(tData);
|
---|
| 458 |
|
---|
[663] | 459 |
|
---|
| 460 | if (mSimulateOctree)
|
---|
| 461 | tData.mAxis = 0;
|
---|
| 462 |
|
---|
[578] | 463 | // first node is kd node, i.e. an axis aligned box
|
---|
[678] | 464 | if (0)
|
---|
[578] | 465 | tData.mIsKdNode = true;
|
---|
| 466 | else
|
---|
| 467 | tData.mIsKdNode = false;
|
---|
[562] | 468 |
|
---|
[600] | 469 | tQueue.push(tData);
|
---|
[463] | 470 |
|
---|
[609] | 471 |
|
---|
[605] | 472 | mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume();
|
---|
[607] | 473 | mTotalPvsSize = tData.mPvs;
|
---|
| 474 |
|
---|
[601] | 475 | mSubdivisionStats
|
---|
[605] | 476 | << "#ViewCells\n1\n" << endl
|
---|
[601] | 477 | << "#RenderCostDecrease\n0\n" << endl
|
---|
[607] | 478 | << "#TotalRenderCost\n" << mTotalCost << endl
|
---|
| 479 | << "#AvgRenderCost\n" << mTotalPvsSize << endl;
|
---|
[601] | 480 |
|
---|
[600] | 481 | Debug << "total cost: " << mTotalCost << endl;
|
---|
| 482 |
|
---|
[601] | 483 |
|
---|
[574] | 484 | mBspStats.Start();
|
---|
[667] | 485 | cout << "Constructing vsp bsp tree ... \n";
|
---|
[463] | 486 |
|
---|
[542] | 487 | long startTime = GetTime();
|
---|
[664] | 488 | int nLeaves = 500;
|
---|
| 489 | int nViewCells = 500;
|
---|
[587] | 490 |
|
---|
| 491 | // used for intermediate time measurements and progress
|
---|
[542] | 492 | long interTime = GetTime();
|
---|
[547] | 493 |
|
---|
[542] | 494 | mOutOfMemory = false;
|
---|
[482] | 495 |
|
---|
[612] | 496 | mCreatedViewCells = 0;
|
---|
| 497 |
|
---|
[600] | 498 | while (!tQueue.empty())
|
---|
[463] | 499 | {
|
---|
[600] | 500 | tData = tQueue.top();
|
---|
| 501 | tQueue.pop();
|
---|
[463] | 502 |
|
---|
[508] | 503 | if (0 && !mOutOfMemory)
|
---|
| 504 | {
|
---|
| 505 | float mem = GetMemUsage();
|
---|
[478] | 506 |
|
---|
[508] | 507 | if (mem > mMaxMemory)
|
---|
| 508 | {
|
---|
| 509 | mOutOfMemory = true;
|
---|
| 510 | Debug << "memory limit reached: " << mem << endl;
|
---|
| 511 | }
|
---|
| 512 | }
|
---|
| 513 |
|
---|
[587] | 514 | // subdivide leaf node
|
---|
[600] | 515 | BspNode *r = Subdivide(tQueue, tData);
|
---|
[463] | 516 |
|
---|
| 517 | if (r == mRoot)
|
---|
[482] | 518 | Debug << "VSP BSP tree construction time spent at root: "
|
---|
[542] | 519 | << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 520 |
|
---|
[654] | 521 | if (mBspStats.Leaves() >= nLeaves)
|
---|
[542] | 522 | {
|
---|
[612] | 523 | nLeaves += 500;
|
---|
| 524 |
|
---|
[574] | 525 | cout << "leaves=" << mBspStats.Leaves() << endl;
|
---|
[542] | 526 | Debug << "needed "
|
---|
[587] | 527 | << TimeDiff(interTime, GetTime())*1e-3
|
---|
[612] | 528 | << " secs to create 500 view cells" << endl;
|
---|
[542] | 529 | interTime = GetTime();
|
---|
| 530 | }
|
---|
[612] | 531 |
|
---|
[664] | 532 | if (mCreatedViewCells >= nViewCells)
|
---|
[612] | 533 | {
|
---|
| 534 | nViewCells += 500;
|
---|
| 535 |
|
---|
| 536 | cout << "generated " << mCreatedViewCells << " viewcells" << endl;
|
---|
| 537 | }
|
---|
[463] | 538 | }
|
---|
| 539 |
|
---|
[542] | 540 | Debug << "Used Memory: " << GetMemUsage() << " MB" << endl << endl;
|
---|
[463] | 541 | cout << "finished\n";
|
---|
| 542 |
|
---|
[574] | 543 | mBspStats.Stop();
|
---|
[463] | 544 | }
|
---|
| 545 |
|
---|
[508] | 546 |
|
---|
[653] | 547 |
|
---|
[654] | 548 | void VspBspTree::ConstructWithSplitQueue(const PolygonContainer &polys,
|
---|
[653] | 549 | RayInfoContainer *rays)
|
---|
| 550 | {
|
---|
| 551 | VspBspSplitQueue tQueue;
|
---|
| 552 |
|
---|
| 553 | mRoot = new BspLeaf();
|
---|
| 554 |
|
---|
| 555 | // constrruct root node geometry
|
---|
| 556 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
| 557 | ConstructGeometry(mRoot, *geom);
|
---|
| 558 |
|
---|
| 559 | const float prop = mUseAreaForPvs ? geom->GetArea() : geom->GetVolume();
|
---|
| 560 |
|
---|
| 561 | VspBspTraversalData tData(mRoot,
|
---|
| 562 | new PolygonContainer(polys),
|
---|
| 563 | 0,
|
---|
| 564 | rays,
|
---|
| 565 | ComputePvsSize(*rays),
|
---|
| 566 | prop,
|
---|
| 567 | geom);
|
---|
| 568 |
|
---|
[664] | 569 |
|
---|
[663] | 570 |
|
---|
[660] | 571 | // compute first split candidate
|
---|
[653] | 572 | VspBspSplitCandidate splitCandidate;
|
---|
| 573 | EvalSplitCandidate(tData, splitCandidate);
|
---|
| 574 |
|
---|
| 575 | tQueue.push(splitCandidate);
|
---|
| 576 |
|
---|
| 577 | mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume();
|
---|
| 578 | mTotalPvsSize = tData.mPvs;
|
---|
| 579 |
|
---|
| 580 | mSubdivisionStats
|
---|
| 581 | << "#ViewCells\n1\n" << endl
|
---|
| 582 | << "#RenderCostDecrease\n0\n" << endl
|
---|
[654] | 583 | << "#dummy\n0\n" << endl
|
---|
[653] | 584 | << "#TotalRenderCost\n" << mTotalCost << endl
|
---|
| 585 | << "#AvgRenderCost\n" << mTotalPvsSize << endl;
|
---|
| 586 |
|
---|
| 587 | Debug << "total cost: " << mTotalCost << endl;
|
---|
| 588 |
|
---|
| 589 |
|
---|
| 590 | mBspStats.Start();
|
---|
[667] | 591 | cout << "Constructing vsp bsp tree ... \n";
|
---|
[653] | 592 |
|
---|
| 593 | long startTime = GetTime();
|
---|
[666] | 594 | int nLeaves = 500;
|
---|
| 595 | int nViewCells = 500;
|
---|
[653] | 596 |
|
---|
| 597 | // used for intermediate time measurements and progress
|
---|
| 598 | long interTime = GetTime();
|
---|
| 599 |
|
---|
| 600 | mOutOfMemory = false;
|
---|
| 601 |
|
---|
| 602 | mCreatedViewCells = 0;
|
---|
| 603 |
|
---|
| 604 | while (!tQueue.empty())
|
---|
| 605 | {
|
---|
| 606 | splitCandidate = tQueue.top();
|
---|
| 607 | tQueue.pop();
|
---|
| 608 |
|
---|
[654] | 609 | // cost ratio of cost decrease / totalCost
|
---|
| 610 | float costRatio = splitCandidate.GetCost() / mTotalCost;
|
---|
| 611 |
|
---|
[655] | 612 | //Debug << "cost ratio: " << costRatio << endl;
|
---|
[654] | 613 |
|
---|
| 614 | if (costRatio < mTermMinGlobalCostRatio)
|
---|
| 615 | ++ mGlobalCostMisses;
|
---|
[655] | 616 |
|
---|
[653] | 617 | if (0 && !mOutOfMemory)
|
---|
| 618 | {
|
---|
| 619 | float mem = GetMemUsage();
|
---|
| 620 |
|
---|
| 621 | if (mem > mMaxMemory)
|
---|
| 622 | {
|
---|
| 623 | mOutOfMemory = true;
|
---|
| 624 | Debug << "memory limit reached: " << mem << endl;
|
---|
| 625 | }
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | // subdivide leaf node
|
---|
| 629 | BspNode *r = Subdivide(tQueue, splitCandidate);
|
---|
| 630 |
|
---|
| 631 | if (r == mRoot)
|
---|
| 632 | Debug << "VSP BSP tree construction time spent at root: "
|
---|
| 633 | << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 634 |
|
---|
[654] | 635 | if (mBspStats.Leaves() >= nLeaves)
|
---|
[653] | 636 | {
|
---|
| 637 | nLeaves += 500;
|
---|
| 638 |
|
---|
| 639 | cout << "leaves=" << mBspStats.Leaves() << endl;
|
---|
| 640 | Debug << "needed "
|
---|
| 641 | << TimeDiff(interTime, GetTime())*1e-3
|
---|
| 642 | << " secs to create 500 view cells" << endl;
|
---|
| 643 | interTime = GetTime();
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | if (mCreatedViewCells == nViewCells)
|
---|
| 647 | {
|
---|
| 648 | nViewCells += 500;
|
---|
| 649 |
|
---|
| 650 | cout << "generated " << mCreatedViewCells << " viewcells" << endl;
|
---|
| 651 | }
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | Debug << "Used Memory: " << GetMemUsage() << " MB" << endl << endl;
|
---|
| 655 | cout << "finished\n";
|
---|
| 656 |
|
---|
| 657 | mBspStats.Stop();
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 |
|
---|
[654] | 661 | bool VspBspTree::LocalTerminationCriteriaMet(const VspBspTraversalData &data) const
|
---|
[463] | 662 | {
|
---|
[482] | 663 | return
|
---|
[463] | 664 | (((int)data.mRays->size() <= mTermMinRays) ||
|
---|
[473] | 665 | (data.mPvs <= mTermMinPvs) ||
|
---|
[547] | 666 | (data.mProbability <= mTermMinProbability) ||
|
---|
[535] | 667 | (data.GetAvgRayContribution() > mTermMaxRayContribution) ||
|
---|
[463] | 668 | (data.mDepth >= mTermMaxDepth));
|
---|
| 669 | }
|
---|
| 670 |
|
---|
[508] | 671 |
|
---|
[654] | 672 | bool VspBspTree::GlobalTerminationCriteriaMet(const VspBspTraversalData &data) const
|
---|
| 673 | {
|
---|
| 674 | return
|
---|
| 675 | (mOutOfMemory
|
---|
| 676 | || (mBspStats.Leaves() >= mMaxViewCells)
|
---|
| 677 | || (mGlobalCostMisses >= mTermGlobalCostMissTolerance)
|
---|
| 678 | );
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 |
|
---|
| 682 |
|
---|
[600] | 683 | BspNode *VspBspTree::Subdivide(VspBspTraversalQueue &tQueue,
|
---|
[463] | 684 | VspBspTraversalData &tData)
|
---|
| 685 | {
|
---|
[473] | 686 | BspNode *newNode = tData.mNode;
|
---|
| 687 |
|
---|
[654] | 688 | if (!LocalTerminationCriteriaMet(tData) && !GlobalTerminationCriteriaMet(tData))
|
---|
[473] | 689 | {
|
---|
| 690 | PolygonContainer coincident;
|
---|
[482] | 691 |
|
---|
[473] | 692 | VspBspTraversalData tFrontData;
|
---|
| 693 | VspBspTraversalData tBackData;
|
---|
[612] | 694 |
|
---|
[663] | 695 | if (mSimulateOctree)
|
---|
| 696 | { // choose axes in circular motion
|
---|
| 697 | tFrontData.mAxis = (tData.mAxis + 1) % 3;
|
---|
| 698 | tBackData.mAxis = (tData.mAxis + 1) % 3;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
[473] | 701 | // create new interior node and two leaf nodes
|
---|
| 702 | // or return leaf as it is (if maxCostRatio missed)
|
---|
[653] | 703 | int splitAxis;
|
---|
| 704 | bool splitFurther = true;
|
---|
| 705 | int maxCostMisses = tData.mMaxCostMisses;
|
---|
| 706 |
|
---|
| 707 | Plane3 splitPlane;
|
---|
| 708 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
| 709 |
|
---|
| 710 | if (!SelectPlane(splitPlane, leaf, tData, tFrontData, tBackData, splitAxis))
|
---|
[473] | 711 | {
|
---|
[653] | 712 | ++ maxCostMisses;
|
---|
| 713 |
|
---|
| 714 | if (maxCostMisses > mTermMissTolerance)
|
---|
| 715 | {
|
---|
| 716 | // terminate branch because of max cost
|
---|
| 717 | ++ mBspStats.maxCostNodes;
|
---|
| 718 | splitFurther = false;
|
---|
| 719 | }
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 | if (splitFurther) //-- continue subdivision
|
---|
| 723 | {
|
---|
| 724 | newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData, coincident);
|
---|
| 725 |
|
---|
| 726 | if (splitAxis < 3)
|
---|
| 727 | ++ mBspStats.splits[splitAxis];
|
---|
| 728 | else
|
---|
| 729 | ++ mBspStats.polySplits;
|
---|
| 730 |
|
---|
| 731 | tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && splitAxis < 3);
|
---|
[663] | 732 |
|
---|
[653] | 733 | // how often was max cost ratio missed in this branch?
|
---|
| 734 | tFrontData.mMaxCostMisses = maxCostMisses;
|
---|
| 735 | tBackData.mMaxCostMisses = maxCostMisses;
|
---|
| 736 |
|
---|
[664] | 737 | EvalPriority(tFrontData);
|
---|
| 738 | EvalPriority(tBackData);
|
---|
[663] | 739 |
|
---|
[600] | 740 | if (1)
|
---|
| 741 | {
|
---|
[675] | 742 |
|
---|
[611] | 743 | float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
|
---|
| 744 | float cBack = (float)tBackData.mPvs * tBackData.mProbability;
|
---|
| 745 | float cData = (float)tData.mPvs * tData.mProbability;;
|
---|
[605] | 746 |
|
---|
[670] | 747 | float costDecr = (cFront + cBack - cData) / mBox.GetVolume();
|
---|
[607] | 748 |
|
---|
[600] | 749 | mTotalCost += costDecr;
|
---|
[607] | 750 | mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
|
---|
[600] | 751 |
|
---|
| 752 | mSubdivisionStats
|
---|
[605] | 753 | << "#ViewCells\n" << mBspStats.Leaves() << endl
|
---|
[600] | 754 | << "#RenderCostDecrease\n" << -costDecr << endl
|
---|
[607] | 755 | << "#TotalRenderCost\n" << mTotalCost << endl
|
---|
[650] | 756 | << "#AvgRenderCost\n" << (float)mTotalPvsSize / (float)mBspStats.Leaves() << endl;
|
---|
[600] | 757 | }
|
---|
| 758 |
|
---|
[473] | 759 | // push the children on the stack
|
---|
[600] | 760 | tQueue.push(tFrontData);
|
---|
| 761 | tQueue.push(tBackData);
|
---|
[473] | 762 |
|
---|
| 763 | // delete old leaf node
|
---|
[482] | 764 | DEL_PTR(tData.mNode);
|
---|
[473] | 765 | }
|
---|
| 766 | }
|
---|
[482] | 767 |
|
---|
[478] | 768 | //-- terminate traversal and create new view cell
|
---|
[473] | 769 | if (newNode->IsLeaf())
|
---|
[463] | 770 | {
|
---|
[473] | 771 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode);
|
---|
[547] | 772 | BspViewCell *viewCell = new BspViewCell();
|
---|
[489] | 773 |
|
---|
[463] | 774 | leaf->SetViewCell(viewCell);
|
---|
[487] | 775 |
|
---|
| 776 | //-- update pvs
|
---|
[556] | 777 | int conSamp = 0;
|
---|
| 778 | float sampCon = 0.0f;
|
---|
| 779 | AddToPvs(leaf, *tData.mRays, sampCon, conSamp);
|
---|
[487] | 780 |
|
---|
[574] | 781 | mBspStats.contributingSamples += conSamp;
|
---|
| 782 | mBspStats.sampleContributions +=(int) sampCon;
|
---|
[487] | 783 |
|
---|
| 784 | //-- store additional info
|
---|
[478] | 785 | if (mStoreRays)
|
---|
| 786 | {
|
---|
| 787 | RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
|
---|
| 788 | for (it = tData.mRays->begin(); it != it_end; ++ it)
|
---|
[639] | 789 | {
|
---|
| 790 | (*it).mRay->Ref();
|
---|
[478] | 791 | leaf->mVssRays.push_back((*it).mRay);
|
---|
[639] | 792 | }
|
---|
[478] | 793 | }
|
---|
[612] | 794 |
|
---|
[564] | 795 | // should I check here?
|
---|
[562] | 796 | if (0 && !mViewCellsManager->CheckValidity(viewCell, 0, mViewCellsManager->GetMaxPvsSize()))
|
---|
[547] | 797 | {
|
---|
| 798 | viewCell->SetValid(false);
|
---|
| 799 | leaf->SetTreeValid(false);
|
---|
| 800 | PropagateUpValidity(leaf);
|
---|
[463] | 801 |
|
---|
[574] | 802 | ++ mBspStats.invalidLeaves;
|
---|
[547] | 803 | }
|
---|
| 804 |
|
---|
[580] | 805 | viewCell->mLeaf = leaf;
|
---|
[547] | 806 |
|
---|
| 807 | if (mUseAreaForPvs)
|
---|
| 808 | viewCell->SetArea(tData.mProbability);
|
---|
| 809 | else
|
---|
| 810 | viewCell->SetVolume(tData.mProbability);
|
---|
| 811 |
|
---|
| 812 | leaf->mProbability = tData.mProbability;
|
---|
| 813 |
|
---|
[487] | 814 | EvaluateLeafStats(tData);
|
---|
[463] | 815 | }
|
---|
[482] | 816 |
|
---|
[473] | 817 | //-- cleanup
|
---|
[478] | 818 | tData.Clear();
|
---|
[463] | 819 |
|
---|
[472] | 820 | return newNode;
|
---|
[463] | 821 | }
|
---|
| 822 |
|
---|
[487] | 823 |
|
---|
[653] | 824 | BspNode *VspBspTree::Subdivide(VspBspSplitQueue &tQueue,
|
---|
| 825 | VspBspSplitCandidate &splitCandidate)
|
---|
| 826 | {
|
---|
| 827 | VspBspTraversalData &tData = splitCandidate.mParentData;
|
---|
| 828 |
|
---|
| 829 | BspNode *newNode = tData.mNode;
|
---|
| 830 |
|
---|
[654] | 831 | if (!LocalTerminationCriteriaMet(tData) && !GlobalTerminationCriteriaMet(tData))
|
---|
[666] | 832 | {
|
---|
[653] | 833 | PolygonContainer coincident;
|
---|
| 834 |
|
---|
| 835 | VspBspTraversalData tFrontData;
|
---|
| 836 | VspBspTraversalData tBackData;
|
---|
| 837 |
|
---|
| 838 | //-- continue subdivision
|
---|
| 839 | // create new interior node and two leaf node
|
---|
| 840 | const Plane3 splitPlane = splitCandidate.mSplitPlane;
|
---|
[654] | 841 |
|
---|
[653] | 842 | newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData, coincident);
|
---|
[666] | 843 |
|
---|
[660] | 844 | const int splitAxis = splitCandidate.mSplitAxis;
|
---|
| 845 | const int maxCostMisses = splitCandidate.mMaxCostMisses;
|
---|
| 846 |
|
---|
[654] | 847 | if (splitAxis < 3)
|
---|
| 848 | ++ mBspStats.splits[splitAxis];
|
---|
| 849 | else
|
---|
| 850 | ++ mBspStats.polySplits;
|
---|
[653] | 851 |
|
---|
[654] | 852 | tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && splitAxis < 3);
|
---|
[660] | 853 |
|
---|
[654] | 854 | // how often was max cost ratio missed in this branch?
|
---|
| 855 | tFrontData.mMaxCostMisses = maxCostMisses;
|
---|
| 856 | tBackData.mMaxCostMisses = maxCostMisses;
|
---|
[664] | 857 |
|
---|
[666] | 858 |
|
---|
[653] | 859 | if (1)
|
---|
| 860 | {
|
---|
| 861 | float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
|
---|
| 862 | float cBack = (float)tBackData.mPvs * tBackData.mProbability;
|
---|
[675] | 863 | float cData = (float)tData.mPvs * tData.mProbability;
|
---|
[653] | 864 |
|
---|
[675] | 865 |
|
---|
[653] | 866 | float costDecr =
|
---|
| 867 | (cFront + cBack - cData) / mBox.GetVolume();
|
---|
| 868 |
|
---|
| 869 | mTotalCost += costDecr;
|
---|
| 870 | mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
|
---|
| 871 |
|
---|
| 872 | mSubdivisionStats
|
---|
| 873 | << "#ViewCells\n" << mBspStats.Leaves() << endl
|
---|
[654] | 874 | << "#RenderCostDecrease\n" << -costDecr << endl
|
---|
| 875 | << "#dummy\n" << splitCandidate.GetCost() << endl
|
---|
[653] | 876 | << "#TotalRenderCost\n" << mTotalCost << endl
|
---|
| 877 | << "#AvgRenderCost\n" << (float)mTotalPvsSize / (float)mBspStats.Leaves() << endl;
|
---|
| 878 | }
|
---|
| 879 |
|
---|
[666] | 880 |
|
---|
[653] | 881 | //-- push the new split candidates on the stack
|
---|
| 882 | VspBspSplitCandidate frontCandidate;
|
---|
| 883 | VspBspSplitCandidate backCandidate;
|
---|
| 884 |
|
---|
[654] | 885 | EvalSplitCandidate(tFrontData, frontCandidate);
|
---|
| 886 | EvalSplitCandidate(tBackData, backCandidate);
|
---|
| 887 |
|
---|
[653] | 888 | tQueue.push(frontCandidate);
|
---|
| 889 | tQueue.push(backCandidate);
|
---|
[666] | 890 |
|
---|
[653] | 891 | // delete old leaf node
|
---|
| 892 | DEL_PTR(tData.mNode);
|
---|
| 893 | }
|
---|
| 894 |
|
---|
[654] | 895 |
|
---|
[653] | 896 | //-- terminate traversal and create new view cell
|
---|
| 897 | if (newNode->IsLeaf())
|
---|
| 898 | {
|
---|
| 899 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode);
|
---|
| 900 | BspViewCell *viewCell = new BspViewCell();
|
---|
| 901 |
|
---|
| 902 | leaf->SetViewCell(viewCell);
|
---|
| 903 |
|
---|
| 904 | //-- update pvs
|
---|
| 905 | int conSamp = 0;
|
---|
| 906 | float sampCon = 0.0f;
|
---|
| 907 | AddToPvs(leaf, *tData.mRays, sampCon, conSamp);
|
---|
| 908 |
|
---|
| 909 | mBspStats.contributingSamples += conSamp;
|
---|
| 910 | mBspStats.sampleContributions +=(int) sampCon;
|
---|
| 911 |
|
---|
| 912 | //-- store additional info
|
---|
| 913 | if (mStoreRays)
|
---|
| 914 | {
|
---|
| 915 | RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
|
---|
| 916 | for (it = tData.mRays->begin(); it != it_end; ++ it)
|
---|
| 917 | {
|
---|
| 918 | (*it).mRay->Ref();
|
---|
| 919 | leaf->mVssRays.push_back((*it).mRay);
|
---|
| 920 | }
|
---|
| 921 | }
|
---|
| 922 |
|
---|
| 923 | // should I check here?
|
---|
| 924 | viewCell->mLeaf = leaf;
|
---|
| 925 |
|
---|
| 926 | if (mUseAreaForPvs)
|
---|
| 927 | viewCell->SetArea(tData.mProbability);
|
---|
| 928 | else
|
---|
| 929 | viewCell->SetVolume(tData.mProbability);
|
---|
| 930 |
|
---|
[675] | 931 | leaf->mProbability = tData.mProbability;
|
---|
[653] | 932 |
|
---|
| 933 | EvaluateLeafStats(tData);
|
---|
| 934 | }
|
---|
| 935 |
|
---|
| 936 | //-- cleanup
|
---|
| 937 | tData.Clear();
|
---|
| 938 |
|
---|
| 939 | return newNode;
|
---|
| 940 | }
|
---|
| 941 |
|
---|
| 942 |
|
---|
[664] | 943 | void VspBspTree::EvalPriority(VspBspTraversalData &tData) const
|
---|
| 944 | {
|
---|
| 945 | tData.mPriority = mBreathFirstSplits ?
|
---|
| 946 | (float)-tData.mDepth : tData.mPvs * tData.mProbability;
|
---|
[670] | 947 | //cout << "priority: " << tData.mPriority << endl;
|
---|
[664] | 948 | }
|
---|
| 949 |
|
---|
| 950 |
|
---|
[652] | 951 | void VspBspTree::EvalSplitCandidate(VspBspTraversalData &tData,
|
---|
| 952 | VspBspSplitCandidate &splitData)
|
---|
| 953 | {
|
---|
| 954 | VspBspTraversalData frontData;
|
---|
| 955 | VspBspTraversalData backData;
|
---|
| 956 |
|
---|
| 957 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
[654] | 958 |
|
---|
| 959 | // compute locally best split plane
|
---|
[663] | 960 | bool success = SelectPlane(splitData.mSplitPlane, leaf, tData,
|
---|
[666] | 961 | frontData, backData, splitData.mSplitAxis);
|
---|
[652] | 962 |
|
---|
| 963 | // TODO: reuse
|
---|
| 964 | DEL_PTR(frontData.mGeometry);
|
---|
| 965 | DEL_PTR(backData.mGeometry);
|
---|
| 966 |
|
---|
[654] | 967 | // compute global decrease in render cost
|
---|
[652] | 968 | splitData.mRenderCost = EvalRenderCostDecrease(splitData.mSplitPlane, tData);
|
---|
| 969 | splitData.mParentData = tData;
|
---|
[660] | 970 | splitData.mMaxCostMisses = success ? tData.mMaxCostMisses : tData.mMaxCostMisses + 1;
|
---|
[652] | 971 | }
|
---|
| 972 |
|
---|
| 973 |
|
---|
[653] | 974 | BspInterior *VspBspTree::SubdivideNode(const Plane3 &splitPlane,
|
---|
| 975 | VspBspTraversalData &tData,
|
---|
| 976 | VspBspTraversalData &frontData,
|
---|
| 977 | VspBspTraversalData &backData,
|
---|
| 978 | PolygonContainer &coincident)
|
---|
[463] | 979 | {
|
---|
| 980 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
[508] | 981 |
|
---|
[473] | 982 | //-- the front and back traversal data is filled with the new values
|
---|
| 983 | frontData.mDepth = tData.mDepth + 1;
|
---|
[508] | 984 | frontData.mPolygons = new PolygonContainer();
|
---|
[473] | 985 | frontData.mRays = new RayInfoContainer();
|
---|
[508] | 986 |
|
---|
[473] | 987 | backData.mDepth = tData.mDepth + 1;
|
---|
[508] | 988 | backData.mPolygons = new PolygonContainer();
|
---|
[473] | 989 | backData.mRays = new RayInfoContainer();
|
---|
[508] | 990 |
|
---|
[653] | 991 |
|
---|
| 992 | //-- subdivide rays
|
---|
[652] | 993 | SplitRays(splitPlane,
|
---|
[482] | 994 | *tData.mRays,
|
---|
| 995 | *frontData.mRays,
|
---|
[463] | 996 | *backData.mRays);
|
---|
[482] | 997 |
|
---|
[463] | 998 |
|
---|
[472] | 999 | // compute pvs
|
---|
[463] | 1000 | frontData.mPvs = ComputePvsSize(*frontData.mRays);
|
---|
| 1001 | backData.mPvs = ComputePvsSize(*backData.mRays);
|
---|
| 1002 |
|
---|
[508] | 1003 | // split front and back node geometry and compute area
|
---|
[547] | 1004 |
|
---|
| 1005 | // if geometry was not already computed
|
---|
[602] | 1006 | if (!frontData.mGeometry && !backData.mGeometry)
|
---|
[463] | 1007 | {
|
---|
[547] | 1008 | frontData.mGeometry = new BspNodeGeometry();
|
---|
| 1009 | backData.mGeometry = new BspNodeGeometry();
|
---|
[482] | 1010 |
|
---|
[547] | 1011 | tData.mGeometry->SplitGeometry(*frontData.mGeometry,
|
---|
| 1012 | *backData.mGeometry,
|
---|
[652] | 1013 | splitPlane,
|
---|
[547] | 1014 | mBox,
|
---|
[676] | 1015 | 0.000000001f);
|
---|
[675] | 1016 | //mEpsilon);
|
---|
[508] | 1017 |
|
---|
[547] | 1018 | if (mUseAreaForPvs)
|
---|
| 1019 | {
|
---|
| 1020 | frontData.mProbability = frontData.mGeometry->GetArea();
|
---|
| 1021 | backData.mProbability = backData.mGeometry->GetArea();
|
---|
[508] | 1022 | }
|
---|
[547] | 1023 | else
|
---|
| 1024 | {
|
---|
| 1025 | frontData.mProbability = frontData.mGeometry->GetVolume();
|
---|
[654] | 1026 | backData.mProbability = tData.mProbability - frontData.mProbability;
|
---|
[676] | 1027 |
|
---|
| 1028 | if (frontData.mProbability < -0.00001)
|
---|
| 1029 | Debug << "here2 error: " << frontData.mProbability << endl;
|
---|
| 1030 | if (backData.mProbability < -0.00001)
|
---|
| 1031 | Debug << "here2 error: " << backData.mProbability << endl;
|
---|
| 1032 |
|
---|
[675] | 1033 | // clamp because of precision issues
|
---|
[676] | 1034 | if (0)
|
---|
| 1035 | {
|
---|
| 1036 | if (frontData.mProbability < 0) frontData.mProbability = 0;
|
---|
| 1037 | if (backData.mProbability < 0) backData.mProbability = 0;
|
---|
| 1038 | }
|
---|
[547] | 1039 | }
|
---|
[463] | 1040 | }
|
---|
[663] | 1041 |
|
---|
[547] | 1042 |
|
---|
[652] | 1043 | // subdivide polygons
|
---|
| 1044 | SplitPolygons(splitPlane,
|
---|
| 1045 | *tData.mPolygons,
|
---|
| 1046 | *frontData.mPolygons,
|
---|
| 1047 | *backData.mPolygons,
|
---|
| 1048 | coincident);
|
---|
[463] | 1049 |
|
---|
[652] | 1050 |
|
---|
| 1051 |
|
---|
[653] | 1052 | ///////////////////////////////////////
|
---|
| 1053 | // subdivide further
|
---|
[652] | 1054 |
|
---|
| 1055 | mBspStats.nodes += 2;
|
---|
| 1056 |
|
---|
| 1057 |
|
---|
| 1058 | BspInterior *interior = new BspInterior(splitPlane);
|
---|
| 1059 |
|
---|
| 1060 | #ifdef _DEBUG
|
---|
| 1061 | Debug << interior << endl;
|
---|
| 1062 | #endif
|
---|
| 1063 |
|
---|
[463] | 1064 | //-- create front and back leaf
|
---|
| 1065 |
|
---|
| 1066 | BspInterior *parent = leaf->GetParent();
|
---|
| 1067 |
|
---|
| 1068 | // replace a link from node's parent
|
---|
[487] | 1069 | if (parent)
|
---|
[463] | 1070 | {
|
---|
| 1071 | parent->ReplaceChildLink(leaf, interior);
|
---|
| 1072 | interior->SetParent(parent);
|
---|
| 1073 | }
|
---|
| 1074 | else // new root
|
---|
| 1075 | {
|
---|
| 1076 | mRoot = interior;
|
---|
| 1077 | }
|
---|
| 1078 |
|
---|
| 1079 | // and setup child links
|
---|
| 1080 | interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior));
|
---|
[482] | 1081 |
|
---|
[463] | 1082 | frontData.mNode = interior->GetFront();
|
---|
| 1083 | backData.mNode = interior->GetBack();
|
---|
[473] | 1084 |
|
---|
[650] | 1085 | interior->mTimeStamp = mTimeStamp ++;
|
---|
[652] | 1086 |
|
---|
[463] | 1087 | //DEL_PTR(leaf);
|
---|
| 1088 | return interior;
|
---|
| 1089 | }
|
---|
| 1090 |
|
---|
[508] | 1091 |
|
---|
[463] | 1092 | void VspBspTree::AddToPvs(BspLeaf *leaf,
|
---|
[482] | 1093 | const RayInfoContainer &rays,
|
---|
[556] | 1094 | float &sampleContributions,
|
---|
[463] | 1095 | int &contributingSamples)
|
---|
| 1096 | {
|
---|
[556] | 1097 | sampleContributions = 0;
|
---|
| 1098 | contributingSamples = 0;
|
---|
| 1099 |
|
---|
| 1100 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
| 1101 |
|
---|
| 1102 | ViewCell *vc = leaf->GetViewCell();
|
---|
| 1103 |
|
---|
| 1104 | // add contributions from samples to the PVS
|
---|
| 1105 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
[463] | 1106 | {
|
---|
[556] | 1107 | float sc = 0.0f;
|
---|
| 1108 | VssRay *ray = (*it).mRay;
|
---|
| 1109 | bool madeContrib = false;
|
---|
| 1110 | float contribution;
|
---|
| 1111 | if (ray->mTerminationObject) {
|
---|
| 1112 | if (vc->GetPvs().AddSample(ray->mTerminationObject, ray->mPdf, contribution))
|
---|
| 1113 | madeContrib = true;
|
---|
| 1114 | sc += contribution;
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
| 1117 | if (ray->mOriginObject) {
|
---|
| 1118 | if (vc->GetPvs().AddSample(ray->mOriginObject, ray->mPdf, contribution))
|
---|
| 1119 | madeContrib = true;
|
---|
| 1120 | sc += contribution;
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
| 1123 | sampleContributions += sc;
|
---|
| 1124 | if (madeContrib)
|
---|
| 1125 | ++ contributingSamples;
|
---|
| 1126 |
|
---|
| 1127 | //leaf->mVssRays.push_back(ray);
|
---|
[463] | 1128 | }
|
---|
| 1129 | }
|
---|
| 1130 |
|
---|
[580] | 1131 |
|
---|
[480] | 1132 | void VspBspTree::SortSplitCandidates(const RayInfoContainer &rays, const int axis)
|
---|
[463] | 1133 | {
|
---|
[480] | 1134 | mSplitCandidates->clear();
|
---|
[463] | 1135 |
|
---|
[480] | 1136 | int requestedSize = 2 * (int)(rays.size());
|
---|
| 1137 | // creates a sorted split candidates array
|
---|
| 1138 | if (mSplitCandidates->capacity() > 500000 &&
|
---|
[612] | 1139 | requestedSize < (int)(mSplitCandidates->capacity() / 10) )
|
---|
[480] | 1140 | {
|
---|
[482] | 1141 | delete mSplitCandidates;
|
---|
[480] | 1142 | mSplitCandidates = new vector<SortableEntry>;
|
---|
| 1143 | }
|
---|
[463] | 1144 |
|
---|
[480] | 1145 | mSplitCandidates->reserve(requestedSize);
|
---|
| 1146 |
|
---|
| 1147 | // insert all queries
|
---|
[612] | 1148 | for (RayInfoContainer::const_iterator ri = rays.begin(); ri < rays.end(); ++ ri)
|
---|
[473] | 1149 | {
|
---|
[612] | 1150 | const bool positive = (*ri).mRay->HasPosDir(axis);
|
---|
[480] | 1151 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax,
|
---|
[482] | 1152 | (*ri).ExtrapOrigin(axis), (*ri).mRay));
|
---|
[480] | 1153 |
|
---|
| 1154 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,
|
---|
[482] | 1155 | (*ri).ExtrapTermination(axis), (*ri).mRay));
|
---|
[473] | 1156 | }
|
---|
[480] | 1157 |
|
---|
| 1158 | stable_sort(mSplitCandidates->begin(), mSplitCandidates->end());
|
---|
[463] | 1159 | }
|
---|
| 1160 |
|
---|
[580] | 1161 |
|
---|
[480] | 1162 | float VspBspTree::BestCostRatioHeuristics(const RayInfoContainer &rays,
|
---|
| 1163 | const AxisAlignedBox3 &box,
|
---|
| 1164 | const int pvsSize,
|
---|
[481] | 1165 | const int &axis,
|
---|
[480] | 1166 | float &position)
|
---|
[463] | 1167 | {
|
---|
[480] | 1168 | SortSplitCandidates(rays, axis);
|
---|
| 1169 |
|
---|
[463] | 1170 | // go through the lists, count the number of objects left and right
|
---|
| 1171 | // and evaluate the following cost funcion:
|
---|
[480] | 1172 | // C = ct_div_ci + (ql*rl + qr*rr)/queries
|
---|
| 1173 |
|
---|
[612] | 1174 | int pvsl = 0, pvsr = pvsSize;
|
---|
[480] | 1175 |
|
---|
[612] | 1176 | int pvsBack = pvsl;
|
---|
| 1177 | int pvsFront = pvsr;
|
---|
| 1178 |
|
---|
[463] | 1179 | float minBox = box.Min(axis);
|
---|
| 1180 | float maxBox = box.Max(axis);
|
---|
[480] | 1181 | float sizeBox = maxBox - minBox;
|
---|
| 1182 |
|
---|
[612] | 1183 | float minBand = minBox + 0.1f * (maxBox - minBox);
|
---|
| 1184 | float maxBand = minBox + 0.9f * (maxBox - minBox);
|
---|
[480] | 1185 |
|
---|
[612] | 1186 | float sum = (float)pvsSize * sizeBox;
|
---|
[463] | 1187 | float minSum = 1e20f;
|
---|
| 1188 |
|
---|
[480] | 1189 | Intersectable::NewMail();
|
---|
| 1190 |
|
---|
[612] | 1191 | RayInfoContainer::const_iterator ri, ri_end = rays.end();
|
---|
| 1192 |
|
---|
| 1193 | // set all object as belonging to the front pvs
|
---|
| 1194 | for(ri = rays.begin(); ri != ri_end; ++ ri)
|
---|
[463] | 1195 | {
|
---|
[612] | 1196 | Intersectable *oObject = (*ri).mRay->mOriginObject;
|
---|
| 1197 | Intersectable *tObject = (*ri).mRay->mTerminationObject;
|
---|
| 1198 |
|
---|
| 1199 | if (oObject)
|
---|
[463] | 1200 | {
|
---|
[612] | 1201 | if (!oObject->Mailed())
|
---|
[482] | 1202 | {
|
---|
[612] | 1203 | oObject->Mail();
|
---|
| 1204 | oObject->mCounter = 1;
|
---|
[482] | 1205 | }
|
---|
[612] | 1206 | else
|
---|
| 1207 | {
|
---|
| 1208 | ++ oObject->mCounter;
|
---|
| 1209 | }
|
---|
[463] | 1210 | }
|
---|
[612] | 1211 | if (tObject)
|
---|
| 1212 | {
|
---|
| 1213 | if (!tObject->Mailed())
|
---|
| 1214 | {
|
---|
| 1215 | tObject->Mail();
|
---|
| 1216 | tObject->mCounter = 1;
|
---|
| 1217 | }
|
---|
| 1218 | else
|
---|
| 1219 | {
|
---|
| 1220 | ++ tObject->mCounter;
|
---|
| 1221 | }
|
---|
| 1222 | }
|
---|
[480] | 1223 | }
|
---|
| 1224 |
|
---|
| 1225 | Intersectable::NewMail();
|
---|
| 1226 |
|
---|
[612] | 1227 | vector<SortableEntry>::const_iterator ci, ci_end = mSplitCandidates->end();
|
---|
| 1228 |
|
---|
| 1229 | for (ci = mSplitCandidates->begin(); ci < ci_end; ++ ci)
|
---|
[480] | 1230 | {
|
---|
| 1231 | VssRay *ray;
|
---|
[612] | 1232 | ray = (*ci).ray;
|
---|
| 1233 |
|
---|
| 1234 | Intersectable *oObject = ray->mOriginObject;
|
---|
| 1235 | Intersectable *tObject = ray->mTerminationObject;
|
---|
| 1236 |
|
---|
[480] | 1237 |
|
---|
| 1238 | switch ((*ci).type)
|
---|
[463] | 1239 | {
|
---|
[480] | 1240 | case SortableEntry::ERayMin:
|
---|
| 1241 | {
|
---|
[612] | 1242 | if (oObject && !oObject->Mailed())
|
---|
[480] | 1243 | {
|
---|
[612] | 1244 | oObject->Mail();
|
---|
| 1245 | ++ pvsl;
|
---|
[480] | 1246 | }
|
---|
[612] | 1247 | if (tObject && !tObject->Mailed())
|
---|
| 1248 | {
|
---|
| 1249 | tObject->Mail();
|
---|
| 1250 | ++ pvsl;
|
---|
| 1251 | }
|
---|
[480] | 1252 | break;
|
---|
| 1253 | }
|
---|
| 1254 | case SortableEntry::ERayMax:
|
---|
| 1255 | {
|
---|
[612] | 1256 | if (oObject)
|
---|
| 1257 | {
|
---|
| 1258 | if (-- oObject->mCounter == 0)
|
---|
| 1259 | -- pvsr;
|
---|
| 1260 | }
|
---|
[463] | 1261 |
|
---|
[612] | 1262 | if (tObject)
|
---|
[480] | 1263 | {
|
---|
[612] | 1264 | if (-- tObject->mCounter == 0)
|
---|
| 1265 | -- pvsr;
|
---|
[480] | 1266 | }
|
---|
| 1267 |
|
---|
| 1268 | break;
|
---|
| 1269 | }
|
---|
| 1270 | }
|
---|
| 1271 |
|
---|
| 1272 | // Note: sufficient to compare size of bounding boxes of front and back side?
|
---|
| 1273 | if ((*ci).value > minBand && (*ci).value < maxBand)
|
---|
| 1274 | {
|
---|
[612] | 1275 | sum = pvsl * ((*ci).value - minBox) + pvsr * (maxBox - (*ci).value);
|
---|
[480] | 1276 |
|
---|
| 1277 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
| 1278 | // cout<<"cost= "<<sum<<endl;
|
---|
| 1279 |
|
---|
| 1280 | if (sum < minSum)
|
---|
[463] | 1281 | {
|
---|
| 1282 | minSum = sum;
|
---|
| 1283 | position = (*ci).value;
|
---|
| 1284 |
|
---|
[612] | 1285 | pvsBack = pvsl;
|
---|
| 1286 | pvsFront = pvsr;
|
---|
[463] | 1287 | }
|
---|
| 1288 | }
|
---|
| 1289 | }
|
---|
| 1290 |
|
---|
[612] | 1291 | // -- compute cost
|
---|
[463] | 1292 |
|
---|
[612] | 1293 | const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
|
---|
| 1294 | const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
|
---|
| 1295 |
|
---|
| 1296 | const float pOverall = sizeBox;
|
---|
| 1297 |
|
---|
| 1298 | const float pBack = position - minBox;
|
---|
| 1299 | const float pFront = maxBox - position;
|
---|
| 1300 |
|
---|
| 1301 | const float penaltyOld = EvalPvsPenalty(pvsSize, lowerPvsLimit, upperPvsLimit);
|
---|
| 1302 | const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
|
---|
| 1303 | const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
|
---|
| 1304 |
|
---|
| 1305 | const float oldRenderCost = penaltyOld * pOverall;
|
---|
| 1306 | const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
|
---|
| 1307 |
|
---|
| 1308 | float ratio = mPvsFactor * newRenderCost / (oldRenderCost + Limits::Small);
|
---|
| 1309 |
|
---|
[480] | 1310 | //Debug << "costRatio=" << ratio << " pos=" << position << " t=" << (position - minBox) / (maxBox - minBox)
|
---|
| 1311 | // <<"\t q=(" << queriesBack << "," << queriesFront << ")\t r=(" << raysBack << "," << raysFront << ")" << endl;
|
---|
| 1312 |
|
---|
| 1313 | return ratio;
|
---|
[463] | 1314 | }
|
---|
| 1315 |
|
---|
[480] | 1316 |
|
---|
[482] | 1317 | float VspBspTree::SelectAxisAlignedPlane(Plane3 &plane,
|
---|
[491] | 1318 | const VspBspTraversalData &tData,
|
---|
[495] | 1319 | int &axis,
|
---|
[508] | 1320 | BspNodeGeometry **frontGeom,
|
---|
| 1321 | BspNodeGeometry **backGeom,
|
---|
[547] | 1322 | float &pFront,
|
---|
| 1323 | float &pBack,
|
---|
| 1324 | const bool useKdSplit)
|
---|
[463] | 1325 | {
|
---|
[508] | 1326 | float nPosition[3];
|
---|
| 1327 | float nCostRatio[3];
|
---|
[547] | 1328 | float nProbFront[3];
|
---|
| 1329 | float nProbBack[3];
|
---|
[508] | 1330 |
|
---|
| 1331 | BspNodeGeometry *nFrontGeom[3];
|
---|
| 1332 | BspNodeGeometry *nBackGeom[3];
|
---|
| 1333 |
|
---|
[612] | 1334 | for (int i = 0; i < 3; ++ i)
|
---|
| 1335 | {
|
---|
| 1336 | nFrontGeom[i] = NULL;
|
---|
| 1337 | nBackGeom[i] = NULL;
|
---|
| 1338 | }
|
---|
| 1339 |
|
---|
[508] | 1340 | int bestAxis = -1;
|
---|
| 1341 |
|
---|
[545] | 1342 | // create bounding box of node geometry
|
---|
[463] | 1343 | AxisAlignedBox3 box;
|
---|
| 1344 | box.Initialize();
|
---|
[547] | 1345 |
|
---|
[562] | 1346 | //TODO: for kd split geometry already is box => only take minmax vertices
|
---|
[551] | 1347 | if (1)
|
---|
[509] | 1348 | {
|
---|
[678] | 1349 | PolygonContainer::const_iterator it, it_end = tData.mGeometry->GetPolys().end();
|
---|
[480] | 1350 |
|
---|
[678] | 1351 | for(it = tData.mGeometry->GetPolys().begin(); it < it_end; ++ it)
|
---|
[509] | 1352 | box.Include(*(*it));
|
---|
| 1353 | }
|
---|
| 1354 | else
|
---|
| 1355 | {
|
---|
| 1356 | RayInfoContainer::const_iterator ri, ri_end = tData.mRays->end();
|
---|
[480] | 1357 |
|
---|
[509] | 1358 | for(ri = tData.mRays->begin(); ri < ri_end; ++ ri)
|
---|
| 1359 | box.Include((*ri).ExtrapTermination());
|
---|
| 1360 | }
|
---|
[663] | 1361 |
|
---|
| 1362 | int sAxis = 0;
|
---|
| 1363 |
|
---|
[664] | 1364 | bool useSpecialAxis = mOnlyDrivingAxis || mUseRandomAxis || mSimulateOctree;
|
---|
[663] | 1365 |
|
---|
[664] | 1366 | // use some kind of specialised fixed axis
|
---|
| 1367 | if (mOnlyDrivingAxis)
|
---|
| 1368 | sAxis = box.Size().DrivingAxis();
|
---|
[663] | 1369 | else if (mUseRandomAxis)
|
---|
| 1370 | sAxis = Random(3);
|
---|
[664] | 1371 | else if (mSimulateOctree)
|
---|
| 1372 | sAxis = tData.mAxis;
|
---|
| 1373 |
|
---|
[670] | 1374 | //Debug << "use special axis: " << useSpecialAxis << endl;
|
---|
| 1375 | //Debug << "axis: " << sAxis << " drivingaxis: " << box.Size().DrivingAxis();
|
---|
[663] | 1376 |
|
---|
[495] | 1377 | for (axis = 0; axis < 3; ++ axis)
|
---|
[480] | 1378 | {
|
---|
[663] | 1379 | if (!useSpecialAxis || (axis == sAxis))
|
---|
[463] | 1380 | {
|
---|
[612] | 1381 | if (!mUseCostHeuristics)
|
---|
[480] | 1382 | {
|
---|
[612] | 1383 | nFrontGeom[axis] = new BspNodeGeometry();
|
---|
| 1384 | nBackGeom[axis] = new BspNodeGeometry();
|
---|
| 1385 |
|
---|
[480] | 1386 | nPosition[axis] = (box.Min()[axis] + box.Max()[axis]) * 0.5f;
|
---|
[543] | 1387 | Vector3 normal(0,0,0); normal[axis] = 1.0f;
|
---|
[547] | 1388 |
|
---|
| 1389 | // allows faster split because we have axis aligned kd tree boxes
|
---|
[542] | 1390 | if (useKdSplit)
|
---|
| 1391 | {
|
---|
| 1392 | nCostRatio[axis] = EvalAxisAlignedSplitCost(tData,
|
---|
| 1393 | box,
|
---|
| 1394 | axis,
|
---|
[547] | 1395 | nPosition[axis],
|
---|
| 1396 | nProbFront[axis],
|
---|
| 1397 | nProbBack[axis]);
|
---|
[542] | 1398 |
|
---|
[543] | 1399 | Vector3 pos;
|
---|
[542] | 1400 |
|
---|
[543] | 1401 | pos = box.Max(); pos[axis] = nPosition[axis];
|
---|
[547] | 1402 | AxisAlignedBox3 bBox(box.Min(), pos);
|
---|
[678] | 1403 | // TODO
|
---|
| 1404 | #if 0
|
---|
| 1405 | bBox.ExtractPolys(nBackGeom[axis]->GetPolys());
|
---|
| 1406 | #endif
|
---|
[542] | 1407 | pos = box.Min(); pos[axis] = nPosition[axis];
|
---|
[547] | 1408 | AxisAlignedBox3 fBox(pos, box.Max());
|
---|
[678] | 1409 | // TODO
|
---|
| 1410 | #if 0
|
---|
| 1411 | fBox.ExtractPolys(nFrontGeom[axis]->GetPolys());
|
---|
| 1412 | #endif
|
---|
[542] | 1413 | }
|
---|
| 1414 | else
|
---|
| 1415 | {
|
---|
[573] | 1416 | nCostRatio[axis] =
|
---|
| 1417 | EvalSplitPlaneCost(Plane3(normal, nPosition[axis]),
|
---|
| 1418 | tData, *nFrontGeom[axis], *nBackGeom[axis],
|
---|
| 1419 | nProbFront[axis], nProbBack[axis]);
|
---|
[542] | 1420 | }
|
---|
[480] | 1421 | }
|
---|
[481] | 1422 | else
|
---|
| 1423 | {
|
---|
[482] | 1424 | nCostRatio[axis] =
|
---|
[481] | 1425 | BestCostRatioHeuristics(*tData.mRays,
|
---|
| 1426 | box,
|
---|
| 1427 | tData.mPvs,
|
---|
| 1428 | axis,
|
---|
| 1429 | nPosition[axis]);
|
---|
| 1430 | }
|
---|
| 1431 |
|
---|
[480] | 1432 | if (bestAxis == -1)
|
---|
| 1433 | {
|
---|
| 1434 | bestAxis = axis;
|
---|
| 1435 | }
|
---|
| 1436 | else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
| 1437 | {
|
---|
| 1438 | bestAxis = axis;
|
---|
| 1439 | }
|
---|
| 1440 |
|
---|
[463] | 1441 | }
|
---|
| 1442 | }
|
---|
| 1443 |
|
---|
[495] | 1444 | //-- assign values
|
---|
| 1445 | axis = bestAxis;
|
---|
[547] | 1446 | pFront = nProbFront[bestAxis];
|
---|
| 1447 | pBack = nProbBack[bestAxis];
|
---|
[495] | 1448 |
|
---|
[508] | 1449 | // assign best split nodes geometry
|
---|
| 1450 | *frontGeom = nFrontGeom[bestAxis];
|
---|
| 1451 | *backGeom = nBackGeom[bestAxis];
|
---|
[542] | 1452 |
|
---|
[508] | 1453 | // and delete other geometry
|
---|
[612] | 1454 | DEL_PTR(nFrontGeom[(bestAxis + 1) % 3]);
|
---|
| 1455 | DEL_PTR(nBackGeom[(bestAxis + 2) % 3]);
|
---|
[495] | 1456 |
|
---|
[508] | 1457 | //-- split plane
|
---|
| 1458 | Vector3 normal(0,0,0); normal[bestAxis] = 1;
|
---|
[480] | 1459 | plane = Plane3(normal, nPosition[bestAxis]);
|
---|
[508] | 1460 |
|
---|
[480] | 1461 | return nCostRatio[bestAxis];
|
---|
[463] | 1462 | }
|
---|
| 1463 |
|
---|
[480] | 1464 |
|
---|
[508] | 1465 | bool VspBspTree::SelectPlane(Plane3 &bestPlane,
|
---|
| 1466 | BspLeaf *leaf,
|
---|
[652] | 1467 | VspBspTraversalData &data,
|
---|
[508] | 1468 | VspBspTraversalData &frontData,
|
---|
[612] | 1469 | VspBspTraversalData &backData,
|
---|
| 1470 | int &splitAxis)
|
---|
[491] | 1471 | {
|
---|
[508] | 1472 | // simplest strategy: just take next polygon
|
---|
| 1473 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
[491] | 1474 | {
|
---|
[508] | 1475 | if (!data.mPolygons->empty())
|
---|
| 1476 | {
|
---|
| 1477 | const int randIdx =
|
---|
| 1478 | (int)RandomValue(0, (Real)((int)data.mPolygons->size() - 1));
|
---|
| 1479 | Polygon3 *nextPoly = (*data.mPolygons)[randIdx];
|
---|
[491] | 1480 |
|
---|
[508] | 1481 | bestPlane = nextPoly->GetSupportingPlane();
|
---|
| 1482 | return true;
|
---|
| 1483 | }
|
---|
[491] | 1484 | }
|
---|
| 1485 |
|
---|
[508] | 1486 | //-- use heuristics to find appropriate plane
|
---|
[491] | 1487 |
|
---|
[508] | 1488 | // intermediate plane
|
---|
| 1489 | Plane3 plane;
|
---|
| 1490 | float lowestCost = MAX_FLOAT;
|
---|
[517] | 1491 |
|
---|
| 1492 | // decides if the first few splits should be only axisAligned
|
---|
[508] | 1493 | const bool onlyAxisAligned =
|
---|
| 1494 | (mSplitPlaneStrategy & AXIS_ALIGNED) &&
|
---|
| 1495 | ((int)data.mRays->size() > mTermMinRaysForAxisAligned) &&
|
---|
| 1496 | ((int)data.GetAvgRayContribution() < mTermMaxRayContriForAxisAligned);
|
---|
| 1497 |
|
---|
| 1498 | const int limit = onlyAxisAligned ? 0 :
|
---|
| 1499 | Min((int)data.mPolygons->size(), mMaxPolyCandidates);
|
---|
[491] | 1500 |
|
---|
[508] | 1501 | float candidateCost;
|
---|
[491] | 1502 |
|
---|
[508] | 1503 | int maxIdx = (int)data.mPolygons->size();
|
---|
[491] | 1504 |
|
---|
[508] | 1505 | for (int i = 0; i < limit; ++ i)
|
---|
[491] | 1506 | {
|
---|
[562] | 1507 | // the already taken candidates are stored behind maxIdx
|
---|
| 1508 | // => assure that no index is taken twice
|
---|
[508] | 1509 | const int candidateIdx = (int)RandomValue(0, (Real)(-- maxIdx));
|
---|
| 1510 | Polygon3 *poly = (*data.mPolygons)[candidateIdx];
|
---|
[491] | 1511 |
|
---|
[508] | 1512 | // swap candidate to the end to avoid testing same plane
|
---|
| 1513 | std::swap((*data.mPolygons)[maxIdx], (*data.mPolygons)[candidateIdx]);
|
---|
| 1514 | //Polygon3 *poly = (*data.mPolygons)[(int)RandomValue(0, (int)polys.size() - 1)];
|
---|
[491] | 1515 |
|
---|
[508] | 1516 | // evaluate current candidate
|
---|
| 1517 | BspNodeGeometry fGeom, bGeom;
|
---|
| 1518 | float fArea, bArea;
|
---|
| 1519 | plane = poly->GetSupportingPlane();
|
---|
[573] | 1520 | candidateCost = EvalSplitPlaneCost(plane, data, fGeom, bGeom, fArea, bArea);
|
---|
[491] | 1521 |
|
---|
[508] | 1522 | if (candidateCost < lowestCost)
|
---|
[491] | 1523 | {
|
---|
[508] | 1524 | bestPlane = plane;
|
---|
| 1525 | lowestCost = candidateCost;
|
---|
[491] | 1526 | }
|
---|
| 1527 | }
|
---|
| 1528 |
|
---|
[508] | 1529 | //-- evaluate axis aligned splits
|
---|
| 1530 | int axis;
|
---|
| 1531 | BspNodeGeometry *fGeom, *bGeom;
|
---|
[547] | 1532 | float pFront, pBack;
|
---|
[491] | 1533 |
|
---|
[653] | 1534 | candidateCost = 99999999.0f;
|
---|
[491] | 1535 |
|
---|
[653] | 1536 | // option: axis aligned split only if no polygon available
|
---|
| 1537 | if (!mUsePolygonSplitIfAvailable || data.mPolygons->empty())
|
---|
| 1538 | {
|
---|
| 1539 | candidateCost = SelectAxisAlignedPlane(plane,
|
---|
| 1540 | data,
|
---|
| 1541 | axis,
|
---|
| 1542 | &fGeom,
|
---|
| 1543 | &bGeom,
|
---|
| 1544 | pFront,
|
---|
| 1545 | pBack,
|
---|
| 1546 | data.mIsKdNode);
|
---|
| 1547 | }
|
---|
| 1548 |
|
---|
[612] | 1549 | splitAxis = 3;
|
---|
[562] | 1550 |
|
---|
[508] | 1551 | if (candidateCost < lowestCost)
|
---|
| 1552 | {
|
---|
| 1553 | bestPlane = plane;
|
---|
| 1554 | lowestCost = candidateCost;
|
---|
[612] | 1555 | splitAxis = axis;
|
---|
[653] | 1556 |
|
---|
[542] | 1557 | // assign already computed values
|
---|
| 1558 | // we can do this because we always save the
|
---|
[562] | 1559 | // computed values from the axis aligned splits
|
---|
[653] | 1560 |
|
---|
[612] | 1561 | if (fGeom && bGeom)
|
---|
| 1562 | {
|
---|
| 1563 | frontData.mGeometry = fGeom;
|
---|
| 1564 | backData.mGeometry = bGeom;
|
---|
[547] | 1565 |
|
---|
[612] | 1566 | frontData.mProbability = pFront;
|
---|
| 1567 | backData.mProbability = pBack;
|
---|
| 1568 | }
|
---|
[508] | 1569 | }
|
---|
| 1570 | else
|
---|
[463] | 1571 | {
|
---|
[508] | 1572 | DEL_PTR(fGeom);
|
---|
| 1573 | DEL_PTR(bGeom);
|
---|
[463] | 1574 | }
|
---|
[678] | 1575 |
|
---|
| 1576 |
|
---|
| 1577 | if (lowestCost > 10)
|
---|
| 1578 | Debug << "warning!! lowest cost: " << lowestCost << endl;
|
---|
[653] | 1579 |
|
---|
[678] | 1580 | //#ifdef _DEBUG
|
---|
[508] | 1581 | Debug << "plane lowest cost: " << lowestCost << endl;
|
---|
[678] | 1582 | //#endif
|
---|
[508] | 1583 |
|
---|
| 1584 | if (lowestCost > mTermMaxCostRatio)
|
---|
[611] | 1585 | {
|
---|
[508] | 1586 | return false;
|
---|
[611] | 1587 | }
|
---|
[508] | 1588 |
|
---|
| 1589 | return true;
|
---|
[463] | 1590 | }
|
---|
| 1591 |
|
---|
[480] | 1592 |
|
---|
[473] | 1593 | Plane3 VspBspTree::ChooseCandidatePlane(const RayInfoContainer &rays) const
|
---|
[482] | 1594 | {
|
---|
[473] | 1595 | const int candidateIdx = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
[482] | 1596 |
|
---|
[473] | 1597 | const Vector3 minPt = rays[candidateIdx].ExtrapOrigin();
|
---|
| 1598 | const Vector3 maxPt = rays[candidateIdx].ExtrapTermination();
|
---|
| 1599 |
|
---|
| 1600 | const Vector3 pt = (maxPt + minPt) * 0.5;
|
---|
| 1601 | const Vector3 normal = Normalize(rays[candidateIdx].mRay->GetDir());
|
---|
| 1602 |
|
---|
| 1603 | return Plane3(normal, pt);
|
---|
| 1604 | }
|
---|
| 1605 |
|
---|
[480] | 1606 |
|
---|
[473] | 1607 | Plane3 VspBspTree::ChooseCandidatePlane2(const RayInfoContainer &rays) const
|
---|
[482] | 1608 | {
|
---|
[473] | 1609 | Vector3 pt[3];
|
---|
[482] | 1610 |
|
---|
[473] | 1611 | int idx[3];
|
---|
| 1612 | int cmaxT = 0;
|
---|
| 1613 | int cminT = 0;
|
---|
| 1614 | bool chooseMin = false;
|
---|
| 1615 |
|
---|
| 1616 | for (int j = 0; j < 3; ++ j)
|
---|
| 1617 | {
|
---|
| 1618 | idx[j] = (int)RandomValue(0, (Real)((int)rays.size() * 2 - 1));
|
---|
[482] | 1619 |
|
---|
[473] | 1620 | if (idx[j] >= (int)rays.size())
|
---|
| 1621 | {
|
---|
| 1622 | idx[j] -= (int)rays.size();
|
---|
[482] | 1623 |
|
---|
[473] | 1624 | chooseMin = (cminT < 2);
|
---|
| 1625 | }
|
---|
| 1626 | else
|
---|
| 1627 | chooseMin = (cmaxT < 2);
|
---|
| 1628 |
|
---|
| 1629 | RayInfo rayInf = rays[idx[j]];
|
---|
| 1630 | pt[j] = chooseMin ? rayInf.ExtrapOrigin() : rayInf.ExtrapTermination();
|
---|
[482] | 1631 | }
|
---|
[473] | 1632 |
|
---|
| 1633 | return Plane3(pt[0], pt[1], pt[2]);
|
---|
| 1634 | }
|
---|
| 1635 |
|
---|
[580] | 1636 |
|
---|
[473] | 1637 | Plane3 VspBspTree::ChooseCandidatePlane3(const RayInfoContainer &rays) const
|
---|
[482] | 1638 | {
|
---|
[473] | 1639 | Vector3 pt[3];
|
---|
[482] | 1640 |
|
---|
[473] | 1641 | int idx1 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
| 1642 | int idx2 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
| 1643 |
|
---|
| 1644 | // check if rays different
|
---|
| 1645 | if (idx1 == idx2)
|
---|
| 1646 | idx2 = (idx2 + 1) % (int)rays.size();
|
---|
| 1647 |
|
---|
| 1648 | const RayInfo ray1 = rays[idx1];
|
---|
| 1649 | const RayInfo ray2 = rays[idx2];
|
---|
| 1650 |
|
---|
| 1651 | // normal vector of the plane parallel to both lines
|
---|
| 1652 | const Vector3 norm = Normalize(CrossProd(ray1.mRay->GetDir(), ray2.mRay->GetDir()));
|
---|
| 1653 |
|
---|
| 1654 | // vector from line 1 to line 2
|
---|
[479] | 1655 | const Vector3 vd = ray2.ExtrapOrigin() - ray1.ExtrapOrigin();
|
---|
[482] | 1656 |
|
---|
[473] | 1657 | // project vector on normal to get distance
|
---|
| 1658 | const float dist = DotProd(vd, norm);
|
---|
| 1659 |
|
---|
| 1660 | // point on plane lies halfway between the two planes
|
---|
| 1661 | const Vector3 planePt = ray1.ExtrapOrigin() + norm * dist * 0.5;
|
---|
| 1662 |
|
---|
| 1663 | return Plane3(norm, planePt);
|
---|
| 1664 | }
|
---|
| 1665 |
|
---|
[495] | 1666 |
|
---|
[463] | 1667 | inline void VspBspTree::GenerateUniqueIdsForPvs()
|
---|
| 1668 | {
|
---|
[580] | 1669 | Intersectable::NewMail(); sBackId = Intersectable::sMailId;
|
---|
| 1670 | Intersectable::NewMail(); sFrontId = Intersectable::sMailId;
|
---|
| 1671 | Intersectable::NewMail(); sFrontAndBackId = Intersectable::sMailId;
|
---|
[463] | 1672 | }
|
---|
| 1673 |
|
---|
[495] | 1674 |
|
---|
[652] | 1675 | float VspBspTree::EvalRenderCostDecrease(const Plane3 &candidatePlane,
|
---|
| 1676 | const VspBspTraversalData &data) const
|
---|
| 1677 | {
|
---|
| 1678 | int pvsFront = 0;
|
---|
| 1679 | int pvsBack = 0;
|
---|
[654] | 1680 | int totalPvs = 0;
|
---|
[652] | 1681 |
|
---|
| 1682 | // probability that view point lies in back / front node
|
---|
| 1683 | float pOverall = data.mProbability;
|
---|
| 1684 | float pFront = 0;
|
---|
| 1685 | float pBack = 0;
|
---|
| 1686 |
|
---|
| 1687 |
|
---|
| 1688 | // create unique ids for pvs heuristics
|
---|
| 1689 | GenerateUniqueIdsForPvs();
|
---|
| 1690 |
|
---|
| 1691 | for (int i = 0; i < data.mRays->size(); ++ i)
|
---|
| 1692 | {
|
---|
| 1693 | RayInfo rayInf = (*data.mRays)[i];
|
---|
| 1694 |
|
---|
| 1695 | float t;
|
---|
| 1696 | VssRay *ray = rayInf.mRay;
|
---|
| 1697 | const int cf = rayInf.ComputeRayIntersection(candidatePlane, t);
|
---|
| 1698 |
|
---|
| 1699 | // find front and back pvs for origing and termination object
|
---|
| 1700 | AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
| 1701 | AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
| 1702 | }
|
---|
| 1703 |
|
---|
| 1704 |
|
---|
| 1705 | BspNodeGeometry geomFront;
|
---|
| 1706 | BspNodeGeometry geomBack;
|
---|
| 1707 |
|
---|
| 1708 | // construct child geometry with regard to the candidate split plane
|
---|
| 1709 | data.mGeometry->SplitGeometry(geomFront,
|
---|
| 1710 | geomBack,
|
---|
| 1711 | candidatePlane,
|
---|
| 1712 | mBox,
|
---|
[676] | 1713 | 0.000000001f);
|
---|
[675] | 1714 | //mEpsilon);
|
---|
[652] | 1715 |
|
---|
| 1716 | if (!mUseAreaForPvs) // use front and back cell areas to approximate volume
|
---|
| 1717 | {
|
---|
| 1718 | pFront = geomFront.GetVolume();
|
---|
| 1719 | pBack = pOverall - pFront;
|
---|
[675] | 1720 |
|
---|
[676] | 1721 | if ((pFront < 0.001) || (pBack < 0.001))
|
---|
| 1722 | {
|
---|
| 1723 | Debug << "vol f " << pFront << " b " << pBack << " p " << pOverall << endl;
|
---|
| 1724 | Debug << "real vol f " << pFront << " b " << geomBack.GetVolume() << " p " << pOverall << endl;
|
---|
[678] | 1725 | Debug << "polys f " << geomFront.Size() << " b " << geomBack.Size() << " data " << data.mGeometry->Size() << endl;
|
---|
[676] | 1726 | }
|
---|
| 1727 |
|
---|
[675] | 1728 | // clamp because of possible precision issues
|
---|
[676] | 1729 | if (0)
|
---|
| 1730 | {
|
---|
| 1731 | if (pFront < 0) pFront = 0;
|
---|
| 1732 | if (pBack < 0) pBack = 0;
|
---|
| 1733 | }
|
---|
[652] | 1734 | }
|
---|
| 1735 | else
|
---|
| 1736 | {
|
---|
| 1737 | pFront = geomFront.GetArea();
|
---|
| 1738 | pBack = geomBack.GetArea();
|
---|
| 1739 | }
|
---|
| 1740 |
|
---|
| 1741 |
|
---|
| 1742 | // -- pvs rendering heuristics
|
---|
| 1743 | const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
|
---|
| 1744 | const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
|
---|
| 1745 |
|
---|
| 1746 | // only render cost heuristics or combined with standard deviation
|
---|
| 1747 | const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit);
|
---|
| 1748 | const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
|
---|
| 1749 | const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
|
---|
| 1750 |
|
---|
| 1751 | const float oldRenderCost = pOverall * penaltyOld;
|
---|
| 1752 | const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
|
---|
| 1753 |
|
---|
[654] | 1754 | //Debug << "decrease: " << oldRenderCost - newRenderCost << endl;
|
---|
| 1755 | return (oldRenderCost - newRenderCost) / mBox.GetVolume();
|
---|
[652] | 1756 | }
|
---|
| 1757 |
|
---|
| 1758 |
|
---|
[573] | 1759 | float VspBspTree::EvalSplitPlaneCost(const Plane3 &candidatePlane,
|
---|
| 1760 | const VspBspTraversalData &data,
|
---|
| 1761 | BspNodeGeometry &geomFront,
|
---|
| 1762 | BspNodeGeometry &geomBack,
|
---|
| 1763 | float &pFront,
|
---|
| 1764 | float &pBack) const
|
---|
[463] | 1765 | {
|
---|
[472] | 1766 | float cost = 0;
|
---|
[463] | 1767 |
|
---|
[652] | 1768 | int totalPvs = 0;
|
---|
[508] | 1769 | int pvsFront = 0;
|
---|
| 1770 | int pvsBack = 0;
|
---|
[652] | 1771 |
|
---|
[508] | 1772 | // probability that view point lies in back / front node
|
---|
[463] | 1773 | float pOverall = 0;
|
---|
[547] | 1774 | pFront = 0;
|
---|
| 1775 | pBack = 0;
|
---|
[463] | 1776 |
|
---|
[508] | 1777 |
|
---|
[463] | 1778 | int limit;
|
---|
| 1779 | bool useRand;
|
---|
| 1780 |
|
---|
[652] | 1781 | // create unique ids for pvs heuristics
|
---|
| 1782 | GenerateUniqueIdsForPvs();
|
---|
| 1783 |
|
---|
[491] | 1784 | // choose test rays randomly if too much
|
---|
[463] | 1785 | if ((int)data.mRays->size() > mMaxTests)
|
---|
| 1786 | {
|
---|
| 1787 | useRand = true;
|
---|
| 1788 | limit = mMaxTests;
|
---|
| 1789 | }
|
---|
| 1790 | else
|
---|
| 1791 | {
|
---|
| 1792 | useRand = false;
|
---|
| 1793 | limit = (int)data.mRays->size();
|
---|
| 1794 | }
|
---|
[508] | 1795 |
|
---|
[463] | 1796 | for (int i = 0; i < limit; ++ i)
|
---|
| 1797 | {
|
---|
[508] | 1798 | const int testIdx = useRand ?
|
---|
| 1799 | (int)RandomValue(0, (Real)((int)data.mRays->size() - 1)) : i;
|
---|
[463] | 1800 | RayInfo rayInf = (*data.mRays)[testIdx];
|
---|
| 1801 |
|
---|
| 1802 | float t;
|
---|
[508] | 1803 | VssRay *ray = rayInf.mRay;
|
---|
[463] | 1804 | const int cf = rayInf.ComputeRayIntersection(candidatePlane, t);
|
---|
| 1805 |
|
---|
[652] | 1806 | // find front and back pvs for origing and termination object
|
---|
| 1807 | AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
| 1808 | AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
| 1809 | }
|
---|
[508] | 1810 |
|
---|
[652] | 1811 | // construct child geometry with regard to the candidate split plane
|
---|
| 1812 | data.mGeometry->SplitGeometry(geomFront,
|
---|
| 1813 | geomBack,
|
---|
| 1814 | candidatePlane,
|
---|
| 1815 | mBox,
|
---|
[678] | 1816 | 0.000000001f);
|
---|
[676] | 1817 | //mEpsilon);
|
---|
[675] | 1818 |
|
---|
[652] | 1819 | pOverall = data.mProbability;
|
---|
[463] | 1820 |
|
---|
[652] | 1821 | if (!mUseAreaForPvs) // use front and back cell areas to approximate volume
|
---|
| 1822 | {
|
---|
| 1823 | pFront = geomFront.GetVolume();
|
---|
| 1824 | pBack = pOverall - pFront;
|
---|
[675] | 1825 |
|
---|
| 1826 | // clamp because of possible precision issues
|
---|
[676] | 1827 | if (1)
|
---|
| 1828 | {
|
---|
[678] | 1829 | if ((pFront <= 0) || (pBack <= 0) ||
|
---|
| 1830 | !geomFront.Valid() || !geomBack.Valid())
|
---|
[676] | 1831 | {
|
---|
[678] | 1832 | Debug << "error f: " << pFront << " b: " << pBack << endl;
|
---|
[676] | 1833 | return 999;
|
---|
| 1834 | }
|
---|
| 1835 | }
|
---|
[463] | 1836 | }
|
---|
[652] | 1837 | else
|
---|
[542] | 1838 | {
|
---|
[652] | 1839 | pFront = geomFront.GetArea();
|
---|
| 1840 | pBack = geomBack.GetArea();
|
---|
[542] | 1841 | }
|
---|
[652] | 1842 |
|
---|
[542] | 1843 |
|
---|
[580] | 1844 | // -- pvs rendering heuristics
|
---|
[652] | 1845 | const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
|
---|
| 1846 | const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
|
---|
[580] | 1847 |
|
---|
[652] | 1848 | // only render cost heuristics or combined with standard deviation
|
---|
| 1849 | const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit);
|
---|
| 1850 | const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
|
---|
| 1851 | const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
|
---|
[579] | 1852 |
|
---|
[652] | 1853 | const float oldRenderCost = pOverall * penaltyOld;
|
---|
| 1854 | const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
|
---|
[579] | 1855 |
|
---|
[652] | 1856 | float oldCost, newCost;
|
---|
[580] | 1857 |
|
---|
[652] | 1858 | // only render cost
|
---|
| 1859 | if (1)
|
---|
| 1860 | {
|
---|
| 1861 | oldCost = oldRenderCost;
|
---|
| 1862 | newCost = newRenderCost;
|
---|
| 1863 | }
|
---|
| 1864 | else // also considering standard deviation
|
---|
| 1865 | {
|
---|
| 1866 | // standard deviation is difference of back and front pvs
|
---|
| 1867 | const float expectedCost = 0.5f * (penaltyFront + penaltyBack);
|
---|
[580] | 1868 |
|
---|
[652] | 1869 | const float newDeviation = 0.5f *
|
---|
| 1870 | fabs(penaltyFront - expectedCost) + fabs(penaltyBack - expectedCost);
|
---|
[580] | 1871 |
|
---|
[652] | 1872 | const float oldDeviation = penaltyOld;
|
---|
[580] | 1873 |
|
---|
[652] | 1874 | newCost = mRenderCostWeight * newRenderCost + (1.0f - mRenderCostWeight) * newDeviation;
|
---|
| 1875 | oldCost = mRenderCostWeight * oldRenderCost + (1.0f - mRenderCostWeight) * oldDeviation;
|
---|
| 1876 | }
|
---|
[580] | 1877 |
|
---|
[652] | 1878 | cost += mPvsFactor * newCost / (oldCost + Limits::Small);
|
---|
[607] | 1879 |
|
---|
[463] | 1880 |
|
---|
| 1881 | #ifdef _DEBUG
|
---|
[474] | 1882 | Debug << "totalpvs: " << data.mPvs << " ptotal: " << pOverall
|
---|
[508] | 1883 | << " frontpvs: " << pvsFront << " pFront: " << pFront
|
---|
| 1884 | << " backpvs: " << pvsBack << " pBack: " << pBack << endl << endl;
|
---|
[600] | 1885 | Debug << "cost: " << cost << endl;
|
---|
[463] | 1886 | #endif
|
---|
[482] | 1887 |
|
---|
[652] | 1888 | return cost;
|
---|
[463] | 1889 | }
|
---|
| 1890 |
|
---|
[508] | 1891 |
|
---|
[580] | 1892 | float VspBspTree::EvalAxisAlignedSplitCost(const VspBspTraversalData &data,
|
---|
| 1893 | const AxisAlignedBox3 &box,
|
---|
| 1894 | const int axis,
|
---|
| 1895 | const float &position,
|
---|
| 1896 | float &pFront,
|
---|
| 1897 | float &pBack) const
|
---|
| 1898 | {
|
---|
| 1899 | int pvsTotal = 0;
|
---|
| 1900 | int pvsFront = 0;
|
---|
| 1901 | int pvsBack = 0;
|
---|
| 1902 |
|
---|
| 1903 | // create unique ids for pvs heuristics
|
---|
| 1904 | GenerateUniqueIdsForPvs();
|
---|
| 1905 |
|
---|
| 1906 | const int pvsSize = data.mPvs;
|
---|
| 1907 |
|
---|
| 1908 | RayInfoContainer::const_iterator rit, rit_end = data.mRays->end();
|
---|
| 1909 |
|
---|
| 1910 | // this is the main ray classification loop!
|
---|
| 1911 | for(rit = data.mRays->begin(); rit != rit_end; ++ rit)
|
---|
| 1912 | {
|
---|
| 1913 | //if (!(*rit).mRay->IsActive()) continue;
|
---|
| 1914 |
|
---|
| 1915 | // determine the side of this ray with respect to the plane
|
---|
| 1916 | float t;
|
---|
| 1917 | const int side = (*rit).ComputeRayIntersection(axis, position, t);
|
---|
| 1918 |
|
---|
| 1919 | AddObjToPvs((*rit).mRay->mTerminationObject, side, pvsFront, pvsBack, pvsTotal);
|
---|
| 1920 | AddObjToPvs((*rit).mRay->mOriginObject, side, pvsFront, pvsBack, pvsTotal);
|
---|
| 1921 | }
|
---|
| 1922 |
|
---|
| 1923 | //-- pvs heuristics
|
---|
| 1924 | float pOverall;
|
---|
| 1925 |
|
---|
| 1926 | //-- compute heurstics
|
---|
| 1927 | // we take simplified computation for mid split
|
---|
| 1928 |
|
---|
| 1929 | pOverall = data.mProbability;
|
---|
| 1930 |
|
---|
| 1931 | if (!mUseAreaForPvs)
|
---|
| 1932 | { // volume
|
---|
| 1933 | pBack = pFront = pOverall * 0.5f;
|
---|
| 1934 |
|
---|
| 1935 | #if 0
|
---|
| 1936 | // box length substitute for probability
|
---|
| 1937 | const float minBox = box.Min(axis);
|
---|
| 1938 | const float maxBox = box.Max(axis);
|
---|
| 1939 |
|
---|
| 1940 | pBack = position - minBox;
|
---|
| 1941 | pFront = maxBox - position;
|
---|
| 1942 | pOverall = maxBox - minBox;
|
---|
| 1943 | #endif
|
---|
| 1944 | }
|
---|
| 1945 | else //-- area substitute for probability
|
---|
| 1946 | {
|
---|
| 1947 | const int axis2 = (axis + 1) % 3;
|
---|
| 1948 | const int axis3 = (axis + 2) % 3;
|
---|
| 1949 |
|
---|
| 1950 | const float faceArea =
|
---|
| 1951 | (box.Max(axis2) - box.Min(axis2)) *
|
---|
| 1952 | (box.Max(axis3) - box.Min(axis3));
|
---|
| 1953 |
|
---|
| 1954 | pBack = pFront = pOverall * 0.5f + faceArea;
|
---|
| 1955 | }
|
---|
| 1956 |
|
---|
| 1957 | #ifdef _DEBUG
|
---|
| 1958 | Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl;
|
---|
| 1959 | Debug << pFront << " " << pBack << " " << pOverall << endl;
|
---|
| 1960 | #endif
|
---|
| 1961 |
|
---|
| 1962 |
|
---|
| 1963 | const float newCost = pvsBack * pBack + pvsFront * pFront;
|
---|
| 1964 | const float oldCost = (float)pvsSize * pOverall + Limits::Small;
|
---|
| 1965 |
|
---|
| 1966 | return (mCtDivCi + newCost) / oldCost;
|
---|
| 1967 | }
|
---|
| 1968 |
|
---|
| 1969 |
|
---|
[463] | 1970 | void VspBspTree::AddObjToPvs(Intersectable *obj,
|
---|
| 1971 | const int cf,
|
---|
| 1972 | int &frontPvs,
|
---|
[508] | 1973 | int &backPvs,
|
---|
| 1974 | int &totalPvs) const
|
---|
[463] | 1975 | {
|
---|
| 1976 | if (!obj)
|
---|
| 1977 | return;
|
---|
[654] | 1978 | // new object
|
---|
[508] | 1979 | if ((obj->mMailbox != sFrontId) &&
|
---|
| 1980 | (obj->mMailbox != sBackId) &&
|
---|
| 1981 | (obj->mMailbox != sFrontAndBackId))
|
---|
| 1982 | {
|
---|
| 1983 | ++ totalPvs;
|
---|
| 1984 | }
|
---|
| 1985 |
|
---|
[463] | 1986 | // TODO: does this really belong to no pvs?
|
---|
| 1987 | //if (cf == Ray::COINCIDENT) return;
|
---|
| 1988 |
|
---|
| 1989 | // object belongs to both PVS
|
---|
| 1990 | if (cf >= 0)
|
---|
| 1991 | {
|
---|
[482] | 1992 | if ((obj->mMailbox != sFrontId) &&
|
---|
[463] | 1993 | (obj->mMailbox != sFrontAndBackId))
|
---|
| 1994 | {
|
---|
| 1995 | ++ frontPvs;
|
---|
[508] | 1996 |
|
---|
[463] | 1997 | if (obj->mMailbox == sBackId)
|
---|
[482] | 1998 | obj->mMailbox = sFrontAndBackId;
|
---|
[463] | 1999 | else
|
---|
[482] | 2000 | obj->mMailbox = sFrontId;
|
---|
[463] | 2001 | }
|
---|
| 2002 | }
|
---|
[482] | 2003 |
|
---|
[463] | 2004 | if (cf <= 0)
|
---|
| 2005 | {
|
---|
| 2006 | if ((obj->mMailbox != sBackId) &&
|
---|
| 2007 | (obj->mMailbox != sFrontAndBackId))
|
---|
| 2008 | {
|
---|
| 2009 | ++ backPvs;
|
---|
[508] | 2010 |
|
---|
[463] | 2011 | if (obj->mMailbox == sFrontId)
|
---|
[482] | 2012 | obj->mMailbox = sFrontAndBackId;
|
---|
[463] | 2013 | else
|
---|
[482] | 2014 | obj->mMailbox = sBackId;
|
---|
[463] | 2015 | }
|
---|
| 2016 | }
|
---|
| 2017 | }
|
---|
| 2018 |
|
---|
[491] | 2019 |
|
---|
[503] | 2020 | void VspBspTree::CollectLeaves(vector<BspLeaf *> &leaves,
|
---|
| 2021 | const bool onlyUnmailed,
|
---|
| 2022 | const int maxPvsSize) const
|
---|
[463] | 2023 | {
|
---|
| 2024 | stack<BspNode *> nodeStack;
|
---|
| 2025 | nodeStack.push(mRoot);
|
---|
[482] | 2026 |
|
---|
| 2027 | while (!nodeStack.empty())
|
---|
[463] | 2028 | {
|
---|
| 2029 | BspNode *node = nodeStack.top();
|
---|
| 2030 | nodeStack.pop();
|
---|
[489] | 2031 |
|
---|
[482] | 2032 | if (node->IsLeaf())
|
---|
[463] | 2033 | {
|
---|
[490] | 2034 | // test if this leaf is in valid view space
|
---|
[503] | 2035 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
| 2036 | if (leaf->TreeValid() &&
|
---|
[508] | 2037 | (!onlyUnmailed || !leaf->Mailed()) &&
|
---|
[503] | 2038 | ((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().GetSize() <= maxPvsSize)))
|
---|
[490] | 2039 | {
|
---|
| 2040 | leaves.push_back(leaf);
|
---|
| 2041 | }
|
---|
[482] | 2042 | }
|
---|
| 2043 | else
|
---|
[463] | 2044 | {
|
---|
| 2045 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 2046 |
|
---|
| 2047 | nodeStack.push(interior->GetBack());
|
---|
| 2048 | nodeStack.push(interior->GetFront());
|
---|
| 2049 | }
|
---|
| 2050 | }
|
---|
| 2051 | }
|
---|
| 2052 |
|
---|
[489] | 2053 |
|
---|
[463] | 2054 | AxisAlignedBox3 VspBspTree::GetBoundingBox() const
|
---|
| 2055 | {
|
---|
| 2056 | return mBox;
|
---|
| 2057 | }
|
---|
| 2058 |
|
---|
[489] | 2059 |
|
---|
[463] | 2060 | BspNode *VspBspTree::GetRoot() const
|
---|
| 2061 | {
|
---|
| 2062 | return mRoot;
|
---|
| 2063 | }
|
---|
| 2064 |
|
---|
[489] | 2065 |
|
---|
[463] | 2066 | void VspBspTree::EvaluateLeafStats(const VspBspTraversalData &data)
|
---|
| 2067 | {
|
---|
| 2068 | // the node became a leaf -> evaluate stats for leafs
|
---|
| 2069 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode);
|
---|
| 2070 |
|
---|
| 2071 | // store maximal and minimal depth
|
---|
[574] | 2072 | if (data.mDepth > mBspStats.maxDepth)
|
---|
| 2073 | mBspStats.maxDepth = data.mDepth;
|
---|
[463] | 2074 |
|
---|
[574] | 2075 | if (data.mPvs > mBspStats.maxPvs)
|
---|
| 2076 | mBspStats.maxPvs = data.mPvs;
|
---|
[508] | 2077 |
|
---|
[656] | 2078 | mBspStats.pvs += data.mPvs;
|
---|
| 2079 |
|
---|
[574] | 2080 | if (data.mDepth < mBspStats.minDepth)
|
---|
| 2081 | mBspStats.minDepth = data.mDepth;
|
---|
[656] | 2082 |
|
---|
[463] | 2083 | if (data.mDepth >= mTermMaxDepth)
|
---|
[574] | 2084 | ++ mBspStats.maxDepthNodes;
|
---|
[611] | 2085 |
|
---|
[508] | 2086 | // accumulate rays to compute rays / leaf
|
---|
[574] | 2087 | mBspStats.accumRays += (int)data.mRays->size();
|
---|
[463] | 2088 |
|
---|
[437] | 2089 | if (data.mPvs < mTermMinPvs)
|
---|
[574] | 2090 | ++ mBspStats.minPvsNodes;
|
---|
[437] | 2091 |
|
---|
| 2092 | if ((int)data.mRays->size() < mTermMinRays)
|
---|
[574] | 2093 | ++ mBspStats.minRaysNodes;
|
---|
[437] | 2094 |
|
---|
| 2095 | if (data.GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
[574] | 2096 | ++ mBspStats.maxRayContribNodes;
|
---|
[482] | 2097 |
|
---|
[547] | 2098 | if (data.mProbability <= mTermMinProbability)
|
---|
[574] | 2099 | ++ mBspStats.minProbabilityNodes;
|
---|
[508] | 2100 |
|
---|
[474] | 2101 | // accumulate depth to compute average depth
|
---|
[574] | 2102 | mBspStats.accumDepth += data.mDepth;
|
---|
[463] | 2103 |
|
---|
[612] | 2104 | ++ mCreatedViewCells;
|
---|
[656] | 2105 |
|
---|
[463] | 2106 | #ifdef _DEBUG
|
---|
| 2107 | Debug << "BSP stats: "
|
---|
| 2108 | << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), "
|
---|
| 2109 | << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
|
---|
[587] | 2110 | // << "Area: " << data.mProbability << " (min: " << mTermMinProbability << "), "
|
---|
[482] | 2111 | << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
|
---|
[463] | 2112 | << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, "
|
---|
| 2113 | << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
|
---|
| 2114 | #endif
|
---|
| 2115 | }
|
---|
| 2116 |
|
---|
[612] | 2117 |
|
---|
[463] | 2118 | int VspBspTree::CastRay(Ray &ray)
|
---|
| 2119 | {
|
---|
| 2120 | int hits = 0;
|
---|
[482] | 2121 |
|
---|
[600] | 2122 | stack<BspRayTraversalData> tQueue;
|
---|
[482] | 2123 |
|
---|
[463] | 2124 | float maxt, mint;
|
---|
| 2125 |
|
---|
| 2126 | if (!mBox.GetRaySegment(ray, mint, maxt))
|
---|
| 2127 | return 0;
|
---|
| 2128 |
|
---|
| 2129 | Intersectable::NewMail();
|
---|
[600] | 2130 | ViewCell::NewMail();
|
---|
[463] | 2131 | Vector3 entp = ray.Extrap(mint);
|
---|
| 2132 | Vector3 extp = ray.Extrap(maxt);
|
---|
[482] | 2133 |
|
---|
[463] | 2134 | BspNode *node = mRoot;
|
---|
| 2135 | BspNode *farChild = NULL;
|
---|
[482] | 2136 |
|
---|
[463] | 2137 | while (1)
|
---|
| 2138 | {
|
---|
[482] | 2139 | if (!node->IsLeaf())
|
---|
[463] | 2140 | {
|
---|
| 2141 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
| 2142 |
|
---|
| 2143 | Plane3 splitPlane = in->GetPlane();
|
---|
| 2144 | const int entSide = splitPlane.Side(entp);
|
---|
| 2145 | const int extSide = splitPlane.Side(extp);
|
---|
| 2146 |
|
---|
| 2147 | if (entSide < 0)
|
---|
| 2148 | {
|
---|
| 2149 | node = in->GetBack();
|
---|
| 2150 |
|
---|
| 2151 | if(extSide <= 0) // plane does not split ray => no far child
|
---|
| 2152 | continue;
|
---|
[482] | 2153 |
|
---|
[463] | 2154 | farChild = in->GetFront(); // plane splits ray
|
---|
| 2155 |
|
---|
| 2156 | } else if (entSide > 0)
|
---|
| 2157 | {
|
---|
| 2158 | node = in->GetFront();
|
---|
| 2159 |
|
---|
| 2160 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
| 2161 | continue;
|
---|
| 2162 |
|
---|
[482] | 2163 | farChild = in->GetBack(); // plane splits ray
|
---|
[463] | 2164 | }
|
---|
| 2165 | else // ray and plane are coincident
|
---|
| 2166 | {
|
---|
| 2167 | // WHAT TO DO IN THIS CASE ?
|
---|
| 2168 | //break;
|
---|
| 2169 | node = in->GetFront();
|
---|
| 2170 | continue;
|
---|
| 2171 | }
|
---|
| 2172 |
|
---|
| 2173 | // push data for far child
|
---|
[600] | 2174 | tQueue.push(BspRayTraversalData(farChild, extp, maxt));
|
---|
[463] | 2175 |
|
---|
| 2176 | // find intersection of ray segment with plane
|
---|
| 2177 | float t;
|
---|
| 2178 | extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t);
|
---|
| 2179 | maxt *= t;
|
---|
[482] | 2180 |
|
---|
[463] | 2181 | } else // reached leaf => intersection with view cell
|
---|
| 2182 | {
|
---|
| 2183 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
[482] | 2184 |
|
---|
[463] | 2185 | if (!leaf->GetViewCell()->Mailed())
|
---|
| 2186 | {
|
---|
| 2187 | //ray.bspIntersections.push_back(Ray::VspBspIntersection(maxt, leaf));
|
---|
| 2188 | leaf->GetViewCell()->Mail();
|
---|
| 2189 | ++ hits;
|
---|
| 2190 | }
|
---|
[482] | 2191 |
|
---|
[463] | 2192 | //-- fetch the next far child from the stack
|
---|
[600] | 2193 | if (tQueue.empty())
|
---|
[463] | 2194 | break;
|
---|
[482] | 2195 |
|
---|
[463] | 2196 | entp = extp;
|
---|
| 2197 | mint = maxt; // NOTE: need this?
|
---|
| 2198 |
|
---|
| 2199 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
|
---|
| 2200 | break;
|
---|
| 2201 |
|
---|
[600] | 2202 | BspRayTraversalData &s = tQueue.top();
|
---|
[463] | 2203 |
|
---|
| 2204 | node = s.mNode;
|
---|
| 2205 | extp = s.mExitPoint;
|
---|
| 2206 | maxt = s.mMaxT;
|
---|
| 2207 |
|
---|
[600] | 2208 | tQueue.pop();
|
---|
[463] | 2209 | }
|
---|
| 2210 | }
|
---|
| 2211 |
|
---|
| 2212 | return hits;
|
---|
| 2213 | }
|
---|
| 2214 |
|
---|
[532] | 2215 |
|
---|
[547] | 2216 | void VspBspTree::CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const
|
---|
[463] | 2217 | {
|
---|
[532] | 2218 | ViewCell::NewMail();
|
---|
[551] | 2219 |
|
---|
| 2220 | CollectViewCells(mRoot, onlyValid, viewCells, true);
|
---|
[532] | 2221 | }
|
---|
| 2222 |
|
---|
| 2223 |
|
---|
[574] | 2224 | void VspBspTree::CollapseViewCells()
|
---|
[542] | 2225 | {
|
---|
[590] | 2226 | // TODO
|
---|
| 2227 | #if VC_HISTORY
|
---|
[542] | 2228 | stack<BspNode *> nodeStack;
|
---|
| 2229 |
|
---|
| 2230 | if (!mRoot)
|
---|
| 2231 | return;
|
---|
| 2232 |
|
---|
| 2233 | nodeStack.push(mRoot);
|
---|
| 2234 |
|
---|
| 2235 | while (!nodeStack.empty())
|
---|
| 2236 | {
|
---|
| 2237 | BspNode *node = nodeStack.top();
|
---|
| 2238 | nodeStack.pop();
|
---|
| 2239 |
|
---|
| 2240 | if (node->IsLeaf())
|
---|
[574] | 2241 | {
|
---|
| 2242 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
[542] | 2243 |
|
---|
[574] | 2244 | if (!viewCell->GetValid())
|
---|
[542] | 2245 | {
|
---|
| 2246 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
[580] | 2247 |
|
---|
| 2248 | ViewCellContainer leaves;
|
---|
[590] | 2249 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
[580] | 2250 |
|
---|
| 2251 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 2252 |
|
---|
| 2253 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[542] | 2254 | {
|
---|
[580] | 2255 | BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
[574] | 2256 | l->SetViewCell(GetOrCreateOutOfBoundsCell());
|
---|
| 2257 | ++ mBspStats.invalidLeaves;
|
---|
| 2258 | }
|
---|
[542] | 2259 |
|
---|
[574] | 2260 | // add to unbounded view cell
|
---|
| 2261 | GetOrCreateOutOfBoundsCell()->GetPvs().AddPvs(viewCell->GetPvs());
|
---|
| 2262 | DEL_PTR(viewCell);
|
---|
| 2263 | }
|
---|
| 2264 | }
|
---|
| 2265 | else
|
---|
| 2266 | {
|
---|
| 2267 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 2268 |
|
---|
| 2269 | nodeStack.push(interior->GetFront());
|
---|
| 2270 | nodeStack.push(interior->GetBack());
|
---|
| 2271 | }
|
---|
| 2272 | }
|
---|
[542] | 2273 |
|
---|
[574] | 2274 | Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl;
|
---|
[590] | 2275 | #endif
|
---|
[574] | 2276 | }
|
---|
| 2277 |
|
---|
| 2278 |
|
---|
[639] | 2279 | void VspBspTree::CollectRays(VssRayContainer &rays)
|
---|
| 2280 | {
|
---|
| 2281 | vector<BspLeaf *> leaves;
|
---|
| 2282 |
|
---|
| 2283 | vector<BspLeaf *>::const_iterator lit, lit_end = leaves.end();
|
---|
| 2284 |
|
---|
| 2285 | for (lit = leaves.begin(); lit != lit_end; ++ lit)
|
---|
| 2286 | {
|
---|
| 2287 | BspLeaf *leaf = *lit;
|
---|
| 2288 | VssRayContainer::const_iterator rit, rit_end = leaf->mVssRays.end();
|
---|
| 2289 |
|
---|
| 2290 | for (rit = leaf->mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 2291 | rays.push_back(*rit);
|
---|
| 2292 | }
|
---|
| 2293 | }
|
---|
| 2294 |
|
---|
| 2295 |
|
---|
[574] | 2296 | void VspBspTree::ValidateTree()
|
---|
| 2297 | {
|
---|
| 2298 | stack<BspNode *> nodeStack;
|
---|
| 2299 |
|
---|
| 2300 | if (!mRoot)
|
---|
| 2301 | return;
|
---|
| 2302 |
|
---|
| 2303 | nodeStack.push(mRoot);
|
---|
| 2304 |
|
---|
| 2305 | mBspStats.invalidLeaves = 0;
|
---|
| 2306 | while (!nodeStack.empty())
|
---|
| 2307 | {
|
---|
| 2308 | BspNode *node = nodeStack.top();
|
---|
| 2309 | nodeStack.pop();
|
---|
| 2310 |
|
---|
| 2311 | if (node->IsLeaf())
|
---|
| 2312 | {
|
---|
| 2313 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
| 2314 |
|
---|
| 2315 | if (!leaf->GetViewCell()->GetValid())
|
---|
| 2316 | ++ mBspStats.invalidLeaves;
|
---|
| 2317 |
|
---|
| 2318 | // validity flags don't match => repair
|
---|
| 2319 | if (leaf->GetViewCell()->GetValid() != leaf->TreeValid())
|
---|
| 2320 | {
|
---|
| 2321 | leaf->SetTreeValid(leaf->GetViewCell()->GetValid());
|
---|
| 2322 | PropagateUpValidity(leaf);
|
---|
[542] | 2323 | }
|
---|
| 2324 | }
|
---|
| 2325 | else
|
---|
| 2326 | {
|
---|
| 2327 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 2328 |
|
---|
| 2329 | nodeStack.push(interior->GetFront());
|
---|
| 2330 | nodeStack.push(interior->GetBack());
|
---|
| 2331 | }
|
---|
| 2332 | }
|
---|
[562] | 2333 |
|
---|
[574] | 2334 | Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl;
|
---|
[542] | 2335 | }
|
---|
| 2336 |
|
---|
[547] | 2337 |
|
---|
[574] | 2338 |
|
---|
[547] | 2339 | void VspBspTree::CollectViewCells(BspNode *root,
|
---|
| 2340 | bool onlyValid,
|
---|
[532] | 2341 | ViewCellContainer &viewCells,
|
---|
| 2342 | bool onlyUnmailed) const
|
---|
| 2343 | {
|
---|
[498] | 2344 | stack<BspNode *> nodeStack;
|
---|
[463] | 2345 |
|
---|
[538] | 2346 | if (!root)
|
---|
[508] | 2347 | return;
|
---|
[463] | 2348 |
|
---|
[538] | 2349 | nodeStack.push(root);
|
---|
[498] | 2350 |
|
---|
| 2351 | while (!nodeStack.empty())
|
---|
| 2352 | {
|
---|
| 2353 | BspNode *node = nodeStack.top();
|
---|
| 2354 | nodeStack.pop();
|
---|
| 2355 |
|
---|
| 2356 | if (node->IsLeaf())
|
---|
| 2357 | {
|
---|
[564] | 2358 | if (!onlyValid || node->TreeValid())
|
---|
[498] | 2359 | {
|
---|
[590] | 2360 | ViewCell *viewCell =
|
---|
| 2361 | mViewCellsTree->GetActiveViewCell(dynamic_cast<BspLeaf *>(node)->GetViewCell());
|
---|
| 2362 |
|
---|
[532] | 2363 | if (!onlyUnmailed || !viewCell->Mailed())
|
---|
[498] | 2364 | {
|
---|
| 2365 | viewCell->Mail();
|
---|
| 2366 | viewCells.push_back(viewCell);
|
---|
| 2367 | }
|
---|
| 2368 | }
|
---|
| 2369 | }
|
---|
| 2370 | else
|
---|
| 2371 | {
|
---|
| 2372 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
[508] | 2373 |
|
---|
[498] | 2374 | nodeStack.push(interior->GetFront());
|
---|
| 2375 | nodeStack.push(interior->GetBack());
|
---|
| 2376 | }
|
---|
| 2377 | }
|
---|
[590] | 2378 |
|
---|
[463] | 2379 | }
|
---|
| 2380 |
|
---|
| 2381 |
|
---|
[648] | 2382 | void VspBspTree::PreprocessPolygons(PolygonContainer &polys)
|
---|
| 2383 | {
|
---|
| 2384 | // preprocess: throw out polygons coincident to the view space box (not needed)
|
---|
| 2385 | PolygonContainer boxPolys;
|
---|
| 2386 | mBox.ExtractPolys(boxPolys);
|
---|
| 2387 | vector<Plane3> boxPlanes;
|
---|
| 2388 |
|
---|
| 2389 | PolygonContainer::iterator pit, pit_end = boxPolys.end();
|
---|
| 2390 |
|
---|
| 2391 | // extract planes of box
|
---|
| 2392 | // TODO: can be done more elegantly than first extracting polygons
|
---|
| 2393 | // and take their planes
|
---|
| 2394 | for (pit = boxPolys.begin(); pit != pit_end; ++ pit)
|
---|
| 2395 | {
|
---|
| 2396 | boxPlanes.push_back((*pit)->GetSupportingPlane());
|
---|
| 2397 | }
|
---|
| 2398 |
|
---|
| 2399 | pit_end = polys.end();
|
---|
| 2400 |
|
---|
| 2401 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
| 2402 | {
|
---|
| 2403 | vector<Plane3>::const_iterator bit, bit_end = boxPlanes.end();
|
---|
| 2404 |
|
---|
| 2405 | for (bit = boxPlanes.begin(); (bit != bit_end) && (*pit); ++ bit)
|
---|
| 2406 | {
|
---|
| 2407 | const int cf = (*pit)->ClassifyPlane(*bit, mEpsilon);
|
---|
| 2408 |
|
---|
| 2409 | if (cf == Polygon3::COINCIDENT)
|
---|
| 2410 | {
|
---|
| 2411 | DEL_PTR(*pit);
|
---|
| 2412 | //Debug << "coincident!!" << endl;
|
---|
| 2413 | }
|
---|
| 2414 | }
|
---|
| 2415 | }
|
---|
| 2416 |
|
---|
| 2417 | // remove deleted entries
|
---|
| 2418 | for (int i = 0; i < (int)polys.size(); ++ i)
|
---|
| 2419 | {
|
---|
| 2420 | while (!polys[i] && (i < (int)polys.size()))
|
---|
| 2421 | {
|
---|
| 2422 | swap(polys[i], polys.back());
|
---|
| 2423 | polys.pop_back();
|
---|
| 2424 | }
|
---|
| 2425 | }
|
---|
| 2426 | }
|
---|
| 2427 |
|
---|
| 2428 |
|
---|
[463] | 2429 | float VspBspTree::AccumulatedRayLength(const RayInfoContainer &rays) const
|
---|
| 2430 | {
|
---|
| 2431 | float len = 0;
|
---|
| 2432 |
|
---|
| 2433 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
| 2434 |
|
---|
| 2435 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 2436 | len += (*it).SegmentLength();
|
---|
| 2437 |
|
---|
| 2438 | return len;
|
---|
| 2439 | }
|
---|
| 2440 |
|
---|
[479] | 2441 |
|
---|
[463] | 2442 | int VspBspTree::SplitRays(const Plane3 &plane,
|
---|
[482] | 2443 | RayInfoContainer &rays,
|
---|
| 2444 | RayInfoContainer &frontRays,
|
---|
[639] | 2445 | RayInfoContainer &backRays) const
|
---|
[463] | 2446 | {
|
---|
| 2447 | int splits = 0;
|
---|
| 2448 |
|
---|
[574] | 2449 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
| 2450 |
|
---|
| 2451 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
[463] | 2452 | {
|
---|
[574] | 2453 | RayInfo bRay = *it;
|
---|
| 2454 |
|
---|
[463] | 2455 | VssRay *ray = bRay.mRay;
|
---|
[473] | 2456 | float t;
|
---|
[463] | 2457 |
|
---|
[485] | 2458 | // get classification and receive new t
|
---|
[463] | 2459 | const int cf = bRay.ComputeRayIntersection(plane, t);
|
---|
[482] | 2460 |
|
---|
[463] | 2461 | switch (cf)
|
---|
| 2462 | {
|
---|
| 2463 | case -1:
|
---|
| 2464 | backRays.push_back(bRay);
|
---|
| 2465 | break;
|
---|
| 2466 | case 1:
|
---|
| 2467 | frontRays.push_back(bRay);
|
---|
| 2468 | break;
|
---|
[482] | 2469 | case 0:
|
---|
| 2470 | {
|
---|
[485] | 2471 | //-- split ray
|
---|
[639] | 2472 | // test if start point behind or in front of plane
|
---|
[485] | 2473 | const int side = plane.Side(bRay.ExtrapOrigin());
|
---|
| 2474 |
|
---|
| 2475 | if (side <= 0)
|
---|
| 2476 | {
|
---|
| 2477 | backRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
| 2478 | frontRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
| 2479 | }
|
---|
| 2480 | else
|
---|
| 2481 | {
|
---|
| 2482 | frontRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
| 2483 | backRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
| 2484 | }
|
---|
[463] | 2485 | }
|
---|
| 2486 | break;
|
---|
| 2487 | default:
|
---|
[485] | 2488 | Debug << "Should not come here" << endl;
|
---|
[463] | 2489 | break;
|
---|
| 2490 | }
|
---|
| 2491 | }
|
---|
| 2492 |
|
---|
| 2493 | return splits;
|
---|
| 2494 | }
|
---|
| 2495 |
|
---|
[479] | 2496 |
|
---|
[463] | 2497 | void VspBspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const
|
---|
| 2498 | {
|
---|
| 2499 | BspNode *lastNode;
|
---|
| 2500 |
|
---|
| 2501 | do
|
---|
| 2502 | {
|
---|
| 2503 | lastNode = n;
|
---|
| 2504 |
|
---|
| 2505 | // want to get planes defining geometry of this node => don't take
|
---|
| 2506 | // split plane of node itself
|
---|
| 2507 | n = n->GetParent();
|
---|
[482] | 2508 |
|
---|
[463] | 2509 | if (n)
|
---|
| 2510 | {
|
---|
| 2511 | BspInterior *interior = dynamic_cast<BspInterior *>(n);
|
---|
| 2512 | Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane();
|
---|
| 2513 |
|
---|
| 2514 | if (interior->GetFront() != lastNode)
|
---|
| 2515 | halfSpace.ReverseOrientation();
|
---|
| 2516 |
|
---|
| 2517 | halfSpaces.push_back(halfSpace);
|
---|
| 2518 | }
|
---|
| 2519 | }
|
---|
| 2520 | while (n);
|
---|
| 2521 | }
|
---|
| 2522 |
|
---|
[485] | 2523 |
|
---|
[482] | 2524 | void VspBspTree::ConstructGeometry(BspNode *n,
|
---|
[503] | 2525 | BspNodeGeometry &geom) const
|
---|
[463] | 2526 | {
|
---|
[437] | 2527 | vector<Plane3> halfSpaces;
|
---|
| 2528 | ExtractHalfSpaces(n, halfSpaces);
|
---|
| 2529 |
|
---|
| 2530 | PolygonContainer candidatePolys;
|
---|
[678] | 2531 | vector<Plane3> candidatePlanes;
|
---|
[437] | 2532 |
|
---|
| 2533 | // bounded planes are added to the polygons (reverse polygons
|
---|
[678] | 2534 | // as they have to be outfacing)
|
---|
[437] | 2535 | for (int i = 0; i < (int)halfSpaces.size(); ++ i)
|
---|
| 2536 | {
|
---|
| 2537 | Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]);
|
---|
[482] | 2538 |
|
---|
[448] | 2539 | if (p->Valid(mEpsilon))
|
---|
[437] | 2540 | {
|
---|
| 2541 | candidatePolys.push_back(p->CreateReversePolygon());
|
---|
[678] | 2542 | // Why?
|
---|
| 2543 | Plane3 candidatePlane = p->GetSupportingPlane(); //halfSpaces[i];
|
---|
| 2544 | //candidatePlane.ReverseOrientation();
|
---|
| 2545 | candidatePlanes.push_back(candidatePlane);
|
---|
| 2546 |
|
---|
[437] | 2547 | DEL_PTR(p);
|
---|
| 2548 | }
|
---|
| 2549 | }
|
---|
| 2550 |
|
---|
| 2551 | // add faces of bounding box (also could be faces of the cell)
|
---|
| 2552 | for (int i = 0; i < 6; ++ i)
|
---|
| 2553 | {
|
---|
| 2554 | VertexContainer vertices;
|
---|
[482] | 2555 |
|
---|
[437] | 2556 | for (int j = 0; j < 4; ++ j)
|
---|
| 2557 | vertices.push_back(mBox.GetFace(i).mVertices[j]);
|
---|
| 2558 |
|
---|
[678] | 2559 | Polygon3 *poly = new Polygon3(vertices);
|
---|
| 2560 |
|
---|
| 2561 | candidatePolys.push_back(poly);
|
---|
| 2562 | candidatePlanes.push_back(poly->GetSupportingPlane());
|
---|
[437] | 2563 | }
|
---|
| 2564 |
|
---|
| 2565 | for (int i = 0; i < (int)candidatePolys.size(); ++ i)
|
---|
| 2566 | {
|
---|
| 2567 | // polygon is split by all other planes
|
---|
| 2568 | for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
|
---|
| 2569 | {
|
---|
| 2570 | if (i == j) // polygon and plane are coincident
|
---|
| 2571 | continue;
|
---|
| 2572 |
|
---|
| 2573 | VertexContainer splitPts;
|
---|
| 2574 | Polygon3 *frontPoly, *backPoly;
|
---|
| 2575 |
|
---|
[482] | 2576 | const int cf =
|
---|
[448] | 2577 | candidatePolys[i]->ClassifyPlane(halfSpaces[j],
|
---|
| 2578 | mEpsilon);
|
---|
[482] | 2579 |
|
---|
[437] | 2580 | switch (cf)
|
---|
| 2581 | {
|
---|
| 2582 | case Polygon3::SPLIT:
|
---|
| 2583 | frontPoly = new Polygon3();
|
---|
| 2584 | backPoly = new Polygon3();
|
---|
| 2585 |
|
---|
[482] | 2586 | candidatePolys[i]->Split(halfSpaces[j],
|
---|
| 2587 | *frontPoly,
|
---|
[448] | 2588 | *backPoly,
|
---|
| 2589 | mEpsilon);
|
---|
[437] | 2590 |
|
---|
| 2591 | DEL_PTR(candidatePolys[i]);
|
---|
| 2592 |
|
---|
[448] | 2593 | if (frontPoly->Valid(mEpsilon))
|
---|
[437] | 2594 | candidatePolys[i] = frontPoly;
|
---|
| 2595 | else
|
---|
| 2596 | DEL_PTR(frontPoly);
|
---|
| 2597 |
|
---|
| 2598 | DEL_PTR(backPoly);
|
---|
| 2599 | break;
|
---|
[678] | 2600 |
|
---|
[437] | 2601 | case Polygon3::BACK_SIDE:
|
---|
| 2602 | DEL_PTR(candidatePolys[i]);
|
---|
| 2603 | break;
|
---|
| 2604 | // just take polygon as it is
|
---|
| 2605 | case Polygon3::FRONT_SIDE:
|
---|
| 2606 | case Polygon3::COINCIDENT:
|
---|
| 2607 | default:
|
---|
| 2608 | break;
|
---|
| 2609 | }
|
---|
| 2610 | }
|
---|
[482] | 2611 |
|
---|
[437] | 2612 | if (candidatePolys[i])
|
---|
[678] | 2613 | {
|
---|
| 2614 | geom.Add(candidatePolys[i], candidatePlanes[i]);
|
---|
| 2615 | // geom.mPolys.push_back(candidates[i]);
|
---|
| 2616 | }
|
---|
[437] | 2617 | }
|
---|
[463] | 2618 | }
|
---|
| 2619 |
|
---|
[485] | 2620 |
|
---|
[582] | 2621 | void VspBspTree::ConstructGeometry(ViewCell *vc,
|
---|
[503] | 2622 | BspNodeGeometry &vcGeom) const
|
---|
[589] | 2623 | {
|
---|
[580] | 2624 | ViewCellContainer leaves;
|
---|
[590] | 2625 |
|
---|
| 2626 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
[463] | 2627 |
|
---|
[580] | 2628 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 2629 |
|
---|
[463] | 2630 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[580] | 2631 | {
|
---|
| 2632 | BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
[590] | 2633 |
|
---|
[580] | 2634 | ConstructGeometry(l, vcGeom);
|
---|
| 2635 | }
|
---|
[463] | 2636 | }
|
---|
| 2637 |
|
---|
[485] | 2638 |
|
---|
[551] | 2639 | typedef pair<BspNode *, BspNodeGeometry *> bspNodePair;
|
---|
| 2640 |
|
---|
[557] | 2641 |
|
---|
[482] | 2642 | int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
|
---|
[562] | 2643 | const bool onlyUnmailed) const
|
---|
[463] | 2644 | {
|
---|
[551] | 2645 | stack<bspNodePair> nodeStack;
|
---|
| 2646 |
|
---|
| 2647 | BspNodeGeometry nodeGeom;
|
---|
| 2648 | ConstructGeometry(n, nodeGeom);
|
---|
| 2649 |
|
---|
[500] | 2650 | // split planes from the root to this node
|
---|
| 2651 | // needed to verify that we found neighbor leaf
|
---|
[557] | 2652 | // TODO: really needed?
|
---|
[463] | 2653 | vector<Plane3> halfSpaces;
|
---|
| 2654 | ExtractHalfSpaces(n, halfSpaces);
|
---|
| 2655 |
|
---|
[551] | 2656 |
|
---|
| 2657 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
| 2658 | ConstructGeometry(mRoot, *rgeom);
|
---|
| 2659 |
|
---|
| 2660 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
| 2661 |
|
---|
[482] | 2662 | while (!nodeStack.empty())
|
---|
[463] | 2663 | {
|
---|
[551] | 2664 | BspNode *node = nodeStack.top().first;
|
---|
| 2665 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
[562] | 2666 |
|
---|
[463] | 2667 | nodeStack.pop();
|
---|
| 2668 |
|
---|
[557] | 2669 | if (node->IsLeaf())
|
---|
[562] | 2670 | {
|
---|
[557] | 2671 | // test if this leaf is in valid view space
|
---|
| 2672 | if (node->TreeValid() &&
|
---|
| 2673 | (node != n) &&
|
---|
| 2674 | (!onlyUnmailed || !node->Mailed()))
|
---|
| 2675 | {
|
---|
| 2676 | bool isAdjacent = true;
|
---|
[551] | 2677 |
|
---|
[570] | 2678 | if (1)
|
---|
[557] | 2679 | {
|
---|
[562] | 2680 | // test all planes of current node if still adjacent
|
---|
| 2681 | for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i)
|
---|
| 2682 | {
|
---|
| 2683 | const int cf =
|
---|
[678] | 2684 | Polygon3::ClassifyPlane(geom->GetPolys(),
|
---|
[562] | 2685 | halfSpaces[i],
|
---|
| 2686 | mEpsilon);
|
---|
[482] | 2687 |
|
---|
[562] | 2688 | if (cf == Polygon3::BACK_SIDE)
|
---|
| 2689 | {
|
---|
| 2690 | isAdjacent = false;
|
---|
| 2691 | }
|
---|
[557] | 2692 | }
|
---|
| 2693 | }
|
---|
[562] | 2694 | else
|
---|
[557] | 2695 | {
|
---|
[562] | 2696 | // TODO: why is this wrong??
|
---|
| 2697 | // test all planes of current node if still adjacent
|
---|
[678] | 2698 | for (int i = 0; (i < nodeGeom.Size()) && isAdjacent; ++ i)
|
---|
[562] | 2699 | {
|
---|
[678] | 2700 | Polygon3 *poly = nodeGeom.GetPolys()[i];
|
---|
[555] | 2701 |
|
---|
[562] | 2702 | const int cf =
|
---|
[678] | 2703 | Polygon3::ClassifyPlane(geom->GetPolys(),
|
---|
[562] | 2704 | poly->GetSupportingPlane(),
|
---|
| 2705 | mEpsilon);
|
---|
[557] | 2706 |
|
---|
[562] | 2707 | if (cf == Polygon3::BACK_SIDE)
|
---|
| 2708 | {
|
---|
| 2709 | isAdjacent = false;
|
---|
| 2710 | }
|
---|
[557] | 2711 | }
|
---|
[570] | 2712 | }
|
---|
[557] | 2713 | // neighbor was found
|
---|
| 2714 | if (isAdjacent)
|
---|
[562] | 2715 | {
|
---|
[551] | 2716 | neighbors.push_back(dynamic_cast<BspLeaf *>(node));
|
---|
[562] | 2717 | }
|
---|
[463] | 2718 | }
|
---|
[562] | 2719 | }
|
---|
| 2720 | else
|
---|
| 2721 | {
|
---|
| 2722 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
[482] | 2723 |
|
---|
[678] | 2724 | const int cf = Polygon3::ClassifyPlane(nodeGeom.GetPolys(),
|
---|
[562] | 2725 | interior->GetPlane(),
|
---|
| 2726 | mEpsilon);
|
---|
[551] | 2727 |
|
---|
[562] | 2728 | BspNode *front = interior->GetFront();
|
---|
| 2729 | BspNode *back = interior->GetBack();
|
---|
[551] | 2730 |
|
---|
[562] | 2731 | BspNodeGeometry *fGeom = new BspNodeGeometry();
|
---|
| 2732 | BspNodeGeometry *bGeom = new BspNodeGeometry();
|
---|
[463] | 2733 |
|
---|
[562] | 2734 | geom->SplitGeometry(*fGeom,
|
---|
| 2735 | *bGeom,
|
---|
| 2736 | interior->GetPlane(),
|
---|
| 2737 | mBox,
|
---|
[675] | 2738 | //0.0000001f);
|
---|
[562] | 2739 | mEpsilon);
|
---|
[551] | 2740 |
|
---|
[562] | 2741 | if (cf == Polygon3::FRONT_SIDE)
|
---|
| 2742 | {
|
---|
| 2743 | nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
|
---|
| 2744 | DEL_PTR(bGeom);
|
---|
| 2745 | }
|
---|
| 2746 | else
|
---|
| 2747 | {
|
---|
| 2748 | if (cf == Polygon3::BACK_SIDE)
|
---|
[551] | 2749 | {
|
---|
[562] | 2750 | nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
|
---|
| 2751 | DEL_PTR(fGeom);
|
---|
[551] | 2752 | }
|
---|
[482] | 2753 | else
|
---|
[562] | 2754 | { // random decision
|
---|
| 2755 | nodeStack.push(bspNodePair(front, fGeom));
|
---|
| 2756 | nodeStack.push(bspNodePair(back, bGeom));
|
---|
[463] | 2757 | }
|
---|
[551] | 2758 | }
|
---|
[463] | 2759 | }
|
---|
[562] | 2760 |
|
---|
[551] | 2761 | DEL_PTR(geom);
|
---|
[463] | 2762 | }
|
---|
[482] | 2763 |
|
---|
[463] | 2764 | return (int)neighbors.size();
|
---|
| 2765 | }
|
---|
| 2766 |
|
---|
[489] | 2767 |
|
---|
[600] | 2768 |
|
---|
| 2769 | int VspBspTree::FindApproximateNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
|
---|
| 2770 | const bool onlyUnmailed) const
|
---|
| 2771 | {
|
---|
| 2772 | stack<bspNodePair> nodeStack;
|
---|
| 2773 |
|
---|
| 2774 | BspNodeGeometry nodeGeom;
|
---|
| 2775 | ConstructGeometry(n, nodeGeom);
|
---|
| 2776 |
|
---|
| 2777 | // split planes from the root to this node
|
---|
| 2778 | // needed to verify that we found neighbor leaf
|
---|
| 2779 | // TODO: really needed?
|
---|
| 2780 | vector<Plane3> halfSpaces;
|
---|
| 2781 | ExtractHalfSpaces(n, halfSpaces);
|
---|
| 2782 |
|
---|
| 2783 |
|
---|
| 2784 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
| 2785 | ConstructGeometry(mRoot, *rgeom);
|
---|
| 2786 |
|
---|
| 2787 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
| 2788 |
|
---|
| 2789 | while (!nodeStack.empty())
|
---|
| 2790 | {
|
---|
| 2791 | BspNode *node = nodeStack.top().first;
|
---|
| 2792 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
| 2793 |
|
---|
| 2794 | nodeStack.pop();
|
---|
| 2795 |
|
---|
| 2796 | if (node->IsLeaf())
|
---|
| 2797 | {
|
---|
| 2798 | // test if this leaf is in valid view space
|
---|
| 2799 | if (node->TreeValid() &&
|
---|
| 2800 | (node != n) &&
|
---|
| 2801 | (!onlyUnmailed || !node->Mailed()))
|
---|
| 2802 | {
|
---|
| 2803 | bool isAdjacent = true;
|
---|
| 2804 |
|
---|
| 2805 | // neighbor was found
|
---|
| 2806 | if (isAdjacent)
|
---|
| 2807 | {
|
---|
| 2808 | neighbors.push_back(dynamic_cast<BspLeaf *>(node));
|
---|
| 2809 | }
|
---|
| 2810 | }
|
---|
| 2811 | }
|
---|
| 2812 | else
|
---|
| 2813 | {
|
---|
| 2814 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 2815 |
|
---|
[678] | 2816 | const int cf = Polygon3::ClassifyPlane(nodeGeom.GetPolys(),
|
---|
[600] | 2817 | interior->GetPlane(),
|
---|
| 2818 | mEpsilon);
|
---|
| 2819 |
|
---|
| 2820 | BspNode *front = interior->GetFront();
|
---|
| 2821 | BspNode *back = interior->GetBack();
|
---|
| 2822 |
|
---|
| 2823 | BspNodeGeometry *fGeom = new BspNodeGeometry();
|
---|
| 2824 | BspNodeGeometry *bGeom = new BspNodeGeometry();
|
---|
| 2825 |
|
---|
| 2826 | geom->SplitGeometry(*fGeom,
|
---|
| 2827 | *bGeom,
|
---|
| 2828 | interior->GetPlane(),
|
---|
| 2829 | mBox,
|
---|
[675] | 2830 | //0.0000001f);
|
---|
[600] | 2831 | mEpsilon);
|
---|
| 2832 |
|
---|
| 2833 | if (cf == Polygon3::FRONT_SIDE)
|
---|
| 2834 | {
|
---|
| 2835 | nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
|
---|
| 2836 | DEL_PTR(bGeom);
|
---|
| 2837 | }
|
---|
| 2838 | else
|
---|
| 2839 | {
|
---|
| 2840 | if (cf == Polygon3::BACK_SIDE)
|
---|
| 2841 | {
|
---|
| 2842 | nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
|
---|
| 2843 | DEL_PTR(fGeom);
|
---|
| 2844 | }
|
---|
| 2845 | else
|
---|
| 2846 | { // random decision
|
---|
| 2847 | nodeStack.push(bspNodePair(front, fGeom));
|
---|
| 2848 | nodeStack.push(bspNodePair(back, bGeom));
|
---|
| 2849 | }
|
---|
| 2850 | }
|
---|
| 2851 | }
|
---|
| 2852 |
|
---|
| 2853 | DEL_PTR(geom);
|
---|
| 2854 | }
|
---|
| 2855 |
|
---|
| 2856 | return (int)neighbors.size();
|
---|
| 2857 | }
|
---|
| 2858 |
|
---|
| 2859 |
|
---|
| 2860 |
|
---|
[463] | 2861 | BspLeaf *VspBspTree::GetRandomLeaf(const Plane3 &halfspace)
|
---|
| 2862 | {
|
---|
| 2863 | stack<BspNode *> nodeStack;
|
---|
| 2864 | nodeStack.push(mRoot);
|
---|
[482] | 2865 |
|
---|
[463] | 2866 | int mask = rand();
|
---|
[482] | 2867 |
|
---|
| 2868 | while (!nodeStack.empty())
|
---|
[463] | 2869 | {
|
---|
| 2870 | BspNode *node = nodeStack.top();
|
---|
| 2871 | nodeStack.pop();
|
---|
[482] | 2872 |
|
---|
| 2873 | if (node->IsLeaf())
|
---|
[463] | 2874 | {
|
---|
| 2875 | return dynamic_cast<BspLeaf *>(node);
|
---|
[482] | 2876 | }
|
---|
| 2877 | else
|
---|
[463] | 2878 | {
|
---|
| 2879 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 2880 | BspNode *next;
|
---|
[503] | 2881 | BspNodeGeometry geom;
|
---|
[482] | 2882 |
|
---|
[463] | 2883 | // todo: not very efficient: constructs full cell everytime
|
---|
[498] | 2884 | ConstructGeometry(interior, geom);
|
---|
[463] | 2885 |
|
---|
[503] | 2886 | const int cf =
|
---|
[678] | 2887 | Polygon3::ClassifyPlane(geom.GetPolys(), halfspace, mEpsilon);
|
---|
[463] | 2888 |
|
---|
| 2889 | if (cf == Polygon3::BACK_SIDE)
|
---|
| 2890 | next = interior->GetFront();
|
---|
| 2891 | else
|
---|
| 2892 | if (cf == Polygon3::FRONT_SIDE)
|
---|
| 2893 | next = interior->GetFront();
|
---|
[482] | 2894 | else
|
---|
[463] | 2895 | {
|
---|
| 2896 | // random decision
|
---|
| 2897 | if (mask & 1)
|
---|
| 2898 | next = interior->GetBack();
|
---|
| 2899 | else
|
---|
| 2900 | next = interior->GetFront();
|
---|
| 2901 | mask = mask >> 1;
|
---|
| 2902 | }
|
---|
| 2903 |
|
---|
| 2904 | nodeStack.push(next);
|
---|
| 2905 | }
|
---|
| 2906 | }
|
---|
[482] | 2907 |
|
---|
[463] | 2908 | return NULL;
|
---|
| 2909 | }
|
---|
| 2910 |
|
---|
| 2911 | BspLeaf *VspBspTree::GetRandomLeaf(const bool onlyUnmailed)
|
---|
| 2912 | {
|
---|
| 2913 | stack<BspNode *> nodeStack;
|
---|
[482] | 2914 |
|
---|
[463] | 2915 | nodeStack.push(mRoot);
|
---|
| 2916 |
|
---|
| 2917 | int mask = rand();
|
---|
[482] | 2918 |
|
---|
| 2919 | while (!nodeStack.empty())
|
---|
[463] | 2920 | {
|
---|
| 2921 | BspNode *node = nodeStack.top();
|
---|
| 2922 | nodeStack.pop();
|
---|
[482] | 2923 |
|
---|
| 2924 | if (node->IsLeaf())
|
---|
[463] | 2925 | {
|
---|
| 2926 | if ( (!onlyUnmailed || !node->Mailed()) )
|
---|
| 2927 | return dynamic_cast<BspLeaf *>(node);
|
---|
| 2928 | }
|
---|
[482] | 2929 | else
|
---|
[463] | 2930 | {
|
---|
| 2931 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 2932 |
|
---|
| 2933 | // random decision
|
---|
| 2934 | if (mask & 1)
|
---|
| 2935 | nodeStack.push(interior->GetBack());
|
---|
| 2936 | else
|
---|
| 2937 | nodeStack.push(interior->GetFront());
|
---|
| 2938 |
|
---|
| 2939 | mask = mask >> 1;
|
---|
| 2940 | }
|
---|
| 2941 | }
|
---|
[482] | 2942 |
|
---|
[463] | 2943 | return NULL;
|
---|
| 2944 | }
|
---|
| 2945 |
|
---|
| 2946 | int VspBspTree::ComputePvsSize(const RayInfoContainer &rays) const
|
---|
| 2947 | {
|
---|
| 2948 | int pvsSize = 0;
|
---|
| 2949 |
|
---|
| 2950 | RayInfoContainer::const_iterator rit, rit_end = rays.end();
|
---|
| 2951 |
|
---|
| 2952 | Intersectable::NewMail();
|
---|
| 2953 |
|
---|
| 2954 | for (rit = rays.begin(); rit != rays.end(); ++ rit)
|
---|
| 2955 | {
|
---|
| 2956 | VssRay *ray = (*rit).mRay;
|
---|
[482] | 2957 |
|
---|
[463] | 2958 | if (ray->mOriginObject)
|
---|
| 2959 | {
|
---|
| 2960 | if (!ray->mOriginObject->Mailed())
|
---|
| 2961 | {
|
---|
| 2962 | ray->mOriginObject->Mail();
|
---|
| 2963 | ++ pvsSize;
|
---|
| 2964 | }
|
---|
| 2965 | }
|
---|
| 2966 | if (ray->mTerminationObject)
|
---|
| 2967 | {
|
---|
| 2968 | if (!ray->mTerminationObject->Mailed())
|
---|
| 2969 | {
|
---|
| 2970 | ray->mTerminationObject->Mail();
|
---|
| 2971 | ++ pvsSize;
|
---|
| 2972 | }
|
---|
| 2973 | }
|
---|
| 2974 | }
|
---|
| 2975 |
|
---|
| 2976 | return pvsSize;
|
---|
| 2977 | }
|
---|
| 2978 |
|
---|
| 2979 | float VspBspTree::GetEpsilon() const
|
---|
| 2980 | {
|
---|
| 2981 | return mEpsilon;
|
---|
| 2982 | }
|
---|
| 2983 |
|
---|
| 2984 |
|
---|
| 2985 | int VspBspTree::SplitPolygons(const Plane3 &plane,
|
---|
[482] | 2986 | PolygonContainer &polys,
|
---|
| 2987 | PolygonContainer &frontPolys,
|
---|
| 2988 | PolygonContainer &backPolys,
|
---|
[463] | 2989 | PolygonContainer &coincident) const
|
---|
| 2990 | {
|
---|
| 2991 | int splits = 0;
|
---|
| 2992 |
|
---|
[574] | 2993 | PolygonContainer::const_iterator it, it_end = polys.end();
|
---|
| 2994 |
|
---|
| 2995 | for (it = polys.begin(); it != polys.end(); ++ it)
|
---|
[463] | 2996 | {
|
---|
[574] | 2997 | Polygon3 *poly = *it;
|
---|
[463] | 2998 |
|
---|
| 2999 | // classify polygon
|
---|
| 3000 | const int cf = poly->ClassifyPlane(plane, mEpsilon);
|
---|
| 3001 |
|
---|
| 3002 | switch (cf)
|
---|
| 3003 | {
|
---|
| 3004 | case Polygon3::COINCIDENT:
|
---|
| 3005 | coincident.push_back(poly);
|
---|
[482] | 3006 | break;
|
---|
| 3007 | case Polygon3::FRONT_SIDE:
|
---|
[463] | 3008 | frontPolys.push_back(poly);
|
---|
| 3009 | break;
|
---|
| 3010 | case Polygon3::BACK_SIDE:
|
---|
| 3011 | backPolys.push_back(poly);
|
---|
| 3012 | break;
|
---|
| 3013 | case Polygon3::SPLIT:
|
---|
| 3014 | backPolys.push_back(poly);
|
---|
| 3015 | frontPolys.push_back(poly);
|
---|
| 3016 | ++ splits;
|
---|
| 3017 | break;
|
---|
| 3018 | default:
|
---|
| 3019 | Debug << "SHOULD NEVER COME HERE\n";
|
---|
| 3020 | break;
|
---|
| 3021 | }
|
---|
| 3022 | }
|
---|
| 3023 |
|
---|
| 3024 | return splits;
|
---|
| 3025 | }
|
---|
[466] | 3026 |
|
---|
| 3027 |
|
---|
[469] | 3028 | int VspBspTree::CastLineSegment(const Vector3 &origin,
|
---|
| 3029 | const Vector3 &termination,
|
---|
| 3030 | vector<ViewCell *> &viewcells)
|
---|
[466] | 3031 | {
|
---|
[469] | 3032 | int hits = 0;
|
---|
[600] | 3033 | stack<BspRayTraversalData> tQueue;
|
---|
[482] | 3034 |
|
---|
[469] | 3035 | float mint = 0.0f, maxt = 1.0f;
|
---|
[482] | 3036 |
|
---|
[469] | 3037 | Intersectable::NewMail();
|
---|
[600] | 3038 | ViewCell::NewMail();
|
---|
[482] | 3039 |
|
---|
[469] | 3040 | Vector3 entp = origin;
|
---|
| 3041 | Vector3 extp = termination;
|
---|
[482] | 3042 |
|
---|
[469] | 3043 | BspNode *node = mRoot;
|
---|
| 3044 | BspNode *farChild = NULL;
|
---|
[482] | 3045 |
|
---|
[485] | 3046 | float t;
|
---|
[666] | 3047 | const float thresh = 1 ? 1e-6f : 0.0f;
|
---|
| 3048 |
|
---|
[482] | 3049 | while (1)
|
---|
[469] | 3050 | {
|
---|
[482] | 3051 | if (!node->IsLeaf())
|
---|
[469] | 3052 | {
|
---|
| 3053 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
[482] | 3054 |
|
---|
[469] | 3055 | Plane3 splitPlane = in->GetPlane();
|
---|
[485] | 3056 |
|
---|
[666] | 3057 | const int entSide = splitPlane.Side(entp, thresh);
|
---|
| 3058 | const int extSide = splitPlane.Side(extp, thresh);
|
---|
[482] | 3059 |
|
---|
[485] | 3060 | if (entSide < 0)
|
---|
[469] | 3061 | {
|
---|
[666] | 3062 | node = in->GetBack();
|
---|
| 3063 |
|
---|
| 3064 | // plane does not split ray => no far child
|
---|
| 3065 | if (extSide <= 0)
|
---|
| 3066 | continue;
|
---|
| 3067 |
|
---|
| 3068 | farChild = in->GetFront(); // plane splits ray
|
---|
[485] | 3069 | }
|
---|
| 3070 | else if (entSide > 0)
|
---|
[469] | 3071 | {
|
---|
[666] | 3072 | node = in->GetFront();
|
---|
[482] | 3073 |
|
---|
[666] | 3074 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
| 3075 | continue;
|
---|
[482] | 3076 |
|
---|
| 3077 | farChild = in->GetBack(); // plane splits ray
|
---|
[469] | 3078 | }
|
---|
[666] | 3079 | else // one of the ray end points on the plane
|
---|
[485] | 3080 | { // NOTE: what to do if ray is coincident with plane?
|
---|
| 3081 | if (extSide < 0)
|
---|
| 3082 | node = in->GetBack();
|
---|
[487] | 3083 | else
|
---|
[485] | 3084 | node = in->GetFront();
|
---|
[487] | 3085 |
|
---|
[485] | 3086 | continue; // no far child
|
---|
[469] | 3087 | }
|
---|
[482] | 3088 |
|
---|
[469] | 3089 | // push data for far child
|
---|
[600] | 3090 | tQueue.push(BspRayTraversalData(farChild, extp));
|
---|
[482] | 3091 |
|
---|
[469] | 3092 | // find intersection of ray segment with plane
|
---|
| 3093 | extp = splitPlane.FindIntersection(origin, extp, &t);
|
---|
[485] | 3094 | }
|
---|
| 3095 | else
|
---|
[469] | 3096 | {
|
---|
| 3097 | // reached leaf => intersection with view cell
|
---|
| 3098 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
[666] | 3099 | ViewCell *viewCell;
|
---|
| 3100 |
|
---|
| 3101 | viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
|
---|
| 3102 | //viewCell = leaf->GetViewCell();
|
---|
[482] | 3103 |
|
---|
[590] | 3104 | if (!viewCell->Mailed())
|
---|
[469] | 3105 | {
|
---|
[590] | 3106 | viewcells.push_back(viewCell);
|
---|
| 3107 | viewCell->Mail();
|
---|
[469] | 3108 | ++ hits;
|
---|
| 3109 | }
|
---|
[482] | 3110 |
|
---|
[469] | 3111 | //-- fetch the next far child from the stack
|
---|
[600] | 3112 | if (tQueue.empty())
|
---|
[469] | 3113 | break;
|
---|
[482] | 3114 |
|
---|
[469] | 3115 | entp = extp;
|
---|
[485] | 3116 |
|
---|
[600] | 3117 | BspRayTraversalData &s = tQueue.top();
|
---|
[482] | 3118 |
|
---|
[469] | 3119 | node = s.mNode;
|
---|
| 3120 | extp = s.mExitPoint;
|
---|
[482] | 3121 |
|
---|
[600] | 3122 | tQueue.pop();
|
---|
[469] | 3123 | }
|
---|
[466] | 3124 | }
|
---|
[487] | 3125 |
|
---|
[469] | 3126 | return hits;
|
---|
[466] | 3127 | }
|
---|
[478] | 3128 |
|
---|
[576] | 3129 |
|
---|
| 3130 |
|
---|
| 3131 |
|
---|
[485] | 3132 | int VspBspTree::TreeDistance(BspNode *n1, BspNode *n2) const
|
---|
[482] | 3133 | {
|
---|
| 3134 | std::deque<BspNode *> path1;
|
---|
| 3135 | BspNode *p1 = n1;
|
---|
[479] | 3136 |
|
---|
[482] | 3137 | // create path from node 1 to root
|
---|
| 3138 | while (p1)
|
---|
| 3139 | {
|
---|
| 3140 | if (p1 == n2) // second node on path
|
---|
| 3141 | return (int)path1.size();
|
---|
| 3142 |
|
---|
| 3143 | path1.push_front(p1);
|
---|
| 3144 | p1 = p1->GetParent();
|
---|
| 3145 | }
|
---|
| 3146 |
|
---|
| 3147 | int depth = n2->GetDepth();
|
---|
| 3148 | int d = depth;
|
---|
| 3149 |
|
---|
| 3150 | BspNode *p2 = n2;
|
---|
| 3151 |
|
---|
| 3152 | // compare with same depth
|
---|
| 3153 | while (1)
|
---|
| 3154 | {
|
---|
| 3155 | if ((d < (int)path1.size()) && (p2 == path1[d]))
|
---|
| 3156 | return (depth - d) + ((int)path1.size() - 1 - d);
|
---|
| 3157 |
|
---|
| 3158 | -- d;
|
---|
| 3159 | p2 = p2->GetParent();
|
---|
| 3160 | }
|
---|
| 3161 |
|
---|
| 3162 | return 0; // never come here
|
---|
| 3163 | }
|
---|
| 3164 |
|
---|
[580] | 3165 |
|
---|
[501] | 3166 | BspNode *VspBspTree::CollapseTree(BspNode *node, int &collapsed)
|
---|
[479] | 3167 | {
|
---|
[590] | 3168 | // TODO
|
---|
| 3169 | #if VC_HISTORY
|
---|
[495] | 3170 | if (node->IsLeaf())
|
---|
[479] | 3171 | return node;
|
---|
| 3172 |
|
---|
[492] | 3173 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 3174 |
|
---|
[501] | 3175 | BspNode *front = CollapseTree(interior->GetFront(), collapsed);
|
---|
| 3176 | BspNode *back = CollapseTree(interior->GetBack(), collapsed);
|
---|
[492] | 3177 |
|
---|
[479] | 3178 | if (front->IsLeaf() && back->IsLeaf())
|
---|
| 3179 | {
|
---|
| 3180 | BspLeaf *frontLeaf = dynamic_cast<BspLeaf *>(front);
|
---|
| 3181 | BspLeaf *backLeaf = dynamic_cast<BspLeaf *>(back);
|
---|
| 3182 |
|
---|
| 3183 | //-- collapse tree
|
---|
| 3184 | if (frontLeaf->GetViewCell() == backLeaf->GetViewCell())
|
---|
| 3185 | {
|
---|
| 3186 | BspViewCell *vc = frontLeaf->GetViewCell();
|
---|
| 3187 |
|
---|
| 3188 | BspLeaf *leaf = new BspLeaf(interior->GetParent(), vc);
|
---|
[489] | 3189 | leaf->SetTreeValid(frontLeaf->TreeValid());
|
---|
[482] | 3190 |
|
---|
[479] | 3191 | // replace a link from node's parent
|
---|
| 3192 | if (leaf->GetParent())
|
---|
| 3193 | leaf->GetParent()->ReplaceChildLink(node, leaf);
|
---|
[517] | 3194 | else
|
---|
| 3195 | mRoot = leaf;
|
---|
| 3196 |
|
---|
[501] | 3197 | ++ collapsed;
|
---|
[479] | 3198 | delete interior;
|
---|
| 3199 |
|
---|
| 3200 | return leaf;
|
---|
| 3201 | }
|
---|
| 3202 | }
|
---|
[590] | 3203 | #endif
|
---|
[495] | 3204 | return node;
|
---|
| 3205 | }
|
---|
| 3206 |
|
---|
| 3207 |
|
---|
[501] | 3208 | int VspBspTree::CollapseTree()
|
---|
[495] | 3209 | {
|
---|
[501] | 3210 | int collapsed = 0;
|
---|
[580] | 3211 | //TODO
|
---|
| 3212 | #if VC_HISTORY
|
---|
[501] | 3213 | (void)CollapseTree(mRoot, collapsed);
|
---|
[517] | 3214 |
|
---|
[485] | 3215 | // revalidate leaves
|
---|
[517] | 3216 | RepairViewCellsLeafLists();
|
---|
[580] | 3217 | #endif
|
---|
[501] | 3218 | return collapsed;
|
---|
[479] | 3219 | }
|
---|
| 3220 |
|
---|
| 3221 |
|
---|
[517] | 3222 | void VspBspTree::RepairViewCellsLeafLists()
|
---|
[492] | 3223 | {
|
---|
[590] | 3224 | // TODO
|
---|
| 3225 | #if VC_HISTORY
|
---|
[479] | 3226 | // list not valid anymore => clear
|
---|
[492] | 3227 | stack<BspNode *> nodeStack;
|
---|
| 3228 | nodeStack.push(mRoot);
|
---|
| 3229 |
|
---|
| 3230 | ViewCell::NewMail();
|
---|
| 3231 |
|
---|
| 3232 | while (!nodeStack.empty())
|
---|
| 3233 | {
|
---|
| 3234 | BspNode *node = nodeStack.top();
|
---|
| 3235 | nodeStack.pop();
|
---|
| 3236 |
|
---|
| 3237 | if (node->IsLeaf())
|
---|
| 3238 | {
|
---|
| 3239 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
| 3240 |
|
---|
| 3241 | BspViewCell *viewCell = leaf->GetViewCell();
|
---|
[590] | 3242 |
|
---|
[492] | 3243 | if (!viewCell->Mailed())
|
---|
| 3244 | {
|
---|
| 3245 | viewCell->mLeaves.clear();
|
---|
| 3246 | viewCell->Mail();
|
---|
| 3247 | }
|
---|
[580] | 3248 |
|
---|
[492] | 3249 | viewCell->mLeaves.push_back(leaf);
|
---|
[590] | 3250 |
|
---|
[492] | 3251 | }
|
---|
| 3252 | else
|
---|
| 3253 | {
|
---|
| 3254 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 3255 |
|
---|
| 3256 | nodeStack.push(interior->GetFront());
|
---|
| 3257 | nodeStack.push(interior->GetBack());
|
---|
| 3258 | }
|
---|
[479] | 3259 | }
|
---|
[590] | 3260 | // TODO
|
---|
| 3261 | #endif
|
---|
[479] | 3262 | }
|
---|
| 3263 |
|
---|
| 3264 |
|
---|
[532] | 3265 | typedef pair<BspNode *, BspNodeGeometry *> bspNodePair;
|
---|
| 3266 |
|
---|
[535] | 3267 |
|
---|
[532] | 3268 | int VspBspTree::CastBeam(Beam &beam)
|
---|
| 3269 | {
|
---|
| 3270 | stack<bspNodePair> nodeStack;
|
---|
| 3271 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
| 3272 | ConstructGeometry(mRoot, *rgeom);
|
---|
| 3273 |
|
---|
| 3274 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
| 3275 |
|
---|
| 3276 | ViewCell::NewMail();
|
---|
| 3277 |
|
---|
| 3278 | while (!nodeStack.empty())
|
---|
| 3279 | {
|
---|
| 3280 | BspNode *node = nodeStack.top().first;
|
---|
| 3281 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
| 3282 | nodeStack.pop();
|
---|
| 3283 |
|
---|
| 3284 | AxisAlignedBox3 box;
|
---|
[535] | 3285 | box.Initialize();
|
---|
| 3286 | geom->IncludeInBox(box);
|
---|
[532] | 3287 |
|
---|
[535] | 3288 | const int side = beam.ComputeIntersection(box);
|
---|
[532] | 3289 |
|
---|
| 3290 | switch (side)
|
---|
| 3291 | {
|
---|
| 3292 | case -1:
|
---|
[547] | 3293 | CollectViewCells(node, true, beam.mViewCells, true);
|
---|
[532] | 3294 | break;
|
---|
| 3295 | case 0:
|
---|
[535] | 3296 |
|
---|
[532] | 3297 | if (node->IsLeaf())
|
---|
| 3298 | {
|
---|
[535] | 3299 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
| 3300 |
|
---|
[532] | 3301 | if (!leaf->GetViewCell()->Mailed() && leaf->TreeValid())
|
---|
[535] | 3302 | {
|
---|
| 3303 | leaf->GetViewCell()->Mail();
|
---|
[532] | 3304 | beam.mViewCells.push_back(leaf->GetViewCell());
|
---|
[535] | 3305 | }
|
---|
[532] | 3306 | }
|
---|
| 3307 | else
|
---|
| 3308 | {
|
---|
| 3309 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
[535] | 3310 |
|
---|
[538] | 3311 | BspNode *first = interior->GetFront();
|
---|
| 3312 | BspNode *second = interior->GetBack();
|
---|
[535] | 3313 |
|
---|
| 3314 | BspNodeGeometry *firstGeom = new BspNodeGeometry();
|
---|
| 3315 | BspNodeGeometry *secondGeom = new BspNodeGeometry();
|
---|
| 3316 |
|
---|
[538] | 3317 | geom->SplitGeometry(*firstGeom,
|
---|
| 3318 | *secondGeom,
|
---|
| 3319 | interior->GetPlane(),
|
---|
| 3320 | mBox,
|
---|
[675] | 3321 | //0.0000001f);
|
---|
[538] | 3322 | mEpsilon);
|
---|
[535] | 3323 |
|
---|
[532] | 3324 | // decide on the order of the nodes
|
---|
| 3325 | if (DotProd(beam.mPlanes[0].mNormal,
|
---|
| 3326 | interior->GetPlane().mNormal) > 0)
|
---|
| 3327 | {
|
---|
| 3328 | swap(first, second);
|
---|
[535] | 3329 | swap(firstGeom, secondGeom);
|
---|
[532] | 3330 | }
|
---|
| 3331 |
|
---|
[535] | 3332 | nodeStack.push(bspNodePair(first, firstGeom));
|
---|
| 3333 | nodeStack.push(bspNodePair(second, secondGeom));
|
---|
[532] | 3334 | }
|
---|
[535] | 3335 |
|
---|
[532] | 3336 | break;
|
---|
[538] | 3337 | default:
|
---|
[532] | 3338 | // default: cull
|
---|
[538] | 3339 | break;
|
---|
[532] | 3340 | }
|
---|
[538] | 3341 |
|
---|
[532] | 3342 | DEL_PTR(geom);
|
---|
[535] | 3343 |
|
---|
[532] | 3344 | }
|
---|
| 3345 |
|
---|
[538] | 3346 | return (int)beam.mViewCells.size();
|
---|
[532] | 3347 | }
|
---|
| 3348 |
|
---|
| 3349 |
|
---|
[485] | 3350 | void VspBspTree::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
[478] | 3351 | {
|
---|
[485] | 3352 | mViewCellsManager = vcm;
|
---|
| 3353 | }
|
---|
| 3354 |
|
---|
| 3355 |
|
---|
[580] | 3356 | int VspBspTree::CollectMergeCandidates(const vector<BspLeaf *> leaves,
|
---|
| 3357 | vector<MergeCandidate> &candidates)
|
---|
[485] | 3358 | {
|
---|
[478] | 3359 | BspLeaf::NewMail();
|
---|
[508] | 3360 |
|
---|
[478] | 3361 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
| 3362 |
|
---|
[580] | 3363 | int numCandidates = 0;
|
---|
[509] | 3364 |
|
---|
[478] | 3365 | // find merge candidates and push them into queue
|
---|
| 3366 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 3367 | {
|
---|
| 3368 | BspLeaf *leaf = *it;
|
---|
[485] | 3369 |
|
---|
| 3370 | // the same leaves must not be part of two merge candidates
|
---|
| 3371 | leaf->Mail();
|
---|
[650] | 3372 |
|
---|
[485] | 3373 | vector<BspLeaf *> neighbors;
|
---|
[650] | 3374 | if (0)
|
---|
| 3375 | FindNeighbors(leaf, neighbors, true);
|
---|
| 3376 | else
|
---|
| 3377 | FindApproximateNeighbors(leaf, neighbors, true);
|
---|
[485] | 3378 | vector<BspLeaf *>::const_iterator nit, nit_end = neighbors.end();
|
---|
| 3379 |
|
---|
| 3380 | // TODO: test if at least one ray goes from one leaf to the other
|
---|
| 3381 | for (nit = neighbors.begin(); nit != nit_end; ++ nit)
|
---|
[508] | 3382 | {
|
---|
| 3383 | if ((*nit)->GetViewCell() != leaf->GetViewCell())
|
---|
[509] | 3384 | {
|
---|
[580] | 3385 | MergeCandidate mc(leaf->GetViewCell(), (*nit)->GetViewCell());
|
---|
[564] | 3386 |
|
---|
[676] | 3387 | // dont't merge view cells if they are empty and not front and back leaf of the same parent
|
---|
| 3388 | // in the tree?
|
---|
| 3389 | if (mEmptyViewCellsMergeAllowed ||
|
---|
| 3390 | (!leaf->GetViewCell()->GetPvs().Empty() && ! (*nit)->GetViewCell()->GetPvs().Empty()) ||
|
---|
| 3391 | leaf->IsSibling(*nit))
|
---|
| 3392 | {
|
---|
| 3393 | candidates.push_back(mc);
|
---|
| 3394 | }
|
---|
| 3395 |
|
---|
[580] | 3396 | ++ numCandidates;
|
---|
| 3397 | if ((numCandidates % 1000) == 0)
|
---|
[566] | 3398 | {
|
---|
[580] | 3399 | cout << "collected " << numCandidates << " merge candidates" << endl;
|
---|
[566] | 3400 | }
|
---|
[509] | 3401 | }
|
---|
[485] | 3402 | }
|
---|
| 3403 | }
|
---|
| 3404 |
|
---|
[580] | 3405 | Debug << "merge queue: " << (int)candidates.size() << endl;
|
---|
| 3406 | Debug << "leaves in queue: " << numCandidates << endl;
|
---|
| 3407 |
|
---|
[508] | 3408 |
|
---|
[485] | 3409 | return (int)leaves.size();
|
---|
| 3410 | }
|
---|
| 3411 |
|
---|
| 3412 |
|
---|
[580] | 3413 | int VspBspTree::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 3414 | vector<MergeCandidate> &candidates)
|
---|
[485] | 3415 | {
|
---|
[547] | 3416 | ViewCell::NewMail();
|
---|
[503] | 3417 | long startTime = GetTime();
|
---|
[574] | 3418 |
|
---|
[485] | 3419 | map<BspLeaf *, vector<BspLeaf*> > neighborMap;
|
---|
[574] | 3420 | ViewCellContainer::const_iterator iit;
|
---|
[485] | 3421 |
|
---|
[503] | 3422 | int numLeaves = 0;
|
---|
[485] | 3423 |
|
---|
| 3424 | BspLeaf::NewMail();
|
---|
| 3425 |
|
---|
[574] | 3426 | for (int i = 0; i < (int)rays.size(); ++ i)
|
---|
[485] | 3427 | {
|
---|
[574] | 3428 | VssRay *ray = rays[i];
|
---|
[547] | 3429 |
|
---|
[485] | 3430 | // traverse leaves stored in the rays and compare and
|
---|
| 3431 | // merge consecutive leaves (i.e., the neighbors in the tree)
|
---|
[574] | 3432 | if (ray->mViewCells.size() < 2)
|
---|
[485] | 3433 | continue;
|
---|
[580] | 3434 | //TODO viewcellhierarchy
|
---|
[574] | 3435 | iit = ray->mViewCells.begin();
|
---|
| 3436 | BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*(iit ++));
|
---|
[580] | 3437 | BspLeaf *leaf = bspVc->mLeaf;
|
---|
[485] | 3438 |
|
---|
| 3439 | // traverse intersections
|
---|
[489] | 3440 | // consecutive leaves are neighbors => add them to queue
|
---|
[574] | 3441 | for (; iit != ray->mViewCells.end(); ++ iit)
|
---|
[485] | 3442 | {
|
---|
[489] | 3443 | // next pair
|
---|
| 3444 | BspLeaf *prevLeaf = leaf;
|
---|
[574] | 3445 | bspVc = dynamic_cast<BspViewCell *>(*iit);
|
---|
[580] | 3446 | leaf = bspVc->mLeaf;
|
---|
[489] | 3447 |
|
---|
[508] | 3448 | // view space not valid or same view cell
|
---|
| 3449 | if (!leaf->TreeValid() || !prevLeaf->TreeValid() ||
|
---|
| 3450 | (leaf->GetViewCell() == prevLeaf->GetViewCell()))
|
---|
[489] | 3451 | continue;
|
---|
| 3452 |
|
---|
[580] | 3453 | vector<BspLeaf *> &neighbors = neighborMap[leaf];
|
---|
[485] | 3454 |
|
---|
| 3455 | bool found = false;
|
---|
[478] | 3456 |
|
---|
[485] | 3457 | // both leaves inserted in queue already =>
|
---|
| 3458 | // look if double pair already exists
|
---|
| 3459 | if (leaf->Mailed() && prevLeaf->Mailed())
|
---|
[478] | 3460 | {
|
---|
[485] | 3461 | vector<BspLeaf *>::const_iterator it, it_end = neighbors.end();
|
---|
| 3462 |
|
---|
| 3463 | for (it = neighbors.begin(); !found && (it != it_end); ++ it)
|
---|
| 3464 | if (*it == prevLeaf)
|
---|
| 3465 | found = true; // already in queue
|
---|
| 3466 | }
|
---|
[547] | 3467 |
|
---|
[485] | 3468 | if (!found)
|
---|
| 3469 | {
|
---|
[564] | 3470 | // this pair is not in map yet
|
---|
[485] | 3471 | // => insert into the neighbor map and the queue
|
---|
| 3472 | neighbors.push_back(prevLeaf);
|
---|
| 3473 | neighborMap[prevLeaf].push_back(leaf);
|
---|
[478] | 3474 |
|
---|
[485] | 3475 | leaf->Mail();
|
---|
| 3476 | prevLeaf->Mail();
|
---|
[547] | 3477 |
|
---|
[580] | 3478 | MergeCandidate mc(leaf->GetViewCell(), prevLeaf->GetViewCell());
|
---|
| 3479 |
|
---|
| 3480 | candidates.push_back(mc);
|
---|
[564] | 3481 |
|
---|
[580] | 3482 | if (((int)candidates.size() % 1000) == 0)
|
---|
[564] | 3483 | {
|
---|
[580] | 3484 | cout << "collected " << (int)candidates.size() << " merge candidates" << endl;
|
---|
[564] | 3485 | }
|
---|
[478] | 3486 | }
|
---|
[485] | 3487 | }
|
---|
| 3488 | }
|
---|
[564] | 3489 |
|
---|
[485] | 3490 | Debug << "neighbormap size: " << (int)neighborMap.size() << endl;
|
---|
[580] | 3491 | Debug << "merge queue: " << (int)candidates.size() << endl;
|
---|
[503] | 3492 | Debug << "leaves in queue: " << numLeaves << endl;
|
---|
[485] | 3493 |
|
---|
[580] | 3494 |
|
---|
[503] | 3495 | //-- collect the leaves which haven't been found by ray casting
|
---|
[542] | 3496 | if (0)
|
---|
| 3497 | {
|
---|
[551] | 3498 | cout << "finding additional merge candidates using geometry" << endl;
|
---|
[542] | 3499 | vector<BspLeaf *> leaves;
|
---|
[547] | 3500 | CollectLeaves(leaves, true);
|
---|
[542] | 3501 | Debug << "found " << (int)leaves.size() << " new leaves" << endl << endl;
|
---|
[580] | 3502 | CollectMergeCandidates(leaves, candidates);
|
---|
[542] | 3503 | }
|
---|
[503] | 3504 |
|
---|
| 3505 | return numLeaves;
|
---|
[485] | 3506 | }
|
---|
| 3507 |
|
---|
| 3508 |
|
---|
| 3509 |
|
---|
| 3510 |
|
---|
[508] | 3511 | ViewCell *VspBspTree::GetViewCell(const Vector3 &point)
|
---|
[492] | 3512 | {
|
---|
| 3513 | if (mRoot == NULL)
|
---|
| 3514 | return NULL;
|
---|
| 3515 |
|
---|
| 3516 | stack<BspNode *> nodeStack;
|
---|
| 3517 | nodeStack.push(mRoot);
|
---|
| 3518 |
|
---|
| 3519 | ViewCell *viewcell = NULL;
|
---|
| 3520 |
|
---|
| 3521 | while (!nodeStack.empty()) {
|
---|
| 3522 | BspNode *node = nodeStack.top();
|
---|
| 3523 | nodeStack.pop();
|
---|
| 3524 |
|
---|
| 3525 | if (node->IsLeaf()) {
|
---|
| 3526 | viewcell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
| 3527 | break;
|
---|
| 3528 | } else {
|
---|
| 3529 |
|
---|
| 3530 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 3531 |
|
---|
| 3532 | // random decision
|
---|
| 3533 | if (interior->GetPlane().Side(point) < 0)
|
---|
| 3534 | nodeStack.push(interior->GetBack());
|
---|
| 3535 | else
|
---|
| 3536 | nodeStack.push(interior->GetFront());
|
---|
| 3537 | }
|
---|
| 3538 | }
|
---|
| 3539 |
|
---|
| 3540 | return viewcell;
|
---|
| 3541 | }
|
---|
| 3542 |
|
---|
| 3543 |
|
---|
[487] | 3544 | bool VspBspTree::ViewPointValid(const Vector3 &viewPoint) const
|
---|
| 3545 | {
|
---|
| 3546 | BspNode *node = mRoot;
|
---|
[485] | 3547 |
|
---|
[487] | 3548 | while (1)
|
---|
| 3549 | {
|
---|
| 3550 | // early exit
|
---|
| 3551 | if (node->TreeValid())
|
---|
| 3552 | return true;
|
---|
| 3553 |
|
---|
| 3554 | if (node->IsLeaf())
|
---|
| 3555 | return false;
|
---|
| 3556 |
|
---|
| 3557 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
[490] | 3558 |
|
---|
| 3559 | if (in->GetPlane().Side(viewPoint) <= 0)
|
---|
[487] | 3560 | {
|
---|
| 3561 | node = in->GetBack();
|
---|
| 3562 | }
|
---|
| 3563 | else
|
---|
| 3564 | {
|
---|
| 3565 | node = in->GetFront();
|
---|
| 3566 | }
|
---|
| 3567 | }
|
---|
| 3568 |
|
---|
| 3569 | // should never come here
|
---|
| 3570 | return false;
|
---|
| 3571 | }
|
---|
| 3572 |
|
---|
| 3573 |
|
---|
| 3574 | void VspBspTree::PropagateUpValidity(BspNode *node)
|
---|
| 3575 | {
|
---|
[574] | 3576 | const bool isValid = node->TreeValid();
|
---|
| 3577 |
|
---|
| 3578 | // propagative up invalid flag until only invalid nodes exist over this node
|
---|
| 3579 | if (!isValid)
|
---|
[487] | 3580 | {
|
---|
[574] | 3581 | while (!node->IsRoot() && node->GetParent()->TreeValid())
|
---|
| 3582 | {
|
---|
| 3583 | node = node->GetParent();
|
---|
| 3584 | node->SetTreeValid(false);
|
---|
| 3585 | }
|
---|
[487] | 3586 | }
|
---|
[574] | 3587 | else
|
---|
| 3588 | {
|
---|
| 3589 | // propagative up valid flag until one of the subtrees is invalid
|
---|
| 3590 | while (!node->IsRoot() && !node->TreeValid())
|
---|
| 3591 | {
|
---|
| 3592 | node = node->GetParent();
|
---|
| 3593 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 3594 |
|
---|
| 3595 | // the parent is valid iff both leaves are valid
|
---|
| 3596 | node->SetTreeValid(interior->GetBack()->TreeValid() &&
|
---|
| 3597 | interior->GetFront()->TreeValid());
|
---|
| 3598 | }
|
---|
| 3599 | }
|
---|
[487] | 3600 | }
|
---|
| 3601 |
|
---|
[508] | 3602 |
|
---|
| 3603 | bool VspBspTree::Export(ofstream &stream)
|
---|
[503] | 3604 | {
|
---|
[508] | 3605 | ExportNode(mRoot, stream);
|
---|
[487] | 3606 |
|
---|
[503] | 3607 | return true;
|
---|
| 3608 | }
|
---|
| 3609 |
|
---|
| 3610 |
|
---|
[508] | 3611 | void VspBspTree::ExportNode(BspNode *node, ofstream &stream)
|
---|
| 3612 | {
|
---|
| 3613 | if (node->IsLeaf())
|
---|
[503] | 3614 | {
|
---|
[508] | 3615 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
[590] | 3616 | ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
|
---|
| 3617 |
|
---|
[508] | 3618 | int id = -1;
|
---|
[590] | 3619 | if (viewCell != mOutOfBoundsCell)
|
---|
| 3620 | id = viewCell->GetId();
|
---|
[503] | 3621 |
|
---|
[508] | 3622 | stream << "<Leaf viewCellId=\"" << id << "\" />" << endl;
|
---|
[503] | 3623 | }
|
---|
[508] | 3624 | else
|
---|
[503] | 3625 | {
|
---|
[508] | 3626 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 3627 |
|
---|
| 3628 | Plane3 plane = interior->GetPlane();
|
---|
| 3629 | stream << "<Interior plane=\"" << plane.mNormal.x << " "
|
---|
| 3630 | << plane.mNormal.y << " " << plane.mNormal.z << " "
|
---|
| 3631 | << plane.mD << "\">" << endl;
|
---|
[503] | 3632 |
|
---|
[508] | 3633 | ExportNode(interior->GetBack(), stream);
|
---|
| 3634 | ExportNode(interior->GetFront(), stream);
|
---|
[503] | 3635 |
|
---|
[508] | 3636 | stream << "</Interior>" << endl;
|
---|
[503] | 3637 | }
|
---|
| 3638 | }
|
---|