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