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