Ignore:
Timestamp:
06/21/06 09:44:39 (18 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r1023 r1027  
    5353void VspTreeStatistics::Print(ostream &app) const 
    5454{ 
    55 #if TODO 
    56         app << "===== BspTree statistics ===============\n"; 
     55        app << "===== VspTree statistics ===============\n"; 
    5756 
    5857        app << setprecision(4); 
     
    6564 
    6665        app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n"; 
    67  
    68         app << "#N_POLYSPLITS ( Number of polygon splits )\n" << polySplits << "\n"; 
    6966 
    7067        app << "#AXIS_ALIGNED_SPLITS (number of axis aligned splits)\n" << splits[0] + splits[1] + splits[2] << endl; 
     
    10097        app << "#AVGDEPTH ( average depth )\n" << AvgDepth() << endl; 
    10198 
    102         app << "#N_INPUTPOLYGONS (number of input polygons )\n" << polys << endl; 
    103  
    10499        app << "#N_INVALIDLEAVES (number of invalid leaves )\n" << invalidLeaves << endl; 
    105100 
     
    107102        //app << "#N_PVS: " << pvs << endl; 
    108103 
    109         app << "#N_ROUTPUT_INPUT_POLYGONS ( ratio polygons after subdivision / input polygons )\n" << 
    110                  (polys + polySplits) / (double)polys << endl; 
    111          
    112         app << "===== END OF BspTree statistics ==========\n"; 
    113 #endif 
     104        app << "===== END OF VspTree statistics ==========\n"; 
    114105} 
    115106 
     
    267258        return Interior; 
    268259} 
     260 
     261 
    269262 
    270263/****************************************************************/ 
     
    449442{ 
    450443        mVspStats.nodes = 1; 
    451         mBoundingBox.Initialize();      // initialise vsp tree bounding box 
    452  
     444         
    453445        if (forcedBoundingBox) 
     446        { 
    454447                mBoundingBox = *forcedBoundingBox; 
    455          
    456         VssRayContainer::const_iterator rit, rit_end = sampleRays.end(); 
    457  
    458         Intersectable::NewMail(); 
    459  
    460         //-- compute bounding box 
    461         for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 
    462         { 
    463                 VssRay *ray = *rit; 
    464  
    465                 // compute bounding box of view space 
    466                 if (!forcedBoundingBox) 
    467                 { 
     448        } 
     449        else // compute vsp tree bounding box 
     450        { 
     451                mBoundingBox.Initialize(); 
     452 
     453                VssRayContainer::const_iterator rit, rit_end = sampleRays.end(); 
     454 
     455                //-- compute bounding box 
     456        for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 
     457                { 
     458                        VssRay *ray = *rit; 
     459 
     460                        // compute bounding box of view space 
    468461                        mBoundingBox.Include(ray->GetTermination()); 
    469462                        mBoundingBox.Include(ray->GetOrigin()); 
    470463                } 
    471         } 
    472  
    473         mTermMinProbability *= mBoundingBox.GetVolume(); 
    474         mGlobalCostMisses = 0; 
     464 
     465                mTermMinProbability *= mBoundingBox.GetVolume(); 
     466                mGlobalCostMisses = 0; 
     467        } 
    475468} 
    476469 
     
    480473                                                                  const float splitCandidateCost, 
    481474                                                                  const float totalRenderCost, 
    482                                                                         const float avgRenderCost) 
     475                                                                  const float avgRenderCost) 
    483476{ 
    484477        mSubdivisionStats  
     
    507500{ 
    508501        return 
     502#if TODO 
    509503                (((int)data.mRays->size() <= mTermMinRays) || 
    510504                 (data.mPvs <= mTermMinPvs)   || 
     
    512506                 (data.GetAvgRayContribution() > mTermMaxRayContribution) || 
    513507                 (data.mDepth >= mTermMaxDepth)); 
     508#else 
     509                false; 
     510#endif 
    514511} 
    515512 
     
    518515{ 
    519516        return 
     517#if TODO 
    520518                (mOutOfMemory ||  
    521519                (mVspStats.Leaves() >= mMaxViewCells) ||  
    522520                (mGlobalCostMisses >= mTermGlobalCostMissTolerance)); 
     521#else 
     522                (mVspStats.Leaves() >= mMaxViewCells) ;          
     523#endif 
    523524} 
    524525 
     
    633634 
    634635void VspTree::EvalSplitCandidate(VspTraversalData &tData, 
    635                                                                  VspSplitCandidate &splitData) 
     636                                                                 VspSplitCandidate &splitCandidate) 
    636637{ 
    637638        float frontProb; 
    638         float backtProb; 
     639        float backProb; 
    639640         
    640641        VspLeaf *leaf = dynamic_cast<VspLeaf *>(tData.mNode); 
     
    642643        // compute locally best split plane 
    643644        const bool success =  
    644                 SelectPlane(tData, splitData.mSplitPlane, 
    645                                         frontProb, backtProb); 
    646  
    647         //TODO 
     645                SelectSplitPlane(tData, splitCandidate.mSplitPlane,     frontProb, backProb); 
     646 
    648647        // compute global decrease in render cost 
    649         splitData.mPriority = EvalRenderCostDecrease(splitData.mSplitPlane, tData); 
    650         splitData.mParentData = tData; 
    651         splitData.mMaxCostMisses = success ? tData.mMaxCostMisses : tData.mMaxCostMisses + 1; 
     648        splitCandidate.mPriority = EvalRenderCostDecrease(splitCandidate.mSplitPlane, tData); 
     649        splitCandidate.mParentData = tData; 
     650        splitCandidate.mMaxCostMisses = success ? tData.mMaxCostMisses : tData.mMaxCostMisses + 1; 
     651 
     652        Debug << "p: " << tData.mNode << " depth: " << tData.mDepth << endl; 
    652653} 
    653654 
     
    673674                          *backData.mRays); 
    674675 
     676        Debug << "f: " << frontData.mRays->size() << " b: " << backData.mRays->size() << "d: " << tData.mRays->size() << endl; 
    675677        //-- compute pvs 
    676678        frontData.mPvs = ComputePvsSize(*frontData.mRays); 
     
    831833 
    832834 
    833 float VspTree::BestCostRatioHeuristics(const RayInfoContainer &rays, 
    834                                                                                   const AxisAlignedBox3 &box, 
    835                                                                                   const int pvsSize, 
    836                                                                                   const int axis, 
    837                                           float &position) 
     835float VspTree::EvalLocalCostHeuristics(const RayInfoContainer &rays, 
     836                                                                           const AxisAlignedBox3 &box, 
     837                                                                           const int pvsSize, 
     838                                                                           const int axis, 
     839                                                                           float &position) 
    838840{ 
    839841        const float minBox = box.Min(axis); 
     
    10031005 
    10041006 
    1005 float VspTree::SelectPlane(const VspTraversalData &tData, 
    1006                                                    AxisAlignedPlane &plane, 
    1007                                                    float &pFront, 
    1008                                                    float &pBack) 
     1007float VspTree::SelectSplitPlane(const VspTraversalData &tData, 
     1008                                                                AxisAlignedPlane &plane, 
     1009                                                                float &pFront, 
     1010                                                                float &pBack) 
    10091011{ 
    10101012        float nPosition[3]; 
     
    10191021        int bestAxis = -1; 
    10201022 
    1021  
    10221023        // if we use some kind of specialised fixed axis 
    10231024    const bool useSpecialAxis =  
    10241025                mOnlyDrivingAxis || mCirculatingAxis; 
    10251026 
     1027        int parentAxis = 0; 
     1028        VspNode *parent = tData.mNode->GetParent(); 
     1029 
     1030        if (parent) 
     1031                parentAxis = dynamic_cast<VspInterior *>(parent)->GetAxis(); 
     1032 
    10261033        if (mCirculatingAxis) 
    1027                 sAxis = (tData.mAxis + 1) % 3; 
     1034                sAxis = (parentAxis + 1) % 3; 
    10281035        else if (mOnlyDrivingAxis) 
    10291036                sAxis = box.Size().DrivingAxis(); 
     
    10391046                        { 
    10401047                                nCostRatio[axis] = 
    1041                                         BestCostRatioHeuristics(*tData.mRays, 
    1042                                                                                     box, 
     1048                                        EvalLocalCostHeuristics(*tData.mRays, 
     1049                                                                                        box, 
    10431050                                                                                        tData.mPvs, 
    10441051                                                                                        axis, 
     
    10491056                                nPosition[axis] = (box.Min()[axis] + box.Max()[axis]) * 0.5f; 
    10501057 
    1051                                 nCostRatio[axis] = EvalSplitCost(tData, 
    1052                                                                                                 box, 
    1053                                                                                                 axis, 
    1054                                                                                                 nPosition[axis], 
    1055                                                                                                 nProbFront[axis],  
    1056                                                                                                 nProbBack[axis]); 
     1058                                nCostRatio[axis] = EvalLocalSplitCost(tData, 
     1059                                                                                                          box, 
     1060                                                                                                          axis, 
     1061                                                                                                          nPosition[axis], 
     1062                                                                                                          nProbFront[axis],  
     1063                                                                                                          nProbBack[axis]); 
    10571064                        } 
    10581065                                                 
     
    10911098                                                                          const VspTraversalData &data) const 
    10921099{ 
     1100#if 1 
     1101        return -data.mDepth; 
     1102#endif 
    10931103        float pvsFront = 0; 
    10941104        float pvsBack = 0; 
     
    11121122                float t; 
    11131123                VssRay *ray = rayInf.mRay; 
     1124 
    11141125                const int cf =  
    11151126                        rayInf.ComputeRayIntersection(candidatePlane.mAxis,  
     
    11201131                AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs); 
    11211132        } 
     1133 
    11221134 
    11231135        AxisAlignedBox3 frontBox; 
     
    11451157        const float renderCostDecrease = (oldRenderCost - newRenderCost) / mBoundingBox.GetVolume(); 
    11461158         
    1147  
    1148 #if 1 
    11491159        // take render cost of node into account  
    11501160        // otherwise danger of being stuck in a local minimum!! 
     1161        const float factor = 0.99f; 
     1162 
    11511163        const float normalizedOldRenderCost = oldRenderCost / mBoundingBox.GetVolume(); 
    1152         return 0.99f * renderCostDecrease + 0.01f * normalizedOldRenderCost; 
    1153 #else 
    1154         return renderCostDecrease; 
    1155 #endif 
    1156 } 
    1157  
    1158  
    1159 float VspTree::EvalSplitCost(const VspTraversalData &data, 
    1160                                                          const AxisAlignedBox3 &box, 
    1161                                                          const int axis, 
    1162                                                          const float &position,                                                                            
    1163                                                          float &pFront, 
    1164                                                          float &pBack) const 
     1164        return factor * renderCostDecrease + (1.0f - factor) * normalizedOldRenderCost; 
     1165} 
     1166 
     1167 
     1168float VspTree::EvalLocalSplitCost(const VspTraversalData &data, 
     1169                                                                  const AxisAlignedBox3 &box, 
     1170                                                                  const int axis, 
     1171                                                                  const float &position, 
     1172                                                                  float &pFront, 
     1173                                                                  float &pBack) const 
    11651174{ 
    11661175        float pvsTotal = 0; 
     
    11901199 
    11911200        //-- compute heurstics 
    1192         //-- we take simplified computation for mid split 
    1193                  
     1201         
    11941202        pOverall = data.mProbability; 
     1203        // we take simplified computation for mid split 
    11951204        pBack = pFront = pOverall * 0.5f; 
    11961205         
     
    12101219 
    12111220void VspTree::AddObjToPvs(Intersectable *obj, 
    1212                                                         const int cf, 
    1213                                                         float &frontPvs, 
    1214                                                         float &backPvs, 
    1215                                                         float &totalPvs) const 
     1221                                                  const int cf, 
     1222                                                  float &frontPvs, 
     1223                                                  float &backPvs, 
     1224                                                  float &totalPvs) const 
    12161225{ 
    12171226        if (!obj) 
     
    13521361        ++ mCreatedViewCells; 
    13531362 
    1354 #ifdef _DEBUG 
     1363//#ifdef _DEBUG 
    13551364        Debug << "BSP stats: " 
    13561365                  << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), " 
    13571366                  << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), " 
    1358           //              << "Area: " << data.mProbability << " (min: " << mTermMinProbability << "), " 
    13591367                  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), " 
    1360                   << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, " 
     1368                  << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "), " 
    13611369                  << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl; 
    1362 #endif 
     1370//#endif 
    13631371} 
    13641372 
     
    16051613                         
    16061614                        if (GetBBox(interior->GetBack()).Side(plane) < 0) 
     1615                        { 
    16071616                                next = interior->GetFront(); 
     1617                        } 
    16081618            else 
     1619                        { 
    16091620                                if (GetBBox(interior->GetFront()).Side(plane) < 0) 
     1621                                { 
    16101622                                        next = interior->GetBack(); 
     1623                                } 
    16111624                                else  
    16121625                                { 
     
    16181631                                        mask = mask >> 1; 
    16191632                                } 
    1620  
    1621                                 nodeStack.push(next); 
     1633                        } 
     1634                         
     1635                        nodeStack.push(next); 
    16221636                } 
    16231637        } 
     
    17041718 
    17051719int VspTree::CastLineSegment(const Vector3 &origin, 
    1706                                                                 const Vector3 &termination, 
    1707                                                                 ViewCellContainer &viewcells) 
     1720                                                         const Vector3 &termination, 
     1721                             ViewCellContainer &viewcells) 
    17081722{ 
    17091723        int hits = 0; 
     
    17221736        VspNode *node = mRoot; 
    17231737        VspNode *farChild; 
     1738 
     1739        float position; 
     1740        int axis; 
     1741 
     1742        while (1) 
     1743        { 
     1744                if (!node->IsLeaf()) 
     1745                { 
     1746                        VspInterior *in = dynamic_cast<VspInterior *>(node); 
     1747                        position = in->GetPosition(); 
     1748                        axis = in->GetAxis(); 
     1749 
     1750                        if (entp[axis] <= position) 
     1751                        { 
     1752                                if (extp[axis] <= position) 
     1753                                { 
     1754                                        node = in->GetFront(); 
     1755                                        // cases N1,N2,N3,P5,Z2,Z3 
     1756                                        continue; 
     1757                                } else 
     1758                                { 
     1759                                        // case N4 
     1760                                        node = in->GetFront(); 
     1761                                        farChild = in->GetBack(); 
     1762                                } 
     1763                        } 
     1764                        else 
     1765                        { 
     1766                                if (position <= extp[axis]) 
     1767                                { 
     1768                                        node = in->GetBack(); 
     1769                                        // cases P1,P2,P3,N5,Z1 
     1770                                        continue; 
     1771                                } 
     1772                                else 
     1773                                { 
     1774                                        node = in->GetBack(); 
     1775                                        farChild = in->GetFront(); 
     1776                                        // case P4 
     1777                                } 
     1778                        } 
     1779 
     1780                        // $$ modification 3.5.2004 - hints from Kamil Ghais 
     1781                        // case N4 or P4 
     1782                        const float tdist = (position - origin[axis]) / dir[axis]; 
     1783                        tStack.push(LineTraversalData(farChild, extp, maxt)); //TODO 
     1784 
     1785                        extp = origin + dir * tdist; 
     1786                        maxt = tdist; 
     1787                } 
     1788                else 
     1789                { 
     1790                        // compute intersection with all objects in this leaf 
     1791                        VspLeaf *leaf = dynamic_cast<VspLeaf *>(node); 
     1792                        ViewCell *vc = leaf->GetViewCell(); 
     1793 
     1794                        if (!vc->Mailed()) 
     1795                        { 
     1796                                vc->Mail(); 
     1797                                viewcells.push_back(vc); 
     1798                                ++ hits; 
     1799                        } 
     1800#if 0 
     1801                        leaf->mRays.push_back(RayInfo(new VssRay(origin, termination, NULL, NULL, 0))); 
     1802#endif 
     1803                        // get the next node from the stack 
     1804                        if (tStack.empty()) 
     1805                                break; 
     1806 
     1807                        entp = extp; 
     1808                        mint = maxt; 
     1809                         
     1810                        LineTraversalData &s  = tStack.top(); 
     1811                        node = s.mNode; 
     1812                        extp = s.mExitPoint; 
     1813                        maxt = s.mMaxT; 
     1814                        tStack.pop(); 
     1815                } 
     1816        } 
     1817 
     1818        return hits; 
     1819} 
     1820 
     1821 
     1822int VspTree::CastRay(Ray &ray) 
     1823{ 
     1824        int hits = 0; 
     1825 
     1826        stack<LineTraversalData> tStack; 
     1827        const Vector3 dir = ray.GetDir(); 
     1828 
     1829        float maxt, mint; 
     1830 
     1831        if (!mBoundingBox.GetRaySegment(ray, mint, maxt)) 
     1832                return 0; 
     1833 
     1834        Intersectable::NewMail(); 
     1835        ViewCell::NewMail(); 
     1836 
     1837        Vector3 entp = ray.Extrap(mint); 
     1838        Vector3 extp = ray.Extrap(maxt); 
     1839 
     1840        const Vector3 origin = entp; 
     1841 
     1842        VspNode *node = mRoot; 
     1843        VspNode *farChild = NULL; 
    17241844 
    17251845        float position; 
     
    17411861                                        // cases N1,N2,N3,P5,Z2,Z3 
    17421862                                        continue; 
    1743                                 } else 
     1863                                }  
     1864                                else 
    17441865                                { 
    17451866                                        // case N4 
     
    17681889                        const float tdist = (position - origin[axis]) / dir[axis]; 
    17691890                        tStack.push(LineTraversalData(farChild, extp, maxt)); //TODO 
    1770  
    17711891                        extp = origin + dir * tdist; 
    17721892                        maxt = tdist; 
     
    17811901                        { 
    17821902                                vc->Mail(); 
    1783                                 viewcells.push_back(vc); 
     1903                                // todo: add view cells to ray 
    17841904                                ++ hits; 
    17851905                        } 
     
    18041924        return hits; 
    18051925} 
    1806  
    1807  
    1808 int VspTree::CastRay(Ray &ray) 
    1809 { 
    1810         int hits = 0; 
    1811  
    1812         stack<LineTraversalData> tStack; 
    1813         const Vector3 dir = ray.GetDir(); 
    1814  
    1815         float maxt, mint; 
    1816  
    1817         if (!mBoundingBox.GetRaySegment(ray, mint, maxt)) 
    1818                 return 0; 
    1819  
    1820         Intersectable::NewMail(); 
    1821         ViewCell::NewMail(); 
    1822  
    1823         Vector3 entp = ray.Extrap(mint); 
    1824         Vector3 extp = ray.Extrap(maxt); 
    1825  
    1826         const Vector3 origin = entp; 
    1827  
    1828         VspNode *node = mRoot; 
    1829         VspNode *farChild = NULL; 
    1830  
    1831         float position; 
    1832         int axis; 
    1833  
    1834         while (1) 
    1835         { 
    1836                 if (!node->IsLeaf()) 
    1837                 { 
    1838                         VspInterior *in = dynamic_cast<VspInterior *>(node); 
    1839                         position = in->GetPosition(); 
    1840                         axis = in->GetAxis(); 
    1841  
    1842                         if (entp[axis] <= position) 
    1843                         { 
    1844                                 if (extp[axis] <= position) 
    1845                                 { 
    1846                                         node = in->GetBack(); 
    1847                                         // cases N1,N2,N3,P5,Z2,Z3 
    1848                                         continue; 
    1849                                 }  
    1850                                 else 
    1851                                 { 
    1852                                         // case N4 
    1853                                         node = in->GetBack(); 
    1854                                         farChild = in->GetFront(); 
    1855                                 } 
    1856                         } 
    1857                         else 
    1858                         { 
    1859                                 if (position <= extp[axis]) 
    1860                                 { 
    1861                                         node = in->GetFront(); 
    1862                                         // cases P1,P2,P3,N5,Z1 
    1863                                         continue; 
    1864                                 } 
    1865                                 else 
    1866                                 { 
    1867                                         node = in->GetFront(); 
    1868                                         farChild = in->GetBack(); 
    1869                                         // case P4 
    1870                                 } 
    1871                         } 
    1872  
    1873                         // $$ modification 3.5.2004 - hints from Kamil Ghais 
    1874                         // case N4 or P4 
    1875                         const float tdist = (position - origin[axis]) / dir[axis]; 
    1876                         tStack.push(LineTraversalData(farChild, extp, maxt)); //TODO 
    1877                         extp = origin + dir * tdist; 
    1878                         maxt = tdist; 
    1879                 } 
    1880                 else 
    1881                 { 
    1882                         // compute intersection with all objects in this leaf 
    1883                         VspLeaf *leaf = dynamic_cast<VspLeaf *>(node); 
    1884                         ViewCell *vc = leaf->GetViewCell(); 
    1885  
    1886                         if (!vc->Mailed()) 
    1887                         { 
    1888                                 vc->Mail(); 
    1889                                 // todo: add view cells to ray 
    1890                                 ++ hits; 
    1891                         } 
    1892 #if 0 
    1893                         leaf->mRays.push_back(RayInfo(new VssRay(origin, termination, NULL, NULL, 0))); 
    1894 #endif 
    1895                         // get the next node from the stack 
    1896                         if (tStack.empty()) 
    1897                                 break; 
    1898  
    1899                         entp = extp; 
    1900                         mint = maxt; 
    1901                          
    1902                         LineTraversalData &s  = tStack.top(); 
    1903                         node = s.mNode; 
    1904                         extp = s.mExitPoint; 
    1905                         maxt = s.mMaxT; 
    1906                         tStack.pop(); 
    1907                 } 
    1908         } 
    1909  
    1910         return hits; 
    1911 } 
    1912  
    1913  
    1914 VspNode *VspTree::CollapseTree(VspNode *node, int &collapsed) 
    1915 { 
    1916 // TODO 
    1917 #if HAS_TO_BE_REDONE 
    1918         if (node->IsLeaf()) 
    1919                 return node; 
    1920  
    1921         VspInterior *interior = dynamic_cast<VspInterior *>(node); 
    1922  
    1923         VspNode *front = CollapseTree(interior->GetFront(), collapsed); 
    1924         VspNode *back = CollapseTree(interior->GetBack(), collapsed); 
    1925  
    1926         if (front->IsLeaf() && back->IsLeaf()) 
    1927         { 
    1928                 VspLeaf *frontLeaf = dynamic_cast<VspLeaf *>(front); 
    1929                 VspLeaf *backLeaf = dynamic_cast<VspLeaf *>(back); 
    1930  
    1931                 //-- collapse tree 
    1932                 if (frontLeaf->GetViewCell() == backLeaf->GetViewCell()) 
    1933                 { 
    1934                         BspViewCell *vc = frontLeaf->GetViewCell(); 
    1935  
    1936                         VspLeaf *leaf = new VspLeaf(interior->GetParent(), vc); 
    1937                         leaf->SetTreeValid(frontLeaf->TreeValid()); 
    1938  
    1939                         // replace a link from node's parent 
    1940                         if (leaf->GetParent()) 
    1941                                 leaf->GetParent()->ReplaceChildLink(node, leaf); 
    1942                         else 
    1943                                 mRoot = leaf; 
    1944  
    1945                         ++ collapsed; 
    1946                         delete interior; 
    1947  
    1948                         return leaf; 
    1949                 } 
    1950         } 
    1951 #endif 
    1952         return node; 
    1953 } 
    1954  
    1955  
    1956 int VspTree::CollapseTree() 
    1957 { 
    1958         int collapsed = 0; 
    1959  
    1960         (void)CollapseTree(mRoot, collapsed); 
    1961         // revalidate leaves 
    1962         RepairViewCellsLeafLists(); 
    1963  
    1964         return collapsed; 
    1965 } 
    1966  
    1967  
    1968 void VspTree::RepairViewCellsLeafLists() 
    1969 { 
    1970 // TODO 
    1971 #if HAS_TO_BE_REDONE 
    1972         // list not valid anymore => clear 
    1973         stack<VspNode *> nodeStack; 
    1974         nodeStack.push(mRoot); 
    1975  
    1976         ViewCell::NewMail(); 
    1977  
    1978         while (!nodeStack.empty()) 
    1979         { 
    1980                 VspNode *node = nodeStack.top(); 
    1981                 nodeStack.pop(); 
    1982  
    1983                 if (node->IsLeaf()) 
    1984                 { 
    1985                         VspLeaf *leaf = dynamic_cast<VspLeaf *>(node); 
    1986  
    1987                         BspViewCell *viewCell = leaf->GetViewCell(); 
    1988  
    1989                         if (!viewCell->Mailed()) 
    1990                         { 
    1991                                 viewCell->mLeaves.clear(); 
    1992                                 viewCell->Mail(); 
    1993                         } 
    1994          
    1995                         viewCell->mLeaves.push_back(leaf); 
    1996  
    1997                 } 
    1998                 else 
    1999                 { 
    2000                         VspInterior *interior = dynamic_cast<VspInterior *>(node); 
    2001  
    2002                         nodeStack.push(interior->GetFront()); 
    2003                         nodeStack.push(interior->GetBack()); 
    2004                 } 
    2005         } 
    2006 // TODO 
    2007 #endif 
    2008 } 
    2009  
    20101926 
    20111927 
     
    21712087                //-- test if start point behind or in front of plane 
    21722088                const int side = plane.ComputeRayIntersection(bRay, t); 
    2173  
     2089#if 1 
    21742090                if (side == 0) 
    21752091                { 
     
    21952111                        backRays.push_back(bRay); 
    21962112                } 
    2197  
     2113#else 
     2114                if (side == 0) 
     2115                { 
     2116                        ++ splits; 
     2117 
     2118                        if (ray->HasPosDir(plane.mAxis)) 
     2119                        { 
     2120                                backRays.push_back(RayInfo(ray, bRay.GetMaxT(), t)); 
     2121                                frontRays.push_back(RayInfo(ray, t, bRay.GetMinT())); 
     2122                        } 
     2123                        else 
     2124                        { 
     2125                                frontRays.push_back(RayInfo(ray, bRay.GetMaxT(), t)); 
     2126                                backRays.push_back(RayInfo(ray, t, bRay.GetMinT())); 
     2127                        } 
     2128                } 
     2129                else if (side == 1) 
     2130                { 
     2131                        backRays.push_back(bRay); 
     2132                } 
     2133                else 
     2134                { 
     2135                        frontRays.push_back(bRay); 
     2136                         
     2137                } 
     2138#endif 
    21982139        } 
    21992140 
     
    22162157        AxisAlignedBox3 box(parent->GetBoundingBox()); 
    22172158 
    2218         if (parent->GetFront() == node) 
     2159        if (parent->GetBack() == node) 
    22192160                box.SetMin(parent->GetAxis(), parent->GetPosition()); 
    22202161        else 
     
    22302171                                                                         ViewCellContainer &viewCells) const 
    22312172{ 
    2232 #if TODO 
    2233         stack<bspNodePair> nodeStack; 
    2234         BspNodeGeometry *rgeom = new BspNodeGeometry(); 
    2235  
    2236         ConstructGeometry(mRoot, *rgeom); 
    2237  
    2238         nodeStack.push(bspNodePair(mRoot, rgeom)); 
    2239    
     2173        stack<VspNode *> nodeStack; 
     2174  
    22402175        ViewCell::NewMail(); 
    22412176 
    22422177        while (!nodeStack.empty())  
    22432178        { 
    2244                 BspNode *node = nodeStack.top().first; 
    2245                 BspNodeGeometry *geom = nodeStack.top().second; 
     2179                VspNode *node = nodeStack.top(); 
    22462180                nodeStack.pop(); 
    22472181 
    2248                 const int side = geom->ComputeIntersection(box); 
    2249                  
    2250                 switch (side) 
    2251                 { 
    2252                 case -1: 
     2182                const AxisAlignedBox3 bbox = GetBBox(node); 
     2183 
     2184                if (bbox.Includes(box)) 
     2185                { 
    22532186                        // node geometry is contained in box 
    22542187                        CollectViewCells(node, true, viewCells, true); 
    2255                         break; 
    2256  
    2257                 case 0: 
     2188                } 
     2189                else if (Overlap(bbox, box)) 
     2190                { 
    22582191                        if (node->IsLeaf()) 
    22592192                        { 
     
    22682201                        else  
    22692202                        { 
    2270                                 BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     2203                                VspInterior *interior = dynamic_cast<VspInterior *>(node); 
    22712204                         
    2272                                 BspNode *first = interior->GetFront(); 
    2273                                 BspNode *second = interior->GetBack(); 
     2205                                VspNode *first = interior->GetFront(); 
     2206                                VspNode *second = interior->GetBack(); 
    22742207             
    2275                                 BspNodeGeometry *firstGeom = new BspNodeGeometry(); 
    2276                                 BspNodeGeometry *secondGeom = new BspNodeGeometry(); 
    2277  
    2278                                 geom->SplitGeometry(*firstGeom, 
    2279                                                                         *secondGeom, 
    2280                                                                         interior->GetPlane(), 
    2281                                                                         mBox, 
    2282                                                                         //0.0000001f); 
    2283                                                                         mEpsilon); 
    2284  
    2285                                 nodeStack.push(bspNodePair(first, firstGeom)); 
    2286                                 nodeStack.push(bspNodePair(second, secondGeom)); 
    2287                         } 
    2288                          
    2289                         break; 
    2290                 default: 
    2291                         // default: cull 
    2292                         break; 
    2293                 } 
    2294                 DEL_PTR(geom); 
    2295         } 
    2296  
    2297 #endif 
     2208                                nodeStack.push(first); 
     2209                                nodeStack.push(second); 
     2210                        } 
     2211                }        
     2212                // default: cull 
     2213        } 
     2214 
    22982215        return (int)viewCells.size(); 
    22992216} 
     
    23042221/*                class OspTree implementation                   */ 
    23052222/*****************************************************************/ 
     2223 
    23062224 
    23072225OspTree::OspTree(): 
     
    24992417                // create new interior node and two leaf node 
    25002418                const AxisAlignedPlane splitPlane = splitCandidate.mSplitPlane; 
     2419                 
    25012420                newNode = SubdivideNode(dynamic_cast<KdLeaf *>(newNode),  
    25022421                                                                splitPlane,  
     
    26352554          //              << "Area: " << data.mProbability << " (min: " << mTermMinProbability << "), " 
    26362555                  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), " 
    2637                   << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, " 
     2556                  << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "), " 
    26382557                  << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl; 
    26392558#endif 
     
    26472566/*********************************************************************/ 
    26482567 
    2649 HierarchyManager::HierarchyManager() 
     2568HierarchyManager::HierarchyManager(VspTree &vspTree, OspTree &ospTree): 
     2569mVspTree(vspTree), mOspTree(ospTree) 
    26502570{ 
    26512571} 
     
    26552575{ 
    26562576        SplitCandidate *splitCandidate = mTQueue.top(); 
     2577        Debug << "priority: " << splitCandidate->GetPriority() << endl; 
    26572578        mTQueue.pop(); 
    26582579 
     
    26622583 
    26632584void HierarchyManager::PrepareConstruction(const VssRayContainer &sampleRays, 
     2585                                                                                   const ObjectContainer &objects, 
    26642586                                                                                   AxisAlignedBox3 *forcedViewSpace, 
    26652587                                                                                   RayInfoContainer &rays) 
    26662588{ 
     2589        mVspTree.PrepareConstruction(sampleRays, forcedViewSpace); 
     2590 
     2591        long startTime = GetTime(); 
     2592 
     2593        cout << "storing rays ... "; 
     2594 
     2595        Intersectable::NewMail(); 
     2596 
    26672597        VssRayContainer::const_iterator rit, rit_end = sampleRays.end(); 
    2668  
    2669         long startTime = GetTime(); 
    2670  
    2671         cout << "storing rays ... "; 
    2672  
    2673         Intersectable::NewMail(); 
    2674  
    2675         mVspTree->PrepareConstruction(sampleRays, forcedViewSpace); 
    26762598 
    26772599        //-- store rays 
     
    26842606                static Ray hray; 
    26852607                hray.Init(*ray); 
    2686  
     2608                 
    26872609                // TODO: not very efficient to implictly cast between rays types 
    2688                 if (mBoundingBox.GetRaySegment(hray, minT, maxT)) 
     2610                if (mVspTree.GetBoundingBox().GetRaySegment(hray, minT, maxT)) 
    26892611                { 
    26902612                        float len = ray->Length(); 
     
    26972619        } 
    26982620 
    2699         cout << "finished" << endl; 
     2621        cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl; 
     2622 
     2623        /// add first candidate for view space partition 
     2624        mVspTree.mRoot = new VspLeaf(); 
     2625        const float prop = mVspTree.mBoundingBox.GetVolume(); 
     2626 
     2627        /// first traversal data 
     2628        VspTree::VspTraversalData tData(mVspTree.mRoot, 
     2629                                                                        0, 
     2630                                                                        &rays, 
     2631                                                                        (int)objects.size(), 
     2632                                                                        //mVspTree.ComputePvsSize(rays), 
     2633                                                                        prop, 
     2634                                                                        mVspTree.mBoundingBox); 
     2635 
     2636        // compute first split candidate 
     2637        VspTree::VspSplitCandidate *splitCandidate = new VspTree::VspSplitCandidate(); 
     2638    mVspTree.EvalSplitCandidate(tData, *splitCandidate); 
     2639 
     2640        mTQueue.push(splitCandidate); 
    27002641 
    27012642        //mOspTree->PrepareConstruction(sampleRays, forcedViewSpace, rays); 
     
    27102651                        dynamic_cast<VspTree::VspSplitCandidate *>(candidate); 
    27112652                 
    2712                 return mVspTree->GlobalTerminationCriteriaMet(sc->mParentData); 
     2653                return mVspTree.GlobalTerminationCriteriaMet(sc->mParentData); 
    27132654        } 
    27142655 
     
    27242665         
    27252666        // prepare vsp and osp trees for traversal 
    2726         PrepareConstruction(sampleRays, forcedViewSpace, *rays); 
    2727  
    2728         mVspTree->mVspStats.Start(); 
     2667        PrepareConstruction(sampleRays, objects, forcedViewSpace, *rays); 
     2668 
     2669        mVspTree.mVspStats.Reset(); 
     2670        mVspTree.mVspStats.Start(); 
    27292671 
    27302672        cout << "Constructing view space / object space tree ... \n"; 
     
    27382680                        GlobalTerminationCriteriaMet(splitCandidate); 
    27392681 
     2682                Debug << "cost: " << splitCandidate->GetPriority(); 
     2683 
    27402684                // cost ratio of cost decrease / totalCost 
    27412685                const float costRatio = splitCandidate->GetPriority() / mTotalCost; 
     
    27442688                if (costRatio < mTermMinGlobalCostRatio) 
    27452689                        ++ mGlobalCostMisses; 
    2746          
     2690 
    27472691                //-- subdivide leaf node 
    27482692                //-- either a object space or view space split 
     
    27522696                                dynamic_cast<VspTree::VspSplitCandidate *>(splitCandidate); 
    27532697 
    2754                         VspNode *r = mVspTree->Subdivide(mTQueue, *sc, globalTerminationCriteriaMet); 
     2698                        VspNode *r = mVspTree.Subdivide(mTQueue, *sc, globalTerminationCriteriaMet); 
    27552699                } 
    27562700                else // object space split 
    27572701                { 
    27582702#if TODO 
    2759                         KdNode *r = mKdtree->Subdivide(tOspQueue, dynamic_cast<OspSplitCandidate<(splitCandidate)); 
     2703                        KdNode *r = mKdtree.Subdivide(tOspQueue, dynamic_cast<OspSplitCandidate<(splitCandidate)); 
    27602704#endif 
    27612705                } 
     
    27662710        cout << "finished in " << TimeDiff(startTime, GetTime())*1e-3 << "secs" << endl; 
    27672711 
    2768         mVspTree->mVspStats.Stop(); 
     2712        mVspTree.mVspStats.Stop(); 
    27692713} 
    27702714 
Note: See TracChangeset for help on using the changeset viewer.