Ignore:
Timestamp:
12/01/05 18:50:06 (19 years ago)
Author:
mattausch
Message:

added visibilitymanager
removed area computation from bsp

Location:
trunk/VUT/GtpVisibilityPreprocessor/src
Files:
13 edited

Legend:

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

    r438 r441  
    11751175                                                                 "true"); 
    11761176 
    1177   RegisterOption("ViewCells.hierarchy", 
     1177  RegisterOption("ViewCells.type", 
    11781178                 optString, 
    1179                  "-view_cells_hierarchy", 
     1179                 "-view_cells_type", 
    11801180                 "bspTree"); 
     1181 
     1182   RegisterOption("ViewCells.loadFromFile", 
     1183                 optBool, 
     1184                 "-view_cells_load_from_file", 
     1185                 "false"); 
    11811186 
    11821187  RegisterOption("ViewCells.maxViewCells", 
     
    13291334          "-bsp_max_plane_candidates=", 
    13301335          "20"); 
     1336 
     1337  RegisterOption("BspTree.maxTests", 
     1338          optInt, 
     1339          "-bsp_max_tests=", 
     1340          "5000"); 
    13311341 
    13321342  RegisterOption("BspTree.Visualization.samples", 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp

    r440 r441  
    175175                int bspConstructionSamples = 0; 
    176176 
     177                mBspTree = new BspTree(); 
     178 
    177179                environment->GetIntValue("BspTree.Construction.samples", bspConstructionSamples); 
    178180                mViewCellsManager = new BspViewCellsManager(mBspTree, bspConstructionSamples); 
     
    212214 
    213215        bool loadViewCells = false; 
    214         environment->GetBoolValue("ViewCell.Construction.loadFromFile", loadViewCells); 
     216        environment->GetBoolValue("ViewCells.loadFromFile", loadViewCells); 
    215217 
    216218        //-- load view cells from file if requested 
  • trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp

    r436 r441  
    229229                dir = vssRay.GetDir() / dist; 
    230230 
    231         intersections.push_back(Intersection(dist, vssRay.mTerminationObject, 0)); 
     231        if (vssRay.mTerminationObject) 
     232                intersections.push_back(Intersection(dist, vssRay.mTerminationObject, 0)); 
    232233 
    233234        Precompute(); 
  • trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.h

    r440 r441  
    3737                return s; 
    3838        }   
     39 
     40        SimulationStatistics() 
     41        { 
     42                Reset(); 
     43        } 
    3944}; 
    4045 
  • trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp

    r440 r441  
    2727void 
    2828SamplingPreprocessor::SetupRay(Ray &ray,  
    29                                                                                                                         const Vector3 &point,  
    30                                                                                                                         const Vector3 &direction, 
     29                                                          const Vector3 &point,  
     30                                                          const Vector3 &direction, 
    3131                                                           const int type, 
    3232                                                           const Ray::Intersection &origin) 
     
    5757 
    5858  
    59 int 
     59void 
    6060SamplingPreprocessor::CastRays(const RayContainer &rays,  
    6161                                                           int &sampleContributions,  
    6262                                                           int &contributingSamples) 
    63         {        
     63{        
    6464        // cast ray to KD tree to find intersection with other objects 
    6565        RayContainer::const_iterator it, it_end = rays.end(); 
    6666  
    6767        for (it = rays.begin(); it != it_end; ++it) 
    68         mKdTree->CastRay((*it)); 
    69  
    70         sampleContributions = mViewCellsManager->CastRays(rays); 
     68        mKdTree->CastRay(*(*it)); 
     69 
     70        // for KD tree, intersecting view cells are already known, so no  
     71        // additional ray must be cast to them 
     72        const bool castRaysToViewCells =  
     73                (mViewCellsManager->GetType() != ViewCellsManager::KD); 
     74 
     75        mViewCellsManager->ComputeSampleContributions(rays,  
     76                                                                                                  castRaysToViewCells, 
     77                                                                                                  sampleContributions,  
     78                                                                                                  contributingSamples); 
    7179} 
    7280 
    7381int 
    74 SamplingPreprocessor::CastRay(const Ray &ray) 
     82SamplingPreprocessor::CastRay(Ray &ray) 
    7583{ 
    7684        // cast ray to KD tree to find intersection with other objects 
    7785        mKdTree->CastRay(ray); 
    7886 
    79         return mViewCellsManager->CastRay(ray); 
     87        bool castRayToViewCells = mViewCellsManager->GetType() != ViewCellsManager::KD; 
     88 
     89        return mViewCellsManager->ComputeSampleContributions(ray, castRayToViewCells); 
    8090} 
    8191 
     
    134144int 
    135145SamplingPreprocessor::CastEdgeSamples( 
    136                                                                                                                                                         Intersectable *object, 
    137                                                                                                                                                         const Vector3 &point, 
    138                                                                                                                                                         MeshInstance *mi, 
    139                                                                                                                                                         const int samples 
    140                                                                                                                                                         ) 
     146                                                                          Intersectable *object, 
     147                                                                          const Vector3 &point, 
     148                                                                          MeshInstance *mi, 
     149                                                                          const int samples 
     150                                                                          ) 
    141151{ 
    142152        Ray ray; 
     
    156166                float t = RandomValue(0.0f,1.0f); 
    157167                Vector3 target = t*poly.mVertices[edge] + (1.0f-t)*poly.mVertices[(edge + 1)% 
    158                                                                                                                                                                                                                                                                                  poly.mVertices.size()]; 
     168                        poly.mVertices.size()]; 
    159169                SetupRay(ray, point, target - point, Ray::LOCAL_RAY, Ray::Intersection(0, object, 0)); 
    160170                if (!mesh->CastRay(ray, mi)) { 
     
    288298                                for (int k=0; k < mSamplesPerPass; k++) { 
    289299                                        bool reverseSample = false; 
    290                                          
    291  
     300                                 
    292301                                        if (nodeToSample) { 
    293302                                                AxisAlignedBox3 box = mKdTree->GetBox(nodeToSample); 
     
    313322                                        Ray::Intersection source =  
    314323                                                Ray::Intersection(0, reverseSample ? NULL : object, faceIndex); 
    315                                          
     324                         
    316325                                        // construct a ray 
    317326                                        SetupRay(*ray, point, direction, Ray::LOCAL_RAY, source); 
     
    323332                                                passSampleContributions += sampleContributions; 
    324333                                        }*/ 
    325                  
    326                                         //------------------- 
    327                                         ProcessViewCells(ray, 
    328                                                                          objects, 
    329                                                                          passContributingSamples, 
    330                                                                          passSampleContributions); 
    331334                                } 
    332335                        } else { 
     
    340343                } 
    341344         
    342                 CastRays(passRays, passSampleContributions, passContributingSamples); 
     345                //------------------- 
     346                // cast rays for view cells construction 
     347                ProcessViewCells(passRays, 
     348                                                 objects, 
     349                                                 passSampleContributions, 
     350                                                 passContributingSamples); 
     351 
    343352                CLEAR_CONTAINER(passRays); 
    344                 passRays.clear(); 
    345  
     353                 
    346354                totalSamples += passSamples;  
    347355                 
     
    349357                //      HoleSamplingPass(); 
    350358     
    351                 mPass++; 
     359                mPass ++; 
    352360         
    353                         pvsSize += passSampleContributions; 
     361                pvsSize += passSampleContributions; 
    354362                 
    355363                float avgRayContrib = (passContributingSamples > 0) ?  
     
    393401        cout << "\nevaluating bsp view cells render time before merge ... "; 
    394402                  
    395         SimulationStatistics ss = mViewCellsManager->SimulateRendering(); 
     403        const SimulationStatistics ss = mViewCellsManager->SimulateRendering(); 
    396404                  
    397405        cout << " finished" << endl; 
     
    400408                  
    401409        mViewCellsManager->Visualize(objects, mSampleRays);      
    402    
    403   return true; 
    404 } 
    405  
    406 void SamplingPreprocessor::ProcessViewCells(const Ray &ray, 
     410     
     411        return true; 
     412} 
     413 
     414void SamplingPreprocessor::ProcessViewCells(const RayContainer &newRays, 
    407415                                                                                        const ObjectContainer &objects, 
    408                                                                                         int &contributingSamples, 
    409                                                                                         int &sampleContributions) 
    410 { 
     416                                                                                        int &sampleContributions, 
     417                                                                                        int &contributingSamples) 
     418{ 
     419        // cast rays to view cells 
     420        CastRays(newRays, sampleContributions, contributingSamples); 
     421 
    411422        // save rays for view cells construction 
    412423        if (!mViewCellsManager->ViewCellsConstructed()) 
     424        { 
     425                RayContainer::const_iterator it, it_end = newRays.end(); 
     426 
     427                if ((int)mVssSampleRays.size() < mViewCellsManager->GetConstructionSamples()) 
    413428                { 
    414                 if ((int)mSampleRays.size() < mViewCellsManager->GetConstructionSamples()) 
    415                         mVssSampleRays.push_back(new VssRay(ray)); 
     429                        for (it = newRays.begin(); it != it_end; ++ it) 
     430                                mVssSampleRays.push_back(new VssRay(*(*it))); 
     431                } 
    416432                else 
    417433                { 
    418434                        mViewCellsManager->Construct(objects, mVssSampleRays); 
     435         
    419436                        // construct view cells using the collected samples 
    420437                        cout << "building view cells from " << (int)mSampleRays.size() << " samples " << endl; 
     
    432449                         ((int)mSampleRays.size() < mViewCellsManager->GetVisualizationSamples())) 
    433450        { 
    434                 mSampleRays.push_back(new Ray(ray)); 
    435         } 
    436 } 
     451                RayContainer::const_iterator it, it_end = newRays.end(); 
     452 
     453                for (it = newRays.begin(); it != it_end; ++ it) 
     454                                mSampleRays.push_back(new Ray(*(*it))); 
     455        } 
     456} 
  • trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h

    r440 r441  
    3737 
    3838        /** Casts a bundle of sample rays into the scene. 
     39                @param sampleContributions the number of sample contributions 
     40                @param contributingSamples the number of samples contributing 
    3941        */ 
    40         int CastRays(const RayContainer &rays, 
    41                                  int &sampleContribtion,  
    42                                  int &contributingSamples); 
     42        void 
     43        CastRays(const RayContainer &rays, 
     44                         int &sampleContributions,  
     45                         int &contributingSamples); 
    4346         
    44         /** Casts a ray into the scene. 
     47        /** Casts a single ray into the scene. 
     48                @returns sample contributions 
    4549        */ 
    46         int 
    47         CastRay(const Ray &ray); 
     50        int CastRay(Ray &ray); 
    4851         
    4952        /** Verify if the exact visibility for this object was established. 
     
    5558                along the visibility skeleton. 
    5659        */ 
    57         int CastEdgeSamples( 
    58                                                                         Intersectable *object, 
    59                                                                         const Vector3 &point, 
    60                                                                         MeshInstance *mi, 
    61                                                                         const int samples 
    62                                                                         ); 
     60        int  
     61        CastEdgeSamples( 
     62                                        Intersectable *object, 
     63                                        const Vector3 &point, 
     64                                        MeshInstance *mi, 
     65                                        const int samples 
     66                                        ); 
    6367 
    6468        /** Processes the view cells during a pass. 
    65                 @param ray the current ray 
    66  
     69                @param newRays the newly cast rays 
     70                @param sampleContributions returns the accumulated contribution of the samples 
    6771                @param contributingSamples returns the samples contributing to pvs 
    68                 @param sampleContributions returns the accumulated  
    69                 contribution of the samples 
    70 */ 
     72        */ 
    7173         
    72         void ProcessViewCells(const Ray &ray, 
     74        void ProcessViewCells(const RayContainer &newRays, 
    7375                                                  const ObjectContainer &objects, 
    74                                                   int &contributingSamples, 
    75                                                   int &sampleContributions); 
     76                                                  int &sampleContributions, 
     77                                                  int &contributingSamples); 
    7678 
    7779        /** Adds objects samples to kd and bsp view cells. 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp

    r440 r441  
    263263/****************************************************************/ 
    264264 
    265 BspTree::BspTree(BspViewCell *viewCell):  
    266 mRootCell(viewCell),  
     265BspTree::BspTree():   
    267266mRoot(NULL),  
    268267mStoreLeavesWithRays(false), 
    269 mPvsUseArea(true), 
     268mPvsUseLen(false), 
    270269mGenerateViewCells(true) 
    271270{ 
    272271        Randomize(); // initialise random generator for heuristics 
     272 
     273        // the view cell corresponding to unbounded space 
     274        mRootCell = new BspViewCell(); 
    273275 
    274276        //-- termination criteria for autopartition 
     
    307309        environment->GetIntValue("BspTree.splitPlaneStrategy", mSplitPlaneStrategy); 
    308310        environment->GetFloatValue("BspTree.AxisAligned.splitBorder", mSplitBorder); 
     311        environment->GetIntValue("BspTree.maxTests", mMaxTests); 
    309312 
    310313        environment->GetFloatValue("BspTree.Construction.sideTolerance", Vector3::sDistTolerance); 
    311314        Vector3::sDistToleranceSqrt = Vector3::sDistTolerance * Vector3::sDistTolerance; 
    312315 
    313         // post processing stuff 
    314         environment->GetIntValue("ViewCells.PostProcessing.minPvsDif", mMinPvsDif); 
    315         environment->GetIntValue("ViewCells.PostProcessing.minPvs", mMinPvs); 
    316         environment->GetIntValue("ViewCells.PostProcessing.maxPvs", mMaxPvs); 
    317316 
    318317    Debug << "BSP max depth: " << mTermMaxDepth << endl; 
     
    410409{ 
    411410        DEL_PTR(mRoot); 
     411        DEL_PTR(mRootCell); 
     412} 
     413 
     414BspViewCell *BspTree::GetRootCell() const 
     415{ 
     416        return mRootCell; 
    412417} 
    413418 
     
    433438                mRoot = new BspLeaf(); 
    434439 
    435         tStack.push(BspTraversalData(mRoot, polys, 0, mRootCell, new BoundedRayContainer(), 0,  
    436                                                                  mBox.SurfaceArea(), new BspNodeGeometry())); 
     440        tStack.push(BspTraversalData(mRoot, polys, 0, mRootCell,  
     441                new BoundedRayContainer(), 0)); 
    437442 
    438443        while (!tStack.empty()) 
     
    470475                                                                                   mRootCell,    
    471476                                                                                   tData.mRays, 
    472                                                                                    tData.mPvs, 
    473                                                                                    mBox.SurfaceArea(), 
    474                                                                                    new BspNodeGeometry()); 
     477                                                                                   tData.mPvs); 
    475478 
    476479                                BspTraversalData backData(interior->GetBack(),  
     
    479482                                                                                  mRootCell,     
    480483                                                                                  tData.mRays, 
    481                                                                                   tData.mPvs, 
    482                                                                                   mBox.SurfaceArea(), 
    483                                                                                   new BspNodeGeometry()); 
     484                                                                                  tData.mPvs); 
    484485 
    485486                                if (!mGenerateViewCells) 
     
    735736 
    736737        BspTraversalData tData(mRoot, polys, 0, mRootCell, rays,  
    737                                                    ComputePvsSize(*rays), cell->GetArea(), cell); 
     738                                                   ComputePvsSize(*rays)); 
    738739 
    739740        tStack.push(tData); 
     
    755756        } 
    756757 
     758        Debug << "here2" << endl; 
    757759        cout << "finished\n"; 
    758760 
     
    766768                 ((int)data.mRays->size() <= mTermMinRays) || 
    767769                 (data.mPvs <= mTermMinPvs) || 
    768                  (data.mArea <= mTermMinArea) || 
    769770                 (data.mDepth >= mTermMaxDepth) || 
    770771                 (data.GetAvgRayContribution() < mTermMaxRayContribution)); 
     
    815816                DEL_PTR(tData.mPolygons); 
    816817                DEL_PTR(tData.mRays); 
    817                 DEL_PTR(tData.mGeometry); 
    818                  
     818                         
    819819                return leaf; 
    820820        } 
     
    824824         
    825825        BspTraversalData tFrontData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell,  
    826                                                                 new BoundedRayContainer(), 0, 0, new BspNodeGeometry()); 
     826                                                                new BoundedRayContainer(), 0); 
    827827        BspTraversalData tBackData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell,  
    828                                                            new BoundedRayContainer(), 0, 0, new BspNodeGeometry()); 
     828                                                           new BoundedRayContainer(), 0); 
    829829 
    830830        // create new interior node and two leaf nodes 
     
    860860        DEL_PTR(tData.mPolygons); 
    861861        DEL_PTR(tData.mRays); 
    862         DEL_PTR(tData.mGeometry);                
    863  
     862         
    864863        return interior; 
    865864} 
     
    901900         
    902901        BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode); 
     902 
     903        Debug << "*********************" << endl; 
     904        long startTime = GetTime(); 
     905         
    903906        // select subdivision plane 
    904907        BspInterior *interior =  
    905908                new BspInterior(SelectPlane(leaf, tData));  
    906  
     909        Debug << "time used for split plane selection: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
    907910#ifdef _DEBUG 
    908911        Debug << interior << endl; 
    909912#endif 
    910913         
     914 
     915        Debug << "number of rays: " << tData.mRays->size() << endl; 
     916        Debug << "number of polys: " << tData.mPolygons->size() << endl; 
     917 
     918        startTime = GetTime(); 
     919         
    911920        // subdivide rays into front and back rays 
    912921        SplitRays(interior->mPlane, *tData.mRays, *frontData.mRays, *backData.mRays); 
    913922         
     923        Debug << "time used for rays splitting: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
     924 
     925        startTime = GetTime(); 
    914926        // subdivide polygons with plane 
    915927        mStat.splits += interior->SplitPolygons(*tData.mPolygons,  
     
    918930                                                                                        coincident); 
    919931 
     932        Debug << "time used for polygon splitting: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
     933 
    920934    // compute pvs 
    921935        frontData.mPvs = ComputePvsSize(*frontData.mRays); 
    922936        backData.mPvs = ComputePvsSize(*backData.mRays); 
    923  
    924         // split geometry and compute area 
    925         if (1) 
    926         { 
    927                 tData.mGeometry->SplitGeometry(*frontData.mGeometry,  
    928                                                                            *backData.mGeometry,  
    929                                                                            *this,  
    930                                                                            interior->mPlane); 
    931          
    932                  
    933                 frontData.mArea = frontData.mGeometry->GetArea(); 
    934                 backData.mArea = backData.mGeometry->GetArea(); 
    935         } 
    936937 
    937938        // compute accumulated ray length 
     
    13081309        const int frontAndBackId = ViewCell::sMailId; 
    13091310 
    1310         PolygonContainer::const_iterator it, it_end = polys.end(); 
    1311  
    1312         for (it = polys.begin(); it != it_end; ++ it) 
    1313         { 
    1314                 const int classification = (*it)->ClassifyPlane(candidatePlane); 
     1311        bool useRand;; 
     1312        int limit; 
     1313 
     1314        // choose test polyongs randomly if over threshold 
     1315        if ((int)polys.size() > mMaxTests) 
     1316        { 
     1317                useRand = true; 
     1318                limit = mMaxTests; 
     1319        } 
     1320        else 
     1321        { 
     1322                useRand = false; 
     1323                limit = (int)polys.size(); 
     1324        } 
     1325         
     1326 
     1327        for (int i = 0; i < limit; ++ i) 
     1328        { 
     1329                const int testIdx = useRand ? Random(limit) : i; 
     1330 
     1331                Polygon3 *poly = polys[testIdx]; 
     1332 
     1333        const int classification = poly->ClassifyPlane(candidatePlane); 
    13151334 
    13161335                if (mSplitPlaneStrategy & BALANCED_POLYS) 
     
    13231342                { 
    13241343                        if (classification == Polygon3::COINCIDENT) 
    1325                                 sumPolyArea += (*it)->GetArea(); 
     1344                                sumPolyArea += poly->GetArea(); 
    13261345                        //totalArea += area; 
    13271346                } 
     
    13291348                if (mSplitPlaneStrategy & BLOCKED_RAYS) 
    13301349                { 
    1331                         const float blockedRays = (float)(*it)->mPiercingRays.size(); 
     1350                        const float blockedRays = (float)poly->mPiercingRays.size(); 
    13321351                 
    13331352                        if (classification == Polygon3::COINCIDENT) 
     
    13401359                if (mSplitPlaneStrategy & BALANCED_VIEW_CELLS) 
    13411360                { 
    1342                         MeshInstance *viewCell = (*it)->mParent; 
     1361                        MeshInstance *viewCell = poly->mParent; 
    13431362                 
    13441363                        // assure that we only count a view cell  
     
    14341453float BspTree::SplitPlaneCost(const Plane3 &candidatePlane,  
    14351454                                                          const BoundedRayContainer &rays, 
    1436                                                           const int pvs, 
    1437                                                           const float area, 
    1438                                                           const BspNodeGeometry &cell) const 
     1455                                                          const int pvs) const 
    14391456{ 
    14401457        float val = 0; 
     
    14421459        float sumBalancedRays = 0; 
    14431460        float sumRaySplits = 0; 
    1444          
    1445         int backId = 0; 
    1446         int frontId = 0; 
    1447         int frontAndBackId = 0; 
    14481461 
    14491462        int frontPvs = 0; 
     
    14511464 
    14521465        // probability that view point lies in child 
    1453         float pOverall = 0; 
    1454         float pFront = 0; 
    1455         float pBack = 0; 
     1466        float pOverall = 1; 
     1467        float pFront = 0.5; 
     1468        float pBack = 0.5; 
    14561469 
    14571470        if (mSplitPlaneStrategy & PVS) 
     
    14591472                // create unique ids for pvs heuristics 
    14601473                GenerateUniqueIdsForPvs(); 
    1461  
    1462                 if (mPvsUseArea) // use front and back cell areas to approximate volume 
    1463                 {        
    1464                         // construct child geometry with regard to the candidate split plane 
    1465                         BspNodeGeometry frontCell; 
    1466                         BspNodeGeometry backCell; 
    1467  
    1468                         cell.SplitGeometry(frontCell, backCell, *this, candidatePlane); 
    1469                  
    1470                         pFront = frontCell.GetArea(); 
    1471                         pBack = backCell.GetArea(); 
    1472  
    1473                         pOverall = area; 
    1474                 } 
    14751474        } 
    14761475                         
    1477         BoundedRayContainer::const_iterator rit, rit_end = rays.end(); 
    1478  
    1479         for (rit = rays.begin(); rit != rays.end(); ++ rit) 
    1480         { 
    1481                 Ray *ray = (*rit)->mRay; 
    1482                 const float minT = (*rit)->mMinT; 
    1483                 const float maxT = (*rit)->mMaxT; 
     1476        bool useRand;; 
     1477        int limit; 
     1478 
     1479        // choose test polyongs randomly if over threshold 
     1480        if ((int)rays.size() > mMaxTests) 
     1481        { 
     1482                useRand = true; 
     1483                limit = mMaxTests; 
     1484        } 
     1485        else 
     1486        { 
     1487                useRand = false; 
     1488                limit = (int)rays.size(); 
     1489        } 
     1490 
     1491        for (int i = 0; i < limit; ++ i) 
     1492        { 
     1493                const int testIdx = useRand ? Random(limit) : i; 
     1494         
     1495                BoundedRay *bRay = rays[testIdx]; 
     1496 
     1497                Ray *ray = bRay->mRay; 
     1498                const float minT = bRay->mMinT; 
     1499                const float maxT = bRay->mMaxT; 
    14841500 
    14851501                Vector3 entP, extP; 
     
    15111527                        AddObjToPvs(ray->sourceObject.mObject, cf, frontPvs, backPvs); 
    15121528                         
    1513                         if (!mPvsUseArea) // use front and back cell areas to approximate volume 
    1514                         {        
    1515                                 float len = Distance(entP, extP); 
    1516                                 pOverall += len; 
    1517  
    1518                                 // use length of rays to approximate volume 
    1519                                 switch (cf) 
     1529                        float len = 1; 
     1530                        if (0){ 
     1531                        if (mPvsUseLen) 
     1532                                len = SqrDistance(entP, extP); 
     1533                                                         
     1534                        // use length of rays to approximate volume 
     1535                        if (Ray::BACK && Ray::COINCIDENT) 
     1536                                pBack += len; 
     1537                        if (Ray::FRONT && Ray::COINCIDENT) 
     1538                                pFront += len; 
     1539                        if (Ray::FRONT_BACK || Ray::BACK_FRONT) 
     1540                        { 
     1541                                if (mPvsUseLen) 
    15201542                                { 
    1521                                         case Ray::COINCIDENT: 
    1522                                                 pBack += len; 
    1523                                                 pFront += len;                                           
    1524                                                 break; 
    1525                                         case Ray::BACK: 
    1526                                                 pBack += len; 
    1527                                                 break; 
    1528                                         case Ray::FRONT: 
    1529                                                 pFront += len; 
    1530                                                 break; 
    1531                                         case Ray::FRONT_BACK: 
    1532                                                 { 
    1533                                                         // find intersection of ray segment with plane 
    1534                                                         const Vector3 extp = ray->Extrap(maxT); 
    1535                                                         const float t = candidatePlane.FindT(ray->GetLoc(), extp); 
    1536                                          
    1537                                                         const float newT = t * maxT; 
    1538                                                         float newLen = Distance(ray->Extrap(newT), extp); 
    1539  
    1540                                                         pFront += len - newLen; 
    1541                                                         pBack += newLen; 
    1542                                                 } 
    1543                                                 break; 
    1544                                         case Ray::BACK_FRONT: 
    1545                                                 { 
    1546                                                         // find intersection of ray segment with plane 
    1547                                                         const Vector3 extp = ray->Extrap(maxT); 
    1548                                                         const float t = candidatePlane.FindT(ray->GetLoc(), extp); 
    1549                                          
    1550                                                         const float newT = t * maxT; 
    1551                                                         float newLen = Distance(ray->Extrap(newT), extp); 
    1552  
    1553                                                         pFront += len; 
    1554                                                         pBack += len - newLen; 
    1555                                                 } 
    1556                                                 break; 
    1557                                         default: 
    1558                                                 Debug << "Should not come here 2" << endl; 
    1559                                                 break; 
     1543                                        const Vector3 extp = ray->Extrap(maxT); 
     1544                                        const float t = candidatePlane.FindT(ray->GetLoc(), extp); 
     1545                                 
     1546                                        const float newT = t * maxT; 
     1547                                        const float newLen = SqrDistance(ray->Extrap(newT), extp); 
     1548 
     1549                                        if (Ray::FRONT_BACK) 
     1550                                        { 
     1551                                                pFront += len - newLen; 
     1552                                                pBack += newLen; 
     1553                                        } 
     1554                                        else 
     1555                                        { 
     1556                                                pBack += len - newLen; 
     1557                                                pFront += newLen; 
     1558                                        } 
    15601559                                } 
    1561                         } 
     1560                                else 
     1561                                { 
     1562                                        ++ pFront; 
     1563                                        ++ pBack; 
     1564                                } 
     1565                        }} 
    15621566                } 
    15631567        } 
     
    15721576 
    15731577        float denom = pOverall * (float)pvs * 2.0f + Limits::Small; 
    1574         if ((mSplitPlaneStrategy & PVS) && area && pvs) 
     1578 
     1579        if (mSplitPlaneStrategy & PVS) 
    15751580        { 
    15761581                val += mPvsFactor * (frontPvs * pFront + (backPvs * pBack)) / denom; 
    15771582 
    15781583                // give penalty to unbalanced split 
    1579                 if (0) 
     1584                if (1) 
    15801585                if (((pFront * 0.2 + Limits::Small) > pBack) || (pFront < (pBack * 0.2 + Limits::Small))) 
    15811586                        val += 0.5; 
    15821587        } 
    15831588 
     1589         
    15841590#ifdef _DEBUG 
    15851591        Debug << "totalpvs: " << pvs << " ptotal: " << pOverall 
     
    16641670                (mSplitPlaneStrategy & PVS)) 
    16651671        { 
    1666                 val += SplitPlaneCost(candidatePlane, *data.mRays, data.mPvs,  
    1667                                                           data.mArea, *data.mGeometry); 
     1672                val += SplitPlaneCost(candidatePlane, *data.mRays, data.mPvs); 
    16681673        } 
    16691674 
     
    17361741                ++ mStat.maxRayContribNodes; 
    17371742         
    1738         if (data.mGeometry->GetArea() <= mTermMinArea)  
    1739                 ++ mStat.minAreaNodes; 
     1743        //if (data.mGeometry->GetArea() <= mTermMinArea)  
     1744        //      ++ mStat.minAreaNodes; 
    17401745 
    17411746#ifdef _DEBUG 
     
    19501955        } 
    19511956} 
    1952  
    1953 bool BspTree::MergeViewCells(BspLeaf *front, BspLeaf *back) const 
    1954 { 
    1955         BspViewCell *viewCell =  
    1956 NULL;//todo             dynamic_cast<BspViewCell *>(ViewCell::Merge(*front->mViewCell, *back->mViewCell)); 
    1957          
    1958         if (!viewCell) 
    1959                 return false; 
    1960  
    1961         // change view cells of all leaves associated with the 
    1962         // previous view cells 
    1963  
    1964         BspViewCell *fVc = front->mViewCell; 
    1965         BspViewCell *bVc = back->mViewCell; 
    1966  
    1967         vector<BspLeaf *> fLeaves = fVc->mBspLeaves; 
    1968         vector<BspLeaf *> bLeaves = bVc->mBspLeaves; 
    1969  
    1970         vector<BspLeaf *>::const_iterator it; 
    1971          
    1972         for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) 
    1973         { 
    1974                 (*it)->SetViewCell(viewCell); 
    1975                 viewCell->mBspLeaves.push_back(*it); 
    1976         } 
    1977         for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) 
    1978         { 
    1979                 (*it)->SetViewCell(viewCell); 
    1980                 viewCell->mBspLeaves.push_back(*it); 
    1981         } 
    1982          
    1983         DEL_PTR(fVc); 
    1984         DEL_PTR(bVc); 
    1985  
    1986         return true; 
    1987 } 
    1988  
    1989 bool BspTree::ShouldMerge(BspLeaf *front, BspLeaf *back) const 
    1990 { 
    1991         ViewCell *fvc = front->mViewCell; 
    1992         ViewCell *bvc = back->mViewCell; 
    1993  
    1994         if ((fvc == mRootCell) || (bvc == mRootCell) || (fvc == bvc)) 
    1995                 return false; 
    1996  
    1997         int fdiff = fvc->GetPvs().Diff(bvc->GetPvs()); 
    1998  
    1999         if (fvc->GetPvs().GetSize() + fdiff < mMaxPvs) 
    2000         { 
    2001                 if ((fvc->GetPvs().GetSize() < mMinPvs) ||       
    2002                         (bvc->GetPvs().GetSize() < mMinPvs) || 
    2003                         ((fdiff < mMinPvsDif) && (bvc->GetPvs().Diff(fvc->GetPvs()) < mMinPvsDif))) 
    2004                 { 
    2005                         return true; 
    2006                 } 
    2007         } 
    2008          
    2009         return false; 
    2010 } 
    2011  
    20121957 
    20131958BspTreeStatistics &BspTree::GetStat() 
     
    24572402                                        if (frontPoly->Valid()) 
    24582403                                                front.mPolys.push_back(frontPoly); 
     2404                                        else 
     2405                                                DEL_PTR(frontPoly); 
     2406 
    24592407                                        if (backPoly->Valid()) 
    24602408                                                back.mPolys.push_back(backPoly); 
     2409                                        else 
     2410                                                DEL_PTR(backPoly); 
    24612411                                } 
    24622412                                 
     
    25102460 
    25112461                                        planePoly->Split(plane, *frontPoly, *backPoly); 
     2462                                         
     2463                                        // don't need anymore 
    25122464                                        DEL_PTR(planePoly); 
    2513  
     2465                                        DEL_PTR(frontPoly); 
     2466 
     2467                                        // back polygon is belonging to geometry 
    25142468                                        if (backPoly->Valid()) 
    25152469                                                planePoly = backPoly; 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h

    r440 r441  
    364364                /// rays piercing this node 
    365365                BoundedRayContainer *mRays; 
    366                 /// area of current node 
    367                 float mArea; 
    368                 BspNodeGeometry *mGeometry; 
    369  
     366                 
    370367                /// pvs size 
    371368                int mPvs; 
     
    385382                mViewCell(NULL), 
    386383                mRays(NULL), 
    387                 mPvs(0), 
    388                 mArea(0.0), 
    389                 mGeometry(NULL) 
     384                mPvs(0) 
    390385                {} 
    391386                 
     
    395390                                                 ViewCell *viewCell, 
    396391                                                 BoundedRayContainer *rays, 
    397                                                  int pvs, 
    398                                                  float area, 
    399                                                  BspNodeGeometry *cell):  
     392                                                 int pvs):  
    400393                mNode(node),  
    401394                mPolygons(polys),  
     
    403396                mViewCell(viewCell), 
    404397                mRays(rays), 
    405                 mPvs(pvs), 
    406                 mArea(area), 
    407                 mGeometry(cell) 
     398                mPvs(pvs) 
    408399                {} 
    409400    }; 
     
    412403 
    413404        /** Default constructor creating an empty tree. 
    414                 @param viewCell view cell corresponding to unbounded space 
    415405        */  
    416         BspTree(BspViewCell *viewCell); 
    417  
     406        BspTree(); 
     407         
    418408        ~BspTree(); 
    419409 
     410         
    420411        const BspTreeStatistics &GetStatistics() const;  
    421412   
     
    506497        BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false); 
    507498 
    508         /** Returns true if merge criteria are reached. 
    509         */ 
    510     bool ShouldMerge(BspLeaf *front, BspLeaf *back) const; 
    511  
    512         /** Merges view cells based on some criteria 
    513             E.g., empty view cells can pe purged, view cells which have  
    514                 a very similar PVS can be merged to one larger view cell. 
    515  
    516                 @returns true if merge was successful. 
    517         */ 
    518         bool MergeViewCells(BspLeaf *front, BspLeaf *back) const; 
    519  
    520499        /** Traverses tree and counts all view cells as well as their PVS size. 
    521500        */ 
    522501        void EvaluateViewCellsStats(BspViewCellsStatistics &stat) const; 
     502 
     503        /** Returns view cell corresponding to unbounded space. 
     504        */ 
     505        BspViewCell *GetRootCell() const; 
    523506 
    524507        /** Parses the environment and stores the global BSP tree parameters 
     
    601584        float SplitPlaneCost(const Plane3 &candidatePlane,  
    602585                                                 const BoundedRayContainer &rays, 
    603                                                  const int pvs, 
    604                                                  const float area, 
    605                                                  const BspNodeGeometry &cell) const; 
     586                                                 const int pvs) const; 
    606587 
    607588        /** Filters next view cell down the tree and inserts it into the appropriate leaves 
     
    834815        /// number of candidates for split planes evaluated using the rays 
    835816        int mMaxRayCandidates; 
    836          
     817        /// maximum tests for split plane evaluation with a single candidate 
     818        int mMaxTests; 
     819 
    837820        float mCtDivCi; 
    838821 
     
    856839        float mBalancedViewCellsFactor; 
    857840 
    858         //-- thresholds used for view cells merge 
    859         int mMinPvsDif; 
    860         int mMinPvs; 
    861         int mMaxPvs; 
    862841        /// if area or accumulated ray lenght should be used for PVS heuristics 
    863         bool mPvsUseArea; 
     842        bool mPvsUseLen; 
    864843 
    865844private: 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp

    r440 r441  
    1313 
    1414 
    15  
    1615ViewCellsManager::ViewCellsManager(): 
    1716mRenderSimulator(NULL), 
     
    2019mVisualizationSamples(0) 
    2120{ 
    22     mUnbounded = GenerateViewCell(); 
     21        // post processing stuff 
     22        environment->GetIntValue("ViewCells.PostProcessing.minPvsDif", mMinPvsDif); 
     23        environment->GetIntValue("ViewCells.PostProcessing.minPvs", mMinPvs); 
     24        environment->GetIntValue("ViewCells.PostProcessing.maxPvs", mMaxPvs); 
    2325} 
    2426 
     
    2931mVisualizationSamples(0) 
    3032{ 
    31         mUnbounded = GenerateViewCell(); 
     33        // post processing stuff 
     34        environment->GetIntValue("ViewCells.PostProcessing.minPvsDif", mMinPvsDif); 
     35        environment->GetIntValue("ViewCells.PostProcessing.minPvs", mMinPvs); 
     36        environment->GetIntValue("ViewCells.PostProcessing.maxPvs", mMaxPvs); 
    3237} 
    3338 
    3439ViewCellsManager::~ViewCellsManager() 
    3540{ 
    36         DEL_PTR(mUnbounded); 
    3741        DEL_PTR(mRenderSimulator); 
    3842} 
     
    6367} 
    6468 
    65 int ViewCellsManager::CastRays(const RayContainer &rays) 
    66 { 
    67         int sampleContributions = 0; 
     69void ViewCellsManager::ComputeSampleContributions(const RayContainer &rays,  
     70                                                                                                  const bool castRays, 
     71                                                                                                  int &sampleContributions, 
     72                                                                                                  int &contributingSamples) 
     73{ 
     74        // view cells not yet constructed 
     75        if (!ViewCellsConstructed()) 
     76                return; 
    6877 
    6978        RayContainer::const_iterator it, it_end = rays.end(); 
    7079 
    7180    for (it = rays.begin(); it != it_end; ++ it) 
    72                 sampleContributions += CastRay(*(*it)); 
    73  
    74         return sampleContributions; 
     81        { 
     82                sampleContributions +=ComputeSampleContributions(*(*it), castRays); 
     83                contributingSamples += sampleContributions > 0; 
     84        } 
    7585} 
    7686 
     
    139149} 
    140150 
    141 ViewCell *ViewCellsManager::Merge(ViewCell &front, ViewCell &back) const 
     151ViewCell *ViewCellsManager::MergeViewCells(ViewCell &front, ViewCell &back) const 
    142152{ 
    143153        ViewCell *vc = GenerateViewCell(); 
     
    158168SimulationStatistics ViewCellsManager::SimulateRendering() const 
    159169{ 
     170        if (!ViewCellsConstructed()) 
     171                return SimulationStatistics(); 
     172 
    160173        return mRenderSimulator->SimulateRendering(); 
    161174} 
     
    223236                                                                   AxisAlignedBox3 *sceneBbox) 
    224237{ 
     238        // if view cells were already constructed 
    225239        if (ViewCellsConstructed()) 
    226240                return 0; 
    227241 
     242        Debug << "Constructing bsp view cells" << endl; 
     243 
    228244        int sampleContributions = 0; 
    229245         
     
    235251                sampleRays.push_back(new Ray(*rays[i])); 
    236252 
    237         mBspTree->Construct(objects, sampleRays); 
    238          
     253        if (mViewCells.empty()) // no view cells loaded 
     254                mBspTree->Construct(objects, sampleRays); 
     255        else 
     256                mBspTree->Construct(mViewCells); 
     257 
    239258        Debug << mBspTree->GetStatistics() << endl; 
    240259        CLEAR_CONTAINER(sampleRays); 
     
    243262} 
    244263 
    245 int BspViewCellsManager::CastRay(Ray &ray) 
    246 { 
    247         mBspTree->CastRay(ray); 
     264int BspViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay) 
     265{ 
     266        // view cells not yet constructed 
     267        if (!ViewCellsConstructed()) 
     268                return 0; 
     269 
     270        int contributingSamples = 0; 
     271 
     272        if (castRay) 
     273                mBspTree->CastRay(ray); 
    248274         
    249275        //if (mBspTree->bspIntersections.empty()) return 0; 
    250276 
    251         Intersectable *term =  
     277        Intersectable *tObject =  
    252278                !ray.intersections.empty() ? ray.intersections[0].mObject : NULL; 
    253279 
    254         return AddSampleContributions(ray, ray.sourceObject.mObject, term); 
     280        Intersectable *sObject = ray.sourceObject.mObject; 
     281 
     282        if (sObject || tObject) 
     283        { 
     284                // object can be seen from the view cell => add to view cell pvs 
     285                for (int j = 0; j < (int)ray.bspIntersections.size(); ++ j) 
     286                {        
     287                        BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 
     288                 
     289                        // if ray not in unbounded space 
     290                        if (leaf->GetViewCell() != mBspTree->GetRootCell()) 
     291                        { 
     292                                if (sObject) 
     293                                { 
     294                                        contributingSamples +=  
     295                                                leaf->GetViewCell()->GetPvs().AddSample(sObject); 
     296                                } 
     297 
     298                                if (tObject) 
     299                                { 
     300                                        contributingSamples +=  
     301                                                leaf->GetViewCell()->GetPvs().AddSample(tObject); 
     302                                } 
     303                        } 
     304                } 
     305        } 
     306 
     307        // rays passing through this viewcell 
     308        if (0) 
     309                for (int j = 1; j < ((int)ray.bspIntersections.size() - 1); ++ j)  
     310                { 
     311                        BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 
     312 
     313                        if (leaf->GetViewCell() != mBspTree->GetRootCell()) 
     314                                leaf->GetViewCell()-> 
     315                                        AddPassingRay(ray, contributingSamples ? 1 : 0); 
     316                } 
     317 
     318        return contributingSamples; 
    255319} 
    256320 
     
    258322                                                                         const RayContainer &rays) 
    259323{ 
     324        if (!ViewCellsConstructed()) 
     325        { 
     326                Debug << "view cells not constructed" << endl; 
     327                return 0; 
     328        } 
     329 
     330 
    260331        //-- post processing of bsp view cells 
    261332    int vcSize = 0; 
     
    277348                        //exporter->ExportBspViewCellPartition(*mBspTree, 0); 
    278349                                 
    279                         if (1) 
     350                        if (0) 
    280351                        { 
    281352                                Material m;//= RandomMaterial(); 
     
    323394                        BspLeaf *leaf = (*iit).mLeaf; 
    324395 
    325                         if (mBspTree->ShouldMerge(leaf, previousLeaf)) 
     396                        if (ShouldMerge(leaf, previousLeaf)) 
    326397                        {                        
    327                                 mBspTree->MergeViewCells(leaf, previousLeaf); 
     398                                MergeBspLeafViewCells(leaf, previousLeaf); 
    328399 
    329400                                ++ merged; 
     
    347418} 
    348419 
    349 int BspViewCellsManager::AddSampleContributions(const Ray &ray, 
    350                                                                                                 Intersectable *sObject, 
    351                                                                                                 Intersectable *tObject) 
    352 { 
    353         int contributingSamples = 0; 
    354          
    355         // rays passing through this viewcell 
    356         if (0) 
    357                 for (int j = 1; j < ((int)ray.bspIntersections.size() - 1); ++ j)  
    358                 { 
    359                         BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 
    360  
    361                         if (leaf->GetViewCell() != mUnbounded) 
    362                                 leaf->GetViewCell()-> 
    363                                         AddPassingRay(ray, contributingSamples ? 1 : 0); 
    364                 } 
    365  
    366         if (!sObject && !tObject) 
    367                 return 0; 
    368  
    369         // object can be seen from the view cell => add to view cell pvs 
    370         for (int j = 0; j < (int)ray.bspIntersections.size(); ++ j) 
    371         {        
    372                 BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 
    373                  
    374                 // if ray not in unbounded space 
    375                 if (leaf->GetViewCell() != mUnbounded) 
    376                 { 
    377                         if (sObject) 
    378                         { 
    379                                 contributingSamples +=  
    380                                         leaf->GetViewCell()->GetPvs().AddSample(sObject); 
    381                         } 
    382  
    383                         if (tObject) 
    384                         { 
    385                                 contributingSamples +=  
    386                                         leaf->GetViewCell()->GetPvs().AddSample(tObject); 
    387                         } 
    388                 } 
    389         } 
    390   
    391         return contributingSamples; 
    392 } 
    393  
    394420 
    395421void BspViewCellsManager::Visualize(const ObjectContainer &objects, 
    396422                                                                        const RayContainer &sampleRays) 
    397423{ 
     424        if (!ViewCellsConstructed()) 
     425                return; 
     426 
    398427        //-- recount pvs 
    399428        BspViewCellsStatistics vcStats; 
     
    473502                } 
    474503 
    475                 if (1) 
     504                if (0) 
    476505                        exporter->ExportGeometry(objects); 
    477506 
     
    681710} 
    682711 
     712 
     713bool BspViewCellsManager::MergeBspLeafViewCells(BspLeaf *front, BspLeaf *back) const 
     714{ 
     715        BspViewCell *viewCell =  
     716                dynamic_cast<BspViewCell *>(MergeViewCells(*front->GetViewCell(), *back->GetViewCell())); 
     717         
     718        if (!viewCell) 
     719                return false; 
     720 
     721        // change view cells of all leaves associated with the 
     722        // previous view cells 
     723 
     724        BspViewCell *fVc = front->GetViewCell(); 
     725        BspViewCell *bVc = back->GetViewCell(); 
     726 
     727        vector<BspLeaf *> fLeaves = fVc->mBspLeaves; 
     728        vector<BspLeaf *> bLeaves = bVc->mBspLeaves; 
     729 
     730        vector<BspLeaf *>::const_iterator it; 
     731         
     732        for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) 
     733        { 
     734                (*it)->SetViewCell(viewCell); 
     735                viewCell->mBspLeaves.push_back(*it); 
     736        } 
     737        for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) 
     738        { 
     739                (*it)->SetViewCell(viewCell); 
     740                viewCell->mBspLeaves.push_back(*it); 
     741        } 
     742         
     743        DEL_PTR(fVc); 
     744        DEL_PTR(bVc); 
     745 
     746        return true; 
     747} 
     748 
     749bool BspViewCellsManager::ShouldMerge(BspLeaf *front, BspLeaf *back) const 
     750{ 
     751        ViewCell *fvc = front->GetViewCell(); 
     752        ViewCell *bvc = back->GetViewCell(); 
     753 
     754        if ((fvc == mBspTree->GetRootCell()) || (bvc == mBspTree->GetRootCell()) || (fvc == bvc)) 
     755                return false; 
     756 
     757        int fdiff = fvc->GetPvs().Diff(bvc->GetPvs()); 
     758 
     759        if (fvc->GetPvs().GetSize() + fdiff < mMaxPvs) 
     760        { 
     761                if ((fvc->GetPvs().GetSize() < mMinPvs) ||       
     762                        (bvc->GetPvs().GetSize() < mMinPvs) || 
     763                        ((fdiff < mMinPvsDif) && (bvc->GetPvs().Diff(fvc->GetPvs()) < mMinPvsDif))) 
     764                { 
     765                        return true; 
     766                } 
     767        } 
     768         
     769        return false; 
     770} 
     771 
     772 
     773 
    683774/**********************************************************************/ 
    684775/*                   KdViewCellsManager implementation               */ 
     
    698789                                                                  AxisAlignedBox3 *sceneBbox) 
    699790{ 
     791        // if view cells already constructed 
    700792        if (ViewCellsConstructed()) 
    701793                return 0; 
     794 
     795        Debug << "Constructing bsp view cells" << endl; 
    702796         
    703797        mKdTree->Construct(); 
     
    720814                                                                   const RayContainer &sampleRays) 
    721815{ 
     816        if (!ViewCellsConstructed()) 
     817                return; 
     818 
    722819        const int pvsOut = min((int)objects.size(), 10); 
    723820 
     
    801898} 
    802899 
    803 int KdViewCellsManager::CastRay(const Ray &ray) 
    804 { 
    805         if (ray.kdLeaves.empty())  
     900int KdViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay) 
     901{ 
     902        // view cells not yet constructed 
     903        if (!ViewCellsConstructed()) 
    806904                return 0; 
    807905 
    808         Intersectable *terminator = 
     906        if (castRay) 
     907                mKdTree->CastRay(ray); 
     908 
     909        if (ray.kdLeaves.empty()) 
     910                return 0; 
     911 
     912        Intersectable *tObject = 
    809913                !ray.intersections.empty() ? ray.intersections[0].mObject : NULL; 
    810914 
    811         return AddSampleContributions(ray, ray.sourceObject.mObject, terminator); 
    812 } 
    813  
    814 int KdViewCellsManager::AddSampleContributions(const Ray &ray, 
    815                                                                                            Intersectable *sObject, 
    816                                                                                            Intersectable *tObject) 
    817 { 
     915        Intersectable *sObject = 
     916                ray.sourceObject.mObject; 
     917 
    818918        int contributingSamples = 0; 
    819919         
     
    843943                        contributingSamples += tObject->mKdPvs.AddSample(node); 
    844944        } 
    845                  
     945 
    846946        return contributingSamples; 
    847947} 
     948 
    848949 
    849950KdNode *KdViewCellsManager::GetNodeForPvs(KdLeaf *leaf) 
     
    874975                                                                         AxisAlignedBox3 *sceneBbox) 
    875976{ 
     977        // if view cells already constructed 
    876978        if (ViewCellsConstructed()) 
    877979                return 0; 
     
    886988} 
    887989 
    888 int VspKdViewCellsManager::CastRay(const Ray &rays) 
    889 { 
     990int VspKdViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay) 
     991{ 
     992        // view cells not yet constructed 
     993        if (!ViewCellsConstructed()) 
     994                return 0; 
     995 
     996        //if (castRay) // TODO 
     997 
    890998        int sampleContributions = 0; 
     999 
    8911000/* 
    8921001    for (int i = 0; i < (int)rays.size(); ++ i) 
     
    9101019                                                                           const RayContainer &rays) 
    9111020{ 
     1021        if (!ViewCellsConstructed()) 
     1022                return 0; 
     1023 
    9121024        return 0; 
    9131025} 
     
    9161028                                                                          const RayContainer &sampleRays) 
    9171029{ 
     1030        if (!ViewCellsConstructed()) 
     1031                return; 
     1032 
    9181033        if (1) 
    9191034        { 
     
    10061121        return VSP_KD; 
    10071122} 
    1008  
    1009 int VspKdViewCellsManager::AddSampleContributions(const Ray &ray, 
    1010                                                                                                 Intersectable *sObject, 
    1011                                                                                                 Intersectable *tObject) 
    1012 { 
    1013         int contributingSamples = 0; 
    1014         /* 
    1015         // rays passing through this viewcell 
    1016         if (0) 
    1017                 for (int j = 1; j < ((int)ray.bspIntersections.size() - 1); ++ j)  
    1018                 { 
    1019                         BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 
    1020  
    1021                         if (leaf->GetViewCell() != mUnbounded) 
    1022                                 leaf->GetViewCell()-> 
    1023                                         AddPassingRay(ray, contributingSamples ? 1 : 0); 
    1024                 } 
    1025  
    1026         if (!sObject && !tObject) 
    1027                 return 0; 
    1028          
    1029         // object can be seen from the view cell => add to view cell pvs 
    1030         for (j=0; j < (int)ray.bspIntersections.size(); ++ j) 
    1031         {        
    1032                 BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 
    1033                  
    1034                 // if ray not in unbounded space 
    1035                 if (leaf->GetViewCell() != mUnbounded) 
    1036                 { 
    1037                         if (sObject) 
    1038                         { 
    1039                                 contributingSamples +=  
    1040                                         leaf->GetViewCell()->GetPvs().AddSample(sObject); 
    1041                         } 
    1042  
    1043                         if (tObject) 
    1044                         { 
    1045                                 contributingSamples +=  
    1046                                         leaf->GetViewCell()->GetPvs().AddSample(tObject); 
    1047                         } 
    1048                 } 
    1049         } 
    1050  */ 
    1051         return contributingSamples; 
    1052 } 
  • trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h

    r440 r441  
    4545                                                  AxisAlignedBox3 *sceneBbox = NULL) = 0; 
    4646 
    47         /** Casts bundle of rays to the view cells and stores contributions in PVS. 
    48         */ 
    49         int CastRays(const RayContainer &rays); 
    50  
    51         /** Casts ray to the view cells and stores contributions in PVS. 
    52         */ 
    53         virtual int CastRay(const Ray &ray) = 0; 
     47        /** Computes sample contributions of the rays to the view cells PVS. 
     48         
     49                @param rays bundle of rays used to find intersections with view cells and  
     50                adding the contribution 
     51                @param castRays true if ray should be cast to gain the information, false if rays 
     52                already hold the view cells intersection information and need not be recast. 
     53                @param sampleContributions returns the number of sample contributions 
     54                @param contributingSamples returns the number of contributingSamples 
     55        */ 
     56        void  ComputeSampleContributions(const RayContainer &rays,  
     57                                                                         const bool castRays, 
     58                                                                         int &sampleContributions, 
     59                                                                         int &contributingSamples); 
     60 
     61 
     62        /** Computes sample contribution of a simgle ray to the view cells PVS. 
     63                @param ray finds intersections with view cells and holds the contribution 
     64                @param castRay true if ray should be cast to gain the information, false if ray 
     65                is already holding the information and need not be recast. 
     66 
     67                @returns number of sample contributions 
     68        */ 
     69        virtual int ComputeSampleContributions(Ray &ray, const bool castRay = true) = 0; 
    5470 
    5571        /** Post processes view cells givemŽa number of rays. 
     
    91107                @returns new view cell based on the merging. 
    92108        */ 
    93         ViewCell *Merge(ViewCell &front, ViewCell &back) const; 
     109        ViewCell *MergeViewCells(ViewCell &front, ViewCell &back) const; 
    94110         
    95111        /** Generates view cell of type specified by this manager 
     
    136152        virtual bool ViewCellsConstructed() const = 0; 
    137153         
     154         
     155 
    138156protected: 
    139         /** Adds sample constributions to the view cell PVS. 
    140                 @returns number of sample contributions 
    141         */ 
    142         virtual int AddSampleContributions(const Ray &ray, 
    143                                                                            Intersectable *sObject, 
    144                                                                            Intersectable *tObject) = 0; 
     157         
     158         
    145159 
    146160        /** Initialises the render time simulator. 
     
    163177        int mPostProcessSamples; 
    164178        int mVisualizationSamples; 
     179 
     180        //-- thresholds used for view cells merge 
     181        int mMinPvsDif; 
     182        int mMinPvs; 
     183        int mMaxPvs; 
    165184}; 
     185 
     186 
    166187 
    167188/** 
     
    179200                                  AxisAlignedBox3 *sceneBbox); 
    180201 
    181         int CastRay(const Ray &rays); 
     202        int ComputeSampleContributions(Ray &ray, const bool castRay = false); 
    182203 
    183204        int PostProcess(const ObjectContainer &objects,  
     
    189210        int GetType() const; 
    190211         
    191         /** Generates view cell of type specified by this manager 
    192         */ 
    193212        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const; 
    194213 
     
    197216protected: 
    198217 
    199         int     AddSampleContributions(const Ray &ray, Intersectable *sObject, Intersectable *tObject); 
     218        /** Merges view cells front and back leaf view cell. 
     219        */ 
     220        bool MergeBspLeafViewCells(BspLeaf *front, BspLeaf *back) const; 
     221 
     222        /** Returns true if front and back leaf should be merged. 
     223        */ 
     224        bool ShouldMerge(BspLeaf *front, BspLeaf *back) const; 
    200225 
    201226        /// the BSP tree. 
     
    247272         KdNode *GetNodeForPvs(KdLeaf *leaf); 
    248273 
    249          int AddSampleContributions(const Ray &ray, Intersectable *sObject, Intersectable *tObject); 
    250  
    251         /// the BSP tree. 
    252         KdTree *mKdTree; 
    253  
    254         /// depth of the KD tree nodes with represent the view cells 
    255         int mKdPvsDepth; 
     274         int ComputeSampleContributions(Ray &ray, const bool castRay = true); 
     275 
     276         /// the BSP tree. 
     277         KdTree *mKdTree; 
     278 
     279         /// depth of the KD tree nodes with represent the view cells 
     280         int mKdPvsDepth; 
    256281}; 
    257282 
     
    271296                                  AxisAlignedBox3 *sceneBbox); 
    272297 
    273         int CastRay(const Ray &rays); 
     298        int ComputeSampleContributions(Ray &ray, const bool castRay = true); 
    274299 
    275300        int PostProcess(const ObjectContainer &objects,  
     
    285310 
    286311protected: 
    287  
    288         int     AddSampleContributions(const Ray &ray, Intersectable *sObject, Intersectable *tObject); 
    289312 
    290313        /// the BSP tree. 
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp

    r439 r441  
    222222mRoot(NULL),  
    223223mStoreLeavesWithRays(false), 
    224 mPvsUseArea(true) 
     224mPvsUseLen(true) 
    225225{ 
    226226        Randomize(); // initialise random generator for heuristics 
     
    485485 
    486486        VspBspTraversalData tData(mRoot, polys, 0, mRootCell, rays,  
    487                                                    ComputePvsSize(*rays), cell->GetArea(), cell); 
     487                                                   ComputePvsSize(*rays)); 
    488488 
    489489        tStack.push(tData); 
     
    515515                (((int)data.mRays->size() <= mTermMinRays) || 
    516516                 (data.mPvs <= mTermMinPvs) || 
    517                  (data.mArea <= mTermMinArea) || 
    518517                 (data.mDepth >= mTermMaxDepth) || 
    519518                 (data.GetAvgRayContribution() < mTermMaxRayContribution)); 
     
    548547                DEL_PTR(tData.mPolygons); 
    549548                DEL_PTR(tData.mRays); 
    550                 DEL_PTR(tData.mGeometry); 
     549                //DEL_PTR(tData.mGeometry); 
    551550                 
    552551                return leaf; 
     
    557556         
    558557        VspBspTraversalData tFrontData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell,  
    559                                                                 new RayInfoContainer(), 0, 0, new VspBspNodeGeometry()); 
     558                                                                new RayInfoContainer(), 0); 
    560559        VspBspTraversalData tBackData(NULL, new PolygonContainer(), tData.mDepth + 1, mRootCell,  
    561                                                            new RayInfoContainer(), 0, 0, new VspBspNodeGeometry()); 
     560                                                           new RayInfoContainer(), 0); 
    562561 
    563562        // create new interior node and two leaf nodes 
     
    583582        DEL_PTR(tData.mPolygons); 
    584583        DEL_PTR(tData.mRays); 
    585         DEL_PTR(tData.mGeometry);                
     584        //DEL_PTR(tData.mGeometry);              
    586585 
    587586        return interior; 
     
    616615        frontData.mPvs = ComputePvsSize(*frontData.mRays); 
    617616        backData.mPvs = ComputePvsSize(*backData.mRays); 
    618  
    619         // split geometry and compute area 
    620         if (1) 
    621         { 
    622                 tData.mGeometry->SplitGeometry(*frontData.mGeometry,  
    623                                                                            *backData.mGeometry,  
    624                                                                            *this,  
    625                                                                            interior->mPlane); 
    626          
    627                  
    628                 frontData.mArea = frontData.mGeometry->GetArea(); 
    629                 backData.mArea = backData.mGeometry->GetArea(); 
    630         } 
    631617 
    632618        // compute accumulated ray length 
     
    1006992 
    1007993        if (mSplitPlaneStrategy & PVS) 
    1008         { 
    1009994                // create unique ids for pvs heuristics 
    1010995                GenerateUniqueIdsForPvs(); 
    1011  
    1012                 if (mPvsUseArea) // use front and back cell areas to approximate volume 
    1013                 {        
    1014                         // construct child geometry with regard to the candidate split plane 
    1015                         VspBspNodeGeometry frontCell; 
    1016                         VspBspNodeGeometry backCell; 
    1017  
    1018                         data.mGeometry->SplitGeometry(frontCell, backCell, *this, candidatePlane); 
    1019                  
    1020                         pFront = frontCell.GetArea(); 
    1021                         pBack = backCell.GetArea(); 
    1022  
    1023                         pOverall = data.mArea; 
    1024                 } 
    1025         } 
     996         
    1026997                         
    1027998        RayInfoContainer::const_iterator rit, rit_end = data.mRays->end(); 
     
    10551026                        AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs); 
    10561027                         
    1057                         if (!mPvsUseArea) // use front and back cell areas to approximate volume 
    1058                         {        
    1059                                 const float len = (*rit).SegmentLength(); 
    1060                                 pOverall += len; 
    1061  
    1062                                 // use length of rays to approximate volume 
    1063                                 if (cf == 1) 
    1064                                         pFront += len; 
    1065                                 if (cf == -1) 
    1066                                         pBack += len; 
    1067                                 if (cf == 0) 
     1028                        float len = 1; 
     1029 
     1030                        if (mPvsUseLen) // use front and back cell areas to approximate volume 
     1031                                len = (*rit).SqrSegmentLength(); 
     1032                         
     1033                        pOverall += len; 
     1034 
     1035                        // use length of rays to approximate volume 
     1036                        if (cf == 1) 
     1037                                pFront += len; 
     1038                        if (cf == -1) 
     1039                                pBack += len; 
     1040                        if (cf == 0) 
     1041                        { 
     1042                                if (mPvsUseLen) 
    10681043                                { 
    10691044                                        float newLen = len *  
     
    10711046                                                ((*rit).GetMaxT() - (*rit).GetMinT()); 
    10721047 
     1048                                 
    10731049                                        if (candidatePlane.Side((*rit).ExtrapOrigin()) <= 0) 
    10741050                                        { 
     
    10811057                                                pBack += len - newLen; 
    10821058                                        } 
    1083                                 }                                
     1059                                } 
     1060                                else 
     1061                                { 
     1062                                        ++ pFront; 
     1063                                        ++ pBack; 
     1064                                } 
    10841065                        } 
    10851066                } 
     
    10941075                val += mBalancedRaysFactor * fabs(sumBalancedRays) /  raysSize; 
    10951076 
    1096         float denom = pOverall * (float)data.mPvs * 2.0f + Limits::Small; 
    1097         if ((mSplitPlaneStrategy & PVS) && data.mArea && data.mPvs) 
     1077        const float denom = pOverall * (float)data.mPvs * 2.0f + Limits::Small; 
     1078 
     1079        if (mSplitPlaneStrategy & PVS) 
    10981080        { 
    10991081                val += mPvsFactor * (frontPvs * pFront + (backPvs * pBack)) / denom; 
    11001082 
    11011083                // give penalty to unbalanced split 
    1102                 if (1) 
     1084                if (0) 
    11031085                if (((pFront * 0.2 + Limits::Small) > pBack) || (pFront < (pBack * 0.2 + Limits::Small))) 
    11041086                        val += 0.5; 
     
    12171199                ++ mStat.maxRayContribNodes; 
    12181200         
    1219         if (data.mGeometry->GetArea() <= mTermMinArea)  
    1220                 ++ mStat.minAreaNodes; 
    12211201 
    12221202#ifdef _DEBUG 
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h

    r440 r441  
    349349                /// rays piercing this node 
    350350                RayInfoContainer *mRays; 
    351                 /// area of current node 
    352                 float mArea; 
    353                 /// geometry of current node induced by split planes 
    354                 VspBspNodeGeometry *mGeometry; 
    355351 
    356352                /// pvs size 
     
    371367                mViewCell(NULL), 
    372368                mRays(NULL), 
    373                 mPvs(0), 
    374                 mArea(0.0), 
    375                 mGeometry(NULL) 
     369                mPvs(0) 
    376370                {} 
    377371                 
     
    381375                                                 ViewCell *viewCell, 
    382376                                                 RayInfoContainer *rays, 
    383                                                  int pvs, 
    384                                                  float area, 
    385                                                  VspBspNodeGeometry *cell):  
     377                                                 int pvs):  
    386378                mNode(node),  
    387379                mPolygons(polys),  
     
    389381                mViewCell(viewCell), 
    390382                mRays(rays), 
    391                 mPvs(pvs), 
    392                 mArea(area), 
    393                 mGeometry(cell) 
     383                mPvs(pvs) 
    394384                {} 
    395385    }; 
     
    762752 
    763753        /// if area or accumulated ray lenght should be used for PVS heuristics 
    764         bool mPvsUseArea; 
     754        bool mPvsUseLen; 
    765755 
    766756private: 
  • trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h

    r438 r441  
    7777 
    7878        if (!ray.intersections.empty()) 
    79           { 
     79        { 
    8080                mTermination = ray.Extrap(ray.intersections[0].mT); 
    8181                mTerminationObject = ray.intersections[0].mObject; 
    82           } 
     82        } 
    8383        else 
    84           { 
     84        { 
    8585                mTermination = ray.Extrap(1e6);//TODO: should be Limits::Infinity 
    8686                mTerminationObject = NULL; 
    87           } 
     87        } 
    8888 
    8989        Precompute(); 
Note: See TracChangeset for help on using the changeset viewer.