Ignore:
Timestamp:
09/16/05 19:24:10 (19 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp

    r276 r286  
    3939/****************************************************************/ 
    4040 
    41 BspNode::BspNode(): mParent(NULL), mPolygons(NULL),mViewCellIdx(0) 
     41BspNode::BspNode(): mParent(NULL), mPolygons(NULL)//,mViewCellIdx(0) 
    4242{} 
    4343 
    44 BspNode::BspNode(BspInterior *parent): mParent(parent), mPolygons(NULL),mViewCellIdx(0) 
     44BspNode::BspNode(BspInterior *parent): mParent(parent), mPolygons(NULL)//,mViewCellIdx(0) 
    4545{} 
    4646 
     
    144144} 
    145145 
    146 bool BspInterior::SplitPolygons(PolygonContainer *polys,  
    147                                                                 PolygonContainer *frontPolys,  
    148                                                                 PolygonContainer *backPolys,  
    149                                                                 PolygonContainer *coincident,  
    150                                                                 int &splits, bool storePolys) 
    151 { 
     146Polygon3 *BspInterior::SplitPolygons(PolygonContainer *polys,  
     147                                                                        PolygonContainer *frontPolys,  
     148                                                                        PolygonContainer *backPolys,  
     149                                                                        int &splits, bool storePolys) 
     150{ 
     151        Polygon3 *splitPoly = NULL; 
    152152#ifdef _Debug 
    153153        if (BspTree::displayDebug)Debug << "Splitting polygons of node " << this << " with plane " << mPlane << endl; 
     
    169169                { 
    170170                        case Polygon3::COINCIDENT: 
     171                                //Debug << "coincident" << endl;         
    171172                                // same surface normal 
    172173                                if (DotProd(mPlane.mNormal, poly->GetSupportingPlane().mNormal) > 0) 
    173174                                { 
    174                                         coincident->push_back(poly); 
     175                                        if (!splitPoly) // store the split polygon if there is none 
     176                                                splitPoly = poly; 
     177                                        else //  discard it otherwise 
     178                                                ProcessPolygon(poly, storePolys); 
     179 
    175180                                        break; 
    176                                 } 
    177                                 //if (BspTree::displayDebug)    Debug << "coincident" << endl;                           
     181                                }                                                        
    178182                                 
    179183                        case Polygon3::FRONT_SIDE:       
    180                                 //if (BspTree::displayDebug)Debug << "front" << endl; 
     184                                //Debug << "front" << endl; 
    181185                                frontPolys->push_back(poly); 
    182186                                break; 
    183187                        case Polygon3::BACK_SIDE: 
    184                                 //if (BspTree::displayDebug)Debug << "back" << endl; 
     188                                //Debug << "back" << endl; 
    185189                                backPolys->push_back(poly); 
    186190                                break; 
    187191                        case Polygon3::SPLIT: 
    188                                 front_piece = new Polygon3(poly->GetParent()); 
    189                                 back_piece = new Polygon3(poly->GetParent()); 
     192                                front_piece = new Polygon3(poly->mParent); 
     193                                back_piece = new Polygon3(poly->mParent); 
    190194 
    191195                                //-- split polygon 
     
    196200 
    197201#ifdef _DEBUG 
    198                                 if (BspTree::displayDebug)Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl; 
     202                                Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl; 
    199203#endif 
    200204                                ProcessPolygon(poly, storePolys); 
     
    207211        } 
    208212        //if (BspTree::displayDebug) Debug << "inside: " << inside << endl; 
    209         // contains nothing 
    210         delete polys; 
     213         
     214        delete polys; // contains nothing 
     215        return splitPoly; 
    211216} 
    212217 
     
    252257mTermMaxDepth(0),  
    253258mRoot(NULL),  
    254 mIsIncremential(false), 
    255259//mStorePolys(true) 
    256260mStorePolys(false) 
     
    354358 
    355359 
    356 void BspTree::InsertViewCell(ViewCell *viewCell) 
     360/*void BspTree::InsertViewCell(ViewCell *viewCell) 
    357361{        
    358362        std::stack<BspTraversalData> tStack; 
     
    384388                                PolygonContainer *frontPolys = new PolygonContainer(); 
    385389                                PolygonContainer *backPolys = new PolygonContainer(); 
    386                                 PolygonContainer *coincident = new PolygonContainer(); 
     390                                Polygon3 *coincident = NULL; 
    387391 
    388392                                int splits = 0; 
     
    400404 
    401405                                // push the children on the stack 
    402                                 tStack.push(BspTraversalData(interior->GetFront(), frontPolys, tData.mDepth + 1)); 
    403                                 tStack.push(BspTraversalData(interior->GetBack(), backPolys, tData.mDepth + 1)); 
     406                                tStack.push(BspTraversalData(interior->GetFront(), frontPolys, tData.mDepth + 1, NULL)); 
     407                                tStack.push(BspTraversalData(interior->GetBack(), backPolys, tData.mDepth + 1, coincident)); 
    404408                         
    405409                        } 
     
    420424                } 
    421425        } 
    422 } 
     426}*/ 
    423427 
    424428void BspTree::InitTree(int maxPolygons, int maxDepth) 
     
    433437 
    434438 
    435 int BspTree::AddMesh2Polygons(Mesh *mesh, PolygonContainer &polys) 
     439int BspTree::AddMesh2Polygons(Mesh *mesh, PolygonContainer &polys, Intersectable *parent) 
    436440{ 
    437441        FaceContainer::const_iterator fi; 
     
    442446        { 
    443447                Polygon3 *poly = new Polygon3((*fi), mesh); 
     448                poly->mParent = parent; // set parent intersectable 
    444449                polys.push_back(poly); 
    445450                //if (displayDebug)Debug << *poly << endl; 
    446                 polysNum ++; 
     451                ++ polysNum; 
    447452        } 
    448453        return polysNum; 
     
    458463                { 
    459464                        mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb 
    460                         AddMesh2Polygons(viewCells[i]->GetMesh(), polys); 
     465                        AddMesh2Polygons(viewCells[i]->GetMesh(), polys, viewCells[i]); 
    461466                } 
    462467        } 
     
    507512        InitTree(sTermMaxPolygons, sTermMaxDepth); 
    508513 
    509     bool savedStorePolys = mStorePolys; 
    510  
    511         // tree is completely constructed once before  
    512         // view cells are inserted one after another =>  
    513         // global tree optimization possible 
    514         if (!mIsIncremential) // todo: not incremential does not work 
    515         { 
    516                 // copy view cell meshes into one big polygon soup 
    517                 PolygonContainer *polys = new PolygonContainer(); 
    518                 Copy2PolygonSoup(viewCells, *polys); 
    519  
    520                 // polygons are stored only during view cell insertion 
    521                 mStorePolys = false; 
    522  
    523                 // construct tree from viewcell polygons 
    524                 Construct(polys); 
    525                 //Export("bsp.x3d"); 
    526         } 
    527          
    528         //-- insert all viewcells one after another 
    529         ViewCellContainer::const_iterator it; 
    530  
    531         mStorePolys = savedStorePolys; 
    532         mStat.polys = 0; 
    533         mStat.splits = 0; 
    534         mStat.accumDepth = 0; 
    535  
    536         long startTime = GetTime(); 
    537 //displayDebug = true; 
    538         counter = 0; 
    539         Debug << "**** Starting view cell insertion ****" << endl; 
    540         for (it = viewCells.begin(); it != viewCells.end(); ++ it) 
    541         { 
    542                 //if ((counter == 12) || (counter == 14)){ 
    543                 //Debug << "** inserting view cell " << counter << " **" << endl; 
    544                 InsertViewCell(*it); 
    545         //}     counter ++; 
    546         } 
    547  
    548         Debug << "**** Finished view cell insertion ****" << endl; 
    549         Debug << "insertion time: "<< TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
     514        // copy view cell meshes into one big polygon soup 
     515        PolygonContainer *polys = new PolygonContainer(); 
     516        Copy2PolygonSoup(viewCells, *polys); 
     517 
     518        // construct tree from viewcell polygons 
     519        Construct(polys); 
     520        //Export("bsp.x3d"); 
    550521} 
    551522 
     
    574545        std::stack<BspTraversalData> tStack; 
    575546         
    576         BspTraversalData tData(new BspLeaf(), polys, 0, true, viewCells); 
     547        BspTraversalData tData(new BspLeaf(), polys, 0, NULL); 
    577548 
    578549        tStack.push(tData); 
     
    585556            tStack.pop(); 
    586557 
    587                 //TODO 
    588                 ViewCell *viewCell = NULL; 
    589  
    590         /*if (viewCells) // generate new view cell in leaf 
    591                 { 
    592                         viewCell = new ViewCell(); 
    593                         viewCells->push_back(viewCell); 
    594                 }*/ 
    595  
    596558                // subdivide leaf node 
    597                 BspNode *root = Subdivide(tStack, tData, viewCell); 
     559                BspNode *root = Subdivide(tStack, tData, viewCells); 
    598560 
    599561                // empty tree => new root corresponding to unbounded space 
     
    608570 
    609571BspNode *BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData,  
    610                                                         ViewCell *viewCell) 
     572                                                        ViewCellContainer *viewCells) 
    611573{ 
    612574        //-- terminate traversal   
    613575        if ((tData.mPolygons->size() <= mTermMaxPolygons) || (tData.mDepth >= mTermMaxDepth)) 
    614                 // if there is another view cell associated with this leaf => subdivide further 
    615                 //&& !(viewCell && tData.mIsInside && dynamic_cast<BspLeaf *>(tData.mNode)->GetViewCell()))   
    616576        { 
    617577#ifdef _DEBUG 
    618                 if (displayDebug)Debug << "subdivision terminated at depth " << tData.mDepth << ", #polys: " << (int)tData.mPolygons->size() << endl; 
     578                Debug << "subdivision terminated at depth " << tData.mDepth << ", #polys: " << (int)tData.mPolygons->size() << endl; 
    619579#endif 
    620580 
    621581                EvaluateLeafStats(tData); 
    622582 
    623             // add view cell if inside object)  
    624                 if (viewCell && tData.mCoincident && (tData.mCoincident->size() > 0))) // || (tData.mGeometry->size() > 0) // or if there is still geometry left  
    625                 { 
    626                         BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); 
    627  
     583                BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); 
     584 
     585                //-- new viewcells are generated and added to each leaf 
     586                if (viewCells) 
     587                { 
     588                        ViewCell *viewCell = new ViewCell(); 
     589                        leaf->SetViewCell(viewCell); 
     590                        viewCells->push_back(viewCell); 
     591                        Debug << "creating new viewcell" << endl; 
     592                } 
     593                //-- add viewcell stored in split polygon 
     594                else if (tData.mSplitPoly && tData.mSplitPoly->mParent) 
     595                { 
    628596                        if (leaf->GetViewCell()) 
    629597                                Debug << "ERROR: leaf already has view cell " << endl;//leaf->mViewCellIdx << endl; 
    630598 
    631                         //leaf->mViewCellIdx = counter; 
    632                         //Debug << "insert view cell" << endl; 
    633  
    634                         leaf->SetViewCell(coincident[0]->GetParent()); 
    635                 } 
    636  
     599                        //leaf->mViewCellIdx = counter;Debug << "insert view cell" << endl; 
     600 
     601                        leaf->SetViewCell(dynamic_cast<ViewCell *>(tData.mSplitPoly->mParent)); 
     602 
     603                        // discard split polygon 
     604                        tData.mNode->GetParent()->ProcessPolygon(tData.mSplitPoly, mStorePolys); 
     605                } 
     606         
    637607                // add or delete remaining polygons 
    638608                tData.mNode->ProcessPolygons(tData.mPolygons, mStorePolys); 
    639                 tData.mNode->ProcessPolygons(tData.coincident, mStorePolys); 
    640  
     609                                 
    641610                return tData.mNode; 
    642611        } 
    643612 
    644         tData.mNode->ProcessPolygons(tData.coincident, mStorePolys); 
     613        // discard the split polygon (not needed if traversal continues) 
     614        if (tData.mSplitPoly) 
     615                tData.mNode->GetParent()->ProcessPolygon(tData.mSplitPoly, mStorePolys); 
    645616 
    646617        //-- create new subdivided node 
    647618        PolygonContainer *backPolys = new PolygonContainer(); 
    648619        PolygonContainer *frontPolys = new PolygonContainer(); 
    649         PolygonContainer *coincident = new PolygonContainer(); 
     620        Polygon3 *splitPoly = NULL; 
    650621         
    651622        BspInterior *interior = SubdivideNode(dynamic_cast<BspLeaf *>(tData.mNode), 
     
    653624                                                                                  frontPolys, 
    654625                                                                                  backPolys,  
    655                                                                                   coincident); 
     626                                                                                  &splitPoly); 
    656627 
    657628        // push the children on the stack 
    658629        // inside information is only propagated with the back leaf 
    659         tStack.push(BspTraversalData(interior->GetBack(), backPolys, tData.mDepth + 1, coincident)); 
    660         tStack.push(BspTraversalData(interior->GetFront(), frontPolys, tData.mDepth + 1)); 
     630        tStack.push(BspTraversalData(interior->GetFront(), frontPolys, tData.mDepth + 1, NULL)); 
     631        tStack.push(BspTraversalData(interior->GetBack(), backPolys, tData.mDepth + 1, splitPoly)); 
    661632 
    662633        return interior; 
     
    667638                                                                        PolygonContainer *polys,  
    668639                                                                        PolygonContainer *frontPolys, 
    669                                                                         PolygonContainer *coincident,  
    670                                                                         PolygonContainer *backPolys, bool &inside) 
     640                                                                        PolygonContainer *backPolys, Polygon3 **splitPoly) 
    671641{ 
    672642        mStat.nodes += 2; 
     
    682652        int splits = 0; 
    683653                 
    684         inside = interior->SplitPolygons(polys, frontPolys, backPolys, coincident, splits, mStorePolys); 
     654        *splitPoly = interior->SplitPolygons(polys, frontPolys, backPolys, splits, mStorePolys); 
    685655         
    686656        mStat.splits += splits; 
Note: See TracChangeset for help on using the changeset viewer.