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