[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"
|
---|
[463] | 18 |
|
---|
[478] | 19 |
|
---|
[463] | 20 | //-- static members
|
---|
[482] | 21 | /** Evaluates split plane classification with respect to the plane's
|
---|
[463] | 22 | contribution for a minimum number of ray splits.
|
---|
| 23 | */
|
---|
| 24 | const float VspBspTree::sLeastRaySplitsTable[] = {0, 0, 1, 1, 0};
|
---|
[482] | 25 | /** Evaluates split plane classification with respect to the plane's
|
---|
[463] | 26 | contribution for balanced rays.
|
---|
| 27 | */
|
---|
| 28 | const float VspBspTree::sBalancedRaysTable[] = {1, -1, 0, 0, 0};
|
---|
| 29 |
|
---|
| 30 |
|
---|
[482] | 31 | int VspBspTree::sFrontId = 0;
|
---|
[463] | 32 | int VspBspTree::sBackId = 0;
|
---|
| 33 | int VspBspTree::sFrontAndBackId = 0;
|
---|
| 34 |
|
---|
[485] | 35 | float BspMergeCandidate::sOverallCost = 0;
|
---|
[463] | 36 |
|
---|
[491] | 37 |
|
---|
| 38 | /// Adds object to the pvs of the front and back node
|
---|
| 39 | inline void AddObject2Pvs(Intersectable *object,
|
---|
| 40 | const int side,
|
---|
| 41 | int &pvsBack,
|
---|
| 42 | int &pvsFront)
|
---|
| 43 | {
|
---|
| 44 | if (!object)
|
---|
| 45 | return;
|
---|
| 46 |
|
---|
| 47 | if (side <= 0)
|
---|
| 48 | {
|
---|
| 49 | if (!object->Mailed() && !object->Mailed(2))
|
---|
| 50 | {
|
---|
| 51 | ++ pvsBack;
|
---|
| 52 |
|
---|
| 53 | if (object->Mailed(1))
|
---|
| 54 | object->Mail(2);
|
---|
| 55 | else
|
---|
| 56 | object->Mail();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | if (side >= 0)
|
---|
| 61 | {
|
---|
| 62 | if (!object->Mailed(1) && !object->Mailed(2))
|
---|
| 63 | {
|
---|
| 64 | ++ pvsFront;
|
---|
| 65 |
|
---|
| 66 | if (object->Mailed())
|
---|
| 67 | object->Mail(2);
|
---|
| 68 | else
|
---|
| 69 | object->Mail(1);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
[463] | 75 | /****************************************************************/
|
---|
| 76 | /* class VspBspTree implementation */
|
---|
| 77 | /****************************************************************/
|
---|
| 78 |
|
---|
[482] | 79 | VspBspTree::VspBspTree():
|
---|
[463] | 80 | mRoot(NULL),
|
---|
[472] | 81 | mPvsUseArea(true),
|
---|
[478] | 82 | mCostNormalizer(Limits::Small),
|
---|
| 83 | mViewCellsManager(NULL),
|
---|
[485] | 84 | mStoreRays(false),
|
---|
[489] | 85 | mOnlyDrivingAxis(false),
|
---|
| 86 | mOutOfBoundsCell(NULL)
|
---|
[463] | 87 | {
|
---|
[486] | 88 | bool randomize = false;
|
---|
| 89 | environment->GetBoolValue("VspBspTree.Construction.randomize", randomize);
|
---|
| 90 | if (randomize)
|
---|
| 91 | Randomize(); // initialise random generator for heuristics
|
---|
[463] | 92 |
|
---|
| 93 | //-- termination criteria for autopartition
|
---|
| 94 | environment->GetIntValue("VspBspTree.Termination.maxDepth", mTermMaxDepth);
|
---|
| 95 | environment->GetIntValue("VspBspTree.Termination.minPvs", mTermMinPvs);
|
---|
| 96 | environment->GetIntValue("VspBspTree.Termination.minRays", mTermMinRays);
|
---|
[482] | 97 | environment->GetFloatValue("VspBspTree.Termination.minArea", mTermMinArea);
|
---|
[463] | 98 | environment->GetFloatValue("VspBspTree.Termination.maxRayContribution", mTermMaxRayContribution);
|
---|
| 99 | environment->GetFloatValue("VspBspTree.Termination.minAccRayLenght", mTermMinAccRayLength);
|
---|
[472] | 100 | environment->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
| 101 | environment->GetIntValue("VspBspTree.Termination.missTolerance", mTermMissTolerance);
|
---|
[463] | 102 |
|
---|
| 103 | //-- factors for bsp tree split plane heuristics
|
---|
| 104 | environment->GetFloatValue("VspBspTree.Factor.balancedRays", mBalancedRaysFactor);
|
---|
| 105 | environment->GetFloatValue("VspBspTree.Factor.pvs", mPvsFactor);
|
---|
| 106 | environment->GetFloatValue("VspBspTree.Termination.ct_div_ci", mCtDivCi);
|
---|
| 107 |
|
---|
[482] | 108 | //-- max cost ratio for early tree termination
|
---|
[472] | 109 | environment->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
[482] | 110 |
|
---|
[463] | 111 | //-- partition criteria
|
---|
| 112 | environment->GetIntValue("VspBspTree.maxPolyCandidates", mMaxPolyCandidates);
|
---|
| 113 | environment->GetIntValue("VspBspTree.maxRayCandidates", mMaxRayCandidates);
|
---|
| 114 | environment->GetIntValue("VspBspTree.splitPlaneStrategy", mSplitPlaneStrategy);
|
---|
| 115 |
|
---|
| 116 | environment->GetFloatValue("VspBspTree.Construction.epsilon", mEpsilon);
|
---|
| 117 | environment->GetIntValue("VspBspTree.maxTests", mMaxTests);
|
---|
| 118 |
|
---|
[478] | 119 | // maximum and minimum number of view cells
|
---|
| 120 | environment->GetIntValue("VspBspTree.Termination.maxViewCells", mMaxViewCells);
|
---|
| 121 | environment->GetIntValue("VspBspTree.PostProcess.minViewCells", mMergeMinViewCells);
|
---|
[480] | 122 | environment->GetFloatValue("VspBspTree.PostProcess.maxCostRatio", mMergeMaxCostRatio);
|
---|
[463] | 123 |
|
---|
[485] | 124 | environment->GetBoolValue("VspBspTree.splitUseOnlyDrivingAxis", mOnlyDrivingAxis);
|
---|
| 125 | environment->GetBoolValue("VspBspTree.PostProcess.useRaysForMerge", mUseRaysForMerge);
|
---|
[487] | 126 | environment->GetIntValue("ViewCells.maxPvs", mMaxPvs);
|
---|
[478] | 127 |
|
---|
[487] | 128 |
|
---|
[478] | 129 | //-- debug output
|
---|
[473] | 130 | Debug << "******* VSP BSP options ******** " << endl;
|
---|
| 131 | Debug << "max depth: " << mTermMaxDepth << endl;
|
---|
| 132 | Debug << "min PVS: " << mTermMinPvs << endl;
|
---|
| 133 | Debug << "min area: " << mTermMinArea << endl;
|
---|
| 134 | Debug << "min rays: " << mTermMinRays << endl;
|
---|
| 135 | Debug << "max ray contri: " << mTermMaxRayContribution << endl;
|
---|
| 136 | //Debug << "VSP BSP mininam accumulated ray lenght: ", mTermMinAccRayLength) << endl;
|
---|
| 137 | Debug << "max cost ratio: " << mTermMaxCostRatio << endl;
|
---|
| 138 | Debug << "miss tolerance: " << mTermMissTolerance << endl;
|
---|
| 139 | Debug << "max view cells: " << mMaxViewCells << endl;
|
---|
| 140 | Debug << "max polygon candidates: " << mMaxPolyCandidates << endl;
|
---|
| 141 | Debug << "max plane candidates: " << mMaxRayCandidates << endl;
|
---|
[486] | 142 | Debug << "randomize: " << randomize << endl;
|
---|
[482] | 143 |
|
---|
[463] | 144 | Debug << "Split plane strategy: ";
|
---|
| 145 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
[474] | 146 | {
|
---|
[463] | 147 | Debug << "random polygon ";
|
---|
[474] | 148 | }
|
---|
[463] | 149 | if (mSplitPlaneStrategy & AXIS_ALIGNED)
|
---|
[472] | 150 | {
|
---|
[463] | 151 | Debug << "axis aligned ";
|
---|
[472] | 152 | }
|
---|
[463] | 153 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
[472] | 154 | {
|
---|
[474] | 155 | mCostNormalizer += mLeastRaySplitsFactor;
|
---|
[463] | 156 | Debug << "least ray splits ";
|
---|
[472] | 157 | }
|
---|
[463] | 158 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
[472] | 159 | {
|
---|
[474] | 160 | mCostNormalizer += mBalancedRaysFactor;
|
---|
[463] | 161 | Debug << "balanced rays ";
|
---|
[472] | 162 | }
|
---|
[463] | 163 | if (mSplitPlaneStrategy & PVS)
|
---|
[472] | 164 | {
|
---|
[474] | 165 | mCostNormalizer += mPvsFactor;
|
---|
[463] | 166 | Debug << "pvs";
|
---|
[472] | 167 | }
|
---|
[482] | 168 |
|
---|
[489] | 169 |
|
---|
[480] | 170 | mSplitCandidates = new vector<SortableEntry>;
|
---|
[463] | 171 |
|
---|
| 172 | Debug << endl;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
[489] | 176 | BspViewCell *VspBspTree::GetOrCreateOutOfBoundsCell()
|
---|
| 177 | {
|
---|
| 178 | if (!mOutOfBoundsCell)
|
---|
| 179 | mOutOfBoundsCell = new BspViewCell();
|
---|
| 180 |
|
---|
| 181 | return mOutOfBoundsCell;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 |
|
---|
[482] | 185 | const BspTreeStatistics &VspBspTree::GetStatistics() const
|
---|
[463] | 186 | {
|
---|
| 187 | return mStat;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | VspBspTree::~VspBspTree()
|
---|
| 192 | {
|
---|
| 193 | DEL_PTR(mRoot);
|
---|
[480] | 194 | DEL_PTR(mSplitCandidates);
|
---|
[463] | 195 | }
|
---|
| 196 |
|
---|
[482] | 197 | int VspBspTree::AddMeshToPolygons(Mesh *mesh,
|
---|
| 198 | PolygonContainer &polys,
|
---|
[463] | 199 | MeshInstance *parent)
|
---|
| 200 | {
|
---|
| 201 | FaceContainer::const_iterator fi;
|
---|
[482] | 202 |
|
---|
[463] | 203 | // copy the face data to polygons
|
---|
| 204 | for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi)
|
---|
| 205 | {
|
---|
| 206 | Polygon3 *poly = new Polygon3((*fi), mesh);
|
---|
[482] | 207 |
|
---|
[463] | 208 | if (poly->Valid(mEpsilon))
|
---|
| 209 | {
|
---|
| 210 | poly->mParent = parent; // set parent intersectable
|
---|
| 211 | polys.push_back(poly);
|
---|
| 212 | }
|
---|
| 213 | else
|
---|
| 214 | DEL_PTR(poly);
|
---|
| 215 | }
|
---|
| 216 | return (int)mesh->mFaces.size();
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[482] | 219 | int VspBspTree::AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
| 220 | PolygonContainer &polys,
|
---|
[463] | 221 | int maxObjects)
|
---|
| 222 | {
|
---|
[482] | 223 | int limit = (maxObjects > 0) ?
|
---|
[463] | 224 | Min((int)viewCells.size(), maxObjects) : (int)viewCells.size();
|
---|
[482] | 225 |
|
---|
[463] | 226 | int polysSize = 0;
|
---|
| 227 |
|
---|
| 228 | for (int i = 0; i < limit; ++ i)
|
---|
| 229 | {
|
---|
| 230 | if (viewCells[i]->GetMesh()) // copy the mesh data to polygons
|
---|
| 231 | {
|
---|
| 232 | mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb
|
---|
[482] | 233 | polysSize +=
|
---|
| 234 | AddMeshToPolygons(viewCells[i]->GetMesh(),
|
---|
| 235 | polys,
|
---|
[463] | 236 | viewCells[i]);
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | return polysSize;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[482] | 243 | int VspBspTree::AddToPolygonSoup(const ObjectContainer &objects,
|
---|
| 244 | PolygonContainer &polys,
|
---|
[463] | 245 | int maxObjects)
|
---|
| 246 | {
|
---|
[482] | 247 | int limit = (maxObjects > 0) ?
|
---|
[463] | 248 | Min((int)objects.size(), maxObjects) : (int)objects.size();
|
---|
[482] | 249 |
|
---|
[463] | 250 | for (int i = 0; i < limit; ++i)
|
---|
| 251 | {
|
---|
| 252 | Intersectable *object = objects[i];//*it;
|
---|
| 253 | Mesh *mesh = NULL;
|
---|
| 254 |
|
---|
| 255 | switch (object->Type()) // extract the meshes
|
---|
| 256 | {
|
---|
| 257 | case Intersectable::MESH_INSTANCE:
|
---|
| 258 | mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
|
---|
| 259 | break;
|
---|
| 260 | case Intersectable::VIEW_CELL:
|
---|
| 261 | mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
|
---|
| 262 | break;
|
---|
| 263 | // TODO: handle transformed mesh instances
|
---|
| 264 | default:
|
---|
| 265 | Debug << "intersectable type not supported" << endl;
|
---|
| 266 | break;
|
---|
| 267 | }
|
---|
[482] | 268 |
|
---|
[463] | 269 | if (mesh) // copy the mesh data to polygons
|
---|
| 270 | {
|
---|
| 271 | mBox.Include(object->GetBox()); // add to BSP tree aabb
|
---|
[485] | 272 | AddMeshToPolygons(mesh, polys, NULL);
|
---|
[463] | 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | return (int)polys.size();
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[483] | 279 | void VspBspTree::Construct(const VssRayContainer &sampleRays,
|
---|
| 280 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
[463] | 281 | {
|
---|
| 282 | mStat.nodes = 1;
|
---|
| 283 | mBox.Initialize(); // initialise BSP tree bounding box
|
---|
[482] | 284 |
|
---|
[484] | 285 | if (forcedBoundingBox)
|
---|
| 286 | mBox = *forcedBoundingBox;
|
---|
| 287 |
|
---|
[463] | 288 | PolygonContainer polys;
|
---|
| 289 | RayInfoContainer *rays = new RayInfoContainer();
|
---|
| 290 |
|
---|
| 291 | VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
| 292 |
|
---|
| 293 | long startTime = GetTime();
|
---|
| 294 |
|
---|
[480] | 295 | cout << "Extracting polygons from rays ... ";
|
---|
[463] | 296 |
|
---|
| 297 | Intersectable::NewMail();
|
---|
| 298 |
|
---|
| 299 | //-- extract polygons intersected by the rays
|
---|
| 300 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
| 301 | {
|
---|
| 302 | VssRay *ray = *rit;
|
---|
[482] | 303 |
|
---|
[484] | 304 | if ((mBox.IsInside(ray->mTermination) || !forcedBoundingBox) &&
|
---|
| 305 | ray->mTerminationObject &&
|
---|
| 306 | !ray->mTerminationObject->Mailed())
|
---|
[463] | 307 | {
|
---|
| 308 | ray->mTerminationObject->Mail();
|
---|
| 309 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mTerminationObject);
|
---|
| 310 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
[484] | 311 | //-- compute bounding box
|
---|
| 312 | if (!forcedBoundingBox)
|
---|
| 313 | mBox.Include(ray->mTermination);
|
---|
[463] | 314 | }
|
---|
| 315 |
|
---|
[484] | 316 | if ((mBox.IsInside(ray->mOrigin) || !forcedBoundingBox) &&
|
---|
| 317 | ray->mOriginObject &&
|
---|
| 318 | !ray->mOriginObject->Mailed())
|
---|
[463] | 319 | {
|
---|
| 320 | ray->mOriginObject->Mail();
|
---|
| 321 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mOriginObject);
|
---|
| 322 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
[484] | 323 | //-- compute bounding box
|
---|
| 324 | if (!forcedBoundingBox)
|
---|
| 325 | mBox.Include(ray->mOrigin);
|
---|
[463] | 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[483] | 329 | //-- compute bounding box
|
---|
[484] | 330 | //if (!forcedBoundingBox) Polygon3::IncludeInBox(polys, mBox);
|
---|
[463] | 331 |
|
---|
| 332 | //-- store rays
|
---|
| 333 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
| 334 | {
|
---|
| 335 | VssRay *ray = *rit;
|
---|
[482] | 336 |
|
---|
[463] | 337 | float minT, maxT;
|
---|
| 338 |
|
---|
[483] | 339 | // TODO: not very efficient to implictly cast between rays types
|
---|
[463] | 340 | if (mBox.GetRaySegment(*ray, minT, maxT))
|
---|
| 341 | {
|
---|
| 342 | float len = ray->Length();
|
---|
[482] | 343 |
|
---|
| 344 | if (!len)
|
---|
[463] | 345 | len = Limits::Small;
|
---|
[482] | 346 |
|
---|
[463] | 347 | rays->push_back(RayInfo(ray, minT / len, maxT / len));
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[474] | 351 | mTermMinArea *= mBox.SurfaceArea(); // normalize
|
---|
[463] | 352 | mStat.polys = (int)polys.size();
|
---|
| 353 |
|
---|
[475] | 354 | cout << "finished" << endl;
|
---|
[463] | 355 |
|
---|
[482] | 356 | Debug << "\nPolygon extraction: " << (int)polys.size() << " polys extracted from "
|
---|
[475] | 357 | << (int)sampleRays.size() << " rays in "
|
---|
| 358 | << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl << endl;
|
---|
| 359 |
|
---|
[463] | 360 | Construct(polys, rays);
|
---|
| 361 |
|
---|
| 362 | // clean up polygons
|
---|
| 363 | CLEAR_CONTAINER(polys);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | void VspBspTree::Construct(const PolygonContainer &polys, RayInfoContainer *rays)
|
---|
| 367 | {
|
---|
[472] | 368 | VspBspTraversalStack tStack;
|
---|
[463] | 369 |
|
---|
| 370 | mRoot = new BspLeaf();
|
---|
| 371 |
|
---|
| 372 | // constrruct root node geometry
|
---|
| 373 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
| 374 | ConstructGeometry(mRoot, *geom);
|
---|
| 375 |
|
---|
[482] | 376 | VspBspTraversalData tData(mRoot,
|
---|
| 377 | new PolygonContainer(polys),
|
---|
[463] | 378 | 0,
|
---|
[482] | 379 | rays,
|
---|
| 380 | ComputePvsSize(*rays),
|
---|
| 381 | geom->GetArea(),
|
---|
[463] | 382 | geom);
|
---|
| 383 |
|
---|
| 384 | tStack.push(tData);
|
---|
| 385 |
|
---|
| 386 | mStat.Start();
|
---|
| 387 | cout << "Contructing vsp bsp tree ... ";
|
---|
| 388 |
|
---|
| 389 | long startTime = GetTime();
|
---|
[482] | 390 |
|
---|
[478] | 391 | while (!tStack.empty())
|
---|
[463] | 392 | {
|
---|
| 393 | tData = tStack.top();
|
---|
| 394 |
|
---|
| 395 | tStack.pop();
|
---|
[478] | 396 |
|
---|
[463] | 397 | // subdivide leaf node
|
---|
| 398 | BspNode *r = Subdivide(tStack, tData);
|
---|
| 399 |
|
---|
| 400 | if (r == mRoot)
|
---|
[482] | 401 | Debug << "VSP BSP tree construction time spent at root: "
|
---|
[475] | 402 | << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl << endl;
|
---|
[463] | 403 | }
|
---|
| 404 |
|
---|
| 405 | cout << "finished\n";
|
---|
| 406 |
|
---|
| 407 | mStat.Stop();
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[478] | 410 | bool VspBspTree::TerminationCriteriaMet(const VspBspTraversalData &data) const
|
---|
[463] | 411 | {
|
---|
[482] | 412 | return
|
---|
[463] | 413 | (((int)data.mRays->size() <= mTermMinRays) ||
|
---|
[473] | 414 | (data.mPvs <= mTermMinPvs) ||
|
---|
[463] | 415 | (data.mArea <= mTermMinArea) ||
|
---|
[485] | 416 | (mStat.Leaves() >= mMaxViewCells) ||
|
---|
[463] | 417 | // (data.GetAvgRayContribution() >= mTermMaxRayContribution) ||
|
---|
| 418 | (data.mDepth >= mTermMaxDepth));
|
---|
| 419 | }
|
---|
| 420 |
|
---|
[482] | 421 | BspNode *VspBspTree::Subdivide(VspBspTraversalStack &tStack,
|
---|
[463] | 422 | VspBspTraversalData &tData)
|
---|
| 423 | {
|
---|
[473] | 424 | BspNode *newNode = tData.mNode;
|
---|
| 425 |
|
---|
[478] | 426 | if (!TerminationCriteriaMet(tData))
|
---|
[473] | 427 | {
|
---|
| 428 | PolygonContainer coincident;
|
---|
[482] | 429 |
|
---|
[473] | 430 | VspBspTraversalData tFrontData;
|
---|
| 431 | VspBspTraversalData tBackData;
|
---|
| 432 |
|
---|
| 433 | // create new interior node and two leaf nodes
|
---|
| 434 | // or return leaf as it is (if maxCostRatio missed)
|
---|
| 435 | newNode = SubdivideNode(tData, tFrontData, tBackData, coincident);
|
---|
| 436 |
|
---|
| 437 | if (!newNode->IsLeaf()) //-- continue subdivision
|
---|
| 438 | {
|
---|
| 439 | // push the children on the stack
|
---|
| 440 | tStack.push(tFrontData);
|
---|
| 441 | tStack.push(tBackData);
|
---|
| 442 |
|
---|
| 443 | // delete old leaf node
|
---|
[482] | 444 | DEL_PTR(tData.mNode);
|
---|
[473] | 445 | }
|
---|
| 446 | }
|
---|
[482] | 447 |
|
---|
[478] | 448 | //-- terminate traversal and create new view cell
|
---|
[473] | 449 | if (newNode->IsLeaf())
|
---|
[463] | 450 | {
|
---|
[473] | 451 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode);
|
---|
[489] | 452 | BspViewCell *viewCell;
|
---|
[482] | 453 |
|
---|
[487] | 454 | if (!CheckValid(tData))
|
---|
| 455 | {
|
---|
| 456 | leaf->SetTreeValid(false);
|
---|
| 457 | PropagateUpValidity(leaf);
|
---|
[489] | 458 | // view cell for invalid view space
|
---|
| 459 | viewCell = GetOrCreateOutOfBoundsCell();
|
---|
[490] | 460 | ++ mStat.invalidLeaves;
|
---|
[487] | 461 | }
|
---|
[489] | 462 | else
|
---|
| 463 | {
|
---|
| 464 | // create new view cell for this leaf
|
---|
| 465 | viewCell = new BspViewCell();
|
---|
| 466 | }
|
---|
| 467 |
|
---|
[463] | 468 | leaf->SetViewCell(viewCell);
|
---|
[487] | 469 |
|
---|
| 470 | //-- update pvs
|
---|
| 471 | int conSamp = 0, sampCon = 0;
|
---|
| 472 | AddToPvs(leaf, *tData.mRays, conSamp, sampCon);
|
---|
| 473 |
|
---|
| 474 | mStat.contributingSamples += conSamp;
|
---|
| 475 | mStat.sampleContributions += sampCon;
|
---|
| 476 |
|
---|
| 477 | //-- store additional info
|
---|
[478] | 478 | if (mStoreRays)
|
---|
| 479 | {
|
---|
| 480 | RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
|
---|
| 481 | for (it = tData.mRays->begin(); it != it_end; ++ it)
|
---|
| 482 | leaf->mVssRays.push_back((*it).mRay);
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[469] | 485 | viewCell->mLeaves.push_back(leaf);
|
---|
[478] | 486 | viewCell->SetArea(tData.mArea);
|
---|
[485] | 487 | leaf->mArea = tData.mArea;
|
---|
[463] | 488 |
|
---|
[487] | 489 | EvaluateLeafStats(tData);
|
---|
[463] | 490 | }
|
---|
[482] | 491 |
|
---|
[473] | 492 | //-- cleanup
|
---|
[478] | 493 | tData.Clear();
|
---|
[463] | 494 |
|
---|
[472] | 495 | return newNode;
|
---|
[463] | 496 | }
|
---|
| 497 |
|
---|
[487] | 498 |
|
---|
[472] | 499 | BspNode *VspBspTree::SubdivideNode(VspBspTraversalData &tData,
|
---|
| 500 | VspBspTraversalData &frontData,
|
---|
| 501 | VspBspTraversalData &backData,
|
---|
| 502 | PolygonContainer &coincident)
|
---|
[463] | 503 | {
|
---|
| 504 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
[473] | 505 |
|
---|
[472] | 506 | int maxCostMisses = tData.mMaxCostMisses;
|
---|
| 507 |
|
---|
[463] | 508 | // select subdivision plane
|
---|
[472] | 509 | Plane3 splitPlane;
|
---|
| 510 | if (!SelectPlane(splitPlane, leaf, tData))
|
---|
| 511 | {
|
---|
| 512 | ++ maxCostMisses;
|
---|
[463] | 513 |
|
---|
[480] | 514 | if (maxCostMisses > mTermMissTolerance)
|
---|
[473] | 515 | {
|
---|
| 516 | // terminate branch because of max cost
|
---|
[482] | 517 | ++ mStat.maxCostNodes;
|
---|
[472] | 518 | return leaf;
|
---|
[474] | 519 | }
|
---|
[472] | 520 | }
|
---|
| 521 |
|
---|
[473] | 522 | mStat.nodes += 2;
|
---|
| 523 |
|
---|
| 524 | //-- subdivide further
|
---|
[482] | 525 | BspInterior *interior = new BspInterior(splitPlane);
|
---|
[472] | 526 |
|
---|
[463] | 527 | #ifdef _DEBUG
|
---|
| 528 | Debug << interior << endl;
|
---|
| 529 | #endif
|
---|
[482] | 530 |
|
---|
[473] | 531 | //-- the front and back traversal data is filled with the new values
|
---|
| 532 | frontData.mPolygons = new PolygonContainer();
|
---|
| 533 | frontData.mDepth = tData.mDepth + 1;
|
---|
| 534 | frontData.mRays = new RayInfoContainer();
|
---|
| 535 | frontData.mGeometry = new BspNodeGeometry();
|
---|
| 536 |
|
---|
| 537 | backData.mPolygons = new PolygonContainer();
|
---|
| 538 | backData.mDepth = tData.mDepth + 1;
|
---|
| 539 | backData.mRays = new RayInfoContainer();
|
---|
| 540 | backData.mGeometry = new BspNodeGeometry();
|
---|
| 541 |
|
---|
| 542 | // subdivide rays
|
---|
[482] | 543 | SplitRays(interior->GetPlane(),
|
---|
| 544 | *tData.mRays,
|
---|
| 545 | *frontData.mRays,
|
---|
[463] | 546 | *backData.mRays);
|
---|
[482] | 547 |
|
---|
[473] | 548 | // subdivide polygons
|
---|
[491] | 549 | SplitPolygons(interior->GetPlane(),
|
---|
| 550 | *tData.mPolygons,
|
---|
| 551 | *frontData.mPolygons,
|
---|
| 552 | *backData.mPolygons,
|
---|
| 553 | coincident);
|
---|
[463] | 554 |
|
---|
[482] | 555 |
|
---|
[473] | 556 | // how often was max cost ratio missed in this branch?
|
---|
[472] | 557 | frontData.mMaxCostMisses = maxCostMisses;
|
---|
| 558 | backData.mMaxCostMisses = maxCostMisses;
|
---|
| 559 |
|
---|
| 560 | // compute pvs
|
---|
[463] | 561 | frontData.mPvs = ComputePvsSize(*frontData.mRays);
|
---|
| 562 | backData.mPvs = ComputePvsSize(*backData.mRays);
|
---|
| 563 |
|
---|
| 564 | // split geometry and compute area
|
---|
| 565 | if (1)
|
---|
| 566 | {
|
---|
[482] | 567 | tData.mGeometry->SplitGeometry(*frontData.mGeometry,
|
---|
| 568 | *backData.mGeometry,
|
---|
[463] | 569 | interior->GetPlane(),
|
---|
| 570 | mBox,
|
---|
| 571 | mEpsilon);
|
---|
[482] | 572 |
|
---|
[474] | 573 | frontData.mArea = frontData.mGeometry->GetArea();
|
---|
| 574 | backData.mArea = backData.mGeometry->GetArea();
|
---|
[463] | 575 | }
|
---|
| 576 |
|
---|
| 577 | // compute accumulated ray length
|
---|
| 578 | //frontData.mAccRayLength = AccumulatedRayLength(*frontData.mRays);
|
---|
| 579 | //backData.mAccRayLength = AccumulatedRayLength(*backData.mRays);
|
---|
| 580 |
|
---|
| 581 | //-- create front and back leaf
|
---|
| 582 |
|
---|
| 583 | BspInterior *parent = leaf->GetParent();
|
---|
| 584 |
|
---|
| 585 | // replace a link from node's parent
|
---|
[487] | 586 | if (parent)
|
---|
[463] | 587 | {
|
---|
| 588 | parent->ReplaceChildLink(leaf, interior);
|
---|
| 589 | interior->SetParent(parent);
|
---|
| 590 | }
|
---|
| 591 | else // new root
|
---|
| 592 | {
|
---|
| 593 | mRoot = interior;
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | // and setup child links
|
---|
| 597 | interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior));
|
---|
[482] | 598 |
|
---|
[463] | 599 | frontData.mNode = interior->GetFront();
|
---|
| 600 | backData.mNode = interior->GetBack();
|
---|
[473] | 601 |
|
---|
[463] | 602 | //DEL_PTR(leaf);
|
---|
| 603 | return interior;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | void VspBspTree::AddToPvs(BspLeaf *leaf,
|
---|
[482] | 607 | const RayInfoContainer &rays,
|
---|
[463] | 608 | int &sampleContributions,
|
---|
| 609 | int &contributingSamples)
|
---|
| 610 | {
|
---|
| 611 | sampleContributions = 0;
|
---|
| 612 | contributingSamples = 0;
|
---|
| 613 |
|
---|
| 614 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
| 615 |
|
---|
| 616 | ViewCell *vc = leaf->GetViewCell();
|
---|
| 617 |
|
---|
| 618 | // add contributions from samples to the PVS
|
---|
| 619 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 620 | {
|
---|
| 621 | int contribution = 0;
|
---|
| 622 | VssRay *ray = (*it).mRay;
|
---|
[482] | 623 |
|
---|
[463] | 624 | if (ray->mTerminationObject)
|
---|
| 625 | contribution += vc->GetPvs().AddSample(ray->mTerminationObject);
|
---|
[482] | 626 |
|
---|
[463] | 627 | if (ray->mOriginObject)
|
---|
| 628 | contribution += vc->GetPvs().AddSample(ray->mOriginObject);
|
---|
| 629 |
|
---|
| 630 | if (contribution)
|
---|
| 631 | {
|
---|
| 632 | sampleContributions += contribution;
|
---|
| 633 | ++ contributingSamples;
|
---|
| 634 | }
|
---|
| 635 | //leaf->mVssRays.push_back(ray);
|
---|
| 636 | }
|
---|
| 637 | }
|
---|
| 638 |
|
---|
[480] | 639 | void VspBspTree::SortSplitCandidates(const RayInfoContainer &rays, const int axis)
|
---|
[463] | 640 | {
|
---|
[480] | 641 | mSplitCandidates->clear();
|
---|
[463] | 642 |
|
---|
[480] | 643 | int requestedSize = 2 * (int)(rays.size());
|
---|
| 644 | // creates a sorted split candidates array
|
---|
| 645 | if (mSplitCandidates->capacity() > 500000 &&
|
---|
| 646 | requestedSize < (int)(mSplitCandidates->capacity()/10) )
|
---|
| 647 | {
|
---|
[482] | 648 | delete mSplitCandidates;
|
---|
[480] | 649 | mSplitCandidates = new vector<SortableEntry>;
|
---|
| 650 | }
|
---|
[463] | 651 |
|
---|
[480] | 652 | mSplitCandidates->reserve(requestedSize);
|
---|
| 653 |
|
---|
| 654 | // insert all queries
|
---|
| 655 | for(RayInfoContainer::const_iterator ri = rays.begin(); ri < rays.end(); ++ ri)
|
---|
[473] | 656 | {
|
---|
[480] | 657 | bool positive = (*ri).mRay->HasPosDir(axis);
|
---|
| 658 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax,
|
---|
[482] | 659 | (*ri).ExtrapOrigin(axis), (*ri).mRay));
|
---|
[480] | 660 |
|
---|
| 661 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,
|
---|
[482] | 662 | (*ri).ExtrapTermination(axis), (*ri).mRay));
|
---|
[473] | 663 | }
|
---|
[480] | 664 |
|
---|
| 665 | stable_sort(mSplitCandidates->begin(), mSplitCandidates->end());
|
---|
[463] | 666 | }
|
---|
| 667 |
|
---|
[480] | 668 | float VspBspTree::BestCostRatioHeuristics(const RayInfoContainer &rays,
|
---|
| 669 | const AxisAlignedBox3 &box,
|
---|
| 670 | const int pvsSize,
|
---|
[481] | 671 | const int &axis,
|
---|
[480] | 672 | float &position)
|
---|
[463] | 673 | {
|
---|
[480] | 674 | int raysBack;
|
---|
| 675 | int raysFront;
|
---|
| 676 | int pvsBack;
|
---|
| 677 | int pvsFront;
|
---|
[463] | 678 |
|
---|
[480] | 679 | SortSplitCandidates(rays, axis);
|
---|
| 680 |
|
---|
[463] | 681 | // go through the lists, count the number of objects left and right
|
---|
| 682 | // and evaluate the following cost funcion:
|
---|
[480] | 683 | // C = ct_div_ci + (ql*rl + qr*rr)/queries
|
---|
| 684 |
|
---|
| 685 | int rl = 0, rr = (int)rays.size();
|
---|
| 686 | int pl = 0, pr = pvsSize;
|
---|
| 687 |
|
---|
[463] | 688 | float minBox = box.Min(axis);
|
---|
| 689 | float maxBox = box.Max(axis);
|
---|
[480] | 690 | float sizeBox = maxBox - minBox;
|
---|
| 691 |
|
---|
| 692 | float minBand = minBox + 0.1f*(maxBox - minBox);
|
---|
| 693 | float maxBand = minBox + 0.9f*(maxBox - minBox);
|
---|
| 694 |
|
---|
| 695 | float sum = rr*sizeBox;
|
---|
[463] | 696 | float minSum = 1e20f;
|
---|
| 697 |
|
---|
[480] | 698 | Intersectable::NewMail();
|
---|
| 699 |
|
---|
| 700 | // set all object as belonging to the fron pvs
|
---|
| 701 | for(RayInfoContainer::const_iterator ri = rays.begin(); ri != rays.end(); ++ ri)
|
---|
[463] | 702 | {
|
---|
[480] | 703 | if ((*ri).mRay->IsActive())
|
---|
[463] | 704 | {
|
---|
[480] | 705 | Intersectable *object = (*ri).mRay->mTerminationObject;
|
---|
| 706 |
|
---|
| 707 | if (object)
|
---|
[482] | 708 | {
|
---|
[480] | 709 | if (!object->Mailed())
|
---|
| 710 | {
|
---|
| 711 | object->Mail();
|
---|
| 712 | object->mCounter = 1;
|
---|
| 713 | }
|
---|
| 714 | else
|
---|
| 715 | ++ object->mCounter;
|
---|
[482] | 716 | }
|
---|
[463] | 717 | }
|
---|
[480] | 718 | }
|
---|
| 719 |
|
---|
| 720 | Intersectable::NewMail();
|
---|
| 721 |
|
---|
| 722 | for (vector<SortableEntry>::const_iterator ci = mSplitCandidates->begin();
|
---|
| 723 | ci < mSplitCandidates->end(); ++ ci)
|
---|
| 724 | {
|
---|
| 725 | VssRay *ray;
|
---|
| 726 |
|
---|
| 727 | switch ((*ci).type)
|
---|
[463] | 728 | {
|
---|
[480] | 729 | case SortableEntry::ERayMin:
|
---|
| 730 | {
|
---|
| 731 | ++ rl;
|
---|
[482] | 732 | ray = (VssRay *) (*ci).ray;
|
---|
| 733 |
|
---|
[480] | 734 | Intersectable *object = ray->mTerminationObject;
|
---|
[482] | 735 |
|
---|
[480] | 736 | if (object && !object->Mailed())
|
---|
| 737 | {
|
---|
| 738 | object->Mail();
|
---|
| 739 | ++ pl;
|
---|
| 740 | }
|
---|
| 741 | break;
|
---|
| 742 | }
|
---|
| 743 | case SortableEntry::ERayMax:
|
---|
| 744 | {
|
---|
| 745 | -- rr;
|
---|
[482] | 746 | ray = (VssRay *) (*ci).ray;
|
---|
[463] | 747 |
|
---|
[480] | 748 | Intersectable *object = ray->mTerminationObject;
|
---|
| 749 |
|
---|
| 750 | if (object)
|
---|
| 751 | {
|
---|
| 752 | if (-- object->mCounter == 0)
|
---|
[482] | 753 | -- pr;
|
---|
[480] | 754 | }
|
---|
| 755 |
|
---|
| 756 | break;
|
---|
| 757 | }
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | // Note: sufficient to compare size of bounding boxes of front and back side?
|
---|
| 761 | if ((*ci).value > minBand && (*ci).value < maxBand)
|
---|
| 762 | {
|
---|
| 763 | sum = pl*((*ci).value - minBox) + pr*(maxBox - (*ci).value);
|
---|
| 764 |
|
---|
| 765 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
| 766 | // cout<<"cost= "<<sum<<endl;
|
---|
| 767 |
|
---|
| 768 | if (sum < minSum)
|
---|
[463] | 769 | {
|
---|
| 770 | minSum = sum;
|
---|
| 771 | position = (*ci).value;
|
---|
| 772 |
|
---|
[480] | 773 | raysBack = rl;
|
---|
| 774 | raysFront = rr;
|
---|
| 775 |
|
---|
| 776 | pvsBack = pl;
|
---|
| 777 | pvsFront = pr;
|
---|
| 778 |
|
---|
[463] | 779 | }
|
---|
| 780 | }
|
---|
| 781 | }
|
---|
| 782 |
|
---|
[480] | 783 | float oldCost = (float)pvsSize;
|
---|
| 784 | float newCost = mCtDivCi + minSum / sizeBox;
|
---|
| 785 | float ratio = newCost / oldCost;
|
---|
[463] | 786 |
|
---|
[480] | 787 | //Debug << "costRatio=" << ratio << " pos=" << position << " t=" << (position - minBox) / (maxBox - minBox)
|
---|
| 788 | // <<"\t q=(" << queriesBack << "," << queriesFront << ")\t r=(" << raysBack << "," << raysFront << ")" << endl;
|
---|
| 789 |
|
---|
| 790 | return ratio;
|
---|
[463] | 791 | }
|
---|
| 792 |
|
---|
[480] | 793 |
|
---|
[482] | 794 | float VspBspTree::SelectAxisAlignedPlane(Plane3 &plane,
|
---|
[491] | 795 | const VspBspTraversalData &tData,
|
---|
| 796 | int &bestAxis)
|
---|
[463] | 797 | {
|
---|
| 798 | AxisAlignedBox3 box;
|
---|
| 799 | box.Initialize();
|
---|
[482] | 800 |
|
---|
[463] | 801 | // create bounding box of region
|
---|
[480] | 802 | RayInfoContainer::const_iterator ri, ri_end = tData.mRays->end();
|
---|
| 803 |
|
---|
| 804 | for(ri = tData.mRays->begin(); ri < ri_end; ++ ri)
|
---|
| 805 | box.Include((*ri).ExtrapTermination());
|
---|
| 806 |
|
---|
| 807 | const bool useCostHeuristics = false;
|
---|
[463] | 808 |
|
---|
[480] | 809 | //-- regular split
|
---|
| 810 | float nPosition[3];
|
---|
| 811 | float nCostRatio[3];
|
---|
[491] | 812 | bestAxis = -1;
|
---|
[480] | 813 |
|
---|
[482] | 814 | const int sAxis = box.Size().DrivingAxis();
|
---|
[480] | 815 |
|
---|
[481] | 816 | for (int axis = 0; axis < 3; ++ axis)
|
---|
[480] | 817 | {
|
---|
| 818 | if (!mOnlyDrivingAxis || axis == sAxis)
|
---|
[463] | 819 | {
|
---|
[480] | 820 | if (!useCostHeuristics)
|
---|
| 821 | {
|
---|
| 822 | nPosition[axis] = (box.Min()[axis] + box.Max()[axis]) * 0.5f;
|
---|
| 823 | Vector3 normal(0,0,0); normal[axis] = 1;
|
---|
| 824 |
|
---|
| 825 | nCostRatio[axis] = SplitPlaneCost(Plane3(normal, nPosition[axis]), tData);
|
---|
| 826 | }
|
---|
[481] | 827 | else
|
---|
| 828 | {
|
---|
[482] | 829 | nCostRatio[axis] =
|
---|
[481] | 830 | BestCostRatioHeuristics(*tData.mRays,
|
---|
| 831 | box,
|
---|
| 832 | tData.mPvs,
|
---|
| 833 | axis,
|
---|
| 834 | nPosition[axis]);
|
---|
| 835 | }
|
---|
| 836 |
|
---|
[480] | 837 | if (bestAxis == -1)
|
---|
| 838 | {
|
---|
| 839 | bestAxis = axis;
|
---|
| 840 | }
|
---|
| 841 |
|
---|
| 842 | else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
| 843 | {
|
---|
| 844 | /*Debug << "pvs front " << nPvsBack[axis]
|
---|
| 845 | << " pvs back " << nPvsFront[axis]
|
---|
| 846 | << " overall pvs " << leaf->GetPvsSize() << endl;*/
|
---|
| 847 | bestAxis = axis;
|
---|
| 848 | }
|
---|
| 849 |
|
---|
[463] | 850 | }
|
---|
| 851 | }
|
---|
| 852 |
|
---|
[480] | 853 | //-- assign best axis
|
---|
| 854 | Vector3 normal(0,0,0); normal[bestAxis] = 1;
|
---|
| 855 | plane = Plane3(normal, nPosition[bestAxis]);
|
---|
[491] | 856 |
|
---|
[480] | 857 | return nCostRatio[bestAxis];
|
---|
[463] | 858 | }
|
---|
| 859 |
|
---|
[480] | 860 |
|
---|
[491] | 861 | float VspBspTree::EvalCostRatio(const VspBspTraversalData &tData,
|
---|
| 862 | const AxisAlignedBox3 &box,
|
---|
| 863 | const int axis,
|
---|
| 864 | const float position,
|
---|
| 865 | int &raysBack,
|
---|
| 866 | int &raysFront,
|
---|
| 867 | int &pvsBack,
|
---|
| 868 | int &pvsFront)
|
---|
| 869 | {
|
---|
| 870 | raysBack = 0;
|
---|
| 871 | raysFront = 0;
|
---|
| 872 | pvsFront = 0;
|
---|
| 873 | pvsBack = 0;
|
---|
| 874 |
|
---|
| 875 | Intersectable::NewMail(3);
|
---|
| 876 |
|
---|
| 877 | // eval pvs size
|
---|
| 878 | const int pvsSize = tData.mPvs;
|
---|
| 879 |
|
---|
| 880 | // create unique ids for pvs heuristics
|
---|
| 881 | GenerateUniqueIdsForPvs();
|
---|
| 882 |
|
---|
| 883 | // this is the main ray classification loop!
|
---|
| 884 | for(RayInfoContainer::iterator ri = tData.mRays->begin();
|
---|
| 885 | ri != tData.mRays->end(); ++ ri)
|
---|
| 886 | {
|
---|
| 887 | if (!(*ri).mRay->IsActive())
|
---|
| 888 | continue;
|
---|
| 889 |
|
---|
| 890 | // determine the side of this ray with respect to the plane
|
---|
| 891 | float t;
|
---|
| 892 | int side = (*ri).ComputeRayIntersection(axis, position, t);
|
---|
| 893 |
|
---|
| 894 | if (side <= 0)
|
---|
| 895 | ++ raysBack;
|
---|
| 896 |
|
---|
| 897 | if (side >= 0)
|
---|
| 898 | ++ raysFront;
|
---|
| 899 |
|
---|
| 900 | AddObjToPvs((*ri).mRay->mTerminationObject, side, pvsBack, pvsFront);
|
---|
| 901 | }
|
---|
| 902 |
|
---|
| 903 | //-- only one of these options should be one
|
---|
| 904 |
|
---|
| 905 | if (0) //-- only pvs
|
---|
| 906 | {
|
---|
| 907 | const float sum = float(pvsBack + pvsFront);
|
---|
| 908 | const float oldCost = (float)pvsSize;
|
---|
| 909 |
|
---|
| 910 | return sum / oldCost;
|
---|
| 911 | }
|
---|
| 912 |
|
---|
| 913 | //-- pvs + probability heuristics
|
---|
| 914 | float pBack, pFront, pOverall;
|
---|
| 915 |
|
---|
| 916 | if (0)
|
---|
| 917 | {
|
---|
| 918 | // box length substitute for probability
|
---|
| 919 | const float minBox = box.Min(axis);
|
---|
| 920 | const float maxBox = box.Max(axis);
|
---|
| 921 |
|
---|
| 922 | pBack = position - minBox;
|
---|
| 923 | pFront = maxBox - position;
|
---|
| 924 | pOverall = maxBox - minBox;
|
---|
| 925 | }
|
---|
| 926 |
|
---|
| 927 | if (1) //-- area substitute for probability
|
---|
| 928 | {
|
---|
| 929 |
|
---|
| 930 | const bool useMidSplit = true;
|
---|
| 931 | //const bool useMidSplit = false;
|
---|
| 932 |
|
---|
| 933 | pOverall = box.SurfaceArea();
|
---|
| 934 |
|
---|
| 935 | if (!useMidSplit)
|
---|
| 936 | {
|
---|
| 937 | Vector3 pos = box.Max(); pos[axis] = position;
|
---|
| 938 | pBack = AxisAlignedBox3(box.Min(), pos).SurfaceArea();
|
---|
| 939 |
|
---|
| 940 | pos = box.Min(); pos[axis] = position;
|
---|
| 941 | pFront = AxisAlignedBox3(pos, box.Max()).SurfaceArea();
|
---|
| 942 | }
|
---|
| 943 | else
|
---|
| 944 | {
|
---|
| 945 | //-- simplified computation for mid split
|
---|
| 946 | const int axis2 = (axis + 1) % 3;
|
---|
| 947 | const int axis3 = (axis + 2) % 3;
|
---|
| 948 |
|
---|
| 949 | const float faceArea =
|
---|
| 950 | (box.Max(axis2) - box.Min(axis2)) *
|
---|
| 951 | (box.Max(axis3) - box.Min(axis3));
|
---|
| 952 |
|
---|
| 953 | pBack = pFront = pOverall * 0.5f + faceArea;
|
---|
| 954 | }
|
---|
| 955 | }
|
---|
| 956 |
|
---|
| 957 | //-- ray density substitute for probability
|
---|
| 958 | if (0)
|
---|
| 959 | {
|
---|
| 960 | pBack = (float)raysBack;
|
---|
| 961 | pFront = (float)raysFront;
|
---|
| 962 | pOverall = (float)tData.mRays->size();
|
---|
| 963 | }
|
---|
| 964 |
|
---|
| 965 | //Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl;
|
---|
| 966 | //Debug << pFront << " " << pBack << " " << pOverall << endl;
|
---|
| 967 |
|
---|
| 968 | // float sum = raysBack*(position - minBox) + raysFront*(maxBox - position);
|
---|
| 969 | const float newCost = pvsBack * pBack + pvsFront * pFront;
|
---|
| 970 | // float oldCost = leaf->mRays.size();
|
---|
| 971 | const float oldCost = (float)pvsSize * pOverall;
|
---|
| 972 |
|
---|
| 973 | return (mCtDivCi + newCost) / oldCost;
|
---|
| 974 | }
|
---|
| 975 |
|
---|
| 976 |
|
---|
| 977 |
|
---|
[482] | 978 | bool VspBspTree::SelectPlane(Plane3 &plane,
|
---|
| 979 | BspLeaf *leaf,
|
---|
[472] | 980 | VspBspTraversalData &data)
|
---|
[463] | 981 | {
|
---|
| 982 | // simplest strategy: just take next polygon
|
---|
| 983 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
| 984 | {
|
---|
| 985 | if (!data.mPolygons->empty())
|
---|
| 986 | {
|
---|
[473] | 987 | const int randIdx = (int)RandomValue(0, (Real)((int)data.mPolygons->size() - 1));
|
---|
[463] | 988 | Polygon3 *nextPoly = (*data.mPolygons)[randIdx];
|
---|
[472] | 989 |
|
---|
| 990 | plane = nextPoly->GetSupportingPlane();
|
---|
| 991 | return true;
|
---|
[463] | 992 | }
|
---|
| 993 | }
|
---|
| 994 |
|
---|
| 995 | // use heuristics to find appropriate plane
|
---|
[482] | 996 | return SelectPlaneHeuristics(plane, leaf, data);
|
---|
[463] | 997 | }
|
---|
| 998 |
|
---|
[480] | 999 |
|
---|
[473] | 1000 | Plane3 VspBspTree::ChooseCandidatePlane(const RayInfoContainer &rays) const
|
---|
[482] | 1001 | {
|
---|
[473] | 1002 | const int candidateIdx = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
[482] | 1003 |
|
---|
[473] | 1004 | const Vector3 minPt = rays[candidateIdx].ExtrapOrigin();
|
---|
| 1005 | const Vector3 maxPt = rays[candidateIdx].ExtrapTermination();
|
---|
| 1006 |
|
---|
| 1007 | const Vector3 pt = (maxPt + minPt) * 0.5;
|
---|
| 1008 | const Vector3 normal = Normalize(rays[candidateIdx].mRay->GetDir());
|
---|
| 1009 |
|
---|
| 1010 | return Plane3(normal, pt);
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
[480] | 1013 |
|
---|
[473] | 1014 | Plane3 VspBspTree::ChooseCandidatePlane2(const RayInfoContainer &rays) const
|
---|
[482] | 1015 | {
|
---|
[473] | 1016 | Vector3 pt[3];
|
---|
[482] | 1017 |
|
---|
[473] | 1018 | int idx[3];
|
---|
| 1019 | int cmaxT = 0;
|
---|
| 1020 | int cminT = 0;
|
---|
| 1021 | bool chooseMin = false;
|
---|
| 1022 |
|
---|
| 1023 | for (int j = 0; j < 3; ++ j)
|
---|
| 1024 | {
|
---|
| 1025 | idx[j] = (int)RandomValue(0, (Real)((int)rays.size() * 2 - 1));
|
---|
[482] | 1026 |
|
---|
[473] | 1027 | if (idx[j] >= (int)rays.size())
|
---|
| 1028 | {
|
---|
| 1029 | idx[j] -= (int)rays.size();
|
---|
[482] | 1030 |
|
---|
[473] | 1031 | chooseMin = (cminT < 2);
|
---|
| 1032 | }
|
---|
| 1033 | else
|
---|
| 1034 | chooseMin = (cmaxT < 2);
|
---|
| 1035 |
|
---|
| 1036 | RayInfo rayInf = rays[idx[j]];
|
---|
| 1037 | pt[j] = chooseMin ? rayInf.ExtrapOrigin() : rayInf.ExtrapTermination();
|
---|
[482] | 1038 | }
|
---|
[473] | 1039 |
|
---|
| 1040 | return Plane3(pt[0], pt[1], pt[2]);
|
---|
| 1041 | }
|
---|
| 1042 |
|
---|
| 1043 | Plane3 VspBspTree::ChooseCandidatePlane3(const RayInfoContainer &rays) const
|
---|
[482] | 1044 | {
|
---|
[473] | 1045 | Vector3 pt[3];
|
---|
[482] | 1046 |
|
---|
[473] | 1047 | int idx1 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
| 1048 | int idx2 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
| 1049 |
|
---|
| 1050 | // check if rays different
|
---|
| 1051 | if (idx1 == idx2)
|
---|
| 1052 | idx2 = (idx2 + 1) % (int)rays.size();
|
---|
| 1053 |
|
---|
| 1054 | const RayInfo ray1 = rays[idx1];
|
---|
| 1055 | const RayInfo ray2 = rays[idx2];
|
---|
| 1056 |
|
---|
| 1057 | // normal vector of the plane parallel to both lines
|
---|
| 1058 | const Vector3 norm = Normalize(CrossProd(ray1.mRay->GetDir(), ray2.mRay->GetDir()));
|
---|
| 1059 |
|
---|
| 1060 | // vector from line 1 to line 2
|
---|
[479] | 1061 | const Vector3 vd = ray2.ExtrapOrigin() - ray1.ExtrapOrigin();
|
---|
[482] | 1062 |
|
---|
[473] | 1063 | // project vector on normal to get distance
|
---|
| 1064 | const float dist = DotProd(vd, norm);
|
---|
| 1065 |
|
---|
| 1066 | // point on plane lies halfway between the two planes
|
---|
| 1067 | const Vector3 planePt = ray1.ExtrapOrigin() + norm * dist * 0.5;
|
---|
| 1068 |
|
---|
| 1069 | return Plane3(norm, planePt);
|
---|
| 1070 | }
|
---|
| 1071 |
|
---|
[482] | 1072 | bool VspBspTree::SelectPlaneHeuristics(Plane3 &bestPlane,
|
---|
| 1073 | BspLeaf *leaf,
|
---|
[472] | 1074 | VspBspTraversalData &data)
|
---|
[463] | 1075 | {
|
---|
| 1076 | float lowestCost = MAX_FLOAT;
|
---|
[472] | 1077 | // intermediate plane
|
---|
[463] | 1078 | Plane3 plane;
|
---|
[491] | 1079 | bool useAxisAlignedPlane = false;
|
---|
[463] | 1080 |
|
---|
[472] | 1081 | const int limit = Min((int)data.mPolygons->size(), mMaxPolyCandidates);
|
---|
[473] | 1082 | int maxIdx = (int)data.mPolygons->size();
|
---|
[482] | 1083 |
|
---|
[480] | 1084 | float candidateCost;
|
---|
| 1085 |
|
---|
[463] | 1086 | for (int i = 0; i < limit; ++ i)
|
---|
| 1087 | {
|
---|
[473] | 1088 | // assure that no index is taken twice
|
---|
| 1089 | const int candidateIdx = (int)RandomValue(0, (Real)(-- maxIdx));
|
---|
| 1090 | //Debug << "current Idx: " << maxIdx << " cand idx " << candidateIdx << endl;
|
---|
[482] | 1091 |
|
---|
[474] | 1092 | Polygon3 *poly = (*data.mPolygons)[candidateIdx];
|
---|
| 1093 |
|
---|
[473] | 1094 | // swap candidate to the end to avoid testing same plane
|
---|
| 1095 | std::swap((*data.mPolygons)[maxIdx], (*data.mPolygons)[candidateIdx]);
|
---|
[482] | 1096 |
|
---|
[473] | 1097 | //Polygon3 *poly = (*data.mPolygons)[(int)RandomValue(0, (int)polys.size() - 1)];
|
---|
| 1098 |
|
---|
[463] | 1099 | // evaluate current candidate
|
---|
[480] | 1100 | candidateCost = SplitPlaneCost(poly->GetSupportingPlane(), data);
|
---|
[463] | 1101 |
|
---|
| 1102 | if (candidateCost < lowestCost)
|
---|
| 1103 | {
|
---|
| 1104 | bestPlane = poly->GetSupportingPlane();
|
---|
| 1105 | lowestCost = candidateCost;
|
---|
| 1106 | }
|
---|
| 1107 | }
|
---|
[482] | 1108 |
|
---|
[491] | 1109 | //-- axis aligned splits
|
---|
| 1110 | int axis;
|
---|
| 1111 | candidateCost = SelectAxisAlignedPlane(plane, data, axis);
|
---|
[463] | 1112 |
|
---|
[491] | 1113 | if (candidateCost < lowestCost)
|
---|
| 1114 | {
|
---|
| 1115 | if (!useAxisAlignedPlane)
|
---|
[463] | 1116 | {
|
---|
[491] | 1117 | useAxisAlignedPlane = true;
|
---|
| 1118 | //! error also computed if cost ratio is missed
|
---|
| 1119 | ++ mStat.splits[axis];
|
---|
[463] | 1120 | }
|
---|
| 1121 |
|
---|
[482] | 1122 | bestPlane = plane;
|
---|
[480] | 1123 | lowestCost = candidateCost;
|
---|
| 1124 | }
|
---|
| 1125 |
|
---|
[463] | 1126 | #ifdef _DEBUG
|
---|
| 1127 | Debug << "plane lowest cost: " << lowestCost << endl;
|
---|
| 1128 | #endif
|
---|
[472] | 1129 |
|
---|
[474] | 1130 | // cost ratio miss
|
---|
| 1131 | if (lowestCost > mTermMaxCostRatio)
|
---|
| 1132 | return false;
|
---|
| 1133 |
|
---|
[472] | 1134 | return true;
|
---|
[463] | 1135 | }
|
---|
| 1136 |
|
---|
| 1137 |
|
---|
| 1138 | inline void VspBspTree::GenerateUniqueIdsForPvs()
|
---|
| 1139 | {
|
---|
| 1140 | Intersectable::NewMail(); sBackId = ViewCell::sMailId;
|
---|
| 1141 | Intersectable::NewMail(); sFrontId = ViewCell::sMailId;
|
---|
| 1142 | Intersectable::NewMail(); sFrontAndBackId = ViewCell::sMailId;
|
---|
| 1143 | }
|
---|
| 1144 |
|
---|
[482] | 1145 | float VspBspTree::SplitPlaneCost(const Plane3 &candidatePlane,
|
---|
[480] | 1146 | const VspBspTraversalData &data) const
|
---|
[463] | 1147 | {
|
---|
[472] | 1148 | float cost = 0;
|
---|
[463] | 1149 |
|
---|
| 1150 | float sumBalancedRays = 0;
|
---|
| 1151 | float sumRaySplits = 0;
|
---|
[482] | 1152 |
|
---|
[463] | 1153 | int frontPvs = 0;
|
---|
| 1154 | int backPvs = 0;
|
---|
| 1155 |
|
---|
| 1156 | // probability that view point lies in child
|
---|
| 1157 | float pOverall = 0;
|
---|
| 1158 | float pFront = 0;
|
---|
| 1159 | float pBack = 0;
|
---|
| 1160 |
|
---|
| 1161 | const bool pvsUseLen = false;
|
---|
| 1162 |
|
---|
| 1163 | if (mSplitPlaneStrategy & PVS)
|
---|
| 1164 | {
|
---|
[491] | 1165 | // matt: change back!!
|
---|
| 1166 | Intersectable::NewMail(3);
|
---|
[463] | 1167 | // create unique ids for pvs heuristics
|
---|
[491] | 1168 | //GenerateUniqueIdsForPvs();
|
---|
[482] | 1169 |
|
---|
[463] | 1170 | if (mPvsUseArea) // use front and back cell areas to approximate volume
|
---|
[482] | 1171 | {
|
---|
[463] | 1172 | // construct child geometry with regard to the candidate split plane
|
---|
| 1173 | BspNodeGeometry frontCell;
|
---|
| 1174 | BspNodeGeometry backCell;
|
---|
[482] | 1175 |
|
---|
| 1176 | data.mGeometry->SplitGeometry(frontCell,
|
---|
| 1177 | backCell,
|
---|
[463] | 1178 | candidatePlane,
|
---|
| 1179 | mBox,
|
---|
| 1180 | mEpsilon);
|
---|
[482] | 1181 |
|
---|
[463] | 1182 | pFront = frontCell.GetArea();
|
---|
| 1183 | pBack = backCell.GetArea();
|
---|
| 1184 |
|
---|
| 1185 | pOverall = data.mArea;
|
---|
| 1186 | }
|
---|
| 1187 | }
|
---|
[482] | 1188 |
|
---|
[463] | 1189 | int limit;
|
---|
| 1190 | bool useRand;
|
---|
| 1191 |
|
---|
[491] | 1192 | // choose test rays randomly if too much
|
---|
[463] | 1193 | if ((int)data.mRays->size() > mMaxTests)
|
---|
| 1194 | {
|
---|
| 1195 | useRand = true;
|
---|
| 1196 | limit = mMaxTests;
|
---|
| 1197 | }
|
---|
| 1198 | else
|
---|
| 1199 | {
|
---|
| 1200 | useRand = false;
|
---|
| 1201 | limit = (int)data.mRays->size();
|
---|
| 1202 | }
|
---|
| 1203 |
|
---|
| 1204 | for (int i = 0; i < limit; ++ i)
|
---|
| 1205 | {
|
---|
[473] | 1206 | const int testIdx = useRand ? (int)RandomValue(0, (Real)(limit - 1)) : i;
|
---|
[463] | 1207 | RayInfo rayInf = (*data.mRays)[testIdx];
|
---|
| 1208 |
|
---|
| 1209 | VssRay *ray = rayInf.mRay;
|
---|
| 1210 | float t;
|
---|
| 1211 | const int cf = rayInf.ComputeRayIntersection(candidatePlane, t);
|
---|
| 1212 |
|
---|
| 1213 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
| 1214 | {
|
---|
| 1215 | sumBalancedRays += cf;
|
---|
| 1216 | }
|
---|
[482] | 1217 |
|
---|
[463] | 1218 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
| 1219 | {
|
---|
| 1220 | if (cf == 0)
|
---|
| 1221 | ++ sumRaySplits;
|
---|
| 1222 | }
|
---|
| 1223 |
|
---|
| 1224 | if (mSplitPlaneStrategy & PVS)
|
---|
| 1225 | {
|
---|
| 1226 | // in case the ray intersects an object
|
---|
[482] | 1227 | // assure that we only count the object
|
---|
[463] | 1228 | // once for the front and once for the back side of the plane
|
---|
[482] | 1229 |
|
---|
[463] | 1230 | // add the termination object
|
---|
[491] | 1231 | //AddObjToPvs(ray->mTerminationObject, cf, frontPvs, backPvs);
|
---|
| 1232 | AddObject2Pvs(ray->mTerminationObject, cf, frontPvs, backPvs);
|
---|
[463] | 1233 | // add the source object
|
---|
[491] | 1234 | //AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs);
|
---|
[482] | 1235 |
|
---|
[491] | 1236 | // use number of rays to approximate volume
|
---|
[463] | 1237 | if (!mPvsUseArea)
|
---|
| 1238 | {
|
---|
[491] | 1239 | ++ pOverall;
|
---|
[463] | 1240 |
|
---|
[491] | 1241 | if (cf >= 0)
|
---|
| 1242 | ++ pFront;
|
---|
| 1243 | if (cf <= 0)
|
---|
| 1244 | ++ pBack;
|
---|
[463] | 1245 | }
|
---|
| 1246 | }
|
---|
| 1247 | }
|
---|
| 1248 |
|
---|
| 1249 | const float raysSize = (float)data.mRays->size() + Limits::Small;
|
---|
[482] | 1250 |
|
---|
[463] | 1251 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
[472] | 1252 | cost += mLeastRaySplitsFactor * sumRaySplits / raysSize;
|
---|
[463] | 1253 |
|
---|
| 1254 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
[472] | 1255 | cost += mBalancedRaysFactor * fabs(sumBalancedRays) / raysSize;
|
---|
[482] | 1256 |
|
---|
[472] | 1257 | // pvs criterium
|
---|
[463] | 1258 | if (mSplitPlaneStrategy & PVS)
|
---|
| 1259 | {
|
---|
[472] | 1260 | const float oldCost = pOverall * (float)data.mPvs + Limits::Small;
|
---|
[474] | 1261 | cost += mPvsFactor * (frontPvs * pFront + backPvs * pBack) / oldCost;
|
---|
[463] | 1262 |
|
---|
[474] | 1263 | //cost += mPvsFactor * 0.5 * (frontPvs * pFront + backPvs * pBack) / oldCost;
|
---|
| 1264 | //Debug << "new cost: " << cost << " over" << frontPvs * pFront + backPvs * pBack << " old cost " << oldCost << endl;
|
---|
[482] | 1265 |
|
---|
[474] | 1266 | if (0) // give penalty to unbalanced split
|
---|
[482] | 1267 | if (((pFront * 0.2 + Limits::Small) > pBack) ||
|
---|
[474] | 1268 | (pFront < (pBack * 0.2 + Limits::Small)))
|
---|
| 1269 | cost += 0.5;
|
---|
[463] | 1270 | }
|
---|
| 1271 |
|
---|
| 1272 | #ifdef _DEBUG
|
---|
[474] | 1273 | Debug << "totalpvs: " << data.mPvs << " ptotal: " << pOverall
|
---|
[482] | 1274 | << " frontpvs: " << frontPvs << " pFront: " << pFront
|
---|
[474] | 1275 | << " backpvs: " << backPvs << " pBack: " << pBack << endl << endl;
|
---|
[463] | 1276 | #endif
|
---|
[482] | 1277 |
|
---|
[474] | 1278 | // normalize cost by sum of linear factors
|
---|
| 1279 | return cost / (float)mCostNormalizer;
|
---|
[463] | 1280 | }
|
---|
| 1281 |
|
---|
| 1282 | void VspBspTree::AddObjToPvs(Intersectable *obj,
|
---|
| 1283 | const int cf,
|
---|
| 1284 | int &frontPvs,
|
---|
| 1285 | int &backPvs) const
|
---|
| 1286 | {
|
---|
| 1287 | if (!obj)
|
---|
| 1288 | return;
|
---|
| 1289 | // TODO: does this really belong to no pvs?
|
---|
| 1290 | //if (cf == Ray::COINCIDENT) return;
|
---|
| 1291 |
|
---|
| 1292 | // object belongs to both PVS
|
---|
| 1293 | if (cf >= 0)
|
---|
| 1294 | {
|
---|
[482] | 1295 | if ((obj->mMailbox != sFrontId) &&
|
---|
[463] | 1296 | (obj->mMailbox != sFrontAndBackId))
|
---|
| 1297 | {
|
---|
| 1298 | ++ frontPvs;
|
---|
| 1299 |
|
---|
| 1300 | if (obj->mMailbox == sBackId)
|
---|
[482] | 1301 | obj->mMailbox = sFrontAndBackId;
|
---|
[463] | 1302 | else
|
---|
[482] | 1303 | obj->mMailbox = sFrontId;
|
---|
[463] | 1304 | }
|
---|
| 1305 | }
|
---|
[482] | 1306 |
|
---|
[463] | 1307 | if (cf <= 0)
|
---|
| 1308 | {
|
---|
| 1309 | if ((obj->mMailbox != sBackId) &&
|
---|
| 1310 | (obj->mMailbox != sFrontAndBackId))
|
---|
| 1311 | {
|
---|
| 1312 | ++ backPvs;
|
---|
| 1313 |
|
---|
| 1314 | if (obj->mMailbox == sFrontId)
|
---|
[482] | 1315 | obj->mMailbox = sFrontAndBackId;
|
---|
[463] | 1316 | else
|
---|
[482] | 1317 | obj->mMailbox = sBackId;
|
---|
[463] | 1318 | }
|
---|
| 1319 | }
|
---|
| 1320 | }
|
---|
| 1321 |
|
---|
[491] | 1322 |
|
---|
[463] | 1323 | void VspBspTree::CollectLeaves(vector<BspLeaf *> &leaves) const
|
---|
| 1324 | {
|
---|
| 1325 | stack<BspNode *> nodeStack;
|
---|
| 1326 | nodeStack.push(mRoot);
|
---|
[482] | 1327 |
|
---|
| 1328 | while (!nodeStack.empty())
|
---|
[463] | 1329 | {
|
---|
| 1330 | BspNode *node = nodeStack.top();
|
---|
| 1331 | nodeStack.pop();
|
---|
[489] | 1332 |
|
---|
[482] | 1333 | if (node->IsLeaf())
|
---|
[463] | 1334 | {
|
---|
[490] | 1335 | // test if this leaf is in valid view space
|
---|
| 1336 | if (node->TreeValid())
|
---|
| 1337 | {
|
---|
| 1338 | BspLeaf *leaf = (BspLeaf *)node;
|
---|
| 1339 | leaves.push_back(leaf);
|
---|
| 1340 | }
|
---|
[482] | 1341 | }
|
---|
| 1342 | else
|
---|
[463] | 1343 | {
|
---|
| 1344 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 1345 |
|
---|
| 1346 | nodeStack.push(interior->GetBack());
|
---|
| 1347 | nodeStack.push(interior->GetFront());
|
---|
| 1348 | }
|
---|
| 1349 | }
|
---|
| 1350 | }
|
---|
| 1351 |
|
---|
[489] | 1352 |
|
---|
[463] | 1353 | AxisAlignedBox3 VspBspTree::GetBoundingBox() const
|
---|
| 1354 | {
|
---|
| 1355 | return mBox;
|
---|
| 1356 | }
|
---|
| 1357 |
|
---|
[489] | 1358 |
|
---|
[463] | 1359 | BspNode *VspBspTree::GetRoot() const
|
---|
| 1360 | {
|
---|
| 1361 | return mRoot;
|
---|
| 1362 | }
|
---|
| 1363 |
|
---|
[489] | 1364 |
|
---|
| 1365 | BspViewCell *VspBspTree::GetOutOfBoundsCell() const
|
---|
| 1366 | {
|
---|
| 1367 | return mOutOfBoundsCell;
|
---|
| 1368 | }
|
---|
| 1369 |
|
---|
| 1370 |
|
---|
[463] | 1371 | void VspBspTree::EvaluateLeafStats(const VspBspTraversalData &data)
|
---|
| 1372 | {
|
---|
| 1373 | // the node became a leaf -> evaluate stats for leafs
|
---|
| 1374 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode);
|
---|
| 1375 |
|
---|
| 1376 | // store maximal and minimal depth
|
---|
| 1377 | if (data.mDepth > mStat.maxDepth)
|
---|
| 1378 | mStat.maxDepth = data.mDepth;
|
---|
| 1379 |
|
---|
[485] | 1380 | if (data.mPvs > mStat.maxPvs)
|
---|
| 1381 | mStat.maxPvs = data.mPvs;
|
---|
[463] | 1382 | if (data.mDepth < mStat.minDepth)
|
---|
| 1383 | mStat.minDepth = data.mDepth;
|
---|
| 1384 | if (data.mDepth >= mTermMaxDepth)
|
---|
| 1385 | ++ mStat.maxDepthNodes;
|
---|
| 1386 |
|
---|
[437] | 1387 | if (data.mPvs < mTermMinPvs)
|
---|
| 1388 | ++ mStat.minPvsNodes;
|
---|
| 1389 |
|
---|
| 1390 | if ((int)data.mRays->size() < mTermMinRays)
|
---|
| 1391 | ++ mStat.minRaysNodes;
|
---|
| 1392 |
|
---|
| 1393 | if (data.GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
| 1394 | ++ mStat.maxRayContribNodes;
|
---|
[482] | 1395 |
|
---|
| 1396 | if (data.mArea <= mTermMinArea)
|
---|
[474] | 1397 | {
|
---|
| 1398 | //Debug << "area: " << data.mArea / mBox.SurfaceArea() << " min area: " << mTermMinArea / mBox.SurfaceArea() << endl;
|
---|
[463] | 1399 | ++ mStat.minAreaNodes;
|
---|
[474] | 1400 | }
|
---|
| 1401 | // accumulate depth to compute average depth
|
---|
| 1402 | mStat.accumDepth += data.mDepth;
|
---|
[463] | 1403 |
|
---|
| 1404 | #ifdef _DEBUG
|
---|
| 1405 | Debug << "BSP stats: "
|
---|
| 1406 | << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), "
|
---|
| 1407 | << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
|
---|
| 1408 | << "Area: " << data.mArea << " (min: " << mTermMinArea << "), "
|
---|
[482] | 1409 | << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
|
---|
[463] | 1410 | << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, "
|
---|
| 1411 | << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
|
---|
| 1412 | #endif
|
---|
| 1413 | }
|
---|
| 1414 |
|
---|
| 1415 | int VspBspTree::CastRay(Ray &ray)
|
---|
| 1416 | {
|
---|
| 1417 | int hits = 0;
|
---|
[482] | 1418 |
|
---|
[463] | 1419 | stack<BspRayTraversalData> tStack;
|
---|
[482] | 1420 |
|
---|
[463] | 1421 | float maxt, mint;
|
---|
| 1422 |
|
---|
| 1423 | if (!mBox.GetRaySegment(ray, mint, maxt))
|
---|
| 1424 | return 0;
|
---|
| 1425 |
|
---|
| 1426 | Intersectable::NewMail();
|
---|
| 1427 |
|
---|
| 1428 | Vector3 entp = ray.Extrap(mint);
|
---|
| 1429 | Vector3 extp = ray.Extrap(maxt);
|
---|
[482] | 1430 |
|
---|
[463] | 1431 | BspNode *node = mRoot;
|
---|
| 1432 | BspNode *farChild = NULL;
|
---|
[482] | 1433 |
|
---|
[463] | 1434 | while (1)
|
---|
| 1435 | {
|
---|
[482] | 1436 | if (!node->IsLeaf())
|
---|
[463] | 1437 | {
|
---|
| 1438 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
| 1439 |
|
---|
| 1440 | Plane3 splitPlane = in->GetPlane();
|
---|
| 1441 | const int entSide = splitPlane.Side(entp);
|
---|
| 1442 | const int extSide = splitPlane.Side(extp);
|
---|
| 1443 |
|
---|
| 1444 | if (entSide < 0)
|
---|
| 1445 | {
|
---|
| 1446 | node = in->GetBack();
|
---|
| 1447 |
|
---|
| 1448 | if(extSide <= 0) // plane does not split ray => no far child
|
---|
| 1449 | continue;
|
---|
[482] | 1450 |
|
---|
[463] | 1451 | farChild = in->GetFront(); // plane splits ray
|
---|
| 1452 |
|
---|
| 1453 | } else if (entSide > 0)
|
---|
| 1454 | {
|
---|
| 1455 | node = in->GetFront();
|
---|
| 1456 |
|
---|
| 1457 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
| 1458 | continue;
|
---|
| 1459 |
|
---|
[482] | 1460 | farChild = in->GetBack(); // plane splits ray
|
---|
[463] | 1461 | }
|
---|
| 1462 | else // ray and plane are coincident
|
---|
| 1463 | {
|
---|
| 1464 | // WHAT TO DO IN THIS CASE ?
|
---|
| 1465 | //break;
|
---|
| 1466 | node = in->GetFront();
|
---|
| 1467 | continue;
|
---|
| 1468 | }
|
---|
| 1469 |
|
---|
| 1470 | // push data for far child
|
---|
| 1471 | tStack.push(BspRayTraversalData(farChild, extp, maxt));
|
---|
| 1472 |
|
---|
| 1473 | // find intersection of ray segment with plane
|
---|
| 1474 | float t;
|
---|
| 1475 | extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t);
|
---|
| 1476 | maxt *= t;
|
---|
[482] | 1477 |
|
---|
[463] | 1478 | } else // reached leaf => intersection with view cell
|
---|
| 1479 | {
|
---|
| 1480 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
[482] | 1481 |
|
---|
[463] | 1482 | if (!leaf->GetViewCell()->Mailed())
|
---|
| 1483 | {
|
---|
| 1484 | //ray.bspIntersections.push_back(Ray::VspBspIntersection(maxt, leaf));
|
---|
| 1485 | leaf->GetViewCell()->Mail();
|
---|
| 1486 | ++ hits;
|
---|
| 1487 | }
|
---|
[482] | 1488 |
|
---|
[463] | 1489 | //-- fetch the next far child from the stack
|
---|
| 1490 | if (tStack.empty())
|
---|
| 1491 | break;
|
---|
[482] | 1492 |
|
---|
[463] | 1493 | entp = extp;
|
---|
| 1494 | mint = maxt; // NOTE: need this?
|
---|
| 1495 |
|
---|
| 1496 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
|
---|
| 1497 | break;
|
---|
| 1498 |
|
---|
| 1499 | BspRayTraversalData &s = tStack.top();
|
---|
| 1500 |
|
---|
| 1501 | node = s.mNode;
|
---|
| 1502 | extp = s.mExitPoint;
|
---|
| 1503 | maxt = s.mMaxT;
|
---|
| 1504 |
|
---|
| 1505 | tStack.pop();
|
---|
| 1506 | }
|
---|
| 1507 | }
|
---|
| 1508 |
|
---|
| 1509 | return hits;
|
---|
| 1510 | }
|
---|
| 1511 |
|
---|
| 1512 | bool VspBspTree::Export(const string filename)
|
---|
| 1513 | {
|
---|
| 1514 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 1515 |
|
---|
[482] | 1516 | if (exporter)
|
---|
[463] | 1517 | {
|
---|
| 1518 | //exporter->ExportVspBspTree(*this);
|
---|
| 1519 | return true;
|
---|
[482] | 1520 | }
|
---|
[463] | 1521 |
|
---|
| 1522 | return false;
|
---|
| 1523 | }
|
---|
| 1524 |
|
---|
| 1525 | void VspBspTree::CollectViewCells(ViewCellContainer &viewCells) const
|
---|
| 1526 | {
|
---|
| 1527 | stack<BspNode *> nodeStack;
|
---|
| 1528 | nodeStack.push(mRoot);
|
---|
| 1529 |
|
---|
| 1530 | ViewCell::NewMail();
|
---|
| 1531 |
|
---|
[482] | 1532 | while (!nodeStack.empty())
|
---|
[463] | 1533 | {
|
---|
| 1534 | BspNode *node = nodeStack.top();
|
---|
| 1535 | nodeStack.pop();
|
---|
| 1536 |
|
---|
[482] | 1537 | if (node->IsLeaf())
|
---|
[463] | 1538 | {
|
---|
| 1539 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
[490] | 1540 | if (node->TreeValid() && !viewCell->Mailed())
|
---|
[463] | 1541 | {
|
---|
| 1542 | viewCell->Mail();
|
---|
| 1543 | viewCells.push_back(viewCell);
|
---|
| 1544 | }
|
---|
| 1545 | }
|
---|
[482] | 1546 | else
|
---|
[463] | 1547 | {
|
---|
| 1548 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 1549 |
|
---|
| 1550 | nodeStack.push(interior->GetFront());
|
---|
| 1551 | nodeStack.push(interior->GetBack());
|
---|
| 1552 | }
|
---|
| 1553 | }
|
---|
| 1554 | }
|
---|
| 1555 |
|
---|
| 1556 |
|
---|
| 1557 | float VspBspTree::AccumulatedRayLength(const RayInfoContainer &rays) const
|
---|
| 1558 | {
|
---|
| 1559 | float len = 0;
|
---|
| 1560 |
|
---|
| 1561 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
| 1562 |
|
---|
| 1563 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 1564 | len += (*it).SegmentLength();
|
---|
| 1565 |
|
---|
| 1566 | return len;
|
---|
| 1567 | }
|
---|
| 1568 |
|
---|
[479] | 1569 |
|
---|
[463] | 1570 | int VspBspTree::SplitRays(const Plane3 &plane,
|
---|
[482] | 1571 | RayInfoContainer &rays,
|
---|
| 1572 | RayInfoContainer &frontRays,
|
---|
[463] | 1573 | RayInfoContainer &backRays)
|
---|
| 1574 | {
|
---|
| 1575 | int splits = 0;
|
---|
| 1576 |
|
---|
| 1577 | while (!rays.empty())
|
---|
| 1578 | {
|
---|
| 1579 | RayInfo bRay = rays.back();
|
---|
[473] | 1580 | rays.pop_back();
|
---|
| 1581 |
|
---|
[463] | 1582 | VssRay *ray = bRay.mRay;
|
---|
[473] | 1583 | float t;
|
---|
[463] | 1584 |
|
---|
[485] | 1585 | // get classification and receive new t
|
---|
[463] | 1586 | const int cf = bRay.ComputeRayIntersection(plane, t);
|
---|
[482] | 1587 |
|
---|
[463] | 1588 | switch (cf)
|
---|
| 1589 | {
|
---|
| 1590 | case -1:
|
---|
| 1591 | backRays.push_back(bRay);
|
---|
| 1592 | break;
|
---|
| 1593 | case 1:
|
---|
| 1594 | frontRays.push_back(bRay);
|
---|
| 1595 | break;
|
---|
[482] | 1596 | case 0:
|
---|
| 1597 | {
|
---|
[485] | 1598 | //-- split ray
|
---|
| 1599 | //-- test if start point behind or in front of plane
|
---|
| 1600 | const int side = plane.Side(bRay.ExtrapOrigin());
|
---|
| 1601 |
|
---|
| 1602 | if (side <= 0)
|
---|
| 1603 | {
|
---|
| 1604 | backRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
| 1605 | frontRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
| 1606 | }
|
---|
| 1607 | else
|
---|
| 1608 | {
|
---|
| 1609 | frontRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
| 1610 | backRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
| 1611 | }
|
---|
[463] | 1612 | }
|
---|
| 1613 | break;
|
---|
| 1614 | default:
|
---|
[485] | 1615 | Debug << "Should not come here" << endl;
|
---|
[463] | 1616 | break;
|
---|
| 1617 | }
|
---|
| 1618 | }
|
---|
| 1619 |
|
---|
| 1620 | return splits;
|
---|
| 1621 | }
|
---|
| 1622 |
|
---|
[479] | 1623 |
|
---|
[463] | 1624 | void VspBspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const
|
---|
| 1625 | {
|
---|
| 1626 | BspNode *lastNode;
|
---|
| 1627 |
|
---|
| 1628 | do
|
---|
| 1629 | {
|
---|
| 1630 | lastNode = n;
|
---|
| 1631 |
|
---|
| 1632 | // want to get planes defining geometry of this node => don't take
|
---|
| 1633 | // split plane of node itself
|
---|
| 1634 | n = n->GetParent();
|
---|
[482] | 1635 |
|
---|
[463] | 1636 | if (n)
|
---|
| 1637 | {
|
---|
| 1638 | BspInterior *interior = dynamic_cast<BspInterior *>(n);
|
---|
| 1639 | Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane();
|
---|
| 1640 |
|
---|
| 1641 | if (interior->GetFront() != lastNode)
|
---|
| 1642 | halfSpace.ReverseOrientation();
|
---|
| 1643 |
|
---|
| 1644 | halfSpaces.push_back(halfSpace);
|
---|
| 1645 | }
|
---|
| 1646 | }
|
---|
| 1647 | while (n);
|
---|
| 1648 | }
|
---|
| 1649 |
|
---|
[485] | 1650 |
|
---|
[482] | 1651 | void VspBspTree::ConstructGeometry(BspNode *n,
|
---|
[463] | 1652 | BspNodeGeometry &cell) const
|
---|
| 1653 | {
|
---|
[485] | 1654 | ConstructGeometry(n, cell.mPolys);
|
---|
[463] | 1655 | }
|
---|
| 1656 |
|
---|
[485] | 1657 |
|
---|
[482] | 1658 | void VspBspTree::ConstructGeometry(BspNode *n,
|
---|
[448] | 1659 | PolygonContainer &cell) const
|
---|
[437] | 1660 | {
|
---|
| 1661 | vector<Plane3> halfSpaces;
|
---|
| 1662 | ExtractHalfSpaces(n, halfSpaces);
|
---|
| 1663 |
|
---|
| 1664 | PolygonContainer candidatePolys;
|
---|
| 1665 |
|
---|
| 1666 | // bounded planes are added to the polygons (reverse polygons
|
---|
| 1667 | // as they have to be outfacing
|
---|
| 1668 | for (int i = 0; i < (int)halfSpaces.size(); ++ i)
|
---|
| 1669 | {
|
---|
| 1670 | Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]);
|
---|
[482] | 1671 |
|
---|
[448] | 1672 | if (p->Valid(mEpsilon))
|
---|
[437] | 1673 | {
|
---|
| 1674 | candidatePolys.push_back(p->CreateReversePolygon());
|
---|
| 1675 | DEL_PTR(p);
|
---|
| 1676 | }
|
---|
| 1677 | }
|
---|
| 1678 |
|
---|
| 1679 | // add faces of bounding box (also could be faces of the cell)
|
---|
| 1680 | for (int i = 0; i < 6; ++ i)
|
---|
| 1681 | {
|
---|
| 1682 | VertexContainer vertices;
|
---|
[482] | 1683 |
|
---|
[437] | 1684 | for (int j = 0; j < 4; ++ j)
|
---|
| 1685 | vertices.push_back(mBox.GetFace(i).mVertices[j]);
|
---|
| 1686 |
|
---|
| 1687 | candidatePolys.push_back(new Polygon3(vertices));
|
---|
| 1688 | }
|
---|
| 1689 |
|
---|
| 1690 | for (int i = 0; i < (int)candidatePolys.size(); ++ i)
|
---|
| 1691 | {
|
---|
| 1692 | // polygon is split by all other planes
|
---|
| 1693 | for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
|
---|
| 1694 | {
|
---|
| 1695 | if (i == j) // polygon and plane are coincident
|
---|
| 1696 | continue;
|
---|
| 1697 |
|
---|
| 1698 | VertexContainer splitPts;
|
---|
| 1699 | Polygon3 *frontPoly, *backPoly;
|
---|
| 1700 |
|
---|
[482] | 1701 | const int cf =
|
---|
[448] | 1702 | candidatePolys[i]->ClassifyPlane(halfSpaces[j],
|
---|
| 1703 | mEpsilon);
|
---|
[482] | 1704 |
|
---|
[437] | 1705 | switch (cf)
|
---|
| 1706 | {
|
---|
| 1707 | case Polygon3::SPLIT:
|
---|
| 1708 | frontPoly = new Polygon3();
|
---|
| 1709 | backPoly = new Polygon3();
|
---|
| 1710 |
|
---|
[482] | 1711 | candidatePolys[i]->Split(halfSpaces[j],
|
---|
| 1712 | *frontPoly,
|
---|
[448] | 1713 | *backPoly,
|
---|
| 1714 | mEpsilon);
|
---|
[437] | 1715 |
|
---|
| 1716 | DEL_PTR(candidatePolys[i]);
|
---|
| 1717 |
|
---|
[448] | 1718 | if (frontPoly->Valid(mEpsilon))
|
---|
[437] | 1719 | candidatePolys[i] = frontPoly;
|
---|
| 1720 | else
|
---|
| 1721 | DEL_PTR(frontPoly);
|
---|
| 1722 |
|
---|
| 1723 | DEL_PTR(backPoly);
|
---|
| 1724 | break;
|
---|
| 1725 | case Polygon3::BACK_SIDE:
|
---|
| 1726 | DEL_PTR(candidatePolys[i]);
|
---|
| 1727 | break;
|
---|
| 1728 | // just take polygon as it is
|
---|
| 1729 | case Polygon3::FRONT_SIDE:
|
---|
| 1730 | case Polygon3::COINCIDENT:
|
---|
| 1731 | default:
|
---|
| 1732 | break;
|
---|
| 1733 | }
|
---|
| 1734 | }
|
---|
[482] | 1735 |
|
---|
[437] | 1736 | if (candidatePolys[i])
|
---|
| 1737 | cell.push_back(candidatePolys[i]);
|
---|
| 1738 | }
|
---|
[463] | 1739 | }
|
---|
| 1740 |
|
---|
[485] | 1741 |
|
---|
| 1742 | void VspBspTree::ConstructGeometry(BspViewCell *vc,
|
---|
| 1743 | PolygonContainer &vcGeom) const
|
---|
[463] | 1744 | {
|
---|
[469] | 1745 | vector<BspLeaf *> leaves = vc->mLeaves;
|
---|
[463] | 1746 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
| 1747 |
|
---|
| 1748 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 1749 | ConstructGeometry(*it, vcGeom);
|
---|
| 1750 | }
|
---|
| 1751 |
|
---|
[485] | 1752 |
|
---|
[482] | 1753 | int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
|
---|
[463] | 1754 | const bool onlyUnmailed) const
|
---|
| 1755 | {
|
---|
| 1756 | PolygonContainer cell;
|
---|
| 1757 | ConstructGeometry(n, cell);
|
---|
| 1758 |
|
---|
| 1759 | stack<BspNode *> nodeStack;
|
---|
| 1760 | nodeStack.push(mRoot);
|
---|
[482] | 1761 |
|
---|
[463] | 1762 | // planes needed to verify that we found neighbor leaf.
|
---|
| 1763 | vector<Plane3> halfSpaces;
|
---|
| 1764 | ExtractHalfSpaces(n, halfSpaces);
|
---|
| 1765 |
|
---|
[482] | 1766 | while (!nodeStack.empty())
|
---|
[463] | 1767 | {
|
---|
| 1768 | BspNode *node = nodeStack.top();
|
---|
| 1769 | nodeStack.pop();
|
---|
| 1770 |
|
---|
| 1771 | if (node->IsLeaf())
|
---|
[490] | 1772 | { // test if this leaf is in valid view space
|
---|
| 1773 | if (node->TreeValid() &&
|
---|
| 1774 | node != n &&
|
---|
| 1775 | (!onlyUnmailed || !node->Mailed()))
|
---|
[463] | 1776 | {
|
---|
| 1777 | // test all planes of current node if candidate really
|
---|
| 1778 | // is neighbour
|
---|
| 1779 | PolygonContainer neighborCandidate;
|
---|
| 1780 | ConstructGeometry(node, neighborCandidate);
|
---|
[482] | 1781 |
|
---|
[463] | 1782 | bool isAdjacent = true;
|
---|
| 1783 | for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i)
|
---|
| 1784 | {
|
---|
[482] | 1785 | const int cf =
|
---|
| 1786 | Polygon3::ClassifyPlane(neighborCandidate,
|
---|
[463] | 1787 | halfSpaces[i],
|
---|
| 1788 | mEpsilon);
|
---|
| 1789 |
|
---|
| 1790 | if (cf == Polygon3::BACK_SIDE)
|
---|
| 1791 | isAdjacent = false;
|
---|
| 1792 | }
|
---|
| 1793 |
|
---|
| 1794 | if (isAdjacent)
|
---|
| 1795 | neighbors.push_back(dynamic_cast<BspLeaf *>(node));
|
---|
| 1796 |
|
---|
| 1797 | CLEAR_CONTAINER(neighborCandidate);
|
---|
| 1798 | }
|
---|
| 1799 | }
|
---|
[482] | 1800 | else
|
---|
[463] | 1801 | {
|
---|
| 1802 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
[482] | 1803 |
|
---|
| 1804 | const int cf = Polygon3::ClassifyPlane(cell,
|
---|
| 1805 | interior->GetPlane(),
|
---|
[463] | 1806 | mEpsilon);
|
---|
| 1807 |
|
---|
| 1808 | if (cf == Polygon3::FRONT_SIDE)
|
---|
| 1809 | nodeStack.push(interior->GetFront());
|
---|
| 1810 | else
|
---|
| 1811 | if (cf == Polygon3::BACK_SIDE)
|
---|
| 1812 | nodeStack.push(interior->GetBack());
|
---|
[482] | 1813 | else
|
---|
[463] | 1814 | {
|
---|
| 1815 | // random decision
|
---|
| 1816 | nodeStack.push(interior->GetBack());
|
---|
| 1817 | nodeStack.push(interior->GetFront());
|
---|
| 1818 | }
|
---|
| 1819 | }
|
---|
| 1820 | }
|
---|
[482] | 1821 |
|
---|
[463] | 1822 | CLEAR_CONTAINER(cell);
|
---|
| 1823 | return (int)neighbors.size();
|
---|
| 1824 | }
|
---|
| 1825 |
|
---|
[489] | 1826 |
|
---|
[463] | 1827 | BspLeaf *VspBspTree::GetRandomLeaf(const Plane3 &halfspace)
|
---|
| 1828 | {
|
---|
| 1829 | stack<BspNode *> nodeStack;
|
---|
| 1830 | nodeStack.push(mRoot);
|
---|
[482] | 1831 |
|
---|
[463] | 1832 | int mask = rand();
|
---|
[482] | 1833 |
|
---|
| 1834 | while (!nodeStack.empty())
|
---|
[463] | 1835 | {
|
---|
| 1836 | BspNode *node = nodeStack.top();
|
---|
| 1837 | nodeStack.pop();
|
---|
[482] | 1838 |
|
---|
| 1839 | if (node->IsLeaf())
|
---|
[463] | 1840 | {
|
---|
| 1841 | return dynamic_cast<BspLeaf *>(node);
|
---|
[482] | 1842 | }
|
---|
| 1843 | else
|
---|
[463] | 1844 | {
|
---|
| 1845 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
[482] | 1846 |
|
---|
[463] | 1847 | BspNode *next;
|
---|
[482] | 1848 |
|
---|
[463] | 1849 | PolygonContainer cell;
|
---|
| 1850 |
|
---|
| 1851 | // todo: not very efficient: constructs full cell everytime
|
---|
| 1852 | ConstructGeometry(interior, cell);
|
---|
| 1853 |
|
---|
| 1854 | const int cf = Polygon3::ClassifyPlane(cell, halfspace, mEpsilon);
|
---|
| 1855 |
|
---|
| 1856 | if (cf == Polygon3::BACK_SIDE)
|
---|
| 1857 | next = interior->GetFront();
|
---|
| 1858 | else
|
---|
| 1859 | if (cf == Polygon3::FRONT_SIDE)
|
---|
| 1860 | next = interior->GetFront();
|
---|
[482] | 1861 | else
|
---|
[463] | 1862 | {
|
---|
| 1863 | // random decision
|
---|
| 1864 | if (mask & 1)
|
---|
| 1865 | next = interior->GetBack();
|
---|
| 1866 | else
|
---|
| 1867 | next = interior->GetFront();
|
---|
| 1868 | mask = mask >> 1;
|
---|
| 1869 | }
|
---|
| 1870 |
|
---|
| 1871 | nodeStack.push(next);
|
---|
| 1872 | }
|
---|
| 1873 | }
|
---|
[482] | 1874 |
|
---|
[463] | 1875 | return NULL;
|
---|
| 1876 | }
|
---|
| 1877 |
|
---|
| 1878 | BspLeaf *VspBspTree::GetRandomLeaf(const bool onlyUnmailed)
|
---|
| 1879 | {
|
---|
| 1880 | stack<BspNode *> nodeStack;
|
---|
[482] | 1881 |
|
---|
[463] | 1882 | nodeStack.push(mRoot);
|
---|
| 1883 |
|
---|
| 1884 | int mask = rand();
|
---|
[482] | 1885 |
|
---|
| 1886 | while (!nodeStack.empty())
|
---|
[463] | 1887 | {
|
---|
| 1888 | BspNode *node = nodeStack.top();
|
---|
| 1889 | nodeStack.pop();
|
---|
[482] | 1890 |
|
---|
| 1891 | if (node->IsLeaf())
|
---|
[463] | 1892 | {
|
---|
| 1893 | if ( (!onlyUnmailed || !node->Mailed()) )
|
---|
| 1894 | return dynamic_cast<BspLeaf *>(node);
|
---|
| 1895 | }
|
---|
[482] | 1896 | else
|
---|
[463] | 1897 | {
|
---|
| 1898 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 1899 |
|
---|
| 1900 | // random decision
|
---|
| 1901 | if (mask & 1)
|
---|
| 1902 | nodeStack.push(interior->GetBack());
|
---|
| 1903 | else
|
---|
| 1904 | nodeStack.push(interior->GetFront());
|
---|
| 1905 |
|
---|
| 1906 | mask = mask >> 1;
|
---|
| 1907 | }
|
---|
| 1908 | }
|
---|
[482] | 1909 |
|
---|
[463] | 1910 | return NULL;
|
---|
| 1911 | }
|
---|
| 1912 |
|
---|
| 1913 | int VspBspTree::ComputePvsSize(const RayInfoContainer &rays) const
|
---|
| 1914 | {
|
---|
| 1915 | int pvsSize = 0;
|
---|
| 1916 |
|
---|
| 1917 | RayInfoContainer::const_iterator rit, rit_end = rays.end();
|
---|
| 1918 |
|
---|
| 1919 | Intersectable::NewMail();
|
---|
| 1920 |
|
---|
| 1921 | for (rit = rays.begin(); rit != rays.end(); ++ rit)
|
---|
| 1922 | {
|
---|
| 1923 | VssRay *ray = (*rit).mRay;
|
---|
[482] | 1924 |
|
---|
[463] | 1925 | if (ray->mOriginObject)
|
---|
| 1926 | {
|
---|
| 1927 | if (!ray->mOriginObject->Mailed())
|
---|
| 1928 | {
|
---|
| 1929 | ray->mOriginObject->Mail();
|
---|
| 1930 | ++ pvsSize;
|
---|
| 1931 | }
|
---|
| 1932 | }
|
---|
| 1933 | if (ray->mTerminationObject)
|
---|
| 1934 | {
|
---|
| 1935 | if (!ray->mTerminationObject->Mailed())
|
---|
| 1936 | {
|
---|
| 1937 | ray->mTerminationObject->Mail();
|
---|
| 1938 | ++ pvsSize;
|
---|
| 1939 | }
|
---|
| 1940 | }
|
---|
| 1941 | }
|
---|
| 1942 |
|
---|
| 1943 | return pvsSize;
|
---|
| 1944 | }
|
---|
| 1945 |
|
---|
| 1946 | float VspBspTree::GetEpsilon() const
|
---|
| 1947 | {
|
---|
| 1948 | return mEpsilon;
|
---|
| 1949 | }
|
---|
| 1950 |
|
---|
| 1951 |
|
---|
| 1952 | int VspBspTree::SplitPolygons(const Plane3 &plane,
|
---|
[482] | 1953 | PolygonContainer &polys,
|
---|
| 1954 | PolygonContainer &frontPolys,
|
---|
| 1955 | PolygonContainer &backPolys,
|
---|
[463] | 1956 | PolygonContainer &coincident) const
|
---|
| 1957 | {
|
---|
| 1958 | int splits = 0;
|
---|
| 1959 |
|
---|
| 1960 | while (!polys.empty())
|
---|
| 1961 | {
|
---|
| 1962 | Polygon3 *poly = polys.back();
|
---|
| 1963 | polys.pop_back();
|
---|
| 1964 |
|
---|
| 1965 | // classify polygon
|
---|
| 1966 | const int cf = poly->ClassifyPlane(plane, mEpsilon);
|
---|
| 1967 |
|
---|
| 1968 | switch (cf)
|
---|
| 1969 | {
|
---|
| 1970 | case Polygon3::COINCIDENT:
|
---|
| 1971 | coincident.push_back(poly);
|
---|
[482] | 1972 | break;
|
---|
| 1973 | case Polygon3::FRONT_SIDE:
|
---|
[463] | 1974 | frontPolys.push_back(poly);
|
---|
| 1975 | break;
|
---|
| 1976 | case Polygon3::BACK_SIDE:
|
---|
| 1977 | backPolys.push_back(poly);
|
---|
| 1978 | break;
|
---|
| 1979 | case Polygon3::SPLIT:
|
---|
| 1980 | backPolys.push_back(poly);
|
---|
| 1981 | frontPolys.push_back(poly);
|
---|
| 1982 | ++ splits;
|
---|
| 1983 | break;
|
---|
| 1984 | default:
|
---|
| 1985 | Debug << "SHOULD NEVER COME HERE\n";
|
---|
| 1986 | break;
|
---|
| 1987 | }
|
---|
| 1988 | }
|
---|
| 1989 |
|
---|
| 1990 | return splits;
|
---|
| 1991 | }
|
---|
[466] | 1992 |
|
---|
| 1993 |
|
---|
[469] | 1994 | int VspBspTree::CastLineSegment(const Vector3 &origin,
|
---|
| 1995 | const Vector3 &termination,
|
---|
| 1996 | vector<ViewCell *> &viewcells)
|
---|
[466] | 1997 | {
|
---|
[469] | 1998 | int hits = 0;
|
---|
| 1999 | stack<BspRayTraversalData> tStack;
|
---|
[482] | 2000 |
|
---|
[469] | 2001 | float mint = 0.0f, maxt = 1.0f;
|
---|
[482] | 2002 |
|
---|
[469] | 2003 | Intersectable::NewMail();
|
---|
[482] | 2004 |
|
---|
[469] | 2005 | Vector3 entp = origin;
|
---|
| 2006 | Vector3 extp = termination;
|
---|
[482] | 2007 |
|
---|
[469] | 2008 | BspNode *node = mRoot;
|
---|
| 2009 | BspNode *farChild = NULL;
|
---|
[482] | 2010 |
|
---|
[485] | 2011 | float t;
|
---|
[482] | 2012 | while (1)
|
---|
[469] | 2013 | {
|
---|
[482] | 2014 | if (!node->IsLeaf())
|
---|
[469] | 2015 | {
|
---|
| 2016 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
[482] | 2017 |
|
---|
[469] | 2018 | Plane3 splitPlane = in->GetPlane();
|
---|
[485] | 2019 |
|
---|
[469] | 2020 | const int entSide = splitPlane.Side(entp);
|
---|
| 2021 | const int extSide = splitPlane.Side(extp);
|
---|
[482] | 2022 |
|
---|
[485] | 2023 | if (entSide < 0)
|
---|
[469] | 2024 | {
|
---|
| 2025 | node = in->GetBack();
|
---|
[485] | 2026 | // plane does not split ray => no far child
|
---|
| 2027 | if (extSide <= 0)
|
---|
[469] | 2028 | continue;
|
---|
[482] | 2029 |
|
---|
[469] | 2030 | farChild = in->GetFront(); // plane splits ray
|
---|
[485] | 2031 | }
|
---|
| 2032 | else if (entSide > 0)
|
---|
[469] | 2033 | {
|
---|
| 2034 | node = in->GetFront();
|
---|
[482] | 2035 |
|
---|
[469] | 2036 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
| 2037 | continue;
|
---|
[482] | 2038 |
|
---|
| 2039 | farChild = in->GetBack(); // plane splits ray
|
---|
[469] | 2040 | }
|
---|
[485] | 2041 | else // ray end point on plane
|
---|
| 2042 | { // NOTE: what to do if ray is coincident with plane?
|
---|
| 2043 | if (extSide < 0)
|
---|
| 2044 | node = in->GetBack();
|
---|
[487] | 2045 | else
|
---|
[485] | 2046 | node = in->GetFront();
|
---|
[487] | 2047 |
|
---|
[485] | 2048 | continue; // no far child
|
---|
[469] | 2049 | }
|
---|
[482] | 2050 |
|
---|
[469] | 2051 | // push data for far child
|
---|
[485] | 2052 | tStack.push(BspRayTraversalData(farChild, extp));
|
---|
[482] | 2053 |
|
---|
[469] | 2054 | // find intersection of ray segment with plane
|
---|
| 2055 | extp = splitPlane.FindIntersection(origin, extp, &t);
|
---|
[485] | 2056 | }
|
---|
| 2057 | else
|
---|
[469] | 2058 | {
|
---|
| 2059 | // reached leaf => intersection with view cell
|
---|
| 2060 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
[482] | 2061 |
|
---|
| 2062 | if (!leaf->GetViewCell()->Mailed())
|
---|
[469] | 2063 | {
|
---|
| 2064 | viewcells.push_back(leaf->GetViewCell());
|
---|
| 2065 | leaf->GetViewCell()->Mail();
|
---|
| 2066 | ++ hits;
|
---|
| 2067 | }
|
---|
[482] | 2068 |
|
---|
[469] | 2069 | //-- fetch the next far child from the stack
|
---|
| 2070 | if (tStack.empty())
|
---|
| 2071 | break;
|
---|
[482] | 2072 |
|
---|
[469] | 2073 | entp = extp;
|
---|
[485] | 2074 |
|
---|
[469] | 2075 | BspRayTraversalData &s = tStack.top();
|
---|
[482] | 2076 |
|
---|
[469] | 2077 | node = s.mNode;
|
---|
| 2078 | extp = s.mExitPoint;
|
---|
[482] | 2079 |
|
---|
[469] | 2080 | tStack.pop();
|
---|
| 2081 | }
|
---|
[466] | 2082 | }
|
---|
[487] | 2083 |
|
---|
[469] | 2084 | return hits;
|
---|
[466] | 2085 | }
|
---|
[478] | 2086 |
|
---|
[485] | 2087 | int VspBspTree::TreeDistance(BspNode *n1, BspNode *n2) const
|
---|
[482] | 2088 | {
|
---|
| 2089 | std::deque<BspNode *> path1;
|
---|
| 2090 | BspNode *p1 = n1;
|
---|
[479] | 2091 |
|
---|
[482] | 2092 | // create path from node 1 to root
|
---|
| 2093 | while (p1)
|
---|
| 2094 | {
|
---|
| 2095 | if (p1 == n2) // second node on path
|
---|
| 2096 | return (int)path1.size();
|
---|
| 2097 |
|
---|
| 2098 | path1.push_front(p1);
|
---|
| 2099 | p1 = p1->GetParent();
|
---|
| 2100 | }
|
---|
| 2101 |
|
---|
| 2102 | int depth = n2->GetDepth();
|
---|
| 2103 | int d = depth;
|
---|
| 2104 |
|
---|
| 2105 | BspNode *p2 = n2;
|
---|
| 2106 |
|
---|
| 2107 | // compare with same depth
|
---|
| 2108 | while (1)
|
---|
| 2109 | {
|
---|
| 2110 | if ((d < (int)path1.size()) && (p2 == path1[d]))
|
---|
| 2111 | return (depth - d) + ((int)path1.size() - 1 - d);
|
---|
| 2112 |
|
---|
| 2113 | -- d;
|
---|
| 2114 | p2 = p2->GetParent();
|
---|
| 2115 | }
|
---|
| 2116 |
|
---|
| 2117 | return 0; // never come here
|
---|
| 2118 | }
|
---|
| 2119 |
|
---|
[479] | 2120 | BspNode *VspBspTree::CollapseTree(BspNode *node)
|
---|
| 2121 | {
|
---|
| 2122 | if (node->IsLeaf())
|
---|
| 2123 | return node;
|
---|
| 2124 |
|
---|
| 2125 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 2126 | |
---|
| 2127 | BspNode *front = CollapseTree(interior->GetFront()); |
---|
| 2128 | BspNode *back = CollapseTree(interior->GetBack()); |
---|
| 2129 | |
---|
| 2130 | if (front->IsLeaf() && back->IsLeaf())
|
---|
| 2131 | {
|
---|
| 2132 | BspLeaf *frontLeaf = dynamic_cast<BspLeaf *>(front);
|
---|
| 2133 | BspLeaf *backLeaf = dynamic_cast<BspLeaf *>(back);
|
---|
| 2134 |
|
---|
| 2135 | //-- collapse tree
|
---|
| 2136 | if (frontLeaf->GetViewCell() == backLeaf->GetViewCell())
|
---|
| 2137 | {
|
---|
| 2138 | BspViewCell *vc = frontLeaf->GetViewCell();
|
---|
| 2139 |
|
---|
| 2140 | BspLeaf *leaf = new BspLeaf(interior->GetParent(), vc);
|
---|
[489] | 2141 | leaf->SetTreeValid(frontLeaf->TreeValid());
|
---|
[482] | 2142 |
|
---|
[479] | 2143 | // replace a link from node's parent
|
---|
| 2144 | if (leaf->GetParent())
|
---|
| 2145 | leaf->GetParent()->ReplaceChildLink(node, leaf);
|
---|
| 2146 |
|
---|
| 2147 | delete interior;
|
---|
| 2148 |
|
---|
| 2149 | return leaf;
|
---|
| 2150 | }
|
---|
| 2151 | }
|
---|
| 2152 |
|
---|
[485] | 2153 | // revalidate leaves
|
---|
| 2154 | RepairVcLeafLists();
|
---|
| 2155 |
|
---|
[479] | 2156 | return node;
|
---|
| 2157 | }
|
---|
| 2158 |
|
---|
| 2159 |
|
---|
| 2160 | void VspBspTree::RepairVcLeafLists()
|
---|
| 2161 | { |
---|
| 2162 | // list not valid anymore => clear
|
---|
| 2163 | stack<BspNode *> nodeStack; |
---|
| 2164 | nodeStack.push(mRoot); |
---|
| 2165 | |
---|
| 2166 | ViewCell::NewMail(); |
---|
| 2167 | |
---|
| 2168 | while (!nodeStack.empty()) |
---|
| 2169 | { |
---|
| 2170 | BspNode *node = nodeStack.top(); |
---|
| 2171 | nodeStack.pop(); |
---|
| 2172 | |
---|
| 2173 | if (node->IsLeaf()) |
---|
| 2174 | { |
---|
| 2175 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); |
---|
| 2176 | |
---|
| 2177 | BspViewCell *viewCell = leaf->GetViewCell(); |
---|
| 2178 | |
---|
| 2179 | if (!viewCell->Mailed()) |
---|
| 2180 | { |
---|
| 2181 | viewCell->mLeaves.clear(); |
---|
| 2182 | viewCell->Mail(); |
---|
| 2183 | } |
---|
| 2184 | |
---|
| 2185 | viewCell->mLeaves.push_back(leaf); |
---|
| 2186 | } |
---|
| 2187 | else |
---|
| 2188 | { |
---|
| 2189 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 2190 | |
---|
| 2191 | nodeStack.push(interior->GetFront()); |
---|
| 2192 | nodeStack.push(interior->GetBack()); |
---|
| 2193 | } |
---|
| 2194 | }
|
---|
| 2195 | }
|
---|
| 2196 |
|
---|
| 2197 |
|
---|
[485] | 2198 | bool VspBspTree::MergeViewCells(BspLeaf *l1, BspLeaf *l2) const |
---|
| 2199 | { |
---|
| 2200 | //-- change pointer to view cells of all leaves associated |
---|
| 2201 | //-- with the previous view cells |
---|
| 2202 | BspViewCell *fVc = l1->GetViewCell(); |
---|
| 2203 | BspViewCell *bVc = l2->GetViewCell(); |
---|
| 2204 | |
---|
| 2205 | BspViewCell *vc = dynamic_cast<BspViewCell *>( |
---|
| 2206 | mViewCellsManager->MergeViewCells(*fVc, *bVc)); |
---|
| 2207 | |
---|
| 2208 | // if merge was unsuccessful |
---|
| 2209 | if (!vc) return false; |
---|
| 2210 | |
---|
| 2211 | // set new size of view cell |
---|
| 2212 | vc->SetArea(fVc->GetArea() + bVc->GetArea()); |
---|
| 2213 | |
---|
| 2214 | vector<BspLeaf *> fLeaves = fVc->mLeaves; |
---|
| 2215 | vector<BspLeaf *> bLeaves = bVc->mLeaves; |
---|
| 2216 | |
---|
| 2217 | vector<BspLeaf *>::const_iterator it; |
---|
| 2218 | |
---|
| 2219 | //-- change view cells of all the other leaves the view cell belongs to |
---|
| 2220 | for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) |
---|
| 2221 | { |
---|
| 2222 | (*it)->SetViewCell(vc); |
---|
| 2223 | vc->mLeaves.push_back(*it); |
---|
| 2224 | } |
---|
| 2225 | |
---|
| 2226 | for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) |
---|
| 2227 | { |
---|
| 2228 | (*it)->SetViewCell(vc); |
---|
| 2229 | vc->mLeaves.push_back(*it); |
---|
| 2230 | } |
---|
| 2231 | |
---|
| 2232 | vc->Mail();
|
---|
| 2233 | |
---|
| 2234 | // clean up old view cells |
---|
| 2235 | DEL_PTR(fVc); |
---|
| 2236 | DEL_PTR(bVc); |
---|
| 2237 | |
---|
| 2238 | return true;
|
---|
| 2239 | }
|
---|
| 2240 |
|
---|
| 2241 |
|
---|
| 2242 | void VspBspTree::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
[478] | 2243 | {
|
---|
[485] | 2244 | mViewCellsManager = vcm;
|
---|
| 2245 | }
|
---|
| 2246 |
|
---|
| 2247 |
|
---|
| 2248 | int VspBspTree::CollectMergeCandidates()
|
---|
| 2249 | {
|
---|
[478] | 2250 | vector<BspLeaf *> leaves;
|
---|
| 2251 |
|
---|
| 2252 | // collect the leaves, e.g., the "voxels" that will build the view cells
|
---|
| 2253 | CollectLeaves(leaves);
|
---|
| 2254 | BspLeaf::NewMail();
|
---|
| 2255 |
|
---|
| 2256 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
| 2257 |
|
---|
| 2258 | // find merge candidates and push them into queue
|
---|
| 2259 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 2260 | {
|
---|
| 2261 | BspLeaf *leaf = *it;
|
---|
[485] | 2262 |
|
---|
| 2263 | /// create leaf pvs (needed for post processing
|
---|
| 2264 | leaf->mPvs = new ObjectPvs(leaf->GetViewCell()->GetPvs());
|
---|
[478] | 2265 |
|
---|
[485] | 2266 | BspMergeCandidate::sOverallCost +=
|
---|
| 2267 | leaf->mArea * leaf->mPvs->GetSize();
|
---|
| 2268 |
|
---|
| 2269 | // the same leaves must not be part of two merge candidates
|
---|
| 2270 | leaf->Mail();
|
---|
| 2271 | vector<BspLeaf *> neighbors;
|
---|
| 2272 | FindNeighbors(leaf, neighbors, true);
|
---|
| 2273 |
|
---|
| 2274 | vector<BspLeaf *>::const_iterator nit, nit_end = neighbors.end();
|
---|
| 2275 |
|
---|
| 2276 | // TODO: test if at least one ray goes from one leaf to the other
|
---|
| 2277 | for (nit = neighbors.begin(); nit != nit_end; ++ nit)
|
---|
| 2278 | {
|
---|
| 2279 | mMergeQueue.push(BspMergeCandidate(leaf, *nit));
|
---|
| 2280 | }
|
---|
| 2281 | }
|
---|
| 2282 |
|
---|
| 2283 | return (int)leaves.size();
|
---|
| 2284 | }
|
---|
| 2285 |
|
---|
| 2286 |
|
---|
| 2287 | int VspBspTree::CollectMergeCandidates(const VssRayContainer &rays)
|
---|
| 2288 | {
|
---|
| 2289 | vector<BspRay *> bspRays;
|
---|
| 2290 |
|
---|
| 2291 | ConstructBspRays(bspRays, rays);
|
---|
| 2292 | map<BspLeaf *, vector<BspLeaf*> > neighborMap;
|
---|
| 2293 |
|
---|
| 2294 | vector<BspIntersection>::const_iterator iit;
|
---|
| 2295 |
|
---|
| 2296 | int leaves = 0;
|
---|
| 2297 |
|
---|
| 2298 | BspLeaf::NewMail();
|
---|
| 2299 |
|
---|
| 2300 | for (int i = 0; i < (int)bspRays.size(); ++ i)
|
---|
| 2301 | {
|
---|
| 2302 | BspRay *ray = bspRays[i];
|
---|
| 2303 |
|
---|
| 2304 | // traverse leaves stored in the rays and compare and
|
---|
| 2305 | // merge consecutive leaves (i.e., the neighbors in the tree)
|
---|
| 2306 | if (ray->intersections.size() < 2)
|
---|
| 2307 | continue;
|
---|
| 2308 |
|
---|
| 2309 | iit = ray->intersections.begin();
|
---|
[489] | 2310 | BspLeaf *leaf = (*(iit ++)).mLeaf;
|
---|
[485] | 2311 |
|
---|
| 2312 | // create leaf pvs (needed for post processing)
|
---|
[489] | 2313 | if (!leaf->mPvs)
|
---|
[478] | 2314 | {
|
---|
[489] | 2315 | leaf->mPvs =
|
---|
| 2316 | new ObjectPvs(leaf->GetViewCell()->GetPvs());
|
---|
[478] | 2317 |
|
---|
[485] | 2318 | BspMergeCandidate::sOverallCost +=
|
---|
[489] | 2319 | leaf->mArea * leaf->mPvs->GetSize();
|
---|
[485] | 2320 |
|
---|
| 2321 | ++ leaves;
|
---|
| 2322 | }
|
---|
| 2323 |
|
---|
| 2324 | // traverse intersections
|
---|
[489] | 2325 | // consecutive leaves are neighbors => add them to queue
|
---|
[485] | 2326 | for (; iit != ray->intersections.end(); ++ iit)
|
---|
| 2327 | {
|
---|
[489] | 2328 | // next pair
|
---|
| 2329 | BspLeaf *prevLeaf = leaf;
|
---|
| 2330 | leaf = (*iit).mLeaf;
|
---|
| 2331 |
|
---|
| 2332 | // view space does not correspond to valid space
|
---|
| 2333 | if (!leaf->TreeValid() || !prevLeaf->TreeValid())
|
---|
| 2334 | continue;
|
---|
| 2335 |
|
---|
| 2336 | // create leaf pvs (needed for post processing)
|
---|
[485] | 2337 | if (!leaf->mPvs)
|
---|
| 2338 | {
|
---|
| 2339 | leaf->mPvs =
|
---|
| 2340 | new ObjectPvs(leaf->GetViewCell()->GetPvs());
|
---|
| 2341 |
|
---|
| 2342 | BspMergeCandidate::sOverallCost +=
|
---|
| 2343 | leaf->mArea * leaf->mPvs->GetSize();
|
---|
[478] | 2344 |
|
---|
[485] | 2345 | ++ leaves;
|
---|
| 2346 | }
|
---|
| 2347 |
|
---|
| 2348 | vector<BspLeaf *> &neighbors = neighborMap[leaf];
|
---|
| 2349 |
|
---|
| 2350 | bool found = false;
|
---|
[478] | 2351 |
|
---|
[485] | 2352 | // both leaves inserted in queue already =>
|
---|
| 2353 | // look if double pair already exists
|
---|
| 2354 | if (leaf->Mailed() && prevLeaf->Mailed())
|
---|
[478] | 2355 | {
|
---|
[485] | 2356 | vector<BspLeaf *>::const_iterator it, it_end = neighbors.end();
|
---|
| 2357 |
|
---|
| 2358 | for (it = neighbors.begin(); !found && (it != it_end); ++ it)
|
---|
| 2359 | if (*it == prevLeaf)
|
---|
| 2360 | found = true; // already in queue
|
---|
| 2361 | }
|
---|
| 2362 |
|
---|
| 2363 | if (!found)
|
---|
| 2364 | {
|
---|
| 2365 | // this pair is not in map already
|
---|
| 2366 | // => insert into the neighbor map and the queue
|
---|
| 2367 | neighbors.push_back(prevLeaf);
|
---|
| 2368 | neighborMap[prevLeaf].push_back(leaf);
|
---|
[478] | 2369 |
|
---|
[485] | 2370 | leaf->Mail();
|
---|
| 2371 | prevLeaf->Mail();
|
---|
| 2372 |
|
---|
| 2373 | mMergeQueue.push(BspMergeCandidate(leaf, prevLeaf));
|
---|
[478] | 2374 | }
|
---|
[485] | 2375 | }
|
---|
| 2376 | }
|
---|
| 2377 | Debug << "neighbormap size: " << (int)neighborMap.size() << endl;
|
---|
| 2378 | Debug << "mergequeue: " << (int)mMergeQueue.size() << endl;
|
---|
| 2379 | Debug << "leaves in queue: " << leaves << endl;
|
---|
| 2380 | Debug << "overall cost: " << BspMergeCandidate::sOverallCost << endl;
|
---|
| 2381 |
|
---|
| 2382 | CLEAR_CONTAINER(bspRays);
|
---|
| 2383 |
|
---|
| 2384 | return leaves;
|
---|
| 2385 | }
|
---|
| 2386 |
|
---|
| 2387 |
|
---|
| 2388 | void VspBspTree::ConstructBspRays(vector<BspRay *> &bspRays,
|
---|
| 2389 | const VssRayContainer &rays)
|
---|
| 2390 | {
|
---|
| 2391 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
| 2392 |
|
---|
| 2393 | for (it = rays.begin(); it != rays.end(); ++ it)
|
---|
| 2394 | {
|
---|
| 2395 | VssRay *vssRay = *it;
|
---|
| 2396 | BspRay *ray = new BspRay(vssRay);
|
---|
| 2397 |
|
---|
| 2398 | ViewCellContainer viewCells;
|
---|
| 2399 |
|
---|
| 2400 | Ray hray(*vssRay);
|
---|
| 2401 | float tmin = 0, tmax = 1.0;
|
---|
| 2402 | // matt TODO: remove this!!
|
---|
| 2403 | //hray.Init(ray.GetOrigin(), ray.GetDir(), Ray::LINE_SEGMENT);
|
---|
| 2404 | if (!mBox.GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
|
---|
| 2405 | continue;
|
---|
| 2406 |
|
---|
| 2407 | Vector3 origin = hray.Extrap(tmin);
|
---|
| 2408 | Vector3 termination = hray.Extrap(tmax);
|
---|
| 2409 |
|
---|
| 2410 | // cast line segment to get intersections with bsp leaves
|
---|
| 2411 | CastLineSegment(origin, termination, viewCells);
|
---|
| 2412 |
|
---|
| 2413 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
| 2414 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 2415 | {
|
---|
| 2416 | BspViewCell *vc = dynamic_cast<BspViewCell *>(*vit);
|
---|
| 2417 | vector<BspLeaf *>::const_iterator it, it_end = vc->mLeaves.end();
|
---|
| 2418 | //NOTE: not sorted!
|
---|
| 2419 | for (it = vc->mLeaves.begin(); it != it_end; ++ it)
|
---|
| 2420 | ray->intersections.push_back(BspIntersection(0, *it));
|
---|
[478] | 2421 | }
|
---|
[485] | 2422 |
|
---|
| 2423 | bspRays.push_back(ray);
|
---|
[478] | 2424 | }
|
---|
[485] | 2425 | }
|
---|
[478] | 2426 |
|
---|
| 2427 |
|
---|
[485] | 2428 | int VspBspTree::MergeViewCells(const VssRayContainer &rays)
|
---|
| 2429 | {
|
---|
| 2430 | MergeStatistics mergeStats;
|
---|
| 2431 | mergeStats.Start();
|
---|
| 2432 | // TODO: REMOVE LATER for performance!
|
---|
| 2433 | const bool showMergeStats = true;
|
---|
| 2434 | //BspMergeCandidate::sOverallCost = mBox.SurfaceArea() * mStat.maxPvs;
|
---|
| 2435 | long startTime = GetTime();
|
---|
[482] | 2436 |
|
---|
[485] | 2437 | if (mUseRaysForMerge)
|
---|
| 2438 | mergeStats.nodes = CollectMergeCandidates(rays);
|
---|
| 2439 | else
|
---|
| 2440 | mergeStats.nodes = CollectMergeCandidates();
|
---|
| 2441 |
|
---|
| 2442 | mergeStats.collectTime = TimeDiff(startTime, GetTime());
|
---|
| 2443 | mergeStats.candidates = (int)mMergeQueue.size();
|
---|
| 2444 | startTime = GetTime();
|
---|
| 2445 |
|
---|
[490] | 2446 | int nViewCells = /*mergeStats.nodes*/ mStat.Leaves() - mStat.invalidLeaves;
|
---|
[485] | 2447 |
|
---|
[480] | 2448 | //-- use priority queue to merge leaf pairs
|
---|
[485] | 2449 | while (!mMergeQueue.empty() && (nViewCells > mMergeMinViewCells) &&
|
---|
| 2450 | (mMergeQueue.top().GetMergeCost() <
|
---|
[479] | 2451 | mMergeMaxCostRatio * BspMergeCandidate::sOverallCost))
|
---|
[478] | 2452 | {
|
---|
[485] | 2453 | //Debug << "abs mergecost: " << mMergeQueue.top().GetMergeCost() << " rel mergecost: "
|
---|
| 2454 | // << mMergeQueue.top().GetMergeCost() / BspMergeCandidate::sOverallCost
|
---|
| 2455 | // << " max ratio: " << mMergeMaxCostRatio << endl;
|
---|
| 2456 | BspMergeCandidate mc = mMergeQueue.top();
|
---|
| 2457 | mMergeQueue.pop();
|
---|
[478] | 2458 |
|
---|
| 2459 | // both view cells equal!
|
---|
| 2460 | if (mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell())
|
---|
| 2461 | continue;
|
---|
| 2462 |
|
---|
| 2463 | if (mc.Valid())
|
---|
| 2464 | {
|
---|
| 2465 | ViewCell::NewMail();
|
---|
| 2466 | MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2());
|
---|
[485] | 2467 | -- nViewCells;
|
---|
| 2468 |
|
---|
| 2469 | ++ mergeStats.merged;
|
---|
| 2470 |
|
---|
[478] | 2471 | // increase absolute merge cost
|
---|
| 2472 | BspMergeCandidate::sOverallCost += mc.GetMergeCost();
|
---|
[482] | 2473 |
|
---|
[485] | 2474 | if (showMergeStats)
|
---|
[482] | 2475 | {
|
---|
[485] | 2476 | if (mc.GetLeaf1()->IsSibling(mc.GetLeaf2()))
|
---|
| 2477 | ++ mergeStats.siblings;
|
---|
[482] | 2478 |
|
---|
[485] | 2479 | const int dist =
|
---|
| 2480 | TreeDistance(mc.GetLeaf1(), mc.GetLeaf2());
|
---|
| 2481 | if (dist > mergeStats.maxTreeDist)
|
---|
| 2482 | mergeStats.maxTreeDist = dist;
|
---|
| 2483 | mergeStats.accTreeDist += dist;
|
---|
[482] | 2484 | }
|
---|
[478] | 2485 | }
|
---|
| 2486 | // merge candidate not valid, because one of the leaves was already
|
---|
[485] | 2487 | // merged with another one => validate and reinsert into queue
|
---|
[478] | 2488 | else
|
---|
[485] | 2489 | {
|
---|
[478] | 2490 | mc.SetValid();
|
---|
[485] | 2491 | mMergeQueue.push(mc);
|
---|
[478] | 2492 | }
|
---|
| 2493 | }
|
---|
| 2494 |
|
---|
[485] | 2495 | mergeStats.mergeTime = TimeDiff(startTime, GetTime());
|
---|
| 2496 | mergeStats.Stop();
|
---|
[478] | 2497 |
|
---|
[485] | 2498 | if (showMergeStats)
|
---|
| 2499 | Debug << mergeStats << endl << endl;
|
---|
| 2500 |
|
---|
| 2501 | //TODO: should return sample contributions?
|
---|
| 2502 | return mergeStats.merged;
|
---|
| 2503 | }
|
---|
[478] | 2504 |
|
---|
[485] | 2505 |
|
---|
| 2506 | int VspBspTree::RefineViewCells(const VssRayContainer &rays)
|
---|
| 2507 | {
|
---|
| 2508 | int shuffled = 0;
|
---|
| 2509 |
|
---|
| 2510 | Debug << "refining " << (int)mMergeQueue.size() << " candidates " << endl;
|
---|
| 2511 | BspLeaf::NewMail();
|
---|
| 2512 |
|
---|
| 2513 | // Use priority queue of remaining leaf pairs
|
---|
| 2514 | // These candidates either share the same view cells or
|
---|
| 2515 | // are border leaves which share a boundary.
|
---|
| 2516 | // We test if they can be shuffled, i.e.,
|
---|
| 2517 | // either one leaf is made part of one view cell or the other
|
---|
| 2518 | // leaf is made part of the other view cell. It is tested if the
|
---|
| 2519 | // remaining view cells are "better" than the old ones.
|
---|
| 2520 | while (!mMergeQueue.empty())
|
---|
[482] | 2521 | {
|
---|
[485] | 2522 | BspMergeCandidate mc = mMergeQueue.top();
|
---|
| 2523 | mMergeQueue.pop();
|
---|
| 2524 |
|
---|
| 2525 | // both view cells equal or already shuffled
|
---|
| 2526 | if ((mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell()) ||
|
---|
| 2527 | (mc.GetLeaf1()->Mailed()) || (mc.GetLeaf2()->Mailed()))
|
---|
| 2528 | continue;
|
---|
| 2529 |
|
---|
| 2530 | // candidate for shuffling
|
---|
| 2531 | const bool wasShuffled =
|
---|
| 2532 | ShuffleLeaves(mc.GetLeaf1(), mc.GetLeaf2());
|
---|
| 2533 |
|
---|
| 2534 | //-- stats
|
---|
| 2535 | if (wasShuffled)
|
---|
| 2536 | ++ shuffled;
|
---|
[482] | 2537 | }
|
---|
| 2538 |
|
---|
[485] | 2539 | return shuffled;
|
---|
[478] | 2540 | }
|
---|
| 2541 |
|
---|
| 2542 |
|
---|
[485] | 2543 | inline int AddedPvsSize(ObjectPvs pvs1, const ObjectPvs &pvs2)
|
---|
| 2544 | {
|
---|
| 2545 | return pvs1.AddPvs(pvs2);
|
---|
[478] | 2546 | }
|
---|
| 2547 |
|
---|
[485] | 2548 | /*inline int SubtractedPvsSize(BspViewCell *vc, BspLeaf *l, const ObjectPvs &pvs2)
|
---|
| 2549 | {
|
---|
| 2550 | ObjectPvs pvs;
|
---|
| 2551 | vector<BspLeaf *>::const_iterator it, it_end = vc->mLeaves.end();
|
---|
| 2552 | for (it = vc->mLeaves.begin(); it != vc->mLeaves.end(); ++ it)
|
---|
| 2553 | if (*it != l)
|
---|
| 2554 | pvs.AddPvs(*(*it)->mPvs);
|
---|
| 2555 | return pvs.GetSize();
|
---|
| 2556 | }*/
|
---|
[478] | 2557 |
|
---|
[485] | 2558 | inline int SubtractedPvsSize(ObjectPvs pvs1, const ObjectPvs &pvs2)
|
---|
[478] | 2559 | {
|
---|
[485] | 2560 | return pvs1.SubtractPvs(pvs2);
|
---|
[478] | 2561 | }
|
---|
| 2562 |
|
---|
[485] | 2563 |
|
---|
| 2564 | float GetShuffledVcCost(BspLeaf *leaf, BspViewCell *vc1, BspViewCell *vc2)
|
---|
| 2565 | {
|
---|
| 2566 | //const int pvs1 = SubtractedPvsSize(vc1, leaf, *leaf->mPvs);
|
---|
| 2567 | const int pvs1 = SubtractedPvsSize(vc1->GetPvs(), *leaf->mPvs);
|
---|
| 2568 | const int pvs2 = AddedPvsSize(vc2->GetPvs(), *leaf->mPvs);
|
---|
| 2569 |
|
---|
| 2570 | const float area1 = vc1->GetArea() - leaf->mArea;
|
---|
| 2571 | const float area2 = vc2->GetArea() + leaf->mArea;
|
---|
| 2572 |
|
---|
| 2573 | const float cost1 = pvs1 * area1;
|
---|
| 2574 | const float cost2 = pvs2 * area2;
|
---|
| 2575 |
|
---|
| 2576 | return cost1 + cost2;
|
---|
| 2577 | }
|
---|
| 2578 |
|
---|
| 2579 |
|
---|
| 2580 | void VspBspTree::ShuffleLeaf(BspLeaf *leaf,
|
---|
| 2581 | BspViewCell *vc1,
|
---|
| 2582 | BspViewCell *vc2) const
|
---|
| 2583 | {
|
---|
| 2584 | // compute new pvs and area
|
---|
| 2585 | vc1->GetPvs().SubtractPvs(*leaf->mPvs);
|
---|
| 2586 | vc2->GetPvs().AddPvs(*leaf->mPvs);
|
---|
| 2587 |
|
---|
| 2588 | vc1->SetArea(vc1->GetArea() - leaf->mArea);
|
---|
| 2589 | vc2->SetArea(vc2->GetArea() + leaf->mArea);
|
---|
| 2590 |
|
---|
| 2591 | /// add to second view cell
|
---|
| 2592 | vc2->mLeaves.push_back(leaf);
|
---|
| 2593 |
|
---|
| 2594 | // erase leaf from old view cell
|
---|
| 2595 | vector<BspLeaf *>::iterator it = vc1->mLeaves.begin();
|
---|
| 2596 |
|
---|
| 2597 | for (; *it != leaf; ++ it);
|
---|
| 2598 | vc1->mLeaves.erase(it);
|
---|
| 2599 |
|
---|
| 2600 | /*vc1->GetPvs().mEntries.clear();
|
---|
| 2601 | for (; it != vc1->mLeaves.end(); ++ it)
|
---|
| 2602 | {
|
---|
| 2603 | if (*it == leaf)
|
---|
| 2604 | vc1->mLeaves.erase(it);
|
---|
| 2605 | else
|
---|
| 2606 | vc1->GetPvs().AddPvs(*(*it)->mPvs);
|
---|
| 2607 | }*/
|
---|
| 2608 |
|
---|
[486] | 2609 | leaf->SetViewCell(vc2); // finally change view cell
|
---|
[485] | 2610 |
|
---|
| 2611 | //Debug << "new pvs: " << vc1->GetPvs().GetSize() + vc2->GetPvs().GetSize()
|
---|
| 2612 | // << " (" << vc1->GetPvs().GetSize() << ", " << vc2->GetPvs().GetSize() << ")" << endl;
|
---|
| 2613 |
|
---|
| 2614 | }
|
---|
| 2615 |
|
---|
| 2616 |
|
---|
| 2617 | bool VspBspTree::ShuffleLeaves(BspLeaf *leaf1, BspLeaf *leaf2) const
|
---|
| 2618 | {
|
---|
| 2619 | BspViewCell *vc1 = leaf1->GetViewCell();
|
---|
| 2620 | BspViewCell *vc2 = leaf2->GetViewCell();
|
---|
| 2621 |
|
---|
| 2622 | const float cost1 = vc1->GetPvs().GetSize() * vc1->GetArea();
|
---|
| 2623 | const float cost2 = vc2->GetPvs().GetSize() * vc2->GetArea();
|
---|
| 2624 |
|
---|
| 2625 | const float oldCost = cost1 + cost2;
|
---|
| 2626 |
|
---|
| 2627 | float shuffledCost1 = Limits::Infinity;
|
---|
| 2628 | float shuffledCost2 = Limits::Infinity;
|
---|
| 2629 |
|
---|
| 2630 | // the view cell should not be empty after the shuffle
|
---|
| 2631 | if (vc1->mLeaves.size() > 1)
|
---|
| 2632 | shuffledCost1 = GetShuffledVcCost(leaf1, vc1, vc2);
|
---|
| 2633 | if (vc2->mLeaves.size() > 1)
|
---|
| 2634 | shuffledCost2 = GetShuffledVcCost(leaf2, vc2, vc1);
|
---|
| 2635 |
|
---|
| 2636 | // shuffling unsuccessful
|
---|
| 2637 | if ((oldCost <= shuffledCost1) && (oldCost <= shuffledCost2))
|
---|
| 2638 | return false;
|
---|
| 2639 |
|
---|
| 2640 | if (shuffledCost1 < shuffledCost2)
|
---|
| 2641 | {
|
---|
| 2642 | //Debug << "old cost: " << oldCost << ", new cost: " << shuffledCost1 << endl;
|
---|
| 2643 | ShuffleLeaf(leaf1, vc1, vc2);
|
---|
| 2644 | leaf1->Mail();
|
---|
| 2645 | }
|
---|
| 2646 | else
|
---|
| 2647 | {
|
---|
| 2648 | //Debug << "old cost: " << oldCost << ", new cost: " << shuffledCost2 << endl;
|
---|
| 2649 | ShuffleLeaf(leaf2, vc2, vc1);
|
---|
| 2650 | leaf2->Mail();
|
---|
| 2651 | }
|
---|
| 2652 |
|
---|
| 2653 | return true;
|
---|
| 2654 | }
|
---|
| 2655 |
|
---|
[487] | 2656 | bool VspBspTree::ViewPointValid(const Vector3 &viewPoint) const
|
---|
| 2657 | {
|
---|
| 2658 | BspNode *node = mRoot;
|
---|
[485] | 2659 |
|
---|
[487] | 2660 | while (1)
|
---|
| 2661 | {
|
---|
| 2662 | // early exit
|
---|
| 2663 | if (node->TreeValid())
|
---|
| 2664 | return true;
|
---|
| 2665 |
|
---|
| 2666 | if (node->IsLeaf())
|
---|
| 2667 | return false;
|
---|
| 2668 |
|
---|
| 2669 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
[490] | 2670 |
|
---|
| 2671 | if (in->GetPlane().Side(viewPoint) <= 0)
|
---|
[487] | 2672 | {
|
---|
| 2673 | node = in->GetBack();
|
---|
| 2674 | }
|
---|
| 2675 | else
|
---|
| 2676 | {
|
---|
| 2677 | node = in->GetFront();
|
---|
| 2678 | }
|
---|
| 2679 | }
|
---|
| 2680 |
|
---|
| 2681 | // should never come here
|
---|
| 2682 | return false;
|
---|
| 2683 | }
|
---|
| 2684 |
|
---|
| 2685 |
|
---|
| 2686 | bool VspBspTree::CheckValid(const VspBspTraversalData &data) const
|
---|
| 2687 | {
|
---|
| 2688 | return data.mPvs <= mMaxPvs;
|
---|
| 2689 | }
|
---|
| 2690 |
|
---|
| 2691 |
|
---|
| 2692 | void VspBspTree::PropagateUpValidity(BspNode *node)
|
---|
| 2693 | {
|
---|
[490] | 2694 | while (!node->IsRoot() && node->GetParent()->TreeValid())
|
---|
[487] | 2695 | {
|
---|
| 2696 | node = node->GetParent();
|
---|
| 2697 | node->SetTreeValid(false);
|
---|
| 2698 | }
|
---|
| 2699 | }
|
---|
| 2700 |
|
---|
| 2701 |
|
---|
[478] | 2702 | /************************************************************************/
|
---|
| 2703 | /* BspMergeCandidate implementation */
|
---|
| 2704 | /************************************************************************/
|
---|
| 2705 |
|
---|
| 2706 |
|
---|
| 2707 | BspMergeCandidate::BspMergeCandidate(BspLeaf *l1, BspLeaf *l2):
|
---|
| 2708 | mMergeCost(0),
|
---|
| 2709 | mLeaf1(l1),
|
---|
| 2710 | mLeaf2(l2),
|
---|
| 2711 | mLeaf1Id(l1->GetViewCell()->mMailbox),
|
---|
| 2712 | mLeaf2Id(l2->GetViewCell()->mMailbox)
|
---|
| 2713 | {
|
---|
| 2714 | EvalMergeCost();
|
---|
| 2715 | }
|
---|
| 2716 |
|
---|
[485] | 2717 | float BspMergeCandidate::GetCost(ViewCell *vc) const
|
---|
| 2718 | {
|
---|
| 2719 | return vc->GetPvs().GetSize() * vc->GetArea();
|
---|
| 2720 | }
|
---|
| 2721 |
|
---|
[478] | 2722 | float BspMergeCandidate::GetLeaf1Cost() const
|
---|
| 2723 | {
|
---|
| 2724 | BspViewCell *vc = mLeaf1->GetViewCell();
|
---|
[485] | 2725 | return GetCost(vc);
|
---|
[478] | 2726 | }
|
---|
| 2727 |
|
---|
| 2728 | float BspMergeCandidate::GetLeaf2Cost() const
|
---|
| 2729 | {
|
---|
| 2730 | BspViewCell *vc = mLeaf2->GetViewCell();
|
---|
[485] | 2731 | return GetCost(vc);
|
---|
[478] | 2732 | }
|
---|
| 2733 |
|
---|
| 2734 | void BspMergeCandidate::EvalMergeCost()
|
---|
| 2735 | {
|
---|
| 2736 | //-- compute pvs difference
|
---|
| 2737 | BspViewCell *vc1 = mLeaf1->GetViewCell();
|
---|
| 2738 | BspViewCell *vc2 = mLeaf2->GetViewCell();
|
---|
| 2739 |
|
---|
| 2740 | const int diff1 = vc1->GetPvs().Diff(vc2->GetPvs());
|
---|
[485] | 2741 | const int newPvs = diff1 + vc1->GetPvs().GetSize();
|
---|
[478] | 2742 |
|
---|
| 2743 | //-- compute ratio of old cost
|
---|
| 2744 | //-- (added size of left and right view cell times pvs size)
|
---|
| 2745 | //-- to new rendering cost (size of merged view cell times pvs size)
|
---|
| 2746 | const float oldCost = GetLeaf1Cost() + GetLeaf2Cost();
|
---|
[482] | 2747 |
|
---|
[478] | 2748 | const float newCost =
|
---|
[485] | 2749 | (float)newPvs * (vc1->GetArea() + vc2->GetArea());
|
---|
[478] | 2750 |
|
---|
| 2751 | mMergeCost = newCost - oldCost;
|
---|
| 2752 | // if (vcPvs > sMaxPvsSize) // strong penalty if pvs size too large
|
---|
| 2753 | // mMergeCost += 1.0;
|
---|
| 2754 | }
|
---|
| 2755 |
|
---|
| 2756 | void BspMergeCandidate::SetLeaf1(BspLeaf *l)
|
---|
| 2757 | {
|
---|
| 2758 | mLeaf1 = l;
|
---|
| 2759 | }
|
---|
| 2760 |
|
---|
| 2761 | void BspMergeCandidate::SetLeaf2(BspLeaf *l)
|
---|
| 2762 | {
|
---|
| 2763 | mLeaf2 = l;
|
---|
| 2764 | }
|
---|
| 2765 |
|
---|
| 2766 | BspLeaf *BspMergeCandidate::GetLeaf1()
|
---|
| 2767 | {
|
---|
| 2768 | return mLeaf1;
|
---|
| 2769 | }
|
---|
| 2770 |
|
---|
| 2771 | BspLeaf *BspMergeCandidate::GetLeaf2()
|
---|
| 2772 | {
|
---|
| 2773 | return mLeaf2;
|
---|
| 2774 | }
|
---|
| 2775 |
|
---|
| 2776 | bool BspMergeCandidate::Valid() const
|
---|
| 2777 | {
|
---|
| 2778 | return
|
---|
| 2779 | (mLeaf1->GetViewCell()->mMailbox == mLeaf1Id) &&
|
---|
| 2780 | (mLeaf2->GetViewCell()->mMailbox == mLeaf2Id);
|
---|
| 2781 | }
|
---|
| 2782 |
|
---|
| 2783 | float BspMergeCandidate::GetMergeCost() const
|
---|
| 2784 | {
|
---|
| 2785 | return mMergeCost;
|
---|
| 2786 | }
|
---|
| 2787 |
|
---|
| 2788 | void BspMergeCandidate::SetValid()
|
---|
| 2789 | {
|
---|
| 2790 | mLeaf1Id = mLeaf1->GetViewCell()->mMailbox;
|
---|
| 2791 | mLeaf2Id = mLeaf2->GetViewCell()->mMailbox;
|
---|
| 2792 |
|
---|
| 2793 | EvalMergeCost();
|
---|
| 2794 | }
|
---|
[485] | 2795 |
|
---|
| 2796 |
|
---|
| 2797 | /************************************************************************/
|
---|
| 2798 | /* MergeStatistics implementation */
|
---|
| 2799 | /************************************************************************/
|
---|
| 2800 |
|
---|
| 2801 |
|
---|
| 2802 | void MergeStatistics::Print(ostream &app) const
|
---|
| 2803 | {
|
---|
| 2804 | app << "===== Merge statistics ===============\n";
|
---|
| 2805 |
|
---|
| 2806 | app << setprecision(4);
|
---|
| 2807 |
|
---|
| 2808 | app << "#N_CTIME ( Overall time [s] )\n" << Time() << " \n";
|
---|
| 2809 |
|
---|
| 2810 | app << "#N_CCTIME ( Collect candidates time [s] )\n" << collectTime * 1e-3f << " \n";
|
---|
| 2811 |
|
---|
| 2812 | app << "#N_MTIME ( Merge time [s] )\n" << mergeTime * 1e-3f << " \n";
|
---|
| 2813 |
|
---|
| 2814 | app << "#N_NODES ( Number of nodes before merge )\n" << nodes << "\n";
|
---|
| 2815 |
|
---|
| 2816 | app << "#N_CANDIDATES ( Number of merge candidates )\n" << candidates << "\n";
|
---|
| 2817 |
|
---|
| 2818 | app << "#N_MERGEDSIBLINGS ( Number of merged siblings )\n" << siblings << "\n";
|
---|
| 2819 | app << "#N_MERGEDNODES ( Number of merged nodes )\n" << merged << "\n";
|
---|
| 2820 |
|
---|
| 2821 | app << "#MAX_TREEDIST ( Maximal distance in tree of merged leaves )\n" << maxTreeDist << "\n";
|
---|
| 2822 |
|
---|
| 2823 | app << "#AVG_TREEDIST ( Average distance in tree of merged leaves )\n" << AvgTreeDist() << "\n";
|
---|
| 2824 |
|
---|
| 2825 | app << "===== END OF BspTree statistics ==========\n";
|
---|
| 2826 | }
|
---|