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 | #include "BvHierarchy.h"
|
---|
13 | #include "Polygon3.h"
|
---|
14 | #include "IntersectableWrapper.h"
|
---|
15 | #include "Timer/PerfTimer.h"
|
---|
16 | #include "Vector2.h"
|
---|
17 | #include "RndGauss.h"
|
---|
18 |
|
---|
19 |
|
---|
20 |
|
---|
21 | namespace GtpVisibilityPreprocessor
|
---|
22 | {
|
---|
23 |
|
---|
24 | #define GVS_DEBUG 0
|
---|
25 | #define NOT_ACCOUNTED_OBJECT 0
|
---|
26 | #define ACCOUNTED_OBJECT 2
|
---|
27 |
|
---|
28 |
|
---|
29 | static const float MIN_DIST = 0.001f;
|
---|
30 |
|
---|
31 | static ObjectContainer myobjects;
|
---|
32 | static int sInvalidSamples = 0;
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 | ///////////
|
---|
37 | //-- timers
|
---|
38 |
|
---|
39 |
|
---|
40 | static PerfTimer rayTimer;
|
---|
41 | static PerfTimer kdPvsTimer;
|
---|
42 | static PerfTimer gvsTimer;
|
---|
43 | static PerfTimer initialTimer;
|
---|
44 | static PerfTimer intersectionTimer;
|
---|
45 | static PerfTimer preparationTimer;
|
---|
46 | static PerfTimer mainLoopTimer;
|
---|
47 | static PerfTimer contributionTimer;
|
---|
48 | static PerfTimer castTimer;
|
---|
49 | static PerfTimer generationTimer;
|
---|
50 |
|
---|
51 |
|
---|
52 | /////////////////////
|
---|
53 |
|
---|
54 |
|
---|
55 | /** Visualization structure for the GVS algorithm.
|
---|
56 | */
|
---|
57 | struct VizStruct
|
---|
58 | {
|
---|
59 | Polygon3 *enlargedTriangle;
|
---|
60 | Triangle3 originalTriangle;
|
---|
61 | VssRay *ray;
|
---|
62 | };
|
---|
63 |
|
---|
64 | static vector<VizStruct> vizContainer;
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | GvsPreprocessor::GvsPreprocessor():
|
---|
70 | Preprocessor(),
|
---|
71 | mProcessedViewCells(0),
|
---|
72 | mCurrentViewCell(NULL),
|
---|
73 | mCurrentViewPoint(Vector3(0.0f, 0.0f, 0.0f)),
|
---|
74 | mOnlyRandomSampling(false),
|
---|
75 | //mOnlyRandomSampling(true),
|
---|
76 | mUseProbablyVisibleSampling(false)
|
---|
77 | {
|
---|
78 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
|
---|
79 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
|
---|
80 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.gvsSamplesPerPass", mGvsSamplesPerPass);
|
---|
81 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.minContribution", mMinContribution);
|
---|
82 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
|
---|
83 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.threshold", mThreshold);
|
---|
84 | Environment::GetSingleton()->GetBoolValue("GvsPreprocessor.perViewCell", mPerViewCell);
|
---|
85 | Environment::GetSingleton()->GetIntValue("GvsPreprocessor.maxViewCells", mMaxViewCells);
|
---|
86 |
|
---|
87 | Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", mEvaluatePixelError);
|
---|
88 |
|
---|
89 | Environment::GetSingleton()->GetBoolValue("ViewCells.useKdPvs", mUseKdPvs);
|
---|
90 |
|
---|
91 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.initialJitter", mInitialJitter);
|
---|
92 | Environment::GetSingleton()->GetBoolValue("GvsPreprocessor.useDeterministicGvs", mUseDeterministicGvs);
|
---|
93 |
|
---|
94 | Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.radiusOfInfluence", mRadiusOfInfluence);
|
---|
95 |
|
---|
96 | char gvsStatsLog[100];
|
---|
97 | Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
|
---|
98 | mGvsStatsStream.open(gvsStatsLog);
|
---|
99 |
|
---|
100 | cout << "number of gvs samples per pass: " << mGvsSamplesPerPass << endl;
|
---|
101 | cout << "number of samples per pass: " << mSamplesPerPass << endl;
|
---|
102 | cout << "only random sampling: " << mOnlyRandomSampling << endl;
|
---|
103 |
|
---|
104 | Debug << "Gvs preprocessor options" << endl;
|
---|
105 | Debug << "number of total samples: " << mTotalSamples << endl;
|
---|
106 | Debug << "number of initial samples: " << mInitialSamples << endl;
|
---|
107 | Debug << "threshold: " << mThreshold << endl;
|
---|
108 | Debug << "epsilon: " << mEps << endl;
|
---|
109 | Debug << "stats: " << gvsStatsLog << endl;
|
---|
110 | Debug << "per view cell: " << mPerViewCell << endl;
|
---|
111 | Debug << "max view cells: " << mMaxViewCells << endl;
|
---|
112 | Debug << "min contribution: " << mMinContribution << endl;
|
---|
113 |
|
---|
114 | mDistribution = new ViewCellBasedDistribution(*this, NULL);
|
---|
115 |
|
---|
116 | mGvsStats.Reset();
|
---|
117 |
|
---|
118 | // hack: some generic statistical values that can be used
|
---|
119 | // by an application using the preprocessor
|
---|
120 | for (int i = 0; i < 2; ++ i)
|
---|
121 | mGenericStats[i] = 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | GvsPreprocessor::~GvsPreprocessor()
|
---|
126 | {
|
---|
127 | delete mDistribution;
|
---|
128 | ClearRayQueue();
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | void GvsPreprocessor::ClearRayQueue()
|
---|
133 | {
|
---|
134 | // clean ray queue
|
---|
135 | while (!mRayQueue.empty())
|
---|
136 | {
|
---|
137 | // handle next ray
|
---|
138 | VssRay *ray = mRayQueue.top();
|
---|
139 | mRayQueue.pop();
|
---|
140 |
|
---|
141 | // note: deletion of the ray is handled by ray pool
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | ViewCell *GvsPreprocessor::NextViewCell()
|
---|
147 | {
|
---|
148 | if (mProcessedViewCells == (int)mViewCells.size())
|
---|
149 | return NULL; // no more view cells
|
---|
150 |
|
---|
151 | ViewCell *vc = mViewCells[mProcessedViewCells];
|
---|
152 |
|
---|
153 | if (!vc->GetMesh())
|
---|
154 | mViewCellsManager->CreateMesh(vc);
|
---|
155 |
|
---|
156 | mGvsStats.mViewCellId = vc->GetId();
|
---|
157 |
|
---|
158 | Debug << "vc: " << vc->GetId() << endl;
|
---|
159 |
|
---|
160 | ++ mProcessedViewCells;
|
---|
161 |
|
---|
162 | return vc;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | int GvsPreprocessor::CheckDiscontinuity(const VssRay ¤tRay,
|
---|
167 | const Triangle3 &hitTriangle,
|
---|
168 | const VssRay &oldRay)
|
---|
169 | {
|
---|
170 | // the predicted hitpoint is the point where the ray would have intersected the hit triangle
|
---|
171 | Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
172 |
|
---|
173 | float predictedLen = Magnitude(predictedHit - currentRay.mOrigin);
|
---|
174 | float len = Magnitude(currentRay.mTermination - currentRay.mOrigin);
|
---|
175 |
|
---|
176 | // discrepancy between predicted and actual hit point large => this is likely to be a discontinuity
|
---|
177 |
|
---|
178 | // matt: problem: this only finds CLOSER discontinuities, but not discontinuities which are FARTHER away
|
---|
179 | if ((predictedLen - len) < mThreshold)
|
---|
180 | // q: rather use relative distance?
|
---|
181 | //if ((predictedLen / len) < mThreshold)
|
---|
182 | return 0;
|
---|
183 |
|
---|
184 | SimpleRay simpleRay;
|
---|
185 |
|
---|
186 | // apply reverse sampling to find the gap
|
---|
187 | if (!ComputeReverseRay(currentRay, hitTriangle, oldRay, simpleRay))
|
---|
188 | return 0;
|
---|
189 |
|
---|
190 | static VssRayContainer reverseRays;
|
---|
191 | reverseRays.clear();
|
---|
192 |
|
---|
193 | if (mUseDeterministicGvs)
|
---|
194 | {
|
---|
195 | VssRay *reverseRay =
|
---|
196 | mRayCaster->CastRay(simpleRay, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
|
---|
197 |
|
---|
198 | reverseRays.push_back(reverseRay);
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | float scale = 0.01f;
|
---|
203 |
|
---|
204 | if (0)
|
---|
205 | CastRayBundle4(simpleRay, reverseRays, mViewCellsManager->GetViewSpaceBox());
|
---|
206 | else
|
---|
207 | CastRayBundle16(simpleRay, reverseRays, scale);
|
---|
208 | }
|
---|
209 |
|
---|
210 | mGvsStats.mReverseSamples += (int)reverseRays.size();
|
---|
211 |
|
---|
212 | EnqueueRays(reverseRays);
|
---|
213 |
|
---|
214 | return (int)reverseRays.size();
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | int GvsPreprocessor::CheckDiscontinuity2(const VssRay ¤tRay,
|
---|
219 | const Triangle3 &hitTriangle,
|
---|
220 | const VssRay &oldRay)
|
---|
221 | {
|
---|
222 | // the predicted hitpoint is the point where the ray would have intersected the hit triangle
|
---|
223 | Vector3 predictedHit = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
224 |
|
---|
225 | float predictedLen = Magnitude(predictedHit - currentRay.mOrigin);
|
---|
226 | float len = Magnitude(currentRay.mTermination - currentRay.mOrigin);
|
---|
227 |
|
---|
228 | // discrepancy between predicted and actual hit point large
|
---|
229 | // => this is likely to be a discontinuity
|
---|
230 | if (fabs(predictedLen - len) < mThreshold)
|
---|
231 | return 0;
|
---|
232 |
|
---|
233 | static SimpleRayContainer simpleRays;
|
---|
234 | simpleRays.clear();
|
---|
235 |
|
---|
236 | CreateRandomizedReverseRays(currentRay.mOrigin, predictedHit, hitTriangle, simpleRays, 16);
|
---|
237 |
|
---|
238 | static VssRayContainer reverseRays;
|
---|
239 | reverseRays.clear();
|
---|
240 | CastRays(simpleRays, reverseRays, false, false);
|
---|
241 |
|
---|
242 | mGvsStats.mReverseSamples += (int)reverseRays.size();
|
---|
243 |
|
---|
244 | EnqueueRays(reverseRays);
|
---|
245 |
|
---|
246 | return (int)reverseRays.size();
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | int GvsPreprocessor::CountObject(Intersectable *triObj)
|
---|
251 | {
|
---|
252 | int numObjects = 0;
|
---|
253 |
|
---|
254 | if ((triObj->mCounter != (NOT_ACCOUNTED_OBJECT + 1)) &&
|
---|
255 | (triObj->mCounter != (ACCOUNTED_OBJECT + 1)))
|
---|
256 | {
|
---|
257 | ++ triObj->mCounter;
|
---|
258 | ++ numObjects;
|
---|
259 | }
|
---|
260 |
|
---|
261 | mGenericStats[1] += numObjects;
|
---|
262 |
|
---|
263 | return numObjects;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | void GvsPreprocessor::UpdateStatsForVisualization(KdIntersectable *kdInt)
|
---|
268 | {
|
---|
269 | int numObj = 0;
|
---|
270 |
|
---|
271 | // count new objects in pvs due to kd node conversion
|
---|
272 | myobjects.clear();
|
---|
273 | // also gather duplicates to avoid mailing
|
---|
274 | mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), myobjects);
|
---|
275 |
|
---|
276 | ObjectContainer::const_iterator oit, oit_end = myobjects.end();
|
---|
277 |
|
---|
278 | for (oit = myobjects.begin(); oit != oit_end; ++ oit)
|
---|
279 | numObj += CountObject(*oit);
|
---|
280 |
|
---|
281 | mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt, numObj);
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | void GvsPreprocessor::AddKdNodeToPvs(const Vector3 &termination)
|
---|
286 | {
|
---|
287 | kdPvsTimer.Entry();
|
---|
288 |
|
---|
289 | // exchange the triangle with the node in the pvs for kd pvs
|
---|
290 | KdNode *node = mKdTree->GetPvsNode(termination);
|
---|
291 |
|
---|
292 | //KdNode *node = mKdTree->GetPvsNodeCheck(ray.mTermination, obj);
|
---|
293 | //if (!node) cerr << "e " << obj->GetBox() << " " << ray.mTermination << endl; else
|
---|
294 | if (!node->Mailed())
|
---|
295 | {
|
---|
296 | // add to pvs
|
---|
297 | node->Mail();
|
---|
298 | KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(node);
|
---|
299 | mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
|
---|
300 |
|
---|
301 | if (QT_VISUALIZATION_SHOWN) UpdateStatsForVisualization(kdInt);
|
---|
302 | }
|
---|
303 |
|
---|
304 | kdPvsTimer.Exit();
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | bool GvsPreprocessor::HasContribution(VssRay &ray)
|
---|
309 | {
|
---|
310 | if (!ray.mTerminationObject)
|
---|
311 | return false;
|
---|
312 |
|
---|
313 | contributionTimer.Entry();
|
---|
314 |
|
---|
315 | bool result;
|
---|
316 |
|
---|
317 | if (!mPerViewCell)
|
---|
318 | {
|
---|
319 | // store the rays + the intersected view cells
|
---|
320 | const bool storeViewCells = false;
|
---|
321 |
|
---|
322 | mViewCellsManager->ComputeSampleContribution(ray,
|
---|
323 | true,
|
---|
324 | storeViewCells,
|
---|
325 | true);
|
---|
326 |
|
---|
327 | result = ray.mPvsContribution > 0;
|
---|
328 | }
|
---|
329 | else // original per view cell gvs
|
---|
330 | {
|
---|
331 | // test if triangle was accounted for yet
|
---|
332 | result = AddTriangleObject(ray.mTerminationObject);
|
---|
333 |
|
---|
334 | if (mUseKdPvs)
|
---|
335 | AddKdNodeToPvs(ray.mTermination);
|
---|
336 | }
|
---|
337 |
|
---|
338 | contributionTimer.Exit();
|
---|
339 |
|
---|
340 | return result;
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | bool GvsPreprocessor::HandleRay(VssRay *vssRay)
|
---|
345 | {
|
---|
346 | if (!vssRay || !HasContribution(*vssRay))
|
---|
347 | return false;
|
---|
348 |
|
---|
349 | if (0 && GVS_DEBUG)
|
---|
350 | mVssRays.push_back(new VssRay(*vssRay));
|
---|
351 |
|
---|
352 | // add new ray to ray queue
|
---|
353 | mRayQueue.push(vssRay);
|
---|
354 |
|
---|
355 | ++ mGvsStats.mTotalContribution;
|
---|
356 |
|
---|
357 | return true;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | /** Creates 3 new vertices for triangle vertex with specified index.
|
---|
362 | */
|
---|
363 | void GvsPreprocessor::CreateDisplacedVertices(VertexContainer &vertices,
|
---|
364 | const Triangle3 &hitTriangle,
|
---|
365 | const VssRay &ray,
|
---|
366 | const int index) const
|
---|
367 | {
|
---|
368 | const int indexU = (index + 1) % 3;
|
---|
369 | const int indexL = (index == 0) ? 2 : index - 1;
|
---|
370 |
|
---|
371 | const Vector3 a = hitTriangle.mVertices[index] - ray.GetOrigin();
|
---|
372 | const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
|
---|
373 | const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
|
---|
374 |
|
---|
375 | const float len = Magnitude(a);
|
---|
376 |
|
---|
377 | const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
|
---|
378 | const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1))
|
---|
379 | const Vector3 dir3 = DotProd(dir2, dir1) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
|
---|
380 | Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir1) + CrossProd(dir2, a));
|
---|
381 |
|
---|
382 | // compute the new three hit points
|
---|
383 | // pi, i + 1: pi+ e·|pi-xp|·di, j
|
---|
384 | const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1;
|
---|
385 | // pi, i - 1: pi+ e·|pi-xp|·di, j
|
---|
386 | const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2;
|
---|
387 | // pi, i: pi+ e·|pi-xp|·di, j
|
---|
388 | const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3;
|
---|
389 |
|
---|
390 | vertices.push_back(pt2);
|
---|
391 | vertices.push_back(pt3);
|
---|
392 | vertices.push_back(pt1);
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | void GvsPreprocessor::EnlargeTriangle(VertexContainer &vertices,
|
---|
397 | const Triangle3 &hitTriangle,
|
---|
398 | const VssRay &ray) const
|
---|
399 | {
|
---|
400 | CreateDisplacedVertices(vertices, hitTriangle, ray, 0);
|
---|
401 | CreateDisplacedVertices(vertices, hitTriangle, ray, 1);
|
---|
402 | CreateDisplacedVertices(vertices, hitTriangle, ray, 2);
|
---|
403 | }
|
---|
404 |
|
---|
405 |
|
---|
406 | Vector3 GvsPreprocessor::CalcPredictedHitPoint(const VssRay &newRay,
|
---|
407 | const Triangle3 &hitTriangle,
|
---|
408 | const VssRay &oldRay) const
|
---|
409 | {
|
---|
410 | // find the intersection of the plane induced by the
|
---|
411 | // hit triangle with the new ray
|
---|
412 | Plane3 plane(hitTriangle.GetNormal(), hitTriangle.mVertices[0]);
|
---|
413 |
|
---|
414 | const Vector3 hitPt =
|
---|
415 | plane.FindIntersection(newRay.mTermination, newRay.mOrigin);
|
---|
416 |
|
---|
417 | return hitPt;
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | static bool EqualVisibility(const VssRay &a, const VssRay &b)
|
---|
422 | {
|
---|
423 | return a.mTerminationObject == b.mTerminationObject;
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | int GvsPreprocessor::SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
428 | const Vector3 &p1,
|
---|
429 | const Vector3 &p2,
|
---|
430 | const VssRay &ray1,
|
---|
431 | const VssRay &ray2,
|
---|
432 | const VssRay &oldRay)
|
---|
433 | {
|
---|
434 | //cout <<"y"<<Magnitude(p1 - p2) << " ";
|
---|
435 | int castRays = 0;
|
---|
436 |
|
---|
437 | if (EqualVisibility(ray1, ray2) || (SqrMagnitude(p1 - p2) <= MIN_DIST))
|
---|
438 | return castRays;
|
---|
439 |
|
---|
440 |
|
---|
441 | // the new subdivision point
|
---|
442 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
443 | //cout << "p: " << p << " " << p1 << " " << p2 << endl;
|
---|
444 |
|
---|
445 | SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f);
|
---|
446 |
|
---|
447 | VssRay *newRay;
|
---|
448 |
|
---|
449 | // cast single ray
|
---|
450 | castTimer.Entry();
|
---|
451 |
|
---|
452 | // cast single ray to check if a triangle was missed. note: not efficient!!
|
---|
453 | newRay = mRayCaster->CastRay(sray, mViewCellsManager->GetViewSpaceBox(), !mPerViewCell);
|
---|
454 | castTimer.Exit();
|
---|
455 |
|
---|
456 | ++ castRays;
|
---|
457 |
|
---|
458 | // this ray will not be further processed
|
---|
459 | // note: ray deletion is handled by ray pool
|
---|
460 | if (!newRay) return castRays;
|
---|
461 |
|
---|
462 | rayTimer.Entry();
|
---|
463 |
|
---|
464 | // new triangle discovered? => add new ray to queue
|
---|
465 | HandleRay(newRay);
|
---|
466 |
|
---|
467 | rayTimer.Exit();
|
---|
468 |
|
---|
469 | //newRay->mFlags |= VssRay::BorderSample;
|
---|
470 |
|
---|
471 | // cast reverse rays if necessary
|
---|
472 | castRays += CheckDiscontinuity(ray2, hitTriangle, oldRay);
|
---|
473 |
|
---|
474 | // subdivide further
|
---|
475 | castRays += SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay);
|
---|
476 | castRays += SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay);
|
---|
477 |
|
---|
478 | return castRays;
|
---|
479 | }
|
---|
480 |
|
---|
481 |
|
---|
482 | int GvsPreprocessor::AdaptiveBorderSampling(const VssRay ¤tRay)
|
---|
483 | {
|
---|
484 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
485 |
|
---|
486 | // question matt: can this be possible?
|
---|
487 | if (!tObj) return 0;
|
---|
488 |
|
---|
489 | Triangle3 hitTriangle;
|
---|
490 |
|
---|
491 | // other types not implemented yet
|
---|
492 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
493 | hitTriangle = static_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
494 | else
|
---|
495 | cout << "border sampling: " << Intersectable::GetTypeName(tObj) << " not yet implemented" << endl;
|
---|
496 |
|
---|
497 | //cout << "type: " << Intersectable::GetTypeName(tObj) << endl;
|
---|
498 | generationTimer.Entry();
|
---|
499 |
|
---|
500 | static VertexContainer enlargedTriangle;
|
---|
501 | enlargedTriangle.clear();
|
---|
502 |
|
---|
503 | /// create 3 new hit points for each vertex
|
---|
504 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
505 |
|
---|
506 | /// create rays from sample points and handle them
|
---|
507 | static SimpleRayContainer simpleRays;
|
---|
508 | simpleRays.clear();
|
---|
509 |
|
---|
510 | VertexContainer::const_iterator vit, vit_end = enlargedTriangle.end();
|
---|
511 |
|
---|
512 | for (vit = enlargedTriangle.begin(); vit != vit_end; ++ vit)
|
---|
513 | {
|
---|
514 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
515 |
|
---|
516 | SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
|
---|
517 | simpleRays.AddRay(sr);
|
---|
518 | }
|
---|
519 |
|
---|
520 | //////////
|
---|
521 | //-- fill up simple rays with random rays so we can cast 16 rays at a time
|
---|
522 |
|
---|
523 | const int numRandomRays = 16 - (int)simpleRays.size();
|
---|
524 | GenerateRays(numRandomRays, *mDistribution, simpleRays, sInvalidSamples);
|
---|
525 |
|
---|
526 | generationTimer.Exit();
|
---|
527 |
|
---|
528 |
|
---|
529 | /////////////////////
|
---|
530 | //-- cast rays to vertices and determine visibility
|
---|
531 |
|
---|
532 | static VssRayContainer vssRays;
|
---|
533 | vssRays.clear();
|
---|
534 |
|
---|
535 | // for the original implementation we don't cast double rays
|
---|
536 | const bool castDoubleRays = !mPerViewCell;
|
---|
537 | // cannot prune invalid rays because we have to compare adjacent rays
|
---|
538 | const bool pruneInvalidRays = false;
|
---|
539 |
|
---|
540 | //gvsTimer.Entry();
|
---|
541 | castTimer.Entry();
|
---|
542 |
|
---|
543 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
544 |
|
---|
545 | castTimer.Exit();
|
---|
546 | //gvsTimer.Exit();
|
---|
547 |
|
---|
548 | const int numBorderSamples = (int)vssRays.size() - numRandomRays;
|
---|
549 | int castRays = (int)simpleRays.size();
|
---|
550 |
|
---|
551 | // handle cast rays
|
---|
552 | EnqueueRays(vssRays);
|
---|
553 |
|
---|
554 | if (0)
|
---|
555 | {
|
---|
556 | // visualize enlarged triangles
|
---|
557 | VizStruct dummy;
|
---|
558 | dummy.enlargedTriangle = new Polygon3(enlargedTriangle);
|
---|
559 | dummy.originalTriangle = hitTriangle;
|
---|
560 |
|
---|
561 | vizContainer.push_back(dummy);
|
---|
562 |
|
---|
563 | // set flags
|
---|
564 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
565 |
|
---|
566 | for (rit = vssRays.begin(); rit != rit_end; ++ rit)
|
---|
567 | (*rit)->mFlags |= VssRay::BorderSample;
|
---|
568 | }
|
---|
569 |
|
---|
570 | // recursivly subdivide each edge
|
---|
571 | for (int i = 0; i < numBorderSamples; ++ i)
|
---|
572 | {
|
---|
573 | castRays += CheckDiscontinuity(*vssRays[i], hitTriangle, currentRay);
|
---|
574 |
|
---|
575 | castRays += SubdivideEdge(hitTriangle,
|
---|
576 | enlargedTriangle[i],
|
---|
577 | enlargedTriangle[(i + 1) % numBorderSamples],
|
---|
578 | *vssRays[i],
|
---|
579 | *vssRays[(i + 1) % numBorderSamples],
|
---|
580 | currentRay);
|
---|
581 | }
|
---|
582 |
|
---|
583 | mGvsStats.mBorderSamples += castRays;
|
---|
584 |
|
---|
585 | return castRays;
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 | int GvsPreprocessor::AdaptiveBorderSamplingOpt(const VssRay ¤tRay)
|
---|
590 | {
|
---|
591 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
592 |
|
---|
593 | // question matt: can this be possible?
|
---|
594 | if (!tObj) return 0;
|
---|
595 |
|
---|
596 | Triangle3 hitTriangle;
|
---|
597 |
|
---|
598 | // other types not implemented yet
|
---|
599 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
600 | hitTriangle = static_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
601 | else
|
---|
602 | cout << "error: gvs sampling for " << Intersectable::GetTypeName(tObj) << " not implemented" << endl;
|
---|
603 |
|
---|
604 | generationTimer.Entry();
|
---|
605 |
|
---|
606 | static VertexContainer enlargedTriangle;
|
---|
607 | enlargedTriangle.clear();
|
---|
608 |
|
---|
609 | /// create 3 new hit points for each vertex
|
---|
610 | EnlargeTriangle(enlargedTriangle, hitTriangle, currentRay);
|
---|
611 |
|
---|
612 | static VertexContainer subdivEnlargedTri;
|
---|
613 | subdivEnlargedTri.clear();
|
---|
614 |
|
---|
615 | for (size_t i = 0; i < enlargedTriangle.size(); ++ i)
|
---|
616 | {
|
---|
617 | const Vector3 p1 = enlargedTriangle[i];
|
---|
618 | const Vector3 p2 = enlargedTriangle[(i + 1) % enlargedTriangle.size()];
|
---|
619 |
|
---|
620 | // the new subdivision point
|
---|
621 | const Vector3 p = (p1 + p2) * 0.5f;
|
---|
622 |
|
---|
623 | subdivEnlargedTri.push_back(p1);
|
---|
624 | subdivEnlargedTri.push_back(p);
|
---|
625 | }
|
---|
626 |
|
---|
627 | /// create rays from sample points and handle them
|
---|
628 | static SimpleRayContainer simpleRays;
|
---|
629 | simpleRays.clear();
|
---|
630 |
|
---|
631 | VertexContainer::const_iterator vit, vit_end = subdivEnlargedTri.end();
|
---|
632 |
|
---|
633 | for (vit = subdivEnlargedTri.begin(); vit != vit_end; ++ vit)
|
---|
634 | {
|
---|
635 | const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
|
---|
636 |
|
---|
637 | SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
|
---|
638 | simpleRays.AddRay(sr);
|
---|
639 | }
|
---|
640 |
|
---|
641 |
|
---|
642 | //////////
|
---|
643 | //-- fill up simple rays with random rays so we can cast 16 rays at a time
|
---|
644 |
|
---|
645 | const int numRandomRays = 16 - ((int)simpleRays.size() % 16);
|
---|
646 |
|
---|
647 | GenerateImportanceSamples(currentRay, hitTriangle, numRandomRays, simpleRays);
|
---|
648 |
|
---|
649 | generationTimer.Exit();
|
---|
650 |
|
---|
651 |
|
---|
652 |
|
---|
653 | /////////////////////
|
---|
654 | //-- cast rays to vertices and determine visibility
|
---|
655 |
|
---|
656 | static VssRayContainer vssRays;
|
---|
657 | vssRays.clear();
|
---|
658 |
|
---|
659 | // for the original implementation we don't cast double rays
|
---|
660 | const bool castDoubleRays = !mPerViewCell;
|
---|
661 | // cannot prune invalid rays because we have to compare adjacent rays
|
---|
662 | const bool pruneInvalidRays = false;
|
---|
663 |
|
---|
664 | //if ((simpleRays.size() % 16) != 0) cerr << "ray size not aligned" << endl;
|
---|
665 |
|
---|
666 | //gvsTimer.Entry();
|
---|
667 | castTimer.Entry();
|
---|
668 |
|
---|
669 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
670 |
|
---|
671 | castTimer.Exit();
|
---|
672 | //gvsTimer.Exit();
|
---|
673 |
|
---|
674 | const int numBorderSamples = (int)vssRays.size() - numRandomRays;
|
---|
675 | int castRays = (int)simpleRays.size();
|
---|
676 |
|
---|
677 | // handle cast rays
|
---|
678 | EnqueueRays(vssRays);
|
---|
679 |
|
---|
680 |
|
---|
681 | #if 0
|
---|
682 | for (size_t i = 0; i < vssRays.size(); ++ i)
|
---|
683 | {
|
---|
684 | // check for discontinuity and cast reverse rays if necessary
|
---|
685 | castRays += CheckDiscontinuity2(*vssRays[i], hitTriangle, currentRay);
|
---|
686 | }
|
---|
687 |
|
---|
688 |
|
---|
689 | // recursivly subdivide each edge
|
---|
690 | for (int i = 0; i < numBorderSamples; ++ i)
|
---|
691 | {
|
---|
692 | castRays += SubdivideEdge(hitTriangle,
|
---|
693 | subdivEnlargedTri[i],
|
---|
694 | subdivEnlargedTri[(i + 1) % numBorderSamples],
|
---|
695 | *vssRays[i],
|
---|
696 | *vssRays[(i + 1) % numBorderSamples],
|
---|
697 | currentRay);
|
---|
698 | }
|
---|
699 |
|
---|
700 | #endif
|
---|
701 |
|
---|
702 |
|
---|
703 | mGvsStats.mBorderSamples += castRays;
|
---|
704 |
|
---|
705 | return castRays;
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 | bool GvsPreprocessor::GetPassingPoint(const VssRay ¤tRay,
|
---|
710 | const Triangle3 &occluder,
|
---|
711 | const VssRay &oldRay,
|
---|
712 | Vector3 &newPoint) const
|
---|
713 | {
|
---|
714 | /////////////////////////
|
---|
715 | //-- We interserct the plane p = (xp, hit(x), hit(xold))
|
---|
716 | //-- with the newly found occluder (xold is the previous ray from
|
---|
717 | //-- which x was generated). On the intersecting line, we select a point
|
---|
718 | //-- pnew which lies just outside of the new triangle so the ray
|
---|
719 | //-- just passes through the gap
|
---|
720 |
|
---|
721 | const Plane3 plane(currentRay.GetOrigin(),
|
---|
722 | currentRay.GetTermination(),
|
---|
723 | oldRay.GetTermination());
|
---|
724 |
|
---|
725 | Vector3 pt1, pt2;
|
---|
726 |
|
---|
727 | if (!occluder.GetPlaneIntersection(plane, pt1, pt2))
|
---|
728 | return false;
|
---|
729 |
|
---|
730 | // get the intersection point on the old ray
|
---|
731 | const Plane3 triPlane(occluder.GetNormal(), occluder.mVertices[0]);
|
---|
732 |
|
---|
733 | const float t = triPlane.FindT(oldRay.mOrigin, oldRay.mTermination);
|
---|
734 | const Vector3 pt3 = oldRay.mOrigin + t * (oldRay.mTermination - oldRay.mOrigin);
|
---|
735 |
|
---|
736 | // evaluate new hitpoint just outside the triangle
|
---|
737 | // the point is chosen to be on the side closer to the original ray
|
---|
738 | if (SqrDistance(pt1, pt3) < SqrDistance(pt2, pt3))
|
---|
739 | newPoint = pt1 + mEps * (pt1 - pt2);
|
---|
740 | else
|
---|
741 | newPoint = pt2 + mEps * (pt2 - pt1);
|
---|
742 |
|
---|
743 | //cout << "passing point: " << newPoint << endl << endl;
|
---|
744 | return true;
|
---|
745 | }
|
---|
746 |
|
---|
747 |
|
---|
748 | bool GvsPreprocessor::ComputeReverseRay(const VssRay ¤tRay,
|
---|
749 | const Triangle3 &hitTriangle,
|
---|
750 | const VssRay &oldRay,
|
---|
751 | SimpleRay &reverseRay)
|
---|
752 | {
|
---|
753 | // optain triangle that occludes the path to the currently processed mesh
|
---|
754 | Triangle3 occluder;
|
---|
755 | Intersectable *tObj = currentRay.mTerminationObject;
|
---|
756 |
|
---|
757 | // q: why can this happen?
|
---|
758 | if (!tObj)
|
---|
759 | return false;
|
---|
760 |
|
---|
761 | // other types not implemented yet
|
---|
762 | if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
763 | occluder = static_cast<TriangleIntersectable *>(tObj)->GetItem();
|
---|
764 | else
|
---|
765 | cout << "reverse sampling: " << tObj->Type() << " not yet implemented" << endl;
|
---|
766 |
|
---|
767 | // get a point which is passing just outside of the occluder
|
---|
768 | Vector3 newPoint;
|
---|
769 |
|
---|
770 | // q: why is there sometimes no intersecton found?
|
---|
771 | if (!GetPassingPoint(currentRay, occluder, oldRay, newPoint))
|
---|
772 | return false;
|
---|
773 |
|
---|
774 | const Vector3 predicted = CalcPredictedHitPoint(currentRay, hitTriangle, oldRay);
|
---|
775 |
|
---|
776 | //////////////
|
---|
777 | //-- Construct the mutated ray with xnew,
|
---|
778 | //-- dir = predicted(x)- pnew as direction vector
|
---|
779 |
|
---|
780 | Vector3 newDir = newDir = predicted - newPoint;
|
---|
781 |
|
---|
782 | // take xnew, p = intersect(viewcell, line(pnew, predicted(x)) as origin ?
|
---|
783 | // difficult to say!!
|
---|
784 | const float offset = 0.5f;
|
---|
785 | Vector3 newOrigin = newPoint - newDir * offset;
|
---|
786 |
|
---|
787 |
|
---|
788 | //////////////
|
---|
789 | //-- for per view cell sampling, we must intersect
|
---|
790 | //-- with the current view cell
|
---|
791 |
|
---|
792 | if (mPerViewCell)
|
---|
793 | {
|
---|
794 | if (!IntersectsViewCell(newOrigin, -newDir))
|
---|
795 | return false;
|
---|
796 | }
|
---|
797 |
|
---|
798 | reverseRay = SimpleRay(newOrigin, newDir, SamplingStrategy::GVS, 1.0f);
|
---|
799 |
|
---|
800 | return true;
|
---|
801 | }
|
---|
802 |
|
---|
803 |
|
---|
804 | bool GvsPreprocessor::IntersectsViewCell(Vector3 &origin, const Vector3 &dir) const
|
---|
805 | {
|
---|
806 | AxisAlignedBox3 box = mCurrentViewCell->GetBox();
|
---|
807 |
|
---|
808 | if (box.IsInside(origin))
|
---|
809 | {
|
---|
810 | //cout<<"z";
|
---|
811 | return true;
|
---|
812 | }
|
---|
813 |
|
---|
814 | // intersect ray with view cell and
|
---|
815 | SimpleRay ray(origin, dir, 0, 1.0f);
|
---|
816 |
|
---|
817 | float tnear, tfar;
|
---|
818 |
|
---|
819 | // check if ray intersects view cell
|
---|
820 | if (!mCurrentViewCell->GetBox().Intersects(ray, tnear, tfar))
|
---|
821 | return false;
|
---|
822 |
|
---|
823 | origin = origin + dir * tnear;
|
---|
824 | //cout<<"r";
|
---|
825 | return true;
|
---|
826 | }
|
---|
827 |
|
---|
828 |
|
---|
829 | int GvsPreprocessor::CastInitialSamples(int numSamples)
|
---|
830 | {
|
---|
831 | initialTimer.Entry();
|
---|
832 |
|
---|
833 | // generate simple rays
|
---|
834 | static SimpleRayContainer simpleRays;
|
---|
835 | simpleRays.clear();
|
---|
836 |
|
---|
837 | generationTimer.Entry();
|
---|
838 |
|
---|
839 | if (mUseProbablyVisibleSampling)// && (RandomValue(0, 1) > 0.5))
|
---|
840 | {
|
---|
841 | ProbablyVisibleDistribution distr(*this, mCurrentViewCell, &mProbablyVisibleTriangles);
|
---|
842 | GenerateRays(numSamples, distr, simpleRays, sInvalidSamples);
|
---|
843 | }
|
---|
844 | else
|
---|
845 | {
|
---|
846 | GenerateRays(mUseDeterministicGvs ? numSamples : numSamples / 16, *mDistribution, simpleRays, sInvalidSamples);
|
---|
847 | }
|
---|
848 |
|
---|
849 | generationTimer.Exit();
|
---|
850 |
|
---|
851 | // cast generated samples
|
---|
852 | static VssRayContainer vssRays;
|
---|
853 | vssRays.clear();
|
---|
854 |
|
---|
855 | castTimer.Entry();
|
---|
856 |
|
---|
857 | if (mUseDeterministicGvs)
|
---|
858 | {
|
---|
859 | const bool castDoubleRays = !mPerViewCell;
|
---|
860 | const bool pruneInvalidRays = false;
|
---|
861 | //const bool pruneInvalidRays = true;
|
---|
862 |
|
---|
863 | CastRays(simpleRays, vssRays, castDoubleRays, pruneInvalidRays);
|
---|
864 | }
|
---|
865 | else
|
---|
866 | {
|
---|
867 | if (0)
|
---|
868 | // casting bundles of 4 rays generated from the simple rays
|
---|
869 | CastRayBundles4(simpleRays, vssRays);
|
---|
870 | else
|
---|
871 | CastRayBundles16(simpleRays, vssRays, mInitialJitter);
|
---|
872 | }
|
---|
873 |
|
---|
874 | castTimer.Exit();
|
---|
875 |
|
---|
876 | // add to ray queue
|
---|
877 | EnqueueRays(vssRays);
|
---|
878 |
|
---|
879 | initialTimer.Exit();
|
---|
880 |
|
---|
881 | return (int)vssRays.size();
|
---|
882 | }
|
---|
883 |
|
---|
884 |
|
---|
885 | void GvsPreprocessor::EnqueueRays(VssRayContainer &samples)
|
---|
886 | {
|
---|
887 | rayTimer.Entry();
|
---|
888 |
|
---|
889 | // add samples to ray queue
|
---|
890 | VssRayContainer::const_iterator vit, vit_end = samples.end();
|
---|
891 | for (vit = samples.begin(); vit != vit_end; ++ vit)
|
---|
892 | {
|
---|
893 | VssRay *ray = *vit;
|
---|
894 | HandleRay(ray);
|
---|
895 | // note: deletion of invalid samples handked by ray pool
|
---|
896 | }
|
---|
897 |
|
---|
898 | rayTimer.Exit();
|
---|
899 | }
|
---|
900 |
|
---|
901 |
|
---|
902 | int GvsPreprocessor::ProcessQueue()
|
---|
903 | {
|
---|
904 | gvsTimer.Entry();
|
---|
905 |
|
---|
906 | ++ mGvsStats.mGvsRuns;
|
---|
907 |
|
---|
908 | int castSamples = 0;
|
---|
909 |
|
---|
910 | while (!mRayQueue.empty())
|
---|
911 | {
|
---|
912 | // handle next ray
|
---|
913 | VssRay *ray = mRayQueue.top();
|
---|
914 | mRayQueue.pop();
|
---|
915 |
|
---|
916 | if (mUseDeterministicGvs)
|
---|
917 | castSamples += AdaptiveBorderSampling(*ray);
|
---|
918 | else
|
---|
919 | castSamples += AdaptiveBorderSamplingOpt(*ray);
|
---|
920 |
|
---|
921 | // note: don't have to delete because handled by ray pool
|
---|
922 | }
|
---|
923 |
|
---|
924 | gvsTimer.Exit();
|
---|
925 |
|
---|
926 | return castSamples;
|
---|
927 | }
|
---|
928 |
|
---|
929 |
|
---|
930 | void ExportVssRays(Exporter *exporter, const VssRayContainer &vssRays)
|
---|
931 | {
|
---|
932 | VssRayContainer vcRays, vcRays2, vcRays3;
|
---|
933 |
|
---|
934 | VssRayContainer::const_iterator rit, rit_end = vssRays.end();
|
---|
935 |
|
---|
936 | // prepare some rays for output
|
---|
937 | for (rit = vssRays.begin(); rit != rit_end; ++ rit)
|
---|
938 | {
|
---|
939 | //const float p = RandomValue(0.0f, (float)vssRays.size());
|
---|
940 |
|
---|
941 | if (1)//(p < raysOut)
|
---|
942 | {
|
---|
943 | if ((*rit)->mFlags & VssRay::BorderSample)
|
---|
944 | vcRays.push_back(*rit);
|
---|
945 | else if ((*rit)->mFlags & VssRay::ReverseSample)
|
---|
946 | vcRays2.push_back(*rit);
|
---|
947 | else
|
---|
948 | vcRays3.push_back(*rit);
|
---|
949 | }
|
---|
950 | }
|
---|
951 |
|
---|
952 | exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
|
---|
953 | exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
|
---|
954 | exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
|
---|
955 | }
|
---|
956 |
|
---|
957 |
|
---|
958 | void GvsPreprocessor::VisualizeViewCell(const ObjectContainer &objects)
|
---|
959 | {
|
---|
960 | Intersectable::NewMail();
|
---|
961 | Material m;
|
---|
962 |
|
---|
963 | char str[64]; sprintf_s(str, "pass%06d.wrl", mProcessedViewCells);
|
---|
964 |
|
---|
965 | Exporter *exporter = Exporter::GetExporter(str);
|
---|
966 | if (!exporter)
|
---|
967 | return;
|
---|
968 |
|
---|
969 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
970 |
|
---|
971 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
972 | {
|
---|
973 | Intersectable *intersect = *oit;
|
---|
974 |
|
---|
975 | m = RandomMaterial();
|
---|
976 | exporter->SetForcedMaterial(m);
|
---|
977 | exporter->ExportIntersectable(intersect);
|
---|
978 | }
|
---|
979 |
|
---|
980 | cout << "vssrays: " << (int)mVssRays.size() << endl;
|
---|
981 | ExportVssRays(exporter, mVssRays);
|
---|
982 |
|
---|
983 |
|
---|
984 | /////////////////
|
---|
985 | //-- export view cell geometry
|
---|
986 |
|
---|
987 | //exporter->SetWireframe();
|
---|
988 |
|
---|
989 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
990 | exporter->SetForcedMaterial(m);
|
---|
991 |
|
---|
992 | AxisAlignedBox3 bbox = mCurrentViewCell->GetMesh()->mBox;
|
---|
993 | exporter->ExportBox(bbox);
|
---|
994 | //exporter->SetFilled();
|
---|
995 |
|
---|
996 | delete exporter;
|
---|
997 | }
|
---|
998 |
|
---|
999 |
|
---|
1000 | void GvsPreprocessor::VisualizeViewCells()
|
---|
1001 | {
|
---|
1002 | char str[64]; sprintf_s(str, "tmp/pass%06d_%04d-", mProcessedViewCells, mPass);
|
---|
1003 |
|
---|
1004 | // visualization
|
---|
1005 | if (mGvsStats.mPassContribution > 0)
|
---|
1006 | {
|
---|
1007 | const bool exportRays = true;
|
---|
1008 | const bool exportPvs = true;
|
---|
1009 |
|
---|
1010 | mViewCellsManager->ExportSingleViewCells(mObjects,
|
---|
1011 | 10,
|
---|
1012 | false,
|
---|
1013 | exportPvs,
|
---|
1014 | exportRays,
|
---|
1015 | 1000,
|
---|
1016 | str);
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | // remove pass samples
|
---|
1020 | ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
|
---|
1021 |
|
---|
1022 | for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
|
---|
1023 | {
|
---|
1024 | (*vit)->DelRayRefs();
|
---|
1025 | }
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 |
|
---|
1029 | void GvsPreprocessor::CompileViewCellsFromPointList()
|
---|
1030 | {
|
---|
1031 | ViewCell::NewMail();
|
---|
1032 |
|
---|
1033 | // Receive list of view cells from view cells manager
|
---|
1034 | ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
|
---|
1035 |
|
---|
1036 | vector<ViewCellPoints *>::const_iterator vit, vit_end = vcPoints->end();
|
---|
1037 |
|
---|
1038 | for (vit = vcPoints->begin(); vit != vit_end; ++ vit)
|
---|
1039 | {
|
---|
1040 | ViewCellPoints *vp = *vit;
|
---|
1041 |
|
---|
1042 | if (vp->first) // view cell specified
|
---|
1043 | {
|
---|
1044 | ViewCell *viewCell = vp->first;
|
---|
1045 |
|
---|
1046 | if (!viewCell->Mailed())
|
---|
1047 | {
|
---|
1048 | viewCell->Mail();
|
---|
1049 | mViewCells.push_back(viewCell);
|
---|
1050 |
|
---|
1051 | if (mViewCells.size() >= mMaxViewCells)
|
---|
1052 | break;
|
---|
1053 | }
|
---|
1054 | }
|
---|
1055 | else // no view cell specified => compute view cells using view point
|
---|
1056 | {
|
---|
1057 | SimpleRayContainer::const_iterator rit, rit_end = vp->second.end();
|
---|
1058 |
|
---|
1059 | for (rit = vp->second.begin(); rit != rit_end; ++ rit)
|
---|
1060 | {
|
---|
1061 | SimpleRay ray = *rit;
|
---|
1062 |
|
---|
1063 | ViewCell *viewCell = mViewCellsManager->GetViewCell(ray.mOrigin);
|
---|
1064 |
|
---|
1065 | if (viewCell && !viewCell->Mailed())
|
---|
1066 | {
|
---|
1067 | viewCell->Mail();
|
---|
1068 | mViewCells.push_back(viewCell);
|
---|
1069 |
|
---|
1070 | if (mViewCells.size() >= mMaxViewCells)
|
---|
1071 | break;
|
---|
1072 | }
|
---|
1073 | }
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 |
|
---|
1079 | void GvsPreprocessor::CompileViewCellsList()
|
---|
1080 | {
|
---|
1081 | if (!mViewCellsManager->GetViewCellPointsList()->empty())
|
---|
1082 | {
|
---|
1083 | cout << "processing view point list" << endl;
|
---|
1084 | CompileViewCellsFromPointList();
|
---|
1085 | }
|
---|
1086 | else
|
---|
1087 | {
|
---|
1088 | ViewCell::NewMail();
|
---|
1089 |
|
---|
1090 | while ((int)mViewCells.size() < mMaxViewCells)
|
---|
1091 | {
|
---|
1092 | const int tries = 10000;
|
---|
1093 | int i = 0;
|
---|
1094 |
|
---|
1095 | for (i = 0; i < tries; ++ i)
|
---|
1096 | {
|
---|
1097 | const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetNumViewCells() - 0.5f);
|
---|
1098 |
|
---|
1099 | ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
|
---|
1100 |
|
---|
1101 | if (!viewCell->Mailed())
|
---|
1102 | {
|
---|
1103 | viewCell->Mail();
|
---|
1104 | break;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | mViewCells.push_back(viewCell);
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | if (i == tries)
|
---|
1111 | {
|
---|
1112 | cerr << "big error! no view cell found" << endl;
|
---|
1113 | break;
|
---|
1114 | }
|
---|
1115 | }
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | cout << "\ncomputing list of " << mViewCells.size() << " view cells" << endl;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 |
|
---|
1122 | void GvsPreprocessor::ComputeViewCellGeometryIntersection()
|
---|
1123 | {
|
---|
1124 | intersectionTimer.Entry();
|
---|
1125 |
|
---|
1126 | AxisAlignedBox3 box = mCurrentViewCell->GetBox();
|
---|
1127 |
|
---|
1128 | int oldTrianglePvs = (int)mTrianglePvs.size();
|
---|
1129 | int newKdObj = 0;
|
---|
1130 |
|
---|
1131 | // compute pvs kd nodes that intersect view cell
|
---|
1132 | ObjectContainer kdobjects;
|
---|
1133 |
|
---|
1134 | // find intersecting objects not in pvs (i.e., only unmailed)
|
---|
1135 | mKdTree->CollectKdObjects(box, kdobjects);
|
---|
1136 |
|
---|
1137 | ObjectContainer pvsKdObjects;
|
---|
1138 |
|
---|
1139 | ObjectContainer::const_iterator oit, oit_end = kdobjects.end();
|
---|
1140 |
|
---|
1141 | for (oit = kdobjects.begin(); oit != oit_end; ++ oit)
|
---|
1142 | {
|
---|
1143 | KdIntersectable *kdInt = static_cast<KdIntersectable *>(*oit);
|
---|
1144 |
|
---|
1145 | myobjects.clear();
|
---|
1146 | mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), myobjects);
|
---|
1147 |
|
---|
1148 | // account for kd object pvs
|
---|
1149 | bool addkdobj = false;
|
---|
1150 |
|
---|
1151 | ObjectContainer::const_iterator oit, oit_end = myobjects.end();
|
---|
1152 |
|
---|
1153 | for (oit = myobjects.begin(); oit != oit_end; ++ oit)
|
---|
1154 | {
|
---|
1155 | TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
|
---|
1156 |
|
---|
1157 | // test if the triangle itself intersects
|
---|
1158 | if (box.Intersects(triObj->GetItem()))
|
---|
1159 | addkdobj = AddTriangleObject(triObj);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | // add node to kd pvs
|
---|
1163 | if (1 && addkdobj)
|
---|
1164 | {
|
---|
1165 | ++ newKdObj;
|
---|
1166 | mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
|
---|
1167 | //mCurrentViewCell->GetPvs().AddSampleDirtyCheck(kdInt, 1.0f);
|
---|
1168 | if (QT_VISUALIZATION_SHOWN) UpdateStatsForVisualization(kdInt);
|
---|
1169 | }
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | cout << "added " << (int)mTrianglePvs.size() - oldTrianglePvs << " triangles (" << newKdObj << " objects) by intersection" << endl;
|
---|
1173 |
|
---|
1174 | intersectionTimer.Exit();
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 |
|
---|
1178 | void GvsPreprocessor::PerViewCellComputation()
|
---|
1179 | {
|
---|
1180 | while (mCurrentViewCell = NextViewCell())
|
---|
1181 | {
|
---|
1182 | preparationTimer.Entry();
|
---|
1183 |
|
---|
1184 | // hack: reset counters (could be done with a mailing-like approach
|
---|
1185 | ObjectContainer::const_iterator oit, oit_end = mObjects.end();
|
---|
1186 | for (oit = mObjects.begin(); oit != oit_end; ++ oit)
|
---|
1187 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
1188 |
|
---|
1189 | preparationTimer.Exit();
|
---|
1190 |
|
---|
1191 | mDistribution->SetViewCell(mCurrentViewCell);
|
---|
1192 |
|
---|
1193 | ComputeViewCell();
|
---|
1194 | }
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 |
|
---|
1198 | void GvsPreprocessor::PerViewCellComputation2()
|
---|
1199 | {
|
---|
1200 | while (1)
|
---|
1201 | {
|
---|
1202 | if (!mRendererWidget)
|
---|
1203 | continue;
|
---|
1204 |
|
---|
1205 | mCurrentViewCell = mViewCellsManager->GetViewCell(mRendererWidget->GetViewPoint());
|
---|
1206 |
|
---|
1207 | // no valid view cell or view cell already computed
|
---|
1208 | if (!mCurrentViewCell || !mCurrentViewCell->GetPvs().Empty() || !mRendererWidget->mComputeGVS)
|
---|
1209 | continue;
|
---|
1210 |
|
---|
1211 | mRendererWidget->mComputeGVS = false;
|
---|
1212 | // hack: reset counters
|
---|
1213 | ObjectContainer::const_iterator oit, oit_end = mObjects.end();
|
---|
1214 |
|
---|
1215 | for (oit = mObjects.begin(); oit != oit_end; ++ oit)
|
---|
1216 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
1217 |
|
---|
1218 | ComputeViewCell();
|
---|
1219 | ++ mProcessedViewCells;
|
---|
1220 | }
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 |
|
---|
1224 | void GvsPreprocessor::StorePvs(const ObjectContainer &objectPvs)
|
---|
1225 | {
|
---|
1226 | ObjectContainer::const_iterator oit, oit_end = objectPvs.end();
|
---|
1227 |
|
---|
1228 | for (oit = objectPvs.begin(); oit != oit_end; ++ oit)
|
---|
1229 | {
|
---|
1230 | mCurrentViewCell->GetPvs().AddSample(*oit, 1);
|
---|
1231 | }
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 |
|
---|
1235 | void GvsPreprocessor::UpdatePvs(ViewCell *currentViewCell)
|
---|
1236 | {
|
---|
1237 | ObjectPvs newPvs;
|
---|
1238 | BvhLeaf::NewMail();
|
---|
1239 |
|
---|
1240 | ObjectPvsIterator pit = currentViewCell->GetPvs().GetIterator();
|
---|
1241 |
|
---|
1242 | // output PVS of view cell
|
---|
1243 | while (pit.HasMoreEntries())
|
---|
1244 | {
|
---|
1245 | Intersectable *intersect = pit.Next();
|
---|
1246 |
|
---|
1247 | BvhLeaf *bv = intersect->mBvhLeaf;
|
---|
1248 |
|
---|
1249 | if (!bv || bv->Mailed())
|
---|
1250 | continue;
|
---|
1251 |
|
---|
1252 | bv->Mail();
|
---|
1253 |
|
---|
1254 | //m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
1255 | newPvs.AddSampleDirty(bv, 1.0f);
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | newPvs.SimpleSort();
|
---|
1259 |
|
---|
1260 | currentViewCell->SetPvs(newPvs);
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 |
|
---|
1264 | void GvsPreprocessor::GetObjectPvs(ObjectContainer &objectPvs) const
|
---|
1265 | {
|
---|
1266 | BvhLeaf::NewMail();
|
---|
1267 |
|
---|
1268 | objectPvs.reserve((int)mTrianglePvs.size());
|
---|
1269 |
|
---|
1270 | ObjectContainer::const_iterator oit, oit_end = mTrianglePvs.end();
|
---|
1271 |
|
---|
1272 | for (oit = mTrianglePvs.begin(); oit != oit_end; ++ oit)
|
---|
1273 | {
|
---|
1274 | Intersectable *intersect = *oit;
|
---|
1275 |
|
---|
1276 | BvhLeaf *bv = intersect->mBvhLeaf;
|
---|
1277 |
|
---|
1278 | // hack: reset counter
|
---|
1279 | (*oit)->mCounter = NOT_ACCOUNTED_OBJECT;
|
---|
1280 |
|
---|
1281 | if (!bv || bv->Mailed())
|
---|
1282 | continue;
|
---|
1283 |
|
---|
1284 | bv->Mail();
|
---|
1285 | objectPvs.push_back(bv);
|
---|
1286 | }
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 |
|
---|
1290 | void GvsPreprocessor::GlobalComputation()
|
---|
1291 | {
|
---|
1292 | int passSamples = 0;
|
---|
1293 | int randomSamples = 0;
|
---|
1294 | int gvsSamples = 0;
|
---|
1295 | int oldContribution = 0;
|
---|
1296 |
|
---|
1297 | while (mGvsStats.mTotalSamples < mTotalSamples)
|
---|
1298 | {
|
---|
1299 | mRayCaster->InitPass();
|
---|
1300 |
|
---|
1301 | int newRandomSamples, newGvsSamples;
|
---|
1302 |
|
---|
1303 | // Ray queue empty =>
|
---|
1304 | // cast a number of uniform samples to fill ray queue
|
---|
1305 | newRandomSamples = CastInitialSamples(mInitialSamples);
|
---|
1306 |
|
---|
1307 | if (!mOnlyRandomSampling)
|
---|
1308 | newGvsSamples = ProcessQueue();
|
---|
1309 |
|
---|
1310 | passSamples += newRandomSamples + newGvsSamples;
|
---|
1311 | mGvsStats.mTotalSamples += newRandomSamples + newGvsSamples;
|
---|
1312 |
|
---|
1313 | mGvsStats.mRandomSamples += newRandomSamples;
|
---|
1314 | mGvsStats.mGvsSamples += newGvsSamples;
|
---|
1315 |
|
---|
1316 |
|
---|
1317 | if (passSamples % (mGvsSamplesPerPass + 1) == mGvsSamplesPerPass)
|
---|
1318 | {
|
---|
1319 | ++ mPass;
|
---|
1320 |
|
---|
1321 | mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
|
---|
1322 |
|
---|
1323 | ////////
|
---|
1324 | //-- stats
|
---|
1325 |
|
---|
1326 | //cout << "\nPass " << mPass << " #samples: " << mGvsStats.mTotalSamples << " of " << mTotalSamples << endl;
|
---|
1327 | mGvsStats.mPass = mPass;
|
---|
1328 | mGvsStats.Stop();
|
---|
1329 | mGvsStats.Print(mGvsStatsStream);
|
---|
1330 |
|
---|
1331 | // reset
|
---|
1332 | oldContribution = mGvsStats.mTotalContribution;
|
---|
1333 | mGvsStats.mPassContribution = 0;
|
---|
1334 | passSamples = 0;
|
---|
1335 |
|
---|
1336 | if (GVS_DEBUG)
|
---|
1337 | VisualizeViewCells();
|
---|
1338 | }
|
---|
1339 | }
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 |
|
---|
1343 | bool GvsPreprocessor::ComputeVisibility()
|
---|
1344 | {
|
---|
1345 | cout << "Gvs Preprocessor started\n" << flush;
|
---|
1346 | const long startTime = GetTime();
|
---|
1347 |
|
---|
1348 | //Randomize(0);
|
---|
1349 | mGvsStats.Reset();
|
---|
1350 | mGvsStats.Start();
|
---|
1351 |
|
---|
1352 | if (!mLoadViewCells)
|
---|
1353 | {
|
---|
1354 | /// construct the view cells from the scratch
|
---|
1355 | ConstructViewCells();
|
---|
1356 | // reset pvs already gathered during view cells construction
|
---|
1357 | mViewCellsManager->ResetPvs();
|
---|
1358 | cout << "finished view cell construction" << endl;
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | if (mPerViewCell)
|
---|
1362 | {
|
---|
1363 | #if 1
|
---|
1364 | // provide list of view cells to compute
|
---|
1365 | CompileViewCellsList();
|
---|
1366 |
|
---|
1367 | // start per view cell gvs
|
---|
1368 | PerViewCellComputation();
|
---|
1369 | #else
|
---|
1370 | PerViewCellComputation2();
|
---|
1371 |
|
---|
1372 | #endif
|
---|
1373 | }
|
---|
1374 | else
|
---|
1375 | {
|
---|
1376 | GlobalComputation();
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | cout << "cast " << mGvsStats.mTotalSamples / (1e3f * TimeDiff(startTime, GetTime())) << "M single rays/s" << endl;
|
---|
1380 |
|
---|
1381 | if (GVS_DEBUG)
|
---|
1382 | {
|
---|
1383 | Visualize();
|
---|
1384 | CLEAR_CONTAINER(mVssRays);
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | // export the preprocessed information to a file
|
---|
1388 | if (0 && mExportVisibility)
|
---|
1389 | ExportPreprocessedData(mVisibilityFileName);
|
---|
1390 |
|
---|
1391 | // compute the pixel error of this visibility solution
|
---|
1392 | if (0 && mEvaluatePixelError)
|
---|
1393 | ComputeRenderError();
|
---|
1394 |
|
---|
1395 | return true;
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 |
|
---|
1399 | void GvsPreprocessor::DeterminePvsObjects(VssRayContainer &rays)
|
---|
1400 | {
|
---|
1401 | // store triangle directly
|
---|
1402 | mViewCellsManager->DeterminePvsObjects(rays, true);
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 |
|
---|
1406 | void GvsPreprocessor::Visualize()
|
---|
1407 | {
|
---|
1408 | Exporter *exporter = Exporter::GetExporter("gvs.wrl");
|
---|
1409 |
|
---|
1410 | if (!exporter)
|
---|
1411 | return;
|
---|
1412 |
|
---|
1413 | vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end();
|
---|
1414 |
|
---|
1415 | for (vit = vizContainer.begin(); vit != vit_end; ++ vit)
|
---|
1416 | {
|
---|
1417 | exporter->SetWireframe();
|
---|
1418 | exporter->ExportPolygon((*vit).enlargedTriangle);
|
---|
1419 | //Material m;
|
---|
1420 | exporter->SetFilled();
|
---|
1421 | Polygon3 poly = Polygon3((*vit).originalTriangle);
|
---|
1422 | exporter->ExportPolygon(&poly);
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | VssRayContainer::const_iterator rit, rit_end = mVssRays.end();
|
---|
1426 |
|
---|
1427 | for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
1428 | {
|
---|
1429 | Intersectable *obj = (*rit)->mTerminationObject;
|
---|
1430 | exporter->ExportIntersectable(obj);
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | ExportVssRays(exporter, mVssRays);
|
---|
1434 |
|
---|
1435 | delete exporter;
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 |
|
---|
1439 | void GvsStatistics::Print(ostream &app) const
|
---|
1440 | {
|
---|
1441 | app << "#ViewCells\n" << mViewCells << endl;
|
---|
1442 | app << "#Id\n" << mViewCellId << endl;
|
---|
1443 | app << "#Time\n" << mTimePerViewCell << endl;;
|
---|
1444 | app << "#TriPvs\n" << mTrianglePvs << endl;
|
---|
1445 | app << "#ObjectPvs\n" << mPerViewCellPvs << endl;
|
---|
1446 | app << "#UpdatedPvs\n" << (int)mPvsCost << endl;
|
---|
1447 |
|
---|
1448 | app << "#PerViewCellSamples\n" << mPerViewCellSamples << endl;
|
---|
1449 | app << "#RaysPerSec\n" << RaysPerSec() << endl;
|
---|
1450 |
|
---|
1451 | app << "#TotalPvs\n" << mTotalPvs << endl;
|
---|
1452 | app << "#TotalTime\n" << mTotalTime << endl;
|
---|
1453 | app << "#TotalSamples\n" << mTotalSamples << endl;
|
---|
1454 |
|
---|
1455 | app << "#AvgPvs: " << mPvsCost / mViewCells << endl;
|
---|
1456 | app << "#TotalTrianglePvs\n" << mTotalTrianglePvs << endl;
|
---|
1457 |
|
---|
1458 | if (0)
|
---|
1459 | {
|
---|
1460 | app << "#ReverseSamples\n" << mReverseSamples << endl;
|
---|
1461 | app << "#BorderSamples\n" << mBorderSamples << endl;
|
---|
1462 |
|
---|
1463 | app << "#Pass\n" << mPass << endl;
|
---|
1464 | app << "#ScDiff\n" << mPassContribution << endl;
|
---|
1465 | app << "#GvsRuns\n" << mGvsRuns << endl;
|
---|
1466 |
|
---|
1467 | app << "#SamplesContri\n" << mTotalContribution << endl;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | app << endl;
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 |
|
---|
1474 | void GvsPreprocessor::ComputeViewCell()
|
---|
1475 | {
|
---|
1476 | const long startTime = GetTime();
|
---|
1477 |
|
---|
1478 | // initialise
|
---|
1479 | mGvsStats.mPerViewCellSamples = 0;
|
---|
1480 | mGvsStats.mRandomSamples = 0;
|
---|
1481 | mGvsStats.mGvsSamples = 0;
|
---|
1482 | mUseProbablyVisibleSampling = false;
|
---|
1483 | //renderer->mPvsStat.currentPass = 0;
|
---|
1484 |
|
---|
1485 | mPass = 0;
|
---|
1486 |
|
---|
1487 | int oldContribution = 0;
|
---|
1488 | int passSamples = 0;
|
---|
1489 |
|
---|
1490 | for (int i = 0; i < 2; ++ i)
|
---|
1491 | mGenericStats[i] = 0;
|
---|
1492 |
|
---|
1493 | mTrianglePvs.clear();
|
---|
1494 |
|
---|
1495 |
|
---|
1496 | // hack: take bounding box of view cell as view cell bounds
|
---|
1497 | mCurrentViewCell->GetMesh()->ComputeBoundingBox();
|
---|
1498 |
|
---|
1499 | // use mailing for kd node pvs
|
---|
1500 | KdNode::NewMail();
|
---|
1501 |
|
---|
1502 | cout << "\n***********************\n"
|
---|
1503 | << "computing view cell " << mProcessedViewCells
|
---|
1504 | << " (id: " << mCurrentViewCell->GetId() << ")" << endl;
|
---|
1505 | cout << "bb: " << mCurrentViewCell->GetBox() << endl;
|
---|
1506 |
|
---|
1507 | mainLoopTimer.Entry();
|
---|
1508 |
|
---|
1509 | while (1)
|
---|
1510 | {
|
---|
1511 | sInvalidSamples = 0;
|
---|
1512 | int oldPvsSize = mCurrentViewCell->GetPvs().GetSize();
|
---|
1513 |
|
---|
1514 | mRayCaster->InitPass();
|
---|
1515 |
|
---|
1516 | int newRandomSamples, newGvsSamples, newSamples;
|
---|
1517 |
|
---|
1518 | // Ray queue empty =>
|
---|
1519 | // cast a number of uniform samples to fill ray queue
|
---|
1520 | newRandomSamples = CastInitialSamples(mInitialSamples);
|
---|
1521 |
|
---|
1522 | //if (!mUseProbablyVisibleSampling && !mOnlyRandomSampling)
|
---|
1523 | if (!mOnlyRandomSampling)
|
---|
1524 | newGvsSamples = ProcessQueue();
|
---|
1525 |
|
---|
1526 | newSamples = newRandomSamples + newGvsSamples;
|
---|
1527 |
|
---|
1528 | passSamples += newSamples;
|
---|
1529 | mGvsStats.mPerViewCellSamples += newSamples;
|
---|
1530 |
|
---|
1531 | mGvsStats.mRandomSamples += newRandomSamples;
|
---|
1532 | mGvsStats.mGvsSamples += newGvsSamples;
|
---|
1533 |
|
---|
1534 |
|
---|
1535 | if (passSamples >= mGvsSamplesPerPass)
|
---|
1536 | {
|
---|
1537 | if (GVS_DEBUG) VisualizeViewCell(mTrianglePvs);
|
---|
1538 |
|
---|
1539 | //const bool convertPerPass = true;
|
---|
1540 | const bool convertPerPass = false;
|
---|
1541 |
|
---|
1542 | if (convertPerPass)
|
---|
1543 | {
|
---|
1544 | int newObjects = ConvertObjectPvs();
|
---|
1545 |
|
---|
1546 | cout << "added " << newObjects << " triangles to triangle pvs" << endl;
|
---|
1547 | mGvsStats.mTotalContribution += newObjects;
|
---|
1548 | }
|
---|
1549 |
|
---|
1550 | ++ mPass;
|
---|
1551 | mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
|
---|
1552 |
|
---|
1553 | if (!mUseDeterministicGvs && (mGvsStats.mPassContribution < 2000))
|
---|
1554 | mUseProbablyVisibleSampling = true;
|
---|
1555 |
|
---|
1556 | if (mUseProbablyVisibleSampling)
|
---|
1557 | PrepareProbablyVisibleSampling();
|
---|
1558 |
|
---|
1559 |
|
---|
1560 | ////////
|
---|
1561 | //-- stats
|
---|
1562 |
|
---|
1563 | mGvsStats.mPass = mPass;
|
---|
1564 |
|
---|
1565 | cout << "\nPass " << mPass << " #samples: " << mGvsStats.mPerViewCellSamples << endl;
|
---|
1566 | cout << "contribution=" << mGvsStats.mPassContribution << " (of " << mMinContribution << ")" << endl;
|
---|
1567 | cout << "obj contri=" << mCurrentViewCell->GetPvs().GetSize() - oldPvsSize << " (of " << mMinContribution << ")" << endl;
|
---|
1568 |
|
---|
1569 | // termination criterium
|
---|
1570 | if (mGvsStats.mPassContribution < mMinContribution)
|
---|
1571 | break;
|
---|
1572 |
|
---|
1573 | // reset stats
|
---|
1574 | oldContribution = mGvsStats.mTotalContribution;
|
---|
1575 | mGvsStats.mPassContribution = 0;
|
---|
1576 | passSamples = 0;
|
---|
1577 |
|
---|
1578 | // compute the pixel error of this visibility solution
|
---|
1579 | if (0 && mEvaluatePixelError)
|
---|
1580 | ComputeRenderError();
|
---|
1581 | }
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | mainLoopTimer.Exit();
|
---|
1585 |
|
---|
1586 | // at last compute objects that directly intersect view cell
|
---|
1587 | ComputeViewCellGeometryIntersection();
|
---|
1588 |
|
---|
1589 | // compute the pixel error of this visibility solution
|
---|
1590 | if (mEvaluatePixelError)
|
---|
1591 | ComputeRenderError();
|
---|
1592 |
|
---|
1593 | ////////
|
---|
1594 | //-- stats
|
---|
1595 |
|
---|
1596 | // timing
|
---|
1597 | mGvsStats.mTimePerViewCell = TimeDiff(startTime, GetTime()) * 1e-3f;
|
---|
1598 |
|
---|
1599 | ComputeStats();
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 |
|
---|
1603 | void GvsPreprocessor::ComputeStats()
|
---|
1604 | {
|
---|
1605 | // compute pvs size using larger (bvh objects)
|
---|
1606 | // note: for kd pvs we had to do this during gvs computation
|
---|
1607 | if (!mUseKdPvs)
|
---|
1608 | {
|
---|
1609 | ObjectContainer objectPvs;
|
---|
1610 |
|
---|
1611 | // optain object pvs
|
---|
1612 | GetObjectPvs(objectPvs);
|
---|
1613 |
|
---|
1614 | // add pvs
|
---|
1615 | ObjectContainer::const_iterator it, it_end = objectPvs.end();
|
---|
1616 |
|
---|
1617 | for (it = objectPvs.begin(); it != it_end; ++ it)
|
---|
1618 | {
|
---|
1619 | mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
|
---|
1620 | }
|
---|
1621 |
|
---|
1622 | cout << "triangle pvs of " << (int)mTrianglePvs.size()
|
---|
1623 | << " was converted to object pvs of " << (int)objectPvs.size() << endl;
|
---|
1624 |
|
---|
1625 | mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
|
---|
1626 | mGvsStats.mPvsCost = 0; // todo objectPvs.EvalPvsCost();
|
---|
1627 | }
|
---|
1628 | else
|
---|
1629 | {
|
---|
1630 | if (0) // compute pvs kd nodes that intersect view cell
|
---|
1631 | {
|
---|
1632 | ObjectContainer mykdobjects;
|
---|
1633 |
|
---|
1634 | // extract kd pvs
|
---|
1635 | ObjectContainer::const_iterator it, it_end = mTrianglePvs.end();
|
---|
1636 |
|
---|
1637 | for (it = mTrianglePvs.begin(); it != it_end; ++ it)
|
---|
1638 | {
|
---|
1639 | Intersectable *obj = *it;
|
---|
1640 | // find intersecting objects not yet in pvs (i.e., only unmailed)
|
---|
1641 | mKdTree->CollectKdObjects(obj->GetBox(), mykdobjects);
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 | // add pvs
|
---|
1645 | it_end = mykdobjects.end();
|
---|
1646 |
|
---|
1647 | for (it = mykdobjects.begin(); it != it_end; ++ it)
|
---|
1648 | {
|
---|
1649 | mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | cout << "added " << (int)mykdobjects.size() << " new objects " << endl;
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();
|
---|
1656 | mGvsStats.mPvsCost = mCurrentViewCell->GetPvs().EvalPvsCost();
|
---|
1657 |
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
|
---|
1661 |
|
---|
1662 |
|
---|
1663 | ////////////////
|
---|
1664 | // global values
|
---|
1665 |
|
---|
1666 | mGvsStats.mViewCells = mProcessedViewCells;
|
---|
1667 | mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
|
---|
1668 | mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
|
---|
1669 | mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
|
---|
1670 | mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples;
|
---|
1671 |
|
---|
1672 | cout << "id: " << mCurrentViewCell->GetId() << endl;
|
---|
1673 | cout << "pvs cost " << mGvsStats.mPvsCost / 1000000 << " M" << endl;
|
---|
1674 | cout << "pvs tri: " << mTrianglePvs.size() << endl;
|
---|
1675 | cout << "samples: " << mGvsStats.mTotalSamples / 1000000 << " M" << endl;
|
---|
1676 | printf("contri: %f tri / K rays\n", 1000.0f * (float)mTrianglePvs.size() / mGvsStats.mTotalSamples);
|
---|
1677 |
|
---|
1678 | //cout << "invalid samples ratio: " << (float)sInvalidSamples / mGvsStats.mPerViewCellSamples << endl;
|
---|
1679 |
|
---|
1680 | double rayTime = rayTimer.TotalTime();
|
---|
1681 | double kdPvsTime = kdPvsTimer.TotalTime();
|
---|
1682 | double gvsTime = gvsTimer.TotalTime();
|
---|
1683 | double initialTime = initialTimer.TotalTime();
|
---|
1684 | double intersectionTime = intersectionTimer.TotalTime();
|
---|
1685 | double preparationTime = preparationTimer.TotalTime();
|
---|
1686 | double mainLoopTime = mainLoopTimer.TotalTime();
|
---|
1687 | double contributionTime = contributionTimer.TotalTime();
|
---|
1688 | double castTime = castTimer.TotalTime();
|
---|
1689 | double generationTime = generationTimer.TotalTime();
|
---|
1690 | double rawCastTime = mRayCaster->rawCastTimer.TotalTime();
|
---|
1691 |
|
---|
1692 | cout << "handleRay : " << rayTime << endl;
|
---|
1693 | cout << "kd pvs : " << kdPvsTime << endl;
|
---|
1694 | cout << "gvs sampling : " << gvsTime << endl;
|
---|
1695 | cout << "initial sampling : " << initialTime << endl;
|
---|
1696 | cout << "view cell intersection : " << intersectionTime << endl;
|
---|
1697 | cout << "preparation : " << preparationTime << endl;
|
---|
1698 | cout << "main loop : " << mainLoopTime << endl;
|
---|
1699 | cout << "has contribution : " << contributionTime << endl;
|
---|
1700 | cout << "cast time : " << castTime << endl;
|
---|
1701 | cout << "generation time : " << generationTime << endl;
|
---|
1702 | cout << "raw cast time : " << rawCastTime << endl;
|
---|
1703 |
|
---|
1704 | double randomRaysPerSec = mGvsStats.mRandomSamples / (1e6 * initialTime);
|
---|
1705 | double gvsRaysPerSec = mGvsStats.mGvsSamples / (1e6 * gvsTime);
|
---|
1706 | double rawRaysPerSec = mGvsStats.mPerViewCellSamples / (1e6 * rawCastTime);
|
---|
1707 |
|
---|
1708 | cout << "cast " << randomRaysPerSec << " M random rays/s: " << endl;
|
---|
1709 | cout << "cast " << gvsRaysPerSec << " M gvs rays/s: " << endl;
|
---|
1710 | cout << "cast " << rawRaysPerSec << " M raw rays/s: " << endl;
|
---|
1711 |
|
---|
1712 | mGvsStats.Stop();
|
---|
1713 | mGvsStats.Print(mGvsStatsStream);
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 |
|
---|
1717 | int GvsPreprocessor::ConvertObjectPvs()
|
---|
1718 | {
|
---|
1719 | static ObjectContainer triangles;
|
---|
1720 |
|
---|
1721 | int newObjects = 0;
|
---|
1722 |
|
---|
1723 | ObjectPvsIterator pit = mCurrentViewCell->GetPvs().GetIterator();
|
---|
1724 |
|
---|
1725 | triangles.clear();
|
---|
1726 |
|
---|
1727 | // output PVS of view cell
|
---|
1728 | while (pit.HasMoreEntries())
|
---|
1729 | {
|
---|
1730 | KdIntersectable *kdInt = static_cast<KdIntersectable *>(pit.Next());
|
---|
1731 |
|
---|
1732 | mKdTree->CollectObjectsWithDublicates(kdInt->GetItem(), triangles);
|
---|
1733 |
|
---|
1734 | ObjectContainer::const_iterator oit, oit_end = triangles.end();
|
---|
1735 |
|
---|
1736 | for (oit = triangles.begin(); oit != oit_end; ++ oit)
|
---|
1737 | {
|
---|
1738 | if (AddTriangleObject(*oit))
|
---|
1739 | ++ newObjects;
|
---|
1740 | }
|
---|
1741 | }
|
---|
1742 |
|
---|
1743 | return newObjects;
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 |
|
---|
1747 | bool GvsPreprocessor::AddTriangleObject(Intersectable *triObj)
|
---|
1748 | {
|
---|
1749 | if ((triObj->mCounter < ACCOUNTED_OBJECT))
|
---|
1750 | {
|
---|
1751 | triObj->mCounter += ACCOUNTED_OBJECT;
|
---|
1752 |
|
---|
1753 | mTrianglePvs.push_back(triObj);
|
---|
1754 | mGenericStats[0] = (int)mTrianglePvs.size();
|
---|
1755 |
|
---|
1756 | return true;
|
---|
1757 | }
|
---|
1758 |
|
---|
1759 | return false;
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 |
|
---|
1763 | void GvsPreprocessor::CastRayBundles4(const SimpleRayContainer &rays, VssRayContainer &vssRays)
|
---|
1764 | {
|
---|
1765 | AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
|
---|
1766 |
|
---|
1767 | SimpleRayContainer::const_iterator it, it_end = rays.end();
|
---|
1768 |
|
---|
1769 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
1770 | CastRayBundle4(*it, vssRays, box);
|
---|
1771 | }
|
---|
1772 |
|
---|
1773 |
|
---|
1774 | void GvsPreprocessor::CastRayBundle4(const SimpleRay &ray,
|
---|
1775 | VssRayContainer &vssRays,
|
---|
1776 | const AxisAlignedBox3 &box)
|
---|
1777 | {
|
---|
1778 | const float pertubDir = 0.01f;
|
---|
1779 | static Vector3 pertub;
|
---|
1780 |
|
---|
1781 | static int hit_triangles[4];
|
---|
1782 | static float dist[4];
|
---|
1783 | static Vector3 dirs[4];
|
---|
1784 | static Vector3 origs[4];
|
---|
1785 |
|
---|
1786 | origs[0] = ray.mOrigin;
|
---|
1787 | dirs[0] = ray.mDirection;
|
---|
1788 |
|
---|
1789 | for (int i = 1; i < 4; ++ i)
|
---|
1790 | {
|
---|
1791 | origs[i] = ray.mOrigin;
|
---|
1792 |
|
---|
1793 | pertub.x = RandomValue(-pertubDir, pertubDir);
|
---|
1794 | pertub.y = RandomValue(-pertubDir, pertubDir);
|
---|
1795 | pertub.z = RandomValue(-pertubDir, pertubDir);
|
---|
1796 |
|
---|
1797 | dirs[i] = ray.mDirection + pertub;
|
---|
1798 | dirs[i] *= 1.0f / Magnitude(dirs[i]);
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | Cast4Rays(dist, dirs, origs, vssRays, box);
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 |
|
---|
1805 | void GvsPreprocessor::CastRays4(const SimpleRayContainer &rays, VssRayContainer &vssRays)
|
---|
1806 | {
|
---|
1807 | SimpleRayContainer::const_iterator it, it_end = rays.end();
|
---|
1808 |
|
---|
1809 | static float dist[4];
|
---|
1810 | static Vector3 dirs[4];
|
---|
1811 | static Vector3 origs[4];
|
---|
1812 |
|
---|
1813 | AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
|
---|
1814 |
|
---|
1815 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
1816 | {
|
---|
1817 | for (int i = 1; i < 4; ++ i)
|
---|
1818 | {
|
---|
1819 | origs[i] = rays[i].mOrigin;
|
---|
1820 | dirs[i] = rays[i].mDirection;
|
---|
1821 | }
|
---|
1822 | }
|
---|
1823 |
|
---|
1824 | Cast4Rays(dist, dirs, origs, vssRays, box);
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 |
|
---|
1828 | void GvsPreprocessor::Cast4Rays(float *dist,
|
---|
1829 | Vector3 *dirs,
|
---|
1830 | Vector3 *origs,
|
---|
1831 | VssRayContainer &vssRays,
|
---|
1832 | const AxisAlignedBox3 &box)
|
---|
1833 | {
|
---|
1834 | static int hit_triangles[4];
|
---|
1835 |
|
---|
1836 | mRayCaster->CastRaysPacket4(box.Min(), box.Max(), origs, dirs, hit_triangles, dist);
|
---|
1837 |
|
---|
1838 | VssRay *ray;
|
---|
1839 |
|
---|
1840 | for (int i = 0; i < 4; ++ i)
|
---|
1841 | {
|
---|
1842 | if (hit_triangles[i] != -1)
|
---|
1843 | {
|
---|
1844 | TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(mObjects[hit_triangles[i]]);
|
---|
1845 |
|
---|
1846 | if (DotProd(triObj->GetItem().GetNormal(), dirs[i] >= 0))
|
---|
1847 | ray = NULL;
|
---|
1848 | else
|
---|
1849 | ray = mRayCaster->RequestRay(origs[i], origs[i] + dirs[i] * dist[i], NULL, triObj, mPass, 1.0f);
|
---|
1850 | }
|
---|
1851 | else
|
---|
1852 | {
|
---|
1853 | ray = NULL;
|
---|
1854 | }
|
---|
1855 |
|
---|
1856 | vssRays.push_back(ray);
|
---|
1857 | }
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 |
|
---|
1861 | void GvsPreprocessor::CastRayBundle16(const SimpleRay &ray, VssRayContainer &vssRays, float scale)
|
---|
1862 | {
|
---|
1863 | static SimpleRayContainer simpleRays;
|
---|
1864 | simpleRays.clear();
|
---|
1865 |
|
---|
1866 | simpleRays.push_back(ray);
|
---|
1867 | GenerateJitteredRays(simpleRays, ray, 15, 0, scale);
|
---|
1868 |
|
---|
1869 | CastRays(simpleRays, vssRays, false, false);
|
---|
1870 | }
|
---|
1871 |
|
---|
1872 |
|
---|
1873 | void GvsPreprocessor::CastRayBundles16(const SimpleRayContainer &rays, VssRayContainer &vssRays, float scale)
|
---|
1874 | {
|
---|
1875 | SimpleRayContainer::const_iterator it, it_end = rays.end();
|
---|
1876 |
|
---|
1877 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
1878 | CastRayBundle16(*it, vssRays, scale);
|
---|
1879 | }
|
---|
1880 |
|
---|
1881 |
|
---|
1882 | void GvsPreprocessor::ComputeRenderError()
|
---|
1883 | {
|
---|
1884 | // compute rendering error
|
---|
1885 | if (renderer && renderer->mPvsStatFrames)
|
---|
1886 | {
|
---|
1887 | if (!mViewCellsManager->GetViewCellPointsList()->empty())
|
---|
1888 | {
|
---|
1889 | ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
|
---|
1890 | ViewCellPointsList::const_iterator vit = vcPoints->begin(), vit_end = vcPoints->end();
|
---|
1891 |
|
---|
1892 | SimpleRayContainer viewPoints;
|
---|
1893 |
|
---|
1894 | for (; vit != vit_end; ++ vit)
|
---|
1895 | {
|
---|
1896 | ViewCellPoints *vp = *vit;
|
---|
1897 |
|
---|
1898 | SimpleRayContainer::const_iterator rit = vp->second.begin(), rit_end = vp->second.end();
|
---|
1899 |
|
---|
1900 | for (; rit!=rit_end; ++rit)
|
---|
1901 | {
|
---|
1902 | ViewCell *vc = mViewCellsManager->GetViewCell((*rit).mOrigin);
|
---|
1903 | if (vc == mCurrentViewCell)
|
---|
1904 | viewPoints.push_back(*rit);
|
---|
1905 | }
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | renderer->mPvsErrorBuffer.clear();
|
---|
1909 |
|
---|
1910 | if (viewPoints.size() != renderer->mPvsErrorBuffer.size())
|
---|
1911 | {
|
---|
1912 | renderer->mPvsErrorBuffer.resize(viewPoints.size());
|
---|
1913 | renderer->ClearErrorBuffer();
|
---|
1914 | }
|
---|
1915 |
|
---|
1916 | cout << "evaluating list of " << viewPoints.size() << " pts" << endl;
|
---|
1917 | renderer->EvalPvsStat(viewPoints);
|
---|
1918 | }
|
---|
1919 | else
|
---|
1920 | {
|
---|
1921 | cout << "evaluating random points" << endl;
|
---|
1922 | renderer->EvalPvsStat();
|
---|
1923 | }
|
---|
1924 |
|
---|
1925 | mStats <<
|
---|
1926 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
1927 | "#AvgPixelError\n" <<renderer->GetAvgPixelError()<<endl<<
|
---|
1928 | "#MaxPixelError\n" <<renderer->GetMaxPixelError()<<endl<<
|
---|
1929 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
1930 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
|
---|
1931 | "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
|
---|
1932 | }
|
---|
1933 | }
|
---|
1934 |
|
---|
1935 |
|
---|
1936 | void GvsPreprocessor::GenerateImportanceSamples(const VssRay &ray,
|
---|
1937 | const Triangle3 &triangle,
|
---|
1938 | int numSamples,
|
---|
1939 | SimpleRayContainer &simpleRays)
|
---|
1940 | {
|
---|
1941 | const size_t samplesSize = simpleRays.size();
|
---|
1942 |
|
---|
1943 | while (simpleRays.size() < (samplesSize + numSamples))
|
---|
1944 | {
|
---|
1945 | SimpleRay sray;
|
---|
1946 | if (GenerateImportanceSample(ray, triangle, sray))
|
---|
1947 | simpleRays.push_back(sray);
|
---|
1948 | }
|
---|
1949 | }
|
---|
1950 |
|
---|
1951 |
|
---|
1952 | bool GvsPreprocessor::GenerateImportanceSample(const VssRay &ray,
|
---|
1953 | const Triangle3 &triangle,
|
---|
1954 | SimpleRay &sray)
|
---|
1955 | {
|
---|
1956 | Vector3 d = ray.GetDir();
|
---|
1957 |
|
---|
1958 | // Compute right handed coordinate system from direction
|
---|
1959 | Vector3 U, V;
|
---|
1960 |
|
---|
1961 | Vector3 nd = Normalize(d);
|
---|
1962 | nd.RightHandedBase(U, V);
|
---|
1963 |
|
---|
1964 | Vector3 origin = ray.mOrigin;
|
---|
1965 | Vector3 termination = ray.mTermination;
|
---|
1966 |
|
---|
1967 | float rr[2];
|
---|
1968 |
|
---|
1969 | rr[0] = RandomValue(0, 1);
|
---|
1970 | rr[1] = RandomValue(0, 1);
|
---|
1971 |
|
---|
1972 | Vector2 vr2(rr[0], rr[1]);
|
---|
1973 | const float sigma = triangle.GetBoundingBox().Radius() * mRadiusOfInfluence;
|
---|
1974 | Vector2 gaussvec2;
|
---|
1975 |
|
---|
1976 | GaussianOn2D(vr2, sigma, // input
|
---|
1977 | gaussvec2); // output
|
---|
1978 |
|
---|
1979 | const Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
|
---|
1980 |
|
---|
1981 | //cout << "t: " << termination;
|
---|
1982 | termination += shift;
|
---|
1983 | //cout << " new t: " << termination << endl;
|
---|
1984 | Vector3 direction = termination - origin;
|
---|
1985 |
|
---|
1986 | const float len = Magnitude(direction);
|
---|
1987 |
|
---|
1988 | if (len < Limits::Small)
|
---|
1989 | return false;
|
---|
1990 |
|
---|
1991 | direction /= len;
|
---|
1992 |
|
---|
1993 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
1994 | const float pdf = 1.0f;
|
---|
1995 | sray = SimpleRay(origin, direction, SamplingStrategy::GVS, pdf);
|
---|
1996 |
|
---|
1997 | return true;
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 |
|
---|
2001 | void GvsPreprocessor::PrepareProbablyVisibleSampling()
|
---|
2002 | {
|
---|
2003 |
|
---|
2004 | // warning: using mailing!
|
---|
2005 | Intersectable::NewMail();
|
---|
2006 |
|
---|
2007 | mProbablyVisibleTriangles.clear();
|
---|
2008 | CollectProbablyVisibleTriangles(mProbablyVisibleTriangles);
|
---|
2009 | }
|
---|
2010 |
|
---|
2011 |
|
---|
2012 | void GvsPreprocessor::CollectProbablyVisibleTriangles(ObjectContainer &triangles)
|
---|
2013 | {
|
---|
2014 | ObjectPvsIterator pit = mCurrentViewCell->GetPvs().GetIterator();
|
---|
2015 |
|
---|
2016 | static ObjectContainer tmpTriangles;
|
---|
2017 |
|
---|
2018 | while (pit.HasMoreEntries())
|
---|
2019 | {
|
---|
2020 | tmpTriangles.clear();
|
---|
2021 |
|
---|
2022 | KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
|
---|
2023 | mKdTree->CollectObjectsWithDublicates(kdObj->GetItem(), tmpTriangles);
|
---|
2024 |
|
---|
2025 | ObjectContainer::const_iterator oit, oit_end = tmpTriangles.end();
|
---|
2026 |
|
---|
2027 | for (oit = tmpTriangles.begin(); oit != oit_end; ++ oit)
|
---|
2028 | {
|
---|
2029 | TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
|
---|
2030 |
|
---|
2031 | // find objects which are not yet accounted for yet contained in kd pvs objects
|
---|
2032 | if (!triObj->Mailed() && (triObj->mCounter < ACCOUNTED_OBJECT))
|
---|
2033 | {
|
---|
2034 | triObj->Mail();
|
---|
2035 | triangles.push_back(triObj);
|
---|
2036 | }
|
---|
2037 | }
|
---|
2038 | }
|
---|
2039 | }
|
---|
2040 |
|
---|
2041 |
|
---|
2042 | void GvsPreprocessor::CreateRandomizedReverseRays(const Vector3 &origin,
|
---|
2043 | const Vector3 &termination,
|
---|
2044 | const Triangle3 &triangle,
|
---|
2045 | SimpleRayContainer &rays,
|
---|
2046 | int number)
|
---|
2047 | {
|
---|
2048 | size_t currentNumber = rays.size();
|
---|
2049 |
|
---|
2050 | int tries = 0;
|
---|
2051 |
|
---|
2052 | while (((rays.size() - currentNumber) < number) && (tries ++ < 20))
|
---|
2053 | {
|
---|
2054 | SimpleRay ray;
|
---|
2055 | if (CreateRandomizedReverseRay(origin, termination, triangle, ray))
|
---|
2056 | rays.push_back(ray);
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | //cout << "generated " << rays.size() << " rays" << endl;
|
---|
2060 | }
|
---|
2061 |
|
---|
2062 |
|
---|
2063 | bool GvsPreprocessor::CreateRandomizedReverseRay(const Vector3 &origin,
|
---|
2064 | const Vector3 &termination,
|
---|
2065 | const Triangle3 &triangle,
|
---|
2066 | SimpleRay &ray)
|
---|
2067 | {
|
---|
2068 | //Vector3 nd = triangle.GetNormal();
|
---|
2069 | const AxisAlignedBox3 box = mCurrentViewCell->GetBox();
|
---|
2070 | Vector3 nd = box.GetNearestFace(origin).GetNormal();
|
---|
2071 | //cout << "pt: " << origin << " face: " << box.GetNearestFace(origin) << endl;
|
---|
2072 | nd.Normalize();
|
---|
2073 |
|
---|
2074 | // Compute right handed coordinate system from direction
|
---|
2075 | Vector3 U, V;
|
---|
2076 | nd.RightHandedBase(U, V);
|
---|
2077 |
|
---|
2078 | float rr[2];
|
---|
2079 |
|
---|
2080 | rr[0] = RandomValue(0, 1);
|
---|
2081 | rr[1] = RandomValue(0, 1);
|
---|
2082 |
|
---|
2083 | Vector2 vr2(rr[0], rr[1]);
|
---|
2084 | const float sigma = triangle.GetBoundingBox().Radius() * mRadiusOfInfluence;
|
---|
2085 | Vector2 gaussvec2;
|
---|
2086 |
|
---|
2087 | GaussianOn2D(vr2, sigma, // input
|
---|
2088 | gaussvec2); // output
|
---|
2089 |
|
---|
2090 | const Vector3 shift = gaussvec2.xx * U + gaussvec2.yy * V;
|
---|
2091 |
|
---|
2092 | Vector3 newOrigin = origin + shift;
|
---|
2093 | //cout << "o: " << origin << " new o: " << newOrigin << endl;
|
---|
2094 |
|
---|
2095 | Vector3 direction = termination - newOrigin;
|
---|
2096 |
|
---|
2097 | const float len = Magnitude(direction);
|
---|
2098 |
|
---|
2099 | if (len < Limits::Small)
|
---|
2100 | return false;
|
---|
2101 |
|
---|
2102 | direction /= len;
|
---|
2103 |
|
---|
2104 | if (!IntersectsViewCell(newOrigin, -direction))
|
---|
2105 | {
|
---|
2106 | //cout << "x";
|
---|
2107 | return false;
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | //cout << "y";
|
---|
2111 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
2112 | const float pdf = 1.0f;
|
---|
2113 | ray = SimpleRay(newOrigin, direction, SamplingStrategy::GVS, pdf);
|
---|
2114 |
|
---|
2115 | return true;
|
---|
2116 | }
|
---|
2117 |
|
---|
2118 |
|
---|
2119 | }
|
---|