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