Changeset 1545 for GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
- Timestamp:
- 09/29/06 22:42:25 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
r1418 r1545 17 17 namespace GtpVisibilityPreprocessor { 18 18 19 19 ////////////// 20 20 //-- static members 21 21 … … 168 168 mRoot(NULL), 169 169 mUseAreaForPvs(false), 170 mGenerateViewCells(true), 171 mTimeStamp(1) 170 mUsePredefinedViewCells(false), 171 mTimeStamp(1), 172 mViewCellsTree(NULL), 173 mOutOfBoundsCellPartOfTree(false) 172 174 { 173 175 Randomize(); // initialise random generator for heuristics 174 175 mOutOfBoundsCell = GetOrCreateOutOfBoundsCell(); 176 176 mOutOfBoundsCell = new BspViewCell(); 177 178 ///////// 177 179 //-- termination criteria for autopartition 180 178 181 Environment::GetSingleton()->GetIntValue("BspTree.Termination.maxDepth", mTermMaxDepth); 179 182 Environment::GetSingleton()->GetIntValue("BspTree.Termination.minPvs", mTermMinPvs); … … 184 187 Environment::GetSingleton()->GetFloatValue("BspTree.Termination.minAccRayLenght", mTermMinAccRayLength); 185 188 189 190 ///////// 186 191 //-- factors for bsp tree split plane heuristics 192 187 193 Environment::GetSingleton()->GetFloatValue("BspTree.Factor.verticalSplits", mVerticalSplitsFactor); 188 194 Environment::GetSingleton()->GetFloatValue("BspTree.Factor.largestPolyArea", mLargestPolyAreaFactor); … … 196 202 Environment::GetSingleton()->GetFloatValue("BspTree.Termination.ct_div_ci", mCtDivCi); 197 203 204 205 /////////// 198 206 //-- termination criteria for axis aligned split 207 199 208 Environment::GetSingleton()->GetFloatValue("BspTree.Termination.AxisAligned.ct_div_ci", mAxisAlignedCtDivCi); 200 209 Environment::GetSingleton()->GetFloatValue("BspTree.Termination.maxCostRatio", mMaxCostRatio); … … 205 214 Environment::GetSingleton()->GetIntValue("BspTree.Termination.AxisAligned.minObjects", 206 215 mTermMinObjectsForAxisAligned); 216 217 218 ////////////// 207 219 //-- partition criteria 220 208 221 Environment::GetSingleton()->GetIntValue("BspTree.maxPolyCandidates", mMaxPolyCandidates); 209 222 Environment::GetSingleton()->GetIntValue("BspTree.maxRayCandidates", mMaxRayCandidates); … … 219 232 mSubdivisionStats.open(subdivisionStatsLog); 220 233 234 /////////////////////7 235 236 Debug << "BSP options: " << endl; 221 237 Debug << "BSP max depth: " << mTermMaxDepth << endl; 222 238 Debug << "BSP min PVS: " << mTermMinPvs << endl; … … 401 417 { 402 418 DEL_PTR(mRoot); 403 404 // TODO must be deleted if used!! 405 //if (mGenerateViewCells) DEL_PTR(mOutOfBoundsCell); 406 } 407 408 BspViewCell *BspTree::GetOutOfBoundsCell() 409 { 410 return mOutOfBoundsCell; 419 420 if (!mOutOfBoundsCellPartOfTree) 421 { 422 // out of bounds cell not part of tree => 423 // delete manually 424 DEL_PTR(mOutOfBoundsCell); 425 } 411 426 } 412 427 … … 418 433 419 434 420 BspViewCell *BspTree::GetOrCreateOutOfBoundsCell() 421 { 422 if (!mOutOfBoundsCell) 423 { 424 mOutOfBoundsCell = new BspViewCell(); 425 mOutOfBoundsCell->SetId(-1); 426 mOutOfBoundsCell->SetValid(false); 427 } 428 429 return mOutOfBoundsCell; 435 void BspTree::SetViewCellsTree(ViewCellsTree *viewCellsTree) 436 { 437 mViewCellsTree = viewCellsTree; 430 438 } 431 439 … … 433 441 void BspTree::InsertViewCell(ViewCellLeaf *viewCell) 434 442 { 443 // don't generate new view cell, insert this view cell 444 mUsePredefinedViewCells = true; 435 445 PolygonContainer *polys = new PolygonContainer(); 436 446 437 // don't generate new view cell, insert this one438 mGenerateViewCells = false;439 447 // extract polygons that guide the split process 440 448 mStat.polys += AddMeshToPolygons(viewCell->GetMesh(), *polys, viewCell); 441 mB ox.Include(viewCell->GetBox()); // add to BSP aabb449 mBbox.Include(viewCell->GetBox()); // add to BSP aabb 442 450 443 451 InsertPolygons(polys); … … 459 467 new BoundedRayContainer(), 460 468 0, 461 mUseAreaForPvs ? mB ox.SurfaceArea() : mBox.GetVolume(),469 mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(), 462 470 new BspNodeGeometry())); 463 471 … … 472 480 BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode); 473 481 482 /////////////////// 474 483 //-- filter view cell polygons down the tree until a leaf is reached 475 484 if (!tData.mPolygons->empty()) … … 489 498 490 499 // extract view cells associated with the split polygons 491 ViewCellLeaf *frontViewCell = GetOrCreateOutOfBoundsCell();492 ViewCellLeaf *backViewCell = GetOrCreateOutOfBoundsCell();500 ViewCellLeaf *frontViewCell = mOutOfBoundsCell; 501 ViewCellLeaf *backViewCell = mOutOfBoundsCell; 493 502 494 503 BspTraversalData frontData(interior->GetFront(), … … 498 507 tData.mRays, 499 508 tData.mPvs, 500 mUseAreaForPvs ? mB ox.SurfaceArea() : mBox.GetVolume(),509 mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(), 501 510 new BspNodeGeometry()); 502 511 … … 507 516 tData.mRays, 508 517 tData.mPvs, 509 mUseAreaForPvs ? mB ox.SurfaceArea() : mBox.GetVolume(),518 mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(), 510 519 new BspNodeGeometry()); 511 520 512 if ( !mGenerateViewCells)521 if (mUsePredefinedViewCells) 513 522 { 514 523 ExtractViewCells(frontData, … … 558 567 } 559 568 else 569 { 560 570 DEL_PTR(poly); 571 } 561 572 } 562 573 return (int)mesh->mFaces.size(); … … 572 583 573 584 int polysSize = 0; 574 585 cout << "here55 " << mBbox << endl; 586 587 cout << "here99"; 575 588 for (int i = 0; i < limit; ++ i) 576 { 577 if (viewCells[i]->GetMesh()) // copy the mesh data to polygons 578 { 579 mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb 580 polysSize += AddMeshToPolygons(viewCells[i]->GetMesh(), polys, viewCells[i]); 581 } 582 } 583 589 {cout << "q"; 590 Mesh *mesh = viewCells[i]->GetMesh(); 591 if (mesh) 592 {cout << "w"; 593 // copy the mesh into polygons and add to BSP tree aabb 594 mBbox.Include(viewCells[i]->GetBox()); 595 polysSize += AddMeshToPolygons(mesh, polys, viewCells[i]); 596 } 597 } 598 cout << "here155 " << mBbox << endl; 584 599 return polysSize; 585 600 } … … 593 608 int limit = (maxObjects > 0) ? 594 609 Min((int)objects.size(), maxObjects) : (int)objects.size(); 595 610 cout << "here88"; 596 611 for (int i = 0; i < limit; ++i) 597 { 612 {cout << "e"; 598 613 Intersectable *object = objects[i]; 599 600 614 Mesh *mesh = NULL; 601 615 … … 616 630 mesh = new Mesh(); 617 631 mi->GetTransformedMesh(*mesh); 618 619 632 break; 620 633 } … … 624 637 } 625 638 626 if (mesh) // copy the mesh data to polygons 627 { 628 if (addToBbox) 629 mBox.Include(object->GetBox()); // add to BSP tree aabb 630 631 AddMeshToPolygons(mesh, polys, mOutOfBoundsCell); 632 633 // cleanup 634 if (object->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE) 635 DEL_PTR(mesh); 639 if (!mesh) continue; 640 // copy the mesh data to polygons 641 if (addToBbox) 642 {cout << "b"; 643 mBbox.Include(object->GetBox()); // add to BSP tree aabb 644 } 645 646 AddMeshToPolygons(mesh, polys, mOutOfBoundsCell); 647 648 // cleanup 649 if (object->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE) 650 { 651 DEL_PTR(mesh); 636 652 } 637 653 } … … 640 656 } 641 657 642 658 643 659 void BspTree::Construct(const ViewCellContainer &viewCells) 644 660 { 661 cout << "here5500000000" << endl; 662 // construct hierarchy over the given view cells 663 mUsePredefinedViewCells = true; 664 645 665 mStat.nodes = 1; 646 mB ox.Initialize();// initialise bsp tree bounding box666 mBbox.Initialize(); // initialise bsp tree bounding box 647 667 648 668 // copy view cell meshes into one big polygon soup … … 650 670 mStat.polys = AddToPolygonSoup(viewCells, *polys); 651 671 652 // view cells are given 653 mGenerateViewCells = false; 672 Exporter *expo = Exporter::GetExporter("dummy2.wrl"); 673 expo->ExportPolygons(*polys); 674 delete expo; 654 675 // construct tree from the view cell polygons 655 676 Construct(polys, new BoundedRayContainer()); … … 659 680 void BspTree::Construct(const ObjectContainer &objects) 660 681 { 682 // generate new view cells for this type 683 mUsePredefinedViewCells = false; 684 661 685 mStat.nodes = 1; 662 mB ox.Initialize(); // initialise bsp tree bounding box686 mBbox.Initialize(); // initialise bsp tree bounding box 663 687 664 688 PolygonContainer *polys = new PolygonContainer(); 665 689 666 mGenerateViewCells = true;667 690 // copy mesh instance polygons into one big polygon soup 668 691 mStat.polys = AddToPolygonSoup(objects, *polys); … … 677 700 // preprocess: throw out polygons coincident to the view space box (not needed) 678 701 PolygonContainer boxPolys; 679 mB ox.ExtractPolys(boxPolys);702 mBbox.ExtractPolys(boxPolys); 680 703 vector<Plane3> boxPlanes; 681 704 … … 722 745 723 746 724 725 747 void BspTree::Construct(const RayContainer &sampleRays, 726 748 AxisAlignedBox3 *forcedBoundingBox) 727 749 { 750 // generate new view cells for this contruction type 751 mUsePredefinedViewCells = false; 752 728 753 mStat.nodes = 1; 729 mBox.Initialize(); // initialise BSP tree bounding box 730 754 731 755 if (forcedBoundingBox) 732 mBox = *forcedBoundingBox; 756 { 757 mBbox = *forcedBoundingBox; 758 } 759 else 760 { 761 mBbox.Initialize(); // initialise BSP tree bounding box 762 } 733 763 734 764 PolygonContainer *polys = new PolygonContainer(); … … 737 767 RayContainer::const_iterator rit, rit_end = sampleRays.end(); 738 768 739 // generate view cells740 mGenerateViewCells = true;741 742 769 long startTime = GetTime(); 743 744 770 Debug << "**** Extracting polygons from rays ****\n"; 745 771 746 772 std::map<Face *, Polygon3 *> facePolyMap; 747 773 748 //-- extract polygons intersected by the rays749 774 for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 750 775 { 776 ////////////// 777 //-- extract polygons intersected by the rays 751 778 Ray *ray = *rit; 752 779 … … 803 830 // compute bounding box 804 831 if (!forcedBoundingBox) 805 mBox.Include(*polys); 806 832 { 833 mBbox.Include(*polys); 834 } 835 836 //////////// 807 837 //-- store rays 808 838 for (rit = sampleRays.begin(); rit != rit_end; ++ rit) … … 812 842 813 843 float minT, maxT; 814 if (mB ox.GetRaySegment(*ray, minT, maxT))844 if (mBbox.GetRaySegment(*ray, minT, maxT)) 815 845 rays->push_back(new BoundedRay(ray, minT, maxT)); 816 846 } … … 833 863 AxisAlignedBox3 *forcedBoundingBox) 834 864 { 865 // generate new view cells for this construction type 866 mUsePredefinedViewCells = false; 867 835 868 mStat.nodes = 1; 836 mB ox.Initialize(); // initialise BSP tree bounding box869 mBbox.Initialize(); // initialise BSP tree bounding box 837 870 838 871 if (forcedBoundingBox) 839 mBox = *forcedBoundingBox; 872 { 873 mBbox = *forcedBoundingBox; 874 } 840 875 841 876 BoundedRayContainer *rays = new BoundedRayContainer(); 842 877 PolygonContainer *polys = new PolygonContainer(); 843 878 844 mGenerateViewCells = true;845 846 879 // copy mesh instance polygons into one big polygon soup 847 880 mStat.polys = AddToPolygonSoup(objects, *polys, 0, !forcedBoundingBox); 848 881 849 882 /////// 883 //-- store rays 850 884 RayContainer::const_iterator rit, rit_end = sampleRays.end(); 851 852 //-- store rays 885 853 886 for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 854 887 { … … 857 890 858 891 float minT, maxT; 859 if (mB ox.GetRaySegment(*ray, minT, maxT))892 if (mBbox.GetRaySegment(*ray, minT, maxT)) 860 893 rays->push_back(new BoundedRay(ray, minT, maxT)); 861 894 } … … 870 903 { 871 904 BspTraversalStack tStack; 872 873 905 mRoot = new BspLeaf(); 874 906 … … 880 912 polys, 881 913 0, 882 GetOrCreateOutOfBoundsCell(),914 mOutOfBoundsCell, 883 915 rays, 884 916 ComputePvsSize(*rays), … … 886 918 geom); 887 919 888 mTotalCost = tData.mPvs * tData.mProbability / mB ox.GetVolume();920 mTotalCost = tData.mPvs * tData.mProbability / mBbox.GetVolume(); 889 921 mTotalPvsSize = tData.mPvs; 890 922 … … 950 982 BspNode *BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData) 951 983 { 952 //-- terminate traversal 984 //////// 985 //-- terminate traversal 986 953 987 if (TerminationCriteriaMet(tData)) 954 988 { … … 957 991 BspViewCell *viewCell; 958 992 959 // generate new view cell for each leaf 960 if (mGenerateViewCells) 961 { 993 if (!mUsePredefinedViewCells) 994 { // generate new view cell for each leaf 962 995 viewCell = new BspViewCell(); 963 996 } 964 997 else 965 998 { 966 // add view cell to leaf999 // add predefined view cell to leaf 967 1000 viewCell = dynamic_cast<BspViewCell *>(tData.mViewCell); 1001 1002 /// out of bounds cell can be handled as any other cell 1003 if (viewCell == mOutOfBoundsCell) 1004 mOutOfBoundsCellPartOfTree = true; 968 1005 } 969 1006 … … 979 1016 viewCell->SetVolume(probability); 980 1017 981 //-- add pvs 1018 1019 /////////// 1020 //-- add pvs contribution of rays 1021 982 1022 if (viewCell != mOutOfBoundsCell) 983 1023 { … … 989 1029 } 990 1030 991 if (1) 992 EvaluateLeafStats(tData); 993 1031 if (1)EvaluateLeafStats(tData); 1032 1033 1034 //////// 994 1035 //-- clean up 995 1036 996 1037 // discard polygons 997 1038 CLEAR_CONTAINER(*tData.mPolygons); … … 1006 1047 } 1007 1048 1049 /////////// 1008 1050 //-- continue subdivision 1051 1009 1052 PolygonContainer coincident; 1010 1053 … … 1019 1062 SubdivideNode(tData, tFrontData, tBackData, coincident); 1020 1063 1021 1022 1064 if (1) 1023 1065 { … … 1028 1070 float cBack = (float)tBackData.mPvs * tBackData.mProbability; 1029 1071 1030 float costDecr = (cFront + cBack - cData) / mB ox.GetVolume();1072 float costDecr = (cFront + cBack - cData) / mBbox.GetVolume(); 1031 1073 1032 1074 mTotalCost += costDecr; … … 1040 1082 } 1041 1083 1042 // extract view cells from coincident polygons according to plane normal 1043 // only if front or back polygons are empty 1044 if (!mGenerateViewCells) 1084 // extract view cells from coincident polygons 1085 // with respect to the orientation of their normal 1086 // note: if front or back polygons are empty, 1087 // we get the valid in - out classification for the view cell 1088 1089 if (mUsePredefinedViewCells) 1045 1090 { 1046 1091 ExtractViewCells(tFrontData, … … 1057 1102 tStack.push(tBackData); 1058 1103 1059 // cleanup 1104 //////// 1105 //-- cleanup 1106 1060 1107 DEL_PTR(tData.mNode); 1061 1062 1108 DEL_PTR(tData.mPolygons); 1063 1109 DEL_PTR(tData.mRays); … … 1080 1126 coincident.begin(), it_end = coincident.end(); 1081 1127 1128 ////////// 1082 1129 //-- find first view cells in front and back leafs 1130 1083 1131 for (; !(foundFront && foundBack) && (it != it_end); ++ it) 1084 1132 { … … 1108 1156 // select subdivision plane 1109 1157 BspInterior *interior = new BspInterior(SelectPlane(leaf, tData)); 1110 1111 1158 1112 1159 #ifdef _DEBUG … … 1135 1182 *backData.mGeometry, 1136 1183 interior->mPlane, 1137 mB ox,1184 mBbox, 1138 1185 //0.000000000001); 1139 1186 mEpsilon); … … 1274 1321 const float ratio = newCost / oldCost; 1275 1322 1276 1277 #if 0 1323 #ifdef _DEBUG 1278 1324 Debug << "====================" << endl; 1279 1325 Debug << "costRatio=" << ratio << " pos=" << position<<" t=" << (position - minBox)/(maxBox - minBox) … … 1430 1476 return Plane3(pt[0], pt[1], pt[2]); 1431 1477 } 1478 1432 1479 1433 1480 Plane3 BspTree::ChooseCandidatePlane3(const BoundedRayContainer &rays) const … … 1697 1744 geomBack, 1698 1745 candidatePlane, 1699 mB ox,1746 mBbox, 1700 1747 mEpsilon); 1701 1748 … … 1856 1903 if (mSplitPlaneStrategy & VERTICAL_AXIS) 1857 1904 { 1858 Vector3 tinyAxis(0,0,0); tinyAxis[mB ox.Size().TinyAxis()] = 1.0f;1905 Vector3 tinyAxis(0,0,0); tinyAxis[mBbox.Size().TinyAxis()] = 1.0f; 1859 1906 // we put a penalty on the dot product between the "tiny" vertical axis 1860 1907 // and the split plane axis … … 1915 1962 AxisAlignedBox3 BspTree::GetBoundingBox() const 1916 1963 { 1917 return mB ox;1964 return mBbox; 1918 1965 } 1919 1966 … … 1972 2019 float maxt, mint; 1973 2020 1974 if (!mB ox.GetRaySegment(ray, mint, maxt))2021 if (!mBbox.GetRaySegment(ray, mint, maxt)) 1975 2022 return 0; 1976 2023 … … 2194 2241 } 2195 2242 } 2243 2244 // also add out of bounds cell 2245 if (0 && !mOutOfBoundsCell->Mailed()) 2246 { 2247 mOutOfBoundsCell->Mail(); 2248 viewCells.push_back(mOutOfBoundsCell); 2249 } 2250 2251 cout << "here555 " << viewCells.size() << endl; 2252 2196 2253 } 2197 2254 … … 2247 2304 case Ray::COINCIDENT: // TODO: should really discard ray? 2248 2305 frontRays.push_back(bRay); 2249 //DEL_PTR(bRay);2250 2306 break; 2251 2307 case Ray::BACK: … … 2322 2378 BspNodeGeometry &vcGeom) const 2323 2379 { 2380 // if false, cannot construct geometry for interior leaf 2381 if (!mViewCellsTree) 2382 return; 2383 2324 2384 ViewCellContainer leaves; 2325 2385 mViewCellsTree->CollectLeaves(vc, leaves); … … 2364 2424 2365 2425 for (int j = 0; j < 4; ++ j) 2366 vertices.push_back(mB ox.GetFace(i).mVertices[j]);2426 vertices.push_back(mBbox.GetFace(i).mVertices[j]); 2367 2427 2368 2428 Polygon3 *poly = new Polygon3(vertices); … … 2533 2593 *bGeom, 2534 2594 interior->GetPlane(), 2535 mB ox,2595 mBbox, 2536 2596 //0.0000001f); 2537 2597 mEpsilon); … … 2807 2867 Debug << "leaves in queue: " << numLeaves << endl; 2808 2868 2809 2869 #if 0 2870 ////////// 2810 2871 //-- collect the leaves which haven't been found by ray casting 2811 #if 02812 2872 cout << "finding additional merge candidates using geometry" << endl; 2813 2873 vector<BspLeaf *> leaves; … … 2822 2882 2823 2883 2824 2825 2884 /***************************************************************/ 2826 2885 /* BspNodeGeometry Implementation */ … … 3050 3109 bool BspNodeGeometry::Valid() const 3051 3110 { 3111 // geometry is degenerated 3052 3112 if (mPolys.size() < 4) 3053 3113 return false; … … 3294 3354 int id = -1; 3295 3355 if (leaf->GetViewCell() != mOutOfBoundsCell) 3356 { 3296 3357 id = leaf->GetViewCell()->GetId(); 3358 } 3297 3359 3298 3360 stream << "<Leaf viewCellId=\"" << id << "\" />" << endl;
Note: See TracChangeset
for help on using the changeset viewer.