Changeset 653


Ignore:
Timestamp:
02/18/06 22:06:33 (18 years ago)
Author:
mattausch
Message:

implemented split queue (but not tested yet!)

Location:
GTP/trunk/Lib/Vis/Preprocessing/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp

    r652 r653  
    353353        } 
    354354         
    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; 
    356357 
    357358        //-- store rays 
     
    521522 
    522523 
     524 
     525void 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 
    523629bool VspBspTree::TerminationCriteriaMet(const VspBspTraversalData &data) const 
    524630{ 
     
    555661                // create new interior node and two leaf nodes 
    556662                // 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 
    561697                        if (1) 
    562698                        { 
     
    643779 
    644780 
     781BspNode *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 
    645897void VspBspTree::EvalSplitCandidate(VspBspTraversalData &tData, 
    646898                                                                        VspBspSplitCandidate &splitData) 
     
    663915 
    664916 
    665 BspNode *VspBspTree::SubdivideNode(VspBspTraversalData &tData, 
    666                                                                    VspBspTraversalData &frontData, 
    667                                                                    VspBspTraversalData &backData, 
    668                                                                    PolygonContainer &coincident) 
     917BspInterior *VspBspTree::SubdivideNode(const Plane3 &splitPlane, 
     918                                                                           VspBspTraversalData &tData, 
     919                                                                           VspBspTraversalData &frontData, 
     920                                                                           VspBspTraversalData &backData, 
     921                                                                           PolygonContainer &coincident) 
    669922{ 
    670923        BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); 
    671924         
    672         // select subdivision plane 
    673         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 cost 
    690                         ++ mBspStats.maxCostNodes; 
    691             return leaf; 
    692                 } 
    693         } 
    694  
    695925        //-- the front and back traversal data is filled with the new values 
    696926        frontData.mDepth = tData.mDepth + 1; 
     
    702932        backData.mRays = new RayInfoContainer(); 
    703933         
    704         // subdivide rays 
     934 
     935        //-- subdivide rays 
    705936        SplitRays(splitPlane, 
    706937                          *tData.mRays, 
     
    708939                          *backData.mRays); 
    709940 
    710  
    711         // how often was max cost ratio missed in this branch? 
    712         frontData.mMaxCostMisses = maxCostMisses; 
    713         backData.mMaxCostMisses = maxCostMisses; 
    714941 
    715942        // compute pvs 
     
    753980 
    754981 
    755         if (splitAxis < 3) 
    756                 ++ mBspStats.splits[splitAxis]; 
    757         else 
    758                 ++ mBspStats.polySplits; 
     982        /////////////////////////////////////// 
     983        // subdivide further 
    759984 
    760985        mBspStats.nodes += 2; 
    761986 
    762  
    763  
    764         /////////////////////////////////////// 
    765          
    766         //-- subdivide further 
     987         
    767988        BspInterior *interior = new BspInterior(splitPlane); 
    768989 
     
    12211442        } 
    12221443 
    1223         // cost ratio miss 
    1224         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  
    12331444        //-- evaluate axis aligned splits 
    12341445        int axis; 
     
    12361447        float pFront, pBack; 
    12371448 
    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        } 
    12461463 
    12471464        splitAxis = 3; 
     
    12521469                lowestCost = candidateCost; 
    12531470                splitAxis = axis; 
     1471         
    12541472                // assign already computed values 
    12551473                // we can do this because we always save the 
    12561474                // computed values from the axis aligned splits          
     1475 
    12571476                if (fGeom && bGeom) 
    12581477                { 
     
    12691488                DEL_PTR(bGeom); 
    12701489        } 
    1271  
    1272         frontData.mIsKdNode = backData.mIsKdNode =  
    1273                 (data.mIsKdNode && splitAxis < 3); 
     1490         
    12741491 
    12751492#ifdef _DEBUG 
     
    12771494#endif 
    12781495 
    1279         // cost ratio miss 
    12801496        if (lowestCost > mTermMaxCostRatio) 
    12811497        { 
  • GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h

    r652 r653  
    171171                float mRenderCost; 
    172172 
     173                VspBspSplitCandidate(): mRenderCost(0)  
     174                {}; 
     175 
    173176                VspBspSplitCandidate(const Plane3 &plane, const VspBspTraversalData &tData):  
    174                 mSplitPlane(plane), mParentData(tData) 
     177                mSplitPlane(plane), mParentData(tData), mRenderCost(0) 
    175178                {} 
    176179 
     
    395398                                                                 const VspBspTraversalData &data) const; 
    396399 
     400        void ConstructWithSplitQueueQueue(const PolygonContainer &polys, RayInfoContainer *rays); 
     401 
     402 
    397403        /** Returns view cell corresponding to  
    398404                the invalid view space. If it does not exist, it is created. 
     
    424430        BspNode *Subdivide(VspBspTraversalQueue &tStack,  
    425431                                           VspBspTraversalData &tData); 
     432 
     433        BspNode *Subdivide(VspBspSplitQueue &tQueue, 
     434                                           VspBspSplitCandidate &splitCandidate); 
    426435 
    427436        /** Constructs the tree from the given traversal data. 
     
    459468                                                         float &pBack) const; 
    460469 
    461         /** Subdivide leaf. 
     470        /** Subdivides leaf. 
    462471                @param leaf the leaf to be subdivided 
    463472                 
     
    473482        */ 
    474483 
    475         BspNode *SubdivideNode(VspBspTraversalData &tData, 
    476                                                    VspBspTraversalData &frontData, 
    477                                                    VspBspTraversalData &backData, 
    478                                                    PolygonContainer &coincident); 
     484        BspInterior *SubdivideNode(const Plane3 &splitPlane, 
     485                                                           VspBspTraversalData &tData, 
     486                                                           VspBspTraversalData &frontData, 
     487                               VspBspTraversalData &backData, 
     488                                                           PolygonContainer &coincident); 
    479489 
    480490        /** Extracts the meshes of the objects and adds them to polygons.  
Note: See TracChangeset for help on using the changeset viewer.