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