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