Ignore:
Timestamp:
01/17/06 23:28:10 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp

    r545 r547  
    3636 
    3737float BspMergeCandidate::sOverallCost = 0; 
     38bool BspMergeCandidate::sUseArea = false; 
    3839 
    3940/********************************************************************/ 
     
    4344VspBspTree::VspBspTree(): 
    4445mRoot(NULL), 
    45 mPvsUseArea(true), 
     46mUseAreaForPvs(false), 
    4647mCostNormalizer(Limits::Small), 
    4748mViewCellsManager(NULL), 
    4849mOutOfBoundsCell(NULL), 
    49 mShowInvalidSpace(false), 
    5050mStoreRays(false) 
    5151{ 
     
    5959        environment->GetIntValue("VspBspTree.Termination.minPvs", mTermMinPvs); 
    6060        environment->GetIntValue("VspBspTree.Termination.minRays", mTermMinRays); 
    61         environment->GetFloatValue("VspBspTree.Termination.minArea", mTermMinArea); 
     61        environment->GetFloatValue("VspBspTree.Termination.minProbability", mTermMinProbability); 
    6262        environment->GetFloatValue("VspBspTree.Termination.maxRayContribution", mTermMaxRayContribution); 
    6363        environment->GetFloatValue("VspBspTree.Termination.minAccRayLenght", mTermMinAccRayLength); 
     
    8989        environment->GetBoolValue("VspBspTree.PostProcess.useRaysForMerge", mUseRaysForMerge); 
    9090 
    91         environment->GetFloatValue("ViewCells.maxPvsRatio", mMaxPvsRatio); 
    9291 
    9392        //-- termination criteria for axis aligned split 
     
    10099        environment->GetFloatValue("VspBspTree.maxStaticMemory", mMaxMemory); 
    101100 
     101         
     102        mStats.open("bspStats.log"); 
     103 
    102104        //-- debug output 
    103105        Debug << "******* VSP BSP options ******** " << endl; 
    104106    Debug << "max depth: " << mTermMaxDepth << endl; 
    105107        Debug << "min PVS: " << mTermMinPvs << endl; 
    106         Debug << "min area: " << mTermMinArea << endl; 
     108        Debug << "min probabiliy: " << mTermMinProbability << endl; 
    107109        Debug << "min rays: " << mTermMinRays << endl; 
    108110        Debug << "max ray contri: " << mTermMaxRayContribution << endl; 
     
    158160                mOutOfBoundsCell = new BspViewCell(); 
    159161                mOutOfBoundsCell->SetId(-1); 
    160         } 
     162                mOutOfBoundsCell->SetValid(false); 
     163        } 
     164 
    161165        return mOutOfBoundsCell; 
    162166} 
     
    311315                } 
    312316        } 
    313  
    314         mMaxPvs = (int)(mMaxPvsRatio * (float)numObj); 
    315          
    316         Debug << "maximal pvs (i.e., pvs still considered as valid) : " << mMaxPvs << endl; 
     317         
     318        Debug << "maximal pvs (i.e., pvs still considered as valid) : " << mViewCellsManager->GetMaxPvsSize() << endl; 
    317319        //-- store rays 
    318320        for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 
     
    334336        } 
    335337 
    336         mTermMinArea *= mBox.SurfaceArea(); // normalize 
     338        if (mUseAreaForPvs) 
     339                mTermMinProbability *= mBox.SurfaceArea(); // normalize 
     340        else  
     341                mTermMinProbability *= mBox.GetVolume(); 
     342 
    337343        mStat.polys = (int)polys.size(); 
    338344 
     
    372378        ConstructGeometry(mRoot, *geom); 
    373379 
     380        const float prop = mUseAreaForPvs ? geom->GetArea() : geom->GetVolume(); 
     381 
    374382        VspBspTraversalData tData(mRoot, 
    375383                                                          new PolygonContainer(polys), 
     
    377385                                                          rays, 
    378386                              ComputePvsSize(*rays), 
    379                                                           geom->GetArea(), 
     387                                                          prop, 
    380388                                                          geom); 
    381389 
     
    386394 
    387395        long startTime = GetTime();      
     396        // used for intermediate time measurements 
    388397        long interTime = GetTime();      
     398 
    389399        mOutOfMemory = false; 
    390400 
     
    437447                (((int)data.mRays->size() <= mTermMinRays) || 
    438448                 (data.mPvs <= mTermMinPvs)   || 
    439                  (data.mArea <= mTermMinArea) || 
     449                 (data.mProbability <= mTermMinProbability) || 
    440450                 (mStat.Leaves() >= mMaxViewCells) || 
    441451                 (data.GetAvgRayContribution() > mTermMaxRayContribution) || 
     
    475485        { 
    476486                BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode); 
    477                 BspViewCell *viewCell; 
    478  
    479                 if (!CheckValid(tData)) 
    480                 { 
    481                         leaf->SetTreeValid(false); 
    482                         PropagateUpValidity(leaf); 
    483                         // view cell for invalid view space 
    484                         viewCell = GetOrCreateOutOfBoundsCell(); 
    485                         ++ mStat.invalidLeaves; 
    486                 } 
    487                 else 
    488                 {       // create new view cell for this leaf                    
    489                         viewCell = new BspViewCell(); 
    490                 } 
     487                BspViewCell *viewCell = new BspViewCell(); 
    491488                 
    492489                leaf->SetViewCell(viewCell); 
     
    506503                                leaf->mVssRays.push_back((*it).mRay); 
    507504                } 
    508  
    509                 viewCell->mLeaves.push_back(leaf); 
    510                 viewCell->SetArea(tData.mArea); 
    511                 leaf->mArea = tData.mArea; 
     505                 
     506                if (!mViewCellsManager->CheckValid(viewCell)) 
     507                { 
     508                        viewCell->SetValid(false); 
     509 
     510                        leaf->SetTreeValid(false); 
     511                        PropagateUpValidity(leaf); 
     512 
     513                        ++ mStat.invalidLeaves; 
     514                } 
     515                 
     516        viewCell->mLeaves.push_back(leaf); 
     517 
     518                if (mUseAreaForPvs) 
     519                        viewCell->SetArea(tData.mProbability); 
     520                else 
     521                        viewCell->SetVolume(tData.mProbability); 
     522 
     523                leaf->mProbability = tData.mProbability; 
    512524 
    513525                EvaluateLeafStats(tData);                
     
    591603 
    592604        // split front and back node geometry and compute area 
    593         if (mPvsUseArea) 
    594         { 
    595                 // if geometry was not already computed 
    596                 if (!frontData.mGeometry && !backData.mGeometry) 
    597                 { 
    598                         frontData.mGeometry = new BspNodeGeometry(); 
    599                         backData.mGeometry = new BspNodeGeometry(); 
    600  
    601                         tData.mGeometry->SplitGeometry(*frontData.mGeometry, 
    602                                                                                    *backData.mGeometry, 
    603                                                                                    interior->GetPlane(), 
    604                                                                                    mBox, 
    605                                                                                    mEpsilon); 
     605         
     606        // if geometry was not already computed 
     607        if (!frontData.mGeometry && !backData.mGeometry) 
     608        { 
     609                frontData.mGeometry = new BspNodeGeometry(); 
     610                backData.mGeometry = new BspNodeGeometry(); 
     611 
     612                tData.mGeometry->SplitGeometry(*frontData.mGeometry, 
     613                                                                           *backData.mGeometry, 
     614                                                                           interior->GetPlane(), 
     615                                                                           mBox, 
     616                                                                           mEpsilon); 
    606617                 
    607                         //frontData.mArea = frontData.mGeometry->GetArea(); 
    608                         //backData.mArea = backData.mGeometry->GetArea(); 
    609                         frontData.mArea = frontData.mGeometry->GetVolume(); 
    610                         backData.mArea = backData.mGeometry->GetVolume(); 
    611                 } 
    612         } 
    613         else 
    614         { 
    615                 frontData.mArea = (float)frontData.mRays->size(); 
    616                 backData.mArea = (float)backData.mRays->size(); 
    617         } 
     618                if (mUseAreaForPvs) 
     619                { 
     620                        frontData.mProbability = frontData.mGeometry->GetArea(); 
     621                        backData.mProbability = backData.mGeometry->GetArea(); 
     622                } 
     623                else 
     624                { 
     625                        frontData.mProbability = frontData.mGeometry->GetVolume(); 
     626                        backData.mProbability = backData.mGeometry->GetVolume(); 
     627                } 
     628        } 
     629         
    618630 
    619631        //-- create front and back leaf 
     
    836848                                                                                 BspNodeGeometry **frontGeom, 
    837849                                                                                 BspNodeGeometry **backGeom, 
    838                                                                                  float &frontArea, 
    839                                                                                  float &backArea, 
    840                                                                                  bool useKdSplit) 
     850                                                                                 float &pFront, 
     851                                                                                 float &pBack, 
     852                                                                                 const bool useKdSplit) 
    841853{ 
    842854        const bool useCostHeuristics = false; 
     
    845857        float nPosition[3]; 
    846858        float nCostRatio[3]; 
    847         float nFrontArea[3]; 
    848         float nBackArea[3]; 
     859        float nProbFront[3]; 
     860        float nProbBack[3]; 
    849861 
    850862        BspNodeGeometry *nFrontGeom[3]; 
     
    856868        AxisAlignedBox3 box; 
    857869        box.Initialize(); 
     870         
    858871        //TODO: for kd split geometry already is box 
    859         if (1 && mPvsUseArea) 
     872        if (1 && mUseAreaForPvs) 
    860873        { 
    861874                PolygonContainer::const_iterator it, it_end = tData.mGeometry->mPolys.end(); 
     
    872885        } 
    873886 
    874         int sAxis = box.Size().DrivingAxis(); 
     887        const int sAxis = box.Size().DrivingAxis(); 
    875888 
    876889        for (axis = 0; axis < 3; ++ axis) 
     
    885898                                nPosition[axis] = (box.Min()[axis] + box.Max()[axis]) * 0.5f; 
    886899                                Vector3 normal(0,0,0); normal[axis] = 1.0f; 
    887                                  
     900 
     901                                // allows faster split because we have axis aligned kd tree boxes 
    888902                                if (useKdSplit) 
    889903                                { 
     
    891905                                                                                                                                box, 
    892906                                                                                                                                axis, 
    893                                                                                                                                 nPosition[axis]); 
     907                                                                                                                                nPosition[axis], 
     908                                                                                                                                nProbFront[axis],  
     909                                                                                                                                nProbBack[axis]); 
    894910                                         
    895911                                        Vector3 pos; 
    896912                                         
    897913                                        pos = box.Max(); pos[axis] = nPosition[axis]; 
    898                                         const AxisAlignedBox3 backBox(box.Min(), pos); 
    899                                         backBox.ExtractPolys(nBackGeom[axis]->mPolys); 
     914                                        AxisAlignedBox3 bBox(box.Min(), pos); 
     915                                        bBox.ExtractPolys(nBackGeom[axis]->mPolys); 
    900916                                         
    901917                                        pos = box.Min(); pos[axis] = nPosition[axis]; 
    902                                         const AxisAlignedBox3 frontBox(pos, box.Max()); 
    903                                         frontBox.ExtractPolys(nFrontGeom[axis]->mPolys); 
    904  
     918                                        AxisAlignedBox3 fBox(pos, box.Max()); 
     919                                        fBox.ExtractPolys(nFrontGeom[axis]->mPolys); 
    905920                                } 
    906921                                else 
     
    908923                                        nCostRatio[axis] = SplitPlaneCost(Plane3(normal, nPosition[axis]),  
    909924                                                                                                          tData, *nFrontGeom[axis], *nBackGeom[axis], 
    910                                                                                                           nFrontArea[axis], nBackArea[axis]); 
     925                                                                                                          nProbFront[axis], nProbBack[axis]); 
    911926                                } 
    912927                        } 
     
    936951        //-- assign values 
    937952        axis = bestAxis; 
    938         frontArea = nFrontArea[bestAxis]; 
    939         backArea = nBackArea[bestAxis]; 
     953        pFront = nProbFront[bestAxis]; 
     954        pBack = nProbBack[bestAxis]; 
    940955 
    941956        // assign best split nodes geometry  
     
    958973                                                                                   const AxisAlignedBox3 &box, 
    959974                                                                                   const int axis, 
    960                                                                                    const float &position) const 
    961 { 
     975                                                                                   const float &position,                                                                                  
     976                                                                                   float &pFront, 
     977                                                                                   float &pBack) const 
     978{ 
     979        int pvsTotal = 0; 
    962980        int pvsFront = 0; 
    963981        int pvsBack = 0; 
    964         int pvsTotal = 0; 
    965  
     982         
    966983        // create unique ids for pvs heuristics 
    967984        GenerateUniqueIdsForPvs(); 
     
    9841001        } 
    9851002 
    986         //-- only one of these options should be one 
    987         //-- pvs + probability heuristics 
    988         float pBack, pFront, pOverall; 
    989  
    990         if (1) 
    991         { 
     1003        //-- pvs heuristics 
     1004        float pOverall; 
     1005 
     1006        // -- simplified computation for mid split 
     1007         
     1008         
     1009        pOverall = data.mProbability; 
     1010 
     1011        if (!mUseAreaForPvs) 
     1012        {   // volume 
     1013                pBack = pFront = pOverall * 0.5f; 
    9921014                // box length substitute for probability 
     1015#if 0 
    9931016                const float minBox = box.Min(axis); 
    9941017                const float maxBox = box.Max(axis); 
     
    9971020                pFront = maxBox - position; 
    9981021                pOverall = maxBox - minBox; 
     1022#endif 
    9991023        } 
    10001024        else //-- area substitute for probability 
    10011025        { 
    1002                 pOverall = box.SurfaceArea(); 
    1003  
    1004                 const bool useMidSplit = true; 
    1005                 //const bool useMidSplit = false;        
    1006                                          
    1007                 //-- simplified computation for mid split 
    10081026                const int axis2 = (axis + 1) % 3; 
    10091027                const int axis3 = (axis + 2) % 3; 
     
    10191037        //Debug << pFront << " " << pBack << " " << pOverall << endl; 
    10201038 
    1021         // float sum = raysBack*(position - minBox) + raysFront*(maxBox - position); 
    10221039        const float newCost = pvsBack * pBack + pvsFront * pFront; 
    1023         //  float oldCost = leaf->mRays.size(); 
    10241040        const float oldCost = (float)pvsSize * pOverall; 
    10251041 
     
    10941110        int axis; 
    10951111        BspNodeGeometry *fGeom, *bGeom; 
    1096         float fArea, bArea; 
     1112        float pFront, pBack; 
    10971113 
    10981114        candidateCost = SelectAxisAlignedPlane(plane, data, axis, 
    10991115                                                                                   &fGeom, &bGeom,  
    1100                                                                                    fArea, bArea, 
     1116                                                                                   pFront, pBack, 
    11011117                                                                                   onlyAxisAligned);      
    11021118 
     
    11091125                // we can do this because we always save the 
    11101126                // computed values from the axis aligned splits 
     1127                 
    11111128                frontData.mGeometry = fGeom; 
    11121129                backData.mGeometry = bGeom; 
    1113                 frontData.mArea = fArea; 
    1114                 backData.mArea = bArea; 
    1115  
     1130         
     1131                frontData.mProbability = pFront; 
     1132                backData.mProbability = pBack; 
     1133                 
    11161134                //! error also computed if cost ratio is missed 
    11171135                ++ mStat.splits[axis]; 
     
    12201238                                                                 BspNodeGeometry &geomFront, 
    12211239                                                                 BspNodeGeometry &geomBack, 
    1222                                                                  float &areaFront, 
    1223                                                                  float &areaBack) const 
     1240                                                                 float &pFront, 
     1241                                                                 float &pBack) const 
    12241242{ 
    12251243        float cost = 0; 
     
    12331251        // probability that view point lies in back / front node 
    12341252        float pOverall = 0; 
    1235         float pFront = 0; 
    1236         float pBack = 0; 
     1253        pFront = 0; 
     1254        pBack = 0; 
    12371255 
    12381256        int raysFront = 0; 
     
    12901308 
    12911309        const float raysSize = (float)data.mRays->size() + Limits::Small; 
     1310 
    12921311        if (mSplitPlaneStrategy & PVS) 
    12931312        { 
     
    12951314                GenerateUniqueIdsForPvs(); 
    12961315 
    1297                 if (mPvsUseArea) // use front and back cell areas to approximate volume 
    1298                 { 
    1299                         // construct child geometry with regard to the candidate split plane 
    1300                         data.mGeometry->SplitGeometry(geomFront, 
    1301                                                                                   geomBack, 
    1302                                                                                   candidatePlane, 
    1303                                                                                   mBox, 
    1304                                                                                   mEpsilon); 
    1305  
    1306                          
     1316                // construct child geometry with regard to the candidate split plane 
     1317                data.mGeometry->SplitGeometry(geomFront, 
     1318                                                                          geomBack, 
     1319                                                                          candidatePlane, 
     1320                                                                          mBox, 
     1321                                                                          mEpsilon); 
     1322 
     1323                pOverall = data.mProbability; 
     1324 
     1325                if (!mUseAreaForPvs) // use front and back cell areas to approximate volume 
     1326                { 
    13071327                        pFront = geomFront.GetVolume(); 
    1308                         pBack = geomBack.GetVolume(); 
    1309                         //areaFront = geomFront.GetArea(); 
    1310                         //areaBack = geomBack.GetArea(); 
    1311  
    1312                         pOverall = data.mArea; 
    1313  
    1314                 } 
    1315                 else // use number of rays to approximate volume 
    1316                 { 
    1317                         pOverall = (float)data.mRays->size(); 
    1318                         pFront = (float)raysFront; 
    1319                         pBack = (float)raysBack; 
     1328                        pBack = pOverall - geomFront.GetVolume(); 
     1329                } 
     1330                else 
     1331                { 
     1332                        pFront = geomFront.GetArea(); 
     1333                        pBack = geomBack.GetArea(); 
    13201334                } 
    13211335        } 
     
    14711485                ++ mStat.maxRayContribNodes; 
    14721486 
    1473         if (data.mArea <= mTermMinArea) 
    1474                 ++ mStat.minAreaNodes; 
     1487        if (data.mProbability <= mTermMinProbability) 
     1488                ++ mStat.minProbabilityNodes; 
    14751489         
    14761490        // accumulate depth to compute average depth 
     
    15861600 
    15871601 
    1588 void VspBspTree::CollectViewCells(ViewCellContainer &viewCells) const 
     1602void VspBspTree::CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const 
    15891603{ 
    15901604        ViewCell::NewMail(); 
    1591         CollectViewCells(mRoot, viewCells, true); 
     1605        CollectViewCells(mRoot, onlyValid, viewCells); 
    15921606} 
    15931607 
     
    16021616        nodeStack.push(mRoot); 
    16031617         
     1618        const bool addToUnbounded = false; 
     1619 
    16041620        while (!nodeStack.empty())  
    16051621        { 
     
    16111627                        BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); 
    16121628 
    1613                         if (node->TreeValid()) 
     1629                        if (!addToUnbounded && node->TreeValid()) 
    16141630                        { 
    16151631                                BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
    16161632                         
    1617                                 if (viewCell->GetPvs().GetSize() > mMaxPvs) 
     1633                                if (!mViewCellsManager->CheckValid(viewCell)) 
    16181634                                { 
    1619                                         while (!viewCell->mLeaves.empty()) 
     1635                                        vector<BspLeaf *>::const_iterator it, it_end = viewCell->mLeaves.end(); 
     1636                                        for (it = viewCell->mLeaves.begin();it != it_end; ++ it) 
    16201637                                        { 
    1621                                                 BspLeaf *l = viewCell->mLeaves.back(); 
     1638                                                BspLeaf *l = *it; 
     1639                                                 
    16221640                                                l->SetTreeValid(false); 
     1641                                                PropagateUpValidity(l); 
     1642                                                 
     1643                                                if (addToUnbounded) 
     1644                                                        l->SetViewCell(GetOrCreateOutOfBoundsCell()); 
     1645 
    16231646                                                ++ mStat.invalidLeaves; 
    1624                                                 PropagateUpValidity(l); 
    1625  
    1626                                                 l->SetViewCell(GetOrCreateOutOfBoundsCell()); 
    1627                                                 viewCell->mLeaves.pop_back(); 
    16281647                                        } 
    16291648 
    1630                                         mOutOfBoundsCell->GetPvs().AddPvs(viewCell->GetPvs()); 
    1631  
    1632                                         DEL_PTR(viewCell); 
     1649                                        // add to unbounded view cell or set to invalid 
     1650                                        if (addToUnbounded) 
     1651                                        { 
     1652                                                GetOrCreateOutOfBoundsCell()->GetPvs().AddPvs(viewCell->GetPvs()); 
     1653                                                DEL_PTR(viewCell); 
     1654                                        } 
     1655                                        else 
     1656                                        { 
     1657                                                viewCell->SetValid(false); 
     1658                                        } 
    16331659                                } 
    16341660                        } 
     
    16441670} 
    16451671 
    1646 void VspBspTree::CollectViewCells(BspNode *root,  
     1672 
     1673void VspBspTree::CollectViewCells(BspNode *root, 
     1674                                                                  bool onlyValid, 
    16471675                                                                  ViewCellContainer &viewCells, 
    16481676                                                                  bool onlyUnmailed) const 
     
    16621690                if (node->IsLeaf()) 
    16631691                { 
    1664                         if (!mShowInvalidSpace && node->TreeValid()) 
     1692                        if (!onlyValid ||node->TreeValid()) 
    16651693                        { 
    16661694                                ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
     
    23572385                { 
    23582386                case -1: 
    2359                         CollectViewCells(node, beam.mViewCells, true); 
     2387                        CollectViewCells(node, true, beam.mViewCells, true); 
    23602388                        break; 
    23612389                case 0: 
     
    24272455 
    24282456        // set new size of view cell 
    2429         vc->SetArea(fVc->GetArea() + bVc->GetArea()); 
    2430  
     2457        if (mUseAreaForPvs) 
     2458                vc->SetArea(fVc->GetArea() + bVc->GetArea()); 
     2459        else 
     2460                vc->SetVolume(fVc->GetVolume() + bVc->GetVolume()); 
     2461         
    24312462        vector<BspLeaf *> fLeaves = fVc->mLeaves; 
    24322463        vector<BspLeaf *> bLeaves = bVc->mLeaves; 
     
    24802511 
    24812512                BspMergeCandidate::sOverallCost +=  
    2482                         leaf->mArea * leaf->mPvs->GetSize(); 
     2513                        leaf->mProbability * leaf->mPvs->GetSize(); 
    24832514 
    24842515                // the same leaves must not be part of two merge candidates 
     
    25102541        vector<BspRay *> bspRays; 
    25112542 
     2543        ViewCell::NewMail(); 
    25122544        long startTime = GetTime(); 
    25132545        ConstructBspRays(bspRays, rays); 
     
    25252557        {   
    25262558                BspRay *ray = bspRays[i]; 
    2527            
     2559         
    25282560                // traverse leaves stored in the rays and compare and  
    25292561                // merge consecutive leaves (i.e., the neighbors in the tree) 
     
    25412573 
    25422574                        BspMergeCandidate::sOverallCost +=  
    2543                                 leaf->mArea * leaf->mPvs->GetSize(); 
     2575                                leaf->mProbability * leaf->mPvs->GetSize(); 
    25442576                         
    25452577                        ++ numLeaves; 
     
    25662598                                 
    25672599                                BspMergeCandidate::sOverallCost +=  
    2568                                         leaf->mArea * leaf->mPvs->GetSize(); 
     2600                                        leaf->mProbability * leaf->mPvs->GetSize(); 
    25692601 
    25702602                                ++ numLeaves; 
     
    25852617                                                found = true; // already in queue 
    25862618                        } 
    2587                          
     2619                 
    25882620                        if (!found) 
    25892621                        { 
     
    25952627                                leaf->Mail(); 
    25962628                                prevLeaf->Mail(); 
    2597  
     2629                 
    25982630                                mMergeQueue.push(BspMergeCandidate(leaf, prevLeaf)); 
    25992631                        } 
     
    26112643        { 
    26122644                vector<BspLeaf *> leaves; 
    2613                 CollectLeaves(leaves, true, mMaxPvs); 
     2645                CollectLeaves(leaves, true); 
    26142646                Debug << "found " << (int)leaves.size() << " new leaves" << endl << endl; 
    26152647                CollectMergeCandidates(leaves); 
     
    26522684                        //NOTE: not sorted! 
    26532685                        for (it = vc->mLeaves.begin(); it != it_end; ++ it) 
     2686                        { 
    26542687                                ray->intersections.push_back(BspIntersection(0, *it)); 
     2688                        } 
    26552689                } 
    26562690 
     
    26602694 
    26612695 
    2662 int VspBspTree::MergeViewCells(const VssRayContainer &rays) 
    2663 { 
    2664         BspMergeCandidate::sMaxPvsSize = mMaxPvs; 
     2696int VspBspTree::MergeViewCells(const VssRayContainer &rays, const ObjectContainer &objects) 
     2697{ 
     2698        BspMergeCandidate::sMaxPvsSize = mViewCellsManager->GetMaxPvsSize(); 
     2699        BspMergeCandidate::sUseArea = mUseAreaForPvs; 
    26652700 
    26662701        MergeStatistics mergeStats; 
     
    26852720 
    26862721        int nViewCells = mStat.Leaves() - mStat.invalidLeaves; 
     2722        int pass = 0; 
     2723        const int nextPass = 200; 
    26872724 
    26882725        //-- use priority queue to merge leaf pairs 
     
    27042741                { 
    27052742                        ViewCell::NewMail(); 
    2706          
     2743                        const float mergeCost = mc.GetMergeCost(); 
     2744 
    27072745                        MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2()); 
    27082746                        -- nViewCells; 
    27092747                         
    2710                         ++ mergeStats.merged; 
    27112748                         
    27122749                        // increase absolute merge cost 
    27132750                        BspMergeCandidate::sOverallCost += mc.GetMergeCost(); 
     2751 
     2752                         
    27142753 
    27152754                        if (showMergeStats) 
     
    27232762                                        mergeStats.maxTreeDist = dist; 
    27242763                                mergeStats.accTreeDist += dist; 
     2764 
     2765 
     2766                                if ((mergeStats.merged % nextPass == 0) || (nViewCells == mMergeMinViewCells)) 
     2767                                { 
     2768                                        mStats  
     2769                                                << "#Pass\n" << pass ++ << endl 
     2770                                                << "#Merged\n" << mergeStats.merged << endl  
     2771                                                << "#Viewcells\n" << nViewCells << endl  
     2772                                                << "#OverallCost\n" << BspMergeCandidate::sOverallCost << endl  
     2773                                                << "#CurrentCost\n" << mergeCost << endl 
     2774                                                << "#RelCost\n" << mc.GetMergeCost() / BspMergeCandidate::sOverallCost << endl 
     2775                                                << "#CurrentPvs\n" << mc.GetLeaf1()->GetViewCell()->GetPvs().GetSize() << endl; 
     2776 
     2777                                        ViewCellContainer viewCells;     
     2778                                        CollectViewCells(mRoot, false, viewCells); 
     2779                         
     2780                                        char s[64]; 
     2781                                        sprintf(s, "merged_viewcells%07d.x3d", nViewCells); 
     2782                                        Exporter *exporter = Exporter::GetExporter(s); 
     2783                                 
     2784                                        if (exporter) 
     2785                                        { 
     2786                                                Debug << "vc size " << viewCells.size() << endl; 
     2787                                                ViewCellContainer::const_iterator it, it_end = viewCells.end(); 
     2788                 
     2789                                                for (it = viewCells.begin(); it != it_end; ++ it) 
     2790                                                { 
     2791                                                        exporter->ExportGeometry(objects); 
     2792                                                        mViewCellsManager->ExportVcGeometry(exporter, *it); 
     2793                                                } 
     2794                                         
     2795                                                delete exporter; 
     2796                                        } 
     2797                                } 
    27252798                        } 
     2799 
    27262800                } 
    27272801                // merge candidate not valid, because one of the leaves was already 
     
    27402814                Debug << mergeStats << endl << endl; 
    27412815         
     2816 
    27422817        //TODO: should return sample contributions? 
    27432818        return mergeStats.merged; 
     
    28362911 
    28372912 
    2838 float GetShuffledVcCost(BspLeaf *leaf, BspViewCell *vc1, BspViewCell *vc2) 
     2913float GetShuffledVcCost(BspLeaf *leaf, BspViewCell *vc1, BspViewCell *vc2, bool useArea) 
    28392914{ 
    28402915        //const int pvs1 = SubtractedPvsSize(vc1, leaf, *leaf->mPvs); 
     
    28422917        const int pvs2 = AddedPvsSize(vc2->GetPvs(), *leaf->mPvs); 
    28432918 
    2844         const float area1 = vc1->GetArea() - leaf->mArea; 
    2845         const float area2 = vc2->GetArea() + leaf->mArea; 
    2846  
    2847         const float cost1 = pvs1 * area1; 
    2848         const float cost2 = pvs2 * area2; 
     2919        float p1, p2; 
     2920 
     2921    if (useArea) 
     2922        { 
     2923                p1 = vc1->GetArea() - leaf->mProbability; 
     2924                p2 = vc2->GetArea() + leaf->mProbability; 
     2925        } 
     2926        else 
     2927        { 
     2928                p1 = vc1->GetVolume() - leaf->mProbability; 
     2929                p2 = vc2->GetVolume() + leaf->mProbability; 
     2930        } 
     2931 
     2932        const float cost1 = pvs1 * p1; 
     2933        const float cost2 = pvs2 * p2; 
    28492934 
    28502935        return cost1 + cost2; 
     
    28602945        vc2->GetPvs().AddPvs(*leaf->mPvs); 
    28612946         
    2862         vc1->SetArea(vc1->GetArea() - leaf->mArea); 
    2863         vc2->SetArea(vc2->GetArea() + leaf->mArea); 
     2947        if (mUseAreaForPvs) 
     2948        { 
     2949                vc1->SetArea(vc1->GetArea() - leaf->mProbability); 
     2950                vc2->SetArea(vc2->GetArea() + leaf->mProbability); 
     2951        } 
     2952        else 
     2953        { 
     2954                vc1->SetVolume(vc1->GetVolume() - leaf->mProbability); 
     2955                vc2->SetVolume(vc2->GetVolume() + leaf->mProbability); 
     2956        } 
    28642957 
    28652958        /// add to second view cell 
     
    28902983        BspViewCell *vc2 = leaf2->GetViewCell(); 
    28912984 
    2892         const float cost1 = vc1->GetPvs().GetSize() * vc1->GetArea(); 
    2893         const float cost2 = vc2->GetPvs().GetSize() * vc2->GetArea(); 
     2985        float cost1;  
     2986        float cost2;  
     2987 
     2988        if (mUseAreaForPvs) 
     2989        { 
     2990                cost1 = vc1->GetPvs().GetSize() * vc1->GetArea(); 
     2991                cost2 = vc2->GetPvs().GetSize() * vc2->GetArea(); 
     2992        } 
     2993        else 
     2994        { 
     2995                cost1 = vc1->GetPvs().GetSize() * vc1->GetVolume(); 
     2996                cost2 = vc2->GetPvs().GetSize() * vc2->GetVolume(); 
     2997        } 
    28942998 
    28952999        const float oldCost = cost1 + cost2; 
     
    29003004        // the view cell should not be empty after the shuffle 
    29013005        if (vc1->mLeaves.size() > 1) 
    2902                 shuffledCost1 = GetShuffledVcCost(leaf1, vc1, vc2); 
     3006                shuffledCost1 = GetShuffledVcCost(leaf1, vc1, vc2, mUseAreaForPvs); 
    29033007        if (vc2->mLeaves.size() > 1) 
    2904                 shuffledCost2 = GetShuffledVcCost(leaf2, vc2, vc1); 
     3008                shuffledCost2 = GetShuffledVcCost(leaf2, vc2, vc1, mUseAreaForPvs); 
    29053009 
    29063010        // shuffling unsuccessful 
     
    29213025        return true; 
    29223026} 
     3027 
    29233028 
    29243029bool VspBspTree::ViewPointValid(const Vector3 &viewPoint) const 
     
    29523057 
    29533058 
    2954 bool VspBspTree::CheckValid(const VspBspTraversalData &data) const 
    2955 { 
    2956         return data.mPvs <= mMaxPvs; 
    2957 } 
    2958  
    2959  
    29603059void VspBspTree::PropagateUpValidity(BspNode *node) 
    29613060{ 
     
    30193118} 
    30203119 
     3120 
    30213121float BspMergeCandidate::GetCost(ViewCell *vc) const 
    30223122{ 
    3023         return vc->GetPvs().GetSize() * vc->GetArea(); 
    3024 } 
     3123        if (sUseArea) 
     3124                return vc->GetPvs().GetSize() * vc->GetArea(); 
     3125 
     3126        return vc->GetPvs().GetSize() * vc->GetVolume(); 
     3127} 
     3128 
    30253129 
    30263130float BspMergeCandidate::GetLeaf1Cost() const 
     
    30303134} 
    30313135 
     3136 
    30323137float BspMergeCandidate::GetLeaf2Cost() const 
    30333138{ 
     
    30353140        return GetCost(vc); 
    30363141} 
     3142 
    30373143 
    30383144void BspMergeCandidate::EvalMergeCost() 
     
    30503156        const float oldCost = GetLeaf1Cost() + GetLeaf2Cost(); 
    30513157 
    3052         const float newCost = 
    3053                 (float)newPvs * (vc1->GetArea() + vc2->GetArea()); 
     3158    const float newCost = sUseArea ?  
     3159                (float)newPvs * (vc1->GetArea() + vc2->GetArea()) : 
     3160                (float)newPvs * (vc1->GetVolume() + vc2->GetVolume()); 
     3161 
    30543162 
    30553163        if (newPvs > sMaxPvsSize) // strong penalty if pvs size too large 
Note: See TracChangeset for help on using the changeset viewer.