Ignore:
Timestamp:
01/16/06 03:23:29 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r538 r542  
    278278 
    279279        int numObj = 0; 
     280 
    280281        //-- extract polygons intersected by the rays 
    281282        for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 
     
    313314        mMaxPvs = (int)(mMaxPvsRatio * (float)numObj); 
    314315         
    315         Debug << "maximal pvs (i.e., view cell considered as valid: " << mMaxPvs << endl; 
     316        Debug << "maximal pvs (i.e., pvs still considered as valid) : " << mMaxPvs << endl; 
    316317        //-- store rays 
    317318        for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 
     
    382383 
    383384        mStat.Start(); 
    384         cout << "Contructing vsp bsp tree ... "; 
    385  
    386         long startTime = GetTime(); 
    387          
    388         bool mOutOfMemory = false; 
     385        cout << "Contructing vsp bsp tree ... \n"; 
     386 
     387        long startTime = GetTime();      
     388        long interTime = GetTime();      
     389        mOutOfMemory = false; 
     390 
     391        int nleaves = 500; 
    389392 
    390393        while (!tStack.empty()) 
     
    404407                } 
    405408 
    406                 // subdivide leaf node 
     409                        // subdivide leaf node 
    407410                BspNode *r = Subdivide(tStack, tData); 
    408411 
    409412                if (r == mRoot) 
    410413                        Debug << "VSP BSP tree construction time spent at root: " 
    411                                   << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl << endl; 
    412         } 
    413  
    414         Debug << "Used Memory: " << GetMemUsage() << " MB" << endl; 
     414                                  << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
     415 
     416                if (mStat.Leaves() >= nleaves) 
     417                { 
     418                        nleaves += 500; 
     419                                 
     420                        cout << "leaves=" << mStat.Leaves() << endl; 
     421                        Debug << "needed " 
     422                          << TimeDiff(interTime, GetTime())*1e-3 << " secs to create 500 leaves" << endl; 
     423                        interTime = GetTime(); 
     424                } 
     425        } 
     426 
     427        Debug << "Used Memory: " << GetMemUsage() << " MB" << endl << endl; 
    415428        cout << "finished\n"; 
    416429 
     
    436449        BspNode *newNode = tData.mNode; 
    437450 
    438         if (!mOutOfMemory || !TerminationCriteriaMet(tData)) 
     451        if (!mOutOfMemory && !TerminationCriteriaMet(tData)) 
    439452        { 
    440453                PolygonContainer coincident; 
     
    508521 
    509522 
     523 
     524 
    510525BspNode *VspBspTree::SubdivideNode(VspBspTraversalData &tData, 
    511526                                                                   VspBspTraversalData &frontData, 
     
    517532        // select subdivision plane 
    518533        Plane3 splitPlane; 
     534         
    519535        int maxCostMisses = tData.mMaxCostMisses; 
    520         bool success = SelectPlane(splitPlane,  
    521                                                            leaf,  
    522                                                            tData, 
    523                                                            frontData, 
    524                                                            backData); 
     536 
     537        const bool success =  
     538                SelectPlane(splitPlane, leaf, tData, frontData, backData); 
     539 
    525540        if (!success) 
    526541        { 
     
    578593        if (mPvsUseArea) 
    579594        { 
    580                 // if not already computed 
     595                // if geometry was not already computed 
    581596                if (!frontData.mGeometry && !backData.mGeometry) 
    582597                { 
     
    820835                                                                                 BspNodeGeometry **backGeom, 
    821836                                                                                 float &frontArea, 
    822                                                                                  float &backArea) 
     837                                                                                 float &backArea, 
     838                                                                                 bool useKdSplit) 
    823839{ 
    824840        const bool useCostHeuristics = false; 
     
    838854        AxisAlignedBox3 box; 
    839855        box.Initialize(); 
    840          
     856        //TODO: for kd split geometry already is box 
    841857        if (1 && mPvsUseArea) 
    842858        { 
     
    868884                                Vector3 normal(0,0,0); normal[axis] = 1; 
    869885                                 
    870                                 nCostRatio[axis] = SplitPlaneCost(Plane3(normal, nPosition[axis]),  
    871                                                                                                   tData, *nFrontGeom[axis], *nBackGeom[axis], 
    872                                                                                                   nFrontArea[axis], nBackArea[axis]); 
     886                                if (useKdSplit) 
     887                                { 
     888                                        nCostRatio[axis] = EvalAxisAlignedSplitCost(tData, 
     889                                                                                                                                box, 
     890                                                                                                                                axis, 
     891                                                                                                                                nPosition[axis]); 
     892                                         
     893                                        Vector3 pos = box.Max(); pos[axis] = nPosition[axis]; 
     894                                        AxisAlignedBox3 frontBox(box.Min(), pos); 
     895                                        frontBox.ExtractPolys(nFrontGeom[axis]->mPolys); 
     896                                         
     897                                        pos = box.Min(); pos[axis] = nPosition[axis]; 
     898                                        AxisAlignedBox3 backBox(pos, box.Max()); 
     899                                        backBox.ExtractPolys(nBackGeom[axis]->mPolys); 
     900 
     901                                } 
     902                                else 
     903                                { 
     904                                        nCostRatio[axis] = SplitPlaneCost(Plane3(normal, nPosition[axis]),  
     905                                                                                                          tData, *nFrontGeom[axis], *nBackGeom[axis], 
     906                                                                                                          nFrontArea[axis], nBackArea[axis]); 
     907                                } 
    873908                        } 
    874909                        else 
     
    903938        *frontGeom = nFrontGeom[bestAxis]; 
    904939        *backGeom = nBackGeom[bestAxis]; 
     940 
    905941        // and delete other geometry 
    906942        delete nFrontGeom[(bestAxis + 1) % 3]; 
     
    913949        return nCostRatio[bestAxis]; 
    914950} 
     951 
     952 
     953float VspBspTree::EvalAxisAlignedSplitCost(const VspBspTraversalData &data, 
     954                                                                                   const AxisAlignedBox3 &box, 
     955                                                                                   const int axis, 
     956                                                                                   const float &position) const 
     957{ 
     958        int pvsFront = 0; 
     959        int pvsBack = 0; 
     960        int pvsTotal = 0; 
     961 
     962        // create unique ids for pvs heuristics 
     963        GenerateUniqueIdsForPvs(); 
     964 
     965        const int pvsSize = data.mPvs; 
     966 
     967        // this is the main ray classification loop! 
     968        for(RayInfoContainer::iterator ri = data.mRays->begin(); 
     969                ri != data.mRays->end(); ++ ri) 
     970        { 
     971                if (!(*ri).mRay->IsActive()) 
     972                        continue; 
     973 
     974                // determine the side of this ray with respect to the plane 
     975                float t; 
     976                int side = (*ri).ComputeRayIntersection(axis, position, t); 
     977         
     978                AddObjToPvs((*ri).mRay->mTerminationObject, side, pvsFront, pvsBack, pvsTotal); 
     979                AddObjToPvs((*ri).mRay->mOriginObject, side, pvsFront, pvsBack, pvsTotal); 
     980        } 
     981 
     982        //-- only one of these options should be one 
     983        //-- pvs + probability heuristics 
     984        float pBack, pFront, pOverall; 
     985 
     986        if (1) 
     987        { 
     988                // box length substitute for probability 
     989                const float minBox = box.Min(axis); 
     990                const float maxBox = box.Max(axis); 
     991 
     992                pBack = position - minBox; 
     993                pFront = maxBox - position; 
     994                pOverall = maxBox - minBox; 
     995        } 
     996        else //-- area substitute for probability 
     997        { 
     998                pOverall = box.SurfaceArea(); 
     999 
     1000                const bool useMidSplit = true; 
     1001                //const bool useMidSplit = false;        
     1002                                         
     1003                //-- simplified computation for mid split 
     1004                const int axis2 = (axis + 1) % 3; 
     1005                const int axis3 = (axis + 2) % 3; 
     1006 
     1007                const float faceArea =  
     1008                        (box.Max(axis2) - box.Min(axis2)) * 
     1009                        (box.Max(axis3) - box.Min(axis3)); 
     1010 
     1011                pBack = pFront = pOverall * 0.5f + faceArea; 
     1012        } 
     1013 
     1014        //Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl; 
     1015        //Debug << pFront << " " << pBack << " " << pOverall << endl; 
     1016 
     1017        // float sum = raysBack*(position - minBox) + raysFront*(maxBox - position); 
     1018        const float newCost = pvsBack * pBack + pvsFront * pFront; 
     1019        //  float oldCost = leaf->mRays.size(); 
     1020        const float oldCost = (float)pvsSize * pOverall; 
     1021 
     1022        return  (mCtDivCi + newCost) / oldCost; 
     1023} 
     1024 
    9151025 
    9161026 
     
    9841094        candidateCost = SelectAxisAlignedPlane(plane, data, axis, 
    9851095                                                                                   &fGeom, &bGeom,  
    986                                                                                    fArea, bArea);                                                 
     1096                                                                                   fArea, bArea, 
     1097                                                                                   onlyAxisAligned);      
    9871098 
    9881099        if (candidateCost < lowestCost) 
     
    9911102                lowestCost = candidateCost; 
    9921103 
    993                 //-- assign already computed values 
     1104                // assign already computed values 
     1105                // we can do this because we always save the 
     1106                // computed values from the axis aligned splits 
    9941107                frontData.mGeometry = fGeom; 
    9951108                backData.mGeometry = bGeom; 
     
    11231236        int totalPvs = 0; 
    11241237 
     1238        int limit; 
     1239        bool useRand; 
     1240 
     1241        // choose test rays randomly if too much 
     1242        if ((int)data.mRays->size() > mMaxTests) 
     1243        { 
     1244                useRand = true; 
     1245                limit = mMaxTests; 
     1246        } 
     1247        else 
     1248        { 
     1249                useRand = false; 
     1250                limit = (int)data.mRays->size(); 
     1251        } 
     1252         
     1253        for (int i = 0; i < limit; ++ i) 
     1254        { 
     1255                const int testIdx = useRand ?  
     1256                        (int)RandomValue(0, (Real)((int)data.mRays->size() - 1)) : i; 
     1257                RayInfo rayInf = (*data.mRays)[testIdx]; 
     1258 
     1259                float t; 
     1260                VssRay *ray = rayInf.mRay; 
     1261                const int cf = rayInf.ComputeRayIntersection(candidatePlane, t); 
     1262 
     1263        if (cf >= 0) 
     1264                        ++ raysFront; 
     1265                if (cf <= 0) 
     1266                        ++ raysBack; 
     1267 
     1268                if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) 
     1269                { 
     1270                        sumBalancedRays += cf; 
     1271                } 
     1272 
     1273                if (mSplitPlaneStrategy & BALANCED_RAYS) 
     1274                { 
     1275                        if (cf == 0) 
     1276                                ++ sumRaySplits; 
     1277                } 
     1278 
     1279                if (mSplitPlaneStrategy & PVS) 
     1280                { 
     1281                        // find front and back pvs for origing and termination object 
     1282                        AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs); 
     1283                        AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs); 
     1284                } 
     1285        } 
     1286 
     1287        const float raysSize = (float)data.mRays->size() + Limits::Small; 
    11251288        if (mSplitPlaneStrategy & PVS) 
    11261289        { 
     
    11361299                                                                                  mBox, 
    11371300                                                                                  mEpsilon); 
    1138  
     1301#if 0 
     1302                        AxisAlignedBox3 fbox; 
     1303                        AxisAlignedBox3 bbox; 
     1304                        AxisAlignedBox3 box; 
     1305                        fbox.Initialize(); 
     1306                        bbox.Initialize(); 
     1307                        box.Initialize(); 
     1308 
     1309                        geomFront.IncludeInBox(fbox); 
     1310                        geomBack.IncludeInBox(bbox); 
     1311                        data.mGeometry->IncludeInBox(box); 
     1312 
     1313                        areaFront = fbox.GetVolume(); 
     1314                        areaBack = bbox.GetVolume(); 
     1315 
     1316                        pOverall = box.GetVolume(); 
     1317#else 
    11391318                        areaFront = geomFront.GetArea(); 
    11401319                        areaBack = geomBack.GetArea(); 
    11411320 
     1321                        pOverall = data.mArea; 
     1322#endif 
    11421323                        pBack = areaBack; 
    11431324                        pFront = areaFront; 
    1144                         pOverall = data.mArea; 
    11451325                } 
    11461326                else // use number of rays to approximate volume 
     
    11521332        } 
    11531333 
    1154         int limit; 
    1155         bool useRand; 
    1156  
    1157         // choose test rays randomly if too much 
    1158         if ((int)data.mRays->size() > mMaxTests) 
    1159         { 
    1160                 useRand = true; 
    1161                 limit = mMaxTests; 
    1162         } 
    1163         else 
    1164         { 
    1165                 useRand = false; 
    1166                 limit = (int)data.mRays->size(); 
    1167         } 
    1168          
    1169         for (int i = 0; i < limit; ++ i) 
    1170         { 
    1171                 const int testIdx = useRand ?  
    1172                         (int)RandomValue(0, (Real)((int)data.mRays->size() - 1)) : i; 
    1173                 RayInfo rayInf = (*data.mRays)[testIdx]; 
    1174  
    1175                 float t; 
    1176                 VssRay *ray = rayInf.mRay; 
    1177                 const int cf = rayInf.ComputeRayIntersection(candidatePlane, t); 
    1178  
    1179         if (cf >= 0) 
    1180                         ++ raysFront; 
    1181                 if (cf <= 0) 
    1182                         ++ raysBack; 
    1183  
    1184                 if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) 
    1185                 { 
    1186                         sumBalancedRays += cf; 
    1187                 } 
    1188  
    1189                 if (mSplitPlaneStrategy & BALANCED_RAYS) 
    1190                 { 
    1191                         if (cf == 0) 
    1192                                 ++ sumRaySplits; 
    1193                 } 
    1194  
    1195                 if (mSplitPlaneStrategy & PVS) 
    1196                 { 
    1197                         // find front and back pvs for origing and termination object 
    1198                         AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs); 
    1199                         AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs); 
    1200                 } 
    1201         } 
    1202  
    1203         const float raysSize = (float)data.mRays->size() + Limits::Small; 
    12041334 
    12051335        if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) 
     
    14731603} 
    14741604 
     1605 
     1606 
     1607void VspBspTree::CheckValidy() 
     1608{ 
     1609        stack<BspNode *> nodeStack; 
     1610 
     1611        if (!mRoot) 
     1612                return; 
     1613 
     1614        nodeStack.push(mRoot); 
     1615         
     1616        while (!nodeStack.empty())  
     1617        { 
     1618                BspNode *node = nodeStack.top(); 
     1619                nodeStack.pop(); 
     1620                 
     1621                if (node->IsLeaf()) 
     1622                { 
     1623                        BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); 
     1624 
     1625                        if (node->TreeValid()) 
     1626                        { 
     1627                                BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
     1628                         
     1629                                if (viewCell->GetPvs().GetSize() > mMaxPvs) 
     1630                                { 
     1631                                        while (!viewCell->mLeaves.empty()) 
     1632                                        { 
     1633                                                BspLeaf *l = viewCell->mLeaves.back(); 
     1634                                                l->SetTreeValid(false); 
     1635                                                PropagateUpValidity(l); 
     1636 
     1637                                                l->SetViewCell(GetOrCreateOutOfBoundsCell()); 
     1638                                                viewCell->mLeaves.pop_back(); 
     1639                                        } 
     1640 
     1641                                        mOutOfBoundsCell->GetPvs().AddPvs(viewCell->GetPvs()); 
     1642 
     1643                                        DEL_PTR(viewCell); 
     1644                                } 
     1645                        } 
     1646                } 
     1647                else 
     1648                { 
     1649                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     1650                 
     1651                        nodeStack.push(interior->GetFront()); 
     1652                        nodeStack.push(interior->GetBack()); 
     1653                } 
     1654        } 
     1655} 
    14751656 
    14761657void VspBspTree::CollectViewCells(BspNode *root,  
     
    24382619 
    24392620        //-- collect the leaves which haven't been found by ray casting 
    2440         vector<BspLeaf *> leaves; 
    2441         CollectLeaves(leaves, true, mMaxPvs); 
    2442         Debug << "found " << (int)leaves.size() << " new leaves" << endl << endl; 
    2443         // TODO 
    2444         CollectMergeCandidates(leaves); 
     2621        if (0) 
     2622        { 
     2623                vector<BspLeaf *> leaves; 
     2624                CollectLeaves(leaves, true, mMaxPvs); 
     2625                Debug << "found " << (int)leaves.size() << " new leaves" << endl << endl; 
     2626                CollectMergeCandidates(leaves); 
     2627        } 
    24452628 
    24462629        return numLeaves; 
     
    25192702                    mMergeMaxCostRatio * BspMergeCandidate::sOverallCost)) 
    25202703        { 
    2521                 //Debug << "abs mergecost: " << mMergeQueue.top().GetMergeCost() << " rel mergecost: " 
    2522                 //        << mMergeQueue.top().GetMergeCost() / BspMergeCandidate::sOverallCost  
    2523                 //        << " max ratio: " << mMergeMaxCostRatio << endl; 
     2704                /*Debug << "abs mergecost: " << mMergeQueue.top().GetMergeCost() << " rel mergecost: " 
     2705                          << mMergeQueue.top().GetMergeCost() / BspMergeCandidate::sOverallCost  
     2706                          << " max ratio: " << mMergeMaxCostRatio << endl;*/ 
    25242707                BspMergeCandidate mc = mMergeQueue.top(); 
    25252708                mMergeQueue.pop(); 
     
    25322715                { 
    25332716                        ViewCell::NewMail(); 
     2717         
    25342718                        MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2()); 
    25352719                        -- nViewCells; 
     
    27092893 
    27102894        leaf->SetViewCell(vc2); // finally change view cell 
    2711          
    2712         //Debug << "new pvs: " << vc1->GetPvs().GetSize() + vc2->GetPvs().GetSize()  
    2713         //        << " (" << vc1->GetPvs().GetSize() << ", " << vc2->GetPvs().GetSize() << ")" << endl; 
    2714  
    27152895} 
    27162896 
     
    27412921        if (shuffledCost1 < shuffledCost2) 
    27422922        { 
    2743                 //Debug << "old cost: " << oldCost << ", new cost: " << shuffledCost1 << endl; 
    27442923                ShuffleLeaf(leaf1, vc1, vc2); 
    27452924                leaf1->Mail(); 
     
    27472926        else 
    27482927        { 
    2749                 //Debug << "old cost: " << oldCost << ", new cost: " << shuffledCost2 << endl; 
    27502928                ShuffleLeaf(leaf2, vc2, vc1); 
    27512929                leaf2->Mail(); 
     
    28793057 
    28803058        //-- compute ratio of old cost 
    2881         //-- (added size of left and right view cell times pvs size) 
    2882         //-- to new rendering cost (size of merged view cell times pvs size) 
     3059        //   (i.e., added size of left and right view cell times pvs size) 
     3060        //   to new rendering cost (i.e, size of merged view cell times pvs size) 
    28833061        const float oldCost = GetLeaf1Cost() + GetLeaf2Cost(); 
    28843062 
     
    28863064                (float)newPvs * (vc1->GetArea() + vc2->GetArea()); 
    28873065 
    2888         mMergeCost = newCost - oldCost; 
    28893066        if (newPvs > sMaxPvsSize) // strong penalty if pvs size too large 
    2890                 mMergeCost += 1.0; 
    2891 } 
     3067        { 
     3068                mMergeCost = 1e15f; 
     3069        } 
     3070        else 
     3071        { 
     3072                mMergeCost = newCost - oldCost; 
     3073        } 
     3074} 
     3075 
    28923076 
    28933077void BspMergeCandidate::SetLeaf1(BspLeaf *l) 
     
    28963080} 
    28973081 
     3082 
    28983083void BspMergeCandidate::SetLeaf2(BspLeaf *l) 
    28993084{ 
     
    29013086} 
    29023087 
     3088 
    29033089BspLeaf *BspMergeCandidate::GetLeaf1() 
    29043090{ 
     
    29063092} 
    29073093 
     3094 
    29083095BspLeaf *BspMergeCandidate::GetLeaf2() 
    29093096{ 
    29103097        return mLeaf2; 
    29113098} 
     3099 
    29123100 
    29133101bool BspMergeCandidate::Valid() const 
     
    29183106} 
    29193107 
     3108 
    29203109float BspMergeCandidate::GetMergeCost() const 
    29213110{ 
    29223111        return mMergeCost; 
    29233112} 
     3113 
    29243114 
    29253115void BspMergeCandidate::SetValid() 
Note: See TracChangeset for help on using the changeset viewer.