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