Ignore:
Timestamp:
01/20/06 22:32:51 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r561 r562  
    329329                float minT, maxT; 
    330330 
     331                //static Ray hray; 
     332                //hray.Init(ray.GetOrigin(), ray.GetDir(), Ray::LINE_SEGMENT); 
     333 
    331334                // TODO: not very efficient to implictly cast between rays types 
    332335                if (mBox.GetRaySegment(*ray, minT, maxT)) 
     
    393396                                                          geom); 
    394397 
     398        // first node is kd node 
     399        tData.mIsKdNode = true; 
     400 
    395401        tStack.push(tData); 
    396402 
     
    510516                } 
    511517                 
    512                 if (!mViewCellsManager->CheckValidity(viewCell, 0.0, mViewCellsManager->GetMaxPvsRatio())) 
     518                if (0 && !mViewCellsManager->CheckValidity(viewCell, 0, mViewCellsManager->GetMaxPvsSize())) 
    513519                { 
    514520                        viewCell->SetValid(false); 
     
    880886        box.Initialize(); 
    881887         
    882         //TODO: for kd split geometry already is box 
     888        //TODO: for kd split geometry already is box => only take minmax vertices 
    883889        if (1) 
    884890        { 
     
    9971003        const int pvsSize = data.mPvs; 
    9981004 
     1005        RayInfoContainer::const_iterator rit, rit_end = data.mRays->end(); 
     1006 
    9991007        // this is the main ray classification loop! 
    1000         for(RayInfoContainer::iterator ri = data.mRays->begin(); 
    1001                 ri != data.mRays->end(); ++ ri) 
    1002         { 
    1003                 if (!(*ri).mRay->IsActive()) 
    1004                         continue; 
     1008        for(rit = data.mRays->begin(); rit != rit_end; ++ rit) 
     1009        { 
     1010                //if (!(*rit).mRay->IsActive()) continue; 
    10051011 
    10061012                // determine the side of this ray with respect to the plane 
    10071013                float t; 
    1008                 int side = (*ri).ComputeRayIntersection(axis, position, t); 
    1009          
    1010                 AddObjToPvs((*ri).mRay->mTerminationObject, side, pvsFront, pvsBack, pvsTotal); 
    1011                 AddObjToPvs((*ri).mRay->mOriginObject, side, pvsFront, pvsBack, pvsTotal); 
     1014                const int side = (*rit).ComputeRayIntersection(axis, position, t); 
     1015         
     1016                AddObjToPvs((*rit).mRay->mTerminationObject, side, pvsFront, pvsBack, pvsTotal); 
     1017                AddObjToPvs((*rit).mRay->mOriginObject, side, pvsFront, pvsBack, pvsTotal); 
    10121018        } 
    10131019 
     
    10151021        float pOverall; 
    10161022 
    1017         // -- simplified computation for mid split 
    1018          
    1019          
     1023        //-- compute heurstics 
     1024        //   we take simplified computation for mid split 
     1025                 
    10201026        pOverall = data.mProbability; 
    10211027 
     
    10461052        } 
    10471053 
    1048         //Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl; 
    1049         //Debug << pFront << " " << pBack << " " << pOverall << endl; 
     1054#ifdef _DEBUG 
     1055        Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl; 
     1056        Debug << pFront << " " << pBack << " " << pOverall << endl; 
     1057#endif 
    10501058 
    10511059        const float newCost = pvsBack * pBack + pvsFront * pFront; 
    1052         const float oldCost = (float)pvsSize * pOverall; 
     1060        const float oldCost = (float)pvsSize * pOverall + Limits::Small; 
    10531061 
    10541062        return  (mCtDivCi + newCost) / oldCost; 
     
    10981106        for (int i = 0; i < limit; ++ i) 
    10991107        { 
    1100                 //-- assure that no index is taken twice 
     1108                // the already taken candidates are stored behind maxIdx 
     1109                // => assure that no index is taken twice 
    11011110                const int candidateIdx = (int)RandomValue(0, (Real)(-- maxIdx)); 
    11021111                Polygon3 *poly = (*data.mPolygons)[candidateIdx]; 
     
    11271136                                                                                   &fGeom, &bGeom,  
    11281137                                                                                   pFront, pBack, 
    1129                                                                                    onlyAxisAligned);      
     1138                                                                                   data.mIsKdNode);       
     1139 
     1140        bool isAxisAlignedSplit = false; 
    11301141 
    11311142        if (candidateCost < lowestCost) 
     
    11361147                // assign already computed values 
    11371148                // we can do this because we always save the 
    1138                 // computed values from the axis aligned splits 
    1139                  
     1149                // computed values from the axis aligned splits          
    11401150                frontData.mGeometry = fGeom; 
    11411151                backData.mGeometry = bGeom; 
     
    11461156                //! error also computed if cost ratio is missed 
    11471157                ++ mStat.splits[axis]; 
     1158                isAxisAlignedSplit = true; 
    11481159        } 
    11491160        else 
     
    11521163                DEL_PTR(bGeom); 
    11531164        } 
     1165 
     1166        frontData.mIsKdNode = backData.mIsKdNode = (data.mIsKdNode && isAxisAlignedSplit); 
    11541167 
    11551168#ifdef _DEBUG 
     
    12951308                const int cf = rayInf.ComputeRayIntersection(candidatePlane, t); 
    12961309 
    1297         if (cf >= 0) 
    1298                         ++ raysFront; 
    1299                 if (cf <= 0) 
    1300                         ++ raysBack; 
     1310        if (0) 
     1311                { 
     1312                        if (cf >= 0) 
     1313                                ++ raysFront; 
     1314                        if (cf <= 0) 
     1315                                ++ raysBack; 
     1316                } 
    13011317 
    13021318                if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) 
     
    13681384 
    13691385        // normalize cost by sum of linear factors 
    1370         return cost / (float)mCostNormalizer; 
     1386        if(0) 
     1387                return cost / (float)mCostNormalizer; 
     1388        else 
     1389                return cost; 
    13711390} 
    13721391 
     
    16441663                                BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
    16451664                         
    1646                                 if (!mViewCellsManager->CheckValidity(viewCell, 0.0, mViewCellsManager->GetMaxPvsRatio())) 
     1665                                if (!mViewCellsManager->CheckValidity(viewCell, 
     1666                                                                                                          mViewCellsManager->GetMinPvsSize(),  
     1667                                                                                                          mViewCellsManager->GetMaxPvsSize())) 
    16471668                                { 
    16481669                                        vector<BspLeaf *>::const_iterator it, it_end = viewCell->mLeaves.end(); 
     
    16811702                } 
    16821703        } 
     1704 
     1705        Debug << "invalid leaves: " << mStat.invalidLeaves << endl; 
    16831706} 
    16841707 
     
    19181941 
    19191942int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, 
    1920                                                    const bool onlyUnmailed) const 
     1943                                                          const bool onlyUnmailed) const 
    19211944{ 
    19221945        stack<bspNodePair> nodeStack; 
     
    19371960        nodeStack.push(bspNodePair(mRoot, rgeom)); 
    19381961 
    1939         Debug << "here77" << endl; 
    1940  
    19411962        while (!nodeStack.empty()) 
    19421963        { 
    19431964                BspNode *node = nodeStack.top().first; 
    19441965                BspNodeGeometry *geom = nodeStack.top().second; 
    1945  
     1966         
    19461967                nodeStack.pop(); 
    19471968 
    19481969                if (node->IsLeaf()) 
    1949                 {        
    1950                         Debug << "here22" << endl; 
     1970                { 
    19511971                        // test if this leaf is in valid view space 
    19521972                        if (node->TreeValid() && 
     
    19541974                                (!onlyUnmailed || !node->Mailed())) 
    19551975                        { 
    1956                                 Debug << "here" << endl; 
    19571976                                bool isAdjacent = true; 
    1958 #if 1 
    1959  
    1960                                 // test all planes of current node if still adjacent 
    1961                                 for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i) 
     1977 
     1978                                if (1) 
    19621979                                { 
    1963                                         const int cf = 
    1964                                                 Polygon3::ClassifyPlane(geom->mPolys, 
    1965                                                                                                 halfSpaces[i], 
    1966                                                                                                 mEpsilon); 
    1967  
    1968                                         if (cf == Polygon3::BACK_SIDE) 
     1980                                        // test all planes of current node if still adjacent 
     1981                                        for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i) 
    19691982                                        { 
    1970                                                 isAdjacent = false; 
     1983                                                const int cf = 
     1984                                                        Polygon3::ClassifyPlane(geom->mPolys, 
     1985                                                                                                        halfSpaces[i], 
     1986                                                                                                        mEpsilon); 
     1987 
     1988                                                if (cf == Polygon3::BACK_SIDE) 
     1989                                                { 
     1990                                                        isAdjacent = false; 
     1991                                                } 
    19711992                                        } 
    19721993                                } 
    1973 #else // TODO: why is this wrong?? 
    1974                                 // test all planes of current node if still adjacent 
    1975                                 for (int i = 0; (i < (int)nodeGeom.mPolys.size()) && isAdjacent; ++ i) 
     1994                                else 
    19761995                                { 
    1977                                         Debug << "here33" << endl; 
    1978                                         Polygon3 *poly = nodeGeom.mPolys[i]; 
    1979  
    1980                                         const int cf = 
    1981                                                 Polygon3::ClassifyPlane(geom->mPolys, 
    1982                                                                                                 poly->GetSupportingPlane(), 
    1983                                                                                                 mEpsilon); 
    1984  
    1985                                         if (cf == Polygon3::BACK_SIDE) 
     1996                                        // TODO: why is this wrong?? 
     1997                                        // test all planes of current node if still adjacent 
     1998                                        for (int i = 0; (i < (int)nodeGeom.mPolys.size()) && isAdjacent; ++ i) 
    19861999                                        { 
    1987                                                 isAdjacent = false; 
     2000                                                Polygon3 *poly = nodeGeom.mPolys[i]; 
     2001 
     2002                                                const int cf = 
     2003                                                        Polygon3::ClassifyPlane(geom->mPolys, 
     2004                                                                                                        poly->GetSupportingPlane(), 
     2005                                                                                                        mEpsilon); 
     2006 
     2007                                                if (cf == Polygon3::BACK_SIDE) 
     2008                                                { 
     2009                                                        isAdjacent = false; 
     2010                                                } 
    19882011                                        } 
    19892012                                } 
    1990 #endif 
    1991                                 Debug << "here2" << endl; 
    19922013                                // neighbor was found 
    19932014                                if (isAdjacent) 
     2015                                {        
    19942016                                        neighbors.push_back(dynamic_cast<BspLeaf *>(node)); 
     2017                                } 
     2018                        } 
     2019                } 
     2020                else 
     2021                { 
     2022                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     2023 
     2024                        const int cf = Polygon3::ClassifyPlane(nodeGeom.mPolys, 
     2025                                                                                                   interior->GetPlane(), 
     2026                                                                                                   mEpsilon); 
     2027                         
     2028                        BspNode *front = interior->GetFront(); 
     2029                        BspNode *back = interior->GetBack(); 
     2030             
     2031                        BspNodeGeometry *fGeom = new BspNodeGeometry(); 
     2032                        BspNodeGeometry *bGeom = new BspNodeGeometry(); 
     2033 
     2034                        geom->SplitGeometry(*fGeom, 
     2035                                                                *bGeom, 
     2036                                                                interior->GetPlane(), 
     2037                                                                mBox, 
     2038                                                                mEpsilon); 
     2039                 
     2040                        if (cf == Polygon3::FRONT_SIDE) 
     2041                        { 
     2042                                nodeStack.push(bspNodePair(interior->GetFront(), fGeom)); 
     2043                                DEL_PTR(bGeom); 
    19952044                        } 
    19962045                        else 
    19972046                        { 
    1998                                 BspInterior *interior = dynamic_cast<BspInterior *>(node); 
    1999  
    2000                                 const int cf = Polygon3::ClassifyPlane(nodeGeom.mPolys, 
    2001                                                                                                            interior->GetPlane(), 
    2002                                                                                                            mEpsilon); 
    2003                          
    2004                                 BspNode *front = interior->GetFront(); 
    2005                                 BspNode *back = interior->GetBack(); 
    2006              
    2007                                 BspNodeGeometry *fGeom = new BspNodeGeometry(); 
    2008                                 BspNodeGeometry *bGeom = new BspNodeGeometry(); 
    2009  
    2010                                 geom->SplitGeometry(*fGeom, 
    2011                                                                         *bGeom, 
    2012                                                                         interior->GetPlane(), 
    2013                                                                         mBox, 
    2014                                                                         mEpsilon); 
    2015                  
    2016                                 if (cf == Polygon3::FRONT_SIDE) 
     2047                                if (cf == Polygon3::BACK_SIDE) 
    20172048                                { 
    2018                                         nodeStack.push(bspNodePair(interior->GetFront(), fGeom)); 
    2019                                         DEL_PTR(bGeom); 
     2049                                        nodeStack.push(bspNodePair(interior->GetBack(), bGeom)); 
     2050                                        DEL_PTR(fGeom); 
    20202051                                } 
    20212052                                else 
    2022                                 { 
    2023                                         if (cf == Polygon3::BACK_SIDE) 
    2024                                         { 
    2025                                                 nodeStack.push(bspNodePair(interior->GetBack(), bGeom)); 
    2026                                                 DEL_PTR(fGeom); 
    2027                                         } 
    2028                                         else 
    2029                                         {       // random decision 
    2030                                                 nodeStack.push(bspNodePair(front, fGeom)); 
    2031                                                 nodeStack.push(bspNodePair(back, bGeom)); 
    2032                                         } 
     2053                                {       // random decision 
     2054                                        nodeStack.push(bspNodePair(front, fGeom)); 
     2055                                        nodeStack.push(bspNodePair(back, bGeom)); 
    20332056                                } 
    20342057                        } 
    20352058                } 
    2036  
     2059         
    20372060                DEL_PTR(geom); 
    20382061        } 
     
    27922815        mergeStats.Start(); 
    27932816        // TODO: REMOVE LATER for performance! 
    2794         const bool showMergeStats = true; 
     2817        const bool showMergeStats = false; 
     2818 
    27952819        //BspMergeCandidate::sOverallCost = mBox.SurfaceArea() * mStat.maxPvs; 
    27962820        long startTime = GetTime(); 
     
    28082832                mergeStats.nodes = CollectMergeCandidates(leaves); 
    28092833        } 
    2810          
     2834                 
    28112835 
    28122836        mergeStats.collectTime = TimeDiff(startTime, GetTime()); 
     
    28142838        startTime = GetTime(); 
    28152839 
     2840        // number of view cells withouth the invalid ones 
    28162841        int nViewCells = mStat.Leaves() - mStat.invalidLeaves; 
    2817         int pass = 0; 
    2818         const int nextPass = 200; 
    2819  
    2820         cout << "starting merge ... "; 
     2842        Debug << "number of view cells taken into account: " << nViewCells << endl; 
     2843        // pass is needed for statistics. the last n passes are 
     2844        // recorded 
     2845        const int maxPasses = 1000; 
     2846        int pass = max(nViewCells - mMergeMinViewCells - maxPasses, 0); 
     2847        const int nextPass = 50; 
     2848 
     2849        cout << "starting merge ... " << endl; 
    28212850 
    28222851        //-- use priority queue to merge leaf pairs 
     
    28252854                    mMergeMaxCostRatio * BspMergeCandidate::sOverallCost)) 
    28262855        { 
    2827                 /*Debug << "abs mergecost: " << mMergeQueue.top().GetMergeCost() << " rel mergecost: " 
     2856#ifdef _DEBUG 
     2857                Debug << "abs mergecost: " << mMergeQueue.top().GetMergeCost() << " rel mergecost: " 
    28282858                          << mMergeQueue.top().GetMergeCost() / BspMergeCandidate::sOverallCost  
    2829                           << " max ratio: " << mMergeMaxCostRatio << endl;*/ 
     2859                          << " max ratio: " << mMergeMaxCostRatio << endl; 
     2860#endif 
     2861 
    28302862                BspMergeCandidate mc = mMergeQueue.top(); 
    28312863                mMergeQueue.pop(); 
     
    28412873 
    28422874                        MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2()); 
    2843                                                  
    2844                          
     2875                                         
    28452876                        // increase absolute merge cost 
    28462877                        BspMergeCandidate::sOverallCost += mc.GetMergeCost(); 
    28472878                         
     2879 
     2880                        -- nViewCells; 
     2881                        ++ mergeStats.merged; 
    28482882 
    28492883                        if (showMergeStats) 
     
    28582892                                mergeStats.accTreeDist += dist; 
    28592893 
    2860  
    2861                                 if (((mergeStats.merged % nextPass) == 0) || (nViewCells == mMergeMinViewCells)) 
     2894                                //Debug << "viewcells: " << nViewCells << " mergemin " << mMergeMinViewCells << endl; 
     2895                                if ((mergeStats.merged == pass) || (nViewCells == mMergeMinViewCells)) 
    28622896                                { 
     2897                                        pass += nextPass; 
    28632898                                        mStats  
    28642899                                                << "#Pass\n" << pass ++ << endl 
    28652900                                                << "#Merged\n" << mergeStats.merged << endl  
    28662901                                                << "#Viewcells\n" << nViewCells << endl  
    2867                                                 << "#OverallCost\n" << BspMergeCandidate::sOverallCost << endl  
     2902                                                << "#OverallCost\n" << BspMergeCandidate::sOverallCost << endl 
    28682903                                                << "#CurrentCost\n" << mergeCost << endl 
    2869                                                 << "#RelCost\n" << mc.GetMergeCost() / BspMergeCandidate::sOverallCost << endl 
     2904                                                << "#RelativeCost\n" << mergeCost / BspMergeCandidate::sOverallCost << endl 
    28702905                                                << "#CurrentPvs\n" << mc.GetLeaf1()->GetViewCell()->GetPvs().GetSize() << endl; 
    2871  
    2872                                         cout << "exporting view cells ... "; 
    2873                                                                  
    2874                                  
     2906                                         
    28752907                                        ViewCellContainer::const_iterator vit, vit_end = viewCells.end(); 
    28762908                                         
     
    28822914                                                while (!viewCells.empty() && (viewCells.back()->GetId() == -2)) 
    28832915                                                { 
    2884                                                         DEL_PTR(viewCells.back()); 
     2916                                                        //DEL_PTR(viewCells.back()); 
    28852917                                                        viewCells.pop_back(); 
    28862918                                                } 
     
    28972929                                        } 
    28982930                                 
    2899                                         cout << "finished" << endl; 
    2900                                         // not part of any leaf 
    2901                                         //CLEAR_CONTAINER(mOldViewCells); 
    2902                                         mOldViewCells.clear(); 
    2903  
    2904                                         int newVcSize = (int)mNewViewCells.size(); 
    2905                                         // add new view cells to container 
     2931                                        int newVcSize = 0; 
     2932                                        // add new view cells to container only if they don't have been  
     2933                                        // merged in the mean time 
    29062934                                        while (!mNewViewCells.empty()) 
    29072935                                        { 
    2908                                                 viewCells.push_back(mNewViewCells.back()); 
     2936                                                if (mNewViewCells.back()->GetId() != -2) 
     2937                                                { 
     2938                                                        viewCells.push_back(mNewViewCells.back()); 
     2939                                                        ++ newVcSize; 
     2940                                                } 
     2941 
    29092942                                                mNewViewCells.pop_back(); 
    29102943                                        } 
     2944 
     2945                                        // delete the view cells which were merged 
     2946                                        CLEAR_CONTAINER(mOldViewCells); 
    29112947 
    29122948                                        char s[64]; 
     
    29162952                                        if (exporter) 
    29172953                                        { 
     2954                                                cout << "exporting " << nViewCells << " merged view cells ... "; 
    29182955                                                exporter->ExportGeometry(objects); 
    2919                                                 Debug << "vc size " << (int)viewCells.size() << " merge queue size: " << (int)mMergeQueue.size() << endl; 
     2956                                                //Debug << "vc size " << (int)viewCells.size() << " merge queue size: " << (int)mMergeQueue.size() << endl; 
    29202957                                                ViewCellContainer::const_iterator it, it_end = viewCells.end(); 
    29212958                 
    2922                                                 // new view cells are on the back 
    29232959                                                int i = 0; 
    29242960                                                for (it = viewCells.begin(); it != it_end; ++ it) 
     
    29262962                                                        Material m; 
    29272963                                                        // assign special material to new view cells 
    2928                                                         if (i >= (viewCells.size() - newVcSize)) 
     2964                                                        // new view cells are on the back of container 
     2965                                                        if (i ++ >= (viewCells.size() - newVcSize)) 
    29292966                                                        { 
    29302967                                                                //m = RandomMaterial(); 
    2931                                                                 m.mDiffuseColor.r = RandomValue(0.5, 1.0); 
    2932                                                                 m.mDiffuseColor.g = RandomValue(0.5, 1.0); 
    2933                                                                 m.mDiffuseColor.b = RandomValue(0.5, 1.0); 
     2968                                                                m.mDiffuseColor.r = RandomValue(0.5f, 1.0f); 
     2969                                                                m.mDiffuseColor.g = RandomValue(0.5f, 1.0f); 
     2970                                                                m.mDiffuseColor.b = RandomValue(0.5f, 1.0f); 
    29342971                                                        } 
    29352972                                                        else 
    29362973                                                        { 
    2937                                                                 float col = RandomValue(0.1, 0.4); 
     2974                                                                float col = RandomValue(0.1f, 0.4f); 
    29382975                                                                m.mDiffuseColor.r = col; 
    29392976                                                                m.mDiffuseColor.g = col; 
     
    29442981                                                        mViewCellsManager->ExportVcGeometry(exporter, *it); 
    29452982                                                } 
     2983                                                delete exporter; 
     2984                                                cout << "finished" << endl; 
     2985                                        } 
    29462986                                         
    2947                                                 delete exporter; 
    2948                                         } 
    2949                                         cout << "finished " << endl; 
    2950  
    29512987                                } 
    29522988                        } 
    2953  
    2954                         -- nViewCells; 
    2955                         ++ mergeStats.merged; 
     2989                        mNewViewCells.clear(); 
    29562990                } 
    29572991                // merge candidate not valid, because one of the leaves was already 
     
    29642998        } 
    29652999 
    2966  
     3000        // view cells which were merged and are not part of any leaf 
     3001        CLEAR_CONTAINER(mOldViewCells); 
     3002         
    29673003        cout << "finished merge" << endl; 
    29683004        mergeStats.mergeTime = TimeDiff(startTime, GetTime()); 
Note: See TracChangeset for help on using the changeset viewer.