Changeset 653 for GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
- Timestamp:
- 02/18/06 22:06:33 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
r652 r653 353 353 } 354 354 355 Debug << "maximal pvs (i.e., pvs still considered as valid) : " << mViewCellsManager->GetMaxPvsSize() << endl; 355 Debug << "maximal pvs (i.e., pvs still considered as valid) : " 356 << mViewCellsManager->GetMaxPvsSize() << endl; 356 357 357 358 //-- store rays … … 521 522 522 523 524 525 void VspBspTree::ConstructWithSplitQueueQueue(const PolygonContainer &polys, 526 RayInfoContainer *rays) 527 { 528 VspBspSplitQueue tQueue; 529 530 mRoot = new BspLeaf(); 531 532 // constrruct root node geometry 533 BspNodeGeometry *geom = new BspNodeGeometry(); 534 ConstructGeometry(mRoot, *geom); 535 536 const float prop = mUseAreaForPvs ? geom->GetArea() : geom->GetVolume(); 537 538 VspBspTraversalData tData(mRoot, 539 new PolygonContainer(polys), 540 0, 541 rays, 542 ComputePvsSize(*rays), 543 prop, 544 geom); 545 546 VspBspSplitCandidate splitCandidate; 547 548 549 EvalSplitCandidate(tData, splitCandidate); 550 551 tQueue.push(splitCandidate); 552 553 554 mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume(); 555 mTotalPvsSize = tData.mPvs; 556 557 mSubdivisionStats 558 << "#ViewCells\n1\n" << endl 559 << "#RenderCostDecrease\n0\n" << endl 560 << "#TotalRenderCost\n" << mTotalCost << endl 561 << "#AvgRenderCost\n" << mTotalPvsSize << endl; 562 563 Debug << "total cost: " << mTotalCost << endl; 564 565 566 mBspStats.Start(); 567 cout << "Contructing vsp bsp tree ... \n"; 568 569 long startTime = GetTime(); 570 int nLeaves = 0; 571 int nViewCells = 0; 572 573 // used for intermediate time measurements and progress 574 long interTime = GetTime(); 575 576 mOutOfMemory = false; 577 578 mCreatedViewCells = 0; 579 580 while (!tQueue.empty()) 581 { 582 splitCandidate = tQueue.top(); 583 tQueue.pop(); 584 585 if (0 && !mOutOfMemory) 586 { 587 float mem = GetMemUsage(); 588 589 if (mem > mMaxMemory) 590 { 591 mOutOfMemory = true; 592 Debug << "memory limit reached: " << mem << endl; 593 } 594 } 595 596 // subdivide leaf node 597 BspNode *r = Subdivide(tQueue, splitCandidate); 598 599 if (r == mRoot) 600 Debug << "VSP BSP tree construction time spent at root: " 601 << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 602 603 if (mBspStats.Leaves() == nLeaves) 604 { 605 nLeaves += 500; 606 607 cout << "leaves=" << mBspStats.Leaves() << endl; 608 Debug << "needed " 609 << TimeDiff(interTime, GetTime())*1e-3 610 << " secs to create 500 view cells" << endl; 611 interTime = GetTime(); 612 } 613 614 if (mCreatedViewCells == nViewCells) 615 { 616 nViewCells += 500; 617 618 cout << "generated " << mCreatedViewCells << " viewcells" << endl; 619 } 620 } 621 622 Debug << "Used Memory: " << GetMemUsage() << " MB" << endl << endl; 623 cout << "finished\n"; 624 625 mBspStats.Stop(); 626 } 627 628 523 629 bool VspBspTree::TerminationCriteriaMet(const VspBspTraversalData &data) const 524 630 { … … 555 661 // create new interior node and two leaf nodes 556 662 // or return leaf as it is (if maxCostRatio missed) 557 newNode = SubdivideNode(tData, tFrontData, tBackData, coincident); 558 559 if (!newNode->IsLeaf()) //-- continue subdivision 560 { 663 664 int splitAxis; 665 bool splitFurther = true; 666 int maxCostMisses = tData.mMaxCostMisses; 667 668 Plane3 splitPlane; 669 BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); 670 671 if (!SelectPlane(splitPlane, leaf, tData, tFrontData, tBackData, splitAxis)) 672 { 673 ++ maxCostMisses; 674 675 if (maxCostMisses > mTermMissTolerance) 676 { 677 // terminate branch because of max cost 678 ++ mBspStats.maxCostNodes; 679 splitFurther = false; 680 } 681 } 682 683 if (splitFurther) //-- continue subdivision 684 { 685 newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData, coincident); 686 687 if (splitAxis < 3) 688 ++ mBspStats.splits[splitAxis]; 689 else 690 ++ mBspStats.polySplits; 691 692 tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && splitAxis < 3); 693 // how often was max cost ratio missed in this branch? 694 tFrontData.mMaxCostMisses = maxCostMisses; 695 tBackData.mMaxCostMisses = maxCostMisses; 696 561 697 if (1) 562 698 { … … 643 779 644 780 781 BspNode *VspBspTree::Subdivide(VspBspSplitQueue &tQueue, 782 VspBspSplitCandidate &splitCandidate) 783 { 784 VspBspTraversalData &tData = splitCandidate.mParentData; 785 786 BspNode *newNode = tData.mNode; 787 788 if (!mOutOfMemory && !TerminationCriteriaMet(tData)) 789 { 790 PolygonContainer coincident; 791 792 VspBspTraversalData tFrontData; 793 VspBspTraversalData tBackData; 794 795 #if OCTREE_HACK 796 //Debug << "new axis:" << (tData.mAxis + 1) % 3 << endl; 797 tFrontData.mAxis = (tData.mAxis + 1) % 3; 798 tBackData.mAxis = (tData.mAxis + 1) % 3; 799 #endif 800 //-- continue subdivision 801 // create new interior node and two leaf node 802 const Plane3 splitPlane = splitCandidate.mSplitPlane; 803 804 newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData, coincident); 805 806 #if 0 // TODO 807 if (splitAxis < 3) 808 ++ mBspStats.splits[splitAxis]; 809 else 810 ++ mBspStats.polySplits; 811 812 tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && splitAxis < 3); 813 // how often was max cost ratio missed in this branch? 814 tFrontData.mMaxCostMisses = maxCostMisses; 815 tBackData.mMaxCostMisses = maxCostMisses; 816 #endif 817 if (1) 818 { 819 float cFront = (float)tFrontData.mPvs * tFrontData.mProbability; 820 float cBack = (float)tBackData.mPvs * tBackData.mProbability; 821 float cData = (float)tData.mPvs * tData.mProbability;; 822 823 float costDecr = 824 (cFront + cBack - cData) / mBox.GetVolume(); 825 826 mTotalCost += costDecr; 827 mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs; 828 829 mSubdivisionStats 830 << "#ViewCells\n" << mBspStats.Leaves() << endl 831 << "#RenderCostDecrease\n" << -costDecr << endl 832 << "#TotalRenderCost\n" << mTotalCost << endl 833 << "#AvgRenderCost\n" << (float)mTotalPvsSize / (float)mBspStats.Leaves() << endl; 834 } 835 836 //-- push the new split candidates on the stack 837 VspBspSplitCandidate frontCandidate; 838 VspBspSplitCandidate backCandidate; 839 840 EvalSplitCandidate(tData, frontCandidate); 841 EvalSplitCandidate(tData, backCandidate); 842 843 tQueue.push(frontCandidate); 844 tQueue.push(backCandidate); 845 846 // delete old leaf node 847 DEL_PTR(tData.mNode); 848 } 849 850 //-- terminate traversal and create new view cell 851 if (newNode->IsLeaf()) 852 { 853 BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode); 854 BspViewCell *viewCell = new BspViewCell(); 855 856 leaf->SetViewCell(viewCell); 857 858 //-- update pvs 859 int conSamp = 0; 860 float sampCon = 0.0f; 861 AddToPvs(leaf, *tData.mRays, sampCon, conSamp); 862 863 mBspStats.contributingSamples += conSamp; 864 mBspStats.sampleContributions +=(int) sampCon; 865 866 //-- store additional info 867 if (mStoreRays) 868 { 869 RayInfoContainer::const_iterator it, it_end = tData.mRays->end(); 870 for (it = tData.mRays->begin(); it != it_end; ++ it) 871 { 872 (*it).mRay->Ref(); 873 leaf->mVssRays.push_back((*it).mRay); 874 } 875 } 876 877 // should I check here? 878 viewCell->mLeaf = leaf; 879 880 if (mUseAreaForPvs) 881 viewCell->SetArea(tData.mProbability); 882 else 883 viewCell->SetVolume(tData.mProbability); 884 885 leaf->mProbability = tData.mProbability; 886 887 EvaluateLeafStats(tData); 888 } 889 890 //-- cleanup 891 tData.Clear(); 892 893 return newNode; 894 } 895 896 645 897 void VspBspTree::EvalSplitCandidate(VspBspTraversalData &tData, 646 898 VspBspSplitCandidate &splitData) … … 663 915 664 916 665 BspNode *VspBspTree::SubdivideNode(VspBspTraversalData &tData, 666 VspBspTraversalData &frontData, 667 VspBspTraversalData &backData, 668 PolygonContainer &coincident) 917 BspInterior *VspBspTree::SubdivideNode(const Plane3 &splitPlane, 918 VspBspTraversalData &tData, 919 VspBspTraversalData &frontData, 920 VspBspTraversalData &backData, 921 PolygonContainer &coincident) 669 922 { 670 923 BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); 671 924 672 // select subdivision plane673 Plane3 splitPlane;674 675 int splitAxis;676 677 const bool success =678 SelectPlane(splitPlane, leaf, tData, frontData, backData, splitAxis);679 680 681 int maxCostMisses = tData.mMaxCostMisses;682 683 if (!success)684 {685 ++ maxCostMisses;686 687 if (maxCostMisses > mTermMissTolerance)688 {689 // terminate branch because of max cost690 ++ mBspStats.maxCostNodes;691 return leaf;692 }693 }694 695 925 //-- the front and back traversal data is filled with the new values 696 926 frontData.mDepth = tData.mDepth + 1; … … 702 932 backData.mRays = new RayInfoContainer(); 703 933 704 // subdivide rays 934 935 //-- subdivide rays 705 936 SplitRays(splitPlane, 706 937 *tData.mRays, … … 708 939 *backData.mRays); 709 940 710 711 // how often was max cost ratio missed in this branch?712 frontData.mMaxCostMisses = maxCostMisses;713 backData.mMaxCostMisses = maxCostMisses;714 941 715 942 // compute pvs … … 753 980 754 981 755 if (splitAxis < 3) 756 ++ mBspStats.splits[splitAxis]; 757 else 758 ++ mBspStats.polySplits; 982 /////////////////////////////////////// 983 // subdivide further 759 984 760 985 mBspStats.nodes += 2; 761 986 762 763 764 /////////////////////////////////////// 765 766 //-- subdivide further 987 767 988 BspInterior *interior = new BspInterior(splitPlane); 768 989 … … 1221 1442 } 1222 1443 1223 // cost ratio miss1224 if (mUsePolygonSplitIfAvailable && !data.mPolygons->empty())1225 {1226 frontData.mIsKdNode = backData.mIsKdNode = false;1227 if (lowestCost > mTermMaxCostRatio)1228 return false;1229 1230 return true;1231 }1232 1233 1444 //-- evaluate axis aligned splits 1234 1445 int axis; … … 1236 1447 float pFront, pBack; 1237 1448 1238 candidateCost = SelectAxisAlignedPlane(plane, 1239 data, 1240 axis, 1241 &fGeom, 1242 &bGeom, 1243 pFront, 1244 pBack, 1245 data.mIsKdNode); 1449 candidateCost = 99999999.0f; 1450 1451 // option: axis aligned split only if no polygon available 1452 if (!mUsePolygonSplitIfAvailable || data.mPolygons->empty()) 1453 { 1454 candidateCost = SelectAxisAlignedPlane(plane, 1455 data, 1456 axis, 1457 &fGeom, 1458 &bGeom, 1459 pFront, 1460 pBack, 1461 data.mIsKdNode); 1462 } 1246 1463 1247 1464 splitAxis = 3; … … 1252 1469 lowestCost = candidateCost; 1253 1470 splitAxis = axis; 1471 1254 1472 // assign already computed values 1255 1473 // we can do this because we always save the 1256 1474 // computed values from the axis aligned splits 1475 1257 1476 if (fGeom && bGeom) 1258 1477 { … … 1269 1488 DEL_PTR(bGeom); 1270 1489 } 1271 1272 frontData.mIsKdNode = backData.mIsKdNode = 1273 (data.mIsKdNode && splitAxis < 3); 1490 1274 1491 1275 1492 #ifdef _DEBUG … … 1277 1494 #endif 1278 1495 1279 // cost ratio miss1280 1496 if (lowestCost > mTermMaxCostRatio) 1281 1497 {
Note: See TracChangeset
for help on using the changeset viewer.