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 | #include "RayCaster.h"
|
---|
10 | #include "Exporter.h"
|
---|
11 | #include "SamplingStrategy.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | namespace GtpVisibilityPreprocessor
|
---|
15 | {
|
---|
16 |
|
---|
17 | #define GVS_DEBUG 1
|
---|
18 |
|
---|
19 | struct VizStruct
|
---|
20 | {
|
---|
21 | Polygon3 *enlargedTriangle;
|
---|
22 | Triangle3 originalTriangle;
|
---|
23 | VssRay *ray;
|
---|
24 | };
|
---|
25 |
|
---|
26 | static vector<VizStruct> vizContainer;
|
---|
27 |
|
---|
28 | GvsPreprocessor::GvsPreprocessor():
|
---|
29 | Preprocessor(),
|
---|
30 | mSamplingType(SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION),
|
---|
31 | mNumViewCells(0),
|
---|
32 | mCurrentViewCell(NULL)
|
---|
33 | {
|
---|
34 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
35 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
36 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
37 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
38 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);
|
---|
39 | Environment::GetSingleton()->GetBoolValue("GvsPreprocessor.UsePerViewCellSampling", mPerViewCell);
|
---|
40 |
|
---|
41 | char gvsStatsLog[100];
|
---|
42 | Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
|
---|
43 | mGvsStatsStream.open(gvsStatsLog);
|
---|
44 |
|
---|
45 | Debug << "Gvs preprocessor options" << endl;
|
---|
46 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
47 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
48 | Debug << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
49 | Debug << "threshold: " << mThreshold << endl;
|
---|
50 | Debug << "epsilon: " << mEps << endl;
|
---|
51 | Debug << "stats: " << gvsStatsLog << endl;
|
---|
52 |
|
---|
53 | if (0)
|
---|
54 | mOnlyRandomSampling = true;
|
---|
55 | else
|
---|
56 | mOnlyRandomSampling = false;
|
---|
57 |
|
---|
58 | mGvsStats.Reset();
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | bool GvsPreprocessor::NextViewCell()
|
---|
63 | {
|
---|
64 | if (mViewCellsManager->GetNumViewCells() == mNumViewCells)
|
---|
65 | return false; // no more view cells
|
---|
66 |
|
---|
67 | mCurrentViewCell = mViewCellsManager->GetViewCell(mNumViewCells);
|
---|
68 | mViewCellsManager->CreateMesh(mCurrentViewCell);
|
---|
69 |
|
---|
70 | ++ mNumViewCells;
|
---|
71 |
|
---|
72 | return true;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | bool GvsPreprocessor::CheckDiscontinuity(const VssRay ¤tRay,
|
---|
77 | const Triangle3 &hitTriangle,
|
---|
78 | const VssRay &oldRay)
|
---|
79 | {
|
---|
80 | // the predicted hitpoint: we expect to hit the same mesh again
|
---|
81 | const Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
82 |
|
---|
83 | const float predictedLen = Magnitude(predictedHit - currentRay.mOrigin);
|
---|
84 | const float len = Magnitude(currentRay.mTermination - currentRay.mOrigin);
|
---|
85 |
|
---|
86 | // distance large => this is likely to be a discontinuity
|
---|
87 | #if 1
|
---|
88 | if ((predictedLen - len) > mThreshold)
|
---|
89 | #else // rather use relative distance
|
---|
90 | if ((predictedLen / len) > mThreshold)
|
---|
91 | #endif
|
---|
92 | {
|
---|
93 | //cout << "r";
|
---|
94 | // apply reverse sampling to find the gap
|
---|
95 | VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
|
---|
96 |
|
---|
97 | if (!newRay)
|
---|
98 | return false;
|
---|
99 |
|
---|
100 | // set flag for visualization
|
---|
101 | newRay->mFlags |= VssRay::ReverseSample;
|
---|
102 |
|
---|
103 | // if ray is not further processed => delete ray
|
---|
104 | if (!HandleRay(newRay))
|
---|
105 | {
|
---|
106 | delete newRay;
|
---|
107 | }
|
---|
108 | else if (GVS_DEBUG && (mVssRays.size() < 9))
|
---|
109 | {
|
---|
110 | mVssRays.push_back(new VssRay(oldRay));
|
---|
111 | mVssRays.push_back(new VssRay(currentRay));
|
---|
112 | mVssRays.push_back(new VssRay(*newRay));
|
---|
113 | }
|
---|
114 |
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | bool GvsPreprocessor::HandleRay(VssRay *vssRay)
|
---|
123 | {
|
---|
124 | // compute the contribution to the view cells
|
---|
125 | const bool storeRaysForViz = GVS_DEBUG;
|
---|
126 |
|
---|
127 | if (!mPerViewCell)
|
---|
128 | {
|
---|
129 | mViewCellsManager->ComputeSampleContribution(*vssRay,
|
---|
130 | true,
|
---|
131 | storeRaysForViz);
|
---|
132 | }
|
---|
133 | else
|
---|
134 | {
|
---|
135 | mViewCellsManager->ComputeSampleContribution(*vssRay,
|
---|
136 | true,
|
---|
137 | mCurrentViewCell);
|
---|
138 | }
|
---|
139 |
|
---|
140 | // some pvs contribution for this ray?
|
---|
141 | if (!vssRay->mPvsContribution)
|
---|
142 | return false;
|
---|
143 |
|
---|
144 | // add new ray to ray queue
|
---|
145 | mRayQueue.push(vssRay);
|
---|
146 |
|
---|
147 | if (storeRaysForViz)
|
---|
148 | {
|
---|
149 | VssRay *nray = new VssRay(*vssRay);
|
---|
150 | nray->mFlags = vssRay->mFlags;
|
---|
151 |
|
---|
152 | // store ray in contributing view cell
|
---|
153 | ViewCellContainer::const_iterator vit, vit_end = vssRay->mViewCells.end();
|
---|
154 | for (vit = vssRay->mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
155 | {
|
---|
156 | (*vit)->GetOrCreateRays()->push_back(nray);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | ++ mGvsStats.mPassContribution;
|
---|
161 |
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /** Creates 3 new vertices for triangle vertex with specified index.
|
---|
167 | */
|
---|
168 | void GvsPreprocessor::CreateDisplacedVertices(VertexContainer &vertices,
|
---|
169 | const Triangle3 &hitTriangle,
|
---|
170 | const VssRay &ray,
|
---|
171 | const int index) const
|
---|
172 | {
|
---|
173 | const int indexU = (index + 1) % 3;
|
---|
174 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
175 |
|
---|
176 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetOrigin();
|
---|
177 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
178 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
179 |
|
---|
180 | const float len = Magnitude(a);
|
---|
181 |
|
---|
182 | const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
|
---|
183 | const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1))
|
---|
184 | const Vector3 dir3 = DotProd(dir2, dir1) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
185 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir1) + CrossProd(dir2, a));
|
---|
186 |
|
---|
187 | // compute the new three hit points
|
---|
188 | // pi, i + 1: pi+ e·|pi-xp|·di, j
|
---|
189 | const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1;
|
---|
190 | // pi, i - 1: pi+ e·|pi-xp|·di, j
|
---|
191 | const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2;
|
---|
192 | // pi, i: pi+ e·|pi-xp|·di, j
|
---|
193 | const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3;
|
---|
194 |
|
---|
195 | vertices.push_back(pt2);
|
---|
196 | vertices.push_back(pt3);
|
---|
197 | vertices.push_back(pt1);
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
|
---|
202 | const Triangle3 &hitTriangle,
|
---|
203 | const VssRay &ray) const
|
---|
204 | {
|
---|
205 | CreateDisplacedVertices(vertices, hitTriangle, ray, 0);
|
---|
206 | CreateDisplacedVertices(vertices, hitTriangle, ray, 1);
|
---|
207 | CreateDisplacedVertices(vertices, hitTriangle, ray, 2);
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | Vector3 GvsPreprocessor::CalcPredictedHitPoint(const VssRay &newRay,
|
---|
212 | const Triangle3 &hitTriangle,
|
---|
213 | const VssRay &oldRay) const
|
---|
214 | {
|
---|
215 | // find the intersection of the plane induced by the
|
---|
216 | // hit triangle with the new ray
|
---|
217 | Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
218 |
|
---|
219 | const Vector3 hitPt =
|
---|
220 | plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
|
---|
221 |
|
---|
222 | return hitPt;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | static bool EqualVisibility(const VssRay &a, const VssRay &b)
|
---|
227 | {
|
---|
228 | return a.mTerminationObject == b.mTerminationObject;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
233 | const Vector3 &p1,
|
---|
234 | const Vector3 &p2,
|
---|
235 | const VssRay &ray1,
|
---|
236 | const VssRay &ray2,
|
---|
237 | const VssRay &oldRay)
|
---|
238 | {
|
---|
239 | CheckDiscontinuity(ray1, hitTriangle, oldRay);
|
---|
240 | CheckDiscontinuity(ray2, hitTriangle, oldRay);
|
---|
241 |
|
---|
242 | if (EqualVisibility(ray1, ray2) || (Magnitude(p1 - p2) <= mEps))
|
---|
243 | {
|
---|
244 | return 0;
|
---|
245 | }
|
---|
246 | else
|
---|
247 | {
|
---|
248 | // the new subdivision point
|
---|
249 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
250 |
|
---|
251 | //cout << "tobj " << ray1.mTerminationObject << " " << ray2.mTerminationObject << " " << p1 << " " << p2 << endl;
|
---|
252 | //cout << "term " << ray1.mTermination << " " << ray2.mTermination << endl;
|
---|
253 |
|
---|
254 | // cast ray into the new point
|
---|
255 | SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f);
|
---|
256 |
|
---|
257 | VssRay *newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox());
|
---|
258 |
|
---|
259 | if (!newRay) return 0;
|
---|
260 |
|
---|
261 | newRay->mFlags |= VssRay::BorderSample;
|
---|
262 |
|
---|
263 | // add new ray to queue
|
---|
264 | const bool enqueued = HandleRay(newRay);
|
---|
265 |
|
---|
266 | // subdivide further
|
---|
267 | const int samples1 = SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay);
|
---|
268 | const int samples2 = SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay);
|
---|
269 |
|
---|
270 | // this ray will not be further processed
|
---|
271 | if (!enqueued)
|
---|
272 | delete newRay;
|
---|
273 |
|
---|
274 | return samples1 + samples2 + 1;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay ¤tRay)
|
---|
280 | {
|
---|
281 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
282 | Triangle3 hitTriangle;
|
---|
283 |
|
---|
284 | // other types not implemented yet
|
---|
285 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
286 | {
|
---|
287 | hitTriangle = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
288 | }
|
---|
289 | else
|
---|
290 | {
|
---|
291 | cout << "not yet implemented" << endl;
|
---|
292 | }
|
---|
293 |
|
---|
294 | VertexContainer enlargedTriangle;
|
---|
295 |
|
---|
296 | /// create 3 new hit points for each vertex
|
---|
297 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
298 |
|
---|
299 | /// create rays from sample points and handle them
|
---|
300 | SimpleRayContainer simpleRays;
|
---|
301 | simpleRays.reserve(9);
|
---|
302 |
|
---|
303 | //cout << "currentRay: " << currentRay.mOrigin << " dir: " << currentRay.GetDir() << endl;
|
---|
304 |
|
---|
305 | VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
|
---|
306 |
|
---|
307 | for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
|
---|
308 | {
|
---|
309 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
310 | SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
|
---|
311 | simpleRays.AddRay(sr);
|
---|
312 |
|
---|
313 | //cout << "new: " << sr.mOrigin << " dist: " << sr.mDirection << endl;
|
---|
314 | }
|
---|
315 |
|
---|
316 | if (0)
|
---|
317 | {
|
---|
318 | // visualize enlarged triangles
|
---|
319 | VizStruct dummy;
|
---|
320 | dummy.enlargedTriangle = new Polygon3(enlargedTriangle);
|
---|
321 | dummy.originalTriangle = hitTriangle;
|
---|
322 | vizContainer.push_back(dummy);
|
---|
323 | }
|
---|
324 |
|
---|
325 | // cast rays to triangle vertices and determine visibility
|
---|
326 | VssRayContainer vssRays;
|
---|
327 |
|
---|
328 | // don't cast double rays as we need only the forward rays
|
---|
329 | const bool castDoubleRays = false;
|
---|
330 | // cannot prune invalid rays because we have to
|
---|
331 | // compare adjacent rays.
|
---|
332 | const bool pruneInvalidRays = false;
|
---|
333 |
|
---|
334 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
335 |
|
---|
336 | // set flags
|
---|
337 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
338 | for (rit = vssRays.begin(); rit != rit_end; ++ rit)
|
---|
339 | {
|
---|
340 | (*rit)->mFlags |= VssRay::BorderSample;
|
---|
341 | }
|
---|
342 |
|
---|
343 | // handle rays
|
---|
344 | EnqueueRays(vssRays);
|
---|
345 |
|
---|
346 | const int n = (int)enlargedTriangle.size();
|
---|
347 | int castRays = (int)vssRays.size();
|
---|
348 |
|
---|
349 | // recursivly subdivide each edge
|
---|
350 | for (int i = 0; i < n; ++ i)
|
---|
351 | {
|
---|
352 | castRays += SubdivideEdge(hitTriangle,
|
---|
353 | enlargedTriangle[i],
|
---|
354 | enlargedTriangle[(i + 1) % n],
|
---|
355 | *vssRays[i],
|
---|
356 | *vssRays[(i + 1) % n],
|
---|
357 | currentRay);
|
---|
358 | }
|
---|
359 |
|
---|
360 | mGvsStats.mBorderSamples += castRays;
|
---|
361 |
|
---|
362 | return castRays;
|
---|
363 | }
|
---|
364 |
|
---|
365 |
|
---|
366 | Vector3 GvsPreprocessor::GetPassingPoint(const VssRay ¤tRay,
|
---|
367 | const Triangle3 &occluder,
|
---|
368 | const VssRay &oldRay) const
|
---|
369 | {
|
---|
370 | //-- The plane p = (xp, hit(x), hit(xold)) is intersected
|
---|
371 | //-- with the newly found occluder (xold is the previous ray from
|
---|
372 | //-- which x was generated). On the intersecting line, we select a point
|
---|
373 | //-- pnew which lies just outside of the new triangle so the ray
|
---|
374 | //-- just passes through the gap
|
---|
375 |
|
---|
376 | const Plane3 plane(currentRay.GetOrigin(),
|
---|
377 | currentRay.GetTermination(),
|
---|
378 | oldRay.GetTermination());
|
---|
379 |
|
---|
380 | Vector3 pt1, pt2;
|
---|
381 |
|
---|
382 | const bool intersects = occluder.GetPlaneIntersection(plane, pt1, pt2);
|
---|
383 |
|
---|
384 | if (!intersects)
|
---|
385 | cerr << "big error!! no intersection" << endl;
|
---|
386 |
|
---|
387 | // get the intersection point on the old ray
|
---|
388 | const Plane3 triPlane(occluder.GetNormal(), occluder.mVertices[0]);
|
---|
389 |
|
---|
390 | const float t = triPlane.FindT(oldRay.mOrigin, oldRay.mTermination);
|
---|
391 | const Vector3 pt3 = oldRay.mOrigin + t * (oldRay.mTermination - oldRay.mOrigin);
|
---|
392 |
|
---|
393 | // Evaluate new hitpoint just outside the triangle
|
---|
394 | Vector3 newPoint;
|
---|
395 |
|
---|
396 | const float eps = mEps;
|
---|
397 | // the point is chosen to be on the side closer to the original ray
|
---|
398 | if (Distance(pt1, pt3) < Distance(pt2, pt3))
|
---|
399 | {
|
---|
400 | newPoint = pt1 + eps * (pt1 - pt2);
|
---|
401 | }
|
---|
402 | else
|
---|
403 | {
|
---|
404 | newPoint = pt2 + eps * (pt2 - pt1);
|
---|
405 | }
|
---|
406 |
|
---|
407 | //cout << "passing point: " << newPoint << endl << endl;
|
---|
408 | return newPoint;
|
---|
409 | }
|
---|
410 |
|
---|
411 |
|
---|
412 | VssRay *GvsPreprocessor::ReverseSampling(const VssRay ¤tRay,
|
---|
413 | const Triangle3 &hitTriangle,
|
---|
414 | const VssRay &oldRay)
|
---|
415 | {
|
---|
416 | // get triangle occluding the path to the hit mesh
|
---|
417 | Triangle3 occluder;
|
---|
418 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
419 |
|
---|
420 | // q: why can this happen?
|
---|
421 | if (!tObj)
|
---|
422 | return NULL;
|
---|
423 |
|
---|
424 | // other types not implemented yet
|
---|
425 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
426 | occluder = dynamic_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
427 | else
|
---|
428 | cout << "not yet implemented" << endl;
|
---|
429 |
|
---|
430 | // get a point which is passing just outside of the occluder
|
---|
431 | const Vector3 newPoint = GetPassingPoint(currentRay, occluder, oldRay);
|
---|
432 | const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
433 |
|
---|
434 | //-- Construct the mutated ray with xnew,
|
---|
435 | //-- dir = predicted(x)- pnew as direction vector
|
---|
436 | const Vector3 newDir = predicted - newPoint;
|
---|
437 |
|
---|
438 | // take xnew, p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
|
---|
439 | // difficult to say!!
|
---|
440 | const float offset = 0.5f;
|
---|
441 | const Vector3 newOrigin = newPoint - newDir * offset;
|
---|
442 |
|
---|
443 | static Ray ray(newOrigin, newDir, Ray::LOCAL_RAY);
|
---|
444 | ray.Precompute();
|
---|
445 |
|
---|
446 | // check if ray intersects view cell
|
---|
447 | if (mPerViewCell && !mCurrentViewCell->CastRay(ray))
|
---|
448 | return NULL;
|
---|
449 |
|
---|
450 | const SimpleRay simpleRay(newOrigin, newDir, SamplingStrategy::GVS, 1.0f);
|
---|
451 |
|
---|
452 | VssRay *reverseRay =
|
---|
453 | mRayCaster->CastRay(simpleRay, mViewCellsManager->GetViewSpaceBox());
|
---|
454 |
|
---|
455 | ++ mGvsStats.mReverseSamples;
|
---|
456 |
|
---|
457 | return reverseRay;
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | int GvsPreprocessor::CastInitialSamples(const int numSamples,
|
---|
462 | const int sampleType)
|
---|
463 | {
|
---|
464 | const long startTime = GetTime();
|
---|
465 |
|
---|
466 | // generate simple rays
|
---|
467 | SimpleRayContainer simpleRays;
|
---|
468 | GenerateRays(numSamples, sampleType, simpleRays);
|
---|
469 |
|
---|
470 | // generate vss rays
|
---|
471 | VssRayContainer samples;
|
---|
472 | CastRays(simpleRays, samples, true);
|
---|
473 | // add to ray queue
|
---|
474 | EnqueueRays(samples);
|
---|
475 |
|
---|
476 | //Debug << "generated " << numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
477 | return (int)samples.size();
|
---|
478 | }
|
---|
479 |
|
---|
480 |
|
---|
481 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples)
|
---|
482 | {
|
---|
483 | // add samples to ray queue
|
---|
484 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
485 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
486 | {
|
---|
487 | HandleRay(*vit);
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | int GvsPreprocessor::Pass()
|
---|
493 | {
|
---|
494 | // reset samples
|
---|
495 | int castSamples = 0;
|
---|
496 | mGvsStats.mPassContribution = 0;
|
---|
497 |
|
---|
498 | while (castSamples < mSamplesPerPass)
|
---|
499 | {
|
---|
500 | // Ray queue empty =>
|
---|
501 | // cast a number of uniform samples to fill ray queue
|
---|
502 | castSamples += CastInitialSamples(mInitialSamples, mSamplingType);
|
---|
503 |
|
---|
504 | if (!mOnlyRandomSampling)
|
---|
505 | castSamples += ProcessQueue();
|
---|
506 | }
|
---|
507 |
|
---|
508 | mGvsStats.mTotalContribution += mGvsStats.mPassContribution;
|
---|
509 | return castSamples;
|
---|
510 | }
|
---|
511 |
|
---|
512 |
|
---|
513 | int GvsPreprocessor::ProcessQueue()
|
---|
514 | {
|
---|
515 | int castSamples = 0;
|
---|
516 | ++ mGvsStats.mGvsPass;
|
---|
517 |
|
---|
518 | while (!mRayQueue.empty())
|
---|
519 | {
|
---|
520 | // handle next ray
|
---|
521 | VssRay *ray = mRayQueue.top();
|
---|
522 | mRayQueue.pop();
|
---|
523 |
|
---|
524 | castSamples += AdaptiveBorderSampling(*ray);
|
---|
525 | delete ray;
|
---|
526 | }
|
---|
527 |
|
---|
528 | return castSamples;
|
---|
529 | }
|
---|
530 |
|
---|
531 |
|
---|
532 | void GvsPreprocessor::VisualizeViewCells()
|
---|
533 | {
|
---|
534 | char str[64]; sprintf(str, "tmp/pass%06d_%04d-", mNumViewCells, mPass);
|
---|
535 |
|
---|
536 | // visualization
|
---|
537 | if (mGvsStats.mPassContribution > 0)
|
---|
538 | {
|
---|
539 | const bool exportRays = true;
|
---|
540 | const bool exportPvs = true;
|
---|
541 |
|
---|
542 | mViewCellsManager->ExportSingleViewCells(mObjects,
|
---|
543 | 10,
|
---|
544 | false,
|
---|
545 | exportPvs,
|
---|
546 | exportRays,
|
---|
547 | 1000,
|
---|
548 | str);
|
---|
549 | }
|
---|
550 |
|
---|
551 | // remove pass samples
|
---|
552 | ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
|
---|
553 |
|
---|
554 | for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
|
---|
555 | {
|
---|
556 | (*vit)->DelRayRefs();
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 | void GvsPreprocessor::PerViewCellComputation()
|
---|
562 | {
|
---|
563 | const int maxViewCells = 5;
|
---|
564 |
|
---|
565 | while (NextViewCell() && mNumViewCells < maxViewCells)
|
---|
566 | {
|
---|
567 | cout << "processing view cell " << mNumViewCells << endl;
|
---|
568 |
|
---|
569 | while (mGvsStats.mTotalSamples < mTotalSamples)
|
---|
570 | {
|
---|
571 | ++ mPass;
|
---|
572 |
|
---|
573 | mGvsStats.mTotalSamples += Pass();
|
---|
574 |
|
---|
575 | ////////
|
---|
576 | //-- stats
|
---|
577 |
|
---|
578 | cout << "\nPass " << mPass << " #samples: " << mGvsStats.mTotalSamples
|
---|
579 | << " of " << mTotalSamples << endl;
|
---|
580 |
|
---|
581 | mGvsStats.mPass = mPass;
|
---|
582 | mGvsStats.Stop();
|
---|
583 | mGvsStats.Print(mGvsStatsStream);
|
---|
584 | //mViewCellsManager->PrintPvsStatistics(mGvsStats);
|
---|
585 |
|
---|
586 | if (GVS_DEBUG)
|
---|
587 | VisualizeViewCells();
|
---|
588 | }
|
---|
589 |
|
---|
590 | // ComputeRenderError();
|
---|
591 | }
|
---|
592 | }
|
---|
593 |
|
---|
594 |
|
---|
595 | void GvsPreprocessor::GlobalComputation()
|
---|
596 | {
|
---|
597 | while (mGvsStats.mTotalSamples < mTotalSamples)
|
---|
598 | {
|
---|
599 | ++ mPass;
|
---|
600 |
|
---|
601 | mGvsStats.mTotalSamples += Pass();
|
---|
602 |
|
---|
603 | ////////
|
---|
604 | //-- stats
|
---|
605 |
|
---|
606 | cout << "\nPass " << mPass << " #samples: " << mGvsStats.mTotalSamples
|
---|
607 | << " of " << mTotalSamples << endl;
|
---|
608 |
|
---|
609 | mGvsStats.mPass = mPass;
|
---|
610 | mGvsStats.Stop();
|
---|
611 | mGvsStats.Print(mGvsStatsStream);
|
---|
612 | //mViewCellsManager->PrintPvsStatistics(mGvsStats);
|
---|
613 |
|
---|
614 | if (GVS_DEBUG)
|
---|
615 | VisualizeViewCells();
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 |
|
---|
620 | bool GvsPreprocessor::ComputeVisibility()
|
---|
621 | {
|
---|
622 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
623 | const long startTime = GetTime();
|
---|
624 |
|
---|
625 | Randomize(0);
|
---|
626 |
|
---|
627 | mGvsStats.Reset();
|
---|
628 | mGvsStats.Start();
|
---|
629 |
|
---|
630 | if (!mLoadViewCells)
|
---|
631 | {
|
---|
632 | /// construct the view cells from the scratch
|
---|
633 | ConstructViewCells();
|
---|
634 | // reset pvs already gathered during view cells construction
|
---|
635 | mViewCellsManager->ResetPvs();
|
---|
636 | cout << "finished view cell construction" << endl;
|
---|
637 | }
|
---|
638 | else if (0)
|
---|
639 | {
|
---|
640 | //-- test successful view cells loading by exporting them again
|
---|
641 | VssRayContainer dummies;
|
---|
642 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
643 | mViewCellsManager->ExportViewCells("test.xml.gz", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
644 | }
|
---|
645 |
|
---|
646 | mGvsStats.Stop();
|
---|
647 | mGvsStats.Print(mGvsStatsStream);
|
---|
648 |
|
---|
649 | if (mPerViewCell)
|
---|
650 | {
|
---|
651 | PerViewCellComputation();
|
---|
652 | }
|
---|
653 | else
|
---|
654 | {
|
---|
655 | GlobalComputation();
|
---|
656 | }
|
---|
657 |
|
---|
658 | cout << "cast " << 2 * mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M rays/s" << endl;
|
---|
659 |
|
---|
660 | if (GVS_DEBUG)
|
---|
661 | {
|
---|
662 | Visualize();
|
---|
663 | CLEAR_CONTAINER(mVssRays);
|
---|
664 | }
|
---|
665 |
|
---|
666 | return true;
|
---|
667 | }
|
---|
668 |
|
---|
669 |
|
---|
670 | void GvsPreprocessor::Visualize()
|
---|
671 | {
|
---|
672 | Exporter *exporter = Exporter::GetExporter("gvs.wrl");
|
---|
673 |
|
---|
674 | if (!exporter)
|
---|
675 | return;
|
---|
676 |
|
---|
677 | vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end();
|
---|
678 | for (vit = vizContainer.begin(); vit != vit_end; ++ vit)
|
---|
679 | {
|
---|
680 | exporter->SetWireframe();
|
---|
681 | exporter->ExportPolygon((*vit).enlargedTriangle);
|
---|
682 | //Material m;
|
---|
683 | exporter->SetFilled();
|
---|
684 | Polygon3 poly = Polygon3((*vit).originalTriangle);
|
---|
685 | exporter->ExportPolygon(&poly);
|
---|
686 | }
|
---|
687 |
|
---|
688 | VssRayContainer::const_iterator rit, rit_end = mVssRays.end();
|
---|
689 | for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
690 | {
|
---|
691 | Intersectable *obj = (*rit)->mTerminationObject;
|
---|
692 | exporter->ExportIntersectable(obj);
|
---|
693 | }
|
---|
694 |
|
---|
695 | VssRayContainer vcRays, vcRays2, vcRays3;
|
---|
696 |
|
---|
697 | // prepare some rays for output
|
---|
698 | for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
699 | {
|
---|
700 | const float p = RandomValue(0.0f, (float)mVssRays.size());
|
---|
701 | if (1)//(p < raysOut)
|
---|
702 | {
|
---|
703 | if ((*rit)->mFlags & VssRay::BorderSample)
|
---|
704 | {
|
---|
705 | vcRays.push_back(*rit);
|
---|
706 | }
|
---|
707 | else if ((*rit)->mFlags & VssRay::ReverseSample)
|
---|
708 | {
|
---|
709 | vcRays2.push_back(*rit);
|
---|
710 | }
|
---|
711 | else
|
---|
712 | {
|
---|
713 | vcRays3.push_back(*rit);
|
---|
714 | }
|
---|
715 | }
|
---|
716 | }
|
---|
717 |
|
---|
718 | exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
|
---|
719 | exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
|
---|
720 | exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
|
---|
721 |
|
---|
722 | //exporter->ExportRays(mVssRays);
|
---|
723 | delete exporter;
|
---|
724 | }
|
---|
725 |
|
---|
726 |
|
---|
727 | void GvsStatistics::Print(ostream &app) const
|
---|
728 | {
|
---|
729 | app << "#Pass\n" << mPass << endl;
|
---|
730 | app << "#Time\n" << Time() << endl;
|
---|
731 | app << "#TotalSamples\n" << mTotalSamples << endl;
|
---|
732 | app << "#ScDiff\n" << mPassContribution << endl;
|
---|
733 | app << "#SamplesContri\n" << mTotalContribution << endl;
|
---|
734 | app << "#ReverseSamples\n" << mReverseSamples << endl;
|
---|
735 | app << "#BorderSamples\n" << mBorderSamples << endl;
|
---|
736 | app << "#GvsRuns\n" << mGvsPass << endl;
|
---|
737 | }
|
---|
738 |
|
---|
739 |
|
---|
740 | }
|
---|