Changeset 482


Ignore:
Timestamp:
12/27/05 12:52:25 (18 years ago)
Author:
mattausch
Message:

added visualizations

Location:
trunk/VUT/GtpVisibilityPreprocessor
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env

    r481 r482  
    146146        loadFromFile false 
    147147        #type kdTree 
    148         #type vspKdTree 
     148        type vspKdTree 
    149149        #type bspTree 
    150         type vspBspTree 
     150        #type vspBspTree 
    151151         
    152152        #type sceneDependent 
     
    166166                # how much samples are be used for visualization 
    167167                samples 90000 
     168                #colorCode PVS 
     169                #colorCode MergedLeaves 
     170                #colorCode MergedTreeDiff 
     171                colorCode Random 
    168172        } 
    169173         
     
    192196        Termination { 
    193197                maxDepth                40 
    194                 minPvs                  5 
    195                 minRays                 500 
     198                minPvs                  50 
     199                minRays                 300 
    196200                minSize                 0.1 
    197                 maxCostRatio            0.9 
    198                 missTolerance           4 
    199                 maxRayContribution      0.2 
     201                maxCostRatio            1.8 
     202                missTolerance           2 
     203                maxRayContribution      0.5 
    200204        } 
    201205         
     
    209213        # maximal cost for merging a view cell 
    210214        PostProcess { 
    211                 maxCostRatio 1.4 
    212                 minViewCells 100 
     215                maxCostRatio 0.005 
     216                minViewCells 300 
    213217                maxPvsSize   50000 
    214218        } 
     
    216220         
    217221        Visualization { 
    218                 exportRays true 
     222                exportRays false 
    219223                exportGeometry false 
    220224        } 
     
    223227VspBspTree { 
    224228        Construction { 
    225                 samples 500000 
     229                samples 300000 
    226230                epsilon 0.005 
    227231        } 
     
    258262                minArea                 0.0001 
    259263                maxRayContribution      0.005 
    260                 maxCostRatio            0.8 
     264                maxCostRatio            0.9 
    261265                missTolerance           2 
    262266                #maxAccRayLength        100 
    263267                 
    264                 maxViewCells            5000 
     268                maxViewCells            1000 
    265269                 
    266270                # used for pvs criterium 
    267271                ct_div_ci 0.0 
    268          
     272        } 
    269273         
    270274        Visualization { 
     
    277281        PostProcess { 
    278282                maxCostRatio 0.005 
    279                 minViewCells 700 
     283                minViewCells 100 
    280284                maxPvsSize   500 
    281285        } 
  • trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp

    r469 r482  
    17711771        return true; 
    17721772} 
     1773 
     1774 
     1775bool AxisAlignedBox3::GetIntersectionFace(Rectangle3 &face,  
     1776                                                                                  const AxisAlignedBox3 &neighbour) const 
     1777                                                                                    
     1778{  
     1779        if (EpsilonEqual(mMin[0], neighbour.Max(0))) 
     1780        { 
     1781                float maxy = min(mMax.y, neighbour.mMax.y); 
     1782                float maxz = min(mMax.z, neighbour.mMax.z); 
     1783                float miny = max(mMin.y, neighbour.mMin.y); 
     1784                float minz = max(mMin.z, neighbour.mMin.z); 
     1785 
     1786                face.mVertices[3].SetValue(mMin.x, miny, minz); 
     1787                face.mVertices[2].SetValue(mMin.x, maxy, minz); 
     1788                face.mVertices[1].SetValue(mMin.x, maxy, maxz); 
     1789                face.mVertices[0].SetValue(mMin.x, miny, maxz); 
     1790 
     1791        return true; 
     1792        } 
     1793    if (EpsilonEqual(mMax[0], neighbour.Min(0))) 
     1794        { 
     1795                float maxy = min(mMax.y, neighbour.mMax.y); 
     1796                float maxz = min(mMax.z, neighbour.mMax.z); 
     1797                float miny = max(mMin.y, neighbour.mMin.y); 
     1798                float minz = max(mMin.z, neighbour.mMin.z); 
     1799 
     1800                face.mVertices[0].SetValue(mMax.x, miny, minz); 
     1801                face.mVertices[1].SetValue(mMax.x, maxy, minz); 
     1802                face.mVertices[2].SetValue(mMax.x, maxy, maxz); 
     1803                face.mVertices[3].SetValue(mMax.x, miny, maxz); 
     1804 
     1805                return true; 
     1806        } 
     1807    if (EpsilonEqual(mMin[1], neighbour.Max(1))) 
     1808        { 
     1809                float maxx = min(mMax.x, neighbour.mMax.x); 
     1810                float maxz = min(mMax.z, neighbour.mMax.z); 
     1811                float minx = max(mMin.x, neighbour.mMin.x); 
     1812                float minz = max(mMin.z, neighbour.mMin.z); 
     1813 
     1814                face.mVertices[3].SetValue(minx, mMin.y, minz); 
     1815                face.mVertices[2].SetValue(minx, mMin.y, maxz); 
     1816                face.mVertices[1].SetValue(maxx, mMin.y, maxz); 
     1817                face.mVertices[0].SetValue(maxx, mMin.y, minz); 
     1818                 
     1819                return true; 
     1820        } 
     1821        if (EpsilonEqual(mMax[1], neighbour.Min(1))) 
     1822        {                
     1823                float maxx = min(mMax.x, neighbour.mMax.x); 
     1824                float maxz = min(mMax.z, neighbour.mMax.z); 
     1825                float minx = max(mMin.x, neighbour.mMin.x); 
     1826                float minz = max(mMin.z, neighbour.mMin.z); 
     1827 
     1828                face.mVertices[0].SetValue(minx, mMax.y, minz); 
     1829                face.mVertices[1].SetValue(minx, mMax.y, maxz); 
     1830                face.mVertices[2].SetValue(maxx, mMax.y, maxz); 
     1831                face.mVertices[3].SetValue(maxx, mMax.y, minz); 
     1832 
     1833                return true; 
     1834        } 
     1835        if (EpsilonEqual(mMin[2], neighbour.Max(2))) 
     1836        { 
     1837                float maxx = min(mMax.x, neighbour.mMax.x); 
     1838                float maxy = min(mMax.y, neighbour.mMax.y); 
     1839                float minx = max(mMin.x, neighbour.mMin.x); 
     1840                float miny = max(mMin.y, neighbour.mMin.y); 
     1841 
     1842                face.mVertices[3].SetValue(minx, miny, mMin.z); 
     1843                face.mVertices[2].SetValue(maxx, miny, mMin.z); 
     1844                face.mVertices[1].SetValue(maxx, maxy, mMin.z); 
     1845                face.mVertices[0].SetValue(minx, maxy, mMin.z); 
     1846         
     1847                return true; 
     1848        } 
     1849        if (EpsilonEqual(mMax[2], neighbour.Min(2))) 
     1850        { 
     1851                float maxx = min(mMax.x, neighbour.mMax.x); 
     1852                float maxy = min(mMax.y, neighbour.mMax.y); 
     1853                float minx = max(mMin.x, neighbour.mMin.x); 
     1854                float miny = max(mMin.y, neighbour.mMin.y); 
     1855 
     1856                face.mVertices[0].SetValue(minx, miny, mMax.z); 
     1857                face.mVertices[1].SetValue(maxx, miny, mMax.z); 
     1858                face.mVertices[2].SetValue(maxx, maxy, mMax.z); 
     1859                face.mVertices[3].SetValue(minx, maxy, mMax.z); 
     1860 
     1861                return true; 
     1862        } 
     1863 
     1864        return false; 
     1865} 
     1866 
  • trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.h

    r459 r482  
    321321                                float &maxT) const; 
    322322 
     323  /** If the boxes are intersecting on a common face, this function  
     324          returns the face intersection, false otherwise. 
     325     
     326          @param neighbour the neighbouring box intersecting with this box. 
     327  */ 
     328  bool GetIntersectionFace(Rectangle3 &face, 
     329                                                   const AxisAlignedBox3 &neighbour) const; 
     330 
    323331#define __EXTENT_HACK 
    324332  // get the extent of face 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp

    r481 r482  
    12311231                 "-view_cells_height=", 
    12321232                 "5.0"); 
    1233     
     1233   RegisterOption("ViewCells.Visualization.colorCode", 
     1234                optString, 
     1235                "-view_cells_visualization.color_code", 
     1236                "PVS"); 
     1237 
    12341238  /************************************************************************************/ 
    12351239  /*                         Render simulation related options                        */ 
     
    16211625        /************************************************************************************/ 
    16221626 
    1623  
    16241627        RegisterOption("VspBspTree.Termination.minPolygons", 
    1625                         optInt, 
    1626                         "-vsp_bsp_term_min_polygons=", 
    1627                         "5"); 
     1628                optInt, 
     1629                "-vsp_bsp_term_min_polygons=", 
     1630                "5"); 
    16281631 
    16291632        RegisterOption("VspBspTree.Termination.minPvs", 
    1630                         optInt, 
    1631                         "-vsp_bsp_term_min_pvs=", 
    1632                         "20"); 
     1633                optInt, 
     1634                "-vsp_bsp_term_min_pvs=", 
     1635                "20"); 
    16331636 
    16341637        RegisterOption("VspBspTree.Termination.minArea", 
    1635                         optFloat, 
    1636                         "-vsp_bsp_term_min_area=", 
    1637                         "0.001"); 
     1638                optFloat, 
     1639                "-vsp_bsp_term_min_area=", 
     1640                "0.001"); 
    16381641 
    16391642        RegisterOption("VspBspTree.Termination.maxRayContribution", 
    1640                         optFloat, 
    1641                         "-vsp_bsp_term_ray_contribution=", 
    1642                         "0.005"); 
     1643                optFloat, 
     1644                "-vsp_bsp_term_ray_contribution=", 
     1645                "0.005"); 
    16431646 
    16441647        RegisterOption("VspBspTree.Termination.minAccRayLenght", 
    1645                         optFloat, 
    1646                         "-vsp_bsp_term_min_acc_ray_length=", 
    1647                         "50"); 
     1648                optFloat, 
     1649                "-vsp_bsp_term_min_acc_ray_length=", 
     1650                "50"); 
    16481651 
    16491652        RegisterOption("VspBspTree.Termination.minRays", 
    1650                         optInt, 
    1651                         "-vsp_bsp_term_min_rays=", 
    1652                         "-1"); 
     1653                optInt, 
     1654                "-vsp_bsp_term_min_rays=", 
     1655                "-1"); 
    16531656 
    16541657        RegisterOption("VspBspTree.Termination.ct_div_ci", 
    1655                         optFloat, 
    1656                         "-vsp_bsp_term_ct_div_ci=", 
    1657                         "0.0"); 
     1658                optFloat, 
     1659                "-vsp_bsp_term_ct_div_ci=", 
     1660                "0.0"); 
    16581661 
    16591662        RegisterOption("VspBspTree.Termination.maxDepth", 
    1660                         optInt, 
    1661                         "-vsp_bsp_term_max_depth=", 
    1662                         "100"); 
     1663                optInt, 
     1664                "-vsp_bsp_term_max_depth=", 
     1665                "100"); 
    16631666 
    16641667        RegisterOption("VspBspTree.Termination.AxisAligned.maxCostRatio", 
    1665                         optFloat, 
    1666                         "-vsp_bsp_term_axis_aligned_max_cost_ratio=", 
    1667                         "1.5"); 
     1668                optFloat, 
     1669                "-vsp_bsp_term_axis_aligned_max_cost_ratio=", 
     1670                "1.5"); 
    16681671 
    16691672        RegisterOption("VspBspTree.Termination.maxViewCells", 
    1670                         optInt, 
    1671                         "-vsp_bsp_term_axis_aligned_max_view_cells=", 
    1672                         "1000"); 
     1673                optInt, 
     1674                "-vsp_bsp_term_axis_aligned_max_view_cells=", 
     1675                "1000"); 
    16731676 
    16741677        RegisterOption("VspBspTree.Termination.maxCostRatio", 
     
    17161719                "-vsp_bsp_visualization.export_splits", 
    17171720                "false"); 
     1721 
    17181722        RegisterOption("VspBspTree.Visualization.exportRays", 
    17191723                optBool, 
     
    17261730                "false"); 
    17271731 
     1732         
    17281733        RegisterOption("VspBspTree.Factor.leastRaySplits", optFloat, "-vsp_bsp_factor_least_ray_splits=", "1.0"); 
    17291734        RegisterOption("VspBspTree.Factor.balancedRays", optFloat, "-vsp_bsp_factor_balanced_rays=", "1.0"); 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h

    r466 r482  
    100100  ExportBspSplits(const VspBspTree &tree, const bool exportDepth = false) = 0; 
    101101 
    102   virtual void 
    103   ExportBspViewCellPartition(const VspBspTree &tree, const int maxPvs = 0) = 0; 
    104  
    105102  virtual void  
    106103  ExportPolygons(const PolygonContainer &polys) = 0; 
    107  
    108   virtual void 
    109   ExportBspViewCellPartition(const BspTree &tree, const int maxPvs = 0) = 0; 
    110104 
    111105  virtual void  
     
    118112  ExportRssTree2(const RssTree &tree, 
    119113                                 const Vector3 direction) = 0; 
    120  
    121   virtual bool  
    122   ExportVspKdTreeViewCells(const VspKdTree &tree, const int maxPvs = 0) = 0; 
    123114 
    124115  void SetExportRayDensity(const bool d) { mExportRayDensity = d; } 
  • trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp

    r480 r482  
    7777                // probability of view cell 
    7878                const float pInVc = mViewCellsManager->GetProbability(vc); 
    79                 // compute render time of PVS times probability that view point is in view cell 
    80                 const float vcCost = pInVc * mViewCellsManager->GetRendercost(vc, mObjRenderCost); 
     79                // compute render time of PVS times probability 
     80                // that view point is in view cell 
     81                const float vcCost = pInVc *  
     82                        mViewCellsManager->GetRendercost(vc, mObjRenderCost); 
    8183         
    8284                // crossing the border of a view cell is depending on the move speed 
    8385                // and the probability that a view cell border is crossed 
    8486                loadPvsOverhead += GetCrossVcProbability() * mVcOverhead; 
    85                 //Debug << "vccost: " << vcCost << " p in vc " << pInVc << " cross vc " << GetCrossVcProbability() << endl; 
     87 
     88                //Debug << "vccost: " << vcCost << " p in vc " << pInVc 
     89                //<< " cross vc " << GetCrossVcProbability() << endl; 
     90 
    8691                //-- update statistics 
    8792                renderTime += vcCost; 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp

    r480 r482  
    5757} 
    5858 
     59 
    5960BspInterior *BspNode::GetParent()  
    6061{  
     
    6263} 
    6364 
     65 
    6466void BspNode::SetParent(BspInterior *parent) 
    6567{ 
    6668        mParent = parent; 
     69} 
     70 
     71 
     72bool BspNode::IsSibling(BspNode *n) const 
     73{ 
     74        return  ((this != n) && mParent &&  
     75                         (mParent->GetFront() == n) || (mParent->GetBack() == n)); 
     76} 
     77 
     78int BspNode::GetDepth() const 
     79{ 
     80        int depth = 0; 
     81        BspNode *p = mParent; 
     82         
     83        while (p) 
     84        { 
     85                p = p->mParent; 
     86                ++ depth; 
     87        } 
     88 
     89        return depth; 
    6790} 
    6891 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h

    r480 r482  
    198198        void SetParent(BspInterior *parent); 
    199199 
    200          
     200        /** Returns true if this node is a sibling of node n. 
     201        */ 
     202        bool IsSibling(BspNode *n) const; 
     203 
     204        /** returns depth of the node. 
     205        */ 
     206        int GetDepth() const; 
    201207        static int sMailId; 
    202208        int mMailbox; 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp

    r480 r482  
    2121mTotalArea(0.0f) 
    2222{ 
    23         // post processing stuff 
    24         environment->GetIntValue("ViewCells.PostProcess.minPvsDif", mMinPvsDif); 
    25         environment->GetIntValue("ViewCells.PostProcess.minPvs", mMinPvs); 
    26         environment->GetIntValue("ViewCells.PostProcess.maxPvs", mMaxPvs); 
     23        ParseEnvironment(); 
    2724} 
    2825 
     
    3330mVisualizationSamples(0) 
    3431{ 
     32        ParseEnvironment(); 
     33} 
     34 
     35void ViewCellsManager::ParseEnvironment() 
     36{ 
    3537        // post processing stuff 
    3638        environment->GetIntValue("ViewCells.PostProcess.minPvsDif", mMinPvsDif); 
    3739        environment->GetIntValue("ViewCells.PostProcess.minPvs", mMinPvs); 
    3840        environment->GetIntValue("ViewCells.PostProcess.maxPvs", mMaxPvs); 
     41 
     42        char buf[50]; 
     43         
     44        environment->GetStringValue("ViewCells.Visualization.colorCode", buf); 
     45         
     46        if (strcmp(buf, "PVS") == 0) 
     47                mColorCode = 1; 
     48        else if (strcmp(buf, "MergedLeaves") == 0) 
     49                mColorCode = 2; 
     50        else if (strcmp(buf, "MergedTreeDiff") == 0) 
     51                mColorCode = 3; 
     52        else 
     53                mColorCode = 0; 
     54 
     55        Debug << "colorCode: " << mColorCode << endl; 
    3956} 
    4057 
     
    336353} 
    337354 
     355 
    338356void ViewCellsManager::PrintStatistics(ostream &s) const 
    339357{ 
    340358        s << mViewCellsStats << endl; 
     359} 
     360 
     361 
     362void ViewCellsManager::ExportViewCells(Exporter *exporter) const 
     363{ 
     364        ViewCellContainer::const_iterator it, it_end = mViewCells.end(); 
     365 
     366        for (it = mViewCells.begin(); it != it_end; ++ it) 
     367        { 
     368                ExportColor(exporter, *it); 
     369                ExportGeometry(exporter, *it); 
     370        } 
    341371} 
    342372 
     
    345375/*                   BspViewCellsManager implementation               */ 
    346376/**********************************************************************/ 
     377 
    347378 
    348379BspViewCellsManager::BspViewCellsManager(BspTree *bspTree,  
     
    490521                        //exporter->SetWireframe(); 
    491522                        exporter->SetFilled(); 
    492                         exporter->ExportBspViewCellPartition(*mBspTree,  
    493                                 mViewCellsStats.maxPvs); 
     523                        ExportViewCells(exporter); 
    494524                                 
    495525                        if (0) 
     
    593623                if (exporter) 
    594624                { 
    595                         exporter->ExportBspViewCellPartition(*mBspTree,  
    596                                 mViewCellsStats.maxPvs); 
     625                        ExportViewCells(exporter); 
    597626                        delete exporter; 
    598627                } 
     
    635664                exporter->ExportBspSplits(*mBspTree, true); 
    636665 
    637                 // take forced material, else big scenes cannot be viewed 
     666                //NOTE: take forced material, else big scenes cannot be viewed 
    638667                m.mDiffuseColor = RgbColor(0, 1, 0); 
    639668                exporter->SetForcedMaterial(m); 
     669                //exporter->ResetForcedMaterial(); 
     670 
    640671                exporter->SetFilled(); 
    641  
    642                 exporter->ResetForcedMaterial(); 
    643                  
     672                                 
    644673                // export rays 
    645674                if (0) 
     
    671700 
    672701        environment->GetBoolValue("VspBspTree.Visualization.exportRays", exportRays); 
    673         environment->GetBoolValue("VspBspTree.Visualization.exportGeometry", exportGeometry); 
     702        environment->GetBoolValue("VspBspTree.Visualization.exportRays", exportRays); 
    674703 
    675704        const int leafOut = 10; 
     
    866895} 
    867896 
     897 
     898void BspViewCellsManager::ExportColor(Exporter *exporter,  
     899                                                                          ViewCell *vc) const 
     900{ 
     901        if (mColorCode == 0) // Random color 
     902        { 
     903                exporter->ResetForcedMaterial(); 
     904                return; 
     905        } 
     906 
     907        float importance = 0; 
     908 
     909        switch (mColorCode) 
     910        { 
     911        case 1: // pvs 
     912                { 
     913                        importance = (float)vc->GetPvs().GetSize() /  
     914                                (float)mViewCellsStats.maxPvs; 
     915                } 
     916                break; 
     917        case 2: // merges 
     918                { 
     919                        BspViewCell *bspVc = dynamic_cast<BspViewCell *>(vc); 
     920 
     921                        importance = (float)bspVc->mLeaves.size() /  
     922                                (float)mViewCellsStats.maxLeaves; 
     923                } 
     924                break; 
     925        case 3: // merge tree differene 
     926                { 
     927                        // TODO 
     928                } 
     929                break; 
     930        default: 
     931                break; 
     932        } 
     933 
     934        Material m; 
     935        m.mDiffuseColor.b = 1.0f; 
     936        m.mDiffuseColor.r = importance; 
     937        m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r; 
     938         
     939        exporter->SetForcedMaterial(m); 
     940} 
     941 
     942void BspViewCellsManager::ExportGeometry(Exporter *exporter,  
     943                                                                                 ViewCell *vc) const 
     944{ 
     945        if (vc->GetMesh()) 
     946                exporter->ExportViewCell(vc); 
     947        else 
     948        { 
     949                PolygonContainer cell; 
     950                mBspTree->ConstructGeometry(dynamic_cast<BspViewCell *>(vc), cell); 
     951 
     952                exporter->ExportPolygons(cell); 
     953        } 
     954} 
    868955 
    869956/**********************************************************************/ 
     
    11161203 
    11171204 
     1205void KdViewCellsManager::ExportColor(Exporter *exporter, 
     1206                                                                         ViewCell *vc) const 
     1207{ 
     1208        // TODO 
     1209} 
     1210 
     1211 
     1212void KdViewCellsManager::ExportGeometry(Exporter *exporter,  
     1213                                                                                ViewCell *vc) const 
     1214{ 
     1215        // TODO 
     1216} 
     1217 
     1218 
    11181219int KdViewCellsManager::GetType() const 
    11191220{ 
     
    11431244/*                   VspKdViewCellsManager implementation             */ 
    11441245/**********************************************************************/ 
     1246 
    11451247 
    11461248VspKdViewCellsManager::VspKdViewCellsManager(VspKdTree *vspKdTree,  
     
    11921294        Debug << mVspKdTree->GetStatistics() << endl; 
    11931295 
    1194         ResetViewCells(); 
    1195         Debug << "\nView cells after construction:\n" << mViewCellsStats << endl; 
     1296        // export leaf building blocks 
     1297        ExportLeaves(objects, rays); 
    11961298 
    11971299        // finally merge kd leaf building blocks to view cells 
    11981300        const int merged = mVspKdTree->MergeLeaves(); 
     1301        // evaluale view cell stats 
     1302        ResetViewCells(); 
     1303 
     1304        Debug << "\nView cells after construction:\n" << mViewCellsStats << endl; 
    11991305 
    12001306        // recast rest of rays 
     
    12211327                return 0; 
    12221328 
     1329        // recalculate stats 
     1330        EvaluateViewCellsStats(); 
     1331 
    12231332        return 0; 
    12241333} 
    12251334 
     1335 
    12261336AxisAlignedBox3 VspKdViewCellsManager::GetSceneBbox() const 
    12271337{ 
    12281338        return mVspKdTree->GetBBox(mVspKdTree->GetRoot()); 
     1339} 
     1340 
     1341 
     1342void VspKdViewCellsManager::ExportLeaves(const ObjectContainer &objects, 
     1343                                                                                 const VssRayContainer &sampleRays) 
     1344{ 
     1345        bool exportRays = false; 
     1346        bool exportGeometry = false; 
     1347 
     1348        environment->GetBoolValue("VspKdTree.Visualization.exportRays", exportRays); 
     1349        environment->GetBoolValue("VspKdTree.Visualization.exportGeometry", exportGeometry); 
     1350 
     1351        if (!ViewCellsConstructed()) 
     1352                return; 
     1353 
     1354        //-- export leaf building blocks 
     1355        Exporter *exporter = Exporter::GetExporter("vspkdtree.x3d");  
     1356        if (!exporter) 
     1357                return; 
     1358 
     1359        //exporter->SetWireframe(); 
     1360        //exporter->ExportVspKdTree(*mVspKdTree, mVspKdTree->GetStatistics().maxPvsSize); 
     1361        exporter->ExportVspKdTree(*mVspKdTree); 
     1362 
     1363        if (exportGeometry)  
     1364                exporter->ExportGeometry(objects); 
     1365 
     1366        if (exportRays)  
     1367        { 
     1368                const float prob = (float)mVisualizationSamples  
     1369                        / ((float)sampleRays.size() + Limits::Small); 
     1370 
     1371                exporter->SetWireframe(); 
     1372 
     1373                //-- collect uniformly distributed rays 
     1374                VssRayContainer rays; 
     1375 
     1376                for (int i = 0; i < sampleRays.size(); ++ i) 
     1377                { 
     1378                        if (RandomValue(0,1) < prob) 
     1379                                rays.push_back(sampleRays[i]); 
     1380                } 
     1381                exporter->ExportRays(rays, RgbColor(1, 0, 0)); 
     1382        } 
     1383 
     1384        delete exporter; 
    12291385} 
    12301386 
     
    12401396        if (!ViewCellsConstructed()) 
    12411397                return; 
    1242  
    1243         //-- export tree leaves 
    1244         if (1) 
    1245         { 
    1246                 Exporter *exporter = Exporter::GetExporter("vspkdtree.x3d");  
    1247                 //exporter->SetWireframe(); 
    1248                 //exporter->ExportVspKdTree(*mVspKdTree, mVspKdTree->GetStatistics().maxPvsSize); 
    1249                 exporter->ExportVspKdTree(*mVspKdTree); 
    1250  
    1251                 if (1)  
    1252                         exporter->ExportGeometry(objects); 
    1253  
    1254                 if (exportRays)  
    1255                   { 
    1256                         int raysSize = 2000; 
    1257                         float prob = raysSize / (float)sampleRays.size(); 
    1258                  
    1259                         exporter->SetWireframe(); 
    1260                          
    1261                         VssRayContainer rays; 
    1262                  
    1263                         for (int i = 0; i < sampleRays.size(); ++ i) 
     1398         
     1399        //-- export single view cells 
     1400        for (int i = 0; i < 10; ++ i) 
     1401        { 
     1402                char s[64]; 
     1403                sprintf(s, "vsp_viewcell%04d.x3d", i); 
     1404                Exporter *exporter = Exporter::GetExporter(s); 
     1405                const int idx =  
     1406                        (int)RandomValue(0.0, (Real)((int)mViewCells.size() - 1)); 
     1407 
     1408                VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>(mViewCells[idx]);  
     1409 
     1410                //-- export geometry 
     1411                Material m; 
     1412                m.mDiffuseColor = RgbColor(0, 1, 1); 
     1413                                 
     1414                exporter->SetForcedMaterial(m); 
     1415                exporter->SetWireframe(); 
     1416 
     1417                ExportGeometry(exporter, vc); 
     1418 
     1419                //-- export stored rays 
     1420                if (exportRays) 
     1421                { 
     1422                        vector<VspKdLeaf *>::const_iterator it,  
     1423                                it_end = vc->mLeaves.end(); 
     1424 
     1425                        for (it = vc->mLeaves.begin(); it != it_end; ++ it) 
    12641426                        { 
    1265                                 if (RandomValue(0,1) < prob) 
    1266                                         rays.push_back(sampleRays[i]); 
     1427                                VspKdLeaf *leaf = *it; 
     1428                                AxisAlignedBox3 box = mVspKdTree->GetBBox(leaf); 
     1429 
     1430                                VssRayContainer vssRays; 
     1431                                leaf->GetRays(vssRays); 
     1432 
     1433                                VssRayContainer rays; 
     1434                                VssRayContainer::const_iterator it, it_end = vssRays.end(); 
     1435 
     1436                                for (it = vssRays.begin(); it != it_end; ++ it) 
     1437                                        rays.push_back(*it); 
     1438                         
     1439                                exporter->ExportRays(rays, RgbColor(1, 0, 0)); 
    12671440                        } 
    1268  
    1269                         exporter->ExportRays(rays, RgbColor(1, 0, 0)); 
     1441                } 
     1442 
     1443                //-- output PVS of view cell 
     1444                m.mDiffuseColor = RgbColor(1, 0, 0); 
     1445                exporter->SetForcedMaterial(m); 
     1446 
     1447                ObjectPvsMap::const_iterator it, 
     1448                        it_end = vc->GetPvs().mEntries.end(); 
     1449 
     1450                exporter->SetFilled(); 
     1451 
     1452                for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)  
     1453                { 
     1454                        Intersectable *intersect = (*it).first; 
     1455 
     1456                        if (!intersect->Mailed()) 
     1457                        { 
     1458                                Material m = RandomMaterial(); 
     1459                                exporter->SetForcedMaterial(m); 
     1460 
     1461                                exporter->ExportIntersectable(intersect); 
     1462                                intersect->Mail(); 
     1463                        }                        
    12701464                } 
    12711465 
    12721466                delete exporter; 
    1273         } 
    1274  
    1275         //-- export single leaves 
    1276         if (1) 
    1277         { 
    1278                 vector<VspKdLeaf *> leafContainer; 
    1279                 mVspKdTree->CollectLeaves(leafContainer); 
    1280  
    1281                 for (int i = 0; i < 10; ++ i) 
    1282                 { 
    1283                         char s[64]; 
    1284                         sprintf(s, "vsp_leaves%04d.x3d", i); 
    1285                         Exporter *exporter = Exporter::GetExporter(s); 
    1286  
    1287                         // export geometry 
    1288                         VspKdLeaf *leaf = leafContainer[(int)RandomValue(0.0, (Real)((int)leafContainer.size() - 1))];  
    1289                         AxisAlignedBox3 box = mVspKdTree->GetBBox(leaf); 
    1290  
    1291                         Material m; 
    1292                         m.mDiffuseColor = RgbColor(0, 1, 1); 
    1293                         exporter->SetForcedMaterial(m); 
    1294                         exporter->SetWireframe(); 
    1295                         exporter->ExportBox(box); 
    1296  
    1297                         //-- export stored rays 
    1298                         VssRayContainer vssRays; 
    1299                         leaf->GetRays(vssRays); 
    1300  
    1301                         VssRayContainer rays; 
    1302                         VssRayContainer::const_iterator it, it_end = vssRays.end(); 
    1303  
    1304                         for (it = vssRays.begin(); it != it_end; ++ it) 
    1305                                 rays.push_back(*it); 
    1306                          
    1307                         exporter->ExportRays(rays, RgbColor(1, 0, 0)); 
    1308  
    1309                         //-- export stored PVS 
    1310                         ObjectContainer pvsObj; 
    1311                         leaf->ExtractPvs(pvsObj); 
    1312                                  
    1313                         exporter->ExportGeometry(pvsObj); 
    1314  
    1315                         delete exporter; 
    1316                 }                        
    13171467        } 
    13181468 
    13191469        //-- export final view cells 
    13201470        Exporter *exporter = Exporter::GetExporter("vspkdtree_merged.x3d");  
    1321         exporter->SetWireframe(); 
    1322         //exporter->ExportVspKdTreeViewCells(*mVspKdTree, vcStats.maxPvs); 
    1323         exporter->ExportVspKdTreeViewCells(*mVspKdTree); 
    1324  
    1325         if (1)  
     1471        exporter->SetFilled(); 
     1472        ExportViewCells(exporter); 
     1473 
     1474        if (exportGeometry)  
    13261475        { 
    13271476                exporter->SetFilled(); 
     
    13311480        if (exportRays)  
    13321481        { 
    1333                 int raysSize = 2000; 
    1334                 float prob = raysSize / (float)sampleRays.size(); 
     1482                const float prob = (float)mVisualizationSamples  
     1483                        / ((float)sampleRays.size() + Limits::Small); 
    13351484                 
    13361485                exporter->SetWireframe(); 
     
    13631512 
    13641513 
     1514void VspKdViewCellsManager::ExportColor(Exporter *exporter, 
     1515                                                                                ViewCell *vc) const 
     1516{ 
     1517        if (mColorCode == 0) // Random color 
     1518                return; 
     1519         
     1520        float importance = 0; 
     1521 
     1522        switch (mColorCode) 
     1523        { 
     1524        case 1: // pvs 
     1525                { 
     1526                        importance = (float)vc->GetPvs().GetSize() /  
     1527                                (float)mViewCellsStats.maxPvs; 
     1528                } 
     1529                break; 
     1530        case 2: // merges 
     1531                { 
     1532            VspKdViewCell *vspKdVc = dynamic_cast<VspKdViewCell *>(vc); 
     1533 
     1534                        importance = (float)vspKdVc->mLeaves.size() /  
     1535                                (float)mViewCellsStats.maxLeaves; 
     1536                } 
     1537                break; 
     1538        case 3: // merge tree differene 
     1539                { 
     1540                        //importance = (float)GetMaxTreeDiff(vc) /  
     1541                        //      (float)(mVspBspTree->GetStatistics().maxDepth * 2); 
     1542                } 
     1543                break; 
     1544        default: 
     1545                break; 
     1546        } 
     1547 
     1548        Material m; 
     1549        m.mDiffuseColor.b = 1.0f; 
     1550        m.mDiffuseColor.r = importance; 
     1551        m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r; 
     1552        //Debug << "importance: " << importance << endl; 
     1553        exporter->SetForcedMaterial(m); 
     1554} 
     1555 
     1556 
     1557void VspKdViewCellsManager::ExportGeometry(Exporter *exporter,  
     1558                                                                                   ViewCell *vc) const 
     1559{ 
     1560        VspKdViewCell *kdVc = dynamic_cast<VspKdViewCell *>(vc); 
     1561 
     1562        vector<VspKdLeaf *>::const_iterator it, it_end = kdVc->mLeaves.end(); 
     1563 
     1564        for (it = kdVc->mLeaves.begin(); it != it_end; ++ it) 
     1565        { 
     1566                exporter->ExportBox(mVspKdTree->GetBBox(*it)); 
     1567        } 
     1568} 
     1569 
    13651570 
    13661571/**********************************************************************/ 
    13671572/*                 VspBspViewCellsManager implementation              */ 
    13681573/**********************************************************************/ 
     1574 
    13691575 
    13701576VspBspViewCellsManager::VspBspViewCellsManager(VspBspTree *vspBspTree,  
     
    15111717                        //exporter->SetWireframe(); 
    15121718                        exporter->SetFilled(); 
    1513                         //exporter->ExportBspViewCellPartition(*mVspBspTree, mViewCellsStats.maxPvs); 
    1514                         exporter->ExportBspViewCellPartition(*mVspBspTree); 
     1719                         
     1720                        ExportViewCells(exporter); 
    15151721 
    15161722                        if (0) 
     
    16121818                if (exporter) 
    16131819                { 
    1614                         //exporter->ExportBspViewCellPartition(*mVspBspTree, mViewCellsStats.maxPvs); 
    1615                         exporter->ExportBspViewCellPartition(*mVspBspTree); 
     1820                        ExportViewCells(exporter); 
    16161821                        delete exporter; 
    16171822                } 
     
    16331838        ExportBspPvs(objects); 
    16341839} 
    1635  
     1840         
    16361841 
    16371842void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects) 
    16381843{ 
    16391844        Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d"); 
     1845 
     1846        bool exportRays = false; 
     1847        bool exportGeometry = false; 
     1848 
     1849        environment->GetBoolValue("VspBspTree.Visualization.exportRays", exportRays); 
     1850        environment->GetBoolValue("VspBspTree.Visualization.exportGeometry", exportGeometry); 
    16401851 
    16411852        if (exporter)  
     
    16561867                 
    16571868                // export rays 
    1658                 if (0) 
     1869                if (exportRays) 
    16591870                { 
    16601871                        VssRayContainer outRays; 
     
    16701881                } 
    16711882 
    1672                 if (0) 
     1883                if (exportGeometry) 
    16731884                        exporter->ExportGeometry(objects); 
    16741885 
     
    18612072        return mVspBspTree->CastLineSegment(origin, termination, viewcells); 
    18622073} 
     2074 
     2075 
     2076void VspBspViewCellsManager::ExportColor(Exporter *exporter,  
     2077                                                                                 ViewCell *vc) const 
     2078{ 
     2079        if (mColorCode == 0) // Random color 
     2080                return; 
     2081         
     2082        float importance = 0; 
     2083 
     2084        switch (mColorCode) 
     2085        { 
     2086        case 1: // pvs 
     2087                { 
     2088                        importance = (float)vc->GetPvs().GetSize() /  
     2089                                (float)mViewCellsStats.maxPvs; 
     2090                } 
     2091                break; 
     2092        case 2: // merges 
     2093                { 
     2094            BspViewCell *bspVc = dynamic_cast<BspViewCell *>(vc); 
     2095 
     2096                        importance = (float)bspVc->mLeaves.size() /  
     2097                                (float)mViewCellsStats.maxLeaves; 
     2098                } 
     2099                break; 
     2100        case 3: // merge tree differene 
     2101                { 
     2102                        importance = (float)GetMaxTreeDiff(vc) /  
     2103                                (float)(mVspBspTree->GetStatistics().maxDepth * 2); 
     2104                } 
     2105                break; 
     2106        default: 
     2107                break; 
     2108        } 
     2109 
     2110        Material m; 
     2111        m.mDiffuseColor.b = 1.0f; 
     2112        m.mDiffuseColor.r = importance; 
     2113        m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r; 
     2114        //Debug << "importance: " << importance << endl; 
     2115        exporter->SetForcedMaterial(m); 
     2116} 
     2117 
     2118 
     2119void VspBspViewCellsManager::ExportGeometry(Exporter *exporter,  
     2120                                                                                        ViewCell *vc) const 
     2121{ 
     2122        if (vc->GetMesh()) 
     2123                exporter->ExportViewCell(vc); 
     2124        else 
     2125        { 
     2126                PolygonContainer cell; 
     2127                mVspBspTree-> 
     2128                        ConstructGeometry(dynamic_cast<BspViewCell *>(vc), cell); 
     2129 
     2130                exporter->ExportPolygons(cell); 
     2131        } 
     2132} 
     2133 
     2134 
     2135int VspBspViewCellsManager::GetMaxTreeDiff(ViewCell *vc) const 
     2136{ 
     2137        BspViewCell *bspVc = dynamic_cast<BspViewCell *>(vc); 
     2138 
     2139        int maxDist = 0; 
     2140        // compute max height difference 
     2141        for (int i = 0; i < (int)bspVc->mLeaves.size(); ++ i) 
     2142                for (int j = 0; j < (int)bspVc->mLeaves.size(); ++ j) 
     2143        { 
     2144                BspLeaf *leaf = bspVc->mLeaves[i]; 
     2145 
     2146                if (i != j) 
     2147                { 
     2148                        BspLeaf *leaf2 = bspVc->mLeaves[j]; 
     2149                        int dist = mVspBspTree->TreeDistance(leaf, leaf2); 
     2150                        if (dist > maxDist) 
     2151                                maxDist = dist; 
     2152                } 
     2153        } 
     2154 
     2155        return maxDist; 
     2156} 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h

    r480 r482  
    2424class BspLeaf; 
    2525class ViewCellsStatistics; 
    26  
     26class Exporter; 
    2727struct BspRay; 
    2828 
     
    214214protected: 
    215215 
     216        void ParseEnvironment(); 
     217 
    216218        /** Recollects view cells and resets statistics. 
    217219        */ 
    218220        void ResetViewCells(); 
     221         
    219222        /** Collects the view cells in the view cell container. 
    220223        */ 
    221224        virtual void CollectViewCells() = 0; 
    222  
     225         
    223226        /** Evaluates view cells statistics and stores it in 
    224227                mViewCellsStatistics. 
    225228        */ 
    226229        void EvaluateViewCellsStats(); 
    227                  
     230 
     231 
     232        //-- helper functions for view cell visualization 
     233 
     234        /** Exports the view cell partition. 
     235        */ 
     236        void ExportViewCells(Exporter *exporter) const; 
     237 
     238        /** Sets exporter color. 
     239        */ 
     240        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0; 
     241 
     242        /** Exports view cell geometry. 
     243        */ 
     244        virtual void ExportGeometry(Exporter *exporter, ViewCell *vc) const = 0; 
     245 
    228246        /// the view cell corresponding to unbounded space 
    229247        ViewCell *mUnbounded; 
     
    247265        float mTotalAreaValid; 
    248266        float mTotalArea; 
     267 
     268        // visualization color code 
     269        int mColorCode; 
    249270 
    250271        ViewCellsStatistics mViewCellsStats; 
     
    307328        void ConstructBspRays(const VssRayContainer &rays, 
    308329                                                  const int numSamples); 
     330 
     331        void ExportColor(Exporter *exporter, ViewCell *vc) const; 
     332        void ExportGeometry(Exporter *exporter, ViewCell *vc) const; 
     333 
    309334        /// the BSP tree. 
    310335        BspTree *mBspTree; 
     
    367392        KdNode *GetNodeForPvs(KdLeaf *leaf); 
    368393 
     394        void ExportColor(Exporter *exporter, ViewCell *vc) const; 
     395        void ExportGeometry(Exporter *exporter, ViewCell *vc) const; 
    369396 
    370397        /// the BSP tree. 
     
    417444protected: 
    418445 
     446        void ExportLeaves(const ObjectContainer &objects, 
     447                                          const VssRayContainer &sampleRays); 
     448 
    419449        void CollectViewCells(); 
     450 
     451        void ExportColor(Exporter *exporter, ViewCell *vc) const; 
     452        void ExportGeometry(Exporter *exporter, ViewCell *vc) const; 
     453 
    420454 
    421455        /// the BSP tree. 
     
    480514        void CollectViewCells(); 
    481515 
     516        void ExportColor(Exporter *exporter, ViewCell *vc) const; 
     517        void ExportGeometry(Exporter *exporter, ViewCell *vc) const; 
     518 
     519        /** Returns maximal depth difference of view cell  
     520                leaves in tree. 
     521        */ 
     522        int GetMaxTreeDiff(ViewCell *vc) const; 
     523 
    482524        /// the view space partition BSP tree. 
    483525        VspBspTree *mVspBspTree; 
    484526 
     527        /// helper array of rays 
    485528        vector<BspRay *> mBspRays; 
    486529 
    487530private: 
    488531 
    489  
    490532        /** Exports visualization of the BSP splits. 
    491533        */ 
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp

    r481 r482  
    1919 
    2020//-- static members 
    21 /** Evaluates split plane classification with respect to the plane's  
     21/** Evaluates split plane classification with respect to the plane's 
    2222        contribution for a minimum number of ray splits. 
    2323*/ 
    2424const float VspBspTree::sLeastRaySplitsTable[] = {0, 0, 1, 1, 0}; 
    25 /** Evaluates split plane classification with respect to the plane's  
     25/** Evaluates split plane classification with respect to the plane's 
    2626        contribution for balanced rays. 
    2727*/ 
     
    2929 
    3030 
    31 int VspBspTree::sFrontId = 0;  
     31int VspBspTree::sFrontId = 0; 
    3232int VspBspTree::sBackId = 0; 
    3333int VspBspTree::sFrontAndBackId = 0; 
     
    3939/****************************************************************/ 
    4040 
    41 VspBspTree::VspBspTree():  
     41VspBspTree::VspBspTree(): 
    4242mRoot(NULL), 
    4343mPvsUseArea(true), 
    4444mCostNormalizer(Limits::Small), 
    4545mViewCellsManager(NULL), 
    46 mStoreRays(true) 
     46mStoreRays(true), 
     47mOnlyDrivingAxis(false) 
    4748{ 
    4849        mRootCell = new BspViewCell(); 
     
    5455        environment->GetIntValue("VspBspTree.Termination.minPvs", mTermMinPvs); 
    5556        environment->GetIntValue("VspBspTree.Termination.minRays", mTermMinRays); 
    56         environment->GetFloatValue("VspBspTree.Termination.minArea", mTermMinArea);      
     57        environment->GetFloatValue("VspBspTree.Termination.minArea", mTermMinArea); 
    5758        environment->GetFloatValue("VspBspTree.Termination.maxRayContribution", mTermMaxRayContribution); 
    5859        environment->GetFloatValue("VspBspTree.Termination.minAccRayLenght", mTermMinAccRayLength); 
     
    6566        environment->GetFloatValue("VspBspTree.Termination.ct_div_ci", mCtDivCi); 
    6667 
    67         //-- termination criteria for axis aligned split 
    68         environment->GetFloatValue("VspBspTree.Termination.AxisAligned.ct_div_ci", mAxisAlignedCtDivCi); 
     68        //-- max cost ratio for early tree termination 
    6969        environment->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio); 
    70          
    71         environment->GetIntValue("VspBspTree.Termination.AxisAligned.minRays",  
    72                                                          mTermMinRaysForAxisAligned); 
    73          
     70 
    7471        //-- partition criteria 
    7572        environment->GetIntValue("VspBspTree.maxPolyCandidates", mMaxPolyCandidates); 
    7673        environment->GetIntValue("VspBspTree.maxRayCandidates", mMaxRayCandidates); 
    7774        environment->GetIntValue("VspBspTree.splitPlaneStrategy", mSplitPlaneStrategy); 
    78         environment->GetFloatValue("VspBspTree.AxisAligned.splitBorder", mAxisAlignedSplitBorder); 
    7975 
    8076        environment->GetFloatValue("VspBspTree.Construction.epsilon", mEpsilon); 
     
    10096        Debug << "max polygon candidates: " << mMaxPolyCandidates << endl; 
    10197        Debug << "max plane candidates: " << mMaxRayCandidates << endl; 
    102          
     98 
    10399        Debug << "Split plane strategy: "; 
    104100        if (mSplitPlaneStrategy & RANDOM_POLYGON) 
     
    125121                Debug << "pvs"; 
    126122        } 
    127          
     123 
    128124        mSplitCandidates = new vector<SortableEntry>; 
    129125 
     
    132128 
    133129 
    134 const BspTreeStatistics &VspBspTree::GetStatistics() const  
     130const BspTreeStatistics &VspBspTree::GetStatistics() const 
    135131{ 
    136132        return mStat; 
     
    145141} 
    146142 
    147 int VspBspTree::AddMeshToPolygons(Mesh *mesh,  
    148                                                                   PolygonContainer &polys,  
     143int VspBspTree::AddMeshToPolygons(Mesh *mesh, 
     144                                                                  PolygonContainer &polys, 
    149145                                                                  MeshInstance *parent) 
    150146{ 
    151147        FaceContainer::const_iterator fi; 
    152          
     148 
    153149        // copy the face data to polygons 
    154150        for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi) 
    155151        { 
    156152                Polygon3 *poly = new Polygon3((*fi), mesh); 
    157                  
     153 
    158154                if (poly->Valid(mEpsilon)) 
    159155                { 
     
    167163} 
    168164 
    169 int VspBspTree::AddToPolygonSoup(const ViewCellContainer &viewCells,  
    170                                                           PolygonContainer &polys,  
     165int VspBspTree::AddToPolygonSoup(const ViewCellContainer &viewCells, 
     166                                                          PolygonContainer &polys, 
    171167                                                          int maxObjects) 
    172168{ 
    173         int limit = (maxObjects > 0) ?  
     169        int limit = (maxObjects > 0) ? 
    174170                Min((int)viewCells.size(), maxObjects) : (int)viewCells.size(); 
    175    
     171 
    176172        int polysSize = 0; 
    177173 
     
    181177                { 
    182178                        mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb 
    183                         polysSize +=  
    184                                 AddMeshToPolygons(viewCells[i]->GetMesh(),  
    185                                                                   polys,  
     179                        polysSize += 
     180                                AddMeshToPolygons(viewCells[i]->GetMesh(), 
     181                                                                  polys, 
    186182                                                                  viewCells[i]); 
    187183                } 
     
    191187} 
    192188 
    193 int VspBspTree::AddToPolygonSoup(const ObjectContainer &objects,  
    194                                                                  PolygonContainer &polys,  
     189int VspBspTree::AddToPolygonSoup(const ObjectContainer &objects, 
     190                                                                 PolygonContainer &polys, 
    195191                                                                 int maxObjects) 
    196192{ 
    197         int limit = (maxObjects > 0) ?  
     193        int limit = (maxObjects > 0) ? 
    198194                Min((int)objects.size(), maxObjects) : (int)objects.size(); 
    199    
     195 
    200196        for (int i = 0; i < limit; ++i) 
    201197        { 
     
    216212                        break; 
    217213                } 
    218                  
     214 
    219215        if (mesh) // copy the mesh data to polygons 
    220216                { 
     
    231227    mStat.nodes = 1; 
    232228        mBox.Initialize();      // initialise BSP tree bounding box 
    233          
     229 
    234230        PolygonContainer polys; 
    235231        RayInfoContainer *rays = new RayInfoContainer(); 
     
    247243        { 
    248244                VssRay *ray = *rit; 
    249          
     245 
    250246                if (ray->mTerminationObject && !ray->mTerminationObject->Mailed()) 
    251247                { 
     
    270266        { 
    271267                VssRay *ray = *rit; 
    272                  
     268 
    273269                float minT, maxT; 
    274270 
     
    277273                { 
    278274                        float len = ray->Length(); 
    279                          
    280                         if (!len)  
     275 
     276                        if (!len) 
    281277                                len = Limits::Small; 
    282                          
     278 
    283279                        rays->push_back(RayInfo(ray, minT / len, maxT / len)); 
    284280                } 
     
    290286        cout << "finished" << endl; 
    291287 
    292         Debug << "\nPolygon extraction: " << (int)polys.size() << " polys extracted from "  
     288        Debug << "\nPolygon extraction: " << (int)polys.size() << " polys extracted from " 
    293289                  << (int)sampleRays.size() << " rays in " 
    294290                  << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl << endl; 
     
    310306        ConstructGeometry(mRoot, *geom); 
    311307 
    312         VspBspTraversalData tData(mRoot,  
    313                                                           new PolygonContainer(polys),  
     308        VspBspTraversalData tData(mRoot, 
     309                                                          new PolygonContainer(polys), 
    314310                                                          0, 
    315                                                           rays,  
    316                               ComputePvsSize(*rays),  
    317                                                           geom->GetArea(),  
     311                                                          rays, 
     312                              ComputePvsSize(*rays), 
     313                                                          geom->GetArea(), 
    318314                                                          geom); 
    319315 
     
    324320 
    325321        long startTime = GetTime(); 
    326          
     322 
    327323        while (!tStack.empty()) 
    328324        { 
     
    335331 
    336332                if (r == mRoot) 
    337                         Debug << "VSP BSP tree construction time spent at root: "  
     333                        Debug << "VSP BSP tree construction time spent at root: " 
    338334                                  << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl << endl; 
    339335        } 
     
    346342bool VspBspTree::TerminationCriteriaMet(const VspBspTraversalData &data) const 
    347343{ 
    348         return  
     344        return 
    349345                (((int)data.mRays->size() <= mTermMinRays) || 
    350346                 (data.mPvs <= mTermMinPvs)   || 
     
    355351} 
    356352 
    357 BspNode *VspBspTree::Subdivide(VspBspTraversalStack &tStack,  
     353BspNode *VspBspTree::Subdivide(VspBspTraversalStack &tStack, 
    358354                                                           VspBspTraversalData &tData) 
    359355{ 
     
    363359        { 
    364360                PolygonContainer coincident; 
    365          
     361 
    366362                VspBspTraversalData tFrontData; 
    367363                VspBspTraversalData tBackData; 
     
    378374 
    379375                        // delete old leaf node 
    380                         DEL_PTR(tData.mNode);    
    381                 } 
    382         } 
    383          
     376                        DEL_PTR(tData.mNode); 
     377                } 
     378        } 
     379 
    384380        //-- terminate traversal and create new view cell 
    385381        if (newNode->IsLeaf()) 
    386382        { 
    387383                BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode); 
    388          
     384 
    389385                // create new view cell for this leaf 
    390386                BspViewCell *viewCell = new BspViewCell(); 
    391387                leaf->SetViewCell(viewCell); 
    392                  
     388 
    393389                if (mStoreRays) 
    394390                { 
     
    404400                int conSamp = 0, sampCon = 0; 
    405401                AddToPvs(leaf, *tData.mRays, conSamp, sampCon); 
    406                          
     402 
    407403                mStat.contributingSamples += conSamp; 
    408404                mStat.sampleContributions += sampCon; 
    409                  
     405 
    410406                EvaluateLeafStats(tData); 
    411407        } 
    412          
    413                  
     408 
     409 
    414410        //-- cleanup 
    415411        tData.Clear(); 
     
    436432                { 
    437433                        // terminate branch because of max cost 
    438                         ++ mStat.maxCostNodes;  
     434                        ++ mStat.maxCostNodes; 
    439435            return leaf; 
    440436                } 
     
    444440 
    445441        //-- subdivide further 
    446         BspInterior *interior = new BspInterior(splitPlane);  
     442        BspInterior *interior = new BspInterior(splitPlane); 
    447443 
    448444#ifdef _DEBUG 
    449445        Debug << interior << endl; 
    450446#endif 
    451          
     447 
    452448        //-- the front and back traversal data is filled with the new values 
    453449        frontData.mPolygons = new PolygonContainer(); 
     
    462458 
    463459        // subdivide rays 
    464         SplitRays(interior->GetPlane(),  
    465                           *tData.mRays,  
    466                           *frontData.mRays,  
     460        SplitRays(interior->GetPlane(), 
     461                          *tData.mRays, 
     462                          *frontData.mRays, 
    467463                          *backData.mRays); 
    468          
     464 
    469465        // subdivide polygons 
    470466        mStat.splits += SplitPolygons(interior->GetPlane(), 
    471                                                                   *tData.mPolygons,  
    472                                           *frontData.mPolygons,  
     467                                                                  *tData.mPolygons, 
     468                                          *frontData.mPolygons, 
    473469                                                                  *backData.mPolygons, 
    474470                                                                  coincident); 
    475471 
    476          
     472 
    477473        // how often was max cost ratio missed in this branch? 
    478474        frontData.mMaxCostMisses = maxCostMisses; 
     
    486482        if (1) 
    487483        { 
    488                 tData.mGeometry->SplitGeometry(*frontData.mGeometry,  
    489                                                                            *backData.mGeometry,  
     484                tData.mGeometry->SplitGeometry(*frontData.mGeometry, 
     485                                                                           *backData.mGeometry, 
    490486                                                                           interior->GetPlane(), 
    491487                                                                           mBox, 
    492488                                                                           mEpsilon); 
    493          
     489 
    494490                frontData.mArea = frontData.mGeometry->GetArea(); 
    495491                backData.mArea = backData.mGeometry->GetArea(); 
     
    517513        // and setup child links 
    518514        interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior)); 
    519          
     515 
    520516        frontData.mNode = interior->GetFront(); 
    521517        backData.mNode = interior->GetBack(); 
     
    526522 
    527523void VspBspTree::AddToPvs(BspLeaf *leaf, 
    528                                                   const RayInfoContainer &rays,  
     524                                                  const RayInfoContainer &rays, 
    529525                                                  int &sampleContributions, 
    530526                                                  int &contributingSamples) 
     
    542538                int contribution = 0; 
    543539                VssRay *ray = (*it).mRay; 
    544                          
     540 
    545541                if (ray->mTerminationObject) 
    546542                        contribution += vc->GetPvs().AddSample(ray->mTerminationObject); 
    547                  
     543 
    548544                if (ray->mOriginObject) 
    549545                        contribution += vc->GetPvs().AddSample(ray->mOriginObject); 
     
    567563                requestedSize < (int)(mSplitCandidates->capacity()/10) ) 
    568564        { 
    569         DEL_PTR(mSplitCandidates); 
     565        delete mSplitCandidates; 
    570566                mSplitCandidates = new vector<SortableEntry>; 
    571567        } 
     
    578574                bool positive = (*ri).mRay->HasPosDir(axis); 
    579575                mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax, 
    580                                                                                                   (*ri).ExtrapOrigin(axis), (void *)&*ri)); 
     576                                                                                                  (*ri).ExtrapOrigin(axis), (*ri).mRay)); 
    581577 
    582578                mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin, 
    583                                                                                                   (*ri).ExtrapTermination(axis), (void *)&*ri)); 
     579                                                                                                  (*ri).ExtrapTermination(axis), (*ri).mRay)); 
    584580        } 
    585581 
     
    593589                                          float &position) 
    594590{ 
    595         //      AxisAlignedBox3 dirBox = GetDirBBox(node); 
    596591        int raysBack; 
    597592        int raysFront; 
     
    599594        int pvsFront; 
    600595 
    601         //axis = box.Size().DrivingAxis(); 
    602  
    603596        SortSplitCandidates(rays, axis); 
    604597 
     
    630623 
    631624                        if (object) 
     625                        { 
    632626                                if (!object->Mailed()) 
    633627                                { 
     
    637631                                else 
    638632                                        ++ object->mCounter; 
     633                        } 
    639634                } 
    640635        } 
     
    652647                                { 
    653648                                        ++ rl; 
    654                                         ray = (VssRay *) (*ci).data; 
     649                                        ray = (VssRay *) (*ci).ray; 
     650 
    655651                                        Intersectable *object = ray->mTerminationObject; 
     652 
    656653                                        if (object && !object->Mailed()) 
    657654                                        { 
     
    664661                                { 
    665662                                        -- rr; 
    666  
    667                                         ray = (VssRay *) (*ci).data; 
     663                                        ray = (VssRay *) (*ci).ray; 
     664 
    668665                                        Intersectable *object = ray->mTerminationObject; 
    669666 
     
    671668                                        { 
    672669                                                if (-- object->mCounter == 0) 
    673                                                 -- pr; 
     670                                                        -- pr; 
    674671                                        } 
    675672 
     
    712709 
    713710 
    714 float VspBspTree::SelectAxisAlignedPlane(Plane3 &plane,  
     711float VspBspTree::SelectAxisAlignedPlane(Plane3 &plane, 
    715712                                                                                 const VspBspTraversalData &tData) 
    716713{ 
    717714        AxisAlignedBox3 box; 
    718715        box.Initialize(); 
    719          
     716 
    720717        // create bounding box of region 
    721718        RayInfoContainer::const_iterator ri, ri_end = tData.mRays->end(); 
     
    731728        int bestAxis = -1; 
    732729 
    733         bool mOnlyDrivingAxis = false; 
    734  
    735730        const int sAxis = box.Size().DrivingAxis(); 
    736                  
     731 
    737732        for (int axis = 0; axis < 3; ++ axis) 
    738733        { 
     
    748743                        else 
    749744                        { 
    750                                 nCostRatio[axis] =  
     745                                nCostRatio[axis] = 
    751746                                        BestCostRatioHeuristics(*tData.mRays, 
    752747                                                                                    box, 
     
    780775 
    781776 
    782 bool VspBspTree::SelectPlane(Plane3 &plane,  
    783                                                          BspLeaf *leaf,  
     777bool VspBspTree::SelectPlane(Plane3 &plane, 
     778                                                         BspLeaf *leaf, 
    784779                                                         VspBspTraversalData &data) 
    785780{ 
     
    799794                        //-- choose plane on midpoint of a ray 
    800795                        const int candidateIdx = (int)RandomValue(0, (Real)((int)data.mRays->size() - 1)); 
    801                                                                          
     796 
    802797                        const Vector3 minPt = (*data.mRays)[candidateIdx].ExtrapOrigin(); 
    803798                        const Vector3 maxPt = (*data.mRays)[candidateIdx].ExtrapTermination(); 
     
    806801 
    807802                        const Vector3 normal = (*data.mRays)[candidateIdx].mRay->GetDir(); 
    808                          
     803 
    809804                        plane = Plane3(normal, pt); 
    810805                        return true; 
     
    815810 
    816811        // use heuristics to find appropriate plane 
    817         return SelectPlaneHeuristics(plane, leaf, data);  
     812        return SelectPlaneHeuristics(plane, leaf, data); 
    818813} 
    819814 
    820815 
    821816Plane3 VspBspTree::ChooseCandidatePlane(const RayInfoContainer &rays) const 
    822 {        
     817{ 
    823818        const int candidateIdx = (int)RandomValue(0, (Real)((int)rays.size() - 1)); 
    824          
     819 
    825820        const Vector3 minPt = rays[candidateIdx].ExtrapOrigin(); 
    826821        const Vector3 maxPt = rays[candidateIdx].ExtrapTermination(); 
     
    834829 
    835830Plane3 VspBspTree::ChooseCandidatePlane2(const RayInfoContainer &rays) const 
    836 {        
     831{ 
    837832        Vector3 pt[3]; 
    838          
     833 
    839834        int idx[3]; 
    840835        int cmaxT = 0; 
     
    845840        { 
    846841                idx[j] = (int)RandomValue(0, (Real)((int)rays.size() * 2 - 1)); 
    847                          
     842 
    848843                if (idx[j] >= (int)rays.size()) 
    849844                { 
    850845                        idx[j] -= (int)rays.size(); 
    851                                  
     846 
    852847                        chooseMin = (cminT < 2); 
    853848                } 
     
    857852                RayInfo rayInf = rays[idx[j]]; 
    858853                pt[j] = chooseMin ? rayInf.ExtrapOrigin() : rayInf.ExtrapTermination(); 
    859         }        
     854        } 
    860855 
    861856        return Plane3(pt[0], pt[1], pt[2]); 
     
    863858 
    864859Plane3 VspBspTree::ChooseCandidatePlane3(const RayInfoContainer &rays) const 
    865 {        
     860{ 
    866861        Vector3 pt[3]; 
    867          
     862 
    868863        int idx1 = (int)RandomValue(0, (Real)((int)rays.size() - 1)); 
    869864        int idx2 = (int)RandomValue(0, (Real)((int)rays.size() - 1)); 
     
    881876        // vector from line 1 to line 2 
    882877        const Vector3 vd = ray2.ExtrapOrigin() - ray1.ExtrapOrigin(); 
    883          
     878 
    884879        // project vector on normal to get distance 
    885880        const float dist = DotProd(vd, norm); 
     
    891886} 
    892887 
    893 bool VspBspTree::SelectPlaneHeuristics(Plane3 &bestPlane,  
    894                                                                            BspLeaf *leaf,  
     888bool VspBspTree::SelectPlaneHeuristics(Plane3 &bestPlane, 
     889                                                                           BspLeaf *leaf, 
    895890                                                                           VspBspTraversalData &data) 
    896891{ 
     
    901896        const int limit = Min((int)data.mPolygons->size(), mMaxPolyCandidates); 
    902897        int maxIdx = (int)data.mPolygons->size(); 
    903          
     898 
    904899        float candidateCost; 
    905900 
     
    909904                const int candidateIdx = (int)RandomValue(0, (Real)(-- maxIdx)); 
    910905                //Debug << "current Idx: " << maxIdx << " cand idx " << candidateIdx << endl; 
    911                  
     906 
    912907                Polygon3 *poly = (*data.mPolygons)[candidateIdx]; 
    913908 
    914909                // swap candidate to the end to avoid testing same plane 
    915910                std::swap((*data.mPolygons)[maxIdx], (*data.mPolygons)[candidateIdx]); 
    916          
     911 
    917912                //Polygon3 *poly = (*data.mPolygons)[(int)RandomValue(0, (int)polys.size() - 1)]; 
    918913 
     
    926921                } 
    927922        } 
    928          
     923 
    929924#if 0 
    930925        //-- choose candidate planes extracted from rays 
     
    937932                if (candidateCost < lowestCost) 
    938933                { 
    939                         bestPlane = plane;       
     934                        bestPlane = plane; 
    940935                        lowestCost = candidateCost; 
    941936                } 
     
    944939 
    945940        // axis aligned splits 
    946         candidateCost = SelectAxisAlignedPlane(plane, data);  
     941        candidateCost = SelectAxisAlignedPlane(plane, data); 
    947942 
    948943        if (candidateCost < lowestCost) 
    949944        { 
    950                 bestPlane = plane;       
     945                bestPlane = plane; 
    951946                lowestCost = candidateCost; 
    952947        } 
     
    971966} 
    972967 
    973 float VspBspTree::SplitPlaneCost(const Plane3 &candidatePlane,  
     968float VspBspTree::SplitPlaneCost(const Plane3 &candidatePlane, 
    974969                                                                 const VspBspTraversalData &data) const 
    975970{ 
     
    978973        float sumBalancedRays = 0; 
    979974        float sumRaySplits = 0; 
    980          
     975 
    981976        int frontPvs = 0; 
    982977        int backPvs = 0; 
     
    993988                // create unique ids for pvs heuristics 
    994989                GenerateUniqueIdsForPvs(); 
    995          
     990 
    996991                if (mPvsUseArea) // use front and back cell areas to approximate volume 
    997                 {        
     992                { 
    998993                        // construct child geometry with regard to the candidate split plane 
    999994                        BspNodeGeometry frontCell; 
    1000995                        BspNodeGeometry backCell; 
    1001                  
    1002                         data.mGeometry->SplitGeometry(frontCell,  
    1003                                                                                   backCell,  
     996 
     997                        data.mGeometry->SplitGeometry(frontCell, 
     998                                                                                  backCell, 
    1004999                                                                                  candidatePlane, 
    10051000                                                                                  mBox, 
    10061001                                                                                  mEpsilon); 
    1007                  
     1002 
    10081003                        pFront = frontCell.GetArea(); 
    10091004                        pBack = backCell.GetArea(); 
     
    10121007                } 
    10131008        } 
    1014                  
     1009 
    10151010        int limit; 
    10161011        bool useRand; 
     
    10411036                        sumBalancedRays += cf; 
    10421037                } 
    1043                  
     1038 
    10441039                if (mSplitPlaneStrategy & BALANCED_RAYS) 
    10451040                { 
     
    10511046                { 
    10521047                        // in case the ray intersects an object 
    1053                         // assure that we only count the object  
     1048                        // assure that we only count the object 
    10541049                        // once for the front and once for the back side of the plane 
    1055                          
     1050 
    10561051                        // add the termination object 
    10571052                        AddObjToPvs(ray->mTerminationObject, cf, frontPvs, backPvs); 
    1058                          
     1053 
    10591054                        // add the source object 
    10601055                        AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs); 
    1061                          
     1056 
    10621057                        // use number or length of rays to approximate volume 
    10631058                        if (!mPvsUseArea) 
     
    10671062                                if (pvsUseLen) // use length of rays 
    10681063                                        len = rayInf.SqrSegmentLength(); 
    1069                          
     1064 
    10701065                                pOverall += len; 
    10711066 
     
    10771072                                { 
    10781073                                        // use length of rays to approximate volume 
    1079                                         if (pvsUseLen)  
     1074                                        if (pvsUseLen) 
    10801075                                        { 
    1081                                                 float newLen = len *  
    1082                                                         (rayInf.GetMaxT() - t) /  
     1076                                                float newLen = len * 
     1077                                                        (rayInf.GetMaxT() - t) / 
    10831078                                                        (rayInf.GetMaxT() - rayInf.GetMinT()); 
    1084                  
     1079 
    10851080                                                if (candidatePlane.Side(rayInf.ExtrapOrigin()) <= 0) 
    10861081                                                { 
     
    11051100 
    11061101        const float raysSize = (float)data.mRays->size() + Limits::Small; 
    1107          
     1102 
    11081103        if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) 
    11091104                cost += mLeastRaySplitsFactor * sumRaySplits / raysSize; 
     
    11111106        if (mSplitPlaneStrategy & BALANCED_RAYS) 
    11121107                cost += mBalancedRaysFactor * fabs(sumBalancedRays) /  raysSize; 
    1113          
     1108 
    11141109        // pvs criterium 
    11151110        if (mSplitPlaneStrategy & PVS) 
     
    11201115                //cost += mPvsFactor * 0.5 * (frontPvs * pFront + backPvs * pBack) / oldCost; 
    11211116                //Debug << "new cost: " << cost << " over" << frontPvs * pFront + backPvs * pBack << " old cost " << oldCost << endl; 
    1122          
     1117 
    11231118                if (0) // give penalty to unbalanced split 
    1124                         if (((pFront * 0.2 + Limits::Small) > pBack) ||  
     1119                        if (((pFront * 0.2 + Limits::Small) > pBack) || 
    11251120                                (pFront < (pBack * 0.2 + Limits::Small))) 
    11261121                                        cost += 0.5; 
     
    11291124#ifdef _DEBUG 
    11301125        Debug << "totalpvs: " << data.mPvs << " ptotal: " << pOverall 
    1131                   << " frontpvs: " << frontPvs << " pFront: " << pFront  
     1126                  << " frontpvs: " << frontPvs << " pFront: " << pFront 
    11321127                  << " backpvs: " << backPvs << " pBack: " << pBack << endl << endl; 
    11331128#endif 
    1134          
     1129 
    11351130        // normalize cost by sum of linear factors 
    11361131        return cost / (float)mCostNormalizer; 
     
    11501145        if (cf >= 0) 
    11511146        { 
    1152                 if ((obj->mMailbox != sFrontId) &&  
     1147                if ((obj->mMailbox != sFrontId) && 
    11531148                        (obj->mMailbox != sFrontAndBackId)) 
    11541149                { 
     
    11561151 
    11571152                        if (obj->mMailbox == sBackId) 
    1158                                 obj->mMailbox = sFrontAndBackId;         
     1153                                obj->mMailbox = sFrontAndBackId; 
    11591154                        else 
    1160                                 obj->mMailbox = sFrontId;                                                                
    1161                 } 
    1162         } 
    1163          
     1155                                obj->mMailbox = sFrontId; 
     1156                } 
     1157        } 
     1158 
    11641159        if (cf <= 0) 
    11651160        { 
     
    11701165 
    11711166                        if (obj->mMailbox == sFrontId) 
    1172                                 obj->mMailbox = sFrontAndBackId;  
     1167                                obj->mMailbox = sFrontAndBackId; 
    11731168                        else 
    1174                                 obj->mMailbox = sBackId;                                 
     1169                                obj->mMailbox = sBackId; 
    11751170                } 
    11761171        } 
     
    11811176        stack<BspNode *> nodeStack; 
    11821177        nodeStack.push(mRoot); 
    1183   
    1184         while (!nodeStack.empty())  
     1178 
     1179        while (!nodeStack.empty()) 
    11851180        { 
    11861181                BspNode *node = nodeStack.top(); 
    1187      
     1182 
    11881183                nodeStack.pop(); 
    1189      
    1190                 if (node->IsLeaf())  
    1191                 { 
    1192                         BspLeaf *leaf = (BspLeaf *)node;                 
     1184 
     1185                if (node->IsLeaf()) 
     1186                { 
     1187                        BspLeaf *leaf = (BspLeaf *)node; 
    11931188                        leaves.push_back(leaf); 
    1194                 }  
    1195                 else  
     1189                } 
     1190                else 
    11961191                { 
    11971192                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     
    12241219        if (data.mDepth < mStat.minDepth) 
    12251220                mStat.minDepth = data.mDepth; 
    1226          
     1221 
    12271222        if (data.mDepth >= mTermMaxDepth) 
    12281223                ++ mStat.maxDepthNodes; 
     
    12361231        if (data.GetAvgRayContribution() > mTermMaxRayContribution) 
    12371232                ++ mStat.maxRayContribNodes; 
    1238          
    1239         if (data.mArea <= mTermMinArea)  
     1233 
     1234        if (data.mArea <= mTermMinArea) 
    12401235        { 
    12411236                //Debug << "area: " << data.mArea / mBox.SurfaceArea() << " min area: " << mTermMinArea / mBox.SurfaceArea() << endl; 
     
    12501245                  << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), " 
    12511246                  << "Area: " << data.mArea << " (min: " << mTermMinArea << "), " 
    1252                   << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "  
     1247                  << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), " 
    12531248                  << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, " 
    12541249                  << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl; 
     
    12591254{ 
    12601255        int hits = 0; 
    1261    
     1256 
    12621257        stack<BspRayTraversalData> tStack; 
    1263    
     1258 
    12641259        float maxt, mint; 
    12651260 
     
    12711266        Vector3 entp = ray.Extrap(mint); 
    12721267        Vector3 extp = ray.Extrap(maxt); 
    1273    
     1268 
    12741269        BspNode *node = mRoot; 
    12751270        BspNode *farChild = NULL; 
    1276          
     1271 
    12771272        while (1) 
    12781273        { 
    1279                 if (!node->IsLeaf())  
     1274                if (!node->IsLeaf()) 
    12801275                { 
    12811276                        BspInterior *in = dynamic_cast<BspInterior *>(node); 
     
    12911286                                if(extSide <= 0) // plane does not split ray => no far child 
    12921287                                        continue; 
    1293                                          
     1288 
    12941289                                farChild = in->GetFront(); // plane splits ray 
    12951290 
     
    13011296                                        continue; 
    13021297 
    1303                                 farChild = in->GetBack(); // plane splits ray                    
     1298                                farChild = in->GetBack(); // plane splits ray 
    13041299                        } 
    13051300                        else // ray and plane are coincident 
     
    13181313                        extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t); 
    13191314                        maxt *= t; 
    1320                          
     1315 
    13211316                } else // reached leaf => intersection with view cell 
    13221317                { 
    13231318                        BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); 
    1324        
     1319 
    13251320                        if (!leaf->GetViewCell()->Mailed()) 
    13261321                        { 
     
    13291324                                ++ hits; 
    13301325                        } 
    1331                          
     1326 
    13321327                        //-- fetch the next far child from the stack 
    13331328                        if (tStack.empty()) 
    13341329                                break; 
    1335        
     1330 
    13361331                        entp = extp; 
    13371332                        mint = maxt; // NOTE: need this? 
     
    13571352        Exporter *exporter = Exporter::GetExporter(filename); 
    13581353 
    1359         if (exporter)  
     1354        if (exporter) 
    13601355        { 
    13611356                //exporter->ExportVspBspTree(*this); 
    13621357                return true; 
    1363         }        
     1358        } 
    13641359 
    13651360        return false; 
     
    13731368        ViewCell::NewMail(); 
    13741369 
    1375         while (!nodeStack.empty())  
     1370        while (!nodeStack.empty()) 
    13761371        { 
    13771372                BspNode *node = nodeStack.top(); 
    13781373                nodeStack.pop(); 
    13791374 
    1380                 if (node->IsLeaf())  
     1375                if (node->IsLeaf()) 
    13811376                { 
    13821377                        ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
    13831378 
    1384                         if (!viewCell->Mailed())  
     1379                        if (!viewCell->Mailed()) 
    13851380                        { 
    13861381                                viewCell->Mail(); 
     
    13881383                        } 
    13891384                } 
    1390                 else  
     1385                else 
    13911386                { 
    13921387                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     
    13991394 
    14001395 
    1401 BspTreeStatistics &VspBspTree::GetStat() 
    1402 { 
    1403         return mStat; 
    1404 } 
    1405  
    1406  
    14071396float VspBspTree::AccumulatedRayLength(const RayInfoContainer &rays) const 
    14081397{ 
     
    14191408 
    14201409int VspBspTree::SplitRays(const Plane3 &plane, 
    1421                                                   RayInfoContainer &rays,  
    1422                                                   RayInfoContainer &frontRays,  
     1410                                                  RayInfoContainer &rays, 
     1411                                                  RayInfoContainer &frontRays, 
    14231412                                                  RayInfoContainer &backRays) 
    14241413{ 
     
    14351424                        // get classification and receive new t 
    14361425                const int cf = bRay.ComputeRayIntersection(plane, t); 
    1437          
     1426 
    14381427                switch (cf) 
    14391428                { 
     
    14441433                        frontRays.push_back(bRay); 
    14451434                        break; 
    1446                 case 0:  
     1435                case 0: 
    14471436                        //-- split ray 
    14481437                        //--  look if start point behind or in front of plane 
    14491438                        if (plane.Side(bRay.ExtrapOrigin()) <= 0) 
    1450                         {        
     1439                        { 
    14511440                                backRays.push_back(RayInfo(ray, bRay.GetMinT(), t)); 
    14521441                                frontRays.push_back(RayInfo(ray, t, bRay.GetMaxT())); 
     
    14791468                // split plane of node itself 
    14801469                n = n->GetParent(); 
    1481                  
     1470 
    14821471                if (n) 
    14831472                { 
     
    14941483} 
    14951484 
    1496 void VspBspTree::ConstructGeometry(BspNode *n,  
     1485void VspBspTree::ConstructGeometry(BspNode *n, 
    14971486                                                                   BspNodeGeometry &cell) const 
    14981487{ 
     
    15021491} 
    15031492 
    1504 void VspBspTree::ConstructGeometry(BspNode *n,  
     1493void VspBspTree::ConstructGeometry(BspNode *n, 
    15051494                                                                   PolygonContainer &cell) const 
    15061495{ 
     
    15151504        { 
    15161505                Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]); 
    1517                  
     1506 
    15181507                if (p->Valid(mEpsilon)) 
    15191508                { 
     
    15271516        { 
    15281517                VertexContainer vertices; 
    1529          
     1518 
    15301519                for (int j = 0; j < 4; ++ j) 
    15311520                        vertices.push_back(mBox.GetFace(i).mVertices[j]); 
     
    15451534                        Polygon3 *frontPoly, *backPoly; 
    15461535 
    1547                         const int cf =  
     1536                        const int cf = 
    15481537                                candidatePolys[i]->ClassifyPlane(halfSpaces[j], 
    15491538                                                                                                 mEpsilon); 
    1550                          
     1539 
    15511540                        switch (cf) 
    15521541                        { 
     
    15551544                                        backPoly = new Polygon3(); 
    15561545 
    1557                                         candidatePolys[i]->Split(halfSpaces[j],  
    1558                                                                                          *frontPoly,  
     1546                                        candidatePolys[i]->Split(halfSpaces[j], 
     1547                                                                                         *frontPoly, 
    15591548                                                                                         *backPoly, 
    15601549                                                                                         mEpsilon); 
     
    15791568                        } 
    15801569                } 
    1581                  
     1570 
    15821571                if (candidatePolys[i]) 
    15831572                        cell.push_back(candidatePolys[i]); 
     
    15951584} 
    15961585 
    1597 int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,  
     1586int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, 
    15981587                                                   const bool onlyUnmailed) const 
    15991588{ 
     
    16041593        stack<BspNode *> nodeStack; 
    16051594        nodeStack.push(mRoot); 
    1606                  
     1595 
    16071596        // planes needed to verify that we found neighbor leaf. 
    16081597        vector<Plane3> halfSpaces; 
    16091598        ExtractHalfSpaces(n, halfSpaces); 
    16101599 
    1611         while (!nodeStack.empty())  
     1600        while (!nodeStack.empty()) 
    16121601        { 
    16131602                BspNode *node = nodeStack.top(); 
     
    16221611                                PolygonContainer neighborCandidate; 
    16231612                                ConstructGeometry(node, neighborCandidate); 
    1624                                  
     1613 
    16251614                                bool isAdjacent = true; 
    16261615                                for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i) 
    16271616                                { 
    1628                                         const int cf =  
    1629                                                 Polygon3::ClassifyPlane(neighborCandidate,  
     1617                                        const int cf = 
     1618                                                Polygon3::ClassifyPlane(neighborCandidate, 
    16301619                                                                                                halfSpaces[i], 
    16311620                                                                                                mEpsilon); 
     
    16411630                        } 
    16421631                } 
    1643                 else  
     1632                else 
    16441633                { 
    16451634                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
    1646          
    1647                         const int cf = Polygon3::ClassifyPlane(cell,  
    1648                                                                                                    interior->GetPlane(),  
     1635 
     1636                        const int cf = Polygon3::ClassifyPlane(cell, 
     1637                                                                                                   interior->GetPlane(), 
    16491638                                                                                                   mEpsilon); 
    16501639 
     
    16541643                                if (cf == Polygon3::BACK_SIDE) 
    16551644                                        nodeStack.push(interior->GetBack()); 
    1656                                 else  
     1645                                else 
    16571646                                { 
    16581647                                        // random decision 
     
    16621651                } 
    16631652        } 
    1664          
     1653 
    16651654        CLEAR_CONTAINER(cell); 
    16661655        return (int)neighbors.size(); 
     
    16711660    stack<BspNode *> nodeStack; 
    16721661        nodeStack.push(mRoot); 
    1673          
     1662 
    16741663        int mask = rand(); 
    1675    
    1676         while (!nodeStack.empty())  
     1664 
     1665        while (!nodeStack.empty()) 
    16771666        { 
    16781667                BspNode *node = nodeStack.top(); 
    16791668                nodeStack.pop(); 
    1680            
    1681                 if (node->IsLeaf())  
     1669 
     1670                if (node->IsLeaf()) 
    16821671                { 
    16831672                        return dynamic_cast<BspLeaf *>(node); 
    1684                 }  
    1685                 else  
     1673                } 
     1674                else 
    16861675                { 
    16871676                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
    1688                          
     1677 
    16891678                        BspNode *next; 
    1690          
     1679 
    16911680                        PolygonContainer cell; 
    16921681 
     
    17011690                                if (cf == Polygon3::FRONT_SIDE) 
    17021691                                        next = interior->GetFront(); 
    1703                         else  
     1692                        else 
    17041693                        { 
    17051694                                // random decision 
     
    17141703                } 
    17151704        } 
    1716          
     1705 
    17171706        return NULL; 
    17181707} 
     
    17211710{ 
    17221711        stack<BspNode *> nodeStack; 
    1723          
     1712 
    17241713        nodeStack.push(mRoot); 
    17251714 
    17261715        int mask = rand(); 
    1727          
    1728         while (!nodeStack.empty())  
     1716 
     1717        while (!nodeStack.empty()) 
    17291718        { 
    17301719                BspNode *node = nodeStack.top(); 
    17311720                nodeStack.pop(); 
    1732                  
    1733                 if (node->IsLeaf())  
     1721 
     1722                if (node->IsLeaf()) 
    17341723                { 
    17351724                        if ( (!onlyUnmailed || !node->Mailed()) ) 
    17361725                                return dynamic_cast<BspLeaf *>(node); 
    17371726                } 
    1738                 else  
     1727                else 
    17391728                { 
    17401729                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     
    17491738                } 
    17501739        } 
    1751          
     1740 
    17521741        return NULL; 
    17531742} 
     
    17641753        { 
    17651754                VssRay *ray = (*rit).mRay; 
    1766                  
     1755 
    17671756                if (ray->mOriginObject) 
    17681757                { 
     
    17971786 
    17981787int VspBspTree::SplitPolygons(const Plane3 &plane, 
    1799                                                           PolygonContainer &polys,  
    1800                                                           PolygonContainer &frontPolys,  
    1801                                                           PolygonContainer &backPolys,  
     1788                                                          PolygonContainer &polys, 
     1789                                                          PolygonContainer &frontPolys, 
     1790                                                          PolygonContainer &backPolys, 
    18021791                                                          PolygonContainer &coincident) const 
    18031792{ 
     
    18161805                        case Polygon3::COINCIDENT: 
    18171806                                coincident.push_back(poly); 
    1818                                 break;                   
    1819                         case Polygon3::FRONT_SIDE:       
     1807                                break; 
     1808                        case Polygon3::FRONT_SIDE: 
    18201809                                frontPolys.push_back(poly); 
    18211810                                break; 
     
    18441833        int hits = 0; 
    18451834        stack<BspRayTraversalData> tStack; 
    1846    
     1835 
    18471836        float mint = 0.0f, maxt = 1.0f; 
    1848    
     1837 
    18491838        Intersectable::NewMail(); 
    1850    
     1839 
    18511840        Vector3 entp = origin; 
    18521841        Vector3 extp = termination; 
    1853    
     1842 
    18541843        BspNode *node = mRoot; 
    18551844        BspNode *farChild = NULL; 
    1856    
    1857         while (1)  
    1858         { 
    1859                 if (!node->IsLeaf())   
     1845 
     1846        while (1) 
     1847        { 
     1848                if (!node->IsLeaf()) 
    18601849                { 
    18611850                        BspInterior *in = dynamic_cast<BspInterior *>(node); 
    1862            
     1851 
    18631852                        Plane3 splitPlane = in->GetPlane(); 
    18641853                        const int entSide = splitPlane.Side(entp); 
    18651854                        const int extSide = splitPlane.Side(extp); 
    1866            
    1867                         if (entSide < 0)  
     1855 
     1856                        if (entSide < 0) 
    18681857                        { 
    18691858                                node = in->GetBack(); 
    1870                  
     1859 
    18711860                                if(extSide <= 0) // plane does not split ray => no far child 
    18721861                                        continue; 
    1873                  
     1862 
    18741863                                farChild = in->GetFront(); // plane splits ray 
    1875                         } else if (entSide > 0)  
     1864                        } else if (entSide > 0) 
    18761865                        { 
    18771866                                node = in->GetFront(); 
    1878                    
     1867 
    18791868                                if (extSide >= 0) // plane does not split ray => no far child 
    18801869                                        continue; 
    1881                    
    1882                                 farChild = in->GetBack(); // plane splits ray                    
     1870 
     1871                                farChild = in->GetBack(); // plane splits ray 
    18831872                        } 
    18841873                        else // ray and plane are coincident 
     
    18891878                                continue; 
    18901879                        } 
    1891            
     1880 
    18921881                        // push data for far child 
    18931882                        tStack.push(BspRayTraversalData(farChild, extp, maxt)); 
    1894            
     1883 
    18951884                        // find intersection of ray segment with plane 
    18961885                        float t; 
    18971886                        extp = splitPlane.FindIntersection(origin, extp, &t); 
    18981887                        maxt *= t; 
    1899            
    1900                 } else  
     1888 
     1889                } else 
    19011890                { 
    19021891                        // reached leaf => intersection with view cell 
    19031892                        BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); 
    1904            
    1905                         if (!leaf->GetViewCell()->Mailed())  
     1893 
     1894                        if (!leaf->GetViewCell()->Mailed()) 
    19061895                        { 
    19071896                                viewcells.push_back(leaf->GetViewCell()); 
     
    19091898                                ++ hits; 
    19101899                        } 
    1911            
     1900 
    19121901                        //-- fetch the next far child from the stack 
    19131902                        if (tStack.empty()) 
    19141903                                break; 
    1915        
     1904 
    19161905                        entp = extp; 
    19171906                        mint = maxt; // NOTE: need this? 
    1918            
    1919                          
     1907 
     1908 
    19201909                        BspRayTraversalData &s = tStack.top(); 
    1921              
     1910 
    19221911                        node = s.mNode; 
    19231912                        extp = s.mExitPoint; 
    19241913                        maxt = s.mMaxT; 
    1925            
     1914 
    19261915                        tStack.pop(); 
    19271916                } 
     
    19301919} 
    19311920 
     1921int VspBspTree::TreeDistance(BspNode *n1, BspNode *n2) 
     1922{ 
     1923        std::deque<BspNode *> path1; 
     1924        BspNode *p1 = n1; 
     1925 
     1926        // create path from node 1 to root 
     1927        while (p1) 
     1928        { 
     1929                if (p1 == n2) // second node on path 
     1930                        return (int)path1.size(); 
     1931 
     1932                path1.push_front(p1); 
     1933                p1 = p1->GetParent(); 
     1934        } 
     1935 
     1936        int depth = n2->GetDepth(); 
     1937        int d = depth; 
     1938 
     1939        BspNode *p2 = n2; 
     1940 
     1941        // compare with same depth 
     1942        while (1) 
     1943        { 
     1944                if ((d < (int)path1.size()) && (p2 == path1[d])) 
     1945                        return (depth - d) + ((int)path1.size() - 1 - d); 
     1946 
     1947                -- d; 
     1948                p2 = p2->GetParent(); 
     1949        } 
     1950 
     1951        return 0; // never come here 
     1952} 
    19321953 
    19331954BspNode *VspBspTree::CollapseTree(BspNode *node) 
     
    19521973 
    19531974                        BspLeaf *leaf = new BspLeaf(interior->GetParent(), vc); 
    1954                          
     1975 
    19551976                        // replace a link from node's parent 
    19561977                        if (leaf->GetParent()) 
     
    20482069 
    20492070        int merged = 0; 
     2071        int mergedSiblings = 0; 
     2072        int accTreeDist = 0; 
     2073        int maxTreeDist = 0; 
     2074        const bool mergeStats = true; 
     2075 
    20502076 
    20512077        //-- use priority queue to merge leaf pairs 
    20522078        while (!mergeQueue.empty() && (vcSize > mMergeMinViewCells) && 
    2053                    (mergeQueue.top().GetMergeCost() <  
     2079                   (mergeQueue.top().GetMergeCost() < 
    20542080                    mMergeMaxCostRatio * BspMergeCandidate::sOverallCost)) 
    20552081        { 
     
    20662092                        ViewCell::NewMail(); 
    20672093                        MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2()); 
    2068  
    2069                         ++ merged; 
    20702094                        -- vcSize; 
    20712095                        // increase absolute merge cost 
    20722096                        BspMergeCandidate::sOverallCost += mc.GetMergeCost(); 
     2097 
     2098                        //-- stats 
     2099                        ++ merged; 
     2100 
     2101                        if (mc.GetLeaf1()->IsSibling(mc.GetLeaf2())) 
     2102                                ++ mergedSiblings; 
     2103 
     2104                        if (mergeStats) 
     2105                        { 
     2106                                const int dist = TreeDistance(mc.GetLeaf1(), mc.GetLeaf2()); 
     2107 
     2108                                if (dist > maxTreeDist) 
     2109                                        maxTreeDist = dist; 
     2110 
     2111                                accTreeDist += dist; 
     2112                        } 
    20732113                } 
    20742114                // merge candidate not valid, because one of the leaves was already 
     
    20882128        RepairVcLeafLists(); 
    20892129 
    2090         //Debug << "merged " << merged << " of " << savedVcSize << " leaves" << endl; 
     2130        Debug << "Merged " << merged << " nodes of " << savedVcSize 
     2131                  << " (merged " << mergedSiblings << " siblings)" << endl; 
     2132 
     2133        if (mergeStats) 
     2134        { 
     2135                Debug << "maximal tree distance: " << maxTreeDist << endl; 
     2136                Debug << "Avg tree distance: " << (float)accTreeDist / (float)merged << endl; 
     2137        } 
    20912138 
    20922139        //TODO: should return sample contributions 
     
    20972144bool VspBspTree::MergeViewCells(BspLeaf *l1, BspLeaf *l2) 
    20982145{ 
    2099         //-- change pointer to view cells of all leaves associated  
     2146        //-- change pointer to view cells of all leaves associated 
    21002147        //-- with the previous view cells 
    21012148        BspViewCell *fVc = l1->GetViewCell(); 
     
    21842231        //-- to new rendering cost (size of merged view cell times pvs size) 
    21852232        const float oldCost = GetLeaf1Cost() + GetLeaf2Cost(); 
    2186          
     2233 
    21872234        const float newCost = 
    21882235                (float)vcPvs * (vc1->GetArea() + vc2->GetArea()); 
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h

    r481 r482  
    191191        enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES}; 
    192192 
    193         /** Returns statistics. 
    194         */ 
    195         BspTreeStatistics &GetStat(); 
    196  
    197193        /** finds neighbouring leaves of this tree node. 
    198194        */ 
     
    255251        BspNode *CollapseTree(BspNode *node); 
    256252         
     253 
     254        /** Returns distance from node 1 to node 2. 
     255        */ 
     256        int TreeDistance(BspNode *n1, BspNode *n2); 
    257257 
    258258protected: 
     
    271271                int type; 
    272272                float value; 
    273                 void *data; 
     273                VssRay *ray; 
    274274   
    275275                SortableEntry() {} 
    276                 SortableEntry(const int t, const float v, void *d):type(t), 
    277                                           value(v), data(d)  
     276                SortableEntry(const int t, const float v, VssRay *r):type(t), 
     277                                          value(v), ray(r)  
    278278                { 
    279279                } 
     
    580580        vector<SortableEntry> *mSplitCandidates; 
    581581 
     582        bool mOnlyDrivingAxis; 
     583 
     584 
    582585private: 
    583586         
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp

    r480 r482  
    343343 
    344344 
    345  
    346 VspKdTree::VspKdTree(): mOnlyDrivingAxis(true) 
     345VspKdTree::VspKdTree(): mOnlyDrivingAxis(false) 
    347346{ 
    348347        environment->GetIntValue("VspKdTree.Termination.maxDepth", mTermMaxDepth); 
     
    431430        app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n"; 
    432431 
    433         app << "#N_SPLITS ( Number of splits in axes x y z dx dy dz \n"; 
    434  
    435         for (int i=0; i<7; i++) 
    436                 app << splits[i] <<" "; 
     432        app << "#N_SPLITS ( Number of splits in axes x y z\n"; 
     433 
     434        for (int i = 0; i < 3; ++ i) 
     435                app << splits[i] << " "; 
    437436        app << endl; 
    438437 
     
    466465                maxRayContribNodes * 100 / (double)Leaves() << endl; 
    467466 
     467        app << "#N_PMAXRCOSTLEAVES  ( Percentage of leaves with maximal cost ratio )\n" << 
     468                maxCostNodes * 100 / (double)Leaves() << endl; 
     469 
    468470        app << "#N_ADDED_RAYREFS  ( Number of dynamically added ray references )\n"<< 
    469471                addedRayRefs << endl; 
     
    476478        app << "#N_( Maximal PVS size / leaf)\n" 
    477479                << maxPvsSize << endl; 
     480 
     481        app << "#N_( Average PVS size / leaf)\n" 
     482                << (double) accPvsSize / (double)Leaves() << endl; 
    478483 
    479484        app << "#N_CTIME  ( Construction time [s] )\n" 
     
    550555                //leaf->AddRay(RayInfo(*ri)); 
    551556                VssRay *ray = *ri; 
    552  
    553557                float minT, maxT; 
    554  
    555                 // TODO: not very efficient to implictly cast between rays types ... 
     558                 
     559                // TODO: not very efficient to implictly cast between rays types! 
    556560                if (mBox.GetRaySegment(*ray, minT, maxT)) 
    557561                { 
     
    692696        if (costRatio > mTermMaxCostRatio) 
    693697        { 
    694                 Debug << "Too big cost ratio " << costRatio << endl; 
    695                 return -1; 
     698                //Debug << "Too big cost ratio " << costRatio << endl; 
     699                ++ leaf->mMaxCostMisses; 
     700                if (leaf->mMaxCostMisses > mTermMissTolerance) 
     701                { 
     702                        ++ mStat.maxCostNodes; 
     703                        return -1; 
     704                } 
    696705        } 
    697706 
     
    721730        pvsBack = 0; 
    722731 
    723         float newCost; 
    724  
    725732        Intersectable::NewMail(3); 
    726733 
    727734        // eval pvs size 
    728         int pvsSize = leaf->GetPvsSize(); 
     735        const int pvsSize = leaf->GetPvsSize(); 
    729736 
    730737        // this is the main ray classification loop! 
     
    748755        } 
    749756 
     757        //-- only one of these options should be one 
     758 
     759        if (0) //-- only pvs 
     760        { 
     761                const float sum = float(pvsBack + pvsFront); 
     762                const float oldCost = (float)pvsSize; 
     763 
     764                return sum / oldCost; 
     765        } 
     766 
     767        //-- pvs + probability heuristics 
     768        float pBack, pFront, pOverall; 
     769 
    750770        if (0) 
    751771        { 
    752                 const float sum = float(pvsBack + pvsFront); 
    753                 const float oldCost = (float)pvsSize * 2; 
    754  
    755                 return sum / oldCost; 
    756         } 
    757         else 
    758         { 
    759 #if 0 
    760                 float minBox = box.Min(axis); 
    761                 float maxBox = box.Max(axis); 
    762  
    763                 float sizeBox = maxBox - minBox; 
    764 #else 
    765  
    766                 float minBox = AxisAlignedBox3(box.Min(), position).SurfaceArea(); 
    767                 float maxBox = AxisAlignedBox3(position, box.Max()).SurfaceArea(); 
    768  
    769                 float sizeBox = box.SurfaceArea(); 
    770 #endif 
    771  
    772                 // float sum = raysBack*(position - minBox) + raysFront*(maxBox - position); 
    773                 const float sum = pvsBack * (position - minBox) + pvsFront * (maxBox - position); 
    774  
    775                 newCost = mCtDivCi + sum  / sizeBox; 
    776  
    777                 //Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl; 
    778                 //  float oldCost = leaf->mRays.size(); 
    779                 const float oldCost = (float)pvsSize; 
    780  
    781                 return newCost / oldCost; 
    782         } 
     772                // box length substitute for probability 
     773                const float minBox = box.Min(axis); 
     774                const float maxBox = box.Max(axis); 
     775 
     776                pBack = position - minBox; 
     777                pFront = maxBox - position; 
     778                pOverall = maxBox - minBox; 
     779        } 
     780 
     781        if (1) //-- area substitute for probability 
     782        { 
     783                 
     784                const bool useMidSplit = true; 
     785                //const bool useMidSplit = false; 
     786                         
     787                pOverall = box.SurfaceArea(); 
     788                         
     789                if (!useMidSplit) 
     790                { 
     791                        Vector3 pos = box.Max(); pos[axis] = position; 
     792                        pBack = AxisAlignedBox3(box.Min(), pos).SurfaceArea(); 
     793 
     794                        pos = box.Min(); pos[axis] = position; 
     795                        pFront = AxisAlignedBox3(pos, box.Max()).SurfaceArea(); 
     796                } 
     797                else 
     798                { 
     799                        //-- simplified computation for mid split 
     800                        const int axis2 = (axis + 1) % 3; 
     801                        const int axis3 = (axis + 2) % 3; 
     802 
     803                        const float faceArea =  
     804                                (box.Max(axis2) - box.Min(axis2)) * 
     805                                (box.Max(axis3) - box.Min(axis3)); 
     806 
     807                        pBack = pFront = pOverall * 0.5f + faceArea; 
     808                } 
     809        } 
     810 
     811        //-- ray density substitute for probability 
     812        if (1) 
     813        { 
     814                pBack = (float)raysBack; 
     815                pFront = (float)raysFront; 
     816                pOverall = (float)leaf->mRays.size(); 
     817        } 
     818 
     819        //Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl; 
     820        //Debug << pFront << " " << pBack << " " << pOverall << endl; 
     821 
     822        // float sum = raysBack*(position - minBox) + raysFront*(maxBox - position); 
     823        const float newCost = pvsBack * pBack + pvsFront * pFront; 
     824        //  float oldCost = leaf->mRays.size(); 
     825        const float oldCost = (float)pvsSize * pOverall; 
     826 
     827        return  (mCtDivCi + newCost) / oldCost; 
    783828} 
    784829 
     
    800845 
    801846        //AxisAlignedBox3 sBox = GetBBox(leaf); 
    802  
    803847        const int sAxis = box.Size().DrivingAxis(); 
    804848 
     
    912956                                { 
    913957                                        ++ rl; 
    914                                         ray = (VssRay *) (*ci).data; 
     958                                        ray = (*ci).ray; 
    915959                                        Intersectable *object = ray->mTerminationObject; 
    916960                                        if (object && !object->Mailed()) 
     
    925969                                        -- rr; 
    926970 
    927                                         ray = (VssRay *) (*ci).data; 
     971                                        ray = (*ci).ray; 
    928972                                        Intersectable *object = ray->mTerminationObject; 
    929973 
     
    9921036                bool positive = (*ri).mRay->HasPosDir(axis); 
    9931037                mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax, 
    994                                                                                                   (*ri).ExtrapOrigin(axis), (void *)&*ri)); 
     1038                                                                                                  (*ri).ExtrapOrigin(axis), (*ri).mRay)); 
    9951039 
    9961040                mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin, 
    997                                                                                                   (*ri).ExtrapTermination(axis), (void *)&*ri)); 
     1041                                                                                                  (*ri).ExtrapTermination(axis), (*ri).mRay)); 
    9981042        } 
    9991043 
     
    10131057                mStat.maxRayRefs = (int)leaf->mRays.size(); 
    10141058 
     1059        mStat.rays += (int)leaf->mRays.size(); 
    10151060 
    10161061        if (data.mDepth >= mTermMaxDepth) 
     
    10201065                ++ mStat.minPvsNodes; 
    10211066 
     1067        mStat.accPvsSize += leaf->GetPvsSize(); 
     1068 
    10221069        if ((int)leaf->GetRays().size() < mTermMinRays) 
    10231070                ++ mStat.minRaysNodes; 
     
    10361083        return ((leaf->GetPvsSize() < mTermMinPvs) || 
    10371084                    (leaf->mRays.size() < mTermMinRays) || 
    1038                         //(leaf->GetAvgRayContribution() > mTermMaxRayContribution ) || 
     1085                        (leaf->GetAvgRayContribution() > mTermMaxRayContribution ) || 
    10391086                        (leaf->mDepth >= mTermMaxDepth) || 
    10401087                        (SqrMagnitude(box.Size()) <= mTermMinSize)); 
     
    10681115        // select subdivision axis 
    10691116        const int axis = SelectPlane(leaf, box, position, raysBack, raysFront, pvsBack, pvsFront); 
    1070  
    10711117        //Debug << "rays back=" << raysBack << " rays front=" << raysFront << " pvs back=" << pvsBack << " pvs front=" <<       pvsFront << endl; 
    10721118 
    1073         int maxCostMisses = leaf->GetMaxCostMisses(); 
    1074  
    10751119        if (axis == -1) 
    1076         { 
    1077                 // cost ratio missed 
    1078                 ++ maxCostMisses; 
    1079                 if (maxCostMisses > mTermMissTolerance) 
    1080                         return leaf; 
    1081         } 
    1082  
     1120                return leaf; 
     1121         
    10831122        mStat.nodes += 2; 
    1084         //++ mStat.splits[axis]; 
     1123        ++ mStat.splits[axis]; 
    10851124 
    10861125        // add the new nodes to the tree 
     
    10941133        frontBBox = box; 
    10951134 
    1096         VspKdLeaf *back = new VspKdLeaf(node, raysBack, maxCostMisses); 
     1135        VspKdLeaf *back = new VspKdLeaf(node, raysBack, leaf->GetMaxCostMisses()); 
    10971136        back->SetPvsSize(pvsBack); 
    1098         VspKdLeaf *front = new VspKdLeaf(node, raysFront, maxCostMisses); 
     1137        VspKdLeaf *front = new VspKdLeaf(node, raysFront, leaf->GetMaxCostMisses()); 
    10991138        front->SetPvsSize(pvsFront); 
    11001139 
     
    16861725 
    16871726                } 
    1688                 else { 
     1727                else 
     1728                { 
    16891729                        VspKdInterior *in = (VspKdInterior *)node; 
    1690                         // both nodes for directional splits 
     1730                         
    16911731                        tstack.push(in->GetFront()); 
    16921732                        tstack.push(in->GetBack()); 
     
    17331773                        VspKdInterior *in = 
    17341774                                dynamic_cast<VspKdInterior *>(node); 
    1735                         // both nodes for directional splits 
     1775                 
    17361776                        tstack.push(in->GetFront()); 
    17371777                        tstack.push(in->GetBack()); 
     
    17671807                { 
    17681808                        VspKdInterior *in = (VspKdInterior *)node; 
    1769                         // both nodes for directional splits 
     1809                         
    17701810                        tstack.push(in->GetFront()); 
    17711811                        tstack.push(in->GetBack()); 
     
    18321872} 
    18331873 
     1874 
    18341875int VspKdTree::ComputePvsSize(VspKdNode *node, 
    18351876                                                          const RayInfoContainer &globalRays) const 
     
    18401881 
    18411882        RayInfoContainer::const_iterator it, it_end = globalRays.end(); 
     1883 
     1884        // TODO 
    18421885 
    18431886        // warning: implicit conversion from VssRay to Ray 
     
    18471890        return pvsSize; 
    18481891} 
     1892 
    18491893 
    18501894void VspKdTree::CollectLeaves(vector<VspKdLeaf *> &leaves) const 
     
    20652109        // set new size of view cell 
    20662110        vc->SetVolume(fVc->GetVolume() + bVc->GetVolume()); 
     2111        // new area 
     2112        vc->SetArea(fVc->GetArea() + bVc->GetArea()); 
    20672113 
    20682114        vector<VspKdLeaf *> fLeaves = fVc->mLeaves; 
     
    21432189        int merged = 0; 
    21442190 
    2145         //Debug << mMinViewCells << " " << mMaxCostRatio << endl; 
    2146  
    21472191        // use priority queue to merge leaves 
    2148         while (!mergeQueue.empty() && 
    2149                    (vcSize > mMergeMinViewCells) && 
     2192        while (!mergeQueue.empty() && (vcSize > mMergeMinViewCells) && 
    21502193                   (mergeQueue.top().GetMergeCost() <  
    2151                     mMergeMaxCostRatio / MergeCandidate::sOverallCost)) 
    2152         { 
    2153                 //Debug << mergeQueue.top().GetMergeCost() << " " << mMaxCostRatio << endl; 
     2194                    mMergeMaxCostRatio * MergeCandidate::sOverallCost)) 
     2195        { 
     2196                //Debug << "mergecost: " << mergeQueue.top().GetMergeCost() / MergeCandidate::sOverallCost << " " << mMergeMaxCostRatio << endl; 
    21542197                MergeCandidate mc = mergeQueue.top(); 
    21552198                mergeQueue.pop(); 
     
    21812224 
    21822225        // collapse siblings belonging to the same view cell 
    2183         //CollapseTree(mRoot); 
     2226        CollapseTree(mRoot); 
    21842227        // revalidate leaves 
    2185         //RepairVcLeafLists(); 
    2186  
    2187         //Debug << "merged " << merged << " of " << savedVcSize << " leaves" << endl; 
     2228        RepairVcLeafLists(); 
     2229 
     2230        Debug << "merged " << merged << " of " << savedVcSize << " leaves" << endl; 
    21882231 
    21892232        //TODO: should return sample contributions 
     
    23322375} 
    23332376 
     2377 
    23342378VspKdLeaf *MergeCandidate::GetLeaf1() 
    23352379{ 
     
    23372381} 
    23382382 
     2383 
    23392384VspKdLeaf *MergeCandidate::GetLeaf2() 
    23402385{ 
    23412386        return mLeaf2; 
    23422387} 
     2388 
    23432389 
    23442390bool MergeCandidate::Valid() const 
     
    23492395} 
    23502396 
     2397 
    23512398float MergeCandidate::GetMergeCost() const 
    23522399{ 
    23532400        return mMergeCost; 
    23542401} 
     2402 
    23552403 
    23562404void MergeCandidate::SetValid() 
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h

    r480 r482  
    140140        int maxPvsSize; 
    141141 
     142        // max cost ratio 
     143        int maxCostNodes; 
     144 
    142145        // number of dynamically added ray refs 
    143146        int addedRayRefs; 
     
    145148        int removedRayRefs; 
    146149   
     150        /// for average pvs 
     151        int accPvsSize; 
     152 
    147153        /** Default constructor. 
    148154        */ 
     
    167173                maxRayContribNodes = 0; 
    168174                minSizeNodes = 0; 
     175                maxCostNodes = 0; 
    169176 
    170177                maxRayRefs = 0; 
     
    173180                initialPvsSize = 0; 
    174181                maxPvsSize = 0; 
     182                accPvsSize = 0; 
    175183        } 
    176184 
     
    236244        VspKdInterior *mParent; 
    237245 
    238         enum {SPLIT_X = 0, SPLIT_Y, SPLIT_Z, SPLIT_DIRX, SPLIT_DIRY, SPLIT_DIRZ}; 
     246        enum {SPLIT_X = 0, SPLIT_Y, SPLIT_Z}; 
    239247   
    240248        /// splitting axis 
     
    431439                int type; 
    432440                float value; 
    433                 void *data; 
     441                VssRay *ray; 
    434442   
    435443                SortableEntry() {} 
    436                 SortableEntry(const int t, const float v, void *d):type(t), 
    437                                           value(v), data(d)  
     444                SortableEntry(const int t, const float v, VssRay *r): type(t), 
     445                                          value(v), ray(r)  
    438446                { 
    439447                } 
  • trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp

    r469 r482  
    224224        for (it = viewCells.begin(); it != it_end; ++ it) 
    225225                ExportViewCell(*it); 
    226 } 
    227  
    228 void  
    229 X3dExporter::ExportBspViewCellPartition(const BspTree &tree, const int maxPvs) 
    230 { 
    231         ViewCellContainer viewCells; 
    232         tree.CollectViewCells(viewCells); 
    233  
    234         ViewCellContainer::const_iterator it, it_end = viewCells.end(); 
    235  
    236         if (maxPvs > 0) 
    237                 mUseForcedMaterial = true; 
    238  
    239         for (it = viewCells.begin(); it != it_end; ++ it) 
    240         { 
    241                 if (maxPvs > 0) 
    242                 { 
    243                         mForcedMaterial.mDiffuseColor.b = 1.0f; 
    244                         float importance = (float)(*it)->GetPvs().GetSize() / (float)maxPvs; 
    245  
    246                         mForcedMaterial.mDiffuseColor.r = importance; 
    247                         mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r; 
    248                 } 
    249  
    250                 if ((*it)->GetMesh()) 
    251                         ExportViewCell(*it); 
    252                 else 
    253                 { 
    254                         PolygonContainer cell; 
    255                         tree.ConstructGeometry(dynamic_cast<BspViewCell *>(*it), cell); 
    256  
    257                         ExportPolygons(cell); 
    258                 } 
    259         } 
    260226} 
    261227 
     
    588554 
    589555        // export view cells 
    590         ExportBspViewCellPartition(tree);        
     556        ExportBspLeaves(tree);   
    591557 
    592558        return true; 
     
    664630} 
    665631 
    666 bool X3dExporter::ExportVspKdTreeViewCells(const VspKdTree &tree, const int maxPvs) 
    667 { 
    668         ViewCellContainer vc; 
    669  
    670         //if (maxPvs > 0) 
    671         mUseForcedMaterial = true; 
    672  
    673         tree.CollectViewCells(vc); 
    674  
    675         ViewCellContainer::const_iterator it, it_end = vc.end(); 
    676  
    677         for (it = vc.begin(); it != it_end; ++ it) 
    678         { 
    679                 VspKdViewCell *viewCell = dynamic_cast<VspKdViewCell *>(*it); 
    680  
    681                 if (maxPvs > 0) 
    682                 {                        
    683                         mForcedMaterial.mDiffuseColor.b = 1.0f; 
    684                          
    685                         const float importance = (float)viewCell->GetPvs().GetSize() / (float)maxPvs; 
    686                         mForcedMaterial.mDiffuseColor.r = importance; 
    687                         mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r; 
    688                 } 
    689                 else  
    690                         mForcedMaterial = RandomMaterial(); 
    691  
    692                 ExportVspKdTreeViewCell(tree, *viewCell); 
    693         } 
    694  
    695         return true; 
    696 } 
    697  
    698 bool X3dExporter::ExportVspKdTreeViewCell(const VspKdTree &tree, const VspKdViewCell &vc) 
    699 { 
    700         vector<VspKdLeaf *>::const_iterator it, it_end = vc.mLeaves.end(); 
    701  
    702         for (it = vc.mLeaves.begin(); it != it_end; ++ it) 
    703                 ExportBox(tree.GetBBox(*it)); 
    704  
    705         return true; 
    706 } 
    707632 
    708633bool X3dExporter::ExportKdTree(const KdTree &tree) 
     
    12321157} 
    12331158 
    1234 void X3dExporter::ExportBspViewCellPartition(const VspBspTree &tree,  
    1235                                                                                          const int maxPvs) 
    1236 { 
    1237         ViewCellContainer viewCells; 
    1238         tree.CollectViewCells(viewCells); 
    1239  
    1240         ViewCellContainer::const_iterator it, it_end = viewCells.end(); 
    1241  
    1242         if (maxPvs > 0) 
    1243                 mUseForcedMaterial = true; 
    1244  
    1245         for (it = viewCells.begin(); it != it_end; ++ it) 
    1246         { 
    1247                 if (maxPvs > 0) 
    1248                 { 
    1249                         mForcedMaterial.mDiffuseColor.b = 1.0f; 
    1250                         float importance = (float)(*it)->GetPvs().GetSize() / (float)maxPvs; 
    1251  
    1252                         mForcedMaterial.mDiffuseColor.r = importance; 
    1253                         mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r; 
    1254                 } 
    1255  
    1256                 if ((*it)->GetMesh()) 
    1257                         ExportViewCell(*it); 
    1258                 else 
    1259                 { 
    1260                         PolygonContainer cell; 
    1261                         tree.ConstructGeometry(dynamic_cast<BspViewCell *>(*it), cell); 
    1262  
    1263                         ExportPolygons(cell); 
    1264                 } 
    1265         } 
    1266 } 
    1267  
    12681159 
    12691160bool 
  • trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h

    r469 r482  
    115115 
    116116  virtual void 
    117   ExportBspViewCellPartition(const BspTree &tree, const int maxPvs = 0); 
    118  
    119   virtual void 
    120117  ExportBspLeaves(const BspTree &tree, const int maxPvs = 0); 
    121118 
     
    123120  virtual void 
    124121  ExportBspSplits(const VspBspTree &tree, const bool exportDepth); 
    125  
    126   virtual void 
    127   ExportBspViewCellPartition(const VspBspTree &tree, const int maxPvs = 0); 
    128  
    129   virtual bool 
    130   ExportVspKdTreeViewCells(const VspKdTree &tree, const int maxPvs = 0); 
    131122 
    132123protected: 
     
    151142                                                   const bool epsilon); 
    152143 
    153   bool ExportVspKdTreeViewCell(const VspKdTree &tree, const VspKdViewCell &vc); 
    154  
    155144 
    156145  void ExportViewpoint(const Vector3 &point, const Vector3 &direction); 
Note: See TracChangeset for help on using the changeset viewer.