Changeset 237 for trunk


Ignore:
Timestamp:
08/12/05 18:42:20 (19 years ago)
Author:
mattausch
Message:

added bsp stuff

Location:
trunk/VUT/GtpVisibilityPreprocessor
Files:
9 edited

Legend:

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

    r235 r237  
    6060BspTree { 
    6161#       splitPlaneStrategy leastSplits 
    62 splitPlaneStrategy nextPolygon 
     62        splitPlaneStrategy nextPolygon 
    6363#       constructionMethod viewCells 
    6464        constructionMethod sceneGeometry 
    6565        Termination { 
    66                 maxPolygons 2 
    67                 maxDepth 18 
     66                maxPolygons 4 
     67                maxDepth 35 
    6868        } 
    6969} 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h

    r225 r237  
    4141                           float *t = NULL, 
    4242                           bool *coplanar = NULL 
    43                            ) const {return Vector3(0,0,0);} // TODO 
     43                           ) const  
     44  { 
     45          const Vector3 v = b - a; // line from A to B 
     46 
     47          float dv = DotProd(mNormal, v); 
     48          float u = 0; 
     49         
     50          if (dv) 
     51          { 
     52                  u = - Distance(a) / dv; 
     53                  Debug << "t: " << u << ", dv: " << dv << endl; 
     54                  if (coplanar) (*coplanar) = false; 
     55          } 
     56          else { 
     57                  Debug << "OHOH: " << u << endl; 
     58                  if (coplanar) (*coplanar) = true;        
     59          } 
     60          if (t) (*t) = u; 
     61          Debug << "A: " << a << ", B: " << b << ", result: " << (a + (u * v)) << endl; 
     62           
     63          return a + (u * v); 
     64  } 
    4465 
    4566  friend bool 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp

    r235 r237  
    1212        VertexIndexContainer::const_iterator it; 
    1313 
     14        Debug << "Creating polygon:\n"; 
     15 
    1416        for (it = face->mVertexIndices.begin(); it != face->mVertexIndices.end(); ++ it) 
    1517        { 
    1618                mVertices.push_back(parent->mVertices[*it]); 
     19                Debug << parent->mVertices[*it] << endl; 
    1720        } 
    1821} 
     
    3740{ 
    3841        splits = 0; 
    39         Vector3 ptA = mVertices[mVertices.size() - 1];; 
     42        Vector3 ptA = mVertices[mVertices.size() - 1]; 
    4043         
    4144        int sideA = partition->Side(ptA); 
     
    5053 
    5154                // vertices on different sides => split 
    52                 if ((sideA != 0) && (sideB != 0) && (sideA != sideB))  
     55            if (sideB > 0) 
    5356                { 
    54                         Vector3 v = ptB - ptA; // line from A to B 
    55                         float dv = DotProd(partition->mNormal, v); 
    56                         float t = 0; 
     57                        if (sideA < 0) 
     58                        { 
     59                                //-- plane - line intersection 
     60                                Vector3 splitPt = partition->FindIntersection(ptA, ptB); 
    5761                         
    58                         if (dv) 
     62                                // add vertex to both polygons 
     63                                front->mVertices.push_back(splitPt); 
     64                                back->mVertices.push_back(splitPt); 
     65                         
     66                                ++ splits; 
     67                        } 
     68                        front->mVertices.push_back(ptB); 
     69                } 
     70                else if (sideB < 0) 
     71                { 
     72                        if (sideA > 0) 
    5973                        { 
    60                                 t = - partition->Distance(ptA) / dv; 
     74                                //-- plane - line intersection 
     75                                Vector3 splitPt = partition->FindIntersection(ptA, ptB); 
     76                         
     77                                // add vertex to both polygons 
     78                                front->mVertices.push_back(splitPt); 
     79                                back->mVertices.push_back(splitPt); 
     80 
     81                                ++ splits; 
    6182                        } 
    62  
    63                         ++ splits; 
    64                 } 
    65                 if (sideB >= 0) 
    66                 { 
    6783                        back->mVertices.push_back(ptB); 
    6884                } 
    69                 else if (sideB <= 0) 
     85                else 
    7086                { 
     87                        // vertex on plane => add vertex to both polygons 
    7188                        front->mVertices.push_back(ptB); 
     89                        back->mVertices.push_back(ptB); 
    7290                } 
    73  
     91         
    7492                ptA = ptB; 
    7593                sideA = sideB; 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h

    r236 r237  
    5757}; 
    5858 
     59// Overload << operator for C++-style output 
     60inline ostream& 
     61operator<< (ostream &s, const Polygon3 &A) 
     62{ 
     63        VertexContainer::const_iterator it; 
     64 
     65        s << "Polygon:\n"; 
     66         
     67        for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it) 
     68                s << *it << endl; 
     69         
     70        return s; 
     71} 
    5972 
    6073 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp

    r235 r237  
    6969        char constructionMethodStr[64]; 
    7070 
    71         environment->GetStringValue("BspTree.constructionMethod", 
    72                            constructionMethodStr); 
     71        environment->GetStringValue("BspTree.constructionMethod", constructionMethodStr); 
    7372 
    7473        int constructionMethod = BspTree::VIEWCELLS; 
    75  
     74         
    7675        if (strcmp(constructionMethodStr, "viewCells") == 0) 
    7776                constructionMethod = BspTree::VIEWCELLS; 
     
    8483    } 
    8584 
    86         Debug << constructionMethodStr << endl; 
    87  
    8885        switch (constructionMethod) 
    8986        { 
    9087        case BspTree::VIEWCELLS: 
     88 
    9189                mViewcells.clear(); 
     90                 
    9291                // derive viewcells from the scene objects 
    9392                ViewCell::DeriveViewCells(objects, mViewcells, 1000); 
     93                 
    9494                mBspTree->Construct(mViewcells); 
    9595                break; 
     
    102102        return true; 
    103103} 
    104  
    105104 
    106105 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp

    r235 r237  
    5858                                                           const int max) 
    5959{ 
     60        // maximal max viewcells 
    6061        int n = max > 0 ? std::min((int)objects.size(), max) : (int)objects.size(); 
     62 
    6163        for (int i = 0; i < n; ++i) 
    6264        { 
     
    7173                        viewCells.push_back(viewCell); 
    7274                } 
     75                //TODO: transformed meshes 
    7376        } 
    7477} 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp

    r236 r237  
    9696                polys->pop(); 
    9797 
     98                Debug << (*poly); 
     99 
    98100                int result = poly->Side(&mPlane); 
    99101 
     
    104106                { 
    105107                        case Polygon3::COINCIDENT: 
    106                                 break; 
     108                                //break; //TODO: comment in 
    107109                        case Polygon3::FRONT_SIDE: 
    108110                                frontPolys->push(poly); 
     
    118120                                poly->Split(&mPlane, front_piece, back_piece, splits); 
    119121                         
     122                                Debug << "SPLIT, plane: " << mPlane << "\norginal: " << (*poly) << "\nback: " << (*back_piece) << "\nfront: " << (*front_piece) << endl; 
    120123                                backPolys->push(back_piece); 
    121124                                frontPolys->push(front_piece); 
     
    123126                                // don't need polygon anymore 
    124127                                DEL_PTR(poly); 
     128 
    125129                                break; 
    126130                        default: 
     
    128132                } 
    129133        } 
    130         // delete old polygons 
    131 //      Polygon3::DeletePolygons(polys); 
    132134} 
    133135 
     
    204206 
    205207        app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n"<< 
    206         maxDepthNodes*100/(double)Leaves()<<endl; 
    207  
    208         app << "#N_PMAXDEPTH ( Maximal reached depth )\n"<< 
    209         maxDepth*100/(double)Leaves()<<endl; 
     208        maxDepthNodes * 100 / (double)Leaves() << endl; 
     209 
     210        app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth <<endl; 
    210211 
    211212        app << "#N_ADDED_RAYREFS  (Number of dynamically added ray references )\n"<< 
     
    334335        ObjectContainer::const_iterator it, it_end = objects.end(); 
    335336 
    336         for (it = objects.begin(); it != it_end; ++ it) 
    337         { 
    338                 Intersectable *object = *it; 
     337        int limit = min((int)objects.size(), 1); 
     338 
     339        //for (it = objects.begin(); it != it_end; ++ it) 
     340        for (int i = 0; i < limit; ++i) 
     341        { 
     342                Intersectable *object = objects[i];//*it; 
    339343                Mesh *mesh = NULL; 
    340344 
     
    343347                { 
    344348                case Intersectable::MESH_INSTANCE: 
    345                          
    346349                        mesh = dynamic_cast<MeshInstance *>(object)->GetMesh(); 
    347                          
     350                        break; 
    348351                case Intersectable::VIEWCELL: 
    349352                        mesh = dynamic_cast<ViewCell *>(object)->GetMesh(); 
    350                         // copy the mesh data to polygons 
    351                         CopyMesh2Polygons(mesh, polys); 
    352353                        break; 
    353354                default: 
    354355                        break; 
    355356                } 
    356         } 
     357                 
     358                if (mesh) // copy the mesh data to polygons 
     359                { 
     360                        CopyMesh2Polygons(mesh, polys); 
     361                } 
     362        } 
     363 
    357364        Debug << "number of polygons: " << polys.size() << endl; 
    358365} 
     
    360367void BspTree::Construct(const ObjectContainer &objects) 
    361368{ 
     369        Debug << "Constructing tree using object container\n"; 
     370 
    362371        mTermMaxPolygons = sTermMaxPolygons; 
    363372        mTermMaxDepth = sTermMaxDepth; 
     
    376385        while (!tStack.empty())  
    377386        { 
    378             tData = tStack.top(); 
     387                tData = tStack.top(); 
    379388            tStack.pop(); 
    380      
     389                 
    381390                Subdivide(tStack, tData); 
    382391        } 
     
    385394void BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell) 
    386395{ 
     396        //Debug << "Subdividing node\n"; 
     397 
    387398        PolygonQueue *backPolys = new PolygonQueue(); 
    388399        PolygonQueue *frontPolys = new PolygonQueue(); 
     
    404415                tStack.push(BspTraversalData(interior->GetFront(), interior, frontPolys, tData.mDepth + 1)); 
    405416        } 
    406         else 
     417        else // tree terminates here 
     418        { 
    407419                EvaluateLeafStats(tData); 
     420                // don't need to store polygon information => delete polygons 
     421                Polygon3::DeletePolygons(tData.mPolygons); 
     422        } 
    408423} 
    409424 
     
    434449        if ((polys->size() <= mTermMaxPolygons) || (depth >= mTermMaxDepth)) 
    435450        { 
    436                 // don't need to store polygon information => delete polygons 
    437                 Polygon3::DeletePolygons(polys); 
    438451                // Debug << polys->size() << ", " << depth << endl;              
    439452                return leaf; 
     
    444457        // add the new nodes to the tree + select subdivision plane 
    445458        BspInterior *node = new BspInterior(SelectPlane(polys));  
    446  
     459        Debug << "new bspinterior: " << (*node) << endl; 
    447460        // split polygon according to current plane 
    448461        int splits = 0; 
     
    463476        node->SetupChildLinks(back, front); 
    464477 
    465         DEL_PTR(leaf); 
     478        DEL_PTR(leaf); // leaf not member of tree anymore 
    466479 
    467480        return node; 
     
    473486        environment->GetIntValue("BspTree.Termination.maxPolygons", sTermMaxPolygons); 
    474487 
     488        //-- extract strategy to choose the next split plane 
    475489        char splitPlaneStrategyStr[60]; 
    476490 
    477491        environment->GetStringValue("BspTree.splitPlaneStrategy", splitPlaneStrategyStr); 
    478         // TODO: Extract nodes 
     492         
     493        sSplitPlaneStrategy = BspTree::NEXT_POLYGON; 
     494         
     495        if (strcmp(splitPlaneStrategyStr, "nextPolygon") == 0) 
     496                sSplitPlaneStrategy = BspTree::NEXT_POLYGON; 
     497        else if (strcmp(splitPlaneStrategyStr, "leastSplits") == 0) 
     498                sSplitPlaneStrategy = BspTree::LEAST_SPLITS; 
     499        else  
     500        { 
     501                cerr << "Wrong BSP split plane strategy " << splitPlaneStrategyStr << endl; 
     502                exit(1); 
     503    } 
    479504} 
    480505 
    481506void BspTree::EvaluateLeafStats(const BspTraversalData &data) 
    482507{ 
    483  
    484508  // the node became a leaf -> evaluate stats for leafs 
    485509  BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode); 
    486510 
    487   if (data.mDepth > mTermMaxDepth) 
    488     ++ mStat.maxDepthNodes;  
     511  if (data.mDepth >= mTermMaxDepth) 
     512  { 
     513          ++ mStat.maxDepthNodes;  
     514  } 
    489515 
    490516  // record maximal depth 
    491517  if (data.mDepth > mStat.maxDepth) 
    492518          mStat.maxDepth = data.mDepth; 
     519 
     520  Debug << "BSP Traversal data. Depth: " << data.mDepth << " (max: " << mTermMaxDepth<< "), #polygons: " <<  
     521          data.mPolygons->size() << " (max: " << mTermMaxPolygons << ")" << endl; 
    493522} 
    494523//} // GtpVisibilityPreprocessor 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h

    r236 r237  
    145145        void SplitPolygons(PolygonQueue *polys, PolygonQueue *frontPolys, PolygonQueue *backPolys, int &splits); 
    146146 
     147        friend ostream &operator<<(ostream &s, const BspInterior &A) 
     148        { 
     149                return s << A.mPlane; 
     150        } 
     151 
     152 
    147153protected: 
    148154         
     
    155161}; 
    156162 
    157  
    158 /** BSP leaf node implementation */ 
     163/** BSP leaf node implementation. 
     164*/ 
    159165class BspLeaf : public BspNode  
    160166{ 
     
    276282        /// Pointer to the root of the tree 
    277283        BspNode *mRoot; 
     284 
    278285        /// Pointer to the root cell of the viewspace 
    279286        // ViewCell *mRootCell; 
    280          
    281          
     287                 
    282288        BspTreeStatistics mStat; 
    283289 
     
    286292        int mTermMaxDepth; 
    287293 
     294        /// Strategies for choosing next split plane. 
    288295        enum {NEXT_POLYGON, LEAST_SPLITS}; 
    289296 
  • trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp

    r235 r237  
    4848  } 
    4949 
    50   if (0) { 
     50  if (1) { 
    5151    Camera camera; 
    52     camera.LookAtBox(p->mKdTree->GetBox()); 
     52    //camera.LookAtBox(p->mKdTree->GetBox()); 
     53        camera.LookInBox(p->mKdTree->GetBox()); 
     54        camera.SetPosition(camera.mPosition + Vector3(0,300,0)); 
    5355    camera.SnapImage("camera.jpg", p->mKdTree); 
    5456 
Note: See TracChangeset for help on using the changeset viewer.