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