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