Ignore:
Timestamp:
02/06/06 19:09:53 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r590 r600  
    8686        //-- max cost ratio for early tree termination 
    8787        environment->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio); 
    88  
     88mTermMinPolygons = 25; 
    8989        //-- factors for bsp tree split plane heuristics 
    9090        environment->GetFloatValue("VspBspTree.Factor.balancedRays", mBalancedRaysFactor); 
     
    116116 
    117117 
     118        mSubdivisionStats.open("subdivisionStats.log"); 
    118119 
    119120 
     
    387388 
    388389// return memory usage in MB 
    389 float VspBspTree::GetMemUsage(/*const VspBspTraversalStack &tstack*/) const 
     390float VspBspTree::GetMemUsage(/*const VspBspTraversalQueue &tstack*/) const 
    390391{ 
    391392        return 
     
    402403void VspBspTree::Construct(const PolygonContainer &polys, RayInfoContainer *rays) 
    403404{ 
    404         VspBspTraversalStack tStack; 
     405        VspBspTraversalQueue tQueue; 
    405406 
    406407        mRoot = new BspLeaf(); 
     
    426427                tData.mIsKdNode = false; 
    427428 
    428         tStack.push(tData); 
    429  
     429        tQueue.push(tData); 
     430 
     431        mTotalCost = tData.GetCost() / mBox.GetVolume(); 
     432 
     433        Debug << "total cost: " << mTotalCost << endl; 
     434        mSplits = 0; 
     435         
    430436        mBspStats.Start(); 
    431437        cout << "Contructing vsp bsp tree ... \n"; 
     
    440446        int nleaves = 500; 
    441447 
    442         while (!tStack.empty()) 
    443         { 
    444                 tData = tStack.top(); 
    445             tStack.pop();                
     448        while (!tQueue.empty()) 
     449        { 
     450                tData = tQueue.top(); 
     451            tQueue.pop();                
    446452 
    447453                if (0 && !mOutOfMemory) 
     
    457463 
    458464                // subdivide leaf node 
    459                 BspNode *r = Subdivide(tStack, tData); 
     465                BspNode *r = Subdivide(tQueue, tData); 
    460466 
    461467                if (r == mRoot) 
     
    489495                 (data.mProbability <= mTermMinProbability) || 
    490496                 (mBspStats.Leaves() >= mMaxViewCells) || 
     497#if 0 
     498                 (((int)data.mPolygons->size() <= mTermMinPolygons) && !data.mPolygons->empty())|| 
     499#endif 
    491500                 (data.GetAvgRayContribution() > mTermMaxRayContribution) || 
    492501                 (data.mDepth >= mTermMaxDepth)); 
     
    494503 
    495504 
    496 BspNode *VspBspTree::Subdivide(VspBspTraversalStack &tStack, 
     505BspNode *VspBspTree::Subdivide(VspBspTraversalQueue &tQueue, 
    497506                                                           VspBspTraversalData &tData) 
    498507{ 
     
    512521                if (!newNode->IsLeaf()) //-- continue subdivision 
    513522                { 
     523                        if (1) 
     524                        { 
     525                                float costDecr =  
     526                                        (tFrontData.GetCost() + tBackData.GetCost() - tData.GetCost()) / mBox.GetVolume(); 
     527                                mTotalCost += costDecr; 
     528 
     529                                mSubdivisionStats  
     530                                                << "#Nodes\n" << ++ mSplits << endl 
     531                                                << "#RenderCostDecrease\n" << -costDecr << endl  
     532                                                << "#TotalRenderCost\n" << mTotalCost << endl; 
     533                        } 
     534 
    514535                        // push the children on the stack 
    515                         tStack.push(tFrontData); 
    516                         tStack.push(tBackData); 
     536                        tQueue.push(tFrontData); 
     537                        tQueue.push(tBackData); 
    517538 
    518539                        // delete old leaf node 
     
    13411362                        float oldCost, newCost; 
    13421363 
     1364                        // only render cost 
    13431365                        if (1) 
    13441366                        { 
     
    13461368                                newCost = newRenderCost; 
    13471369                        } 
    1348                         else 
     1370                        else // also considering standard deviation 
    13491371                        { 
    13501372                                // standard deviation is difference of back and front pvs 
     
    13611383 
    13621384                        cost += mPvsFactor * newCost / oldCost; 
    1363                 } 
    1364                 else 
    1365                 { 
    1366                         //-- compute weighted sum of expected render cost and standard deviation 
    1367                          
    1368                         //-- first compute expected render cost          
    1369                         const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit); 
    1370                 const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit); 
    1371                         const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit); 
    1372                          
    1373                         const float renderCostFront = penaltyFront * pFront; 
    1374                         const float renderCostBack = penaltyBack * pBack; 
    1375  
    1376                         const float oldRenderCost = pOverall * (float)penaltyOld + Limits::Small; 
    1377                         const float newRenderCost = renderCostFront + renderCostBack; 
    1378  
    1379  
    1380                         //-- compute standard deviation 
    1381                         const float expectedCost = 0.5f * (renderCostBack + renderCostFront); 
    1382  
    1383                         const float devFront = fabs(renderCostFront - expectedCost); 
    1384                          
    1385                         const float devBack = fabs(renderCostBack - expectedCost); 
    1386  
    1387                         const float newDev = 0.5f * (devFront + devBack); 
    1388                         const float oldDev = oldRenderCost * oldRenderCost; 
    1389  
    1390                         const float newCost = newRenderCost * mRenderCostWeight + newDev * (1.0f - mRenderCostWeight); 
    1391                         const float oldCost = oldRenderCost * mRenderCostWeight + oldDev * (1.0f - mRenderCostWeight); 
    1392  
    1393                         cost += mPvsFactor * newCost / oldCost; 
    1394  
    1395                         // also try to equalize render differences between front and back pvs 
    1396                         //const float oldCost = pOverall + Limits::Small; 
    1397                         //float newCost = (float)abs(pvsFront - pvsBack); 
    1398                          
    13991385                } 
    14001386        } 
     
    14041390                  << " frontpvs: " << pvsFront << " pFront: " << pFront 
    14051391                  << " backpvs: " << pvsBack << " pBack: " << pBack << endl << endl; 
     1392        Debug << "cost: " << cost << endl; 
    14061393#endif 
    14071394 
     1395         
    14081396        // normalize cost by sum of linear factors 
    14091397        if(0) 
     
    16381626        int hits = 0; 
    16391627 
    1640         stack<BspRayTraversalData> tStack; 
     1628        stack<BspRayTraversalData> tQueue; 
    16411629 
    16421630        float maxt, mint; 
     
    16461634 
    16471635        Intersectable::NewMail(); 
    1648  
     1636        ViewCell::NewMail(); 
    16491637        Vector3 entp = ray.Extrap(mint); 
    16501638        Vector3 extp = ray.Extrap(maxt); 
     
    16901678 
    16911679                        // push data for far child 
    1692                         tStack.push(BspRayTraversalData(farChild, extp, maxt)); 
     1680                        tQueue.push(BspRayTraversalData(farChild, extp, maxt)); 
    16931681 
    16941682                        // find intersection of ray segment with plane 
     
    17091697 
    17101698                        //-- fetch the next far child from the stack 
    1711                         if (tStack.empty()) 
     1699                        if (tQueue.empty()) 
    17121700                                break; 
    17131701 
     
    17181706                                break; 
    17191707 
    1720                         BspRayTraversalData &s = tStack.top(); 
     1708                        BspRayTraversalData &s = tQueue.top(); 
    17211709 
    17221710                        node = s.mNode; 
     
    17241712                        maxt = s.mMaxT; 
    17251713 
    1726                         tStack.pop(); 
     1714                        tQueue.pop(); 
    17271715                } 
    17281716        } 
     
    22042192        return (int)neighbors.size(); 
    22052193} 
     2194 
     2195 
     2196 
     2197int VspBspTree::FindApproximateNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, 
     2198                                                                                 const bool onlyUnmailed) const 
     2199{ 
     2200        stack<bspNodePair> nodeStack; 
     2201         
     2202        BspNodeGeometry nodeGeom; 
     2203        ConstructGeometry(n, nodeGeom); 
     2204         
     2205        // split planes from the root to this node 
     2206        // needed to verify that we found neighbor leaf 
     2207        // TODO: really needed? 
     2208        vector<Plane3> halfSpaces; 
     2209        ExtractHalfSpaces(n, halfSpaces); 
     2210 
     2211 
     2212        BspNodeGeometry *rgeom = new BspNodeGeometry(); 
     2213        ConstructGeometry(mRoot, *rgeom); 
     2214 
     2215        nodeStack.push(bspNodePair(mRoot, rgeom)); 
     2216 
     2217        while (!nodeStack.empty()) 
     2218        { 
     2219                BspNode *node = nodeStack.top().first; 
     2220                BspNodeGeometry *geom = nodeStack.top().second; 
     2221         
     2222                nodeStack.pop(); 
     2223 
     2224                if (node->IsLeaf()) 
     2225                { 
     2226                        // test if this leaf is in valid view space 
     2227                        if (node->TreeValid() && 
     2228                                (node != n) &&  
     2229                                (!onlyUnmailed || !node->Mailed())) 
     2230                        { 
     2231                                bool isAdjacent = true; 
     2232 
     2233                                // neighbor was found 
     2234                                if (isAdjacent) 
     2235                                {        
     2236                                        neighbors.push_back(dynamic_cast<BspLeaf *>(node)); 
     2237                                } 
     2238                        } 
     2239                } 
     2240                else 
     2241                { 
     2242                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     2243 
     2244                        const int cf = Polygon3::ClassifyPlane(nodeGeom.mPolys, 
     2245                                                                                                   interior->GetPlane(), 
     2246                                                                                                   mEpsilon); 
     2247                         
     2248                        BspNode *front = interior->GetFront(); 
     2249                        BspNode *back = interior->GetBack(); 
     2250             
     2251                        BspNodeGeometry *fGeom = new BspNodeGeometry(); 
     2252                        BspNodeGeometry *bGeom = new BspNodeGeometry(); 
     2253 
     2254                        geom->SplitGeometry(*fGeom, 
     2255                                                                *bGeom, 
     2256                                                                interior->GetPlane(), 
     2257                                                                mBox, 
     2258                                                                mEpsilon); 
     2259                 
     2260                        if (cf == Polygon3::FRONT_SIDE) 
     2261                        { 
     2262                                nodeStack.push(bspNodePair(interior->GetFront(), fGeom)); 
     2263                                DEL_PTR(bGeom); 
     2264                        } 
     2265                        else 
     2266                        { 
     2267                                if (cf == Polygon3::BACK_SIDE) 
     2268                                { 
     2269                                        nodeStack.push(bspNodePair(interior->GetBack(), bGeom)); 
     2270                                        DEL_PTR(fGeom); 
     2271                                } 
     2272                                else 
     2273                                {       // random decision 
     2274                                        nodeStack.push(bspNodePair(front, fGeom)); 
     2275                                        nodeStack.push(bspNodePair(back, bGeom)); 
     2276                                } 
     2277                        } 
     2278                } 
     2279         
     2280                DEL_PTR(geom); 
     2281        } 
     2282 
     2283        return (int)neighbors.size(); 
     2284} 
     2285 
    22062286 
    22072287 
     
    23782458{ 
    23792459        int hits = 0; 
    2380         stack<BspRayTraversalData> tStack; 
     2460        stack<BspRayTraversalData> tQueue; 
    23812461 
    23822462        float mint = 0.0f, maxt = 1.0f; 
    23832463 
    23842464        Intersectable::NewMail(); 
     2465        ViewCell::NewMail(); 
    23852466 
    23862467        Vector3 entp = origin; 
     
    24312512 
    24322513                        // push data for far child 
    2433                         tStack.push(BspRayTraversalData(farChild, extp)); 
     2514                        tQueue.push(BspRayTraversalData(farChild, extp)); 
    24342515 
    24352516                        // find intersection of ray segment with plane 
     
    24502531 
    24512532                        //-- fetch the next far child from the stack 
    2452                         if (tStack.empty()) 
     2533                        if (tQueue.empty()) 
    24532534                                break; 
    24542535 
    24552536                        entp = extp; 
    24562537                         
    2457                         BspRayTraversalData &s = tStack.top(); 
     2538                        BspRayTraversalData &s = tQueue.top(); 
    24582539 
    24592540                        node = s.mNode; 
    24602541                        extp = s.mExitPoint; 
    24612542 
    2462                         tStack.pop(); 
     2543                        tQueue.pop(); 
    24632544                } 
    24642545        } 
     
    27112792                vector<BspLeaf *> neighbors; 
    27122793                FindNeighbors(leaf, neighbors, true); 
    2713  
     2794                //FindApproximateNeighbors(leaf, neighbors, true); 
    27142795                vector<BspLeaf *>::const_iterator nit, nit_end = neighbors.end(); 
    27152796 
Note: See TracChangeset for help on using the changeset viewer.