Changeset 2198 for GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
- Timestamp:
- 03/06/07 17:37:28 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
r2187 r2198 366 366 ObjectContainer::const_iterator oit, oit_end = objects.end(); 367 367 368 // warning: not exact number (there can be rays counted twice) 369 // otherwise we would have to traverse trough all rays 368 370 for (oit = objects.begin(); oit != oit_end; ++ oit) 369 371 { … … 373 375 return nRays; 374 376 } 377 375 378 376 379 float BvHierarchy::GetViewSpaceVolume() const … … 384 387 BvhTraversalData &backData) 385 388 { 389 // fill view cells cache 386 390 mNodeTimer.Entry(); 391 392 #if STORE_VIEWCELLS_WITH_BVH 393 AssociateViewCellsWithObjects(sc.mFrontObjects); 394 AssociateViewCellsWithObjects(sc.mBackObjects); 395 #endif 387 396 const BvhTraversalData &tData = sc.mParentData; 388 397 BvhLeaf *leaf = tData.mNode; … … 400 409 // add the new nodes to the tree 401 410 BvhInterior *node = new BvhInterior(parentBox, leaf->GetParent()); 402 411 403 412 404 413 ////////////////// … … 480 489 AssignSortedObjects(sc, frontData, backData); 481 490 } 482 491 492 #if STORE_VIEWCELLS_WITH_BVH 493 ReleaseViewCells(sc.mFrontObjects); 494 ReleaseViewCells(sc.mBackObjects); 495 #endif 496 483 497 mNodeTimer.Exit(); 484 498 … … 615 629 bool computeSplitPlane) 616 630 { 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 618 638 if (computeSplitPlane) 619 639 { … … 640 660 previousMisses + 1 : previousMisses); 641 661 } 642 662 mPlaneTimer.Exit(); 663 664 mEvalTimer.Entry(); 643 665 644 666 const BvhTraversalData &tData = splitCandidate.mParentData; … … 705 727 // compute global decrease in render cost 706 728 splitCandidate.SetPriority(priority); 729 730 #if STORE_VIEWCELLS_WITH_BVH 731 ReleaseViewCells(*splitCandidate.mParentData.mSortedObjects[3]); 732 #endif 733 707 734 mEvalTimer.Exit(); 708 735 } … … 710 737 711 738 int BvHierarchy::EvalPvsEntriesIncr(BvhSubdivisionCandidate &splitCandidate, 712 const float avgRayContri) const739 const float avgRayContri) //const 713 740 { 714 741 const float oldPvsSize = (float)CountViewCells(splitCandidate.mParentData.mNode->mObjects); … … 991 1018 992 1019 SortableEntryContainer::reverse_iterator rcit = 993 mSubdivisionCandidates->rbegin(), rcit_end = 994 mSubdivisionCandidates->rend(); 1020 mSubdivisionCandidates->rbegin(), rcit_end = mSubdivisionCandidates->rend(); 995 1021 996 1022 vector<float>::reverse_iterator rbit = bordersRight.rbegin(); … … 1406 1432 void BvHierarchy::PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData, 1407 1433 const int axis) 1408 { 1434 {mSort2Timer.Entry(); 1409 1435 //-- insert object queries 1410 1436 ObjectContainer *objects = mUseGlobalSorting ? … … 1412 1438 1413 1439 CreateLocalSubdivisionCandidates(*objects, &mSubdivisionCandidates, !mUseGlobalSorting, axis); 1440 mSort2Timer.Exit(); 1414 1441 } 1415 1442 … … 1472 1499 ViewCellContainer viewCells; 1473 1500 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 1475 1509 //cout << "number of rays: " << numRays << endl; 1476 1510 … … 1781 1815 1782 1816 1783 float BvHierarchy::EvalRenderCost(const ObjectContainer &objects) const1817 float BvHierarchy::EvalRenderCost(const ObjectContainer &objects)// const 1784 1818 { 1785 1819 /////////////// … … 1793 1827 1794 1828 return objRenderCost * p; 1795 }1796 1797 1798 float BvHierarchy::EvalProb(const ObjectContainer &objects) const1799 {1800 ///////////////1801 //-- render cost heuristics1802 1803 const float viewSpaceVol = mViewCellsManager->GetViewSpaceBox().GetVolume();1804 1805 // probability that view point lies in a view cell which sees this node1806 return EvalViewCellsVolume(objects) / viewSpaceVol;1807 1829 } 1808 1830 … … 1890 1912 ViewCellContainer &viewCells, 1891 1913 const bool setCounter, 1892 const bool onlyUnmailedRays) const1914 const bool onlyUnmailedRays)// const 1893 1915 { 1894 1916 ViewCell::NewMail(); 1917 1895 1918 ObjectContainer::const_iterator oit, oit_end = objects.end(); 1919 1920 // use mailing to avoid dublicates 1921 const bool useMailBoxing = true; 1896 1922 1897 1923 int numRays = 0; … … 1899 1925 for (oit = objects.begin(); oit != oit_end; ++ oit) 1900 1926 { 1901 // always use only mailed objects1902 numRays += CollectViewCells(*oit, viewCells, true, setCounter, onlyUnmailedRays);1927 // use mailing to avoid duplicates 1928 numRays += CollectViewCells(*oit, viewCells, useMailBoxing, setCounter, onlyUnmailedRays); 1903 1929 } 1904 1930 1905 1931 return numRays; 1932 } 1933 1934 1935 #if STORE_VIEWCELLS_WITH_BVH 1936 1937 1938 void 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 1949 void 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 1963 int 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 1990 int 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; 1906 2017 } 1907 2018 … … 1911 2022 const bool useMailBoxing, 1912 2023 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 2076 int BvHierarchy::CollectViewCellsFromRays(Intersectable *obj, 2077 ViewCellContainer &viewCells, 2078 const bool useMailBoxing, 2079 const bool setCounter, 2080 const bool onlyUnmailedRays) 2081 { 2082 mCollectTimer.Entry(); 1915 2083 VssRayContainer::const_iterator rit, rit_end = obj->GetOrCreateRays()->end(); 1916 2084 … … 1922 2090 1923 2091 if (onlyUnmailedRays && ray->Mailed()) 1924 {1925 2092 continue; 1926 } 1927 2093 1928 2094 ++ numRays; 1929 2095 … … 1945 2111 vc->Mail(); 1946 2112 if (setCounter) 1947 {1948 2113 vc->mCounter = 0; 1949 }1950 2114 } 1951 2115 … … 1954 2118 1955 2119 if (setCounter) 1956 {1957 2120 ++ vc->mCounter; 1958 1959 1960 } 1961 2121 } 2122 } 2123 2124 mCollectTimer.Exit(); 1962 2125 return numRays; 1963 2126 } 1964 2127 1965 2128 1966 int BvHierarchy::CountViewCells (Intersectable *obj)const2129 int BvHierarchy::CountViewCellsFromRays(Intersectable *obj) //const 1967 2130 { 1968 2131 int result = 0; … … 1994 2157 } 1995 2158 1996 1997 int BvHierarchy::CountViewCells(const ObjectContainer &objects) const 2159 #else 2160 2161 int 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 2192 int 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 2246 int BvHierarchy::CountViewCells(const ObjectContainer &objects)// const 1998 2247 { 1999 2248 int nViewCells = 0; 2000 ViewCell::NewMail(); 2249 2250 //ViewCell::NewMail(); 2001 2251 ObjectContainer::const_iterator oit, oit_end = objects.end(); 2002 2252 … … 2179 2429 2180 2430 2181 float BvHierarchy::EvalViewCellsVolume(const ObjectContainer &objects) const2431 float BvHierarchy::EvalViewCellsVolume(const ObjectContainer &objects)// const 2182 2432 { 2183 2433 float vol = 0; … … 2257 2507 //cout << "using " << nRays << " of " << (int)sampleRays.size() << " rays" << endl; 2258 2508 2509 #if STORE_VIEWCELLS_WITH_BVH 2510 AssociateViewCellsWithObjects(objects); 2511 #endif 2512 2259 2513 // probability that volume is "seen" from the view cells 2260 2514 const float prop = EvalViewCellsVolume(objects) / GetViewSpaceVolume(); … … 2283 2537 bvhLeaf->SetSubdivisionCandidate(oSubdivisionCandidate); 2284 2538 2539 #if STORE_VIEWCELLS_WITH_BVH 2540 ReleaseViewCells(objects); 2541 #endif 2542 2285 2543 if (mApplyInitialPartition) 2286 2544 { … … 2325 2583 const ObjectContainer &objects) 2326 2584 { 2327 mSortTimer.Entry();2328 2329 2585 const bool doSort = true; 2330 2586 … … 2362 2618 stable_sort(tData.mSortedObjects[3]->begin(), tData.mSortedObjects[3]->end(), smallerSize); 2363 2619 //sort(tData.mSortedObjects[3]->begin(), tData.mSortedObjects[3]->end(), smallerSize); 2364 2365 mSortTimer.Exit();2366 2620 } 2367 2621 … … 2412 2666 const ObjectContainer &objects) 2413 2667 { 2668 2414 2669 // reset stats 2415 2670 mBvhStats.Reset(); … … 2424 2679 mRoot = bvhleaf; 2425 2680 2681 #if STORE_VIEWCELLS_WITH_BVH 2682 AssociateViewCellsWithObjects(objects); 2683 #endif 2684 2426 2685 //mTermMinProbability *= mVspTree->GetBoundingBox().GetVolume(); 2427 2686 // probability that volume is "seen" from the view cells … … 2438 2697 AssignInitialSortedObjectList(oData, objects); 2439 2698 2440 2699 #if STORE_VIEWCELLS_WITH_BVH 2700 ReleaseViewCells(objects); 2701 #endif 2441 2702 /////////////////// 2442 2703 //-- add first candidate for object space partition
Note: See TracChangeset
for help on using the changeset viewer.