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 |
|
---|
12 | VssPreprocessor::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 |
|
---|
22 | VssPreprocessor::~VssPreprocessor()
|
---|
23 | {
|
---|
24 | CLEAR_CONTAINER(mVssRays);
|
---|
25 | }
|
---|
26 |
|
---|
27 | void
|
---|
28 | VssPreprocessor::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 |
|
---|
38 | VssRay *
|
---|
39 | VssPreprocessor::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 |
|
---|
98 | Vector3
|
---|
99 | VssPreprocessor::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 |
|
---|
112 | Vector3
|
---|
113 | VssPreprocessor::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 |
|
---|
124 | bool
|
---|
125 | VssPreprocessor::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 totalSamples = 0;
|
---|
134 |
|
---|
135 | AxisAlignedBox3 *viewSpaceBox = NULL;
|
---|
136 |
|
---|
137 | AxisAlignedBox3 box = mKdTree->GetBox();
|
---|
138 | box.Enlarge(box.Size()*-Vector3(0.45, 0.45, 0.45));
|
---|
139 |
|
---|
140 | bool useViewSpaceBox = false;
|
---|
141 | if (useViewSpaceBox)
|
---|
142 | viewSpaceBox = &box;
|
---|
143 |
|
---|
144 | while (totalSamples < mTotalSamples) {
|
---|
145 | int passContributingSamples = 0;
|
---|
146 | int passSampleContributions = 0;
|
---|
147 | int passSamples = 0;
|
---|
148 | int index = 0;
|
---|
149 |
|
---|
150 | int sampleContributions;
|
---|
151 |
|
---|
152 |
|
---|
153 | for (int k=0; k < mSamplesPerPass; k++) {
|
---|
154 |
|
---|
155 | Vector3 viewpoint = GetViewpoint(viewSpaceBox);
|
---|
156 | Vector3 direction = GetDirection(viewpoint);
|
---|
157 |
|
---|
158 | VssRay *vssRay = CastRay(viewpoint, direction);
|
---|
159 |
|
---|
160 | if (vssRay) {
|
---|
161 | sampleContributions = vssRay->HitCount();
|
---|
162 | mVssRays.push_back(vssRay);
|
---|
163 | }
|
---|
164 |
|
---|
165 | //-- CORR matt: put block inside loop
|
---|
166 | if (sampleContributions) {
|
---|
167 | passContributingSamples ++;
|
---|
168 | passSampleContributions += sampleContributions;
|
---|
169 | }
|
---|
170 | passSamples++;
|
---|
171 | totalSamples++;
|
---|
172 | }
|
---|
173 |
|
---|
174 | mPass++;
|
---|
175 |
|
---|
176 | int pvsSize = 0;
|
---|
177 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
178 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
179 |
|
---|
180 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
181 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
182 | << "k #SampleContributions=" << passSampleContributions << " ("
|
---|
183 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
184 | << pvsSize/(float)mObjects.size() << endl
|
---|
185 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
186 |
|
---|
187 | mStats <<
|
---|
188 | "#Pass\n" <<mPass<<endl<<
|
---|
189 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
190 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
191 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
192 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
193 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
194 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
195 | }
|
---|
196 |
|
---|
197 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
198 | cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush;
|
---|
199 |
|
---|
200 | VssTree *vssTree = new VssTree;
|
---|
201 |
|
---|
202 | vssTree->Construct(mVssRays, viewSpaceBox);
|
---|
203 |
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 |
|
---|