1 | #include "Environment.h"
|
---|
2 | #include "GvsPreprocessor.h"
|
---|
3 | #include "GlRenderer.h"
|
---|
4 | #include "VssRay.h"
|
---|
5 | #include "ViewCellsManager.h"
|
---|
6 | #include "Triangle3.h"
|
---|
7 | #include "IntersectableWrapper.h"
|
---|
8 | #include "Plane3.h"
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 | namespace GtpVisibilityPreprocessor
|
---|
13 | {
|
---|
14 |
|
---|
15 |
|
---|
16 | GvsPreprocessor::GvsPreprocessor(): Preprocessor(), mSamplingType(0)
|
---|
17 | {
|
---|
18 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
19 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
20 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
21 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
22 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);
|
---|
23 |
|
---|
24 | Debug << "Gvs preprocessor options" << endl;
|
---|
25 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
26 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
27 | Debug << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
28 | Debug << "threshold: " << mThreshold << endl;
|
---|
29 | Debug << "eps: " << mEps << endl;
|
---|
30 |
|
---|
31 | mStats.open("gvspreprocessor.log");
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | bool GvsPreprocessor::CheckDiscontinuity(const VssRay ¤tRay,
|
---|
36 | const Triangle3 &hitTriangle,
|
---|
37 | const VssRay &oldRay)
|
---|
38 | {
|
---|
39 | const float dist = Magnitude(oldRay.GetDir());
|
---|
40 | const float newDist = Magnitude(currentRay.GetDir());
|
---|
41 |
|
---|
42 | #if 0
|
---|
43 | if ((dist - newDist) > mThresHold)
|
---|
44 | #else // rather take relative distance
|
---|
45 | if ((dist / newDist) > mThreshold)
|
---|
46 | #endif
|
---|
47 | {
|
---|
48 | VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
|
---|
49 | HandleRay(*newRay);
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 |
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | bool GvsPreprocessor::HandleRay(VssRay &vssRay)
|
---|
58 | {
|
---|
59 | if (mViewCellsManager->ComputeSampleContribution(vssRay, true, false))
|
---|
60 | {
|
---|
61 | //cout << "h";
|
---|
62 | mRayQueue.push(&vssRay);
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 |
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | /** Creates 3 new vertices for triangle vertex with specified index.
|
---|
71 | */
|
---|
72 | static void CreateNewVertices(VertexContainer &vertices,
|
---|
73 | const Triangle3 &hitTriangle,
|
---|
74 | const VssRay &ray,
|
---|
75 | const int index,
|
---|
76 | const float eps)
|
---|
77 | {
|
---|
78 | const int indexU = (index + 1) % 3;
|
---|
79 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
80 |
|
---|
81 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetDir();
|
---|
82 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
83 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
84 |
|
---|
85 | const float len = Magnitude(a);
|
---|
86 |
|
---|
87 | const Vector3 dir1 = CrossProd(a, b); //N((pi-xp)×(pi+1- pi));
|
---|
88 | const Vector3 dir2 = CrossProd(a, c); // N((pi-xp)×(pi- pi-1))
|
---|
89 | const Vector3 dir3 = DotProd(dir1, dir2) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
90 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir2) + CrossProd(dir1, a));
|
---|
91 |
|
---|
92 | // compute the new three hit points
|
---|
93 | // pi, i + 1: pi+ e·|pi-xp|·di, j
|
---|
94 | const Vector3 pt1 = hitTriangle.mVertices[index] + eps * len * dir1;
|
---|
95 | // pi, i - 1: pi+ e·|pi-xp|·di, j
|
---|
96 | const Vector3 pt2 = hitTriangle.mVertices[index] + eps * len * dir2;
|
---|
97 | // pi, i: pi+ e·|pi-xp|·di, j
|
---|
98 | const Vector3 pt3 = hitTriangle.mVertices[index] + eps * len * dir3;
|
---|
99 |
|
---|
100 | vertices.push_back(pt1);
|
---|
101 | vertices.push_back(pt2);
|
---|
102 | vertices.push_back(pt3);
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
|
---|
107 | const Triangle3 &hitTriangle,
|
---|
108 | const VssRay &ray)
|
---|
109 | {
|
---|
110 | CreateNewVertices(vertices, hitTriangle, ray, 0, mEps);
|
---|
111 | CreateNewVertices(vertices, hitTriangle, ray, 1, mEps);
|
---|
112 | CreateNewVertices(vertices, hitTriangle, ray, 2, mEps);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | static Vector3 CalcPredictedHitPoint(const VssRay &newRay,
|
---|
117 | const Triangle3 &hitTriangle,
|
---|
118 | const VssRay &oldRay)
|
---|
119 | {
|
---|
120 | Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
121 |
|
---|
122 | const Vector3 hitPt =
|
---|
123 | plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
|
---|
124 |
|
---|
125 | return hitPt;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | static bool EqualVisibility(const VssRay &a, const VssRay &b)
|
---|
130 | {
|
---|
131 | return a.mTerminationObject == b.mTerminationObject;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
136 | const Vector3 &p1,
|
---|
137 | const Vector3 &p2,
|
---|
138 | const VssRay &x,
|
---|
139 | const VssRay &y,
|
---|
140 | const VssRay &oldRay)
|
---|
141 | {
|
---|
142 | // the predicted hitpoint expects to hit the same mesh again
|
---|
143 | const Vector3 predictedHitX = CalcPredictedHitPoint(x, hitTriangle, oldRay);
|
---|
144 | const Vector3 predictedHitY = CalcPredictedHitPoint(y, hitTriangle, oldRay);
|
---|
145 |
|
---|
146 | CheckDiscontinuity(x, hitTriangle, oldRay);
|
---|
147 | CheckDiscontinuity(y, hitTriangle, oldRay);
|
---|
148 |
|
---|
149 | if (EqualVisibility(x, y))
|
---|
150 | {
|
---|
151 | return 2;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
156 | SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin);
|
---|
157 | VssRay *newRay = NULL;
|
---|
158 | //VssRay *newRay = CastSingleRay(sray);
|
---|
159 | HandleRay(*newRay);
|
---|
160 |
|
---|
161 | const int s1 = SubdivideEdge(hitTriangle, p1, p, x, *newRay, oldRay);
|
---|
162 | const int s2 = SubdivideEdge(hitTriangle, p, p2, *newRay, y, oldRay);
|
---|
163 |
|
---|
164 | return s1 + s2;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay ¤tRay)
|
---|
170 | {
|
---|
171 | cout << "a";
|
---|
172 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
173 | Triangle3 hitTriangle;
|
---|
174 |
|
---|
175 | // other types not implemented yet
|
---|
176 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
177 | {
|
---|
178 | hitTriangle = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
179 | }
|
---|
180 |
|
---|
181 | VertexContainer enlargedTriangle;
|
---|
182 |
|
---|
183 | /// create 3 new hit points for each vertex
|
---|
184 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
185 |
|
---|
186 | /// create rays from sample points and handle them
|
---|
187 | SimpleRayContainer simpleRays;
|
---|
188 | simpleRays.reserve(9);
|
---|
189 |
|
---|
190 | VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
|
---|
191 |
|
---|
192 | for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
|
---|
193 | {
|
---|
194 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
195 | simpleRays.push_back(SimpleRay(*vit, rayDir));
|
---|
196 | }
|
---|
197 |
|
---|
198 | // establish visibility
|
---|
199 | VssRayContainer vssRays;
|
---|
200 | CastRays(simpleRays, vssRays, false);
|
---|
201 | // add to ray queue
|
---|
202 | EnqueueRays(vssRays);
|
---|
203 | /*
|
---|
204 | // recursivly subdivide each edge
|
---|
205 | for (int i = 0; i < 9; ++ i)
|
---|
206 | {
|
---|
207 | SubdivideEdge(
|
---|
208 | hitTriangle,
|
---|
209 | enlargedTriangle[i],
|
---|
210 | enlargedTriangle[(i + 1) % 9],
|
---|
211 | *vssRays[i],
|
---|
212 | *vssRays[(i + 1) % 9],
|
---|
213 | currentRay);
|
---|
214 | }
|
---|
215 | */
|
---|
216 | return (int)vssRays.size();
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | static Vector3 GetPassingPoint(const VssRay ¤tRay,
|
---|
221 | const Triangle3 &hitTriangle,
|
---|
222 | const VssRay &oldRay)
|
---|
223 | {
|
---|
224 | // intersect triangle plane with plane spanned by current samples
|
---|
225 | Plane3 plane(currentRay.GetOrigin(), currentRay.GetTermination(), oldRay.GetTermination());
|
---|
226 | Plane3 triPlane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
227 |
|
---|
228 | SimpleRay intersectLine = GetPlaneIntersection(plane, triPlane);
|
---|
229 |
|
---|
230 | // Evaluate new hitpoint just outside the triangle
|
---|
231 | const float factor = 0.95f;
|
---|
232 | float t = triPlane.FindT(intersectLine);
|
---|
233 |
|
---|
234 | const Vector3 newPoint = intersectLine.mOrigin + t * factor * intersectLine.mDirection;
|
---|
235 |
|
---|
236 | return newPoint;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | VssRay *GvsPreprocessor::ReverseSampling(const VssRay ¤tRay,
|
---|
241 | const Triangle3 &hitTriangle,
|
---|
242 | const VssRay &oldRay)
|
---|
243 | {
|
---|
244 | cout << "r" << endl;
|
---|
245 | //-- The plane p = (xp, hit(x), hit(xold)) is intersected
|
---|
246 | //-- with the newly found triangle (xold is the previous ray from
|
---|
247 | //-- which x was generated). On the intersecting line, we select a point
|
---|
248 | //-- pnew which lies just outside of the new triangle so the ray
|
---|
249 | //-- just passes by inside the gap
|
---|
250 | const Vector3 newPoint = GetPassingPoint(currentRay, hitTriangle, oldRay);
|
---|
251 | const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
252 |
|
---|
253 | //-- Construct the mutated ray with xnew,dir = predicted(x)- pnew
|
---|
254 | //-- as direction vector
|
---|
255 | const Vector3 newDir = predicted - newPoint ;
|
---|
256 | // take xnew,p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
|
---|
257 | // difficult to say!!
|
---|
258 | const Vector3 newOrigin = newDir * -5000.0f;
|
---|
259 |
|
---|
260 | return new VssRay(currentRay);
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | int GvsPreprocessor::CastInitialSamples(const int numSamples,
|
---|
265 | const int sampleType)
|
---|
266 | {
|
---|
267 | const long startTime = GetTime();
|
---|
268 |
|
---|
269 | // generate simple rays
|
---|
270 | SimpleRayContainer simpleRays;
|
---|
271 | GenerateRays(numSamples, sampleType, simpleRays);
|
---|
272 |
|
---|
273 | // generate vss rays
|
---|
274 | VssRayContainer samples;
|
---|
275 | CastRays(simpleRays, samples, true);
|
---|
276 |
|
---|
277 | // add to ray queue
|
---|
278 | EnqueueRays(samples);
|
---|
279 |
|
---|
280 | Debug << "generated " << numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
281 |
|
---|
282 | return (int)samples.size();
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples)
|
---|
287 | {
|
---|
288 | // add samples to ray queue
|
---|
289 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
290 |
|
---|
291 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
292 | {
|
---|
293 | HandleRay(*(*vit));
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | int GvsPreprocessor::Pass()
|
---|
299 | {
|
---|
300 | int castSamples = 0;
|
---|
301 | const int mSampleType = 0;
|
---|
302 | while (castSamples < mSamplesPerPass)
|
---|
303 | {
|
---|
304 | // Ray queue empty =>
|
---|
305 | // cast a number of uniform samples to fill ray Queue
|
---|
306 | CastInitialSamples(mInitialSamples, mSampleType);
|
---|
307 |
|
---|
308 | const int gvsSamples = ProcessQueue();
|
---|
309 | #if 0
|
---|
310 | castSamples += gvsSamples;
|
---|
311 | #else
|
---|
312 | castSamples += mInitialSamples;
|
---|
313 | #endif
|
---|
314 | //cout << "\ncast " << castSamples << " of " << mSamplesPerPass << endl;
|
---|
315 | }
|
---|
316 |
|
---|
317 | return castSamples;
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 | int GvsPreprocessor::ProcessQueue()
|
---|
322 | {
|
---|
323 | int castSamples = 0;
|
---|
324 |
|
---|
325 | while (!mRayQueue.empty())
|
---|
326 | {
|
---|
327 | // handle next ray
|
---|
328 | VssRay *ray = mRayQueue.top();
|
---|
329 | mRayQueue.pop();
|
---|
330 |
|
---|
331 | castSamples += AdaptiveBorderSampling(*ray);
|
---|
332 | }
|
---|
333 |
|
---|
334 | return castSamples;
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | bool GvsPreprocessor::ComputeVisibility()
|
---|
339 | {
|
---|
340 | Randomize(0);
|
---|
341 | const long startTime = GetTime();
|
---|
342 |
|
---|
343 | mViewSpaceBox = mKdTree->GetBox();
|
---|
344 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
345 |
|
---|
346 | if (!mLoadViewCells)
|
---|
347 | { /// construct the view cells from the scratch
|
---|
348 | ConstructViewCells(mViewSpaceBox);
|
---|
349 | cout << "view cells loaded" << endl;
|
---|
350 | }
|
---|
351 |
|
---|
352 | int castSamples = 0;
|
---|
353 |
|
---|
354 | while (castSamples < mTotalSamples)
|
---|
355 | {
|
---|
356 | const int passSamples = Pass();
|
---|
357 | castSamples += passSamples;
|
---|
358 |
|
---|
359 | ////////
|
---|
360 | //-- stats
|
---|
361 | cout << "+";
|
---|
362 | cout << "\nsamples cast " << passSamples << " (=" << castSamples << " of " << mTotalSamples << ")" << endl;
|
---|
363 | //mVssRays.PrintStatistics(mStats);
|
---|
364 | mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
|
---|
365 | << "#TotalSamples\n" << castSamples << endl;
|
---|
366 |
|
---|
367 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
368 | // ComputeRenderError();
|
---|
369 | }
|
---|
370 |
|
---|
371 | return true;
|
---|
372 | }
|
---|
373 |
|
---|
374 | }
|
---|