Changeset 235


Ignore:
Timestamp:
08/12/05 01:17:56 (19 years ago)
Author:
mattausch
Message:

added some bsp stuff

Location:
trunk/VUT/GtpVisibilityPreprocessor
Files:
5 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/include/ViewCellBsp.h

    r225 r235  
    6969  { 
    7070  public: 
    71           /** Constructor takes a pointer to the cell corresponding to the whole 
    72           viewspace */ 
    73           BspTree(ViewCell *cell); 
     71          /** Default constructor. 
     72          */ 
     73          BspTree(); 
    7474         
    7575  protected: 
     76 
    7677          /// Pointer to the root of the tree 
    7778          BspNode *mRoot; 
  • trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj

    r234 r235  
    6363                                Name="VCCLCompilerTool" 
    6464                                AdditionalIncludeDirectories="..\support;..\support\devil\include;..\support\zlib\include;"$(QTDIR)\include";"$(QTDIR)\include\Qt";..\include" 
    65                                 PreprocessorDefinitions="WIN32;NDEBUG;_LIB;DEST_BSP_VIEWCELLS" 
     65                                PreprocessorDefinitions="WIN32;NDEBUG;_LIB;TEST_BSP_VIEWCELLS" 
    6666                                RuntimeLibrary="2" 
    6767                                RuntimeTypeInfo="TRUE" 
     
    199199                        </File> 
    200200                        <File 
     201                                RelativePath="..\src\Polygon3.cpp"> 
     202                        </File> 
     203                        <File 
     204                                RelativePath="..\src\Polygon3.h"> 
     205                        </File> 
     206                        <File 
    201207                                RelativePath="..\src\Preprocessor.cpp"> 
    202208                        </File> 
     
    251257                        <File 
    252258                                RelativePath="..\src\Vector3.h"> 
     259                        </File> 
     260                        <File 
     261                                RelativePath="..\src\ViewCell.cpp"> 
    253262                        </File> 
    254263                        <File 
     
    303312                        </File> 
    304313                        <File 
     314                                RelativePath="..\include\Polygon3.h"> 
     315                        </File> 
     316                        <File 
    305317                                RelativePath="..\include\Preprocessor.h"> 
    306318                        </File> 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Containers.h

    r176 r235  
    33 
    44#include <vector> 
     5#include <queue> 
     6 
    57using namespace std; 
    68 
     
    911class SceneGraphNode; 
    1012class Intersectable; 
     13class Polygon3; 
     14 
    1115 
    1216/** Container for Mesh pointers primarily for the use within the kDTree and 
     
    1822 
    1923typedef vector<ViewCell *> ViewCellContainer; 
     24 
     25/** Container for ViewCell pointers primarily for the use within the kDTree and 
     26    BSP hierarchies */ 
     27//typedef vector<MeshInstance *> MeshInstanceContainer; 
     28 
    2029/** Container for HierarchyNodes pointers primarily for the use within the kDTree and 
    2130    BSP hierarchies */ 
    2231 
    2332typedef vector<HierarchyNode *> NodeContainer; 
     33 
     34/** Queue storing a soup of polygons used during BSP tree construction 
     35*/ 
     36typedef queue<Polygon3 *> PolygonQueue; 
     37 
     38 
    2439 
    2540typedef vector<SceneGraphNode *> SceneGraphNodeContainer; 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp

    r177 r235  
    11511151                 "10"); 
    11521152 
    1153    
     1153  RegisterOption("BspTree.Termination.maxPolygons", 
     1154                 optInt, 
     1155                 "-bsp_term_max_polygons=", 
     1156                 "5"); 
     1157   
     1158  RegisterOption("BspTree.Termination.maxDepth", 
     1159                 optInt, 
     1160                 "-bsp_term_max_depth=", 
     1161                 "100"); 
     1162 
     1163  RegisterOption("BspTree.splitPlaneStrategy", 
     1164                optString, 
     1165                "-bsp_split_method=", 
     1166                "leastSplits"); 
     1167 
     1168  RegisterOption("BspTree.constructionMethod", 
     1169          optString, 
     1170          "-bsp_construction_method=", 
     1171          "viewCells"); 
    11541172} 
    11551173 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp

    r234 r235  
    44#include "X3dParser.h" 
    55#include "Preprocessor.h" 
    6  
    7  
     6#include "ViewCell.h" 
     7#include "Environment.h" 
    88 
    99   
     
    1818Preprocessor::GenerateViewcells() 
    1919{ 
    20         ObjectContainer objects; 
    21         mSceneGraph->CollectObjects(&objects); 
    22         //mBspTree = new BspTree(objects); 
    23    
    24         return true; 
     20        return BuildBspTree(); 
    2521} 
    2622 
     
    6662Preprocessor::BuildBspTree() 
    6763{ 
    68    
    69   return true; 
     64        ObjectContainer objects; 
     65        mSceneGraph->CollectObjects(&objects); 
     66 
     67        mBspTree = new BspTree(); 
     68 
     69        char constructionMethodStr[64]; 
     70 
     71        environment->GetStringValue("BspTree.constructionMethod", 
     72                           constructionMethodStr); 
     73 
     74        int constructionMethod = BspTree::VIEWCELLS; 
     75 
     76        if (strcmp(constructionMethodStr, "viewCells") == 0) 
     77                constructionMethod = BspTree::VIEWCELLS; 
     78        else if (strcmp(constructionMethodStr, "sceneGeometry") == 0) 
     79                constructionMethod = BspTree::SCENE_GEOMETRY; 
     80        else  
     81        { 
     82                cerr << "Wrong bsp construction method " << constructionMethodStr << endl; 
     83                exit(1); 
     84    } 
     85 
     86        Debug << constructionMethodStr << endl; 
     87 
     88        switch (constructionMethod) 
     89        { 
     90        case BspTree::VIEWCELLS: 
     91                mViewcells.clear(); 
     92                // derive viewcells from the scene objects 
     93                ViewCell::DeriveViewCells(objects, mViewcells, 1000); 
     94                mBspTree->Construct(mViewcells); 
     95                break; 
     96        case BspTree::SCENE_GEOMETRY: 
     97                mBspTree->Construct(objects); 
     98                break; 
     99        default: 
     100                break; 
     101        } 
     102        return true; 
    70103} 
    71104 
     
    81114Preprocessor::BspTreeStatistics(ostream &s) 
    82115{ 
    83 //  s<<mBspTree->GetStatistics(); 
     116        s << mBspTree->GetStatistics(); 
    84117} 
    85118 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h

    r234 r235  
    9898   
    9999  BspTree * mBspTree; 
    100    
    101100}; 
    102101 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h

    r224 r235  
    33 
    44#include "Intersectable.h" 
     5#include "Containers.h" 
    56 
    67class Mesh; 
     
    1920                the viewcell. 
    2021        */ 
    21         ViewCell(Mesh *mesh): mMesh(mesh), mPvs(NULL) {} 
     22        ViewCell(Mesh *mesh); 
    2223 
    2324        /** Returns pointer to the mesh which represents the shape of the viewcell. 
    2425        */ 
    25         Mesh *GetMesh() {return mMesh;} 
     26        Mesh *GetMesh(); 
    2627 
    2728        /** Returns pointer to PVS.  
    2829                @returns PVS, i.e., the visible BSP tree nodes. 
    2930        */ 
    30         BspPvs *GetPVS() {return mPvs;} 
     31        BspPvs *GetPVS(); 
    3132 
    32         AxisAlignedBox3 GetBox()  {return mMesh->mBox;} 
     33        AxisAlignedBox3 GetBox(); 
    3334   
    34         int CastRay(Ray &ray) {return 0;} 
     35        int CastRay(Ray &ray); 
    3536   
    36         bool IsConvex() {return mMesh->mIsConvex;} 
    37         bool IsWatertight() {return mMesh->mIsWatertight;} 
    38         float IntersectionComplexity() {return mMesh->mFaces.size();} 
     37        bool IsConvex(); 
     38        bool IsWatertight(); 
     39        float IntersectionComplexity(); 
    3940 
    40         int Type() const { return VIEWCELL; } 
     41        int Type() const; 
    4142 
    42         void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal) {point = Vector3(0,0,0);}; 
     43        void GetRandomSurfacePoint(Vector3 &point, Vector3 &normal); 
    4344 
     45        /** Derives viewcells from object container. 
     46                @param objects the intersectables the viewcells are derived from 
     47                @param viewCells the viewcells are returned in this container 
     48                @param if >0, indicates the maximim number of viewcells that will be created 
     49        */ 
     50        static void DeriveViewCells(const ObjectContainer &objects,  
     51                                                                ViewCellContainer &viewCells,  
     52                                                                const int max); 
    4453protected: 
    4554 
     55        /// the mesh defining the geometry of this viewcell 
    4656        Mesh *mMesh; 
    4757        /// the PVS (i.e., the visible bsp tree nodes) 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp

    r234 r235  
    22#include "ViewCellBsp.h" 
    33#include "Mesh.h" 
    4  
     4#include "common.h" 
    55#include "ViewCell.h" 
    66#include <stack> 
    7  
    8 #define DEL_PTR(ptr) while (0) {if (ptr) {delete (ptr); (ptr) = NULL;}} 
     7#include "Environment.h" 
     8#include "Polygon3.h" 
     9 
     10int BspTree::sTermMaxPolygons = 10; 
     11int BspTree::sTermMaxDepth = 20; 
    912 
    1013//namespace GtpVisibilityPreprocessor { 
     14 
     15 
    1116/****************************************************************/ 
    1217/*                  class BspNode implementation                */ 
    1318/****************************************************************/ 
     19 
     20BspNode::BspNode(): mParent(NULL) 
     21{} 
     22 
     23BspNode::BspNode(BspInterior *parent): mParent(parent) 
     24{} 
     25 
     26 
    1427bool BspNode::IsRoot() const  
    1528{ 
     
    2134        return mParent;  
    2235} 
     36 
     37void BspNode::SetParent(BspInterior *parent) 
     38{ 
     39        mParent = parent; 
     40} 
     41 
    2342/****************************************************************/ 
    2443/*              class BspInterior implementation                */ 
     
    6786} 
    6887 
    69 void BspInterior::SplitPolygons(PolygonQueue *polys, PolygonQueue *frontPolys, PolygonQueue *backPolys) 
     88void BspInterior::SplitPolygons(PolygonQueue *polys,  
     89                                                                PolygonQueue *frontPolys,  
     90                                                                PolygonQueue *backPolys,  
     91                                                                int &splits) 
    7092{ 
    7193        while (!polys->empty()) 
    7294        { 
    73                 Polygon *poly = polys->front(); 
     95                Polygon3 *poly = polys->front(); 
    7496 
    7597                polys->pop(); 
     
    7799                int result = poly->Side(&mPlane); 
    78100 
    79                 Polygon *front_piece = NULL; 
    80                 Polygon *back_piece = NULL; 
     101                Polygon3 *front_piece = NULL; 
     102                Polygon3 *back_piece = NULL; 
    81103 
    82104                switch (result) 
    83105                { 
    84                         case Polygon::COINCIDENT: 
     106                        case Polygon3::COINCIDENT: 
    85107                                break; 
    86                         case Polygon::FRONT_SIDE: 
     108                        case Polygon3::FRONT_SIDE: 
    87109                                frontPolys->push(poly); 
    88110                                break; 
    89                         case Polygon::BACK_SIDE: 
     111                        case Polygon3::BACK_SIDE: 
    90112                                backPolys->push(poly); 
    91113                                break; 
    92                         case Polygon::SPLIT: 
    93                                 front_piece = new Polygon(); 
    94                                 back_piece = new Polygon(); 
     114                        case Polygon3::SPLIT: 
     115                                front_piece = new Polygon3(); 
     116                                back_piece = new Polygon3(); 
    95117 
    96118                                //-- split polygon 
    97                 poly->Split(&mPlane, front_piece, back_piece); 
    98  
     119                                poly->Split(&mPlane, front_piece, back_piece, splits); 
     120                         
    99121                                backPolys->push(back_piece); 
    100122                                frontPolys->push(front_piece); 
     
    108130        } 
    109131        // delete old polygons 
    110         Polygon::DeletePolygons(polys); 
     132//      Polygon3::DeletePolygons(polys); 
    111133} 
    112134 
     
    129151} 
    130152 
    131 /****************************************************************/ 
    132 /*                  class Polygon implementation                */ 
    133 /****************************************************************/ 
    134  
    135 Polygon::Polygon() 
    136 {} 
    137  
    138 Polygon::Polygon(const VertexContainer &vertices): mVertices(vertices)  
    139 {} 
    140  
    141 Polygon::Polygon(Face *face, Mesh *parent) 
    142 { 
    143         VertexIndexContainer::const_iterator it; 
    144  
    145         for (it = face->mVertexIndices.begin(); it != face->mVertexIndices.end(); ++ it) 
    146         { 
    147                 mVertices.push_back(parent->mVertices[*it]); 
    148         } 
    149 } 
    150  
    151 Plane3 Polygon::GetSupportingPlane() 
    152 { 
    153         return Plane3(mVertices[0], mVertices[1], mVertices[2]); 
    154 } 
    155  
    156 void Polygon::DeletePolygons(PolygonQueue *polys) 
    157 { 
    158         // don't need to store polygon information = delete polygons 
    159         while(!polys->empty()) 
    160         { 
    161                 Polygon *poly = polys->front(); 
    162                 polys->pop(); 
    163                 DEL_PTR(poly); 
    164         } 
    165 } 
    166  
    167 void Polygon::Split(Plane3 *partition, Polygon *front, Polygon *back) 
    168 { 
    169         Vector3 ptA = mVertices[mVertices.size() - 1];; 
    170          
    171         int sideA = partition->Side(ptA); 
    172          
    173         VertexContainer::const_iterator it; 
    174  
    175         // find line - plane intersections 
    176         for (it = mVertices.begin(); it != mVertices.end(); ++ it) 
    177         { 
    178                 Vector3 ptB = (*it); 
    179                 int sideB = partition->Side(ptB); 
    180  
    181                 // vertices on different sides => split 
    182                 if ((sideA != 0) && (sideB != 0) && (sideA != sideB))  
    183                 { 
    184                         Vector3 v = ptB - ptA; // line from A to B 
    185                         float dv = DotProd(partition->mNormal, v); 
    186                         float t = 0; 
    187                          
    188                         if (dv) 
    189                         { 
    190                                 t = - partition->Distance(ptA) / dv; 
    191                         } 
    192                 } 
    193                 if (sideB >= 0) 
    194                 { 
    195                         back->mVertices.push_back(ptB); 
    196                 } 
    197                 else if (sideB <= 0) 
    198                 { 
    199                         front->mVertices.push_back(ptB); 
    200                 } 
    201  
    202                 ptA = ptB; 
    203                 sideA = sideB; 
    204         } 
    205 } 
    206  
    207 int Polygon::Side(Plane3 *plane) 
    208 { 
    209         VertexContainer::const_iterator it; 
    210  
    211         bool onFrontSide = false; 
    212         bool onBackSide = false; 
    213  
    214         // find line - plane intersections 
    215         for (it = mVertices.begin(); it != mVertices.end(); ++ it) 
    216         { 
    217                 int side = plane->Side(*it); 
    218                  
    219                 if (side > 0) 
    220                 { 
    221                         onFrontSide = true; 
    222                 } 
    223                 else if (side < 0) 
    224                 { 
    225                         onBackSide = true; 
    226                 } 
    227  
    228                 if (onFrontSide && onBackSide) // splits 
    229                         return SPLIT; 
    230         } 
    231  
    232         if (onBackSide) 
    233         { 
    234                 return BACK_SIDE; 
    235         } 
    236         else if (onFrontSide) 
    237         { 
    238                 return FRONT_SIDE; 
    239         } 
    240  
    241         return COINCIDENT; 
    242 } 
    243153 
    244154/****************************************************************/ 
     
    246156/****************************************************************/ 
    247157 
    248 BspTree::BspTree(const ViewCellContainer &viewCells):  
    249 mMaxPolys(0), mMaxDepth(999999), mRoot(NULL) // null => until we stripped all polygons 
    250 { 
    251         //mRootCell = cell; 
    252         Subdivide(viewCells); 
    253 } 
    254  
    255 BspTree::BspTree(const ObjectContainer &objects, int maxPolys, int maxDepth): 
    256 mMaxPolys(maxPolys), mMaxDepth(maxDepth) 
     158BspTree::BspTree(): mTermMaxPolygons(0), mTermMaxDepth(0) 
    257159{ 
    258160        mRoot = new BspLeaf(); 
    259  
    260         Subdivide(objects); 
    261161} 
    262162  
     163const BspTreeStatistics &BspTree::GetStatistics() const  
     164{ 
     165        return mStat; 
     166} 
     167 
     168void BspTreeStatistics::Print(ostream &app) const 
     169{ 
     170        app << "===== BspTree statistics ===============\n"; 
     171 
     172        app << "#N_RAYS Number of rays )\n" 
     173                << rays <<endl; 
     174        app << "#N_DOMAINS  ( Number of query domains )\n" 
     175                << queryDomains <<endl; 
     176 
     177        app << "#N_NODES ( Number of nodes )\n" << nodes << "\n"; 
     178 
     179        app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n"; 
     180 
     181        app << "#N_SPLITS ( Number of splits)\n" << splits << "\n"; 
     182 
     183        app << "#N_RAYREFS ( Number of rayRefs )\n" << 
     184        rayRefs << "\n"; 
     185 
     186        app << "#N_RAYRAYREFS  ( Number of rayRefs / ray )\n" << 
     187        rayRefs/(double)rays << "\n"; 
     188 
     189        app << "#N_LEAFRAYREFS  ( Number of rayRefs / leaf )\n" << 
     190        rayRefs/(double)Leaves() << "\n"; 
     191 
     192        app << "#N_MAXOBJECTREFS  ( Max number of rayRefs / leaf )\n" << 
     193        maxObjectRefs << "\n"; 
     194 
     195        app << "#N_NONEMPTYRAYREFS  ( Number of rayRefs in nonEmpty leaves / non empty leaf )\n" << 
     196        rayRefsNonZeroQuery/(double)(Leaves() - zeroQueryNodes) << "\n"; 
     197 
     198        app << "#N_LEAFDOMAINREFS  ( Number of query domain Refs / leaf )\n" << 
     199        objectRefs/(double)Leaves() << "\n"; 
     200 
     201        //  app << setprecision(4); 
     202 
     203        app << "#N_PEMPTYLEAVES  ( Percentage of leaves with zero query domains )\n"<< 
     204        zeroQueryNodes*100/(double)Leaves()<<endl; 
     205 
     206        app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n"<< 
     207        maxDepthNodes*100/(double)Leaves()<<endl; 
     208 
     209        app << "#N_PMAXDEPTH ( Maximal reached depth )\n"<< 
     210        maxDepth*100/(double)Leaves()<<endl; 
     211 
     212        app << "#N_ADDED_RAYREFS  (Number of dynamically added ray references )\n"<< 
     213        addedRayRefs<<endl; 
     214 
     215        app << "#N_REMOVED_RAYREFS  (Number of dynamically removed ray references )\n"<< 
     216        removedRayRefs<<endl; 
     217 
     218        //  app << setprecision(4); 
     219 
     220        //  app << "#N_CTIME  ( Construction time [s] )\n" 
     221        //      << Time() << " \n"; 
     222 
     223        app << "===== END OF BspTree statistics ==========\n"; 
     224} 
     225 
    263226BspTree::~BspTree() 
    264227{ 
     
    296259        PolygonQueue polys; 
    297260        // copy polygon information to guide the split process 
    298         CopyMesh2Polygon(viewCell->GetMesh(), polys); 
     261        CopyMesh2Polygons(viewCell->GetMesh(), polys); 
    299262 
    300263        tStack.push(BspTraversalData(mRoot, NULL, &polys, 0)); 
     
    315278                        PolygonQueue *backPolys = new PolygonQueue(); 
    316279 
    317                         interior->SplitPolygons(tData.mPolys, frontPolys, backPolys); 
     280                        int splits = 0; 
     281                        interior->SplitPolygons(tData.mPolygons, frontPolys, backPolys, splits); 
     282                        mStat.splits += splits; 
    318283 
    319284                        // push the children on the stack 
    320285                        if (frontPolys->size() > 0) // if polygons on this side of bsp tree 
    321                                 tStack.push(BspTraversalData(interior->GetFront(), interior->GetParent(), frontPolys, tData.mDepth + 1)); 
     286                                tStack.push(BspTraversalData(interior->GetFront(), interior->GetParent(),  
     287                                                        frontPolys, tData.mDepth + 1)); 
    322288                        else 
    323289                                delete frontPolys; 
    324290 
    325291                        if (backPolys > 0) // if polygons on this side of bsp tree 
    326                                 tStack.push(BspTraversalData(interior->GetBack(), interior->GetParent(), backPolys, tData.mDepth + 1)); 
     292                                tStack.push(BspTraversalData(interior->GetBack(), interior->GetParent(),  
     293                                                        backPolys, tData.mDepth + 1)); 
    327294                        else 
    328295                                delete backPolys; 
    329296                } 
    330                 else // reached leaf and must split 
     297                else // reached leaf => subdivide 
    331298                { 
    332                         BuildTree(tStack, tData, NULL); 
     299                        Subdivide(tStack, tData, NULL); 
    333300                } 
    334301        } 
    335302} 
    336303 
    337 void BspTree::Subdivide(const ViewCellContainer &viewCells) 
    338 { 
    339 } 
    340  
    341 void BspTree::CopyMesh2Polygon(Mesh *mesh, PolygonQueue &polys) 
     304void BspTree::Construct(const ViewCellContainer &viewCells) 
     305{ 
     306        // for this type of construction we split until no polygons is left 
     307        mTermMaxPolygons = 0; 
     308        mTermMaxDepth = sTermMaxDepth; 
     309 
     310        mStat.nodes = 1; 
     311 
     312        // insert all viewcells 
     313        ViewCellContainer::const_iterator it; 
     314 
     315        for (it = viewCells.begin(); it != viewCells.end(); ++ it) 
     316        { 
     317                InsertViewCell(*it); 
     318        } 
     319} 
     320 
     321void BspTree::CopyMesh2Polygons(Mesh *mesh, PolygonQueue &polys) 
    342322{ 
    343323        FaceContainer::const_iterator fi; 
     324         
    344325        // copy the face data to polygons 
    345326        for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi) 
    346327        { 
    347                 Polygon *poly = new Polygon((*fi), mesh); 
     328                Polygon3 *poly = new Polygon3((*fi), mesh); 
    348329                polys.push(poly); 
    349330        } 
     
    357338        { 
    358339                Intersectable *object = *it; 
    359  
    360                 // extract the mesh instances 
    361                 if (object->Type() == Intersectable::MESH_INSTANCE) 
     340                Mesh *mesh = NULL; 
     341 
     342                // extract the meshes 
     343                switch (object->Type()) 
    362344                { 
    363                         MeshInstance *inst = dynamic_cast<MeshInstance *>(object); 
    364  
    365                         Mesh *mesh = inst->GetMesh(); 
    366  
     345                case Intersectable::MESH_INSTANCE: 
     346                         
     347                        mesh = dynamic_cast<MeshInstance *>(object)->GetMesh(); 
     348                         
     349                case Intersectable::VIEWCELL: 
     350                        mesh = dynamic_cast<ViewCell *>(object)->GetMesh(); 
    367351                        // copy the mesh data to polygons 
    368                         CopyMesh2Polygon(mesh, polys); 
     352                        CopyMesh2Polygons(mesh, polys); 
     353                        break; 
     354                default: 
     355                        break; 
    369356                } 
    370357        } 
    371 } 
    372  
    373 void BspTree::Subdivide(const ObjectContainer &objects) 
    374 { 
     358        Debug << "number of polygons: " << polys.size() << endl; 
     359} 
     360 
     361void BspTree::Construct(const ObjectContainer &objects) 
     362{ 
     363        mTermMaxPolygons = sTermMaxPolygons; 
     364        mTermMaxDepth = sTermMaxDepth; 
     365 
     366        mStat.nodes = 1; 
     367 
    375368        std::stack<BspTraversalData> tStack; 
    376369        PolygonQueue *polys = new PolygonQueue(); 
     
    385378        { 
    386379            tData = tStack.top(); 
    387  
    388380            tStack.pop(); 
    389381     
    390                 BuildTree(tStack, tData); 
    391         } 
    392 } 
    393  
    394 void BspTree::BuildTree(BspTraversalStack &tStack, BspTraversalData &currentData, ViewCell *viewCell) 
     382                Subdivide(tStack, tData); 
     383        } 
     384} 
     385 
     386void BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell) 
    395387{ 
    396388        PolygonQueue *backPolys = new PolygonQueue(); 
    397389        PolygonQueue *frontPolys = new PolygonQueue(); 
    398390 
    399         BspNode *node = SubdivideNode(dynamic_cast<BspLeaf *>(currentData.mNode), 
    400                                                                   currentData.mParent, 
    401                                                                   currentData.mPolys, 
    402                                                                   currentData.mDepth, 
     391        BspNode *node = SubdivideNode(dynamic_cast<BspLeaf *>(tData.mNode), 
     392                                                                  tData.mParent, 
     393                                                                  tData.mPolygons, 
     394                                                                  tData.mDepth, 
    403395                                                                  viewCell, 
    404396                                                                  backPolys, 
     
    410402 
    411403                // push the children on the stack (there are always two children) 
    412                 tStack.push(BspTraversalData(interior->GetBack(), interior, backPolys, currentData.mDepth + 1)); 
    413                 tStack.push(BspTraversalData(interior->GetFront(), interior, frontPolys, currentData.mDepth + 1)); 
    414         } 
     404                tStack.push(BspTraversalData(interior->GetBack(), interior, backPolys, tData.mDepth + 1)); 
     405                tStack.push(BspTraversalData(interior->GetFront(), interior, frontPolys, tData.mDepth + 1)); 
     406        } 
     407        else 
     408                EvaluateLeafStats(tData); 
    415409} 
    416410 
     
    429423                                                                PolygonQueue *backPolys) 
    430424{ 
    431         // terminate traversal if no more faces in mesh 
    432         if ((polys->size() <= mMaxPolys) || (depth >= mMaxDepth)) 
    433         { 
    434                 // don't need to store polygon information = delete polygons 
    435                 Polygon::DeletePolygons(polys); 
    436                  
     425        // terminate traversal 
     426        if ((polys->size() <= mTermMaxPolygons) || (depth >= mTermMaxDepth)) 
     427        { 
     428                // don't need to store polygon information => delete polygons 
     429                Polygon3::DeletePolygons(polys); 
     430                // Debug << polys->size() << ", " << depth << endl;              
    437431                return leaf; 
    438432        } 
     433 
     434         mStat.nodes += 2; 
    439435 
    440436        // add the new nodes to the tree + select subdivision plane 
     
    442438 
    443439        // split polygon according to current plane 
    444         node->SplitPolygons(polys, frontPolys, backPolys); 
    445          
     440        int splits = 0; 
     441        node->SplitPolygons(polys, frontPolys, backPolys, splits); 
     442        mStat.splits += splits; 
     443 
    446444        // two new leaves 
    447445        BspLeaf *back = new BspLeaf(); 
     
    461459        return node; 
    462460} 
     461 
     462void BspTree::ParseEnvironment() 
     463{ 
     464        environment->GetIntValue("BspTree.Termination.maxDepth", sTermMaxDepth); 
     465        environment->GetIntValue("BspTree.Termination.maxPolygons", sTermMaxPolygons); 
     466} 
     467 
     468void BspTree::EvaluateLeafStats(const BspTraversalData &data) 
     469{ 
     470 
     471  // the node became a leaf -> evaluate stats for leafs 
     472  BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode); 
     473 
     474  if (data.mDepth > mTermMaxDepth) 
     475    ++ mStat.maxDepthNodes;  
     476 
     477  // record maximal depth 
     478  if (data.mDepth > mStat.maxDepth) 
     479          mStat.maxDepth = data.mDepth; 
     480} 
    463481//} // GtpVisibilityPreprocessor 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h

    r234 r235  
    1414class BspTree;   
    1515class BspInterior; 
    16 class Polygon; 
    17  
    18 typedef std::queue<Polygon *> PolygonQueue; 
     16class Polygon3; 
     17 
     18/** Queue storing a soup of polygons used during BSP tree construction 
     19*/ 
     20typedef std::queue<Polygon3 *> PolygonQueue; 
    1921 
    2022class BspTreeStatistics // TODO: should have common superclass with KdTreeStatistics 
     
    2325  // total number of nodes 
    2426  int nodes; 
     27  // number of splits 
     28  int splits; 
    2529  // totals number of rays 
    2630  int rays; 
     31  // maximal reached depth 
     32  int maxDepth; 
    2733  // total number of query domains 
    2834  int queryDomains; 
     
    4753  BspTreeStatistics()  
    4854  { 
    49     Reset(); 
     55          Reset(); 
    5056  } 
    5157 
     
    5763  { 
    5864          nodes = 0; 
    59  
     65          splits = 0; 
    6066          rays = queryDomains = 0; 
    6167          rayRefs = rayRefsNonZeroQuery = objectRefs = 0; 
     
    8591 
    8692public: 
     93        BspNode(); 
     94        BspNode(BspInterior *parent); 
     95 
    8796        /** Determines whether this node is a leaf or not 
    8897        @return true if leaf 
     
    98107        */ 
    99108        BspInterior *GetParent(); 
    100          
     109        /** Sets parent node. 
     110        */ 
     111        void SetParent(BspInterior *parent); 
     112 
    101113protected: 
    102114 
     
    129141                @param frontPolys returns the polygons in the front of the split plane 
    130142                @param backPolys returns the polygons in the back of the split plane 
    131         */ 
    132         void SplitPolygons(PolygonQueue *polys, PolygonQueue *frontPolys, PolygonQueue *backPolys); 
     143                @param splits number of splits 
     144        */ 
     145        void SplitPolygons(PolygonQueue *polys, PolygonQueue *frontPolys, PolygonQueue *backPolys, int &splits); 
    133146 
    134147protected: 
     
    161174 
    162175 
    163 /** Class representing a polygon. 
    164 */ 
    165 class Polygon 
    166 { 
    167 public: 
    168         Polygon(); 
    169         Polygon(const VertexContainer &vertices); 
    170  
    171         /** Copies all the vertices of the face. 
    172         */ 
    173         Polygon(Face *face, Mesh *parent); 
    174          
    175         /** Returns supporting plane of this polygon. 
    176         */ 
    177         Plane3 GetSupportingPlane(); 
    178  
    179         /** Splits polygon. 
    180                 @param partition the split plane 
    181                 @param front the front polygon 
    182                 @param back the back polygon 
    183         */ 
    184         void Split(Plane3 *partition, Polygon *front, Polygon *back); 
    185  
    186         enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT}; 
    187  
    188         /** Side of the plane where the polygon is located. 
    189             @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT 
    190         */ 
    191         int Side(Plane3 *plane); 
    192  
    193         static void DeletePolygons(PolygonQueue *polys); 
    194  
    195         /// vertices are connected in counterclockwise order. 
    196         VertexContainer mVertices; 
    197 }; 
    198  
    199176/** Implementation of the ViewCell BSP tree. */ 
    200177class BspTree  
     
    211188                BspInterior *mParent; 
    212189                /// polygonal data for splitting 
    213                 PolygonQueue *mPolys; 
     190                PolygonQueue *mPolygons; 
    214191                /// current depth 
    215192                int mDepth; 
     
    218195                 
    219196                BspTraversalData(BspNode *node, BspInterior *parent, PolygonQueue *polys, const int depth):  
    220                 mNode(node), mParent(parent), mPolys(polys), mDepth(depth) {} 
     197                mNode(node), mParent(parent), mPolygons(polys), mDepth(depth) {} 
    221198    }; 
    222199 
    223200        typedef std::stack<BspTraversalData> BspTraversalStack; 
    224201 
    225         /** Constructs BSP tree from list of view cells. 
     202        /// BSP tree construction type 
     203        enum {VIEWCELLS, SCENE_GEOMETRY}; 
     204 
     205        /** Default constructor creating an empty tree. 
    226206        */  
    227         BspTree(const ViewCellContainer &viewCells); 
    228         /** Constructs BSP tree from list of objects. 
    229                 @param object list of intersectables 
    230                 @param maxPolys maximal allowed number of polygons 
    231                 @param maxDepth maximal allowed BSP tree depth 
    232         */ 
    233         BspTree(const ObjectContainer &objects, int maxPolys, int maxDepth); 
     207        BspTree(); 
    234208 
    235209        ~BspTree(); 
    236210 
     211        const BspTreeStatistics &GetStatistics() const;  
     212   
     213        /** Constructs tree using the given list of viewcells. 
     214            A pointer to the appropriate viewcell is stored within each leaf.  
     215                Many leafs can point to the same viewcell. 
     216        */ 
     217        void Construct(const ViewCellContainer &viewCells); 
     218        /** Constructs tree using the given list of objects. Each leaf is taken as viewcell. 
     219            No objects are treated as viewcells explicitly. 
     220 
     221                @param objects list of objects 
     222        */ 
     223        void Construct(const ObjectContainer &objects); 
     224 
     225 
    237226protected: 
    238         /** Builds a new extension of the tree. 
    239         */ 
    240         void BuildTree(BspTraversalStack &tStack, BspTraversalData &currentData, ViewCell *viewCell = NULL); 
    241  
    242         /** Selects a splitting plane from the given polygons.  
     227        void EvaluateLeafStats(const BspTraversalData &data); 
     228 
     229        /** Subdivides node with respect to the traversal data. 
     230            @param tStack current traversal stack 
     231                @param tData traversal data also holding node to be subdivided 
     232                @param viewCell the view cell that will be represented with this part of the Bsp tree. 
     233        */ 
     234        void Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell = NULL); 
     235 
     236        /** Selects a splitting plane.  
     237                @param polyQueue the polygon data on which the split decition is based 
    243238        */ 
    244239        Plane3 SelectPlane(PolygonQueue *polyQueue); 
     
    248243        */ 
    249244        void InsertViewCell(ViewCell *viewCell); 
    250  
    251         /** Subdivides tree according to the given list of viewcells. 
    252         */ 
    253         void Subdivide(const ViewCellContainer &viewCells); 
    254         /** Subdivides tree according to the given list of objects. 
    255         */ 
    256         void Subdivide(const ObjectContainer &objects); 
    257245         
    258         /** Subdivides leaf. 
     246        /** Subdivide leaf. 
    259247                @param leaf the leaf to be subdivided 
    260248                @param parent the parent node of this leaf 
     
    275263                @param backPolys returns the polygons in the back of the split plane 
    276264        */ 
    277         void FilterPolygons(BspInterior *node, PolygonQueue *polys, PolygonQueue *frontPolys, PolygonQueue *backPolys); 
    278  
    279         /** Extracts the mesh instances of the objects and copies them into the mesh. 
     265        void FilterPolygons(BspInterior *node, PolygonQueue *polys,  
     266                                                PolygonQueue *frontPolys, PolygonQueue *backPolys); 
     267 
     268        /** Extracts the meshes of the objects and copies them into the mesh. 
    280269        */ 
    281270        static void Copy2PolygonSoup(const ObjectContainer &objects, PolygonQueue &polys); 
    282271 
    283         static void CopyMesh2Polygon(Mesh *mesh, PolygonQueue &polys); 
     272        /** copy this mesh into polygons. 
     273        */ 
     274        static void CopyMesh2Polygons(Mesh *mesh, PolygonQueue &polys); 
    284275 
    285276        /// Pointer to the root of the tree 
     
    287278        /// Pointer to the root cell of the viewspace 
    288279        // ViewCell *mRootCell; 
    289         /// maximal number of polygons allowed in leaf 
    290         int mMaxPolys; 
    291         /// maximal depth 
    292         int mMaxDepth; 
    293  
     280         
     281         
    294282        BspTreeStatistics mStat; 
     283 
     284        /// local maximal polygons (changes depending on method) 
     285        int mTermMaxPolygons; 
     286        int mTermMaxDepth; 
     287 
     288public: 
     289        /// Parses the environment and stores the global BSP tree parameters 
     290        static void ParseEnvironment(); 
     291 
     292        /// maximal number of polygons where tree construction is terminated 
     293        static int sTermMaxPolygons; 
     294        /// maximal possible depth 
     295        static int sTermMaxDepth; 
    295296}; 
    296297 
  • trunk/VUT/GtpVisibilityPreprocessor/src/common.h

    r162 r235  
    127127#define MAX_FLOAT  1e30f 
    128128 
     129#ifndef DEL_PTR 
     130#define DEL_PTR(ptr) while (0) {if (ptr) {delete (ptr); (ptr) = NULL;}} 
     131#endif 
     132 
    129133inline 
    130134int signum(const Real a, const Real thresh = TRASH) 
  • trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp

    r234 r235  
    1818  environment->Parse(argc, argv, USE_EXE_PATH); 
    1919  MeshKdTree::ParseEnvironment(); 
    20    
     20  BspTree::ParseEnvironment(); 
     21 
    2122  Preprocessor *p = 
    2223    new SamplingPreprocessor(); 
     
    2728 
    2829  p->LoadScene(filename); 
     30 
    2931  p->BuildKdTree(); 
    3032  p->KdTreeStatistics(cout); 
    31  
    32 #ifdef DEST_BSP_VIEWCELLS 
     33#ifdef TEST_BSP_VIEWCELLS 
    3334  p->GenerateViewcells(); 
    34   p->BspTreeStatistics(cout); 
     35  p->BspTreeStatistics(Debug); 
    3536#endif 
    3637 
     
    4647    p->ExportPreprocessedData("scene.vis"); 
    4748  } 
    48    
     49 
    4950  if (0) { 
    5051    Camera camera; 
Note: See TracChangeset for help on using the changeset viewer.