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