Changeset 1545


Ignore:
Timestamp:
09/29/06 22:42:25 (18 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/Lib/Vis/Preprocessing/src
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/BoundingBoxConverter.h

    r1006 r1545  
    1313 
    1414        IndexedBoundingBox(int idx, const AxisAlignedBox3 &box): IndexedBoundingBoxParent(idx, box) 
    15         { 
    16  
    17         } 
     15        {} 
    1816}; 
    1917 
  • GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp

    r1522 r1545  
    236236bool 
    237237Environment::GetParam(const char name, 
    238                        const int index, 
    239                        char *value) const 
     238                                          const int index, 
     239                                          char *value) const 
    240240{ 
    241241  int column; 
     
    15221522                "0"); 
    15231523 
    1524          
     1524        RegisterOption("ViewCells.loadGeometry", 
     1525                optBool,  
     1526                "view_cells_load_geometry=",  
     1527                "false"); 
     1528         
     1529        RegisterOption("ViewCells.geometryFilename", 
     1530                optString, 
     1531                "view_cells_geometry_filename=", 
     1532                "viewCellsGeometry.x3d"); 
     1533 
     1534        RegisterOption("ViewCells.useBaseTrianglesAsGeometry", 
     1535                optBool,  
     1536                "view_cells_use_base_triangles_as_geometry=",  
     1537                "false"); 
     1538 
     1539         
     1540 
    15251541        /****************************************************************************/ 
    15261542        /*                     Render simulation related options                    */ 
     
    16671683        RegisterOption("BspTree.Visualization.exportSplits", 
    16681684                optBool, 
    1669                 "bsp_visualization.export_splits", 
     1685                "bsp_visualization.export_splits=", 
    16701686                "false"); 
    16711687 
     
    24952511                                        "-1"); 
    24962512 
    2497         RegisterOption("Hierarchy.Construction.useMultiLevelConstruction", 
    2498                                         optBool, 
    2499                                         "hierarchy_construction_use_multilevel_construction=", 
     2513        RegisterOption("Hierarchy.Construction.useMultiLevel", 
     2514                                        optBool, 
     2515                                        "hierarchy_construction_multilevel=", 
    25002516                                        "false"); 
    25012517 
  • GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp

    r1533 r1545  
    99#include "RayCaster.h" 
    1010#include "Exporter.h" 
     11#include "SamplingStrategy.h" 
    1112 
    1213 
     
    2425 
    2526GvsPreprocessor::GvsPreprocessor():  
    26 Preprocessor(), mSamplingType(0), mSampleContriPerPass(0), mTotalSampleContri(0), 
    27 mReverseSamples(0), mBorderSamples(0) 
     27Preprocessor(),  
     28//mSamplingType(SamplingStrategy::DIRECTION_BASED_DISTRIBUTION), 
     29mSamplingType(SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION), 
     30mSampleContriPerPass(0),  
     31mTotalSampleContri(0), 
     32mReverseSamples(0),  
     33mBorderSamples(0) 
    2834{ 
    2935        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples); 
     
    5359#if 0 
    5460        if ((dist - newDist) > mThresHold) 
    55 #else // rather take relative distance 
     61#else  
     62        // rather take relative distance 
    5663        if ((dist / newDist) > mThreshold) 
    5764#endif 
     
    6269                        delete newRay; 
    6370                } 
    64  
    6571                return true; 
    6672        } 
     
    7985                //cout << " h " << mSampleContriPerPass << " " << mSamplesPerPass << " " << mTotalSamples << endl; 
    8086                mRayQueue.push(vssRay); 
    81                 mVssRays.push_back(new VssRay(*vssRay)); 
    82          
    83                 ++ mSampleContriPerPass; 
     87                //mVssRays.push_back(new VssRay(*vssRay)); 
     88        ++ mSampleContriPerPass; 
    8489 
    8590                return true; 
     
    179184                SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin); 
    180185         
    181                 VssRay *newRay = mRayCaster->CastSingleRay(sray, mViewSpaceBox); 
    182  
     186                VssRay *newRay = mRayCaster->CastRay(sray, mViewSpaceBox, false); 
     187                //cout << "\np1: " << p1 << "\np : " <<  p << "\np2: " << p2 << endl; 
    183188                if (!newRay)  
    184189                        return 0; 
    185  
     190cout << "here42" << endl; 
    186191                const bool enqueued = HandleRay(newRay); 
    187192                 
     
    230235                simpleRays.AddRay(sr); 
    231236        } 
    232  
     237#if 0 
    233238        VizStruct dummy; 
    234239        dummy.enlargedTriangle = new Polygon3(enlargedTriangle); 
     
    236241        //dummy.ray = new VssRay(currentRay); 
    237242        vizContainer.push_back(dummy); 
     243#endif 
    238244 
    239245        // cast rays to triangle vertices and determine visibility 
     
    260266#endif 
    261267 
    262         mBorderSamples = castRays; 
     268        mBorderSamples += castRays; 
    263269        return castRays; 
    264270} 
     
    322328        VssRayContainer samples; 
    323329        CastRays(simpleRays, samples, true); 
    324          
    325330        // add to ray queue 
    326331        EnqueueRays(samples); 
    327332 
    328         Debug << "generated " <<  numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl; 
     333        //Debug << "generated " <<  numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl; 
    329334 
    330335        return (int)samples.size(); 
     
    346351int GvsPreprocessor::Pass() 
    347352{ 
    348         const int mSampleType = 0; 
    349353        // reset samples 
    350354        int castSamples = 0; 
    351355        mSampleContriPerPass = 0; 
    352          
     356 
    353357        while (castSamples < mSamplesPerPass)  
    354358        {        
    355359                // Ray queue empty =>  
    356                 // cast a number of uniform samples to fill ray Queue 
    357                 castSamples += CastInitialSamples(mInitialSamples, mSampleType); 
     360                // cast a number of uniform samples to fill ray queue 
     361                castSamples += CastInitialSamples(mInitialSamples, mSamplingType); 
    358362                castSamples += ProcessQueue(); 
    359                 cout << "\ncast " << castSamples << " samples in a processing pass" << endl; 
     363                //cout << "\ncast " << castSamples << " samples in a processing pass" << endl; 
    360364        } 
    361365 
     
    369373{ 
    370374        int castSamples = 0; 
     375        ++ mGvsPass; 
    371376 
    372377        while (!mRayQueue.empty())  
     
    375380                VssRay *ray = mRayQueue.top(); 
    376381                mRayQueue.pop(); 
    377          
     382 
    378383                castSamples += AdaptiveBorderSampling(*ray); 
    379                  
    380384                delete ray; 
    381385        } 
     
    388392{ 
    389393        cout << "Gvs Preprocessor started\n" << flush; 
    390  
     394        const long startTime = GetTime(); 
     395 
     396        mViewSpaceBox = mKdTree->GetBox(); 
    391397        Randomize(0); 
    392         const long startTime = GetTime(); 
     398         
     399        mPass = 0; 
     400        mGvsPass = 0; 
     401        mSampleContriPerPass = 0;  
     402        mTotalSampleContri = 0; 
     403        mReverseSamples = 0; 
     404        mBorderSamples = 0; 
     405 
    393406        int castSamples = 0; 
    394         mViewSpaceBox = mKdTree->GetBox(); 
    395          
     407 
    396408        if (!mLoadViewCells) 
    397409        {       /// construct the view cells from the scratch 
     
    406418                //////// 
    407419                //-- stats 
    408                 //cout << "+"; 
    409                 cout << "\nsamples cast: " << castSamples << " of " << mTotalSamples << endl; 
     420                cout << "\nPass " << mPass << " #samples: " << castSamples << " of " << mTotalSamples << endl; 
    410421 
    411422                //mVssRays.PrintStatistics(mStats); 
    412                 mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl 
    413                            << "#TotalSamples\n" << castSamples << endl 
    414                            << "#ContributingSamples\n" << mTotalSampleContri << endl 
    415                            << "#Reverse samples\n" << mReverseSamples << endl 
    416                            << "#Border samples\n" << mBorderSamples << endl; 
    417                                  
     423                mStats  
     424                        << "#Pass\n" << mPass << endl 
     425                        << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl 
     426                        << "#TotalSamples\n" << castSamples << endl 
     427                        << "#ScDiff\n" << mSampleContriPerPass << endl 
     428                        << "#SamplesContri\n" << mTotalSampleContri << endl 
     429                        << "#ReverseSamples\n" << mReverseSamples << endl 
     430                        << "#BorderSamples\n" << mBorderSamples << endl                  
     431                        << "#GvsRuns\n" << mGvsPass << endl; 
     432 
    418433 
    419434                mViewCellsManager->PrintPvsStatistics(mStats); 
    420435                // ComputeRenderError(); 
    421         } 
    422  
    423         Visualize();     
     436                ++ mPass; 
     437        } 
     438 
     439        cout << 2 * castSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M rays/s" << endl; 
     440        Visualize(); 
     441 
    424442        return true; 
    425443} 
  • GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h

    r1533 r1545  
    116116 
    117117        void Visualize(); 
     118 
    118119        ////////////////////// 
    119120 
     
    121122 
    122123        int mSamplesPerPass; 
     124        int mTotalSamples; 
     125        int mInitialSamples; 
     126 
    123127        RayQueue mRayQueue;  
    124128        int mSamplingType; 
    125         int mTotalSamples; 
    126         int mInitialSamples; 
     129 
    127130        AxisAlignedBox3 mViewSpaceBox; 
    128131        float mEps; 
    129132        float mThreshold; 
    130133        VssRayContainer mVssRays; 
     134         
     135        /////////// 
     136        // stats 
    131137        int mSampleContriPerPass; 
    132138        int mTotalSampleContri; 
    133139        int mReverseSamples; 
    134140        int mBorderSamples; 
     141        int mGvsPass; 
    135142}; 
    136143 
  • GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp

    r1528 r1545  
    396396Preprocessor::Export( const string filename, 
    397397                                          const bool scene, 
    398                                           const bool kdtree, 
    399                                           const bool bsptree 
     398                                          const bool kdtree 
    400399                                          ) 
    401400{ 
     
    403402         
    404403  if (exporter) { 
    405     if (0 && scene) 
     404    if (2 && scene) 
    406405      exporter->ExportScene(mSceneGraph->GetRoot()); 
    407406 
    408     if (0 && kdtree) { 
     407    if (1 && kdtree) { 
    409408      exporter->SetWireframe(); 
    410409      exporter->ExportKdTree(*mKdTree); 
    411410    } 
    412  
    413         if (0 && bsptree) { 
    414                 //exporter->SetWireframe(); 
    415                 exporter->ExportBspTree(*mBspTree); 
    416         } 
    417411 
    418412    delete exporter; 
     
    447441                // default view space is the extent of the scene 
    448442                mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox()); 
     443 
     444                bool loadVcGeometry; 
     445                Environment::GetSingleton()->GetBoolValue("ViewCells.loadGeometry", loadVcGeometry); 
     446 
     447                bool extrudeBaseTriangles; 
     448                Environment::GetSingleton()->GetBoolValue("ViewCells.useBaseTrianglesAsGeometry", extrudeBaseTriangles); 
     449 
     450                char vcGeomFilename[100]; 
     451                Environment::GetSingleton()->GetStringValue("ViewCells.geometryFilename", vcGeomFilename); 
     452                 
     453                if (mViewCellsManager->GetType() == ViewCellsManager::BSP) 
     454                { 
     455                        if (!mViewCellsManager->LoadViewCellsGeometry(vcGeomFilename, extrudeBaseTriangles)) 
     456                        { 
     457                                cerr << "loading view cells geometry failed" << endl; 
     458                        } 
     459                } 
     460                else 
     461                { 
     462                        cerr << "loading view cells geometry is not implemented for this manager" << endl; 
     463                } 
    449464        } 
    450465         
  • GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h

    r1528 r1545  
    9898  SamplingStrategy *GenerateSamplingStrategy(const int strategyId) const; 
    9999 
     100  /** Export preprocessor data. 
     101  */ 
    100102  bool Export( 
    101103                const string filename,  
    102104                const bool scene,  
    103                 const bool kdtree,  
    104                 const bool bsptree);   
     105                const bool kdtree);   
    105106   
    106107  virtual void KdTreeStatistics(ostream &s); 
  • GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj

    r1521 r1545  
    748748                                RelativePath="..\src\X3dParser.h"> 
    749749                        </File> 
    750                         <File 
    751                                 RelativePath="..\src\X3dParserXerces.h"> 
    752                         </File> 
    753750                </Filter> 
    754751                <Filter 
     
    788785                        <File 
    789786                                RelativePath="..\include\ViewCell.h"> 
     787                        </File> 
     788                        <File 
     789                                RelativePath="..\src\X3dParserXerces.h"> 
    790790                        </File> 
    791791                </Filter> 
  • GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp

    r1528 r1545  
    2222 
    2323 
    24 VssRay *RayCaster::CastSingleRay(const SimpleRay &simpleRay, 
    25                                                                  const AxisAlignedBox3 &box) 
     24VssRay *RayCaster::CastRay(const SimpleRay &simpleRay, 
     25                                                   const AxisAlignedBox3 &box, 
     26                                                   const bool castDoubleRay) 
    2627{ 
    2728        VssRayContainer rays; 
    28         CastRay(simpleRay, rays, box, false); 
     29        CastRay(simpleRay, rays, box, castDoubleRay); 
    2930 
    3031        if (!rays.empty()) 
  • GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h

    r1528 r1545  
    4141 
    4242        /** Wrapper for casting single ray. 
     43                @returns ray or NULL if invalid 
    4344        */ 
    44         VssRay *CastSingleRay( 
     45        VssRay *CastRay( 
    4546                const SimpleRay &simpleRay, 
    46             const AxisAlignedBox3 &box 
     47            const AxisAlignedBox3 &box, 
     48                const bool castDoubleRay 
    4749                ); 
    4850 
  • GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp

    r1489 r1545  
    273273 
    274274        for (it = mChildren.begin(); it != it_end; ++ it) 
     275        { 
    275276                delete (*it); 
     277        } 
    276278} 
    277279 
     
    18561858        int pvsSize = 0; 
    18571859 
     1860        ///////////// 
    18581861        //-- compressed pvs 
    18591862 
  • GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h

    r1297 r1545  
    232232 
    233233 
    234         //////////////////////////////////////////// 
     234        //////////////// 
    235235        //-- mailing stuff 
    236236 
     
    255255        static int sMailId; 
    256256        static int sReservedMailboxes; 
    257         //int mMailbox; 
    258          
     257         
     258 
    259259protected: 
    260260 
    261261        /// parent view cell in the view cell hierarchy 
    262262        ViewCellInterior *mParent; 
     263         
    263264        /// the potentially visible objects 
    264265        ObjectPvs mPvs; 
     266         
    265267        /// the volume of this view cell 
    266268        float mVolume; 
    267269         
     270        /// the area of this view cell 
    268271        float mArea; 
     272 
    269273        /// the cost that were paid for merging this view cells from two others. 
    270274        float mMergeCost; 
     275         
    271276        /// if the view cell is valid view space 
    272277        bool mValid; 
     278         
    273279        /// color used for consistent visualization 
    274280        RgbColor mColor; 
     281         
    275282        /// store pvs size, used for evaluation purpose when pvss are stored only in the leaves 
    276283        int mPvsSize; 
  • GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp

    r1418 r1545  
    1717namespace GtpVisibilityPreprocessor { 
    1818 
    19  
     19////////////// 
    2020//-- static members 
    2121 
     
    168168mRoot(NULL),  
    169169mUseAreaForPvs(false), 
    170 mGenerateViewCells(true), 
    171 mTimeStamp(1) 
     170mUsePredefinedViewCells(false), 
     171mTimeStamp(1), 
     172mViewCellsTree(NULL), 
     173 mOutOfBoundsCellPartOfTree(false) 
    172174{ 
    173175        Randomize(); // initialise random generator for heuristics 
    174  
    175         mOutOfBoundsCell = GetOrCreateOutOfBoundsCell(); 
    176  
     176        mOutOfBoundsCell = new BspViewCell(); 
     177 
     178        ///////// 
    177179        //-- termination criteria for autopartition 
     180 
    178181        Environment::GetSingleton()->GetIntValue("BspTree.Termination.maxDepth", mTermMaxDepth); 
    179182        Environment::GetSingleton()->GetIntValue("BspTree.Termination.minPvs", mTermMinPvs); 
     
    184187        Environment::GetSingleton()->GetFloatValue("BspTree.Termination.minAccRayLenght", mTermMinAccRayLength); 
    185188 
     189         
     190        ///////// 
    186191        //-- factors for bsp tree split plane heuristics 
     192 
    187193        Environment::GetSingleton()->GetFloatValue("BspTree.Factor.verticalSplits", mVerticalSplitsFactor); 
    188194        Environment::GetSingleton()->GetFloatValue("BspTree.Factor.largestPolyArea", mLargestPolyAreaFactor); 
     
    196202        Environment::GetSingleton()->GetFloatValue("BspTree.Termination.ct_div_ci", mCtDivCi); 
    197203 
     204 
     205        /////////// 
    198206        //-- termination criteria for axis aligned split 
     207 
    199208        Environment::GetSingleton()->GetFloatValue("BspTree.Termination.AxisAligned.ct_div_ci", mAxisAlignedCtDivCi); 
    200209        Environment::GetSingleton()->GetFloatValue("BspTree.Termination.maxCostRatio", mMaxCostRatio); 
     
    205214        Environment::GetSingleton()->GetIntValue("BspTree.Termination.AxisAligned.minObjects",  
    206215                                                         mTermMinObjectsForAxisAligned); 
     216         
     217         
     218        ////////////// 
    207219        //-- partition criteria 
     220 
    208221        Environment::GetSingleton()->GetIntValue("BspTree.maxPolyCandidates", mMaxPolyCandidates); 
    209222        Environment::GetSingleton()->GetIntValue("BspTree.maxRayCandidates", mMaxRayCandidates); 
     
    219232        mSubdivisionStats.open(subdivisionStatsLog); 
    220233 
     234        /////////////////////7 
     235 
     236        Debug << "BSP options: " << endl; 
    221237    Debug << "BSP max depth: " << mTermMaxDepth << endl; 
    222238        Debug << "BSP min PVS: " << mTermMinPvs << endl; 
     
    401417{ 
    402418        DEL_PTR(mRoot); 
    403  
    404         // TODO must be deleted if used!! 
    405         //if (mGenerateViewCells) DEL_PTR(mOutOfBoundsCell); 
    406 } 
    407  
    408 BspViewCell *BspTree::GetOutOfBoundsCell() 
    409 { 
    410         return mOutOfBoundsCell; 
     419         
     420        if (!mOutOfBoundsCellPartOfTree) 
     421        { 
     422                // out of bounds cell not part of tree => 
     423                // delete manually 
     424                DEL_PTR(mOutOfBoundsCell); 
     425        } 
    411426} 
    412427 
     
    418433 
    419434 
    420 BspViewCell *BspTree::GetOrCreateOutOfBoundsCell() 
    421 { 
    422         if (!mOutOfBoundsCell) 
    423         { 
    424                 mOutOfBoundsCell = new BspViewCell(); 
    425                 mOutOfBoundsCell->SetId(-1); 
    426                 mOutOfBoundsCell->SetValid(false); 
    427         } 
    428  
    429         return mOutOfBoundsCell; 
     435void BspTree::SetViewCellsTree(ViewCellsTree *viewCellsTree) 
     436{ 
     437        mViewCellsTree = viewCellsTree; 
    430438} 
    431439 
     
    433441void BspTree::InsertViewCell(ViewCellLeaf *viewCell) 
    434442{ 
     443        // don't generate new view cell, insert this view cell 
     444        mUsePredefinedViewCells = true; 
    435445        PolygonContainer *polys = new PolygonContainer(); 
    436446 
    437         // don't generate new view cell, insert this one 
    438         mGenerateViewCells = false; 
    439447        // extract polygons that guide the split process 
    440448        mStat.polys += AddMeshToPolygons(viewCell->GetMesh(), *polys, viewCell); 
    441         mBox.Include(viewCell->GetBox()); // add to BSP aabb 
     449        mBbox.Include(viewCell->GetBox()); // add to BSP aabb 
    442450 
    443451        InsertPolygons(polys); 
     
    459467                                                                 new BoundedRayContainer(),  
    460468                                                                 0,  
    461                                                                  mUseAreaForPvs ? mBox.SurfaceArea() : mBox.GetVolume(),  
     469                                                                 mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(),  
    462470                                                                 new BspNodeGeometry())); 
    463471 
     
    472480                        BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode); 
    473481 
     482                        /////////////////// 
    474483                        //-- filter view cell polygons down the tree until a leaf is reached 
    475484                        if (!tData.mPolygons->empty()) 
     
    489498                                 
    490499                                // extract view cells associated with the split polygons 
    491                                 ViewCellLeaf *frontViewCell = GetOrCreateOutOfBoundsCell(); 
    492                                 ViewCellLeaf *backViewCell = GetOrCreateOutOfBoundsCell(); 
     500                                ViewCellLeaf *frontViewCell = mOutOfBoundsCell; 
     501                                ViewCellLeaf *backViewCell = mOutOfBoundsCell; 
    493502                         
    494503                                BspTraversalData frontData(interior->GetFront(),  
     
    498507                                                                                   tData.mRays, 
    499508                                                                                   tData.mPvs, 
    500                                                                                    mUseAreaForPvs ? mBox.SurfaceArea() : mBox.GetVolume(),  
     509                                                                                   mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(),  
    501510                                                                                   new BspNodeGeometry()); 
    502511 
     
    507516                                                                                  tData.mRays, 
    508517                                                                                  tData.mPvs, 
    509                                                                                    mUseAreaForPvs ? mBox.SurfaceArea() : mBox.GetVolume(),  
     518                                                                                   mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(),  
    510519                                                                                  new BspNodeGeometry()); 
    511520 
    512                                 if (!mGenerateViewCells) 
     521                                if (mUsePredefinedViewCells) 
    513522                                { 
    514523                                        ExtractViewCells(frontData, 
     
    558567                } 
    559568                else 
     569                { 
    560570                        DEL_PTR(poly); 
     571                } 
    561572        } 
    562573        return (int)mesh->mFaces.size(); 
     
    572583   
    573584        int polysSize = 0; 
    574  
     585        cout << "here55 " << mBbox << endl; 
     586         
     587cout << "here99"; 
    575588        for (int i = 0; i < limit; ++ i) 
    576         { 
    577                 if (viewCells[i]->GetMesh()) // copy the mesh data to polygons 
    578                 { 
    579                         mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb 
    580                         polysSize += AddMeshToPolygons(viewCells[i]->GetMesh(), polys, viewCells[i]); 
    581                 } 
    582         } 
    583  
     589        {cout << "q"; 
     590        Mesh *mesh = viewCells[i]->GetMesh(); 
     591                if (mesh)  
     592                {cout << "w"; 
     593                        // copy the mesh into polygons and add to BSP tree aabb 
     594                        mBbox.Include(viewCells[i]->GetBox());  
     595                        polysSize += AddMeshToPolygons(mesh, polys, viewCells[i]); 
     596                } 
     597        } 
     598cout << "here155 " << mBbox << endl; 
    584599        return polysSize; 
    585600} 
     
    593608        int limit = (maxObjects > 0) ?  
    594609                Min((int)objects.size(), maxObjects) : (int)objects.size(); 
    595    
     610  cout << "here88"; 
    596611        for (int i = 0; i < limit; ++i) 
    597         { 
     612        {cout << "e"; 
    598613                Intersectable *object = objects[i]; 
    599  
    600614                Mesh *mesh = NULL; 
    601615 
     
    616630                                mesh = new Mesh(); 
    617631                                mi->GetTransformedMesh(*mesh); 
    618                                  
    619632                                break; 
    620633                        } 
     
    624637                } 
    625638                 
    626         if (mesh) // copy the mesh data to polygons 
    627                 { 
    628                         if (addToBbox) 
    629                                 mBox.Include(object->GetBox()); // add to BSP tree aabb 
    630                                  
    631                         AddMeshToPolygons(mesh, polys, mOutOfBoundsCell); 
    632  
    633                         // cleanup 
    634                         if (object->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE) 
    635                                 DEL_PTR(mesh); 
     639        if (!mesh) continue; 
     640                // copy the mesh data to polygons 
     641                if (addToBbox) 
     642                {cout << "b"; 
     643                        mBbox.Include(object->GetBox()); // add to BSP tree aabb 
     644                } 
     645 
     646                AddMeshToPolygons(mesh, polys, mOutOfBoundsCell); 
     647 
     648                // cleanup 
     649                if (object->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE) 
     650                { 
     651                        DEL_PTR(mesh); 
    636652                }        
    637653        } 
     
    640656} 
    641657 
    642  
     658         
    643659void BspTree::Construct(const ViewCellContainer &viewCells) 
    644660{ 
     661        cout << "here5500000000" << endl; 
     662        // construct hierarchy over the given view cells 
     663        mUsePredefinedViewCells = true; 
     664 
    645665        mStat.nodes = 1; 
    646         mBox.Initialize();      // initialise bsp tree bounding box 
     666        mBbox.Initialize(); // initialise bsp tree bounding box 
    647667 
    648668        // copy view cell meshes into one big polygon soup 
     
    650670        mStat.polys = AddToPolygonSoup(viewCells, *polys); 
    651671 
    652         // view cells are given 
    653         mGenerateViewCells = false; 
     672        Exporter *expo = Exporter::GetExporter("dummy2.wrl"); 
     673        expo->ExportPolygons(*polys); 
     674        delete expo; 
    654675        // construct tree from the view cell polygons 
    655676        Construct(polys, new BoundedRayContainer()); 
     
    659680void BspTree::Construct(const ObjectContainer &objects) 
    660681{ 
     682        // generate new view cells for this type 
     683        mUsePredefinedViewCells = false; 
     684 
    661685        mStat.nodes = 1; 
    662         mBox.Initialize();      // initialise bsp tree bounding box 
     686        mBbox.Initialize();     // initialise bsp tree bounding box 
    663687         
    664688        PolygonContainer *polys = new PolygonContainer(); 
    665689 
    666         mGenerateViewCells = true; 
    667690        // copy mesh instance polygons into one big polygon soup 
    668691        mStat.polys = AddToPolygonSoup(objects, *polys); 
     
    677700        // preprocess: throw out polygons coincident to the view space box (not needed) 
    678701        PolygonContainer boxPolys; 
    679         mBox.ExtractPolys(boxPolys); 
     702        mBbox.ExtractPolys(boxPolys); 
    680703        vector<Plane3> boxPlanes; 
    681704 
     
    722745 
    723746 
    724  
    725747void BspTree::Construct(const RayContainer &sampleRays, 
    726748                                                AxisAlignedBox3 *forcedBoundingBox) 
    727749{ 
     750        // generate new view cells for this contruction type 
     751        mUsePredefinedViewCells = false; 
     752 
    728753    mStat.nodes = 1; 
    729         mBox.Initialize();      // initialise BSP tree bounding box 
    730          
     754 
    731755        if (forcedBoundingBox) 
    732                 mBox = *forcedBoundingBox; 
     756        { 
     757                mBbox = *forcedBoundingBox; 
     758        } 
     759        else 
     760        { 
     761                mBbox.Initialize(); // initialise BSP tree bounding box  
     762        } 
    733763 
    734764        PolygonContainer *polys = new PolygonContainer(); 
     
    737767        RayContainer::const_iterator rit, rit_end = sampleRays.end(); 
    738768 
    739         // generate view cells 
    740         mGenerateViewCells = true; 
    741  
    742769        long startTime = GetTime(); 
    743  
    744770        Debug << "**** Extracting polygons from rays ****\n"; 
    745771 
    746772        std::map<Face *, Polygon3 *> facePolyMap; 
    747773 
    748         //-- extract polygons intersected by the rays 
    749774        for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 
    750775        { 
     776                ////////////// 
     777                //-- extract polygons intersected by the rays 
    751778                Ray *ray = *rit; 
    752779         
     
    803830        // compute bounding box 
    804831        if (!forcedBoundingBox) 
    805                 mBox.Include(*polys); 
    806  
     832        { 
     833                mBbox.Include(*polys); 
     834        } 
     835 
     836        //////////// 
    807837        //-- store rays 
    808838        for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 
     
    812842 
    813843                float minT, maxT; 
    814                 if (mBox.GetRaySegment(*ray, minT, maxT)) 
     844                if (mBbox.GetRaySegment(*ray, minT, maxT)) 
    815845                        rays->push_back(new BoundedRay(ray, minT, maxT)); 
    816846        } 
     
    833863                                                AxisAlignedBox3 *forcedBoundingBox) 
    834864{ 
     865        // generate new view cells for this construction type 
     866        mUsePredefinedViewCells = false; 
     867 
    835868    mStat.nodes = 1; 
    836         mBox.Initialize();      // initialise BSP tree bounding box 
     869        mBbox.Initialize();     // initialise BSP tree bounding box 
    837870         
    838871        if (forcedBoundingBox) 
    839                 mBox = *forcedBoundingBox; 
     872        { 
     873                mBbox = *forcedBoundingBox; 
     874        } 
    840875 
    841876        BoundedRayContainer *rays = new BoundedRayContainer(); 
    842877        PolygonContainer *polys = new PolygonContainer(); 
    843878         
    844         mGenerateViewCells = true; 
    845  
    846879        // copy mesh instance polygons into one big polygon soup 
    847880        mStat.polys = AddToPolygonSoup(objects, *polys, 0, !forcedBoundingBox); 
    848881 
    849  
     882        /////// 
     883        //-- store rays 
    850884        RayContainer::const_iterator rit, rit_end = sampleRays.end(); 
    851  
    852         //-- store rays 
     885         
    853886        for (rit = sampleRays.begin(); rit != rit_end; ++ rit) 
    854887        { 
     
    857890 
    858891                float minT, maxT; 
    859                 if (mBox.GetRaySegment(*ray, minT, maxT)) 
     892                if (mBbox.GetRaySegment(*ray, minT, maxT)) 
    860893                        rays->push_back(new BoundedRay(ray, minT, maxT)); 
    861894        } 
     
    870903{ 
    871904        BspTraversalStack tStack; 
    872  
    873905        mRoot = new BspLeaf(); 
    874906 
     
    880912                                                   polys,  
    881913                                                   0,  
    882                                                    GetOrCreateOutOfBoundsCell(),  
     914                                                   mOutOfBoundsCell,  
    883915                                                   rays,  
    884916                                                   ComputePvsSize(*rays),  
     
    886918                                                   geom); 
    887919 
    888         mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume(); 
     920        mTotalCost = tData.mPvs * tData.mProbability / mBbox.GetVolume(); 
    889921        mTotalPvsSize = tData.mPvs; 
    890922         
     
    950982BspNode *BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData) 
    951983{ 
    952         //-- terminate traversal   
     984        //////// 
     985        //-- terminate traversal 
     986 
    953987        if (TerminationCriteriaMet(tData))               
    954988        { 
     
    957991                BspViewCell *viewCell; 
    958992 
    959                 // generate new view cell for each leaf 
    960                 if (mGenerateViewCells) 
    961                 { 
     993                if (!mUsePredefinedViewCells) 
     994                {       // generate new view cell for each leaf 
    962995                        viewCell = new BspViewCell(); 
    963996                } 
    964997                else 
    965998                { 
    966                         // add view cell to leaf 
     999                        // add predefined view cell to leaf 
    9671000                        viewCell = dynamic_cast<BspViewCell *>(tData.mViewCell); 
     1001                 
     1002                        /// out of bounds cell can be handled as any other cell 
     1003                        if (viewCell == mOutOfBoundsCell) 
     1004                                mOutOfBoundsCellPartOfTree = true; 
    9681005                } 
    9691006 
     
    9791016                        viewCell->SetVolume(probability); 
    9801017 
    981                 //-- add pvs 
     1018                 
     1019                /////////// 
     1020                //-- add pvs contribution of rays 
     1021 
    9821022                if (viewCell != mOutOfBoundsCell) 
    9831023                { 
     
    9891029                } 
    9901030 
    991                 if (1) 
    992                         EvaluateLeafStats(tData); 
    993                  
     1031                if (1)EvaluateLeafStats(tData); 
     1032                 
     1033 
     1034                //////// 
    9941035                //-- clean up 
    995                  
     1036 
    9961037                // discard polygons 
    9971038                CLEAR_CONTAINER(*tData.mPolygons); 
     
    10061047        } 
    10071048 
     1049        /////////// 
    10081050        //-- continue subdivision 
     1051 
    10091052        PolygonContainer coincident; 
    10101053         
     
    10191062                SubdivideNode(tData, tFrontData, tBackData, coincident); 
    10201063 
    1021  
    10221064        if (1) 
    10231065        { 
     
    10281070                float cBack = (float)tBackData.mPvs * tBackData.mProbability; 
    10291071 
    1030                 float costDecr = (cFront + cBack - cData) / mBox.GetVolume(); 
     1072                float costDecr = (cFront + cBack - cData) / mBbox.GetVolume(); 
    10311073                 
    10321074                mTotalCost += costDecr; 
     
    10401082        } 
    10411083 
    1042         // extract view cells from coincident polygons according to plane normal 
    1043     // only if front or back polygons are empty 
    1044         if (!mGenerateViewCells) 
     1084        // extract view cells from coincident polygons  
     1085        // with respect to the orientation of their normal 
     1086    // note: if front or back polygons are empty,  
     1087        // we get the valid in - out classification for the view cell 
     1088 
     1089        if (mUsePredefinedViewCells) 
    10451090        { 
    10461091                ExtractViewCells(tFrontData, 
     
    10571102        tStack.push(tBackData); 
    10581103 
    1059         // cleanup 
     1104        //////// 
     1105        //-- cleanup 
     1106 
    10601107        DEL_PTR(tData.mNode); 
    1061  
    10621108        DEL_PTR(tData.mPolygons); 
    10631109        DEL_PTR(tData.mRays); 
     
    10801126                coincident.begin(), it_end = coincident.end(); 
    10811127 
     1128        ////////// 
    10821129        //-- find first view cells in front and back leafs 
     1130 
    10831131        for (; !(foundFront && foundBack) && (it != it_end); ++ it) 
    10841132        { 
     
    11081156        // select subdivision plane 
    11091157        BspInterior *interior = new BspInterior(SelectPlane(leaf, tData));  
    1110          
    11111158         
    11121159#ifdef _DEBUG 
     
    11351182                                                                           *backData.mGeometry,  
    11361183                                                                           interior->mPlane, 
    1137                                                                            mBox, 
     1184                                                                           mBbox, 
    11381185                                                                           //0.000000000001); 
    11391186                                                                           mEpsilon); 
     
    12741321        const float ratio = newCost / oldCost; 
    12751322 
    1276  
    1277 #if 0 
     1323#ifdef _DEBUG 
    12781324  Debug << "====================" << endl; 
    12791325  Debug << "costRatio=" << ratio << " pos=" << position<<" t=" << (position - minBox)/(maxBox - minBox) 
     
    14301476        return Plane3(pt[0], pt[1], pt[2]); 
    14311477} 
     1478 
    14321479 
    14331480Plane3 BspTree::ChooseCandidatePlane3(const BoundedRayContainer &rays) const 
     
    16971744                                                           geomBack,  
    16981745                                                           candidatePlane, 
    1699                                                            mBox, 
     1746                                                           mBbox, 
    17001747                                                           mEpsilon); 
    17011748 
     
    18561903        if (mSplitPlaneStrategy & VERTICAL_AXIS) 
    18571904        { 
    1858                 Vector3 tinyAxis(0,0,0); tinyAxis[mBox.Size().TinyAxis()] = 1.0f; 
     1905                Vector3 tinyAxis(0,0,0); tinyAxis[mBbox.Size().TinyAxis()] = 1.0f; 
    18591906                // we put a penalty on the dot product between the "tiny" vertical axis 
    18601907                // and the split plane axis 
     
    19151962AxisAlignedBox3 BspTree::GetBoundingBox() const 
    19161963{ 
    1917         return mBox; 
     1964        return mBbox; 
    19181965} 
    19191966 
     
    19722019        float maxt, mint; 
    19732020 
    1974         if (!mBox.GetRaySegment(ray, mint, maxt)) 
     2021        if (!mBbox.GetRaySegment(ray, mint, maxt)) 
    19752022                return 0; 
    19762023 
     
    21942241                } 
    21952242        } 
     2243 
     2244        // also add out of bounds cell 
     2245        if (0 && !mOutOfBoundsCell->Mailed()) 
     2246        { 
     2247                mOutOfBoundsCell->Mail(); 
     2248                viewCells.push_back(mOutOfBoundsCell); 
     2249        } 
     2250 
     2251        cout << "here555 " << viewCells.size() << endl; 
     2252 
    21962253} 
    21972254 
     
    22472304                case Ray::COINCIDENT: // TODO: should really discard ray? 
    22482305                        frontRays.push_back(bRay); 
    2249                         //DEL_PTR(bRay); 
    22502306                        break; 
    22512307                case Ray::BACK: 
     
    23222378                                                                BspNodeGeometry &vcGeom) const 
    23232379{ 
     2380        // if false, cannot construct geometry for interior leaf 
     2381        if (!mViewCellsTree) 
     2382                return; 
     2383 
    23242384        ViewCellContainer leaves; 
    23252385        mViewCellsTree->CollectLeaves(vc, leaves); 
     
    23642424 
    23652425                for (int j = 0; j < 4; ++ j) 
    2366                         vertices.push_back(mBox.GetFace(i).mVertices[j]); 
     2426                        vertices.push_back(mBbox.GetFace(i).mVertices[j]); 
    23672427 
    23682428                Polygon3 *poly = new Polygon3(vertices); 
     
    25332593                                                                *bGeom, 
    25342594                                                                interior->GetPlane(), 
    2535                                                                 mBox, 
     2595                                                                mBbox, 
    25362596                                                                //0.0000001f); 
    25372597                                                                mEpsilon); 
     
    28072867        Debug << "leaves in queue: " << numLeaves << endl; 
    28082868 
    2809  
     2869#if 0 
     2870        ////////// 
    28102871        //-- collect the leaves which haven't been found by ray casting 
    2811 #if 0 
    28122872        cout << "finding additional merge candidates using geometry" << endl; 
    28132873        vector<BspLeaf *> leaves; 
     
    28222882 
    28232883 
    2824  
    28252884/***************************************************************/ 
    28262885/*              BspNodeGeometry Implementation                 */ 
     
    30503109bool BspNodeGeometry::Valid() const 
    30513110{ 
     3111        // geometry is degenerated 
    30523112        if (mPolys.size() < 4) 
    30533113                return false; 
     
    32943354                int id = -1; 
    32953355                if (leaf->GetViewCell() != mOutOfBoundsCell) 
     3356                { 
    32963357                        id = leaf->GetViewCell()->GetId(); 
     3358                } 
    32973359 
    32983360                stream << "<Leaf viewCellId=\"" << id << "\" />" << endl; 
  • GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.h

    r1449 r1545  
    2727{ 
    2828public: 
     29        /** Default constructor. 
     30        */ 
    2931        BspNodeGeometry() 
    3032        {};   
     
    343345{ 
    344346        friend class BspTree; 
     347 
    345348public: 
    346349        /** Standard contructor taking split plane as argument. 
     
    438441        } 
    439442         
    440  
    441443        /// Rays piercing this leaf. 
    442444        VssRayContainer mVssRays; 
     
    444446        /// leaf pvs 
    445447        ObjectPvs *mPvs; 
    446  
     448         
    447449        /// Probability that the view point lies in this leaf 
    448450        float mProbability; 
     
    607609        BspNode *GetRoot() const; 
    608610 
    609          
    610         //bool Export(const string filename); 
    611  
    612611        /** Collects the leaf view cells of the tree 
    613612                @param viewCells returns the view cells  
     
    629628   
    630629        /// bsp tree construction types 
    631         enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES}; 
     630        //enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES}; 
    632631 
    633632        /** Returns statistics. 
     
    678677        bool Export(ofstream &stream); 
    679678 
    680  
    681         /** Returns view cell corresponding to  
    682                 the invalid view space. If it does not exist, it is created. 
    683         */ 
    684         BspViewCell *GetOutOfBoundsCell(); 
    685  
    686         ViewCellsTree *mViewCellsTree; 
    687          
     679        /** Pointer to the view cells tree. 
     680        */ 
     681        void SetViewCellsTree(ViewCellsTree *vct); 
     682                 
    688683protected: 
    689  
    690         // -------------------------------------------------------------- 
    691         // For sorting objects 
    692         // -------------------------------------------------------------- 
    693         struct SortableEntry 
    694         { 
    695                 enum {POLY_MIN, POLY_MAX}; 
    696      
    697                 int type; 
    698                 float value; 
    699                 Polygon3 *poly; 
    700                 SortableEntry() {} 
    701                 SortableEntry(const int t, const float v, Polygon3 *poly):  
    702                 type(t), value(v), poly(poly) {} 
    703                  
    704                 bool operator<(const SortableEntry &b) const  
    705                 { 
    706                         return value < b.value; 
    707                 }   
    708         }; 
    709  
    710         void ExportNode(BspNode *node, ofstream &stream); 
    711  
    712         /** Evaluates tree stats in the BSP tree leafs. 
    713         */ 
    714         void EvaluateLeafStats(const BspTraversalData &data); 
    715  
    716         /** Subdivides node with respect to the traversal data. 
    717             @param tStack current traversal stack 
    718                 @param tData traversal data also holding node to be subdivided 
    719                 @returns new root of the subtree 
    720         */ 
    721         BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData); 
    722  
    723         /** Constructs the tree from the given list of polygons and rays. 
    724                 @param polys stores set of polygons on which subdivision may be based 
    725                 @param rays storesset of rays on which subdivision may be based 
    726         */ 
    727         void Construct(PolygonContainer *polys, BoundedRayContainer *rays); 
    728  
    729         /** Selects the best possible splitting plane.  
    730                 @param leaf the leaf to be split 
    731                 @param polys the polygon list on which the split decition is based 
    732                 @param rays ray container on which selection may be based 
    733                 @note the polygons can be reordered in the process 
    734                 @returns the split plane 
    735         */ 
    736         Plane3 SelectPlane(BspLeaf *leaf,  
    737                                            BspTraversalData &data); 
    738  
    739         /** Evaluates the contribution of the candidate split plane. 
    740                  
    741                 @param candidatePlane the candidate split plane 
    742                 @param polys the polygons the split can be based on 
    743                 @param rays the rays the split can be based on 
    744  
    745                 @returns the cost of the candidate split plane 
    746         */ 
    747         float SplitPlaneCost(const Plane3 &candidatePlane, 
    748                                                  BspTraversalData &data) const; 
    749  
    750         /** Strategies where the effect of the split plane is tested 
    751             on all input rays. 
    752                 @returns the cost of the candidate split plane 
    753         */ 
    754         float SplitPlaneCost(const Plane3 &candidatePlane, 
    755                                                  const PolygonContainer &polys) const; 
    756  
    757         /** Strategies where the effect of the split plane is tested 
    758             on all input rays. 
    759  
    760                 @returns the cost of the candidate split plane 
    761         */ 
    762         float SplitPlaneCost(const Plane3 &candidatePlane,  
    763                                                  const BoundedRayContainer &rays, 
    764                                                  const int pvs, 
    765                                                  const float probability, 
    766                                                  const BspNodeGeometry &cell) const; 
    767  
    768         /** Filters next view cell down the tree and inserts it into the appropriate leaves 
    769                 (i.e., possibly more than one leaf). 
    770         */ 
    771         void InsertViewCell(ViewCellLeaf *viewCell); 
    772         /** Inserts polygons down the tree. The polygons are filtered until a leaf is reached, 
    773                 then further subdivided. 
    774         */ 
    775         void InsertPolygons(PolygonContainer *polys); 
    776  
    777         /** Subdivide leaf. 
    778                 @param leaf the leaf to be subdivided 
    779                  
    780                 @param polys the polygons to be split 
    781                 @param frontPolys returns the polygons in front of the split plane 
    782                 @param backPolys returns the polygons in the back of the split plane 
    783                  
    784                 @param rays the polygons to be filtered 
    785                 @param frontRays returns the polygons in front of the split plane 
    786                 @param backRays returns the polygons in the back of the split plane 
    787  
    788                 @returns the root of the subdivision 
    789         */ 
    790  
    791         BspInterior *SubdivideNode(BspTraversalData &tData, 
    792                                                            BspTraversalData &frontData, 
    793                                                            BspTraversalData &backData, 
    794                                                            PolygonContainer &coincident); 
    795  
    796         /** Filters polygons down the tree. 
    797                 @param node the current BSP node 
    798                 @param polys the polygons to be filtered 
    799                 @param frontPolys returns the polygons in front of the split plane 
    800                 @param backPolys returns the polygons in the back of the split plane 
    801         */ 
    802         void FilterPolygons(BspInterior *node,  
    803                                                 PolygonContainer *polys,  
    804                                                 PolygonContainer *frontPolys,  
    805                                                 PolygonContainer *backPolys); 
    806  
    807         /** Take 3 ray endpoints, where two are minimum and one a maximum 
    808                 point or the other way round. 
    809         */ 
    810         Plane3 ChooseCandidatePlane(const BoundedRayContainer &rays) const; 
    811  
    812         /** Take plane normal as plane normal and the midpoint of the ray. 
    813                 PROBLEM: does not resemble any point where visibility is likely to change 
    814         */ 
    815         Plane3 ChooseCandidatePlane2(const BoundedRayContainer &rays) const; 
    816  
    817         /** Fit the plane between the two lines so that the plane has equal shortest  
    818                 distance to both lines. 
    819         */ 
    820         Plane3 ChooseCandidatePlane3(const BoundedRayContainer &rays) const; 
    821  
    822         /** Selects the split plane in order to construct a tree with 
    823                 certain characteristics (e.g., balanced tree, least splits,  
    824                 2.5d aligned) 
    825                 @param polygons container of polygons 
    826                 @param rays bundle of rays on which the split can be based 
    827         */ 
    828         Plane3 SelectPlaneHeuristics(BspLeaf *leaf, 
    829                                                                  BspTraversalData &data); 
    830  
    831         /** Extracts the meshes of the objects and adds them to polygons.  
    832                 Adds object aabb to the aabb of the tree. 
    833                 @param maxPolys the maximal number of objects to be stored as polygons 
    834                 @returns the number of polygons 
    835         */ 
    836         int AddToPolygonSoup(const ObjectContainer &objects,  
    837                                                  PolygonContainer &polys,  
    838                                                  int maxObjects = 0, 
    839                                                  bool addToBbox = true); 
    840  
    841         /** Extracts the meshes of the view cells and and adds them to polygons. 
    842                 Adds view cell aabb to the aabb of the tree. 
    843                 @param maxPolys the maximal number of objects to be stored as polygons 
    844                 @returns the number of polygons 
    845         */ 
    846         int AddToPolygonSoup(const ViewCellContainer &viewCells,  
    847                                                  PolygonContainer &polys,  
    848                                                  int maxObjects = 0); 
    849  
    850         /** Extract polygons of this mesh and add to polygon container. 
    851                 @param mesh the mesh that drives the polygon construction 
    852                 @param parent the parent intersectable this polygon is constructed from 
    853                 @returns number of polygons 
    854         */ 
    855         int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent); 
    856  
    857         /** Helper function which extracts a view cell on the front and the back 
    858                 of the split plane. 
    859                 @param backViewCell returns view cell on the back of the split plane 
    860                 @param frontViewCell returns a view cell on the front of the split plane 
    861                 @param coincident container of polygons coincident to the split plane 
    862                 @param splitPlane the split plane which decides about back and front 
    863                 @param extractBack if a back view cell is extracted 
    864                 @param extractFront if a front view cell is extracted 
    865         */ 
    866         void ExtractViewCells(BspTraversalData &frontData, 
    867                                                   BspTraversalData &backData,  
    868                                                   const PolygonContainer &coincident, 
    869                                                   const Plane3 &splitPlane) const; 
    870          
    871         /** Computes best cost ratio for the suface area heuristics for axis aligned 
    872                 splits. This heuristics minimizes the cost for ray traversal. 
    873                 @param polys the polygons guiding the ratio computation 
    874                 @param box the bounding box of the leaf 
    875                 @param axis the current split axis 
    876                 @param position returns the split position 
    877                 @param objectsBack the number of objects in the back of the split plane 
    878                 @param objectsFront the number of objects in the front of the split plane 
    879         */ 
    880         float BestCostRatio(const PolygonContainer &polys, 
    881                                                 const AxisAlignedBox3 &box, 
    882                                                 const int axis, 
    883                                                 float &position, 
    884                                                 int &objectsBack, 
    885                                                 int &objectsFront) const; 
    886          
    887         /** Sorts split candidates for cost heuristics using axis aligned splits. 
    888                 @param polys the input for choosing split candidates 
    889                 @param axis the current split axis 
    890                 @param splitCandidates returns sorted list of split candidates 
    891         */ 
    892         void SortSubdivisionCandidates(const PolygonContainer &polys,  
    893                                                          const int axis,  
    894                                                          vector<SortableEntry> &splitCandidates) const; 
    895  
    896         /** Selects an axis aligned split plane. 
    897                 Returns true if split is valied 
    898         */ 
    899         bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const; 
    900  
    901         /** Subdivides the rays into front and back rays according to the split plane. 
    902                  
    903                 @param plane the split plane 
    904                 @param rays contains the rays to be split. The rays are  
    905                            distributed into front and back rays. 
    906                 @param frontRays returns rays on the front side of the plane 
    907                 @param backRays returns rays on the back side of the plane 
    908                  
    909                 @returns the number of splits 
    910         */ 
    911         int SplitRays(const Plane3 &plane, 
    912                                   BoundedRayContainer &rays,  
    913                               BoundedRayContainer &frontRays,  
    914                                   BoundedRayContainer &backRays); 
    915  
    916  
    917         /** Extracts the split planes representing the space bounded by node n. 
    918         */ 
    919         void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const; 
    920  
    921         /** Adds the object to the pvs of the front and back leaf with a given classification. 
    922  
    923                 @param obj the object to be added 
    924                 @param cf the ray classification regarding the split plane 
    925                 @param frontPvs returns the PVS of the front partition 
    926                 @param backPvs returns the PVS of the back partition 
    927          
    928         */ 
    929         void AddObjToPvs(Intersectable *obj, const int cf, int &frontPvs, int &backPvs) const; 
    930  
    931         /** Computes PVS size induced by the rays. 
    932         */ 
    933         int ComputePvsSize(const BoundedRayContainer &rays) const; 
    934  
    935         /** Returns true if tree can be terminated. 
    936         */ 
    937         inline bool TerminationCriteriaMet(const BspTraversalData &data) const; 
    938  
    939         /** Computes accumulated ray lenght of this rays. 
    940         */ 
    941         float AccumulatedRayLength(BoundedRayContainer &rays) const; 
    942  
    943         /** Splits polygons with respect to the split plane. 
    944                 @param polys the polygons to be split. the polygons are consumed and 
    945                            distributed to the containers frontPolys, backPolys, coincident. 
    946                 @param frontPolys returns the polygons in the front of the split plane 
    947                 @param backPolys returns the polygons in the back of the split plane 
    948                 @param coincident returns the polygons coincident to the split plane 
    949  
    950                 @returns the number of splits    
    951         */ 
    952         int SplitPolygons(const Plane3 &plane, 
    953                                           PolygonContainer &polys,  
    954                                           PolygonContainer &frontPolys,  
    955                                           PolygonContainer &backPolys,  
    956                                           PolygonContainer &coincident) const; 
    957  
    958         /** Adds ray sample contributions to the PVS. 
    959                 @param sampleContributions the number contributions of the samples 
    960                 @param contributingSampels the number of contributing rays 
    961                  
    962         */ 
    963         void AddToPvs(BspLeaf *leaf, 
    964                                   const BoundedRayContainer &rays,  
    965                                   int &sampleContributions,      
    966                                   int &contributingSamples); 
    967  
    968         /** Preprocesses polygons and throws out all polygons which  
    969                 are coincident to the view space box faces: 
    970                 These polygons can can be problematic for bsp because they create 
    971                 bad view cells. 
    972         */ 
    973         void PreprocessPolygons(PolygonContainer &polys); 
    974  
    975         /** Returns view cell corresponding to the invalid view space.  
    976                 If it does not exist, it is created. 
    977         */ 
    978         BspViewCell *GetOrCreateOutOfBoundsCell(); 
    979  
    980         /// Pointer to the root of the tree. 
    981         BspNode *mRoot; 
    982  
    983         /// Stores statistics during traversal. 
    984         BspTreeStatistics mStat; 
    985684 
    986685        /// Strategies for choosing next split plane. 
     
    999698                }; 
    1000699 
     700        /**  
     701                For sorting polygons 
     702        */ 
     703        struct SortableEntry 
     704        { 
     705                enum {POLY_MIN, POLY_MAX}; 
     706     
     707                int type; 
     708                float value; 
     709                Polygon3 *poly; 
     710                SortableEntry() {} 
     711                SortableEntry(const int t, const float v, Polygon3 *poly):  
     712                type(t), value(v), poly(poly) {} 
     713                 
     714                bool operator<(const SortableEntry &b) const  
     715                { 
     716                        return value < b.value; 
     717                }   
     718        }; 
     719 
     720        void ExportNode(BspNode *node, ofstream &stream); 
     721 
     722        /** Evaluates tree stats in the BSP tree leafs. 
     723        */ 
     724        void EvaluateLeafStats(const BspTraversalData &data); 
     725 
     726        /** Subdivides node with respect to the traversal data. 
     727            @param tStack current traversal stack 
     728                @param tData traversal data also holding node to be subdivided 
     729                @returns new root of the subtree 
     730        */ 
     731        BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData); 
     732 
     733        /** Constructs the tree from the given list of polygons and rays. 
     734                @param polys stores set of polygons on which subdivision may be based 
     735                @param rays storesset of rays on which subdivision may be based 
     736        */ 
     737        void Construct(PolygonContainer *polys, BoundedRayContainer *rays); 
     738 
     739        /** Selects the best possible splitting plane.  
     740                @param leaf the leaf to be split 
     741                @param polys the polygon list on which the split decition is based 
     742                @param rays ray container on which selection may be based 
     743                @note the polygons can be reordered in the process 
     744                @returns the split plane 
     745        */ 
     746        Plane3 SelectPlane(BspLeaf *leaf,  
     747                                           BspTraversalData &data); 
     748 
     749        /** Evaluates the contribution of the candidate split plane. 
     750                 
     751                @param candidatePlane the candidate split plane 
     752                @param polys the polygons the split can be based on 
     753                @param rays the rays the split can be based on 
     754 
     755                @returns the cost of the candidate split plane 
     756        */ 
     757        float SplitPlaneCost(const Plane3 &candidatePlane, 
     758                                                 BspTraversalData &data) const; 
     759 
     760        /** Strategies where the effect of the split plane is tested 
     761            on all input rays. 
     762                @returns the cost of the candidate split plane 
     763        */ 
     764        float SplitPlaneCost(const Plane3 &candidatePlane, 
     765                                                 const PolygonContainer &polys) const; 
     766 
     767        /** Strategies where the effect of the split plane is tested 
     768            on all input rays. 
     769 
     770                @returns the cost of the candidate split plane 
     771        */ 
     772        float SplitPlaneCost(const Plane3 &candidatePlane,  
     773                                                 const BoundedRayContainer &rays, 
     774                                                 const int pvs, 
     775                                                 const float probability, 
     776                                                 const BspNodeGeometry &cell) const; 
     777 
     778        /** Filters next view cell down the tree and inserts it into the appropriate leaves 
     779                (i.e., possibly more than one leaf). 
     780        */ 
     781        void InsertViewCell(ViewCellLeaf *viewCell); 
     782        /** Inserts polygons down the tree. The polygons are filtered until a leaf is reached, 
     783                then further subdivided. 
     784        */ 
     785        void InsertPolygons(PolygonContainer *polys); 
     786 
     787        /** Subdivide leaf. 
     788                @param leaf the leaf to be subdivided 
     789                 
     790                @param polys the polygons to be split 
     791                @param frontPolys returns the polygons in front of the split plane 
     792                @param backPolys returns the polygons in the back of the split plane 
     793                 
     794                @param rays the polygons to be filtered 
     795                @param frontRays returns the polygons in front of the split plane 
     796                @param backRays returns the polygons in the back of the split plane 
     797 
     798                @returns the root of the subdivision 
     799        */ 
     800 
     801        BspInterior *SubdivideNode(BspTraversalData &tData, 
     802                                                           BspTraversalData &frontData, 
     803                                                           BspTraversalData &backData, 
     804                                                           PolygonContainer &coincident); 
     805 
     806        /** Filters polygons down the tree. 
     807                @param node the current BSP node 
     808                @param polys the polygons to be filtered 
     809                @param frontPolys returns the polygons in front of the split plane 
     810                @param backPolys returns the polygons in the back of the split plane 
     811        */ 
     812        void FilterPolygons(BspInterior *node,  
     813                                                PolygonContainer *polys,  
     814                                                PolygonContainer *frontPolys,  
     815                                                PolygonContainer *backPolys); 
     816 
     817        /** Take 3 ray endpoints, where two are minimum and one a maximum 
     818                point or the other way round. 
     819        */ 
     820        Plane3 ChooseCandidatePlane(const BoundedRayContainer &rays) const; 
     821 
     822        /** Take plane normal as plane normal and the midpoint of the ray. 
     823                PROBLEM: does not resemble any point where visibility is likely to change 
     824        */ 
     825        Plane3 ChooseCandidatePlane2(const BoundedRayContainer &rays) const; 
     826 
     827        /** Fit the plane between the two lines so that the plane has equal shortest  
     828                distance to both lines. 
     829        */ 
     830        Plane3 ChooseCandidatePlane3(const BoundedRayContainer &rays) const; 
     831 
     832        /** Selects the split plane in order to construct a tree with 
     833                certain characteristics (e.g., balanced tree, least splits,  
     834                2.5d aligned) 
     835                @param polygons container of polygons 
     836                @param rays bundle of rays on which the split can be based 
     837        */ 
     838        Plane3 SelectPlaneHeuristics(BspLeaf *leaf, 
     839                                                                 BspTraversalData &data); 
     840 
     841        /** Extracts the meshes of the objects and adds them to polygons.  
     842                Adds object aabb to the aabb of the tree. 
     843                @param maxPolys the maximal number of objects to be stored as polygons 
     844                @returns the number of polygons 
     845        */ 
     846        int AddToPolygonSoup(const ObjectContainer &objects,  
     847                                                 PolygonContainer &polys,  
     848                                                 int maxObjects = 0, 
     849                                                 bool addToBbox = true); 
     850 
     851        /** Extracts the meshes of the view cells and and adds them to polygons. 
     852                Adds view cell aabb to the aabb of the tree. 
     853                @param maxPolys the maximal number of objects to be stored as polygons 
     854                @returns the number of polygons 
     855        */ 
     856        int AddToPolygonSoup(const ViewCellContainer &viewCells,  
     857                                                 PolygonContainer &polys,  
     858                                                 int maxObjects = 0); 
     859 
     860        /** Extract polygons of this mesh and add to polygon container. 
     861                @param mesh the mesh that drives the polygon construction 
     862                @param parent the parent intersectable this polygon is constructed from 
     863                @returns number of polygons 
     864        */ 
     865        int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent); 
     866 
     867        /** Helper function which extracts a view cell on the front and the back 
     868                of the split plane. 
     869                @param backViewCell returns view cell on the back of the split plane 
     870                @param frontViewCell returns a view cell on the front of the split plane 
     871                @param coincident container of polygons coincident to the split plane 
     872                @param splitPlane the split plane which decides about back and front 
     873                @param extractBack if a back view cell is extracted 
     874                @param extractFront if a front view cell is extracted 
     875        */ 
     876        void ExtractViewCells(BspTraversalData &frontData, 
     877                                                  BspTraversalData &backData,  
     878                                                  const PolygonContainer &coincident, 
     879                                                  const Plane3 &splitPlane) const; 
     880         
     881        /** Computes best cost ratio for the suface area heuristics for axis aligned 
     882                splits. This heuristics minimizes the cost for ray traversal. 
     883                @param polys the polygons guiding the ratio computation 
     884                @param box the bounding box of the leaf 
     885                @param axis the current split axis 
     886                @param position returns the split position 
     887                @param objectsBack the number of objects in the back of the split plane 
     888                @param objectsFront the number of objects in the front of the split plane 
     889        */ 
     890        float BestCostRatio(const PolygonContainer &polys, 
     891                                                const AxisAlignedBox3 &box, 
     892                                                const int axis, 
     893                                                float &position, 
     894                                                int &objectsBack, 
     895                                                int &objectsFront) const; 
     896         
     897        /** Sorts split candidates for cost heuristics using axis aligned splits. 
     898                @param polys the input for choosing split candidates 
     899                @param axis the current split axis 
     900                @param splitCandidates returns sorted list of split candidates 
     901        */ 
     902        void SortSubdivisionCandidates(const PolygonContainer &polys,  
     903                                                         const int axis,  
     904                                                         vector<SortableEntry> &splitCandidates) const; 
     905 
     906        /** Selects an axis aligned split plane. 
     907                Returns true if split is valied 
     908        */ 
     909        bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const; 
     910 
     911        /** Subdivides the rays into front and back rays according to the split plane. 
     912                 
     913                @param plane the split plane 
     914                @param rays contains the rays to be split. The rays are  
     915                           distributed into front and back rays. 
     916                @param frontRays returns rays on the front side of the plane 
     917                @param backRays returns rays on the back side of the plane 
     918                 
     919                @returns the number of splits 
     920        */ 
     921        int SplitRays(const Plane3 &plane, 
     922                                  BoundedRayContainer &rays,  
     923                              BoundedRayContainer &frontRays,  
     924                                  BoundedRayContainer &backRays); 
     925 
     926 
     927        /** Extracts the split planes representing the space bounded by node n. 
     928        */ 
     929        void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const; 
     930 
     931        /** Adds the object to the pvs of the front and back leaf with a given classification. 
     932 
     933                @param obj the object to be added 
     934                @param cf the ray classification regarding the split plane 
     935                @param frontPvs returns the PVS of the front partition 
     936                @param backPvs returns the PVS of the back partition 
     937         
     938        */ 
     939        void AddObjToPvs(Intersectable *obj, const int cf, int &frontPvs, int &backPvs) const; 
     940 
     941        /** Computes PVS size induced by the rays. 
     942        */ 
     943        int ComputePvsSize(const BoundedRayContainer &rays) const; 
     944 
     945        /** Returns true if tree can be terminated. 
     946        */ 
     947        inline bool TerminationCriteriaMet(const BspTraversalData &data) const; 
     948 
     949        /** Computes accumulated ray lenght of this rays. 
     950        */ 
     951        float AccumulatedRayLength(BoundedRayContainer &rays) const; 
     952 
     953        /** Splits polygons with respect to the split plane. 
     954                @param polys the polygons to be split. the polygons are consumed and 
     955                           distributed to the containers frontPolys, backPolys, coincident. 
     956                @param frontPolys returns the polygons in the front of the split plane 
     957                @param backPolys returns the polygons in the back of the split plane 
     958                @param coincident returns the polygons coincident to the split plane 
     959 
     960                @returns the number of splits    
     961        */ 
     962        int SplitPolygons(const Plane3 &plane, 
     963                                          PolygonContainer &polys,  
     964                                          PolygonContainer &frontPolys,  
     965                                          PolygonContainer &backPolys,  
     966                                          PolygonContainer &coincident) const; 
     967 
     968        /** Adds ray sample contributions to the PVS. 
     969                @param sampleContributions the number contributions of the samples 
     970                @param contributingSampels the number of contributing rays 
     971                 
     972        */ 
     973        void AddToPvs(BspLeaf *leaf, 
     974                                  const BoundedRayContainer &rays,  
     975                                  int &sampleContributions,      
     976                                  int &contributingSamples); 
     977 
     978        /** Preprocesses polygons and throws out all polygons which  
     979                are coincident to the view space box faces: 
     980                These polygons can can be problematic for bsp because they create 
     981                bad view cells. 
     982        */ 
     983        void PreprocessPolygons(PolygonContainer &polys); 
     984 
     985 
     986        /////////////////////////////////// 
     987                 
     988        /// Pointer to the root of the tree. 
     989        BspNode *mRoot; 
     990 
     991        /// Stores statistics during traversal. 
     992        BspTreeStatistics mStat; 
     993 
    1001994        /// box around the whole view domain 
    1002         AxisAlignedBox3 mBox; 
     995        AxisAlignedBox3 mBbox; 
    1003996 
    1004997        /// view cell corresponding to unbounded space 
     
    1006999 
    10071000        /// if view cells should be generated or the given view cells should be used. 
    1008         bool mGenerateViewCells; 
     1001        bool mUsePredefinedViewCells; 
    10091002 
    10101003        /// maximal number of polygons before subdivision termination 
     
    10761069        ofstream  mSubdivisionStats; 
    10771070 
     1071        ViewCellsTree *mViewCellsTree; 
     1072 
     1073        bool mOutOfBoundsCellPartOfTree; 
    10781074private: 
    10791075         
  • GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp

    r1528 r1545  
    2323#include "BvHierarchy.h" 
    2424#include "SamplingStrategy.h" 
     25#include "SceneGraph.h" 
    2526 
    2627 
    2728// should not count origin object for sampling because it disturbs heuristics 
    2829#define SAMPLE_ORIGIN_OBJECTS 0 
    29  
    3030 
    3131 
     
    6363mViewCellPvsIsUpdated(false), 
    6464mPreprocessor(NULL), 
    65 mViewCellsTree(viewCellsTree) 
     65mViewCellsTree(viewCellsTree), 
     66mUsePredefinedViewCells(false) 
    6667{ 
    6768        mViewSpaceBox.Initialize(); 
     
    649650{ 
    650651        if (!ViewCellsConstructed() || !ViewCellsTreeConstructed()) 
     652        { 
    651653                return false; 
     654        } 
    652655 
    653656        cout << "exporting view cells to xml ... "; 
     
    10441047 
    10451048        const int n = min(mMaxFilterSize, (int)neighborhood.size()); 
     1049         
     1050        ///////////////// 
    10461051        //-- use priority queue to merge leaf pairs 
    1047  
    1048         //cout << "neighborhood: " << neighborhood.size() << endl; 
    1049         //const float maxAvgCost = 350; 
     1052         
    10501053        for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells) 
    10511054        { 
     
    10581061                neighborhood.pop_back(); 
    10591062         
    1060                 //              cout << "vc idx: " << bestViewCellIdx << endl; 
    10611063                if (!bestViewCell || !root) 
    1062                         cout << "warning!!" << endl; 
     1064            cout << "warning!!" << endl; 
    10631065                 
    10641066                // create new root of the hierarchy 
     
    11741176                                                           const float filterWidth) 
    11751177{ 
    1176  
    1177          
    1178  
    1179          
    1180   ViewCell *currentViewCell = GetViewCell(viewPoint); 
    1181  
    1182   if (mMaxFilterSize < 1) { 
    1183         prvs.mViewCell = currentViewCell; 
    1184         return; 
    1185   } 
    1186    
    1187   const AxisAlignedBox3 box = GetFilterBBox(viewPoint, filterWidth); 
    1188    
    1189   if (currentViewCell) { 
    1190         ViewCellContainer viewCells; 
    1191         ComputeBoxIntersections(box, viewCells); 
    1192  
    1193  
    1194         ViewCell *root = ConstructLocalMergeTree2(currentViewCell, viewCells); 
    1195         prvs.mViewCell = root; 
    1196          
    1197   } else 
    1198         prvs.mViewCell = NULL; 
    1199   //prvs.mPvs = root->GetPvs(); 
     1178        ViewCell *currentViewCell = GetViewCell(viewPoint); 
     1179 
     1180        if (mMaxFilterSize < 1) { 
     1181                prvs.mViewCell = currentViewCell; 
     1182                return; 
     1183        } 
     1184 
     1185        const AxisAlignedBox3 box = GetFilterBBox(viewPoint, filterWidth); 
     1186 
     1187        if (currentViewCell)  
     1188        { 
     1189                ViewCellContainer viewCells; 
     1190                ComputeBoxIntersections(box, viewCells); 
     1191 
     1192                ViewCell *root = ConstructLocalMergeTree2(currentViewCell, viewCells); 
     1193                prvs.mViewCell = root; 
     1194 
     1195        }  
     1196        else 
     1197        { 
     1198                prvs.mViewCell = NULL; 
     1199                //prvs.mPvs = root->GetPvs(); 
     1200        } 
    12001201} 
    12011202 
     
    12621263 
    12631264 
    1264 bool ViewCellsManager::LoadViewCellsGeometry(const string filename) 
    1265 { 
     1265bool ViewCellsManager::LoadViewCellsGeometry(const string filename,  
     1266                                                                                         const bool extrudeBaseTriangles) 
     1267{ 
     1268        bool success; 
     1269 
     1270        /// we use predefined view cells from now on 
     1271        mUsePredefinedViewCells = true; 
    12661272        X3dParser parser; 
    1267  
    1268         Environment::GetSingleton()->GetFloatValue("ViewCells.height", parser.mViewCellHeight); 
    1269  
    1270         bool success = parser.ParseFile(filename, *this); 
    1271         Debug << (int)mViewCells.size() << " view cells loaded" << endl; 
     1273         
     1274        if (extrudeBaseTriangles) 
     1275        { 
     1276                Environment::GetSingleton()->GetFloatValue("ViewCells.height", parser.mViewCellHeight); 
     1277                success = parser.ParseFile(filename, *this); 
     1278        } 
     1279        else 
     1280        { 
     1281                // hack: use standard mesh loading 
     1282                // create temporary scene graph for loading the view cells geometry 
     1283                SceneGraphNode *root = new SceneGraphNode(); 
     1284                success = parser.ParseFile(filename, root, true); 
     1285                                 
     1286                if (success) 
     1287                { 
     1288                        ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end(); 
     1289                        for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit) 
     1290                        { 
     1291                                if ((*oit)->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE) 
     1292                                { 
     1293                                        TransformedMeshInstance *mit = dynamic_cast<TransformedMeshInstance *>(*oit); 
     1294                                        Mesh *mesh = MeshManager::GetSingleton()->CreateResource(); 
     1295                                        mit->GetTransformedMesh(*mesh); 
     1296                                        mesh->ComputeBoundingBox(); 
     1297                                        mViewCells.push_back(GenerateViewCell(mesh)); 
     1298                                } 
     1299                                else if ((*oit)->Type() == Intersectable::MESH_INSTANCE) 
     1300                                { 
     1301                                        MeshInstance *mit = dynamic_cast<MeshInstance *>(*oit); 
     1302                                        mViewCells.push_back(GenerateViewCell(mit->GetMesh())); 
     1303                                }                        
     1304                        } 
     1305                } 
     1306                DEL_PTR(root); 
     1307        } 
     1308 
     1309        if (success) 
     1310        { 
     1311                // set view space box to bounding box of the view cells 
     1312                /*AxisAlignedBox3 bbox; 
     1313                bbox.Initialize(); 
     1314                ViewCellContainer::iterator it = mViewCells.begin(), it_end = mViewCells.end(); 
     1315                for (; it != it_end; ++ it)  
     1316                { 
     1317                        bbox.Include(GetViewCellBox(*it)); 
     1318                } 
     1319                SetViewSpaceBox(bbox);*/ 
     1320         
     1321                cout << "generated " << (int)mViewCells.size() << " view cells using the geometry from file " << filename << endl; 
     1322        } 
    12721323 
    12731324        return success; 
     
    13961447 
    13971448 
    1398  
    1399 void ViewCellsManager::AddViewCell(ViewCell *viewCell) 
    1400 { 
    1401         mViewCells.push_back(viewCell); 
    1402 } 
    1403  
    1404  
    14051449float ViewCellsManager::GetArea(ViewCell *viewCell) const 
    14061450{ 
     
    14121456{ 
    14131457        return viewCell->GetVolume(); 
    1414 } 
    1415  
    1416  
    1417 void ViewCellsManager::DeriveViewCellsFromObjects(const ObjectContainer &objects, 
    1418                                                                                                   ViewCellContainer &viewCells, 
    1419                                                                                                   const int maxViewCells) const 
    1420 { 
    1421         // maximal max viewcells 
    1422         int limit = maxViewCells > 0 ? 
    1423                 Min((int)objects.size(), maxViewCells) : (int)objects.size(); 
    1424  
    1425         for (int i = 0; i < limit; ++ i) 
    1426         { 
    1427                 Intersectable *object = objects[i]; 
    1428  
    1429                 // extract the mesh instances 
    1430                 if (object->Type() == Intersectable::MESH_INSTANCE) 
    1431                 { 
    1432                         MeshInstance *inst = dynamic_cast<MeshInstance *>(object); 
    1433  
    1434                         ViewCell *viewCell = GenerateViewCell(inst->GetMesh()); 
    1435                         viewCells.push_back(viewCell); 
    1436                 } 
    1437                 else if (object->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE) 
    1438                 { 
    1439                         TransformedMeshInstance *mi = dynamic_cast<TransformedMeshInstance *>(object); 
    1440  
    1441                         Mesh *mesh = MeshManager::GetSingleton()->CreateResource(); 
    1442                          
    1443                         // transform mesh 
    1444                         mi->GetTransformedMesh(*mesh); 
    1445                          
    1446                         // create bb + kd tree 
    1447                         mesh->Preprocess(); 
    1448  
    1449                         ViewCell *viewCell = GenerateViewCell(mi->GetMesh()); 
    1450                         viewCells.push_back(viewCell); 
    1451  
    1452                         break; 
    1453                 } 
    1454         } 
    14551458} 
    14561459 
     
    14621465        Mesh *mesh = MeshManager::GetSingleton()->CreateResource(); 
    14631466 
     1467        //////////// 
    14641468        //-- construct prism 
    14651469 
     
    14731477        mesh->mFaces.push_back(new Face(3, 5, 2, 0)); 
    14741478 
    1475         //--- extrude new vertices for top of prism 
     1479 
     1480        ///////////// 
     1481        //-- extrude new vertices for top of prism 
     1482 
    14761483        Vector3 triNorm = baseTri.GetNormal(); 
    1477  
    14781484        Triangle3 topTri; 
    14791485 
    14801486        // add base vertices and calculate top vertices 
    14811487        for (int i = 0; i < 3; ++ i) 
     1488        { 
    14821489                mesh->mVertices.push_back(baseTri.mVertices[i]); 
     1490        } 
    14831491 
    14841492        // add top vertices 
    14851493        for (int i = 0; i < 3; ++ i) 
     1494        { 
    14861495                mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm); 
    1487  
    1488         mesh->Preprocess(); 
    1489  
     1496        } 
     1497 
     1498        // do we have to preprocess this mesh (we don't want to trace view cells!)? 
     1499        mesh->ComputeBoundingBox(); 
     1500         
    14901501        return GenerateViewCell(mesh); 
    14911502} 
     
    18751886                                // if view point is valid, add new object to the pvs 
    18761887                                if (ray.mTerminationObject) 
    1877                                 {cout <<"here22" << endl; 
     1888                                { 
    18781889                                        viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf); 
    18791890                                }                                
     
    20072018        mNumActiveViewCells = n; 
    20082019        mViewCells.clear(); 
     2020        // implemented in subclasses 
    20092021        CollectViewCells(); 
    20102022} 
     
    24972509        Environment::GetSingleton()->GetIntValue("BspTree.Construction.samples", mInitialSamples); 
    24982510        mBspTree->SetViewCellsManager(this); 
    2499         mBspTree->mViewCellsTree = mViewCellsTree; 
     2511        mBspTree->SetViewCellsTree(mViewCellsTree); 
    25002512} 
    25012513 
     
    25162528                                                                                          const VssRayContainer &rays) 
    25172529{ 
    2518         // if view cells were already constructed 
     2530        // if view cells were already constructed, we can finish 
    25192531        if (ViewCellsConstructed()) 
    25202532                return 0; 
     
    25402552        } 
    25412553 
    2542     if (mViewCells.empty()) 
     2554    if (!mUsePredefinedViewCells) 
    25432555        { 
    25442556                // no view cells loaded 
     
    25482560        } 
    25492561        else 
    2550         { 
     2562        {       cout << "here222" << endl; 
     2563                // use predefined view cells geometry =>  
     2564                // contruct bsp hierarchy over them 
    25512565                mBspTree->Construct(mViewCells); 
    25522566        } 
     
    25752589 
    25762590void BspViewCellsManager::CollectViewCells() 
    2577 { 
    2578         // view cells tree constructed 
     2591{        
    25792592        if (!ViewCellsTreeConstructed()) 
    2580         {                
     2593        {       // view cells tree constructed   
    25812594                mBspTree->CollectViewCells(mViewCells); 
    25822595        } 
    25832596        else  
    2584         { 
    2585                 // we can use the view cells tree hierarchy to get the right set 
    2586                 mViewCellsTree->CollectBestViewCellSet(mViewCells,  
    2587                                                                                            mNumActiveViewCells); 
     2597        {       // we can use the view cells tree hierarchy to get the right set 
     2598                mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells); 
    25882599        } 
    25892600} 
     
    25922603float BspViewCellsManager::GetProbability(ViewCell *viewCell) 
    25932604{ 
    2594         // compute view cell area as subsititute for probability 
    25952605        if (1) 
    25962606                return GetVolume(viewCell) / GetViewSpaceBox().GetVolume(); 
    25972607        else 
     2608                // compute view cell area as subsititute for probability 
    25982609                return GetArea(viewCell) / GetAccVcArea(); 
    25992610} 
     
    26092620 
    26102621 
     2622void BspViewCellsManager::ExportMergedViewCells(const ObjectContainer &objects) 
     2623{ 
     2624        // save color code 
     2625        const int savedColorCode = mColorCode; 
     2626 
     2627        // export merged view cells 
     2628        mColorCode = 0; // use random colors 
     2629 
     2630        Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl"); 
     2631 
     2632        cout << "exporting view cells after merge ... "; 
     2633 
     2634        if (exporter) 
     2635        { 
     2636                if (mExportGeometry) 
     2637                { 
     2638                        exporter->ExportGeometry(objects); 
     2639                } 
     2640 
     2641                exporter->SetFilled(); 
     2642                ExportViewCellsForViz(exporter, NULL, GetClipPlane()); 
     2643 
     2644                delete exporter; 
     2645        } 
     2646        cout << "finished" << endl; 
     2647 
     2648        // export merged view cells using pvs color coding 
     2649        mColorCode = 1; 
     2650 
     2651        exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl"); 
     2652        cout << "exporting view cells after merge (pvs size) ... ";      
     2653 
     2654        if (exporter) 
     2655        { 
     2656                if (mExportGeometry) 
     2657                { 
     2658                        exporter->ExportGeometry(objects); 
     2659                } 
     2660 
     2661                exporter->SetFilled(); 
     2662                ExportViewCellsForViz(exporter, NULL, GetClipPlane()); 
     2663 
     2664                delete exporter; 
     2665        } 
     2666        cout << "finished" << endl; 
     2667         
     2668        mColorCode = savedColorCode; 
     2669} 
     2670 
     2671 
    26112672int BspViewCellsManager::PostProcess(const ObjectContainer &objects, 
    26122673                                                                         const VssRayContainer &rays) 
     
    26282689        } 
    26292690 
    2630     int vcSize = 0; 
    2631         int pvsSize = 0; 
    2632          
    2633  
     2691if (mUsePredefinedViewCells) return 0; 
    26342692        ////////////////// 
    26352693        //-- merge leaves of the view cell hierarchy     
     
    26492707        { 
    26502708                cout << "constructing spatial merge tree" << endl; 
    2651  
    2652                 // create spatial merge hierarchy 
    2653                 ViewCell *root = ConstructSpatialMergeTree(mBspTree->GetRoot()); 
     2709                ViewCell *root; 
     2710                // the spatial merge tree is difficult to build for  
     2711                // this type of construction, as view cells cover several 
     2712                // leaves => create dummy tree which is only 2 levels deep 
     2713                if (mUsePredefinedViewCells)  
     2714                { 
     2715                        root = ConstructDummyMergeTree(mBspTree->GetRoot()); 
     2716                } 
     2717                else 
     2718                { 
     2719                        // create spatial merge hierarchy 
     2720                        root = ConstructSpatialMergeTree(mBspTree->GetRoot()); 
     2721                } 
     2722                 
    26542723                mViewCellsTree->SetRoot(root); 
    26552724 
     
    26572726                ObjectPvs pvs; 
    26582727                UpdatePvsForEvaluation(root, pvs); 
     2728                 
    26592729        } 
    26602730 
     
    26802750        Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl; 
    26812751 
    2682         // save color code 
    2683         const int savedColorCode = mColorCode; 
    2684          
    2685         if (1) // export merged view cells 
    2686         { 
    2687                 mColorCode = 0; // use random colors 
    2688                 Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl"); 
     2752        if (1) ExportMergedViewCells(objects); 
     2753 
     2754        // only for debugging purpose: test if the subdivision is valid 
     2755        if (0) TestSubdivision(); 
     2756 
     2757        // compute final meshes and volume / area 
     2758        if (1) FinalizeViewCells(true); 
     2759 
     2760        // write view cells to disc 
     2761        if (1 && mExportViewCells) 
     2762        { 
     2763                char filename[100]; 
     2764                Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename); 
     2765                ExportViewCells(filename, mExportPvs, objects); 
     2766        } 
     2767         
     2768        return 0; 
     2769} 
     2770 
     2771 
     2772BspViewCellsManager::~BspViewCellsManager() 
     2773{ 
     2774} 
     2775 
     2776 
     2777int BspViewCellsManager::GetType() const 
     2778{ 
     2779        return BSP; 
     2780} 
     2781 
     2782 
     2783void BspViewCellsManager::Visualize(const ObjectContainer &objects, 
     2784                                                                        const VssRayContainer &sampleRays) 
     2785{ 
     2786        if (!ViewCellsConstructed()) 
     2787                return; 
     2788         
     2789        int savedColorCode = mColorCode; 
    26892790                 
    2690                 cout << "exporting view cells after merge ... "; 
     2791        if (1) // export final view cells 
     2792        { 
     2793                mColorCode = 1; // hack color code 
     2794                Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl"); 
     2795         
     2796                cout << "exporting view cells after merge (pvs size) ... ";      
    26912797 
    26922798                if (exporter) 
     
    26972803                        } 
    26982804 
    2699                         exporter->SetFilled(); 
    2700                         ExportViewCellsForViz(exporter, NULL, GetClipPlane()); 
    2701  
    2702                         delete exporter; 
    2703                 } 
    2704                 cout << "finished" << endl; 
    2705         } 
    2706  
    2707         if (1)  
    2708         { 
    2709                 // export merged view cells using pvs color coding 
    2710                 mColorCode = 1; 
    2711  
    2712                 Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl"); 
    2713                 cout << "exporting view cells after merge (pvs size) ... ";      
    2714  
    2715                 if (exporter) 
    2716                 { 
    2717                         if (mExportGeometry) 
    2718                         { 
    2719                                 exporter->ExportGeometry(objects); 
    2720                         } 
    2721                          
    2722                         exporter->SetFilled(); 
    2723                         ExportViewCellsForViz(exporter, NULL, GetClipPlane()); 
    2724  
    2725                         delete exporter; 
    2726                 } 
    2727                 cout << "finished" << endl; 
    2728         } 
    2729          
    2730         // only for debugging purpose: test if the subdivision is valid 
    2731         if (0) TestSubdivision(); 
    2732  
    2733         mColorCode = savedColorCode; 
    2734  
    2735         // compute final meshes and volume / area 
    2736         if (1) FinalizeViewCells(true); 
    2737  
    2738         // write view cells to disc 
    2739         if (1 && mExportViewCells) 
    2740         { 
    2741                 char filename[100]; 
    2742                 Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename); 
    2743                 ExportViewCells(filename, mExportPvs, objects); 
    2744         } 
    2745          
    2746         return 0; 
    2747 } 
    2748  
    2749  
    2750 BspViewCellsManager::~BspViewCellsManager() 
    2751 { 
    2752 } 
    2753  
    2754  
    2755 int BspViewCellsManager::GetType() const 
    2756 { 
    2757         return BSP; 
    2758 } 
    2759  
    2760  
    2761 void BspViewCellsManager::Visualize(const ObjectContainer &objects, 
    2762                                                                         const VssRayContainer &sampleRays) 
    2763 { 
    2764         if (!ViewCellsConstructed()) 
    2765                 return; 
    2766          
    2767         int savedColorCode = mColorCode; 
    2768          
    2769          
    2770         if (1) // export final view cells 
    2771         { 
    2772                 mColorCode = 1; // hack color code 
    2773                 Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl"); 
    2774          
    2775                 cout << "exporting view cells after merge (pvs size) ... ";      
    2776  
    2777                 if (exporter) 
    2778                 { 
    2779                         if (mExportGeometry) 
    2780                         { 
    2781                                 exporter->ExportGeometry(objects); 
    2782                         } 
    2783  
    27842805                        ExportViewCellsForViz(exporter, NULL, GetClipPlane()); 
    27852806                        delete exporter; 
     
    27872808                cout << "finished" << endl; 
    27882809        } 
    2789  
     2810        // reset color code 
    27902811        mColorCode = savedColorCode; 
    27912812 
     
    28162837        { 
    28172838                //exporter->SetFilled(); 
    2818  
    28192839                if (mExportGeometry) 
     2840                { 
    28202841                        exporter->ExportGeometry(objects); 
     2842                } 
    28212843 
    28222844                Material m; 
     
    28272849                exporter->ExportBspSplits(*mBspTree, true); 
    28282850 
    2829                 //NOTE: take forced material, else big scenes cannot be viewed 
     2851                // NOTE: take forced material, else big scenes cannot be viewed 
    28302852                m.mDiffuseColor = RgbColor(0, 1, 0); 
    28312853                exporter->SetForcedMaterial(m); 
     
    28402862{ 
    28412863        const int leafOut = 10; 
    2842  
    28432864        ViewCell::NewMail(); 
    28442865 
     2866        ////////// 
    28452867        //-- some rays for output 
    28462868        const int raysOut = min((int)mBspRays.size(), mVisualizationSamples); 
     
    28612883                ViewCell *vc; 
    28622884 
    2863                 if (0) 
     2885                if (0 || ((int)mViewCells.size() <= limit)) 
    28642886                        vc = mViewCells[i]; 
    28652887                else 
     
    28782900                                        BspLeaf *leaf = ray->intersections[j].mLeaf; 
    28792901                                        if (vc == leaf->GetViewCell()) 
     2902                                        { 
    28802903                                                vcRays.push_back(ray->vssRay); 
     2904                                        } 
    28812905                                } 
    28822906                        } 
     
    30803104{ 
    30813105        if (!ViewCellsConstructed()) 
     3106        { 
    30823107                return NULL; 
     3108        } 
    30833109 
    30843110        if (!mViewSpaceBox.IsInside(point)) 
     3111        { 
    30853112                return NULL; 
    3086          
     3113        } 
     3114 
    30873115        return mBspTree->GetViewCell(point); 
    30883116} 
     
    31543182#endif 
    31553183        return true; 
     3184} 
     3185 
     3186 
     3187ViewCell *BspViewCellsManager::ConstructDummyMergeTree(BspNode *root) 
     3188{ 
     3189        ViewCellInterior *vcRoot = new ViewCellInterior(); 
     3190                 
     3191        // evaluate merge cost for priority traversal 
     3192        const float mergeCost = 1.0f / (float)root->mTimeStamp; 
     3193        vcRoot->SetMergeCost(mergeCost); 
     3194 
     3195        float volume = 0; 
     3196        vector<BspLeaf *> leaves; 
     3197        mBspTree->CollectLeaves(leaves); 
     3198        vector<BspLeaf *>::const_iterator lit, lit_end = leaves.end(); 
     3199         
     3200        for (lit = leaves.begin(); lit != lit_end; ++ lit) 
     3201        { 
     3202                BspLeaf *leaf = *lit; 
     3203                ViewCell *vc = leaf->GetViewCell(); 
     3204 
     3205                vc->SetMergeCost(0.0f); 
     3206                vcRoot->SetupChildLink(vc); 
     3207         
     3208                volume += vc->GetVolume(); 
     3209                volume += vc->GetVolume();       
     3210 
     3211                vcRoot->SetVolume(volume); 
     3212        } 
     3213         
     3214        return vcRoot; 
    31563215} 
    31573216 
     
    31803239 
    31813240 
     3241        //////////// 
    31823242        //-- recursivly compute child hierarchies 
     3243 
    31833244        ViewCell *backVc = ConstructSpatialMergeTree(back); 
    31843245        ViewCell *frontVc = ConstructSpatialMergeTree(front); 
    3185  
    31863246 
    31873247        viewCellInterior->SetupChildLink(backVc); 
     
    35283588void VspBspViewCellsManager::CollectViewCells() 
    35293589{ 
    3530         // view cells tree constructed 
     3590        // view cells tree constructed? 
    35313591        if (!ViewCellsTreeConstructed()) 
    35323592        { 
     
    35813641        // if view cells were already constructed 
    35823642        if (ViewCellsConstructed()) 
     3643        { 
    35833644                return 0; 
     3645        } 
    35843646 
    35853647        int sampleContributions = 0; 
    3586  
    35873648        VssRayContainer sampleRays; 
    35883649 
    3589         int limit = min (mInitialSamples, (int)rays.size()); 
    3590  
    3591         VssRayContainer constructionRays; 
    3592         VssRayContainer savedRays; 
     3650        const int limit = min(mInitialSamples, (int)rays.size()); 
    35933651 
    35943652        Debug << "samples used for vsp bsp subdivision: " << mInitialSamples  
    35953653                  << ", actual rays: " << (int)rays.size() << endl; 
    35963654 
    3597         GetRaySets(rays, mInitialSamples, constructionRays, &savedRays); 
    3598  
    3599         Debug << "initial rays: " << (int)constructionRays.size() << endl; 
    3600         Debug << "saved rays: " << (int)savedRays.size() << endl; 
    3601  
    3602         long startTime; 
    3603  
    3604         if (1) 
    3605         { 
     3655        VssRayContainer savedRays; 
     3656 
     3657        if (SAMPLE_AFTER_SUBDIVISION) 
     3658        { 
     3659                VssRayContainer constructionRays; 
     3660                 
     3661                GetRaySets(rays, mInitialSamples, constructionRays, &savedRays); 
     3662 
     3663                Debug << "rays used for initial construction: " << (int)constructionRays.size() << endl; 
     3664                Debug << "rays saved for later use: " << (int)savedRays.size() << endl; 
     3665         
    36063666                mVspBspTree->Construct(constructionRays, &mViewSpaceBox); 
    36073667        } 
    36083668        else 
    36093669        { 
     3670                Debug << "rays used for initial construction: " << (int)rays.size() << endl; 
    36103671                mVspBspTree->Construct(rays, &mViewSpaceBox); 
    36113672        } 
     
    36133674        // collapse invalid regions 
    36143675        cout << "collapsing invalid tree regions ... "; 
    3615         startTime = GetTime(); 
     3676        long startTime = GetTime(); 
     3677 
    36163678        const int collapsedLeaves = mVspBspTree->CollapseTree(); 
    36173679        Debug << "collapsed in " << TimeDiff(startTime, GetTime()) * 1e-3  
     
    36303692 
    36313693        ////////////////////// 
    3632         //-- recast rest of rays 
     3694        //-- recast the rest of the rays 
    36333695        startTime = GetTime(); 
    36343696 
     
    36463708 
    36473709        if (0) 
    3648         { 
     3710        {       //////// 
    36493711                //-- real meshes are contructed at this stage 
    36503712                cout << "finalizing view cells ... "; 
     
    41794241                // export rays 
    41804242                if (mExportRays) 
     4243                { 
    41814244                        exporter->ExportRays(rays, RgbColor(1, 1, 0)); 
     4245                } 
    41824246 
    41834247                if (mExportGeometry) 
     4248                { 
    41844249                        exporter->ExportGeometry(objects); 
     4250                } 
    41854251 
    41864252                delete exporter; 
     
    47834849        mHierarchyManager->Construct(constructionRays, objects, &mViewSpaceBox); 
    47844850 
    4785         VssRayContainer::const_iterator vit, vit_end = constructionRays.end(); 
    4786  
    4787         for (vit = constructionRays.begin(); vit != vit_end; ++ vit) 
    4788         { 
    4789                 storedRays.push_back(new VssRay(*(*vit))); 
    4790         } 
    4791  
    4792         //////////// 
     4851         
     4852        ///////////////////////// 
    47934853        //-- print satistics for subdivision and view cells 
    47944854 
     
    48054865 
    48064866 
    4807         ////////// 
     4867        ////////////// 
    48084868        //-- recast rest of rays 
    48094869         
     
    48184878 
    48194879        if (0) 
    4820         { 
    4821                 // real meshes are constructed at this stage 
     4880        {       // real meshes are constructed at this stage 
    48224881                cout << "finalizing view cells ... "; 
    48234882                FinalizeViewCells(true); 
     
    49615020        VspNode *back = interior->GetBack(); 
    49625021 
    4963  
    49645022        ObjectPvs frontPvs, backPvs; 
    49655023 
     5024        ///////// 
    49665025        //-- recursivly compute child hierarchies 
     5026 
    49675027        ViewCell *backVc = ConstructSpatialMergeTree(back); 
    49685028        ViewCell *frontVc = ConstructSpatialMergeTree(front); 
    4969  
    49705029 
    49715030        viewCellInterior->SetupChildLink(backVc); 
     
    50935152                        AxisAlignedBox3 bbox = mHierarchyManager->GetViewSpaceBox(); 
    50945153                        bbox.Scale(Vector3(0.5, 1, 0.5)); 
    5095                                  
    50965154                        if (CLAMP_TO_BOX) 
    50975155                        {        
     
    51015159                        } 
    51025160                                 
    5103                         /////////////////// 
    5104  
    51055161                        if (0 && mExportGeometry) 
    51065162                        { 
    51075163                                exporter->ExportGeometry(objects, true, CLAMP_TO_BOX ? &bbox : NULL); 
    51085164                        } 
    5109  
    5110                         // export rays 
     5165                         
    51115166                        if (0 && mExportRays) 
    5112                         { 
     5167                        {        
    51135168                                exporter->ExportRays(visRays, RgbColor(0, 1, 0)); 
    51145169                        } 
     
    56505705        } 
    56515706 
    5652         Debug << "\nrendercost hack: " << rc / mHierarchyManager->GetVspTree()->GetBoundingBox().GetVolume() << endl; 
     5707        Debug << "\nrendercost hack: " << rc / mHierarchyManager->GetViewSpaceBox() << endl; 
    56535708        mViewCellsTree->ExportStats(fileName); 
    56545709        cout << "finished" << endl; 
  • GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h

    r1421 r1545  
    7070                float avgPvs; 
    7171                int viewcells; 
    72   }; 
     72        }; 
    7373 
    7474        /// view cell container types 
     
    7878        enum {PER_OBJECT, PER_TRIANGLE}; 
    7979 
     80        friend class X3dViewCellsParseHandlers; 
    8081        /** Default constructor. 
    8182        */ 
     
    144145 
    145146        /** Load the input viewcells. The input viewcells should be given as a collection 
    146                 of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of 
    147                 the viewcell. The method then builds a BSP tree of these view cells. 
     147                of meshes. Each mesh is assume to form a bounded polyhedron  
     148                defining the interior of the viewcell. The view cells manager 
     149                is responsible for building a hierarchy over these view cells. 
    148150                 
    149151                @param filename file to load 
    150152                @return true on success 
    151153    */ 
    152     virtual bool LoadViewCellsGeometry(const string filename); 
    153          
     154    virtual bool LoadViewCellsGeometry(const string filename, const bool extrudeBaseTriangle); 
     155         
     156        /** Merges two view cells. 
     157                @note the piercing rays of the front and back will be ordered    
     158                @returns new view cell based on the merging. 
     159        */ 
     160        ViewCellInterior *MergeViewCells(ViewCell *front, ViewCell *back) const; 
     161 
     162        /** Merges a container of view cells. 
     163                @returns new view cell based on the merging. 
     164        */ 
     165        ViewCellInterior *MergeViewCells(ViewCellContainer &children) const; 
     166         
     167        /** Generates view cell of the type specified by this manager 
     168        */ 
     169        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const = 0; 
     170 
    154171        /** Constructs view cell from base triangle.  
    155172                The view cell is extruded along the normal vector. 
     
    158175        */ 
    159176        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const; 
    160  
    161         /** Merges two view cells. 
    162                 @note the piercing rays of the front and back will be ordered    
    163                 @returns new view cell based on the merging. 
    164         */ 
    165         ViewCellInterior *MergeViewCells(ViewCell *front, ViewCell *back) const; 
    166  
    167         /** Merges a container of view cells. 
    168                 @returns new view cell based on the merging. 
    169         */ 
    170         ViewCellInterior *MergeViewCells(ViewCellContainer &children) const; 
    171          
    172         /** Generates view cell of type specified by this manager 
    173         */ 
    174         virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const = 0; 
    175  
    176         /** Adds a new view cell to the list of predefined view cells. 
    177         */ 
    178         void AddViewCell(ViewCell *viewCell); 
    179  
    180         /** Derive view cells from scene objects. The view ells are created by taking the object 
    181                 geometry into account. 
    182         */ 
    183         void DeriveViewCellsFromObjects(const ObjectContainer &objects,  
    184                                                                         ViewCellContainer &viewCells,  
    185                                                                         const int maxViewCells) const; 
    186177 
    187178        /** Sets maximal number of samples used for the  
     
    338329        void SetValidityPercentage(const float minValid, const float maxValid); 
    339330 
     331        /** Returns number of valid view cells. 
     332        */ 
    340333    int CountValidViewcells() const; 
    341334 
     
    378371        ViewCellsTree *GetViewCellsTree(); 
    379372 
     373        /** Collect candidates for the view cell merge. 
     374        */ 
    380375        virtual void CollectMergeCandidates(const VssRayContainer &rays,  
    381376                                                                                vector<MergeCandidate> &candidates); 
     
    438433        bool GetExportPvs() const; 
    439434 
     435        ///////////////////////////// 
     436        // static members 
     437         
    440438        /** Loads view cells from file. The view cells manager is created with  
    441439                respect to the loaded view cells. 
     
    569567        */ 
    570568        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const; 
     569         
    571570        /** Creates meshes from the view cells. 
    572571        */ 
    573572        void CreateViewCellMeshes(); 
    574         /** Creates clip plane for visualization. 
     573 
     574    /** Creates clip plane for visualization. 
    575575        */ 
    576576        void CreateClipPlane(); 
    577577 
    578578        AxisAlignedPlane *GetClipPlane(); 
     579         
    579580        /////////////////////// 
    580581 
     
    663664        int mMaxFilterSize; 
    664665 
     666        bool mStoreObjectPvs; 
     667 
     668        ////////////////// 
    665669        //-- visualization options 
    666670         
    667671        /// color code for view cells visualization 
     672                bool mShowVisualization; 
    668673        int mColorCode; 
    669674        bool mExportGeometry; 
    670675        bool mExportRays; 
    671  
    672676        bool mViewCellsFinished; 
    673  
    674677        bool mEvaluateViewCells; 
    675  
    676         bool mShowVisualization; 
    677  
    678678        int mRenderCostEvaluationType; 
    679679 
     
    681681        bool mExportPvs; 
    682682 
    683         bool mStoreObjectPvs; 
    684  
    685         VssRayContainer storedRays; 
     683        bool mUsePredefinedViewCells; 
    686684}; 
    687685 
    688686 
    689 /** 
    690         Manages different higher order operations on the view cells. 
     687/** Manages different higher order operations on the view cells. 
    691688*/ 
    692689class BspViewCellsManager: public ViewCellsManager 
     
    754751        ViewCell *ConstructSpatialMergeTree(BspNode *root); 
    755752 
    756          
     753 
    757754protected: 
    758755 
    759756        void CollectViewCells(); 
    760  
    761757         
    762758        /// the BSP tree. 
    763759        BspTree *mBspTree; 
    764          
    765760        vector<BspRay *> mBspRays; 
    766761 
    767762private: 
    768763 
     764        /** Constructs a spatial merge tree only 2 levels deep. 
     765        */ 
     766        ViewCell *ConstructDummyMergeTree(BspNode *root); 
     767 
    769768        /** Exports visualization of the BSP splits. 
    770769        */ 
     
    778777        */ 
    779778        void TestSubdivision(); 
     779 
     780        void ExportMergedViewCells(const ObjectContainer &objects); 
    780781}; 
     782 
    781783 
    782784/** 
     
    10911093 
    10921094 
    1093 /*class ViewCellsManagerFactory 
    1094 { 
    1095 public: 
    1096         ViewCellsManager *Create(const string mName); 
    1097 };*/ 
    1098  
    10991095} 
    11001096 
  • GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp

    r1449 r1545  
    265265 
    266266        int polysSize = 0; 
    267  
     267         
    268268        for (int i = 0; i < limit; ++ i) 
    269269        { 
    270                 if (viewCells[i]->GetMesh()) // copy the mesh data to polygons 
    271                 { 
    272                         mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb 
    273                         polysSize += 
    274                                 AddMeshToPolygons(viewCells[i]->GetMesh(), 
    275                                                                   polys, 
    276                                                                   viewCells[i]); 
    277                 } 
    278         } 
    279  
     270                Mesh *mesh = viewCells[i]->GetMesh(); 
     271                if (mesh)  
     272                {       // // copy the mesh into polygons and add to BSP tree aabb 
     273                        mBox.Include(viewCells[i]->GetBox());  
     274                        polysSize += AddMeshToPolygons(mesh, 
     275                                                                                   polys, 
     276                                                                                   viewCells[i]); 
     277                } 
     278        } 
     279         
    280280        return polysSize; 
    281281} 
     
    25882588{ 
    25892589        ViewCell::NewMail(); 
    2590          
    25912590        CollectViewCells(mRoot, onlyValid, viewCells, true); 
    25922591} 
    2593  
    2594  
    2595 void VspBspTree::CollapseViewCells() 
    2596 { 
    2597 // TODO 
    2598 #if HAS_TO_BE_REDONE 
    2599         stack<BspNode *> nodeStack; 
    2600  
    2601         if (!mRoot) 
    2602                 return; 
    2603  
    2604         nodeStack.push(mRoot); 
    2605          
    2606         while (!nodeStack.empty())  
    2607         { 
    2608                 BspNode *node = nodeStack.top(); 
    2609                 nodeStack.pop(); 
    2610                  
    2611                 if (node->IsLeaf()) 
    2612         { 
    2613                         BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
    2614  
    2615                         if (!viewCell->GetValid()) 
    2616                         { 
    2617                                 BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
    2618          
    2619                                 ViewCellContainer leaves; 
    2620                                 mViewCellsTree->CollectLeaves(viewCell, leaves); 
    2621  
    2622                                 ViewCellContainer::const_iterator it, it_end = leaves.end(); 
    2623  
    2624                                 for (it = leaves.begin(); it != it_end; ++ it) 
    2625                                 { 
    2626                                         BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf; 
    2627                                         l->SetViewCell(GetOrCreateOutOfBoundsCell()); 
    2628                                         ++ mBspStats.invalidLeaves; 
    2629                                 } 
    2630  
    2631                                 // add to unbounded view cell 
    2632                                 GetOrCreateOutOfBoundsCell()->GetPvs().AddPvs(viewCell->GetPvs()); 
    2633                                 DEL_PTR(viewCell); 
    2634                         } 
    2635                 } 
    2636                 else 
    2637                 { 
    2638                         BspInterior *interior = dynamic_cast<BspInterior *>(node); 
    2639                  
    2640                         nodeStack.push(interior->GetFront()); 
    2641                         nodeStack.push(interior->GetBack()); 
    2642                 } 
    2643         } 
    2644  
    2645         Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl; 
    2646 #endif 
    2647 } 
    2648  
    2649  
    2650 void VspBspTree::CollectRays(VssRayContainer &rays) 
    2651 { 
    2652         vector<BspLeaf *> leaves; 
    2653  
    2654         vector<BspLeaf *>::const_iterator lit, lit_end = leaves.end(); 
    2655  
    2656         for (lit = leaves.begin(); lit != lit_end; ++ lit) 
    2657         { 
    2658                 BspLeaf *leaf = *lit; 
    2659                 VssRayContainer::const_iterator rit, rit_end = leaf->mVssRays.end(); 
    2660  
    2661                 for (rit = leaf->mVssRays.begin(); rit != rit_end; ++ rit) 
    2662                         rays.push_back(*rit); 
    2663         } 
    2664 } 
    2665  
    2666  
    2667 void VspBspTree::ValidateTree() 
    2668 { 
    2669         stack<BspNode *> nodeStack; 
    2670  
    2671         if (!mRoot) 
    2672                 return; 
    2673  
    2674         nodeStack.push(mRoot); 
    2675          
    2676         mBspStats.invalidLeaves = 0; 
    2677         while (!nodeStack.empty())  
    2678         { 
    2679                 BspNode *node = nodeStack.top(); 
    2680                 nodeStack.pop(); 
    2681                  
    2682                 if (node->IsLeaf()) 
    2683                 { 
    2684                         BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); 
    2685  
    2686                         if (!leaf->GetViewCell()->GetValid()) 
    2687                                 ++ mBspStats.invalidLeaves; 
    2688  
    2689                         // validity flags don't match => repair 
    2690                         if (leaf->GetViewCell()->GetValid() != leaf->TreeValid()) 
    2691                         { 
    2692                                 leaf->SetTreeValid(leaf->GetViewCell()->GetValid()); 
    2693                                 PropagateUpValidity(leaf); 
    2694                         } 
    2695                 } 
    2696                 else 
    2697                 { 
    2698                         BspInterior *interior = dynamic_cast<BspInterior *>(node); 
    2699                  
    2700                         nodeStack.push(interior->GetFront()); 
    2701                         nodeStack.push(interior->GetBack()); 
    2702                 } 
    2703         } 
    2704  
    2705         Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl; 
    2706 } 
    2707  
    27082592 
    27092593 
     
    27482632                } 
    27492633        } 
    2750  
     2634} 
     2635 
     2636 
     2637void VspBspTree::CollapseViewCells() 
     2638{ 
     2639// TODO 
     2640#if HAS_TO_BE_REDONE 
     2641        stack<BspNode *> nodeStack; 
     2642 
     2643        if (!mRoot) 
     2644                return; 
     2645 
     2646        nodeStack.push(mRoot); 
     2647         
     2648        while (!nodeStack.empty())  
     2649        { 
     2650                BspNode *node = nodeStack.top(); 
     2651                nodeStack.pop(); 
     2652                 
     2653                if (node->IsLeaf()) 
     2654        { 
     2655                        BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
     2656 
     2657                        if (!viewCell->GetValid()) 
     2658                        { 
     2659                                BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); 
     2660         
     2661                                ViewCellContainer leaves; 
     2662                                mViewCellsTree->CollectLeaves(viewCell, leaves); 
     2663 
     2664                                ViewCellContainer::const_iterator it, it_end = leaves.end(); 
     2665 
     2666                                for (it = leaves.begin(); it != it_end; ++ it) 
     2667                                { 
     2668                                        BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf; 
     2669                                        l->SetViewCell(GetOrCreateOutOfBoundsCell()); 
     2670                                        ++ mBspStats.invalidLeaves; 
     2671                                } 
     2672 
     2673                                // add to unbounded view cell 
     2674                                GetOrCreateOutOfBoundsCell()->GetPvs().AddPvs(viewCell->GetPvs()); 
     2675                                DEL_PTR(viewCell); 
     2676                        } 
     2677                } 
     2678                else 
     2679                { 
     2680                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     2681                 
     2682                        nodeStack.push(interior->GetFront()); 
     2683                        nodeStack.push(interior->GetBack()); 
     2684                } 
     2685        } 
     2686 
     2687        Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl; 
     2688#endif 
     2689} 
     2690 
     2691 
     2692void VspBspTree::CollectRays(VssRayContainer &rays) 
     2693{ 
     2694        vector<BspLeaf *> leaves; 
     2695 
     2696        vector<BspLeaf *>::const_iterator lit, lit_end = leaves.end(); 
     2697 
     2698        for (lit = leaves.begin(); lit != lit_end; ++ lit) 
     2699        { 
     2700                BspLeaf *leaf = *lit; 
     2701                VssRayContainer::const_iterator rit, rit_end = leaf->mVssRays.end(); 
     2702 
     2703                for (rit = leaf->mVssRays.begin(); rit != rit_end; ++ rit) 
     2704                        rays.push_back(*rit); 
     2705        } 
     2706} 
     2707 
     2708 
     2709void VspBspTree::ValidateTree() 
     2710{ 
     2711        stack<BspNode *> nodeStack; 
     2712 
     2713        if (!mRoot) 
     2714                return; 
     2715 
     2716        nodeStack.push(mRoot); 
     2717         
     2718        mBspStats.invalidLeaves = 0; 
     2719        while (!nodeStack.empty())  
     2720        { 
     2721                BspNode *node = nodeStack.top(); 
     2722                nodeStack.pop(); 
     2723                 
     2724                if (node->IsLeaf()) 
     2725                { 
     2726                        BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); 
     2727 
     2728                        if (!leaf->GetViewCell()->GetValid()) 
     2729                                ++ mBspStats.invalidLeaves; 
     2730 
     2731                        // validity flags don't match => repair 
     2732                        if (leaf->GetViewCell()->GetValid() != leaf->TreeValid()) 
     2733                        { 
     2734                                leaf->SetTreeValid(leaf->GetViewCell()->GetValid()); 
     2735                                PropagateUpValidity(leaf); 
     2736                        } 
     2737                } 
     2738                else 
     2739                { 
     2740                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
     2741                 
     2742                        nodeStack.push(interior->GetFront()); 
     2743                        nodeStack.push(interior->GetBack()); 
     2744                } 
     2745        } 
     2746 
     2747        Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl; 
    27512748} 
    27522749 
  • GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h

    r1449 r1545  
    151151                } 
    152152    }; 
    153          
    154153 
    155154        typedef std::priority_queue<VspBspTraversalData> VspBspTraversalQueue; 
    156          
    157155         
    158156        class VspBspSubdivisionCandidate 
  • GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp

    r1522 r1545  
    18561856 
    18571857                                // add to unbounded view cell 
    1858                                 GetOrCreateOutOfBoundsCell()->GetPvs().AddPvs(viewCell->GetPvs()); 
     1858                                ViewCell *outOfBounds = GetOrCreateOutOfBoundsCell(); 
     1859                                outOfBounds->GetPvs().AddPvs(viewCell->GetPvs()); 
    18591860                                DEL_PTR(viewCell); 
    18601861                        } 
     
    19001901void VspTree::ValidateTree() 
    19011902{ 
    1902         mVspStats.invalidLeaves = 0; 
    1903  
    1904         stack<VspNode *> nodeStack; 
    1905  
    19061903        if (!mRoot) 
    19071904                return; 
     1905 
     1906        mVspStats.invalidLeaves = 0; 
     1907        stack<VspNode *> nodeStack; 
    19081908 
    19091909        nodeStack.push(mRoot); 
     
    19471947                                                                  bool onlyUnmailed) const 
    19481948{ 
    1949         stack<VspNode *> nodeStack; 
    1950  
    19511949        if (!root) 
    19521950                return; 
    19531951 
     1952        stack<VspNode *> nodeStack; 
    19541953        nodeStack.push(root); 
    19551954         
  • GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp

    r1486 r1545  
    998998} 
    999999 
     1000 
    10001001void X3dExporter::ExportBspSplits(const BspTree &tree, 
    10011002                                                                  const bool exportDepth) 
     
    10071008} 
    10081009 
     1010 
    10091011void X3dExporter::ExportBspSplits(const VspBspTree &tree, 
    10101012                                                                  const bool exportDepth) 
     
    10151017                                                tree.GetEpsilon()); 
    10161018} 
     1019 
    10171020 
    10181021void X3dExporter::ExportBspSplitPlanes(const BspTree &tree) 
  • GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp

    r1421 r1545  
    918918 
    919919                // create view cell from base triangle 
    920                 mViewCellsManager->AddViewCell( 
    921                         mViewCellsManager->ExtrudeViewCell(baseTri,  
    922                         mViewCellHeight)); 
     920                ViewCell *vc = mViewCellsManager->ExtrudeViewCell(baseTri, mViewCellHeight); 
     921                mViewCellsManager->mViewCells.push_back(vc); 
    923922        } 
    924923} 
  • GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp

    r1522 r1545  
    172172        if (0)  
    173173        { 
    174                 preprocessor->Export(filename + "-out.x3d", true, false, false); 
    175                 preprocessor->Export(filename + "-kdtree.x3d", false, true, false);      
     174                preprocessor->Export(filename + "-out.x3d", true, false); 
     175                preprocessor->Export(filename + "-kdtree.x3d", false, true);     
    176176        } 
    177177 
Note: See TracChangeset for help on using the changeset viewer.