1 | #include "ViewCellsManager.h"
|
---|
2 | #include "RenderSimulator.h"
|
---|
3 | #include "Mesh.h"
|
---|
4 | #include "Triangle3.h"
|
---|
5 | #include "ViewCell.h"
|
---|
6 | #include "Environment.h"
|
---|
7 | #include "X3dParser.h"
|
---|
8 | #include "ViewCellBsp.h"
|
---|
9 | #include "KdTree.h"
|
---|
10 | #include "HierarchyManager.h"
|
---|
11 | #include "Exporter.h"
|
---|
12 | #include "VspBspTree.h"
|
---|
13 | #include "ViewCellsParser.h"
|
---|
14 | #include "Beam.h"
|
---|
15 | #include "VssPreprocessor.h"
|
---|
16 | #include "RssPreprocessor.h"
|
---|
17 | #include "BoundingBoxConverter.h"
|
---|
18 | #include "GlRenderer.h"
|
---|
19 | #include "ResourceManager.h"
|
---|
20 | #include "IntersectableWrapper.h"
|
---|
21 | #include "VspTree.h"
|
---|
22 | #include "OspTree.h"
|
---|
23 | #include "BvHierarchy.h"
|
---|
24 | #include "SamplingStrategy.h"
|
---|
25 | #include "SceneGraph.h"
|
---|
26 | #include "Timer/PerfTimer.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | #define USE_RAY_LENGTH_AS_CONTRIBUTION 0
|
---|
30 | #define DIST_WEIGHTED_CONTRIBUTION 0
|
---|
31 | #define SUM_RAY_CONTRIBUTIONS 1
|
---|
32 | #define AVG_RAY_CONTRIBUTIONS 0
|
---|
33 | #define CONTRIBUTION_RELATIVE_TO_PVS_SIZE 0
|
---|
34 | #define PVS_ADD_DIRTY 1
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | namespace GtpVisibilityPreprocessor {
|
---|
39 |
|
---|
40 |
|
---|
41 | PerfTimer viewCellCastTimer;
|
---|
42 | PerfTimer pvsTimer;
|
---|
43 | PerfTimer objTimer;
|
---|
44 |
|
---|
45 | // HACK
|
---|
46 | const static bool SAMPLE_AFTER_SUBDIVISION = true;
|
---|
47 | //const static bool CLAMP_TO_BOX = false;
|
---|
48 | const static bool CLAMP_TO_BOX = true;
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
53 | {
|
---|
54 | return obj1->mId < obj2->mId;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | template <typename T> class myless
|
---|
59 | {
|
---|
60 | public:
|
---|
61 | //bool operator() (HierarchyNode *v1, HierarchyNode *v2) const
|
---|
62 | bool operator() (T v1, T v2) const
|
---|
63 | {
|
---|
64 | return (v1->GetMergeCost() < v2->GetMergeCost());
|
---|
65 | }
|
---|
66 | };
|
---|
67 |
|
---|
68 |
|
---|
69 | ViewCellsManager::ViewCellsManager(ViewCellsTree *viewCellsTree):
|
---|
70 | mRenderer(NULL),
|
---|
71 | mInitialSamples(0),
|
---|
72 | mConstructionSamples(0),
|
---|
73 | mPostProcessSamples(0),
|
---|
74 | mVisualizationSamples(0),
|
---|
75 | mTotalAreaValid(false),
|
---|
76 | mTotalArea(0.0f),
|
---|
77 | mViewCellsFinished(false),
|
---|
78 | mMaxPvsSize(9999999),
|
---|
79 | mMinPvsSize(0),
|
---|
80 | mMaxPvsRatio(1.0),
|
---|
81 | mViewCellPvsIsUpdated(false),
|
---|
82 | mPreprocessor(NULL),
|
---|
83 | mViewCellsTree(viewCellsTree),
|
---|
84 | mUsePredefinedViewCells(false),
|
---|
85 | mMixtureDistribution(NULL),
|
---|
86 | mEvaluationSamples(0)
|
---|
87 | {
|
---|
88 | mViewSpaceBox.Initialize();
|
---|
89 | ParseEnvironment();
|
---|
90 |
|
---|
91 | mViewCellsTree->SetViewCellsManager(this);
|
---|
92 | mSamplesStat.Reset();
|
---|
93 | mStats.open("mystats.log");
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | int ViewCellsManager::CastEvaluationSamples(const int samplesPerPass,
|
---|
98 | VssRayContainer &passSamples)
|
---|
99 | {
|
---|
100 | static int pass = 0;
|
---|
101 | static int totalRays = 0;
|
---|
102 |
|
---|
103 | SimpleRayContainer rays;
|
---|
104 | rays.reserve(samplesPerPass);
|
---|
105 | //passSamples.reserve(samplesPerPass * 2);
|
---|
106 |
|
---|
107 | long startTime = GetTime();
|
---|
108 |
|
---|
109 | cout<<"Progress :"<<totalRays / 1e6f << " M rays, " << (100.0f * totalRays) / (mEvaluationSamples + 1) << "%" << endl;
|
---|
110 |
|
---|
111 | //rays.clear();
|
---|
112 | //passSamples.clear();
|
---|
113 |
|
---|
114 | mMixtureDistribution->GenerateSamples(samplesPerPass, rays);
|
---|
115 |
|
---|
116 | const bool doubleRays = true;
|
---|
117 | const bool pruneInvalidRays = true;
|
---|
118 | mPreprocessor->CastRays(rays, passSamples, doubleRays, pruneInvalidRays);
|
---|
119 |
|
---|
120 | mMixtureDistribution->ComputeContributions(passSamples);
|
---|
121 | mMixtureDistribution->UpdateDistributions(passSamples);
|
---|
122 |
|
---|
123 | Real time = TimeDiff(startTime, GetTime());
|
---|
124 | PrintPvsStatistics(mStats);
|
---|
125 |
|
---|
126 | mStats <<
|
---|
127 | "#Pass\n" << pass ++ <<endl<<
|
---|
128 | "#Time\n" << time <<endl<<
|
---|
129 | "#TotalSamples\n" << totalRays << endl;
|
---|
130 |
|
---|
131 |
|
---|
132 | totalRays += samplesPerPass;
|
---|
133 |
|
---|
134 | return (int)passSamples.size();
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | void ViewCellsManager::ParseEnvironment()
|
---|
139 | {
|
---|
140 | // visualization stuff
|
---|
141 | Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportRays", mExportRays);
|
---|
142 | Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportGeometry", mExportGeometry);
|
---|
143 | Environment::GetSingleton()->GetFloatValue("ViewCells.maxPvsRatio", mMaxPvsRatio);
|
---|
144 |
|
---|
145 | Environment::GetSingleton()->GetBoolValue("ViewCells.processOnlyValidViewCells", mOnlyValidViewCells);
|
---|
146 |
|
---|
147 | Environment::GetSingleton()->GetIntValue("ViewCells.Construction.samples", mConstructionSamples);
|
---|
148 | Environment::GetSingleton()->GetIntValue("ViewCells.PostProcess.samples", mPostProcessSamples);
|
---|
149 | Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.useRaysForMerge", mUseRaysForMerge);
|
---|
150 |
|
---|
151 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.samples", mVisualizationSamples);
|
---|
152 |
|
---|
153 | Environment::GetSingleton()->GetIntValue("ViewCells.Construction.samplesPerPass", mSamplesPerPass);
|
---|
154 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportToFile", mExportViewCells);
|
---|
155 |
|
---|
156 | Environment::GetSingleton()->GetIntValue("ViewCells.active", mNumActiveViewCells);
|
---|
157 | Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.compress", mCompressViewCells);
|
---|
158 | Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.useClipPlane", mUseClipPlaneForViz);
|
---|
159 | Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.merge", mMergeViewCells);
|
---|
160 | Environment::GetSingleton()->GetBoolValue("ViewCells.evaluateViewCells", mEvaluateViewCells);
|
---|
161 | Environment::GetSingleton()->GetBoolValue("ViewCells.showVisualization", mShowVisualization);
|
---|
162 | Environment::GetSingleton()->GetIntValue("ViewCells.Filter.maxSize", mMaxFilterSize);
|
---|
163 | Environment::GetSingleton()->GetFloatValue("ViewCells.Filter.width", mFilterWidth);
|
---|
164 |
|
---|
165 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportBboxesForPvs", mExportBboxesForPvs);
|
---|
166 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportPvs", mExportPvs);
|
---|
167 |
|
---|
168 | Environment::GetSingleton()->GetBoolValue("ViewCells.useKdPvs", mUseKdPvs);
|
---|
169 |
|
---|
170 | char buf[100];
|
---|
171 | Environment::GetSingleton()->GetStringValue("ViewCells.samplingType", buf);
|
---|
172 |
|
---|
173 | Environment::GetSingleton()->GetFloatValue("ViewCells.triangleWeight", mTriangleWeight);
|
---|
174 | Environment::GetSingleton()->GetFloatValue("ViewCells.objectWeight", mObjectWeight);
|
---|
175 |
|
---|
176 | // choose mix of sampling strategies
|
---|
177 | if (0)
|
---|
178 | {
|
---|
179 | mStrategies.push_back(SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION);
|
---|
180 | //mStrategies.push_back(SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION);
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | //mStrategies.push_back(SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION);
|
---|
185 |
|
---|
186 | mStrategies.push_back(SamplingStrategy::OBJECT_BASED_DISTRIBUTION);
|
---|
187 | mStrategies.push_back(SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION);
|
---|
188 |
|
---|
189 | mStrategies.push_back(SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION);
|
---|
190 | }
|
---|
191 |
|
---|
192 | Debug << "casting strategies: ";
|
---|
193 | for (int i = 0; i < (int)mStrategies.size(); ++ i)
|
---|
194 | Debug << mStrategies[i] << " ";
|
---|
195 | Debug << endl;
|
---|
196 |
|
---|
197 | // sampling type for view cells construction samples
|
---|
198 | if (strcmp(buf, "object") == 0)
|
---|
199 | {
|
---|
200 | mSamplingType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
|
---|
201 | }
|
---|
202 | else if (strcmp(buf, "box") == 0)
|
---|
203 | {
|
---|
204 | mSamplingType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
205 | }
|
---|
206 | else if (strcmp(buf, "directional") == 0)
|
---|
207 | {
|
---|
208 | mSamplingType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
|
---|
209 | }
|
---|
210 | else if (strcmp(buf, "object_directional") == 0)
|
---|
211 | {
|
---|
212 | mSamplingType = SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION;
|
---|
213 | }
|
---|
214 | else if (strcmp(buf, "reverse_object") == 0)
|
---|
215 | {
|
---|
216 | mSamplingType = SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION;
|
---|
217 | }
|
---|
218 | /*else if (strcmp(buf, "interior") == 0)
|
---|
219 | {
|
---|
220 | mSamplingType = Preprocessor::OBJECTS_INTERIOR_DISTRIBUTION;
|
---|
221 | }*/
|
---|
222 | else
|
---|
223 | {
|
---|
224 | Debug << "error! wrong sampling type" << endl;
|
---|
225 | exit(0);
|
---|
226 | }
|
---|
227 |
|
---|
228 | // sampling type for evaluation samples
|
---|
229 | Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.samplingType", buf);
|
---|
230 |
|
---|
231 | if (strcmp(buf, "object") == 0)
|
---|
232 | {
|
---|
233 | mEvaluationSamplingType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
|
---|
234 | }
|
---|
235 | else if (strcmp(buf, "box") == 0)
|
---|
236 | {
|
---|
237 | mEvaluationSamplingType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
238 | }
|
---|
239 | else if (strcmp(buf, "directional") == 0)
|
---|
240 | {
|
---|
241 | mEvaluationSamplingType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
|
---|
242 | }
|
---|
243 | else if (strcmp(buf, "object_directional") == 0)
|
---|
244 | {
|
---|
245 | mEvaluationSamplingType = SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION;
|
---|
246 | }
|
---|
247 | else if (strcmp(buf, "reverse_object") == 0)
|
---|
248 | {
|
---|
249 | mEvaluationSamplingType = SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION;
|
---|
250 | }
|
---|
251 | else
|
---|
252 | {
|
---|
253 | mEvaluationSamplingType = -1;
|
---|
254 | Debug << "error! wrong sampling type" << endl;
|
---|
255 | exit(0);
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | /** The color code used during visualization.
|
---|
260 | */
|
---|
261 | Environment::GetSingleton()->GetStringValue("ViewCells.Visualization.colorCode", buf);
|
---|
262 |
|
---|
263 | if (strcmp(buf, "PVS") == 0)
|
---|
264 | mColorCode = 1;
|
---|
265 | else if (strcmp(buf, "MergedLeaves") == 0)
|
---|
266 | mColorCode = 2;
|
---|
267 | else if (strcmp(buf, "MergedTreeDiff") == 0)
|
---|
268 | mColorCode = 3;
|
---|
269 | else
|
---|
270 | mColorCode = 0;
|
---|
271 |
|
---|
272 |
|
---|
273 | Debug << "************ View Cells Manager options ***************" << endl;
|
---|
274 | Debug << "color code: " << mColorCode << endl;
|
---|
275 |
|
---|
276 | Debug << "export rays: " << mExportRays << endl;
|
---|
277 | Debug << "export geometry: " << mExportGeometry << endl;
|
---|
278 | Debug << "max pvs ratio: " << mMaxPvsRatio << endl;
|
---|
279 |
|
---|
280 | Debug << "process only valid view cells: " << mOnlyValidViewCells << endl;
|
---|
281 | Debug << "construction samples: " << mConstructionSamples << endl;
|
---|
282 | Debug << "post process samples: " << mPostProcessSamples << endl;
|
---|
283 | Debug << "post process use rays for merge: " << mUseRaysForMerge << endl;
|
---|
284 | Debug << "visualization samples: " << mVisualizationSamples << endl;
|
---|
285 | Debug << "construction samples per pass: " << mSamplesPerPass << endl;
|
---|
286 | Debug << "export to file: " << mExportViewCells << endl;
|
---|
287 |
|
---|
288 | Debug << "active view cells: " << mNumActiveViewCells << endl;
|
---|
289 | Debug << "post process compress: " << mCompressViewCells << endl;
|
---|
290 | Debug << "visualization use clipPlane: " << mUseClipPlaneForViz << endl;
|
---|
291 | Debug << "post process merge: " << mMergeViewCells << endl;
|
---|
292 | Debug << "evaluate view cells: " << mEvaluateViewCells << endl;
|
---|
293 | Debug << "sampling type: " << mSamplingType << endl;
|
---|
294 | Debug << "evaluation sampling type: " << mEvaluationSamplingType << endl;
|
---|
295 | Debug << "show visualization: " << mShowVisualization << endl;
|
---|
296 | Debug << "filter width: " << mFilterWidth << endl;
|
---|
297 | Debug << "sample after subdivision: " << SAMPLE_AFTER_SUBDIVISION << endl;
|
---|
298 |
|
---|
299 | Debug << "export bounding boxes: " << mExportBboxesForPvs << endl;
|
---|
300 | Debug << "export pvs for view cells: " << mExportPvs << endl;
|
---|
301 | Debug << "use kd pvs " << mUseKdPvs << endl;
|
---|
302 |
|
---|
303 | Debug << "triangle weight: " << mTriangleWeight << endl;
|
---|
304 | Debug << "object weight: " << mObjectWeight << endl;
|
---|
305 |
|
---|
306 | Debug << endl;
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | ViewCell *ViewCellsManager::GetViewCellById(const int id)
|
---|
311 | {
|
---|
312 | ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
|
---|
313 |
|
---|
314 | for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
315 | {
|
---|
316 | if ((*vit)->GetId() == id)
|
---|
317 | return (*vit);
|
---|
318 | }
|
---|
319 | return NULL;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | bool ViewCellsManager::ExportRandomViewCells(const string &filename,
|
---|
324 | const vector<ViewCellPoints *> &viewCells)
|
---|
325 | {
|
---|
326 | std::ofstream outStream;
|
---|
327 | outStream.open(filename.c_str());
|
---|
328 |
|
---|
329 | vector<ViewCellPoints *>::const_iterator vit, vit_end = viewCells.end();
|
---|
330 |
|
---|
331 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
332 | {
|
---|
333 | ViewCell *vc = (*vit)->first;
|
---|
334 |
|
---|
335 | outStream << "v " << vc->GetId() << endl;
|
---|
336 |
|
---|
337 | SimpleRayContainer viewPoints;
|
---|
338 |
|
---|
339 | SimpleRayContainer::const_iterator pit, pit_end = (*vit)->second.end();
|
---|
340 |
|
---|
341 | for (pit = (*vit)->second.begin(); pit != pit_end; ++ pit)
|
---|
342 | {
|
---|
343 | const Vector3 pt = (*pit).mOrigin;
|
---|
344 | const Vector3 dir = (*pit).mDirection;
|
---|
345 |
|
---|
346 | outStream << "p " << pt.x << " " << pt.y << " " << pt.z
|
---|
347 | << " " << dir.x << " " << dir.y << " " << dir.z << endl;
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | return true;
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | bool ViewCellsManager::GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,
|
---|
356 | const int nViewCells,
|
---|
357 | const int nViewPoints)
|
---|
358 | {
|
---|
359 | ViewCellContainer rViewCells;
|
---|
360 |
|
---|
361 | cout << "generating " << nViewCells << " random view cells" << endl;
|
---|
362 | ///GenerateRandomViewCells(rViewCells, nViewCells);
|
---|
363 |
|
---|
364 | //cout << "finished" << endl;
|
---|
365 |
|
---|
366 | //for (int i = 0; i < viewCells.size(); ++ i)
|
---|
367 | // cout << "vc " << i << ": " << viewCells[i]->GetId() << endl;
|
---|
368 |
|
---|
369 | cout << "generating " << nViewPoints << " view points per view cell" << endl;
|
---|
370 |
|
---|
371 | int generatedViewCells = 0;
|
---|
372 | int i = 0;
|
---|
373 | while (generatedViewCells < nViewCells)
|
---|
374 | {
|
---|
375 | ++ i;
|
---|
376 |
|
---|
377 | const int idx = (int)RandomValue(0.0f, (float)mViewCells.size() - 0.5f);
|
---|
378 |
|
---|
379 | ViewCell *viewCell = GetViewCell(idx);
|
---|
380 |
|
---|
381 | cout << "testing view cell: " << viewCell->GetId() << endl;
|
---|
382 |
|
---|
383 | if (!viewCell->Mailed())
|
---|
384 | {
|
---|
385 | viewCell->Mail();
|
---|
386 |
|
---|
387 | SimpleRayContainer viewPoints;
|
---|
388 |
|
---|
389 | // generate random view points
|
---|
390 | if (IsValidViewSpace(viewCell) &&
|
---|
391 | GenerateViewPoints(viewCell, nViewPoints, viewPoints))
|
---|
392 | {
|
---|
393 | Debug << "vc: " << viewCell->GetId() << endl;
|
---|
394 |
|
---|
395 | ViewCellPoints *vcPts = new ViewCellPoints();
|
---|
396 | viewCells.push_back(vcPts);
|
---|
397 |
|
---|
398 | // valid view cell found
|
---|
399 | vcPts->first = viewCell;
|
---|
400 |
|
---|
401 | // generate view points
|
---|
402 | ++ generatedViewCells;
|
---|
403 |
|
---|
404 | SimpleRayContainer::const_iterator pit, pit_end = viewPoints.end();
|
---|
405 |
|
---|
406 | for (pit = viewPoints.begin(); pit != pit_end; ++ pit)
|
---|
407 | {
|
---|
408 | Debug << "vp: " << (*pit) << endl;
|
---|
409 | vcPts->second.push_back(*pit);
|
---|
410 | }
|
---|
411 | cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;
|
---|
412 | }
|
---|
413 | else
|
---|
414 | {
|
---|
415 | cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | if (i > nViewCells * 1000000) // safety
|
---|
420 | {
|
---|
421 | cout << "big error" << endl;
|
---|
422 | break;
|
---|
423 | }
|
---|
424 | cout << "processd view cells " << generatedViewCells << " of " << nViewCells << endl << endl;
|
---|
425 | }
|
---|
426 |
|
---|
427 | return true;
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | bool ViewCellsManager::ImportRandomViewCells(const string &filename,
|
---|
432 | vector<ViewCellPoints *> &viewCells)
|
---|
433 | {
|
---|
434 | ifstream inStream(filename.c_str());
|
---|
435 | if (!inStream.is_open())
|
---|
436 | return false;
|
---|
437 |
|
---|
438 | ViewCellPoints *currentViewCell = NULL;
|
---|
439 |
|
---|
440 | string buf;
|
---|
441 | while (!(getline(inStream, buf)).eof())
|
---|
442 | {
|
---|
443 | switch (buf[0])
|
---|
444 | {
|
---|
445 | case 'v':
|
---|
446 | {
|
---|
447 | int id;
|
---|
448 | sscanf(buf.c_str(), "v %d", &id);
|
---|
449 |
|
---|
450 | currentViewCell = new ViewCellPoints();
|
---|
451 | currentViewCell->first = GetViewCellById(id);
|
---|
452 |
|
---|
453 | viewCells.push_back(currentViewCell);
|
---|
454 | break;
|
---|
455 | }
|
---|
456 | case 'p':
|
---|
457 | {
|
---|
458 | Vector3 pt, dir;
|
---|
459 | sscanf(buf.c_str(), "p %f %f %f %f %f %f", &pt.x, &pt.y, &pt.z, &dir.x, &dir.y, &dir.z);
|
---|
460 |
|
---|
461 | SimpleRay ray(pt, dir, 0, 1);
|
---|
462 | currentViewCell->second.push_back(ray);
|
---|
463 | break;
|
---|
464 | }
|
---|
465 | default:
|
---|
466 | break;
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | return true;
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | bool ViewCellsManager::ImportRandomViewCells(const string &filename)
|
---|
475 | {
|
---|
476 | return ImportRandomViewCells(filename, mViewCellPoints);
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 | bool ViewCellsManager::GenerateViewPoints(ViewCell *viewCell,
|
---|
481 | const int numViewPoints,
|
---|
482 | SimpleRayContainer &viewPoints)
|
---|
483 | {
|
---|
484 | bool success = true;
|
---|
485 | int generatedPts = 0;
|
---|
486 | int i = 0;
|
---|
487 |
|
---|
488 | cout << "generating view points for view cell " << viewCell->GetId() << endl;
|
---|
489 |
|
---|
490 | while (generatedPts < numViewPoints)
|
---|
491 | {
|
---|
492 | SimpleRay pt;
|
---|
493 |
|
---|
494 | if (GenerateViewPoint(viewCell, pt))
|
---|
495 | {
|
---|
496 | ++ generatedPts;
|
---|
497 | cout << "generated view point " << generatedPts << endl;
|
---|
498 | viewPoints.push_back(pt);
|
---|
499 | }
|
---|
500 |
|
---|
501 | // savety criterium
|
---|
502 | if (++ i > numViewPoints * 3)
|
---|
503 | {
|
---|
504 | return false;
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | cout << "view point generation finished" << endl;
|
---|
509 |
|
---|
510 | return true;
|
---|
511 | }
|
---|
512 |
|
---|
513 |
|
---|
514 | bool ViewCellsManager::ExportRandomViewCells(const string &filename)
|
---|
515 | {
|
---|
516 | const int numViewCells = 100;
|
---|
517 | const int numViewPoints = 10;
|
---|
518 |
|
---|
519 | preprocessor->mViewCellsManager->
|
---|
520 | GenerateRandomViewCells(mViewCellPoints, numViewCells, numViewPoints);
|
---|
521 |
|
---|
522 | //cout << "exporting random view cells" << endl;
|
---|
523 | return preprocessor->mViewCellsManager->ExportRandomViewCells(filename, mViewCellPoints);
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 | bool ViewCellsManager::GenerateViewPoint(ViewCell *viewCell,
|
---|
528 | SimpleRay &ray)
|
---|
529 | {
|
---|
530 | // do not use this function since it could return different
|
---|
531 | // viewpoints for different executions of the algorithm
|
---|
532 |
|
---|
533 | int tries = 0;
|
---|
534 | Vector3 viewPoint, direction;
|
---|
535 | const int maxTries = 10;
|
---|
536 |
|
---|
537 | while (1)
|
---|
538 | {
|
---|
539 | // hack
|
---|
540 | if (!viewCell->GetMesh())
|
---|
541 | CreateMesh(viewCell);
|
---|
542 |
|
---|
543 | Mesh *mesh = viewCell->GetMesh();
|
---|
544 | AxisAlignedBox3 box = mesh->mBox;
|
---|
545 |
|
---|
546 | Vector3 pVector = Vector3(Random(1.0f),
|
---|
547 | Random(1.0f),
|
---|
548 | Random(1.0f));
|
---|
549 |
|
---|
550 | viewPoint = box.GetPoint(pVector);
|
---|
551 |
|
---|
552 | const Vector3 dVector = Vector3(2 * M_PI * Random(1.0f), M_PI * Random(1.0f), 0.0f);
|
---|
553 | direction = Normalize(Vector3(sin(dVector.x), 0.0f, cos(dVector.x)));
|
---|
554 |
|
---|
555 | ViewCell *v = GetViewCell(viewPoint);
|
---|
556 |
|
---|
557 | if (v && v->GetValid())
|
---|
558 | {
|
---|
559 | mPreprocessor->GetRenderer()->mViewPoint = viewPoint;
|
---|
560 | mPreprocessor->GetRenderer()->mViewDirection = direction;
|
---|
561 |
|
---|
562 | if (mPreprocessor->GetRenderer()->ValidViewPoint())
|
---|
563 | {
|
---|
564 | cout << "view point valid " << viewPoint << " " << direction << endl;
|
---|
565 | break;
|
---|
566 | }
|
---|
567 |
|
---|
568 | }
|
---|
569 |
|
---|
570 | if (++ tries > maxTries)
|
---|
571 | {
|
---|
572 | cerr << "error: no view point computed" << endl;
|
---|
573 | return false;
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | ray = SimpleRay(viewPoint, direction, 0, 1);
|
---|
578 | //cout << "view point generated: " << viewPoint << " " << direction << endl;
|
---|
579 |
|
---|
580 | return true;
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | bool ViewCellsManager::IsValidViewSpace(ViewCell *vc)
|
---|
585 | {
|
---|
586 | SimpleRay simpleRay;
|
---|
587 | //check if view point can be generated
|
---|
588 | return GenerateViewPoint(vc, simpleRay);
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 | bool ViewCellsManager::GenerateRandomViewCells(ViewCellContainer &viewCells,
|
---|
593 | const int numViewCells)
|
---|
594 | {
|
---|
595 | int generatedViewCells = 0;
|
---|
596 | //HaltonSequence halton;
|
---|
597 | //float r[1];
|
---|
598 |
|
---|
599 | ViewCell::NewMail();
|
---|
600 |
|
---|
601 | while (generatedViewCells < numViewCells)
|
---|
602 | {
|
---|
603 | // savety criterium
|
---|
604 | const int tries = 100000 + generatedViewCells;
|
---|
605 | int i = 0;
|
---|
606 |
|
---|
607 | // generate next view cell
|
---|
608 | while (1)
|
---|
609 | {
|
---|
610 | //halton.GetNext(1, r);
|
---|
611 | //const int idx = (int)(r[0] * mViewCells.size() - 1.0f);
|
---|
612 | const int idx = (int)RandomValue(0.0f, (float)mViewCells.size() - 0.5f);
|
---|
613 |
|
---|
614 | ViewCell *viewCell = GetViewCell(idx);
|
---|
615 |
|
---|
616 | if (!viewCell->Mailed())
|
---|
617 | {
|
---|
618 | viewCell->Mail();
|
---|
619 |
|
---|
620 | // check for valid view space
|
---|
621 | if (IsValidViewSpace(viewCell))
|
---|
622 | {
|
---|
623 | // valid view cell found
|
---|
624 | viewCells.push_back(viewCell);
|
---|
625 |
|
---|
626 | ++ generatedViewCells;
|
---|
627 | //cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;
|
---|
628 | break;
|
---|
629 | }
|
---|
630 | else
|
---|
631 | {
|
---|
632 | cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;
|
---|
633 | }
|
---|
634 | }
|
---|
635 |
|
---|
636 | if (++ i == tries) // no new view cell fond
|
---|
637 | {
|
---|
638 | cerr << "big error! no view cell found" << endl;
|
---|
639 | return false;
|
---|
640 | }
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | return true;
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | ViewCellsManager::~ViewCellsManager()
|
---|
649 | {
|
---|
650 | // HACK: if view cells tree does not
|
---|
651 | // handle view cells, we have to do it here
|
---|
652 | // question: rather create view cells resource manager?
|
---|
653 | if (!ViewCellsTreeConstructed())
|
---|
654 | {
|
---|
655 | CLEAR_CONTAINER(mViewCells);
|
---|
656 | }
|
---|
657 | else
|
---|
658 | {
|
---|
659 | DEL_PTR(mViewCellsTree);
|
---|
660 | }
|
---|
661 |
|
---|
662 | DEL_PTR(mMixtureDistribution);
|
---|
663 | CLEAR_CONTAINER(mViewCellPoints);
|
---|
664 | }
|
---|
665 |
|
---|
666 |
|
---|
667 | Intersectable *ViewCellsManager::GetIntersectable(const VssRay &ray, const bool isTermination) const
|
---|
668 | {
|
---|
669 | if (mUseKdPvs)
|
---|
670 | {
|
---|
671 | KdNode *node = GetPreprocessor()->
|
---|
672 | mKdTree->GetPvsNode(isTermination ? ray.mTermination : ray.mOrigin);
|
---|
673 |
|
---|
674 | return GetPreprocessor()->mKdTree->GetOrCreateKdIntersectable(node);
|
---|
675 | }
|
---|
676 | else
|
---|
677 | {
|
---|
678 | return isTermination ? ray.mTerminationObject : ray.mOriginObject;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | void ViewCellsManager::CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects)
|
---|
684 | {
|
---|
685 | GetPreprocessor()->mKdTree->CollectObjects(box, objects);
|
---|
686 | }
|
---|
687 |
|
---|
688 |
|
---|
689 | AxisAlignedBox3 ViewCellsManager::GetViewCellBox(ViewCell *vc)
|
---|
690 | {
|
---|
691 | Mesh *m = vc->GetMesh();
|
---|
692 |
|
---|
693 | if (m)
|
---|
694 | {
|
---|
695 | m->ComputeBoundingBox();
|
---|
696 | return m->mBox;
|
---|
697 | }
|
---|
698 |
|
---|
699 | AxisAlignedBox3 box;
|
---|
700 | box.Initialize();
|
---|
701 |
|
---|
702 | if (!vc->IsLeaf())
|
---|
703 | {
|
---|
704 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
705 |
|
---|
706 | ViewCellContainer::iterator it = vci->mChildren.begin();
|
---|
707 | for (; it != vci->mChildren.end(); ++it)
|
---|
708 | {
|
---|
709 | box.Include(GetViewCellBox(*it));
|
---|
710 | }
|
---|
711 | }
|
---|
712 |
|
---|
713 | return box;
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | int ViewCellsManager::CastPassSamples(const int samplesPerPass,
|
---|
718 | const vector<int> &strategies,
|
---|
719 | VssRayContainer &passSamples
|
---|
720 | ) const
|
---|
721 | {
|
---|
722 | long startTime = GetTime();
|
---|
723 |
|
---|
724 | SimpleRayContainer simpleRays;
|
---|
725 |
|
---|
726 | simpleRays.reserve(samplesPerPass);
|
---|
727 | // reserve 2 times the size because always creates double rays
|
---|
728 | passSamples.reserve(samplesPerPass * 2);
|
---|
729 |
|
---|
730 | // create one third of each type
|
---|
731 | int castRays = 0;
|
---|
732 |
|
---|
733 | const int numRaysPerPass = samplesPerPass / (int)strategies.size();
|
---|
734 | vector<int>::const_iterator iit, iit_end = strategies.end();
|
---|
735 |
|
---|
736 | for (iit = strategies.begin(); iit != iit_end; ++ iit)
|
---|
737 | {
|
---|
738 | const int stype = *iit;
|
---|
739 | const int newRays =
|
---|
740 | mPreprocessor->GenerateRays(numRaysPerPass, stype, simpleRays);
|
---|
741 |
|
---|
742 | cout << "cast " << newRays << " rays of strategy " << stype << endl;
|
---|
743 | castRays += newRays;
|
---|
744 | }
|
---|
745 |
|
---|
746 | cout << "generated " << samplesPerPass << " samples in "
|
---|
747 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
748 |
|
---|
749 | startTime = GetTime();
|
---|
750 |
|
---|
751 | // shoot simple ray and add it to importance samples
|
---|
752 | mPreprocessor->CastRays(simpleRays, passSamples, true);
|
---|
753 |
|
---|
754 | cout << "cast " << samplesPerPass << " samples in "
|
---|
755 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
756 |
|
---|
757 | return (int)passSamples.size();
|
---|
758 | }
|
---|
759 |
|
---|
760 |
|
---|
761 | /// helper function which destroys rays or copies them into the output ray container
|
---|
762 | inline void disposeRays(VssRayContainer &rays, VssRayContainer *outRays)
|
---|
763 | {
|
---|
764 | cout << "disposing samples ... ";
|
---|
765 | long startTime = GetTime();
|
---|
766 | int n = (int)rays.size();
|
---|
767 |
|
---|
768 | if (outRays)
|
---|
769 | {
|
---|
770 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
771 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
772 | {
|
---|
773 | outRays->push_back(*it);
|
---|
774 | }
|
---|
775 | }
|
---|
776 | else
|
---|
777 | {
|
---|
778 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
779 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
780 | {
|
---|
781 | if (!(*it)->IsActive())
|
---|
782 | delete (*it);
|
---|
783 | }
|
---|
784 | }
|
---|
785 |
|
---|
786 | cout << "finished" << endl;
|
---|
787 | Debug << "disposed " << n << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
788 | }
|
---|
789 |
|
---|
790 |
|
---|
791 | int ViewCellsManager::Construct(Preprocessor *preprocessor, VssRayContainer *outRays)
|
---|
792 | {
|
---|
793 | int numSamples = 0;
|
---|
794 |
|
---|
795 | SimpleRayContainer simpleRays;
|
---|
796 | VssRayContainer initialSamples;
|
---|
797 |
|
---|
798 | // store pointer to preprocessor for further use during construction
|
---|
799 | mPreprocessor = preprocessor;
|
---|
800 |
|
---|
801 | // now decode distribution string
|
---|
802 | char mix[1024];
|
---|
803 | Environment::GetSingleton()->GetStringValue("RssPreprocessor.distributions", mix);
|
---|
804 | Debug << "using mixture distributions: " << mix << endl;
|
---|
805 |
|
---|
806 | mMixtureDistribution = new MixtureDistribution(*mPreprocessor);
|
---|
807 | mMixtureDistribution->Construct(mix);
|
---|
808 |
|
---|
809 | ///////////////////////////////////////////////////////
|
---|
810 | //-- Initial sampling for the construction of the view cell hierarchy.
|
---|
811 | //-- We use uniform sampling / box based sampling.
|
---|
812 |
|
---|
813 | long startTime = GetTime();
|
---|
814 | cout << "view cell construction: casting " << mInitialSamples << " initial samples ... " << endl;
|
---|
815 |
|
---|
816 | // cast initial samples
|
---|
817 |
|
---|
818 | // mix of sampling strategies
|
---|
819 | #if 0
|
---|
820 | vector<int>dummy;
|
---|
821 | dummy.push_back(SamplingStrategy::OBJECT_BASED_DISTRIBUTION);
|
---|
822 | dummy.push_back(SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION);
|
---|
823 | dummy.push_back(SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION);
|
---|
824 | #endif
|
---|
825 | CastPassSamples(mInitialSamples, mStrategies, initialSamples);
|
---|
826 |
|
---|
827 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
828 |
|
---|
829 | // construct view cells
|
---|
830 | ConstructSubdivision(preprocessor->mObjects, initialSamples);
|
---|
831 |
|
---|
832 | // initial samples count for overall samples ...
|
---|
833 | numSamples += mInitialSamples;
|
---|
834 |
|
---|
835 | // rays can be passed or deleted
|
---|
836 | disposeRays(initialSamples, outRays);
|
---|
837 |
|
---|
838 | cout << "time needed for initial construction: "
|
---|
839 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
840 |
|
---|
841 | Debug << "time needed for initial construction: "
|
---|
842 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
843 |
|
---|
844 | // collect view cells and compute statistics
|
---|
845 | ResetViewCells();
|
---|
846 |
|
---|
847 |
|
---|
848 | ///////////////////
|
---|
849 | //-- Initial hierarchy construction finished.
|
---|
850 | //-- We can do some stats and visualization
|
---|
851 |
|
---|
852 | if (0)
|
---|
853 | {
|
---|
854 | //-- export initial view cell partition
|
---|
855 | Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
|
---|
856 |
|
---|
857 | const string filename("viewcells.wrl");
|
---|
858 | Exporter *exporter = Exporter::GetExporter(filename.c_str());
|
---|
859 |
|
---|
860 | if (exporter)
|
---|
861 | {
|
---|
862 | cout << "exporting initial view cells (=leaves) to " << filename.c_str() << " ... ";
|
---|
863 |
|
---|
864 | if (mExportGeometry)
|
---|
865 | {
|
---|
866 | exporter->ExportGeometry(preprocessor->mObjects);
|
---|
867 | }
|
---|
868 |
|
---|
869 | exporter->SetWireframe();
|
---|
870 | ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
|
---|
871 |
|
---|
872 | delete exporter;
|
---|
873 | cout << "finished" << endl;
|
---|
874 | }
|
---|
875 | }
|
---|
876 |
|
---|
877 |
|
---|
878 | //////////////////////
|
---|
879 | //-- Cast some more sampling after initial construction.
|
---|
880 | //-- The additional rays can be used to gain
|
---|
881 | //-- some more information before the bottom-up merge
|
---|
882 | //-- note: guided rays could be used for this task
|
---|
883 |
|
---|
884 | // time spent after construction of the initial partition
|
---|
885 | startTime = GetTime();
|
---|
886 | const int n = mConstructionSamples; //+initialSamples;
|
---|
887 |
|
---|
888 | while (numSamples < n)
|
---|
889 | {
|
---|
890 | cout << "casting " << mSamplesPerPass << " samples of " << n << " ... ";
|
---|
891 | Debug << "casting " << mSamplesPerPass << " samples of " << n << " ... ";
|
---|
892 |
|
---|
893 | VssRayContainer constructionSamples;
|
---|
894 |
|
---|
895 | // cast new samples
|
---|
896 | numSamples += CastPassSamples(mSamplesPerPass,
|
---|
897 | mStrategies,
|
---|
898 | constructionSamples);
|
---|
899 |
|
---|
900 | cout << "finished" << endl;
|
---|
901 | cout << "computing sample contribution for " << (int)constructionSamples.size() << " samples ... ";
|
---|
902 |
|
---|
903 | // computes sample contribution of cast rays
|
---|
904 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
905 | ComputeSampleContributions(constructionSamples, true, false);
|
---|
906 |
|
---|
907 | cout << "finished" << endl;
|
---|
908 |
|
---|
909 | disposeRays(constructionSamples, outRays);
|
---|
910 | cout << "total samples: " << numSamples << endl;
|
---|
911 | }
|
---|
912 |
|
---|
913 | if (0)
|
---|
914 | {
|
---|
915 | ///////////////
|
---|
916 | //-- Get stats after the additional sampling step
|
---|
917 | //-- and before the bottom-up merge step
|
---|
918 |
|
---|
919 | EvaluateViewCellsStats();
|
---|
920 | Debug << "\noriginal view cell partition before post process:\n"
|
---|
921 | << mCurrentViewCellsStats << endl;
|
---|
922 |
|
---|
923 | mRenderer->RenderScene();
|
---|
924 | SimulationStatistics ss;
|
---|
925 | static_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
926 |
|
---|
927 | Debug << ss << endl;
|
---|
928 | }
|
---|
929 |
|
---|
930 | ////////////////////
|
---|
931 | //-- post processing of the initial construction
|
---|
932 | //-- We can bottom-up merge the view cells in this step
|
---|
933 | //-- We can additionally cast some post processing sample rays.
|
---|
934 | //-- These rays can be used to store the view cells with the rays
|
---|
935 |
|
---|
936 | VssRayContainer postProcessSamples;
|
---|
937 | cout << "casting " << mPostProcessSamples << " post process samples ..." << endl;
|
---|
938 |
|
---|
939 | CastPassSamples(mPostProcessSamples, mStrategies, postProcessSamples);
|
---|
940 |
|
---|
941 | cout << "finished post process sampling" << endl;
|
---|
942 | cout << "starting post processing and visualization" << endl;
|
---|
943 |
|
---|
944 | // store view cells with rays for post processing?
|
---|
945 | const bool storeViewCells = true;
|
---|
946 |
|
---|
947 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
948 | ComputeSampleContributions(postProcessSamples, true, storeViewCells);
|
---|
949 |
|
---|
950 | PostProcess(preprocessor->mObjects, postProcessSamples);
|
---|
951 |
|
---|
952 | const float secs = TimeDiff(startTime, GetTime()) * 1e-3f;
|
---|
953 | cout << "post processing (=merge) finished in " << secs << " secs" << endl;
|
---|
954 |
|
---|
955 | Debug << "post processing time: " << secs << endl;
|
---|
956 | disposeRays(postProcessSamples, outRays);
|
---|
957 |
|
---|
958 |
|
---|
959 | ////////////////
|
---|
960 | //-- Evaluation of the resulting view cell partition.
|
---|
961 | //-- We cast a number of new samples and measure the render cost
|
---|
962 |
|
---|
963 | if (mEvaluateViewCells)
|
---|
964 | {
|
---|
965 | EvalViewCellPartition();
|
---|
966 | }
|
---|
967 |
|
---|
968 | /////////////////
|
---|
969 | //-- Show some visualizations
|
---|
970 |
|
---|
971 | if (mShowVisualization)
|
---|
972 | {
|
---|
973 | ///////////////
|
---|
974 | //-- visualization rays, e.g., to show some samples in the scene
|
---|
975 |
|
---|
976 | VssRayContainer visSamples;
|
---|
977 | int numSamples = CastPassSamples(mVisualizationSamples,
|
---|
978 | mStrategies,
|
---|
979 | visSamples);
|
---|
980 |
|
---|
981 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
982 | ComputeSampleContributions(visSamples, true, storeViewCells);
|
---|
983 |
|
---|
984 | // various visualizations
|
---|
985 | Visualize(preprocessor->mObjects, visSamples);
|
---|
986 |
|
---|
987 | disposeRays(visSamples, outRays);
|
---|
988 | }
|
---|
989 |
|
---|
990 | // recalculate view cells
|
---|
991 | EvaluateViewCellsStats();
|
---|
992 |
|
---|
993 | // compress the final solution
|
---|
994 | if (0) CompressViewCells();
|
---|
995 |
|
---|
996 | // write view cells to disc
|
---|
997 | if (mExportViewCells)
|
---|
998 | {
|
---|
999 | char filename[100];
|
---|
1000 |
|
---|
1001 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
|
---|
1002 | ExportViewCells(filename, mExportPvs, mPreprocessor->mObjects);
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | return numSamples;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 |
|
---|
1009 | AxisAlignedPlane *ViewCellsManager::GetClipPlane()
|
---|
1010 | {
|
---|
1011 | return mUseClipPlaneForViz ? &mClipPlaneForViz : NULL;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 |
|
---|
1015 | ViewCellsManager *ViewCellsManager::LoadViewCells(const string &filename,
|
---|
1016 | ObjectContainer &pvsObjects,
|
---|
1017 | bool finalizeViewCells,
|
---|
1018 | BoundingBoxConverter *bconverter)
|
---|
1019 |
|
---|
1020 | {
|
---|
1021 | if (!Debug.is_open()) Debug.open("debug.log");
|
---|
1022 |
|
---|
1023 |
|
---|
1024 | if (strstr(filename.c_str(), ".bn"))
|
---|
1025 | {
|
---|
1026 | Debug << "binary view cells format detected" << endl;
|
---|
1027 |
|
---|
1028 | return LoadViewCellsBinary(filename,
|
---|
1029 | pvsObjects,
|
---|
1030 | finalizeViewCells,
|
---|
1031 | bconverter);
|
---|
1032 | }
|
---|
1033 | else
|
---|
1034 | {
|
---|
1035 | Debug << "xml view cells format detected" << endl;
|
---|
1036 |
|
---|
1037 | // give just an empty container as parameter:
|
---|
1038 | // this loading function is used in the engine, thus it
|
---|
1039 | // does not need to load the entities the preprocessor works on
|
---|
1040 | ObjectContainer preprocessorObjects;
|
---|
1041 |
|
---|
1042 | return LoadViewCells(filename,
|
---|
1043 | pvsObjects,
|
---|
1044 | preprocessorObjects,
|
---|
1045 | finalizeViewCells,
|
---|
1046 | bconverter);
|
---|
1047 | }
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | void ViewCellsManager::LoadIndexedBoundingBoxesBinary(IN_STREAM &stream, IndexedBoundingBoxContainer &iboxes)
|
---|
1051 | {
|
---|
1052 | int numBoxes;
|
---|
1053 | stream.read(reinterpret_cast<char *>(&numBoxes), sizeof(int));
|
---|
1054 |
|
---|
1055 | iboxes.reserve(numBoxes);
|
---|
1056 |
|
---|
1057 | Vector3 bmin;
|
---|
1058 | Vector3 bmax;
|
---|
1059 | int id;
|
---|
1060 |
|
---|
1061 | PerfTimer boxTimer;
|
---|
1062 |
|
---|
1063 | boxTimer.Start();
|
---|
1064 | boxTimer.Entry();
|
---|
1065 |
|
---|
1066 | for (int i = 0; i < numBoxes; ++ i)
|
---|
1067 | {
|
---|
1068 | stream.read(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
|
---|
1069 | stream.read(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
|
---|
1070 | stream.read(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
1071 |
|
---|
1072 | iboxes.push_back(IndexedBoundingBox(id, AxisAlignedBox3(bmin, bmax)));
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 |
|
---|
1076 | boxTimer.Exit();
|
---|
1077 |
|
---|
1078 | Debug << numBoxes << " boxes loaded in " << boxTimer.TotalTime() << " secs" << endl;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 |
|
---|
1082 | ViewCellsManager *ViewCellsManager::LoadViewCells(const string &filename,
|
---|
1083 | ObjectContainer &pvsObjects,
|
---|
1084 | ObjectContainer &preprocessorObjects,
|
---|
1085 | bool finalizeViewCells,
|
---|
1086 | BoundingBoxConverter *bconverter)
|
---|
1087 | {
|
---|
1088 | ViewCellsParser parser;
|
---|
1089 | ViewCellsManager *vm = NULL;
|
---|
1090 |
|
---|
1091 | const long startTime = GetTime();
|
---|
1092 | const bool success = parser.ParseViewCellsFile(filename,
|
---|
1093 | &vm,
|
---|
1094 | pvsObjects,
|
---|
1095 | preprocessorObjects,
|
---|
1096 | bconverter);
|
---|
1097 |
|
---|
1098 | if (!success)
|
---|
1099 | {
|
---|
1100 | Debug << "Error: loading view cells failed!" << endl;
|
---|
1101 | DEL_PTR(vm);
|
---|
1102 |
|
---|
1103 | return NULL;
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | if (0) //hack: reset function should work, but something is wrong ..
|
---|
1107 | {
|
---|
1108 | vm->ResetViewCells();
|
---|
1109 | }
|
---|
1110 | else
|
---|
1111 | {
|
---|
1112 | ViewCellContainer leaves;
|
---|
1113 |
|
---|
1114 | vm->mViewCells.clear();
|
---|
1115 | vm->mViewCellsTree->CollectLeaves(vm->mViewCellsTree->GetRoot(), leaves);
|
---|
1116 |
|
---|
1117 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
1118 |
|
---|
1119 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
1120 | {
|
---|
1121 | vm->mViewCells.push_back(*it);
|
---|
1122 | }
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | vm->mViewCellsFinished = true;
|
---|
1126 | vm->mMaxPvsSize = (int)pvsObjects.size();
|
---|
1127 |
|
---|
1128 | if (finalizeViewCells)
|
---|
1129 | {
|
---|
1130 | // do some final computations, e.g.,
|
---|
1131 | // create the meshes and compute view cell volumes
|
---|
1132 | const bool createMeshes = true;
|
---|
1133 | vm->FinalizeViewCells(createMeshes);
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | cout << (int)vm->mViewCells.size() << " view cells loaded in "
|
---|
1137 | << TimeDiff(startTime, GetTime()) * 1e-3f << " secs" << endl;
|
---|
1138 |
|
---|
1139 | Debug << (int)vm->mViewCells.size() << " view cells loaded in "
|
---|
1140 | << TimeDiff(startTime, GetTime()) * 1e-3f << " secs" << endl;
|
---|
1141 |
|
---|
1142 | return vm;
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 |
|
---|
1146 | bool VspBspViewCellsManager::ExportViewCells(const string filename,
|
---|
1147 | const bool exportPvs,
|
---|
1148 | const ObjectContainer &objects)
|
---|
1149 | {
|
---|
1150 | // no view cells constructed yet
|
---|
1151 | if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
|
---|
1152 | return false;
|
---|
1153 |
|
---|
1154 | cout << "exporting view cells to xml ... ";
|
---|
1155 |
|
---|
1156 | OUT_STREAM stream(filename.c_str());
|
---|
1157 |
|
---|
1158 | // we need unique ids for each view cell
|
---|
1159 | CreateUniqueViewCellIds();
|
---|
1160 |
|
---|
1161 | stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
1162 | stream << "<VisibilitySolution>" << endl;
|
---|
1163 |
|
---|
1164 | if (exportPvs)
|
---|
1165 | {
|
---|
1166 | //-- export bounding boxes
|
---|
1167 | stream << "<BoundingBoxes>" << endl;
|
---|
1168 |
|
---|
1169 | if (mUseKdPvs)
|
---|
1170 | {
|
---|
1171 | vector<KdIntersectable *>::iterator kit, kit_end = GetPreprocessor()->mKdTree->mKdIntersectables.end();
|
---|
1172 |
|
---|
1173 | int id = 0;
|
---|
1174 | for (kit = GetPreprocessor()->mKdTree->mKdIntersectables.begin(); kit != kit_end; ++ kit, ++ id)
|
---|
1175 | {
|
---|
1176 | Intersectable *obj = *kit;
|
---|
1177 | const AxisAlignedBox3 box = obj->GetBox();
|
---|
1178 |
|
---|
1179 | obj->SetId(id);
|
---|
1180 |
|
---|
1181 | stream << "<BoundingBox" << " id=\"" << id << "\""
|
---|
1182 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
1183 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 | else
|
---|
1187 | {
|
---|
1188 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
1189 |
|
---|
1190 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
1191 | {
|
---|
1192 | const AxisAlignedBox3 box = (*oit)->GetBox();
|
---|
1193 |
|
---|
1194 | ////////////
|
---|
1195 | //-- the bounding boxes
|
---|
1196 |
|
---|
1197 | stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
|
---|
1198 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
1199 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
1200 | }
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | stream << "</BoundingBoxes>" << endl;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 |
|
---|
1207 | /////////////
|
---|
1208 | //-- export the view cells and the pvs
|
---|
1209 |
|
---|
1210 | const int numViewCells = mCurrentViewCellsStats.viewCells;
|
---|
1211 | stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
|
---|
1212 |
|
---|
1213 | mViewCellsTree->Export(stream, exportPvs);
|
---|
1214 |
|
---|
1215 | stream << "</ViewCells>" << endl;
|
---|
1216 |
|
---|
1217 |
|
---|
1218 | //////////
|
---|
1219 | //-- export the view space hierarchy
|
---|
1220 |
|
---|
1221 | stream << "<ViewSpaceHierarchy type=\"bsp\""
|
---|
1222 | << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
|
---|
1223 | << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
|
---|
1224 |
|
---|
1225 | mVspBspTree->Export(stream);
|
---|
1226 | stream << "</ViewSpaceHierarchy>" << endl;
|
---|
1227 |
|
---|
1228 | stream << "</VisibilitySolution>" << endl;
|
---|
1229 |
|
---|
1230 | stream.close();
|
---|
1231 | cout << "finished" << endl;
|
---|
1232 |
|
---|
1233 | return true;
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 |
|
---|
1237 | void ViewCellsManager::EvalViewCellHistogram(const string filename,
|
---|
1238 | const int nViewCells)
|
---|
1239 | {
|
---|
1240 | std::ofstream outstream;
|
---|
1241 | outstream.open(filename.c_str());
|
---|
1242 |
|
---|
1243 | ViewCellContainer viewCells;
|
---|
1244 |
|
---|
1245 | // the collect best viewcells does not work?
|
---|
1246 | mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
|
---|
1247 |
|
---|
1248 | float maxRenderCost, minRenderCost;
|
---|
1249 |
|
---|
1250 | // sort by render cost
|
---|
1251 | sort(viewCells.begin(), viewCells.end(), SmallerRenderCost);
|
---|
1252 |
|
---|
1253 | minRenderCost = viewCells.front()->GetRenderCost();
|
---|
1254 | maxRenderCost = viewCells.back()->GetRenderCost();
|
---|
1255 |
|
---|
1256 | Debug << "histogram min rc: " << minRenderCost << " max rc: " << maxRenderCost << endl;
|
---|
1257 |
|
---|
1258 | int histoIntervals;
|
---|
1259 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
|
---|
1260 | const int intervals = min(histoIntervals, (int)viewCells.size());
|
---|
1261 |
|
---|
1262 | int histoMaxVal;
|
---|
1263 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
|
---|
1264 | maxRenderCost = min((float)histoMaxVal, maxRenderCost);
|
---|
1265 |
|
---|
1266 |
|
---|
1267 | const float range = maxRenderCost - minRenderCost;
|
---|
1268 | const float stepSize = range / (float)intervals;
|
---|
1269 |
|
---|
1270 | float currentRenderCost = minRenderCost;//(int)ceil(minRenderCost);
|
---|
1271 |
|
---|
1272 | const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
|
---|
1273 | const float totalVol = GetViewSpaceBox().GetVolume();
|
---|
1274 | //const float totalVol = mViewCellsTree->GetRoot()->GetVolume();
|
---|
1275 |
|
---|
1276 | int j = 0;
|
---|
1277 | int i = 0;
|
---|
1278 |
|
---|
1279 | ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
1280 |
|
---|
1281 | // count for integral
|
---|
1282 | float volSum = 0;
|
---|
1283 | int smallerCostSum = 0;
|
---|
1284 |
|
---|
1285 | // note can skip computations for view cells already
|
---|
1286 | // evaluated and delete them from vector ...
|
---|
1287 | while (1)
|
---|
1288 | {
|
---|
1289 | // count for histogram value
|
---|
1290 | float volDif = 0;
|
---|
1291 | int smallerCostDif = 0;
|
---|
1292 |
|
---|
1293 | while ((i < (int)viewCells.size()) && (viewCells[i]->GetRenderCost() < currentRenderCost))
|
---|
1294 | {
|
---|
1295 | volSum += viewCells[i]->GetVolume();
|
---|
1296 | volDif += viewCells[i]->GetVolume();
|
---|
1297 |
|
---|
1298 | ++ i;
|
---|
1299 | ++ smallerCostSum;
|
---|
1300 | ++ smallerCostDif;
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | if ((i >= (int)viewCells.size()) || (currentRenderCost >= maxRenderCost))
|
---|
1304 | break;
|
---|
1305 |
|
---|
1306 | const float rcRatio = currentRenderCost / maxRenderCost;
|
---|
1307 | const float volRatioSum = volSum / totalVol;
|
---|
1308 | const float volRatioDif = volDif / totalVol;
|
---|
1309 |
|
---|
1310 | outstream << "#Pass\n" << j ++ << endl;
|
---|
1311 | outstream << "#RenderCostRatio\n" << rcRatio << endl;
|
---|
1312 | outstream << "#WeightedCost\n" << currentRenderCost / totalVol << endl;
|
---|
1313 | outstream << "#ViewCellsDif\n" << smallerCostDif << endl;
|
---|
1314 | outstream << "#ViewCellsSum\n" << smallerCostSum << endl;
|
---|
1315 | outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
|
---|
1316 | outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
|
---|
1317 |
|
---|
1318 | // increase current render cost
|
---|
1319 | currentRenderCost += stepSize;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | outstream.close();
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | void ViewCellsManager::EvalViewCellHistogramForPvsSize(const string filename,
|
---|
1326 | const int nViewCells)
|
---|
1327 | {
|
---|
1328 | std::ofstream outstream;
|
---|
1329 | outstream.open(filename.c_str());
|
---|
1330 |
|
---|
1331 | ViewCellContainer viewCells;
|
---|
1332 |
|
---|
1333 | #ifdef USE_BIT_PVS
|
---|
1334 | cout << "objects size: " << (int)ObjectPvsIterator::sObjects.size() << endl;
|
---|
1335 | cout << "pvs size: " << ObjectPvs::sPvsSize << endl;
|
---|
1336 | #endif
|
---|
1337 |
|
---|
1338 | // $$ JB hack - the collect best viewcells does not work?
|
---|
1339 | #if 0
|
---|
1340 | mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
|
---|
1341 | #else
|
---|
1342 | viewCells = mViewCells;
|
---|
1343 | #endif
|
---|
1344 |
|
---|
1345 | ViewCellContainer::iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
1346 |
|
---|
1347 | for (; it != it_end; ++it)
|
---|
1348 | {
|
---|
1349 | (*it)->UpdatePvsCost();
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | float maxPvs, maxVal, minVal;
|
---|
1353 |
|
---|
1354 | // sort by pvs size
|
---|
1355 | sort(viewCells.begin(), viewCells.end(), SmallerPvs);
|
---|
1356 |
|
---|
1357 | maxPvs = viewCells.back()->GetTrianglesInPvs();
|
---|
1358 | minVal = 0;
|
---|
1359 |
|
---|
1360 | // hack: normalize pvs size
|
---|
1361 | int histoMaxVal;
|
---|
1362 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
|
---|
1363 | maxVal = max((float)histoMaxVal, maxPvs);
|
---|
1364 |
|
---|
1365 | Debug << "histogram minpvssize: " << minVal << " maxpvssize: " << maxVal
|
---|
1366 | << " real maxpvs " << maxPvs << endl;
|
---|
1367 |
|
---|
1368 | int histoIntervals;
|
---|
1369 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
|
---|
1370 | const int intervals = min(histoIntervals, (int)viewCells.size());
|
---|
1371 |
|
---|
1372 | const float range = maxVal - minVal;
|
---|
1373 | int stepSize = (int)(range / intervals);
|
---|
1374 |
|
---|
1375 | // set step size to avoid endless loop
|
---|
1376 | if (!stepSize) stepSize = 1;
|
---|
1377 |
|
---|
1378 | Debug << "intervals " << histoIntervals << endl;
|
---|
1379 | Debug << "stepsize: " << stepSize << endl;
|
---|
1380 | cout << "intervals " << histoIntervals << endl;
|
---|
1381 | cout << "stepsize: " << stepSize << endl;
|
---|
1382 |
|
---|
1383 | const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
|
---|
1384 | const float totalVol = GetViewSpaceBox().GetVolume();
|
---|
1385 |
|
---|
1386 | float currentPvs = minVal;
|
---|
1387 |
|
---|
1388 | int i = 0;
|
---|
1389 | int j = 0;
|
---|
1390 | float volSum = 0;
|
---|
1391 | int smallerSum = 0;
|
---|
1392 |
|
---|
1393 | it = viewCells.begin();
|
---|
1394 |
|
---|
1395 | for (int j = 0; j < intervals; ++ j)
|
---|
1396 | {
|
---|
1397 | float volDif = 0;
|
---|
1398 | int smallerDif = 0;
|
---|
1399 |
|
---|
1400 | while ((i < (int)viewCells.size()) &&
|
---|
1401 | (viewCells[i]->GetTrianglesInPvs() < currentPvs))
|
---|
1402 | {
|
---|
1403 | volDif += viewCells[i]->GetVolume();
|
---|
1404 | volSum += viewCells[i]->GetVolume();
|
---|
1405 |
|
---|
1406 | ++ i;
|
---|
1407 | ++ smallerDif;
|
---|
1408 | ++ smallerSum;
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | const float volRatioDif = volDif / totalVol;
|
---|
1412 | const float volRatioSum = volSum / totalVol;
|
---|
1413 |
|
---|
1414 | outstream << "#Pass\n" << j << endl;
|
---|
1415 | outstream << "#Pvs\n" << currentPvs << endl;
|
---|
1416 | outstream << "#ViewCellsDif\n" << smallerDif << endl;
|
---|
1417 | outstream << "#ViewCellsSum\n" << smallerSum << endl;
|
---|
1418 | outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
|
---|
1419 | outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
|
---|
1420 |
|
---|
1421 | //-- increase current pvs size to define next interval
|
---|
1422 | currentPvs += stepSize;
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | outstream.close();
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 |
|
---|
1429 | void ViewCellsManager::EvalViewCellHistogramForPvsSize(const string filename,
|
---|
1430 | ViewCellContainer &viewCells)
|
---|
1431 | {
|
---|
1432 | std::ofstream outstream;
|
---|
1433 | outstream.open(filename.c_str());
|
---|
1434 |
|
---|
1435 | float maxPvs, maxVal, minVal;
|
---|
1436 |
|
---|
1437 | // sort by pvs size
|
---|
1438 | sort(viewCells.begin(), viewCells.end(), SmallerPvs);
|
---|
1439 |
|
---|
1440 | maxPvs = viewCells.back()->GetTrianglesInPvs();
|
---|
1441 | minVal = 0;
|
---|
1442 |
|
---|
1443 | // hack: normalize pvs size
|
---|
1444 | int histoMaxVal;
|
---|
1445 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
|
---|
1446 | maxVal = max((float)histoMaxVal, maxPvs);
|
---|
1447 |
|
---|
1448 | Debug << "histogram minpvssize: " << minVal << " maxpvssize: " << maxVal
|
---|
1449 | << " real maxpvs " << maxPvs << endl;
|
---|
1450 |
|
---|
1451 | int histoIntervals;
|
---|
1452 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
|
---|
1453 | const int intervals = min(histoIntervals, (int)viewCells.size());
|
---|
1454 |
|
---|
1455 | const float range = maxVal - minVal;
|
---|
1456 | int stepSize = (int)(range / intervals);
|
---|
1457 |
|
---|
1458 | // set step size to avoid endless loop
|
---|
1459 | if (!stepSize) stepSize = 1;
|
---|
1460 |
|
---|
1461 | Debug << "intervals " << histoIntervals << endl;
|
---|
1462 | Debug << "stepsize: " << stepSize << endl;
|
---|
1463 | cout << "intervals " << histoIntervals << endl;
|
---|
1464 | cout << "stepsize: " << stepSize << endl;
|
---|
1465 |
|
---|
1466 | const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
|
---|
1467 | const float totalVol = GetViewSpaceBox().GetVolume();
|
---|
1468 |
|
---|
1469 | float currentPvs = minVal;
|
---|
1470 |
|
---|
1471 | int i = 0;
|
---|
1472 | int j = 0;
|
---|
1473 | float volSum = 0;
|
---|
1474 | int smallerSum = 0;
|
---|
1475 |
|
---|
1476 | //ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
1477 |
|
---|
1478 | for (int j = 0; j < intervals; ++ j)
|
---|
1479 | {
|
---|
1480 | float volDif = 0;
|
---|
1481 | int smallerDif = 0;
|
---|
1482 |
|
---|
1483 | while ((i < (int)viewCells.size()) &&
|
---|
1484 | (viewCells[i]->GetTrianglesInPvs() < currentPvs))
|
---|
1485 | {
|
---|
1486 | volDif += viewCells[i]->GetVolume();
|
---|
1487 | volSum += viewCells[i]->GetVolume();
|
---|
1488 |
|
---|
1489 | ++ i;
|
---|
1490 | ++ smallerDif;
|
---|
1491 | ++ smallerSum;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | // if (0 && (i < (int)viewCells.size()))
|
---|
1495 | // Debug << "new pvs cost increase: " << mViewCellsTree->GetTrianglesInPvs(viewCells[i])
|
---|
1496 | // << " " << currentPvs << endl;
|
---|
1497 |
|
---|
1498 | const float volRatioDif = volDif / totalVol;
|
---|
1499 | const float volRatioSum = volSum / totalVol;
|
---|
1500 |
|
---|
1501 | outstream << "#Pass\n" << j << endl;
|
---|
1502 | outstream << "#Pvs\n" << currentPvs << endl;
|
---|
1503 | outstream << "#ViewCellsDif\n" << smallerDif << endl;
|
---|
1504 | outstream << "#ViewCellsSum\n" << smallerSum << endl;
|
---|
1505 | outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
|
---|
1506 | outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
|
---|
1507 |
|
---|
1508 | //-- increase current pvs size to define next interval
|
---|
1509 | currentPvs += stepSize;
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 | outstream.close();
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 |
|
---|
1516 | bool ViewCellsManager::GetExportPvs() const
|
---|
1517 | {
|
---|
1518 | return mExportPvs;
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 |
|
---|
1522 | void ViewCellsManager::ResetPvs()
|
---|
1523 | {
|
---|
1524 | if (ViewCellsTreeConstructed())
|
---|
1525 | {
|
---|
1526 | mViewCellsTree->ResetPvs();
|
---|
1527 | }
|
---|
1528 | else
|
---|
1529 | {
|
---|
1530 | cout << "view cells tree not constructed" << endl;
|
---|
1531 | }
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 |
|
---|
1535 | void ViewCellsManager::ExportStats(const string &fileName)
|
---|
1536 | {
|
---|
1537 | mViewCellsTree->ExportStats(fileName);
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 |
|
---|
1541 | void ViewCellsManager::EvalViewCellPartition()
|
---|
1542 | {
|
---|
1543 | int samplesPerPass;
|
---|
1544 | int numSamples;
|
---|
1545 | int castSamples = 0;
|
---|
1546 | char str[64];
|
---|
1547 | int oldSamples = 0;
|
---|
1548 |
|
---|
1549 | int samplesForStats;
|
---|
1550 |
|
---|
1551 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
|
---|
1552 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesForStats", samplesForStats);
|
---|
1553 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samples", numSamples);
|
---|
1554 |
|
---|
1555 | char statsPrefix[100];
|
---|
1556 | Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
|
---|
1557 |
|
---|
1558 | Debug << "view cell evaluation samples per pass: " << samplesPerPass << endl;
|
---|
1559 | Debug << "view cell evaluation samples: " << numSamples << endl;
|
---|
1560 | Debug << "view cell stats prefix: " << statsPrefix << endl;
|
---|
1561 |
|
---|
1562 | cout << "reseting pvs ... ";
|
---|
1563 |
|
---|
1564 | const bool startFromZero = true;
|
---|
1565 |
|
---|
1566 | // reset pvs and start over from zero
|
---|
1567 | if (startFromZero)
|
---|
1568 | {
|
---|
1569 | mViewCellsTree->ResetPvs();
|
---|
1570 | }
|
---|
1571 | else // start from current sampless
|
---|
1572 | {
|
---|
1573 | // statistics before casting more samples
|
---|
1574 | cout << "compute new statistics ... ";
|
---|
1575 | sprintf(str, "-%09d-eval.log", castSamples);
|
---|
1576 | string fName = string(statsPrefix) + string(str);
|
---|
1577 |
|
---|
1578 | mViewCellsTree->ExportStats(fName);
|
---|
1579 | cout << "finished" << endl;
|
---|
1580 | }
|
---|
1581 |
|
---|
1582 | cout << "finished" << endl;
|
---|
1583 | cout << "Evaluating view cell partition ... " << endl;
|
---|
1584 |
|
---|
1585 | while (castSamples < numSamples)
|
---|
1586 | {
|
---|
1587 | ///////////////
|
---|
1588 | //-- we have to use uniform sampling strategy for construction rays
|
---|
1589 |
|
---|
1590 | VssRayContainer evaluationSamples;
|
---|
1591 | const int samplingType = mEvaluationSamplingType;
|
---|
1592 |
|
---|
1593 | long startTime = GetTime();
|
---|
1594 |
|
---|
1595 | cout << "casting " << samplesPerPass << " samples ... ";
|
---|
1596 | Debug << "casting " << samplesPerPass << " samples ... ";
|
---|
1597 |
|
---|
1598 | CastPassSamples(samplesPerPass, mStrategies, evaluationSamples);
|
---|
1599 |
|
---|
1600 | castSamples += samplesPerPass;
|
---|
1601 |
|
---|
1602 | Real timeDiff = TimeDiff(startTime, GetTime());
|
---|
1603 |
|
---|
1604 | cout << "finished in " << timeDiff * 1e-3f << " secs" << endl;
|
---|
1605 | cout << "computing sample contributions of " << (int)evaluationSamples.size() << " samples ... ";
|
---|
1606 |
|
---|
1607 | Debug << "finished in " << timeDiff * 1e-3f << " secs" << endl;
|
---|
1608 | Debug << "computing sample contributions of " << (int)evaluationSamples.size() << " samples ... ";
|
---|
1609 |
|
---|
1610 | startTime = GetTime();
|
---|
1611 |
|
---|
1612 | ComputeSampleContributions(evaluationSamples, true, false);
|
---|
1613 |
|
---|
1614 | timeDiff = TimeDiff(startTime, GetTime());
|
---|
1615 | cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
1616 | Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
1617 |
|
---|
1618 | if ((castSamples >= samplesForStats + oldSamples) || (castSamples >= numSamples))
|
---|
1619 | {
|
---|
1620 | oldSamples += samplesForStats;
|
---|
1621 |
|
---|
1622 | ///////////
|
---|
1623 | //-- output stats
|
---|
1624 |
|
---|
1625 | sprintf(str, "-%09d-eval.log", castSamples);
|
---|
1626 | string fileName = string(statsPrefix) + string(str);
|
---|
1627 |
|
---|
1628 | ///////////////
|
---|
1629 | //-- propagate pvs or pvs size information
|
---|
1630 |
|
---|
1631 | startTime = GetTime();
|
---|
1632 | ObjectPvs pvs;
|
---|
1633 |
|
---|
1634 | cout << "updating pvs for evaluation ... " << endl;
|
---|
1635 |
|
---|
1636 | UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), pvs);
|
---|
1637 |
|
---|
1638 | timeDiff = TimeDiff(startTime, GetTime());
|
---|
1639 | cout << "finished updating the pvs in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
1640 | Debug << "pvs evaluated in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
1641 |
|
---|
1642 | startTime = GetTime();
|
---|
1643 | cout << "compute new statistics ... " << endl;
|
---|
1644 |
|
---|
1645 | ExportStats(fileName);
|
---|
1646 |
|
---|
1647 | timeDiff = TimeDiff(startTime, GetTime());
|
---|
1648 | cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
1649 | Debug << "statistis computed in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | disposeRays(evaluationSamples, NULL);
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 |
|
---|
1656 | ////////////
|
---|
1657 | //-- histogram
|
---|
1658 |
|
---|
1659 | const int numLeaves = mViewCellsTree->GetNumInitialViewCells(mViewCellsTree->GetRoot());
|
---|
1660 | bool useHisto;
|
---|
1661 | int histoStepSize;
|
---|
1662 |
|
---|
1663 | Environment::GetSingleton()->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
|
---|
1664 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.histoStepSize", histoStepSize);
|
---|
1665 |
|
---|
1666 | if (useHisto)
|
---|
1667 | {
|
---|
1668 | // evaluate view cells in a histogram
|
---|
1669 | char s[64];
|
---|
1670 |
|
---|
1671 | for (int pass = histoStepSize; pass <= numLeaves; pass += histoStepSize)
|
---|
1672 | {
|
---|
1673 | string filename;
|
---|
1674 |
|
---|
1675 | cout << "computing histogram for " << pass << " view cells" << endl;
|
---|
1676 | #if 0
|
---|
1677 | //-- evaluate histogram for render cost
|
---|
1678 | sprintf(s, "-%09d-histo.log", pass);
|
---|
1679 | filename = string(statsPrefix) + string(s);
|
---|
1680 |
|
---|
1681 | EvalViewCellHistogram(filename, pass);
|
---|
1682 |
|
---|
1683 | #endif
|
---|
1684 | //////////////////////////////////////////
|
---|
1685 | //-- evaluate histogram for pvs size
|
---|
1686 |
|
---|
1687 | cout << "computing pvs histogram for " << pass << " view cells" << endl;
|
---|
1688 |
|
---|
1689 | sprintf(s, "-%09d-histo-pvs.log", pass);
|
---|
1690 | filename = string(statsPrefix) + string(s);
|
---|
1691 |
|
---|
1692 | EvalViewCellHistogram(filename, pass);
|
---|
1693 | // EvalViewCellHistogramForPvsSize(filename, pass);
|
---|
1694 | }
|
---|
1695 | }
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 |
|
---|
1699 | inline float EvalMergeCost(ViewCell *root, ViewCell *candidate)
|
---|
1700 | {
|
---|
1701 | return root->GetPvs().GetPvsHomogenity(candidate->GetPvs());
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 |
|
---|
1705 | /// Returns index of the best view cells of the neighborhood
|
---|
1706 | static int GetBestViewCellIdx(ViewCell *root, const ViewCellContainer &neighborhood)
|
---|
1707 | {
|
---|
1708 | int bestViewCellIdx = 0;
|
---|
1709 |
|
---|
1710 | float mergeCost = Limits::Infinity;
|
---|
1711 | int i = 0;
|
---|
1712 |
|
---|
1713 | ViewCellContainer::const_iterator vit, vit_end = neighborhood.end();
|
---|
1714 |
|
---|
1715 | for (vit = neighborhood.begin(); vit != vit_end; ++ vit, ++ i)
|
---|
1716 | {
|
---|
1717 | const float mc = EvalMergeCost(root, *vit);
|
---|
1718 |
|
---|
1719 | if (mc < mergeCost)
|
---|
1720 | {
|
---|
1721 | mergeCost = mc;
|
---|
1722 | bestViewCellIdx = i;
|
---|
1723 | }
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 | return bestViewCellIdx;
|
---|
1727 | }
|
---|
1728 |
|
---|
1729 |
|
---|
1730 | void ViewCellsManager::SetMaxFilterSize(const int size)
|
---|
1731 | {
|
---|
1732 | mMaxFilterSize = size;
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 |
|
---|
1736 | float ViewCellsManager::ComputeRenderCost(const int tri, const int obj) //const
|
---|
1737 | {
|
---|
1738 | return max((float)tri * mTriangleWeight, (float)obj * mObjectWeight);
|
---|
1739 | }
|
---|
1740 |
|
---|
1741 |
|
---|
1742 | ViewCell *ViewCellsManager::ConstructLocalMergeTree(ViewCell *currentViewCell,
|
---|
1743 | const ViewCellContainer &viewCells)
|
---|
1744 | {
|
---|
1745 | ViewCell *root = currentViewCell;
|
---|
1746 | ViewCellContainer neighborhood = viewCells;
|
---|
1747 |
|
---|
1748 | ViewCellContainer::const_iterator it, it_end = neighborhood.end();
|
---|
1749 |
|
---|
1750 | const int n = min(mMaxFilterSize, (int)neighborhood.size());
|
---|
1751 |
|
---|
1752 | /////////////////
|
---|
1753 | //-- use priority queue to merge leaf pairs
|
---|
1754 |
|
---|
1755 | for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
|
---|
1756 | {
|
---|
1757 | const int bestViewCellIdx = GetBestViewCellIdx(root, neighborhood);
|
---|
1758 |
|
---|
1759 | ViewCell *bestViewCell = neighborhood[bestViewCellIdx];
|
---|
1760 |
|
---|
1761 | // remove from vector
|
---|
1762 | swap(neighborhood[bestViewCellIdx], neighborhood.back());
|
---|
1763 | neighborhood.pop_back();
|
---|
1764 |
|
---|
1765 | if (!bestViewCell || !root)
|
---|
1766 | cout << "warning!!" << endl;
|
---|
1767 |
|
---|
1768 | // create new root of the hierarchy
|
---|
1769 | root = MergeViewCells(root, bestViewCell);
|
---|
1770 | }
|
---|
1771 |
|
---|
1772 | return root;
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 |
|
---|
1776 | struct SortableViewCellEntry {
|
---|
1777 |
|
---|
1778 | SortableViewCellEntry() {}
|
---|
1779 | SortableViewCellEntry(const float v, ViewCell *cell):mValue(v), mViewCell(cell) {}
|
---|
1780 |
|
---|
1781 | float mValue;
|
---|
1782 | ViewCell *mViewCell;
|
---|
1783 |
|
---|
1784 | friend bool operator<(const SortableViewCellEntry &a, const SortableViewCellEntry &b) {
|
---|
1785 | return a.mValue < b.mValue;
|
---|
1786 | }
|
---|
1787 | };
|
---|
1788 |
|
---|
1789 |
|
---|
1790 | ViewCell * ViewCellsManager::ConstructLocalMergeTree2(ViewCell *currentViewCell,
|
---|
1791 | const ViewCellContainer &viewCells)
|
---|
1792 | {
|
---|
1793 |
|
---|
1794 | vector<SortableViewCellEntry> neighborhood(viewCells.size());
|
---|
1795 | int i, j;
|
---|
1796 | for (i = 0, j = 0; i < viewCells.size(); i++) {
|
---|
1797 | if (viewCells[i] != currentViewCell)
|
---|
1798 | neighborhood[j++] = SortableViewCellEntry(
|
---|
1799 | EvalMergeCost(currentViewCell, viewCells[i]),
|
---|
1800 | viewCells[i]);
|
---|
1801 | }
|
---|
1802 | neighborhood.resize(j);
|
---|
1803 |
|
---|
1804 | sort(neighborhood.begin(), neighborhood.end());
|
---|
1805 |
|
---|
1806 | ViewCell *root = currentViewCell;
|
---|
1807 |
|
---|
1808 | vector<SortableViewCellEntry>::const_iterator it, it_end = neighborhood.end();
|
---|
1809 |
|
---|
1810 | const int n = min(mMaxFilterSize, (int)neighborhood.size());
|
---|
1811 | //-- use priority queue to merge leaf pairs
|
---|
1812 |
|
---|
1813 | //cout << "neighborhood: " << neighborhood.size() << endl;
|
---|
1814 | for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
|
---|
1815 | {
|
---|
1816 | ViewCell *bestViewCell = neighborhood[nMergedViewCells].mViewCell;
|
---|
1817 | //cout <<nMergedViewCells<<":"<<"homogenity=" <<neighborhood[nMergedViewCells].mValue<<endl;
|
---|
1818 | // create new root of the hierarchy
|
---|
1819 | root = MergeViewCells(root, bestViewCell);
|
---|
1820 | // set negative cost so that this view cell gets deleted
|
---|
1821 | root->SetMergeCost(-1.0f);
|
---|
1822 | }
|
---|
1823 |
|
---|
1824 | return root;
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | void
|
---|
1828 | ViewCellsManager::DeleteLocalMergeTree(ViewCell *vc
|
---|
1829 | ) const
|
---|
1830 | {
|
---|
1831 | if (!vc->IsLeaf() && vc->GetMergeCost() < 0.0f)
|
---|
1832 | {
|
---|
1833 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
1834 | ViewCellContainer::const_iterator it, it_end = vci->mChildren.end();
|
---|
1835 |
|
---|
1836 | for (it = vci->mChildren.begin(); it != it_end; ++ it)
|
---|
1837 | DeleteLocalMergeTree(*it);
|
---|
1838 |
|
---|
1839 | vci->mChildren.clear();
|
---|
1840 |
|
---|
1841 | delete vci;
|
---|
1842 | }
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 |
|
---|
1846 | bool ViewCellsManager::CheckValidity(ViewCell *vc,
|
---|
1847 | int minPvsSize,
|
---|
1848 | int maxPvsSize) const
|
---|
1849 | {
|
---|
1850 |
|
---|
1851 | const float pvsCost = vc->GetPvs().EvalPvsCost();
|
---|
1852 |
|
---|
1853 | if ((pvsCost > maxPvsSize) || (pvsCost < minPvsSize))
|
---|
1854 | {
|
---|
1855 | cout << "err: " << pvsCost << " " << minPvsSize << " " << maxPvsSize << endl;
|
---|
1856 | return false;
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | return true;
|
---|
1860 | }
|
---|
1861 |
|
---|
1862 |
|
---|
1863 | int ViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
1864 | ViewCellContainer &viewCells) const
|
---|
1865 | {
|
---|
1866 | return 0;
|
---|
1867 | };
|
---|
1868 |
|
---|
1869 |
|
---|
1870 | AxisAlignedBox3 ViewCellsManager::GetFilterBBox(const Vector3 &viewPoint,
|
---|
1871 | const float width) const
|
---|
1872 | {
|
---|
1873 | float w = Magnitude(mViewSpaceBox.Size())*width;
|
---|
1874 | Vector3 min = viewPoint - w * 0.5f;
|
---|
1875 | Vector3 max = viewPoint + w * 0.5f;
|
---|
1876 |
|
---|
1877 | return AxisAlignedBox3(min, max);
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 |
|
---|
1881 | void ViewCellsManager::GetPrVS(const Vector3 &viewPoint,
|
---|
1882 | PrVs &prvs,
|
---|
1883 | const float filterWidth)
|
---|
1884 | {
|
---|
1885 | ViewCell *currentViewCell = GetViewCell(viewPoint);
|
---|
1886 |
|
---|
1887 | if (mMaxFilterSize < 1) {
|
---|
1888 | prvs.mViewCell = currentViewCell;
|
---|
1889 | return;
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | const AxisAlignedBox3 box = GetFilterBBox(viewPoint, filterWidth);
|
---|
1893 |
|
---|
1894 | if (currentViewCell)
|
---|
1895 | {
|
---|
1896 | ViewCellContainer viewCells;
|
---|
1897 | ComputeBoxIntersections(box, viewCells);
|
---|
1898 |
|
---|
1899 | ViewCell *root = ConstructLocalMergeTree2(currentViewCell, viewCells);
|
---|
1900 | prvs.mViewCell = root;
|
---|
1901 |
|
---|
1902 | }
|
---|
1903 | else
|
---|
1904 | {
|
---|
1905 | prvs.mViewCell = NULL;
|
---|
1906 | //prvs.mPvs = root->GetPvs();
|
---|
1907 | }
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 |
|
---|
1911 | bool ViewCellsManager::ViewCellsTreeConstructed() const
|
---|
1912 | {
|
---|
1913 | return (mViewCellsTree && mViewCellsTree->GetRoot());
|
---|
1914 | }
|
---|
1915 |
|
---|
1916 |
|
---|
1917 | void ViewCellsManager::SetValidity(ViewCell *vc,
|
---|
1918 | int minPvs,
|
---|
1919 | int maxPvs) const
|
---|
1920 | {
|
---|
1921 | vc->SetValid(CheckValidity(vc, minPvs, maxPvs));
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 |
|
---|
1925 | void
|
---|
1926 | ViewCellsManager::SetValidity(
|
---|
1927 | int minPvsSize,
|
---|
1928 | int maxPvsSize) const
|
---|
1929 | {
|
---|
1930 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
1931 |
|
---|
1932 |
|
---|
1933 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
1934 | {
|
---|
1935 | SetValidity(*it, minPvsSize, maxPvsSize);
|
---|
1936 | }
|
---|
1937 | }
|
---|
1938 |
|
---|
1939 | void
|
---|
1940 | ViewCellsManager::SetValidityPercentage(
|
---|
1941 | const float minValid,
|
---|
1942 | const float maxValid
|
---|
1943 | )
|
---|
1944 | {
|
---|
1945 | ObjectPvs dummyPvs;
|
---|
1946 | // update pvs sizes
|
---|
1947 | for (int i = 0; i < (int)mViewCells.size(); ++ i) {
|
---|
1948 | UpdatePvsForEvaluation(mViewCells[i], dummyPvs);
|
---|
1949 | }
|
---|
1950 |
|
---|
1951 | sort(mViewCells.begin(), mViewCells.end(), SmallerPvs);
|
---|
1952 |
|
---|
1953 | int start = (int)(mViewCells.size() * minValid);
|
---|
1954 | int end = (int)(mViewCells.size() * maxValid);
|
---|
1955 |
|
---|
1956 | for (int i = 0; i < (int)mViewCells.size(); ++ i)
|
---|
1957 | {
|
---|
1958 | // cout<<"pvs:"<<mViewCells[i]->GetCachedPvsCost()<<endl;
|
---|
1959 | mViewCells[i]->SetValid(i >= start && i <= end);
|
---|
1960 | }
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 |
|
---|
1964 | int ViewCellsManager::CountValidViewcells() const
|
---|
1965 | {
|
---|
1966 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
1967 | int valid = 0;
|
---|
1968 |
|
---|
1969 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
1970 | {
|
---|
1971 | if ((*it)->GetValid())
|
---|
1972 | ++ valid;
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 | return valid;
|
---|
1976 | }
|
---|
1977 |
|
---|
1978 |
|
---|
1979 | bool ViewCellsManager::LoadViewCellsGeometry(const string filename,
|
---|
1980 | const bool extrudeBaseTriangles)
|
---|
1981 | {
|
---|
1982 | /// we use predefined view cells from now on
|
---|
1983 | mUsePredefinedViewCells = true;
|
---|
1984 | X3dParser parser;
|
---|
1985 |
|
---|
1986 | if (extrudeBaseTriangles)
|
---|
1987 | {
|
---|
1988 | Environment::GetSingleton()->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
|
---|
1989 | const bool success = parser.ParseFile(filename, *this);
|
---|
1990 |
|
---|
1991 | if (!success)
|
---|
1992 | return false;
|
---|
1993 | }
|
---|
1994 | else
|
---|
1995 | {
|
---|
1996 | // hack: use standard mesh loading
|
---|
1997 | // create temporary scene graph for loading the view cells geometry
|
---|
1998 | // note: delete the meshes as they are created two times for transformed mesh instances.
|
---|
1999 | SceneGraphNode *root = new SceneGraphNode();
|
---|
2000 | const bool success = parser.ParseFile(filename, root, true);
|
---|
2001 |
|
---|
2002 | if (!success)
|
---|
2003 | {
|
---|
2004 | DEL_PTR(root);
|
---|
2005 | return false;
|
---|
2006 | }
|
---|
2007 |
|
---|
2008 | ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end();
|
---|
2009 |
|
---|
2010 | for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit)
|
---|
2011 | {
|
---|
2012 | Mesh *mesh;
|
---|
2013 | if ((*oit)->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
|
---|
2014 | {
|
---|
2015 | TransformedMeshInstance *mit = static_cast<TransformedMeshInstance *>(*oit);
|
---|
2016 | mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
2017 | mit->GetTransformedMesh(*mesh);
|
---|
2018 | }
|
---|
2019 | else if ((*oit)->Type() == Intersectable::MESH_INSTANCE)
|
---|
2020 | {
|
---|
2021 | MeshInstance *mit = static_cast<MeshInstance *>(*oit);
|
---|
2022 | mesh = mit->GetMesh();
|
---|
2023 | }
|
---|
2024 | mesh->ComputeBoundingBox();
|
---|
2025 | mViewCells.push_back(GenerateViewCell(mesh));
|
---|
2026 | }
|
---|
2027 |
|
---|
2028 | DEL_PTR(root);
|
---|
2029 | }
|
---|
2030 |
|
---|
2031 | // set view space box to bounding box of the view cells
|
---|
2032 | AxisAlignedBox3 bbox;
|
---|
2033 | bbox.Initialize();
|
---|
2034 | ViewCellContainer::iterator it = mViewCells.begin(), it_end = mViewCells.end();
|
---|
2035 |
|
---|
2036 | for (; it != it_end; ++ it)
|
---|
2037 | {
|
---|
2038 | bbox.Include((*it)->GetMesh()->mBox);
|
---|
2039 | }
|
---|
2040 |
|
---|
2041 | SetViewSpaceBox(bbox);
|
---|
2042 | cout << "view space box: " << bbox << endl;
|
---|
2043 | cout << "generated " << (int)mViewCells.size() << " view cells using the geometry " << filename << endl;
|
---|
2044 |
|
---|
2045 | return true;
|
---|
2046 | }
|
---|
2047 |
|
---|
2048 |
|
---|
2049 | bool ViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
|
---|
2050 | {
|
---|
2051 | viewPoint = mViewSpaceBox.GetRandomPoint();
|
---|
2052 | return true;
|
---|
2053 | }
|
---|
2054 |
|
---|
2055 | bool ViewCellsManager::GetViewPoint(Vector3 &viewPoint, const Vector3 ¶ms) const
|
---|
2056 | {
|
---|
2057 | viewPoint = mViewSpaceBox.GetRandomPoint(params);
|
---|
2058 | return true;
|
---|
2059 | }
|
---|
2060 |
|
---|
2061 |
|
---|
2062 | float ViewCellsManager::GetViewSpaceVolume()
|
---|
2063 | {
|
---|
2064 | return mViewSpaceBox.GetVolume() * (2.0f * sqr((float)M_PI));
|
---|
2065 | }
|
---|
2066 |
|
---|
2067 |
|
---|
2068 | bool ViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
|
---|
2069 | {
|
---|
2070 | if (!ViewCellsConstructed())
|
---|
2071 | {
|
---|
2072 | return mViewSpaceBox.IsInside(viewPoint);
|
---|
2073 | }
|
---|
2074 | else
|
---|
2075 | {
|
---|
2076 | if (!mViewSpaceBox.IsInside(viewPoint))
|
---|
2077 | return false;
|
---|
2078 |
|
---|
2079 | ViewCell *viewcell = GetViewCell(viewPoint);
|
---|
2080 |
|
---|
2081 | if (!viewcell || !viewcell->GetValid())
|
---|
2082 | return false;
|
---|
2083 | }
|
---|
2084 |
|
---|
2085 | return true;
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 |
|
---|
2089 | float
|
---|
2090 | ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays,
|
---|
2091 | const bool addContributions,
|
---|
2092 | const bool storeViewCells,
|
---|
2093 | const bool useHitObjects)
|
---|
2094 | {
|
---|
2095 | float sum = 0.0f;
|
---|
2096 |
|
---|
2097 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
2098 |
|
---|
2099 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
2100 | {
|
---|
2101 | if (!ViewCellsConstructed())
|
---|
2102 | {
|
---|
2103 | // view cells not yet constructed
|
---|
2104 | // just take the lenghts of the rays as contributions
|
---|
2105 | if ((*it)->mTerminationObject)
|
---|
2106 | {
|
---|
2107 | sum += (*it)->Length();
|
---|
2108 | }
|
---|
2109 | }
|
---|
2110 | else
|
---|
2111 | {
|
---|
2112 | sum += ComputeSampleContribution(*(*it), addContributions, storeViewCells, useHitObjects);
|
---|
2113 | }
|
---|
2114 | }
|
---|
2115 |
|
---|
2116 | cout << "view cell cast time: " << viewCellCastTimer.TotalTime() << " s" << endl;
|
---|
2117 | Debug << "view cell cast time: " << viewCellCastTimer.TotalTime() << " s" << endl;
|
---|
2118 |
|
---|
2119 | cout << "pvs time: " << pvsTimer.TotalTime() << " s" << endl;
|
---|
2120 | Debug << "pvs time: " << pvsTimer.TotalTime() << " s" << endl;
|
---|
2121 |
|
---|
2122 | return sum;
|
---|
2123 | }
|
---|
2124 |
|
---|
2125 |
|
---|
2126 | void ViewCellsManager::EvaluateViewCellsStats()
|
---|
2127 | {
|
---|
2128 | mCurrentViewCellsStats.Reset();
|
---|
2129 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
2130 |
|
---|
2131 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
2132 | {
|
---|
2133 | mViewCellsTree->UpdateViewCellsStats(*it, mCurrentViewCellsStats);
|
---|
2134 | }
|
---|
2135 | }
|
---|
2136 |
|
---|
2137 |
|
---|
2138 | void ViewCellsManager::EvaluateRenderStatistics(float &totalRenderCost,
|
---|
2139 | float &expectedRenderCost,
|
---|
2140 | float &deviation,
|
---|
2141 | float &variance,
|
---|
2142 | float &totalCost,
|
---|
2143 | float &avgRenderCost)
|
---|
2144 | {
|
---|
2145 | ////////////
|
---|
2146 | //-- compute expected value
|
---|
2147 |
|
---|
2148 | totalRenderCost = 0;
|
---|
2149 | totalCost = 0;
|
---|
2150 |
|
---|
2151 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
2152 |
|
---|
2153 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
2154 | {
|
---|
2155 | ViewCell *vc = *it;
|
---|
2156 | totalRenderCost += vc->GetPvs().EvalPvsCost() * vc->GetVolume();
|
---|
2157 | totalCost += (int)vc->GetPvs().EvalPvsCost();
|
---|
2158 | }
|
---|
2159 |
|
---|
2160 | // normalize with view space box
|
---|
2161 | totalRenderCost /= mViewSpaceBox.GetVolume();
|
---|
2162 | expectedRenderCost = totalRenderCost / (float)mViewCells.size();
|
---|
2163 | avgRenderCost = totalCost / (float)mViewCells.size();
|
---|
2164 |
|
---|
2165 |
|
---|
2166 | ///////////
|
---|
2167 | //-- compute standard defiation
|
---|
2168 |
|
---|
2169 | variance = 0;
|
---|
2170 | deviation = 0;
|
---|
2171 |
|
---|
2172 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
2173 | {
|
---|
2174 | ViewCell *vc = *it;
|
---|
2175 |
|
---|
2176 | float renderCost = vc->GetPvs().EvalPvsCost() * vc->GetVolume();
|
---|
2177 | float dev;
|
---|
2178 |
|
---|
2179 | if (1)
|
---|
2180 | dev = fabs(avgRenderCost - (float)vc->GetPvs().EvalPvsCost());
|
---|
2181 | else
|
---|
2182 | dev = fabs(expectedRenderCost - renderCost);
|
---|
2183 |
|
---|
2184 | deviation += dev;
|
---|
2185 | variance += dev * dev;
|
---|
2186 | }
|
---|
2187 |
|
---|
2188 | variance /= (float)mViewCells.size();
|
---|
2189 | deviation /= (float)mViewCells.size();
|
---|
2190 | }
|
---|
2191 |
|
---|
2192 |
|
---|
2193 | float ViewCellsManager::GetArea(ViewCell *viewCell) const
|
---|
2194 | {
|
---|
2195 | return viewCell->GetArea();
|
---|
2196 | }
|
---|
2197 |
|
---|
2198 |
|
---|
2199 | float ViewCellsManager::GetVolume(ViewCell *viewCell) const
|
---|
2200 | {
|
---|
2201 | return viewCell->GetVolume();
|
---|
2202 | }
|
---|
2203 |
|
---|
2204 |
|
---|
2205 | void ViewCellsManager::CompressViewCells()
|
---|
2206 | {
|
---|
2207 | if (!(ViewCellsTreeConstructed() && mCompressViewCells))
|
---|
2208 | return;
|
---|
2209 |
|
---|
2210 | ////////////
|
---|
2211 | //-- compression
|
---|
2212 |
|
---|
2213 | int pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
|
---|
2214 |
|
---|
2215 | cout << "number of entries before compress: " << pvsEntries << endl;
|
---|
2216 | Debug << "number of entries before compress: " << pvsEntries << endl;
|
---|
2217 |
|
---|
2218 | mViewCellsTree->SetViewCellsStorage(ViewCellsTree::COMPRESSED);
|
---|
2219 |
|
---|
2220 | pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
|
---|
2221 |
|
---|
2222 | Debug << "number of entries after compress: " << pvsEntries << endl;
|
---|
2223 | cout << "number of entries after compress: " << pvsEntries << endl;
|
---|
2224 | }
|
---|
2225 |
|
---|
2226 |
|
---|
2227 | ViewCell *ViewCellsManager::ExtrudeViewCell(const Triangle3 &baseTri,
|
---|
2228 | const float height) const
|
---|
2229 | {
|
---|
2230 | // one mesh per view cell
|
---|
2231 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
2232 | //ootl::hash_map<int, Intersectable *> hmap(-2, NULL);
|
---|
2233 | ////////////
|
---|
2234 | //-- construct prism
|
---|
2235 |
|
---|
2236 | // bottom
|
---|
2237 | mesh->mFaces.push_back(new Face(2,1,0));
|
---|
2238 | // top
|
---|
2239 | mesh->mFaces.push_back(new Face(3,4,5));
|
---|
2240 | // sides
|
---|
2241 | mesh->mFaces.push_back(new Face(1, 4, 3, 0));
|
---|
2242 | mesh->mFaces.push_back(new Face(2, 5, 4, 1));
|
---|
2243 | mesh->mFaces.push_back(new Face(3, 5, 2, 0));
|
---|
2244 |
|
---|
2245 |
|
---|
2246 | /////////////
|
---|
2247 | //-- extrude new vertices for top of prism
|
---|
2248 |
|
---|
2249 | const Vector3 triNorm = baseTri.GetNormal();
|
---|
2250 | Triangle3 topTri;
|
---|
2251 |
|
---|
2252 | // add base vertices and calculate top vertices
|
---|
2253 | for (int i = 0; i < 3; ++ i)
|
---|
2254 | {
|
---|
2255 | mesh->mVertices.push_back(baseTri.mVertices[i]);
|
---|
2256 | }
|
---|
2257 |
|
---|
2258 | // add top vertices
|
---|
2259 | for (int i = 0; i < 3; ++ i)
|
---|
2260 | {
|
---|
2261 | mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | // do we have to preprocess this mesh (we don't want to trace view cells!)?
|
---|
2265 | mesh->ComputeBoundingBox();
|
---|
2266 |
|
---|
2267 | return GenerateViewCell(mesh);
|
---|
2268 | }
|
---|
2269 |
|
---|
2270 |
|
---|
2271 | void ViewCellsManager::FinalizeViewCells(const bool createMesh)
|
---|
2272 | {
|
---|
2273 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
2274 |
|
---|
2275 | // volume and area of the view cells are recomputed
|
---|
2276 | // a view cell mesh is created
|
---|
2277 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
2278 | {
|
---|
2279 | Finalize(*it, createMesh);
|
---|
2280 | }
|
---|
2281 |
|
---|
2282 | mViewCellsTree->AssignRandomColors();
|
---|
2283 |
|
---|
2284 | mTotalAreaValid = false;
|
---|
2285 | }
|
---|
2286 |
|
---|
2287 |
|
---|
2288 | void ViewCellsManager::Finalize(ViewCell *viewCell, const bool createMesh)
|
---|
2289 | {
|
---|
2290 | // implemented in subclasses
|
---|
2291 | }
|
---|
2292 |
|
---|
2293 |
|
---|
2294 | /** fast way of merging 2 view cells.
|
---|
2295 | */
|
---|
2296 | ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCell *left, ViewCell *right) const
|
---|
2297 | {
|
---|
2298 | // generate parent view cell
|
---|
2299 | ViewCellInterior *vc = new ViewCellInterior();
|
---|
2300 |
|
---|
2301 | vc->GetPvs().Clear();
|
---|
2302 | ObjectPvs::Merge(vc->GetPvs(), left->GetPvs(), right->GetPvs());
|
---|
2303 |
|
---|
2304 | // set only links to child (not from child to parent, maybe not wished!!)
|
---|
2305 | vc->mChildren.push_back(left);
|
---|
2306 | vc->mChildren.push_back(right);
|
---|
2307 |
|
---|
2308 | // update pvs size
|
---|
2309 | UpdateScalarPvsSize(vc, vc->GetPvs().EvalPvsCost(), vc->GetPvs().GetSize());
|
---|
2310 |
|
---|
2311 | return vc;
|
---|
2312 | }
|
---|
2313 |
|
---|
2314 |
|
---|
2315 | ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCellContainer &children) const
|
---|
2316 | {
|
---|
2317 | ViewCellInterior *vc = new ViewCellInterior();
|
---|
2318 | ViewCellContainer::const_iterator it, it_end = children.end();
|
---|
2319 |
|
---|
2320 | for (it = children.begin(); it != it_end; ++ it)
|
---|
2321 | {
|
---|
2322 | vc->GetPvs().MergeInPlace((*it)->GetPvs());
|
---|
2323 |
|
---|
2324 | vc->mChildren.push_back(*it);
|
---|
2325 | }
|
---|
2326 |
|
---|
2327 | return vc;
|
---|
2328 | }
|
---|
2329 |
|
---|
2330 |
|
---|
2331 | void ViewCellsManager::SetRenderer(Renderer *renderer)
|
---|
2332 | {
|
---|
2333 | mRenderer = renderer;
|
---|
2334 | }
|
---|
2335 |
|
---|
2336 |
|
---|
2337 | ViewCellsTree *ViewCellsManager::GetViewCellsTree()
|
---|
2338 | {
|
---|
2339 | return mViewCellsTree;
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 |
|
---|
2343 | void ViewCellsManager::SetVisualizationSamples(const int visSamples)
|
---|
2344 | {
|
---|
2345 | mVisualizationSamples = visSamples;
|
---|
2346 | }
|
---|
2347 |
|
---|
2348 |
|
---|
2349 | void ViewCellsManager::SetConstructionSamples(const int constructionSamples)
|
---|
2350 | {
|
---|
2351 | mConstructionSamples = constructionSamples;
|
---|
2352 | }
|
---|
2353 |
|
---|
2354 |
|
---|
2355 | void ViewCellsManager::SetInitialSamples(const int initialSamples)
|
---|
2356 | {
|
---|
2357 | mInitialSamples = initialSamples;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 |
|
---|
2361 | void ViewCellsManager::SetPostProcessSamples(const int postProcessSamples)
|
---|
2362 | {
|
---|
2363 | mPostProcessSamples = postProcessSamples;
|
---|
2364 | }
|
---|
2365 |
|
---|
2366 |
|
---|
2367 | int ViewCellsManager::GetVisualizationSamples() const
|
---|
2368 | {
|
---|
2369 | return mVisualizationSamples;
|
---|
2370 | }
|
---|
2371 |
|
---|
2372 |
|
---|
2373 | int ViewCellsManager::GetConstructionSamples() const
|
---|
2374 | {
|
---|
2375 | return mConstructionSamples;
|
---|
2376 | }
|
---|
2377 |
|
---|
2378 |
|
---|
2379 | int ViewCellsManager::GetPostProcessSamples() const
|
---|
2380 | {
|
---|
2381 | return mPostProcessSamples;
|
---|
2382 | }
|
---|
2383 |
|
---|
2384 |
|
---|
2385 | void ViewCellsManager::UpdatePvs()
|
---|
2386 | {
|
---|
2387 | if (mViewCellPvsIsUpdated || !ViewCellsTreeConstructed())
|
---|
2388 | return;
|
---|
2389 |
|
---|
2390 | mViewCellPvsIsUpdated = true;
|
---|
2391 |
|
---|
2392 | ViewCellContainer leaves;
|
---|
2393 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
2394 |
|
---|
2395 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
2396 |
|
---|
2397 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2398 | {
|
---|
2399 | mViewCellsTree->PropagatePvs(*it);
|
---|
2400 | }
|
---|
2401 | }
|
---|
2402 |
|
---|
2403 |
|
---|
2404 | void ViewCellsManager::GetPvsStatistics(PvsStatistics &stat)
|
---|
2405 | {
|
---|
2406 | // update pvs of view cells tree if necessary
|
---|
2407 | if (0) UpdatePvs();
|
---|
2408 |
|
---|
2409 | ViewCellContainer::const_iterator it = mViewCells.begin();
|
---|
2410 |
|
---|
2411 | stat.viewcells = 0;
|
---|
2412 | stat.minPvs = 100000000;
|
---|
2413 | stat.maxPvs = 0;
|
---|
2414 | stat.avgPvs = 0.0f;
|
---|
2415 | stat.avgPvsEntries = 0.0f;
|
---|
2416 | stat.avgFilteredPvs = 0.0f;
|
---|
2417 | stat.avgFilteredPvsEntries = 0.0f;
|
---|
2418 | stat.avgFilterContribution = 0.0f;
|
---|
2419 | stat.avgFilterRadius = 0;
|
---|
2420 | stat.avgFilterRatio = 0;
|
---|
2421 | stat.avgRelPvsIncrease = 0.0f;
|
---|
2422 | stat.devRelPvsIncrease = 0.0f;
|
---|
2423 | stat.renderCost = 0.0f;
|
---|
2424 | stat.mem = 0.0f;
|
---|
2425 |
|
---|
2426 | if (mPerViewCellStat.size() != mViewCells.size()) {
|
---|
2427 | // reset the pvs size array after the first call to this routine
|
---|
2428 | mPerViewCellStat.resize(mViewCells.size());
|
---|
2429 | for (int i=0; i < mPerViewCellStat.size(); i++) {
|
---|
2430 | mPerViewCellStat[i].pvsSize = 0.0f;
|
---|
2431 | mPerViewCellStat[i].relPvsIncrease = 0.0f;
|
---|
2432 | }
|
---|
2433 | }
|
---|
2434 | int i;
|
---|
2435 | bool evaluateFilter;
|
---|
2436 | Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluateFilter", evaluateFilter);
|
---|
2437 |
|
---|
2438 | const float vol = mViewSpaceBox.GetVolume();
|
---|
2439 |
|
---|
2440 | for (i = 0; it != mViewCells.end(); ++ it, ++ i)
|
---|
2441 | {
|
---|
2442 | ViewCell *viewcell = *it;
|
---|
2443 | if (viewcell->GetValid()) {
|
---|
2444 | const float pvsCost = mViewCellsTree->GetTrianglesInPvs(viewcell);
|
---|
2445 | const float renderCost = pvsCost * viewcell->GetVolume() / vol;
|
---|
2446 |
|
---|
2447 | if (pvsCost < stat.minPvs)
|
---|
2448 | stat.minPvs = pvsCost;
|
---|
2449 | if (pvsCost > stat.maxPvs)
|
---|
2450 | stat.maxPvs = pvsCost;
|
---|
2451 |
|
---|
2452 | stat.avgPvs += pvsCost;
|
---|
2453 | stat.renderCost += renderCost;
|
---|
2454 |
|
---|
2455 | const float pvsEntries = (float)mViewCellsTree->GetPvsEntries(viewcell);
|
---|
2456 | stat.avgPvsEntries += pvsEntries;
|
---|
2457 |
|
---|
2458 | if (mPerViewCellStat[i].pvsSize > 0.0f)
|
---|
2459 | mPerViewCellStat[i].relPvsIncrease = (pvsCost - mPerViewCellStat[i].pvsSize) / mPerViewCellStat[i].pvsSize;
|
---|
2460 |
|
---|
2461 | stat.avgRelPvsIncrease += mPerViewCellStat[i].relPvsIncrease;
|
---|
2462 |
|
---|
2463 | // update the pvs size
|
---|
2464 | mPerViewCellStat[i].pvsSize = pvsCost;
|
---|
2465 |
|
---|
2466 |
|
---|
2467 | if (evaluateFilter)
|
---|
2468 | {
|
---|
2469 | ObjectPvs filteredPvs;
|
---|
2470 |
|
---|
2471 | PvsFilterStatistics fstat = ApplyFilter2(viewcell, false, mFilterWidth, filteredPvs);
|
---|
2472 |
|
---|
2473 | const float filteredCost = filteredPvs.EvalPvsCost();
|
---|
2474 |
|
---|
2475 | stat.avgFilteredPvs += filteredCost;
|
---|
2476 | stat.avgFilteredPvsEntries += filteredPvs.GetSize();
|
---|
2477 |
|
---|
2478 | stat.avgFilterContribution += filteredCost - pvsCost;
|
---|
2479 |
|
---|
2480 | stat.avgFilterRadius += fstat.mAvgFilterRadius;
|
---|
2481 | int sum = fstat.mGlobalFilterCount + fstat.mLocalFilterCount;
|
---|
2482 | if (sum) {
|
---|
2483 | stat.avgFilterRatio += fstat.mLocalFilterCount /
|
---|
2484 | (float) sum;
|
---|
2485 | }
|
---|
2486 |
|
---|
2487 | } else {
|
---|
2488 | stat.avgFilteredPvs += pvsCost;
|
---|
2489 | stat.avgFilterContribution += 0;
|
---|
2490 | }
|
---|
2491 |
|
---|
2492 | ++ stat.viewcells;
|
---|
2493 | }
|
---|
2494 | }
|
---|
2495 |
|
---|
2496 |
|
---|
2497 |
|
---|
2498 | if (stat.viewcells) {
|
---|
2499 | stat.mem = (float)(ObjectPvs::GetEntrySizeByte() * stat.avgPvsEntries + stat.viewcells * 16) / float(1024 * 1024);
|
---|
2500 |
|
---|
2501 | stat.avgPvs/=stat.viewcells;
|
---|
2502 | stat.avgPvsEntries/=stat.viewcells;
|
---|
2503 | stat.avgFilteredPvsEntries/=stat.viewcells;
|
---|
2504 | stat.avgFilteredPvs/=stat.viewcells;
|
---|
2505 | stat.avgFilterContribution/=stat.viewcells;
|
---|
2506 | stat.avgFilterRadius/=stat.viewcells;
|
---|
2507 | stat.avgFilterRatio/=stat.viewcells;
|
---|
2508 | stat.avgRelPvsIncrease/=stat.viewcells;
|
---|
2509 | stat.renderCostRatio=stat.renderCost / stat.mem;
|
---|
2510 |
|
---|
2511 | // evaluate std deviation of relPvsIncrease
|
---|
2512 | float sum=0.0f;
|
---|
2513 | for (i=0; i < stat.viewcells; i++) {
|
---|
2514 | sum += sqr(mPerViewCellStat[i].relPvsIncrease - stat.avgRelPvsIncrease);
|
---|
2515 | }
|
---|
2516 | stat.devRelPvsIncrease = sqrt(sum/stat.viewcells);
|
---|
2517 | }
|
---|
2518 |
|
---|
2519 | }
|
---|
2520 |
|
---|
2521 |
|
---|
2522 | void ViewCellsManager::PrintPvsStatistics(ostream &s)
|
---|
2523 | {
|
---|
2524 | s<<"############# Viewcell PVS STAT ##################\n";
|
---|
2525 | PvsStatistics pvsStat;
|
---|
2526 | GetPvsStatistics(pvsStat);
|
---|
2527 | s<<"#AVG_PVS\n"<<pvsStat.avgPvs<<endl;
|
---|
2528 | s<<"#AVG_ENTRIES_PVS\n"<<pvsStat.avgPvsEntries<<endl;
|
---|
2529 | s<<"#RENDERCOST\n"<<pvsStat.renderCost<<endl;
|
---|
2530 | s<<"#AVG_FILTERED_PVS\n"<<pvsStat.avgFilteredPvs<<endl;
|
---|
2531 | s<<"#AVG_FILTERED_ENTRIES_PVS\n"<<pvsStat.avgFilteredPvsEntries<<endl;
|
---|
2532 | s<<"#AVG_FILTER_CONTRIBUTION\n"<<pvsStat.avgFilterContribution<<endl;
|
---|
2533 | s<<"#AVG_FILTER_RADIUS\n"<<pvsStat.avgFilterRadius<<endl;
|
---|
2534 | s<<"#AVG_FILTER_RATIO\n"<<pvsStat.avgFilterRatio<<endl;
|
---|
2535 | s<<"#MAX_PVS\n"<<pvsStat.maxPvs<<endl;
|
---|
2536 | s<<"#MIN_PVS\n"<<pvsStat.minPvs<<endl;
|
---|
2537 | s<<"#AVG_REL_PVS_INCREASE\n"<<pvsStat.avgRelPvsIncrease<<endl;
|
---|
2538 | s<<"#DEV_REL_PVS_INCREASE\n"<<pvsStat.devRelPvsIncrease<<endl;
|
---|
2539 | s<<"#MEMORY\n"<<pvsStat.mem<<endl;
|
---|
2540 | s<<"#RATIO\n"<<pvsStat.renderCost / (pvsStat.mem + Limits::Small)<<endl;
|
---|
2541 | s<<"#CONTRIBUTING_RAYS\n"<<mSamplesStat.mContributingRays<<endl;
|
---|
2542 |
|
---|
2543 | if (mSamplesStat.mRays) {
|
---|
2544 | s<<"#AVG_VIEWCELLS_PER_RAY\n"<<mSamplesStat.mViewCells/(float)mSamplesStat.mRays<<endl;
|
---|
2545 | } else {
|
---|
2546 | s<<"#AVG_VIEWCELLS_PER_RAY\n 1 \n";
|
---|
2547 | }
|
---|
2548 | mSamplesStat.Reset();
|
---|
2549 | }
|
---|
2550 |
|
---|
2551 |
|
---|
2552 | int ViewCellsManager::CastBeam(Beam &beam)
|
---|
2553 | {
|
---|
2554 | return 0;
|
---|
2555 | }
|
---|
2556 |
|
---|
2557 |
|
---|
2558 | ViewCellContainer &ViewCellsManager::GetViewCells()
|
---|
2559 | {
|
---|
2560 | return mViewCells;
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 |
|
---|
2564 | void ViewCellsManager::SetViewSpaceBox(const AxisAlignedBox3 &box)
|
---|
2565 | {
|
---|
2566 | mViewSpaceBox = box;
|
---|
2567 |
|
---|
2568 | // hack: create clip plane relative to new view space box
|
---|
2569 | CreateClipPlane();
|
---|
2570 | // the total area of the view space has changed
|
---|
2571 | mTotalAreaValid = false;
|
---|
2572 | }
|
---|
2573 |
|
---|
2574 |
|
---|
2575 | void ViewCellsManager::CreateClipPlane()
|
---|
2576 | {
|
---|
2577 | int axis = 0;
|
---|
2578 | float pos;
|
---|
2579 | bool orientation;
|
---|
2580 | Vector3 absPos;
|
---|
2581 |
|
---|
2582 | Environment::GetSingleton()->GetFloatValue("ViewCells.Visualization.clipPlanePos", pos);
|
---|
2583 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.clipPlaneAxis", axis);
|
---|
2584 |
|
---|
2585 | if (axis < 0)
|
---|
2586 | {
|
---|
2587 | axis = -axis;
|
---|
2588 | orientation = false;
|
---|
2589 | absPos = mViewSpaceBox.Max() - mViewSpaceBox.Size() * pos;
|
---|
2590 | }
|
---|
2591 | else
|
---|
2592 | {
|
---|
2593 | orientation = true;
|
---|
2594 | absPos = mViewSpaceBox.Min() + mViewSpaceBox.Size() * pos;
|
---|
2595 | }
|
---|
2596 |
|
---|
2597 | mClipPlaneForViz = AxisAlignedPlane(axis, absPos[axis]);
|
---|
2598 | mClipPlaneForViz.mOrientation = orientation;
|
---|
2599 | }
|
---|
2600 |
|
---|
2601 |
|
---|
2602 | AxisAlignedBox3 ViewCellsManager::GetViewSpaceBox() const
|
---|
2603 | {
|
---|
2604 | return mViewSpaceBox;
|
---|
2605 | }
|
---|
2606 |
|
---|
2607 |
|
---|
2608 | void ViewCellsManager::ResetViewCells()
|
---|
2609 | {
|
---|
2610 | // recollect view cells
|
---|
2611 | mViewCells.clear();
|
---|
2612 | CollectViewCells();
|
---|
2613 |
|
---|
2614 | // stats are computed once more
|
---|
2615 | EvaluateViewCellsStats();
|
---|
2616 |
|
---|
2617 | // has to be recomputed
|
---|
2618 | mTotalAreaValid = false;
|
---|
2619 | }
|
---|
2620 |
|
---|
2621 |
|
---|
2622 | int ViewCellsManager::GetMaxPvsSize() const
|
---|
2623 | {
|
---|
2624 | return mMaxPvsSize;
|
---|
2625 | }
|
---|
2626 |
|
---|
2627 |
|
---|
2628 | int ViewCellsManager::GetMinPvsSize() const
|
---|
2629 | {
|
---|
2630 | return mMinPvsSize;
|
---|
2631 | }
|
---|
2632 |
|
---|
2633 |
|
---|
2634 |
|
---|
2635 | float ViewCellsManager::GetMaxPvsRatio() const
|
---|
2636 | {
|
---|
2637 | return mMaxPvsRatio;
|
---|
2638 | }
|
---|
2639 |
|
---|
2640 |
|
---|
2641 | inline static void AddSampleToPvs(ObjectPvs &pvs,
|
---|
2642 | Intersectable *obj,
|
---|
2643 | const float pdf)
|
---|
2644 | {
|
---|
2645 | #if PVS_ADD_DIRTY
|
---|
2646 | pvs.AddSampleDirtyCheck(obj, pdf);
|
---|
2647 |
|
---|
2648 | if (pvs.RequiresResort())
|
---|
2649 | {
|
---|
2650 | pvs.SimpleSort();
|
---|
2651 | }
|
---|
2652 | #else
|
---|
2653 | pvs.AddSample(obj, pdf);
|
---|
2654 | #endif
|
---|
2655 | }
|
---|
2656 |
|
---|
2657 |
|
---|
2658 | void ViewCellsManager::SortViewCellPvs()
|
---|
2659 | {
|
---|
2660 | ViewCellContainer::iterator it, it_end = mViewCells.end();
|
---|
2661 |
|
---|
2662 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
2663 | {
|
---|
2664 | ObjectPvs &pvs = (*it)->GetPvs();
|
---|
2665 | if (pvs.RequiresResortLog())
|
---|
2666 | pvs.SimpleSort();
|
---|
2667 | }
|
---|
2668 | }
|
---|
2669 |
|
---|
2670 |
|
---|
2671 | void ViewCellsManager::ComputeViewCellContribution(ViewCell *viewCell,
|
---|
2672 | VssRay &ray,
|
---|
2673 | Intersectable *obj,
|
---|
2674 | const Vector3 &pt,
|
---|
2675 | const bool addSamplesToPvs)
|
---|
2676 | {
|
---|
2677 | // check if we are outside of view space
|
---|
2678 | // $$JB tmp commented to speedup up computations
|
---|
2679 | #if 0
|
---|
2680 | if (!obj || !viewCell->GetValid())
|
---|
2681 | return;
|
---|
2682 | #endif
|
---|
2683 |
|
---|
2684 | // if ray not outside of view space
|
---|
2685 | float relContribution = 0.0f;
|
---|
2686 | float absContribution = 0.0f;
|
---|
2687 | bool hasAbsContribution;
|
---|
2688 |
|
---|
2689 | // todo: maybe not correct for kd node pvs
|
---|
2690 | if (addSamplesToPvs)
|
---|
2691 | {
|
---|
2692 | hasAbsContribution = viewCell->GetPvs().AddSampleDirtyCheck(obj, ray.mPdf);
|
---|
2693 | //hasAbsContribution = viewCell->GetPvs().AddSample(obj,ray.mPdf);
|
---|
2694 | }
|
---|
2695 | else
|
---|
2696 | {
|
---|
2697 | hasAbsContribution =
|
---|
2698 | viewCell->GetPvs().GetSampleContribution(obj, ray.mPdf, relContribution);
|
---|
2699 | }
|
---|
2700 |
|
---|
2701 | // $$ clear the relative contribution as it is currently not correct anyway
|
---|
2702 | // relContribution = 0.0f;
|
---|
2703 |
|
---|
2704 | if (hasAbsContribution)
|
---|
2705 | {
|
---|
2706 | ++ ray.mPvsContribution;
|
---|
2707 | absContribution = relContribution = 1.0f;
|
---|
2708 |
|
---|
2709 | if (viewCell->GetPvs().RequiresResort())
|
---|
2710 | viewCell->GetPvs().SimpleSort();
|
---|
2711 |
|
---|
2712 | #if CONTRIBUTION_RELATIVE_TO_PVS_SIZE
|
---|
2713 | relContribution /= viewcell->GetPvs().GetSize();
|
---|
2714 | #endif
|
---|
2715 |
|
---|
2716 | #if DIST_WEIGHTED_CONTRIBUTION
|
---|
2717 | // recalculate the contribution - weight the 1.0f contribution by the sqr distance to the
|
---|
2718 | // object-> a new contribution in the proximity of the viewcell has a larger weight!
|
---|
2719 | relContribution /=
|
---|
2720 | SqrDistance(GetViewCellBox(viewcell).Center(), ray.mTermination);
|
---|
2721 |
|
---|
2722 | #endif
|
---|
2723 | }
|
---|
2724 |
|
---|
2725 | #if SUM_RAY_CONTRIBUTIONS || AVG_RAY_CONTRIBUTIONS
|
---|
2726 | ray.mRelativePvsContribution += relContribution;
|
---|
2727 | #else
|
---|
2728 | // recalculate relative contribution - use max of Rel Contribution
|
---|
2729 | if (ray.mRelativePvsContribution < relContribution)
|
---|
2730 | ray.mRelativePvsContribution = relContribution;
|
---|
2731 | #endif
|
---|
2732 | }
|
---|
2733 |
|
---|
2734 |
|
---|
2735 | int ViewCellsManager::GetNumViewCells() const
|
---|
2736 | {
|
---|
2737 | return (int)mViewCells.size();
|
---|
2738 | }
|
---|
2739 |
|
---|
2740 |
|
---|
2741 | void
|
---|
2742 | ViewCellsManager::DeterminePvsObjects(
|
---|
2743 | VssRayContainer &rays,
|
---|
2744 | const bool useHitObjects)
|
---|
2745 | {
|
---|
2746 | if (!useHitObjects)
|
---|
2747 | {
|
---|
2748 | // store higher order object (e.g., bvh node) instead of object itself
|
---|
2749 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
2750 |
|
---|
2751 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
2752 | {
|
---|
2753 | VssRay *vssRay = *it;
|
---|
2754 |
|
---|
2755 | // set only the termination object
|
---|
2756 | vssRay->mTerminationObject = GetIntersectable(*vssRay, true);
|
---|
2757 | }
|
---|
2758 | }
|
---|
2759 | }
|
---|
2760 |
|
---|
2761 |
|
---|
2762 | float ViewCellsManager::ComputeSampleContribution(VssRay &ray,
|
---|
2763 | const bool addRays,
|
---|
2764 | ViewCell *currentViewCell,
|
---|
2765 | const bool useHitObjects)
|
---|
2766 | {
|
---|
2767 | ray.mPvsContribution = 0;
|
---|
2768 | ray.mRelativePvsContribution = 0.0f;
|
---|
2769 |
|
---|
2770 | if (!ray.mTerminationObject)
|
---|
2771 | return 0.0f;
|
---|
2772 |
|
---|
2773 | // optain pvs entry (can be different from hit object)
|
---|
2774 | Intersectable *terminationObj;
|
---|
2775 |
|
---|
2776 | terminationObj = ray.mTerminationObject;
|
---|
2777 |
|
---|
2778 | ComputeViewCellContribution(currentViewCell,
|
---|
2779 | ray,
|
---|
2780 | terminationObj,
|
---|
2781 | ray.mTermination,
|
---|
2782 | addRays);
|
---|
2783 |
|
---|
2784 | #if USE_RAY_LENGTH_AS_CONTRIBUTION
|
---|
2785 | float c = 0.0f;
|
---|
2786 | if (terminationObj)
|
---|
2787 | c = ray.Length();
|
---|
2788 |
|
---|
2789 | ray.mRelativePvsContribution = ray.mPvsContribution = c;
|
---|
2790 | return c;
|
---|
2791 | #else
|
---|
2792 | return ABS_CONTRIBUTION_WEIGHT*ray.mPvsContribution +
|
---|
2793 | (1.0f - ABS_CONTRIBUTION_WEIGHT)*ray.mRelativePvsContribution;
|
---|
2794 | #endif
|
---|
2795 | }
|
---|
2796 |
|
---|
2797 |
|
---|
2798 | float
|
---|
2799 | ViewCellsManager::ComputeSampleContribution(VssRay &ray,
|
---|
2800 | const bool addRays,
|
---|
2801 | const bool storeViewCells,
|
---|
2802 | const bool useHitObjects)
|
---|
2803 | {
|
---|
2804 | ray.mPvsContribution = 0;
|
---|
2805 | ray.mRelativePvsContribution = 0.0f;
|
---|
2806 |
|
---|
2807 | ++ mSamplesStat.mRays;
|
---|
2808 |
|
---|
2809 | if (!ray.mTerminationObject)
|
---|
2810 | return 0.0f;
|
---|
2811 |
|
---|
2812 | static Ray hray;
|
---|
2813 | hray.Init(ray);
|
---|
2814 |
|
---|
2815 | float tmin = 0, tmax = 1.0;
|
---|
2816 |
|
---|
2817 | if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
|
---|
2818 | {
|
---|
2819 | // cerr<<"ray outside view space box\n";
|
---|
2820 | return 0;
|
---|
2821 | }
|
---|
2822 |
|
---|
2823 | Vector3 origin = hray.Extrap(tmin);
|
---|
2824 | Vector3 termination = hray.Extrap(tmax);
|
---|
2825 |
|
---|
2826 | ViewCell::NewMail();
|
---|
2827 |
|
---|
2828 | viewCellCastTimer.Entry();
|
---|
2829 |
|
---|
2830 | static ViewCellContainer viewCells;
|
---|
2831 | static VssRay *lastVssRay = NULL;
|
---|
2832 |
|
---|
2833 | // check if last ray was not same ray with reverse direction
|
---|
2834 | if (1)
|
---|
2835 | // tmp matt: don't use that before origin objects are accounted for, currently the second ray is lost!!
|
---|
2836 | //!lastVssRay ||
|
---|
2837 | //!(ray.mOrigin == lastVssRay->mTermination) ||
|
---|
2838 | //!(ray.mTermination == lastVssRay->mOrigin))
|
---|
2839 | {
|
---|
2840 | viewCells.clear();
|
---|
2841 |
|
---|
2842 | // traverse the view space subdivision
|
---|
2843 | CastLineSegment(origin, termination, viewCells);
|
---|
2844 |
|
---|
2845 | lastVssRay = &ray;
|
---|
2846 | }
|
---|
2847 |
|
---|
2848 | viewCellCastTimer.Exit();
|
---|
2849 |
|
---|
2850 | mSamplesStat.mViewCells += (int)viewCells.size();
|
---|
2851 |
|
---|
2852 | if (storeViewCells)
|
---|
2853 | {
|
---|
2854 | // cerr << "Store viewcells should not be used in the test!" << endl;
|
---|
2855 | // copy viewcells memory efficiently
|
---|
2856 | #if VSS_STORE_VIEWCELLS
|
---|
2857 | ray.mViewCells.reserve(viewCells.size());
|
---|
2858 | ray.mViewCells = viewCells;
|
---|
2859 | #else
|
---|
2860 | cerr << "Vss store viewcells not supported." << endl;
|
---|
2861 | exit(1);
|
---|
2862 | #endif
|
---|
2863 | }
|
---|
2864 |
|
---|
2865 | Intersectable *terminationObj;
|
---|
2866 |
|
---|
2867 | objTimer.Entry();
|
---|
2868 |
|
---|
2869 | // obtain pvs entry (can be different from hit object)
|
---|
2870 | terminationObj = ray.mTerminationObject;
|
---|
2871 |
|
---|
2872 | objTimer.Exit();
|
---|
2873 |
|
---|
2874 | pvsTimer.Entry();
|
---|
2875 |
|
---|
2876 | ViewCellContainer::const_iterator it = viewCells.begin();
|
---|
2877 |
|
---|
2878 | for (; it != viewCells.end(); ++ it)
|
---|
2879 | {
|
---|
2880 | ComputeViewCellContribution(*it,
|
---|
2881 | ray,
|
---|
2882 | terminationObj,
|
---|
2883 | ray.mTermination,
|
---|
2884 | addRays);
|
---|
2885 | }
|
---|
2886 |
|
---|
2887 | pvsTimer.Exit();
|
---|
2888 |
|
---|
2889 | mSamplesStat.mPvsContributions += ray.mPvsContribution;
|
---|
2890 | if (ray.mPvsContribution)
|
---|
2891 | ++ mSamplesStat.mContributingRays;
|
---|
2892 |
|
---|
2893 | #if AVG_RAY_CONTRIBUTIONS
|
---|
2894 | ray.mRelativePvsContribution /= (float)viewCells.size();
|
---|
2895 | #endif
|
---|
2896 |
|
---|
2897 | #if USE_RAY_LENGTH_AS_CONTRIBUTION
|
---|
2898 | float c = 0.0f;
|
---|
2899 | if (terminationObj)
|
---|
2900 | c = ray.Length();
|
---|
2901 | ray.mRelativePvsContribution = ray.mPvsContribution = c;
|
---|
2902 | return c;
|
---|
2903 | #else
|
---|
2904 | return ABS_CONTRIBUTION_WEIGHT*ray.mPvsContribution +
|
---|
2905 | (1.0f - ABS_CONTRIBUTION_WEIGHT)*ray.mRelativePvsContribution;
|
---|
2906 | #endif
|
---|
2907 | }
|
---|
2908 |
|
---|
2909 |
|
---|
2910 | void ViewCellsManager::GetRaySets(const VssRayContainer &sourceRays,
|
---|
2911 | const int maxSize,
|
---|
2912 | VssRayContainer &usedRays,
|
---|
2913 | VssRayContainer *savedRays) const
|
---|
2914 | {
|
---|
2915 | const int limit = min(maxSize, (int)sourceRays.size());
|
---|
2916 | const float prop = (float)limit / ((float)sourceRays.size() + Limits::Small);
|
---|
2917 |
|
---|
2918 | VssRayContainer::const_iterator it, it_end = sourceRays.end();
|
---|
2919 | for (it = sourceRays.begin(); it != it_end; ++ it)
|
---|
2920 | {
|
---|
2921 | if (Random(1.0f) < prop)
|
---|
2922 | usedRays.push_back(*it);
|
---|
2923 | else if (savedRays)
|
---|
2924 | savedRays->push_back(*it);
|
---|
2925 | }
|
---|
2926 | }
|
---|
2927 |
|
---|
2928 |
|
---|
2929 | float ViewCellsManager::GetRendercost(ViewCell *viewCell) const
|
---|
2930 | {
|
---|
2931 | return (float)mViewCellsTree->GetTrianglesInPvs(viewCell);
|
---|
2932 | }
|
---|
2933 |
|
---|
2934 |
|
---|
2935 | float ViewCellsManager::GetAccVcArea()
|
---|
2936 | {
|
---|
2937 | // if already computed
|
---|
2938 | if (mTotalAreaValid)
|
---|
2939 | {
|
---|
2940 | return mTotalArea;
|
---|
2941 | }
|
---|
2942 |
|
---|
2943 | mTotalArea = 0;
|
---|
2944 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
2945 |
|
---|
2946 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
2947 | {
|
---|
2948 | //Debug << "area: " << GetArea(*it);
|
---|
2949 | mTotalArea += GetArea(*it);
|
---|
2950 | }
|
---|
2951 |
|
---|
2952 | mTotalAreaValid = true;
|
---|
2953 |
|
---|
2954 | return mTotalArea;
|
---|
2955 | }
|
---|
2956 |
|
---|
2957 |
|
---|
2958 | void ViewCellsManager::PrintStatistics(ostream &s) const
|
---|
2959 | {
|
---|
2960 | s << mCurrentViewCellsStats << endl;
|
---|
2961 | }
|
---|
2962 |
|
---|
2963 |
|
---|
2964 | void ViewCellsManager::CreateUniqueViewCellIds()
|
---|
2965 | {
|
---|
2966 | if (ViewCellsTreeConstructed())
|
---|
2967 | {
|
---|
2968 | mViewCellsTree->CreateUniqueViewCellsIds();
|
---|
2969 | }
|
---|
2970 | else // no view cells tree, handle view cells "myself"
|
---|
2971 | {
|
---|
2972 | int i = 0;
|
---|
2973 | ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
|
---|
2974 | for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
2975 | {
|
---|
2976 | if ((*vit)->GetId() != OUT_OF_BOUNDS_ID)
|
---|
2977 | {
|
---|
2978 | mViewCells[i]->SetId(i ++);
|
---|
2979 | }
|
---|
2980 | }
|
---|
2981 | }
|
---|
2982 | }
|
---|
2983 |
|
---|
2984 |
|
---|
2985 | void ViewCellsManager::ExportViewCellsForViz(Exporter *exporter,
|
---|
2986 | const AxisAlignedBox3 *sceneBox,
|
---|
2987 | const bool colorCode,
|
---|
2988 | const AxisAlignedPlane *clipPlane
|
---|
2989 | ) const
|
---|
2990 | {
|
---|
2991 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
2992 |
|
---|
2993 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
2994 | {
|
---|
2995 | if (!mOnlyValidViewCells || (*it)->GetValid())
|
---|
2996 | {
|
---|
2997 | ExportColor(exporter, *it, colorCode);
|
---|
2998 | ExportViewCellGeometry(exporter, *it, sceneBox, clipPlane);
|
---|
2999 | }
|
---|
3000 | }
|
---|
3001 | }
|
---|
3002 |
|
---|
3003 |
|
---|
3004 | void ViewCellsManager::CreateViewCellMeshes()
|
---|
3005 | {
|
---|
3006 | // convert to meshes
|
---|
3007 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
3008 |
|
---|
3009 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
3010 | {
|
---|
3011 | if (!(*it)->GetMesh())
|
---|
3012 | {
|
---|
3013 | CreateMesh(*it);
|
---|
3014 | }
|
---|
3015 | }
|
---|
3016 | }
|
---|
3017 |
|
---|
3018 |
|
---|
3019 | bool ViewCellsManager::ExportViewCells(const string filename,
|
---|
3020 | const bool exportPvs,
|
---|
3021 | const ObjectContainer &objects)
|
---|
3022 | {
|
---|
3023 | return false;
|
---|
3024 | }
|
---|
3025 |
|
---|
3026 |
|
---|
3027 | void ViewCellsManager::CollectViewCells(const int n)
|
---|
3028 | {
|
---|
3029 | mNumActiveViewCells = n;
|
---|
3030 | mViewCells.clear();
|
---|
3031 | // implemented in subclasses
|
---|
3032 | CollectViewCells();
|
---|
3033 | }
|
---|
3034 |
|
---|
3035 |
|
---|
3036 | void ViewCellsManager::SetViewCellActive(ViewCell *vc) const
|
---|
3037 | {
|
---|
3038 | ViewCellContainer leaves;
|
---|
3039 | // sets the pointers to the currently active view cells
|
---|
3040 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
3041 |
|
---|
3042 | ViewCellContainer::const_iterator lit, lit_end = leaves.end();
|
---|
3043 | for (lit = leaves.begin(); lit != lit_end; ++ lit)
|
---|
3044 | {
|
---|
3045 | static_cast<ViewCellLeaf *>(*lit)->SetActiveViewCell(vc);
|
---|
3046 | }
|
---|
3047 | }
|
---|
3048 |
|
---|
3049 |
|
---|
3050 | void ViewCellsManager::SetViewCellsActive()
|
---|
3051 | {
|
---|
3052 | // collect leaf view cells and set the pointers to
|
---|
3053 | // the currently active view cells
|
---|
3054 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
3055 |
|
---|
3056 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
3057 | {
|
---|
3058 | SetViewCellActive(*it);
|
---|
3059 | }
|
---|
3060 | }
|
---|
3061 |
|
---|
3062 |
|
---|
3063 | int ViewCellsManager::GetMaxFilterSize() const
|
---|
3064 | {
|
---|
3065 | return mMaxFilterSize;
|
---|
3066 | }
|
---|
3067 |
|
---|
3068 |
|
---|
3069 | static const bool USE_ASCII = true;
|
---|
3070 |
|
---|
3071 |
|
---|
3072 | bool ViewCellsManager::ExportBoundingBoxes(const string filename,
|
---|
3073 | const ObjectContainer &objects) const
|
---|
3074 | {
|
---|
3075 | ObjectContainer::const_iterator it, it_end = objects.end();
|
---|
3076 |
|
---|
3077 | if (USE_ASCII)
|
---|
3078 | {
|
---|
3079 | ofstream boxesOut(filename.c_str());
|
---|
3080 | if (!boxesOut.is_open())
|
---|
3081 | return false;
|
---|
3082 |
|
---|
3083 | for (it = objects.begin(); it != it_end; ++ it)
|
---|
3084 | {
|
---|
3085 | MeshInstance *mi = static_cast<MeshInstance *>(*it);
|
---|
3086 | const AxisAlignedBox3 box = mi->GetBox();
|
---|
3087 |
|
---|
3088 | boxesOut << mi->GetId() << " "
|
---|
3089 | << box.Min().x << " "
|
---|
3090 | << box.Min().y << " "
|
---|
3091 | << box.Min().z << " "
|
---|
3092 | << box.Max().x << " "
|
---|
3093 | << box.Max().y << " "
|
---|
3094 | << box.Max().z << endl;
|
---|
3095 | }
|
---|
3096 |
|
---|
3097 | boxesOut.close();
|
---|
3098 | }
|
---|
3099 | else
|
---|
3100 | {
|
---|
3101 | ofstream boxesOut(filename.c_str(), ios::binary);
|
---|
3102 |
|
---|
3103 | if (!boxesOut.is_open())
|
---|
3104 | return false;
|
---|
3105 |
|
---|
3106 | for (it = objects.begin(); it != it_end; ++ it)
|
---|
3107 | {
|
---|
3108 | MeshInstance *mi = static_cast<MeshInstance *>(*it);
|
---|
3109 | const AxisAlignedBox3 box = mi->GetBox();
|
---|
3110 |
|
---|
3111 | Vector3 bmin = box.Min();
|
---|
3112 | Vector3 bmax = box.Max();
|
---|
3113 |
|
---|
3114 | int id = mi->GetId();
|
---|
3115 |
|
---|
3116 | boxesOut.write(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
3117 | boxesOut.write(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
|
---|
3118 | boxesOut.write(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
|
---|
3119 | }
|
---|
3120 |
|
---|
3121 | boxesOut.close();
|
---|
3122 | }
|
---|
3123 |
|
---|
3124 | return true;
|
---|
3125 | }
|
---|
3126 |
|
---|
3127 |
|
---|
3128 | bool ViewCellsManager::LoadBoundingBoxes(const string filename,
|
---|
3129 | IndexedBoundingBoxContainer &boxes) const
|
---|
3130 | {
|
---|
3131 | Vector3 bmin, bmax;
|
---|
3132 | int id;
|
---|
3133 |
|
---|
3134 | if (USE_ASCII)
|
---|
3135 | {
|
---|
3136 | ifstream boxesIn(filename.c_str());
|
---|
3137 |
|
---|
3138 | if (!boxesIn.is_open())
|
---|
3139 | {
|
---|
3140 | cout << "failed to open file " << filename << endl;
|
---|
3141 | return false;
|
---|
3142 | }
|
---|
3143 |
|
---|
3144 | string buf;
|
---|
3145 | while (!(getline(boxesIn, buf)).eof())
|
---|
3146 | {
|
---|
3147 | sscanf(buf.c_str(), "%d %f %f %f %f %f %f",
|
---|
3148 | &id, &bmin.x, &bmin.y, &bmin.z,
|
---|
3149 | &bmax.x, &bmax.y, &bmax.z);
|
---|
3150 |
|
---|
3151 | AxisAlignedBox3 box(bmin, bmax);
|
---|
3152 | // MeshInstance *mi = new MeshInstance();
|
---|
3153 | // HACK: set bounding box to new box
|
---|
3154 | //mi->mBox = box;
|
---|
3155 |
|
---|
3156 | boxes.push_back(IndexedBoundingBox(id, box));
|
---|
3157 | }
|
---|
3158 |
|
---|
3159 | boxesIn.close();
|
---|
3160 | }
|
---|
3161 | else
|
---|
3162 | {
|
---|
3163 | ifstream boxesIn(filename.c_str(), ios::binary);
|
---|
3164 |
|
---|
3165 | if (!boxesIn.is_open())
|
---|
3166 | return false;
|
---|
3167 |
|
---|
3168 | while (1)
|
---|
3169 | {
|
---|
3170 | boxesIn.read(reinterpret_cast<char *>(&id), sizeof(Vector3));
|
---|
3171 | boxesIn.read(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
|
---|
3172 | boxesIn.read(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
|
---|
3173 |
|
---|
3174 | if (boxesIn.eof())
|
---|
3175 | break;
|
---|
3176 |
|
---|
3177 | AxisAlignedBox3 box(bmin, bmax);
|
---|
3178 | MeshInstance *mi = new MeshInstance(NULL);
|
---|
3179 |
|
---|
3180 | boxes.push_back(IndexedBoundingBox(id, box));
|
---|
3181 | }
|
---|
3182 |
|
---|
3183 | boxesIn.close();
|
---|
3184 | }
|
---|
3185 |
|
---|
3186 | return true;
|
---|
3187 | }
|
---|
3188 |
|
---|
3189 |
|
---|
3190 | float ViewCellsManager::GetFilterWidth()
|
---|
3191 | {
|
---|
3192 | return mFilterWidth;
|
---|
3193 | }
|
---|
3194 |
|
---|
3195 |
|
---|
3196 | float ViewCellsManager::GetAbsFilterWidth()
|
---|
3197 | {
|
---|
3198 | return Magnitude(mViewSpaceBox.Size()) * mFilterWidth;
|
---|
3199 | }
|
---|
3200 |
|
---|
3201 |
|
---|
3202 | void ViewCellsManager::UpdateScalarPvsSize(ViewCell *vc,
|
---|
3203 | const float pvsCost,
|
---|
3204 | const int entriesInPvs) const
|
---|
3205 | {
|
---|
3206 | vc->mPvsCost = pvsCost;
|
---|
3207 | vc->mEntriesInPvs = entriesInPvs;
|
---|
3208 |
|
---|
3209 | vc->mPvsSizeValid = true;
|
---|
3210 | }
|
---|
3211 |
|
---|
3212 |
|
---|
3213 | void
|
---|
3214 | ViewCellsManager::ApplyFilter(ViewCell *viewCell,
|
---|
3215 | KdTree *kdTree,
|
---|
3216 | const float viewSpaceFilterSize,
|
---|
3217 | const float spatialFilterSize,
|
---|
3218 | ObjectPvs &pvs
|
---|
3219 | )
|
---|
3220 | {
|
---|
3221 | // extend the pvs of the viewcell by pvs of its neighbors
|
---|
3222 | // and apply spatial filter by including all neighbors of the objects
|
---|
3223 | // in the pvs
|
---|
3224 |
|
---|
3225 | // get all viewcells intersecting the viewSpaceFilterBox
|
---|
3226 | // and compute the pvs union
|
---|
3227 |
|
---|
3228 | //Vector3 center = viewCell->GetBox().Center();
|
---|
3229 | // Vector3 center = m->mBox.Center();
|
---|
3230 |
|
---|
3231 | // AxisAlignedBox3 box(center - Vector3(viewSpaceFilterSize/2),
|
---|
3232 | // center + Vector3(viewSpaceFilterSize/2));
|
---|
3233 | if (!ViewCellsConstructed())
|
---|
3234 | return;
|
---|
3235 |
|
---|
3236 | if (viewSpaceFilterSize >= 0.0f)
|
---|
3237 | {
|
---|
3238 | const bool usePrVS = false;
|
---|
3239 |
|
---|
3240 | if (!usePrVS)
|
---|
3241 | {
|
---|
3242 | AxisAlignedBox3 box = GetViewCellBox(viewCell);
|
---|
3243 | box.Enlarge(Vector3(viewSpaceFilterSize/2));
|
---|
3244 |
|
---|
3245 | ViewCellContainer viewCells;
|
---|
3246 | ComputeBoxIntersections(box, viewCells);
|
---|
3247 |
|
---|
3248 | // cout<<"box="<<box<<endl;
|
---|
3249 | ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
3250 |
|
---|
3251 | for (; it != it_end; ++ it)
|
---|
3252 | {
|
---|
3253 | ObjectPvs interPvs;
|
---|
3254 | //cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
|
---|
3255 | ObjectPvs::Merge(interPvs, pvs, (*it)->GetPvs());
|
---|
3256 |
|
---|
3257 | pvs = interPvs;
|
---|
3258 | }
|
---|
3259 | }
|
---|
3260 | else
|
---|
3261 | {
|
---|
3262 | PrVs prvs;
|
---|
3263 | AxisAlignedBox3 box = GetViewCellBox(viewCell);
|
---|
3264 |
|
---|
3265 | // mViewCellsManager->SetMaxFilterSize(1);
|
---|
3266 | GetPrVS(box.Center(), prvs, viewSpaceFilterSize);
|
---|
3267 | pvs = prvs.mViewCell->GetPvs();
|
---|
3268 | DeleteLocalMergeTree(prvs.mViewCell);
|
---|
3269 | }
|
---|
3270 | }
|
---|
3271 | else
|
---|
3272 | {
|
---|
3273 | pvs = viewCell->GetPvs();
|
---|
3274 | }
|
---|
3275 |
|
---|
3276 | if (spatialFilterSize >=0.0f)
|
---|
3277 | ApplySpatialFilter(kdTree, spatialFilterSize, pvs);
|
---|
3278 |
|
---|
3279 | }
|
---|
3280 |
|
---|
3281 |
|
---|
3282 |
|
---|
3283 | void
|
---|
3284 | ViewCellsManager::ApplyFilter(KdTree *kdTree,
|
---|
3285 | const float relViewSpaceFilterSize,
|
---|
3286 | const float relSpatialFilterSize
|
---|
3287 | )
|
---|
3288 | {
|
---|
3289 |
|
---|
3290 | if (!ViewCellsConstructed())
|
---|
3291 | return;
|
---|
3292 |
|
---|
3293 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
3294 |
|
---|
3295 | ObjectPvs *newPvs;
|
---|
3296 | newPvs = new ObjectPvs[mViewCells.size()];
|
---|
3297 |
|
---|
3298 | float viewSpaceFilterSize = Magnitude(mViewSpaceBox.Size())*relViewSpaceFilterSize;
|
---|
3299 | float spatialFilterSize = Magnitude(kdTree->GetBox().Size())*relSpatialFilterSize;
|
---|
3300 |
|
---|
3301 | int i;
|
---|
3302 | for (i=0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
|
---|
3303 | ApplyFilter(*it,
|
---|
3304 | kdTree,
|
---|
3305 | viewSpaceFilterSize,
|
---|
3306 | spatialFilterSize,
|
---|
3307 | newPvs[i]
|
---|
3308 | );
|
---|
3309 | }
|
---|
3310 |
|
---|
3311 | // now replace all pvss
|
---|
3312 | for (i = 0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
|
---|
3313 |
|
---|
3314 | ObjectPvs &pvs = (*it)->GetPvs();
|
---|
3315 | pvs.Clear();
|
---|
3316 | pvs = newPvs[i];
|
---|
3317 | newPvs[i].Clear();
|
---|
3318 | }
|
---|
3319 |
|
---|
3320 | delete [] newPvs;
|
---|
3321 | }
|
---|
3322 |
|
---|
3323 |
|
---|
3324 | void
|
---|
3325 | ViewCellsManager::ApplySpatialFilter(
|
---|
3326 | KdTree *kdTree,
|
---|
3327 | const float spatialFilterSize,
|
---|
3328 | ObjectPvs &pvs
|
---|
3329 | )
|
---|
3330 | {
|
---|
3331 | // now compute a new Pvs by including also objects intersecting the
|
---|
3332 | // extended boxes of visible objects
|
---|
3333 | Intersectable::NewMail();
|
---|
3334 |
|
---|
3335 | ObjectPvsIterator pit = pvs.GetIterator();
|
---|
3336 |
|
---|
3337 | while (pit.HasMoreEntries())
|
---|
3338 | pit.Next()->Mail();
|
---|
3339 |
|
---|
3340 | ObjectPvs nPvs;
|
---|
3341 | int nPvsSize = 0;
|
---|
3342 |
|
---|
3343 | ObjectPvsIterator pit2 = pvs.GetIterator();
|
---|
3344 |
|
---|
3345 | while (pit2.HasMoreEntries())
|
---|
3346 | {
|
---|
3347 | // now go through the pvs again
|
---|
3348 | Intersectable *object = pit2.Next();
|
---|
3349 |
|
---|
3350 | // Vector3 center = object->GetBox().Center();
|
---|
3351 | // AxisAlignedBox3 box(center - Vector3(spatialFilterSize/2),
|
---|
3352 | // center + Vector3(spatialFilterSize/2));
|
---|
3353 |
|
---|
3354 | AxisAlignedBox3 box = object->GetBox();
|
---|
3355 | box.Enlarge(Vector3(spatialFilterSize/2));
|
---|
3356 |
|
---|
3357 | ObjectContainer objects;
|
---|
3358 |
|
---|
3359 | // $$ warning collect objects takes only unmailed ones!
|
---|
3360 | kdTree->CollectObjects(box, objects);
|
---|
3361 | // cout<<"collected objects="<<objects.size()<<endl;
|
---|
3362 | ObjectContainer::const_iterator noi = objects.begin();
|
---|
3363 | for (; noi != objects.end(); ++ noi)
|
---|
3364 | {
|
---|
3365 | Intersectable *o = *noi;
|
---|
3366 | cout<<"w";
|
---|
3367 | // $$ JB warning: pdfs are not correct at this point!
|
---|
3368 | nPvs.AddSample(o, Limits::Small);
|
---|
3369 | nPvsSize ++;
|
---|
3370 | }
|
---|
3371 | }
|
---|
3372 |
|
---|
3373 | // cout<<"nPvs size = "<<nPvsSize<<endl;
|
---|
3374 | pvs.MergeInPlace(nPvs);
|
---|
3375 | }
|
---|
3376 |
|
---|
3377 |
|
---|
3378 | void ViewCellsManager::MergeViewCellsRecursivly(ObjectPvs &pvs,
|
---|
3379 | const ViewCellContainer &viewCells) const
|
---|
3380 | {
|
---|
3381 | MergeViewCellsRecursivly(pvs, viewCells, 0, (int)viewCells.size() - 1);
|
---|
3382 | }
|
---|
3383 |
|
---|
3384 |
|
---|
3385 | void ViewCellsManager::MergeViewCellsRecursivly(ObjectPvs &pvs,
|
---|
3386 | const ViewCellContainer &viewCells,
|
---|
3387 | const int leftIdx,
|
---|
3388 | const int rightIdx) const
|
---|
3389 | {
|
---|
3390 | if (leftIdx == rightIdx)
|
---|
3391 | {
|
---|
3392 | pvs = viewCells[leftIdx]->GetPvs();
|
---|
3393 | }
|
---|
3394 | else
|
---|
3395 | {
|
---|
3396 | const int midSplit = (leftIdx + rightIdx) / 2;
|
---|
3397 |
|
---|
3398 | ObjectPvs leftPvs, rightPvs;
|
---|
3399 | MergeViewCellsRecursivly(leftPvs, viewCells, leftIdx, midSplit);
|
---|
3400 | MergeViewCellsRecursivly(rightPvs, viewCells, midSplit, rightIdx);
|
---|
3401 |
|
---|
3402 | ObjectPvs::Merge(pvs, leftPvs, rightPvs);
|
---|
3403 | }
|
---|
3404 | }
|
---|
3405 |
|
---|
3406 |
|
---|
3407 | PvsFilterStatistics
|
---|
3408 | ViewCellsManager::ApplyFilter2(ViewCell *viewCell,
|
---|
3409 | const bool useViewSpaceFilter,
|
---|
3410 | const float filterSize,
|
---|
3411 | ObjectPvs &pvs,
|
---|
3412 | vector<AxisAlignedBox3> *filteredBoxes
|
---|
3413 | )
|
---|
3414 | {
|
---|
3415 | pvs.Reserve(viewCell->GetFilteredPvsSize());
|
---|
3416 |
|
---|
3417 | PvsFilterStatistics stats;
|
---|
3418 |
|
---|
3419 | AxisAlignedBox3 vbox = GetViewCellBox(viewCell);
|
---|
3420 | const Vector3 center = vbox.Center();
|
---|
3421 |
|
---|
3422 | // copy the PVS
|
---|
3423 | if (!mUseKdPvs)
|
---|
3424 | Intersectable::NewMail();
|
---|
3425 | else
|
---|
3426 | KdNode::NewMail();
|
---|
3427 |
|
---|
3428 | ObjectPvs basePvs = viewCell->GetPvs();
|
---|
3429 | ObjectPvsIterator pit = basePvs.GetIterator();
|
---|
3430 |
|
---|
3431 | if (!mUseKdPvs)
|
---|
3432 | { // first mark all objects from this pvs
|
---|
3433 | while (pit.HasMoreEntries())
|
---|
3434 | pit.Next()->Mail();
|
---|
3435 | }
|
---|
3436 |
|
---|
3437 | int pvsSize = 0;
|
---|
3438 | int nPvsSize = 0;
|
---|
3439 |
|
---|
3440 | //Debug<<"f #s="<<samples<<" pvs size = "<<basePvs.GetSize();
|
---|
3441 | // cout<<"Filter size = "<<filterSize<<endl;
|
---|
3442 | // cout<<"vbox = "<<vbox<<endl;
|
---|
3443 | // cout<<"center = "<<center<<endl;
|
---|
3444 |
|
---|
3445 |
|
---|
3446 | // Minimal number of local samples to take into account
|
---|
3447 | // the local sampling density.
|
---|
3448 | // The size of the filter is a minimum of the conservative
|
---|
3449 | // local sampling density estimate (#rays intersecting teh viewcell and
|
---|
3450 | // the object)
|
---|
3451 | // and gobal estimate for the view cell
|
---|
3452 | // (total #rays intersecting the viewcell)
|
---|
3453 | const int minLocalSamples = 2;
|
---|
3454 | const float viewCellRadius = 0.5f * Magnitude(vbox.Diagonal());
|
---|
3455 |
|
---|
3456 | float samples = (float)basePvs.GetSamples();
|
---|
3457 |
|
---|
3458 |
|
---|
3459 | //////////
|
---|
3460 | //-- now compute the filter box around the current viewCell
|
---|
3461 |
|
---|
3462 | if (useViewSpaceFilter)
|
---|
3463 | {
|
---|
3464 | // float radius = Max(viewCellRadius/100.0f, avgRadius - viewCellRadius);
|
---|
3465 | float radius = viewCellRadius / 100.0f;
|
---|
3466 | vbox.Enlarge(radius);
|
---|
3467 | cout<<"vbox = "<<vbox<<endl;
|
---|
3468 |
|
---|
3469 | ViewCellContainer viewCells;
|
---|
3470 | ComputeBoxIntersections(vbox, viewCells);
|
---|
3471 |
|
---|
3472 | ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
3473 |
|
---|
3474 | for (int i = 0; it != it_end; ++ it, ++ i)
|
---|
3475 | {
|
---|
3476 | if ((*it) != viewCell)
|
---|
3477 | {
|
---|
3478 | //cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
|
---|
3479 | basePvs.MergeInPlace((*it)->GetPvs());
|
---|
3480 | }
|
---|
3481 |
|
---|
3482 | // update samples and globalC
|
---|
3483 | samples = (float)pvs.GetSamples();
|
---|
3484 | // cout<<"neighboring viewcells = "<<i-1<<endl;
|
---|
3485 | // cout<<"Samples' = "<<samples<<endl;
|
---|
3486 | }
|
---|
3487 | }
|
---|
3488 |
|
---|
3489 | // Minimal number of samples so that filtering takes place
|
---|
3490 | const float MIN_SAMPLES = 50;
|
---|
3491 |
|
---|
3492 | if (samples > MIN_SAMPLES)
|
---|
3493 | {
|
---|
3494 | float globalC = 2.0f * filterSize / sqrt(samples);
|
---|
3495 |
|
---|
3496 | ObjectContainer objects;
|
---|
3497 | PvsData pvsData;
|
---|
3498 |
|
---|
3499 | pit = basePvs.GetIterator();
|
---|
3500 |
|
---|
3501 | while (pit.HasMoreEntries())
|
---|
3502 | {
|
---|
3503 | Intersectable *object = pit.Next(pvsData);
|
---|
3504 |
|
---|
3505 | // compute filter size based on the distance and the numebr of samples
|
---|
3506 | AxisAlignedBox3 box = object->GetBox();
|
---|
3507 |
|
---|
3508 | float distance = Distance(center, box.Center());
|
---|
3509 | float globalRadius = distance*globalC;
|
---|
3510 |
|
---|
3511 | int objectSamples = (int)pvsData.mSumPdf;
|
---|
3512 | float localRadius = MAX_FLOAT;
|
---|
3513 |
|
---|
3514 | localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/
|
---|
3515 | sqrt((float)objectSamples);
|
---|
3516 |
|
---|
3517 | // cout<<"os="<<objectSamples<<" lr="<<localRadius<<" gr="<<globalRadius<<endl;
|
---|
3518 |
|
---|
3519 | // now compute the filter size
|
---|
3520 | float radius;
|
---|
3521 |
|
---|
3522 | #if 0
|
---|
3523 | if (objectSamples <= 1)
|
---|
3524 | {
|
---|
3525 | if (localRadius > globalRadius)
|
---|
3526 | {
|
---|
3527 | radius = 0.5flRadius;
|
---|
3528 | stats.mLocalFilterCount++;
|
---|
3529 | }
|
---|
3530 | else
|
---|
3531 | {
|
---|
3532 | radius = globalRadius;
|
---|
3533 | stats.mGlobalFilterCount++;
|
---|
3534 | }
|
---|
3535 | }
|
---|
3536 | else
|
---|
3537 | {
|
---|
3538 | radius = localRadius;
|
---|
3539 | stats.mLocalFilterCount++;
|
---|
3540 | }
|
---|
3541 | #else
|
---|
3542 |
|
---|
3543 | // radius = 0.5f*globalRadius + 0.5f*localRadius;
|
---|
3544 | radius = Min(globalRadius, localRadius);
|
---|
3545 |
|
---|
3546 | if (localRadius > globalRadius)
|
---|
3547 | ++ stats.mLocalFilterCount;
|
---|
3548 | else
|
---|
3549 | ++ stats.mGlobalFilterCount;
|
---|
3550 | #endif
|
---|
3551 |
|
---|
3552 | stats.mAvgFilterRadius += radius;
|
---|
3553 |
|
---|
3554 | // cout<<"box = "<<box<<endl;
|
---|
3555 | // cout<<"distance = "<<distance<<endl;
|
---|
3556 | // cout<<"radiues = "<<radius<<endl;
|
---|
3557 |
|
---|
3558 | box.Enlarge(Vector3(radius));
|
---|
3559 |
|
---|
3560 | if (filteredBoxes)
|
---|
3561 | filteredBoxes->push_back(box);
|
---|
3562 |
|
---|
3563 | objects.clear();
|
---|
3564 |
|
---|
3565 | // $$ warning collect objects takes only unmailed ones!
|
---|
3566 | if (mUseKdPvs)
|
---|
3567 | GetPreprocessor()->mKdTree->CollectKdObjects(box, objects);
|
---|
3568 | else
|
---|
3569 | CollectObjects(box, objects);
|
---|
3570 |
|
---|
3571 | // cout<<"collected objects="<<objects.size()<<endl;
|
---|
3572 | ObjectContainer::const_iterator noi = objects.begin();
|
---|
3573 | for (; noi != objects.end(); ++ noi)
|
---|
3574 | {
|
---|
3575 | Intersectable *o = *noi;
|
---|
3576 | // $$ JB warning: pdfs are not correct at this point!
|
---|
3577 | pvs.AddSampleDirty(o, Limits::Small);
|
---|
3578 | }
|
---|
3579 | }
|
---|
3580 |
|
---|
3581 | stats.mAvgFilterRadius /= (stats.mLocalFilterCount + stats.mGlobalFilterCount);
|
---|
3582 | }
|
---|
3583 |
|
---|
3584 | //Debug << " nPvs size = " << pvs.GetSize() << endl;
|
---|
3585 |
|
---|
3586 | if (!mUseKdPvs)
|
---|
3587 | {
|
---|
3588 | PvsData pvsData;
|
---|
3589 |
|
---|
3590 | // copy the base pvs to the new pvs
|
---|
3591 | pit = basePvs.GetIterator();
|
---|
3592 | while (pit.HasMoreEntries())
|
---|
3593 | {
|
---|
3594 | Intersectable *obj = pit.Next(pvsData);
|
---|
3595 | pvs.AddSampleDirty(obj, pvsData.mSumPdf);
|
---|
3596 | }
|
---|
3597 | }
|
---|
3598 |
|
---|
3599 | pvs.SimpleSort();
|
---|
3600 | viewCell->SetFilteredPvsSize(pvs.GetSize());
|
---|
3601 |
|
---|
3602 | // warning: not thread-safe!
|
---|
3603 | if (!mUseKdPvs)
|
---|
3604 | Intersectable::NewMail();
|
---|
3605 |
|
---|
3606 | return stats;
|
---|
3607 | }
|
---|
3608 |
|
---|
3609 |
|
---|
3610 |
|
---|
3611 | void ViewCellsManager::ExportColor(Exporter *exporter,
|
---|
3612 | ViewCell *vc,
|
---|
3613 | int colorCode) const
|
---|
3614 | {
|
---|
3615 | const bool vcValid = CheckValidity(vc, mMinPvsSize, mMaxPvsSize);
|
---|
3616 |
|
---|
3617 | float importance = 0;
|
---|
3618 | static Material m;
|
---|
3619 | //cout << "color code: " << colorCode << endl;
|
---|
3620 |
|
---|
3621 | switch (colorCode)
|
---|
3622 | {
|
---|
3623 | case 0: // Random
|
---|
3624 | {
|
---|
3625 | if (vcValid)
|
---|
3626 | {
|
---|
3627 | m.mDiffuseColor.r = 0.2f + RandomValue(0.0f, 0.8f);
|
---|
3628 | m.mDiffuseColor.g = 0.2f + RandomValue(0.0f, 0.8f);
|
---|
3629 | m.mDiffuseColor.b = 0.2f + RandomValue(0.0f, 0.8f);
|
---|
3630 | }
|
---|
3631 | else
|
---|
3632 | {
|
---|
3633 | m.mDiffuseColor.r = 0.0f;
|
---|
3634 | m.mDiffuseColor.g = 1.0f;
|
---|
3635 | m.mDiffuseColor.b = 0.0f;
|
---|
3636 | }
|
---|
3637 |
|
---|
3638 | exporter->SetForcedMaterial(m);
|
---|
3639 | return;
|
---|
3640 | }
|
---|
3641 |
|
---|
3642 | case 1: // pvs
|
---|
3643 | {
|
---|
3644 | if (mCurrentViewCellsStats.maxPvs)
|
---|
3645 | {
|
---|
3646 | importance = //(float)mViewCellsTree->GetTrianglesInPvs(vc) / 700;
|
---|
3647 | (float)mCurrentViewCellsStats.maxPvs;
|
---|
3648 | }
|
---|
3649 | }
|
---|
3650 | break;
|
---|
3651 | case 2: // merges
|
---|
3652 | {
|
---|
3653 | const int lSize = mViewCellsTree->GetNumInitialViewCells(vc);
|
---|
3654 | importance = (float)lSize / (float)mCurrentViewCellsStats.maxLeaves;
|
---|
3655 | }
|
---|
3656 | break;
|
---|
3657 | default:
|
---|
3658 | break;
|
---|
3659 | }
|
---|
3660 |
|
---|
3661 | // special color code for invalid view cells
|
---|
3662 | m.mDiffuseColor.r = importance;
|
---|
3663 | m.mDiffuseColor.b = 1.0f;//vcValid ? 0.0f : 1.0f;
|
---|
3664 | m.mDiffuseColor.g = 1.0f - importance;
|
---|
3665 |
|
---|
3666 | //Debug << "importance: " << importance << endl;
|
---|
3667 | exporter->SetForcedMaterial(m);
|
---|
3668 | }
|
---|
3669 |
|
---|
3670 |
|
---|
3671 | void ViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
3672 | vector<MergeCandidate> &candidates)
|
---|
3673 | {
|
---|
3674 | // implemented in subclasses
|
---|
3675 | }
|
---|
3676 |
|
---|
3677 |
|
---|
3678 | void ViewCellsManager::UpdatePvsForEvaluation()
|
---|
3679 | {
|
---|
3680 | ObjectPvs objPvs;
|
---|
3681 | UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), objPvs);
|
---|
3682 | }
|
---|
3683 |
|
---|
3684 |
|
---|
3685 | void ViewCellsManager::UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs)
|
---|
3686 | {
|
---|
3687 | // terminate traversal
|
---|
3688 | if (root->IsLeaf())
|
---|
3689 | {
|
---|
3690 | // we assume that pvs is explicitly stored in leaves
|
---|
3691 | pvs = root->GetPvs();
|
---|
3692 | UpdateScalarPvsSize(root, pvs.EvalPvsCost(), pvs.GetSize());
|
---|
3693 |
|
---|
3694 | return;
|
---|
3695 | }
|
---|
3696 |
|
---|
3697 | ////////////////
|
---|
3698 | //-- interior node => propagate pvs up the tree
|
---|
3699 |
|
---|
3700 | ViewCellInterior *interior = static_cast<ViewCellInterior *>(root);
|
---|
3701 |
|
---|
3702 | // reset interior pvs
|
---|
3703 | interior->GetPvs().Clear();
|
---|
3704 |
|
---|
3705 | // reset recursive pvs
|
---|
3706 | pvs.Clear();
|
---|
3707 |
|
---|
3708 | // pvss of child nodes
|
---|
3709 | vector<ObjectPvs> pvsList;
|
---|
3710 | pvsList.resize((int)interior->mChildren.size());
|
---|
3711 |
|
---|
3712 | ViewCellContainer::const_iterator vit, vit_end = interior->mChildren.end();
|
---|
3713 |
|
---|
3714 | int i = 0;
|
---|
3715 |
|
---|
3716 | ////////
|
---|
3717 | //-- recursivly compute child pvss
|
---|
3718 |
|
---|
3719 | for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ i)
|
---|
3720 | {
|
---|
3721 | UpdatePvsForEvaluation(*vit, pvsList[i]);
|
---|
3722 | }
|
---|
3723 |
|
---|
3724 | #if 1
|
---|
3725 | Intersectable::NewMail();
|
---|
3726 |
|
---|
3727 |
|
---|
3728 | ///////////
|
---|
3729 | //-- merge pvss
|
---|
3730 |
|
---|
3731 | PvsData pvsData;
|
---|
3732 |
|
---|
3733 | vector<ObjectPvs>::iterator oit = pvsList.begin();
|
---|
3734 |
|
---|
3735 | for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ oit)
|
---|
3736 | {
|
---|
3737 | ObjectPvsIterator pit = (*oit).GetIterator();
|
---|
3738 |
|
---|
3739 | // add pvss to new pvs: use mailing to avoid adding entries two times.
|
---|
3740 | while (pit.HasMoreEntries())
|
---|
3741 | {
|
---|
3742 | Intersectable *intersect = pit.Next(pvsData);
|
---|
3743 |
|
---|
3744 | if (!intersect->Mailed())
|
---|
3745 | {
|
---|
3746 | intersect->Mail();
|
---|
3747 | pvs.AddSampleDirty(intersect, pvsData.mSumPdf);
|
---|
3748 | }
|
---|
3749 | }
|
---|
3750 | }
|
---|
3751 |
|
---|
3752 | // store pvs in this node
|
---|
3753 | if (mViewCellsTree->ViewCellsStorage() == ViewCellsTree::PVS_IN_INTERIORS)
|
---|
3754 | {
|
---|
3755 | interior->SetPvs(pvs);
|
---|
3756 | }
|
---|
3757 |
|
---|
3758 | // set new pvs size
|
---|
3759 | UpdateScalarPvsSize(interior, pvs.EvalPvsCost(), pvs.GetSize());
|
---|
3760 |
|
---|
3761 | #else
|
---|
3762 | // really merge cells: slow but sumPdf is correct
|
---|
3763 | viewCellInterior->GetPvs().Merge(backVc->GetPvs());
|
---|
3764 | viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
|
---|
3765 | #endif
|
---|
3766 | }
|
---|
3767 |
|
---|
3768 |
|
---|
3769 |
|
---|
3770 | /*******************************************************************/
|
---|
3771 | /* BspViewCellsManager implementation */
|
---|
3772 | /*******************************************************************/
|
---|
3773 |
|
---|
3774 |
|
---|
3775 | BspViewCellsManager::BspViewCellsManager(ViewCellsTree *vcTree, BspTree *bspTree):
|
---|
3776 | ViewCellsManager(vcTree), mBspTree(bspTree)
|
---|
3777 | {
|
---|
3778 | Environment::GetSingleton()->GetIntValue("BspTree.Construction.samples", mInitialSamples);
|
---|
3779 |
|
---|
3780 | mBspTree->SetViewCellsManager(this);
|
---|
3781 | mBspTree->SetViewCellsTree(mViewCellsTree);
|
---|
3782 | }
|
---|
3783 |
|
---|
3784 |
|
---|
3785 | bool BspViewCellsManager::ViewCellsConstructed() const
|
---|
3786 | {
|
---|
3787 | return mBspTree->GetRoot() != NULL;
|
---|
3788 | }
|
---|
3789 |
|
---|
3790 |
|
---|
3791 | ViewCell *BspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
3792 | {
|
---|
3793 | return new BspViewCell(mesh);
|
---|
3794 | }
|
---|
3795 |
|
---|
3796 |
|
---|
3797 | int BspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
3798 | const VssRayContainer &rays)
|
---|
3799 | {
|
---|
3800 | // if view cells were already constructed, we can finish
|
---|
3801 | if (ViewCellsConstructed())
|
---|
3802 | return 0;
|
---|
3803 |
|
---|
3804 | int sampleContributions = 0;
|
---|
3805 |
|
---|
3806 | // construct view cells using the collected samples
|
---|
3807 | RayContainer constructionRays;
|
---|
3808 | VssRayContainer savedRays;
|
---|
3809 |
|
---|
3810 | // choose a a number of rays based on the ratio of cast rays / requested rays
|
---|
3811 | const int limit = min(mInitialSamples, (int)rays.size());
|
---|
3812 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
3813 |
|
---|
3814 | const float prop = (float)limit / ((float)rays.size() + Limits::Small);
|
---|
3815 |
|
---|
3816 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
3817 | {
|
---|
3818 | if (Random(1.0f) < prop)
|
---|
3819 | constructionRays.push_back(new Ray(*(*it)));
|
---|
3820 | else
|
---|
3821 | savedRays.push_back(*it);
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 | if (!mUsePredefinedViewCells)
|
---|
3825 | {
|
---|
3826 | // no view cells loaded
|
---|
3827 | mBspTree->Construct(objects, constructionRays, &mViewSpaceBox);
|
---|
3828 | // collect final view cells
|
---|
3829 | mBspTree->CollectViewCells(mViewCells);
|
---|
3830 | }
|
---|
3831 | else
|
---|
3832 | {
|
---|
3833 | // use predefined view cells geometry =>
|
---|
3834 | // contruct bsp hierarchy over them
|
---|
3835 | mBspTree->Construct(mViewCells);
|
---|
3836 | }
|
---|
3837 |
|
---|
3838 | // destroy rays created only for construction
|
---|
3839 | CLEAR_CONTAINER(constructionRays);
|
---|
3840 |
|
---|
3841 | Debug << mBspTree->GetStatistics() << endl;
|
---|
3842 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
3843 |
|
---|
3844 | // recast rest of the rays
|
---|
3845 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
3846 | ComputeSampleContributions(savedRays, true, false);
|
---|
3847 |
|
---|
3848 | // real meshes are contructed at this stage
|
---|
3849 | if (0)
|
---|
3850 | {
|
---|
3851 | cout << "finalizing view cells ... ";
|
---|
3852 | FinalizeViewCells(true);
|
---|
3853 | cout << "finished" << endl;
|
---|
3854 | }
|
---|
3855 |
|
---|
3856 | return sampleContributions;
|
---|
3857 | }
|
---|
3858 |
|
---|
3859 |
|
---|
3860 | void BspViewCellsManager::CollectViewCells()
|
---|
3861 | {
|
---|
3862 | if (!ViewCellsTreeConstructed())
|
---|
3863 | { // view cells tree constructed
|
---|
3864 | mBspTree->CollectViewCells(mViewCells);
|
---|
3865 | }
|
---|
3866 | else
|
---|
3867 | { // we can use the view cells tree hierarchy to get the right set
|
---|
3868 | mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
|
---|
3869 | }
|
---|
3870 | }
|
---|
3871 |
|
---|
3872 |
|
---|
3873 | float BspViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
3874 | {
|
---|
3875 | if (1)
|
---|
3876 | return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
|
---|
3877 | else
|
---|
3878 | // compute view cell area as subsititute for probability
|
---|
3879 | return GetArea(viewCell) / GetAccVcArea();
|
---|
3880 | }
|
---|
3881 |
|
---|
3882 |
|
---|
3883 |
|
---|
3884 | int BspViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
3885 | const Vector3 &termination,
|
---|
3886 | ViewCellContainer &viewcells)
|
---|
3887 | {
|
---|
3888 | return mBspTree->CastLineSegment(origin, termination, viewcells);
|
---|
3889 | }
|
---|
3890 |
|
---|
3891 |
|
---|
3892 | bool BspViewCellsManager::LineSegmentIntersects(const Vector3 &origin,
|
---|
3893 | const Vector3 &termination,
|
---|
3894 | ViewCell *viewCell)
|
---|
3895 | {
|
---|
3896 | return false;
|
---|
3897 | }
|
---|
3898 |
|
---|
3899 |
|
---|
3900 | void ViewCellsManager::ExportMergedViewCells(const ObjectContainer &objects)
|
---|
3901 | {
|
---|
3902 | // save color code
|
---|
3903 | const int savedColorCode = mColorCode;
|
---|
3904 |
|
---|
3905 | Exporter *exporter;
|
---|
3906 |
|
---|
3907 | // export merged view cells using pvs color coding
|
---|
3908 | exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
|
---|
3909 | cout << "exporting view cells after merge (pvs size) ... ";
|
---|
3910 |
|
---|
3911 | if (exporter)
|
---|
3912 | {
|
---|
3913 | if (mExportGeometry)
|
---|
3914 | {
|
---|
3915 | exporter->ExportGeometry(objects);
|
---|
3916 | }
|
---|
3917 |
|
---|
3918 | exporter->SetFilled();
|
---|
3919 | mColorCode = 1;
|
---|
3920 |
|
---|
3921 | ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
|
---|
3922 |
|
---|
3923 | delete exporter;
|
---|
3924 | }
|
---|
3925 | cout << "finished" << endl;
|
---|
3926 |
|
---|
3927 | mColorCode = savedColorCode;
|
---|
3928 | }
|
---|
3929 |
|
---|
3930 |
|
---|
3931 | int BspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
3932 | const VssRayContainer &rays)
|
---|
3933 | {
|
---|
3934 | if (!ViewCellsConstructed())
|
---|
3935 | {
|
---|
3936 | Debug << "view cells not constructed" << endl;
|
---|
3937 | return 0;
|
---|
3938 | }
|
---|
3939 |
|
---|
3940 | // view cells already finished before post processing step,
|
---|
3941 | // i.e., because they were loaded from disc
|
---|
3942 | if (mViewCellsFinished)
|
---|
3943 | {
|
---|
3944 | FinalizeViewCells(true);
|
---|
3945 | EvaluateViewCellsStats();
|
---|
3946 |
|
---|
3947 | return 0;
|
---|
3948 | }
|
---|
3949 |
|
---|
3950 | //////////////////
|
---|
3951 | //-- merge leaves of the view cell hierarchy
|
---|
3952 |
|
---|
3953 | cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
|
---|
3954 | long startTime = GetTime();
|
---|
3955 |
|
---|
3956 | VssRayContainer postProcessRays;
|
---|
3957 | GetRaySets(rays, mPostProcessSamples, postProcessRays);
|
---|
3958 |
|
---|
3959 | if (mMergeViewCells)
|
---|
3960 | {
|
---|
3961 | cout << "constructing visibility based merge tree" << endl;
|
---|
3962 | mViewCellsTree->ConstructMergeTree(rays, objects);
|
---|
3963 | }
|
---|
3964 | else
|
---|
3965 | {
|
---|
3966 | cout << "constructing spatial merge tree" << endl;
|
---|
3967 | ViewCell *root;
|
---|
3968 | // the spatial merge tree is difficult to build for
|
---|
3969 | // this type of construction, as view cells cover several
|
---|
3970 | // leaves => create dummy tree which is only 2 levels deep
|
---|
3971 | if (mUsePredefinedViewCells)
|
---|
3972 | {
|
---|
3973 | root = ConstructDummyMergeTree(mBspTree->GetRoot());
|
---|
3974 | }
|
---|
3975 | else
|
---|
3976 | {
|
---|
3977 | // create spatial merge hierarchy
|
---|
3978 | root = ConstructSpatialMergeTree(mBspTree->GetRoot());
|
---|
3979 | }
|
---|
3980 |
|
---|
3981 | mViewCellsTree->SetRoot(root);
|
---|
3982 |
|
---|
3983 | // recompute pvs in the whole hierarchy
|
---|
3984 | ObjectPvs pvs;
|
---|
3985 | UpdatePvsForEvaluation(root, pvs);
|
---|
3986 | }
|
---|
3987 |
|
---|
3988 | cout << "finished" << endl;
|
---|
3989 | cout << "merged view cells in "
|
---|
3990 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
3991 |
|
---|
3992 | Debug << "Postprocessing: Merged view cells in "
|
---|
3993 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl << endl;
|
---|
3994 |
|
---|
3995 |
|
---|
3996 | ////////////////////////
|
---|
3997 | //-- visualization and statistics after merge
|
---|
3998 |
|
---|
3999 | if (1)
|
---|
4000 | {
|
---|
4001 | char mstats[100];
|
---|
4002 | Environment::GetSingleton()->GetStringValue("ViewCells.mergeStats", mstats);
|
---|
4003 | mViewCellsTree->ExportStats(mstats);
|
---|
4004 | }
|
---|
4005 |
|
---|
4006 | // recompute view cells and stats
|
---|
4007 | ResetViewCells();
|
---|
4008 | Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
|
---|
4009 |
|
---|
4010 | // visualization of the view cells
|
---|
4011 | if (1) ExportMergedViewCells(objects);
|
---|
4012 |
|
---|
4013 | // compute final meshes and volume / area
|
---|
4014 | if (1) FinalizeViewCells(true);
|
---|
4015 |
|
---|
4016 | return 0;
|
---|
4017 | }
|
---|
4018 |
|
---|
4019 |
|
---|
4020 | BspViewCellsManager::~BspViewCellsManager()
|
---|
4021 | {
|
---|
4022 | }
|
---|
4023 |
|
---|
4024 |
|
---|
4025 | int BspViewCellsManager::GetType() const
|
---|
4026 | {
|
---|
4027 | return BSP;
|
---|
4028 | }
|
---|
4029 |
|
---|
4030 |
|
---|
4031 | void BspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
4032 | const VssRayContainer &sampleRays)
|
---|
4033 | {
|
---|
4034 | if (!ViewCellsConstructed())
|
---|
4035 | return;
|
---|
4036 |
|
---|
4037 | const int savedColorCode = mColorCode;
|
---|
4038 |
|
---|
4039 | if (1) // export final view cells
|
---|
4040 | {
|
---|
4041 | mColorCode = 1; // 0 = pvs, 1 = random
|
---|
4042 | Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
|
---|
4043 |
|
---|
4044 | cout << "exporting view cells after merge (pvs size) ... ";
|
---|
4045 |
|
---|
4046 | if (exporter)
|
---|
4047 | {
|
---|
4048 | if (mExportGeometry)
|
---|
4049 | {
|
---|
4050 | exporter->ExportGeometry(objects);
|
---|
4051 | }
|
---|
4052 |
|
---|
4053 | ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
|
---|
4054 | delete exporter;
|
---|
4055 | }
|
---|
4056 | cout << "finished" << endl;
|
---|
4057 | }
|
---|
4058 |
|
---|
4059 | // reset color code
|
---|
4060 | mColorCode = savedColorCode;
|
---|
4061 |
|
---|
4062 |
|
---|
4063 | //////////////////
|
---|
4064 | //-- visualization of the BSP splits
|
---|
4065 |
|
---|
4066 | bool exportSplits = false;
|
---|
4067 | Environment::GetSingleton()->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
|
---|
4068 |
|
---|
4069 | if (exportSplits)
|
---|
4070 | {
|
---|
4071 | cout << "exporting splits ... ";
|
---|
4072 | ExportSplits(objects);
|
---|
4073 | cout << "finished" << endl;
|
---|
4074 | }
|
---|
4075 |
|
---|
4076 | int leafOut;
|
---|
4077 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
|
---|
4078 | const int raysOut = 100;
|
---|
4079 | ExportSingleViewCells(objects, leafOut, false, true, false, raysOut, "");
|
---|
4080 | }
|
---|
4081 |
|
---|
4082 |
|
---|
4083 | void BspViewCellsManager::ExportSplits(const ObjectContainer &objects)
|
---|
4084 | {
|
---|
4085 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
4086 |
|
---|
4087 | if (exporter)
|
---|
4088 | {
|
---|
4089 | //exporter->SetFilled();
|
---|
4090 | if (mExportGeometry)
|
---|
4091 | {
|
---|
4092 | exporter->ExportGeometry(objects);
|
---|
4093 | }
|
---|
4094 |
|
---|
4095 | Material m;
|
---|
4096 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
4097 | exporter->SetForcedMaterial(m);
|
---|
4098 | exporter->SetWireframe();
|
---|
4099 |
|
---|
4100 | exporter->ExportBspSplits(*mBspTree, true);
|
---|
4101 |
|
---|
4102 | // NOTE: take forced material, else big scenes cannot be viewed
|
---|
4103 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
4104 | exporter->SetForcedMaterial(m);
|
---|
4105 | //exporter->ResetForcedMaterial();
|
---|
4106 |
|
---|
4107 | delete exporter;
|
---|
4108 | }
|
---|
4109 | }
|
---|
4110 |
|
---|
4111 |
|
---|
4112 | void BspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
|
---|
4113 | const int maxViewCells,
|
---|
4114 | const bool sortViewCells,
|
---|
4115 | const bool exportPvs,
|
---|
4116 | const bool exportRays,
|
---|
4117 | const int maxRays,
|
---|
4118 | const string &prefix,
|
---|
4119 | VssRayContainer *visRays)
|
---|
4120 | {
|
---|
4121 | if (sortViewCells)
|
---|
4122 | { // sort view cells to visualize the largest view cells
|
---|
4123 | sort(mViewCells.begin(), mViewCells.end(), LargerRenderCost);
|
---|
4124 | }
|
---|
4125 |
|
---|
4126 | //////////
|
---|
4127 | //-- export visualizations of some view cells
|
---|
4128 |
|
---|
4129 | ViewCell::NewMail();
|
---|
4130 | const int limit = min(maxViewCells, (int)mViewCells.size());
|
---|
4131 |
|
---|
4132 | for (int i = 0; i < limit; ++ i)
|
---|
4133 | {
|
---|
4134 | const int idx = sortViewCells ? (int)RandomValue(0, (float)mViewCells.size() - 0.5f) : i;
|
---|
4135 | ViewCell *vc = mViewCells[idx];
|
---|
4136 |
|
---|
4137 | if (vc->Mailed() || vc->GetId() == OUT_OF_BOUNDS_ID)
|
---|
4138 | continue;
|
---|
4139 |
|
---|
4140 | vc->Mail();
|
---|
4141 |
|
---|
4142 | ObjectPvs pvs;
|
---|
4143 | mViewCellsTree->GetPvs(vc, pvs);
|
---|
4144 |
|
---|
4145 | char s[64]; sprintf(s, "%sviewcell-%04d.wrl", prefix.c_str(), i);
|
---|
4146 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
4147 |
|
---|
4148 | cout << "view cell " << idx << ": pvs cost=" << (int)mViewCellsTree->GetTrianglesInPvs(vc) << endl;
|
---|
4149 |
|
---|
4150 | if (exportRays)
|
---|
4151 | {
|
---|
4152 | ////////////
|
---|
4153 | //-- export rays piercing this view cell
|
---|
4154 |
|
---|
4155 | // use rays stored with the view cells
|
---|
4156 | VssRayContainer vcRays, vcRays2, vcRays3;
|
---|
4157 | VssRayContainer collectRays;
|
---|
4158 |
|
---|
4159 | // collect initial view cells
|
---|
4160 | ViewCellContainer leaves;
|
---|
4161 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
4162 |
|
---|
4163 | ViewCellContainer::const_iterator vit, vit_end = leaves.end();
|
---|
4164 | for (vit = leaves.begin(); vit != vit_end; ++ vit)
|
---|
4165 | {
|
---|
4166 | // prepare some rays for visualization
|
---|
4167 | VssRayContainer::const_iterator rit, rit_end = (*vit)->GetOrCreateRays()->end();
|
---|
4168 | for (rit = (*vit)->GetOrCreateRays()->begin(); rit != rit_end; ++ rit)
|
---|
4169 | {
|
---|
4170 | collectRays.push_back(*rit);
|
---|
4171 | }
|
---|
4172 | }
|
---|
4173 |
|
---|
4174 | const int raysOut = min((int)collectRays.size(), maxRays);
|
---|
4175 |
|
---|
4176 | // prepare some rays for visualization
|
---|
4177 | VssRayContainer::const_iterator rit, rit_end = collectRays.end();
|
---|
4178 | for (rit = collectRays.begin(); rit != rit_end; ++ rit)
|
---|
4179 | {
|
---|
4180 | const float p = RandomValue(0.0f, (float)collectRays.size());
|
---|
4181 | if (p < raysOut)
|
---|
4182 | {
|
---|
4183 | if ((*rit)->mFlags & VssRay::BorderSample)
|
---|
4184 | {
|
---|
4185 | vcRays.push_back(*rit);
|
---|
4186 | }
|
---|
4187 | else if ((*rit)->mFlags & VssRay::ReverseSample)
|
---|
4188 | {
|
---|
4189 | vcRays2.push_back(*rit);
|
---|
4190 | }
|
---|
4191 | else
|
---|
4192 | {
|
---|
4193 | vcRays3.push_back(*rit);
|
---|
4194 | }
|
---|
4195 | }
|
---|
4196 | }
|
---|
4197 |
|
---|
4198 | exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
|
---|
4199 | exporter->ExportRays(vcRays2, RgbColor(0, 1, 0));
|
---|
4200 | exporter->ExportRays(vcRays3, RgbColor(1, 1, 1));
|
---|
4201 | }
|
---|
4202 |
|
---|
4203 | ////////////////
|
---|
4204 | //-- export view cell geometry
|
---|
4205 |
|
---|
4206 | exporter->SetWireframe();
|
---|
4207 |
|
---|
4208 | Material m;//= RandomMaterial();
|
---|
4209 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
4210 | exporter->SetForcedMaterial(m);
|
---|
4211 |
|
---|
4212 | ExportViewCellGeometry(exporter, vc, NULL, NULL);
|
---|
4213 | exporter->SetFilled();
|
---|
4214 |
|
---|
4215 | if (exportPvs)
|
---|
4216 | {
|
---|
4217 | Intersectable::NewMail();
|
---|
4218 | ObjectPvsIterator pit = pvs.GetIterator();
|
---|
4219 |
|
---|
4220 | while (pit.HasMoreEntries())
|
---|
4221 | {
|
---|
4222 | Intersectable *intersect = pit.Next();
|
---|
4223 |
|
---|
4224 | // output PVS of view cell
|
---|
4225 | if (!intersect->Mailed())
|
---|
4226 | {
|
---|
4227 | intersect->Mail();
|
---|
4228 |
|
---|
4229 | m = RandomMaterial();
|
---|
4230 | exporter->SetForcedMaterial(m);
|
---|
4231 | exporter->ExportIntersectable(intersect);
|
---|
4232 | }
|
---|
4233 | }
|
---|
4234 | cout << endl;
|
---|
4235 | }
|
---|
4236 |
|
---|
4237 | DEL_PTR(exporter);
|
---|
4238 | cout << "finished" << endl;
|
---|
4239 | }
|
---|
4240 | }
|
---|
4241 |
|
---|
4242 |
|
---|
4243 | void BspViewCellsManager::TestSubdivision()
|
---|
4244 | {
|
---|
4245 | ViewCellContainer leaves;
|
---|
4246 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
4247 |
|
---|
4248 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
4249 |
|
---|
4250 | const float vol = mViewSpaceBox.GetVolume();
|
---|
4251 | float subdivVol = 0;
|
---|
4252 | float newVol = 0;
|
---|
4253 |
|
---|
4254 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
4255 | {
|
---|
4256 | BspNodeGeometry geom;
|
---|
4257 | mBspTree->ConstructGeometry(*it, geom);
|
---|
4258 |
|
---|
4259 | const float lVol = geom.GetVolume();
|
---|
4260 | newVol += lVol;
|
---|
4261 | subdivVol += (*it)->GetVolume();
|
---|
4262 |
|
---|
4263 | const float thres = 0.9f;
|
---|
4264 | if ((lVol < ((*it)->GetVolume() * thres)) ||
|
---|
4265 | (lVol * thres > ((*it)->GetVolume())))
|
---|
4266 | Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
|
---|
4267 | }
|
---|
4268 |
|
---|
4269 | Debug << "exact volume: " << vol << endl;
|
---|
4270 | Debug << "subdivision volume: " << subdivVol << endl;
|
---|
4271 | Debug << "new volume: " << newVol << endl;
|
---|
4272 | }
|
---|
4273 |
|
---|
4274 |
|
---|
4275 | void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
4276 | ViewCell *vc,
|
---|
4277 | const AxisAlignedBox3 *sceneBox,
|
---|
4278 | const AxisAlignedPlane *clipPlane
|
---|
4279 | ) const
|
---|
4280 | {
|
---|
4281 | if (clipPlane)
|
---|
4282 | {
|
---|
4283 | const Plane3 plane = clipPlane->GetPlane();
|
---|
4284 |
|
---|
4285 | ViewCellContainer leaves;
|
---|
4286 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
4287 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
4288 |
|
---|
4289 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
4290 | {
|
---|
4291 | BspNodeGeometry geom;
|
---|
4292 | BspNodeGeometry front;
|
---|
4293 | BspNodeGeometry back;
|
---|
4294 |
|
---|
4295 | mBspTree->ConstructGeometry(*it, geom);
|
---|
4296 |
|
---|
4297 | const float eps = 0.0001f;
|
---|
4298 | const int cf = geom.Side(plane, eps);
|
---|
4299 |
|
---|
4300 | if (cf == -1)
|
---|
4301 | {
|
---|
4302 | exporter->ExportPolygons(geom.GetPolys());
|
---|
4303 | }
|
---|
4304 | else if (cf == 0)
|
---|
4305 | {
|
---|
4306 | geom.SplitGeometry(front,
|
---|
4307 | back,
|
---|
4308 | plane,
|
---|
4309 | mViewSpaceBox,
|
---|
4310 | eps);
|
---|
4311 |
|
---|
4312 | if (back.Valid())
|
---|
4313 | {
|
---|
4314 | exporter->ExportPolygons(back.GetPolys());
|
---|
4315 | }
|
---|
4316 | }
|
---|
4317 | }
|
---|
4318 | }
|
---|
4319 | else
|
---|
4320 | {
|
---|
4321 | // export mesh if available
|
---|
4322 | // TODO: some bug here?
|
---|
4323 | if (1 && vc->GetMesh())
|
---|
4324 | {
|
---|
4325 | exporter->ExportMesh(vc->GetMesh());
|
---|
4326 | }
|
---|
4327 | else
|
---|
4328 | {
|
---|
4329 | BspNodeGeometry geom;
|
---|
4330 | mBspTree->ConstructGeometry(vc, geom);
|
---|
4331 | exporter->ExportPolygons(geom.GetPolys());
|
---|
4332 | }
|
---|
4333 | }
|
---|
4334 | }
|
---|
4335 |
|
---|
4336 |
|
---|
4337 | void BspViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
4338 | {
|
---|
4339 | // note: should previous mesh be deleted (via mesh manager?)
|
---|
4340 | BspNodeGeometry geom;
|
---|
4341 | mBspTree->ConstructGeometry(vc, geom);
|
---|
4342 |
|
---|
4343 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
4344 |
|
---|
4345 | IncludeNodeGeomInMesh(geom, *mesh);
|
---|
4346 | mesh->ComputeBoundingBox();
|
---|
4347 |
|
---|
4348 | vc->SetMesh(mesh);
|
---|
4349 | }
|
---|
4350 |
|
---|
4351 |
|
---|
4352 | void BspViewCellsManager::Finalize(ViewCell *viewCell,
|
---|
4353 | const bool createMesh)
|
---|
4354 | {
|
---|
4355 | float area = 0;
|
---|
4356 | float volume = 0;
|
---|
4357 |
|
---|
4358 | ViewCellContainer leaves;
|
---|
4359 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
4360 |
|
---|
4361 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
4362 |
|
---|
4363 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
4364 | {
|
---|
4365 | BspNodeGeometry geom;
|
---|
4366 |
|
---|
4367 | mBspTree->ConstructGeometry(*it, geom);
|
---|
4368 |
|
---|
4369 | const float lVol = geom.GetVolume();
|
---|
4370 | const float lArea = geom.GetArea();
|
---|
4371 |
|
---|
4372 | area += lArea;
|
---|
4373 | volume += lVol;
|
---|
4374 |
|
---|
4375 | CreateMesh(*it);
|
---|
4376 | }
|
---|
4377 |
|
---|
4378 | viewCell->SetVolume(volume);
|
---|
4379 | viewCell->SetArea(area);
|
---|
4380 | }
|
---|
4381 |
|
---|
4382 |
|
---|
4383 | ViewCell *BspViewCellsManager::GetViewCell(const Vector3 &point, const bool active) const
|
---|
4384 | {
|
---|
4385 | if (!ViewCellsConstructed())
|
---|
4386 | {
|
---|
4387 | return NULL;
|
---|
4388 | }
|
---|
4389 | if (!mViewSpaceBox.IsInside(point))
|
---|
4390 | {
|
---|
4391 | return NULL;
|
---|
4392 | }
|
---|
4393 | return mBspTree->GetViewCell(point);
|
---|
4394 | }
|
---|
4395 |
|
---|
4396 |
|
---|
4397 | void BspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
4398 | vector<MergeCandidate> &candidates)
|
---|
4399 | {
|
---|
4400 | cout << "collecting merge candidates ... " << endl;
|
---|
4401 |
|
---|
4402 | if (mUseRaysForMerge)
|
---|
4403 | {
|
---|
4404 | mBspTree->CollectMergeCandidates(rays, candidates);
|
---|
4405 | }
|
---|
4406 | else
|
---|
4407 | {
|
---|
4408 | vector<BspLeaf *> leaves;
|
---|
4409 | mBspTree->CollectLeaves(leaves);
|
---|
4410 | mBspTree->CollectMergeCandidates(leaves, candidates);
|
---|
4411 | }
|
---|
4412 |
|
---|
4413 | cout << "fininshed collecting candidates" << endl;
|
---|
4414 | }
|
---|
4415 |
|
---|
4416 |
|
---|
4417 |
|
---|
4418 | bool BspViewCellsManager::ExportViewCells(const string filename,
|
---|
4419 | const bool exportPvs,
|
---|
4420 | const ObjectContainer &objects)
|
---|
4421 | {
|
---|
4422 | if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
|
---|
4423 | {
|
---|
4424 | return false;
|
---|
4425 | }
|
---|
4426 |
|
---|
4427 | cout << "exporting view cells to xml ... ";
|
---|
4428 |
|
---|
4429 | OUT_STREAM stream(filename.c_str());
|
---|
4430 |
|
---|
4431 | // we need unique ids for each view cell
|
---|
4432 | CreateUniqueViewCellIds();
|
---|
4433 |
|
---|
4434 | stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
4435 | stream << "<VisibilitySolution>" << endl;
|
---|
4436 |
|
---|
4437 | if (exportPvs)
|
---|
4438 | {
|
---|
4439 | //////////
|
---|
4440 | //-- export bounding boxes: they are used to identify the objects from the pvs and
|
---|
4441 | //-- assign them to the entities in the rendering engine
|
---|
4442 |
|
---|
4443 | stream << "<BoundingBoxes>" << endl;
|
---|
4444 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
4445 |
|
---|
4446 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
4447 | {
|
---|
4448 | const AxisAlignedBox3 box = (*oit)->GetBox();
|
---|
4449 |
|
---|
4450 | stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
|
---|
4451 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
4452 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
4453 | }
|
---|
4454 |
|
---|
4455 | stream << "</BoundingBoxes>" << endl;
|
---|
4456 | }
|
---|
4457 |
|
---|
4458 | ///////////
|
---|
4459 | //-- export the view cells and the pvs
|
---|
4460 |
|
---|
4461 | const int numViewCells = mCurrentViewCellsStats.viewCells;
|
---|
4462 | stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
|
---|
4463 |
|
---|
4464 | mViewCellsTree->Export(stream, exportPvs);
|
---|
4465 |
|
---|
4466 | stream << "</ViewCells>" << endl;
|
---|
4467 |
|
---|
4468 | /////////////
|
---|
4469 | //-- export the view space hierarchy
|
---|
4470 | stream << "<ViewSpaceHierarchy type=\"bsp\""
|
---|
4471 | << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
|
---|
4472 | << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
|
---|
4473 |
|
---|
4474 | mBspTree->Export(stream);
|
---|
4475 |
|
---|
4476 | // end tags
|
---|
4477 | stream << "</ViewSpaceHierarchy>" << endl;
|
---|
4478 | stream << "</VisibilitySolution>" << endl;
|
---|
4479 |
|
---|
4480 | stream.close();
|
---|
4481 | cout << "finished" << endl;
|
---|
4482 |
|
---|
4483 | return true;
|
---|
4484 | }
|
---|
4485 |
|
---|
4486 |
|
---|
4487 | ViewCell *BspViewCellsManager::ConstructDummyMergeTree(BspNode *root)
|
---|
4488 | {
|
---|
4489 | ViewCellInterior *vcRoot = new ViewCellInterior();
|
---|
4490 |
|
---|
4491 | // evaluate merge cost for priority traversal
|
---|
4492 | const float mergeCost = -(float)root->mTimeStamp;
|
---|
4493 | vcRoot->SetMergeCost(mergeCost);
|
---|
4494 |
|
---|
4495 | float volume = 0;
|
---|
4496 | vector<BspLeaf *> leaves;
|
---|
4497 | mBspTree->CollectLeaves(leaves);
|
---|
4498 | vector<BspLeaf *>::const_iterator lit, lit_end = leaves.end();
|
---|
4499 | ViewCell::NewMail();
|
---|
4500 |
|
---|
4501 | for (lit = leaves.begin(); lit != lit_end; ++ lit)
|
---|
4502 | {
|
---|
4503 | BspLeaf *leaf = *lit;
|
---|
4504 | ViewCell *vc = leaf->GetViewCell();
|
---|
4505 |
|
---|
4506 | if (!vc->Mailed())
|
---|
4507 | {
|
---|
4508 | vc->Mail();
|
---|
4509 | vc->SetMergeCost(0.0f);
|
---|
4510 | vcRoot->SetupChildLink(vc);
|
---|
4511 |
|
---|
4512 | volume += vc->GetVolume();
|
---|
4513 | volume += vc->GetVolume();
|
---|
4514 | vcRoot->SetVolume(volume);
|
---|
4515 | }
|
---|
4516 | }
|
---|
4517 |
|
---|
4518 | return vcRoot;
|
---|
4519 | }
|
---|
4520 |
|
---|
4521 |
|
---|
4522 | ViewCell *BspViewCellsManager::ConstructSpatialMergeTree(BspNode *root)
|
---|
4523 | {
|
---|
4524 | // terminate recursion
|
---|
4525 | if (root->IsLeaf())
|
---|
4526 | {
|
---|
4527 | BspLeaf *leaf = static_cast<BspLeaf *>(root);
|
---|
4528 | leaf->GetViewCell()->SetMergeCost(0.0f);
|
---|
4529 | return leaf->GetViewCell();
|
---|
4530 | }
|
---|
4531 |
|
---|
4532 | BspInterior *interior = static_cast<BspInterior *>(root);
|
---|
4533 | ViewCellInterior *viewCellInterior = new ViewCellInterior();
|
---|
4534 |
|
---|
4535 | // evaluate merge cost for priority traversal
|
---|
4536 | const float mergeCost = -(float)root->mTimeStamp;
|
---|
4537 | viewCellInterior->SetMergeCost(mergeCost);
|
---|
4538 |
|
---|
4539 | float volume = 0;
|
---|
4540 |
|
---|
4541 | BspNode *front = interior->GetFront();
|
---|
4542 | BspNode *back = interior->GetBack();
|
---|
4543 |
|
---|
4544 |
|
---|
4545 | ////////////
|
---|
4546 | //-- recursivly compute child hierarchies
|
---|
4547 |
|
---|
4548 | ViewCell *backVc = ConstructSpatialMergeTree(back);
|
---|
4549 | ViewCell *frontVc = ConstructSpatialMergeTree(front);
|
---|
4550 |
|
---|
4551 | viewCellInterior->SetupChildLink(backVc);
|
---|
4552 | viewCellInterior->SetupChildLink(frontVc);
|
---|
4553 |
|
---|
4554 | volume += backVc->GetVolume();
|
---|
4555 | volume += frontVc->GetVolume();
|
---|
4556 |
|
---|
4557 | viewCellInterior->SetVolume(volume);
|
---|
4558 |
|
---|
4559 | return viewCellInterior;
|
---|
4560 | }
|
---|
4561 |
|
---|
4562 |
|
---|
4563 | /************************************************************************/
|
---|
4564 | /* KdViewCellsManager implementation */
|
---|
4565 | /************************************************************************/
|
---|
4566 |
|
---|
4567 |
|
---|
4568 |
|
---|
4569 | KdViewCellsManager::KdViewCellsManager(ViewCellsTree *vcTree, KdTree *kdTree):
|
---|
4570 | ViewCellsManager(vcTree), mKdTree(kdTree), mKdPvsDepth(100)
|
---|
4571 | {
|
---|
4572 | }
|
---|
4573 |
|
---|
4574 |
|
---|
4575 | float KdViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
4576 | {
|
---|
4577 | // compute view cell area / volume as subsititute for probability
|
---|
4578 | if (0)
|
---|
4579 | return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
|
---|
4580 | else
|
---|
4581 | return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
|
---|
4582 | }
|
---|
4583 |
|
---|
4584 |
|
---|
4585 |
|
---|
4586 |
|
---|
4587 | void KdViewCellsManager::CollectViewCells()
|
---|
4588 | {
|
---|
4589 | //mKdTree->CollectViewCells(mViewCells); TODO
|
---|
4590 | }
|
---|
4591 |
|
---|
4592 |
|
---|
4593 | int KdViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
4594 | const VssRayContainer &rays)
|
---|
4595 | {
|
---|
4596 | // if view cells already constructed
|
---|
4597 | if (ViewCellsConstructed())
|
---|
4598 | return 0;
|
---|
4599 |
|
---|
4600 | mKdTree->Construct();
|
---|
4601 |
|
---|
4602 | mTotalAreaValid = false;
|
---|
4603 | // create the view cells
|
---|
4604 | mKdTree->CreateAndCollectViewCells(mViewCells);
|
---|
4605 | // cast rays
|
---|
4606 | ComputeSampleContributions(rays, true, false);
|
---|
4607 |
|
---|
4608 | EvaluateViewCellsStats();
|
---|
4609 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
4610 |
|
---|
4611 | return 0;
|
---|
4612 | }
|
---|
4613 |
|
---|
4614 |
|
---|
4615 | bool KdViewCellsManager::ViewCellsConstructed() const
|
---|
4616 | {
|
---|
4617 | return mKdTree->GetRoot() != NULL;
|
---|
4618 | }
|
---|
4619 |
|
---|
4620 |
|
---|
4621 | int KdViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
4622 | const VssRayContainer &rays)
|
---|
4623 | {
|
---|
4624 | return 0;
|
---|
4625 | }
|
---|
4626 |
|
---|
4627 |
|
---|
4628 | void KdViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
|
---|
4629 | const int maxViewCells,
|
---|
4630 | const bool sortViewCells,
|
---|
4631 | const bool exportPvs,
|
---|
4632 | const bool exportRays,
|
---|
4633 | const int maxRays,
|
---|
4634 | const string &prefix,
|
---|
4635 | VssRayContainer *visRays)
|
---|
4636 | {
|
---|
4637 | // TODO
|
---|
4638 | }
|
---|
4639 |
|
---|
4640 |
|
---|
4641 | void KdViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
4642 | const VssRayContainer &sampleRays)
|
---|
4643 | {
|
---|
4644 | if (!ViewCellsConstructed())
|
---|
4645 | return;
|
---|
4646 |
|
---|
4647 | // using view cells instead of the kd PVS of objects
|
---|
4648 | const bool useViewCells = true;
|
---|
4649 | bool exportRays = false;
|
---|
4650 |
|
---|
4651 | int limit = min(mVisualizationSamples, (int)sampleRays.size());
|
---|
4652 | const int pvsOut = min((int)objects.size(), 10);
|
---|
4653 | VssRayContainer *rays = new VssRayContainer[pvsOut];
|
---|
4654 |
|
---|
4655 | if (useViewCells)
|
---|
4656 | {
|
---|
4657 | const int leafOut = 10;
|
---|
4658 |
|
---|
4659 | ViewCell::NewMail();
|
---|
4660 |
|
---|
4661 | //-- some rays for visualization
|
---|
4662 | const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
|
---|
4663 | Debug << "visualization using " << raysOut << " samples" << endl;
|
---|
4664 |
|
---|
4665 | //-- some random view cells and rays for visualization
|
---|
4666 | vector<KdLeaf *> kdLeaves;
|
---|
4667 |
|
---|
4668 | for (int i = 0; i < leafOut; ++ i)
|
---|
4669 | kdLeaves.push_back(static_cast<KdLeaf *>(mKdTree->GetRandomLeaf()));
|
---|
4670 |
|
---|
4671 | for (int i = 0; i < kdLeaves.size(); ++ i)
|
---|
4672 | {
|
---|
4673 | KdLeaf *leaf = kdLeaves[i];
|
---|
4674 | RayContainer vcRays;
|
---|
4675 |
|
---|
4676 | cout << "creating output for view cell " << i << " ... ";
|
---|
4677 | #if 0
|
---|
4678 | // check whether we can add the current ray to the output rays
|
---|
4679 | for (int k = 0; k < raysOut; ++ k)
|
---|
4680 | {
|
---|
4681 | Ray *ray = sampleRays[k];
|
---|
4682 |
|
---|
4683 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
4684 | {
|
---|
4685 | BspLeaf *leaf2 = ray->bspIntersections[j].mLeaf;
|
---|
4686 |
|
---|
4687 | if (leaf->GetViewCell() == leaf2->GetViewCell())
|
---|
4688 | {
|
---|
4689 | vcRays.push_back(ray);
|
---|
4690 | }
|
---|
4691 | }
|
---|
4692 | }
|
---|
4693 | #endif
|
---|
4694 | Intersectable::NewMail();
|
---|
4695 |
|
---|
4696 | ViewCell *vc = leaf->mViewCell;
|
---|
4697 | char str[64]; sprintf(str, "viewcell%04d.wrl", i);
|
---|
4698 |
|
---|
4699 | Exporter *exporter = Exporter::GetExporter(str);
|
---|
4700 | exporter->SetFilled();
|
---|
4701 |
|
---|
4702 | exporter->SetWireframe();
|
---|
4703 | //exporter->SetFilled();
|
---|
4704 |
|
---|
4705 | Material m;//= RandomMaterial();
|
---|
4706 | m.mDiffuseColor = RgbColor(1, 1, 0);
|
---|
4707 | exporter->SetForcedMaterial(m);
|
---|
4708 |
|
---|
4709 | AxisAlignedBox3 box = mKdTree->GetBox(leaf);
|
---|
4710 | exporter->ExportBox(box);
|
---|
4711 |
|
---|
4712 | // export rays piercing this view cell
|
---|
4713 | exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));
|
---|
4714 |
|
---|
4715 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
4716 | exporter->SetForcedMaterial(m);
|
---|
4717 |
|
---|
4718 | // exporter->SetWireframe();
|
---|
4719 | exporter->SetFilled();
|
---|
4720 |
|
---|
4721 | ObjectPvsIterator pit = vc->GetPvs().GetIterator();
|
---|
4722 |
|
---|
4723 | while (pit.HasMoreEntries())
|
---|
4724 | {
|
---|
4725 | //-- output PVS of view cell
|
---|
4726 | Intersectable *intersect = pit.Next();
|
---|
4727 |
|
---|
4728 | if (!intersect->Mailed())
|
---|
4729 | {
|
---|
4730 | exporter->ExportIntersectable(intersect);
|
---|
4731 | intersect->Mail();
|
---|
4732 | }
|
---|
4733 | }
|
---|
4734 |
|
---|
4735 | DEL_PTR(exporter);
|
---|
4736 | cout << "finished" << endl;
|
---|
4737 | }
|
---|
4738 |
|
---|
4739 | DEL_PTR(rays);
|
---|
4740 | }
|
---|
4741 | else // using kd PVS of objects
|
---|
4742 | {
|
---|
4743 | for (int i = 0; i < limit; ++ i)
|
---|
4744 | {
|
---|
4745 | VssRay *ray = sampleRays[i];
|
---|
4746 |
|
---|
4747 | // check whether we can add this to the rays
|
---|
4748 | for (int j = 0; j < pvsOut; j++)
|
---|
4749 | {
|
---|
4750 | if (objects[j] == ray->mTerminationObject)
|
---|
4751 | {
|
---|
4752 | rays[j].push_back(ray);
|
---|
4753 | }
|
---|
4754 | }
|
---|
4755 | }
|
---|
4756 |
|
---|
4757 | if (exportRays)
|
---|
4758 | {
|
---|
4759 | Exporter *exporter = NULL;
|
---|
4760 | exporter = Exporter::GetExporter("sample-rays.x3d");
|
---|
4761 | exporter->SetWireframe();
|
---|
4762 | exporter->ExportKdTree(*mKdTree);
|
---|
4763 |
|
---|
4764 | for (int i = 0; i < pvsOut; i++)
|
---|
4765 | exporter->ExportRays(rays[i], RgbColor(1, 0, 0));
|
---|
4766 |
|
---|
4767 | exporter->SetFilled();
|
---|
4768 | delete exporter;
|
---|
4769 | }
|
---|
4770 |
|
---|
4771 | for (int k=0; k < pvsOut; k++)
|
---|
4772 | {
|
---|
4773 | Intersectable *object = objects[k];
|
---|
4774 | char str[64]; sprintf(str, "viewcell%04d.wrl", k);
|
---|
4775 |
|
---|
4776 | Exporter *exporter = Exporter::GetExporter(str);
|
---|
4777 | exporter->SetWireframe();
|
---|
4778 |
|
---|
4779 | // matt: we do not use kd pvs
|
---|
4780 | #if 0
|
---|
4781 | KdPvsMap::iterator kit = object->mKdPvs.mEntries.begin();
|
---|
4782 | Intersectable::NewMail();
|
---|
4783 |
|
---|
4784 | // avoid adding the object to the list
|
---|
4785 | object->Mail();
|
---|
4786 | ObjectContainer visibleObjects;
|
---|
4787 |
|
---|
4788 | for (; kit != object->mKdPvs.mEntries.end(); i++)
|
---|
4789 | {
|
---|
4790 | KdNode *node = (*kit).first;
|
---|
4791 | exporter->ExportBox(mKdTree->GetBox(node));
|
---|
4792 |
|
---|
4793 | mKdTree->CollectObjects(node, visibleObjects);
|
---|
4794 | }
|
---|
4795 |
|
---|
4796 | exporter->ExportRays(rays[k], RgbColor(0, 1, 0));
|
---|
4797 | exporter->SetFilled();
|
---|
4798 |
|
---|
4799 | for (int j = 0; j < visibleObjects.size(); j++)
|
---|
4800 | exporter->ExportIntersectable(visibleObjects[j]);
|
---|
4801 |
|
---|
4802 | Material m;
|
---|
4803 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
4804 | exporter->SetForcedMaterial(m);
|
---|
4805 | exporter->ExportIntersectable(object);
|
---|
4806 | #endif
|
---|
4807 | delete exporter;
|
---|
4808 | }
|
---|
4809 | }
|
---|
4810 | }
|
---|
4811 |
|
---|
4812 |
|
---|
4813 | ViewCell *KdViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
4814 | {
|
---|
4815 | return new KdViewCell(mesh);
|
---|
4816 | }
|
---|
4817 |
|
---|
4818 |
|
---|
4819 | void KdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
4820 | ViewCell *vc,
|
---|
4821 | const AxisAlignedBox3 *sceneBox,
|
---|
4822 | const AxisAlignedPlane *clipPlane
|
---|
4823 | ) const
|
---|
4824 | {
|
---|
4825 | ViewCellContainer leaves;
|
---|
4826 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
4827 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
4828 |
|
---|
4829 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
4830 | {
|
---|
4831 | KdViewCell *kdVc = static_cast<KdViewCell *>(*it);
|
---|
4832 | exporter->ExportBox(mKdTree->GetBox(kdVc->mLeaves[0]));
|
---|
4833 | }
|
---|
4834 | }
|
---|
4835 |
|
---|
4836 |
|
---|
4837 | int KdViewCellsManager::GetType() const
|
---|
4838 | {
|
---|
4839 | return ViewCellsManager::KD;
|
---|
4840 | }
|
---|
4841 |
|
---|
4842 |
|
---|
4843 |
|
---|
4844 | KdNode *KdViewCellsManager::GetNodeForPvs(KdLeaf *leaf)
|
---|
4845 | {
|
---|
4846 | KdNode *node = leaf;
|
---|
4847 |
|
---|
4848 | while (node->mParent && node->mDepth > mKdPvsDepth)
|
---|
4849 | node = node->mParent;
|
---|
4850 |
|
---|
4851 | return node;
|
---|
4852 | }
|
---|
4853 |
|
---|
4854 | int KdViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
4855 | const Vector3 &termination,
|
---|
4856 | ViewCellContainer &viewcells)
|
---|
4857 | {
|
---|
4858 | return mKdTree->CastLineSegment(origin, termination, viewcells);
|
---|
4859 | }
|
---|
4860 |
|
---|
4861 |
|
---|
4862 | bool KdViewCellsManager::LineSegmentIntersects(const Vector3 &origin,
|
---|
4863 | const Vector3 &termination,
|
---|
4864 | ViewCell *viewCell)
|
---|
4865 | {
|
---|
4866 | return false;
|
---|
4867 | }
|
---|
4868 |
|
---|
4869 |
|
---|
4870 | void KdViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
4871 | {
|
---|
4872 | // TODO
|
---|
4873 | }
|
---|
4874 |
|
---|
4875 |
|
---|
4876 |
|
---|
4877 | void KdViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
4878 | vector<MergeCandidate> &candidates)
|
---|
4879 | {
|
---|
4880 | // TODO
|
---|
4881 | }
|
---|
4882 |
|
---|
4883 |
|
---|
4884 |
|
---|
4885 | /**************************************************************************/
|
---|
4886 | /* VspBspViewCellsManager implementation */
|
---|
4887 | /**************************************************************************/
|
---|
4888 |
|
---|
4889 |
|
---|
4890 | VspBspViewCellsManager::VspBspViewCellsManager(ViewCellsTree *vcTree, VspBspTree *vspBspTree):
|
---|
4891 | ViewCellsManager(vcTree), mVspBspTree(vspBspTree)
|
---|
4892 | {
|
---|
4893 | Environment::GetSingleton()->GetIntValue("VspBspTree.Construction.samples", mInitialSamples);
|
---|
4894 | mVspBspTree->SetViewCellsManager(this);
|
---|
4895 | mVspBspTree->mViewCellsTree = mViewCellsTree;
|
---|
4896 | }
|
---|
4897 |
|
---|
4898 |
|
---|
4899 | VspBspViewCellsManager::~VspBspViewCellsManager()
|
---|
4900 | {
|
---|
4901 | }
|
---|
4902 |
|
---|
4903 |
|
---|
4904 | float VspBspViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
4905 | {
|
---|
4906 | if (0 && mVspBspTree->mUseAreaForPvs)
|
---|
4907 | return GetArea(viewCell) / GetAccVcArea();
|
---|
4908 | else
|
---|
4909 | return GetVolume(viewCell) / mViewSpaceBox.GetVolume();
|
---|
4910 | }
|
---|
4911 |
|
---|
4912 |
|
---|
4913 | void VspBspViewCellsManager::CollectViewCells()
|
---|
4914 | {
|
---|
4915 | // view cells tree constructed?
|
---|
4916 | if (!ViewCellsTreeConstructed())
|
---|
4917 | {
|
---|
4918 | mVspBspTree->CollectViewCells(mViewCells, false);
|
---|
4919 | }
|
---|
4920 | else
|
---|
4921 | {
|
---|
4922 | // we can use the view cells tree hierarchy to get the right set
|
---|
4923 | mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
|
---|
4924 | }
|
---|
4925 | }
|
---|
4926 |
|
---|
4927 |
|
---|
4928 | void VspBspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
4929 | vector<MergeCandidate> &candidates)
|
---|
4930 | {
|
---|
4931 | cout << "collecting merge candidates ... " << endl;
|
---|
4932 |
|
---|
4933 | if (mUseRaysForMerge)
|
---|
4934 | {
|
---|
4935 | mVspBspTree->CollectMergeCandidates(rays, candidates);
|
---|
4936 | }
|
---|
4937 | else
|
---|
4938 | {
|
---|
4939 | vector<BspLeaf *> leaves;
|
---|
4940 | mVspBspTree->CollectLeaves(leaves);
|
---|
4941 |
|
---|
4942 | mVspBspTree->CollectMergeCandidates(leaves, candidates);
|
---|
4943 | }
|
---|
4944 |
|
---|
4945 | cout << "fininshed collecting candidates" << endl;
|
---|
4946 | }
|
---|
4947 |
|
---|
4948 |
|
---|
4949 | bool VspBspViewCellsManager::ViewCellsConstructed() const
|
---|
4950 | {
|
---|
4951 | return mVspBspTree->GetRoot() != NULL;
|
---|
4952 | }
|
---|
4953 |
|
---|
4954 |
|
---|
4955 | ViewCell *VspBspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
4956 | {
|
---|
4957 | return new BspViewCell(mesh);
|
---|
4958 | }
|
---|
4959 |
|
---|
4960 |
|
---|
4961 | int VspBspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
4962 | const VssRayContainer &rays)
|
---|
4963 | {
|
---|
4964 | mMaxPvsSize = (int)(mMaxPvsRatio * (float)objects.size());
|
---|
4965 |
|
---|
4966 | // if view cells were already constructed
|
---|
4967 | if (ViewCellsConstructed())
|
---|
4968 | {
|
---|
4969 | return 0;
|
---|
4970 | }
|
---|
4971 |
|
---|
4972 | int sampleContributions = 0;
|
---|
4973 | VssRayContainer sampleRays;
|
---|
4974 |
|
---|
4975 | const int limit = min(mInitialSamples, (int)rays.size());
|
---|
4976 |
|
---|
4977 | Debug << "samples used for vsp bsp subdivision: " << mInitialSamples
|
---|
4978 | << ", actual rays: " << (int)rays.size() << endl;
|
---|
4979 |
|
---|
4980 | VssRayContainer savedRays;
|
---|
4981 |
|
---|
4982 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
4983 | {
|
---|
4984 | VssRayContainer constructionRays;
|
---|
4985 |
|
---|
4986 | GetRaySets(rays, mInitialSamples, constructionRays, &savedRays);
|
---|
4987 |
|
---|
4988 | Debug << "rays used for initial construction: " << (int)constructionRays.size() << endl;
|
---|
4989 | Debug << "rays saved for later use: " << (int)savedRays.size() << endl;
|
---|
4990 |
|
---|
4991 | mVspBspTree->Construct(constructionRays, &mViewSpaceBox);
|
---|
4992 | }
|
---|
4993 | else
|
---|
4994 | {
|
---|
4995 | Debug << "rays used for initial construction: " << (int)rays.size() << endl;
|
---|
4996 | mVspBspTree->Construct(rays, &mViewSpaceBox);
|
---|
4997 | }
|
---|
4998 |
|
---|
4999 | // collapse invalid regions
|
---|
5000 | cout << "collapsing invalid tree regions ... ";
|
---|
5001 | long startTime = GetTime();
|
---|
5002 |
|
---|
5003 | const int collapsedLeaves = mVspBspTree->CollapseTree();
|
---|
5004 | Debug << "collapsed in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
5005 | << " seconds" << endl;
|
---|
5006 |
|
---|
5007 | cout << "finished" << endl;
|
---|
5008 |
|
---|
5009 | /////////////////
|
---|
5010 | //-- stats after construction
|
---|
5011 |
|
---|
5012 | Debug << mVspBspTree->GetStatistics() << endl;
|
---|
5013 |
|
---|
5014 | ResetViewCells();
|
---|
5015 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
5016 |
|
---|
5017 |
|
---|
5018 | //////////////////////
|
---|
5019 | //-- recast the rest of the rays
|
---|
5020 |
|
---|
5021 | startTime = GetTime();
|
---|
5022 |
|
---|
5023 | cout << "Computing remaining ray contributions ... ";
|
---|
5024 |
|
---|
5025 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
5026 | ComputeSampleContributions(savedRays, true, false);
|
---|
5027 |
|
---|
5028 | cout << "finished" << endl;
|
---|
5029 |
|
---|
5030 | Debug << "Computed remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
5031 | << " secs" << endl;
|
---|
5032 |
|
---|
5033 | cout << "construction finished" << endl;
|
---|
5034 |
|
---|
5035 | if (0)
|
---|
5036 | { ////////
|
---|
5037 | //-- real meshes are contructed at this stage
|
---|
5038 |
|
---|
5039 | cout << "finalizing view cells ... ";
|
---|
5040 | FinalizeViewCells(true);
|
---|
5041 | cout << "finished" << endl;
|
---|
5042 | }
|
---|
5043 |
|
---|
5044 | return sampleContributions;
|
---|
5045 | }
|
---|
5046 |
|
---|
5047 |
|
---|
5048 | void VspBspViewCellsManager::MergeViewCells(const VssRayContainer &rays,
|
---|
5049 | const ObjectContainer &objects)
|
---|
5050 | {
|
---|
5051 | int vcSize = 0;
|
---|
5052 | int pvsSize = 0;
|
---|
5053 |
|
---|
5054 | //-- merge view cells
|
---|
5055 | cout << "starting merge using " << mPostProcessSamples << " samples ... " << endl;
|
---|
5056 | long startTime = GetTime();
|
---|
5057 |
|
---|
5058 |
|
---|
5059 | if (mMergeViewCells)
|
---|
5060 | {
|
---|
5061 | // TODO: should be done BEFORE the ray casting
|
---|
5062 | // compute tree by merging the nodes based on cost heuristics
|
---|
5063 | mViewCellsTree->ConstructMergeTree(rays, objects);
|
---|
5064 | }
|
---|
5065 | else
|
---|
5066 | {
|
---|
5067 | // compute tree by merging the nodes of the spatial hierarchy
|
---|
5068 | ViewCell *root = ConstructSpatialMergeTree(mVspBspTree->GetRoot());
|
---|
5069 | mViewCellsTree->SetRoot(root);
|
---|
5070 |
|
---|
5071 | // compute pvs
|
---|
5072 | ObjectPvs pvs;
|
---|
5073 | UpdatePvsForEvaluation(root, pvs);
|
---|
5074 | }
|
---|
5075 |
|
---|
5076 | if (1)
|
---|
5077 | {
|
---|
5078 | char mstats[100];
|
---|
5079 | ObjectPvs pvs;
|
---|
5080 |
|
---|
5081 | Environment::GetSingleton()->GetStringValue("ViewCells.mergeStats", mstats);
|
---|
5082 | mViewCellsTree->ExportStats(mstats);
|
---|
5083 | }
|
---|
5084 |
|
---|
5085 | cout << "merged view cells in "
|
---|
5086 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
5087 |
|
---|
5088 | Debug << "Postprocessing: Merged view cells in "
|
---|
5089 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
|
---|
5090 |
|
---|
5091 |
|
---|
5092 | //////////////////
|
---|
5093 | //-- stats and visualizations
|
---|
5094 |
|
---|
5095 | int savedColorCode = mColorCode;
|
---|
5096 |
|
---|
5097 | // get currently active view cell set
|
---|
5098 | ResetViewCells();
|
---|
5099 | Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
|
---|
5100 |
|
---|
5101 | if (mShowVisualization) // export merged view cells
|
---|
5102 | {
|
---|
5103 | mColorCode = 0;
|
---|
5104 | Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl");
|
---|
5105 |
|
---|
5106 | cout << "exporting view cells after merge ... ";
|
---|
5107 |
|
---|
5108 | if (exporter)
|
---|
5109 | {
|
---|
5110 | if (0)
|
---|
5111 | exporter->SetWireframe();
|
---|
5112 | else
|
---|
5113 | exporter->SetFilled();
|
---|
5114 |
|
---|
5115 | ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
|
---|
5116 |
|
---|
5117 | if (mExportGeometry)
|
---|
5118 | {
|
---|
5119 | Material m;
|
---|
5120 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
5121 | exporter->SetForcedMaterial(m);
|
---|
5122 | exporter->SetFilled();
|
---|
5123 |
|
---|
5124 | exporter->ExportGeometry(objects);
|
---|
5125 | }
|
---|
5126 |
|
---|
5127 | delete exporter;
|
---|
5128 | }
|
---|
5129 | cout << "finished" << endl;
|
---|
5130 | }
|
---|
5131 |
|
---|
5132 | mColorCode = savedColorCode;
|
---|
5133 | }
|
---|
5134 |
|
---|
5135 |
|
---|
5136 | void VspBspViewCellsManager::RefineViewCells(const VssRayContainer &rays,
|
---|
5137 | const ObjectContainer &objects)
|
---|
5138 | {
|
---|
5139 | mRenderer->RenderScene();
|
---|
5140 |
|
---|
5141 | SimulationStatistics ss;
|
---|
5142 | static_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
5143 | Debug << "render time before refine\n\n" << ss << endl;
|
---|
5144 |
|
---|
5145 | const long startTime = GetTime();
|
---|
5146 | cout << "Refining the merged view cells ... ";
|
---|
5147 |
|
---|
5148 | // refining the merged view cells
|
---|
5149 | const int refined = mViewCellsTree->RefineViewCells(rays, objects);
|
---|
5150 |
|
---|
5151 | //-- stats and visualizations
|
---|
5152 | cout << "finished" << endl;
|
---|
5153 | cout << "refined " << refined << " view cells in "
|
---|
5154 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
5155 |
|
---|
5156 | Debug << "Postprocessing: refined " << refined << " view cells in "
|
---|
5157 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
|
---|
5158 | }
|
---|
5159 |
|
---|
5160 |
|
---|
5161 | int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
5162 | const VssRayContainer &rays)
|
---|
5163 | {
|
---|
5164 | if (!ViewCellsConstructed())
|
---|
5165 | {
|
---|
5166 | Debug << "postprocess error: no view cells constructed" << endl;
|
---|
5167 | return 0;
|
---|
5168 | }
|
---|
5169 |
|
---|
5170 | // view cells already finished before post processing step
|
---|
5171 | // (i.e. because they were loaded)
|
---|
5172 | if (mViewCellsFinished)
|
---|
5173 | {
|
---|
5174 | FinalizeViewCells(true);
|
---|
5175 | EvaluateViewCellsStats();
|
---|
5176 |
|
---|
5177 | return 0;
|
---|
5178 | }
|
---|
5179 |
|
---|
5180 | // check if new view cells turned invalid
|
---|
5181 | int minPvs, maxPvs;
|
---|
5182 |
|
---|
5183 | if (0)
|
---|
5184 | {
|
---|
5185 | minPvs = mMinPvsSize;
|
---|
5186 | maxPvs = mMaxPvsSize;
|
---|
5187 | }
|
---|
5188 | else
|
---|
5189 | {
|
---|
5190 | // problem matt: why did I start here from zero?
|
---|
5191 | minPvs = 0;
|
---|
5192 | maxPvs = mMaxPvsSize;
|
---|
5193 | }
|
---|
5194 |
|
---|
5195 | Debug << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
5196 | cout << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
5197 |
|
---|
5198 | SetValidity(minPvs, maxPvs);
|
---|
5199 |
|
---|
5200 | // update valid view space according to valid view cells
|
---|
5201 | if (0) mVspBspTree->ValidateTree();
|
---|
5202 |
|
---|
5203 | // area has to be recomputed
|
---|
5204 | mTotalAreaValid = false;
|
---|
5205 | VssRayContainer postProcessRays;
|
---|
5206 | GetRaySets(rays, mPostProcessSamples, postProcessRays);
|
---|
5207 |
|
---|
5208 | Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
|
---|
5209 |
|
---|
5210 | //////////
|
---|
5211 | //-- merge neighbouring view cells
|
---|
5212 | MergeViewCells(postProcessRays, objects);
|
---|
5213 |
|
---|
5214 | // refines the merged view cells
|
---|
5215 | if (0) RefineViewCells(postProcessRays, objects);
|
---|
5216 |
|
---|
5217 |
|
---|
5218 | ///////////
|
---|
5219 | //-- render simulation after merge + refine
|
---|
5220 |
|
---|
5221 | cout << "\nview cells partition render time before compress" << endl << endl;;
|
---|
5222 | static_cast<RenderSimulator *>(mRenderer)->RenderScene();
|
---|
5223 | SimulationStatistics ss;
|
---|
5224 | static_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
5225 | cout << ss << endl;
|
---|
5226 |
|
---|
5227 | if (0) CompressViewCells();
|
---|
5228 |
|
---|
5229 | // collapse sibling leaves that share the same view cell
|
---|
5230 | if (0) mVspBspTree->CollapseTree();
|
---|
5231 |
|
---|
5232 | // recompute view cell list and statistics
|
---|
5233 | ResetViewCells();
|
---|
5234 |
|
---|
5235 | // compute final meshes and volume / area
|
---|
5236 | if (1) FinalizeViewCells(true);
|
---|
5237 |
|
---|
5238 | return 0;
|
---|
5239 | }
|
---|
5240 |
|
---|
5241 |
|
---|
5242 | int VspBspViewCellsManager::GetType() const
|
---|
5243 | {
|
---|
5244 | return VSP_BSP;
|
---|
5245 | }
|
---|
5246 |
|
---|
5247 |
|
---|
5248 | ViewCell *VspBspViewCellsManager::ConstructSpatialMergeTree(BspNode *root)
|
---|
5249 | {
|
---|
5250 | // terminate recursion
|
---|
5251 | if (root->IsLeaf())
|
---|
5252 | {
|
---|
5253 | BspLeaf *leaf = static_cast<BspLeaf *>(root);
|
---|
5254 | leaf->GetViewCell()->SetMergeCost(0.0f);
|
---|
5255 | return leaf->GetViewCell();
|
---|
5256 | }
|
---|
5257 |
|
---|
5258 |
|
---|
5259 | BspInterior *interior = static_cast<BspInterior *>(root);
|
---|
5260 | ViewCellInterior *viewCellInterior = new ViewCellInterior();
|
---|
5261 |
|
---|
5262 | // evaluate merge cost for priority traversal
|
---|
5263 | float mergeCost = 1.0f / (float)root->mTimeStamp;
|
---|
5264 | viewCellInterior->SetMergeCost(mergeCost);
|
---|
5265 |
|
---|
5266 | float volume = 0;
|
---|
5267 |
|
---|
5268 | BspNode *front = interior->GetFront();
|
---|
5269 | BspNode *back = interior->GetBack();
|
---|
5270 |
|
---|
5271 |
|
---|
5272 | ObjectPvs frontPvs, backPvs;
|
---|
5273 |
|
---|
5274 | //-- recursivly compute child hierarchies
|
---|
5275 | ViewCell *backVc = ConstructSpatialMergeTree(back);
|
---|
5276 | ViewCell *frontVc = ConstructSpatialMergeTree(front);
|
---|
5277 |
|
---|
5278 |
|
---|
5279 | viewCellInterior->SetupChildLink(backVc);
|
---|
5280 | viewCellInterior->SetupChildLink(frontVc);
|
---|
5281 |
|
---|
5282 | volume += backVc->GetVolume();
|
---|
5283 | volume += frontVc->GetVolume();
|
---|
5284 |
|
---|
5285 | viewCellInterior->SetVolume(volume);
|
---|
5286 |
|
---|
5287 | return viewCellInterior;
|
---|
5288 | }
|
---|
5289 |
|
---|
5290 |
|
---|
5291 | bool VspBspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
|
---|
5292 | {
|
---|
5293 | if (!ViewCellsConstructed())
|
---|
5294 | return ViewCellsManager::GetViewPoint(viewPoint);
|
---|
5295 |
|
---|
5296 | // TODO: set reasonable limit
|
---|
5297 | const int limit = 20;
|
---|
5298 |
|
---|
5299 | for (int i = 0; i < limit; ++ i)
|
---|
5300 | {
|
---|
5301 | viewPoint = mViewSpaceBox.GetRandomPoint();
|
---|
5302 | if (mVspBspTree->ViewPointValid(viewPoint))
|
---|
5303 | {
|
---|
5304 | return true;
|
---|
5305 | }
|
---|
5306 | }
|
---|
5307 |
|
---|
5308 | Debug << "failed to find valid view point, taking " << viewPoint << endl;
|
---|
5309 | return false;
|
---|
5310 | }
|
---|
5311 |
|
---|
5312 |
|
---|
5313 | bool VspBspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
|
---|
5314 | {
|
---|
5315 | // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
|
---|
5316 | // validy update in preprocessor for all managers)
|
---|
5317 | return ViewCellsManager::ViewPointValid(viewPoint);
|
---|
5318 |
|
---|
5319 | // return mViewSpaceBox.IsInside(viewPoint) &&
|
---|
5320 | // mVspBspTree->ViewPointValid(viewPoint);
|
---|
5321 | }
|
---|
5322 |
|
---|
5323 |
|
---|
5324 | void VspBspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
5325 | const VssRayContainer &sampleRays)
|
---|
5326 | {
|
---|
5327 | if (!ViewCellsConstructed())
|
---|
5328 | return;
|
---|
5329 |
|
---|
5330 | VssRayContainer visRays;
|
---|
5331 | GetRaySets(sampleRays, mVisualizationSamples, visRays);
|
---|
5332 |
|
---|
5333 | if (1)
|
---|
5334 | {
|
---|
5335 | //////////////////
|
---|
5336 | //-- export final view cell partition
|
---|
5337 |
|
---|
5338 | Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
|
---|
5339 |
|
---|
5340 | if (exporter)
|
---|
5341 | {
|
---|
5342 | cout << "exporting view cells after post process ... ";
|
---|
5343 |
|
---|
5344 | if (0)
|
---|
5345 | { // export view space box
|
---|
5346 | exporter->SetWireframe();
|
---|
5347 | exporter->ExportBox(mViewSpaceBox);
|
---|
5348 | exporter->SetFilled();
|
---|
5349 | }
|
---|
5350 |
|
---|
5351 | Material m;
|
---|
5352 | m.mDiffuseColor.r = 0.0f;
|
---|
5353 | m.mDiffuseColor.g = 0.5f;
|
---|
5354 | m.mDiffuseColor.b = 0.5f;
|
---|
5355 |
|
---|
5356 | exporter->SetForcedMaterial(m);
|
---|
5357 |
|
---|
5358 | if (1 && mExportGeometry)
|
---|
5359 | {
|
---|
5360 | exporter->ExportGeometry(objects);
|
---|
5361 | }
|
---|
5362 |
|
---|
5363 | if (0 && mExportRays)
|
---|
5364 | {
|
---|
5365 | exporter->ExportRays(visRays, RgbColor(1, 0, 0));
|
---|
5366 | }
|
---|
5367 | ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
|
---|
5368 |
|
---|
5369 | delete exporter;
|
---|
5370 | cout << "finished" << endl;
|
---|
5371 | }
|
---|
5372 | }
|
---|
5373 |
|
---|
5374 | ////////////////
|
---|
5375 | //-- visualization of the BSP splits
|
---|
5376 |
|
---|
5377 | bool exportSplits = false;
|
---|
5378 | Environment::GetSingleton()->GetBoolValue("VspBspTree.Visualization.exportSplits", exportSplits);
|
---|
5379 |
|
---|
5380 | if (exportSplits)
|
---|
5381 | {
|
---|
5382 | cout << "exporting splits ... ";
|
---|
5383 | ExportSplits(objects, visRays);
|
---|
5384 | cout << "finished" << endl;
|
---|
5385 | }
|
---|
5386 |
|
---|
5387 | ////////
|
---|
5388 | //-- export single view cells
|
---|
5389 |
|
---|
5390 | int leafOut;
|
---|
5391 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
|
---|
5392 | const int raysOut = 100;
|
---|
5393 |
|
---|
5394 | ExportSingleViewCells(objects, leafOut, false, true, false, raysOut, "");
|
---|
5395 | }
|
---|
5396 |
|
---|
5397 |
|
---|
5398 | void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects,
|
---|
5399 | const VssRayContainer &rays)
|
---|
5400 | {
|
---|
5401 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
5402 |
|
---|
5403 | if (exporter)
|
---|
5404 | {
|
---|
5405 | Material m;
|
---|
5406 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
5407 | exporter->SetForcedMaterial(m);
|
---|
5408 | exporter->SetWireframe();
|
---|
5409 |
|
---|
5410 | exporter->ExportBspSplits(*mVspBspTree, true);
|
---|
5411 |
|
---|
5412 | // take forced material, else big scenes cannot be viewed
|
---|
5413 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
5414 | exporter->SetForcedMaterial(m);
|
---|
5415 | exporter->SetFilled();
|
---|
5416 |
|
---|
5417 | exporter->ResetForcedMaterial();
|
---|
5418 |
|
---|
5419 | // export rays
|
---|
5420 | if (mExportRays)
|
---|
5421 | {
|
---|
5422 | exporter->ExportRays(rays, RgbColor(1, 1, 0));
|
---|
5423 | }
|
---|
5424 |
|
---|
5425 | if (mExportGeometry)
|
---|
5426 | {
|
---|
5427 | exporter->ExportGeometry(objects);
|
---|
5428 | }
|
---|
5429 | delete exporter;
|
---|
5430 | }
|
---|
5431 | }
|
---|
5432 |
|
---|
5433 |
|
---|
5434 | void VspBspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
|
---|
5435 | const int maxViewCells,
|
---|
5436 | const bool sortViewCells,
|
---|
5437 | const bool exportPvs,
|
---|
5438 | const bool exportRays,
|
---|
5439 | const int maxRays,
|
---|
5440 | const string &prefix,
|
---|
5441 | VssRayContainer *visRays)
|
---|
5442 | {
|
---|
5443 | if (sortViewCells)
|
---|
5444 | {
|
---|
5445 | // sort view cells to visualize the largest view cells
|
---|
5446 | sort(mViewCells.begin(), mViewCells.end(), LargerRenderCost);
|
---|
5447 | }
|
---|
5448 |
|
---|
5449 | //////////
|
---|
5450 | //-- export some view cells for visualization
|
---|
5451 |
|
---|
5452 | ViewCell::NewMail();
|
---|
5453 | const int limit = min(maxViewCells, (int)mViewCells.size());
|
---|
5454 |
|
---|
5455 | for (int i = 0; i < limit; ++ i)
|
---|
5456 | {
|
---|
5457 | cout << "creating output for view cell " << i << " ... ";
|
---|
5458 |
|
---|
5459 | ViewCell *vc = sortViewCells ? // largest view cell pvs first?
|
---|
5460 | mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 0.5f)] : mViewCells[i];
|
---|
5461 |
|
---|
5462 | if (vc->Mailed() || vc->GetId() == OUT_OF_BOUNDS_ID)
|
---|
5463 | continue;
|
---|
5464 |
|
---|
5465 | vc->Mail();
|
---|
5466 |
|
---|
5467 | ObjectPvs pvs;
|
---|
5468 | mViewCellsTree->GetPvs(vc, pvs);
|
---|
5469 |
|
---|
5470 | char s[64]; sprintf(s, "%sviewcell%04d.wrl", prefix.c_str(), i);
|
---|
5471 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
5472 |
|
---|
5473 | const float pvsCost = mViewCellsTree->GetTrianglesInPvs(vc);
|
---|
5474 | cout << "view cell " << vc->GetId() << ": pvs cost=" << pvsCost << endl;
|
---|
5475 |
|
---|
5476 | if (exportRays)
|
---|
5477 | {
|
---|
5478 | ////////////
|
---|
5479 | //-- export rays piercing this view cell
|
---|
5480 |
|
---|
5481 | // take rays stored with the view cells during subdivision
|
---|
5482 | VssRayContainer vcRays;
|
---|
5483 | VssRayContainer collectRays;
|
---|
5484 |
|
---|
5485 | // collect initial view cells
|
---|
5486 | ViewCellContainer leaves;
|
---|
5487 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
5488 |
|
---|
5489 | ViewCellContainer::const_iterator vit, vit_end = leaves.end();
|
---|
5490 | for (vit = leaves.begin(); vit != vit_end; ++ vit)
|
---|
5491 | {
|
---|
5492 | BspLeaf *vcLeaf = static_cast<BspViewCell *>(*vit)->mLeaves[0];
|
---|
5493 | VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
|
---|
5494 |
|
---|
5495 | for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
5496 | {
|
---|
5497 | collectRays.push_back(*rit);
|
---|
5498 | }
|
---|
5499 | }
|
---|
5500 |
|
---|
5501 | const int raysOut = min((int)collectRays.size(), maxRays);
|
---|
5502 |
|
---|
5503 | // prepare some rays for visualization
|
---|
5504 | VssRayContainer::const_iterator rit, rit_end = collectRays.end();
|
---|
5505 | for (rit = collectRays.begin(); rit != rit_end; ++ rit)
|
---|
5506 | {
|
---|
5507 | const float p = RandomValue(0.0f, (float)collectRays.size());
|
---|
5508 |
|
---|
5509 | if (p < raysOut)
|
---|
5510 | {
|
---|
5511 | vcRays.push_back(*rit);
|
---|
5512 | }
|
---|
5513 | }
|
---|
5514 |
|
---|
5515 | exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
|
---|
5516 | }
|
---|
5517 |
|
---|
5518 | ////////////////
|
---|
5519 | //-- export view cell geometry
|
---|
5520 |
|
---|
5521 | exporter->SetWireframe();
|
---|
5522 |
|
---|
5523 | Material m;//= RandomMaterial();
|
---|
5524 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
5525 | exporter->SetForcedMaterial(m);
|
---|
5526 |
|
---|
5527 | ExportViewCellGeometry(exporter, vc, NULL, NULL);
|
---|
5528 | exporter->SetFilled();
|
---|
5529 |
|
---|
5530 | if (exportPvs)
|
---|
5531 | {
|
---|
5532 | Intersectable::NewMail();
|
---|
5533 | ObjectPvsIterator pit = pvs.GetIterator();
|
---|
5534 |
|
---|
5535 | cout << endl;
|
---|
5536 |
|
---|
5537 | // output PVS of view cell
|
---|
5538 | while (pit.HasMoreEntries())
|
---|
5539 | {
|
---|
5540 | Intersectable *intersect = pit.Next();
|
---|
5541 |
|
---|
5542 | if (!intersect->Mailed())
|
---|
5543 | {
|
---|
5544 | intersect->Mail();
|
---|
5545 |
|
---|
5546 | m = RandomMaterial();
|
---|
5547 | exporter->SetForcedMaterial(m);
|
---|
5548 | exporter->ExportIntersectable(intersect);
|
---|
5549 | }
|
---|
5550 | }
|
---|
5551 | cout << endl;
|
---|
5552 | }
|
---|
5553 |
|
---|
5554 | DEL_PTR(exporter);
|
---|
5555 | cout << "finished" << endl;
|
---|
5556 | }
|
---|
5557 | }
|
---|
5558 |
|
---|
5559 |
|
---|
5560 | void VspBspViewCellsManager::TestFilter(const ObjectContainer &objects)
|
---|
5561 | {
|
---|
5562 | Exporter *exporter = Exporter::GetExporter("filter.x3d");
|
---|
5563 |
|
---|
5564 | Vector3 bsize = mViewSpaceBox.Size();
|
---|
5565 | const Vector3 viewPoint(mViewSpaceBox.Center());
|
---|
5566 | float w = Magnitude(mViewSpaceBox.Size()) * mFilterWidth;
|
---|
5567 | const Vector3 width = Vector3(w);
|
---|
5568 |
|
---|
5569 | PrVs testPrVs;
|
---|
5570 |
|
---|
5571 | if (exporter)
|
---|
5572 | {
|
---|
5573 | ViewCellContainer viewCells;
|
---|
5574 |
|
---|
5575 | const AxisAlignedBox3 tbox = GetFilterBBox(viewPoint, mFilterWidth);
|
---|
5576 |
|
---|
5577 | GetPrVS(viewPoint, testPrVs, GetFilterWidth());
|
---|
5578 |
|
---|
5579 | exporter->SetWireframe();
|
---|
5580 |
|
---|
5581 | exporter->SetForcedMaterial(RgbColor(1,1,1));
|
---|
5582 | exporter->ExportBox(tbox);
|
---|
5583 |
|
---|
5584 | exporter->SetFilled();
|
---|
5585 |
|
---|
5586 | exporter->SetForcedMaterial(RgbColor(0,1,0));
|
---|
5587 | ExportViewCellGeometry(exporter, GetViewCell(viewPoint), NULL, NULL);
|
---|
5588 |
|
---|
5589 | //exporter->ResetForcedMaterial();
|
---|
5590 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
5591 | ExportViewCellGeometry(exporter, testPrVs.mViewCell, NULL, NULL);
|
---|
5592 |
|
---|
5593 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
5594 | exporter->ExportGeometry(objects);
|
---|
5595 |
|
---|
5596 | delete exporter;
|
---|
5597 | }
|
---|
5598 | }
|
---|
5599 |
|
---|
5600 |
|
---|
5601 | int VspBspViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
5602 | ViewCellContainer &viewCells) const
|
---|
5603 | {
|
---|
5604 | return mVspBspTree->ComputeBoxIntersections(box, viewCells);
|
---|
5605 | }
|
---|
5606 |
|
---|
5607 |
|
---|
5608 | int VspBspViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
5609 | const Vector3 &termination,
|
---|
5610 | ViewCellContainer &viewcells)
|
---|
5611 | {
|
---|
5612 | return mVspBspTree->CastLineSegment(origin, termination, viewcells);
|
---|
5613 | }
|
---|
5614 |
|
---|
5615 |
|
---|
5616 | bool VspBspViewCellsManager::LineSegmentIntersects(const Vector3 &origin,
|
---|
5617 | const Vector3 &termination,
|
---|
5618 | ViewCell *viewCell)
|
---|
5619 | {
|
---|
5620 | return false;
|
---|
5621 | }
|
---|
5622 |
|
---|
5623 |
|
---|
5624 | void VspBspViewCellsManager::VisualizeWithFromPointQueries()
|
---|
5625 | {
|
---|
5626 | int numSamples;
|
---|
5627 |
|
---|
5628 | Environment::GetSingleton()->GetIntValue("RenderSampler.samples", numSamples);
|
---|
5629 | cout << "samples" << numSamples << endl;
|
---|
5630 |
|
---|
5631 | vector<RenderCostSample> samples;
|
---|
5632 |
|
---|
5633 | if (!mPreprocessor->GetRenderer())
|
---|
5634 | return;
|
---|
5635 |
|
---|
5636 | //start the view point queries
|
---|
5637 | long startTime = GetTime();
|
---|
5638 | cout << "starting sampling of render cost ... ";
|
---|
5639 |
|
---|
5640 | mPreprocessor->GetRenderer()->SampleRenderCost(numSamples, samples, true);
|
---|
5641 |
|
---|
5642 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
5643 |
|
---|
5644 |
|
---|
5645 | // for each sample:
|
---|
5646 | // find view cells associated with the samples
|
---|
5647 | // store the sample pvs with the pvs associated with the view cell
|
---|
5648 | //
|
---|
5649 | // for each view cell:
|
---|
5650 | // compute difference point sampled pvs - view cell pvs
|
---|
5651 | // export geometry with color coded pvs difference
|
---|
5652 |
|
---|
5653 | std::map<ViewCell *, ObjectPvs> sampleMap;
|
---|
5654 |
|
---|
5655 | vector<RenderCostSample>::const_iterator rit, rit_end = samples.end();
|
---|
5656 |
|
---|
5657 | for (rit = samples.begin(); rit != rit_end; ++ rit)
|
---|
5658 | {
|
---|
5659 | RenderCostSample sample = *rit;
|
---|
5660 |
|
---|
5661 | ViewCell *vc = GetViewCell(sample.mPosition);
|
---|
5662 |
|
---|
5663 | std::map<ViewCell *, ObjectPvs>::iterator it = sampleMap.find(vc);
|
---|
5664 |
|
---|
5665 | if (it == sampleMap.end())
|
---|
5666 | {
|
---|
5667 | sampleMap[vc] = sample.mPvs;
|
---|
5668 | }
|
---|
5669 | else
|
---|
5670 | {
|
---|
5671 | (*it).second.MergeInPlace(sample.mPvs);
|
---|
5672 | }
|
---|
5673 | }
|
---|
5674 |
|
---|
5675 | // visualize the view cells
|
---|
5676 | std::map<ViewCell *, ObjectPvs>::const_iterator vit, vit_end = sampleMap.end();
|
---|
5677 |
|
---|
5678 | Material m;//= RandomMaterial();
|
---|
5679 |
|
---|
5680 | for (vit = sampleMap.begin(); vit != vit_end; ++ vit)
|
---|
5681 | {
|
---|
5682 | ViewCell *vc = (*vit).first;
|
---|
5683 |
|
---|
5684 | const int pvsVc = mViewCellsTree->GetPvsEntries(vc);
|
---|
5685 | const int pvsPtSamples = (*vit).second.GetSize();
|
---|
5686 |
|
---|
5687 | m.mDiffuseColor.r = (float) (pvsVc - pvsPtSamples);
|
---|
5688 | m.mDiffuseColor.b = 1.0f;
|
---|
5689 | //exporter->SetForcedMaterial(m);
|
---|
5690 | //ExportViewCellGeometry(exporter, vc, mClipPlaneForViz);
|
---|
5691 |
|
---|
5692 | /* // counting the pvss
|
---|
5693 | for (rit = samples.begin(); rit != rit_end; ++ rit)
|
---|
5694 | {
|
---|
5695 | RenderCostSample sample = *rit;
|
---|
5696 | ViewCell *vc = GetViewCell(sample.mPosition);
|
---|
5697 |
|
---|
5698 | AxisAlignedBox3 box(sample.mPosition - Vector3(1, 1, 1), sample.mPosition + Vector3(1, 1, 1));
|
---|
5699 | Mesh *hMesh = CreateMeshFromBox(box);
|
---|
5700 |
|
---|
5701 | DEL_PTR(hMesh);
|
---|
5702 | }
|
---|
5703 | */
|
---|
5704 | }
|
---|
5705 | }
|
---|
5706 |
|
---|
5707 |
|
---|
5708 | void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
5709 | ViewCell *vc,
|
---|
5710 | const AxisAlignedBox3 *sceneBox,
|
---|
5711 | const AxisAlignedPlane *clipPlane
|
---|
5712 | ) const
|
---|
5713 | {
|
---|
5714 | if (clipPlane)
|
---|
5715 | {
|
---|
5716 | const Plane3 plane = clipPlane->GetPlane();
|
---|
5717 |
|
---|
5718 | ViewCellContainer leaves;
|
---|
5719 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
5720 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
5721 |
|
---|
5722 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
5723 | {
|
---|
5724 | BspNodeGeometry geom;
|
---|
5725 | BspNodeGeometry front;
|
---|
5726 | BspNodeGeometry back;
|
---|
5727 |
|
---|
5728 | mVspBspTree->ConstructGeometry(*it, geom);
|
---|
5729 |
|
---|
5730 | const float eps = 0.0001f;
|
---|
5731 | const int cf = geom.Side(plane, eps);
|
---|
5732 |
|
---|
5733 | if (cf == -1)
|
---|
5734 | {
|
---|
5735 | exporter->ExportPolygons(geom.GetPolys());
|
---|
5736 | }
|
---|
5737 | else if (cf == 0)
|
---|
5738 | {
|
---|
5739 | geom.SplitGeometry(front,
|
---|
5740 | back,
|
---|
5741 | plane,
|
---|
5742 | mViewSpaceBox,
|
---|
5743 | eps);
|
---|
5744 |
|
---|
5745 | if (back.Valid())
|
---|
5746 | {
|
---|
5747 | exporter->ExportPolygons(back.GetPolys());
|
---|
5748 | }
|
---|
5749 | }
|
---|
5750 | }
|
---|
5751 | }
|
---|
5752 | else
|
---|
5753 | {
|
---|
5754 | // export mesh if available
|
---|
5755 | // TODO: some bug here?
|
---|
5756 | if (1 && vc->GetMesh())
|
---|
5757 | {
|
---|
5758 | exporter->ExportMesh(vc->GetMesh());
|
---|
5759 | }
|
---|
5760 | else
|
---|
5761 | {
|
---|
5762 | BspNodeGeometry geom;
|
---|
5763 | mVspBspTree->ConstructGeometry(vc, geom);
|
---|
5764 | exporter->ExportPolygons(geom.GetPolys());
|
---|
5765 | }
|
---|
5766 | }
|
---|
5767 | }
|
---|
5768 |
|
---|
5769 |
|
---|
5770 | int VspBspViewCellsManager::GetMaxTreeDiff(ViewCell *vc) const
|
---|
5771 | {
|
---|
5772 | ViewCellContainer leaves;
|
---|
5773 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
5774 |
|
---|
5775 | int maxDist = 0;
|
---|
5776 |
|
---|
5777 | // compute max height difference
|
---|
5778 | for (int i = 0; i < (int)leaves.size(); ++ i)
|
---|
5779 | {
|
---|
5780 | for (int j = 0; j < (int)leaves.size(); ++ j)
|
---|
5781 | {
|
---|
5782 | BspLeaf *leaf = static_cast<BspViewCell *>(leaves[i])->mLeaves[0];
|
---|
5783 |
|
---|
5784 | if (i != j)
|
---|
5785 | {
|
---|
5786 | BspLeaf *leaf2 =static_cast<BspViewCell *>(leaves[j])->mLeaves[0];
|
---|
5787 | const int dist = mVspBspTree->TreeDistance(leaf, leaf2);
|
---|
5788 |
|
---|
5789 | if (dist > maxDist)
|
---|
5790 | maxDist = dist;
|
---|
5791 | }
|
---|
5792 | }
|
---|
5793 | }
|
---|
5794 |
|
---|
5795 | return maxDist;
|
---|
5796 | }
|
---|
5797 |
|
---|
5798 |
|
---|
5799 | ViewCell *VspBspViewCellsManager::GetViewCell(const Vector3 &point, const bool active) const
|
---|
5800 | {
|
---|
5801 | if (!ViewCellsConstructed())
|
---|
5802 | return NULL;
|
---|
5803 |
|
---|
5804 | if (!mViewSpaceBox.IsInside(point))
|
---|
5805 | return NULL;
|
---|
5806 |
|
---|
5807 | return mVspBspTree->GetViewCell(point, active);
|
---|
5808 | }
|
---|
5809 |
|
---|
5810 |
|
---|
5811 | void VspBspViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
5812 | {
|
---|
5813 | BspNodeGeometry geom;
|
---|
5814 | mVspBspTree->ConstructGeometry(vc, geom);
|
---|
5815 |
|
---|
5816 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
5817 |
|
---|
5818 | IncludeNodeGeomInMesh(geom, *mesh);
|
---|
5819 | mesh->ComputeBoundingBox();
|
---|
5820 |
|
---|
5821 | vc->SetMesh(mesh);
|
---|
5822 | }
|
---|
5823 |
|
---|
5824 |
|
---|
5825 | int VspBspViewCellsManager::CastBeam(Beam &beam)
|
---|
5826 | {
|
---|
5827 | return mVspBspTree->CastBeam(beam);
|
---|
5828 | }
|
---|
5829 |
|
---|
5830 |
|
---|
5831 | void VspBspViewCellsManager::Finalize(ViewCell *viewCell,
|
---|
5832 | const bool createMesh)
|
---|
5833 | {
|
---|
5834 | float area = 0;
|
---|
5835 | float volume = 0;
|
---|
5836 |
|
---|
5837 | ViewCellContainer leaves;
|
---|
5838 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
5839 |
|
---|
5840 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
5841 |
|
---|
5842 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
5843 | {
|
---|
5844 | BspNodeGeometry geom;
|
---|
5845 | mVspBspTree->ConstructGeometry(*it, geom);
|
---|
5846 |
|
---|
5847 | const float lVol = geom.GetVolume();
|
---|
5848 | const float lArea = geom.GetArea();
|
---|
5849 |
|
---|
5850 | area += lArea;
|
---|
5851 | volume += lVol;
|
---|
5852 |
|
---|
5853 | if (createMesh)
|
---|
5854 | CreateMesh(*it);
|
---|
5855 | }
|
---|
5856 |
|
---|
5857 | viewCell->SetVolume(volume);
|
---|
5858 | viewCell->SetArea(area);
|
---|
5859 | }
|
---|
5860 |
|
---|
5861 |
|
---|
5862 | void VspBspViewCellsManager::TestSubdivision()
|
---|
5863 | {
|
---|
5864 | ViewCellContainer leaves;
|
---|
5865 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
5866 |
|
---|
5867 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
5868 |
|
---|
5869 | const float vol = mViewSpaceBox.GetVolume();
|
---|
5870 | float subdivVol = 0;
|
---|
5871 | float newVol = 0;
|
---|
5872 |
|
---|
5873 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
5874 | {
|
---|
5875 | BspNodeGeometry geom;
|
---|
5876 | mVspBspTree->ConstructGeometry(*it, geom);
|
---|
5877 |
|
---|
5878 | const float lVol = geom.GetVolume();
|
---|
5879 |
|
---|
5880 | newVol += lVol;
|
---|
5881 | subdivVol += (*it)->GetVolume();
|
---|
5882 |
|
---|
5883 | float thres = 0.9f;
|
---|
5884 | if ((lVol < ((*it)->GetVolume() * thres)) || (lVol * thres > ((*it)->GetVolume())))
|
---|
5885 | Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
|
---|
5886 | }
|
---|
5887 |
|
---|
5888 | Debug << "exact volume: " << vol << endl;
|
---|
5889 | Debug << "subdivision volume: " << subdivVol << endl;
|
---|
5890 | Debug << "new volume: " << newVol << endl;
|
---|
5891 | }
|
---|
5892 |
|
---|
5893 |
|
---|
5894 | void VspBspViewCellsManager::PrepareLoadedViewCells()
|
---|
5895 | {
|
---|
5896 | // TODO: do I still need this here?
|
---|
5897 | if (0) mVspBspTree->RepairViewCellsLeafLists();
|
---|
5898 | }
|
---|
5899 |
|
---|
5900 |
|
---|
5901 |
|
---|
5902 | /************************************************************************/
|
---|
5903 | /* VspOspViewCellsManager implementation */
|
---|
5904 | /************************************************************************/
|
---|
5905 |
|
---|
5906 |
|
---|
5907 | VspOspViewCellsManager::VspOspViewCellsManager(ViewCellsTree *vcTree,
|
---|
5908 | const string &hierarchyType)
|
---|
5909 | : ViewCellsManager(vcTree)
|
---|
5910 | {
|
---|
5911 | Environment::GetSingleton()->GetIntValue("Hierarchy.Construction.samples", mInitialSamples);
|
---|
5912 | Environment::GetSingleton()->GetBoolValue("ViewCells.compressObjects", mCompressObjects);
|
---|
5913 |
|
---|
5914 | Debug << "compressing objects: " << mCompressObjects << endl;
|
---|
5915 | cout << "compressing objects: " << mCompressObjects << endl;
|
---|
5916 |
|
---|
5917 | mHierarchyManager = CreateHierarchyManager(hierarchyType);
|
---|
5918 |
|
---|
5919 | mHierarchyManager->SetViewCellsManager(this);
|
---|
5920 | mHierarchyManager->SetViewCellsTree(mViewCellsTree);
|
---|
5921 | }
|
---|
5922 |
|
---|
5923 |
|
---|
5924 | VspOspViewCellsManager::VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm)
|
---|
5925 | : ViewCellsManager(vcTree), mHierarchyManager(hm)
|
---|
5926 | {
|
---|
5927 | Environment::GetSingleton()->GetIntValue("Hierarchy.Construction.samples", mInitialSamples);
|
---|
5928 | Environment::GetSingleton()->GetBoolValue("ViewCells.compressObjects", mCompressObjects);
|
---|
5929 |
|
---|
5930 | Debug << "compressing objects: " << mCompressObjects << endl;
|
---|
5931 | cout << "compressing objects: " << mCompressObjects << endl;
|
---|
5932 |
|
---|
5933 | mHierarchyManager->SetViewCellsManager(this);
|
---|
5934 | mHierarchyManager->SetViewCellsTree(mViewCellsTree);
|
---|
5935 | }
|
---|
5936 |
|
---|
5937 |
|
---|
5938 | Intersectable *VspOspViewCellsManager::GetIntersectable(const VssRay &ray,
|
---|
5939 | const bool isTermination) const
|
---|
5940 | {
|
---|
5941 | if (mUseKdPvs)
|
---|
5942 | return ViewCellsManager::GetIntersectable(ray, isTermination);
|
---|
5943 | else
|
---|
5944 | return mHierarchyManager->GetIntersectable(ray, isTermination);
|
---|
5945 | }
|
---|
5946 |
|
---|
5947 |
|
---|
5948 | HierarchyManager *VspOspViewCellsManager::CreateHierarchyManager(const string &hierarchyType)
|
---|
5949 | {
|
---|
5950 | HierarchyManager *hierarchyManager;
|
---|
5951 |
|
---|
5952 | if (strcmp(hierarchyType.c_str(), "osp") == 0)
|
---|
5953 | {
|
---|
5954 | Debug << "hierarchy manager: osp" << endl;
|
---|
5955 | hierarchyManager = new HierarchyManager(HierarchyManager::KD_BASED_OBJ_SUBDIV);
|
---|
5956 | }
|
---|
5957 | else if (strcmp(hierarchyType.c_str(), "bvh") == 0)
|
---|
5958 | {
|
---|
5959 | Debug << "hierarchy manager: bvh" << endl;
|
---|
5960 | hierarchyManager = new HierarchyManager(HierarchyManager::BV_BASED_OBJ_SUBDIV);
|
---|
5961 | }
|
---|
5962 | else // only view space partition
|
---|
5963 | {
|
---|
5964 | Debug << "hierarchy manager: obj" << endl;
|
---|
5965 | hierarchyManager = new HierarchyManager(HierarchyManager::NO_OBJ_SUBDIV);
|
---|
5966 | }
|
---|
5967 |
|
---|
5968 | return hierarchyManager;
|
---|
5969 | }
|
---|
5970 |
|
---|
5971 |
|
---|
5972 | VspOspViewCellsManager::~VspOspViewCellsManager()
|
---|
5973 | {
|
---|
5974 | DEL_PTR(mHierarchyManager);
|
---|
5975 | }
|
---|
5976 |
|
---|
5977 |
|
---|
5978 | float VspOspViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
5979 | {
|
---|
5980 | return GetVolume(viewCell) / mViewSpaceBox.GetVolume();
|
---|
5981 | }
|
---|
5982 |
|
---|
5983 |
|
---|
5984 | void VspOspViewCellsManager::CollectViewCells()
|
---|
5985 | {
|
---|
5986 | // view cells tree constructed
|
---|
5987 | if (!ViewCellsTreeConstructed())
|
---|
5988 | {
|
---|
5989 | mHierarchyManager->GetVspTree()->CollectViewCells(mViewCells, false);
|
---|
5990 | }
|
---|
5991 | else
|
---|
5992 | { // we can use the view cells tree hierarchy to get the right set
|
---|
5993 | mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
|
---|
5994 | }
|
---|
5995 | }
|
---|
5996 |
|
---|
5997 |
|
---|
5998 | bool VspOspViewCellsManager::ViewCellsConstructed() const
|
---|
5999 | {
|
---|
6000 | return mHierarchyManager->GetVspTree()->GetRoot() != NULL;
|
---|
6001 | }
|
---|
6002 |
|
---|
6003 |
|
---|
6004 | ViewCell *VspOspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
6005 | {
|
---|
6006 | return new VspViewCell(mesh);
|
---|
6007 | }
|
---|
6008 |
|
---|
6009 |
|
---|
6010 | int VspOspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
6011 | const VssRayContainer &rays)
|
---|
6012 | {
|
---|
6013 | mMaxPvsSize = (int)(mMaxPvsRatio * (float)objects.size());
|
---|
6014 |
|
---|
6015 | // skip rest if view cells were already constructed
|
---|
6016 | if (ViewCellsConstructed())
|
---|
6017 | return 0;
|
---|
6018 |
|
---|
6019 | int sampleContributions = 0;
|
---|
6020 | VssRayContainer sampleRays;
|
---|
6021 |
|
---|
6022 | int limit = min (mInitialSamples, (int)rays.size());
|
---|
6023 |
|
---|
6024 | VssRayContainer constructionRays;
|
---|
6025 | VssRayContainer savedRays;
|
---|
6026 |
|
---|
6027 | Debug << "samples used for vsp bsp subdivision: " << mInitialSamples
|
---|
6028 | << ", actual rays: " << (int)rays.size() << endl;
|
---|
6029 |
|
---|
6030 | GetRaySets(rays, mInitialSamples, constructionRays, &savedRays);
|
---|
6031 |
|
---|
6032 | Debug << "initial rays used for construction: " << (int)constructionRays.size() << endl;
|
---|
6033 | Debug << "saved rays: " << (int)savedRays.size() << endl;
|
---|
6034 |
|
---|
6035 | mHierarchyManager->Construct(constructionRays, objects, &mViewSpaceBox);
|
---|
6036 |
|
---|
6037 | #if TEST_EVALUATION
|
---|
6038 | VssRayContainer::const_iterator tit, tit_end = constructionRays.end();
|
---|
6039 | for (tit = constructionRays.begin(); tit != tit_end; ++ tit)
|
---|
6040 | {
|
---|
6041 | storedRays.push_back(new VssRay(*(*tit)));
|
---|
6042 | }
|
---|
6043 | #endif
|
---|
6044 |
|
---|
6045 | /////////////////////////
|
---|
6046 | //-- print satistics for subdivision and view cells
|
---|
6047 |
|
---|
6048 | Debug << endl << endl << *mHierarchyManager << endl;
|
---|
6049 |
|
---|
6050 | ResetViewCells();
|
---|
6051 | //Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
6052 |
|
---|
6053 | //////////////
|
---|
6054 | //-- recast rest of rays
|
---|
6055 |
|
---|
6056 | const long startTime = GetTime();
|
---|
6057 | cout << "Computing remaining ray contributions ... ";
|
---|
6058 |
|
---|
6059 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
6060 | ComputeSampleContributions(savedRays, true, false);
|
---|
6061 |
|
---|
6062 | Debug << "finished computing remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
6063 | << " secs" << endl;
|
---|
6064 |
|
---|
6065 | if (0)
|
---|
6066 | {
|
---|
6067 | // real meshes are constructed at this stage
|
---|
6068 | cout << "finalizing view cells ... ";
|
---|
6069 | FinalizeViewCells(true);
|
---|
6070 | cout << "finished" << endl;
|
---|
6071 | }
|
---|
6072 |
|
---|
6073 | return sampleContributions;
|
---|
6074 | }
|
---|
6075 |
|
---|
6076 |
|
---|
6077 | int VspOspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
6078 | const VssRayContainer &rays)
|
---|
6079 | {
|
---|
6080 | if (!ViewCellsConstructed())
|
---|
6081 | {
|
---|
6082 | Debug << "post process error: no view cells constructed" << endl;
|
---|
6083 | return 0;
|
---|
6084 | }
|
---|
6085 |
|
---|
6086 | // if view cells were already constructed before post processing step
|
---|
6087 | // (e.g., because they were loaded), we are finished
|
---|
6088 | if (mViewCellsFinished)
|
---|
6089 | {
|
---|
6090 | FinalizeViewCells(true);
|
---|
6091 | EvaluateViewCellsStats();
|
---|
6092 |
|
---|
6093 | return 0;
|
---|
6094 | }
|
---|
6095 |
|
---|
6096 | // check if new view cells turned invalid
|
---|
6097 | int minPvs, maxPvs;
|
---|
6098 |
|
---|
6099 | if (0)
|
---|
6100 | {
|
---|
6101 | minPvs = mMinPvsSize;
|
---|
6102 | maxPvs = mMaxPvsSize;
|
---|
6103 | }
|
---|
6104 | else
|
---|
6105 | {
|
---|
6106 | // problem matt: why did I start here from zero?
|
---|
6107 | minPvs = 0;
|
---|
6108 | maxPvs = mMaxPvsSize;
|
---|
6109 | }
|
---|
6110 |
|
---|
6111 | Debug << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
6112 | cout << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
6113 |
|
---|
6114 | SetValidity(minPvs, maxPvs);
|
---|
6115 |
|
---|
6116 |
|
---|
6117 | // area is not up to date, has to be recomputed
|
---|
6118 | mTotalAreaValid = false;
|
---|
6119 | VssRayContainer postProcessRays;
|
---|
6120 | GetRaySets(rays, mPostProcessSamples, postProcessRays);
|
---|
6121 |
|
---|
6122 | Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
|
---|
6123 |
|
---|
6124 |
|
---|
6125 | // compute tree by merging the nodes of the spatial hierarchy
|
---|
6126 | ViewCell *root = ConstructSpatialMergeTree(mHierarchyManager->GetVspTree()->GetRoot());
|
---|
6127 | mViewCellsTree->SetRoot(root);
|
---|
6128 |
|
---|
6129 | //////////////////////////
|
---|
6130 | //-- update pvs up to the root of the hierarchy
|
---|
6131 |
|
---|
6132 | ObjectPvs pvs;
|
---|
6133 | UpdatePvsForEvaluation(root, pvs);
|
---|
6134 |
|
---|
6135 |
|
---|
6136 | //////////////////////
|
---|
6137 | //-- render simulation after merge + refine
|
---|
6138 |
|
---|
6139 | cout << "\nview cells partition render time before compress" << endl << endl;
|
---|
6140 | static_cast<RenderSimulator *>(mRenderer)->RenderScene();
|
---|
6141 | SimulationStatistics ss;
|
---|
6142 | static_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
6143 | cout << ss << endl;
|
---|
6144 |
|
---|
6145 |
|
---|
6146 | mHierarchyManager->CreateUniqueObjectIds();
|
---|
6147 |
|
---|
6148 | ///////////
|
---|
6149 | //-- compression
|
---|
6150 |
|
---|
6151 | if (0) CompressViewCells();
|
---|
6152 |
|
---|
6153 | /////////////
|
---|
6154 | //-- some tasks still to do on the view cells:
|
---|
6155 | //-- Compute meshes from view cell geometry, evaluate volume and / or area
|
---|
6156 |
|
---|
6157 | if (1) FinalizeViewCells(true);
|
---|
6158 |
|
---|
6159 | return 0;
|
---|
6160 | }
|
---|
6161 |
|
---|
6162 |
|
---|
6163 | int VspOspViewCellsManager::GetType() const
|
---|
6164 | {
|
---|
6165 | return VSP_OSP;
|
---|
6166 | }
|
---|
6167 |
|
---|
6168 |
|
---|
6169 | ViewCell *VspOspViewCellsManager::ConstructSpatialMergeTree(VspNode *root)
|
---|
6170 | {
|
---|
6171 | // terminate recursion
|
---|
6172 | if (root->IsLeaf())
|
---|
6173 | {
|
---|
6174 | VspLeaf *leaf = static_cast<VspLeaf *>(root);
|
---|
6175 | leaf->GetViewCell()->SetMergeCost(0.0f);
|
---|
6176 | return leaf->GetViewCell();
|
---|
6177 | }
|
---|
6178 |
|
---|
6179 | VspInterior *interior = static_cast<VspInterior *>(root);
|
---|
6180 | ViewCellInterior *viewCellInterior = new ViewCellInterior();
|
---|
6181 |
|
---|
6182 | // evaluate merge cost for priority traversal
|
---|
6183 | const float mergeCost = -(float)root->mTimeStamp;
|
---|
6184 | viewCellInterior->SetMergeCost(mergeCost);
|
---|
6185 |
|
---|
6186 | float volume = 0;
|
---|
6187 |
|
---|
6188 | VspNode *front = interior->GetFront();
|
---|
6189 | VspNode *back = interior->GetBack();
|
---|
6190 |
|
---|
6191 | ObjectPvs frontPvs, backPvs;
|
---|
6192 |
|
---|
6193 | /////////
|
---|
6194 | //-- recursivly compute child hierarchies
|
---|
6195 |
|
---|
6196 | ViewCell *backVc = ConstructSpatialMergeTree(back);
|
---|
6197 | ViewCell *frontVc = ConstructSpatialMergeTree(front);
|
---|
6198 |
|
---|
6199 | viewCellInterior->SetupChildLink(backVc);
|
---|
6200 | viewCellInterior->SetupChildLink(frontVc);
|
---|
6201 |
|
---|
6202 | volume += backVc->GetVolume();
|
---|
6203 | volume += frontVc->GetVolume();
|
---|
6204 |
|
---|
6205 | viewCellInterior->SetVolume(volume);
|
---|
6206 |
|
---|
6207 | return viewCellInterior;
|
---|
6208 | }
|
---|
6209 |
|
---|
6210 |
|
---|
6211 | bool VspOspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
|
---|
6212 | {
|
---|
6213 | if (!ViewCellsConstructed())
|
---|
6214 | return ViewCellsManager::GetViewPoint(viewPoint);
|
---|
6215 |
|
---|
6216 | // TODO: set reasonable limit
|
---|
6217 | const int limit = 20;
|
---|
6218 |
|
---|
6219 | for (int i = 0; i < limit; ++ i)
|
---|
6220 | {
|
---|
6221 | viewPoint = mViewSpaceBox.GetRandomPoint();
|
---|
6222 |
|
---|
6223 | if (mHierarchyManager->GetVspTree()->ViewPointValid(viewPoint))
|
---|
6224 | {
|
---|
6225 | return true;
|
---|
6226 | }
|
---|
6227 | }
|
---|
6228 |
|
---|
6229 | Debug << "failed to find valid view point, taking " << viewPoint << endl;
|
---|
6230 | return false;
|
---|
6231 | }
|
---|
6232 |
|
---|
6233 |
|
---|
6234 | void VspOspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
6235 | ViewCell *vc,
|
---|
6236 | const AxisAlignedBox3 *sceneBox,
|
---|
6237 | const AxisAlignedPlane *clipPlane
|
---|
6238 | ) const
|
---|
6239 | {
|
---|
6240 | Plane3 plane;
|
---|
6241 | if (clipPlane)
|
---|
6242 | {
|
---|
6243 | // arbitrary plane definition
|
---|
6244 | plane = clipPlane->GetPlane();
|
---|
6245 | }
|
---|
6246 |
|
---|
6247 | ViewCellContainer leaves;
|
---|
6248 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
6249 |
|
---|
6250 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
6251 |
|
---|
6252 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
6253 | {
|
---|
6254 | VspViewCell *vspVc = static_cast<VspViewCell *>(*it);
|
---|
6255 | VspLeaf *l = vspVc->mLeaves[0];
|
---|
6256 |
|
---|
6257 | const AxisAlignedBox3 box =
|
---|
6258 | mHierarchyManager->GetVspTree()->GetBoundingBox(vspVc->mLeaves[0]);
|
---|
6259 |
|
---|
6260 | if (sceneBox && !Overlap(*sceneBox, box))
|
---|
6261 | continue;
|
---|
6262 |
|
---|
6263 | if (clipPlane)
|
---|
6264 | {
|
---|
6265 | if (box.Side(plane) == -1)
|
---|
6266 | {
|
---|
6267 | exporter->ExportBox(box);
|
---|
6268 | }
|
---|
6269 | else if (box.Side(plane) == 0)
|
---|
6270 | {
|
---|
6271 | // intersection
|
---|
6272 | AxisAlignedBox3 fbox, bbox;
|
---|
6273 | box.Split(clipPlane->mAxis, clipPlane->mPosition, fbox, bbox);
|
---|
6274 | exporter->ExportBox(bbox);
|
---|
6275 | }
|
---|
6276 | }
|
---|
6277 | else
|
---|
6278 | {
|
---|
6279 | exporter->ExportBox(box);
|
---|
6280 | }
|
---|
6281 | }
|
---|
6282 | }
|
---|
6283 |
|
---|
6284 |
|
---|
6285 | bool VspOspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
|
---|
6286 | {
|
---|
6287 | // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
|
---|
6288 | // validy update in preprocessor for all managers)
|
---|
6289 | return ViewCellsManager::ViewPointValid(viewPoint);
|
---|
6290 |
|
---|
6291 | // return mViewSpaceBox.IsInside(viewPoint) &&
|
---|
6292 | // mVspTree->ViewPointValid(viewPoint);
|
---|
6293 | }
|
---|
6294 |
|
---|
6295 |
|
---|
6296 | float VspOspViewCellsManager::UpdateObjectCosts()
|
---|
6297 | {
|
---|
6298 | float maxRenderCost = 0;
|
---|
6299 |
|
---|
6300 | cout << "updating object pvs cost ... ";
|
---|
6301 | const long startTime = GetTime();
|
---|
6302 |
|
---|
6303 | ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
|
---|
6304 |
|
---|
6305 | Intersectable::NewMail();
|
---|
6306 |
|
---|
6307 | const float invViewSpaceVol = 1.0f / GetViewSpaceBox().GetVolume();
|
---|
6308 |
|
---|
6309 | for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
6310 | {
|
---|
6311 | ViewCell *vc = *vit;
|
---|
6312 |
|
---|
6313 | ObjectPvsIterator pit = vc->GetPvs().GetIterator();
|
---|
6314 |
|
---|
6315 | // output PVS of view cell
|
---|
6316 | while (pit.HasMoreEntries())
|
---|
6317 | {
|
---|
6318 | Intersectable *obj = pit.Next();
|
---|
6319 |
|
---|
6320 | BvhNode *node = static_cast<BvhNode *>(obj);
|
---|
6321 |
|
---|
6322 | // hack!!
|
---|
6323 | if (!node->IsLeaf())
|
---|
6324 | {
|
---|
6325 | cout << "error, can only process leaves" << endl;
|
---|
6326 | return 0;
|
---|
6327 | }
|
---|
6328 |
|
---|
6329 | if (!node->Mailed())
|
---|
6330 | {
|
---|
6331 | node->Mail();
|
---|
6332 | node->mRenderCost = 0;
|
---|
6333 | }
|
---|
6334 |
|
---|
6335 | const float rc = (float)((BvhLeaf *)node)->mObjects.size();
|
---|
6336 |
|
---|
6337 | node->mRenderCost += rc * vc->GetVolume() * invViewSpaceVol;
|
---|
6338 |
|
---|
6339 | if (node->mRenderCost > maxRenderCost)
|
---|
6340 | maxRenderCost = node->mRenderCost;
|
---|
6341 | }
|
---|
6342 | }
|
---|
6343 |
|
---|
6344 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3f << " secs" << endl;
|
---|
6345 |
|
---|
6346 | return maxRenderCost;
|
---|
6347 | }
|
---|
6348 |
|
---|
6349 |
|
---|
6350 | void VspOspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
6351 | const VssRayContainer &sampleRays)
|
---|
6352 | {
|
---|
6353 | if (!ViewCellsConstructed())
|
---|
6354 | return;
|
---|
6355 |
|
---|
6356 | VssRayContainer visRays;
|
---|
6357 | GetRaySets(sampleRays, mVisualizationSamples, visRays);
|
---|
6358 |
|
---|
6359 | ////////////
|
---|
6360 | //-- export final view cells
|
---|
6361 |
|
---|
6362 | Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
|
---|
6363 |
|
---|
6364 | Vector3 scale(0.9f, 0.9f, 0.9f);
|
---|
6365 | //Vector3 scale(1.0f, 1.0f, 1.0f);
|
---|
6366 |
|
---|
6367 | if (exporter)
|
---|
6368 | {
|
---|
6369 | // clamp to a scene boudning box
|
---|
6370 | if (CLAMP_TO_BOX)
|
---|
6371 | exporter->mClampToBox = true;
|
---|
6372 |
|
---|
6373 | const long starttime = GetTime();
|
---|
6374 | cout << "exporting final view cells (after initial construction + post process) ... " << endl;
|
---|
6375 |
|
---|
6376 | // matt: hack for clamping scene
|
---|
6377 | AxisAlignedBox3 bbox = mViewSpaceBox;
|
---|
6378 | bbox.Scale(scale);
|
---|
6379 |
|
---|
6380 | if (1 && mExportRays)
|
---|
6381 | {
|
---|
6382 | exporter->ExportRays(visRays, RgbColor(0, 1, 0));
|
---|
6383 | }
|
---|
6384 |
|
---|
6385 | // hack color code
|
---|
6386 | const int savedColorCode = mColorCode;
|
---|
6387 |
|
---|
6388 | EvaluateViewCellsStats();
|
---|
6389 | const int colorCode = 0;
|
---|
6390 |
|
---|
6391 | const float maxRenderCost = -1;//UpdateObjectCosts();
|
---|
6392 | const bool exportBounds = false;
|
---|
6393 |
|
---|
6394 | //cout << "maxRenderCost: " << maxRenderCost << endl;
|
---|
6395 | if (1)
|
---|
6396 | mHierarchyManager->ExportObjectSpaceHierarchy(exporter,
|
---|
6397 | objects,
|
---|
6398 | CLAMP_TO_BOX ? &bbox : NULL,
|
---|
6399 | maxRenderCost,
|
---|
6400 | exportBounds);
|
---|
6401 |
|
---|
6402 | //ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, mColorCode, GetClipPlane());
|
---|
6403 | ExportViewCellsForViz(exporter, NULL, colorCode, GetClipPlane());
|
---|
6404 |
|
---|
6405 | delete exporter;
|
---|
6406 |
|
---|
6407 | cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
|
---|
6408 | }
|
---|
6409 |
|
---|
6410 | if (1)
|
---|
6411 | {
|
---|
6412 | exporter = Exporter::GetExporter("final_object_partition.wrl");
|
---|
6413 |
|
---|
6414 | if (exporter)
|
---|
6415 | {
|
---|
6416 | if (CLAMP_TO_BOX)
|
---|
6417 | {
|
---|
6418 | exporter->mClampToBox = true;
|
---|
6419 | }
|
---|
6420 |
|
---|
6421 | const long starttime = GetTime();
|
---|
6422 | cout << "exporting final objects (after initial construction + post process) ... ";
|
---|
6423 |
|
---|
6424 | // matt: hack for clamping scene
|
---|
6425 | AxisAlignedBox3 bbox = mViewSpaceBox;
|
---|
6426 | bbox.Scale(scale);
|
---|
6427 |
|
---|
6428 | // hack color code (show pvs size)
|
---|
6429 | const int savedColorCode = mColorCode;
|
---|
6430 |
|
---|
6431 | EvaluateViewCellsStats();
|
---|
6432 | mColorCode = 1; // 0 = random, 1 = export pvs
|
---|
6433 |
|
---|
6434 | // don't visualize render cost
|
---|
6435 | const float maxRenderCost = -1;
|
---|
6436 | //const bool exportBounds = true;
|
---|
6437 | const bool exportBounds = false;
|
---|
6438 |
|
---|
6439 | mHierarchyManager->ExportObjectSpaceHierarchy(exporter,
|
---|
6440 | objects,
|
---|
6441 | CLAMP_TO_BOX ? &bbox : NULL,
|
---|
6442 | maxRenderCost,
|
---|
6443 | exportBounds);
|
---|
6444 |
|
---|
6445 |
|
---|
6446 | delete exporter;
|
---|
6447 |
|
---|
6448 | cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
|
---|
6449 | mColorCode = savedColorCode;
|
---|
6450 | }
|
---|
6451 | }
|
---|
6452 |
|
---|
6453 | // export some view cells
|
---|
6454 | int leafOut;
|
---|
6455 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
|
---|
6456 |
|
---|
6457 | const bool sortViewCells = false;
|
---|
6458 | const bool exportPvs = true;
|
---|
6459 | const bool exportRays = true;
|
---|
6460 | const int raysOut = 100;
|
---|
6461 |
|
---|
6462 | ExportSingleViewCells(objects,
|
---|
6463 | leafOut,
|
---|
6464 | sortViewCells,
|
---|
6465 | exportPvs,
|
---|
6466 | exportRays,
|
---|
6467 | raysOut,
|
---|
6468 | "");
|
---|
6469 | }
|
---|
6470 |
|
---|
6471 |
|
---|
6472 | void VspOspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
|
---|
6473 | const int maxViewCells,
|
---|
6474 | const bool sortViewCells,
|
---|
6475 | const bool exportPvs,
|
---|
6476 | const bool exportRays,
|
---|
6477 | const int maxRays,
|
---|
6478 | const string &prefix,
|
---|
6479 | VssRayContainer *visRays)
|
---|
6480 | {
|
---|
6481 | if (sortViewCells)
|
---|
6482 | {
|
---|
6483 | // sort view cells to visualize the view cells with highest render cost
|
---|
6484 | sort(mViewCells.begin(), mViewCells.end(), LargerRenderCost);
|
---|
6485 | }
|
---|
6486 |
|
---|
6487 | ViewCell::NewMail();
|
---|
6488 | const int limit = min(maxViewCells, (int)mViewCells.size());
|
---|
6489 |
|
---|
6490 | cout << "\nExporting " << limit << " single view cells: " << endl;
|
---|
6491 |
|
---|
6492 | for (int i = 0; i < limit; ++ i)
|
---|
6493 | {
|
---|
6494 | cout << "creating output for view cell " << i << " ... ";
|
---|
6495 |
|
---|
6496 | // largest view cell pvs first of random view cell
|
---|
6497 | ViewCell *vc = sortViewCells ?
|
---|
6498 | mViewCells[i] : mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 1)];
|
---|
6499 |
|
---|
6500 | if (vc->Mailed()) // view cell already processed
|
---|
6501 | continue;
|
---|
6502 |
|
---|
6503 | vc->Mail();
|
---|
6504 |
|
---|
6505 | ObjectPvs pvs;
|
---|
6506 | mViewCellsTree->GetPvs(vc, pvs);
|
---|
6507 |
|
---|
6508 | char s[64]; sprintf(s, "%sviewcell%04d.wrl", prefix.c_str(), i);
|
---|
6509 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
6510 |
|
---|
6511 | cout << "view cell " << vc->GetId() << ": pvs cost=" << mViewCellsTree->GetTrianglesInPvs(vc) << endl;
|
---|
6512 |
|
---|
6513 | if (exportPvs)
|
---|
6514 | {
|
---|
6515 | Material m;
|
---|
6516 | Intersectable::NewMail();
|
---|
6517 |
|
---|
6518 | ObjectPvsIterator pit = pvs.GetIterator();
|
---|
6519 |
|
---|
6520 | // output PVS of view cell
|
---|
6521 | while (pit.HasMoreEntries())
|
---|
6522 | {
|
---|
6523 | Intersectable *intersect = pit.Next();
|
---|
6524 |
|
---|
6525 | if (!intersect->Mailed())
|
---|
6526 | {
|
---|
6527 | m = RandomMaterial();
|
---|
6528 | exporter->SetForcedMaterial(m);
|
---|
6529 |
|
---|
6530 | exporter->ExportIntersectable(intersect);
|
---|
6531 | intersect->Mail();
|
---|
6532 | }
|
---|
6533 | }
|
---|
6534 | }
|
---|
6535 |
|
---|
6536 | if (exportRays)
|
---|
6537 | {
|
---|
6538 | ////////////
|
---|
6539 | //-- export the sample rays
|
---|
6540 |
|
---|
6541 | // output rays stored with the view cells during subdivision
|
---|
6542 | VssRayContainer vcRays;
|
---|
6543 | VssRayContainer collectRays;
|
---|
6544 |
|
---|
6545 | // collect intial view cells
|
---|
6546 | ViewCellContainer leaves;
|
---|
6547 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
6548 |
|
---|
6549 | ViewCellContainer::const_iterator vit, vit_end = leaves.end();
|
---|
6550 |
|
---|
6551 | for (vit = leaves.begin(); vit != vit_end; ++ vit)
|
---|
6552 | {
|
---|
6553 | VspLeaf *vcLeaf = static_cast<VspViewCell *>(*vit)->mLeaves[0];
|
---|
6554 | VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
|
---|
6555 |
|
---|
6556 | for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
6557 | {
|
---|
6558 | collectRays.push_back(*rit);
|
---|
6559 | }
|
---|
6560 | }
|
---|
6561 |
|
---|
6562 | const int raysOut = min((int)collectRays.size(), maxRays);
|
---|
6563 |
|
---|
6564 | VssRayContainer::const_iterator rit, rit_end = collectRays.end();
|
---|
6565 |
|
---|
6566 | for (rit = collectRays.begin(); rit != rit_end; ++ rit)
|
---|
6567 | {
|
---|
6568 | const float p = RandomValue(0.0f, (float)collectRays.size());
|
---|
6569 |
|
---|
6570 | if (p < raysOut)
|
---|
6571 | vcRays.push_back(*rit);
|
---|
6572 | }
|
---|
6573 |
|
---|
6574 | exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
|
---|
6575 | }
|
---|
6576 |
|
---|
6577 |
|
---|
6578 | /////////////////
|
---|
6579 | //-- export view cell geometry
|
---|
6580 |
|
---|
6581 | exporter->SetWireframe();
|
---|
6582 |
|
---|
6583 | Material m;
|
---|
6584 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
6585 | exporter->SetForcedMaterial(m);
|
---|
6586 |
|
---|
6587 | ExportViewCellGeometry(exporter, vc, NULL, NULL);
|
---|
6588 | exporter->SetFilled();
|
---|
6589 |
|
---|
6590 | DEL_PTR(exporter);
|
---|
6591 | cout << "finished" << endl;
|
---|
6592 | }
|
---|
6593 |
|
---|
6594 | cout << endl;
|
---|
6595 | }
|
---|
6596 |
|
---|
6597 |
|
---|
6598 | int VspOspViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
6599 | ViewCellContainer &viewCells) const
|
---|
6600 | {
|
---|
6601 | return mHierarchyManager->GetVspTree()->ComputeBoxIntersections(box, viewCells);
|
---|
6602 | }
|
---|
6603 |
|
---|
6604 |
|
---|
6605 | int VspOspViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
6606 | const Vector3 &termination,
|
---|
6607 | ViewCellContainer &viewcells)
|
---|
6608 | {
|
---|
6609 | return mHierarchyManager->CastLineSegment(origin, termination, viewcells);
|
---|
6610 | }
|
---|
6611 |
|
---|
6612 |
|
---|
6613 | bool VspOspViewCellsManager::LineSegmentIntersects(const Vector3 &origin,
|
---|
6614 | const Vector3 &termination,
|
---|
6615 | ViewCell *viewCell)
|
---|
6616 | {
|
---|
6617 | return false;
|
---|
6618 | }
|
---|
6619 |
|
---|
6620 |
|
---|
6621 | bool VspOspViewCellsManager::ExportViewCells(const string filename,
|
---|
6622 | const bool exportPvs,
|
---|
6623 | const ObjectContainer &objects)
|
---|
6624 | {
|
---|
6625 | // no view cells were computed
|
---|
6626 | if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
|
---|
6627 | return false;
|
---|
6628 |
|
---|
6629 | if (strstr(filename.c_str(), ".bn"))
|
---|
6630 | {
|
---|
6631 | return ExportViewCellsBinary(filename, exportPvs, objects);
|
---|
6632 | }
|
---|
6633 |
|
---|
6634 | //cout << "exporting binary" << endl; string fname("test.vc"); return ExportViewCellsBinary(fname, exportPvs, objects);
|
---|
6635 |
|
---|
6636 | const long starttime = GetTime();
|
---|
6637 | cout << "exporting view cells to xml ... ";
|
---|
6638 |
|
---|
6639 | OUT_STREAM stream(filename.c_str());
|
---|
6640 |
|
---|
6641 | // we need unique ids for each view cell
|
---|
6642 | CreateUniqueViewCellIds();
|
---|
6643 |
|
---|
6644 | stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
6645 | stream << "<VisibilitySolution>" << endl;
|
---|
6646 |
|
---|
6647 | if (exportPvs)
|
---|
6648 | {
|
---|
6649 | ///////////////
|
---|
6650 | //-- export bounding boxes
|
---|
6651 | //-- we need the boxes to identify objects in the target engine
|
---|
6652 |
|
---|
6653 | if (mUseKdPvs)
|
---|
6654 | {
|
---|
6655 | stream << "<BoundingBoxes>" << endl;
|
---|
6656 |
|
---|
6657 | int id = 0;
|
---|
6658 | vector<KdIntersectable *>::const_iterator kit, kit_end = mPreprocessor->mKdTree->mKdIntersectables.end();
|
---|
6659 |
|
---|
6660 | for (kit = mPreprocessor->mKdTree->mKdIntersectables.begin(); kit != kit_end; ++ kit, ++ id)
|
---|
6661 | {
|
---|
6662 | Intersectable *obj = (*kit);
|
---|
6663 | const AxisAlignedBox3 box = obj->GetBox();
|
---|
6664 |
|
---|
6665 | // set kd node id
|
---|
6666 | obj->SetId(id);
|
---|
6667 |
|
---|
6668 | stream << "<BoundingBox" << " id=\"" << id << "\""
|
---|
6669 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
6670 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
6671 | }
|
---|
6672 |
|
---|
6673 | stream << "</BoundingBoxes>" << endl;
|
---|
6674 | }
|
---|
6675 | else
|
---|
6676 | {
|
---|
6677 | mHierarchyManager->ExportBoundingBoxes(stream, objects);
|
---|
6678 | }
|
---|
6679 | }
|
---|
6680 |
|
---|
6681 |
|
---|
6682 | //////////////////////////
|
---|
6683 | //-- export the view cells and the pvs
|
---|
6684 |
|
---|
6685 | const int numViewCells = mCurrentViewCellsStats.viewCells;
|
---|
6686 |
|
---|
6687 | stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
|
---|
6688 | mViewCellsTree->Export(stream, exportPvs);
|
---|
6689 | stream << "</ViewCells>" << endl;
|
---|
6690 |
|
---|
6691 |
|
---|
6692 | /////////////////
|
---|
6693 | //-- export the view space hierarchy
|
---|
6694 |
|
---|
6695 | stream << "<ViewSpaceHierarchy type=\"vsp\""
|
---|
6696 | << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
|
---|
6697 | << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
|
---|
6698 |
|
---|
6699 | mHierarchyManager->GetVspTree()->Export(stream);
|
---|
6700 | stream << "</ViewSpaceHierarchy>" << endl;
|
---|
6701 |
|
---|
6702 | /////////////////
|
---|
6703 | //-- export the object space hierarchy
|
---|
6704 |
|
---|
6705 | mHierarchyManager->ExportObjectSpaceHierarchy(stream);
|
---|
6706 |
|
---|
6707 | stream << "</VisibilitySolution>" << endl;
|
---|
6708 | stream.close();
|
---|
6709 |
|
---|
6710 | cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
6711 | return true;
|
---|
6712 | }
|
---|
6713 |
|
---|
6714 |
|
---|
6715 | bool VspOspViewCellsManager::ExportViewCellsBinary(const string filename,
|
---|
6716 | const bool exportPvs,
|
---|
6717 | const ObjectContainer &objects)
|
---|
6718 | {
|
---|
6719 | // no view cells constructed yet
|
---|
6720 | if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
|
---|
6721 | return false;
|
---|
6722 |
|
---|
6723 | const long starttime = GetTime();
|
---|
6724 | cout << "exporting view cells to binary format ... ";
|
---|
6725 |
|
---|
6726 | OUT_STREAM stream(filename.c_str());
|
---|
6727 |
|
---|
6728 | // we need unique ids for each view cell
|
---|
6729 | CreateUniqueViewCellIds();
|
---|
6730 |
|
---|
6731 | int numBoxes = mPreprocessor->mKdTree->mKdIntersectables.size();
|
---|
6732 | stream.write(reinterpret_cast<char *>(&numBoxes), sizeof(int));
|
---|
6733 |
|
---|
6734 |
|
---|
6735 | ///////////////
|
---|
6736 | //-- export bounding boxes
|
---|
6737 |
|
---|
6738 | // we use bounding box intersection to identify pvs objects in the target engine
|
---|
6739 | vector<KdIntersectable *>::const_iterator kit, kit_end = mPreprocessor->mKdTree->mKdIntersectables.end();
|
---|
6740 |
|
---|
6741 | int id = 0;
|
---|
6742 |
|
---|
6743 | for (kit = mPreprocessor->mKdTree->mKdIntersectables.begin(); kit != kit_end; ++ kit, ++ id)
|
---|
6744 | {
|
---|
6745 | Intersectable *obj = (*kit);
|
---|
6746 | // set the kd node id to identify the kd node as a pvs entry
|
---|
6747 | obj->SetId(id);
|
---|
6748 |
|
---|
6749 | const AxisAlignedBox3 &box = obj->GetBox();
|
---|
6750 | Vector3 bmin = box.Min();
|
---|
6751 | Vector3 bmax = box.Max();
|
---|
6752 |
|
---|
6753 | stream.write(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
|
---|
6754 | stream.write(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
|
---|
6755 | stream.write(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
6756 | }
|
---|
6757 |
|
---|
6758 | cout << "written " << numBoxes << " kd nodes" << endl;
|
---|
6759 |
|
---|
6760 | ///////////////
|
---|
6761 | //-- export the view cells and the pvs
|
---|
6762 |
|
---|
6763 | int numViewCells = mViewCells.size();
|
---|
6764 | stream.write(reinterpret_cast<char *>(&numViewCells), sizeof(int));
|
---|
6765 |
|
---|
6766 | Vector3 vmin = mViewSpaceBox.Min();
|
---|
6767 | Vector3 vmax = mViewSpaceBox.Max();
|
---|
6768 |
|
---|
6769 | stream.write(reinterpret_cast<char *>(&vmin), sizeof(Vector3));
|
---|
6770 | stream.write(reinterpret_cast<char *>(&vmax), sizeof(Vector3));
|
---|
6771 |
|
---|
6772 |
|
---|
6773 | //////////
|
---|
6774 | //-- export binary view cells
|
---|
6775 |
|
---|
6776 | mViewCellsTree->ExportBinary(stream);
|
---|
6777 |
|
---|
6778 |
|
---|
6779 | /////////
|
---|
6780 | //-- export the view space hierarchy
|
---|
6781 |
|
---|
6782 | mHierarchyManager->GetVspTree()->ExportBinary(stream);
|
---|
6783 |
|
---|
6784 |
|
---|
6785 | ////////
|
---|
6786 | //-- export the object space hierarchy
|
---|
6787 |
|
---|
6788 | //mHierarchyManager->ExportObjectSpaceHierarchyBinary();
|
---|
6789 |
|
---|
6790 | stream.close();
|
---|
6791 |
|
---|
6792 | //mHierarchyManager->GetVspTree()->TestOutput("output.txt");
|
---|
6793 |
|
---|
6794 | cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
6795 | return true;
|
---|
6796 | }
|
---|
6797 |
|
---|
6798 |
|
---|
6799 | ViewCell *VspOspViewCellsManager::GetViewCell(const Vector3 &point,
|
---|
6800 | const bool active) const
|
---|
6801 | {
|
---|
6802 | if (!ViewCellsConstructed())
|
---|
6803 | return NULL;
|
---|
6804 |
|
---|
6805 | if (!mViewSpaceBox.IsInside(point))
|
---|
6806 | return NULL;
|
---|
6807 |
|
---|
6808 | return mHierarchyManager->GetVspTree()->GetViewCell(point, active);
|
---|
6809 | }
|
---|
6810 |
|
---|
6811 |
|
---|
6812 | void VspOspViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
6813 | {
|
---|
6814 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
6815 |
|
---|
6816 | ViewCellContainer leaves;
|
---|
6817 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
6818 |
|
---|
6819 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
6820 |
|
---|
6821 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
6822 | {
|
---|
6823 | VspLeaf *leaf = static_cast<VspViewCell *>(*it)->mLeaves[0];
|
---|
6824 | const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(leaf);
|
---|
6825 | IncludeBoxInMesh(box, *mesh);
|
---|
6826 | }
|
---|
6827 |
|
---|
6828 | mesh->ComputeBoundingBox();
|
---|
6829 |
|
---|
6830 | vc->SetMesh(mesh);
|
---|
6831 | }
|
---|
6832 |
|
---|
6833 |
|
---|
6834 | int VspOspViewCellsManager::CastBeam(Beam &beam)
|
---|
6835 | {
|
---|
6836 | // matt: TODO
|
---|
6837 | return 0;
|
---|
6838 | }
|
---|
6839 |
|
---|
6840 |
|
---|
6841 | void VspOspViewCellsManager::Finalize(ViewCell *viewCell, const bool createMesh)
|
---|
6842 | {
|
---|
6843 | float area = 0;
|
---|
6844 | float volume = 0;
|
---|
6845 |
|
---|
6846 | ViewCellContainer leaves;
|
---|
6847 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
6848 |
|
---|
6849 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
6850 |
|
---|
6851 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
6852 | {
|
---|
6853 | VspLeaf *leaf = static_cast<VspViewCell *>(*it)->mLeaves[0];
|
---|
6854 |
|
---|
6855 | const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(leaf);
|
---|
6856 |
|
---|
6857 | const float lVol = box.GetVolume();
|
---|
6858 | const float lArea = box.SurfaceArea();
|
---|
6859 |
|
---|
6860 | area += lArea;
|
---|
6861 | volume += lVol;
|
---|
6862 |
|
---|
6863 | CreateMesh(*it);
|
---|
6864 | }
|
---|
6865 |
|
---|
6866 | viewCell->SetVolume(volume);
|
---|
6867 | viewCell->SetArea(area);
|
---|
6868 | }
|
---|
6869 |
|
---|
6870 |
|
---|
6871 | void VspOspViewCellsManager::PrepareLoadedViewCells()
|
---|
6872 | {
|
---|
6873 | // TODO
|
---|
6874 | }
|
---|
6875 |
|
---|
6876 |
|
---|
6877 | void VspOspViewCellsManager::PrintCompressionStats(HierarchyManager *hm, const int pvsEntries)
|
---|
6878 | {
|
---|
6879 | const float mem = (float)pvsEntries * ObjectPvs::GetEntrySize();
|
---|
6880 |
|
---|
6881 | float fullmem = mem +
|
---|
6882 | (hm->GetVspTree()->GetStatistics().Leaves() * 16 +
|
---|
6883 | hm->mBvHierarchy->GetStatistics().Leaves() * 16) / float(1024 * 1024);
|
---|
6884 |
|
---|
6885 | cout << "entries: " << pvsEntries << ", mem=" << mem << ", fullmem=" << fullmem <<endl;
|
---|
6886 | Debug << "entries: " << pvsEntries << ", mem=" << mem << ", fullmem=" << fullmem <<endl;
|
---|
6887 | }
|
---|
6888 |
|
---|
6889 |
|
---|
6890 | void VspOspViewCellsManager::CompressViewCells()
|
---|
6891 | {
|
---|
6892 | if (!(ViewCellsTreeConstructed() && mCompressViewCells))
|
---|
6893 | return;
|
---|
6894 |
|
---|
6895 |
|
---|
6896 | ////////////
|
---|
6897 | //-- compression
|
---|
6898 |
|
---|
6899 | int pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
|
---|
6900 |
|
---|
6901 | cout << "before compression: " << endl;
|
---|
6902 | Debug << "before compression: " << endl;
|
---|
6903 |
|
---|
6904 | PrintCompressionStats(mHierarchyManager, pvsEntries);
|
---|
6905 |
|
---|
6906 | if (mCompressObjects)
|
---|
6907 | {
|
---|
6908 | cout << "compressing in the objects: " << endl;
|
---|
6909 | Debug << "compressing in the objects: " << endl;
|
---|
6910 |
|
---|
6911 | pvsEntries = mHierarchyManager->CompressObjectSpace();
|
---|
6912 | }
|
---|
6913 | else
|
---|
6914 | {
|
---|
6915 | cout << "compressing in the view space: " << endl;
|
---|
6916 | Debug << "compressing in the view space: " << endl;
|
---|
6917 |
|
---|
6918 | mViewCellsTree->SetViewCellsStorage(ViewCellsTree::COMPRESSED);
|
---|
6919 | pvsEntries = mViewCellsTree->CountStoredPvsEntries(mViewCellsTree->GetRoot());
|
---|
6920 | }
|
---|
6921 |
|
---|
6922 | PrintCompressionStats(mHierarchyManager, pvsEntries);
|
---|
6923 | }
|
---|
6924 |
|
---|
6925 |
|
---|
6926 | void VspOspViewCellsManager::CollectObjects(const AxisAlignedBox3 &box,
|
---|
6927 | ObjectContainer &objects)
|
---|
6928 | {
|
---|
6929 | mHierarchyManager->CollectObjects(box, objects);
|
---|
6930 | }
|
---|
6931 |
|
---|
6932 |
|
---|
6933 | void VspOspViewCellsManager::EvalViewCellPartition()
|
---|
6934 | {
|
---|
6935 | int samplesPerPass;
|
---|
6936 | int castSamples = 0;
|
---|
6937 | int oldSamples = 0;
|
---|
6938 | int samplesForStats;
|
---|
6939 | char statsPrefix[100];
|
---|
6940 | char suffix[100];
|
---|
6941 | int splitsStepSize;
|
---|
6942 |
|
---|
6943 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
|
---|
6944 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesForStats", samplesForStats);
|
---|
6945 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samples", mEvaluationSamples);
|
---|
6946 | Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
|
---|
6947 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.stepSize", splitsStepSize);
|
---|
6948 |
|
---|
6949 | bool useHisto;
|
---|
6950 | int histoMem;
|
---|
6951 |
|
---|
6952 | Environment::GetSingleton()->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
|
---|
6953 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.histoMem", histoMem);
|
---|
6954 |
|
---|
6955 | Debug << "step size: " << splitsStepSize << endl;
|
---|
6956 | Debug << "view cell evaluation samples per pass: " << samplesPerPass << endl;
|
---|
6957 | Debug << "view cell evaluation samples: " << mEvaluationSamples << endl;
|
---|
6958 | Debug << "view cell stats prefix: " << statsPrefix << endl;
|
---|
6959 |
|
---|
6960 | cout << "reseting pvs ... ";
|
---|
6961 |
|
---|
6962 | // reset pvs and start over from zero
|
---|
6963 | mViewCellsTree->ResetPvs();
|
---|
6964 |
|
---|
6965 | cout << "finished" << endl;
|
---|
6966 | cout << "Evaluating view cell partition ... " << endl;
|
---|
6967 |
|
---|
6968 | int pass = 0;
|
---|
6969 |
|
---|
6970 | while (castSamples < mEvaluationSamples)
|
---|
6971 | {
|
---|
6972 | ///////////////
|
---|
6973 | //-- we have to use uniform sampling strategy for construction rays
|
---|
6974 |
|
---|
6975 | VssRayContainer evaluationSamples;
|
---|
6976 | const int samplingType = mEvaluationSamplingType;
|
---|
6977 |
|
---|
6978 | long startTime = GetTime();
|
---|
6979 | Real timeDiff;
|
---|
6980 |
|
---|
6981 | cout << "casting " << samplesPerPass << " samples ... ";
|
---|
6982 | Debug << "casting " << samplesPerPass << " samples ... ";
|
---|
6983 |
|
---|
6984 | // use mixed distributions
|
---|
6985 | CastEvaluationSamples(samplesPerPass, evaluationSamples);
|
---|
6986 |
|
---|
6987 | timeDiff = TimeDiff(startTime, GetTime());
|
---|
6988 | cout << "finished in " << timeDiff * 1e-3f << " secs" << endl;
|
---|
6989 | Debug << "finished in " << timeDiff * 1e-3f << " secs" << endl;
|
---|
6990 |
|
---|
6991 | // don't computed sample contributions
|
---|
6992 | // because already accounted for inside the mixture distribution!
|
---|
6993 |
|
---|
6994 | castSamples += samplesPerPass;
|
---|
6995 |
|
---|
6996 | if ((castSamples >= samplesForStats + oldSamples) ||
|
---|
6997 | (castSamples >= mEvaluationSamples))
|
---|
6998 | {
|
---|
6999 | oldSamples += samplesForStats;
|
---|
7000 |
|
---|
7001 | ///////////
|
---|
7002 | //-- output stats
|
---|
7003 |
|
---|
7004 | sprintf(suffix, "-%09d-eval.log", castSamples);
|
---|
7005 | const string filename = string(statsPrefix) + string(suffix);
|
---|
7006 |
|
---|
7007 | startTime = GetTime();
|
---|
7008 |
|
---|
7009 | cout << "compute new statistics ... " << endl;
|
---|
7010 |
|
---|
7011 | ofstream ofstr(filename.c_str());
|
---|
7012 | mHierarchyManager->EvaluateSubdivision(ofstr, splitsStepSize, false, useHisto, histoMem, pass);
|
---|
7013 |
|
---|
7014 | timeDiff = TimeDiff(startTime, GetTime());
|
---|
7015 | cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
7016 | cout << "*************************************" << endl;
|
---|
7017 |
|
---|
7018 | Debug << "statistics computed in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
7019 |
|
---|
7020 | ++ pass;
|
---|
7021 | }
|
---|
7022 |
|
---|
7023 | disposeRays(evaluationSamples, NULL);
|
---|
7024 | }
|
---|
7025 |
|
---|
7026 | ////////////
|
---|
7027 | //-- histogram
|
---|
7028 |
|
---|
7029 | const int numLeaves = mViewCellsTree->GetNumInitialViewCells(mViewCellsTree->GetRoot());
|
---|
7030 | int histoStepSize;
|
---|
7031 |
|
---|
7032 | Environment::GetSingleton()->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
|
---|
7033 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.histoStepSize", histoStepSize);
|
---|
7034 |
|
---|
7035 | if (useHisto)
|
---|
7036 | {
|
---|
7037 | // evaluate view cells in a histogram
|
---|
7038 | char str[64];
|
---|
7039 |
|
---|
7040 | // hack: just show final view cells
|
---|
7041 | const int pass = (int)mViewCells.size();
|
---|
7042 |
|
---|
7043 | //for (int pass = histoStepSize; pass <= numLeaves; pass += histoStepSize)
|
---|
7044 |
|
---|
7045 | string filename;
|
---|
7046 |
|
---|
7047 | cout << "computing histogram for " << pass << " view cells" << endl;
|
---|
7048 |
|
---|
7049 | ///////////////////
|
---|
7050 | //-- evaluate histogram for pvs size
|
---|
7051 |
|
---|
7052 | cout << "computing pvs histogram for " << pass << " view cells" << endl;
|
---|
7053 |
|
---|
7054 | sprintf(str, "-%09d-histo-pvs2.log", pass);
|
---|
7055 | filename = string(statsPrefix) + string(str);
|
---|
7056 |
|
---|
7057 | EvalViewCellHistogramForPvsSize(filename, pass);
|
---|
7058 | }
|
---|
7059 | }
|
---|
7060 |
|
---|
7061 |
|
---|
7062 | void VspOspViewCellsManager::FinalizeViewCells(const bool createMesh)
|
---|
7063 | {
|
---|
7064 | ViewCellsManager::FinalizeViewCells(createMesh);
|
---|
7065 |
|
---|
7066 | if (mHierarchyManager->mUseTraversalTree)
|
---|
7067 | { // create a traversal tree for optimal view cell casting
|
---|
7068 | mHierarchyManager->CreateTraversalTree();
|
---|
7069 | }
|
---|
7070 | }
|
---|
7071 |
|
---|
7072 |
|
---|
7073 | #if TEST_PACKETS
|
---|
7074 |
|
---|
7075 | float VspOspViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays,
|
---|
7076 | const bool addContributions,
|
---|
7077 | const bool storeViewCells,
|
---|
7078 | const bool useHitObjects)
|
---|
7079 | {
|
---|
7080 | if (!ViewCellsConstructed())
|
---|
7081 | return 0;
|
---|
7082 |
|
---|
7083 | float sum = 0.0f;
|
---|
7084 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
7085 |
|
---|
7086 | VssRayContainer tmpRays;
|
---|
7087 |
|
---|
7088 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
7089 | {
|
---|
7090 | sum += ComputeSampleContribution(*(*it), addContributions, storeViewCells, useHitObjects);
|
---|
7091 |
|
---|
7092 | tmpRays.push_back(new VssRay(*(*it)));
|
---|
7093 |
|
---|
7094 | if (tmpRays.size() == 4)
|
---|
7095 | {
|
---|
7096 | // cast packets of 4 rays
|
---|
7097 | RayPacket packet(tmpRays);
|
---|
7098 | mHierarchyManager->CastLineSegment(packet);
|
---|
7099 |
|
---|
7100 | for (int i = 0; i < 4; ++ i)
|
---|
7101 | {
|
---|
7102 | ComputeSampleContribution(*tmpRays[i], addContributions, true, useHitObjects);
|
---|
7103 | // compare results
|
---|
7104 | cout << "ray " << i << ": " << (int)tmpRays[i]->mViewCells.size() << " "
|
---|
7105 | << (int)packet.mViewCells[i].size() << endl;
|
---|
7106 | }
|
---|
7107 |
|
---|
7108 | CLEAR_CONTAINER(tmpRays);
|
---|
7109 | }
|
---|
7110 | }
|
---|
7111 |
|
---|
7112 | CLEAR_CONTAINER(tmpRays);
|
---|
7113 |
|
---|
7114 | cout << "view cell cast time: " << viewCellCastTimer.TotalTime() << " s" << endl;
|
---|
7115 | Debug << "view cell cast time: " << viewCellCastTimer.TotalTime() << " s" << endl;
|
---|
7116 |
|
---|
7117 | cout << "pvs time: " << pvsTimer.TotalTime() << " s" << endl;
|
---|
7118 | Debug << "pvs time: " << pvsTimer.TotalTime() << " s" << endl;
|
---|
7119 |
|
---|
7120 | return sum;
|
---|
7121 | }
|
---|
7122 |
|
---|
7123 | #endif
|
---|
7124 |
|
---|
7125 |
|
---|
7126 | ViewCellsManager *ViewCellsManager::LoadViewCellsBinary(const string &filename,
|
---|
7127 | ObjectContainer &pvsObjects,
|
---|
7128 | bool finalizeViewCells,
|
---|
7129 | BoundingBoxConverter *bconverter)
|
---|
7130 | {
|
---|
7131 | IN_STREAM stream(filename.c_str());
|
---|
7132 |
|
---|
7133 | if (!stream.is_open())
|
---|
7134 | {
|
---|
7135 | Debug << "View cells loading failed: could not open file" << endl;
|
---|
7136 | return NULL;
|
---|
7137 | }
|
---|
7138 |
|
---|
7139 | Debug << "loading boxes" << endl;
|
---|
7140 |
|
---|
7141 | const long startTime = GetTime();
|
---|
7142 |
|
---|
7143 | // load all the bounding boxes
|
---|
7144 | IndexedBoundingBoxContainer iboxes;
|
---|
7145 | ViewCellsManager::LoadIndexedBoundingBoxesBinary(stream, iboxes);
|
---|
7146 |
|
---|
7147 | pvsObjects.reserve(iboxes.size());
|
---|
7148 |
|
---|
7149 | if (bconverter)
|
---|
7150 | {
|
---|
7151 | // associate object ids with bounding boxes
|
---|
7152 | bconverter->IdentifyObjects(iboxes, pvsObjects);
|
---|
7153 | }
|
---|
7154 |
|
---|
7155 |
|
---|
7156 | ObjectContainer pvsLookup;
|
---|
7157 | pvsLookup.resize(iboxes.size());
|
---|
7158 |
|
---|
7159 | for (size_t i = 0; i < pvsLookup.size(); ++ i)
|
---|
7160 | {
|
---|
7161 | pvsLookup[i] = NULL;
|
---|
7162 | }
|
---|
7163 |
|
---|
7164 | for (size_t i = 0; i < pvsObjects.size(); ++ i)
|
---|
7165 | {
|
---|
7166 | pvsLookup[pvsObjects[i]->GetId()] = pvsObjects[i];
|
---|
7167 | }
|
---|
7168 |
|
---|
7169 |
|
---|
7170 | /////////////
|
---|
7171 | //-- load the view cells
|
---|
7172 |
|
---|
7173 | int numViewCells;
|
---|
7174 | stream.read(reinterpret_cast<char *>(&numViewCells), sizeof(int));
|
---|
7175 |
|
---|
7176 | Debug << "loading " << numViewCells << " view cells " << endl;
|
---|
7177 |
|
---|
7178 | Vector3 vmin, vmax;
|
---|
7179 |
|
---|
7180 | stream.read(reinterpret_cast<char *>(&vmin), sizeof(Vector3));
|
---|
7181 | stream.read(reinterpret_cast<char *>(&vmax), sizeof(Vector3));
|
---|
7182 |
|
---|
7183 | AxisAlignedBox3 viewSpaceBox(vmin, vmax);
|
---|
7184 |
|
---|
7185 | Debug << "view space box: " << viewSpaceBox << endl;
|
---|
7186 |
|
---|
7187 | ViewCellsTree *vcTree = new ViewCellsTree();
|
---|
7188 |
|
---|
7189 | if (!vcTree->ImportBinary(stream, pvsLookup))
|
---|
7190 | {
|
---|
7191 | Debug << "Error: loading view cells tree failed!" << endl;
|
---|
7192 | delete vcTree;
|
---|
7193 |
|
---|
7194 | return NULL;
|
---|
7195 | }
|
---|
7196 |
|
---|
7197 | Debug << "loading the view space partition tree" << endl;
|
---|
7198 | VspTree *vspTree = new VspTree(viewSpaceBox);
|
---|
7199 |
|
---|
7200 | if (!vspTree->ImportBinary(stream))
|
---|
7201 | {
|
---|
7202 | Debug << "Error: loading vsp tree failed!" << endl;
|
---|
7203 | delete vcTree; delete vspTree;
|
---|
7204 |
|
---|
7205 | return NULL;
|
---|
7206 | }
|
---|
7207 |
|
---|
7208 |
|
---|
7209 | HierarchyManager * hm =
|
---|
7210 | new HierarchyManager(HierarchyManager::BV_BASED_OBJ_SUBDIV);
|
---|
7211 |
|
---|
7212 | hm->SetVspTree(vspTree);
|
---|
7213 |
|
---|
7214 |
|
---|
7215 | /////////
|
---|
7216 | //-- create view cells manager
|
---|
7217 |
|
---|
7218 | VspOspViewCellsManager *vm = new VspOspViewCellsManager(vcTree, hm);
|
---|
7219 |
|
---|
7220 |
|
---|
7221 | //////////////
|
---|
7222 | //-- do some more preparation
|
---|
7223 |
|
---|
7224 | vm->mViewSpaceBox = viewSpaceBox;
|
---|
7225 | vm->mViewCells.clear();
|
---|
7226 |
|
---|
7227 | ViewCellContainer viewCells;
|
---|
7228 | vcTree->CollectLeaves(vcTree->GetRoot(), viewCells);
|
---|
7229 |
|
---|
7230 | ViewCellContainer::const_iterator cit, cit_end = viewCells.end();
|
---|
7231 |
|
---|
7232 | for (cit = viewCells.begin(); cit != cit_end; ++ cit)
|
---|
7233 | {
|
---|
7234 | vm->mViewCells.push_back(*cit);
|
---|
7235 | }
|
---|
7236 |
|
---|
7237 |
|
---|
7238 | //////////////
|
---|
7239 | //-- associate view cells with vsp leaves
|
---|
7240 |
|
---|
7241 | vector<VspLeaf *> vspLeaves;
|
---|
7242 | vspTree->CollectLeaves(vspLeaves);
|
---|
7243 |
|
---|
7244 | vector<VspLeaf *>::const_iterator vit, vit_end = vspLeaves.end();
|
---|
7245 | cit = viewCells.begin();
|
---|
7246 |
|
---|
7247 | for (vit = vspLeaves.begin(); vit != vit_end; ++ vit, ++ cit)
|
---|
7248 | {
|
---|
7249 | VspLeaf *leaf = *vit;
|
---|
7250 | VspViewCell *vc = static_cast<VspViewCell *>(*cit);
|
---|
7251 |
|
---|
7252 | leaf->SetViewCell(vc);
|
---|
7253 | vc->mLeaves.push_back(leaf);
|
---|
7254 | }
|
---|
7255 |
|
---|
7256 |
|
---|
7257 | /*for (cit = viewCells.begin(); cit != cit_end; ++ cit)
|
---|
7258 | {
|
---|
7259 | Debug << "pvssize: " << (*cit)->GetPvs().GetSize();
|
---|
7260 | }*/
|
---|
7261 |
|
---|
7262 |
|
---|
7263 | vm->mViewCellsFinished = true;
|
---|
7264 | vm->mMaxPvsSize = (int)pvsObjects.size();
|
---|
7265 |
|
---|
7266 | if (finalizeViewCells)
|
---|
7267 | {
|
---|
7268 | // create the meshes and compute view cell volumes
|
---|
7269 | const bool createMeshes = true;
|
---|
7270 | vm->FinalizeViewCells(createMeshes);
|
---|
7271 | }
|
---|
7272 |
|
---|
7273 | Debug << (int)vm->mViewCells.size() << " view cells loaded in "
|
---|
7274 | << TimeDiff(startTime, GetTime()) * 1e-6f << " secs" << endl;
|
---|
7275 |
|
---|
7276 | //vspTree->TestOutput("input.txt");
|
---|
7277 |
|
---|
7278 | return vm;
|
---|
7279 | }
|
---|
7280 |
|
---|
7281 | } |
---|