Changeset 225 for trunk/VUT


Ignore:
Timestamp:
08/10/05 01:50:13 (19 years ago)
Author:
mattausch
Message:

updated bsp trees

Location:
trunk/VUT/GtpVisibilityPreprocessor
Files:
5 edited

Legend:

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

    r221 r225  
    2929  protected: 
    3030 
    31           void SplitPoly(Plane3 *plane, Mesh *viewcell); 
    32  
    3331          /// parent of this node 
    3432          BspInterior *mParent; 
     
    3836  class BspInterior : public BspNode  
    3937  {     
    40         /** @return false since it is an interior node */ 
    41         bool IsLeaf() const; 
    42  
    43         protected: 
    44         /// Splitting plane corresponding to this node 
    45         Plane3 mPlane; 
    46         /// back node 
    47         BspNode *mBack; 
    48         /// front node 
    49         BspNode *mFront; 
     38          /** @return false since it is an interior node */ 
     39          bool IsLeaf() const; 
     40   
     41  protected: 
     42          /// Splitting plane corresponding to this node 
     43          Plane3 mPlane; 
     44          /// back node 
     45          BspNode *mBack; 
     46          /// front node 
     47          BspNode *mFront; 
    5048  }; 
    5149   
    5250   
    53   /** BSP leaf node implementation */ 
     51  /** BSP leaf node implementation. 
     52  */ 
    5453  class BspLeaf : public BspNode  
    5554  { 
     
    6564  }; 
    6665   
    67   /** Implementation of the ViewCell BSP tree */ 
     66  /** Implementation of the ViewCell BSP tree. 
     67  */ 
    6868  class BspTree  
    6969  { 
  • trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj

    r221 r225  
    217217                        </File> 
    218218                        <File 
     219                                RelativePath="..\src\Rectangle3.cpp"> 
     220                        </File> 
     221                        <File 
    219222                                RelativePath="..\src\Rectangle3.h"> 
    220223                        </File> 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h

    r209 r225  
    4141                           float *t = NULL, 
    4242                           bool *coplanar = NULL 
    43                            ) const; 
     43                           ) const {return Vector3(0,0,0);} // TODO 
    4444 
    4545  friend bool 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp

    r224 r225  
    8686 
    8787/****************************************************************/ 
     88/*                  class Polygon implementation                */ 
     89/****************************************************************/ 
     90 
     91Polygon::Polygon() 
     92{} 
     93 
     94Polygon::Polygon(const VertexContainer &vertices): mVertices(vertices)  
     95{} 
     96 
     97Polygon::Polygon(Face *face, Mesh *parent) 
     98{ 
     99        VertexIndexContainer::const_iterator it; 
     100 
     101        for (it = face->mVertexIndices.begin(); it != face->mVertexIndices.end(); ++ it) 
     102        { 
     103                mVertices.push_back(parent->mVertices[*it]); 
     104        } 
     105} 
     106 
     107Plane3 Polygon::GetSupportingPlane() 
     108{ 
     109        return Plane3(mVertices[0], mVertices[1], mVertices[2]); 
     110} 
     111 
     112void Polygon::DeletePolygons(PolygonQueue *polys) 
     113{ 
     114        // don't need to store polygon information = delete polygons 
     115        while(!polys->empty()) 
     116        { 
     117                Polygon *poly = polys->front(); 
     118                polys->pop(); 
     119                DEL_PTR(poly); 
     120        } 
     121 
     122} 
     123 
     124void Polygon::Split(Plane3 *partition, Polygon *front, Polygon *back) 
     125{ 
     126        Vector3 ptA = mVertices[mVertices.size() - 1];; 
     127         
     128        int sideA = partition->Side(ptA); 
     129         
     130        VertexContainer::const_iterator it; 
     131 
     132        // find line - plane intersections 
     133        for (it = mVertices.begin(); it != mVertices.end(); ++ it) 
     134        { 
     135                Vector3 ptB = (*it); 
     136                int sideB = partition->Side(ptB); 
     137 
     138                // vertices on different sides => split 
     139                if ((sideA != 0) && (sideB != 0) && (sideA != sideB))  
     140                { 
     141                        Vector3 v = ptB - ptA; // line from A to B 
     142                        float dv = DotProd(partition->mNormal, v); 
     143                        float t = 0; 
     144                         
     145                        if (dv) 
     146                        { 
     147                                t = - partition->Distance(ptA) / dv; 
     148                        } 
     149                } 
     150                if (sideB >= 0) 
     151                { 
     152                        back->mVertices.push_back(ptB); 
     153                } 
     154                else if (sideB <= 0) 
     155                { 
     156                        front->mVertices.push_back(ptB); 
     157                } 
     158 
     159                ptA = ptB; 
     160                sideA = sideB; 
     161        } 
     162} 
     163 
     164int Polygon::Side(Plane3 *plane) 
     165{ 
     166        VertexContainer::const_iterator it; 
     167 
     168        bool onFrontSide = false; 
     169        bool onBackSide = false; 
     170 
     171        // find line - plane intersections 
     172        for (it = mVertices.begin(); it != mVertices.end(); ++ it) 
     173        { 
     174                int side = plane->Side(*it); 
     175                 
     176                if (side > 0) 
     177                { 
     178                        onFrontSide = true; 
     179                } 
     180                else if (side < 0) 
     181                { 
     182                        onBackSide = true; 
     183                } 
     184 
     185                if (onFrontSide && onBackSide) // splits 
     186                        return SPLIT; 
     187        } 
     188 
     189        if (onBackSide) 
     190        { 
     191                return BACK_SIDE; 
     192        } 
     193        else if (onFrontSide) 
     194        { 
     195                return FRONT_SIDE; 
     196        } 
     197 
     198        return COINCIDENT; 
     199} 
     200 
     201/****************************************************************/ 
    88202/*                  class BspTree implementation                */ 
    89203/****************************************************************/ 
     
    128242                } 
    129243 
    130                 delete node; 
    131                 node = NULL; 
     244                DEL_PTR(node); 
    132245        } 
    133246} 
     
    136249void BspTree::InsertViewCell(ViewCell *viewCell) 
    137250{ 
    138         BspNode *currentNode = mRoot; 
     251        std::stack<BspTraversalData> tStack; 
     252         
     253        PolygonQueue polys; 
     254        // copy polygon information to guide the split process 
     255        CopyMesh2Polygon(viewCell->GetMesh(), polys); 
     256 
     257        tStack.push(BspTraversalData(mRoot, NULL, &polys, 0)); 
     258 
     259        while (!tStack.empty()) 
     260        { 
     261                /*BspNode *node = tStack.top(); 
     262 
     263            tStack.pop(); 
     264         
     265                if (!node->IsLeaf()) 
     266                { 
     267                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     268 
     269                        // push the children on the stack (there are always two children) 
     270                        interior->GetBack()->mParent = NULL; 
     271                        interior->GetFront()->mParent = NULL; 
     272 
     273                        tStack.push(interior->GetBack()); 
     274                        tStack.push(interior->GetFront()); 
     275                } 
     276 
     277                DEL_PTR(node);*/ 
     278        } 
     279/*      BspNode *currentNode = mRoot; 
    139280   
    140281        std::stack<BspTraversalData> tStack; 
     
    170311                        } 
    171312                } 
    172         } 
     313        }*/ 
    173314} 
    174315 
     
    177318} 
    178319 
    179 void BspTree::Copy2PolygonSoup(const ObjectContainer &objects, Mesh &mesh) 
     320void BspTree::CopyMesh2Polygon(Mesh *mesh, PolygonQueue &polys) 
     321{ 
     322        FaceContainer::const_iterator fi; 
     323        // copy the face data to polygons 
     324        for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi) 
     325        { 
     326                Polygon *poly = new Polygon((*fi), mesh); 
     327                polys.push(poly); 
     328        } 
     329} 
     330 
     331void BspTree::Copy2PolygonSoup(const ObjectContainer &objects, PolygonQueue &polys) 
    180332{ 
    181333        ObjectContainer::const_iterator it, it_end = objects.end(); 
     
    183335        for (it = objects.begin(); it != it_end; ++ it) 
    184336        { 
    185                 FaceContainer::const_iterator fi; 
    186337                Intersectable *object = *it; 
    187338 
     
    191342                        MeshInstance *inst = dynamic_cast<MeshInstance *>(object); 
    192343 
    193                         Mesh *polys = inst->GetMesh(); 
    194  
    195                         // copy the face data 
    196                         for (fi = polys->mFaces.begin(); fi != polys->mFaces.end(); ++ fi) 
    197                         { 
    198                                 Face *face = new Face((*fi)->mVertexIndices); 
    199                                 mesh.AddFace(face); 
    200                         } 
     344                        Mesh *mesh = inst->GetMesh(); 
     345 
     346                        // copy the mesh data to polygons 
     347                        CopyMesh2Polygon(mesh, polys); 
    201348                } 
    202349        } 
     
    206353{ 
    207354        std::stack<BspTraversalData> tStack; 
    208         Mesh polys; 
     355        PolygonQueue polys; 
    209356         
    210357        // copy mesh instance polygons into one big polygon soup 
     
    220367            tStack.pop(); 
    221368     
    222                 Mesh backPolys; 
    223                 Mesh frontPolys; 
     369                PolygonQueue *backPolys = new PolygonQueue(); 
     370                PolygonQueue *frontPolys = new PolygonQueue(); 
    224371 
    225372                BspNode *node = SubdivideNode(dynamic_cast<BspLeaf *>(data.mNode), 
     
    235382 
    236383                        // push the children on the stack (there are always two children) 
    237                         tStack.push(BspTraversalData(interior->GetBack(), interior, &backPolys, data.mDepth + 1)); 
    238                         tStack.push(BspTraversalData(interior->GetFront(), interior, &frontPolys, data.mDepth + 1)); 
    239                 } 
    240         } 
    241 } 
    242  
    243 Plane3 BspTree::SelectPlane(Mesh *polys)  
     384                        tStack.push(BspTraversalData(interior->GetBack(), interior, backPolys, data.mDepth + 1)); 
     385                        tStack.push(BspTraversalData(interior->GetFront(), interior, frontPolys, data.mDepth + 1)); 
     386                } 
     387        } 
     388} 
     389 
     390Plane3 BspTree::SelectPlane(PolygonQueue *polyQueue)  
    244391{ 
    245392        // TODO: more sophisticated criteria 
    246         return polys->GetFacePlane(0); 
     393        return polyQueue->front()->GetSupportingPlane(); 
    247394} 
    248395 
    249396BspNode *BspTree::SubdivideNode(BspLeaf *leaf, BspInterior *parent,  
    250                                                                 Mesh *polys, const int depth,  
    251                                                                 Mesh &frontPolys, Mesh &backPolys) 
     397                                                                PolygonQueue *polys, const int depth,  
     398                                                                PolygonQueue *frontPolys, PolygonQueue *backPolys) 
    252399{ 
    253400        // terminate traversal if no more faces in mesh 
    254         if ((polys->mFaces.size() <= mMaxPolys) || (depth >= mMaxDepth)) 
    255         { 
    256                 // don't need to store polygon information 
    257                 polys->mFaces->clear(); // HACK: faces were deleted before 
    258                 DEL_PTR(polys); // we can savely delete mesh 
    259  
     401        if ((polys->size() <= mMaxPolys) || (depth >= mMaxDepth)) 
     402        { 
     403                // don't need to store polygon information = delete polygons 
     404                Polygon::DeletePolygons(polys); 
     405                 
    260406                return leaf; 
    261407        } 
     
    264410        BspInterior *node = new BspInterior(SelectPlane(polys));  
    265411 
    266         FaceContainer::const_iterator fi; 
    267  
    268         for (fi = polys->mFaces.begin(); fi != polys->mFaces.end(); ++ fi) 
    269         { 
    270                 Face *face = (*fi); 
     412        while (!polys->empty()) 
     413        { 
     414                Polygon *poly = polys->front(); 
     415 
     416                polys->pop(); 
    271417 
    272418                int  result = 0;// = node->GetPlane()->Side(node->GetPlane()); 
    273419 
    274                 Face *front_piece, *back_piece; 
     420                Polygon *front_piece = NULL; 
     421                Polygon *back_piece = NULL; 
    275422 
    276423                switch (result) 
    277424                { 
    278                         case 1: 
    279                                 frontPolys.AddFace(face); 
     425                        case Polygon::COINCIDENT: 
     426                        case Polygon::FRONT_SIDE: 
     427                                frontPolys->push(poly); 
    280428                                break; 
    281                         case -1: 
    282                                 backPolys.AddFace(face); 
     429                        case Polygon::BACK_SIDE: 
     430                                backPolys->push(poly); 
    283431                                break; 
    284                         case 0: 
    285                                 //Split_Polygon (face, tree->partition, front_piece, back_piece); 
    286                                 backPolys.AddFace(back_piece); 
    287                                 frontPolys.AddFace(front_piece); 
    288  
    289                                 // face not needed anymore 
    290                                 DEL_PTR(face); 
    291  
     432                        case Polygon::SPLIT: 
     433                                front_piece = new Polygon(); 
     434                                back_piece = new Polygon(); 
     435 
     436                                //-- split polygon 
     437                poly->Split(node->GetPlane(), front_piece, back_piece); 
     438 
     439 
     440                                backPolys->push(back_piece); 
     441                                frontPolys->push(front_piece); 
     442 
     443                                // don't need polygon anymore 
     444                                DEL_PTR(poly); 
    292445                                break; 
    293446                        default: 
     447                                frontPolys->push(poly); 
    294448                                break; 
    295449                } 
     
    309463        node->SetupChildLinks(back, front); 
    310464 
    311         // don't need to store polygon information 
    312         polys->mFaces->clear(); // HACK: faces were deleted before 
    313         DEL_PTR(polys); // we can savely delete mesh 
    314  
    315465        DEL_PTR(leaf); 
    316466 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h

    r224 r225  
    44#include "Mesh.h" 
    55#include "Containers.h" 
    6  
     6#include <queue> 
    77 
    88class ViewCell; 
     
    1313class BspTree;   
    1414class BspInterior; 
    15  
     15class Polygon; 
    1616 
    1717  /** 
     
    9191  }; 
    9292   
     93  typedef std::queue<Polygon *> PolygonQueue; 
     94 
     95  /** Class representing a polygon. 
     96  */ 
     97  class Polygon 
     98  { 
     99  public: 
     100          Polygon(); 
     101          Polygon(const VertexContainer &vertices); 
     102 
     103          /** Copies all the vertices of the face. 
     104          */ 
     105          Polygon(Face *face, Mesh *parent); 
     106          
     107           /** Returns supporting plane of this polygon. 
     108          */ 
     109          Plane3 GetSupportingPlane(); 
     110 
     111          /** Splits polygon. 
     112                  @param partition the split plane 
     113                  @param front the front polygon 
     114                  @param back the back polygon 
     115          */ 
     116          void Split(Plane3 *partition, Polygon *front, Polygon *back); 
     117 
     118          enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT}; 
     119 
     120          /** Side of the plane where the polygon is located. 
     121              @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT 
     122          */ 
     123          int Side(Plane3 *plane); 
     124 
     125          static void DeletePolygons(PolygonQueue *polys); 
     126 
     127          /// vertices are connected in counterclockwise order. 
     128          VertexContainer mVertices; 
     129  }; 
     130 
    93131  /** Implementation of the ViewCell BSP tree. */ 
    94132  class BspTree  
    95133  { 
    96134  public: 
     135                   
     136          /** Additional data which is passed down the BSP tree during traversal. 
     137          */ 
    97138          struct BspTraversalData 
    98139          {   
     
    102143                  BspInterior *mParent; 
    103144                  /// polygonal data for splitting 
    104                   Mesh *mPolys; 
     145                  PolygonQueue *mPolys; 
    105146                  /// current depth 
    106147                  int mDepth; 
     
    108149                  BspTraversalData() {} 
    109150                   
    110                   BspTraversalData(BspNode *node, BspInterior *parent, Mesh *polys, const int depth):  
     151                  BspTraversalData(BspNode *node, BspInterior *parent, PolygonQueue *polys, const int depth):  
    111152                  mNode(node), mParent(parent), mPolys(polys), mDepth(depth) {} 
    112153      }; 
    113           
     154 
     155 
    114156          /** Constructs BSP tree from list of view cells. 
    115157          */  
     
    127169          /** Selects a splitting plane from the given polygons.  
    128170          */ 
    129           Plane3 SelectPlane(Mesh *polys); 
     171          Plane3 SelectPlane(PolygonQueue *polyQueue); 
    130172 
    131173          /** Filters next viewcell down the tree and inserts it into the appropriate leaves 
     
    150192          */ 
    151193          BspNode *SubdivideNode(BspLeaf *leaf, BspInterior *parent,  
    152                                                          Mesh *polys, const int depth,  
    153                                                          Mesh &frontPolys, Mesh &backPolys); 
     194                                                         PolygonQueue *polys, const int depth,  
     195                                                         PolygonQueue *frontPolys, PolygonQueue *backPolys); 
    154196 
    155197          /** Extracts the mesh instances of the objects and copies them into the mesh. 
    156198          */ 
    157           static void Copy2PolygonSoup(const ObjectContainer &objects, Mesh &mesh); 
    158  
     199          static void Copy2PolygonSoup(const ObjectContainer &objects, PolygonQueue &polys); 
     200 
     201          static void CopyMesh2Polygon(Mesh *mesh, PolygonQueue &polys); 
    159202 
    160203          /// Pointer to the root of the tree 
Note: See TracChangeset for help on using the changeset viewer.