source: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp @ 2671

Revision 2671, 33.3 KB checked in by mattausch, 16 years ago (diff)
  • Property svn:executable set to *
RevLine 
[1460]1#include "Environment.h"
2#include "GvsPreprocessor.h"
3#include "GlRenderer.h"
[1473]4#include "VssRay.h"
5#include "ViewCellsManager.h"
[1486]6#include "Triangle3.h"
[1489]7#include "IntersectableWrapper.h"
[1500]8#include "Plane3.h"
[1521]9#include "RayCaster.h"
[1522]10#include "Exporter.h"
[1545]11#include "SamplingStrategy.h"
[1990]12#include "BvHierarchy.h"
[2116]13#include "Polygon3.h"
[2643]14#include "IntersectableWrapper.h"
[1460]15
[1473]16
[2643]17
[1460]18namespace GtpVisibilityPreprocessor
19{
20 
[2633]21#define GVS_DEBUG 0
[2669]22#define NOT_ACCOUNTED_OBJECT 0
23#define ACCOUNTED_OBJECT 2
24#define SHOW_QT_VISUALIZATION 0
[1934]25
[2669]26static const float MIN_DIST = 0.001f;
27
28static ObjectContainer myobjects;
29
30
31/** Visualization structure for the GVS algorithm.
32*/
[1522]33struct VizStruct
34{
35        Polygon3 *enlargedTriangle;
36        Triangle3 originalTriangle;
37        VssRay *ray;
38};
[1460]39
[2669]40static vector<VizStruct> vizContainer;
[2048]41
[1990]42
[1522]43
[2648]44
[1533]45GvsPreprocessor::GvsPreprocessor():
[1545]46Preprocessor(),
[2000]47mSamplingType(SamplingStrategy::VIEWCELL_BASED_DISTRIBUTION),
[1996]48mProcessedViewCells(0),
[2625]49mCurrentViewCell(NULL),
50mCurrentViewPoint(Vector3(0.0f, 0.0f, 0.0f))
[1460]51{
[1486]52        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
53        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
[2048]54        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.gvsSamplesPerPass", mGvsSamplesPerPass);
[1996]55        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.minContribution", mMinContribution);
[1486]56        Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
[1500]57        Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);   
[1990]58        Environment::GetSingleton()->GetBoolValue("GvsPreprocessor.perViewCell", mPerViewCell);
[1996]59        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.maxViewCells", mMaxViewCells);
[1473]60
[2048]61        Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", mEvaluatePixelError);
62
[2615]63        Environment::GetSingleton()->GetBoolValue("ViewCells.useKdPvs", mUseKdPvs);
64
65
[1934]66        char gvsStatsLog[100];
67        Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
68        mGvsStatsStream.open(gvsStatsLog);
69
[1486]70        Debug << "Gvs preprocessor options" << endl;
71        Debug << "number of total samples: " << mTotalSamples << endl;
72        Debug << "number of initial samples: " << mInitialSamples << endl;
[2048]73        cout << "number of gvs samples per pass: " << mGvsSamplesPerPass << endl;
74        cout << "number of samples per pass: " << mSamplesPerPass << endl;
[1501]75        Debug << "threshold: " << mThreshold << endl;
[1934]76        Debug << "epsilon: " << mEps << endl;
77        Debug << "stats: " << gvsStatsLog << endl;
[1996]78        Debug << "per view cell: " << mPerViewCell << endl;
79        Debug << "max view cells: " << mMaxViewCells << endl;
80        Debug << "min contribution: " << mMinContribution << endl;
[1486]81
[1990]82        if (1)
83                mOnlyRandomSampling = false;           
84        else
[1934]85                mOnlyRandomSampling = true;
86
87        mGvsStats.Reset();
[2643]88
[2648]89        // hack: some generic statistical values that can be read
90        // by an application using the preprocessor
[2643]91        mGenericStats = 0;
92        mGenericStats2 = 0;
[1460]93}
94
[1473]95
[1990]96GvsPreprocessor::~GvsPreprocessor()
97{
[1996]98        ClearRayQueue();
99}
100
101
102void GvsPreprocessor::ClearRayQueue()
103{
[1990]104        // clean ray queue
105        while (!mRayQueue.empty())
106        {
107                // handle next ray
108                VssRay *ray = mRayQueue.top();
109                mRayQueue.pop();
110
[2048]111                //delete ray;
[1990]112        }
113}
114
115
[2625]116ViewCell *GvsPreprocessor::NextViewCell()
[1982]117{
[1996]118        if (mProcessedViewCells == (int)mViewCells.size())
[2625]119                return NULL; // no more view cells
[1982]120
[2625]121        ViewCell *vc = mViewCells[mProcessedViewCells];
[1982]122
[2625]123   if (!vc->GetMesh())
124                mViewCellsManager->CreateMesh(vc);
[1990]125
[2625]126        mGvsStats.mViewCellId = vc->GetId();
[1996]127
[2625]128        Debug << "vc: " << vc->GetId() << endl;
[1996]129
130        ++ mProcessedViewCells;
[1982]131   
[2625]132        return vc;
[1982]133}
134
135
[1996]136int GvsPreprocessor::CheckDiscontinuity(const VssRay &currentRay,
137                                                                                const Triangle3 &hitTriangle,
138                                                                                const VssRay &oldRay)
[1460]139{
[1932]140        // the predicted hitpoint: we expect to hit the same mesh again
141        const Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
142
143        const float predictedLen = Magnitude(predictedHit - currentRay.mOrigin);
144        const float len = Magnitude(currentRay.mTermination - currentRay.mOrigin);
145       
146        // distance large => this is likely to be a discontinuity
[1933]147#if 1
[1932]148        if ((predictedLen - len) > mThreshold)
[1933]149#else // rather use relative distance
150        if ((predictedLen / len) > mThreshold)
151#endif
[1500]152        {
[1934]153                //cout << "r";
[1933]154                // apply reverse sampling to find the gap
[1500]155                VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
[1933]156
157                if (!newRay)
[1996]158                        return 1;
[1933]159
[1572]160                // set flag for visualization
[2053]161                //newRay->mFlags |= VssRay::ReverseSample;
[1580]162               
[2669]163                // check if ray is not further processed (ray not deleted because using ray pool)
164                HandleRay(newRay);
[1999]165               
[1996]166                return 1;
[1500]167        }
168
[1996]169        return 0;
[1486]170}
[1473]171
[2053]172
[2669]173void GvsPreprocessor::CountObject(Intersectable *triObj)
174{
175        if ((triObj->mCounter != (NOT_ACCOUNTED_OBJECT + 1)) && (triObj->mCounter != (ACCOUNTED_OBJECT + 1)))
176        {
177                ++ triObj->mCounter;
178                ++ mGenericStats2;
179        }
180}
181
182
183void GvsPreprocessor::UpdateStatsForVisualization(KdIntersectable *kdInt)
184{
185        mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
186
187        // count new objects in pvs due to kd node conversion   
188        myobjects.clear();
189        mKdTree->CollectObjects(kdInt->GetItem(), myobjects);
190
191        ObjectContainer::const_iterator oit, oit_end = myobjects.end();
192
193        for (oit = myobjects.begin(); oit != oit_end; ++ oit)
194                CountObject(*oit);
195}       
196
197
[1999]198bool GvsPreprocessor::HasContribution(VssRay &ray)
[1486]199{
[1999]200        if (!ray.mTerminationObject)
201                return false;
[1933]202
[1999]203        bool result;
204
[1977]205        if (!mPerViewCell)
206        {
[1999]207                // store the rays + the intersected view cells
208                const bool storeViewCells = false; //GVS_DEBUG;
209
210                mViewCellsManager->ComputeSampleContribution(ray,
[1977]211                                                                                                         true,
[1999]212                                                                                                         storeViewCells,
[1990]213                                                                                                         true);
[1999]214
215                result = ray.mPvsContribution > 0;
[1977]216        }
[2625]217        else // original per view cell gvs
[1977]218        {
[1999]219                Intersectable *obj = ray.mTerminationObject;
220
[2669]221                // counter < 2 => not accounted for yet
222                if (obj->mCounter < ACCOUNTED_OBJECT)
[1999]223                {
[2669]224                        obj->mCounter += ACCOUNTED_OBJECT;
[1999]225                        mTrianglePvs.push_back(obj);
[2615]226               
[2643]227                        mGenericStats = mTrianglePvs.size();
228
[2669]229                        // exchange the triangle with the node in the pvs for kd pvs
[2615]230                        if (mUseKdPvs)
231                        {
232                                KdNode *node = mKdTree->GetPvsNode(ray.mTermination);
[1999]233
[2615]234                                if (!node->Mailed())
235                                {
[2669]236                                        // add to pvs
[2615]237                                        node->Mail();
[2625]238                                        KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(node);
239                                        mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
[2669]240                               
241                                        if (SHOW_QT_VISUALIZATION) UpdateStatsForVisualization(kdInt); 
242                                }
[2615]243                        }
[2643]244
[1999]245                        result = true;
246                }
247                else
248                {
249                        result = false;
250                }
[1977]251        }
[1932]252
[1999]253        return result;
254}
255
256
257bool GvsPreprocessor::HandleRay(VssRay *vssRay)
258{
259        if (!HasContribution(*vssRay))
[1977]260                return false;
261
[2633]262//      cout << "t";
[2005]263        if (0 && GVS_DEBUG)
[1990]264                mVssRays.push_back(new VssRay(*vssRay));
265
[1977]266        // add new ray to ray queue
267        mRayQueue.push(vssRay);
268
[1990]269        ++ mGvsStats.mTotalContribution;
[1533]270
[1977]271        return true;
[1460]272}
273
[1500]274
275/** Creates 3 new vertices for triangle vertex with specified index.
[1489]276*/
[1932]277void GvsPreprocessor::CreateDisplacedVertices(VertexContainer &vertices,
278                                                                                          const Triangle3 &hitTriangle,
279                                                                                          const VssRay &ray,
280                                                                                          const int index) const
[1460]281{
[1486]282        const int indexU = (index + 1) % 3;
283        const int indexL = (index == 0) ? 2 : index - 1;
284
[1524]285        const Vector3 a = hitTriangle.mVertices[index] - ray.GetOrigin();
[1486]286        const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
287        const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
[1492]288       
[1533]289        const float len = Magnitude(a);
[1932]290       
[1523]291        const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
292        const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1))
[1524]293        const Vector3 dir3 = DotProd(dir2, dir1) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
294                Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir1) + CrossProd(dir2, a));
[1492]295
[1486]296        // compute the new three hit points
[1500]297        // pi, i + 1:  pi+ e·|pi-xp|·di, j
[1932]298        const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1;
[1500]299        // pi, i - 1:  pi+ e·|pi-xp|·di, j
[1932]300    const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2;
[1500]301        // pi, i:  pi+ e·|pi-xp|·di, j
[1932]302        const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3;
[1489]303       
[1524]304        vertices.push_back(pt2);
305        vertices.push_back(pt3);
[1500]306        vertices.push_back(pt1);
[1489]307}
308
309
[1500]310void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
311                                                                          const Triangle3 &hitTriangle,
[1932]312                                                                          const VssRay &ray) const
[1500]313{
[1932]314        CreateDisplacedVertices(vertices, hitTriangle, ray, 0);
315        CreateDisplacedVertices(vertices, hitTriangle, ray, 1);
316        CreateDisplacedVertices(vertices, hitTriangle, ray, 2);
[1500]317}
[1492]318
[1500]319
[1932]320Vector3 GvsPreprocessor::CalcPredictedHitPoint(const VssRay &newRay,
321                                                                                           const Triangle3 &hitTriangle,
322                                                                                           const VssRay &oldRay) const
[1489]323{
[1932]324        // find the intersection of the plane induced by the
325        // hit triangle with the new ray
[1500]326        Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
327
328        const Vector3 hitPt =
329                plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
330       
331        return hitPt;
332}
333
334
335static bool EqualVisibility(const VssRay &a, const VssRay &b)
336{
337        return a.mTerminationObject == b.mTerminationObject;
338}
339
340
341int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
342                                                                   const Vector3 &p1,
343                                                                   const Vector3 &p2,
[1932]344                                                                   const VssRay &ray1,
345                                                                   const VssRay &ray2,
[1500]346                                                                   const VssRay &oldRay)
[2048]347{
348        //cout <<"y"<<Magnitude(p1 - p2) << " ";
[1996]349        int castRays = 0;
350
[1990]351        // cast reverse rays if necessary
[1996]352        castRays += CheckDiscontinuity(ray1, hitTriangle, oldRay);
353        castRays += CheckDiscontinuity(ray2, hitTriangle, oldRay);
[1500]354
[2017]355        if (EqualVisibility(ray1, ray2) || (SqrMagnitude(p1 - p2) <= MIN_DIST))
[1500]356        {
[1996]357                return castRays;
[1500]358        }
[1932]359       
[1990]360        // the new subdivision point
361        const Vector3 p = (p1 + p2) * 0.5f;
[2017]362        //cout << "p: " << p << " " << p1 << " " << p2 << endl;
[1990]363        // cast ray into the new point
364        SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f);
[1932]365
[1996]366        VssRay *newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
367       
368        ++ castRays;
[1580]369
[1996]370        if (!newRay) return castRays;
[1580]371
[2053]372        //newRay->mFlags |= VssRay::BorderSample;
[1990]373
374        // add new ray to queue
375        const bool enqueued = HandleRay(newRay);
376
377        // subdivide further
[1996]378        castRays += SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay);
379        castRays += SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay);
[1990]380
381        // this ray will not be further processed
[2048]382        //if (!enqueued) delete newRay;
[1990]383
[1996]384        return castRays;
[1500]385}
386
387
388int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &currentRay)
389{
390        Intersectable *tObj = currentRay.mTerminationObject;
[2597]391        // q matt: can this be possible
392        if (!tObj) return 0;
393
[1486]394        Triangle3 hitTriangle;
[1489]395
396        // other types not implemented yet
397        if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
[2017]398                hitTriangle = static_cast<TriangleIntersectable *>(tObj)->GetItem();
[1522]399        else
[2601]400                cout << "border sampling: " << Intersectable::GetTypeName(tObj) << " not yet implemented" << endl;
[1489]401
[2601]402        //cout << "type: " << Intersectable::GetTypeName(tObj) << endl;
403
[1500]404        VertexContainer enlargedTriangle;
405       
406        /// create 3 new hit points for each vertex
407        EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
408       
409        /// create rays from sample points and handle them
[1492]410        SimpleRayContainer simpleRays;
411        simpleRays.reserve(9);
[1486]412
[1500]413        VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
[1486]414
[1500]415        for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
416        {
417                const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
[1990]418
[1883]419                SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
[1990]420                simpleRays.AddRay(sr); 
[1500]421        }
[1522]422
[1566]423        if (0)
424        {
[1932]425                // visualize enlarged triangles
[1566]426                VizStruct dummy;
427                dummy.enlargedTriangle = new Polygon3(enlargedTriangle);
428                dummy.originalTriangle = hitTriangle;
429                vizContainer.push_back(dummy);
430        }
431
[1524]432        // cast rays to triangle vertices and determine visibility
[1489]433        VssRayContainer vssRays;
[1533]434
[1932]435        // don't cast double rays as we need only the forward rays
[1996]436        const bool castDoubleRays = !mPerViewCell;
[1990]437        // cannot prune invalid rays because we have to compare adjacent  rays.
[1932]438        const bool pruneInvalidRays = false;
439
[1996]440
441        //////////
442        //-- fill up simple rays with random rays so we can cast 16
443
444        //const int numRandomRays = 0;
445        const int numRandomRays = 16 - (int)simpleRays.size();
446        ViewCellBasedDistribution vcStrat(*this, mCurrentViewCell);
447
448        GenerateRays(numRandomRays, vcStrat, simpleRays);
449
[2053]450
[1996]451        /////////////////////
452
[1990]453        // keep origin for per view cell sampling
[1996]454        CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
[1932]455
[1996]456        const int numBorderSamples = (int)vssRays.size() - numRandomRays;
[2648]457
458#if 0
[1580]459        // set flags
[2648]460        VssRayContainer::const_iterator rit, rit_end = vssRays.end();
[1996]461        for (rit = vssRays.begin(); rit != rit_end; ++ rit, ++ i)
[2053]462                (*rit)->mFlags |= VssRay::BorderSample;
[2648]463#endif
464
[1999]465        int castRays = (int)simpleRays.size();
466
467        VssRayContainer invalidSamples;
468
[1932]469        // handle rays
[1999]470        EnqueueRays(vssRays, invalidSamples);
471
[1500]472    // recursivly subdivide each edge
[1996]473        for (int i = 0; i < numBorderSamples; ++ i)
[1500]474        {
[1932]475                castRays += SubdivideEdge(hitTriangle,
476                                                                  enlargedTriangle[i],
[1996]477                                                                  enlargedTriangle[(i + 1) % numBorderSamples],
[1932]478                                                                  *vssRays[i],
[1996]479                                                                  *vssRays[(i + 1) % numBorderSamples],
[1932]480                                                                  currentRay);
[1500]481        }
[1999]482       
[1934]483        mGvsStats.mBorderSamples += castRays;
[1932]484
[2053]485        //CLEAR_CONTAINER(invalidSamples);
[2048]486       
487        //cout << "cast rays: " << castRays << endl;
[1533]488        return castRays;
[1473]489}
490
491
[1996]492bool GvsPreprocessor::GetPassingPoint(const VssRay &currentRay,
493                                                                          const Triangle3 &occluder,
494                                                                          const VssRay &oldRay,
495                                                                          Vector3 &newPoint) const
[1933]496{
497        //-- The plane p = (xp, hit(x), hit(xold)) is intersected
498        //-- with the newly found occluder (xold is the previous ray from
499        //-- which x was generated). On the intersecting line, we select a point
500        //-- pnew which lies just outside of the new triangle so the ray
501        //-- just passes through the gap
502
503        const Plane3 plane(currentRay.GetOrigin(),
504                                           currentRay.GetTermination(),
505                                           oldRay.GetTermination());
506       
507        Vector3 pt1, pt2;
508
509        const bool intersects = occluder.GetPlaneIntersection(plane, pt1, pt2);
510
511        if (!intersects)
[1996]512        {
[1999]513                //cerr << "big error!! no intersection " << pt1 << " " << pt2 << endl;
[1996]514                return false;
515        }
[1933]516
517        // get the intersection point on the old ray
518        const Plane3 triPlane(occluder.GetNormal(), occluder.mVertices[0]);
519
520        const float t = triPlane.FindT(oldRay.mOrigin, oldRay.mTermination);
521        const Vector3 pt3 = oldRay.mOrigin + t * (oldRay.mTermination - oldRay.mOrigin);
522
523        // Evaluate new hitpoint just outside the triangle
524        const float eps = mEps;
[1990]525
[1933]526        // the point is chosen to be on the side closer to the original ray
527        if (Distance(pt1, pt3) < Distance(pt2, pt3))
528                newPoint = pt1 + eps * (pt1 - pt2);
529        else
530                newPoint = pt2 + eps * (pt2 - pt1);
531
[1934]532        //cout << "passing point: " << newPoint << endl << endl;
[1996]533        return true;
[1500]534}
535
536
537VssRay *GvsPreprocessor::ReverseSampling(const VssRay &currentRay,
538                                                                                 const Triangle3 &hitTriangle,
539                                                                                 const VssRay &oldRay)
540{
[1933]541        // get triangle occluding the path to the hit mesh
542        Triangle3 occluder;
543        Intersectable *tObj = currentRay.mTerminationObject;
[1934]544
[1933]545        // q: why can this happen?
546        if (!tObj)
547                return NULL;
548
549        // other types not implemented yet
550        if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
[2048]551        {
[2017]552                occluder = static_cast<TriangleIntersectable *>(tObj)->GetItem();
[2048]553        }
[1933]554        else
[2048]555        {
556                cout << "reverse sampling: " << tObj->Type() << " not yet implemented" << endl;
557        }
[1933]558        // get a point which is passing just outside of the occluder
[1996]559    Vector3 newPoint;
560
561        // why is there sometimes no intersecton found?
562        if (!GetPassingPoint(currentRay, occluder, oldRay, newPoint))
563                return NULL;
564
[1500]565        const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
566
[1990]567        Vector3 newDir, newOrigin;
568
[1932]569        //-- Construct the mutated ray with xnew,
570        //-- dir = predicted(x)- pnew as direction vector
[1990]571        newDir = predicted - newPoint;
[1932]572
[1933]573        // take xnew, p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
[1500]574        // difficult to say!!
[1935]575        const float offset = 0.5f;
[1990]576        newOrigin = newPoint - newDir * offset;
577       
578        //////////////
579        //-- for per view cell sampling, we must check for intersection
580        //-- with the current view cell
[1500]581
[1990]582    if (mPerViewCell)
583        {
584                // send ray to view cell
585                static Ray ray;
586                ray.Clear();
587                ray.Init(newOrigin, -newDir, Ray::LOCAL_RAY);
588               
589                //cout << "z";
590                // check if ray intersects view cell
591                if (!mCurrentViewCell->CastRay(ray))
592                        return NULL;
[1981]593
[1990]594                Ray::Intersection &hit = ray.intersections[0];
595       
596                //cout << "q";
597                // the ray starts from the view cell
598                newOrigin = ray.Extrap(hit.mT);
599        }
[1981]600
[1933]601        const SimpleRay simpleRay(newOrigin, newDir, SamplingStrategy::GVS, 1.0f);
[1571]602
[1933]603        VssRay *reverseRay =
[1996]604                mRayCaster->CastRay(simpleRay, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
[1933]605
[1934]606    ++ mGvsStats.mReverseSamples;
[1933]607
608        return reverseRay;
[1473]609}
610
[1489]611
612int GvsPreprocessor::CastInitialSamples(const int numSamples,
613                                                                                const int sampleType)
614{       
[1473]615        const long startTime = GetTime();
[1595]616
[1489]617        // generate simple rays
618        SimpleRayContainer simpleRays;
[1990]619       
620        ViewCellBasedDistribution vcStrat(*this, mCurrentViewCell);
621    GenerateRays(numSamples, vcStrat, simpleRays);
622
623        //cout << "sr: " << simpleRays.size() << endl;
[1489]624        // generate vss rays
[1473]625        VssRayContainer samples;
[1990]626       
[1996]627        const bool castDoubleRays = !mPerViewCell;
[1990]628        const bool pruneInvalidRays = true;
629       
[1996]630        CastRays(simpleRays, samples, castDoubleRays, pruneInvalidRays);
631       
[1999]632        VssRayContainer invalidSamples;
633
[1489]634        // add to ray queue
[1999]635        EnqueueRays(samples, invalidSamples);
[1533]636
[2053]637        //CLEAR_CONTAINER(invalidSamples);
[2633]638        //Debug << "generated " <<  numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
[1990]639        return (int)simpleRays.size();
[1489]640}
641
642
[1999]643void GvsPreprocessor::EnqueueRays(VssRayContainer &samples, VssRayContainer &invalidSamples)
[1489]644{
[1486]645        // add samples to ray queue
[1473]646        VssRayContainer::const_iterator vit, vit_end = samples.end();
[1576]647        for (vit = samples.begin(); vit != vit_end; ++ vit)
[1473]648        {
[1999]649                VssRay *ray = *vit;
650
[2053]651                HandleRay(ray);
[2633]652                //if (!HandleRay(ray)) invalidSamples.push_back(ray);
[1460]653        }
654}
655
[1473]656
[1990]657int GvsPreprocessor::ProcessQueue()
[1460]658{
[1533]659        int castSamples = 0;
[1990]660        ++ mGvsStats.mGvsPass;
[1545]661
[2048]662        while (!mRayQueue.empty())//&& (mGvsStats.mTotalSamples + castSamples < mTotalSamples) )
[1990]663        {
[2597]664                //cout << "queue size: " << mRayQueue.size() << endl;
[1990]665                // handle next ray
666                VssRay *ray = mRayQueue.top();
667                mRayQueue.pop();
[1996]668               
[1990]669                const int newSamples = AdaptiveBorderSampling(*ray);
[1934]670
[1990]671                castSamples += newSamples;
[2048]672                //delete ray;
[1473]673        }
674
[2161]675        /*if (mRayCaster->mVssRayPool.mIndex > mSamplesPerPass)
[2048]676        {
677                cout << "warning: new samples: " << castSamples << " " << "queue: "  << (int)mRayQueue.size() << endl;
678                Debug << "warning: new samples: " << castSamples << " " << "queue: "  << (int)mRayQueue.size() << endl;
[2161]679        }*/
[2048]680
[1533]681        return castSamples;
[1460]682}
683
[1473]684
[1990]685void ExportVssRays(Exporter *exporter, const VssRayContainer &vssRays)
[1460]686{
[1990]687        VssRayContainer vcRays, vcRays2, vcRays3;
[1473]688
[1990]689        VssRayContainer::const_iterator rit, rit_end = vssRays.end();
690
691        // prepare some rays for output
692        for (rit = vssRays.begin(); rit != rit_end; ++ rit)
[1473]693        {
[1990]694                //const float p = RandomValue(0.0f, (float)vssRays.size());
[1545]695
[1990]696                if (1)//(p < raysOut)
697                {
698                        if ((*rit)->mFlags & VssRay::BorderSample)
699                        {
700                                vcRays.push_back(*rit);
701                        }
702                        else if ((*rit)->mFlags & VssRay::ReverseSample)
703                        {
704                                vcRays2.push_back(*rit);
705                        }
706                        else
707                        {
708                                vcRays3.push_back(*rit);
709                        }       
710                }
[1460]711        }
[1990]712
[2005]713        exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
714        exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
[2003]715        exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
[1990]716}
717
718
[1999]719void GvsPreprocessor::VisualizeViewCell(const ObjectContainer &objects)
[1990]720{
721    Intersectable::NewMail();
722        Material m;
[1528]723       
[1996]724        char str[64]; sprintf(str, "pass%06d.wrl", mProcessedViewCells);
[1990]725
726        Exporter *exporter = Exporter::GetExporter(str);
727        if (!exporter)
728                return;
729
[1999]730        ObjectContainer::const_iterator oit, oit_end = objects.end();
731
732        for (oit = objects.begin(); oit != oit_end; ++ oit)
733        {
734                Intersectable *intersect = *oit;
735               
736                m = RandomMaterial();
737                exporter->SetForcedMaterial(m);
738                exporter->ExportIntersectable(intersect);
[1990]739        }
740
741        cout << "vssrays: " << (int)mVssRays.size() << endl;
742        ExportVssRays(exporter, mVssRays);
743
[2597]744
[1990]745        /////////////////
746        //-- export view cell geometry
747
[2597]748        //exporter->SetWireframe();
[1990]749
750        m.mDiffuseColor = RgbColor(0, 1, 0);
751        exporter->SetForcedMaterial(m);
752
[1999]753        //mViewCellsManager->ExportViewCellGeometry(exporter, vc, NULL, NULL);
754        //mViewCellsManager->ExportViewCellGeometry(exporter, mCurrentViewCell, NULL, NULL);
755
756        AxisAlignedBox3 bbox = mCurrentViewCell->GetMesh()->mBox;
757        exporter->ExportBox(bbox);
[1990]758        //exporter->SetFilled();
759
[2053]760        delete exporter;
[1460]761}
762
763
[1982]764void GvsPreprocessor::VisualizeViewCells()
765{
[1996]766        char str[64]; sprintf(str, "tmp/pass%06d_%04d-", mProcessedViewCells, mPass);
[1982]767                       
768        // visualization
769        if (mGvsStats.mPassContribution > 0)
770        {
771                const bool exportRays = true;
772                const bool exportPvs = true;
773
774                mViewCellsManager->ExportSingleViewCells(mObjects,
775                                                                                                 10,
776                                                                                                 false,
777                                                                                                 exportPvs,
778                                                                                                 exportRays,
779                                                                                                 1000,
780                                                                                                 str);
781        }
782
783        // remove pass samples
784        ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
785
786        for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
787        {
788                (*vit)->DelRayRefs();
789        }
790}
791
792
[1990]793void GvsPreprocessor::ProcessViewCell()
[1982]794{
[2597]795        // compute object that directly intersect view cell
[1999]796        IntersectWithViewCell();
797
[1990]798        mGvsStats.mPerViewCellSamples = 0;
[2643]799
[1996]800        int oldContribution = mGvsStats.mTotalContribution;
[1990]801        int passSamples = 0;
[2648]802
[2633]803        mGenericStats = 0;
[2643]804        mGenericStats2 = 0;
[2597]805
[1996]806        //while (mGvsStats.mPerViewCellSamples < mTotalSamples)
[2643]807        while (1)
[1982]808        {
[2048]809                mRayCaster->InitPass();
810
[1990]811                // Ray queue empty =>
812                // cast a number of uniform samples to fill ray queue
813                int newSamples = CastInitialSamples(mInitialSamples, mSamplingType);
[1982]814
[1990]815                if (!mOnlyRandomSampling)
816                        newSamples += ProcessQueue();
[2003]817               
[2597]818                passSamples += newSamples;
819        mGvsStats.mPerViewCellSamples += newSamples;
820
[2048]821                if (passSamples >= mGvsSamplesPerPass)
[1982]822                {
823                        ++ mPass;
[1990]824                        mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
825
[1982]826                        ////////
827                        //-- stats
828
829                        mGvsStats.mPass = mPass;
830
[1996]831                        cout << "\nPass " << mPass << " #samples: " << mGvsStats.mPerViewCellSamples << endl;
832                        cout << "contribution=" << mGvsStats.mPassContribution << " (of " << mMinContribution << ")" << endl;
[1990]833
[2643]834                        //mGenericStats = mGvsStats.mPassContribution;
[2633]835
[1996]836                        // termination criterium
837                        if (mGvsStats.mPassContribution < mMinContribution)
838                                break;
839
[1990]840                        // reset
841                        oldContribution = mGvsStats.mTotalContribution;
842                        mGvsStats.mPassContribution = 0;
843                        passSamples = 0;
[1982]844                }
845        }
846}
847
848
[1996]849void GvsPreprocessor::CompileViewCellsList()
850{
[2648]851        // Receive list of view cells from view cells manager
[2598]852        ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
[2053]853       
[2048]854        if (!vcPoints->empty())
855        {
[2597]856                cout << "processing view cell list" << endl;
[2053]857               
[2048]858                vector<ViewCellPoints *>::const_iterator vit, vit_end = vcPoints->end();
859
860                for (vit = vcPoints->begin(); vit != vit_end; ++ vit)
[2648]861                {
[2048]862                        mViewCells.push_back((*vit)->first);
863
[2662]864                        if (mViewCells.size() >= mMaxViewCells)
865                                break;
866                }
[2048]867        }
[2662]868        else
869        {
870                while ((int)mViewCells.size() < mMaxViewCells)
[1996]871                {
[2662]872                        if (0)
873                        {
874                                mViewCells.push_back(mViewCellsManager->GetViewCell((int)mViewCells.size()));
875                                continue;
876                        }
[1999]877
[2662]878                        // HACK
879                        const int tries = 10000;
880                        int i = 0;
[1996]881
[2662]882                        for (i = 0; i < tries; ++ i)
[1996]883                        {
[2662]884                                const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetNumViewCells() - 0.5f);
885
886                                ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
887
888                                if (!viewCell->Mailed())
889                                {
890                                        viewCell->Mail();
891                                        break;
892                                }
893
894                                mViewCells.push_back(viewCell);
[1999]895                        }
[2597]896
[2662]897                        if (i == tries)
898                        {
899                                cerr << "big error! no view cell found" << endl;
900                                break;
901                        }
[1999]902                }
903        }
[2662]904
905        cout << "\ncomputing list of " << mViewCells.size() << " view cells" << endl;
[1999]906}
[1996]907
[1999]908
909void GvsPreprocessor::IntersectWithViewCell()
910{
911        mCurrentViewCell->GetMesh()->ComputeBoundingBox();
912        AxisAlignedBox3 box = mCurrentViewCell->GetMesh()->mBox;
[2544]913       
[2643]914        //vector<KdLeaf *> leaves;
915        //mKdTree->GetBoxIntersections(box, leaves);
[1999]916
[2643]917        ObjectContainer kdobjects;
918        mKdTree->CollectKdObjects(box, kdobjects);
[2669]919
[2643]920        ObjectContainer::const_iterator oit, oit_end = kdobjects.end();
[2669]921
[2643]922        for (oit = kdobjects.begin(); oit != oit_end; ++ oit)
[1999]923        {
[2643]924        // add to kdnode pvs
925                KdIntersectable *kdInt = static_cast<KdIntersectable *>(*oit);
[2615]926               
[2643]927                //mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
928                mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
[2669]929
[2643]930                mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
[2615]931
[2643]932                myobjects.clear();
933                mKdTree->CollectObjects(kdInt->GetItem(), myobjects);
[1999]934
[2643]935                // account for kd object pvs
936                ObjectContainer::const_iterator oit, oit_end = myobjects.end();
937
938                for (oit = myobjects.begin(); oit != oit_end; ++ oit)
[1999]939                {
[2017]940                        TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
[2643]941           
[2669]942                        CountObject(triObj);
[2643]943           
944                        // the triangle itself intersects
[1999]945                        if (box.Intersects(triObj->GetItem()))
[1996]946                        {
[2669]947                                if ((triObj->mCounter < ACCOUNTED_OBJECT))
[1999]948                                {
[2669]949                                        triObj->mCounter += ACCOUNTED_OBJECT;
[2643]950
[1999]951                                        mTrianglePvs.push_back(triObj);
[2643]952                                        mGenericStats = mTrianglePvs.size();
[1999]953                                }
[2643]954                        }                       
[1996]955                }
956        }
957}
958
959
[1990]960void GvsPreprocessor::PerViewCellComputation()
[1982]961{
[2625]962        ViewCell *vc;
[1999]963
[2625]964        while (vc = NextViewCell())
[1999]965        {
[2625]966                // hack: reset counters
967                ObjectContainer::const_iterator oit, oit_end = mObjects.end();
[2615]968
[2625]969                for (oit = mObjects.begin(); oit != oit_end; ++ oit)
[2669]970                        (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
[1982]971
[2648]972        ComputeViewCell(vc);
[2667]973                // $$ JB HACK compute just first viewcell
974                //              break;
[2625]975        }
976}
[1990]977
[1996]978
[2625]979void GvsPreprocessor::PerViewCellComputation2()
980{
981        ViewCell *vc;
[1999]982
[2625]983        while (1)
984        {
985                if (!mRendererWidget)
986                        continue;
[1990]987
[2625]988        ViewCell *vc = mViewCellsManager->GetViewCell(mRendererWidget->GetViewPoint());
[2601]989
[2625]990                // no valid view cell or view cell already computed
[2643]991                if (!vc || !vc->GetPvs().Empty() || !mRendererWidget->mComputeGVS)
[2625]992                        continue;
[2615]993
[2643]994                mRendererWidget->mComputeGVS = false;
[2625]995                // hack: reset counters
996                ObjectContainer::const_iterator oit, oit_end = mObjects.end();
[2615]997
[2625]998                for (oit = mObjects.begin(); oit != oit_end; ++ oit)
[2669]999                        (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
[2601]1000
[2625]1001                ComputeViewCell(vc);
1002                ++ mProcessedViewCells;
1003        }
1004}
[1999]1005
[1982]1006
[2048]1007void GvsPreprocessor::StorePvs(const ObjectContainer &objectPvs)
1008{
1009        ObjectContainer::const_iterator oit, oit_end = objectPvs.end();
1010
1011        for (oit = objectPvs.begin(); oit != oit_end; ++ oit)
1012        {
1013                mCurrentViewCell->GetPvs().AddSample(*oit, 1);
1014        }
1015}
1016
1017
[1990]1018void GvsPreprocessor::UpdatePvs(ViewCell *currentViewCell)
1019{
1020        ObjectPvs newPvs;
1021        BvhLeaf::NewMail();
1022
1023        ObjectPvsIterator pit = currentViewCell->GetPvs().GetIterator();
1024
1025        // output PVS of view cell
1026        while (pit.HasMoreEntries())
1027        {               
[2117]1028                Intersectable *intersect = pit.Next();
[1990]1029
1030                BvhLeaf *bv = intersect->mBvhLeaf;
1031
1032                if (!bv || bv->Mailed())
1033                        continue;
1034               
1035                bv->Mail();
1036
1037                //m.mDiffuseColor = RgbColor(1, 0, 0);
1038                newPvs.AddSampleDirty(bv, 1.0f);
[1982]1039        }
[1990]1040
1041        newPvs.SimpleSort();
1042
1043        currentViewCell->SetPvs(newPvs);
[1982]1044}
1045
[1999]1046 
1047void GvsPreprocessor::GetObjectPvs(ObjectContainer &objectPvs) const
1048{
[2053]1049        objectPvs.reserve((int)mTrianglePvs.size());
1050
[1999]1051        BvhLeaf::NewMail();
[1982]1052
[1999]1053        ObjectContainer::const_iterator oit, oit_end = mTrianglePvs.end();
1054
1055        for (oit = mTrianglePvs.begin(); oit != oit_end; ++ oit)
1056        {
1057                Intersectable *intersect = *oit;
1058       
1059                BvhLeaf *bv = intersect->mBvhLeaf;
1060
[2053]1061                // hack: reset counter
[2669]1062                (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
[2053]1063
[1999]1064                if (!bv || bv->Mailed())
1065                        continue;
[2615]1066
[1999]1067                bv->Mail();
1068                objectPvs.push_back(bv);
1069        }
1070}
1071
1072
[1990]1073void GvsPreprocessor::GlobalComputation()
1074{
1075        int passSamples = 0;
1076        int oldContribution = 0;
1077
1078        while (mGvsStats.mTotalSamples < mTotalSamples)
1079        {
[2048]1080                mRayCaster->InitPass();
[1990]1081                // Ray queue empty =>
1082                // cast a number of uniform samples to fill ray queue
1083                int newSamples = CastInitialSamples(mInitialSamples, mSamplingType);
1084
1085                if (!mOnlyRandomSampling)
1086                        newSamples += ProcessQueue();
1087
1088                passSamples += newSamples;
1089                mGvsStats.mTotalSamples += newSamples;
1090
[2048]1091                if (passSamples % (mGvsSamplesPerPass + 1) == mGvsSamplesPerPass)
[1990]1092                {
1093                        ++ mPass;
1094
1095                        mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
1096
1097                        ////////
1098                        //-- stats
1099
1100                        //cout << "\nPass " << mPass << " #samples: " << mGvsStats.mTotalSamples << " of " << mTotalSamples << endl;
1101                        mGvsStats.mPass = mPass;
1102                        mGvsStats.Stop();
1103                        mGvsStats.Print(mGvsStatsStream);
1104
1105                        // reset
1106                        oldContribution = mGvsStats.mTotalContribution;
1107                        mGvsStats.mPassContribution = 0;
1108                        passSamples = 0;
1109
1110                        if (GVS_DEBUG)
1111                                VisualizeViewCells();
1112                }
1113        }
1114}
1115
1116
[1489]1117bool GvsPreprocessor::ComputeVisibility()
[1460]1118{
[1533]1119        cout << "Gvs Preprocessor started\n" << flush;
[1545]1120        const long startTime = GetTime();
[1533]1121
[1990]1122        //Randomize(0);
[1934]1123        mGvsStats.Reset();
1124        mGvsStats.Start();
[1545]1125
[1486]1126        if (!mLoadViewCells)
[1563]1127        {       
1128                /// construct the view cells from the scratch
1129                ConstructViewCells();
[1580]1130                // reset pvs already gathered during view cells construction
1131                mViewCellsManager->ResetPvs();
[1563]1132                cout << "finished view cell construction" << endl;
[1486]1133        }
[1460]1134
[1982]1135        if (mPerViewCell)
[1473]1136        {
[2648]1137#if 1
[2597]1138                // provide list of view cells to compute
[1996]1139                CompileViewCellsList();
[2597]1140                // start per view cell gvs
[2048]1141                PerViewCellComputation();
[2625]1142#else
1143                PerViewCellComputation2();
1144
1145#endif
[1460]1146        }
[1982]1147        else
1148        {
1149                GlobalComputation();
1150        }
[1460]1151
[1934]1152        cout << "cast " << 2 * mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M rays/s" << endl;
[1545]1153
[1934]1154        if (GVS_DEBUG)
1155        {
1156                Visualize();
1157                CLEAR_CONTAINER(mVssRays);
1158        }
[2048]1159       
1160        // export the preprocessed information to a file
1161        if (0 && mExportVisibility)
1162        {
1163                ExportPreprocessedData(mVisibilityFileName);
1164        }
[2648]1165       
1166        // compute the pixel error of this visibility solution
1167        if (mEvaluatePixelError)
1168        {
1169                ComputeRenderError();
1170        }
[1934]1171
[1473]1172        return true;
[1460]1173}
1174
[1522]1175
[2048]1176void GvsPreprocessor::DeterminePvsObjects(VssRayContainer &rays)
1177{
[2187]1178        // store triangle directly
[2048]1179        mViewCellsManager->DeterminePvsObjects(rays, true);
1180}
1181
1182
[1522]1183void GvsPreprocessor::Visualize()
1184{
1185        Exporter *exporter = Exporter::GetExporter("gvs.wrl");
1186
1187        if (!exporter)
1188                return;
1189       
1190        vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end();
[1999]1191       
[1522]1192        for (vit = vizContainer.begin(); vit != vit_end; ++ vit)
1193        {
[1524]1194                exporter->SetWireframe();
[1522]1195                exporter->ExportPolygon((*vit).enlargedTriangle);
1196                //Material m;
[1524]1197                exporter->SetFilled();
1198                Polygon3 poly = Polygon3((*vit).originalTriangle);
1199                exporter->ExportPolygon(&poly);
[1522]1200        }
1201
[1933]1202        VssRayContainer::const_iterator rit, rit_end = mVssRays.end();
[1999]1203
[1933]1204        for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
1205        {
1206                Intersectable *obj = (*rit)->mTerminationObject;
1207                exporter->ExportIntersectable(obj);
1208        }
1209
[1990]1210        ExportVssRays(exporter, mVssRays);
1211       
[1522]1212        delete exporter;
[1460]1213}
[1522]1214
[1934]1215
1216void GvsStatistics::Print(ostream &app) const
1217{
[2003]1218        app << "#ViewCells\n" << mViewCells << endl;
[2648]1219        app << "#Id\n" << mViewCellId << endl;
1220        app << "#Time\n" << mTimePerViewCell << endl;;
[2003]1221        app << "#TrianglePvs\n" << mTrianglePvs << endl;
[2648]1222        app << "#ObjectPvs\n" << mPerViewCellPvs << endl;
1223        app << "#PvsCost\n" << (int)mPvsCost << endl;
[2003]1224
[2648]1225#if 0
[2003]1226        app << "#TotalPvs\n" << mTotalPvs << endl;
[2648]1227        app << "#TotalTime\n" << mTotalTime << endl;
1228        app << "#RaysPerSec\n" << RaysPerSec() << endl;
[2003]1229       
1230        app << "#PerViewCellSamples\n" << mPerViewCellSamples << endl;
[1934]1231        app << "#TotalSamples\n" << mTotalSamples << endl;
1232        app     << "#SamplesContri\n" << mTotalContribution << endl;
[2648]1233        app << "#TotalTrianglePvs\n" << mTotalTrianglePvs << endl;
1234       
1235#endif
1236
[2003]1237        //app << "#ReverseSamples\n" << mReverseSamples << endl;
1238        //app << "#BorderSamples\n" << mBorderSamples << endl;
1239
1240        //app << "#Pass\n" << mPass << endl;
1241        //app << "#ScDiff\n" << mPassContribution << endl;
1242        //app << "#GvsRuns\n" << mGvsPass << endl;     
1243
1244        app << endl;
[1883]1245}
[1934]1246
1247
[2625]1248void GvsPreprocessor::ComputeViewCell(ViewCell *vc)
1249{
[2643]1250        KdNode::NewMail();
[2625]1251
[2670]1252        mCurrentViewCell = vc;
1253       
1254        const long startTime = GetTime();
1255
[2625]1256        cout << "\n***********************\n"
[2643]1257                << "computing view cell " << mProcessedViewCells
[2625]1258                << " (id: " << mCurrentViewCell->GetId() << ")" << endl;
1259
1260        // compute the pvs of the current view cell
1261        ProcessViewCell();
1262
1263        mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
1264        mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
1265
1266        if (!mUseKdPvs)
1267        {
1268                ObjectContainer objectPvs;
1269
1270                // optain object pvs
1271                GetObjectPvs(objectPvs);
1272
1273                // add pvs
1274                ObjectContainer::const_iterator it, it_end = objectPvs.end();
1275
1276                for (it = objectPvs.begin(); it != it_end; ++ it)
1277                {
1278                        mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
1279                }
1280
1281                cout << "triangle pvs of " << (int)mTrianglePvs.size()
1282                        << " was converted to object pvs of " << (int)objectPvs.size() << endl;
1283
1284                mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
1285        }
1286        else
1287        {
[2643]1288                mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();
[2648]1289                mGvsStats.mPvsCost = mCurrentViewCell->GetPvs().EvalPvsCost();
[2643]1290
[2648]1291                cout << "id: " << mCurrentViewCell->GetId() << " pvs cost: "
1292                         << mGvsStats.mPvsCost << " pvs tri: " << mTrianglePvs.size() << endl;
[2625]1293        }
1294
1295
1296        ////////
1297        //-- stats
1298
1299        mGvsStats.mViewCells = mProcessedViewCells;//mPass;
1300        mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
1301        mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples;
1302
1303        // timing
1304        const long currentTime = GetTime();
1305
1306        mGvsStats.mTimePerViewCell = TimeDiff(startTime, currentTime) * 1e-3f;
1307        mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
1308
1309        mGvsStats.Stop();
1310        mGvsStats.Print(mGvsStatsStream);
1311
1312        mTrianglePvs.clear();
[1934]1313}
[2625]1314
1315}
Note: See TracBrowser for help on using the repository browser.