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