Ignore:
Timestamp:
10/05/05 02:19:15 (19 years ago)
Author:
mattausch
Message:

bsp tree view cells sampling possible

File:
1 edited

Legend:

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

    r307 r308  
    424424                                interior->ProcessPolygons(&coincident, mStoreSplitPolys); 
    425425 
    426                                 //Debug << "split node on level " << tData.mDepth << " bk: " << backPolys->size() << " frnt: " << frontPolys->size() << endl; 
    427426                                mStat.splits += splits; 
    428427 
     
    437436                else // reached leaf => subdivide current viewcell 
    438437                { 
    439                         //if (tData.mPolygons->size() > 0) 
    440                         //      Debug << "Subdividing further: polygon size: " << (int)tData.mPolygons->size() << ", view cell: " << tData.mViewCell << endl; 
    441  
    442                         BspNode *root = Subdivide(tStack, tData, viewCells);             
    443  
     438                        //if (tData.mPolygons->size() > 0) Debug << "Subdividing further: polygon size: " << (int)tData.mPolygons->size() << ", view cell: " << tData.mViewCell << endl; 
     439                        BspNode *root = Subdivide(tStack, tData);                
     440 
     441                        if (viewCells && root->IsLeaf()) 
     442                        {       // generate new view cell for each leaf 
     443                                ViewCell *viewCell = new ViewCell(); 
     444                                viewCells->push_back(viewCell); 
     445                                 
     446                                dynamic_cast<BspLeaf *>(root)->SetViewCell(viewCell); 
     447                } 
    444448                        if (!mRoot) // tree empty => new root 
    445449                                mRoot = root; 
     
    448452} 
    449453 
    450 int BspTree::AddMesh2Polygons(Mesh *mesh, PolygonContainer &polys, Intersectable *parent) 
     454int BspTree::AddMesh2Polygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent) 
    451455{ 
    452456        FaceContainer::const_iterator fi; 
     
    498502                        mesh = dynamic_cast<MeshInstance *>(object)->GetMesh(); 
    499503                        break; 
    500                 case Intersectable::VIEWCELL: 
     504                case Intersectable::VIEW_CELL: 
    501505                        mesh = dynamic_cast<ViewCell *>(object)->GetMesh(); 
    502506                        break; 
     
    586590 
    587591                // subdivide leaf node 
    588                 BspNode *root = Subdivide(tStack, tData, viewCells); 
     592                BspNode *root = Subdivide(tStack, tData); 
     593 
     594                if (viewCells && root->IsLeaf()) 
     595                {       // generate new view cell for each leaf 
     596                        ViewCell *viewCell = new ViewCell(); 
     597                        viewCells->push_back(viewCell); 
     598 
     599                        dynamic_cast<BspLeaf *>(root)->SetViewCell(viewCell); 
     600                } 
    589601 
    590602                // empty tree => new root corresponding to unbounded space 
     
    598610 
    599611 
    600 BspNode *BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData,  
    601                                                         ViewCellContainer *viewCells) 
     612BspNode *BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData) 
    602613{ 
    603614        //-- terminate traversal   
     
    605616        { 
    606617#ifdef _DEBUG 
    607                 Debug << "subdivision terminated at depth " << tData.mDepth << ", #polys: " << (int)tData.mPolygons->size() << endl; 
     618                Debug << "subdivision terminated at depth " << tData.mDepth << ", #polys: "  
     619                          << (int)tData.mPolygons->size() << endl; 
    608620#endif 
    609621 
     
    611623 
    612624                BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); 
    613  
    614                 //-- new viewcells are generated and added to each leaf 
    615                 if (viewCells) 
    616                 { 
    617                         ViewCell *viewCell = new ViewCell(); 
    618                         leaf->SetViewCell(viewCell); 
    619                         viewCells->push_back(viewCell); 
    620                         // Debug << "creating new viewcell" << endl; 
    621                 } 
    622                 //-- add viewcell stored in split polygon 
    623                 else if (!leaf->GetViewCell()) 
     625                 
     626                // add view cell to leaf 
     627                if (!leaf->GetViewCell()) 
    624628                        leaf->SetViewCell(tData.mViewCell); 
    625                 //else Debug << "Leaf already has view cell " << endl; 
    626629 
    627630                // remaining polygons are discarded or added to node 
    628                 tData.mNode->ProcessPolygons(tData.mPolygons, mStoreSplitPolys); 
     631                leaf->ProcessPolygons(tData.mPolygons, mStoreSplitPolys); 
    629632                DEL_PTR(tData.mPolygons); 
    630633 
    631                 return tData.mNode; 
     634                return leaf; 
    632635        } 
    633636 
     
    644647                                                                                  &coincident); 
    645648 
    646         //-- extract view cells from coincident polygons according to plane normal 
    647649        ViewCell *frontViewCell = mViewCell; 
    648650        ViewCell *backViewCell = mViewCell; 
    649          
    650     if (!viewCells) 
    651         {       // extract the pointer to the view cells 
    652                 ExtractViewCells(&backViewCell, 
    653                                                  &frontViewCell, 
    654                                                  coincident,  
    655                                                  interior->mPlane,  
    656                                                  frontPolys->empty(),  
    657                                                  backPolys->empty()); 
    658         } 
     651 
     652#ifdef _DEBUG    
     653        if (frontPolys->empty() && backPolys->empty() && (coincident.size() > 2)) 
     654        {       for (PolygonContainer::iterator it = coincident.begin(); it != coincident.end(); ++it) 
     655                        Debug << (*it) << " " << (*it)->GetArea() << " " << (*it)->mParent << endl ; 
     656                Debug << endl;} 
     657#endif 
     658 
     659        //-- extract view cells from coincident polygons according to plane normal 
     660        ExtractViewCells(&backViewCell, &frontViewCell, coincident, interior->mPlane,  
     661                                                 frontPolys->empty(), backPolys->empty()); 
    659662         
    660663        // don't need coincident polygons anymore 
     
    684687        PolygonContainer::const_iterator it, it_end = coincident.end(); 
    685688 
     689        //-- find first view cells in front and back leafs 
    686690        for (it = coincident.begin(); !(foundFront && foundBack) &&  
    687                 (it < coincident.end()); ++it) 
     691                (it != it_end); ++ it) 
    688692        { 
    689693                if (DotProd((*it)->GetSupportingPlane().mNormal, splitPlane.mNormal > 0)) 
     
    9961000                if (sSplitPlaneStrategy & BALANCED_VIEW_CELLS) 
    9971001                { 
    998                         Intersectable *viewCell = (*it)->mParent; 
     1002                        MeshInstance *viewCell = (*it)->mParent; 
    9991003                 
    10001004                        if (classification == Polygon3::FRONT_SIDE) 
     
    10111015                val += sLeastSplitsFactor * sumSplits / (float)polys.size(); 
    10121016 
    1013         if ((sSplitPlaneStrategy & LARGEST_POLY_AREA) && sumPolyArea)  
     1017        if (sSplitPlaneStrategy & LARGEST_POLY_AREA)  
    10141018                val += sLargestPolyAreaFactor * (float)polys.size() / sumPolyArea; // HACK 
    10151019 
     
    10221026                ObjectContainer::const_iterator frontIt, frontIt_end = frontViewCells.end(); 
    10231027         
    1024                 Intersectable *intersect = NULL; 
     1028                Intersectable *vc = NULL; 
    10251029                // increase counter for view cells in front of plane 
    10261030                for (frontIt = frontViewCells.begin(); frontIt != frontIt_end; ++frontIt) 
    10271031                { 
    1028                         if (*frontIt != intersect) 
     1032                        if (*frontIt != vc) 
    10291033                        { 
    1030                                 intersect = *frontIt; 
     1034                                vc = *frontIt; 
    10311035                                sumBalancedViewCells += 1.0f; 
    10321036                        } 
     
    10341038 
    10351039                ObjectContainer::const_iterator backIt, backIt_end = backViewCells.end(); 
    1036                 intersect = NULL; 
     1040                vc = NULL; 
    10371041                // decrease counter for view cells on back side of plane 
    10381042                for (backIt = backViewCells.begin(); backIt != backIt_end; ++backIt) 
    10391043                { 
    1040                         if (*backIt != intersect) 
     1044                        if (*backIt != vc) 
    10411045                        { 
    1042                                 intersect = *backIt; 
     1046                                vc = *backIt; 
    10431047                                sumBalancedViewCells -= 1.0f; 
    10441048                        } 
     
    11361140} 
    11371141 
    1138 int BspTree::CollectLeafPvs() 
    1139 { 
    1140         int totalPvsSize = 0; 
    1141    
    1142         stack<BspNode *> nodeStack; 
    1143    
    1144         nodeStack.push(mRoot); 
    1145    
    1146         while (!nodeStack.empty())  
    1147         { 
    1148                 BspNode *node = nodeStack.top(); 
    1149                  
    1150                 nodeStack.pop(); 
    1151                  
    1152                 if (node->IsLeaf())  
    1153                 { 
    1154                         BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); 
    1155  
    1156                         ViewCell *viewcell = leaf->GetViewCell(); 
    1157                          
    1158                         if (!viewcell->Mailed())  
    1159                         { 
    1160                                 viewcell->Mail(); // what does mail mean? 
    1161                                          
    1162                                 // add this node to pvs of all nodes it can see 
    1163                                 BspPvsMap::iterator ni;  
    1164            
    1165         /*                      for (ni = object->mBspPvs.mEntries.begin(); ni != object->mKdPvs.mEntries.end(); ni++)  
    1166                                 { 
    1167                                         BspNode *node = (*ni).first; 
    1168              
    1169                                         // $$ JB TEMPORARY solution -> should add object PVS or explictly computed 
    1170                                         // BSP tree PVS 
    1171                                 if (leaf->mBspPvs.AddNodeSample(node)) 
    1172                                                 totalPvsSize++; 
    1173                                 }*/ 
    1174                         } 
    1175                 } else  
    1176                 { 
    1177                         // traverse tree 
    1178                         BspInterior *interior = (BspInterior *)node; 
    1179        
    1180                         nodeStack.push(interior->GetFront()); 
    1181                         nodeStack.push(interior->GetBack()); 
    1182                 } 
    1183         } 
    1184  
    1185         return totalPvsSize; 
    1186 } 
    1187  
    11881142AxisAlignedBox3 BspTree::GetBoundingBox() const 
    11891143{ 
     
    13031257                        extp = splitPlane->FindIntersection(ray.GetLoc(), extp, &maxt); 
    13041258 
    1305                 } else // compute intersections with objects in leaf 
     1259                } else // reached leaf => intersection with view cell 
    13061260                { 
    13071261                        BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); 
    1308                         //ray.leaves.push_back(leaf); 
    1309       // TODO 
    1310                         /*ObjectContainer::const_iterator mi; 
    1311                         for (mi = leaf->mObjects.begin(); mi != leaf->mObjects.end(); ++mi)  
     1262                         
     1263                        //ray.mBspLeaves.push_back(leaf); 
     1264                         
     1265                        if (!leaf->mViewCell->Mailed()) 
    13121266                        { 
    1313                                 Intersectable *object = *mi; 
    1314                                 if (!object->Mailed())  
    1315                                 { 
    1316                                         object->Mail(); 
    1317                                         //ray.meshes.push_back(mesh); 
    1318                                         hits += object->CastRay(ray); 
    1319                                 } 
    1320                         }*/ 
    1321  
    1322                         if (hits && ray.GetType() == Ray::LOCAL_RAY) 
     1267                                ray.viewCells.push_back(leaf->mViewCell); 
     1268                                mViewCell->Mail(); 
     1269                                ++ hits; 
     1270                        } 
     1271 
     1272                        if (hits && ray.GetType() == Ray::LOCAL_RAY) 
    13231273                                if (ray.intersections[0].mT <= maxt) 
    13241274                                        break; 
     
    13551305        return false; 
    13561306} 
     1307 
     1308void BspTree::CollectViewCells(BspNode *n, ViewCellContainer &viewCells) 
     1309{ 
     1310        stack<BspNode *> nodeStack; 
     1311 
     1312        nodeStack.push(n); 
     1313 
     1314        while (!nodeStack.empty())  
     1315        { 
     1316                BspNode *node = nodeStack.top(); 
     1317                nodeStack.pop(); 
     1318 
     1319                if (node->IsLeaf())  
     1320                { 
     1321                        ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->mViewCell; 
     1322 
     1323                        if (!viewCell->Mailed())  
     1324                        { 
     1325                                viewCell->Mail(); 
     1326                                viewCells.push_back(viewCell); 
     1327                        } 
     1328                } 
     1329                else  
     1330                { 
     1331                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     1332                        nodeStack.push(interior->mFront); 
     1333                        nodeStack.push(interior->mBack); 
     1334                } 
     1335        } 
     1336} 
    13571337//} // GtpVisibilityPreprocessor 
Note: See TracChangeset for help on using the changeset viewer.