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