Ignore:
Timestamp:
03/06/07 17:37:28 (17 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2187 r2198  
    366366        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
    367367 
     368        // warning: not exact number (there can be rays counted twice) 
     369        // otherwise we would have to traverse trough all rays 
    368370        for (oit = objects.begin(); oit != oit_end; ++ oit) 
    369371        { 
     
    373375        return nRays; 
    374376} 
     377 
    375378                                                                         
    376379float BvHierarchy::GetViewSpaceVolume() const 
     
    384387                                                                                BvhTraversalData &backData) 
    385388{  
     389        // fill view cells cache 
    386390        mNodeTimer.Entry(); 
     391 
     392#if STORE_VIEWCELLS_WITH_BVH 
     393        AssociateViewCellsWithObjects(sc.mFrontObjects); 
     394        AssociateViewCellsWithObjects(sc.mBackObjects); 
     395#endif   
    387396        const BvhTraversalData &tData = sc.mParentData; 
    388397        BvhLeaf *leaf = tData.mNode; 
     
    400409        // add the new nodes to the tree 
    401410        BvhInterior *node = new BvhInterior(parentBox, leaf->GetParent()); 
    402          
     411 
    403412 
    404413        ////////////////// 
     
    480489                AssignSortedObjects(sc, frontData, backData); 
    481490        } 
    482          
     491 
     492#if STORE_VIEWCELLS_WITH_BVH 
     493        ReleaseViewCells(sc.mFrontObjects); 
     494        ReleaseViewCells(sc.mBackObjects); 
     495#endif 
     496 
    483497        mNodeTimer.Exit(); 
    484498 
     
    615629                                                                                   bool computeSplitPlane) 
    616630{ 
    617         mEvalTimer.Entry(); 
     631        mPlaneTimer.Entry(); 
     632 
     633#if STORE_VIEWCELLS_WITH_BVH 
     634        // fill view cells cache 
     635        AssociateViewCellsWithObjects(*splitCandidate.mParentData.mSortedObjects[3]); 
     636#endif 
     637 
    618638        if (computeSplitPlane) 
    619639        { 
     
    640660                                                                                previousMisses + 1 : previousMisses); 
    641661        } 
    642  
     662        mPlaneTimer.Exit(); 
     663 
     664        mEvalTimer.Entry(); 
    643665 
    644666        const BvhTraversalData &tData = splitCandidate.mParentData; 
     
    705727        // compute global decrease in render cost 
    706728        splitCandidate.SetPriority(priority); 
     729 
     730#if STORE_VIEWCELLS_WITH_BVH 
     731        ReleaseViewCells(*splitCandidate.mParentData.mSortedObjects[3]); 
     732#endif 
     733 
    707734        mEvalTimer.Exit(); 
    708735} 
     
    710737 
    711738int BvHierarchy::EvalPvsEntriesIncr(BvhSubdivisionCandidate &splitCandidate,  
    712                                                                         const float avgRayContri) const 
     739                                                                        const float avgRayContri) //const 
    713740{ 
    714741        const float oldPvsSize = (float)CountViewCells(splitCandidate.mParentData.mNode->mObjects); 
     
    9911018 
    9921019        SortableEntryContainer::reverse_iterator rcit =  
    993                 mSubdivisionCandidates->rbegin(), rcit_end =  
    994                 mSubdivisionCandidates->rend(); 
     1020                mSubdivisionCandidates->rbegin(), rcit_end = mSubdivisionCandidates->rend(); 
    9951021 
    9961022        vector<float>::reverse_iterator rbit = bordersRight.rbegin(); 
     
    14061432void BvHierarchy::PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData, 
    14071433                                                                                                        const int axis)                                                                                  
    1408 { 
     1434{mSort2Timer.Entry(); 
    14091435        //-- insert object queries 
    14101436        ObjectContainer *objects = mUseGlobalSorting ?  
     
    14121438 
    14131439        CreateLocalSubdivisionCandidates(*objects, &mSubdivisionCandidates, !mUseGlobalSorting, axis); 
     1440                mSort2Timer.Exit(); 
    14141441} 
    14151442 
     
    14721499        ViewCellContainer viewCells; 
    14731500 
    1474         const int numRays = CollectViewCells(tData.mNode->mObjects, viewCells, true, true); 
     1501        const bool setCounter = true; 
     1502        const bool onlyUnmailed = true; 
     1503 
     1504        const int numRays = CollectViewCells(tData.mNode->mObjects,  
     1505                                                                                 viewCells,  
     1506                                                                                 setCounter,  
     1507                                                                                 onlyUnmailed); 
     1508 
    14751509        //cout << "number of rays: " << numRays << endl; 
    14761510 
     
    17811815 
    17821816 
    1783 float BvHierarchy::EvalRenderCost(const ObjectContainer &objects) const 
     1817float BvHierarchy::EvalRenderCost(const ObjectContainer &objects)// const 
    17841818{        
    17851819        /////////////// 
     
    17931827         
    17941828        return objRenderCost * p; 
    1795 } 
    1796  
    1797  
    1798 float BvHierarchy::EvalProb(const ObjectContainer &objects) const 
    1799 {        
    1800         /////////////// 
    1801         //-- render cost heuristics 
    1802  
    1803         const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume(); 
    1804  
    1805         // probability that view point lies in a view cell which sees this node 
    1806         return EvalViewCellsVolume(objects) / viewSpaceVol; 
    18071829} 
    18081830 
     
    18901912                                                                  ViewCellContainer &viewCells, 
    18911913                                                                  const bool setCounter, 
    1892                                                                   const bool onlyUnmailedRays) const 
     1914                                                                  const bool onlyUnmailedRays)// const 
    18931915{ 
    18941916        ViewCell::NewMail(); 
     1917 
    18951918        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
     1919 
     1920        // use mailing to avoid dublicates 
     1921        const bool useMailBoxing = true; 
    18961922 
    18971923        int numRays = 0; 
     
    18991925        for (oit = objects.begin(); oit != oit_end; ++ oit) 
    19001926        { 
    1901                 // always use only mailed objects 
    1902                 numRays += CollectViewCells(*oit, viewCells, true, setCounter, onlyUnmailedRays); 
     1927                // use mailing to avoid duplicates 
     1928                numRays += CollectViewCells(*oit, viewCells, useMailBoxing, setCounter, onlyUnmailedRays); 
    19031929        } 
    19041930 
    19051931        return numRays; 
     1932} 
     1933 
     1934 
     1935#if STORE_VIEWCELLS_WITH_BVH 
     1936 
     1937 
     1938void BvHierarchy::ReleaseViewCells(const ObjectContainer &objects) 
     1939{ 
     1940        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
     1941 
     1942        for (oit = objects.begin(); oit != oit_end; ++ oit) 
     1943        { 
     1944                (*oit)->DelViewCells(); 
     1945        } 
     1946} 
     1947 
     1948 
     1949void BvHierarchy::AssociateViewCellsWithObjects(const ObjectContainer &objects) const 
     1950{ 
     1951        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
     1952 
     1953        const bool useMailBoxing = true; 
     1954        for (oit = objects.begin(); oit != oit_end; ++ oit) 
     1955        { 
     1956                ViewCell::NewMail(); 
     1957                // use mailing to avoid duplicates 
     1958                AssociateViewCellsWithObject(*oit, useMailBoxing); 
     1959        } 
     1960} 
     1961 
     1962 
     1963int BvHierarchy::AssociateViewCellsWithObject(Intersectable *obj, const bool useMailBoxing) const 
     1964{ 
     1965        if (!obj->GetOrCreateViewCells()->empty()) 
     1966        { 
     1967                cerr << "AssociateViewCellsWithObject: view cells cache not working" << endl; 
     1968        } 
     1969 
     1970        ViewCellContainer *objViewCells = obj->GetOrCreateViewCells(); 
     1971        VssRayContainer *vssRays = obj->GetOrCreateRays(); 
     1972 
     1973        VssRayContainer::const_iterator rit, rit_end = vssRays->end(); 
     1974 
     1975        // fill cache 
     1976        for (rit = vssRays->begin(); rit < rit_end; ++ rit) 
     1977        { 
     1978                VssRay *ray = (*rit); 
     1979 
     1980                //      if (onlyUnmailedRays && ray->Mailed()) 
     1981                //              continue; 
     1982                mHierarchyManager->mVspTree->GetViewCells(*ray, *objViewCells); 
     1983        } 
     1984 
     1985        return (int)vssRays->size(); 
     1986} 
     1987 
     1988 
     1989 
     1990int BvHierarchy::CountViewCells(Intersectable *obj) //const 
     1991{ 
     1992        ViewCellContainer *viewCells = obj->GetOrCreateViewCells(); 
     1993 
     1994        if (obj->GetOrCreateViewCells()->empty()) 
     1995        { 
     1996                //cerr << "h";//CountViewCells: view cells empty, view cells cache not working" << endl; 
     1997                return CountViewCellsFromRays(obj); 
     1998        } 
     1999         
     2000        int result = 0; 
     2001 
     2002        ViewCellContainer::const_iterator vit, vit_end = viewCells->end(); 
     2003         
     2004        for (vit = viewCells->begin(); vit != vit_end; ++ vit) 
     2005        { 
     2006                ViewCell *vc = *vit; 
     2007 
     2008                // store view cells 
     2009                if (!vc->Mailed()) 
     2010                { 
     2011                        vc->Mail(); 
     2012                        ++ result; 
     2013                } 
     2014        } 
     2015 
     2016        return result; 
    19062017} 
    19072018 
     
    19112022                                                                  const bool useMailBoxing, 
    19122023                                                                  const bool setCounter, 
    1913                                                                   const bool onlyUnmailedRays) const 
    1914 { 
     2024                                                                  const bool onlyUnmailedRays)// const 
     2025{ 
     2026        if (obj->GetOrCreateViewCells()->empty()) 
     2027        { 
     2028                //cerr << "g";//CollectViewCells: view cells empty, view cells cache not working" << endl; 
     2029                return CollectViewCellsFromRays(obj, viewCells, useMailBoxing, setCounter, onlyUnmailedRays); 
     2030        } 
     2031 
     2032        mCollectTimer.Entry(); 
     2033 
     2034        ViewCellContainer *objViewCells = obj->GetOrCreateViewCells(); 
     2035 
     2036        /////////// 
     2037        //-- use view cells cache 
     2038 
     2039        // fastest way: just copy to the end 
     2040        //if (!useMailBoxing)   viewCells.insert(viewCells.end(), objViewCells->begin(), objViewCells->end()); 
     2041 
     2042        // loop through view cells 
     2043        // matt: probably slow to insert  view cells one by one 
     2044        ViewCellContainer::const_iterator vit, vit_end = objViewCells->end(); 
     2045 
     2046        for (vit = objViewCells->begin(); vit != vit_end; ++ vit) 
     2047        { 
     2048                ViewCell *vc = *vit; 
     2049 
     2050                // store view cells 
     2051                if (!useMailBoxing || !vc->Mailed()) 
     2052                { 
     2053                        if (useMailBoxing)  
     2054                        { 
     2055                                // view cell not mailed 
     2056                                vc->Mail(); 
     2057                                 
     2058                                if (setCounter) 
     2059                                        vc->mCounter = 0; 
     2060                                //viewCells.push_back(vc); 
     2061                        } 
     2062 
     2063                        viewCells.push_back(vc); 
     2064                } 
     2065 
     2066                if (setCounter) 
     2067                        ++ vc->mCounter; 
     2068        } 
     2069 
     2070        mCollectTimer.Exit(); 
     2071 
     2072        return (int)objViewCells->size(); 
     2073} 
     2074 
     2075 
     2076int BvHierarchy::CollectViewCellsFromRays(Intersectable *obj,  
     2077                                                                                  ViewCellContainer &viewCells, 
     2078                                                                                  const bool useMailBoxing, 
     2079                                                                                  const bool setCounter, 
     2080                                                                                  const bool onlyUnmailedRays) 
     2081{ 
     2082        mCollectTimer.Entry(); 
    19152083        VssRayContainer::const_iterator rit, rit_end = obj->GetOrCreateRays()->end(); 
    19162084 
     
    19222090 
    19232091                if (onlyUnmailedRays && ray->Mailed()) 
    1924                 { 
    19252092                        continue; 
    1926                 } 
    1927  
     2093                 
    19282094                ++ numRays; 
    19292095 
     
    19452111                                        vc->Mail(); 
    19462112                                        if (setCounter) 
    1947                                         { 
    19482113                                                vc->mCounter = 0; 
    1949                                         } 
    19502114                                } 
    19512115 
     
    19542118                         
    19552119                        if (setCounter) 
    1956                         { 
    19572120                                ++ vc->mCounter; 
    1958                         } 
    1959                 } 
    1960         } 
    1961  
     2121                } 
     2122        } 
     2123 
     2124        mCollectTimer.Exit(); 
    19622125        return numRays; 
    19632126} 
    19642127 
    19652128 
    1966 int BvHierarchy::CountViewCells(Intersectable *obj) const 
     2129int BvHierarchy::CountViewCellsFromRays(Intersectable *obj) //const 
    19672130{ 
    19682131        int result = 0; 
     
    19942157} 
    19952158 
    1996  
    1997 int BvHierarchy::CountViewCells(const ObjectContainer &objects) const 
     2159#else 
     2160 
     2161int BvHierarchy::CountViewCells(Intersectable *obj) //const 
     2162{ 
     2163        int result = 0; 
     2164         
     2165        VssRayContainer::const_iterator rit, rit_end = obj->GetOrCreateRays()->end(); 
     2166 
     2167        for (rit = obj->GetOrCreateRays()->begin(); rit < rit_end; ++ rit) 
     2168        { 
     2169                VssRay *ray = (*rit); 
     2170                ViewCellContainer tmpViewCells; 
     2171         
     2172                mHierarchyManager->mVspTree->GetViewCells(*ray, tmpViewCells); 
     2173                 
     2174                ViewCellContainer::const_iterator vit, vit_end = tmpViewCells.end(); 
     2175                for (vit = tmpViewCells.begin(); vit != vit_end; ++ vit) 
     2176                { 
     2177                        ViewCell *vc = *vit; 
     2178 
     2179                        // store view cells 
     2180                        if (!vc->Mailed()) 
     2181                        { 
     2182                                vc->Mail(); 
     2183                                ++ result; 
     2184                        } 
     2185                } 
     2186        } 
     2187 
     2188        return result; 
     2189} 
     2190 
     2191 
     2192int BvHierarchy::CollectViewCells(Intersectable *obj,  
     2193                                                                  ViewCellContainer &viewCells, 
     2194                                                                  const bool useMailBoxing, 
     2195                                                                  const bool setCounter, 
     2196                                                                  const bool onlyUnmailedRays) 
     2197{ 
     2198        mCollectTimer.Entry(); 
     2199        VssRayContainer::const_iterator rit, rit_end = obj->GetOrCreateRays()->end(); 
     2200 
     2201        int numRays = 0; 
     2202 
     2203        for (rit = obj->GetOrCreateRays()->begin(); rit < rit_end; ++ rit) 
     2204        { 
     2205                VssRay *ray = (*rit); 
     2206 
     2207                if (onlyUnmailedRays && ray->Mailed()) 
     2208                        continue; 
     2209                 
     2210                ++ numRays; 
     2211 
     2212                ViewCellContainer tmpViewCells; 
     2213                mHierarchyManager->mVspTree->GetViewCells(*ray, tmpViewCells); 
     2214 
     2215                // matt: probably slow to allocate memory for view cells every time 
     2216                ViewCellContainer::const_iterator vit, vit_end = tmpViewCells.end(); 
     2217 
     2218                for (vit = tmpViewCells.begin(); vit != vit_end; ++ vit) 
     2219                { 
     2220                        ViewCell *vc = *vit; 
     2221 
     2222                        // store view cells 
     2223                        if (!useMailBoxing || !vc->Mailed()) 
     2224                        { 
     2225                                if (useMailBoxing) // => view cell not mailed 
     2226                                { 
     2227                                        vc->Mail(); 
     2228                                        if (setCounter) 
     2229                                                vc->mCounter = 0; 
     2230                                } 
     2231 
     2232                                viewCells.push_back(vc); 
     2233                        } 
     2234                         
     2235                        if (setCounter) 
     2236                                ++ vc->mCounter; 
     2237                } 
     2238        } 
     2239 
     2240        mCollectTimer.Exit(); 
     2241        return numRays; 
     2242} 
     2243#endif 
     2244 
     2245 
     2246int BvHierarchy::CountViewCells(const ObjectContainer &objects)// const 
    19982247{ 
    19992248        int nViewCells = 0; 
    2000         ViewCell::NewMail(); 
     2249 
     2250        //ViewCell::NewMail(); 
    20012251        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
    20022252 
     
    21792429 
    21802430 
    2181 float BvHierarchy::EvalViewCellsVolume(const ObjectContainer &objects) const 
     2431float BvHierarchy::EvalViewCellsVolume(const ObjectContainer &objects)// const 
    21822432{ 
    21832433        float vol = 0; 
     
    22572507        //cout << "using " << nRays << " of " << (int)sampleRays.size() << " rays" << endl; 
    22582508         
     2509#if STORE_VIEWCELLS_WITH_BVH 
     2510        AssociateViewCellsWithObjects(objects); 
     2511#endif 
     2512 
    22592513        // probability that volume is "seen" from the view cells 
    22602514        const float prop = EvalViewCellsVolume(objects) / GetViewSpaceVolume(); 
     
    22832537        bvhLeaf->SetSubdivisionCandidate(oSubdivisionCandidate); 
    22842538 
     2539#if STORE_VIEWCELLS_WITH_BVH 
     2540        ReleaseViewCells(objects); 
     2541#endif 
     2542 
    22852543        if (mApplyInitialPartition) 
    22862544        { 
     
    23252583                                                                                                const ObjectContainer &objects) 
    23262584{ 
    2327         mSortTimer.Entry(); 
    2328  
    23292585        const bool doSort = true; 
    23302586 
     
    23622618        stable_sort(tData.mSortedObjects[3]->begin(), tData.mSortedObjects[3]->end(), smallerSize); 
    23632619        //sort(tData.mSortedObjects[3]->begin(), tData.mSortedObjects[3]->end(), smallerSize); 
    2364  
    2365         mSortTimer.Exit(); 
    23662620} 
    23672621 
     
    24122666                                                const ObjectContainer &objects) 
    24132667{ 
     2668 
    24142669        // reset stats 
    24152670        mBvhStats.Reset(); 
     
    24242679        mRoot = bvhleaf; 
    24252680         
     2681#if STORE_VIEWCELLS_WITH_BVH 
     2682        AssociateViewCellsWithObjects(objects); 
     2683#endif 
     2684 
    24262685        //mTermMinProbability *= mVspTree->GetBoundingBox().GetVolume(); 
    24272686        // probability that volume is "seen" from the view cells 
     
    24382697                AssignInitialSortedObjectList(oData, objects); 
    24392698         
    2440  
     2699#if STORE_VIEWCELLS_WITH_BVH 
     2700        ReleaseViewCells(objects); 
     2701#endif 
    24412702        /////////////////// 
    24422703        //-- add first candidate for object space partition      
Note: See TracChangeset for help on using the changeset viewer.