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