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