[440] | 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 "VspKdTree.h"
|
---|
| 11 | #include "Exporter.h"
|
---|
[442] | 12 | #include "VspBspTree.h"
|
---|
[508] | 13 | #include "ViewCellsParser.h"
|
---|
[532] | 14 | #include "Beam.h"
|
---|
[540] | 15 | #include "VssPreprocessor.h"
|
---|
| 16 | #include "RssPreprocessor.h"
|
---|
[440] | 17 |
|
---|
[712] | 18 | // HACK
|
---|
| 19 | const static bool SAMPLE_AFTER_SUBDIVISION = true;
|
---|
| 20 | const static bool TEST_EMPTY_VIEW_CELLS = false;
|
---|
[517] | 21 |
|
---|
[697] | 22 |
|
---|
| 23 |
|
---|
| 24 | template <typename T> class myless
|
---|
| 25 | {
|
---|
| 26 | public:
|
---|
| 27 | //bool operator() (HierarchyNode *v1, HierarchyNode *v2) const
|
---|
| 28 | bool operator() (T v1, T v2) const
|
---|
| 29 | {
|
---|
| 30 | return (v1->GetMergeCost() < v2->GetMergeCost());
|
---|
| 31 | }
|
---|
| 32 | };
|
---|
| 33 |
|
---|
[706] | 34 | //typedef priority_queue<ViewCell *, vector<ViewCell *>, myless<vector<ViewCell *>::value_type> > FilterQueue;
|
---|
[697] | 35 |
|
---|
| 36 |
|
---|
| 37 |
|
---|
[440] | 38 | ViewCellsManager::ViewCellsManager():
|
---|
[480] | 39 | mRenderer(NULL),
|
---|
[574] | 40 | mInitialSamples(0),
|
---|
[440] | 41 | mConstructionSamples(0),
|
---|
| 42 | mPostProcessSamples(0),
|
---|
[470] | 43 | mVisualizationSamples(0),
|
---|
| 44 | mTotalAreaValid(false),
|
---|
[517] | 45 | mTotalArea(0.0f),
|
---|
[547] | 46 | mViewCellsFinished(false),
|
---|
[562] | 47 | mMaxPvsSize(9999999),
|
---|
[564] | 48 | mMinPvsSize(0), // one means only empty view cells are invalid
|
---|
[719] | 49 | mMaxPvsRatio(1.0),
|
---|
| 50 | mViewCellPvsIsUpdated(false)
|
---|
[440] | 51 | {
|
---|
[542] | 52 | mViewSpaceBox.Initialize();
|
---|
[482] | 53 | ParseEnvironment();
|
---|
[580] | 54 |
|
---|
| 55 | mViewCellsTree = new ViewCellsTree(this);
|
---|
[440] | 56 | }
|
---|
| 57 |
|
---|
[517] | 58 |
|
---|
[482] | 59 | void ViewCellsManager::ParseEnvironment()
|
---|
| 60 | {
|
---|
[503] | 61 | // visualization stuff
|
---|
[485] | 62 | environment->GetBoolValue("ViewCells.Visualization.exportRays", mExportRays);
|
---|
| 63 | environment->GetBoolValue("ViewCells.Visualization.exportGeometry", mExportGeometry);
|
---|
[547] | 64 | environment->GetFloatValue("ViewCells.maxPvsRatio", mMaxPvsRatio);
|
---|
[664] | 65 |
|
---|
[710] | 66 | environment->GetBoolValue("ViewCells.pruneEmptyViewCells", mPruneEmptyViewCells);
|
---|
[664] | 67 |
|
---|
[710] | 68 | // HACK
|
---|
| 69 | if (0)
|
---|
| 70 | mMinPvsSize = mPruneEmptyViewCells ? 1 : 0;
|
---|
| 71 | else
|
---|
| 72 | mMinPvsSize = 0;
|
---|
| 73 |
|
---|
[564] | 74 | environment->GetBoolValue("ViewCells.processOnlyValidViewCells", mOnlyValidViewCells);
|
---|
[485] | 75 |
|
---|
[574] | 76 | environment->GetIntValue("ViewCells.Construction.samples", mConstructionSamples);
|
---|
| 77 | environment->GetIntValue("ViewCells.PostProcess.samples", mPostProcessSamples);
|
---|
[595] | 78 | environment->GetBoolValue("ViewCells.PostProcess.useRaysForMerge", mUseRaysForMerge);
|
---|
| 79 |
|
---|
[574] | 80 | environment->GetIntValue("ViewCells.Visualization.samples", mVisualizationSamples);
|
---|
| 81 |
|
---|
| 82 | environment->GetIntValue("ViewCells.Construction.samplesPerPass", mSamplesPerPass);
|
---|
[577] | 83 | environment->GetBoolValue("ViewCells.exportToFile", mExportViewCells);
|
---|
[581] | 84 |
|
---|
[600] | 85 | environment->GetIntValue("ViewCells.active", mNumActiveViewCells);
|
---|
[586] | 86 | environment->GetBoolValue("ViewCells.PostProcess.compress", mCompressViewCells);
|
---|
[660] | 87 | environment->GetBoolValue("ViewCells.Visualization.useClipPlane", mUseClipPlaneForViz);
|
---|
[591] | 88 | environment->GetBoolValue("ViewCells.PostProcess.merge", mMergeViewCells);
|
---|
[664] | 89 | environment->GetBoolValue("ViewCells.evaluateViewCells", mEvaluateViewCells);
|
---|
[666] | 90 | environment->GetBoolValue("ViewCells.showVisualization", mShowVisualization);
|
---|
[697] | 91 | environment->GetIntValue("ViewCells.Filter.maxSize", mMaxFilterSize);
|
---|
| 92 | environment->GetFloatValue("ViewCells.Filter.width", mFilterWidth);
|
---|
[710] | 93 |
|
---|
[664] | 94 | char buf[100];
|
---|
| 95 | environment->GetStringValue("ViewCells.samplingType", buf);
|
---|
| 96 |
|
---|
[660] | 97 |
|
---|
[664] | 98 | if (strcmp(buf, "box") == 0)
|
---|
[660] | 99 | mSamplingType = Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
[664] | 100 | else if (strcmp(buf, "directional") == 0)
|
---|
[660] | 101 | mSamplingType = Preprocessor::DIRECTION_BASED_DISTRIBUTION;
|
---|
[666] | 102 | else
|
---|
| 103 | {
|
---|
| 104 | Debug << "error! wrong sampling type" << endl;
|
---|
| 105 | exit(0);
|
---|
| 106 | }
|
---|
[660] | 107 |
|
---|
[482] | 108 | environment->GetStringValue("ViewCells.Visualization.colorCode", buf);
|
---|
[503] | 109 |
|
---|
[482] | 110 | if (strcmp(buf, "PVS") == 0)
|
---|
| 111 | mColorCode = 1;
|
---|
| 112 | else if (strcmp(buf, "MergedLeaves") == 0)
|
---|
| 113 | mColorCode = 2;
|
---|
| 114 | else if (strcmp(buf, "MergedTreeDiff") == 0)
|
---|
| 115 | mColorCode = 3;
|
---|
| 116 | else
|
---|
| 117 | mColorCode = 0;
|
---|
| 118 |
|
---|
[664] | 119 |
|
---|
[710] | 120 |
|
---|
[664] | 121 | Debug << "***********View Cells options ****************" << endl;
|
---|
| 122 | Debug << "color code: " << mColorCode << endl;
|
---|
| 123 |
|
---|
| 124 | Debug << "export rays: " << mExportRays << endl;
|
---|
| 125 | Debug << "export geometry: " << mExportGeometry << endl;
|
---|
| 126 | Debug << "max pvs ratio: " << mMaxPvsRatio << endl;
|
---|
| 127 |
|
---|
[710] | 128 | Debug << "prune empty view cells: " << mPruneEmptyViewCells << endl;
|
---|
[664] | 129 |
|
---|
| 130 | Debug << "process only valid view cells: " << mOnlyValidViewCells << endl;
|
---|
| 131 | Debug << "construction samples: " << mConstructionSamples << endl;
|
---|
| 132 | Debug << "post process samples: " << mPostProcessSamples << endl;
|
---|
| 133 | Debug << "post process use rays for merge: " << mUseRaysForMerge << endl;
|
---|
| 134 | Debug << "visualization samples: " << mVisualizationSamples << endl;
|
---|
| 135 | Debug << "construction samples per pass: " << mSamplesPerPass << endl;
|
---|
| 136 | Debug << "export to file: " << mExportViewCells << endl;
|
---|
| 137 |
|
---|
| 138 | Debug << "active: " << mNumActiveViewCells << endl;
|
---|
| 139 | Debug << "post process compress: " << mCompressViewCells << endl;
|
---|
| 140 | Debug << "visualization use clipPlane: " << mUseClipPlaneForViz << endl;
|
---|
| 141 | Debug << "post process merge: " << mMergeViewCells << endl;
|
---|
| 142 | Debug << "evaluate view cells: " << mEvaluateViewCells << endl;
|
---|
| 143 | Debug << "sampling type: " << mSamplingType << endl;
|
---|
[666] | 144 | Debug << "show visualization: " << mShowVisualization << endl;
|
---|
[697] | 145 | Debug << "filter width: " << mFilterWidth << endl;
|
---|
[710] | 146 | Debug << "sample after subdivision: " << SAMPLE_AFTER_SUBDIVISION << endl;
|
---|
[664] | 147 | Debug << endl;
|
---|
[440] | 148 | }
|
---|
| 149 |
|
---|
[485] | 150 |
|
---|
[440] | 151 | ViewCellsManager::~ViewCellsManager()
|
---|
| 152 | {
|
---|
[480] | 153 | DEL_PTR(mRenderer);
|
---|
[580] | 154 |
|
---|
[598] | 155 | if (!ViewCellsTreeConstructed())
|
---|
| 156 | CLEAR_CONTAINER(mViewCells);
|
---|
| 157 | else
|
---|
| 158 | DEL_PTR(mViewCellsTree);
|
---|
| 159 |
|
---|
[503] | 160 | CLEAR_CONTAINER(mMeshContainer);
|
---|
[440] | 161 | }
|
---|
| 162 |
|
---|
[485] | 163 |
|
---|
[693] | 164 | void ViewCellsManager::CollectEmptyViewCells()
|
---|
| 165 | {
|
---|
| 166 | mEmptyViewCells.clear();
|
---|
| 167 | ViewCellContainer leaves;
|
---|
| 168 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 169 |
|
---|
| 170 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 171 |
|
---|
| 172 | cout << "collecting empty view cells" << endl;
|
---|
[694] | 173 |
|
---|
[693] | 174 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 175 | {
|
---|
| 176 | if ((*it)->GetPvs().Empty())
|
---|
| 177 | {
|
---|
| 178 | mEmptyViewCells.push_back(*it);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
[694] | 181 | Debug << "empty view cells found: " << (int)mEmptyViewCells.size() << endl;
|
---|
[693] | 182 | }
|
---|
| 183 |
|
---|
| 184 |
|
---|
[694] | 185 | void ViewCellsManager::TestEmptyViewCells(const ObjectContainer &obj)
|
---|
[693] | 186 | {
|
---|
| 187 | ViewCellContainer::const_iterator it, it_end = mEmptyViewCells.end();
|
---|
| 188 |
|
---|
| 189 | char buf[50];
|
---|
| 190 | int i = 0;
|
---|
| 191 | for (it = mEmptyViewCells.begin(); it != it_end; ++ it)
|
---|
| 192 | {
|
---|
| 193 | if (!(*it)->GetPvs().Empty())
|
---|
| 194 | {
|
---|
[694] | 195 | sprintf(buf, "empty-viewcells-%09d.x3d", i/*(*it)->GetId()*/);
|
---|
[693] | 196 | Exporter *exporter = Exporter::GetExporter(buf);
|
---|
| 197 |
|
---|
| 198 | if (exporter && i < 20)
|
---|
| 199 | {
|
---|
[694] | 200 | Ray *pray = (*it)->mPiercingRays[0];
|
---|
[697] | 201 | Debug << "view cell " << (*it)->GetId() << " not empty, pvs: " << (*it)->GetPvs().GetSize() << " " << (int)pray->intersections.size() << endl;
|
---|
[694] | 202 |
|
---|
| 203 | exporter->ExportRays((*it)->mPiercingRays);
|
---|
| 204 |
|
---|
| 205 | exporter->SetFilled();
|
---|
| 206 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 207 |
|
---|
| 208 | for (int j = 0; j < (int)pray->intersections.size(); ++ j)
|
---|
| 209 | {
|
---|
| 210 | if (pray->intersections[j].mObject)
|
---|
| 211 | exporter->ExportIntersectable(pray->intersections[j].mObject);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | //exporter->SetWireframe();
|
---|
| 215 | exporter->SetForcedMaterial(RgbColor(0,1,0));
|
---|
| 216 | exporter->ExportGeometry(obj);
|
---|
| 217 |
|
---|
| 218 | exporter->SetFilled();
|
---|
| 219 |
|
---|
| 220 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
[693] | 221 | ExportViewCellGeometry(exporter, *it);
|
---|
[694] | 222 |
|
---|
| 223 | delete exporter;
|
---|
[693] | 224 | }
|
---|
| 225 |
|
---|
[694] | 226 |
|
---|
[693] | 227 | ++ i;
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
[694] | 230 | Debug << "\nSampled " << i << " new view cells (" << " of " << (int)mEmptyViewCells.size() << ")" << endl << endl;
|
---|
[693] | 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
[574] | 234 | int ViewCellsManager::CastPassSamples(const int samplesPerPass,
|
---|
| 235 | const int sampleType,
|
---|
| 236 | VssRayContainer &passSamples) const
|
---|
[527] | 237 | {
|
---|
[570] | 238 | SimpleRayContainer simpleRays;
|
---|
[540] | 239 |
|
---|
[573] | 240 | preprocessor->GenerateRays(samplesPerPass,
|
---|
| 241 | sampleType,
|
---|
[570] | 242 | simpleRays);
|
---|
[564] | 243 |
|
---|
[573] | 244 | // shoot simple ray and add it to importance samples
|
---|
| 245 | preprocessor->CastRays(simpleRays, passSamples);
|
---|
[570] | 246 |
|
---|
[574] | 247 | return (int)passSamples.size();
|
---|
| 248 | }
|
---|
[570] | 249 |
|
---|
[574] | 250 |
|
---|
[607] | 251 |
|
---|
[574] | 252 | /// helper function which destroys rays or copies them into the output ray container
|
---|
[639] | 253 | inline void disposeRays(VssRayContainer &rays, VssRayContainer *outRays)
|
---|
[574] | 254 | {
|
---|
| 255 | cout << "disposing samples ... ";
|
---|
[582] | 256 | long startTime = GetTime();
|
---|
| 257 | int n = (int)rays.size();
|
---|
| 258 |
|
---|
[573] | 259 | if (outRays)
|
---|
[542] | 260 | {
|
---|
[574] | 261 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
| 262 |
|
---|
| 263 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 264 | {
|
---|
| 265 | outRays->push_back(*it);
|
---|
| 266 | }
|
---|
[573] | 267 | }
|
---|
| 268 | else
|
---|
| 269 | {
|
---|
[639] | 270 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
| 271 |
|
---|
| 272 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 273 | {
|
---|
| 274 | //(*it)->Unref();
|
---|
| 275 | if (!(*it)->IsActive())
|
---|
| 276 | delete (*it);
|
---|
| 277 | }
|
---|
[573] | 278 | }
|
---|
[639] | 279 |
|
---|
[574] | 280 | cout << "finished" << endl;
|
---|
[582] | 281 | Debug << "disposed " << n << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[573] | 282 | }
|
---|
[570] | 283 |
|
---|
| 284 |
|
---|
[573] | 285 | int ViewCellsManager::Construct(Preprocessor *preprocessor, VssRayContainer *outRays)
|
---|
| 286 | {
|
---|
| 287 | int numSamples = 0;
|
---|
| 288 | SimpleRayContainer simpleRays;
|
---|
| 289 |
|
---|
[574] | 290 | VssRayContainer initialSamples;
|
---|
[540] | 291 |
|
---|
[574] | 292 | cout << "view cell construction: casting " << mInitialSamples << " initial samples ... ";
|
---|
[712] | 293 |
|
---|
[573] | 294 | //-- construction rays => we use uniform samples for this
|
---|
[574] | 295 | CastPassSamples(mInitialSamples,
|
---|
[660] | 296 | mSamplingType,
|
---|
[574] | 297 | initialSamples);
|
---|
[573] | 298 |
|
---|
[574] | 299 | cout << "finished" << endl;
|
---|
| 300 |
|
---|
[660] | 301 |
|
---|
[573] | 302 | // construct view cells
|
---|
[574] | 303 | const int numInitialSamples =
|
---|
| 304 | ConstructSubdivision(preprocessor->mObjects, initialSamples);
|
---|
[540] | 305 |
|
---|
[574] | 306 | numSamples += numInitialSamples;
|
---|
[542] | 307 |
|
---|
[574] | 308 | // rays can be passed or deleted
|
---|
[639] | 309 | disposeRays(initialSamples, outRays);
|
---|
[574] | 310 |
|
---|
[697] | 311 | cout << "testing filter ... ";
|
---|
[570] | 312 |
|
---|
[697] | 313 | TestFilter(preprocessor->mObjects);
|
---|
| 314 |
|
---|
| 315 | cout << "finished" << endl;
|
---|
| 316 |
|
---|
[660] | 317 | // -- stats after contruction
|
---|
| 318 | ResetViewCells();
|
---|
| 319 |
|
---|
[667] | 320 | Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
|
---|
| 321 |
|
---|
[683] | 322 | if (1) // export initial view cells
|
---|
[660] | 323 | {
|
---|
[712] | 324 | const char filename[] = "view_cells.wrl";
|
---|
| 325 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
[660] | 326 |
|
---|
[712] | 327 |
|
---|
[660] | 328 | if (exporter)
|
---|
| 329 | {
|
---|
[712] | 330 | cout << "exporting initial view cells (=leaves) to " << filename << " ... ";
|
---|
[660] | 331 | if (mExportGeometry)
|
---|
[712] | 332 | {
|
---|
[660] | 333 | exporter->ExportGeometry(preprocessor->mObjects);
|
---|
[712] | 334 | }
|
---|
[660] | 335 |
|
---|
[712] | 336 | //exporter->SetWireframe();
|
---|
[660] | 337 | ExportViewCellsForViz(exporter);
|
---|
| 338 |
|
---|
| 339 | delete exporter;
|
---|
[712] | 340 | cout << "finished" << endl;
|
---|
[660] | 341 | }
|
---|
| 342 | }
|
---|
| 343 |
|
---|
[573] | 344 | //-- guided rays are used for further sampling
|
---|
[574] | 345 | const int n = mConstructionSamples; //+initialSamples;
|
---|
[570] | 346 |
|
---|
[666] | 347 | // should we use directional samples?
|
---|
[694] | 348 | bool dirSamples = (mSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
|
---|
[573] | 349 |
|
---|
[712] | 350 | // -- the main loop
|
---|
[573] | 351 | while (numSamples < n)
|
---|
| 352 | {
|
---|
[596] | 353 | cout << "casting " << mSamplesPerPass << " samples of " << n << " ... ";
|
---|
[574] | 354 | VssRayContainer constructionSamples;
|
---|
| 355 |
|
---|
[694] | 356 | const int samplingType = mSamplingType;
|
---|
| 357 | /*dirSamples ?
|
---|
[574] | 358 | Preprocessor::DIRECTION_BASED_DISTRIBUTION :
|
---|
[694] | 359 | Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;*/
|
---|
[667] | 360 |
|
---|
[600] | 361 | if (0)
|
---|
[666] | 362 | dirSamples = !dirSamples; // toggle sampling method
|
---|
[667] | 363 |
|
---|
[574] | 364 | numSamples += CastPassSamples(mSamplesPerPass,
|
---|
[666] | 365 | samplingType,
|
---|
[574] | 366 | constructionSamples);
|
---|
| 367 |
|
---|
| 368 | cout << "finished" << endl;
|
---|
| 369 |
|
---|
| 370 | cout << "computing sample contribution for " << (int)constructionSamples.size() << " samples ... ";
|
---|
| 371 |
|
---|
| 372 | // TODO: leak?
|
---|
[712] | 373 | if (1 || SAMPLE_AFTER_SUBDIVISION)
|
---|
[605] | 374 | ComputeSampleContributions(constructionSamples, true, false);
|
---|
[712] | 375 |
|
---|
[574] | 376 | cout << "finished" << endl;
|
---|
| 377 |
|
---|
| 378 |
|
---|
[639] | 379 | disposeRays(constructionSamples, outRays);
|
---|
[574] | 380 |
|
---|
| 381 | cout << "total samples: " << numSamples << endl;
|
---|
[542] | 382 | }
|
---|
[573] | 383 |
|
---|
[570] | 384 |
|
---|
[573] | 385 | //-- post processing
|
---|
| 386 | VssRayContainer postProcessSamples;
|
---|
[570] | 387 |
|
---|
[694] | 388 | //-- construction rays
|
---|
[574] | 389 | CastPassSamples(mPostProcessSamples,
|
---|
[694] | 390 | mSamplingType,
|
---|
[574] | 391 | postProcessSamples);
|
---|
[660] | 392 |
|
---|
| 393 |
|
---|
| 394 | // stats before post processing (i.e., merge)
|
---|
| 395 | EvaluateViewCellsStats();
|
---|
| 396 | Debug << "\noriginal view cell partition before post process:\n" << mCurrentViewCellsStats << endl;
|
---|
| 397 |
|
---|
| 398 | mRenderer->RenderScene();
|
---|
| 399 | SimulationStatistics ss;
|
---|
| 400 | dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
| 401 |
|
---|
| 402 | Debug << ss << endl;
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 |
|
---|
[574] | 406 | cout << "starting post processing and visualization" << endl;
|
---|
| 407 |
|
---|
[660] | 408 |
|
---|
[666] | 409 | // store view cells for postprocessing
|
---|
[574] | 410 | const bool storeViewCells = true;
|
---|
[605] | 411 |
|
---|
[660] | 412 |
|
---|
[605] | 413 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 414 | ComputeSampleContributions(postProcessSamples, true, storeViewCells);
|
---|
[663] | 415 |
|
---|
[570] | 416 | // merge the view cells
|
---|
[573] | 417 | PostProcess(preprocessor->mObjects, postProcessSamples);
|
---|
[570] | 418 |
|
---|
[693] | 419 | // only for testing
|
---|
[694] | 420 | if (TEST_EMPTY_VIEW_CELLS)
|
---|
| 421 | CollectEmptyViewCells();
|
---|
[570] | 422 |
|
---|
[693] | 423 |
|
---|
[574] | 424 | //-- visualization
|
---|
[666] | 425 | if (mShowVisualization)
|
---|
| 426 | {
|
---|
| 427 | VssRayContainer visualizationSamples;
|
---|
[574] | 428 |
|
---|
[666] | 429 | //-- construction rays => we use uniform samples for this
|
---|
| 430 | CastPassSamples(mVisualizationSamples,
|
---|
| 431 | Preprocessor::DIRECTION_BASED_DISTRIBUTION,
|
---|
| 432 | visualizationSamples);
|
---|
[574] | 433 |
|
---|
[666] | 434 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 435 | ComputeSampleContributions(visualizationSamples, true, storeViewCells);
|
---|
[574] | 436 |
|
---|
[666] | 437 | // different visualizations
|
---|
| 438 | Visualize(preprocessor->mObjects, visualizationSamples);
|
---|
[574] | 439 |
|
---|
[666] | 440 | disposeRays(visualizationSamples, outRays);
|
---|
| 441 | }
|
---|
[574] | 442 |
|
---|
[707] | 443 | // needed to fix empty view cells
|
---|
[710] | 444 | //SetValidity(0, 99999999999);
|
---|
[707] | 445 |
|
---|
[664] | 446 | if (mEvaluateViewCells)
|
---|
| 447 | {
|
---|
[666] | 448 | EvalViewCellPartition(preprocessor);
|
---|
[664] | 449 | }
|
---|
[666] | 450 |
|
---|
[667] | 451 | if (0)
|
---|
| 452 | {
|
---|
| 453 | ViewCellContainer leaves;
|
---|
| 454 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 455 | mViewCellsTree->ResetPvs();
|
---|
| 456 | VssRayContainer dummyRays;
|
---|
| 457 | CastPassSamples(mInitialSamples, mSamplingType, dummyRays);
|
---|
[666] | 458 |
|
---|
[667] | 459 | //ComputeSampleContributions(initialSamples, true, false);
|
---|
| 460 | ComputeSampleContributions(dummyRays, true, false);
|
---|
[664] | 461 |
|
---|
[667] | 462 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
[666] | 463 |
|
---|
[667] | 464 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 465 | {
|
---|
| 466 | mViewCellsTree->PropagatePvs(*it);
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | mViewCellsTree->ExportStats("dummy.log");
|
---|
[666] | 470 | }
|
---|
| 471 |
|
---|
[573] | 472 | return numSamples;
|
---|
[527] | 473 | }
|
---|
| 474 |
|
---|
[547] | 475 |
|
---|
[664] | 476 | void ViewCellsManager::EvalViewCellPartition(Preprocessor *preprocessor)
|
---|
[660] | 477 | {
|
---|
| 478 | int samplesPerPass;
|
---|
| 479 | int numSamples;
|
---|
| 480 | int castSamples = 0;
|
---|
| 481 |
|
---|
[664] | 482 | char s[64];
|
---|
| 483 |
|
---|
[660] | 484 | environment->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
|
---|
| 485 | environment->GetIntValue("ViewCells.Evaluation.samples", numSamples);
|
---|
| 486 |
|
---|
[664] | 487 | char statsPrefix[100];
|
---|
| 488 | environment->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
|
---|
[663] | 489 |
|
---|
[664] | 490 | Debug << "view cell evaluation samples per pass: " << samplesPerPass << endl;
|
---|
| 491 | Debug << "view cell evaluation samples: " << numSamples << endl;
|
---|
| 492 | Debug << "view cell stats prefix: " << statsPrefix << endl;
|
---|
| 493 |
|
---|
| 494 | //VssRayContainer outRays;
|
---|
[666] | 495 | // should directional sampling be used?
|
---|
| 496 | bool dirSamples = mSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION;
|
---|
[664] | 497 |
|
---|
| 498 | cout << "Evaluating view cell partition" << endl;
|
---|
| 499 |
|
---|
| 500 | ViewCellContainer leaves;
|
---|
| 501 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 502 |
|
---|
[666] | 503 | bool startFromZero = true;
|
---|
| 504 |
|
---|
| 505 | // reset pvs and start over from zero
|
---|
| 506 | if (startFromZero)
|
---|
| 507 | {
|
---|
| 508 | mViewCellsTree->ResetPvs();
|
---|
| 509 | }
|
---|
| 510 | else // statistics without addidtional samples
|
---|
| 511 | {
|
---|
| 512 | cout << "compute new statistics ... ";
|
---|
| 513 | sprintf(s, "-%09d-eval.log", castSamples);
|
---|
| 514 | string fName = string(statsPrefix) + string(s);
|
---|
| 515 |
|
---|
| 516 | mViewCellsTree->ExportStats(fName);
|
---|
| 517 | cout << "finished" << endl;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 |
|
---|
[660] | 521 | while (castSamples < numSamples)
|
---|
| 522 | {
|
---|
[664] | 523 | VssRayContainer evaluationSamples;
|
---|
[663] | 524 |
|
---|
[666] | 525 | const int samplingType = mSamplingType;
|
---|
| 526 | /* dirSamples ?
|
---|
[664] | 527 | Preprocessor::DIRECTION_BASED_DISTRIBUTION :
|
---|
| 528 | Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
[666] | 529 | */
|
---|
[664] | 530 | //-- construction rays => we use uniform samples for this
|
---|
| 531 | CastPassSamples(samplesPerPass, samplingType, evaluationSamples);
|
---|
| 532 |
|
---|
| 533 | castSamples += samplesPerPass;
|
---|
| 534 |
|
---|
| 535 | cout << "casting " << (int)evaluationSamples.size() << " samples " << endl;
|
---|
| 536 |
|
---|
| 537 | ComputeSampleContributions(evaluationSamples, true, false);
|
---|
| 538 |
|
---|
| 539 | cout << "compute new statistics ... ";
|
---|
| 540 |
|
---|
| 541 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 542 |
|
---|
| 543 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 544 | {
|
---|
| 545 | mViewCellsTree->PropagatePvs(*it);
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | // output stats
|
---|
[667] | 549 | sprintf(s, "-%09d-eval.log", castSamples);
|
---|
[664] | 550 | string fileName = string(statsPrefix) + string(s);
|
---|
| 551 |
|
---|
| 552 | mViewCellsTree->ExportStats(fileName);
|
---|
| 553 |
|
---|
| 554 | cout << "finished" << endl;
|
---|
| 555 |
|
---|
| 556 | disposeRays(evaluationSamples, NULL);
|
---|
[660] | 557 | }
|
---|
[693] | 558 |
|
---|
| 559 | // find empty view cells bug
|
---|
[694] | 560 | if (TEST_EMPTY_VIEW_CELLS)
|
---|
[719] | 561 | {
|
---|
[694] | 562 | TestEmptyViewCells(preprocessor->mObjects);
|
---|
[719] | 563 | }
|
---|
[660] | 564 | }
|
---|
| 565 |
|
---|
[704] | 566 |
|
---|
| 567 | inline float EvalMergeCost(ViewCell *root, ViewCell *candidate)
|
---|
[703] | 568 | {
|
---|
[704] | 569 | return root->GetPvs().GetPvsHomogenity(candidate->GetPvs());
|
---|
| 570 | }
|
---|
[660] | 571 |
|
---|
[703] | 572 |
|
---|
[704] | 573 | int GetBestViewCellIdx(ViewCell *root, const ViewCellContainer &neighborhood)
|
---|
| 574 | {
|
---|
[705] | 575 | int bestViewCellIdx = 0;
|
---|
[703] | 576 |
|
---|
[704] | 577 | float mergeCost = Limits::Infinity;
|
---|
| 578 | int i = 0;
|
---|
[703] | 579 |
|
---|
[704] | 580 | ViewCellContainer::const_iterator it, it_end = neighborhood.end();
|
---|
[703] | 581 |
|
---|
[704] | 582 | for (it = neighborhood.begin(); it != it_end; ++ it, ++ i)
|
---|
[703] | 583 | {
|
---|
[704] | 584 | const float mc = EvalMergeCost(root, *it);
|
---|
[705] | 585 |
|
---|
| 586 | if (mc < mergeCost)
|
---|
[704] | 587 | {
|
---|
| 588 | mergeCost = mc;
|
---|
| 589 | bestViewCellIdx = i;
|
---|
[703] | 590 | }
|
---|
[704] | 591 | }
|
---|
[703] | 592 |
|
---|
[704] | 593 | return bestViewCellIdx;
|
---|
| 594 | }
|
---|
[703] | 595 |
|
---|
| 596 |
|
---|
[704] | 597 | ViewCell *ViewCellsManager::ConstructLocalMergeTree(ViewCell *currentViewCell,
|
---|
| 598 | const ViewCellContainer &viewCells)
|
---|
| 599 | {
|
---|
| 600 | ViewCell *root = currentViewCell;
|
---|
| 601 | ViewCellContainer neighborhood = viewCells;
|
---|
[703] | 602 |
|
---|
[704] | 603 | ViewCellContainer::const_iterator it, it_end = neighborhood.end();
|
---|
[703] | 604 |
|
---|
[704] | 605 | const int n = min(mMaxFilterSize, (int)neighborhood.size());
|
---|
| 606 | //-- use priority queue to merge leaf pairs
|
---|
[703] | 607 |
|
---|
[705] | 608 | //cout << "neighborhood: " << neighborhood.size() << endl;
|
---|
[704] | 609 | //const float maxAvgCost = 350;
|
---|
| 610 | for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
|
---|
[703] | 611 | {
|
---|
[704] | 612 | const int bestViewCellIdx = GetBestViewCellIdx(root, neighborhood);
|
---|
[703] | 613 |
|
---|
[704] | 614 | ViewCell *bestViewCell = neighborhood[bestViewCellIdx];
|
---|
[706] | 615 |
|
---|
[704] | 616 | // remove from vector
|
---|
| 617 | swap(neighborhood[bestViewCellIdx], neighborhood.back());
|
---|
| 618 | neighborhood.pop_back();
|
---|
[705] | 619 |
|
---|
[713] | 620 | // cout << "vc idx: " << bestViewCellIdx << endl;
|
---|
[704] | 621 | if (!bestViewCell || !root)
|
---|
| 622 | cout << "warning!!" << endl;
|
---|
[705] | 623 |
|
---|
[704] | 624 | // create new root of the hierarchy
|
---|
| 625 | root = MergeViewCells(root, bestViewCell);
|
---|
[703] | 626 | }
|
---|
| 627 |
|
---|
[704] | 628 | return root;
|
---|
[703] | 629 | }
|
---|
| 630 |
|
---|
[704] | 631 |
|
---|
[713] | 632 | struct SortableViewCellEntry {
|
---|
| 633 |
|
---|
| 634 | SortableViewCellEntry() {}
|
---|
| 635 | SortableViewCellEntry(const float v, ViewCell *cell):mValue(v), mViewCell(cell) {}
|
---|
| 636 |
|
---|
| 637 | float mValue;
|
---|
| 638 | ViewCell *mViewCell;
|
---|
| 639 |
|
---|
| 640 | friend bool operator<(const SortableViewCellEntry &a, const SortableViewCellEntry &b) {
|
---|
| 641 | return a.mValue < b.mValue;
|
---|
| 642 | }
|
---|
| 643 | };
|
---|
| 644 |
|
---|
| 645 | ViewCell *
|
---|
| 646 | ViewCellsManager::ConstructLocalMergeTree2(ViewCell *currentViewCell,
|
---|
| 647 | const ViewCellContainer &viewCells)
|
---|
| 648 | {
|
---|
| 649 |
|
---|
| 650 | vector<SortableViewCellEntry> neighborhood(viewCells.size());
|
---|
| 651 | int i, j;
|
---|
| 652 | for (i = 0, j = 0; i < viewCells.size(); i++) {
|
---|
| 653 | if (viewCells[i] != currentViewCell)
|
---|
| 654 | neighborhood[j++] = SortableViewCellEntry(
|
---|
| 655 | EvalMergeCost(currentViewCell, viewCells[i]),
|
---|
| 656 | viewCells[i]);
|
---|
| 657 | }
|
---|
| 658 | neighborhood.resize(j);
|
---|
| 659 |
|
---|
| 660 | sort(neighborhood.begin(), neighborhood.end());
|
---|
| 661 |
|
---|
| 662 | ViewCell *root = currentViewCell;
|
---|
| 663 |
|
---|
| 664 | vector<SortableViewCellEntry>::const_iterator it, it_end = neighborhood.end();
|
---|
| 665 |
|
---|
| 666 | const int n = min(mMaxFilterSize, (int)neighborhood.size());
|
---|
| 667 | //-- use priority queue to merge leaf pairs
|
---|
| 668 |
|
---|
| 669 | //cout << "neighborhood: " << neighborhood.size() << endl;
|
---|
| 670 | for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
|
---|
| 671 | {
|
---|
| 672 | ViewCell *bestViewCell = neighborhood[nMergedViewCells].mViewCell;
|
---|
| 673 | //cout <<nMergedViewCells<<":"<<"homogenity=" <<neighborhood[nMergedViewCells].mValue<<endl;
|
---|
| 674 | // create new root of the hierarchy
|
---|
| 675 | root = MergeViewCells(root, bestViewCell);
|
---|
| 676 | // set negative cost so that this view cell gets deleted
|
---|
| 677 | root->SetMergeCost(-1.0f);
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | return root;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
| 683 | void
|
---|
| 684 | ViewCellsManager::DeleteLocalMergeTree(ViewCell *vc
|
---|
| 685 | ) const
|
---|
| 686 | {
|
---|
| 687 | if (!vc->IsLeaf() && vc->GetMergeCost() < 0.0f) {
|
---|
| 688 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
| 689 | ViewCellContainer::const_iterator it, it_end = vci->mChildren.end();
|
---|
| 690 |
|
---|
| 691 | for (it = vci->mChildren.begin(); it != it_end; ++ it)
|
---|
| 692 | DeleteLocalMergeTree(*it);
|
---|
| 693 | vci->mChildren.clear();
|
---|
| 694 | delete vci;
|
---|
| 695 | }
|
---|
| 696 | }
|
---|
| 697 |
|
---|
[564] | 698 | bool ViewCellsManager::CheckValidity(ViewCell *vc,
|
---|
| 699 | int minPvsSize,
|
---|
[562] | 700 | int maxPvsSize) const
|
---|
[547] | 701 | {
|
---|
[569] | 702 |
|
---|
[710] | 703 | if ((vc->GetPvs().GetSize() > maxPvsSize) ||
|
---|
| 704 | (vc->GetPvs().GetSize() < minPvsSize))
|
---|
[564] | 705 | {
|
---|
[710] | 706 | return false;
|
---|
[561] | 707 | }
|
---|
[570] | 708 |
|
---|
[710] | 709 | return true;
|
---|
[547] | 710 | }
|
---|
| 711 |
|
---|
| 712 |
|
---|
[710] | 713 | bool ViewCellsManager::EqualToSpatialNode(ViewCell *viewCell) const
|
---|
| 714 | {
|
---|
| 715 | return false;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 | int ViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 719 | ViewCellContainer &viewCells) const
|
---|
| 720 | {
|
---|
| 721 | return 0;
|
---|
| 722 | };
|
---|
| 723 |
|
---|
[697] | 724 | AxisAlignedBox3 ViewCellsManager::GetFilterBBox(const Vector3 &viewPoint,
|
---|
| 725 | const float width) const
|
---|
| 726 | {
|
---|
| 727 | Vector3 min = viewPoint - width * 0.5f;
|
---|
| 728 | Vector3 max = viewPoint + width * 0.5;
|
---|
| 729 |
|
---|
| 730 | return AxisAlignedBox3(min, max);
|
---|
| 731 | }
|
---|
| 732 |
|
---|
| 733 |
|
---|
| 734 | void ViewCellsManager::GetPrVS(const Vector3 &viewPoint, PrVs &prvs)
|
---|
| 735 | {
|
---|
| 736 | const AxisAlignedBox3 box = GetFilterBBox(viewPoint, mFilterWidth);
|
---|
| 737 |
|
---|
[704] | 738 | ViewCell *currentViewCell = GetViewCell(viewPoint);
|
---|
| 739 |
|
---|
[697] | 740 | ViewCellContainer viewCells;
|
---|
| 741 | ComputeBoxIntersections(box, viewCells);
|
---|
| 742 |
|
---|
[713] | 743 | ViewCell *root = ConstructLocalMergeTree2(currentViewCell, viewCells);
|
---|
[704] | 744 |
|
---|
| 745 | prvs.mViewCell = root;
|
---|
| 746 | //prvs.mPvs = root->GetPvs();
|
---|
[697] | 747 | }
|
---|
| 748 |
|
---|
| 749 |
|
---|
[581] | 750 | bool ViewCellsManager::ViewCellsTreeConstructed() const
|
---|
| 751 | {
|
---|
| 752 | return mViewCellsTree->GetRoot();
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 |
|
---|
[564] | 756 | void ViewCellsManager::SetValidity(ViewCell *vc,
|
---|
| 757 | int minPvs,
|
---|
[562] | 758 | int maxPvs) const
|
---|
| 759 | {
|
---|
| 760 | vc->SetValid(CheckValidity(vc, minPvs, maxPvs));
|
---|
| 761 | }
|
---|
| 762 |
|
---|
[577] | 763 |
|
---|
[569] | 764 | void
|
---|
| 765 | ViewCellsManager::SetValidity(
|
---|
[570] | 766 | int minPvsSize,
|
---|
[569] | 767 | int maxPvsSize) const
|
---|
| 768 | {
|
---|
| 769 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
[570] | 770 |
|
---|
[569] | 771 | for (it = mViewCells.begin(); it != it_end; ++ it) {
|
---|
| 772 | SetValidity(*it, minPvsSize, maxPvsSize);
|
---|
| 773 | }
|
---|
| 774 | }
|
---|
[562] | 775 |
|
---|
[569] | 776 | void
|
---|
| 777 | ViewCellsManager::SetValidityPercentage(
|
---|
| 778 | const float minValid,
|
---|
| 779 | const float maxValid
|
---|
| 780 | )
|
---|
| 781 | {
|
---|
| 782 | sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
|
---|
[570] | 783 |
|
---|
[569] | 784 | int start = mViewCells.size()*minValid;
|
---|
| 785 | int end = mViewCells.size()*maxValid;
|
---|
[570] | 786 |
|
---|
[569] | 787 | for (int i=0; i < mViewCells.size(); i++)
|
---|
| 788 | mViewCells[i]->SetValid(i >= start && i <= end);
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | int
|
---|
| 792 | ViewCellsManager::CountValidViewcells() const
|
---|
| 793 | {
|
---|
| 794 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 795 | int valid = 0;
|
---|
| 796 | for (it = mViewCells.begin(); it != it_end; ++ it) {
|
---|
| 797 | if ((*it)->GetValid())
|
---|
[706] | 798 | valid ++;
|
---|
[569] | 799 | }
|
---|
| 800 | return valid;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[577] | 803 |
|
---|
| 804 | bool ViewCellsManager::LoadViewCellsGeometry(const string filename)
|
---|
[440] | 805 | {
|
---|
| 806 | X3dParser parser;
|
---|
[503] | 807 |
|
---|
[440] | 808 | environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
|
---|
[503] | 809 |
|
---|
[440] | 810 | bool success = parser.ParseFile(filename, *this);
|
---|
| 811 | Debug << (int)mViewCells.size() << " view cells loaded" << endl;
|
---|
| 812 |
|
---|
| 813 | return success;
|
---|
| 814 | }
|
---|
| 815 |
|
---|
[485] | 816 |
|
---|
[487] | 817 | bool ViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
|
---|
| 818 | {
|
---|
[542] | 819 | viewPoint = mViewSpaceBox.GetRandomPoint();
|
---|
[487] | 820 |
|
---|
| 821 | return true;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 |
|
---|
[557] | 825 | float ViewCellsManager::GetViewSpaceVolume()
|
---|
| 826 | {
|
---|
[610] | 827 | return mViewSpaceBox.GetVolume() * (2.0f * sqr((float)M_PI));
|
---|
[557] | 828 | }
|
---|
| 829 |
|
---|
| 830 |
|
---|
[490] | 831 | bool ViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
|
---|
| 832 | {
|
---|
[569] | 833 | if (!ViewCellsConstructed())
|
---|
[542] | 834 | return mViewSpaceBox.IsInside(viewPoint);
|
---|
[569] | 835 | else {
|
---|
| 836 | if (!mViewSpaceBox.IsInside(viewPoint))
|
---|
| 837 | return false;
|
---|
| 838 | ViewCell *viewcell = GetViewCell(viewPoint);
|
---|
| 839 | if (!viewcell || !viewcell->GetValid())
|
---|
| 840 | return false;
|
---|
| 841 | }
|
---|
| 842 | return true;
|
---|
[490] | 843 | }
|
---|
| 844 |
|
---|
[501] | 845 |
|
---|
[563] | 846 | float
|
---|
| 847 | ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays,
|
---|
[574] | 848 | const bool addRays,
|
---|
| 849 | const bool storeViewCells
|
---|
[563] | 850 | )
|
---|
[440] | 851 | {
|
---|
[563] | 852 | // view cells not yet constructed
|
---|
| 853 | if (!ViewCellsConstructed())
|
---|
| 854 | return 0.0f;
|
---|
[564] | 855 |
|
---|
[563] | 856 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
[503] | 857 |
|
---|
[563] | 858 | float sum = 0.0f;
|
---|
[664] | 859 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 860 | {
|
---|
| 861 | sum += ComputeSampleContributions(*(*it), addRays, storeViewCells);
|
---|
| 862 | //ComputeSampleContributions(*(*it), addRays);
|
---|
| 863 | // sum += (*it)->mPvsContribution;
|
---|
[563] | 864 | }
|
---|
[664] | 865 |
|
---|
[563] | 866 | return sum;
|
---|
[440] | 867 | }
|
---|
| 868 |
|
---|
[485] | 869 |
|
---|
[479] | 870 | void ViewCellsManager::EvaluateViewCellsStats()
|
---|
| 871 | {
|
---|
[660] | 872 | mCurrentViewCellsStats.Reset();
|
---|
[479] | 873 |
|
---|
| 874 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 875 |
|
---|
| 876 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
[487] | 877 | {
|
---|
[660] | 878 | mViewCellsTree->UpdateViewCellsStats(*it, mCurrentViewCellsStats);
|
---|
[487] | 879 | }
|
---|
[479] | 880 | }
|
---|
| 881 |
|
---|
[485] | 882 |
|
---|
[580] | 883 | void ViewCellsManager::EvaluateRenderStatistics(float &totalRenderCost,
|
---|
[579] | 884 | float &expectedRenderCost,
|
---|
[580] | 885 | float &deviation,
|
---|
| 886 | float &variance,
|
---|
| 887 | int &totalPvs,
|
---|
| 888 | float &avgRenderCost)
|
---|
[579] | 889 | {
|
---|
| 890 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 891 |
|
---|
[580] | 892 |
|
---|
[579] | 893 | //-- compute expected value
|
---|
| 894 |
|
---|
| 895 | totalRenderCost = 0;
|
---|
[580] | 896 | totalPvs = 0;
|
---|
[579] | 897 |
|
---|
| 898 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 899 | {
|
---|
| 900 | ViewCell *vc = *it;
|
---|
| 901 | totalRenderCost += vc->GetPvs().GetSize() * vc->GetVolume();
|
---|
[580] | 902 | totalPvs += (int)vc->GetPvs().GetSize();
|
---|
[579] | 903 | }
|
---|
| 904 |
|
---|
[580] | 905 | // normalize with view space box
|
---|
| 906 | totalRenderCost /= mViewSpaceBox.GetVolume();
|
---|
[579] | 907 | expectedRenderCost = totalRenderCost / (float)mViewCells.size();
|
---|
[580] | 908 | avgRenderCost = totalPvs / (float)mViewCells.size();
|
---|
[579] | 909 |
|
---|
| 910 |
|
---|
[580] | 911 | //-- compute standard defiation
|
---|
[579] | 912 | variance = 0;
|
---|
[580] | 913 | deviation = 0;
|
---|
[579] | 914 |
|
---|
| 915 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 916 | {
|
---|
| 917 | ViewCell *vc = *it;
|
---|
| 918 |
|
---|
| 919 | float renderCost = vc->GetPvs().GetSize() * vc->GetVolume();
|
---|
[580] | 920 | float dev;
|
---|
[579] | 921 |
|
---|
[580] | 922 | if (1)
|
---|
| 923 | dev = fabs(avgRenderCost - (float)vc->GetPvs().GetSize());
|
---|
| 924 | else
|
---|
| 925 | dev = fabs(expectedRenderCost - renderCost);
|
---|
[579] | 926 |
|
---|
[580] | 927 | deviation += dev;
|
---|
| 928 | variance += dev * dev;
|
---|
[579] | 929 | }
|
---|
[580] | 930 |
|
---|
[579] | 931 | variance /= (float)mViewCells.size();
|
---|
[580] | 932 | deviation /= (float)mViewCells.size();
|
---|
[579] | 933 | }
|
---|
| 934 |
|
---|
| 935 |
|
---|
| 936 |
|
---|
[440] | 937 | void ViewCellsManager::AddViewCell(ViewCell *viewCell)
|
---|
| 938 | {
|
---|
| 939 | mViewCells.push_back(viewCell);
|
---|
| 940 | }
|
---|
| 941 |
|
---|
[485] | 942 |
|
---|
[478] | 943 | float ViewCellsManager::GetArea(ViewCell *viewCell) const
|
---|
| 944 | {
|
---|
| 945 | return viewCell->GetArea();
|
---|
| 946 | }
|
---|
| 947 |
|
---|
| 948 |
|
---|
| 949 | float ViewCellsManager::GetVolume(ViewCell *viewCell) const
|
---|
| 950 | {
|
---|
| 951 | return viewCell->GetVolume();
|
---|
| 952 | }
|
---|
| 953 |
|
---|
[485] | 954 |
|
---|
[503] | 955 | void ViewCellsManager::DeriveViewCells(const ObjectContainer &objects,
|
---|
| 956 | ViewCellContainer &viewCells,
|
---|
[440] | 957 | const int maxViewCells) const
|
---|
| 958 | {
|
---|
| 959 | // maximal max viewcells
|
---|
[503] | 960 | int limit = maxViewCells > 0 ?
|
---|
[440] | 961 | Min((int)objects.size(), maxViewCells) : (int)objects.size();
|
---|
| 962 |
|
---|
| 963 | for (int i = 0; i < limit; ++ i)
|
---|
| 964 | {
|
---|
| 965 | Intersectable *object = objects[i];
|
---|
[503] | 966 |
|
---|
[440] | 967 | // extract the mesh instances
|
---|
| 968 | if (object->Type() == Intersectable::MESH_INSTANCE)
|
---|
| 969 | {
|
---|
| 970 | MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
|
---|
| 971 |
|
---|
| 972 | ViewCell *viewCell = GenerateViewCell(inst->GetMesh());
|
---|
| 973 | viewCells.push_back(viewCell);
|
---|
| 974 | }
|
---|
| 975 | //TODO: transformed meshes
|
---|
| 976 | }
|
---|
| 977 | }
|
---|
| 978 |
|
---|
[485] | 979 |
|
---|
[503] | 980 | ViewCell *ViewCellsManager::ExtrudeViewCell(const Triangle3 &baseTri,
|
---|
[440] | 981 | const float height) const
|
---|
| 982 | {
|
---|
| 983 | // one mesh per view cell
|
---|
| 984 | Mesh *mesh = new Mesh();
|
---|
[503] | 985 |
|
---|
[440] | 986 | //-- construct prism
|
---|
| 987 |
|
---|
[503] | 988 | // bottom
|
---|
[440] | 989 | mesh->mFaces.push_back(new Face(2,1,0));
|
---|
| 990 | // top
|
---|
| 991 | mesh->mFaces.push_back(new Face(3,4,5));
|
---|
| 992 | // sides
|
---|
| 993 | mesh->mFaces.push_back(new Face(1, 4, 3, 0));
|
---|
| 994 | mesh->mFaces.push_back(new Face(2, 5, 4, 1));
|
---|
| 995 | mesh->mFaces.push_back(new Face(3, 5, 2, 0));
|
---|
| 996 |
|
---|
| 997 | //--- extrude new vertices for top of prism
|
---|
| 998 | Vector3 triNorm = baseTri.GetNormal();
|
---|
| 999 |
|
---|
[503] | 1000 | Triangle3 topTri;
|
---|
[440] | 1001 |
|
---|
| 1002 | // add base vertices and calculate top vertices
|
---|
| 1003 | for (int i = 0; i < 3; ++ i)
|
---|
| 1004 | mesh->mVertices.push_back(baseTri.mVertices[i]);
|
---|
[503] | 1005 |
|
---|
| 1006 | // add top vertices
|
---|
[440] | 1007 | for (int i = 0; i < 3; ++ i)
|
---|
| 1008 | mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
|
---|
[503] | 1009 |
|
---|
[440] | 1010 | mesh->Preprocess();
|
---|
| 1011 |
|
---|
| 1012 | return GenerateViewCell(mesh);
|
---|
| 1013 | }
|
---|
| 1014 |
|
---|
[485] | 1015 |
|
---|
[551] | 1016 | void ViewCellsManager::FinalizeViewCells(const bool createMesh)
|
---|
| 1017 | {
|
---|
| 1018 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
[693] | 1019 |
|
---|
| 1020 | // volume and area of the view cells are recomputed and a view cell mesh is created
|
---|
[551] | 1021 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1022 | {
|
---|
| 1023 | Finalize(*it, createMesh);
|
---|
| 1024 | }
|
---|
[555] | 1025 |
|
---|
| 1026 | mTotalAreaValid = false;
|
---|
[551] | 1027 | }
|
---|
| 1028 |
|
---|
| 1029 |
|
---|
| 1030 | void ViewCellsManager::Finalize(ViewCell *viewCell, const bool createMesh)
|
---|
| 1031 | {
|
---|
| 1032 | // implemented in subclasses
|
---|
| 1033 | }
|
---|
| 1034 |
|
---|
| 1035 |
|
---|
[582] | 1036 | ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCell *left, ViewCell *right) const
|
---|
[440] | 1037 | {
|
---|
[580] | 1038 | // generate parent view cell
|
---|
| 1039 | ViewCellInterior *vc = new ViewCellInterior();//GenerateViewCell();
|
---|
[710] | 1040 |
|
---|
[610] | 1041 | vc->GetPvs() = left->GetPvs();
|
---|
[440] | 1042 | // merge pvs
|
---|
[610] | 1043 | vc->GetPvs().Merge(right->GetPvs());
|
---|
[440] | 1044 |
|
---|
[462] | 1045 | //-- merge ray sets
|
---|
[564] | 1046 | if (0)
|
---|
| 1047 | {
|
---|
[582] | 1048 | stable_sort(left->mPiercingRays.begin(), left->mPiercingRays.end());
|
---|
| 1049 | stable_sort(right->mPiercingRays.begin(), right->mPiercingRays.end());
|
---|
[440] | 1050 |
|
---|
[582] | 1051 | std::merge(left->mPiercingRays.begin(), left->mPiercingRays.end(),
|
---|
| 1052 | right->mPiercingRays.begin(), right->mPiercingRays.end(),
|
---|
[564] | 1053 | vc->mPiercingRays.begin());
|
---|
| 1054 | }
|
---|
[440] | 1055 |
|
---|
[710] | 1056 | // set only links to child (not from child to parent, maybe not wished!!)
|
---|
| 1057 | vc->mChildren.push_back(left);
|
---|
| 1058 | vc->mChildren.push_back(right);
|
---|
| 1059 |
|
---|
[440] | 1060 | return vc;
|
---|
| 1061 | }
|
---|
| 1062 |
|
---|
[485] | 1063 |
|
---|
[582] | 1064 | ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCellContainer &children) const
|
---|
| 1065 | {
|
---|
| 1066 | ViewCellInterior *vc = new ViewCellInterior();//GenerateViewCell();
|
---|
| 1067 |
|
---|
| 1068 | ViewCellContainer::const_iterator it, it_end = children.end();
|
---|
| 1069 |
|
---|
| 1070 | for (it = children.begin(); it != it_end; ++ it)
|
---|
| 1071 | {
|
---|
| 1072 | // merge pvs
|
---|
[610] | 1073 | vc->GetPvs().Merge((*it)->GetPvs());
|
---|
[710] | 1074 | vc->mChildren.push_back(*it);
|
---|
| 1075 | //vc->SetupChildLink(*it);
|
---|
[582] | 1076 | }
|
---|
| 1077 |
|
---|
| 1078 | return vc;
|
---|
| 1079 | }
|
---|
| 1080 |
|
---|
| 1081 |
|
---|
[480] | 1082 | void ViewCellsManager::SetRenderer(Renderer *renderer)
|
---|
| 1083 | {
|
---|
| 1084 | mRenderer = renderer;
|
---|
| 1085 | }
|
---|
[441] | 1086 |
|
---|
[485] | 1087 |
|
---|
[580] | 1088 | ViewCellsTree *ViewCellsManager::GetViewCellsTree()
|
---|
[440] | 1089 | {
|
---|
[580] | 1090 | return mViewCellsTree;
|
---|
[440] | 1091 | }
|
---|
| 1092 |
|
---|
| 1093 |
|
---|
| 1094 | void ViewCellsManager::SetVisualizationSamples(const int visSamples)
|
---|
| 1095 | {
|
---|
[444] | 1096 | mVisualizationSamples = visSamples;
|
---|
[440] | 1097 | }
|
---|
| 1098 |
|
---|
[485] | 1099 |
|
---|
[440] | 1100 | void ViewCellsManager::SetConstructionSamples(const int constructionSamples)
|
---|
| 1101 | {
|
---|
| 1102 | mConstructionSamples = constructionSamples;
|
---|
| 1103 | }
|
---|
| 1104 |
|
---|
[485] | 1105 |
|
---|
[574] | 1106 | void ViewCellsManager::SetInitialSamples(const int initialSamples)
|
---|
| 1107 | {
|
---|
| 1108 | mInitialSamples = initialSamples;
|
---|
| 1109 | }
|
---|
| 1110 |
|
---|
| 1111 |
|
---|
[440] | 1112 | void ViewCellsManager::SetPostProcessSamples(const int postProcessSamples)
|
---|
| 1113 | {
|
---|
| 1114 | mPostProcessSamples = postProcessSamples;
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
[485] | 1117 |
|
---|
[440] | 1118 | int ViewCellsManager::GetVisualizationSamples() const
|
---|
| 1119 | {
|
---|
| 1120 | return mVisualizationSamples;
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
[485] | 1123 |
|
---|
[440] | 1124 | int ViewCellsManager::GetConstructionSamples() const
|
---|
| 1125 | {
|
---|
| 1126 | return mConstructionSamples;
|
---|
| 1127 | }
|
---|
| 1128 |
|
---|
[485] | 1129 |
|
---|
[440] | 1130 | int ViewCellsManager::GetPostProcessSamples() const
|
---|
| 1131 | {
|
---|
| 1132 | return mPostProcessSamples;
|
---|
| 1133 | }
|
---|
| 1134 |
|
---|
[485] | 1135 |
|
---|
[719] | 1136 | void ViewCellsManager::UpdatePvs()
|
---|
| 1137 | {
|
---|
| 1138 | if (mViewCellPvsIsUpdated)
|
---|
| 1139 | return;
|
---|
| 1140 |
|
---|
| 1141 | mViewCellPvsIsUpdated = true;
|
---|
| 1142 |
|
---|
| 1143 | ViewCellContainer leaves;
|
---|
| 1144 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 1145 |
|
---|
| 1146 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 1147 |
|
---|
| 1148 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 1149 | {
|
---|
| 1150 | mViewCellsTree->PropagatePvs(*it);
|
---|
| 1151 | }
|
---|
| 1152 | }
|
---|
| 1153 |
|
---|
| 1154 |
|
---|
[485] | 1155 | void ViewCellsManager::GetPvsStatistics(PvsStatistics &stat)
|
---|
[467] | 1156 | {
|
---|
[719] | 1157 | // update pvs of view cells tree if necessary
|
---|
| 1158 | UpdatePvs();
|
---|
[467] | 1159 |
|
---|
[719] | 1160 | ViewCellContainer::const_iterator it = mViewCells.begin();
|
---|
| 1161 |
|
---|
| 1162 | stat.viewcells = 0;
|
---|
| 1163 | stat.minPvs = 100000000;
|
---|
| 1164 | stat.maxPvs = 0;
|
---|
| 1165 | stat.avgPvs = 0.0f;
|
---|
| 1166 |
|
---|
| 1167 | for (; it != mViewCells.end(); ++ it)
|
---|
| 1168 | {
|
---|
| 1169 | ViewCell *viewcell = *it;
|
---|
| 1170 |
|
---|
| 1171 | const int pvsSize = mViewCellsTree->GetPvsSize(viewcell);
|
---|
| 1172 |
|
---|
| 1173 | if (pvsSize < stat.minPvs)
|
---|
| 1174 | stat.minPvs = pvsSize;
|
---|
| 1175 | if (pvsSize > stat.maxPvs)
|
---|
| 1176 | stat.maxPvs = pvsSize;
|
---|
| 1177 | stat.avgPvs += pvsSize;
|
---|
| 1178 |
|
---|
| 1179 | ++ stat.viewcells;
|
---|
| 1180 | }
|
---|
| 1181 |
|
---|
| 1182 | if (stat.viewcells)
|
---|
| 1183 | stat.avgPvs/=stat.viewcells;
|
---|
[467] | 1184 | }
|
---|
| 1185 |
|
---|
[485] | 1186 |
|
---|
[480] | 1187 | void ViewCellsManager::PrintPvsStatistics(ostream &s)
|
---|
[467] | 1188 | {
|
---|
| 1189 | s<<"############# Viewcell PVS STAT ##################\n";
|
---|
| 1190 | PvsStatistics pvsStat;
|
---|
| 1191 | GetPvsStatistics(pvsStat);
|
---|
| 1192 | s<<"#AVG_PVS\n"<<pvsStat.avgPvs<<endl;
|
---|
| 1193 | s<<"#MAX_PVS\n"<<pvsStat.maxPvs<<endl;
|
---|
| 1194 | s<<"#MIN_PVS\n"<<pvsStat.minPvs<<endl;
|
---|
| 1195 | }
|
---|
| 1196 |
|
---|
[485] | 1197 |
|
---|
[532] | 1198 | int ViewCellsManager::CastBeam(Beam &beam)
|
---|
| 1199 | {
|
---|
| 1200 | return 0;
|
---|
| 1201 | }
|
---|
| 1202 |
|
---|
[547] | 1203 |
|
---|
[468] | 1204 | ViewCellContainer &ViewCellsManager::GetViewCells()
|
---|
| 1205 | {
|
---|
| 1206 | return mViewCells;
|
---|
| 1207 | }
|
---|
| 1208 |
|
---|
[485] | 1209 |
|
---|
[487] | 1210 | void ViewCellsManager::SetViewSpaceBox(const AxisAlignedBox3 &box)
|
---|
| 1211 | {
|
---|
[542] | 1212 | mViewSpaceBox = box;
|
---|
[591] | 1213 |
|
---|
[660] | 1214 | CreateClipPlane();
|
---|
[591] | 1215 |
|
---|
[577] | 1216 | mTotalAreaValid = false;
|
---|
[487] | 1217 | }
|
---|
| 1218 |
|
---|
| 1219 |
|
---|
[660] | 1220 | void ViewCellsManager::CreateClipPlane()
|
---|
[591] | 1221 | {
|
---|
| 1222 | int axis = 0;
|
---|
[667] | 1223 | float pos;
|
---|
[591] | 1224 |
|
---|
[667] | 1225 | environment->GetFloatValue("ViewCells.Visualization.clipPlanePos", pos);
|
---|
[591] | 1226 |
|
---|
[667] | 1227 | Vector3 point = mViewSpaceBox.Min() + mViewSpaceBox.Size() * pos;
|
---|
| 1228 |
|
---|
[660] | 1229 | if (mUseClipPlaneForViz)
|
---|
| 1230 | environment->GetIntValue("ViewCells.Visualization.clipPlaneAxis", axis);
|
---|
[591] | 1231 |
|
---|
| 1232 | Vector3 normal(0,0,0);
|
---|
| 1233 | normal[axis] = 1;
|
---|
| 1234 |
|
---|
[660] | 1235 | mClipPlane = Plane3(normal, point);
|
---|
[591] | 1236 | }
|
---|
| 1237 |
|
---|
| 1238 |
|
---|
[519] | 1239 | AxisAlignedBox3 ViewCellsManager::GetViewSpaceBox() const
|
---|
| 1240 | {
|
---|
[542] | 1241 | return mViewSpaceBox;
|
---|
[519] | 1242 | }
|
---|
| 1243 |
|
---|
| 1244 |
|
---|
[480] | 1245 | void ViewCellsManager::ResetViewCells()
|
---|
| 1246 | {
|
---|
| 1247 | mViewCells.clear();
|
---|
[590] | 1248 |
|
---|
[480] | 1249 | CollectViewCells();
|
---|
[590] | 1250 |
|
---|
[660] | 1251 | mCurrentViewCellsStats.Reset();
|
---|
[480] | 1252 | EvaluateViewCellsStats();
|
---|
[590] | 1253 |
|
---|
[520] | 1254 | // has to be recomputed
|
---|
[480] | 1255 | mTotalAreaValid = false;
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
[485] | 1258 |
|
---|
[547] | 1259 | int ViewCellsManager::GetMaxPvsSize() const
|
---|
| 1260 | {
|
---|
| 1261 | return mMaxPvsSize;
|
---|
| 1262 | }
|
---|
| 1263 |
|
---|
[590] | 1264 |
|
---|
[563] | 1265 | void
|
---|
| 1266 | ViewCellsManager::AddSampleContributions(const VssRayContainer &rays)
|
---|
| 1267 | {
|
---|
| 1268 | if (!ViewCellsConstructed())
|
---|
| 1269 | return;
|
---|
[564] | 1270 |
|
---|
[563] | 1271 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
[564] | 1272 |
|
---|
[563] | 1273 | for (it = rays.begin(); it != it_end; ++ it) {
|
---|
| 1274 | AddSampleContributions(*(*it));
|
---|
| 1275 | }
|
---|
| 1276 | }
|
---|
[547] | 1277 |
|
---|
[581] | 1278 |
|
---|
[562] | 1279 | int ViewCellsManager::GetMinPvsSize() const
|
---|
| 1280 | {
|
---|
| 1281 | return mMinPvsSize;
|
---|
| 1282 | }
|
---|
| 1283 |
|
---|
| 1284 |
|
---|
| 1285 |
|
---|
[561] | 1286 | float ViewCellsManager::GetMaxPvsRatio() const
|
---|
| 1287 | {
|
---|
| 1288 | return mMaxPvsRatio;
|
---|
| 1289 | }
|
---|
| 1290 |
|
---|
| 1291 |
|
---|
[563] | 1292 | void
|
---|
| 1293 | ViewCellsManager::AddSampleContributions(VssRay &ray)
|
---|
[468] | 1294 | {
|
---|
[563] | 1295 | // assumes viewcells have been stored...
|
---|
| 1296 | ViewCellContainer *viewcells = &ray.mViewCells;
|
---|
| 1297 | ViewCellContainer::const_iterator it;
|
---|
| 1298 | for (it = viewcells->begin(); it != viewcells->end(); ++it) {
|
---|
| 1299 | ViewCell *viewcell = *it;
|
---|
[569] | 1300 | if (viewcell->GetValid()) {
|
---|
| 1301 | // if ray not outside of view space
|
---|
| 1302 | viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
|
---|
| 1303 | }
|
---|
[563] | 1304 | }
|
---|
| 1305 | }
|
---|
[503] | 1306 |
|
---|
[574] | 1307 |
|
---|
[570] | 1308 | float ViewCellsManager::ComputeSampleContributions(VssRay &ray,
|
---|
[574] | 1309 | const bool addRays,
|
---|
| 1310 | const bool storeViewCells)
|
---|
[563] | 1311 | {
|
---|
[570] | 1312 | ViewCellContainer viewcells;
|
---|
[564] | 1313 |
|
---|
[570] | 1314 | ray.mPvsContribution = 0;
|
---|
| 1315 | ray.mRelativePvsContribution = 0.0f;
|
---|
[485] | 1316 |
|
---|
[570] | 1317 | static Ray hray;
|
---|
[564] | 1318 | hray.Init(ray);
|
---|
| 1319 | //hray.mFlags |= Ray::CULL_BACKFACES;
|
---|
| 1320 | //Ray hray(ray);
|
---|
| 1321 |
|
---|
| 1322 | float tmin = 0, tmax = 1.0;
|
---|
| 1323 |
|
---|
| 1324 | if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
|
---|
[565] | 1325 | return 0;
|
---|
[564] | 1326 |
|
---|
| 1327 | Vector3 origin = hray.Extrap(tmin);
|
---|
| 1328 | Vector3 termination = hray.Extrap(tmax);
|
---|
| 1329 |
|
---|
| 1330 | CastLineSegment(origin, termination, viewcells);
|
---|
[664] | 1331 |
|
---|
[570] | 1332 | // copy viewcells memory efficiently
|
---|
[574] | 1333 | //const bool storeViewcells = !addRays;
|
---|
[564] | 1334 |
|
---|
[574] | 1335 | if (storeViewCells)
|
---|
[570] | 1336 | {
|
---|
| 1337 | ray.mViewCells.reserve(viewcells.size());
|
---|
| 1338 | ray.mViewCells = viewcells;
|
---|
| 1339 | }
|
---|
[564] | 1340 |
|
---|
[570] | 1341 | ViewCellContainer::const_iterator it = viewcells.begin();
|
---|
[564] | 1342 |
|
---|
[664] | 1343 | for (; it != viewcells.end(); ++ it)
|
---|
| 1344 | {
|
---|
| 1345 | ViewCell *viewcell = *it;
|
---|
[694] | 1346 |
|
---|
[708] | 1347 | if (viewcell->GetValid())
|
---|
[664] | 1348 | {
|
---|
[694] | 1349 | // HACK
|
---|
[706] | 1350 | #if TEST_EMPTY_VIEW_CELLS
|
---|
[694] | 1351 | for (int i = 0; i < mEmptyViewCells.size(); ++i)
|
---|
| 1352 | {
|
---|
| 1353 | if (viewcell == mEmptyViewCells[i])
|
---|
| 1354 | {
|
---|
| 1355 | viewcell->mPiercingRays.push_back(new Ray(ray));
|
---|
| 1356 | Debug << "empty view cell ray found: " << ray.mOriginObject << ", " << ray.mTerminationObject << endl;
|
---|
| 1357 | }
|
---|
| 1358 | }
|
---|
[706] | 1359 | #endif
|
---|
[664] | 1360 | // if ray not outside of view space
|
---|
| 1361 | float contribution;
|
---|
[666] | 1362 | if (ray.mTerminationObject &&
|
---|
| 1363 | viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
|
---|
[664] | 1364 | ray.mPdf,
|
---|
| 1365 | contribution))
|
---|
| 1366 | {
|
---|
| 1367 | ++ ray.mPvsContribution;
|
---|
| 1368 | ray.mRelativePvsContribution += contribution;
|
---|
| 1369 | }
|
---|
[718] | 1370 |
|
---|
| 1371 | //for directional sampling it is important to count only contributions
|
---|
| 1372 | // made in one direction!!!
|
---|
| 1373 | // the other contributions of this sample will be counted for the oposite ray!
|
---|
| 1374 | #if 0
|
---|
[666] | 1375 | if (ray.mOriginObject &&
|
---|
| 1376 | viewcell->GetPvs().GetSampleContribution(ray.mOriginObject,
|
---|
| 1377 | ray.mPdf,
|
---|
| 1378 | contribution))
|
---|
| 1379 | {
|
---|
| 1380 | ++ ray.mPvsContribution;
|
---|
| 1381 | ray.mRelativePvsContribution += contribution;
|
---|
| 1382 | }
|
---|
[718] | 1383 | #endif
|
---|
[664] | 1384 | }
|
---|
[569] | 1385 | }
|
---|
[564] | 1386 |
|
---|
[664] | 1387 |
|
---|
| 1388 | if (addRays)
|
---|
| 1389 | {
|
---|
| 1390 | for (it = viewcells.begin(); it != viewcells.end(); ++ it)
|
---|
| 1391 | {
|
---|
| 1392 | ViewCell *viewcell = *it;
|
---|
| 1393 |
|
---|
[706] | 1394 | if (viewcell->GetValid())
|
---|
[664] | 1395 | {
|
---|
| 1396 | // if ray not outside of view space
|
---|
[666] | 1397 | if (ray.mTerminationObject)
|
---|
| 1398 | viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
|
---|
[718] | 1399 |
|
---|
| 1400 | #if 0
|
---|
[666] | 1401 | if (ray.mOriginObject)
|
---|
| 1402 | viewcell->GetPvs().AddSample(ray.mOriginObject, ray.mPdf);
|
---|
[718] | 1403 | #endif
|
---|
[664] | 1404 | }
|
---|
| 1405 | }
|
---|
[563] | 1406 | }
|
---|
[570] | 1407 |
|
---|
| 1408 | return ray.mRelativePvsContribution;
|
---|
[468] | 1409 | }
|
---|
| 1410 |
|
---|
| 1411 |
|
---|
[469] | 1412 | void ViewCellsManager::GetRaySets(const VssRayContainer &sourceRays,
|
---|
[485] | 1413 | const int maxSize,
|
---|
[503] | 1414 | VssRayContainer &usedRays,
|
---|
[485] | 1415 | VssRayContainer *savedRays) const
|
---|
[469] | 1416 | {
|
---|
[485] | 1417 | const int limit = min(maxSize, (int)sourceRays.size());
|
---|
[473] | 1418 | const float prop = (float)limit / ((float)sourceRays.size() + Limits::Small);
|
---|
[469] | 1419 |
|
---|
[485] | 1420 | VssRayContainer::const_iterator it, it_end = sourceRays.end();
|
---|
[469] | 1421 | for (it = sourceRays.begin(); it != it_end; ++ it)
|
---|
| 1422 | {
|
---|
[473] | 1423 | if (Random(1.0f) < prop)
|
---|
[485] | 1424 | usedRays.push_back(*it);
|
---|
| 1425 | else if (savedRays)
|
---|
| 1426 | savedRays->push_back(*it);
|
---|
[469] | 1427 | }
|
---|
| 1428 | }
|
---|
| 1429 |
|
---|
[477] | 1430 |
|
---|
[605] | 1431 | float ViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
|
---|
| 1432 | {
|
---|
| 1433 | return mViewCellsTree->GetPvsSize(viewCell) * objRendercost;
|
---|
| 1434 | }
|
---|
| 1435 |
|
---|
| 1436 |
|
---|
[477] | 1437 | float ViewCellsManager::GetAccVcArea()
|
---|
[470] | 1438 | {
|
---|
| 1439 | // if already computed
|
---|
| 1440 | if (mTotalAreaValid)
|
---|
| 1441 | return mTotalArea;
|
---|
| 1442 |
|
---|
| 1443 | mTotalArea = 0;
|
---|
| 1444 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1445 |
|
---|
| 1446 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
[480] | 1447 | {
|
---|
| 1448 | //Debug << "area: " << GetArea(*it);
|
---|
[470] | 1449 | mTotalArea += GetArea(*it);
|
---|
[480] | 1450 | }
|
---|
| 1451 |
|
---|
[470] | 1452 | mTotalAreaValid = true;
|
---|
| 1453 |
|
---|
| 1454 | return mTotalArea;
|
---|
| 1455 | }
|
---|
| 1456 |
|
---|
[482] | 1457 |
|
---|
[475] | 1458 | void ViewCellsManager::PrintStatistics(ostream &s) const
|
---|
| 1459 | {
|
---|
[660] | 1460 | s << mCurrentViewCellsStats << endl;
|
---|
[475] | 1461 | }
|
---|
| 1462 |
|
---|
[477] | 1463 |
|
---|
[508] | 1464 | void ViewCellsManager::CreateUniqueViewCellIds()
|
---|
[482] | 1465 | {
|
---|
[651] | 1466 | if (ViewCellsTreeConstructed())
|
---|
| 1467 | mViewCellsTree->CreateUniqueViewCellsIds();
|
---|
| 1468 | else
|
---|
| 1469 | for (int i = 0; i < (int)mViewCells.size(); ++ i)
|
---|
| 1470 | mViewCells[i]->SetId(i);
|
---|
[508] | 1471 | }
|
---|
| 1472 |
|
---|
| 1473 |
|
---|
| 1474 | void ViewCellsManager::ExportViewCellsForViz(Exporter *exporter) const
|
---|
| 1475 | {
|
---|
[482] | 1476 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1477 |
|
---|
| 1478 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1479 | {
|
---|
[574] | 1480 | if (!mOnlyValidViewCells || (*it)->GetValid())
|
---|
| 1481 | {
|
---|
[582] | 1482 | ExportColor(exporter, *it);
|
---|
[591] | 1483 | ExportViewCellGeometry(exporter, *it,
|
---|
[660] | 1484 | mUseClipPlaneForViz ? &mClipPlane : NULL);
|
---|
[574] | 1485 | }
|
---|
[482] | 1486 | }
|
---|
| 1487 | }
|
---|
| 1488 |
|
---|
| 1489 |
|
---|
[517] | 1490 | void ViewCellsManager::CreateViewCellMeshes()
|
---|
[503] | 1491 | {
|
---|
| 1492 | // convert to meshes
|
---|
| 1493 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1494 |
|
---|
| 1495 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1496 | {
|
---|
[517] | 1497 | if (!(*it)->GetMesh())
|
---|
| 1498 | CreateMesh(*it);
|
---|
[503] | 1499 | }
|
---|
| 1500 | }
|
---|
| 1501 |
|
---|
[508] | 1502 |
|
---|
[564] | 1503 | bool ViewCellsManager::ExportViewCells(const string filename)
|
---|
| 1504 | {
|
---|
[508] | 1505 | return false;
|
---|
| 1506 | }
|
---|
| 1507 |
|
---|
| 1508 |
|
---|
[610] | 1509 | void ViewCellsManager::CollectViewCells(const int n)
|
---|
[508] | 1510 | {
|
---|
[610] | 1511 | mNumActiveViewCells = n;
|
---|
| 1512 | mViewCells.clear();
|
---|
| 1513 | CollectViewCells();
|
---|
| 1514 | }
|
---|
[508] | 1515 |
|
---|
| 1516 |
|
---|
[660] | 1517 | void ViewCellsManager::SetViewCellsActive()
|
---|
| 1518 | {
|
---|
| 1519 | ++ ViewCell::sLastUpdated;
|
---|
| 1520 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1521 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1522 | {
|
---|
| 1523 | (*it)->SetActive();
|
---|
| 1524 | }
|
---|
| 1525 | }
|
---|
[508] | 1526 |
|
---|
[440] | 1527 | /**********************************************************************/
|
---|
| 1528 | /* BspViewCellsManager implementation */
|
---|
| 1529 | /**********************************************************************/
|
---|
| 1530 |
|
---|
[482] | 1531 |
|
---|
[574] | 1532 | BspViewCellsManager::BspViewCellsManager(BspTree *bspTree):
|
---|
| 1533 | ViewCellsManager(), mBspTree(bspTree)
|
---|
[440] | 1534 | {
|
---|
[574] | 1535 | environment->GetIntValue("BspTree.Construction.samples", mInitialSamples);
|
---|
[587] | 1536 | mBspTree->SetViewCellsManager(this);
|
---|
[590] | 1537 | mBspTree->mViewCellsTree = mViewCellsTree;
|
---|
[440] | 1538 | }
|
---|
| 1539 |
|
---|
[462] | 1540 |
|
---|
[440] | 1541 | bool BspViewCellsManager::ViewCellsConstructed() const
|
---|
| 1542 | {
|
---|
| 1543 | return mBspTree->GetRoot() != NULL;
|
---|
| 1544 | }
|
---|
| 1545 |
|
---|
[480] | 1546 |
|
---|
[440] | 1547 | ViewCell *BspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
| 1548 | {
|
---|
| 1549 | return new BspViewCell(mesh);
|
---|
| 1550 | }
|
---|
| 1551 |
|
---|
[480] | 1552 |
|
---|
[574] | 1553 | int BspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
| 1554 | const VssRayContainer &rays)
|
---|
[440] | 1555 | {
|
---|
[441] | 1556 | // if view cells were already constructed
|
---|
[440] | 1557 | if (ViewCellsConstructed())
|
---|
| 1558 | return 0;
|
---|
| 1559 |
|
---|
| 1560 | int sampleContributions = 0;
|
---|
[503] | 1561 |
|
---|
[469] | 1562 | // construct view cells using the collected samples
|
---|
| 1563 | RayContainer constructionRays;
|
---|
| 1564 | VssRayContainer savedRays;
|
---|
[440] | 1565 |
|
---|
[574] | 1566 | const int limit = min(mInitialSamples, (int)rays.size());
|
---|
[440] | 1567 |
|
---|
[469] | 1568 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
[448] | 1569 |
|
---|
[473] | 1570 | const float prop = (float)limit / ((float)rays.size() + Limits::Small);
|
---|
| 1571 |
|
---|
[469] | 1572 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 1573 | {
|
---|
[473] | 1574 | if (Random(1.0f) < prop)
|
---|
[469] | 1575 | constructionRays.push_back(new Ray(*(*it)));
|
---|
| 1576 | else
|
---|
| 1577 | savedRays.push_back(*it);
|
---|
| 1578 | }
|
---|
[440] | 1579 |
|
---|
[503] | 1580 | if (mViewCells.empty())
|
---|
| 1581 | {
|
---|
| 1582 | // no view cells loaded
|
---|
[587] | 1583 | mBspTree->Construct(objects, constructionRays, &mViewSpaceBox);
|
---|
[469] | 1584 | // collect final view cells
|
---|
| 1585 | mBspTree->CollectViewCells(mViewCells);
|
---|
[503] | 1586 | }
|
---|
[469] | 1587 | else
|
---|
| 1588 | {
|
---|
| 1589 | mBspTree->Construct(mViewCells);
|
---|
| 1590 | }
|
---|
| 1591 |
|
---|
| 1592 | // destroy rays created only for construction
|
---|
| 1593 | CLEAR_CONTAINER(constructionRays);
|
---|
| 1594 |
|
---|
[440] | 1595 | Debug << mBspTree->GetStatistics() << endl;
|
---|
[503] | 1596 |
|
---|
| 1597 | //EvaluateViewCellsStats();
|
---|
[660] | 1598 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
[480] | 1599 |
|
---|
[469] | 1600 | // recast rest of the rays
|
---|
[649] | 1601 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 1602 | ComputeSampleContributions(savedRays, true, false);
|
---|
[469] | 1603 |
|
---|
[719] | 1604 | // real meshes are contructed at this stage
|
---|
| 1605 | if (0)
|
---|
| 1606 | {
|
---|
| 1607 | cout << "finalizing view cells ... ";
|
---|
| 1608 | FinalizeViewCells(true);
|
---|
| 1609 | cout << "finished" << endl;
|
---|
| 1610 | }
|
---|
| 1611 |
|
---|
[440] | 1612 | return sampleContributions;
|
---|
| 1613 | }
|
---|
| 1614 |
|
---|
[503] | 1615 |
|
---|
[480] | 1616 | void BspViewCellsManager::CollectViewCells()
|
---|
| 1617 | {
|
---|
[587] | 1618 | // view cells tree constructed
|
---|
| 1619 | if (!ViewCellsTreeConstructed())
|
---|
[591] | 1620 | {
|
---|
[587] | 1621 | mBspTree->CollectViewCells(mViewCells);
|
---|
| 1622 | }
|
---|
| 1623 | else
|
---|
| 1624 | {
|
---|
| 1625 | // we can use the view cells tree hierarchy to get the right set
|
---|
[591] | 1626 | mViewCellsTree->CollectBestViewCellSet(mViewCells,
|
---|
[600] | 1627 | mNumActiveViewCells);
|
---|
[587] | 1628 | }
|
---|
[480] | 1629 | }
|
---|
[468] | 1630 |
|
---|
[503] | 1631 |
|
---|
[471] | 1632 | float BspViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
[440] | 1633 | {
|
---|
[471] | 1634 | // compute view cell area as subsititute for probability
|
---|
[591] | 1635 | if (1)
|
---|
| 1636 | return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
|
---|
| 1637 | else
|
---|
| 1638 | return GetArea(viewCell) / GetAccVcArea();
|
---|
[466] | 1639 | }
|
---|
[440] | 1640 |
|
---|
[503] | 1641 |
|
---|
[440] | 1642 |
|
---|
[468] | 1643 | int BspViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
| 1644 | const Vector3 &termination,
|
---|
| 1645 | ViewCellContainer &viewcells)
|
---|
[440] | 1646 | {
|
---|
[468] | 1647 | return mBspTree->CastLineSegment(origin, termination, viewcells);
|
---|
| 1648 | }
|
---|
| 1649 |
|
---|
| 1650 |
|
---|
[503] | 1651 | int BspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
[468] | 1652 | const VssRayContainer &rays)
|
---|
| 1653 | {
|
---|
[469] | 1654 | if (!ViewCellsConstructed())
|
---|
[441] | 1655 | {
|
---|
| 1656 | Debug << "view cells not constructed" << endl;
|
---|
| 1657 | return 0;
|
---|
| 1658 | }
|
---|
[590] | 1659 |
|
---|
| 1660 | // view cells already finished before post processing step (i.e. because they were loaded)
|
---|
| 1661 | if (mViewCellsFinished)
|
---|
| 1662 | {
|
---|
| 1663 | FinalizeViewCells(true);
|
---|
| 1664 | EvaluateViewCellsStats();
|
---|
| 1665 |
|
---|
| 1666 | return 0;
|
---|
| 1667 | }
|
---|
| 1668 |
|
---|
[440] | 1669 | //-- post processing of bsp view cells
|
---|
[600] | 1670 |
|
---|
[440] | 1671 | int vcSize = 0;
|
---|
| 1672 | int pvsSize = 0;
|
---|
| 1673 |
|
---|
[591] | 1674 | //-- merge view cells
|
---|
[649] | 1675 | cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
|
---|
| 1676 | long startTime = GetTime();
|
---|
| 1677 |
|
---|
| 1678 | VssRayContainer postProcessRays;
|
---|
| 1679 | GetRaySets(rays, mPostProcessSamples, postProcessRays);
|
---|
| 1680 |
|
---|
[592] | 1681 | if (mMergeViewCells)
|
---|
[591] | 1682 | {
|
---|
[660] | 1683 | cout << "constructing visibility based merge tree" << endl;
|
---|
[650] | 1684 | mViewCellsTree->ConstructMergeTree(rays, objects);
|
---|
[649] | 1685 | }
|
---|
| 1686 | else
|
---|
| 1687 | {
|
---|
[660] | 1688 | cout << "constructing spatial merge tree" << endl;
|
---|
[649] | 1689 | // create spatial merge hierarchy
|
---|
[650] | 1690 | mViewCellsTree->SetRoot(ConstructSpatialMergeTree(mBspTree->GetRoot()));
|
---|
[649] | 1691 | }
|
---|
[440] | 1692 |
|
---|
[660] | 1693 | // export statistics after merge
|
---|
[666] | 1694 | if (1)
|
---|
| 1695 | {
|
---|
| 1696 | char mstats[100];
|
---|
| 1697 | environment->GetStringValue("ViewCells.mergeStats", mstats);
|
---|
| 1698 | mViewCellsTree->ExportStats(mstats);
|
---|
| 1699 | }
|
---|
[650] | 1700 |
|
---|
[649] | 1701 | //-- stats and visualizations
|
---|
| 1702 | cout << "finished" << endl;
|
---|
[650] | 1703 | cout << "merged view cells in "
|
---|
[649] | 1704 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
[440] | 1705 |
|
---|
[650] | 1706 | Debug << "Postprocessing: Merged view cells in "
|
---|
[649] | 1707 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
|
---|
| 1708 |
|
---|
[503] | 1709 |
|
---|
[660] | 1710 | //-- visualization and statistics
|
---|
[600] | 1711 | // reset view cells and stats
|
---|
[480] | 1712 | ResetViewCells();
|
---|
[660] | 1713 | Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
|
---|
[600] | 1714 |
|
---|
[660] | 1715 |
|
---|
[600] | 1716 | int savedColorCode = mColorCode;
|
---|
[605] | 1717 |
|
---|
[600] | 1718 | //BspLeaf::NewMail();
|
---|
| 1719 | if (1) // export merged view cells
|
---|
| 1720 | {
|
---|
| 1721 | mColorCode = 0;
|
---|
[605] | 1722 |
|
---|
[600] | 1723 | Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
|
---|
[605] | 1724 |
|
---|
[600] | 1725 |
|
---|
| 1726 | cout << "exporting view cells after merge ... ";
|
---|
| 1727 |
|
---|
| 1728 | if (exporter)
|
---|
| 1729 | {
|
---|
| 1730 | if (mExportGeometry)
|
---|
| 1731 | exporter->ExportGeometry(objects);
|
---|
| 1732 |
|
---|
| 1733 | //exporter->SetWireframe();
|
---|
| 1734 | exporter->SetFilled();
|
---|
| 1735 | ExportViewCellsForViz(exporter);
|
---|
| 1736 |
|
---|
| 1737 |
|
---|
| 1738 | delete exporter;
|
---|
| 1739 | }
|
---|
| 1740 | cout << "finished" << endl;
|
---|
| 1741 | }
|
---|
| 1742 |
|
---|
| 1743 | if (1) // export merged view cells
|
---|
| 1744 | {
|
---|
| 1745 | mColorCode = 1;
|
---|
| 1746 |
|
---|
| 1747 | Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.x3d");
|
---|
| 1748 |
|
---|
| 1749 | cout << "exporting view cells after merge (pvs size) ... ";
|
---|
| 1750 |
|
---|
| 1751 | if (exporter)
|
---|
| 1752 | {
|
---|
| 1753 | //exporter->SetWireframe();
|
---|
| 1754 |
|
---|
| 1755 | if (mExportGeometry)
|
---|
| 1756 | exporter->ExportGeometry(objects);
|
---|
| 1757 |
|
---|
| 1758 | //exporter->SetWireframe();
|
---|
| 1759 | exporter->SetFilled();
|
---|
| 1760 | ExportViewCellsForViz(exporter);
|
---|
| 1761 |
|
---|
| 1762 | delete exporter;
|
---|
| 1763 | }
|
---|
| 1764 | cout << "finished" << endl;
|
---|
| 1765 | }
|
---|
| 1766 |
|
---|
[693] | 1767 |
|
---|
| 1768 | // only for testing
|
---|
| 1769 | TestSubdivision();
|
---|
| 1770 |
|
---|
[600] | 1771 | mColorCode = savedColorCode;
|
---|
| 1772 |
|
---|
[719] | 1773 | // compute final meshes and volume / area
|
---|
| 1774 | if (1) FinalizeViewCells(true);
|
---|
| 1775 |
|
---|
[587] | 1776 | // write view cells to disc
|
---|
| 1777 | if (mExportViewCells)
|
---|
| 1778 | {
|
---|
| 1779 | char buff[100];
|
---|
| 1780 | environment->GetStringValue("ViewCells.filename", buff);
|
---|
| 1781 | string vcFilename(buff);
|
---|
| 1782 |
|
---|
| 1783 | ExportViewCells(buff);
|
---|
| 1784 | }
|
---|
| 1785 |
|
---|
[650] | 1786 | return 0;
|
---|
[440] | 1787 | }
|
---|
| 1788 |
|
---|
[477] | 1789 |
|
---|
| 1790 | BspViewCellsManager::~BspViewCellsManager()
|
---|
| 1791 | {
|
---|
| 1792 | }
|
---|
| 1793 |
|
---|
| 1794 |
|
---|
[440] | 1795 | int BspViewCellsManager::GetType() const
|
---|
| 1796 | {
|
---|
| 1797 | return BSP;
|
---|
| 1798 | }
|
---|
| 1799 |
|
---|
| 1800 |
|
---|
| 1801 | void BspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
[466] | 1802 | const VssRayContainer &sampleRays)
|
---|
[440] | 1803 | {
|
---|
[469] | 1804 | if (!ViewCellsConstructed())
|
---|
| 1805 | return;
|
---|
[574] | 1806 |
|
---|
[600] | 1807 | int savedColorCode = mColorCode;
|
---|
| 1808 |
|
---|
| 1809 |
|
---|
| 1810 |
|
---|
| 1811 | if (1) // export final view cells
|
---|
[440] | 1812 | {
|
---|
[600] | 1813 | mColorCode = 1;
|
---|
[587] | 1814 |
|
---|
[712] | 1815 | Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
|
---|
[600] | 1816 |
|
---|
| 1817 | cout << "exporting view cells after merge (pvs size) ... ";
|
---|
[587] | 1818 |
|
---|
[469] | 1819 | if (exporter)
|
---|
[440] | 1820 | {
|
---|
[600] | 1821 | //exporter->SetWireframe();
|
---|
| 1822 |
|
---|
| 1823 | if (mExportGeometry)
|
---|
| 1824 | exporter->ExportGeometry(objects);
|
---|
[590] | 1825 |
|
---|
[600] | 1826 | //exporter->SetWireframe();
|
---|
| 1827 | exporter->SetFilled();
|
---|
[508] | 1828 | ExportViewCellsForViz(exporter);
|
---|
[587] | 1829 |
|
---|
[469] | 1830 | delete exporter;
|
---|
[440] | 1831 | }
|
---|
[469] | 1832 | cout << "finished" << endl;
|
---|
[503] | 1833 | }
|
---|
[477] | 1834 |
|
---|
[600] | 1835 | mColorCode = savedColorCode;
|
---|
| 1836 |
|
---|
[469] | 1837 | //-- visualization of the BSP splits
|
---|
| 1838 | bool exportSplits = false;
|
---|
| 1839 | environment->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
|
---|
[503] | 1840 |
|
---|
[469] | 1841 | if (exportSplits)
|
---|
[440] | 1842 | {
|
---|
[469] | 1843 | cout << "exporting splits ... ";
|
---|
[477] | 1844 | ExportSplits(objects);
|
---|
[469] | 1845 | cout << "finished" << endl;
|
---|
[440] | 1846 | }
|
---|
[477] | 1847 |
|
---|
[600] | 1848 | // export single view cells
|
---|
[477] | 1849 | ExportBspPvs(objects);
|
---|
[440] | 1850 | }
|
---|
| 1851 |
|
---|
| 1852 |
|
---|
[503] | 1853 | inline bool vc_gt(ViewCell *a, ViewCell *b)
|
---|
| 1854 | {
|
---|
[440] | 1855 | return a->GetPvs().GetSize() > b->GetPvs().GetSize();
|
---|
| 1856 | }
|
---|
| 1857 |
|
---|
[477] | 1858 |
|
---|
| 1859 | void BspViewCellsManager::ExportSplits(const ObjectContainer &objects)
|
---|
[440] | 1860 | {
|
---|
| 1861 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
| 1862 |
|
---|
[503] | 1863 | if (exporter)
|
---|
| 1864 | {
|
---|
[598] | 1865 | //exporter->SetFilled();
|
---|
| 1866 |
|
---|
| 1867 | if (mExportGeometry)
|
---|
| 1868 | exporter->ExportGeometry(objects);
|
---|
| 1869 |
|
---|
[503] | 1870 | Material m;
|
---|
[440] | 1871 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 1872 | exporter->SetForcedMaterial(m);
|
---|
| 1873 | exporter->SetWireframe();
|
---|
[503] | 1874 |
|
---|
[440] | 1875 | exporter->ExportBspSplits(*mBspTree, true);
|
---|
| 1876 |
|
---|
[482] | 1877 | //NOTE: take forced material, else big scenes cannot be viewed
|
---|
[440] | 1878 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 1879 | exporter->SetForcedMaterial(m);
|
---|
[482] | 1880 | //exporter->ResetForcedMaterial();
|
---|
| 1881 |
|
---|
[440] | 1882 | delete exporter;
|
---|
| 1883 | }
|
---|
| 1884 | }
|
---|
| 1885 |
|
---|
[477] | 1886 |
|
---|
| 1887 | void BspViewCellsManager::ExportBspPvs(const ObjectContainer &objects)
|
---|
[440] | 1888 | {
|
---|
[477] | 1889 | const int leafOut = 10;
|
---|
[503] | 1890 |
|
---|
[477] | 1891 | ViewCell::NewMail();
|
---|
[440] | 1892 |
|
---|
[477] | 1893 | //-- some rays for output
|
---|
| 1894 | const int raysOut = min((int)mBspRays.size(), mVisualizationSamples);
|
---|
[503] | 1895 |
|
---|
[477] | 1896 | cout << "visualization using " << mVisualizationSamples << " samples" << endl;
|
---|
| 1897 | Debug << "\nOutput view cells: " << endl;
|
---|
[503] | 1898 |
|
---|
[478] | 1899 | // sort view cells to get largest view cells
|
---|
[587] | 1900 | if (0)
|
---|
| 1901 | stable_sort(mViewCells.begin(), mViewCells.end(), vc_gt);
|
---|
| 1902 |
|
---|
[503] | 1903 | int limit = min(leafOut, (int)mViewCells.size());
|
---|
| 1904 |
|
---|
[478] | 1905 | for (int i = 0; i < limit; ++ i)
|
---|
[477] | 1906 | {
|
---|
[478] | 1907 | cout << "creating output for view cell " << i << " ... ";
|
---|
| 1908 | VssRayContainer vcRays;
|
---|
| 1909 | Intersectable::NewMail();
|
---|
[582] | 1910 | ViewCell *vc;
|
---|
| 1911 |
|
---|
| 1912 | if (0)
|
---|
| 1913 | vc = mViewCells[i];
|
---|
| 1914 | else
|
---|
| 1915 | vc = mViewCells[Random((int)mViewCells.size())];
|
---|
| 1916 |
|
---|
[478] | 1917 | cout << "creating output for view cell " << i << " ... ";
|
---|
[469] | 1918 |
|
---|
[587] | 1919 | if(0)
|
---|
[477] | 1920 | {
|
---|
[587] | 1921 | // check whether we can add the current ray to the output rays
|
---|
| 1922 | for (int k = 0; k < raysOut; ++ k)
|
---|
[477] | 1923 | {
|
---|
[587] | 1924 | BspRay *ray = mBspRays[k];
|
---|
| 1925 | for (int j = 0; j < (int)ray->intersections.size(); ++ j)
|
---|
| 1926 | {
|
---|
| 1927 | BspLeaf *leaf = ray->intersections[j].mLeaf;
|
---|
| 1928 | if (vc == leaf->GetViewCell())
|
---|
| 1929 | vcRays.push_back(ray->vssRay);
|
---|
| 1930 | }
|
---|
[477] | 1931 | }
|
---|
| 1932 | }
|
---|
[587] | 1933 |
|
---|
[478] | 1934 | //bspLeaves[j]->Mail();
|
---|
| 1935 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
[477] | 1936 |
|
---|
[478] | 1937 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
[503] | 1938 |
|
---|
[478] | 1939 | exporter->SetWireframe();
|
---|
[477] | 1940 |
|
---|
[478] | 1941 | Material m;//= RandomMaterial();
|
---|
| 1942 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 1943 | exporter->SetForcedMaterial(m);
|
---|
[477] | 1944 |
|
---|
[587] | 1945 | ExportViewCellGeometry(exporter, vc);
|
---|
| 1946 |
|
---|
[478] | 1947 | // export rays piercing this view cell
|
---|
| 1948 | exporter->ExportRays(vcRays, RgbColor(0, 1, 0));
|
---|
[580] | 1949 |
|
---|
[478] | 1950 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 1951 | exporter->SetForcedMaterial(m);
|
---|
[440] | 1952 |
|
---|
[478] | 1953 | ObjectPvsMap::const_iterator it,
|
---|
| 1954 | it_end = vc->GetPvs().mEntries.end();
|
---|
[440] | 1955 |
|
---|
[478] | 1956 | exporter->SetFilled();
|
---|
[469] | 1957 |
|
---|
[478] | 1958 | // output PVS of view cell
|
---|
[503] | 1959 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
[478] | 1960 | {
|
---|
| 1961 | Intersectable *intersect = (*it).first;
|
---|
[469] | 1962 |
|
---|
[478] | 1963 | if (!intersect->Mailed())
|
---|
| 1964 | {
|
---|
| 1965 | Material m = RandomMaterial();
|
---|
| 1966 | exporter->SetForcedMaterial(m);
|
---|
| 1967 |
|
---|
| 1968 | exporter->ExportIntersectable(intersect);
|
---|
| 1969 | intersect->Mail();
|
---|
[503] | 1970 | }
|
---|
[477] | 1971 | }
|
---|
[503] | 1972 |
|
---|
[478] | 1973 | DEL_PTR(exporter);
|
---|
| 1974 | cout << "finished" << endl;
|
---|
[477] | 1975 | }
|
---|
| 1976 |
|
---|
| 1977 | Debug << endl;
|
---|
[440] | 1978 | }
|
---|
| 1979 |
|
---|
[441] | 1980 |
|
---|
[503] | 1981 | void BspViewCellsManager::ExportColor(Exporter *exporter,
|
---|
[482] | 1982 | ViewCell *vc) const
|
---|
| 1983 | {
|
---|
[600] | 1984 | const bool vcValid = CheckValidity(vc, mMinPvsSize, mMaxPvsSize);
|
---|
[482] | 1985 |
|
---|
| 1986 | float importance = 0;
|
---|
[598] | 1987 | static Material m;
|
---|
[482] | 1988 |
|
---|
| 1989 | switch (mColorCode)
|
---|
| 1990 | {
|
---|
[598] | 1991 | case 0: // Random
|
---|
| 1992 | {
|
---|
| 1993 | if (vcValid)
|
---|
| 1994 | {
|
---|
| 1995 | m.mDiffuseColor.r = 0.5f + RandomValue(0.0f, 0.5f);
|
---|
| 1996 | m.mDiffuseColor.g = 0.5f + RandomValue(0.0f, 0.5f);
|
---|
[600] | 1997 | m.mDiffuseColor.b = 0.5f + RandomValue(0.0f, 0.5f);
|
---|
[598] | 1998 | }
|
---|
| 1999 | else
|
---|
| 2000 | {
|
---|
| 2001 | m.mDiffuseColor.r = 0.0f;
|
---|
| 2002 | m.mDiffuseColor.g = 1.0f;
|
---|
| 2003 | m.mDiffuseColor.b = 0.0f;
|
---|
| 2004 | }
|
---|
| 2005 |
|
---|
| 2006 | exporter->SetForcedMaterial(m);
|
---|
| 2007 | return;
|
---|
| 2008 | }
|
---|
| 2009 |
|
---|
[482] | 2010 | case 1: // pvs
|
---|
| 2011 | {
|
---|
[503] | 2012 | importance = (float)vc->GetPvs().GetSize() /
|
---|
[660] | 2013 | (float)mCurrentViewCellsStats.maxPvs;
|
---|
[598] | 2014 |
|
---|
[482] | 2015 | }
|
---|
| 2016 | break;
|
---|
| 2017 | case 2: // merges
|
---|
| 2018 | {
|
---|
[598] | 2019 | int lSize = mViewCellsTree->GetSize(vc);
|
---|
[660] | 2020 | importance = (float)lSize / (float)mCurrentViewCellsStats.maxLeaves;
|
---|
[482] | 2021 | }
|
---|
[598] | 2022 | //break;
|
---|
[482] | 2023 | case 3: // merge tree differene
|
---|
| 2024 | {
|
---|
| 2025 | // TODO
|
---|
[598] | 2026 | //importance = (float)GetMaxTreeDiff(vc) /
|
---|
| 2027 | // (float)(mVspBspTree->GetStatistics().maxDepth * 2);
|
---|
| 2028 |
|
---|
[482] | 2029 | }
|
---|
| 2030 | break;
|
---|
| 2031 | default:
|
---|
| 2032 | break;
|
---|
| 2033 | }
|
---|
| 2034 |
|
---|
[598] | 2035 | // special color code for invalid view cells
|
---|
[482] | 2036 | m.mDiffuseColor.r = importance;
|
---|
| 2037 | m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r;
|
---|
[598] | 2038 | m.mDiffuseColor.b = vcValid ? 1.0f : 0.0f;
|
---|
[503] | 2039 |
|
---|
[598] | 2040 | //Debug << "importance: " << importance << endl;
|
---|
[482] | 2041 | exporter->SetForcedMaterial(m);
|
---|
| 2042 | }
|
---|
| 2043 |
|
---|
[587] | 2044 |
|
---|
[693] | 2045 | void BspViewCellsManager::TestSubdivision()
|
---|
| 2046 | {
|
---|
| 2047 | ViewCellContainer leaves;
|
---|
| 2048 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 2049 |
|
---|
| 2050 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 2051 |
|
---|
| 2052 | const float vol = mViewSpaceBox.GetVolume();
|
---|
| 2053 | float subdivVol = 0;
|
---|
| 2054 | float newVol = 0;
|
---|
| 2055 |
|
---|
| 2056 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 2057 | {
|
---|
| 2058 | BspNodeGeometry geom;
|
---|
| 2059 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 2060 | mBspTree->ConstructGeometry(leaf, geom);
|
---|
| 2061 |
|
---|
| 2062 | const float lVol = geom.GetVolume();
|
---|
| 2063 |
|
---|
| 2064 | newVol += lVol;
|
---|
| 2065 | subdivVol += (*it)->GetVolume();
|
---|
[694] | 2066 |
|
---|
[704] | 2067 | float thres = 0.9f;
|
---|
[694] | 2068 | if ((lVol < ((*it)->GetVolume() * thres)) ||
|
---|
| 2069 | (lVol * thres > ((*it)->GetVolume())))
|
---|
| 2070 | Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
|
---|
[693] | 2071 | }
|
---|
| 2072 |
|
---|
| 2073 | Debug << "exact volume: " << vol << endl;
|
---|
| 2074 | Debug << "subdivision volume: " << subdivVol << endl;
|
---|
| 2075 | Debug << "new volume: " << newVol << endl;
|
---|
| 2076 | }
|
---|
| 2077 |
|
---|
| 2078 |
|
---|
[580] | 2079 | void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
[591] | 2080 | ViewCell *vc,
|
---|
[660] | 2081 | const Plane3 *clipPlane) const
|
---|
[482] | 2082 | {
|
---|
| 2083 | if (vc->GetMesh())
|
---|
| 2084 | {
|
---|
[587] | 2085 | exporter->ExportMesh(vc->GetMesh());
|
---|
| 2086 | return;
|
---|
[482] | 2087 | }
|
---|
[587] | 2088 |
|
---|
| 2089 | BspNodeGeometry geom;
|
---|
| 2090 | mBspTree->ConstructGeometry(vc, geom);
|
---|
[591] | 2091 |
|
---|
[660] | 2092 | if (clipPlane)
|
---|
[591] | 2093 | {
|
---|
[681] | 2094 | const int cf = Polygon3::ClassifyPlane(geom.GetPolys(), *clipPlane, 0.0001f);
|
---|
[591] | 2095 |
|
---|
[681] | 2096 | if (cf == Polygon3::BACK_SIDE)
|
---|
| 2097 | {
|
---|
| 2098 | exporter->ExportPolygons(geom.GetPolys());
|
---|
| 2099 | }
|
---|
| 2100 | else if (cf == Polygon3::SPLIT)
|
---|
| 2101 | {
|
---|
| 2102 | BspNodeGeometry front;
|
---|
| 2103 | BspNodeGeometry back;
|
---|
[591] | 2104 |
|
---|
[681] | 2105 | geom.SplitGeometry(front,
|
---|
| 2106 | back,
|
---|
| 2107 | *clipPlane,
|
---|
| 2108 | mViewSpaceBox,
|
---|
| 2109 | 0.0001f);
|
---|
| 2110 |
|
---|
| 2111 | if (back.Valid())
|
---|
| 2112 | exporter->ExportPolygons(back.GetPolys());
|
---|
| 2113 | }
|
---|
[591] | 2114 | }
|
---|
| 2115 | else
|
---|
| 2116 | {
|
---|
[678] | 2117 | exporter->ExportPolygons(geom.GetPolys());
|
---|
[591] | 2118 | }
|
---|
[482] | 2119 | }
|
---|
| 2120 |
|
---|
[503] | 2121 |
|
---|
| 2122 | void BspViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
| 2123 | {
|
---|
[587] | 2124 | if (vc->GetMesh())
|
---|
| 2125 | delete vc->GetMesh();
|
---|
| 2126 |
|
---|
| 2127 | BspNodeGeometry geom;
|
---|
| 2128 |
|
---|
| 2129 | mBspTree->ConstructGeometry(vc, geom);
|
---|
| 2130 |
|
---|
| 2131 | Mesh *mesh = new Mesh();
|
---|
| 2132 | geom.AddToMesh(*mesh);
|
---|
| 2133 | vc->SetMesh(mesh);
|
---|
| 2134 | mMeshContainer.push_back(mesh);
|
---|
[503] | 2135 | }
|
---|
| 2136 |
|
---|
[551] | 2137 |
|
---|
[587] | 2138 | void BspViewCellsManager::Finalize(ViewCell *viewCell,
|
---|
| 2139 | const bool createMesh)
|
---|
[502] | 2140 | {
|
---|
[587] | 2141 | float area = 0;
|
---|
| 2142 | float volume = 0;
|
---|
[572] | 2143 |
|
---|
[587] | 2144 | ViewCellContainer leaves;
|
---|
| 2145 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
| 2146 |
|
---|
| 2147 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 2148 |
|
---|
[693] | 2149 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[587] | 2150 | {
|
---|
| 2151 | BspNodeGeometry geom;
|
---|
| 2152 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 2153 | mBspTree->ConstructGeometry(leaf, geom);
|
---|
| 2154 |
|
---|
[719] | 2155 | const float lVol = geom.GetVolume();
|
---|
| 2156 | const float lArea = geom.GetArea();
|
---|
[693] | 2157 |
|
---|
| 2158 | //(*it)->SetVolume(vol);
|
---|
| 2159 | //(*it)->SetArea(area);
|
---|
| 2160 |
|
---|
| 2161 | area += lArea;
|
---|
| 2162 | volume += lVol;
|
---|
| 2163 |
|
---|
| 2164 | CreateMesh(*it);
|
---|
[587] | 2165 | }
|
---|
| 2166 |
|
---|
| 2167 | viewCell->SetVolume(volume);
|
---|
| 2168 | viewCell->SetArea(area);
|
---|
[502] | 2169 | }
|
---|
| 2170 |
|
---|
[580] | 2171 |
|
---|
[587] | 2172 | ViewCell *BspViewCellsManager::GetViewCell(const Vector3 &point) const
|
---|
[580] | 2173 | {
|
---|
[587] | 2174 | if (!mBspTree)
|
---|
| 2175 | return NULL;
|
---|
| 2176 |
|
---|
| 2177 | if (!mViewSpaceBox.IsInside(point))
|
---|
| 2178 | return NULL;
|
---|
| 2179 |
|
---|
| 2180 | return mBspTree->GetViewCell(point);
|
---|
[580] | 2181 | }
|
---|
| 2182 |
|
---|
| 2183 |
|
---|
[587] | 2184 | void BspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 2185 | vector<MergeCandidate> &candidates)
|
---|
| 2186 | {
|
---|
| 2187 | cout << "collecting merge candidates ... " << endl;
|
---|
| 2188 |
|
---|
| 2189 | if (mUseRaysForMerge)
|
---|
| 2190 | {
|
---|
| 2191 | mBspTree->CollectMergeCandidates(rays, candidates);
|
---|
| 2192 | }
|
---|
| 2193 | else
|
---|
| 2194 | {
|
---|
| 2195 | vector<BspLeaf *> leaves;
|
---|
| 2196 | mBspTree->CollectLeaves(leaves);
|
---|
| 2197 | mBspTree->CollectMergeCandidates(leaves, candidates);
|
---|
| 2198 | }
|
---|
| 2199 |
|
---|
| 2200 | cout << "fininshed collecting candidates" << endl;
|
---|
| 2201 | }
|
---|
| 2202 |
|
---|
| 2203 |
|
---|
[440] | 2204 |
|
---|
[590] | 2205 | bool BspViewCellsManager::ExportViewCells(const string filename)
|
---|
| 2206 | {
|
---|
| 2207 | cout << "exporting view cells to xml ... ";
|
---|
| 2208 | std::ofstream stream;
|
---|
[503] | 2209 |
|
---|
[590] | 2210 | // for output we need unique ids for each view cell
|
---|
| 2211 | CreateUniqueViewCellIds();
|
---|
[503] | 2212 |
|
---|
[590] | 2213 |
|
---|
| 2214 | stream.open(filename.c_str());
|
---|
| 2215 | stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
| 2216 | stream << "<Visibility_Solution>" << endl;
|
---|
| 2217 |
|
---|
| 2218 | //-- the view space bounding box
|
---|
| 2219 | stream << "<ViewSpaceBox"
|
---|
| 2220 | << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
|
---|
| 2221 | << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\" />" << endl;
|
---|
| 2222 |
|
---|
| 2223 | //-- the type of the view cells hierarchy
|
---|
| 2224 | //stream << "<Hierarchy name=\"bspTree\" />" << endl;
|
---|
| 2225 | stream << "<Hierarchy name=\"vspBspTree\" />" << endl; // write vsp bsp here because can use same tree and is bug free
|
---|
| 2226 | //-- load the view cells itself, i.e., the ids and the pvs
|
---|
| 2227 | stream << "<ViewCells>" << endl;
|
---|
[610] | 2228 |
|
---|
| 2229 | #if 0
|
---|
| 2230 |
|
---|
[590] | 2231 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
[610] | 2232 |
|
---|
[590] | 2233 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 2234 | ExportViewCell(*it, stream);
|
---|
[610] | 2235 | #else
|
---|
| 2236 | mViewCellsTree->Export(stream);
|
---|
| 2237 | #endif
|
---|
[590] | 2238 |
|
---|
| 2239 | stream << "</ViewCells>" << endl;
|
---|
| 2240 |
|
---|
| 2241 | //-- load the hierarchy
|
---|
| 2242 | stream << "<Hierarchy>" << endl;
|
---|
| 2243 | mBspTree->Export(stream);
|
---|
| 2244 | stream << endl << "</Hierarchy>" << endl;
|
---|
| 2245 |
|
---|
| 2246 | stream << "</Visibility_Solution>" << endl;
|
---|
| 2247 | stream.close();
|
---|
| 2248 |
|
---|
| 2249 | cout << "finished" << endl;
|
---|
| 2250 |
|
---|
| 2251 | return true;
|
---|
| 2252 | }
|
---|
| 2253 |
|
---|
| 2254 |
|
---|
| 2255 | void BspViewCellsManager::AddCurrentViewCellsToHierarchy()
|
---|
| 2256 | {
|
---|
| 2257 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 2258 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 2259 | {
|
---|
| 2260 | ViewCell *vc = *it;
|
---|
| 2261 | ViewCellContainer leaves;
|
---|
| 2262 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 2263 |
|
---|
| 2264 | ViewCellContainer::const_iterator lit, lit_end = leaves.end();
|
---|
| 2265 |
|
---|
| 2266 | for (lit = leaves.begin(); lit != lit_end; ++ lit)
|
---|
| 2267 | {
|
---|
| 2268 | BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*lit);
|
---|
| 2269 | bspVc->mLeaf->SetViewCell(vc);
|
---|
| 2270 | }
|
---|
| 2271 | }
|
---|
| 2272 | }
|
---|
| 2273 |
|
---|
[610] | 2274 |
|
---|
[650] | 2275 | ViewCell *BspViewCellsManager::ConstructSpatialMergeTree(BspNode *root)
|
---|
[610] | 2276 | {
|
---|
[650] | 2277 | if (root->IsLeaf())
|
---|
| 2278 | {
|
---|
| 2279 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(root)->GetViewCell();
|
---|
[610] | 2280 |
|
---|
[650] | 2281 | viewCell->SetMergeCost(0.0f);
|
---|
[610] | 2282 |
|
---|
[650] | 2283 | return viewCell;
|
---|
| 2284 | }
|
---|
| 2285 |
|
---|
| 2286 | BspInterior *interior = dynamic_cast<BspInterior *>(root);
|
---|
| 2287 | ViewCellInterior *viewCellInterior = new ViewCellInterior();
|
---|
| 2288 |
|
---|
| 2289 | float mergeCost = 1.0f / (float)root->mTimeStamp;
|
---|
| 2290 | //Debug << "timestamp: " << root->mTimeStamp;
|
---|
| 2291 | //Debug << "merge cost: " << mergeCost << endl;
|
---|
[649] | 2292 |
|
---|
[650] | 2293 | viewCellInterior->SetMergeCost(mergeCost);
|
---|
[649] | 2294 |
|
---|
[650] | 2295 | float volume = 0;
|
---|
[649] | 2296 |
|
---|
[650] | 2297 | BspNode *front = interior->GetFront();
|
---|
| 2298 | BspNode *back = interior->GetBack();
|
---|
[649] | 2299 |
|
---|
[650] | 2300 | // recursivly compute child hierarchies
|
---|
| 2301 | ViewCell *backVc = ConstructSpatialMergeTree(back);
|
---|
| 2302 | ViewCell *frontVc = ConstructSpatialMergeTree(front);
|
---|
[610] | 2303 |
|
---|
[650] | 2304 | viewCellInterior->SetupChildLink(frontVc);
|
---|
| 2305 | viewCellInterior->SetupChildLink(backVc);
|
---|
[610] | 2306 |
|
---|
[649] | 2307 |
|
---|
[650] | 2308 | viewCellInterior->GetPvs().Merge(backVc->GetPvs());
|
---|
| 2309 | viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
|
---|
[649] | 2310 |
|
---|
[650] | 2311 | volume += backVc->GetVolume();
|
---|
| 2312 | volume += frontVc->GetVolume();
|
---|
[649] | 2313 |
|
---|
[650] | 2314 | viewCellInterior->SetVolume(volume);
|
---|
[649] | 2315 |
|
---|
[650] | 2316 | return viewCellInterior;
|
---|
[610] | 2317 | }
|
---|
| 2318 |
|
---|
[650] | 2319 |
|
---|
[590] | 2320 | /************************************************************************/
|
---|
| 2321 | /* KdViewCellsManager implementation */
|
---|
| 2322 | /************************************************************************/
|
---|
| 2323 |
|
---|
| 2324 |
|
---|
| 2325 |
|
---|
[440] | 2326 | KdViewCellsManager::KdViewCellsManager(KdTree *kdTree):
|
---|
[469] | 2327 | ViewCellsManager(), mKdTree(kdTree), mKdPvsDepth(100)
|
---|
[440] | 2328 | {
|
---|
| 2329 | }
|
---|
| 2330 |
|
---|
[605] | 2331 |
|
---|
[471] | 2332 | float KdViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
[468] | 2333 | {
|
---|
[479] | 2334 | // compute view cell area / volume as subsititute for probability
|
---|
[605] | 2335 | if (0)
|
---|
| 2336 | return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
|
---|
| 2337 | else
|
---|
| 2338 | return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
|
---|
[468] | 2339 | }
|
---|
| 2340 |
|
---|
| 2341 |
|
---|
| 2342 |
|
---|
[479] | 2343 |
|
---|
[480] | 2344 | void KdViewCellsManager::CollectViewCells()
|
---|
| 2345 | {
|
---|
| 2346 | //mKdTree->CollectViewCells(mViewCells); TODO
|
---|
| 2347 | }
|
---|
[479] | 2348 |
|
---|
[519] | 2349 |
|
---|
[574] | 2350 | int KdViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
[487] | 2351 | const VssRayContainer &rays)
|
---|
[440] | 2352 | {
|
---|
[441] | 2353 | // if view cells already constructed
|
---|
[440] | 2354 | if (ViewCellsConstructed())
|
---|
| 2355 | return 0;
|
---|
[441] | 2356 |
|
---|
[440] | 2357 | mKdTree->Construct();
|
---|
| 2358 |
|
---|
[480] | 2359 | mTotalAreaValid = false;
|
---|
[469] | 2360 | // create the view cells
|
---|
| 2361 | mKdTree->CreateAndCollectViewCells(mViewCells);
|
---|
[503] | 2362 |
|
---|
[480] | 2363 | // cast rays
|
---|
[574] | 2364 | ComputeSampleContributions(rays, true, false);
|
---|
[480] | 2365 |
|
---|
[479] | 2366 | EvaluateViewCellsStats();
|
---|
[660] | 2367 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
[469] | 2368 |
|
---|
[440] | 2369 | return 0;
|
---|
| 2370 | }
|
---|
| 2371 |
|
---|
[587] | 2372 |
|
---|
[440] | 2373 | bool KdViewCellsManager::ViewCellsConstructed() const
|
---|
| 2374 | {
|
---|
| 2375 | return mKdTree->GetRoot() != NULL;
|
---|
| 2376 | }
|
---|
| 2377 |
|
---|
[503] | 2378 | int KdViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
[466] | 2379 | const VssRayContainer &rays)
|
---|
[440] | 2380 | {
|
---|
| 2381 | return 0;
|
---|
| 2382 | }
|
---|
| 2383 |
|
---|
| 2384 | void KdViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
[466] | 2385 | const VssRayContainer &sampleRays)
|
---|
[440] | 2386 | {
|
---|
[441] | 2387 | if (!ViewCellsConstructed())
|
---|
| 2388 | return;
|
---|
| 2389 |
|
---|
[469] | 2390 | // using view cells instead of the kd PVS of objects
|
---|
| 2391 | const bool useViewCells = true;
|
---|
| 2392 | bool exportRays = false;
|
---|
[440] | 2393 |
|
---|
| 2394 | int limit = min(mVisualizationSamples, (int)sampleRays.size());
|
---|
[469] | 2395 | const int pvsOut = min((int)objects.size(), 10);
|
---|
[466] | 2396 | VssRayContainer *rays = new VssRayContainer[pvsOut];
|
---|
[440] | 2397 |
|
---|
[469] | 2398 | if (useViewCells)
|
---|
[440] | 2399 | {
|
---|
[469] | 2400 | const int leafOut = 10;
|
---|
[503] | 2401 |
|
---|
[469] | 2402 | ViewCell::NewMail();
|
---|
[503] | 2403 |
|
---|
[469] | 2404 | //-- some rays for output
|
---|
| 2405 | const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
|
---|
| 2406 | Debug << "visualization using " << raysOut << " samples" << endl;
|
---|
| 2407 |
|
---|
| 2408 | //-- some random view cells and rays for output
|
---|
| 2409 | vector<KdLeaf *> kdLeaves;
|
---|
[503] | 2410 |
|
---|
[469] | 2411 | for (int i = 0; i < leafOut; ++ i)
|
---|
| 2412 | kdLeaves.push_back(dynamic_cast<KdLeaf *>(mKdTree->GetRandomLeaf()));
|
---|
[503] | 2413 |
|
---|
[469] | 2414 | for (int i = 0; i < kdLeaves.size(); ++ i)
|
---|
[440] | 2415 | {
|
---|
[469] | 2416 | KdLeaf *leaf = kdLeaves[i];
|
---|
| 2417 | RayContainer vcRays;
|
---|
[503] | 2418 |
|
---|
[469] | 2419 | cout << "creating output for view cell " << i << " ... ";
|
---|
[503] | 2420 | #if 0
|
---|
[469] | 2421 | // check whether we can add the current ray to the output rays
|
---|
[503] | 2422 | for (int k = 0; k < raysOut; ++ k)
|
---|
[440] | 2423 | {
|
---|
[469] | 2424 | Ray *ray = sampleRays[k];
|
---|
[503] | 2425 |
|
---|
[469] | 2426 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
| 2427 | {
|
---|
| 2428 | BspLeaf *leaf2 = ray->bspIntersections[j].mLeaf;
|
---|
[503] | 2429 |
|
---|
| 2430 | if (leaf->GetViewCell() == leaf2->GetViewCell())
|
---|
[469] | 2431 | {
|
---|
| 2432 | vcRays.push_back(ray);
|
---|
| 2433 | }
|
---|
| 2434 | }
|
---|
[479] | 2435 | }
|
---|
[503] | 2436 | #endif
|
---|
[469] | 2437 | Intersectable::NewMail();
|
---|
[503] | 2438 |
|
---|
[469] | 2439 | ViewCell *vc = leaf->mViewCell;
|
---|
[503] | 2440 |
|
---|
[469] | 2441 | //bspLeaves[j]->Mail();
|
---|
| 2442 | char s[64]; sprintf(s, "kd-pvs%04d.x3d", i);
|
---|
[503] | 2443 |
|
---|
[469] | 2444 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
| 2445 | exporter->SetFilled();
|
---|
| 2446 |
|
---|
| 2447 | exporter->SetWireframe();
|
---|
| 2448 | //exporter->SetFilled();
|
---|
[503] | 2449 |
|
---|
[469] | 2450 | Material m;//= RandomMaterial();
|
---|
| 2451 | m.mDiffuseColor = RgbColor(1, 1, 0);
|
---|
| 2452 | exporter->SetForcedMaterial(m);
|
---|
[503] | 2453 |
|
---|
[469] | 2454 | AxisAlignedBox3 box = mKdTree->GetBox(leaf);
|
---|
| 2455 | exporter->ExportBox(box);
|
---|
| 2456 |
|
---|
| 2457 | // export rays piercing this view cell
|
---|
[503] | 2458 | exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));
|
---|
| 2459 |
|
---|
[469] | 2460 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 2461 | exporter->SetForcedMaterial(m);
|
---|
[503] | 2462 |
|
---|
[469] | 2463 | // exporter->SetWireframe();
|
---|
| 2464 | exporter->SetFilled();
|
---|
[503] | 2465 |
|
---|
[469] | 2466 | ObjectPvsMap::iterator it, it_end = vc->GetPvs().mEntries.end();
|
---|
| 2467 | // output PVS of view cell
|
---|
[503] | 2468 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
[469] | 2469 | {
|
---|
| 2470 | Intersectable *intersect = (*it).first;
|
---|
| 2471 | if (!intersect->Mailed())
|
---|
| 2472 | {
|
---|
| 2473 | exporter->ExportIntersectable(intersect);
|
---|
| 2474 | intersect->Mail();
|
---|
[503] | 2475 | }
|
---|
[440] | 2476 | }
|
---|
[503] | 2477 |
|
---|
[469] | 2478 | DEL_PTR(exporter);
|
---|
| 2479 | cout << "finished" << endl;
|
---|
[440] | 2480 | }
|
---|
[469] | 2481 |
|
---|
| 2482 | DEL_PTR(rays);
|
---|
[440] | 2483 | }
|
---|
[469] | 2484 | else // using kd PVS of objects
|
---|
| 2485 | {
|
---|
| 2486 | for (int i = 0; i < limit; ++ i)
|
---|
| 2487 | {
|
---|
| 2488 | VssRay *ray = sampleRays[i];
|
---|
[503] | 2489 |
|
---|
[469] | 2490 | // check whether we can add this to the rays
|
---|
[503] | 2491 | for (int j = 0; j < pvsOut; j++)
|
---|
[469] | 2492 | {
|
---|
| 2493 | if (objects[j] == ray->mTerminationObject)
|
---|
| 2494 | {
|
---|
| 2495 | rays[j].push_back(ray);
|
---|
| 2496 | }
|
---|
| 2497 | }
|
---|
| 2498 | }
|
---|
[440] | 2499 |
|
---|
[503] | 2500 | if (exportRays)
|
---|
[469] | 2501 | {
|
---|
| 2502 | Exporter *exporter = NULL;
|
---|
| 2503 | exporter = Exporter::GetExporter("sample-rays.x3d");
|
---|
| 2504 | exporter->SetWireframe();
|
---|
| 2505 | exporter->ExportKdTree(*mKdTree);
|
---|
[503] | 2506 |
|
---|
| 2507 | for (i=0; i < pvsOut; i++)
|
---|
[469] | 2508 | exporter->ExportRays(rays[i], RgbColor(1, 0, 0));
|
---|
| 2509 |
|
---|
| 2510 | exporter->SetFilled();
|
---|
[503] | 2511 |
|
---|
[469] | 2512 | delete exporter;
|
---|
| 2513 | }
|
---|
[440] | 2514 |
|
---|
[503] | 2515 | for (int k=0; k < pvsOut; k++)
|
---|
[469] | 2516 | {
|
---|
| 2517 | Intersectable *object = objects[k];
|
---|
[503] | 2518 | char s[64];
|
---|
[469] | 2519 | sprintf(s, "sample-pvs%04d.x3d", k);
|
---|
[440] | 2520 |
|
---|
[469] | 2521 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
| 2522 | exporter->SetWireframe();
|
---|
[503] | 2523 |
|
---|
[469] | 2524 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
|
---|
| 2525 | Intersectable::NewMail();
|
---|
[503] | 2526 |
|
---|
[469] | 2527 | // avoid adding the object to the list
|
---|
| 2528 | object->Mail();
|
---|
| 2529 | ObjectContainer visibleObjects;
|
---|
[503] | 2530 |
|
---|
| 2531 | for (; i != object->mKdPvs.mEntries.end(); i++)
|
---|
[469] | 2532 | {
|
---|
| 2533 | KdNode *node = (*i).first;
|
---|
| 2534 | exporter->ExportBox(mKdTree->GetBox(node));
|
---|
[503] | 2535 |
|
---|
[469] | 2536 | mKdTree->CollectObjects(node, visibleObjects);
|
---|
| 2537 | }
|
---|
[503] | 2538 |
|
---|
[469] | 2539 | exporter->ExportRays(rays[k], RgbColor(0, 1, 0));
|
---|
| 2540 | exporter->SetFilled();
|
---|
[503] | 2541 |
|
---|
[469] | 2542 | for (int j = 0; j < visibleObjects.size(); j++)
|
---|
| 2543 | exporter->ExportIntersectable(visibleObjects[j]);
|
---|
[503] | 2544 |
|
---|
[469] | 2545 | Material m;
|
---|
| 2546 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 2547 | exporter->SetForcedMaterial(m);
|
---|
| 2548 | exporter->ExportIntersectable(object);
|
---|
[503] | 2549 |
|
---|
[469] | 2550 | delete exporter;
|
---|
[440] | 2551 | }
|
---|
[503] | 2552 | }
|
---|
[440] | 2553 | }
|
---|
| 2554 |
|
---|
[469] | 2555 |
|
---|
[482] | 2556 | void KdViewCellsManager::ExportColor(Exporter *exporter,
|
---|
| 2557 | ViewCell *vc) const
|
---|
| 2558 | {
|
---|
| 2559 | // TODO
|
---|
| 2560 | }
|
---|
| 2561 |
|
---|
[666] | 2562 |
|
---|
[580] | 2563 | ViewCell *KdViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
| 2564 | {
|
---|
| 2565 | return new KdViewCell(mesh);
|
---|
| 2566 | }
|
---|
[482] | 2567 |
|
---|
[580] | 2568 |
|
---|
| 2569 | void KdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
[591] | 2570 | ViewCell *vc,
|
---|
[660] | 2571 | const Plane3 *clipPlane) const
|
---|
[482] | 2572 | {
|
---|
[580] | 2573 | ViewCellContainer leaves;
|
---|
[485] | 2574 |
|
---|
[580] | 2575 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 2576 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 2577 |
|
---|
| 2578 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 2579 | {
|
---|
| 2580 | KdViewCell *kdVc = dynamic_cast<KdViewCell *>(*it);
|
---|
| 2581 |
|
---|
| 2582 | exporter->ExportBox(mKdTree->GetBox(kdVc->mLeaf));
|
---|
| 2583 | }
|
---|
[482] | 2584 | }
|
---|
| 2585 |
|
---|
| 2586 |
|
---|
[440] | 2587 | int KdViewCellsManager::GetType() const
|
---|
| 2588 | {
|
---|
| 2589 | return ViewCellsManager::KD;
|
---|
| 2590 | }
|
---|
| 2591 |
|
---|
| 2592 |
|
---|
[441] | 2593 |
|
---|
[440] | 2594 | KdNode *KdViewCellsManager::GetNodeForPvs(KdLeaf *leaf)
|
---|
| 2595 | {
|
---|
| 2596 | KdNode *node = leaf;
|
---|
| 2597 |
|
---|
| 2598 | while (node->mParent && node->mDepth > mKdPvsDepth)
|
---|
| 2599 | node = node->mParent;
|
---|
| 2600 | return node;
|
---|
| 2601 | }
|
---|
| 2602 |
|
---|
[469] | 2603 | int KdViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
| 2604 | const Vector3 &termination,
|
---|
| 2605 | ViewCellContainer &viewcells)
|
---|
[466] | 2606 | {
|
---|
[469] | 2607 | return mKdTree->CastLineSegment(origin, termination, viewcells);
|
---|
[466] | 2608 | }
|
---|
| 2609 |
|
---|
[469] | 2610 |
|
---|
[503] | 2611 | void KdViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
| 2612 | {
|
---|
[551] | 2613 | // TODO
|
---|
[503] | 2614 | }
|
---|
| 2615 |
|
---|
[580] | 2616 |
|
---|
| 2617 |
|
---|
| 2618 | void KdViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 2619 | vector<MergeCandidate> &candidates)
|
---|
| 2620 | {
|
---|
| 2621 | // TODO
|
---|
| 2622 | }
|
---|
| 2623 |
|
---|
| 2624 |
|
---|
[440] | 2625 | /**********************************************************************/
|
---|
| 2626 | /* VspKdViewCellsManager implementation */
|
---|
| 2627 | /**********************************************************************/
|
---|
| 2628 |
|
---|
[482] | 2629 |
|
---|
[574] | 2630 | VspKdViewCellsManager::VspKdViewCellsManager(VspKdTree *vspKdTree):
|
---|
| 2631 | ViewCellsManager(), mVspKdTree(vspKdTree)
|
---|
[440] | 2632 | {
|
---|
[574] | 2633 | environment->GetIntValue("VspKdTree.Construction.samples", mInitialSamples);
|
---|
[468] | 2634 | mVspKdTree->SetViewCellsManager(this);
|
---|
| 2635 | }
|
---|
[462] | 2636 |
|
---|
[471] | 2637 | float VspKdViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
[468] | 2638 | {
|
---|
[605] | 2639 | // compute view cell area / volume as subsititute for probability
|
---|
| 2640 | if (0)
|
---|
| 2641 | return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
|
---|
| 2642 | else
|
---|
| 2643 | return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
|
---|
[440] | 2644 | }
|
---|
| 2645 |
|
---|
[462] | 2646 |
|
---|
[468] | 2647 |
|
---|
[503] | 2648 |
|
---|
[480] | 2649 | void VspKdViewCellsManager::CollectViewCells()
|
---|
| 2650 | {
|
---|
| 2651 | mVspKdTree->CollectViewCells(mViewCells);
|
---|
| 2652 | }
|
---|
[462] | 2653 |
|
---|
[570] | 2654 |
|
---|
[574] | 2655 | int VspKdViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
| 2656 | const VssRayContainer &rays)
|
---|
[440] | 2657 | {
|
---|
[441] | 2658 | // if view cells already constructed
|
---|
[440] | 2659 | if (ViewCellsConstructed())
|
---|
| 2660 | return 0;
|
---|
[503] | 2661 |
|
---|
[469] | 2662 | VssRayContainer constructionRays;
|
---|
| 2663 | VssRayContainer savedRays;
|
---|
[440] | 2664 |
|
---|
[503] | 2665 | GetRaySets(rays,
|
---|
[574] | 2666 | mInitialSamples,
|
---|
[503] | 2667 | constructionRays,
|
---|
[485] | 2668 | &savedRays);
|
---|
[503] | 2669 |
|
---|
| 2670 | Debug << "constructing vsp kd tree using "
|
---|
[469] | 2671 | << (int)constructionRays.size() << " samples" << endl;
|
---|
[452] | 2672 |
|
---|
[542] | 2673 | mVspKdTree->Construct(constructionRays, &mViewSpaceBox);
|
---|
[452] | 2674 | Debug << mVspKdTree->GetStatistics() << endl;
|
---|
| 2675 |
|
---|
[482] | 2676 | // export leaf building blocks
|
---|
| 2677 | ExportLeaves(objects, rays);
|
---|
[480] | 2678 |
|
---|
[469] | 2679 | // finally merge kd leaf building blocks to view cells
|
---|
[485] | 2680 | const int merged = mVspKdTree->MergeViewCells(rays);
|
---|
| 2681 |
|
---|
| 2682 | // collapse siblings belonging to the same view cell
|
---|
[486] | 2683 | mVspKdTree->RefineViewCells(rays);
|
---|
[485] | 2684 |
|
---|
[542] | 2685 | // collapse siblings belonging to the same view cell
|
---|
[503] | 2686 | mVspKdTree->CollapseTree();
|
---|
| 2687 |
|
---|
[482] | 2688 | // evaluale view cell stats
|
---|
| 2689 | ResetViewCells();
|
---|
[469] | 2690 |
|
---|
[660] | 2691 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
[482] | 2692 |
|
---|
[485] | 2693 | long startTime = GetTime();
|
---|
[660] | 2694 |
|
---|
[469] | 2695 | // recast rest of rays
|
---|
[574] | 2696 | ComputeSampleContributions(savedRays, true, false);
|
---|
[503] | 2697 |
|
---|
| 2698 | Debug << "Computed remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
[485] | 2699 | << " secs" << endl;
|
---|
[469] | 2700 |
|
---|
| 2701 | return merged;
|
---|
[440] | 2702 | }
|
---|
| 2703 |
|
---|
| 2704 | bool VspKdViewCellsManager::ViewCellsConstructed() const
|
---|
| 2705 | {
|
---|
| 2706 | return mVspKdTree->GetRoot() != NULL;
|
---|
| 2707 | }
|
---|
| 2708 |
|
---|
[441] | 2709 |
|
---|
[462] | 2710 | ViewCell *VspKdViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
| 2711 | {
|
---|
| 2712 | return new VspKdViewCell(mesh);
|
---|
| 2713 | }
|
---|
| 2714 |
|
---|
[503] | 2715 | int VspKdViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
[466] | 2716 | const VssRayContainer &rays)
|
---|
[440] | 2717 | {
|
---|
[441] | 2718 | if (!ViewCellsConstructed())
|
---|
| 2719 | return 0;
|
---|
| 2720 |
|
---|
[482] | 2721 | // recalculate stats
|
---|
| 2722 | EvaluateViewCellsStats();
|
---|
| 2723 |
|
---|
[469] | 2724 | return 0;
|
---|
[440] | 2725 | }
|
---|
| 2726 |
|
---|
[482] | 2727 |
|
---|
| 2728 | void VspKdViewCellsManager::ExportLeaves(const ObjectContainer &objects,
|
---|
| 2729 | const VssRayContainer &sampleRays)
|
---|
[440] | 2730 | {
|
---|
[441] | 2731 | if (!ViewCellsConstructed())
|
---|
| 2732 | return;
|
---|
| 2733 |
|
---|
[482] | 2734 | //-- export leaf building blocks
|
---|
[503] | 2735 | Exporter *exporter = Exporter::GetExporter("vspkdtree.x3d");
|
---|
[482] | 2736 | if (!exporter)
|
---|
| 2737 | return;
|
---|
| 2738 |
|
---|
[598] | 2739 | if (mExportGeometry)
|
---|
| 2740 | exporter->ExportGeometry(objects);
|
---|
| 2741 |
|
---|
[482] | 2742 | //exporter->SetWireframe();
|
---|
| 2743 | //exporter->ExportVspKdTree(*mVspKdTree, mVspKdTree->GetStatistics().maxPvsSize);
|
---|
| 2744 | exporter->ExportVspKdTree(*mVspKdTree);
|
---|
| 2745 |
|
---|
[503] | 2746 | if (mExportRays)
|
---|
[440] | 2747 | {
|
---|
[503] | 2748 | const float prob = (float)mVisualizationSamples
|
---|
[482] | 2749 | / ((float)sampleRays.size() + Limits::Small);
|
---|
[440] | 2750 |
|
---|
[482] | 2751 | exporter->SetWireframe();
|
---|
[440] | 2752 |
|
---|
[482] | 2753 | //-- collect uniformly distributed rays
|
---|
| 2754 | VssRayContainer rays;
|
---|
[440] | 2755 |
|
---|
[482] | 2756 | for (int i = 0; i < sampleRays.size(); ++ i)
|
---|
| 2757 | {
|
---|
| 2758 | if (RandomValue(0,1) < prob)
|
---|
| 2759 | rays.push_back(sampleRays[i]);
|
---|
[440] | 2760 | }
|
---|
[482] | 2761 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
[440] | 2762 | }
|
---|
| 2763 |
|
---|
[482] | 2764 | delete exporter;
|
---|
| 2765 | }
|
---|
| 2766 |
|
---|
| 2767 | void VspKdViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
| 2768 | const VssRayContainer &sampleRays)
|
---|
| 2769 | {
|
---|
| 2770 | if (!ViewCellsConstructed())
|
---|
| 2771 | return;
|
---|
[503] | 2772 |
|
---|
[482] | 2773 | //-- export single view cells
|
---|
| 2774 | for (int i = 0; i < 10; ++ i)
|
---|
[440] | 2775 | {
|
---|
[482] | 2776 | char s[64];
|
---|
| 2777 | sprintf(s, "vsp_viewcell%04d.x3d", i);
|
---|
| 2778 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
[503] | 2779 | const int idx =
|
---|
[482] | 2780 | (int)RandomValue(0.0, (Real)((int)mViewCells.size() - 1));
|
---|
[440] | 2781 |
|
---|
[503] | 2782 | VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>(mViewCells[idx]);
|
---|
[482] | 2783 |
|
---|
| 2784 | //-- export geometry
|
---|
| 2785 | Material m;
|
---|
| 2786 | m.mDiffuseColor = RgbColor(0, 1, 1);
|
---|
[503] | 2787 |
|
---|
[482] | 2788 | exporter->SetForcedMaterial(m);
|
---|
| 2789 | exporter->SetWireframe();
|
---|
| 2790 |
|
---|
[580] | 2791 | ExportViewCellGeometry(exporter, vc);
|
---|
[482] | 2792 |
|
---|
| 2793 | //-- export stored rays
|
---|
[580] | 2794 |
|
---|
[485] | 2795 | if (mExportRays)
|
---|
[440] | 2796 | {
|
---|
[580] | 2797 | ViewCellContainer leaves;
|
---|
| 2798 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
[440] | 2799 |
|
---|
[580] | 2800 | ViewCellContainer::const_iterator it,
|
---|
| 2801 | it_end = leaves.end();
|
---|
| 2802 |
|
---|
| 2803 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[482] | 2804 | {
|
---|
[580] | 2805 | VspKdViewCell *vspKdVc = dynamic_cast<VspKdViewCell *>(*it);
|
---|
| 2806 | VspKdLeaf *leaf = vspKdVc->mLeaf;
|
---|
[482] | 2807 | AxisAlignedBox3 box = mVspKdTree->GetBBox(leaf);
|
---|
[440] | 2808 |
|
---|
[482] | 2809 | VssRayContainer vssRays;
|
---|
[483] | 2810 |
|
---|
| 2811 | VssRayContainer castRays;
|
---|
| 2812 | VssRayContainer initRays;
|
---|
| 2813 |
|
---|
[482] | 2814 | leaf->GetRays(vssRays);
|
---|
[440] | 2815 |
|
---|
[482] | 2816 | VssRayContainer::const_iterator it, it_end = vssRays.end();
|
---|
[483] | 2817 | const float prop = 200.0f / (float)vssRays.size();
|
---|
[440] | 2818 |
|
---|
[482] | 2819 | for (it = vssRays.begin(); it != it_end; ++ it)
|
---|
[483] | 2820 | {
|
---|
[485] | 2821 | if (Random(1) < prop)
|
---|
[483] | 2822 | if ((*it)->mTerminationObject == NULL)
|
---|
| 2823 | castRays.push_back(*it);
|
---|
[503] | 2824 | else
|
---|
[483] | 2825 | initRays.push_back(*it);
|
---|
| 2826 | }
|
---|
| 2827 |
|
---|
| 2828 | exporter->ExportRays(castRays, RgbColor(1, 0, 0));
|
---|
| 2829 | exporter->ExportRays(initRays, RgbColor(0, 1, 0));
|
---|
[482] | 2830 | }
|
---|
| 2831 | }
|
---|
[580] | 2832 |
|
---|
[482] | 2833 | //-- output PVS of view cell
|
---|
| 2834 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 2835 | exporter->SetForcedMaterial(m);
|
---|
[440] | 2836 |
|
---|
[483] | 2837 | Intersectable::NewMail();
|
---|
| 2838 |
|
---|
[482] | 2839 | ObjectPvsMap::const_iterator it,
|
---|
| 2840 | it_end = vc->GetPvs().mEntries.end();
|
---|
| 2841 |
|
---|
| 2842 | exporter->SetFilled();
|
---|
| 2843 |
|
---|
[503] | 2844 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
[482] | 2845 | {
|
---|
| 2846 | Intersectable *intersect = (*it).first;
|
---|
| 2847 |
|
---|
| 2848 | if (!intersect->Mailed())
|
---|
| 2849 | {
|
---|
| 2850 | Material m = RandomMaterial();
|
---|
| 2851 | exporter->SetForcedMaterial(m);
|
---|
| 2852 |
|
---|
| 2853 | exporter->ExportIntersectable(intersect);
|
---|
| 2854 | intersect->Mail();
|
---|
[503] | 2855 | }
|
---|
[482] | 2856 | }
|
---|
| 2857 |
|
---|
| 2858 | delete exporter;
|
---|
[440] | 2859 | }
|
---|
[462] | 2860 |
|
---|
| 2861 | //-- export final view cells
|
---|
[503] | 2862 | Exporter *exporter = Exporter::GetExporter("vspkdtree_merged.x3d");
|
---|
| 2863 |
|
---|
[485] | 2864 | //if (exportGeometry) exporter->SetWireframe();
|
---|
| 2865 | //else exporter->SetFilled();
|
---|
[483] | 2866 |
|
---|
[508] | 2867 | ExportViewCellsForViz(exporter);
|
---|
[462] | 2868 |
|
---|
[503] | 2869 | if (mExportGeometry)
|
---|
[465] | 2870 | {
|
---|
| 2871 | exporter->SetFilled();
|
---|
[462] | 2872 | exporter->ExportGeometry(objects);
|
---|
[465] | 2873 | }
|
---|
[469] | 2874 |
|
---|
[503] | 2875 | if (mExportRays)
|
---|
[462] | 2876 | {
|
---|
[503] | 2877 | const float prob = (float)mVisualizationSamples
|
---|
[482] | 2878 | / ((float)sampleRays.size() + Limits::Small);
|
---|
[503] | 2879 |
|
---|
[462] | 2880 | exporter->SetWireframe();
|
---|
[503] | 2881 |
|
---|
[466] | 2882 | VssRayContainer rays;
|
---|
[503] | 2883 |
|
---|
[462] | 2884 | for (int i = 0; i < sampleRays.size(); ++ i)
|
---|
| 2885 | {
|
---|
[466] | 2886 | if (RandomValue(0,1) < prob)
|
---|
| 2887 | rays.push_back(sampleRays[i]);
|
---|
[462] | 2888 | }
|
---|
[503] | 2889 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
[462] | 2890 | }
|
---|
| 2891 |
|
---|
| 2892 | delete exporter;
|
---|
[440] | 2893 | }
|
---|
| 2894 |
|
---|
| 2895 | int VspKdViewCellsManager::GetType() const
|
---|
| 2896 | {
|
---|
| 2897 | return VSP_KD;
|
---|
| 2898 | }
|
---|
[442] | 2899 |
|
---|
[462] | 2900 |
|
---|
[469] | 2901 | int VspKdViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
| 2902 | const Vector3 &termination,
|
---|
| 2903 | ViewCellContainer &viewcells)
|
---|
[466] | 2904 | {
|
---|
[469] | 2905 | return mVspKdTree->CastLineSegment(origin, termination, viewcells);
|
---|
[466] | 2906 | }
|
---|
| 2907 |
|
---|
[477] | 2908 |
|
---|
[482] | 2909 | void VspKdViewCellsManager::ExportColor(Exporter *exporter,
|
---|
| 2910 | ViewCell *vc) const
|
---|
| 2911 | {
|
---|
| 2912 | if (mColorCode == 0) // Random color
|
---|
| 2913 | return;
|
---|
[503] | 2914 |
|
---|
[482] | 2915 | float importance = 0;
|
---|
[477] | 2916 |
|
---|
[482] | 2917 | switch (mColorCode)
|
---|
| 2918 | {
|
---|
| 2919 | case 1: // pvs
|
---|
| 2920 | {
|
---|
[503] | 2921 | importance = (float)vc->GetPvs().GetSize() /
|
---|
[660] | 2922 | (float)mCurrentViewCellsStats.maxPvs;
|
---|
[482] | 2923 | }
|
---|
| 2924 | break;
|
---|
[580] | 2925 | case 2: // merged leaves
|
---|
[482] | 2926 | {
|
---|
[580] | 2927 | int lSize = mViewCellsTree->GetSize(vc);
|
---|
| 2928 | importance = (float)lSize /
|
---|
[660] | 2929 | (float)mCurrentViewCellsStats.maxLeaves;
|
---|
[482] | 2930 | }
|
---|
| 2931 | break;
|
---|
[485] | 2932 | case 3: // merged tree depth difference
|
---|
[482] | 2933 | {
|
---|
[503] | 2934 | //importance = (float)GetMaxTreeDiff(vc) /
|
---|
[482] | 2935 | // (float)(mVspBspTree->GetStatistics().maxDepth * 2);
|
---|
| 2936 | }
|
---|
| 2937 | break;
|
---|
| 2938 | default:
|
---|
| 2939 | break;
|
---|
| 2940 | }
|
---|
| 2941 |
|
---|
| 2942 | Material m;
|
---|
| 2943 | m.mDiffuseColor.b = 1.0f;
|
---|
| 2944 | m.mDiffuseColor.r = importance;
|
---|
| 2945 | m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r;
|
---|
| 2946 | //Debug << "importance: " << importance << endl;
|
---|
| 2947 | exporter->SetForcedMaterial(m);
|
---|
| 2948 | }
|
---|
| 2949 |
|
---|
| 2950 |
|
---|
[580] | 2951 | void VspKdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
[591] | 2952 | ViewCell *vc,
|
---|
[660] | 2953 | const Plane3 *clipPlane) const
|
---|
[482] | 2954 | {
|
---|
| 2955 | VspKdViewCell *kdVc = dynamic_cast<VspKdViewCell *>(vc);
|
---|
| 2956 |
|
---|
[486] | 2957 | Mesh m;
|
---|
[580] | 2958 |
|
---|
| 2959 | ViewCellContainer leaves;
|
---|
| 2960 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 2961 |
|
---|
| 2962 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 2963 |
|
---|
| 2964 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[486] | 2965 | {
|
---|
[580] | 2966 | VspKdLeaf *l = dynamic_cast<VspKdViewCell *>(*it)->mLeaf;
|
---|
| 2967 | mVspKdTree->GetBBox(l).AddBoxToMesh(&m);
|
---|
[486] | 2968 | }
|
---|
| 2969 |
|
---|
| 2970 | exporter->ExportMesh(&m);
|
---|
[482] | 2971 | }
|
---|
| 2972 |
|
---|
| 2973 |
|
---|
[503] | 2974 | void VspKdViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
| 2975 | {
|
---|
[551] | 2976 | //TODO
|
---|
[503] | 2977 | }
|
---|
[442] | 2978 |
|
---|
[574] | 2979 |
|
---|
[584] | 2980 | void VspKdViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 2981 | vector<MergeCandidate> &candidates)
|
---|
[580] | 2982 | {
|
---|
| 2983 | // TODO
|
---|
| 2984 | }
|
---|
[574] | 2985 |
|
---|
[580] | 2986 |
|
---|
[503] | 2987 | /**************************************************************************/
|
---|
| 2988 | /* VspBspViewCellsManager implementation */
|
---|
| 2989 | /**************************************************************************/
|
---|
[482] | 2990 |
|
---|
[503] | 2991 |
|
---|
[574] | 2992 | VspBspViewCellsManager::VspBspViewCellsManager(VspBspTree *vspBspTree):
|
---|
| 2993 | ViewCellsManager(), mVspBspTree(vspBspTree)
|
---|
[442] | 2994 | {
|
---|
[574] | 2995 | environment->GetIntValue("VspBspTree.Construction.samples", mInitialSamples);
|
---|
[478] | 2996 | mVspBspTree->SetViewCellsManager(this);
|
---|
[590] | 2997 | mVspBspTree->mViewCellsTree = mViewCellsTree;
|
---|
[442] | 2998 | }
|
---|
| 2999 |
|
---|
[477] | 3000 |
|
---|
[475] | 3001 | VspBspViewCellsManager::~VspBspViewCellsManager()
|
---|
| 3002 | {
|
---|
| 3003 | }
|
---|
| 3004 |
|
---|
[477] | 3005 |
|
---|
[471] | 3006 | float VspBspViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
[468] | 3007 | {
|
---|
[551] | 3008 | if (0 && mVspBspTree->mUseAreaForPvs)
|
---|
[547] | 3009 | return GetArea(viewCell) / GetAccVcArea();
|
---|
| 3010 | else
|
---|
| 3011 | return GetVolume(viewCell) / mViewSpaceBox.GetVolume();
|
---|
[468] | 3012 | }
|
---|
| 3013 |
|
---|
[477] | 3014 |
|
---|
[480] | 3015 | void VspBspViewCellsManager::CollectViewCells()
|
---|
| 3016 | {
|
---|
[581] | 3017 | // view cells tree constructed
|
---|
| 3018 | if (!ViewCellsTreeConstructed())
|
---|
| 3019 | {
|
---|
[590] | 3020 | mVspBspTree->CollectViewCells(mViewCells, false);
|
---|
[581] | 3021 | }
|
---|
| 3022 | else
|
---|
[664] | 3023 | { // we can use the view cells tree hierarchy to get the right set
|
---|
[600] | 3024 | mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
|
---|
[581] | 3025 | }
|
---|
[480] | 3026 | }
|
---|
| 3027 |
|
---|
| 3028 |
|
---|
[442] | 3029 | bool VspBspViewCellsManager::ViewCellsConstructed() const
|
---|
| 3030 | {
|
---|
| 3031 | return mVspBspTree->GetRoot() != NULL;
|
---|
| 3032 | }
|
---|
| 3033 |
|
---|
[462] | 3034 |
|
---|
[442] | 3035 | ViewCell *VspBspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
| 3036 | {
|
---|
| 3037 | return new BspViewCell(mesh);
|
---|
| 3038 | }
|
---|
| 3039 |
|
---|
[569] | 3040 |
|
---|
[574] | 3041 | int VspBspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
[600] | 3042 | const VssRayContainer &rays)
|
---|
[442] | 3043 | {
|
---|
[710] | 3044 | mMaxPvsSize = (int)(mMaxPvsRatio * (float)objects.size());
|
---|
| 3045 |
|
---|
[503] | 3046 | // if view cells were already constructed
|
---|
| 3047 | if (ViewCellsConstructed())
|
---|
| 3048 | return 0;
|
---|
[442] | 3049 |
|
---|
[503] | 3050 | int sampleContributions = 0;
|
---|
| 3051 |
|
---|
| 3052 | VssRayContainer sampleRays;
|
---|
| 3053 |
|
---|
[574] | 3054 | int limit = min (mInitialSamples, (int)rays.size());
|
---|
[503] | 3055 |
|
---|
[469] | 3056 | VssRayContainer constructionRays;
|
---|
| 3057 | VssRayContainer savedRays;
|
---|
[442] | 3058 |
|
---|
[660] | 3059 | Debug << "samples used for vsp bsp subdivision: " << mInitialSamples
|
---|
[664] | 3060 | << ", actual rays: " << (int)rays.size() << endl;
|
---|
[587] | 3061 |
|
---|
[574] | 3062 | GetRaySets(rays, mInitialSamples, constructionRays, &savedRays);
|
---|
[487] | 3063 |
|
---|
[574] | 3064 | Debug << "initial rays: " << (int)constructionRays.size() << endl;
|
---|
[508] | 3065 | Debug << "saved rays: " << (int)savedRays.size() << endl;
|
---|
[485] | 3066 |
|
---|
[564] | 3067 |
|
---|
[666] | 3068 | //TODO: remove
|
---|
[667] | 3069 | if (1)
|
---|
| 3070 | mVspBspTree->Construct(constructionRays, &mViewSpaceBox);
|
---|
[666] | 3071 | else
|
---|
[667] | 3072 | mVspBspTree->Construct(rays, &mViewSpaceBox);
|
---|
| 3073 |
|
---|
[490] | 3074 | // collapse invalid regions
|
---|
[491] | 3075 | cout << "collapsing invalid tree regions ... ";
|
---|
[495] | 3076 | long startTime = GetTime();
|
---|
[503] | 3077 | int collapsedLeaves = mVspBspTree->CollapseTree();
|
---|
[587] | 3078 | Debug << "collapsed in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
| 3079 | << " seconds" << endl;
|
---|
| 3080 |
|
---|
[495] | 3081 | cout << "finished" << endl;
|
---|
[503] | 3082 |
|
---|
[660] | 3083 | // -- stats
|
---|
| 3084 | Debug << mVspBspTree->GetStatistics() << endl;
|
---|
| 3085 |
|
---|
[480] | 3086 | ResetViewCells();
|
---|
[660] | 3087 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
[503] | 3088 |
|
---|
[491] | 3089 |
|
---|
[495] | 3090 | startTime = GetTime();
|
---|
[503] | 3091 |
|
---|
[491] | 3092 | cout << "Computing remaining ray contributions ... ";
|
---|
[605] | 3093 |
|
---|
[469] | 3094 | // recast rest of rays
|
---|
[605] | 3095 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 3096 | ComputeSampleContributions(savedRays, true, false);
|
---|
[491] | 3097 | cout << "finished" << endl;
|
---|
| 3098 |
|
---|
[503] | 3099 | Debug << "Computed remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
[485] | 3100 | << " secs" << endl;
|
---|
[442] | 3101 |
|
---|
[485] | 3102 | cout << "construction finished" << endl;
|
---|
[503] | 3103 |
|
---|
[719] | 3104 | // real meshes are contructed at this stage
|
---|
| 3105 | if (0)
|
---|
| 3106 | {
|
---|
| 3107 | cout << "finalizing view cells ... ";
|
---|
| 3108 | FinalizeViewCells(true);
|
---|
| 3109 | cout << "finished" << endl;
|
---|
| 3110 | }
|
---|
| 3111 |
|
---|
[442] | 3112 | return sampleContributions;
|
---|
| 3113 | }
|
---|
| 3114 |
|
---|
[489] | 3115 |
|
---|
[503] | 3116 | void VspBspViewCellsManager::MergeViewCells(const VssRayContainer &rays,
|
---|
| 3117 | const ObjectContainer &objects)
|
---|
[475] | 3118 | {
|
---|
[442] | 3119 | int vcSize = 0;
|
---|
| 3120 | int pvsSize = 0;
|
---|
| 3121 |
|
---|
[650] | 3122 | //-- merge view cells
|
---|
| 3123 | cout << "starting merge using " << mPostProcessSamples << " samples ... " << endl;
|
---|
| 3124 | long startTime = GetTime();
|
---|
| 3125 |
|
---|
| 3126 |
|
---|
[597] | 3127 | if (mMergeViewCells)
|
---|
| 3128 | {
|
---|
| 3129 | // TODO: should be done BEFORE the ray casting
|
---|
[710] | 3130 | // compute tree by merging the nodes based on cost heuristics
|
---|
[650] | 3131 | mViewCellsTree->ConstructMergeTree(rays, objects);
|
---|
| 3132 | }
|
---|
| 3133 | else
|
---|
| 3134 | {
|
---|
[710] | 3135 | // compute tree by merging the nodes of the spatial hierarchy
|
---|
| 3136 | ViewCell *root = ConstructSpatialMergeTree(mVspBspTree->GetRoot());
|
---|
| 3137 | mViewCellsTree->SetRoot(root);
|
---|
[650] | 3138 | }
|
---|
[442] | 3139 |
|
---|
[666] | 3140 | if (1)
|
---|
| 3141 | {
|
---|
| 3142 | char mstats[100];
|
---|
| 3143 | environment->GetStringValue("ViewCells.mergeStats", mstats);
|
---|
| 3144 | mViewCellsTree->ExportStats(mstats);
|
---|
| 3145 | }
|
---|
[485] | 3146 |
|
---|
[650] | 3147 | //-- stats and visualizations
|
---|
| 3148 | cout << "finished merging" << endl;
|
---|
| 3149 | cout << "merged view cells in "
|
---|
| 3150 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
[503] | 3151 |
|
---|
[650] | 3152 | Debug << "Postprocessing: Merged view cells in "
|
---|
| 3153 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
|
---|
| 3154 |
|
---|
| 3155 |
|
---|
[600] | 3156 | int savedColorCode = mColorCode;
|
---|
| 3157 |
|
---|
[660] | 3158 | // get currently active view cell set
|
---|
| 3159 | ResetViewCells();
|
---|
| 3160 | Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
|
---|
| 3161 |
|
---|
[485] | 3162 | //BspLeaf::NewMail();
|
---|
[667] | 3163 | if (1) // export merged view cells
|
---|
[483] | 3164 | {
|
---|
[600] | 3165 | mColorCode = 0;
|
---|
[485] | 3166 | Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
|
---|
[660] | 3167 |
|
---|
[485] | 3168 | cout << "exporting view cells after merge ... ";
|
---|
| 3169 |
|
---|
| 3170 | if (exporter)
|
---|
[483] | 3171 | {
|
---|
[600] | 3172 | if (0)
|
---|
| 3173 | exporter->SetWireframe();
|
---|
| 3174 | else
|
---|
| 3175 | exporter->SetFilled();
|
---|
| 3176 |
|
---|
| 3177 | ExportViewCellsForViz(exporter);
|
---|
| 3178 |
|
---|
[485] | 3179 | if (mExportGeometry)
|
---|
[483] | 3180 | {
|
---|
[485] | 3181 | Material m;
|
---|
| 3182 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 3183 | exporter->SetForcedMaterial(m);
|
---|
| 3184 | exporter->SetFilled();
|
---|
[442] | 3185 |
|
---|
[485] | 3186 | exporter->ExportGeometry(objects);
|
---|
[442] | 3187 | }
|
---|
[485] | 3188 |
|
---|
[600] | 3189 | delete exporter;
|
---|
| 3190 | }
|
---|
| 3191 | cout << "finished" << endl;
|
---|
| 3192 | }
|
---|
| 3193 |
|
---|
[667] | 3194 | if (1) // export merged view cells using pvs coding
|
---|
[600] | 3195 | {
|
---|
| 3196 | mColorCode = 1;
|
---|
| 3197 |
|
---|
| 3198 | Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.x3d");
|
---|
| 3199 |
|
---|
| 3200 | cout << "exporting view cells after merge (pvs size) ... ";
|
---|
| 3201 |
|
---|
| 3202 | if (exporter)
|
---|
| 3203 | {
|
---|
[598] | 3204 | if (0)
|
---|
| 3205 | exporter->SetWireframe();
|
---|
| 3206 | else
|
---|
| 3207 | exporter->SetFilled();
|
---|
| 3208 |
|
---|
| 3209 | ExportViewCellsForViz(exporter);
|
---|
| 3210 |
|
---|
[600] | 3211 | if (mExportGeometry)
|
---|
| 3212 | {
|
---|
| 3213 | Material m;
|
---|
| 3214 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 3215 | exporter->SetForcedMaterial(m);
|
---|
| 3216 | exporter->SetFilled();
|
---|
| 3217 |
|
---|
| 3218 | exporter->ExportGeometry(objects);
|
---|
| 3219 | }
|
---|
| 3220 |
|
---|
[485] | 3221 | delete exporter;
|
---|
[442] | 3222 | }
|
---|
[485] | 3223 | cout << "finished" << endl;
|
---|
[442] | 3224 | }
|
---|
[600] | 3225 |
|
---|
| 3226 | mColorCode = savedColorCode;
|
---|
| 3227 |
|
---|
[503] | 3228 | }
|
---|
[475] | 3229 |
|
---|
[503] | 3230 |
|
---|
[609] | 3231 | bool VspBspViewCellsManager::EqualToSpatialNode(ViewCell *viewCell) const
|
---|
[607] | 3232 | {
|
---|
[609] | 3233 | return GetSpatialNode(viewCell) != NULL;
|
---|
[607] | 3234 | }
|
---|
| 3235 |
|
---|
| 3236 |
|
---|
[609] | 3237 | BspNode *VspBspViewCellsManager::GetSpatialNode(ViewCell *viewCell) const
|
---|
[607] | 3238 | {
|
---|
| 3239 | if (!viewCell->IsLeaf())
|
---|
| 3240 | {
|
---|
| 3241 | BspViewCell *bspVc = dynamic_cast<BspViewCell *>(viewCell);
|
---|
| 3242 |
|
---|
[609] | 3243 | return bspVc->mLeaf;
|
---|
| 3244 | }
|
---|
[607] | 3245 | else
|
---|
| 3246 | {
|
---|
| 3247 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(viewCell);
|
---|
| 3248 |
|
---|
| 3249 | // cannot be node of binary tree
|
---|
| 3250 | if (interior->mChildren.size() != 2)
|
---|
[609] | 3251 | return NULL;
|
---|
[607] | 3252 |
|
---|
| 3253 | ViewCell *left = interior->mChildren[0];
|
---|
| 3254 | ViewCell *right = interior->mChildren[1];
|
---|
| 3255 |
|
---|
[609] | 3256 | BspNode *leftNode = GetSpatialNode(left);
|
---|
| 3257 | BspNode *rightNode = GetSpatialNode(right);
|
---|
[607] | 3258 |
|
---|
[609] | 3259 | if (leftNode && rightNode && leftNode->IsSibling(rightNode))
|
---|
[607] | 3260 | {
|
---|
[609] | 3261 | return leftNode->GetParent();
|
---|
[607] | 3262 | }
|
---|
[609] | 3263 | }
|
---|
[607] | 3264 |
|
---|
[609] | 3265 | return NULL;
|
---|
[607] | 3266 | }
|
---|
| 3267 |
|
---|
| 3268 |
|
---|
[564] | 3269 | void VspBspViewCellsManager::RefineViewCells(const VssRayContainer &rays,
|
---|
| 3270 | const ObjectContainer &objects)
|
---|
[503] | 3271 | {
|
---|
[485] | 3272 | Debug << "render time before refine:" << endl;
|
---|
| 3273 | mRenderer->RenderScene();
|
---|
[503] | 3274 | SimulationStatistics ss;
|
---|
[485] | 3275 | dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
| 3276 | Debug << ss << endl;
|
---|
| 3277 |
|
---|
[489] | 3278 | cout << "Refining the merged view cells ... ";
|
---|
[503] | 3279 | long startTime = GetTime();
|
---|
[485] | 3280 |
|
---|
| 3281 | // refining the merged view cells
|
---|
[580] | 3282 | const int refined = mViewCellsTree->RefineViewCells(rays, objects);
|
---|
[485] | 3283 |
|
---|
[442] | 3284 | //-- stats and visualizations
|
---|
| 3285 | cout << "finished" << endl;
|
---|
[485] | 3286 | cout << "refined " << refined << " view cells in "
|
---|
[442] | 3287 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
| 3288 |
|
---|
[485] | 3289 | Debug << "Postprocessing: refined " << refined << " view cells in "
|
---|
[475] | 3290 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
|
---|
[503] | 3291 | }
|
---|
[495] | 3292 |
|
---|
[517] | 3293 |
|
---|
[503] | 3294 | int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
| 3295 | const VssRayContainer &rays)
|
---|
| 3296 | {
|
---|
| 3297 | if (!ViewCellsConstructed())
|
---|
| 3298 | {
|
---|
[517] | 3299 | Debug << "postprocess error: no view cells constructed" << endl;
|
---|
[503] | 3300 | return 0;
|
---|
| 3301 | }
|
---|
[520] | 3302 |
|
---|
[564] | 3303 |
|
---|
[609] | 3304 | // view cells already finished before post processing step
|
---|
| 3305 | // (i.e. because they were loaded)
|
---|
[520] | 3306 | if (mViewCellsFinished)
|
---|
[542] | 3307 | {
|
---|
[551] | 3308 | FinalizeViewCells(true);
|
---|
[542] | 3309 | EvaluateViewCellsStats();
|
---|
[577] | 3310 |
|
---|
[517] | 3311 | return 0;
|
---|
[542] | 3312 | }
|
---|
[448] | 3313 |
|
---|
[710] | 3314 | // check if new view cells turned invalid
|
---|
| 3315 | int minPvs, maxPvs;
|
---|
[564] | 3316 |
|
---|
[710] | 3317 | if (0)
|
---|
| 3318 | {
|
---|
| 3319 | minPvs = mMinPvsSize;
|
---|
| 3320 | maxPvs = mMaxPvsSize;
|
---|
| 3321 | }
|
---|
| 3322 | else
|
---|
| 3323 | {
|
---|
| 3324 | minPvs = mPruneEmptyViewCells ? 1 : 0;
|
---|
| 3325 | maxPvs = mMaxPvsSize;
|
---|
| 3326 | }
|
---|
| 3327 | Debug << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
| 3328 | cout << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
| 3329 |
|
---|
| 3330 | SetValidity(minPvs, maxPvs);
|
---|
| 3331 |
|
---|
[574] | 3332 | // update valid view space according to valid view cells
|
---|
[708] | 3333 | if (0)
|
---|
| 3334 | mVspBspTree->ValidateTree();
|
---|
[542] | 3335 |
|
---|
[574] | 3336 | // has to be recomputed
|
---|
| 3337 | mTotalAreaValid = false;
|
---|
[517] | 3338 | VssRayContainer postProcessRays;
|
---|
[605] | 3339 | GetRaySets(rays, mPostProcessSamples, postProcessRays);
|
---|
| 3340 |
|
---|
[517] | 3341 | Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
|
---|
[564] | 3342 |
|
---|
[517] | 3343 |
|
---|
[552] | 3344 | // should maybe be done here to allow merge working with area or volume
|
---|
| 3345 | // and to correct the rendering statistics
|
---|
[719] | 3346 | if (0) FinalizeViewCells(false);
|
---|
[708] | 3347 |
|
---|
[719] | 3348 |
|
---|
[597] | 3349 | //-- merge the individual view cells
|
---|
| 3350 | MergeViewCells(postProcessRays, objects);
|
---|
[591] | 3351 |
|
---|
[693] | 3352 | // only for testing
|
---|
| 3353 | TestSubdivision();
|
---|
| 3354 |
|
---|
[597] | 3355 | //-- refines the merged view cells
|
---|
| 3356 | if (0)
|
---|
| 3357 | RefineViewCells(postProcessRays, objects);
|
---|
[568] | 3358 |
|
---|
[719] | 3359 |
|
---|
[660] | 3360 | //-- render simulation after merge + refine
|
---|
| 3361 |
|
---|
[605] | 3362 | cout << "\nevaluating bsp view cells render time before compress ... ";
|
---|
| 3363 | dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
|
---|
| 3364 | SimulationStatistics ss;
|
---|
| 3365 | dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
[600] | 3366 |
|
---|
[580] | 3367 |
|
---|
[605] | 3368 | cout << " finished" << endl;
|
---|
| 3369 | cout << ss << endl;
|
---|
| 3370 | Debug << ss << endl;
|
---|
[600] | 3371 |
|
---|
| 3372 |
|
---|
| 3373 | //-- compression
|
---|
| 3374 |
|
---|
| 3375 | if (ViewCellsTreeConstructed() && mCompressViewCells)
|
---|
| 3376 | {
|
---|
| 3377 | int pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
|
---|
| 3378 | Debug << "number of entries before compress: " << pvsEntries << endl;
|
---|
| 3379 |
|
---|
| 3380 | mViewCellsTree->CompressViewCellsPvs();
|
---|
| 3381 |
|
---|
| 3382 | pvsEntries = mViewCellsTree->GetNumPvsEntries(mViewCellsTree->GetRoot());
|
---|
| 3383 | Debug << "number of entries after compress: " << pvsEntries << endl;
|
---|
| 3384 | }
|
---|
| 3385 |
|
---|
[660] | 3386 |
|
---|
[517] | 3387 | // collapse sibling leaves that share the same view cell
|
---|
[586] | 3388 | if (0)
|
---|
| 3389 | mVspBspTree->CollapseTree();
|
---|
| 3390 |
|
---|
[574] | 3391 | // recompute view cell list and statistics
|
---|
[520] | 3392 | ResetViewCells();
|
---|
| 3393 |
|
---|
[719] | 3394 | // compute final meshes and volume / area
|
---|
| 3395 | if (1) FinalizeViewCells(true);
|
---|
[517] | 3396 |
|
---|
[719] | 3397 |
|
---|
[508] | 3398 | // write view cells to disc
|
---|
| 3399 | if (mExportViewCells)
|
---|
| 3400 | {
|
---|
[517] | 3401 | char buff[100];
|
---|
| 3402 | environment->GetStringValue("ViewCells.filename", buff);
|
---|
| 3403 | string vcFilename(buff);
|
---|
| 3404 |
|
---|
| 3405 | ExportViewCells(buff);
|
---|
[508] | 3406 | }
|
---|
[503] | 3407 |
|
---|
| 3408 | return 0;
|
---|
[442] | 3409 | }
|
---|
| 3410 |
|
---|
[490] | 3411 |
|
---|
[442] | 3412 | int VspBspViewCellsManager::GetType() const
|
---|
| 3413 | {
|
---|
| 3414 | return VSP_BSP;
|
---|
| 3415 | }
|
---|
| 3416 |
|
---|
| 3417 |
|
---|
[650] | 3418 | ViewCell *VspBspViewCellsManager::ConstructSpatialMergeTree(BspNode *root)
|
---|
[609] | 3419 | {
|
---|
[650] | 3420 | if (root->IsLeaf())
|
---|
| 3421 | {
|
---|
| 3422 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(root)->GetViewCell();
|
---|
[610] | 3423 |
|
---|
[650] | 3424 | viewCell->SetMergeCost(0.0f);
|
---|
[649] | 3425 |
|
---|
[650] | 3426 | return viewCell;
|
---|
| 3427 | }
|
---|
| 3428 |
|
---|
| 3429 | BspInterior *interior = dynamic_cast<BspInterior *>(root);
|
---|
| 3430 | ViewCellInterior *viewCellInterior = new ViewCellInterior();
|
---|
| 3431 |
|
---|
| 3432 | float mergeCost = 1.0f / (float)root->mTimeStamp;
|
---|
| 3433 | //Debug << "timestamp: " << root->mTimeStamp;
|
---|
| 3434 | //Debug << "merge cost: " << mergeCost << endl;
|
---|
[610] | 3435 |
|
---|
[650] | 3436 | viewCellInterior->SetMergeCost(mergeCost);
|
---|
[610] | 3437 |
|
---|
[650] | 3438 | float volume = 0;
|
---|
| 3439 |
|
---|
| 3440 | BspNode *front = interior->GetFront();
|
---|
| 3441 | BspNode *back = interior->GetBack();
|
---|
[649] | 3442 |
|
---|
[650] | 3443 | // recursivly compute child hierarchies
|
---|
| 3444 | ViewCell *backVc = ConstructSpatialMergeTree(back);
|
---|
| 3445 | ViewCell *frontVc = ConstructSpatialMergeTree(front);
|
---|
[610] | 3446 |
|
---|
[650] | 3447 | viewCellInterior->SetupChildLink(frontVc);
|
---|
| 3448 | viewCellInterior->SetupChildLink(backVc);
|
---|
| 3449 |
|
---|
| 3450 |
|
---|
| 3451 | viewCellInterior->GetPvs().Merge(backVc->GetPvs());
|
---|
| 3452 | viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
|
---|
| 3453 |
|
---|
| 3454 | volume += backVc->GetVolume();
|
---|
| 3455 | volume += frontVc->GetVolume();
|
---|
| 3456 |
|
---|
| 3457 | viewCellInterior->SetVolume(volume);
|
---|
| 3458 |
|
---|
| 3459 | return viewCellInterior;
|
---|
[609] | 3460 | }
|
---|
| 3461 |
|
---|
| 3462 |
|
---|
[490] | 3463 | bool VspBspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
|
---|
| 3464 | {
|
---|
| 3465 | if (!ViewCellsConstructed())
|
---|
| 3466 | return ViewCellsManager::GetViewPoint(viewPoint);
|
---|
| 3467 |
|
---|
| 3468 | // TODO: set reasonable limit
|
---|
| 3469 | const int limit = 20;
|
---|
| 3470 |
|
---|
| 3471 | for (int i = 0; i < limit; ++ i)
|
---|
| 3472 | {
|
---|
[542] | 3473 | viewPoint = mViewSpaceBox.GetRandomPoint();
|
---|
[490] | 3474 | if (mVspBspTree->ViewPointValid(viewPoint))
|
---|
| 3475 | {
|
---|
| 3476 | return true;
|
---|
| 3477 | }
|
---|
| 3478 | }
|
---|
| 3479 | Debug << "failed to find valid view point, taking " << viewPoint << endl;
|
---|
| 3480 | return false;
|
---|
| 3481 | }
|
---|
| 3482 |
|
---|
| 3483 |
|
---|
| 3484 | bool VspBspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
|
---|
| 3485 | {
|
---|
[569] | 3486 | // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
|
---|
| 3487 | // validy update in preprocessor for all managers)
|
---|
| 3488 | return ViewCellsManager::ViewPointValid(viewPoint);
|
---|
[570] | 3489 |
|
---|
[569] | 3490 | // return mViewSpaceBox.IsInside(viewPoint) &&
|
---|
| 3491 | // mVspBspTree->ViewPointValid(viewPoint);
|
---|
[490] | 3492 | }
|
---|
| 3493 |
|
---|
| 3494 |
|
---|
[442] | 3495 | void VspBspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
[466] | 3496 | const VssRayContainer &sampleRays)
|
---|
[442] | 3497 | {
|
---|
| 3498 | if (!ViewCellsConstructed())
|
---|
| 3499 | return;
|
---|
| 3500 |
|
---|
[485] | 3501 | VssRayContainer visRays;
|
---|
| 3502 | GetRaySets(sampleRays, mVisualizationSamples, visRays);
|
---|
[483] | 3503 |
|
---|
[600] | 3504 |
|
---|
[651] | 3505 | if (1) // export view cells
|
---|
[612] | 3506 | { // hack pvs
|
---|
[600] | 3507 | int savedColorCode = mColorCode;
|
---|
[651] | 3508 | mColorCode = 0;
|
---|
[712] | 3509 | Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
|
---|
[600] | 3510 |
|
---|
[442] | 3511 | if (exporter)
|
---|
| 3512 | {
|
---|
[580] | 3513 | cout << "exporting view cells after post process ... ";
|
---|
| 3514 |
|
---|
[600] | 3515 | if (0)
|
---|
[542] | 3516 | {
|
---|
| 3517 | exporter->SetWireframe();
|
---|
| 3518 | exporter->ExportBox(mViewSpaceBox);
|
---|
| 3519 | exporter->SetFilled();
|
---|
| 3520 | }
|
---|
| 3521 |
|
---|
[485] | 3522 | if (mExportGeometry)
|
---|
[542] | 3523 | {
|
---|
[485] | 3524 | exporter->ExportGeometry(objects);
|
---|
[542] | 3525 | }
|
---|
[489] | 3526 |
|
---|
| 3527 | // export rays
|
---|
[490] | 3528 | if (mExportRays)
|
---|
[489] | 3529 | {
|
---|
[490] | 3530 | exporter->ExportRays(visRays, RgbColor(0, 1, 0));
|
---|
| 3531 | }
|
---|
[564] | 3532 |
|
---|
[508] | 3533 | ExportViewCellsForViz(exporter);
|
---|
[442] | 3534 | delete exporter;
|
---|
[580] | 3535 | cout << "finished" << endl;
|
---|
[442] | 3536 | }
|
---|
[600] | 3537 |
|
---|
| 3538 | mColorCode = savedColorCode;
|
---|
[557] | 3539 | }
|
---|
[503] | 3540 |
|
---|
[600] | 3541 | if (0)
|
---|
[577] | 3542 | {
|
---|
| 3543 | cout << "exporting depth map ... ";
|
---|
| 3544 |
|
---|
| 3545 | Exporter *exporter = Exporter::GetExporter("depth_map.x3d");
|
---|
| 3546 | if (exporter)
|
---|
| 3547 | {
|
---|
| 3548 | if (1)
|
---|
| 3549 | {
|
---|
| 3550 | exporter->SetWireframe();
|
---|
| 3551 | exporter->ExportBox(mViewSpaceBox);
|
---|
| 3552 | exporter->SetFilled();
|
---|
| 3553 | }
|
---|
| 3554 |
|
---|
| 3555 | if (mExportGeometry)
|
---|
| 3556 | {
|
---|
| 3557 | exporter->ExportGeometry(objects);
|
---|
| 3558 | }
|
---|
| 3559 |
|
---|
| 3560 | const int maxDepth = mVspBspTree->mBspStats.maxDepth;
|
---|
| 3561 |
|
---|
| 3562 | ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
|
---|
| 3563 |
|
---|
| 3564 | for (vit = mViewCells.begin(); vit != mViewCells.end(); ++ vit)
|
---|
| 3565 | {
|
---|
[582] | 3566 | ViewCell *vc = *vit;
|
---|
[577] | 3567 |
|
---|
[580] | 3568 | ViewCellContainer leaves;
|
---|
| 3569 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
[577] | 3570 |
|
---|
[580] | 3571 | ViewCellContainer::const_iterator lit, lit_end = leaves.end();
|
---|
| 3572 |
|
---|
| 3573 | for (lit = leaves.begin(); lit != lit_end; ++ lit)
|
---|
[577] | 3574 | {
|
---|
[580] | 3575 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*lit)->mLeaf;
|
---|
[577] | 3576 |
|
---|
| 3577 | Material m;
|
---|
| 3578 |
|
---|
| 3579 | float relDepth = (float)leaf->GetDepth() / (float)maxDepth;
|
---|
| 3580 | m.mDiffuseColor.r = relDepth;
|
---|
| 3581 | m.mDiffuseColor.g = 0.0f;
|
---|
| 3582 | m.mDiffuseColor.b = 1.0f - relDepth;
|
---|
| 3583 |
|
---|
| 3584 | exporter->SetForcedMaterial(m);
|
---|
| 3585 |
|
---|
| 3586 |
|
---|
| 3587 | BspNodeGeometry geom;
|
---|
| 3588 | mVspBspTree->ConstructGeometry(leaf, geom);
|
---|
[678] | 3589 | exporter->ExportPolygons(geom.GetPolys());
|
---|
[577] | 3590 | }
|
---|
| 3591 | }
|
---|
| 3592 |
|
---|
| 3593 | delete exporter;
|
---|
| 3594 | }
|
---|
| 3595 |
|
---|
| 3596 |
|
---|
| 3597 | cout << "finished" << endl;
|
---|
| 3598 | }
|
---|
| 3599 |
|
---|
[442] | 3600 | //-- visualization of the BSP splits
|
---|
| 3601 | bool exportSplits = false;
|
---|
[489] | 3602 | environment->GetBoolValue("VspBspTree.Visualization.exportSplits", exportSplits);
|
---|
[503] | 3603 |
|
---|
[442] | 3604 | if (exportSplits)
|
---|
| 3605 | {
|
---|
| 3606 | cout << "exporting splits ... ";
|
---|
[485] | 3607 | ExportSplits(objects, visRays);
|
---|
[442] | 3608 | cout << "finished" << endl;
|
---|
| 3609 | }
|
---|
| 3610 |
|
---|
[542] | 3611 | //-- export single view cells
|
---|
[485] | 3612 | ExportBspPvs(objects, visRays);
|
---|
[442] | 3613 | }
|
---|
| 3614 |
|
---|
[503] | 3615 |
|
---|
[485] | 3616 | void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects,
|
---|
| 3617 | const VssRayContainer &rays)
|
---|
[442] | 3618 | {
|
---|
| 3619 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
| 3620 |
|
---|
[503] | 3621 | if (exporter)
|
---|
| 3622 | {
|
---|
| 3623 | Material m;
|
---|
[442] | 3624 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 3625 | exporter->SetForcedMaterial(m);
|
---|
| 3626 | exporter->SetWireframe();
|
---|
[503] | 3627 |
|
---|
[448] | 3628 | exporter->ExportBspSplits(*mVspBspTree, true);
|
---|
[442] | 3629 |
|
---|
| 3630 | // take forced material, else big scenes cannot be viewed
|
---|
| 3631 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 3632 | exporter->SetForcedMaterial(m);
|
---|
| 3633 | exporter->SetFilled();
|
---|
| 3634 |
|
---|
| 3635 | exporter->ResetForcedMaterial();
|
---|
[503] | 3636 |
|
---|
[442] | 3637 | // export rays
|
---|
[485] | 3638 | if (mExportRays)
|
---|
| 3639 | exporter->ExportRays(rays, RgbColor(1, 1, 0));
|
---|
[503] | 3640 |
|
---|
[485] | 3641 | if (mExportGeometry)
|
---|
[442] | 3642 | exporter->ExportGeometry(objects);
|
---|
| 3643 |
|
---|
| 3644 | delete exporter;
|
---|
| 3645 | }
|
---|
| 3646 | }
|
---|
| 3647 |
|
---|
[503] | 3648 |
|
---|
[485] | 3649 | void VspBspViewCellsManager::ExportBspPvs(const ObjectContainer &objects,
|
---|
| 3650 | const VssRayContainer &rays)
|
---|
[442] | 3651 | {
|
---|
[639] | 3652 | const int leafOut = 20;
|
---|
[503] | 3653 |
|
---|
[442] | 3654 | ViewCell::NewMail();
|
---|
[503] | 3655 |
|
---|
[639] | 3656 | cout << "visualization using " << (int)rays.size() << " samples" << endl;
|
---|
| 3657 | Debug << "visualization using " << (int)rays.size() << " samples" << endl;
|
---|
[475] | 3658 | Debug << "\nOutput view cells: " << endl;
|
---|
[503] | 3659 |
|
---|
[694] | 3660 | const bool sortViewCells = true;
|
---|
| 3661 |
|
---|
[485] | 3662 | // sort view cells to visualize the largest view cells
|
---|
[694] | 3663 | if (sortViewCells)
|
---|
[587] | 3664 | stable_sort(mViewCells.begin(), mViewCells.end(), vc_gt);
|
---|
[639] | 3665 |
|
---|
[503] | 3666 | int limit = min(leafOut, (int)mViewCells.size());
|
---|
[483] | 3667 |
|
---|
[639] | 3668 | int raysOut = 0;
|
---|
[587] | 3669 |
|
---|
[485] | 3670 | //-- some rays for output
|
---|
[478] | 3671 | for (int i = 0; i < limit; ++ i)
|
---|
[442] | 3672 | {
|
---|
[478] | 3673 | cout << "creating output for view cell " << i << " ... ";
|
---|
[503] | 3674 |
|
---|
[582] | 3675 | ViewCell *vc;
|
---|
[574] | 3676 |
|
---|
[694] | 3677 | if (sortViewCells) // largest view cell pvs first
|
---|
[582] | 3678 | vc = mViewCells[i];
|
---|
[574] | 3679 | else
|
---|
[639] | 3680 | vc = mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 1)];
|
---|
[442] | 3681 |
|
---|
[639] | 3682 | //bspLeaves[j]->Mail();
|
---|
| 3683 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
| 3684 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
| 3685 |
|
---|
| 3686 | Debug << i << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc) << endl;
|
---|
[587] | 3687 |
|
---|
[639] | 3688 | if (1 || mExportRays)
|
---|
[442] | 3689 | {
|
---|
[694] | 3690 | // output rays stored with the view cells during subdivision
|
---|
[643] | 3691 | if (0)
|
---|
[442] | 3692 | {
|
---|
[639] | 3693 | VssRayContainer vcRays;
|
---|
| 3694 | VssRayContainer collectRays;
|
---|
| 3695 |
|
---|
| 3696 | raysOut = min((int)rays.size(), 100);
|
---|
| 3697 |
|
---|
| 3698 | // collect intial view cells
|
---|
| 3699 | ViewCellContainer leaves;
|
---|
| 3700 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 3701 |
|
---|
| 3702 | ViewCellContainer::const_iterator vit, vit_end = leaves.end();
|
---|
| 3703 |
|
---|
| 3704 | for (vit = leaves.begin(); vit != vit_end; ++ vit)
|
---|
[574] | 3705 | {
|
---|
[639] | 3706 | BspLeaf *vcLeaf = dynamic_cast<BspViewCell *>(*vit)->mLeaf;
|
---|
| 3707 |
|
---|
| 3708 | VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
|
---|
[587] | 3709 |
|
---|
[639] | 3710 | for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 3711 | {
|
---|
| 3712 | collectRays.push_back(*rit);
|
---|
| 3713 | }
|
---|
[574] | 3714 | }
|
---|
[639] | 3715 |
|
---|
| 3716 | VssRayContainer::const_iterator rit, rit_end = collectRays.end();
|
---|
| 3717 |
|
---|
| 3718 | for (rit = collectRays.begin(); rit != rit_end; ++ rit)
|
---|
| 3719 | {
|
---|
| 3720 | float p = RandomValue(0.0f, (float)collectRays.size());
|
---|
| 3721 |
|
---|
| 3722 | if (p < raysOut)
|
---|
| 3723 | vcRays.push_back(*rit);
|
---|
| 3724 | }
|
---|
| 3725 | //-- export rays piercing this view cell
|
---|
| 3726 | exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
|
---|
[442] | 3727 | }
|
---|
[651] | 3728 |
|
---|
[694] | 3729 | // associate new rays with output view cell
|
---|
[643] | 3730 | if (1)
|
---|
[639] | 3731 | {
|
---|
| 3732 | VssRayContainer vcRays;
|
---|
| 3733 | raysOut = min((int)rays.size(), mVisualizationSamples);
|
---|
| 3734 | // check whether we can add the current ray to the output rays
|
---|
| 3735 | for (int k = 0; k < raysOut; ++ k)
|
---|
| 3736 | {
|
---|
| 3737 | VssRay *ray = rays[k];
|
---|
| 3738 | for (int j = 0; j < (int)ray->mViewCells.size(); ++ j)
|
---|
| 3739 | {
|
---|
| 3740 | ViewCell *rayvc = ray->mViewCells[j];
|
---|
| 3741 |
|
---|
| 3742 | if (rayvc == vc)
|
---|
| 3743 | vcRays.push_back(ray);
|
---|
| 3744 | }
|
---|
| 3745 | }
|
---|
| 3746 |
|
---|
| 3747 | //-- export rays piercing this view cell
|
---|
| 3748 | exporter->ExportRays(vcRays, RgbColor(1, 1, 0));
|
---|
| 3749 | }
|
---|
| 3750 |
|
---|
[442] | 3751 | }
|
---|
[639] | 3752 |
|
---|
[574] | 3753 |
|
---|
[478] | 3754 | exporter->SetWireframe();
|
---|
[442] | 3755 |
|
---|
[478] | 3756 | Material m;//= RandomMaterial();
|
---|
| 3757 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 3758 | exporter->SetForcedMaterial(m);
|
---|
[442] | 3759 |
|
---|
[580] | 3760 | ExportViewCellGeometry(exporter, vc);
|
---|
[639] | 3761 |
|
---|
| 3762 | exporter->SetFilled();
|
---|
[503] | 3763 |
|
---|
[639] | 3764 |
|
---|
[574] | 3765 | if (1)
|
---|
| 3766 | {
|
---|
[639] | 3767 | ObjectPvsMap::const_iterator oit,
|
---|
| 3768 | oit_end = vc->GetPvs().mEntries.end();
|
---|
[483] | 3769 |
|
---|
[580] | 3770 |
|
---|
[639] | 3771 | exporter->SetFilled();
|
---|
[580] | 3772 |
|
---|
[639] | 3773 | Intersectable::NewMail();
|
---|
[574] | 3774 |
|
---|
[639] | 3775 | // output PVS of view cell
|
---|
| 3776 | for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
|
---|
| 3777 | {
|
---|
| 3778 | Intersectable *intersect = (*oit).first;
|
---|
[442] | 3779 |
|
---|
[639] | 3780 | if (!intersect->Mailed())
|
---|
| 3781 | {
|
---|
[643] | 3782 | m = RandomMaterial();
|
---|
| 3783 | exporter->SetForcedMaterial(m);
|
---|
[442] | 3784 |
|
---|
[639] | 3785 | exporter->ExportIntersectable(intersect);
|
---|
| 3786 | intersect->Mail();
|
---|
| 3787 | }
|
---|
[503] | 3788 | }
|
---|
[442] | 3789 | }
|
---|
[639] | 3790 | else
|
---|
| 3791 | {
|
---|
[643] | 3792 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 3793 | exporter->SetForcedMaterial(m);
|
---|
| 3794 |
|
---|
[639] | 3795 | exporter->ExportGeometry(objects);
|
---|
| 3796 | }
|
---|
[503] | 3797 |
|
---|
[478] | 3798 | DEL_PTR(exporter);
|
---|
| 3799 | cout << "finished" << endl;
|
---|
[442] | 3800 | }
|
---|
[475] | 3801 |
|
---|
| 3802 | Debug << endl;
|
---|
[442] | 3803 | }
|
---|
| 3804 |
|
---|
| 3805 |
|
---|
[697] | 3806 | int VspBspViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const
|
---|
| 3807 | {
|
---|
| 3808 | return mVspBspTree->ComputeBoxIntersections(box, viewCells);
|
---|
| 3809 | }
|
---|
| 3810 |
|
---|
| 3811 |
|
---|
[469] | 3812 | int VspBspViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
| 3813 | const Vector3 &termination,
|
---|
| 3814 | ViewCellContainer &viewcells)
|
---|
[466] | 3815 | {
|
---|
[469] | 3816 | return mVspBspTree->CastLineSegment(origin, termination, viewcells);
|
---|
[466] | 3817 | }
|
---|
[482] | 3818 |
|
---|
| 3819 |
|
---|
[503] | 3820 | void VspBspViewCellsManager::ExportColor(Exporter *exporter,
|
---|
[482] | 3821 | ViewCell *vc) const
|
---|
| 3822 | {
|
---|
[564] | 3823 | const bool vcValid = CheckValidity(vc, mMinPvsSize, mMaxPvsSize);
|
---|
[503] | 3824 |
|
---|
[482] | 3825 | float importance = 0;
|
---|
[564] | 3826 | static Material m;
|
---|
| 3827 |
|
---|
[482] | 3828 | switch (mColorCode)
|
---|
| 3829 | {
|
---|
[564] | 3830 | case 0: // Random
|
---|
| 3831 | {
|
---|
| 3832 | if (vcValid)
|
---|
| 3833 | {
|
---|
[598] | 3834 | m.mDiffuseColor.r = 0.5f + RandomValue(0.0f, 0.5f);
|
---|
| 3835 | m.mDiffuseColor.g = 0.5f + RandomValue(0.0f, 0.5f);
|
---|
| 3836 | m.mDiffuseColor.b = 0.5f + RandomValue(0.f, 0.5f);
|
---|
[564] | 3837 | }
|
---|
| 3838 | else
|
---|
| 3839 | {
|
---|
| 3840 | m.mDiffuseColor.r = 0.0f;
|
---|
| 3841 | m.mDiffuseColor.g = 1.0f;
|
---|
| 3842 | m.mDiffuseColor.b = 0.0f;
|
---|
| 3843 | }
|
---|
[591] | 3844 |
|
---|
| 3845 | exporter->SetForcedMaterial(m);
|
---|
| 3846 | return;
|
---|
[564] | 3847 | }
|
---|
[591] | 3848 |
|
---|
[482] | 3849 | case 1: // pvs
|
---|
| 3850 | {
|
---|
[503] | 3851 | importance = (float)vc->GetPvs().GetSize() /
|
---|
[660] | 3852 | (float)mCurrentViewCellsStats.maxPvs;
|
---|
[570] | 3853 |
|
---|
[482] | 3854 | }
|
---|
| 3855 | break;
|
---|
| 3856 | case 2: // merges
|
---|
| 3857 | {
|
---|
[580] | 3858 | int lSize = mViewCellsTree->GetSize(vc);
|
---|
[660] | 3859 | importance = (float)lSize / (float)mCurrentViewCellsStats.maxLeaves;
|
---|
[482] | 3860 | }
|
---|
| 3861 | break;
|
---|
| 3862 | case 3: // merge tree differene
|
---|
| 3863 | {
|
---|
[503] | 3864 | importance = (float)GetMaxTreeDiff(vc) /
|
---|
[482] | 3865 | (float)(mVspBspTree->GetStatistics().maxDepth * 2);
|
---|
[547] | 3866 |
|
---|
[482] | 3867 | }
|
---|
| 3868 | break;
|
---|
| 3869 | default:
|
---|
| 3870 | break;
|
---|
| 3871 | }
|
---|
| 3872 |
|
---|
[564] | 3873 | // special color code for invalid view cells
|
---|
[482] | 3874 | m.mDiffuseColor.r = importance;
|
---|
| 3875 | m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r;
|
---|
[564] | 3876 | m.mDiffuseColor.b = vcValid ? 1.0f : 0.0f;
|
---|
[547] | 3877 |
|
---|
[482] | 3878 | //Debug << "importance: " << importance << endl;
|
---|
| 3879 | exporter->SetForcedMaterial(m);
|
---|
| 3880 | }
|
---|
| 3881 |
|
---|
| 3882 |
|
---|
[580] | 3883 | void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
[591] | 3884 | ViewCell *vc,
|
---|
[660] | 3885 | const Plane3 *clipPlane) const
|
---|
[482] | 3886 | {
|
---|
[542] | 3887 | if (vc->GetMesh())
|
---|
[503] | 3888 | {
|
---|
| 3889 | exporter->ExportMesh(vc->GetMesh());
|
---|
[590] | 3890 |
|
---|
[503] | 3891 | return;
|
---|
| 3892 | }
|
---|
| 3893 |
|
---|
[591] | 3894 |
|
---|
[660] | 3895 | if (clipPlane)
|
---|
[591] | 3896 | {
|
---|
| 3897 | ViewCellContainer leaves;
|
---|
| 3898 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 3899 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 3900 |
|
---|
| 3901 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 3902 | {
|
---|
| 3903 | BspNodeGeometry geom;
|
---|
| 3904 |
|
---|
| 3905 | BspNodeGeometry front;
|
---|
| 3906 | BspNodeGeometry back;
|
---|
| 3907 |
|
---|
| 3908 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 3909 | mVspBspTree->ConstructGeometry(leaf, geom);
|
---|
| 3910 |
|
---|
[710] | 3911 | const float eps = 0.00000001f;
|
---|
| 3912 | const int cf = geom.Side(*clipPlane, eps);
|
---|
[682] | 3913 |
|
---|
[710] | 3914 | if (cf == -1)
|
---|
[682] | 3915 | {
|
---|
| 3916 | exporter->ExportPolygons(geom.GetPolys());
|
---|
| 3917 | }
|
---|
[710] | 3918 | else if (cf == 0)
|
---|
[684] | 3919 | {
|
---|
[682] | 3920 | geom.SplitGeometry(front,
|
---|
| 3921 | back,
|
---|
| 3922 | *clipPlane,
|
---|
| 3923 | mViewSpaceBox,
|
---|
[710] | 3924 | eps);
|
---|
[682] | 3925 |
|
---|
[694] | 3926 | //Debug << "geo size: " << geom.Size() << endl;
|
---|
| 3927 | //Debug << "size b: " << back.Size() << " f: " << front.Size() << endl;
|
---|
[683] | 3928 | if (back.Valid())
|
---|
| 3929 | {
|
---|
[682] | 3930 | exporter->ExportPolygons(back.GetPolys());
|
---|
[708] | 3931 | }
|
---|
[684] | 3932 | }
|
---|
[591] | 3933 | }
|
---|
| 3934 | }
|
---|
| 3935 | else
|
---|
| 3936 | {
|
---|
| 3937 | BspNodeGeometry geom;
|
---|
| 3938 | mVspBspTree->ConstructGeometry(vc, geom);
|
---|
| 3939 |
|
---|
[678] | 3940 | exporter->ExportPolygons(geom.GetPolys());
|
---|
[591] | 3941 | }
|
---|
[482] | 3942 | }
|
---|
| 3943 |
|
---|
| 3944 |
|
---|
| 3945 | int VspBspViewCellsManager::GetMaxTreeDiff(ViewCell *vc) const
|
---|
| 3946 | {
|
---|
[580] | 3947 | ViewCellContainer leaves;
|
---|
| 3948 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
[482] | 3949 |
|
---|
| 3950 | int maxDist = 0;
|
---|
[580] | 3951 |
|
---|
[482] | 3952 | // compute max height difference
|
---|
[580] | 3953 | for (int i = 0; i < (int)leaves.size(); ++ i)
|
---|
| 3954 | for (int j = 0; j < (int)leaves.size(); ++ j)
|
---|
[482] | 3955 | {
|
---|
[580] | 3956 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(leaves[i])->mLeaf;
|
---|
[482] | 3957 |
|
---|
| 3958 | if (i != j)
|
---|
| 3959 | {
|
---|
[580] | 3960 | BspLeaf *leaf2 =dynamic_cast<BspViewCell *>(leaves[j])->mLeaf;
|
---|
[482] | 3961 | int dist = mVspBspTree->TreeDistance(leaf, leaf2);
|
---|
| 3962 | if (dist > maxDist)
|
---|
| 3963 | maxDist = dist;
|
---|
| 3964 | }
|
---|
| 3965 | }
|
---|
[580] | 3966 |
|
---|
[492] | 3967 | return maxDist;
|
---|
| 3968 | }
|
---|
[482] | 3969 |
|
---|
[492] | 3970 |
|
---|
[569] | 3971 | ViewCell *VspBspViewCellsManager::GetViewCell(const Vector3 &point) const
|
---|
[492] | 3972 | {
|
---|
[508] | 3973 | if (!mVspBspTree)
|
---|
| 3974 | return NULL;
|
---|
[572] | 3975 |
|
---|
| 3976 | if (!mViewSpaceBox.IsInside(point))
|
---|
| 3977 | return NULL;
|
---|
| 3978 |
|
---|
[508] | 3979 | return mVspBspTree->GetViewCell(point);
|
---|
[492] | 3980 | }
|
---|
[503] | 3981 |
|
---|
| 3982 |
|
---|
| 3983 | void VspBspViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
| 3984 | {
|
---|
[577] | 3985 | if (vc->GetMesh())
|
---|
| 3986 | delete vc->GetMesh();
|
---|
| 3987 |
|
---|
[590] | 3988 |
|
---|
[503] | 3989 | BspNodeGeometry geom;
|
---|
[590] | 3990 |
|
---|
| 3991 | mVspBspTree->ConstructGeometry(vc, geom);
|
---|
[582] | 3992 |
|
---|
[503] | 3993 | Mesh *mesh = new Mesh();
|
---|
| 3994 | geom.AddToMesh(*mesh);
|
---|
| 3995 | vc->SetMesh(mesh);
|
---|
| 3996 | mMeshContainer.push_back(mesh);
|
---|
| 3997 | }
|
---|
[508] | 3998 |
|
---|
| 3999 |
|
---|
[577] | 4000 | ViewCellsManager *ViewCellsManager::LoadViewCells(const string filename,
|
---|
| 4001 | ObjectContainer *objects)
|
---|
[508] | 4002 | {
|
---|
| 4003 | ViewCellsParser parser;
|
---|
[564] | 4004 |
|
---|
[577] | 4005 | ViewCellsManager *vm = NULL;
|
---|
[564] | 4006 |
|
---|
[577] | 4007 | if (parser.ParseFile(filename, &vm, objects))
|
---|
| 4008 | {
|
---|
[590] | 4009 | //vm->PrepareLoadedViewCells();
|
---|
[577] | 4010 | vm->ResetViewCells();
|
---|
[564] | 4011 |
|
---|
[577] | 4012 | vm->mViewCellsFinished = true;
|
---|
| 4013 | vm->mMaxPvsSize = (int)objects->size();
|
---|
[517] | 4014 |
|
---|
[719] | 4015 | // create the meshes and compute volumes
|
---|
[577] | 4016 | vm->FinalizeViewCells(true);
|
---|
[564] | 4017 |
|
---|
[685] | 4018 | vm->mViewCellsTree->AssignRandomColors();
|
---|
[577] | 4019 | Debug << (int)vm->mViewCells.size() << " view cells loaded" << endl;
|
---|
| 4020 | }
|
---|
| 4021 | else
|
---|
| 4022 | {
|
---|
| 4023 | Debug << "failed loading view cells" << endl;
|
---|
| 4024 | DEL_PTR(vm);
|
---|
| 4025 | }
|
---|
| 4026 |
|
---|
| 4027 | return vm;
|
---|
[508] | 4028 | }
|
---|
| 4029 |
|
---|
| 4030 |
|
---|
| 4031 | inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 4032 | {
|
---|
| 4033 | return obj1->mId < obj2->mId;
|
---|
| 4034 | }
|
---|
| 4035 |
|
---|
| 4036 |
|
---|
| 4037 | bool VspBspViewCellsManager::ExportViewCells(const string filename)
|
---|
| 4038 | {
|
---|
[564] | 4039 | cout << "exporting view cells to xml ... ";
|
---|
[508] | 4040 | std::ofstream stream;
|
---|
| 4041 |
|
---|
[577] | 4042 | // for output we need unique ids for each view cell
|
---|
[590] | 4043 | CreateUniqueViewCellIds();
|
---|
[508] | 4044 |
|
---|
| 4045 | stream.open(filename.c_str());
|
---|
| 4046 | stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
| 4047 | stream << "<Visibility_Solution>" << endl;
|
---|
[564] | 4048 |
|
---|
[577] | 4049 | //-- the view space bounding box
|
---|
| 4050 | stream << "<ViewSpaceBox"
|
---|
| 4051 | << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
|
---|
| 4052 | << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\" />" << endl;
|
---|
| 4053 |
|
---|
| 4054 | //-- the type of the view cells hierarchy
|
---|
| 4055 | stream << "<Hierarchy name=\"vspBspTree\" />" << endl;
|
---|
| 4056 |
|
---|
[508] | 4057 | //-- load the view cells itself, i.e., the ids and the pvs
|
---|
| 4058 | stream << "<ViewCells>" << endl;
|
---|
[610] | 4059 |
|
---|
| 4060 | #if 0
|
---|
| 4061 |
|
---|
[508] | 4062 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
[610] | 4063 |
|
---|
[508] | 4064 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 4065 | ExportViewCell(*it, stream);
|
---|
[610] | 4066 | #else
|
---|
| 4067 | mViewCellsTree->Export(stream);
|
---|
| 4068 | #endif
|
---|
[564] | 4069 |
|
---|
[508] | 4070 | stream << "</ViewCells>" << endl;
|
---|
[564] | 4071 |
|
---|
[508] | 4072 | //-- load the hierarchy
|
---|
[590] | 4073 | stream << "<Hierarchy>" << endl;
|
---|
[508] | 4074 | mVspBspTree->Export(stream);
|
---|
[590] | 4075 | stream << endl << "</Hierarchy>" << endl;
|
---|
[564] | 4076 |
|
---|
[508] | 4077 | stream << "</Visibility_Solution>" << endl;
|
---|
| 4078 | stream.close();
|
---|
| 4079 |
|
---|
| 4080 | cout << "finished" << endl;
|
---|
| 4081 |
|
---|
| 4082 | return true;
|
---|
[532] | 4083 | }
|
---|
| 4084 |
|
---|
| 4085 |
|
---|
| 4086 | int VspBspViewCellsManager::CastBeam(Beam &beam)
|
---|
| 4087 | {
|
---|
[535] | 4088 | return mVspBspTree->CastBeam(beam);
|
---|
[532] | 4089 | }
|
---|
[551] | 4090 |
|
---|
| 4091 |
|
---|
[587] | 4092 | void VspBspViewCellsManager::Finalize(ViewCell *viewCell,
|
---|
| 4093 | const bool createMesh)
|
---|
[551] | 4094 | {
|
---|
| 4095 | float area = 0;
|
---|
| 4096 | float volume = 0;
|
---|
| 4097 |
|
---|
[580] | 4098 | ViewCellContainer leaves;
|
---|
[582] | 4099 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
[580] | 4100 |
|
---|
| 4101 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 4102 |
|
---|
[693] | 4103 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[551] | 4104 | {
|
---|
| 4105 | BspNodeGeometry geom;
|
---|
[580] | 4106 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
[551] | 4107 | mVspBspTree->ConstructGeometry(leaf, geom);
|
---|
| 4108 |
|
---|
[719] | 4109 | const float lVol = geom.GetVolume();
|
---|
| 4110 | const float lArea = geom.GetArea();
|
---|
[693] | 4111 |
|
---|
| 4112 | //(*it)->SetVolume(vol);
|
---|
| 4113 | //(*it)->SetArea(area);
|
---|
| 4114 |
|
---|
| 4115 | area += lArea;
|
---|
| 4116 | volume += lVol;
|
---|
| 4117 |
|
---|
| 4118 | CreateMesh(*it);
|
---|
[551] | 4119 | }
|
---|
| 4120 |
|
---|
| 4121 | viewCell->SetVolume(volume);
|
---|
| 4122 | viewCell->SetArea(area);
|
---|
[563] | 4123 | }
|
---|
[575] | 4124 |
|
---|
| 4125 |
|
---|
[693] | 4126 | void VspBspViewCellsManager::TestSubdivision()
|
---|
| 4127 | {
|
---|
| 4128 | ViewCellContainer leaves;
|
---|
| 4129 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 4130 |
|
---|
| 4131 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 4132 |
|
---|
| 4133 | const float vol = mViewSpaceBox.GetVolume();
|
---|
| 4134 | float subdivVol = 0;
|
---|
| 4135 | float newVol = 0;
|
---|
| 4136 |
|
---|
| 4137 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 4138 | {
|
---|
| 4139 | BspNodeGeometry geom;
|
---|
| 4140 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 4141 | mVspBspTree->ConstructGeometry(leaf, geom);
|
---|
| 4142 |
|
---|
| 4143 | const float lVol = geom.GetVolume();
|
---|
| 4144 |
|
---|
| 4145 | newVol += lVol;
|
---|
| 4146 | subdivVol += (*it)->GetVolume();
|
---|
[697] | 4147 |
|
---|
[704] | 4148 | float thres = 0.9f;
|
---|
[697] | 4149 | if ((lVol < ((*it)->GetVolume() * thres)) || (lVol * thres > ((*it)->GetVolume())))
|
---|
| 4150 | Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
|
---|
[693] | 4151 | }
|
---|
| 4152 |
|
---|
| 4153 | Debug << "exact volume: " << vol << endl;
|
---|
| 4154 | Debug << "subdivision volume: " << subdivVol << endl;
|
---|
| 4155 | Debug << "new volume: " << newVol << endl;
|
---|
| 4156 | }
|
---|
| 4157 |
|
---|
| 4158 |
|
---|
[577] | 4159 | void VspBspViewCellsManager::PrepareLoadedViewCells()
|
---|
| 4160 | {
|
---|
| 4161 | // TODO: do I still need this here?
|
---|
[590] | 4162 | if (0)
|
---|
[693] | 4163 | mVspBspTree->RepairViewCellsLeafLists();
|
---|
[577] | 4164 | }
|
---|
[575] | 4165 |
|
---|
[577] | 4166 |
|
---|
[697] | 4167 | void VspBspViewCellsManager::TestFilter(const ObjectContainer &objects)
|
---|
| 4168 | {
|
---|
| 4169 | Exporter *exporter = Exporter::GetExporter("filter.x3d");
|
---|
[580] | 4170 |
|
---|
[704] | 4171 | Vector3 bsize = mViewSpaceBox.Size();
|
---|
| 4172 | const Vector3 viewPoint(mViewSpaceBox.Center());
|
---|
| 4173 | const Vector3 width = Vector3(mFilterWidth);
|
---|
| 4174 |
|
---|
| 4175 | PrVs testPrVs;
|
---|
| 4176 |
|
---|
[697] | 4177 | if (exporter)
|
---|
| 4178 | {
|
---|
| 4179 | ViewCellContainer viewCells;
|
---|
[704] | 4180 |
|
---|
| 4181 | const AxisAlignedBox3 tbox = GetFilterBBox(viewPoint, mFilterWidth);
|
---|
| 4182 |
|
---|
| 4183 | GetPrVS(viewPoint, testPrVs);
|
---|
| 4184 |
|
---|
[697] | 4185 | exporter->SetWireframe();
|
---|
| 4186 |
|
---|
| 4187 | exporter->SetForcedMaterial(RgbColor(1,1,1));
|
---|
[704] | 4188 | exporter->ExportBox(tbox);
|
---|
[697] | 4189 |
|
---|
| 4190 | exporter->SetFilled();
|
---|
| 4191 |
|
---|
[705] | 4192 | exporter->SetForcedMaterial(RgbColor(0,1,0));
|
---|
| 4193 | ExportViewCellGeometry(exporter, GetViewCell(viewPoint));
|
---|
| 4194 |
|
---|
| 4195 | //exporter->ResetForcedMaterial();
|
---|
| 4196 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
[704] | 4197 | ExportViewCellGeometry(exporter, testPrVs.mViewCell);
|
---|
| 4198 |
|
---|
| 4199 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
[697] | 4200 | exporter->ExportGeometry(objects);
|
---|
| 4201 |
|
---|
| 4202 | delete exporter;
|
---|
| 4203 | }
|
---|
| 4204 | }
|
---|
| 4205 |
|
---|
| 4206 |
|
---|
[580] | 4207 | void VspBspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 4208 | vector<MergeCandidate> &candidates)
|
---|
| 4209 | {
|
---|
| 4210 | cout << "collecting merge candidates ... " << endl;
|
---|
| 4211 |
|
---|
| 4212 | if (mUseRaysForMerge)
|
---|
| 4213 | {
|
---|
| 4214 | mVspBspTree->CollectMergeCandidates(rays, candidates);
|
---|
| 4215 | }
|
---|
| 4216 | else
|
---|
| 4217 | {
|
---|
| 4218 | vector<BspLeaf *> leaves;
|
---|
| 4219 | mVspBspTree->CollectLeaves(leaves);
|
---|
[708] | 4220 |
|
---|
[580] | 4221 | mVspBspTree->CollectMergeCandidates(leaves, candidates);
|
---|
| 4222 | }
|
---|
| 4223 |
|
---|
| 4224 | cout << "fininshed collecting candidates" << endl;
|
---|
| 4225 | }
|
---|
| 4226 |
|
---|
| 4227 |
|
---|
[590] | 4228 | void VspBspViewCellsManager::AddCurrentViewCellsToHierarchy()
|
---|
| 4229 | {
|
---|
| 4230 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 4231 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 4232 | {
|
---|
| 4233 | }
|
---|
| 4234 | }
|
---|
[580] | 4235 |
|
---|
| 4236 | //////////////////////////////////
|
---|
[575] | 4237 | ViewCellsManager *ViewCellsManagerFactory::Create(const string mName)
|
---|
| 4238 | {
|
---|
| 4239 | //TODO
|
---|
| 4240 | return NULL;// new VspBspViewCellsManager();
|
---|
| 4241 | }
|
---|
[580] | 4242 |
|
---|