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