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