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

Revision 1489, 6.8 KB checked in by mattausch, 18 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"
[1460]8
[1473]9
[1460]10namespace GtpVisibilityPreprocessor
11{
12 
13
[1473]14GvsPreprocessor::GvsPreprocessor(): Preprocessor(), mSamplingType(0)
[1460]15{
[1486]16        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
17        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
18        Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
19        Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
20               
[1473]21
[1486]22        Debug << "Gvs preprocessor options" << endl;
23        Debug << "number of total samples: " << mTotalSamples << endl;
24        Debug << "number of initial samples: " << mInitialSamples << endl;
25        Debug << "number of samples per pass: " << mSamplesPerPass << endl;
26
27        mStats.open("gvspreprocessor.log");
[1460]28}
29
[1473]30
[1489]31const bool GvsPreprocessor::DiscontinuityFound(const VssRay &ray,
32                                                                                           const VssRay &oldRay) const
[1460]33{
[1489]34    //const Plane3 plane = tri->GetPlane();
[1486]35        return false;
36}
[1473]37
[1486]38
39int GvsPreprocessor::HandleRay(const GvsRayInfo &gvsRay)
40{
41        VssRay *ray = gvsRay.mRay;
42
43        if (!mViewCellsManager->ComputeSampleContribution(*ray, true, false))
44                return 1;
45
46        if (gvsRay.mFoundDiscontinuity)
[1473]47        {
[1486]48                return ReverseSampling(*ray);
[1473]49        }
50        else
51        {
[1486]52                return AdaptiveBorderSampling(*ray);
[1473]53        }
[1460]54}
55
[1489]56/** Hepler function for adaptive border sampling. It finds
57        new sample points around a triangle in a eps environment
58*/
59static void CreateNewSamples(VertexContainer &samples,
60                                                         const Triangle3 &hitTriangle,
61                                                         const VssRay &ray,
62                                                         const int index,
63                                                         const float eps)
[1460]64{
[1486]65        const int indexU = (index + 1) % 3;
66        const int indexL = (index == 0) ? 2 : index - 1;
67
68        const Vector3 a = hitTriangle.mVertices[index] - ray.GetDir();
69        const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
70        const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
71        const float len = Magnitude(a);
72
73        // compute the new three hit points
74        // pi, i + 1
75        const Vector3 dir1 = CrossProd(a, b); //N((pi-xp)×(pi+1- pi));
76        const Vector3 pt1 = hitTriangle.mVertices[index] + eps * len * dir1; //pi+ e·|pi-xp|·di, j
77        samples.push_back(pt1);
78
79        // pi, i - 1
80    const Vector3 dir2 = CrossProd(a, c); // N((pi-xp)×(pi- pi-1))
81        const Vector3 pt2 = hitTriangle.mVertices[index] + eps * len * dir2; //pi+ e·|pi-xp|·di, j
82        samples.push_back(pt2);
83
84        // pi, i
85        const Vector3 dir3 = DotProd(dir1, dir2) > 0 ?
86                Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir2) + CrossProd(dir1, a)); // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
87        const Vector3 pt3 = hitTriangle.mVertices[index] + eps * len * dir3; //pi+ e·|pi-xp|·di, j
88        samples.push_back(pt3);
89}
90
91
[1489]92/** Generate rays from sample points.
93*/
94static void CreateRays(VssRayContainer &rays,
95                                           const VertexContainer &samples)
[1486]96{
[1489]97        VertexContainer::const_iterator vit, vit_end = samples.end();
98       
99        for (vit = samples.begin(); vit != vit_end; ++ vit)
100        {
101                const Vector3 currentSample = *vit;
102                VssRay *ray;// = new VssRay(ray->mOrigin, currentSample);
103        }
104}
105
106
107int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &prevRay)
108{
[1486]109        cout << "a";
[1489]110        Intersectable *tObj = prevRay.mTerminationObject;
[1486]111        Triangle3 hitTriangle;
[1489]112
113        // other types not implemented yet
114        if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
115        {
116                hitTriangle = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
117        }
118
119        VertexContainer samples;
[1486]120        samples.reserve(9);
121
[1489]122        CreateNewSamples(samples, hitTriangle, prevRay, 0, mEps);
123        CreateNewSamples(samples, hitTriangle, prevRay, 1, mEps);
124        CreateNewSamples(samples, hitTriangle, prevRay, 2, mEps);
[1486]125
[1489]126        VssRayContainer vssRays;
127        CreateRays(vssRays, samples);
128
129        VssRayContainer::const_iterator rit, rit_end = vssRays.end();
[1486]130       
[1489]131        for (rit = vssRays.begin(); rit != rit_end; ++ rit)
[1486]132        {
[1489]133                VssRay *ray = *rit;
[1486]134
[1489]135                // discontinuity found?
136                // schedule for reverse sampling or adaptive border sampling
137                const bool gap = DiscontinuityFound(*ray, prevRay);
138                mRayQueue.push(GvsRayInfo(ray, gap));
[1486]139        }
140
[1489]141        return 9;
[1473]142}
143
144
[1486]145int GvsPreprocessor::ReverseSampling(const VssRay &ray)
[1473]146{
[1486]147        cout << "r" << endl;
[1473]148        // TODO
[1486]149        return 1;
[1473]150}
151
[1489]152
153int GvsPreprocessor::CastInitialSamples(const int numSamples,
154                                                                                const int sampleType)
155{       
[1473]156        const long startTime = GetTime();
157
[1489]158        // generate simple rays
159        SimpleRayContainer simpleRays;
160        GenerateRays(numSamples, sampleType, simpleRays);
161       
162        // generate vss rays
[1473]163        VssRayContainer samples;
[1486]164        CastRays(simpleRays, samples);
[1489]165       
166        // add to ray queue
167        EnqueueSamples(samples);
[1473]168
[1489]169        Debug << "generated " <<  numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
[1473]170
[1489]171        return (int)samples.size();
172}
173
174
175void GvsPreprocessor::EnqueueSamples(VssRayContainer &samples, VssRay *oldRay)
176{
[1486]177        // add samples to ray queue
[1473]178        VssRayContainer::const_iterator vit, vit_end = samples.end();
179        for (vit = samples.begin(); vit != vit_end; ++ vit)
180        {
[1489]181                /// if there is no old ray, no discontinuity
182                const bool gap = oldRay ? DiscontinuityFound(*(*vit), *oldRay) : false;
183                mRayQueue.push(GvsRayInfo(*vit, gap));
[1460]184        }
[1473]185
[1460]186}
187
[1473]188
[1486]189int GvsPreprocessor::Pass()
[1460]190{
[1473]191        int castSamples = 0;
[1489]192        const int mSampleType = 0;
[1486]193        while (castSamples < mSamplesPerPass)
[1473]194        {
195                // Ray queue empty =>
196                // cast a number of uniform samples to fill ray Queue
[1489]197                CastInitialSamples(mInitialSamples, mSampleType);
198
199                const int gvsSamples = ProcessQueue();
[1486]200                castSamples += gvsSamples;
[1489]201                //cout << "\ncast " << castSamples << " of " << mSamplesPerPass << endl;
[1473]202        }
203
204        return castSamples;
[1460]205}
206
[1473]207
[1489]208int GvsPreprocessor::ProcessQueue()
[1460]209{
[1473]210        int castSamples = 0;
211
212        while (!mRayQueue.empty())
213        {
214                // handle next ray
[1489]215                GvsRayInfo rayInfo = mRayQueue.top();
[1473]216                mRayQueue.pop();
217               
[1489]218                castSamples += HandleRay(rayInfo);
[1460]219        }
[1473]220
221        return castSamples;
[1460]222}
223
224
[1489]225bool GvsPreprocessor::ComputeVisibility()
[1460]226{
[1473]227        Randomize(0);
228        const long startTime = GetTime();
229       
[1486]230        mViewSpaceBox = mKdTree->GetBox();
231        cout << "Gvs Preprocessor started\n" << flush;
[1473]232       
[1486]233        if (!mLoadViewCells)
234        {       /// construct the view cells from the scratch
235                ConstructViewCells(mViewSpaceBox);
236                cout << "view cells loaded" << endl;
237        }
[1460]238
[1486]239        int castSamples = 0;
[1460]240
[1486]241        while (castSamples < mTotalSamples)
[1473]242        {
[1486]243                const int passSamples = Pass();
244                castSamples += passSamples;
[1489]245               
246                /////////////
247                // -- stats
[1486]248                cout << "+";
249                cout << "\nsamples cast " << passSamples << " (=" << castSamples << " of " << mTotalSamples << ")" << endl;
250                //mVssRays.PrintStatistics(mStats);
[1489]251                mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
252                           << "#TotalSamples\n" << castSamples << endl;
[1460]253
[1473]254                mViewCellsManager->PrintPvsStatistics(mStats);
255                // ComputeRenderError();
[1460]256        }
257
[1473]258        return true;
[1460]259}
260
261}
Note: See TracBrowser for help on using the repository browser.