[375] | 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" |
---|
| 10 | #include <stack> |
---|
[462] | 11 | |
---|
[375] | 12 | #include "Exporter.h" |
---|
| 13 | #include "Plane3.h" |
---|
| 14 | |
---|
[421] | 15 | //-- static members |
---|
[375] | 16 | |
---|
[421] | 17 | int BspNode::sMailId = 1; |
---|
| 18 | |
---|
[375] | 19 | /** Evaluates split plane classification with respect to the plane's |
---|
| 20 | contribution for a minimum number splits in the tree. |
---|
| 21 | */ |
---|
[421] | 22 | const float BspTree::sLeastPolySplitsTable[] = {0, 0, 1, 0}; |
---|
[375] | 23 | /** Evaluates split plane classification with respect to the plane's |
---|
| 24 | contribution for a balanced tree. |
---|
| 25 | */ |
---|
[421] | 26 | const float BspTree::sBalancedPolysTable[] = {1, -1, 0, 0}; |
---|
[375] | 27 | |
---|
| 28 | /** Evaluates split plane classification with respect to the plane's |
---|
| 29 | contribution for a minimum number of ray splits. |
---|
| 30 | */ |
---|
[421] | 31 | const float BspTree::sLeastRaySplitsTable[] = {0, 0, 1, 1, 0}; |
---|
[375] | 32 | /** Evaluates split plane classification with respect to the plane's |
---|
| 33 | contribution for balanced rays. |
---|
| 34 | */ |
---|
[421] | 35 | const float BspTree::sBalancedRaysTable[] = {1, -1, 0, 0, 0}; |
---|
[375] | 36 | |
---|
[428] | 37 | int BspTree::sFrontId = 0; |
---|
| 38 | int BspTree::sBackId = 0; |
---|
| 39 | int BspTree::sFrontAndBackId = 0; |
---|
[421] | 40 | |
---|
[375] | 41 | /****************************************************************/ |
---|
| 42 | /* class BspNode implementation */ |
---|
| 43 | /****************************************************************/ |
---|
| 44 | |
---|
| 45 | BspNode::BspNode(): |
---|
[390] | 46 | mParent(NULL) |
---|
[375] | 47 | {} |
---|
| 48 | |
---|
| 49 | BspNode::BspNode(BspInterior *parent): |
---|
[390] | 50 | mParent(parent) |
---|
[375] | 51 | {} |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | bool BspNode::IsRoot() const |
---|
| 55 | { |
---|
| 56 | return mParent == NULL; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | BspInterior *BspNode::GetParent() |
---|
| 60 | { |
---|
| 61 | return mParent; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void BspNode::SetParent(BspInterior *parent) |
---|
| 65 | { |
---|
| 66 | mParent = parent; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /****************************************************************/ |
---|
| 70 | /* class BspInterior implementation */ |
---|
| 71 | /****************************************************************/ |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | BspInterior::BspInterior(const Plane3 &plane): |
---|
| 75 | mPlane(plane), mFront(NULL), mBack(NULL) |
---|
| 76 | {} |
---|
| 77 | |
---|
[411] | 78 | BspInterior::~BspInterior() |
---|
| 79 | { |
---|
| 80 | DEL_PTR(mFront); |
---|
| 81 | DEL_PTR(mBack); |
---|
| 82 | } |
---|
| 83 | |
---|
[375] | 84 | bool BspInterior::IsLeaf() const |
---|
| 85 | { |
---|
| 86 | return false; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | BspNode *BspInterior::GetBack() |
---|
| 90 | { |
---|
| 91 | return mBack; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | BspNode *BspInterior::GetFront() |
---|
| 95 | { |
---|
| 96 | return mFront; |
---|
| 97 | } |
---|
| 98 | |
---|
[448] | 99 | Plane3 BspInterior::GetPlane() const |
---|
[375] | 100 | { |
---|
[448] | 101 | return mPlane; |
---|
[375] | 102 | } |
---|
| 103 | |
---|
| 104 | void BspInterior::ReplaceChildLink(BspNode *oldChild, BspNode *newChild) |
---|
| 105 | { |
---|
| 106 | if (mBack == oldChild) |
---|
| 107 | mBack = newChild; |
---|
| 108 | else |
---|
| 109 | mFront = newChild; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void BspInterior::SetupChildLinks(BspNode *b, BspNode *f) |
---|
| 113 | { |
---|
| 114 | mBack = b; |
---|
| 115 | mFront = f; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /****************************************************************/ |
---|
| 119 | /* class BspLeaf implementation */ |
---|
| 120 | /****************************************************************/ |
---|
[448] | 121 | |
---|
| 122 | |
---|
[375] | 123 | BspLeaf::BspLeaf(): mViewCell(NULL) |
---|
| 124 | { |
---|
| 125 | } |
---|
| 126 | |
---|
[448] | 127 | |
---|
[375] | 128 | BspLeaf::BspLeaf(BspViewCell *viewCell): |
---|
| 129 | mViewCell(viewCell) |
---|
| 130 | { |
---|
| 131 | } |
---|
| 132 | |
---|
[448] | 133 | |
---|
[375] | 134 | BspLeaf::BspLeaf(BspInterior *parent): |
---|
| 135 | BspNode(parent), mViewCell(NULL) |
---|
| 136 | {} |
---|
| 137 | |
---|
[448] | 138 | |
---|
| 139 | |
---|
[375] | 140 | BspLeaf::BspLeaf(BspInterior *parent, BspViewCell *viewCell): |
---|
| 141 | BspNode(parent), mViewCell(viewCell) |
---|
| 142 | { |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | BspViewCell *BspLeaf::GetViewCell() const |
---|
| 146 | { |
---|
| 147 | return mViewCell; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | void BspLeaf::SetViewCell(BspViewCell *viewCell) |
---|
| 151 | { |
---|
| 152 | mViewCell = viewCell; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | bool BspLeaf::IsLeaf() const |
---|
| 156 | { |
---|
| 157 | return true; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | /****************************************************************/ |
---|
| 162 | /* class BspTree implementation */ |
---|
| 163 | /****************************************************************/ |
---|
| 164 | |
---|
[441] | 165 | BspTree::BspTree(): |
---|
[420] | 166 | mRoot(NULL), |
---|
[442] | 167 | mPvsUseArea(true), |
---|
[437] | 168 | mGenerateViewCells(true) |
---|
[375] | 169 | { |
---|
| 170 | Randomize(); // initialise random generator for heuristics |
---|
[421] | 171 | |
---|
[441] | 172 | // the view cell corresponding to unbounded space |
---|
| 173 | mRootCell = new BspViewCell(); |
---|
| 174 | |
---|
[421] | 175 | //-- termination criteria for autopartition |
---|
| 176 | environment->GetIntValue("BspTree.Termination.maxDepth", mTermMaxDepth); |
---|
| 177 | environment->GetIntValue("BspTree.Termination.minPvs", mTermMinPvs); |
---|
[426] | 178 | environment->GetIntValue("BspTree.Termination.minPolygons", mTermMinPolys); |
---|
| 179 | environment->GetIntValue("BspTree.Termination.minRays", mTermMinRays); |
---|
[421] | 180 | environment->GetFloatValue("BspTree.Termination.minArea", mTermMinArea); |
---|
| 181 | environment->GetFloatValue("BspTree.Termination.maxRayContribution", mTermMaxRayContribution); |
---|
[426] | 182 | environment->GetFloatValue("BspTree.Termination.minAccRayLenght", mTermMinAccRayLength); |
---|
[421] | 183 | |
---|
[424] | 184 | //-- factors for bsp tree split plane heuristics |
---|
| 185 | environment->GetFloatValue("BspTree.Factor.verticalSplits", mVerticalSplitsFactor); |
---|
| 186 | environment->GetFloatValue("BspTree.Factor.largestPolyArea", mLargestPolyAreaFactor); |
---|
| 187 | environment->GetFloatValue("BspTree.Factor.blockedRays", mBlockedRaysFactor); |
---|
| 188 | environment->GetFloatValue("BspTree.Factor.leastRaySplits", mLeastRaySplitsFactor); |
---|
| 189 | environment->GetFloatValue("BspTree.Factor.balancedRays", mBalancedRaysFactor); |
---|
[445] | 190 | environment->GetFloatValue("BspTree.Factor.pvs", mPvsFactor); |
---|
[424] | 191 | environment->GetFloatValue("BspTree.Factor.leastSplits" , mLeastSplitsFactor); |
---|
| 192 | environment->GetFloatValue("BspTree.Factor.balancedPolys", mBalancedPolysFactor); |
---|
| 193 | environment->GetFloatValue("BspTree.Factor.balancedViewCells", mBalancedViewCellsFactor); |
---|
| 194 | environment->GetFloatValue("BspTree.Termination.ct_div_ci", mCtDivCi); |
---|
| 195 | |
---|
[421] | 196 | //-- termination criteria for axis aligned split |
---|
[424] | 197 | environment->GetFloatValue("BspTree.Termination.AxisAligned.ct_div_ci", mAaCtDivCi); |
---|
[421] | 198 | environment->GetFloatValue("BspTree.Termination.AxisAligned.maxCostRatio", mMaxCostRatio); |
---|
[426] | 199 | environment->GetIntValue("BspTree.Termination.AxisAligned.minPolys", |
---|
| 200 | mTermMinPolysForAxisAligned); |
---|
[430] | 201 | environment->GetIntValue("BspTree.Termination.AxisAligned.minRays", |
---|
[426] | 202 | mTermMinRaysForAxisAligned); |
---|
[430] | 203 | environment->GetIntValue("BspTree.Termination.AxisAligned.minObjects", |
---|
[426] | 204 | mTermMinObjectsForAxisAligned); |
---|
[421] | 205 | //-- partition criteria |
---|
| 206 | environment->GetIntValue("BspTree.maxPolyCandidates", mMaxPolyCandidates); |
---|
| 207 | environment->GetIntValue("BspTree.maxRayCandidates", mMaxRayCandidates); |
---|
| 208 | environment->GetIntValue("BspTree.splitPlaneStrategy", mSplitPlaneStrategy); |
---|
| 209 | environment->GetFloatValue("BspTree.AxisAligned.splitBorder", mSplitBorder); |
---|
[441] | 210 | environment->GetIntValue("BspTree.maxTests", mMaxTests); |
---|
[421] | 211 | |
---|
[448] | 212 | environment->GetFloatValue("BspTree.Construction.epsilon", mEpsilon); |
---|
| 213 | |
---|
[421] | 214 | Debug << "BSP max depth: " << mTermMaxDepth << endl; |
---|
| 215 | Debug << "BSP min PVS: " << mTermMinPvs << endl; |
---|
| 216 | Debug << "BSP min area: " << mTermMinArea << endl; |
---|
[426] | 217 | Debug << "BSP max polys: " << mTermMinPolys << endl; |
---|
| 218 | Debug << "BSP max rays: " << mTermMinRays << endl; |
---|
[421] | 219 | Debug << "BSP max polygon candidates: " << mMaxPolyCandidates << endl; |
---|
| 220 | Debug << "BSP max plane candidates: " << mMaxRayCandidates << endl; |
---|
| 221 | |
---|
| 222 | Debug << "Split plane strategy: "; |
---|
| 223 | if (mSplitPlaneStrategy & RANDOM_POLYGON) |
---|
| 224 | Debug << "random polygon "; |
---|
| 225 | if (mSplitPlaneStrategy & AXIS_ALIGNED) |
---|
| 226 | Debug << "axis aligned "; |
---|
| 227 | if (mSplitPlaneStrategy & LEAST_SPLITS) |
---|
| 228 | Debug << "least splits "; |
---|
| 229 | if (mSplitPlaneStrategy & BALANCED_POLYS) |
---|
| 230 | Debug << "balanced polygons "; |
---|
| 231 | if (mSplitPlaneStrategy & BALANCED_VIEW_CELLS) |
---|
| 232 | Debug << "balanced view cells "; |
---|
| 233 | if (mSplitPlaneStrategy & LARGEST_POLY_AREA) |
---|
| 234 | Debug << "largest polygon area "; |
---|
| 235 | if (mSplitPlaneStrategy & VERTICAL_AXIS) |
---|
| 236 | Debug << "vertical axis "; |
---|
| 237 | if (mSplitPlaneStrategy & BLOCKED_RAYS) |
---|
| 238 | Debug << "blocked rays "; |
---|
| 239 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) |
---|
| 240 | Debug << "least ray splits "; |
---|
| 241 | if (mSplitPlaneStrategy & BALANCED_RAYS) |
---|
| 242 | Debug << "balanced rays "; |
---|
| 243 | if (mSplitPlaneStrategy & PVS) |
---|
| 244 | Debug << "pvs"; |
---|
| 245 | |
---|
| 246 | Debug << endl; |
---|
[375] | 247 | } |
---|
[421] | 248 | |
---|
[448] | 249 | |
---|
[375] | 250 | const BspTreeStatistics &BspTree::GetStatistics() const |
---|
| 251 | { |
---|
| 252 | return mStat; |
---|
| 253 | } |
---|
| 254 | |
---|
[448] | 255 | |
---|
| 256 | int BspTree::SplitPolygons(const Plane3 &plane, |
---|
| 257 | PolygonContainer &polys, |
---|
| 258 | PolygonContainer &frontPolys, |
---|
| 259 | PolygonContainer &backPolys, |
---|
| 260 | PolygonContainer &coincident) const |
---|
| 261 | { |
---|
| 262 | int splits = 0; |
---|
| 263 | |
---|
| 264 | #ifdef _Debug |
---|
| 265 | Debug << "splitting polygons of node " << this << " with plane " << mPlane << endl; |
---|
| 266 | #endif |
---|
| 267 | while (!polys.empty()) |
---|
| 268 | { |
---|
| 269 | Polygon3 *poly = polys.back(); |
---|
| 270 | polys.pop_back(); |
---|
| 271 | |
---|
| 272 | //Debug << "New polygon with plane: " << poly->GetSupportingPlane() << "\n"; |
---|
| 273 | |
---|
| 274 | // classify polygon |
---|
| 275 | const int cf = poly->ClassifyPlane(plane, mEpsilon); |
---|
| 276 | |
---|
| 277 | switch (cf) |
---|
| 278 | { |
---|
| 279 | case Polygon3::COINCIDENT: |
---|
| 280 | coincident.push_back(poly); |
---|
| 281 | break; |
---|
| 282 | case Polygon3::FRONT_SIDE: |
---|
| 283 | frontPolys.push_back(poly); |
---|
| 284 | break; |
---|
| 285 | case Polygon3::BACK_SIDE: |
---|
| 286 | backPolys.push_back(poly); |
---|
| 287 | break; |
---|
| 288 | case Polygon3::SPLIT: |
---|
| 289 | { |
---|
| 290 | Polygon3 *front_piece = new Polygon3(poly->mParent); |
---|
| 291 | Polygon3 *back_piece = new Polygon3(poly->mParent); |
---|
| 292 | |
---|
| 293 | //-- split polygon into front and back part |
---|
| 294 | poly->Split(plane, |
---|
| 295 | *front_piece, |
---|
| 296 | *back_piece, |
---|
| 297 | mEpsilon); |
---|
| 298 | |
---|
| 299 | ++ splits; // increase number of splits |
---|
| 300 | |
---|
| 301 | //-- inherit rays from parent polygon for blocked ray criterium |
---|
| 302 | poly->InheritRays(*front_piece, *back_piece); |
---|
| 303 | |
---|
| 304 | // check if polygons still valid |
---|
| 305 | if (front_piece->Valid(mEpsilon)) |
---|
| 306 | frontPolys.push_back(front_piece); |
---|
| 307 | else |
---|
| 308 | DEL_PTR(front_piece); |
---|
| 309 | |
---|
| 310 | if (back_piece->Valid(mEpsilon)) |
---|
| 311 | backPolys.push_back(back_piece); |
---|
| 312 | else |
---|
| 313 | DEL_PTR(back_piece); |
---|
| 314 | |
---|
| 315 | #ifdef _DEBUG |
---|
| 316 | Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl; |
---|
| 317 | #endif |
---|
| 318 | DEL_PTR(poly); |
---|
| 319 | } |
---|
| 320 | break; |
---|
| 321 | default: |
---|
| 322 | Debug << "SHOULD NEVER COME HERE\n"; |
---|
| 323 | break; |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | return splits; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | |
---|
[375] | 331 | void BspTreeStatistics::Print(ostream &app) const |
---|
| 332 | { |
---|
| 333 | app << "===== BspTree statistics ===============\n"; |
---|
| 334 | |
---|
| 335 | app << setprecision(4); |
---|
| 336 | |
---|
[414] | 337 | app << "#N_CTIME ( Construction time [s] )\n" << Time() << " \n"; |
---|
| 338 | |
---|
[375] | 339 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n"; |
---|
| 340 | |
---|
| 341 | app << "#N_INTERIORS ( Number of interior nodes )\n" << Interior() << "\n"; |
---|
| 342 | |
---|
| 343 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n"; |
---|
| 344 | |
---|
| 345 | app << "#N_SPLITS ( Number of splits )\n" << splits << "\n"; |
---|
| 346 | |
---|
[426] | 347 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maximum depth )\n" |
---|
| 348 | << maxDepthNodes * 100 / (double)Leaves() << endl; |
---|
[375] | 349 | |
---|
[426] | 350 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maximum depth )\n" |
---|
| 351 | << maxDepthNodes * 100 / (double)Leaves() << endl; |
---|
| 352 | |
---|
[462] | 353 | app << "#N_PMINPVSLEAVES ( Percentage of leaves with mininimal PVS )\n" |
---|
| 354 | << minPvsNodes * 100 / (double)Leaves() << endl; |
---|
[426] | 355 | |
---|
[462] | 356 | app << "#N_PMINRAYSLEAVES ( Percentage of leaves with minimal number of rays)\n" |
---|
| 357 | << minRaysNodes * 100 / (double)Leaves() << endl; |
---|
| 358 | |
---|
| 359 | app << "#N_PMINAREALEAVES ( Percentage of leaves with mininum area )\n" |
---|
| 360 | << minAreaNodes * 100 / (double)Leaves() << endl; |
---|
| 361 | |
---|
| 362 | app << "#N_PMAXRAYCONTRIBLEAVES ( Percentage of leaves with maximal ray contribution )\n" |
---|
| 363 | << maxRayContribNodes * 100 / (double)Leaves() << endl; |
---|
| 364 | |
---|
[375] | 365 | app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth << endl; |
---|
| 366 | |
---|
| 367 | app << "#N_PMINDEPTH ( Minimal reached depth )\n" << minDepth << endl; |
---|
| 368 | |
---|
| 369 | app << "#AVGDEPTH ( average depth )\n" << AvgDepth() << endl; |
---|
| 370 | |
---|
| 371 | app << "#N_INPUT_POLYGONS (number of input polygons )\n" << polys << endl; |
---|
| 372 | |
---|
| 373 | //app << "#N_PVS: " << pvs << endl; |
---|
| 374 | |
---|
| 375 | app << "#N_ROUTPUT_INPUT_POLYGONS ( ratio polygons after subdivision / input polygons )\n" << |
---|
| 376 | (polys + splits) / (double)polys << endl; |
---|
| 377 | |
---|
| 378 | app << "===== END OF BspTree statistics ==========\n"; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | |
---|
| 382 | BspTree::~BspTree() |
---|
| 383 | { |
---|
[411] | 384 | DEL_PTR(mRoot); |
---|
[441] | 385 | DEL_PTR(mRootCell); |
---|
[375] | 386 | } |
---|
| 387 | |
---|
[441] | 388 | BspViewCell *BspTree::GetRootCell() const |
---|
| 389 | { |
---|
| 390 | return mRootCell; |
---|
| 391 | } |
---|
| 392 | |
---|
[375] | 393 | void BspTree::InsertViewCell(ViewCell *viewCell) |
---|
| 394 | { |
---|
| 395 | PolygonContainer *polys = new PolygonContainer(); |
---|
| 396 | |
---|
[437] | 397 | // don't generate new view cell, insert this one |
---|
| 398 | mGenerateViewCells = false; |
---|
[375] | 399 | // extract polygons that guide the split process |
---|
| 400 | mStat.polys += AddMeshToPolygons(viewCell->GetMesh(), *polys, viewCell); |
---|
| 401 | mBox.Include(viewCell->GetBox()); // add to BSP aabb |
---|
| 402 | |
---|
| 403 | InsertPolygons(polys); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | void BspTree::InsertPolygons(PolygonContainer *polys) |
---|
| 407 | { |
---|
| 408 | std::stack<BspTraversalData> tStack; |
---|
[437] | 409 | |
---|
[375] | 410 | // traverse existing tree or create new tree |
---|
| 411 | if (!mRoot) |
---|
| 412 | mRoot = new BspLeaf(); |
---|
| 413 | |
---|
[442] | 414 | tStack.push(BspTraversalData(mRoot, polys, 0, mRootCell, new BoundedRayContainer(), 0, |
---|
| 415 | mBox.SurfaceArea(), new BspNodeGeometry())); |
---|
[375] | 416 | |
---|
| 417 | while (!tStack.empty()) |
---|
| 418 | { |
---|
| 419 | // filter polygons donw the tree |
---|
| 420 | BspTraversalData tData = tStack.top(); |
---|
| 421 | tStack.pop(); |
---|
| 422 | |
---|
| 423 | if (!tData.mNode->IsLeaf()) |
---|
| 424 | { |
---|
| 425 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode); |
---|
| 426 | |
---|
| 427 | //-- filter view cell polygons down the tree until a leaf is reached |
---|
| 428 | if (!tData.mPolygons->empty()) |
---|
| 429 | { |
---|
| 430 | PolygonContainer *frontPolys = new PolygonContainer(); |
---|
| 431 | PolygonContainer *backPolys = new PolygonContainer(); |
---|
| 432 | PolygonContainer coincident; |
---|
| 433 | |
---|
| 434 | int splits = 0; |
---|
| 435 | |
---|
| 436 | // split viewcell polygons with respect to split plane |
---|
[448] | 437 | splits += SplitPolygons(interior->GetPlane(), |
---|
| 438 | *tData.mPolygons, |
---|
| 439 | *frontPolys, |
---|
| 440 | *backPolys, |
---|
| 441 | coincident); |
---|
[375] | 442 | |
---|
| 443 | // extract view cells associated with the split polygons |
---|
| 444 | ViewCell *frontViewCell = mRootCell; |
---|
| 445 | ViewCell *backViewCell = mRootCell; |
---|
[390] | 446 | |
---|
| 447 | BspTraversalData frontData(interior->GetFront(), |
---|
| 448 | frontPolys, |
---|
| 449 | tData.mDepth + 1, |
---|
| 450 | mRootCell, |
---|
| 451 | tData.mRays, |
---|
[442] | 452 | tData.mPvs, |
---|
| 453 | mBox.SurfaceArea(), |
---|
| 454 | new BspNodeGeometry()); |
---|
[390] | 455 | |
---|
| 456 | BspTraversalData backData(interior->GetBack(), |
---|
| 457 | backPolys, |
---|
| 458 | tData.mDepth + 1, |
---|
| 459 | mRootCell, |
---|
| 460 | tData.mRays, |
---|
[442] | 461 | tData.mPvs, |
---|
| 462 | mBox.SurfaceArea(), |
---|
| 463 | new BspNodeGeometry()); |
---|
[390] | 464 | |
---|
[375] | 465 | if (!mGenerateViewCells) |
---|
| 466 | { |
---|
[390] | 467 | ExtractViewCells(frontData, |
---|
| 468 | backData, |
---|
| 469 | coincident, |
---|
| 470 | interior->mPlane); |
---|
[375] | 471 | } |
---|
| 472 | |
---|
| 473 | // don't need coincident polygons anymore |
---|
[390] | 474 | CLEAR_CONTAINER(coincident); |
---|
[375] | 475 | |
---|
| 476 | mStat.splits += splits; |
---|
| 477 | |
---|
| 478 | // push the children on the stack |
---|
[390] | 479 | tStack.push(frontData); |
---|
| 480 | tStack.push(backData); |
---|
[375] | 481 | } |
---|
| 482 | |
---|
| 483 | // cleanup |
---|
| 484 | DEL_PTR(tData.mPolygons); |
---|
[448] | 485 | DEL_PTR(tData.mRays); |
---|
[375] | 486 | } |
---|
| 487 | else |
---|
| 488 | { |
---|
| 489 | // reached leaf => subdivide current viewcell |
---|
| 490 | BspNode *subRoot = Subdivide(tStack, tData); |
---|
| 491 | } |
---|
| 492 | } |
---|
| 493 | } |
---|
| 494 | |
---|
[448] | 495 | int BspTree::AddMeshToPolygons(Mesh *mesh, |
---|
| 496 | PolygonContainer &polys, |
---|
| 497 | MeshInstance *parent) |
---|
[375] | 498 | { |
---|
| 499 | FaceContainer::const_iterator fi; |
---|
| 500 | |
---|
| 501 | // copy the face data to polygons |
---|
| 502 | for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi) |
---|
| 503 | { |
---|
| 504 | Polygon3 *poly = new Polygon3((*fi), mesh); |
---|
| 505 | |
---|
[448] | 506 | if (poly->Valid(mEpsilon)) |
---|
[375] | 507 | { |
---|
| 508 | poly->mParent = parent; // set parent intersectable |
---|
| 509 | polys.push_back(poly); |
---|
| 510 | } |
---|
| 511 | else |
---|
| 512 | DEL_PTR(poly); |
---|
| 513 | } |
---|
| 514 | return (int)mesh->mFaces.size(); |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | int BspTree::AddToPolygonSoup(const ViewCellContainer &viewCells, |
---|
| 518 | PolygonContainer &polys, |
---|
| 519 | int maxObjects) |
---|
| 520 | { |
---|
| 521 | int limit = (maxObjects > 0) ? |
---|
| 522 | Min((int)viewCells.size(), maxObjects) : (int)viewCells.size(); |
---|
| 523 | |
---|
| 524 | int polysSize = 0; |
---|
| 525 | |
---|
| 526 | for (int i = 0; i < limit; ++ i) |
---|
| 527 | { |
---|
| 528 | if (viewCells[i]->GetMesh()) // copy the mesh data to polygons |
---|
| 529 | { |
---|
| 530 | mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb |
---|
| 531 | polysSize += AddMeshToPolygons(viewCells[i]->GetMesh(), polys, viewCells[i]); |
---|
| 532 | } |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | return polysSize; |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | int BspTree::AddToPolygonSoup(const ObjectContainer &objects, PolygonContainer &polys, int maxObjects) |
---|
| 539 | { |
---|
| 540 | int limit = (maxObjects > 0) ? Min((int)objects.size(), maxObjects) : (int)objects.size(); |
---|
| 541 | |
---|
| 542 | for (int i = 0; i < limit; ++i) |
---|
| 543 | { |
---|
| 544 | Intersectable *object = objects[i];//*it; |
---|
| 545 | Mesh *mesh = NULL; |
---|
| 546 | |
---|
| 547 | switch (object->Type()) // extract the meshes |
---|
| 548 | { |
---|
| 549 | case Intersectable::MESH_INSTANCE: |
---|
| 550 | mesh = dynamic_cast<MeshInstance *>(object)->GetMesh(); |
---|
| 551 | break; |
---|
| 552 | case Intersectable::VIEW_CELL: |
---|
| 553 | mesh = dynamic_cast<ViewCell *>(object)->GetMesh(); |
---|
| 554 | break; |
---|
| 555 | // TODO: handle transformed mesh instances |
---|
| 556 | default: |
---|
| 557 | Debug << "intersectable type not supported" << endl; |
---|
| 558 | break; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | if (mesh) // copy the mesh data to polygons |
---|
| 562 | { |
---|
| 563 | mBox.Include(object->GetBox()); // add to BSP tree aabb |
---|
| 564 | AddMeshToPolygons(mesh, polys, mRootCell); |
---|
| 565 | } |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | return (int)polys.size(); |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | void BspTree::Construct(const ViewCellContainer &viewCells) |
---|
| 572 | { |
---|
| 573 | mStat.nodes = 1; |
---|
| 574 | mBox.Initialize(); // initialise bsp tree bounding box |
---|
| 575 | |
---|
| 576 | // copy view cell meshes into one big polygon soup |
---|
| 577 | PolygonContainer *polys = new PolygonContainer(); |
---|
| 578 | mStat.polys = AddToPolygonSoup(viewCells, *polys); |
---|
| 579 | |
---|
[437] | 580 | // view cells are given |
---|
| 581 | mGenerateViewCells = false; |
---|
[375] | 582 | // construct tree from the view cell polygons |
---|
| 583 | Construct(polys, new BoundedRayContainer()); |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | |
---|
| 587 | void BspTree::Construct(const ObjectContainer &objects) |
---|
| 588 | { |
---|
| 589 | mStat.nodes = 1; |
---|
| 590 | mBox.Initialize(); // initialise bsp tree bounding box |
---|
| 591 | |
---|
| 592 | PolygonContainer *polys = new PolygonContainer(); |
---|
[437] | 593 | |
---|
| 594 | mGenerateViewCells = true; |
---|
[375] | 595 | // copy mesh instance polygons into one big polygon soup |
---|
| 596 | mStat.polys = AddToPolygonSoup(objects, *polys); |
---|
| 597 | |
---|
| 598 | // construct tree from polygon soup |
---|
| 599 | Construct(polys, new BoundedRayContainer()); |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | void BspTree::Construct(const RayContainer &sampleRays) |
---|
| 603 | { |
---|
[414] | 604 | mStat.nodes = 1; |
---|
[375] | 605 | mBox.Initialize(); // initialise BSP tree bounding box |
---|
| 606 | |
---|
| 607 | PolygonContainer *polys = new PolygonContainer(); |
---|
| 608 | BoundedRayContainer *rays = new BoundedRayContainer(); |
---|
| 609 | |
---|
| 610 | RayContainer::const_iterator rit, rit_end = sampleRays.end(); |
---|
| 611 | |
---|
[437] | 612 | // generate view cells |
---|
[444] | 613 | mGenerateViewCells = true; |
---|
[437] | 614 | |
---|
[375] | 615 | long startTime = GetTime(); |
---|
| 616 | |
---|
| 617 | Debug << "**** Extracting polygons from rays ****\n"; |
---|
| 618 | |
---|
| 619 | std::map<Face *, Polygon3 *> facePolyMap; |
---|
| 620 | |
---|
| 621 | //-- extract polygons intersected by the rays |
---|
| 622 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit) |
---|
| 623 | { |
---|
| 624 | Ray *ray = *rit; |
---|
| 625 | |
---|
| 626 | // get ray-face intersection. Store polygon representing the rays together |
---|
| 627 | // with rays intersecting the face. |
---|
| 628 | if (!ray->intersections.empty()) |
---|
| 629 | { |
---|
| 630 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->intersections[0].mObject); |
---|
| 631 | Face *face = obj->GetMesh()->mFaces[ray->intersections[0].mFace]; |
---|
| 632 | |
---|
| 633 | std::map<Face *, Polygon3 *>::iterator it = facePolyMap.find(face); |
---|
| 634 | |
---|
| 635 | if (it != facePolyMap.end()) |
---|
| 636 | { |
---|
| 637 | //store rays if needed for heuristics |
---|
[420] | 638 | if (mSplitPlaneStrategy & BLOCKED_RAYS) |
---|
[375] | 639 | (*it).second->mPiercingRays.push_back(ray); |
---|
| 640 | } |
---|
| 641 | else |
---|
| 642 | { //store rays if needed for heuristics |
---|
| 643 | Polygon3 *poly = new Polygon3(face, obj->GetMesh()); |
---|
| 644 | poly->mParent = obj; |
---|
| 645 | polys->push_back(poly); |
---|
| 646 | |
---|
[420] | 647 | if (mSplitPlaneStrategy & BLOCKED_RAYS) |
---|
[375] | 648 | poly->mPiercingRays.push_back(ray); |
---|
| 649 | |
---|
| 650 | facePolyMap[face] = poly; |
---|
| 651 | } |
---|
| 652 | } |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | facePolyMap.clear(); |
---|
| 656 | |
---|
[436] | 657 | // compute bounding box |
---|
[375] | 658 | Polygon3::IncludeInBox(*polys, mBox); |
---|
| 659 | |
---|
| 660 | //-- store rays |
---|
| 661 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit) |
---|
| 662 | { |
---|
| 663 | Ray *ray = *rit; |
---|
| 664 | ray->SetId(-1); // reset id |
---|
| 665 | |
---|
| 666 | float minT, maxT; |
---|
[448] | 667 | if (mBox.GetRaySegment(*ray, minT, maxT)) |
---|
[375] | 668 | rays->push_back(new BoundedRay(ray, minT, maxT)); |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | mStat.polys = (int)polys->size(); |
---|
| 672 | |
---|
| 673 | Debug << "**** Finished polygon extraction ****" << endl; |
---|
[380] | 674 | Debug << (int)polys->size() << " polys extracted from " << (int)sampleRays.size() << " rays" << endl; |
---|
[375] | 675 | Debug << "extraction time: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; |
---|
| 676 | |
---|
| 677 | Construct(polys, rays); |
---|
| 678 | } |
---|
| 679 | |
---|
[436] | 680 | void BspTree::Construct(const ObjectContainer &objects, const RayContainer &sampleRays) |
---|
| 681 | { |
---|
| 682 | mStat.nodes = 1; |
---|
| 683 | mBox.Initialize(); // initialise BSP tree bounding box |
---|
| 684 | |
---|
| 685 | BoundedRayContainer *rays = new BoundedRayContainer(); |
---|
| 686 | PolygonContainer *polys = new PolygonContainer(); |
---|
| 687 | |
---|
[437] | 688 | mGenerateViewCells = true; |
---|
| 689 | |
---|
[436] | 690 | // copy mesh instance polygons into one big polygon soup |
---|
| 691 | mStat.polys = AddToPolygonSoup(objects, *polys); |
---|
| 692 | |
---|
| 693 | RayContainer::const_iterator rit, rit_end = sampleRays.end(); |
---|
| 694 | |
---|
| 695 | //-- store rays |
---|
| 696 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit) |
---|
| 697 | { |
---|
| 698 | Ray *ray = *rit; |
---|
| 699 | ray->SetId(-1); // reset id |
---|
| 700 | |
---|
| 701 | float minT, maxT; |
---|
[448] | 702 | if (mBox.GetRaySegment(*ray, minT, maxT)) |
---|
[436] | 703 | rays->push_back(new BoundedRay(ray, minT, maxT)); |
---|
| 704 | } |
---|
| 705 | |
---|
| 706 | Debug << "tree has " << (int)polys->size() << " polys, " << (int)sampleRays.size() << " rays" << endl; |
---|
| 707 | Construct(polys, rays); |
---|
| 708 | } |
---|
| 709 | |
---|
[375] | 710 | void BspTree::Construct(PolygonContainer *polys, BoundedRayContainer *rays) |
---|
| 711 | { |
---|
| 712 | std::stack<BspTraversalData> tStack; |
---|
| 713 | |
---|
| 714 | mRoot = new BspLeaf(); |
---|
| 715 | |
---|
[442] | 716 | // constrruct root node geometry |
---|
[390] | 717 | BspNodeGeometry *cell = new BspNodeGeometry(); |
---|
| 718 | ConstructGeometry(mRoot, *cell); |
---|
| 719 | |
---|
| 720 | BspTraversalData tData(mRoot, polys, 0, mRootCell, rays, |
---|
[442] | 721 | ComputePvsSize(*rays), cell->GetArea(), cell); |
---|
[390] | 722 | |
---|
[375] | 723 | tStack.push(tData); |
---|
| 724 | |
---|
[414] | 725 | mStat.Start(); |
---|
[436] | 726 | cout << "Contructing bsp tree ... "; |
---|
| 727 | long startTime = GetTime(); |
---|
[375] | 728 | while (!tStack.empty()) |
---|
| 729 | { |
---|
| 730 | tData = tStack.top(); |
---|
[436] | 731 | |
---|
[375] | 732 | tStack.pop(); |
---|
| 733 | |
---|
| 734 | // subdivide leaf node |
---|
[436] | 735 | BspNode *r = Subdivide(tStack, tData); |
---|
| 736 | |
---|
| 737 | if (r == mRoot) |
---|
| 738 | Debug << "BSP tree construction time spent at root: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; |
---|
[375] | 739 | } |
---|
| 740 | |
---|
[436] | 741 | cout << "finished\n"; |
---|
| 742 | |
---|
[414] | 743 | mStat.Stop(); |
---|
[375] | 744 | } |
---|
| 745 | |
---|
[420] | 746 | bool BspTree::TerminationCriteriaMet(const BspTraversalData &data) const |
---|
[397] | 747 | { |
---|
| 748 | return |
---|
[426] | 749 | (((int)data.mPolygons->size() <= mTermMinPolys) || |
---|
| 750 | ((int)data.mRays->size() <= mTermMinRays) || |
---|
[420] | 751 | (data.mPvs <= mTermMinPvs) || |
---|
[442] | 752 | (data.mArea <= mTermMinArea) || |
---|
[420] | 753 | (data.mDepth >= mTermMaxDepth) || |
---|
[423] | 754 | (data.GetAvgRayContribution() < mTermMaxRayContribution)); |
---|
[397] | 755 | } |
---|
[375] | 756 | |
---|
| 757 | BspNode *BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData) |
---|
| 758 | { |
---|
| 759 | //-- terminate traversal |
---|
[397] | 760 | if (TerminationCriteriaMet(tData)) |
---|
[375] | 761 | { |
---|
| 762 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); |
---|
| 763 | |
---|
[392] | 764 | BspViewCell *viewCell; |
---|
| 765 | |
---|
[375] | 766 | // generate new view cell for each leaf |
---|
| 767 | if (mGenerateViewCells) |
---|
| 768 | { |
---|
[439] | 769 | viewCell = new BspViewCell(); |
---|
[375] | 770 | } |
---|
| 771 | else |
---|
| 772 | { |
---|
| 773 | // add view cell to leaf |
---|
[392] | 774 | viewCell = dynamic_cast<BspViewCell *>(tData.mViewCell); |
---|
[375] | 775 | } |
---|
| 776 | |
---|
[392] | 777 | leaf->SetViewCell(viewCell); |
---|
| 778 | viewCell->mBspLeaves.push_back(leaf); |
---|
| 779 | |
---|
| 780 | //-- add pvs |
---|
| 781 | if (viewCell != mRootCell) |
---|
| 782 | { |
---|
| 783 | int conSamp = 0, sampCon = 0; |
---|
[448] | 784 | AddToPvs(leaf, *tData.mRays, conSamp, sampCon); |
---|
[392] | 785 | |
---|
| 786 | mStat.contributingSamples += conSamp; |
---|
| 787 | mStat.sampleContributions += sampCon; |
---|
| 788 | } |
---|
| 789 | |
---|
[375] | 790 | EvaluateLeafStats(tData); |
---|
| 791 | |
---|
| 792 | //-- clean up |
---|
| 793 | |
---|
[390] | 794 | // discard polygons |
---|
| 795 | CLEAR_CONTAINER(*tData.mPolygons); |
---|
[375] | 796 | // discard rays |
---|
| 797 | CLEAR_CONTAINER(*tData.mRays); |
---|
[390] | 798 | |
---|
| 799 | DEL_PTR(tData.mPolygons); |
---|
[375] | 800 | DEL_PTR(tData.mRays); |
---|
[448] | 801 | DEL_PTR(tData.mGeometry); |
---|
| 802 | |
---|
[375] | 803 | return leaf; |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | //-- continue subdivision |
---|
| 807 | PolygonContainer coincident; |
---|
| 808 | |
---|
[390] | 809 | BspTraversalData tFrontData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell, |
---|
[442] | 810 | new BoundedRayContainer(), 0, 0, new BspNodeGeometry()); |
---|
[390] | 811 | BspTraversalData tBackData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell, |
---|
[442] | 812 | new BoundedRayContainer(), 0, 0, new BspNodeGeometry()); |
---|
[390] | 813 | |
---|
[375] | 814 | // create new interior node and two leaf nodes |
---|
[437] | 815 | BspInterior *interior = |
---|
| 816 | SubdivideNode(tData, tFrontData, tBackData, coincident); |
---|
[375] | 817 | |
---|
| 818 | #ifdef _DEBUG |
---|
[450] | 819 | // if (frontPolys->empty() && backPolys->empty() && (coincident.size() > 2)) |
---|
| 820 | // { for (PolygonContainer::iterator it = coincident.begin(); it != coincident.end(); ++it) |
---|
| 821 | // Debug << (*it) << " " << (*it)->GetArea() << " " << (*it)->mParent << endl ; |
---|
| 822 | // Debug << endl;} |
---|
[375] | 823 | #endif |
---|
| 824 | |
---|
| 825 | // extract view cells from coincident polygons according to plane normal |
---|
| 826 | // only if front or back polygons are empty |
---|
| 827 | if (!mGenerateViewCells) |
---|
| 828 | { |
---|
[390] | 829 | ExtractViewCells(tFrontData, |
---|
| 830 | tBackData, |
---|
[375] | 831 | coincident, |
---|
[422] | 832 | interior->mPlane); |
---|
[375] | 833 | } |
---|
| 834 | |
---|
[390] | 835 | // don't need coincident polygons anymory |
---|
| 836 | CLEAR_CONTAINER(coincident); |
---|
| 837 | |
---|
[375] | 838 | // push the children on the stack |
---|
[390] | 839 | tStack.push(tFrontData); |
---|
| 840 | tStack.push(tBackData); |
---|
[375] | 841 | |
---|
| 842 | // cleanup |
---|
| 843 | DEL_PTR(tData.mNode); |
---|
[448] | 844 | |
---|
[375] | 845 | DEL_PTR(tData.mPolygons); |
---|
| 846 | DEL_PTR(tData.mRays); |
---|
[442] | 847 | DEL_PTR(tData.mGeometry); |
---|
[441] | 848 | |
---|
[375] | 849 | return interior; |
---|
| 850 | } |
---|
| 851 | |
---|
[390] | 852 | void BspTree::ExtractViewCells(BspTraversalData &frontData, |
---|
| 853 | BspTraversalData &backData, |
---|
[375] | 854 | const PolygonContainer &coincident, |
---|
[437] | 855 | const Plane3 &splitPlane) const |
---|
[375] | 856 | { |
---|
[390] | 857 | // if not empty, tree is further subdivided => don't have to find view cell |
---|
| 858 | bool foundFront = !frontData.mPolygons->empty(); |
---|
| 859 | bool foundBack = !frontData.mPolygons->empty(); |
---|
[375] | 860 | |
---|
| 861 | PolygonContainer::const_iterator it = |
---|
| 862 | coincident.begin(), it_end = coincident.end(); |
---|
| 863 | |
---|
| 864 | //-- find first view cells in front and back leafs |
---|
| 865 | for (; !(foundFront && foundBack) && (it != it_end); ++ it) |
---|
| 866 | { |
---|
| 867 | if (DotProd((*it)->GetNormal(), splitPlane.mNormal) > 0) |
---|
| 868 | { |
---|
[390] | 869 | backData.mViewCell = dynamic_cast<ViewCell *>((*it)->mParent); |
---|
[375] | 870 | foundBack = true; |
---|
| 871 | } |
---|
| 872 | else |
---|
| 873 | { |
---|
[390] | 874 | frontData.mViewCell = dynamic_cast<ViewCell *>((*it)->mParent); |
---|
[375] | 875 | foundFront = true; |
---|
| 876 | } |
---|
| 877 | } |
---|
| 878 | } |
---|
| 879 | |
---|
[390] | 880 | BspInterior *BspTree::SubdivideNode(BspTraversalData &tData, |
---|
| 881 | BspTraversalData &frontData, |
---|
| 882 | BspTraversalData &backData, |
---|
| 883 | PolygonContainer &coincident) |
---|
[375] | 884 | { |
---|
| 885 | mStat.nodes += 2; |
---|
| 886 | |
---|
[390] | 887 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); |
---|
[441] | 888 | |
---|
| 889 | Debug << "*********************" << endl; |
---|
| 890 | long startTime = GetTime(); |
---|
| 891 | |
---|
[375] | 892 | // select subdivision plane |
---|
| 893 | BspInterior *interior = |
---|
[400] | 894 | new BspInterior(SelectPlane(leaf, tData)); |
---|
[441] | 895 | Debug << "time used for split plane selection: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; |
---|
[375] | 896 | #ifdef _DEBUG |
---|
| 897 | Debug << interior << endl; |
---|
| 898 | #endif |
---|
| 899 | |
---|
[441] | 900 | |
---|
[448] | 901 | Debug << "number of rays: " << (int)tData.mRays->size() << endl; |
---|
| 902 | Debug << "number of polys: " << (int)tData.mPolygons->size() << endl; |
---|
[441] | 903 | |
---|
| 904 | startTime = GetTime(); |
---|
| 905 | |
---|
[375] | 906 | // subdivide rays into front and back rays |
---|
[390] | 907 | SplitRays(interior->mPlane, *tData.mRays, *frontData.mRays, *backData.mRays); |
---|
[375] | 908 | |
---|
[441] | 909 | Debug << "time used for rays splitting: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; |
---|
| 910 | |
---|
| 911 | startTime = GetTime(); |
---|
[375] | 912 | // subdivide polygons with plane |
---|
[448] | 913 | mStat.splits += SplitPolygons(interior->GetPlane(), |
---|
| 914 | *tData.mPolygons, |
---|
| 915 | *frontData.mPolygons, |
---|
| 916 | *backData.mPolygons, |
---|
| 917 | coincident); |
---|
[375] | 918 | |
---|
[441] | 919 | Debug << "time used for polygon splitting: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; |
---|
| 920 | |
---|
[409] | 921 | // compute pvs |
---|
[400] | 922 | frontData.mPvs = ComputePvsSize(*frontData.mRays); |
---|
| 923 | backData.mPvs = ComputePvsSize(*backData.mRays); |
---|
| 924 | |
---|
[442] | 925 | // split geometry and compute area |
---|
| 926 | if (1) |
---|
| 927 | { |
---|
| 928 | tData.mGeometry->SplitGeometry(*frontData.mGeometry, |
---|
| 929 | *backData.mGeometry, |
---|
[448] | 930 | interior->mPlane, |
---|
| 931 | mBox, |
---|
| 932 | mEpsilon); |
---|
[442] | 933 | |
---|
| 934 | |
---|
| 935 | frontData.mArea = frontData.mGeometry->GetArea(); |
---|
| 936 | backData.mArea = backData.mGeometry->GetArea(); |
---|
| 937 | } |
---|
| 938 | |
---|
[409] | 939 | // compute accumulated ray length |
---|
| 940 | //frontData.mAccRayLength = AccumulatedRayLength(*frontData.mRays); |
---|
| 941 | //backData.mAccRayLength = AccumulatedRayLength(*backData.mRays); |
---|
| 942 | |
---|
[404] | 943 | //-- create front and back leaf |
---|
[400] | 944 | |
---|
[375] | 945 | BspInterior *parent = leaf->GetParent(); |
---|
| 946 | |
---|
| 947 | // replace a link from node's parent |
---|
| 948 | if (!leaf->IsRoot()) |
---|
| 949 | { |
---|
| 950 | parent->ReplaceChildLink(leaf, interior); |
---|
| 951 | interior->SetParent(parent); |
---|
| 952 | } |
---|
| 953 | else // new root |
---|
| 954 | { |
---|
| 955 | mRoot = interior; |
---|
| 956 | } |
---|
| 957 | |
---|
| 958 | // and setup child links |
---|
| 959 | interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior)); |
---|
| 960 | |
---|
[448] | 961 | frontData.mNode = interior->GetFront(); |
---|
| 962 | backData.mNode = interior->GetBack(); |
---|
[390] | 963 | |
---|
[414] | 964 | //DEL_PTR(leaf); |
---|
[375] | 965 | return interior; |
---|
| 966 | } |
---|
| 967 | |
---|
| 968 | void BspTree::SortSplitCandidates(const PolygonContainer &polys, |
---|
| 969 | const int axis, |
---|
| 970 | vector<SortableEntry> &splitCandidates) const |
---|
| 971 | { |
---|
| 972 | splitCandidates.clear(); |
---|
| 973 | |
---|
| 974 | int requestedSize = 2 * (int)polys.size(); |
---|
| 975 | // creates a sorted split candidates array |
---|
| 976 | splitCandidates.reserve(requestedSize); |
---|
| 977 | |
---|
| 978 | PolygonContainer::const_iterator it, it_end = polys.end(); |
---|
| 979 | |
---|
| 980 | AxisAlignedBox3 box; |
---|
| 981 | |
---|
| 982 | // insert all queries |
---|
| 983 | for(it = polys.begin(); it != it_end; ++ it) |
---|
| 984 | { |
---|
| 985 | box.Initialize(); |
---|
| 986 | box.Include(*(*it)); |
---|
| 987 | |
---|
| 988 | splitCandidates.push_back(SortableEntry(SortableEntry::POLY_MIN, box.Min(axis), *it)); |
---|
| 989 | splitCandidates.push_back(SortableEntry(SortableEntry::POLY_MAX, box.Max(axis), *it)); |
---|
| 990 | } |
---|
| 991 | |
---|
| 992 | stable_sort(splitCandidates.begin(), splitCandidates.end()); |
---|
| 993 | } |
---|
| 994 | |
---|
| 995 | |
---|
| 996 | float BspTree::BestCostRatio(const PolygonContainer &polys, |
---|
| 997 | const AxisAlignedBox3 &box, |
---|
| 998 | const int axis, |
---|
| 999 | float &position, |
---|
| 1000 | int &objectsBack, |
---|
| 1001 | int &objectsFront) const |
---|
| 1002 | { |
---|
| 1003 | vector<SortableEntry> splitCandidates; |
---|
| 1004 | |
---|
| 1005 | SortSplitCandidates(polys, axis, splitCandidates); |
---|
| 1006 | |
---|
| 1007 | // go through the lists, count the number of objects left and right |
---|
| 1008 | // and evaluate the following cost funcion: |
---|
| 1009 | // C = ct_div_ci + (ol + or)/queries |
---|
| 1010 | |
---|
| 1011 | int objectsLeft = 0, objectsRight = (int)polys.size(); |
---|
| 1012 | |
---|
| 1013 | float minBox = box.Min(axis); |
---|
| 1014 | float maxBox = box.Max(axis); |
---|
| 1015 | float boxArea = box.SurfaceArea(); |
---|
| 1016 | |
---|
[420] | 1017 | float minBand = minBox + mSplitBorder * (maxBox - minBox); |
---|
| 1018 | float maxBand = minBox + (1.0f - mSplitBorder) * (maxBox - minBox); |
---|
[375] | 1019 | |
---|
| 1020 | float minSum = 1e20f; |
---|
| 1021 | vector<SortableEntry>::const_iterator ci, ci_end = splitCandidates.end(); |
---|
| 1022 | |
---|
| 1023 | for(ci = splitCandidates.begin(); ci != ci_end; ++ ci) |
---|
| 1024 | { |
---|
| 1025 | switch ((*ci).type) |
---|
| 1026 | { |
---|
| 1027 | case SortableEntry::POLY_MIN: |
---|
| 1028 | ++ objectsLeft; |
---|
| 1029 | break; |
---|
| 1030 | case SortableEntry::POLY_MAX: |
---|
| 1031 | -- objectsRight; |
---|
| 1032 | break; |
---|
| 1033 | default: |
---|
| 1034 | break; |
---|
| 1035 | } |
---|
| 1036 | |
---|
| 1037 | if ((*ci).value > minBand && (*ci).value < maxBand) |
---|
| 1038 | { |
---|
| 1039 | AxisAlignedBox3 lbox = box; |
---|
| 1040 | AxisAlignedBox3 rbox = box; |
---|
| 1041 | lbox.SetMax(axis, (*ci).value); |
---|
| 1042 | rbox.SetMin(axis, (*ci).value); |
---|
| 1043 | |
---|
| 1044 | float sum = objectsLeft * lbox.SurfaceArea() + |
---|
| 1045 | objectsRight * rbox.SurfaceArea(); |
---|
| 1046 | |
---|
| 1047 | if (sum < minSum) |
---|
| 1048 | { |
---|
| 1049 | minSum = sum; |
---|
| 1050 | position = (*ci).value; |
---|
| 1051 | |
---|
| 1052 | objectsBack = objectsLeft; |
---|
| 1053 | objectsFront = objectsRight; |
---|
| 1054 | } |
---|
| 1055 | } |
---|
| 1056 | } |
---|
| 1057 | |
---|
| 1058 | float oldCost = (float)polys.size(); |
---|
[424] | 1059 | float newCost = mAaCtDivCi + minSum / boxArea; |
---|
[375] | 1060 | float ratio = newCost / oldCost; |
---|
| 1061 | |
---|
| 1062 | |
---|
| 1063 | #if 0 |
---|
| 1064 | Debug << "====================" << endl; |
---|
| 1065 | Debug << "costRatio=" << ratio << " pos=" << position<<" t=" << (position - minBox)/(maxBox - minBox) |
---|
| 1066 | << "\t o=(" << objectsBack << "," << objectsFront << ")" << endl; |
---|
| 1067 | #endif |
---|
| 1068 | return ratio; |
---|
| 1069 | } |
---|
| 1070 | |
---|
| 1071 | bool BspTree::SelectAxisAlignedPlane(Plane3 &plane, |
---|
| 1072 | const PolygonContainer &polys) const |
---|
| 1073 | { |
---|
| 1074 | AxisAlignedBox3 box; |
---|
| 1075 | box.Initialize(); |
---|
| 1076 | |
---|
| 1077 | // create bounding box of region |
---|
| 1078 | Polygon3::IncludeInBox(polys, box); |
---|
| 1079 | |
---|
| 1080 | int objectsBack = 0, objectsFront = 0; |
---|
| 1081 | int axis = 0; |
---|
| 1082 | float costRatio = MAX_FLOAT; |
---|
| 1083 | Vector3 position; |
---|
| 1084 | |
---|
| 1085 | //-- area subdivision |
---|
| 1086 | for (int i = 0; i < 3; ++ i) |
---|
| 1087 | { |
---|
| 1088 | float p = 0; |
---|
| 1089 | float r = BestCostRatio(polys, box, i, p, objectsBack, objectsFront); |
---|
| 1090 | |
---|
| 1091 | if (r < costRatio) |
---|
| 1092 | { |
---|
| 1093 | costRatio = r; |
---|
| 1094 | axis = i; |
---|
| 1095 | position = p; |
---|
| 1096 | } |
---|
| 1097 | } |
---|
| 1098 | |
---|
[420] | 1099 | if (costRatio >= mMaxCostRatio) |
---|
[375] | 1100 | return false; |
---|
| 1101 | |
---|
| 1102 | Vector3 norm(0,0,0); norm[axis] = 1.0f; |
---|
| 1103 | plane = Plane3(norm, position); |
---|
| 1104 | |
---|
| 1105 | return true; |
---|
| 1106 | } |
---|
| 1107 | |
---|
[406] | 1108 | Plane3 BspTree::SelectPlane(BspLeaf *leaf, BspTraversalData &data) |
---|
[375] | 1109 | { |
---|
[390] | 1110 | if (data.mPolygons->empty() && data.mRays->empty()) |
---|
[375] | 1111 | { |
---|
| 1112 | Debug << "Warning: No autopartition polygon candidate available\n"; |
---|
| 1113 | |
---|
| 1114 | // return axis aligned split |
---|
| 1115 | AxisAlignedBox3 box; |
---|
| 1116 | box.Initialize(); |
---|
| 1117 | |
---|
| 1118 | // create bounding box of region |
---|
[390] | 1119 | Polygon3::IncludeInBox(*data.mPolygons, box); |
---|
[375] | 1120 | |
---|
| 1121 | const int axis = box.Size().DrivingAxis(); |
---|
| 1122 | const Vector3 position = (box.Min()[axis] + box.Max()[axis])*0.5f; |
---|
| 1123 | |
---|
| 1124 | Vector3 norm(0,0,0); norm[axis] = 1.0f; |
---|
| 1125 | return Plane3(norm, position); |
---|
| 1126 | } |
---|
| 1127 | |
---|
[420] | 1128 | if ((mSplitPlaneStrategy & AXIS_ALIGNED) && |
---|
[426] | 1129 | ((int)data.mPolygons->size() > mTermMinPolysForAxisAligned) && |
---|
| 1130 | ((int)data.mRays->size() > mTermMinRaysForAxisAligned) && |
---|
| 1131 | ((mTermMinObjectsForAxisAligned < 0) || |
---|
| 1132 | (Polygon3::ParentObjectsSize(*data.mPolygons) > mTermMinObjectsForAxisAligned))) |
---|
[375] | 1133 | { |
---|
| 1134 | Plane3 plane; |
---|
[390] | 1135 | if (SelectAxisAlignedPlane(plane, *data.mPolygons)) |
---|
[375] | 1136 | return plane; |
---|
| 1137 | } |
---|
| 1138 | |
---|
| 1139 | // simplest strategy: just take next polygon |
---|
[420] | 1140 | if (mSplitPlaneStrategy & RANDOM_POLYGON) |
---|
[375] | 1141 | { |
---|
[406] | 1142 | if (!data.mPolygons->empty()) |
---|
[397] | 1143 | { |
---|
| 1144 | Polygon3 *nextPoly = (*data.mPolygons)[Random((int)data.mPolygons->size())]; |
---|
| 1145 | return nextPoly->GetSupportingPlane(); |
---|
| 1146 | } |
---|
[406] | 1147 | else |
---|
| 1148 | { |
---|
| 1149 | const int candidateIdx = Random((int)data.mRays->size()); |
---|
| 1150 | BoundedRay *bRay = (*data.mRays)[candidateIdx]; |
---|
| 1151 | |
---|
| 1152 | Ray *ray = bRay->mRay; |
---|
| 1153 | |
---|
| 1154 | const Vector3 minPt = ray->Extrap(bRay->mMinT); |
---|
| 1155 | const Vector3 maxPt = ray->Extrap(bRay->mMaxT); |
---|
| 1156 | |
---|
| 1157 | const Vector3 pt = (maxPt + minPt) * 0.5; |
---|
| 1158 | |
---|
| 1159 | const Vector3 normal = ray->GetDir(); |
---|
| 1160 | |
---|
| 1161 | return Plane3(normal, pt); |
---|
| 1162 | } |
---|
| 1163 | |
---|
[397] | 1164 | return Plane3(); |
---|
[375] | 1165 | } |
---|
| 1166 | |
---|
| 1167 | // use heuristics to find appropriate plane |
---|
[400] | 1168 | return SelectPlaneHeuristics(leaf, data); |
---|
[375] | 1169 | } |
---|
| 1170 | |
---|
[406] | 1171 | Plane3 BspTree::SelectPlaneHeuristics(BspLeaf *leaf, BspTraversalData &data) |
---|
[375] | 1172 | { |
---|
| 1173 | float lowestCost = MAX_FLOAT; |
---|
[390] | 1174 | Plane3 bestPlane; |
---|
[406] | 1175 | Plane3 plane; |
---|
| 1176 | |
---|
[420] | 1177 | int limit = Min((int)data.mPolygons->size(), mMaxPolyCandidates); |
---|
[375] | 1178 | |
---|
| 1179 | int candidateIdx = limit; |
---|
[379] | 1180 | |
---|
[383] | 1181 | for (int i = 0; i < limit; ++ i) |
---|
[375] | 1182 | { |
---|
[390] | 1183 | candidateIdx = GetNextCandidateIdx(candidateIdx, *data.mPolygons); |
---|
[383] | 1184 | |
---|
[390] | 1185 | Polygon3 *poly = (*data.mPolygons)[candidateIdx]; |
---|
[383] | 1186 | // evaluate current candidate |
---|
| 1187 | const float candidateCost = |
---|
[400] | 1188 | SplitPlaneCost(poly->GetSupportingPlane(), data); |
---|
[375] | 1189 | |
---|
| 1190 | if (candidateCost < lowestCost) |
---|
| 1191 | { |
---|
[390] | 1192 | bestPlane = poly->GetSupportingPlane(); |
---|
[375] | 1193 | lowestCost = candidateCost; |
---|
| 1194 | } |
---|
[383] | 1195 | } |
---|
[379] | 1196 | |
---|
[410] | 1197 | //Debug << "lowest: " << lowestCost << endl; |
---|
[397] | 1198 | |
---|
[410] | 1199 | //-- choose candidate planes extracted from rays |
---|
| 1200 | // we currently use two methods |
---|
| 1201 | // 1) take 3 ray endpoints, where two are minimum and one a maximum |
---|
| 1202 | // point or the other way round |
---|
| 1203 | // 2) take plane normal as plane normal and the midpoint of the ray. |
---|
| 1204 | // PROBLEM: does not resemble any point where visibility is likely to change |
---|
[390] | 1205 | const BoundedRayContainer *rays = data.mRays; |
---|
[379] | 1206 | |
---|
[420] | 1207 | for (int i = 0; i < mMaxRayCandidates / 2; ++ i) |
---|
[379] | 1208 | { |
---|
[406] | 1209 | candidateIdx = Random((int)rays->size()); |
---|
| 1210 | BoundedRay *bRay = (*rays)[candidateIdx]; |
---|
[379] | 1211 | |
---|
[406] | 1212 | Ray *ray = bRay->mRay; |
---|
| 1213 | |
---|
| 1214 | const Vector3 minPt = ray->Extrap(bRay->mMinT); |
---|
| 1215 | const Vector3 maxPt = ray->Extrap(bRay->mMaxT); |
---|
[404] | 1216 | |
---|
[406] | 1217 | const Vector3 pt = (maxPt + minPt) * 0.5; |
---|
[404] | 1218 | |
---|
[406] | 1219 | const Vector3 normal = ray->GetDir(); |
---|
[397] | 1220 | |
---|
[406] | 1221 | plane = Plane3(normal, pt); |
---|
| 1222 | |
---|
| 1223 | const float candidateCost = SplitPlaneCost(plane, data); |
---|
| 1224 | |
---|
| 1225 | if (candidateCost < lowestCost) |
---|
| 1226 | { |
---|
| 1227 | bestPlane = plane; |
---|
| 1228 | |
---|
| 1229 | lowestCost = candidateCost; |
---|
[379] | 1230 | } |
---|
[406] | 1231 | } |
---|
[379] | 1232 | |
---|
[410] | 1233 | //Debug << "lowest: " << lowestCost << endl; |
---|
[420] | 1234 | for (int i = 0; i < mMaxRayCandidates / 2; ++ i) |
---|
[406] | 1235 | { |
---|
| 1236 | Vector3 pt[3]; |
---|
| 1237 | int idx[3]; |
---|
| 1238 | int cmaxT = 0; |
---|
| 1239 | int cminT = 0; |
---|
| 1240 | bool chooseMin = false; |
---|
[379] | 1241 | |
---|
[406] | 1242 | for (int j = 0; j < 3; j ++) |
---|
| 1243 | { |
---|
| 1244 | idx[j] = Random((int)rays->size() * 2); |
---|
| 1245 | |
---|
| 1246 | if (idx[j] >= (int)rays->size()) |
---|
| 1247 | { |
---|
| 1248 | idx[j] -= (int)rays->size(); |
---|
| 1249 | |
---|
| 1250 | chooseMin = (cminT < 2); |
---|
| 1251 | } |
---|
| 1252 | else |
---|
| 1253 | chooseMin = (cmaxT < 2); |
---|
[390] | 1254 | |
---|
[406] | 1255 | BoundedRay *bRay = (*rays)[idx[j]]; |
---|
[448] | 1256 | pt[j] = chooseMin ? bRay->mRay->Extrap(bRay->mMinT) : |
---|
| 1257 | bRay->mRay->Extrap(bRay->mMaxT); |
---|
[406] | 1258 | } |
---|
[390] | 1259 | |
---|
[406] | 1260 | plane = Plane3(pt[0], pt[1], pt[2]); |
---|
| 1261 | |
---|
[400] | 1262 | const float candidateCost = SplitPlaneCost(plane, data); |
---|
[390] | 1263 | |
---|
[379] | 1264 | if (candidateCost < lowestCost) |
---|
| 1265 | { |
---|
[410] | 1266 | //Debug << "choose ray plane 2: " << candidateCost << endl; |
---|
[390] | 1267 | bestPlane = plane; |
---|
[379] | 1268 | |
---|
| 1269 | lowestCost = candidateCost; |
---|
| 1270 | } |
---|
[406] | 1271 | } |
---|
[379] | 1272 | |
---|
[410] | 1273 | #ifdef _DEBUG |
---|
[406] | 1274 | Debug << "plane lowest cost: " << lowestCost << endl; |
---|
[410] | 1275 | #endif |
---|
[390] | 1276 | return bestPlane; |
---|
[375] | 1277 | } |
---|
| 1278 | |
---|
| 1279 | int BspTree::GetNextCandidateIdx(int currentIdx, PolygonContainer &polys) |
---|
| 1280 | { |
---|
| 1281 | const int candidateIdx = Random(currentIdx --); |
---|
| 1282 | |
---|
| 1283 | // swap candidates to avoid testing same plane 2 times |
---|
| 1284 | std::swap(polys[currentIdx], polys[candidateIdx]); |
---|
| 1285 | |
---|
| 1286 | return currentIdx; |
---|
| 1287 | //return Random((int)polys.size()); |
---|
| 1288 | } |
---|
| 1289 | |
---|
[384] | 1290 | float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, |
---|
[375] | 1291 | const PolygonContainer &polys) const |
---|
| 1292 | { |
---|
| 1293 | float val = 0; |
---|
| 1294 | |
---|
| 1295 | float sumBalancedPolys = 0; |
---|
| 1296 | float sumSplits = 0; |
---|
| 1297 | float sumPolyArea = 0; |
---|
| 1298 | float sumBalancedViewCells = 0; |
---|
| 1299 | float sumBlockedRays = 0; |
---|
| 1300 | float totalBlockedRays = 0; |
---|
| 1301 | //float totalArea = 0; |
---|
| 1302 | int totalViewCells = 0; |
---|
| 1303 | |
---|
| 1304 | // need three unique ids for each type of view cell |
---|
| 1305 | // for balanced view cells criterium |
---|
| 1306 | ViewCell::NewMail(); |
---|
| 1307 | const int backId = ViewCell::sMailId; |
---|
| 1308 | ViewCell::NewMail(); |
---|
| 1309 | const int frontId = ViewCell::sMailId; |
---|
| 1310 | ViewCell::NewMail(); |
---|
| 1311 | const int frontAndBackId = ViewCell::sMailId; |
---|
| 1312 | |
---|
[441] | 1313 | bool useRand;; |
---|
| 1314 | int limit; |
---|
[375] | 1315 | |
---|
[441] | 1316 | // choose test polyongs randomly if over threshold |
---|
| 1317 | if ((int)polys.size() > mMaxTests) |
---|
[375] | 1318 | { |
---|
[441] | 1319 | useRand = true; |
---|
| 1320 | limit = mMaxTests; |
---|
| 1321 | } |
---|
| 1322 | else |
---|
| 1323 | { |
---|
| 1324 | useRand = false; |
---|
| 1325 | limit = (int)polys.size(); |
---|
| 1326 | } |
---|
| 1327 | |
---|
[375] | 1328 | |
---|
[441] | 1329 | for (int i = 0; i < limit; ++ i) |
---|
| 1330 | { |
---|
| 1331 | const int testIdx = useRand ? Random(limit) : i; |
---|
| 1332 | |
---|
| 1333 | Polygon3 *poly = polys[testIdx]; |
---|
| 1334 | |
---|
[448] | 1335 | const int classification = |
---|
| 1336 | poly->ClassifyPlane(candidatePlane, mEpsilon); |
---|
[441] | 1337 | |
---|
[420] | 1338 | if (mSplitPlaneStrategy & BALANCED_POLYS) |
---|
[375] | 1339 | sumBalancedPolys += sBalancedPolysTable[classification]; |
---|
| 1340 | |
---|
[420] | 1341 | if (mSplitPlaneStrategy & LEAST_SPLITS) |
---|
[375] | 1342 | sumSplits += sLeastPolySplitsTable[classification]; |
---|
| 1343 | |
---|
[420] | 1344 | if (mSplitPlaneStrategy & LARGEST_POLY_AREA) |
---|
[375] | 1345 | { |
---|
| 1346 | if (classification == Polygon3::COINCIDENT) |
---|
[441] | 1347 | sumPolyArea += poly->GetArea(); |
---|
[375] | 1348 | //totalArea += area; |
---|
| 1349 | } |
---|
| 1350 | |
---|
[420] | 1351 | if (mSplitPlaneStrategy & BLOCKED_RAYS) |
---|
[375] | 1352 | { |
---|
[441] | 1353 | const float blockedRays = (float)poly->mPiercingRays.size(); |
---|
[375] | 1354 | |
---|
| 1355 | if (classification == Polygon3::COINCIDENT) |
---|
| 1356 | sumBlockedRays += blockedRays; |
---|
| 1357 | |
---|
| 1358 | totalBlockedRays += blockedRays; |
---|
| 1359 | } |
---|
| 1360 | |
---|
| 1361 | // assign view cells to back or front according to classificaion |
---|
[420] | 1362 | if (mSplitPlaneStrategy & BALANCED_VIEW_CELLS) |
---|
[375] | 1363 | { |
---|
[441] | 1364 | MeshInstance *viewCell = poly->mParent; |
---|
[375] | 1365 | |
---|
| 1366 | // assure that we only count a view cell |
---|
| 1367 | // once for the front and once for the back side of the plane |
---|
| 1368 | if (classification == Polygon3::FRONT_SIDE) |
---|
| 1369 | { |
---|
| 1370 | if ((viewCell->mMailbox != frontId) && |
---|
| 1371 | (viewCell->mMailbox != frontAndBackId)) |
---|
| 1372 | { |
---|
| 1373 | sumBalancedViewCells += 1.0; |
---|
| 1374 | |
---|
| 1375 | if (viewCell->mMailbox != backId) |
---|
| 1376 | viewCell->mMailbox = frontId; |
---|
| 1377 | else |
---|
| 1378 | viewCell->mMailbox = frontAndBackId; |
---|
| 1379 | |
---|
| 1380 | ++ totalViewCells; |
---|
| 1381 | } |
---|
| 1382 | } |
---|
| 1383 | else if (classification == Polygon3::BACK_SIDE) |
---|
| 1384 | { |
---|
| 1385 | if ((viewCell->mMailbox != backId) && |
---|
| 1386 | (viewCell->mMailbox != frontAndBackId)) |
---|
| 1387 | { |
---|
| 1388 | sumBalancedViewCells -= 1.0; |
---|
| 1389 | |
---|
| 1390 | if (viewCell->mMailbox != frontId) |
---|
| 1391 | viewCell->mMailbox = backId; |
---|
| 1392 | else |
---|
| 1393 | viewCell->mMailbox = frontAndBackId; |
---|
| 1394 | |
---|
| 1395 | ++ totalViewCells; |
---|
| 1396 | } |
---|
| 1397 | } |
---|
| 1398 | } |
---|
| 1399 | } |
---|
| 1400 | |
---|
[420] | 1401 | const float polysSize = (float)polys.size() + Limits::Small; |
---|
| 1402 | |
---|
[375] | 1403 | // all values should be approx. between 0 and 1 so they can be combined |
---|
| 1404 | // and scaled with the factors according to their importance |
---|
[420] | 1405 | if (mSplitPlaneStrategy & BALANCED_POLYS) |
---|
[421] | 1406 | val += mBalancedPolysFactor * fabs(sumBalancedPolys) / polysSize; |
---|
[384] | 1407 | |
---|
[420] | 1408 | if (mSplitPlaneStrategy & LEAST_SPLITS) |
---|
[421] | 1409 | val += mLeastSplitsFactor * sumSplits / polysSize; |
---|
[375] | 1410 | |
---|
[420] | 1411 | if (mSplitPlaneStrategy & LARGEST_POLY_AREA) |
---|
[375] | 1412 | // HACK: polys.size should be total area so scaling is between 0 and 1 |
---|
[421] | 1413 | val += mLargestPolyAreaFactor * (float)polys.size() / sumPolyArea; |
---|
[375] | 1414 | |
---|
[420] | 1415 | if (mSplitPlaneStrategy & BLOCKED_RAYS) |
---|
[375] | 1416 | if (totalBlockedRays != 0) |
---|
[421] | 1417 | val += mBlockedRaysFactor * (totalBlockedRays - sumBlockedRays) / totalBlockedRays; |
---|
[375] | 1418 | |
---|
[420] | 1419 | if (mSplitPlaneStrategy & BALANCED_VIEW_CELLS) |
---|
[421] | 1420 | val += mBalancedViewCellsFactor * fabs(sumBalancedViewCells) / |
---|
[420] | 1421 | ((float)totalViewCells + Limits::Small); |
---|
[375] | 1422 | |
---|
| 1423 | return val; |
---|
| 1424 | } |
---|
| 1425 | |
---|
| 1426 | |
---|
[428] | 1427 | inline void BspTree::GenerateUniqueIdsForPvs() |
---|
| 1428 | { |
---|
| 1429 | Intersectable::NewMail(); sBackId = ViewCell::sMailId; |
---|
| 1430 | Intersectable::NewMail(); sFrontId = ViewCell::sMailId; |
---|
| 1431 | Intersectable::NewMail(); sFrontAndBackId = ViewCell::sMailId; |
---|
| 1432 | } |
---|
| 1433 | |
---|
[384] | 1434 | float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, |
---|
| 1435 | const BoundedRayContainer &rays, |
---|
[442] | 1436 | const int pvs, |
---|
| 1437 | const float area, |
---|
| 1438 | const BspNodeGeometry &cell) const |
---|
[375] | 1439 | { |
---|
| 1440 | float val = 0; |
---|
| 1441 | |
---|
| 1442 | float sumBalancedRays = 0; |
---|
| 1443 | float sumRaySplits = 0; |
---|
| 1444 | |
---|
[400] | 1445 | int frontPvs = 0; |
---|
| 1446 | int backPvs = 0; |
---|
[390] | 1447 | |
---|
| 1448 | // probability that view point lies in child |
---|
[442] | 1449 | float pOverall = 0; |
---|
| 1450 | float pFront = 0; |
---|
| 1451 | float pBack = 0; |
---|
[436] | 1452 | |
---|
[442] | 1453 | const bool pvsUseLen = false; |
---|
| 1454 | |
---|
[420] | 1455 | if (mSplitPlaneStrategy & PVS) |
---|
[390] | 1456 | { |
---|
[428] | 1457 | // create unique ids for pvs heuristics |
---|
| 1458 | GenerateUniqueIdsForPvs(); |
---|
[442] | 1459 | |
---|
| 1460 | if (mPvsUseArea) // use front and back cell areas to approximate volume |
---|
| 1461 | { |
---|
| 1462 | // construct child geometry with regard to the candidate split plane |
---|
| 1463 | BspNodeGeometry frontCell; |
---|
| 1464 | BspNodeGeometry backCell; |
---|
| 1465 | |
---|
[448] | 1466 | cell.SplitGeometry(frontCell, |
---|
| 1467 | backCell, |
---|
| 1468 | candidatePlane, |
---|
| 1469 | mBox, |
---|
| 1470 | mEpsilon); |
---|
[442] | 1471 | |
---|
| 1472 | pFront = frontCell.GetArea(); |
---|
| 1473 | pBack = backCell.GetArea(); |
---|
| 1474 | |
---|
| 1475 | pOverall = area; |
---|
| 1476 | } |
---|
[390] | 1477 | } |
---|
| 1478 | |
---|
[441] | 1479 | bool useRand;; |
---|
| 1480 | int limit; |
---|
[375] | 1481 | |
---|
[441] | 1482 | // choose test polyongs randomly if over threshold |
---|
| 1483 | if ((int)rays.size() > mMaxTests) |
---|
[375] | 1484 | { |
---|
[441] | 1485 | useRand = true; |
---|
| 1486 | limit = mMaxTests; |
---|
| 1487 | } |
---|
| 1488 | else |
---|
| 1489 | { |
---|
| 1490 | useRand = false; |
---|
| 1491 | limit = (int)rays.size(); |
---|
| 1492 | } |
---|
[375] | 1493 | |
---|
[441] | 1494 | for (int i = 0; i < limit; ++ i) |
---|
| 1495 | { |
---|
| 1496 | const int testIdx = useRand ? Random(limit) : i; |
---|
| 1497 | |
---|
| 1498 | BoundedRay *bRay = rays[testIdx]; |
---|
| 1499 | |
---|
| 1500 | Ray *ray = bRay->mRay; |
---|
| 1501 | const float minT = bRay->mMinT; |
---|
| 1502 | const float maxT = bRay->mMaxT; |
---|
| 1503 | |
---|
[406] | 1504 | Vector3 entP, extP; |
---|
| 1505 | |
---|
[375] | 1506 | const int cf = |
---|
[406] | 1507 | ray->ClassifyPlane(candidatePlane, minT, maxT, entP, extP); |
---|
[375] | 1508 | |
---|
[420] | 1509 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) |
---|
[375] | 1510 | { |
---|
| 1511 | sumBalancedRays += sBalancedRaysTable[cf]; |
---|
| 1512 | } |
---|
| 1513 | |
---|
[420] | 1514 | if (mSplitPlaneStrategy & BALANCED_RAYS) |
---|
[375] | 1515 | { |
---|
| 1516 | sumRaySplits += sLeastRaySplitsTable[cf]; |
---|
| 1517 | } |
---|
| 1518 | |
---|
[420] | 1519 | if (mSplitPlaneStrategy & PVS) |
---|
[375] | 1520 | { |
---|
[428] | 1521 | // in case the ray intersects an object |
---|
| 1522 | // assure that we only count the object |
---|
| 1523 | // once for the front and once for the back side of the plane |
---|
| 1524 | |
---|
| 1525 | // add the termination object |
---|
[437] | 1526 | if (!ray->intersections.empty()) |
---|
| 1527 | AddObjToPvs(ray->intersections[0].mObject, cf, frontPvs, backPvs); |
---|
[428] | 1528 | |
---|
| 1529 | // add the source object |
---|
| 1530 | AddObjToPvs(ray->sourceObject.mObject, cf, frontPvs, backPvs); |
---|
| 1531 | |
---|
[442] | 1532 | if (mPvsUseArea) |
---|
[441] | 1533 | { |
---|
[442] | 1534 | float len = 1; |
---|
| 1535 | |
---|
| 1536 | if (pvsUseLen) |
---|
| 1537 | len = SqrDistance(entP, extP); |
---|
| 1538 | |
---|
| 1539 | // use length of rays to approximate volume |
---|
| 1540 | if (Ray::BACK && Ray::COINCIDENT) |
---|
| 1541 | pBack += len; |
---|
| 1542 | if (Ray::FRONT && Ray::COINCIDENT) |
---|
| 1543 | pFront += len; |
---|
| 1544 | if (Ray::FRONT_BACK || Ray::BACK_FRONT) |
---|
[406] | 1545 | { |
---|
[442] | 1546 | if (pvsUseLen) |
---|
| 1547 | { |
---|
| 1548 | const Vector3 extp = ray->Extrap(maxT); |
---|
| 1549 | const float t = candidatePlane.FindT(ray->GetLoc(), extp); |
---|
[441] | 1550 | |
---|
[442] | 1551 | const float newT = t * maxT; |
---|
| 1552 | const float newLen = SqrDistance(ray->Extrap(newT), extp); |
---|
[406] | 1553 | |
---|
[442] | 1554 | if (Ray::FRONT_BACK) |
---|
| 1555 | { |
---|
| 1556 | pFront += len - newLen; |
---|
| 1557 | pBack += newLen; |
---|
| 1558 | } |
---|
| 1559 | else |
---|
| 1560 | { |
---|
| 1561 | pBack += len - newLen; |
---|
| 1562 | pFront += newLen; |
---|
| 1563 | } |
---|
[441] | 1564 | } |
---|
| 1565 | else |
---|
| 1566 | { |
---|
[442] | 1567 | ++ pFront; |
---|
| 1568 | ++ pBack; |
---|
[441] | 1569 | } |
---|
[406] | 1570 | } |
---|
[442] | 1571 | } |
---|
[375] | 1572 | } |
---|
| 1573 | } |
---|
| 1574 | |
---|
[420] | 1575 | const float raysSize = (float)rays.size() + Limits::Small; |
---|
[437] | 1576 | |
---|
[420] | 1577 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) |
---|
[421] | 1578 | val += mLeastRaySplitsFactor * sumRaySplits / raysSize; |
---|
[375] | 1579 | |
---|
[420] | 1580 | if (mSplitPlaneStrategy & BALANCED_RAYS) |
---|
[421] | 1581 | val += mBalancedRaysFactor * fabs(sumBalancedRays) / raysSize; |
---|
[400] | 1582 | |
---|
[442] | 1583 | const float denom = pOverall * (float)pvs * 2.0f + Limits::Small; |
---|
[441] | 1584 | |
---|
| 1585 | if (mSplitPlaneStrategy & PVS) |
---|
[402] | 1586 | { |
---|
[421] | 1587 | val += mPvsFactor * (frontPvs * pFront + (backPvs * pBack)) / denom; |
---|
[375] | 1588 | |
---|
[409] | 1589 | // give penalty to unbalanced split |
---|
[452] | 1590 | if (0) |
---|
[442] | 1591 | if (((pFront * 0.2 + Limits::Small) > pBack) || |
---|
| 1592 | (pFront < (pBack * 0.2 + Limits::Small))) |
---|
[402] | 1593 | val += 0.5; |
---|
| 1594 | } |
---|
[404] | 1595 | |
---|
[441] | 1596 | |
---|
[418] | 1597 | #ifdef _DEBUG |
---|
[404] | 1598 | Debug << "totalpvs: " << pvs << " ptotal: " << pOverall |
---|
[400] | 1599 | << " frontpvs: " << frontPvs << " pFront: " << pFront |
---|
[418] | 1600 | << " backpvs: " << backPvs << " pBack: " << pBack << endl << endl; |
---|
| 1601 | #endif |
---|
[437] | 1602 | |
---|
[375] | 1603 | return val; |
---|
| 1604 | } |
---|
| 1605 | |
---|
[428] | 1606 | void BspTree::AddObjToPvs(Intersectable *obj, |
---|
| 1607 | const int cf, |
---|
| 1608 | int &frontPvs, |
---|
| 1609 | int &backPvs) const |
---|
[375] | 1610 | { |
---|
[428] | 1611 | if (!obj) |
---|
| 1612 | return; |
---|
[404] | 1613 | // TODO: does this really belong to no pvs? |
---|
| 1614 | //if (cf == Ray::COINCIDENT) return; |
---|
[375] | 1615 | |
---|
[428] | 1616 | // object belongs to both PVS |
---|
| 1617 | const bool bothSides = (cf == Ray::FRONT_BACK) || |
---|
| 1618 | (cf == Ray::BACK_FRONT) || |
---|
| 1619 | (cf == Ray::COINCIDENT); |
---|
| 1620 | |
---|
| 1621 | if ((cf == Ray::FRONT) || bothSides) |
---|
[375] | 1622 | { |
---|
[428] | 1623 | if ((obj->mMailbox != sFrontId) && |
---|
| 1624 | (obj->mMailbox != sFrontAndBackId)) |
---|
[384] | 1625 | { |
---|
[390] | 1626 | ++ frontPvs; |
---|
[375] | 1627 | |
---|
[428] | 1628 | if (obj->mMailbox == sBackId) |
---|
| 1629 | obj->mMailbox = sFrontAndBackId; |
---|
[384] | 1630 | else |
---|
[428] | 1631 | obj->mMailbox = sFrontId; |
---|
[384] | 1632 | } |
---|
[375] | 1633 | } |
---|
[428] | 1634 | |
---|
| 1635 | if ((cf == Ray::BACK) || bothSides) |
---|
[375] | 1636 | { |
---|
[428] | 1637 | if ((obj->mMailbox != sBackId) && |
---|
| 1638 | (obj->mMailbox != sFrontAndBackId)) |
---|
[375] | 1639 | { |
---|
[390] | 1640 | ++ backPvs; |
---|
[375] | 1641 | |
---|
[428] | 1642 | if (obj->mMailbox == sFrontId) |
---|
| 1643 | obj->mMailbox = sFrontAndBackId; |
---|
[375] | 1644 | else |
---|
[428] | 1645 | obj->mMailbox = sBackId; |
---|
[375] | 1646 | } |
---|
| 1647 | } |
---|
| 1648 | } |
---|
| 1649 | |
---|
[390] | 1650 | float BspTree::SplitPlaneCost(const Plane3 &candidatePlane, |
---|
[400] | 1651 | BspTraversalData &data) const |
---|
[375] | 1652 | { |
---|
| 1653 | float val = 0; |
---|
| 1654 | |
---|
[420] | 1655 | if (mSplitPlaneStrategy & VERTICAL_AXIS) |
---|
[375] | 1656 | { |
---|
| 1657 | Vector3 tinyAxis(0,0,0); tinyAxis[mBox.Size().TinyAxis()] = 1.0f; |
---|
| 1658 | // we put a penalty on the dot product between the "tiny" vertical axis |
---|
| 1659 | // and the split plane axis |
---|
[421] | 1660 | val += mVerticalSplitsFactor * |
---|
[375] | 1661 | fabs(DotProd(candidatePlane.mNormal, tinyAxis)); |
---|
| 1662 | } |
---|
| 1663 | |
---|
| 1664 | // the following criteria loop over all polygons to find the cost value |
---|
[420] | 1665 | if ((mSplitPlaneStrategy & BALANCED_POLYS) || |
---|
| 1666 | (mSplitPlaneStrategy & LEAST_SPLITS) || |
---|
| 1667 | (mSplitPlaneStrategy & LARGEST_POLY_AREA) || |
---|
| 1668 | (mSplitPlaneStrategy & BALANCED_VIEW_CELLS) || |
---|
| 1669 | (mSplitPlaneStrategy & BLOCKED_RAYS)) |
---|
[375] | 1670 | { |
---|
[390] | 1671 | val += SplitPlaneCost(candidatePlane, *data.mPolygons); |
---|
[375] | 1672 | } |
---|
| 1673 | |
---|
| 1674 | // the following criteria loop over all rays to find the cost value |
---|
[420] | 1675 | if ((mSplitPlaneStrategy & BALANCED_RAYS) || |
---|
| 1676 | (mSplitPlaneStrategy & LEAST_RAY_SPLITS) || |
---|
| 1677 | (mSplitPlaneStrategy & PVS)) |
---|
[375] | 1678 | { |
---|
[442] | 1679 | val += SplitPlaneCost(candidatePlane, *data.mRays, data.mPvs, |
---|
| 1680 | data.mArea, *data.mGeometry); |
---|
[375] | 1681 | } |
---|
| 1682 | |
---|
| 1683 | // return linear combination of the sums |
---|
| 1684 | return val; |
---|
| 1685 | } |
---|
| 1686 | |
---|
| 1687 | void BspTree::CollectLeaves(vector<BspLeaf *> &leaves) const |
---|
| 1688 | { |
---|
| 1689 | stack<BspNode *> nodeStack; |
---|
| 1690 | nodeStack.push(mRoot); |
---|
| 1691 | |
---|
| 1692 | while (!nodeStack.empty()) |
---|
| 1693 | { |
---|
| 1694 | BspNode *node = nodeStack.top(); |
---|
| 1695 | |
---|
| 1696 | nodeStack.pop(); |
---|
| 1697 | |
---|
| 1698 | if (node->IsLeaf()) |
---|
| 1699 | { |
---|
| 1700 | BspLeaf *leaf = (BspLeaf *)node; |
---|
| 1701 | leaves.push_back(leaf); |
---|
| 1702 | } |
---|
| 1703 | else |
---|
| 1704 | { |
---|
| 1705 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 1706 | |
---|
| 1707 | nodeStack.push(interior->GetBack()); |
---|
| 1708 | nodeStack.push(interior->GetFront()); |
---|
| 1709 | } |
---|
| 1710 | } |
---|
| 1711 | } |
---|
| 1712 | |
---|
| 1713 | AxisAlignedBox3 BspTree::GetBoundingBox() const |
---|
| 1714 | { |
---|
| 1715 | return mBox; |
---|
| 1716 | } |
---|
| 1717 | |
---|
| 1718 | BspNode *BspTree::GetRoot() const |
---|
| 1719 | { |
---|
| 1720 | return mRoot; |
---|
| 1721 | } |
---|
| 1722 | |
---|
| 1723 | void BspTree::EvaluateLeafStats(const BspTraversalData &data) |
---|
| 1724 | { |
---|
| 1725 | // the node became a leaf -> evaluate stats for leafs |
---|
| 1726 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode); |
---|
| 1727 | |
---|
| 1728 | // store maximal and minimal depth |
---|
| 1729 | if (data.mDepth > mStat.maxDepth) |
---|
| 1730 | mStat.maxDepth = data.mDepth; |
---|
| 1731 | |
---|
| 1732 | if (data.mDepth < mStat.minDepth) |
---|
| 1733 | mStat.minDepth = data.mDepth; |
---|
| 1734 | |
---|
[409] | 1735 | // accumulate depth to compute average depth |
---|
[375] | 1736 | mStat.accumDepth += data.mDepth; |
---|
| 1737 | |
---|
[426] | 1738 | |
---|
| 1739 | if (data.mDepth >= mTermMaxDepth) |
---|
| 1740 | ++ mStat.maxDepthNodes; |
---|
| 1741 | |
---|
[462] | 1742 | if (data.mPvs < mTermMinPvs) |
---|
| 1743 | ++ mStat.minPvsNodes; |
---|
| 1744 | |
---|
| 1745 | if ((int)data.mRays->size() < mTermMinRays) |
---|
| 1746 | ++ mStat.minRaysNodes; |
---|
| 1747 | |
---|
| 1748 | if (data.GetAvgRayContribution() > mTermMaxRayContribution) |
---|
| 1749 | ++ mStat.maxRayContribNodes; |
---|
| 1750 | |
---|
[442] | 1751 | if (data.mGeometry->GetArea() <= mTermMinArea) |
---|
| 1752 | ++ mStat.minAreaNodes; |
---|
[426] | 1753 | |
---|
[410] | 1754 | #ifdef _DEBUG |
---|
[375] | 1755 | Debug << "BSP stats: " |
---|
[420] | 1756 | << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), " |
---|
| 1757 | << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), " |
---|
| 1758 | << "Area: " << data.mArea << " (min: " << mTermMinArea << "), " |
---|
[426] | 1759 | << "#polygons: " << (int)data.mPolygons->size() << " (max: " << mTermMinPolys << "), " |
---|
| 1760 | << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), " |
---|
[409] | 1761 | << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, " |
---|
| 1762 | << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl; |
---|
[410] | 1763 | #endif |
---|
[375] | 1764 | } |
---|
| 1765 | |
---|
| 1766 | int BspTree::CastRay(Ray &ray) |
---|
| 1767 | { |
---|
| 1768 | int hits = 0; |
---|
| 1769 | |
---|
| 1770 | stack<BspRayTraversalData> tStack; |
---|
| 1771 | |
---|
| 1772 | float maxt, mint; |
---|
| 1773 | |
---|
[448] | 1774 | if (!mBox.GetRaySegment(ray, mint, maxt)) |
---|
[375] | 1775 | return 0; |
---|
| 1776 | |
---|
| 1777 | Intersectable::NewMail(); |
---|
| 1778 | |
---|
| 1779 | Vector3 entp = ray.Extrap(mint); |
---|
| 1780 | Vector3 extp = ray.Extrap(maxt); |
---|
| 1781 | |
---|
| 1782 | BspNode *node = mRoot; |
---|
| 1783 | BspNode *farChild = NULL; |
---|
| 1784 | |
---|
| 1785 | while (1) |
---|
| 1786 | { |
---|
| 1787 | if (!node->IsLeaf()) |
---|
| 1788 | { |
---|
[448] | 1789 | BspInterior *in = dynamic_cast<BspInterior *>(node); |
---|
[375] | 1790 | |
---|
[448] | 1791 | Plane3 splitPlane = in->GetPlane(); |
---|
| 1792 | const int entSide = splitPlane.Side(entp); |
---|
| 1793 | const int extSide = splitPlane.Side(extp); |
---|
[375] | 1794 | |
---|
| 1795 | if (entSide < 0) |
---|
| 1796 | { |
---|
| 1797 | node = in->GetBack(); |
---|
| 1798 | |
---|
| 1799 | if(extSide <= 0) // plane does not split ray => no far child |
---|
| 1800 | continue; |
---|
| 1801 | |
---|
| 1802 | farChild = in->GetFront(); // plane splits ray |
---|
| 1803 | |
---|
| 1804 | } else if (entSide > 0) |
---|
| 1805 | { |
---|
| 1806 | node = in->GetFront(); |
---|
| 1807 | |
---|
| 1808 | if (extSide >= 0) // plane does not split ray => no far child |
---|
| 1809 | continue; |
---|
| 1810 | |
---|
| 1811 | farChild = in->GetBack(); // plane splits ray |
---|
| 1812 | } |
---|
[389] | 1813 | else // ray and plane are coincident |
---|
[375] | 1814 | { |
---|
[420] | 1815 | // WHAT TO DO IN THIS CASE ? |
---|
| 1816 | //break; |
---|
| 1817 | node = in->GetFront(); |
---|
| 1818 | continue; |
---|
[375] | 1819 | } |
---|
| 1820 | |
---|
| 1821 | // push data for far child |
---|
| 1822 | tStack.push(BspRayTraversalData(farChild, extp, maxt)); |
---|
| 1823 | |
---|
| 1824 | // find intersection of ray segment with plane |
---|
| 1825 | float t; |
---|
[448] | 1826 | extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t); |
---|
[375] | 1827 | maxt *= t; |
---|
| 1828 | |
---|
| 1829 | } else // reached leaf => intersection with view cell |
---|
| 1830 | { |
---|
| 1831 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); |
---|
| 1832 | |
---|
| 1833 | if (!leaf->mViewCell->Mailed()) |
---|
| 1834 | { |
---|
| 1835 | ray.bspIntersections.push_back(Ray::BspIntersection(maxt, leaf)); |
---|
| 1836 | leaf->mViewCell->Mail(); |
---|
| 1837 | ++ hits; |
---|
| 1838 | } |
---|
| 1839 | |
---|
| 1840 | //-- fetch the next far child from the stack |
---|
| 1841 | if (tStack.empty()) |
---|
| 1842 | break; |
---|
| 1843 | |
---|
| 1844 | entp = extp; |
---|
| 1845 | mint = maxt; // NOTE: need this? |
---|
| 1846 | |
---|
| 1847 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f) |
---|
| 1848 | break; |
---|
| 1849 | |
---|
| 1850 | BspRayTraversalData &s = tStack.top(); |
---|
| 1851 | |
---|
| 1852 | node = s.mNode; |
---|
| 1853 | extp = s.mExitPoint; |
---|
| 1854 | maxt = s.mMaxT; |
---|
| 1855 | |
---|
| 1856 | tStack.pop(); |
---|
| 1857 | } |
---|
| 1858 | } |
---|
| 1859 | |
---|
| 1860 | return hits; |
---|
| 1861 | } |
---|
| 1862 | |
---|
| 1863 | bool BspTree::Export(const string filename) |
---|
| 1864 | { |
---|
| 1865 | Exporter *exporter = Exporter::GetExporter(filename); |
---|
| 1866 | |
---|
| 1867 | if (exporter) |
---|
| 1868 | { |
---|
| 1869 | exporter->ExportBspTree(*this); |
---|
| 1870 | return true; |
---|
| 1871 | } |
---|
| 1872 | |
---|
| 1873 | return false; |
---|
| 1874 | } |
---|
| 1875 | |
---|
| 1876 | void BspTree::CollectViewCells(ViewCellContainer &viewCells) const |
---|
| 1877 | { |
---|
| 1878 | stack<BspNode *> nodeStack; |
---|
| 1879 | nodeStack.push(mRoot); |
---|
| 1880 | |
---|
| 1881 | ViewCell::NewMail(); |
---|
| 1882 | |
---|
| 1883 | while (!nodeStack.empty()) |
---|
| 1884 | { |
---|
| 1885 | BspNode *node = nodeStack.top(); |
---|
| 1886 | nodeStack.pop(); |
---|
| 1887 | |
---|
| 1888 | if (node->IsLeaf()) |
---|
| 1889 | { |
---|
| 1890 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->mViewCell; |
---|
| 1891 | |
---|
| 1892 | if (!viewCell->Mailed()) |
---|
| 1893 | { |
---|
| 1894 | viewCell->Mail(); |
---|
| 1895 | viewCells.push_back(viewCell); |
---|
| 1896 | } |
---|
| 1897 | } |
---|
| 1898 | else |
---|
| 1899 | { |
---|
| 1900 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 1901 | |
---|
[448] | 1902 | nodeStack.push(interior->GetFront()); |
---|
| 1903 | nodeStack.push(interior->GetBack()); |
---|
[375] | 1904 | } |
---|
| 1905 | } |
---|
| 1906 | } |
---|
| 1907 | |
---|
[462] | 1908 | void BspTree::EvaluateViewCellsStats(ViewCellsStatistics &vcStat) const |
---|
[375] | 1909 | { |
---|
[462] | 1910 | vcStat.Reset(); |
---|
[375] | 1911 | |
---|
| 1912 | stack<BspNode *> nodeStack; |
---|
| 1913 | nodeStack.push(mRoot); |
---|
| 1914 | |
---|
| 1915 | ViewCell::NewMail(); |
---|
| 1916 | |
---|
| 1917 | // exclude root cell |
---|
| 1918 | mRootCell->Mail(); |
---|
| 1919 | |
---|
| 1920 | while (!nodeStack.empty()) |
---|
| 1921 | { |
---|
| 1922 | BspNode *node = nodeStack.top(); |
---|
| 1923 | nodeStack.pop(); |
---|
| 1924 | |
---|
| 1925 | if (node->IsLeaf()) |
---|
| 1926 | { |
---|
[462] | 1927 | ++ vcStat.leaves; |
---|
[375] | 1928 | |
---|
| 1929 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->mViewCell; |
---|
| 1930 | |
---|
| 1931 | if (!viewCell->Mailed()) |
---|
| 1932 | { |
---|
| 1933 | viewCell->Mail(); |
---|
| 1934 | |
---|
[462] | 1935 | ++ vcStat.viewCells; |
---|
[414] | 1936 | const int pvsSize = viewCell->GetPvs().GetSize(); |
---|
[375] | 1937 | |
---|
[462] | 1938 | vcStat.pvs += pvsSize; |
---|
[375] | 1939 | |
---|
[414] | 1940 | if (pvsSize < 1) |
---|
[462] | 1941 | ++ vcStat.emptyPvs; |
---|
[375] | 1942 | |
---|
[462] | 1943 | if (pvsSize > vcStat.maxPvs) |
---|
| 1944 | vcStat.maxPvs = pvsSize; |
---|
[375] | 1945 | |
---|
[462] | 1946 | if (pvsSize < vcStat.minPvs) |
---|
| 1947 | vcStat.minPvs = pvsSize; |
---|
[375] | 1948 | |
---|
[462] | 1949 | if ((int)viewCell->mBspLeaves.size() > vcStat.maxLeaves) |
---|
| 1950 | vcStat.maxLeaves = (int)viewCell->mBspLeaves.size(); |
---|
[375] | 1951 | } |
---|
| 1952 | } |
---|
| 1953 | else |
---|
| 1954 | { |
---|
| 1955 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 1956 | |
---|
[448] | 1957 | nodeStack.push(interior->GetFront()); |
---|
| 1958 | nodeStack.push(interior->GetBack()); |
---|
[375] | 1959 | } |
---|
| 1960 | } |
---|
| 1961 | } |
---|
| 1962 | |
---|
| 1963 | BspTreeStatistics &BspTree::GetStat() |
---|
| 1964 | { |
---|
| 1965 | return mStat; |
---|
| 1966 | } |
---|
| 1967 | |
---|
[409] | 1968 | float BspTree::AccumulatedRayLength(BoundedRayContainer &rays) const |
---|
| 1969 | { |
---|
| 1970 | float len = 0; |
---|
| 1971 | |
---|
| 1972 | BoundedRayContainer::const_iterator it, it_end = rays.end(); |
---|
| 1973 | |
---|
| 1974 | for (it = rays.begin(); it != it_end; ++ it) |
---|
| 1975 | { |
---|
| 1976 | len += SqrDistance((*it)->mRay->Extrap((*it)->mMinT), |
---|
| 1977 | (*it)->mRay->Extrap((*it)->mMaxT)); |
---|
| 1978 | } |
---|
| 1979 | |
---|
| 1980 | return len; |
---|
| 1981 | } |
---|
| 1982 | |
---|
[375] | 1983 | int BspTree::SplitRays(const Plane3 &plane, |
---|
| 1984 | BoundedRayContainer &rays, |
---|
| 1985 | BoundedRayContainer &frontRays, |
---|
| 1986 | BoundedRayContainer &backRays) |
---|
| 1987 | { |
---|
| 1988 | int splits = 0; |
---|
| 1989 | |
---|
| 1990 | while (!rays.empty()) |
---|
| 1991 | { |
---|
| 1992 | BoundedRay *bRay = rays.back(); |
---|
| 1993 | Ray *ray = bRay->mRay; |
---|
[406] | 1994 | float minT = bRay->mMinT; |
---|
| 1995 | float maxT = bRay->mMaxT; |
---|
| 1996 | |
---|
[375] | 1997 | rays.pop_back(); |
---|
[406] | 1998 | |
---|
| 1999 | Vector3 entP, extP; |
---|
| 2000 | |
---|
[375] | 2001 | const int cf = |
---|
[406] | 2002 | ray->ClassifyPlane(plane, minT, maxT, entP, extP); |
---|
[375] | 2003 | |
---|
[406] | 2004 | // set id to ray classification |
---|
[375] | 2005 | ray->SetId(cf); |
---|
| 2006 | |
---|
| 2007 | switch (cf) |
---|
| 2008 | { |
---|
[406] | 2009 | case Ray::COINCIDENT: // TODO: should really discard ray? |
---|
[420] | 2010 | frontRays.push_back(bRay); |
---|
| 2011 | //DEL_PTR(bRay); |
---|
[375] | 2012 | break; |
---|
| 2013 | case Ray::BACK: |
---|
| 2014 | backRays.push_back(bRay); |
---|
| 2015 | break; |
---|
| 2016 | case Ray::FRONT: |
---|
| 2017 | frontRays.push_back(bRay); |
---|
| 2018 | break; |
---|
| 2019 | case Ray::FRONT_BACK: |
---|
| 2020 | { |
---|
| 2021 | // find intersection of ray segment with plane |
---|
[406] | 2022 | const float t = plane.FindT(ray->GetLoc(), extP); |
---|
[375] | 2023 | |
---|
[406] | 2024 | const float newT = t * maxT; |
---|
[375] | 2025 | |
---|
[406] | 2026 | frontRays.push_back(new BoundedRay(ray, minT, newT)); |
---|
| 2027 | backRays.push_back(new BoundedRay(ray, newT, maxT)); |
---|
[375] | 2028 | |
---|
| 2029 | DEL_PTR(bRay); |
---|
| 2030 | } |
---|
| 2031 | break; |
---|
| 2032 | case Ray::BACK_FRONT: |
---|
| 2033 | { |
---|
| 2034 | // find intersection of ray segment with plane |
---|
[406] | 2035 | const float t = plane.FindT(ray->GetLoc(), extP); |
---|
[375] | 2036 | const float newT = t * bRay->mMaxT; |
---|
| 2037 | |
---|
[444] | 2038 | backRays.push_back(new BoundedRay(ray, minT, newT)); |
---|
| 2039 | frontRays.push_back(new BoundedRay(ray, newT, maxT)); |
---|
| 2040 | |
---|
[375] | 2041 | DEL_PTR(bRay); |
---|
| 2042 | |
---|
| 2043 | ++ splits; |
---|
| 2044 | } |
---|
| 2045 | break; |
---|
| 2046 | default: |
---|
[430] | 2047 | Debug << "Should not come here 4" << endl; |
---|
[375] | 2048 | break; |
---|
| 2049 | } |
---|
| 2050 | } |
---|
| 2051 | |
---|
| 2052 | return splits; |
---|
| 2053 | } |
---|
| 2054 | |
---|
[396] | 2055 | void BspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const |
---|
[375] | 2056 | { |
---|
| 2057 | BspNode *lastNode; |
---|
| 2058 | do |
---|
| 2059 | { |
---|
| 2060 | lastNode = n; |
---|
| 2061 | |
---|
| 2062 | // want to get planes defining geometry of this node => don't take |
---|
| 2063 | // split plane of node itself |
---|
| 2064 | n = n->GetParent(); |
---|
| 2065 | |
---|
| 2066 | if (n) |
---|
| 2067 | { |
---|
| 2068 | BspInterior *interior = dynamic_cast<BspInterior *>(n); |
---|
[448] | 2069 | Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane(); |
---|
[375] | 2070 | |
---|
[448] | 2071 | if (interior->GetFront() != lastNode) |
---|
[396] | 2072 | halfSpace.ReverseOrientation(); |
---|
| 2073 | |
---|
| 2074 | halfSpaces.push_back(halfSpace); |
---|
[375] | 2075 | } |
---|
| 2076 | } |
---|
| 2077 | while (n); |
---|
| 2078 | } |
---|
| 2079 | |
---|
[390] | 2080 | void BspTree::ConstructGeometry(BspNode *n, BspNodeGeometry &cell) const |
---|
[375] | 2081 | { |
---|
[396] | 2082 | PolygonContainer polys; |
---|
| 2083 | ConstructGeometry(n, polys); |
---|
| 2084 | cell.mPolys = polys; |
---|
[375] | 2085 | } |
---|
| 2086 | |
---|
| 2087 | void BspTree::ConstructGeometry(BspViewCell *vc, PolygonContainer &cell) const |
---|
| 2088 | { |
---|
| 2089 | vector<BspLeaf *> leaves = vc->mBspLeaves; |
---|
| 2090 | |
---|
| 2091 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end(); |
---|
| 2092 | |
---|
| 2093 | for (it = leaves.begin(); it != it_end; ++ it) |
---|
| 2094 | ConstructGeometry(*it, cell); |
---|
| 2095 | } |
---|
| 2096 | |
---|
[396] | 2097 | |
---|
[462] | 2098 | void BspTree::ConstructGeometry(BspNode *n, PolygonContainer &cell) const |
---|
| 2099 | { |
---|
| 2100 | vector<Plane3> halfSpaces; |
---|
| 2101 | ExtractHalfSpaces(n, halfSpaces); |
---|
| 2102 | |
---|
| 2103 | PolygonContainer candidates; |
---|
| 2104 | |
---|
| 2105 | // bounded planes are added to the polygons (reverse polygons |
---|
| 2106 | // as they have to be outfacing |
---|
| 2107 | for (int i = 0; i < (int)halfSpaces.size(); ++ i) |
---|
| 2108 | { |
---|
| 2109 | Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]); |
---|
| 2110 | |
---|
| 2111 | if (p->Valid(mEpsilon)) |
---|
| 2112 | { |
---|
| 2113 | candidates.push_back(p->CreateReversePolygon()); |
---|
| 2114 | DEL_PTR(p); |
---|
| 2115 | } |
---|
| 2116 | } |
---|
| 2117 | |
---|
| 2118 | // add faces of bounding box (also could be faces of the cell) |
---|
| 2119 | for (int i = 0; i < 6; ++ i) |
---|
| 2120 | { |
---|
| 2121 | VertexContainer vertices; |
---|
| 2122 | |
---|
| 2123 | for (int j = 0; j < 4; ++ j) |
---|
| 2124 | vertices.push_back(mBox.GetFace(i).mVertices[j]); |
---|
| 2125 | |
---|
| 2126 | candidates.push_back(new Polygon3(vertices)); |
---|
| 2127 | } |
---|
| 2128 | |
---|
| 2129 | for (int i = 0; i < (int)candidates.size(); ++ i) |
---|
| 2130 | { |
---|
| 2131 | // polygon is split by all other planes |
---|
| 2132 | for (int j = 0; (j < (int)halfSpaces.size()) && candidates[i]; ++ j) |
---|
| 2133 | { |
---|
| 2134 | if (i == j) // polygon and plane are coincident |
---|
| 2135 | continue; |
---|
| 2136 | |
---|
| 2137 | VertexContainer splitPts; |
---|
| 2138 | Polygon3 *frontPoly, *backPoly; |
---|
| 2139 | |
---|
| 2140 | const int cf = candidates[i]-> |
---|
| 2141 | ClassifyPlane(halfSpaces[j], mEpsilon); |
---|
| 2142 | |
---|
| 2143 | switch (cf) |
---|
| 2144 | { |
---|
| 2145 | case Polygon3::SPLIT: |
---|
| 2146 | frontPoly = new Polygon3(); |
---|
| 2147 | backPoly = new Polygon3(); |
---|
| 2148 | |
---|
| 2149 | candidates[i]->Split(halfSpaces[j], |
---|
| 2150 | *frontPoly, |
---|
| 2151 | *backPoly, |
---|
| 2152 | mEpsilon); |
---|
| 2153 | |
---|
| 2154 | DEL_PTR(candidates[i]); |
---|
| 2155 | |
---|
| 2156 | if (frontPoly->Valid(mEpsilon)) |
---|
| 2157 | candidates[i] = frontPoly; |
---|
| 2158 | else |
---|
| 2159 | DEL_PTR(frontPoly); |
---|
| 2160 | |
---|
| 2161 | DEL_PTR(backPoly); |
---|
| 2162 | break; |
---|
| 2163 | case Polygon3::BACK_SIDE: |
---|
| 2164 | DEL_PTR(candidates[i]); |
---|
| 2165 | break; |
---|
| 2166 | // just take polygon as it is |
---|
| 2167 | case Polygon3::FRONT_SIDE: |
---|
| 2168 | case Polygon3::COINCIDENT: |
---|
| 2169 | default: |
---|
| 2170 | break; |
---|
| 2171 | } |
---|
| 2172 | } |
---|
| 2173 | |
---|
| 2174 | if (candidates[i]) |
---|
| 2175 | cell.push_back(candidates[i]); |
---|
| 2176 | } |
---|
[396] | 2177 | } |
---|
| 2178 | |
---|
| 2179 | |
---|
[375] | 2180 | int BspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, |
---|
| 2181 | const bool onlyUnmailed) const |
---|
| 2182 | { |
---|
| 2183 | PolygonContainer cell; |
---|
| 2184 | |
---|
| 2185 | ConstructGeometry(n, cell); |
---|
| 2186 | |
---|
| 2187 | stack<BspNode *> nodeStack; |
---|
| 2188 | nodeStack.push(mRoot); |
---|
| 2189 | |
---|
| 2190 | // planes needed to verify that we found neighbor leaf. |
---|
[396] | 2191 | vector<Plane3> halfSpaces; |
---|
| 2192 | ExtractHalfSpaces(n, halfSpaces); |
---|
[375] | 2193 | |
---|
| 2194 | while (!nodeStack.empty()) |
---|
| 2195 | { |
---|
| 2196 | BspNode *node = nodeStack.top(); |
---|
| 2197 | nodeStack.pop(); |
---|
| 2198 | |
---|
| 2199 | if (node->IsLeaf()) |
---|
| 2200 | { |
---|
| 2201 | if (node != n && (!onlyUnmailed || !node->Mailed())) |
---|
| 2202 | { |
---|
[396] | 2203 | // test all planes of current node if candidate really |
---|
| 2204 | // is neighbour |
---|
[375] | 2205 | PolygonContainer neighborCandidate; |
---|
| 2206 | ConstructGeometry(node, neighborCandidate); |
---|
| 2207 | |
---|
| 2208 | bool isAdjacent = true; |
---|
[396] | 2209 | for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i) |
---|
[375] | 2210 | { |
---|
| 2211 | const int cf = |
---|
[448] | 2212 | Polygon3::ClassifyPlane(neighborCandidate, |
---|
| 2213 | halfSpaces[i], |
---|
| 2214 | mEpsilon); |
---|
[375] | 2215 | |
---|
[396] | 2216 | if (cf == Polygon3::BACK_SIDE) |
---|
[375] | 2217 | isAdjacent = false; |
---|
| 2218 | } |
---|
| 2219 | |
---|
| 2220 | if (isAdjacent) |
---|
| 2221 | neighbors.push_back(dynamic_cast<BspLeaf *>(node)); |
---|
| 2222 | |
---|
| 2223 | CLEAR_CONTAINER(neighborCandidate); |
---|
| 2224 | } |
---|
| 2225 | } |
---|
| 2226 | else |
---|
| 2227 | { |
---|
| 2228 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 2229 | |
---|
[448] | 2230 | const int cf = Polygon3::ClassifyPlane(cell, |
---|
| 2231 | interior->mPlane, |
---|
| 2232 | mEpsilon); |
---|
[375] | 2233 | |
---|
| 2234 | if (cf == Polygon3::FRONT_SIDE) |
---|
[448] | 2235 | nodeStack.push(interior->GetFront()); |
---|
[375] | 2236 | else |
---|
| 2237 | if (cf == Polygon3::BACK_SIDE) |
---|
[448] | 2238 | nodeStack.push(interior->GetBack()); |
---|
[375] | 2239 | else |
---|
| 2240 | { |
---|
| 2241 | // random decision |
---|
[448] | 2242 | nodeStack.push(interior->GetBack()); |
---|
| 2243 | nodeStack.push(interior->GetFront()); |
---|
[375] | 2244 | } |
---|
| 2245 | } |
---|
| 2246 | } |
---|
| 2247 | |
---|
| 2248 | CLEAR_CONTAINER(cell); |
---|
| 2249 | return (int)neighbors.size(); |
---|
| 2250 | } |
---|
| 2251 | |
---|
| 2252 | BspLeaf *BspTree::GetRandomLeaf(const Plane3 &halfspace) |
---|
| 2253 | { |
---|
| 2254 | stack<BspNode *> nodeStack; |
---|
| 2255 | nodeStack.push(mRoot); |
---|
| 2256 | |
---|
| 2257 | int mask = rand(); |
---|
| 2258 | |
---|
| 2259 | while (!nodeStack.empty()) |
---|
| 2260 | { |
---|
| 2261 | BspNode *node = nodeStack.top(); |
---|
| 2262 | nodeStack.pop(); |
---|
| 2263 | |
---|
| 2264 | if (node->IsLeaf()) |
---|
| 2265 | { |
---|
| 2266 | return dynamic_cast<BspLeaf *>(node); |
---|
| 2267 | } |
---|
| 2268 | else |
---|
| 2269 | { |
---|
| 2270 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 2271 | |
---|
| 2272 | BspNode *next; |
---|
| 2273 | |
---|
| 2274 | PolygonContainer cell; |
---|
| 2275 | |
---|
| 2276 | // todo: not very efficient: constructs full cell everytime |
---|
| 2277 | ConstructGeometry(interior, cell); |
---|
| 2278 | |
---|
[448] | 2279 | const int cf = Polygon3::ClassifyPlane(cell, |
---|
| 2280 | halfspace, |
---|
| 2281 | mEpsilon); |
---|
[375] | 2282 | |
---|
| 2283 | if (cf == Polygon3::BACK_SIDE) |
---|
[448] | 2284 | next = interior->GetFront(); |
---|
[375] | 2285 | else |
---|
| 2286 | if (cf == Polygon3::FRONT_SIDE) |
---|
[448] | 2287 | next = interior->GetFront(); |
---|
[375] | 2288 | else |
---|
| 2289 | { |
---|
| 2290 | // random decision |
---|
| 2291 | if (mask & 1) |
---|
[448] | 2292 | next = interior->GetBack(); |
---|
[375] | 2293 | else |
---|
[448] | 2294 | next = interior->GetFront(); |
---|
[375] | 2295 | mask = mask >> 1; |
---|
| 2296 | } |
---|
| 2297 | |
---|
| 2298 | nodeStack.push(next); |
---|
| 2299 | } |
---|
| 2300 | } |
---|
| 2301 | |
---|
| 2302 | return NULL; |
---|
| 2303 | } |
---|
| 2304 | |
---|
| 2305 | BspLeaf *BspTree::GetRandomLeaf(const bool onlyUnmailed) |
---|
| 2306 | { |
---|
| 2307 | stack<BspNode *> nodeStack; |
---|
| 2308 | |
---|
| 2309 | nodeStack.push(mRoot); |
---|
| 2310 | |
---|
| 2311 | int mask = rand(); |
---|
| 2312 | |
---|
| 2313 | while (!nodeStack.empty()) |
---|
| 2314 | { |
---|
| 2315 | BspNode *node = nodeStack.top(); |
---|
| 2316 | nodeStack.pop(); |
---|
| 2317 | |
---|
| 2318 | if (node->IsLeaf()) |
---|
| 2319 | { |
---|
| 2320 | if ( (!onlyUnmailed || !node->Mailed()) ) |
---|
| 2321 | return dynamic_cast<BspLeaf *>(node); |
---|
| 2322 | } |
---|
| 2323 | else |
---|
| 2324 | { |
---|
| 2325 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 2326 | |
---|
| 2327 | // random decision |
---|
| 2328 | if (mask & 1) |
---|
[448] | 2329 | nodeStack.push(interior->GetBack()); |
---|
[375] | 2330 | else |
---|
[448] | 2331 | nodeStack.push(interior->GetFront()); |
---|
[375] | 2332 | |
---|
| 2333 | mask = mask >> 1; |
---|
| 2334 | } |
---|
| 2335 | } |
---|
| 2336 | |
---|
| 2337 | return NULL; |
---|
| 2338 | } |
---|
[384] | 2339 | |
---|
[448] | 2340 | void BspTree::AddToPvs(BspLeaf *leaf, |
---|
| 2341 | const BoundedRayContainer &rays, |
---|
| 2342 | int &sampleContributions, |
---|
| 2343 | int &contributingSamples) |
---|
| 2344 | { |
---|
| 2345 | sampleContributions = 0; |
---|
| 2346 | contributingSamples = 0; |
---|
| 2347 | |
---|
| 2348 | BoundedRayContainer::const_iterator it, it_end = rays.end(); |
---|
| 2349 | |
---|
| 2350 | ViewCell *vc = leaf->GetViewCell(); |
---|
| 2351 | |
---|
| 2352 | // add contributions from samples to the PVS |
---|
| 2353 | for (it = rays.begin(); it != it_end; ++ it) |
---|
| 2354 | { |
---|
| 2355 | int contribution = 0; |
---|
| 2356 | Ray *ray = (*it)->mRay; |
---|
| 2357 | |
---|
| 2358 | if (!ray->intersections.empty()) |
---|
| 2359 | contribution += vc->GetPvs().AddSample(ray->intersections[0].mObject); |
---|
| 2360 | |
---|
| 2361 | if (ray->sourceObject.mObject) |
---|
| 2362 | contribution += vc->GetPvs().AddSample(ray->sourceObject.mObject); |
---|
| 2363 | |
---|
| 2364 | if (contribution) |
---|
| 2365 | { |
---|
| 2366 | sampleContributions += contribution; |
---|
| 2367 | ++ contributingSamples; |
---|
| 2368 | } |
---|
| 2369 | |
---|
| 2370 | //if (ray->mFlags & Ray::STORE_BSP_INTERSECTIONS) |
---|
| 2371 | // ray->bspIntersections.push_back(Ray::BspIntersection((*it)->mMinT, this)); |
---|
| 2372 | } |
---|
| 2373 | } |
---|
| 2374 | |
---|
[384] | 2375 | int BspTree::ComputePvsSize(const BoundedRayContainer &rays) const |
---|
| 2376 | { |
---|
| 2377 | int pvsSize = 0; |
---|
| 2378 | |
---|
| 2379 | BoundedRayContainer::const_iterator rit, rit_end = rays.end(); |
---|
| 2380 | |
---|
| 2381 | Intersectable::NewMail(); |
---|
| 2382 | |
---|
| 2383 | for (rit = rays.begin(); rit != rays.end(); ++ rit) |
---|
| 2384 | { |
---|
| 2385 | Ray *ray = (*rit)->mRay; |
---|
| 2386 | |
---|
| 2387 | if (!ray->intersections.empty()) |
---|
| 2388 | { |
---|
| 2389 | if (!ray->intersections[0].mObject->Mailed()) |
---|
| 2390 | { |
---|
| 2391 | ray->intersections[0].mObject->Mail(); |
---|
| 2392 | ++ pvsSize; |
---|
| 2393 | } |
---|
| 2394 | } |
---|
[399] | 2395 | if (ray->sourceObject.mObject) |
---|
[384] | 2396 | { |
---|
[399] | 2397 | if (!ray->sourceObject.mObject->Mailed()) |
---|
| 2398 | { |
---|
| 2399 | ray->sourceObject.mObject->Mail(); |
---|
| 2400 | ++ pvsSize; |
---|
| 2401 | } |
---|
[384] | 2402 | } |
---|
| 2403 | } |
---|
| 2404 | |
---|
| 2405 | return pvsSize; |
---|
[390] | 2406 | } |
---|
| 2407 | |
---|
[448] | 2408 | float BspTree::GetEpsilon() const |
---|
| 2409 | { |
---|
| 2410 | return mEpsilon; |
---|
| 2411 | } |
---|
[390] | 2412 | |
---|
[448] | 2413 | |
---|
| 2414 | /*************************************************************/ |
---|
| 2415 | /* BspNodeGeometry Implementation */ |
---|
| 2416 | /*************************************************************/ |
---|
| 2417 | |
---|
[390] | 2418 | BspNodeGeometry::~BspNodeGeometry() |
---|
| 2419 | { |
---|
| 2420 | CLEAR_CONTAINER(mPolys); |
---|
| 2421 | } |
---|
| 2422 | |
---|
| 2423 | float BspNodeGeometry::GetArea() const |
---|
| 2424 | { |
---|
| 2425 | return Polygon3::GetArea(mPolys); |
---|
| 2426 | } |
---|
| 2427 | |
---|
[396] | 2428 | void BspNodeGeometry::SplitGeometry(BspNodeGeometry &front, |
---|
| 2429 | BspNodeGeometry &back, |
---|
[448] | 2430 | const Plane3 &splitPlane, |
---|
| 2431 | const AxisAlignedBox3 &box, |
---|
| 2432 | const float epsilon) const |
---|
[396] | 2433 | { |
---|
[390] | 2434 | // get cross section of new polygon |
---|
[448] | 2435 | Polygon3 *planePoly = box.CrossSection(splitPlane); |
---|
[390] | 2436 | |
---|
[448] | 2437 | // split polygon with all other polygons |
---|
| 2438 | planePoly = SplitPolygon(planePoly, epsilon); |
---|
[394] | 2439 | |
---|
[448] | 2440 | //-- new polygon splits all other polygons |
---|
[396] | 2441 | for (int i = 0; i < (int)mPolys.size(); ++ i) |
---|
[390] | 2442 | { |
---|
[448] | 2443 | /// don't use epsilon here to get exact split planes |
---|
| 2444 | const int cf = |
---|
| 2445 | mPolys[i]->ClassifyPlane(splitPlane, Limits::Small); |
---|
[390] | 2446 | |
---|
| 2447 | switch (cf) |
---|
| 2448 | { |
---|
| 2449 | case Polygon3::SPLIT: |
---|
| 2450 | { |
---|
[396] | 2451 | Polygon3 *poly = new Polygon3(mPolys[i]->mVertices); |
---|
| 2452 | |
---|
| 2453 | Polygon3 *frontPoly = new Polygon3(); |
---|
| 2454 | Polygon3 *backPoly = new Polygon3(); |
---|
[390] | 2455 | |
---|
[448] | 2456 | poly->Split(splitPlane, |
---|
| 2457 | *frontPoly, |
---|
| 2458 | *backPoly, |
---|
| 2459 | epsilon); |
---|
[390] | 2460 | |
---|
[396] | 2461 | DEL_PTR(poly); |
---|
[390] | 2462 | |
---|
[448] | 2463 | if (frontPoly->Valid(epsilon)) |
---|
[396] | 2464 | front.mPolys.push_back(frontPoly); |
---|
[441] | 2465 | else |
---|
| 2466 | DEL_PTR(frontPoly); |
---|
| 2467 | |
---|
[448] | 2468 | if (backPoly->Valid(epsilon)) |
---|
[396] | 2469 | back.mPolys.push_back(backPoly); |
---|
[441] | 2470 | else |
---|
| 2471 | DEL_PTR(backPoly); |
---|
[390] | 2472 | } |
---|
[396] | 2473 | |
---|
[390] | 2474 | break; |
---|
| 2475 | case Polygon3::BACK_SIDE: |
---|
[396] | 2476 | back.mPolys.push_back(new Polygon3(mPolys[i]->mVertices)); |
---|
[390] | 2477 | break; |
---|
| 2478 | case Polygon3::FRONT_SIDE: |
---|
[396] | 2479 | front.mPolys.push_back(new Polygon3(mPolys[i]->mVertices)); |
---|
[390] | 2480 | break; |
---|
| 2481 | case Polygon3::COINCIDENT: |
---|
[396] | 2482 | //front.mPolys.push_back(CreateReversePolygon(mPolys[i])); |
---|
[404] | 2483 | back.mPolys.push_back(new Polygon3(mPolys[i]->mVertices)); |
---|
[390] | 2484 | break; |
---|
| 2485 | default: |
---|
| 2486 | break; |
---|
| 2487 | } |
---|
| 2488 | } |
---|
[396] | 2489 | |
---|
[448] | 2490 | //-- finally add the new polygon to the child node geometries |
---|
[396] | 2491 | if (planePoly) |
---|
[390] | 2492 | { |
---|
[396] | 2493 | // add polygon with normal pointing into positive half space to back cell |
---|
| 2494 | back.mPolys.push_back(planePoly); |
---|
| 2495 | // add polygon with reverse orientation to front cell |
---|
| 2496 | front.mPolys.push_back(planePoly->CreateReversePolygon()); |
---|
| 2497 | } |
---|
| 2498 | |
---|
[400] | 2499 | //Debug << "returning new geometry " << mPolys.size() << " f: " << front.mPolys.size() << " b: " << back.mPolys.size() << endl; |
---|
| 2500 | //Debug << "old area " << GetArea() << " f: " << front.GetArea() << " b: " << back.GetArea() << endl; |
---|
[396] | 2501 | } |
---|
| 2502 | |
---|
| 2503 | Polygon3 *BspNodeGeometry::SplitPolygon(Polygon3 *planePoly, |
---|
[448] | 2504 | const float epsilon) const |
---|
[396] | 2505 | { |
---|
[448] | 2506 | if (!planePoly->Valid(epsilon)) |
---|
| 2507 | DEL_PTR(planePoly); |
---|
| 2508 | |
---|
[396] | 2509 | // polygon is split by all other planes |
---|
| 2510 | for (int i = 0; (i < (int)mPolys.size()) && planePoly; ++ i) |
---|
| 2511 | { |
---|
| 2512 | Plane3 plane = mPolys[i]->GetSupportingPlane(); |
---|
| 2513 | |
---|
[448] | 2514 | /// don't use epsilon here to get exact split planes |
---|
[396] | 2515 | const int cf = |
---|
[448] | 2516 | planePoly->ClassifyPlane(plane, Limits::Small); |
---|
[390] | 2517 | |
---|
| 2518 | // split new polygon with all previous planes |
---|
| 2519 | switch (cf) |
---|
| 2520 | { |
---|
| 2521 | case Polygon3::SPLIT: |
---|
| 2522 | { |
---|
| 2523 | Polygon3 *frontPoly = new Polygon3(); |
---|
| 2524 | Polygon3 *backPoly = new Polygon3(); |
---|
[392] | 2525 | |
---|
[448] | 2526 | planePoly->Split(plane, |
---|
| 2527 | *frontPoly, |
---|
| 2528 | *backPoly, |
---|
| 2529 | epsilon); |
---|
[441] | 2530 | |
---|
| 2531 | // don't need anymore |
---|
[396] | 2532 | DEL_PTR(planePoly); |
---|
[441] | 2533 | DEL_PTR(frontPoly); |
---|
[390] | 2534 | |
---|
[441] | 2535 | // back polygon is belonging to geometry |
---|
[448] | 2536 | if (backPoly->Valid(epsilon)) |
---|
[396] | 2537 | planePoly = backPoly; |
---|
[390] | 2538 | else |
---|
[396] | 2539 | DEL_PTR(backPoly); |
---|
[390] | 2540 | } |
---|
| 2541 | break; |
---|
| 2542 | case Polygon3::FRONT_SIDE: |
---|
[396] | 2543 | DEL_PTR(planePoly); |
---|
| 2544 | break; |
---|
| 2545 | // polygon is taken as it is |
---|
| 2546 | case Polygon3::BACK_SIDE: |
---|
[390] | 2547 | case Polygon3::COINCIDENT: |
---|
| 2548 | default: |
---|
| 2549 | break; |
---|
| 2550 | } |
---|
| 2551 | } |
---|
| 2552 | |
---|
[396] | 2553 | return planePoly; |
---|
[450] | 2554 | } |
---|