source: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp @ 382

Revision 382, 5.2 KB checked in by bittner, 19 years ago (diff)

vsspreprocessor update

Line 
1#include "SceneGraph.h"
2#include "KdTree.h"
3#include "VssPreprocessor.h"
4#include "X3dExporter.h"
5#include "Environment.h"
6#include "MutualVisibility.h"
7#include "Polygon3.h"
8#include "ViewCell.h"
9#include "VssRay.h"
10#include "VssTree.h"
11
12VssPreprocessor::VssPreprocessor():
13        mPass(0),
14        mVssRays()
15{
16  // this should increase coherence of the samples
17  environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
18  environment->GetIntValue("VssPreprocessor.totalSamples", mTotalSamples);
19  mStats.open("stats.log");
20}
21
22VssPreprocessor::~VssPreprocessor()
23{
24        CLEAR_CONTAINER(mVssRays);
25}
26
27void
28VssPreprocessor::SetupRay(Ray &ray,
29                                                                                                        const Vector3 &point,
30                                                                                                        const Vector3 &direction
31                                                                                                        )
32{
33  ray.intersections.clear();
34        // do not store anything else then intersections at the ray
35  ray.Init(point, direction, Ray::LOCAL_RAY);
36}
37
38VssRay *
39VssPreprocessor::CastRay(
40                                                                                                 Vector3 &viewPoint,
41                                                                                                 Vector3 &direction
42                                                                                                 )
43{
44        static Ray ray;
45        AxisAlignedBox3 box = mKdTree->GetBox();
46       
47        SetupRay(ray, viewPoint, direction);
48        // cast ray to KD tree to find intersection with other objects
49        Intersectable *objectA, *objectB;
50        Vector3 pointA, pointB;
51        float bsize = Magnitude(box.Size());
52        if (mKdTree->CastRay(ray)) {
53                objectA = ray.intersections[0].mObject;
54                pointA = ray.Extrap(ray.intersections[0].mT);
55        } else {
56                objectA = NULL;
57                // compute intersection with the scene bounding box
58                float tmin, tmax;
59                box.ComputeMinMaxT(ray, &tmin, &tmax);
60                if (tmax > bsize) {
61                        cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl;
62                        cerr<<"ray"<<ray<<endl;
63                }
64                pointA = ray.Extrap(tmax);
65               
66        }
67       
68        SetupRay(ray, viewPoint, -direction);
69       
70        if (mKdTree->CastRay(ray)) {
71                objectB = ray.intersections[0].mObject;
72          pointB = ray.Extrap(ray.intersections[0].mT);
73        } else {
74                objectB = NULL;
75                float tmin, tmax;
76                box.ComputeMinMaxT(ray, &tmin, &tmax);
77                if (tmax > bsize) {
78                        cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl;
79                        cerr<<"ray"<<ray<<endl;
80                }
81               
82                pointB = ray.Extrap(tmax);
83        }
84
85        VssRay *vssRay  = NULL;
86       
87        if (objectA || objectB) {
88                vssRay = new VssRay(pointA,
89                                                                                                pointB,
90                                                                                                objectA,
91                                                                                                objectB);
92        }
93       
94        return vssRay;
95}
96
97
98Vector3
99VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
100{
101        AxisAlignedBox3 box;
102       
103        if (viewSpaceBox)
104                box =*viewSpaceBox;
105        else
106                box = mKdTree->GetBox();
107       
108        // shrink the box in the y direction
109        return box.GetRandomPoint();
110}
111
112Vector3
113VssPreprocessor::GetDirection(const Vector3 &viewpoint)
114{
115        int i = RandomValue(0, mObjects.size()-1);
116        Intersectable *object = mObjects[i];
117        Vector3 point, normal;
118        object->GetRandomSurfacePoint(point, normal);
119        return point - viewpoint;
120}
121
122
123
124bool
125VssPreprocessor::ComputeVisibility()
126{
127 
128  mSceneGraph->CollectObjects(&mObjects);
129        cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<mObjects.size()<<endl;
130       
131  long startTime = GetTime();
132 
133  int i;
134  int totalSamples = 0;
135
136        AxisAlignedBox3 *viewSpaceBox = NULL;
137
138        AxisAlignedBox3 box = mKdTree->GetBox();
139        box.Enlarge(box.Size()*-Vector3(0.45, 0.45, 0.45));
140
141        bool useViewSpaceBox = false;
142        if (useViewSpaceBox)
143                viewSpaceBox = &box;
144       
145  while (totalSamples < mTotalSamples) {
146                int passContributingSamples = 0;
147                int passSampleContributions = 0;
148                int passSamples = 0;
149                int index = 0;
150               
151                int sampleContributions;
152               
153               
154                for (int k=0; k < mSamplesPerPass; k++) {
155                       
156                        Vector3 viewpoint = GetViewpoint(viewSpaceBox);
157                        Vector3 direction = GetDirection(viewpoint);
158                       
159                        VssRay *vssRay = CastRay(viewpoint, direction);
160                       
161                        if (vssRay) {
162                                sampleContributions = vssRay->HitCount();
163                                mVssRays.push_back(vssRay);
164                        }
165                       
166                        //-- CORR matt: put block inside loop
167                        if (sampleContributions) {
168                                passContributingSamples ++;
169                                passSampleContributions += sampleContributions;
170                        }
171                        passSamples++;
172                        totalSamples++;
173                }
174   
175                mPass++;
176               
177                int pvsSize = 0;
178                float avgRayContrib = (passContributingSamples > 0) ?
179                        passSampleContributions/(float)passContributingSamples : 0;
180               
181                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
182                cout << "#TotalSamples=" << totalSamples/1000
183                                 << "k   #SampleContributions=" << passSampleContributions << " ("
184                                 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
185                                 << pvsSize/(float)mObjects.size() << endl
186                                 << "avg ray contrib=" << avgRayContrib << endl;
187               
188                mStats <<
189                        "#Pass\n" <<mPass<<endl<<
190                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
191                        "#TotalSamples\n" << totalSamples<< endl<<
192                        "#SampleContributions\n" << passSampleContributions << endl <<
193                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
194                        "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
195                        "#AvgRayContrib\n" << avgRayContrib << endl;
196        }
197       
198        cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
199        cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush;
200
201        VssTree *vssTree = new VssTree;
202
203        vssTree->Construct(mVssRays, viewSpaceBox);
204       
205  return true;
206}
207
Note: See TracBrowser for help on using the repository browser.