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