[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"
|
---|
[1233] | 10 | #include "HierarchyManager.h"
|
---|
[440] | 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"
|
---|
[931] | 17 | #include "BoundingBoxConverter.h"
|
---|
[979] | 18 | #include "GlRenderer.h"
|
---|
[1001] | 19 | #include "ResourceManager.h"
|
---|
[1315] | 20 | #include "IntersectableWrapper.h"
|
---|
[1233] | 21 | #include "VspTree.h"
|
---|
| 22 | #include "OspTree.h"
|
---|
[1259] | 23 | #include "BvHierarchy.h"
|
---|
[440] | 24 |
|
---|
[1278] | 25 |
|
---|
[1159] | 26 | // should not count origin object for sampling because it disturbs heuristics
|
---|
[1166] | 27 | #define SAMPLE_ORIGIN_OBJECTS 0
|
---|
[931] | 28 |
|
---|
[863] | 29 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 30 |
|
---|
| 31 |
|
---|
[712] | 32 | // HACK
|
---|
| 33 | const static bool SAMPLE_AFTER_SUBDIVISION = true;
|
---|
| 34 | const static bool TEST_EMPTY_VIEW_CELLS = false;
|
---|
[517] | 35 |
|
---|
[697] | 36 |
|
---|
| 37 | template <typename T> class myless
|
---|
| 38 | {
|
---|
| 39 | public:
|
---|
| 40 | //bool operator() (HierarchyNode *v1, HierarchyNode *v2) const
|
---|
| 41 | bool operator() (T v1, T v2) const
|
---|
| 42 | {
|
---|
| 43 | return (v1->GetMergeCost() < v2->GetMergeCost());
|
---|
| 44 | }
|
---|
| 45 | };
|
---|
| 46 |
|
---|
| 47 |
|
---|
[1264] | 48 | ViewCellsManager::ViewCellsManager(ViewCellsTree *viewCellsTree):
|
---|
[480] | 49 | mRenderer(NULL),
|
---|
[574] | 50 | mInitialSamples(0),
|
---|
[440] | 51 | mConstructionSamples(0),
|
---|
| 52 | mPostProcessSamples(0),
|
---|
[470] | 53 | mVisualizationSamples(0),
|
---|
| 54 | mTotalAreaValid(false),
|
---|
[517] | 55 | mTotalArea(0.0f),
|
---|
[547] | 56 | mViewCellsFinished(false),
|
---|
[562] | 57 | mMaxPvsSize(9999999),
|
---|
[564] | 58 | mMinPvsSize(0), // one means only empty view cells are invalid
|
---|
[719] | 59 | mMaxPvsRatio(1.0),
|
---|
[1145] | 60 | mViewCellPvsIsUpdated(false),
|
---|
[1264] | 61 | mPreprocessor(NULL),
|
---|
| 62 | mViewCellsTree(viewCellsTree)
|
---|
[440] | 63 | {
|
---|
[542] | 64 | mViewSpaceBox.Initialize();
|
---|
[482] | 65 | ParseEnvironment();
|
---|
[580] | 66 |
|
---|
[1264] | 67 | mViewCellsTree->SetViewCellsManager(this);
|
---|
| 68 | //mViewCellsTree = new ViewCellsTree(this);
|
---|
[440] | 69 | }
|
---|
| 70 |
|
---|
[517] | 71 |
|
---|
[482] | 72 | void ViewCellsManager::ParseEnvironment()
|
---|
| 73 | {
|
---|
[503] | 74 | // visualization stuff
|
---|
[1004] | 75 | Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportRays", mExportRays);
|
---|
| 76 | Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportGeometry", mExportGeometry);
|
---|
| 77 | Environment::GetSingleton()->GetFloatValue("ViewCells.maxPvsRatio", mMaxPvsRatio);
|
---|
[664] | 78 |
|
---|
[1004] | 79 | Environment::GetSingleton()->GetBoolValue("ViewCells.pruneEmptyViewCells", mPruneEmptyViewCells);
|
---|
[664] | 80 |
|
---|
[710] | 81 | // HACK
|
---|
| 82 | if (0)
|
---|
| 83 | mMinPvsSize = mPruneEmptyViewCells ? 1 : 0;
|
---|
| 84 | else
|
---|
| 85 | mMinPvsSize = 0;
|
---|
| 86 |
|
---|
[1004] | 87 | Environment::GetSingleton()->GetBoolValue("ViewCells.processOnlyValidViewCells", mOnlyValidViewCells);
|
---|
[485] | 88 |
|
---|
[1004] | 89 | Environment::GetSingleton()->GetIntValue("ViewCells.Construction.samples", mConstructionSamples);
|
---|
| 90 | Environment::GetSingleton()->GetIntValue("ViewCells.PostProcess.samples", mPostProcessSamples);
|
---|
| 91 | Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.useRaysForMerge", mUseRaysForMerge);
|
---|
[595] | 92 |
|
---|
[1004] | 93 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.samples", mVisualizationSamples);
|
---|
[574] | 94 |
|
---|
[1004] | 95 | Environment::GetSingleton()->GetIntValue("ViewCells.Construction.samplesPerPass", mSamplesPerPass);
|
---|
| 96 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportToFile", mExportViewCells);
|
---|
[581] | 97 |
|
---|
[1004] | 98 | Environment::GetSingleton()->GetIntValue("ViewCells.active", mNumActiveViewCells);
|
---|
| 99 | Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.compress", mCompressViewCells);
|
---|
| 100 | Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.useClipPlane", mUseClipPlaneForViz);
|
---|
| 101 | Environment::GetSingleton()->GetBoolValue("ViewCells.PostProcess.merge", mMergeViewCells);
|
---|
| 102 | Environment::GetSingleton()->GetBoolValue("ViewCells.evaluateViewCells", mEvaluateViewCells);
|
---|
| 103 | Environment::GetSingleton()->GetBoolValue("ViewCells.showVisualization", mShowVisualization);
|
---|
| 104 | Environment::GetSingleton()->GetIntValue("ViewCells.Filter.maxSize", mMaxFilterSize);
|
---|
| 105 | Environment::GetSingleton()->GetFloatValue("ViewCells.Filter.width", mFilterWidth);
|
---|
| 106 | Environment::GetSingleton()->GetIntValue("ViewCells.renderCostEvaluationType", mRenderCostEvaluationType);
|
---|
[710] | 107 |
|
---|
[1004] | 108 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportBboxesForPvs", mExportBboxesForPvs);
|
---|
| 109 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportPvs", mExportPvs);
|
---|
[1142] | 110 |
|
---|
[664] | 111 | char buf[100];
|
---|
[1004] | 112 | Environment::GetSingleton()->GetStringValue("ViewCells.samplingType", buf);
|
---|
[664] | 113 |
|
---|
[660] | 114 |
|
---|
[1020] | 115 | // sampling type for view cells construction samples
|
---|
[664] | 116 | if (strcmp(buf, "box") == 0)
|
---|
[722] | 117 | {
|
---|
[660] | 118 | mSamplingType = Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
[722] | 119 | }
|
---|
[664] | 120 | else if (strcmp(buf, "directional") == 0)
|
---|
[722] | 121 | {
|
---|
[660] | 122 | mSamplingType = Preprocessor::DIRECTION_BASED_DISTRIBUTION;
|
---|
[722] | 123 | }
|
---|
[1020] | 124 | else if (strcmp(buf, "object_directional") == 0)
|
---|
| 125 | {
|
---|
| 126 | mSamplingType = Preprocessor::OBJECT_DIRECTION_BASED_DISTRIBUTION;
|
---|
| 127 | }
|
---|
[1022] | 128 | /*else if (strcmp(buf, "interior") == 0)
|
---|
| 129 | {
|
---|
| 130 | mSamplingType = Preprocessor::OBJECTS_INTERIOR_DISTRIBUTION;
|
---|
| 131 | }*/
|
---|
[1020] | 132 | else
|
---|
| 133 | {
|
---|
[666] | 134 | Debug << "error! wrong sampling type" << endl;
|
---|
| 135 | exit(0);
|
---|
| 136 | }
|
---|
[660] | 137 |
|
---|
[1020] | 138 | // sampling type for evaluation samples
|
---|
[1004] | 139 | Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.samplingType", buf);
|
---|
[722] | 140 |
|
---|
| 141 | if (strcmp(buf, "box") == 0)
|
---|
| 142 | {
|
---|
| 143 | mEvaluationSamplingType = Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
| 144 | }
|
---|
| 145 | else if (strcmp(buf, "directional") == 0)
|
---|
| 146 | {
|
---|
| 147 | mEvaluationSamplingType = Preprocessor::DIRECTION_BASED_DISTRIBUTION;
|
---|
| 148 | }
|
---|
[1020] | 149 | else if (strcmp(buf, "object_directional") == 0)
|
---|
[722] | 150 | {
|
---|
[1020] | 151 | mEvaluationSamplingType = Preprocessor::OBJECT_DIRECTION_BASED_DISTRIBUTION;
|
---|
| 152 | }
|
---|
[1022] | 153 | /*else if (strcmp(buf, "interior") == 0)
|
---|
[1020] | 154 | {
|
---|
| 155 | mEvaluationSamplingType = Preprocessor::OBJECTS_INTERIOR_DISTRIBUTION;
|
---|
[1022] | 156 | }*/
|
---|
[1020] | 157 | else
|
---|
| 158 | {
|
---|
[722] | 159 | Debug << "error! wrong sampling type" << endl;
|
---|
| 160 | exit(0);
|
---|
| 161 | }
|
---|
[503] | 162 |
|
---|
[1004] | 163 | Environment::GetSingleton()->GetStringValue("ViewCells.renderCostEvaluationType", buf);
|
---|
[728] | 164 |
|
---|
[734] | 165 | if (strcmp(buf, "perobject") == 0)
|
---|
[728] | 166 | {
|
---|
| 167 | mRenderCostEvaluationType = ViewCellsManager::PER_OBJECT;
|
---|
| 168 | }
|
---|
| 169 | else if (strcmp(buf, "directional") == 0)
|
---|
| 170 | {
|
---|
| 171 | mRenderCostEvaluationType = ViewCellsManager::PER_TRIANGLE;
|
---|
| 172 | }
|
---|
| 173 | else
|
---|
| 174 | {
|
---|
| 175 | Debug << "error! wrong sampling type" << endl;
|
---|
| 176 | exit(0);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[1004] | 179 | Environment::GetSingleton()->GetStringValue("ViewCells.Visualization.colorCode", buf);
|
---|
[722] | 180 |
|
---|
[482] | 181 | if (strcmp(buf, "PVS") == 0)
|
---|
| 182 | mColorCode = 1;
|
---|
| 183 | else if (strcmp(buf, "MergedLeaves") == 0)
|
---|
| 184 | mColorCode = 2;
|
---|
| 185 | else if (strcmp(buf, "MergedTreeDiff") == 0)
|
---|
| 186 | mColorCode = 3;
|
---|
| 187 | else
|
---|
| 188 | mColorCode = 0;
|
---|
| 189 |
|
---|
[664] | 190 |
|
---|
[710] | 191 |
|
---|
[1027] | 192 | Debug << "************ View Cells options ***************" << endl;
|
---|
[664] | 193 | Debug << "color code: " << mColorCode << endl;
|
---|
| 194 |
|
---|
| 195 | Debug << "export rays: " << mExportRays << endl;
|
---|
| 196 | Debug << "export geometry: " << mExportGeometry << endl;
|
---|
| 197 | Debug << "max pvs ratio: " << mMaxPvsRatio << endl;
|
---|
| 198 |
|
---|
[710] | 199 | Debug << "prune empty view cells: " << mPruneEmptyViewCells << endl;
|
---|
[664] | 200 |
|
---|
| 201 | Debug << "process only valid view cells: " << mOnlyValidViewCells << endl;
|
---|
| 202 | Debug << "construction samples: " << mConstructionSamples << endl;
|
---|
| 203 | Debug << "post process samples: " << mPostProcessSamples << endl;
|
---|
| 204 | Debug << "post process use rays for merge: " << mUseRaysForMerge << endl;
|
---|
| 205 | Debug << "visualization samples: " << mVisualizationSamples << endl;
|
---|
| 206 | Debug << "construction samples per pass: " << mSamplesPerPass << endl;
|
---|
| 207 | Debug << "export to file: " << mExportViewCells << endl;
|
---|
| 208 |
|
---|
[801] | 209 | Debug << "active view cells: " << mNumActiveViewCells << endl;
|
---|
[664] | 210 | Debug << "post process compress: " << mCompressViewCells << endl;
|
---|
| 211 | Debug << "visualization use clipPlane: " << mUseClipPlaneForViz << endl;
|
---|
| 212 | Debug << "post process merge: " << mMergeViewCells << endl;
|
---|
| 213 | Debug << "evaluate view cells: " << mEvaluateViewCells << endl;
|
---|
| 214 | Debug << "sampling type: " << mSamplingType << endl;
|
---|
[801] | 215 | Debug << "render cost evaluation type: " << mRenderCostEvaluationType << endl;
|
---|
[728] | 216 | Debug << "evaluation sampling type: " << mEvaluationSamplingType << endl;
|
---|
[666] | 217 | Debug << "show visualization: " << mShowVisualization << endl;
|
---|
[697] | 218 | Debug << "filter width: " << mFilterWidth << endl;
|
---|
[710] | 219 | Debug << "sample after subdivision: " << SAMPLE_AFTER_SUBDIVISION << endl;
|
---|
[850] | 220 |
|
---|
| 221 | Debug << "export bounding boxes: " << mExportBboxesForPvs << endl;
|
---|
| 222 | Debug << "export pvs for view cells: " << mExportPvs << endl;
|
---|
[1121] | 223 |
|
---|
[664] | 224 | Debug << endl;
|
---|
[440] | 225 | }
|
---|
| 226 |
|
---|
[485] | 227 |
|
---|
[440] | 228 | ViewCellsManager::~ViewCellsManager()
|
---|
| 229 | {
|
---|
[1020] | 230 | // HACK: if view cells tree does not
|
---|
[1264] | 231 | // handle view cells, we have to do it here
|
---|
[1020] | 232 | // question: rather create view cells resource manager?
|
---|
[598] | 233 | if (!ViewCellsTreeConstructed())
|
---|
[1141] | 234 | {
|
---|
[598] | 235 | CLEAR_CONTAINER(mViewCells);
|
---|
[1141] | 236 | }
|
---|
[1264] | 237 | //DEL_PTR(mViewCellsTree);
|
---|
[440] | 238 | }
|
---|
| 239 |
|
---|
[1221] | 240 |
|
---|
| 241 | AxisAlignedBox3 ViewCellsManager::GetViewCellBox(ViewCell *vc)
|
---|
[859] | 242 | {
|
---|
| 243 | Mesh *m = vc->GetMesh();
|
---|
| 244 |
|
---|
| 245 | if (m) {
|
---|
| 246 | m->ComputeBoundingBox();
|
---|
| 247 | return m->mBox;
|
---|
| 248 | }
|
---|
[485] | 249 |
|
---|
[859] | 250 | AxisAlignedBox3 box;
|
---|
| 251 |
|
---|
| 252 | box.Initialize();
|
---|
| 253 |
|
---|
| 254 | if (!vc->IsLeaf()) {
|
---|
| 255 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
| 256 |
|
---|
| 257 | ViewCellContainer::iterator it = vci->mChildren.begin();
|
---|
| 258 | for (; it != vci->mChildren.end(); ++it) {
|
---|
| 259 | box.Include(GetViewCellBox(*it));
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | return box;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[693] | 266 |
|
---|
[1021] | 267 | bool ViewCellsManager::EqualToSpatialNode(ViewCell *viewCell) const
|
---|
| 268 | {
|
---|
| 269 | return false;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 |
|
---|
[574] | 273 | int ViewCellsManager::CastPassSamples(const int samplesPerPass,
|
---|
| 274 | const int sampleType,
|
---|
| 275 | VssRayContainer &passSamples) const
|
---|
[527] | 276 | {
|
---|
[570] | 277 | SimpleRayContainer simpleRays;
|
---|
[1221] | 278 | const long startTime = GetTime();
|
---|
[564] | 279 |
|
---|
[1221] | 280 | mPreprocessor->GenerateRays(samplesPerPass, sampleType, simpleRays);
|
---|
| 281 | Debug << "generated " << mInitialSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
| 282 |
|
---|
[573] | 283 | // shoot simple ray and add it to importance samples
|
---|
[1145] | 284 | mPreprocessor->CastRays(simpleRays, passSamples);
|
---|
[1221] | 285 | Debug << "cast " << mInitialSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[570] | 286 |
|
---|
[574] | 287 | return (int)passSamples.size();
|
---|
| 288 | }
|
---|
[570] | 289 |
|
---|
[574] | 290 |
|
---|
| 291 | /// helper function which destroys rays or copies them into the output ray container
|
---|
[639] | 292 | inline void disposeRays(VssRayContainer &rays, VssRayContainer *outRays)
|
---|
[574] | 293 | {
|
---|
| 294 | cout << "disposing samples ... ";
|
---|
[582] | 295 | long startTime = GetTime();
|
---|
| 296 | int n = (int)rays.size();
|
---|
| 297 |
|
---|
[573] | 298 | if (outRays)
|
---|
[542] | 299 | {
|
---|
[574] | 300 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
| 301 |
|
---|
| 302 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 303 | {
|
---|
| 304 | outRays->push_back(*it);
|
---|
| 305 | }
|
---|
[573] | 306 | }
|
---|
| 307 | else
|
---|
| 308 | {
|
---|
[639] | 309 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
| 310 |
|
---|
| 311 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 312 | {
|
---|
| 313 | //(*it)->Unref();
|
---|
| 314 | if (!(*it)->IsActive())
|
---|
| 315 | delete (*it);
|
---|
| 316 | }
|
---|
[573] | 317 | }
|
---|
[639] | 318 |
|
---|
[574] | 319 | cout << "finished" << endl;
|
---|
[582] | 320 | Debug << "disposed " << n << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[573] | 321 | }
|
---|
[570] | 322 |
|
---|
| 323 |
|
---|
[573] | 324 | int ViewCellsManager::Construct(Preprocessor *preprocessor, VssRayContainer *outRays)
|
---|
| 325 | {
|
---|
| 326 | int numSamples = 0;
|
---|
[1077] | 327 |
|
---|
[573] | 328 | SimpleRayContainer simpleRays;
|
---|
[574] | 329 | VssRayContainer initialSamples;
|
---|
[540] | 330 |
|
---|
[1381] | 331 | // store pointer to preprocessor for further use during construction
|
---|
| 332 | mPreprocessor = preprocessor;
|
---|
| 333 |
|
---|
| 334 |
|
---|
| 335 | ///////////////////////////////////////////////////////
|
---|
| 336 | //-- Initial sampling for the construction of the view cell hierarchy.
|
---|
| 337 | //-- We use uniform sampling / box based sampling.
|
---|
| 338 |
|
---|
[752] | 339 | long startTime = GetTime();
|
---|
| 340 |
|
---|
[1381] | 341 | cout << "view cell construction: casting " << mInitialSamples << " initial samples ... ";
|
---|
| 342 | // cast initial samples
|
---|
| 343 | CastPassSamples(mInitialSamples, mSamplingType, initialSamples);
|
---|
[1278] | 344 |
|
---|
[1221] | 345 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[574] | 346 |
|
---|
[573] | 347 | // construct view cells
|
---|
[752] | 348 | ConstructSubdivision(preprocessor->mObjects, initialSamples);
|
---|
[540] | 349 |
|
---|
[727] | 350 | // initial samples count for overall samples ...
|
---|
[752] | 351 | numSamples += mInitialSamples;
|
---|
[542] | 352 |
|
---|
[574] | 353 | // rays can be passed or deleted
|
---|
[639] | 354 | disposeRays(initialSamples, outRays);
|
---|
[752] | 355 |
|
---|
| 356 | cout << "time needed for initial construction: "
|
---|
| 357 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
| 358 |
|
---|
| 359 | Debug << "time needed for initial construction: "
|
---|
| 360 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
| 361 |
|
---|
| 362 |
|
---|
[1381] | 363 | ///////////////////////////////////////////
|
---|
| 364 | //-- Initial hierarchy construction finished
|
---|
| 365 | //-- We can do some stats and visualization
|
---|
| 366 |
|
---|
[660] | 367 | ResetViewCells();
|
---|
[667] | 368 | Debug << "\nView cells after initial sampling:\n" << mCurrentViewCellsStats << endl;
|
---|
| 369 |
|
---|
[1381] | 370 |
|
---|
| 371 | //-- optionally export initial view cells
|
---|
| 372 | if (0)
|
---|
[660] | 373 | {
|
---|
[712] | 374 | const char filename[] = "view_cells.wrl";
|
---|
| 375 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 376 |
|
---|
[660] | 377 | if (exporter)
|
---|
| 378 | {
|
---|
[712] | 379 | cout << "exporting initial view cells (=leaves) to " << filename << " ... ";
|
---|
[660] | 380 | if (mExportGeometry)
|
---|
[712] | 381 | {
|
---|
[660] | 382 | exporter->ExportGeometry(preprocessor->mObjects);
|
---|
[712] | 383 | }
|
---|
[660] | 384 |
|
---|
[750] | 385 | exporter->SetWireframe();
|
---|
[660] | 386 | ExportViewCellsForViz(exporter);
|
---|
| 387 |
|
---|
| 388 | delete exporter;
|
---|
[712] | 389 | cout << "finished" << endl;
|
---|
[660] | 390 | }
|
---|
| 391 | }
|
---|
| 392 |
|
---|
[1381] | 393 |
|
---|
| 394 | ////////////////////////////////////////////////
|
---|
| 395 | //
|
---|
| 396 | //-- Cast some more sampling after initial construction.
|
---|
| 397 | //-- The additional rays can be used to gain
|
---|
| 398 | //-- some more information before the bottom-up merge
|
---|
| 399 | //-- note: guided rays could be used for this task
|
---|
| 400 |
|
---|
| 401 | // time spent after initial construction
|
---|
| 402 | startTime = GetTime();
|
---|
| 403 |
|
---|
[574] | 404 | const int n = mConstructionSamples; //+initialSamples;
|
---|
[666] | 405 | // should we use directional samples?
|
---|
[694] | 406 | bool dirSamples = (mSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
|
---|
[573] | 407 |
|
---|
| 408 | while (numSamples < n)
|
---|
| 409 | {
|
---|
[596] | 410 | cout << "casting " << mSamplesPerPass << " samples of " << n << " ... ";
|
---|
[727] | 411 | Debug << "casting " << mSamplesPerPass << " samples of " << n << " ... ";
|
---|
| 412 |
|
---|
[574] | 413 | VssRayContainer constructionSamples;
|
---|
| 414 |
|
---|
[694] | 415 | const int samplingType = mSamplingType;
|
---|
| 416 | /*dirSamples ?
|
---|
[574] | 417 | Preprocessor::DIRECTION_BASED_DISTRIBUTION :
|
---|
[694] | 418 | Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;*/
|
---|
[667] | 419 |
|
---|
[720] | 420 | if (0) dirSamples = !dirSamples; // toggle sampling method
|
---|
[667] | 421 |
|
---|
[727] | 422 | // cast new samples
|
---|
[574] | 423 | numSamples += CastPassSamples(mSamplesPerPass,
|
---|
[666] | 424 | samplingType,
|
---|
[574] | 425 | constructionSamples);
|
---|
| 426 |
|
---|
| 427 | cout << "finished" << endl;
|
---|
| 428 | cout << "computing sample contribution for " << (int)constructionSamples.size() << " samples ... ";
|
---|
| 429 |
|
---|
[727] | 430 | // computes sample contribution of cast rays TODO: leak?
|
---|
| 431 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
[605] | 432 | ComputeSampleContributions(constructionSamples, true, false);
|
---|
[712] | 433 |
|
---|
[574] | 434 | cout << "finished" << endl;
|
---|
| 435 |
|
---|
[639] | 436 | disposeRays(constructionSamples, outRays);
|
---|
[574] | 437 | cout << "total samples: " << numSamples << endl;
|
---|
[542] | 438 | }
|
---|
[752] | 439 |
|
---|
[1381] | 440 |
|
---|
| 441 | ////////////////////////////////////////////////
|
---|
| 442 | //-- get stats after the additional sampling step
|
---|
| 443 | //-- and before the bottom-up merge step
|
---|
[570] | 444 |
|
---|
[660] | 445 | EvaluateViewCellsStats();
|
---|
[1381] | 446 | Debug << "\noriginal view cell partition before post process:\n"
|
---|
| 447 | << mCurrentViewCellsStats << endl;
|
---|
[660] | 448 |
|
---|
| 449 | mRenderer->RenderScene();
|
---|
| 450 | SimulationStatistics ss;
|
---|
| 451 | dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
| 452 |
|
---|
| 453 | Debug << ss << endl;
|
---|
| 454 |
|
---|
| 455 |
|
---|
[1381] | 456 |
|
---|
| 457 | ///////////////////////////////////////////////
|
---|
| 458 | //
|
---|
| 459 | //-- post processing of the initial construction
|
---|
| 460 | //-- We can bottom-up merge the view cells in this step
|
---|
| 461 |
|
---|
| 462 |
|
---|
| 463 | // we can additionally cast some post processing sample rays.
|
---|
| 464 | // These rays can be used to store the view cells with the rays
|
---|
| 465 | VssRayContainer postProcessSamples;
|
---|
| 466 | cout << "casting " << mPostProcessSamples << " post processing samples ... ";
|
---|
| 467 |
|
---|
| 468 | CastPassSamples(mPostProcessSamples, mSamplingType, postProcessSamples);
|
---|
| 469 |
|
---|
| 470 | cout << "finished" << endl;
|
---|
[574] | 471 | cout << "starting post processing and visualization" << endl;
|
---|
| 472 |
|
---|
[1381] | 473 | // store view cells for post processing?
|
---|
[574] | 474 | const bool storeViewCells = true;
|
---|
[605] | 475 |
|
---|
| 476 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 477 | ComputeSampleContributions(postProcessSamples, true, storeViewCells);
|
---|
[663] | 478 |
|
---|
[1381] | 479 | PostProcess(preprocessor->mObjects, postProcessSamples);
|
---|
[570] | 480 |
|
---|
[955] | 481 | cout << "time needed for post processing (merge) step: "
|
---|
[752] | 482 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
| 483 |
|
---|
[955] | 484 | Debug << "time needed for post processing (merge) step: "
|
---|
[752] | 485 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
| 486 |
|
---|
[1002] | 487 | disposeRays(postProcessSamples, outRays);
|
---|
| 488 |
|
---|
[801] | 489 |
|
---|
[1381] | 490 | /////////////////////////////////////////////////////////////////////
|
---|
| 491 | //-- Evaluation of the resulting view cell partition.
|
---|
| 492 | //-- We cast a number of new samples and measure the render cost
|
---|
| 493 |
|
---|
[801] | 494 | if (mEvaluateViewCells)
|
---|
| 495 | {
|
---|
[1145] | 496 | EvalViewCellPartition();
|
---|
[801] | 497 | }
|
---|
[1002] | 498 |
|
---|
[1381] | 499 |
|
---|
| 500 | ///////////////////////////////////
|
---|
| 501 | //-- Finally, we do some visualization
|
---|
| 502 |
|
---|
[666] | 503 | if (mShowVisualization)
|
---|
| 504 | {
|
---|
| 505 | VssRayContainer visualizationSamples;
|
---|
[574] | 506 |
|
---|
[1381] | 507 | //-- visualization rays, e.g., to show some samples in the scene
|
---|
[666] | 508 | CastPassSamples(mVisualizationSamples,
|
---|
| 509 | Preprocessor::DIRECTION_BASED_DISTRIBUTION,
|
---|
| 510 | visualizationSamples);
|
---|
[574] | 511 |
|
---|
[666] | 512 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 513 | ComputeSampleContributions(visualizationSamples, true, storeViewCells);
|
---|
[574] | 514 |
|
---|
[666] | 515 | // different visualizations
|
---|
| 516 | Visualize(preprocessor->mObjects, visualizationSamples);
|
---|
[574] | 517 |
|
---|
[666] | 518 | disposeRays(visualizationSamples, outRays);
|
---|
| 519 | }
|
---|
[574] | 520 |
|
---|
[573] | 521 | return numSamples;
|
---|
[527] | 522 | }
|
---|
| 523 |
|
---|
[547] | 524 |
|
---|
[752] | 525 | void ViewCellsManager::EvalViewCellHistogram(const string filename,
|
---|
| 526 | const int nViewCells)
|
---|
[728] | 527 | {
|
---|
| 528 | std::ofstream outstream;
|
---|
| 529 | outstream.open(filename.c_str());
|
---|
| 530 |
|
---|
| 531 | ViewCellContainer viewCells;
|
---|
[735] | 532 | mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
|
---|
[728] | 533 |
|
---|
| 534 | float maxRenderCost, minRenderCost;
|
---|
[734] | 535 |
|
---|
| 536 | // sort by render cost
|
---|
| 537 | sort(viewCells.begin(), viewCells.end(), ViewCell::SmallerRenderCost);
|
---|
| 538 |
|
---|
| 539 | minRenderCost = viewCells.front()->GetRenderCost();
|
---|
| 540 | maxRenderCost = viewCells.back()->GetRenderCost();
|
---|
| 541 |
|
---|
[991] | 542 | Debug << "histogram min rc: " << minRenderCost << " max rc: " << maxRenderCost << endl;
|
---|
[736] | 543 |
|
---|
[997] | 544 | int histoIntervals;
|
---|
[1004] | 545 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
|
---|
[997] | 546 | const int intervals = min(histoIntervals, (int)viewCells.size());
|
---|
[735] | 547 |
|
---|
[997] | 548 | int histoMaxVal;
|
---|
[1004] | 549 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
|
---|
[1145] | 550 | maxRenderCost = max((float)histoMaxVal, maxRenderCost);
|
---|
[991] | 551 |
|
---|
[997] | 552 |
|
---|
[728] | 553 | const float range = maxRenderCost - minRenderCost;
|
---|
[735] | 554 | const float stepSize = range / (float)intervals;
|
---|
[728] | 555 |
|
---|
[735] | 556 | float currentRenderCost = minRenderCost;//(int)ceil(minRenderCost);
|
---|
| 557 |
|
---|
| 558 | const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
|
---|
| 559 | const float totalVol = GetViewSpaceBox().GetVolume();
|
---|
[736] | 560 | //const float totalVol = mViewCellsTree->GetRoot()->GetVolume();
|
---|
[735] | 561 |
|
---|
[736] | 562 | int j = 0;
|
---|
[735] | 563 | int i = 0;
|
---|
| 564 |
|
---|
[734] | 565 | ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
| 566 |
|
---|
[744] | 567 | // count for integral
|
---|
| 568 | float volSum = 0;
|
---|
| 569 | int smallerCostSum = 0;
|
---|
[735] | 570 |
|
---|
[734] | 571 | // note can skip computations for view cells already evaluated and delete them from vector ...
|
---|
| 572 | while (1)
|
---|
| 573 | {
|
---|
[744] | 574 | // count for histogram value
|
---|
| 575 | float volDif = 0;
|
---|
| 576 | int smallerCostDif = 0;
|
---|
[736] | 577 |
|
---|
[734] | 578 | while ((i < (int)viewCells.size()) && (viewCells[i]->GetRenderCost() < currentRenderCost))
|
---|
| 579 | {
|
---|
[744] | 580 | volSum += viewCells[i]->GetVolume();
|
---|
| 581 | volDif += viewCells[i]->GetVolume();
|
---|
| 582 |
|
---|
[734] | 583 | ++ i;
|
---|
[744] | 584 | ++ smallerCostSum;
|
---|
| 585 | ++ smallerCostDif;
|
---|
[734] | 586 | }
|
---|
[736] | 587 |
|
---|
[744] | 588 | if ((i >= (int)viewCells.size()) || (currentRenderCost >= maxRenderCost))
|
---|
[735] | 589 | break;
|
---|
[736] | 590 |
|
---|
[744] | 591 | const float rcRatio = currentRenderCost / maxRenderCost;
|
---|
| 592 | const float volRatioSum = volSum / totalVol;
|
---|
| 593 | const float volRatioDif = volDif / totalVol;
|
---|
[735] | 594 |
|
---|
[991] | 595 | outstream << "#Pass\n" << j ++ << endl;
|
---|
[736] | 596 | outstream << "#RenderCostRatio\n" << rcRatio << endl;
|
---|
| 597 | outstream << "#WeightedCost\n" << currentRenderCost / totalVol << endl;
|
---|
[744] | 598 | outstream << "#ViewCellsDif\n" << smallerCostDif << endl;
|
---|
| 599 | outstream << "#ViewCellsSum\n" << smallerCostSum << endl;
|
---|
| 600 | outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
|
---|
| 601 | outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
|
---|
[735] | 602 |
|
---|
| 603 | // increase current render cost
|
---|
[734] | 604 | currentRenderCost += stepSize;
|
---|
| 605 | }
|
---|
[735] | 606 |
|
---|
[734] | 607 | outstream.close();
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[735] | 610 |
|
---|
[979] | 611 |
|
---|
| 612 | ViewCellsManager *ViewCellsManager::LoadViewCells(const string &filename,
|
---|
| 613 | ObjectContainer *objects,
|
---|
| 614 | const bool finalizeViewCells,
|
---|
| 615 | BoundingBoxConverter *bconverter)
|
---|
| 616 |
|
---|
| 617 | {
|
---|
| 618 | ViewCellsParser parser;
|
---|
| 619 |
|
---|
| 620 | ViewCellsManager *vm = NULL;
|
---|
| 621 |
|
---|
[1221] | 622 | if (parser.ParseViewCellsFile(filename, &vm, objects, bconverter))
|
---|
[979] | 623 | {
|
---|
| 624 | long startTime = GetTime();
|
---|
| 625 |
|
---|
| 626 | //vm->PrepareLoadedViewCells();
|
---|
| 627 | vm->ResetViewCells();
|
---|
| 628 |
|
---|
| 629 | vm->mViewCellsFinished = true;
|
---|
| 630 | vm->mMaxPvsSize = (int)objects->size();
|
---|
| 631 |
|
---|
| 632 | // create the meshes and compute volumes
|
---|
| 633 | if (finalizeViewCells)
|
---|
| 634 | {
|
---|
| 635 | vm->FinalizeViewCells(true);
|
---|
| 636 | vm->mViewCellsTree->AssignRandomColors();
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | Debug << (int)vm->mViewCells.size() << " view cells loaded in "
|
---|
| 640 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
| 641 | }
|
---|
| 642 | else
|
---|
| 643 | {
|
---|
| 644 | Debug << "Error: loading view cells failed!" << endl;
|
---|
| 645 | DEL_PTR(vm);
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | return vm;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 |
|
---|
| 652 | bool VspBspViewCellsManager::ExportViewCells(const string filename,
|
---|
| 653 | const bool exportPvs,
|
---|
| 654 | const ObjectContainer &objects)
|
---|
| 655 | {
|
---|
[1002] | 656 | if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
|
---|
[997] | 657 | return false;
|
---|
| 658 |
|
---|
[979] | 659 | cout << "exporting view cells to xml ... ";
|
---|
[1264] | 660 |
|
---|
[1201] | 661 | OUT_STREAM stream(filename.c_str());
|
---|
[979] | 662 |
|
---|
| 663 | // for output we need unique ids for each view cell
|
---|
| 664 | CreateUniqueViewCellIds();
|
---|
| 665 |
|
---|
| 666 | stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
| 667 | stream << "<VisibilitySolution>" << endl;
|
---|
| 668 |
|
---|
[1262] | 669 | if (exportPvs)
|
---|
| 670 | {
|
---|
| 671 | //-- export bounding boxes
|
---|
[1264] | 672 |
|
---|
[1262] | 673 | stream << "<BoundingBoxes>" << endl;
|
---|
[979] | 674 |
|
---|
[1262] | 675 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
[979] | 676 |
|
---|
[1262] | 677 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 678 | {
|
---|
[979] | 679 | MeshInstance *mi = dynamic_cast<MeshInstance *>(*oit);
|
---|
| 680 | const AxisAlignedBox3 box = mi->GetBox();
|
---|
| 681 |
|
---|
| 682 | //-- the bounding boxes
|
---|
| 683 | stream << "<BoundingBox" << " id=\"" << mi->GetId() << "\""
|
---|
| 684 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
| 685 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
[1262] | 686 | }
|
---|
[979] | 687 |
|
---|
[1262] | 688 | stream << "</BoundingBoxes>" << endl;
|
---|
[1112] | 689 | }
|
---|
[979] | 690 |
|
---|
[1264] | 691 |
|
---|
[979] | 692 | //-- export the view cells and the pvs
|
---|
| 693 |
|
---|
| 694 | const int numViewCells = mCurrentViewCellsStats.viewCells;
|
---|
| 695 |
|
---|
| 696 | stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
|
---|
| 697 |
|
---|
| 698 | mViewCellsTree->Export(stream, exportPvs);
|
---|
| 699 |
|
---|
| 700 | stream << "</ViewCells>" << endl;
|
---|
| 701 |
|
---|
| 702 |
|
---|
[1262] | 703 | //-- export the view space hierarchy
|
---|
[979] | 704 |
|
---|
[1262] | 705 | stream << "<ViewSpaceHierarchy type=\"bsp\""
|
---|
| 706 | << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
|
---|
[1264] | 707 | << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
|
---|
[1262] | 708 |
|
---|
[979] | 709 | mVspBspTree->Export(stream);
|
---|
[1286] | 710 | stream << "</ViewSpaceHierarchy>" << endl;
|
---|
[979] | 711 |
|
---|
| 712 | stream << "</VisibilitySolution>" << endl;
|
---|
| 713 |
|
---|
| 714 | stream.close();
|
---|
| 715 | cout << "finished" << endl;
|
---|
| 716 |
|
---|
| 717 | return true;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 |
|
---|
[735] | 721 | void ViewCellsManager::EvalViewCellHistogramForPvsSize(const string filename,
|
---|
| 722 | const int nViewCells)
|
---|
[734] | 723 | {
|
---|
| 724 | std::ofstream outstream;
|
---|
| 725 | outstream.open(filename.c_str());
|
---|
| 726 |
|
---|
| 727 | ViewCellContainer viewCells;
|
---|
[735] | 728 | mViewCellsTree->CollectBestViewCellSet(viewCells, nViewCells);
|
---|
[734] | 729 |
|
---|
[997] | 730 | int maxPvs, maxVal, minVal;
|
---|
[734] | 731 |
|
---|
[997] | 732 | // sort by pvs size
|
---|
[735] | 733 | sort(viewCells.begin(), viewCells.end(), ViewCell::SmallerPvs);
|
---|
| 734 |
|
---|
[1142] | 735 | maxPvs = mViewCellsTree->GetPvsSize(viewCells.back());
|
---|
[997] | 736 | minVal = 0;
|
---|
| 737 |
|
---|
[991] | 738 | // hack: normalize pvs size
|
---|
[997] | 739 | int histoMaxVal;
|
---|
[1004] | 740 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.maxValue", histoMaxVal);
|
---|
[997] | 741 | maxVal = max(histoMaxVal, maxPvs);
|
---|
| 742 |
|
---|
[1121] | 743 | Debug << "histogram minpvssize: " << minVal << " maxpvssize: " << maxVal
|
---|
| 744 | << " real maxpvs " << maxPvs << endl;
|
---|
[752] | 745 |
|
---|
[997] | 746 | int histoIntervals;
|
---|
[1004] | 747 | Environment::GetSingleton()->GetIntValue("Preprocessor.histogram.intervals", histoIntervals);
|
---|
[997] | 748 | const int intervals = min(histoIntervals, (int)viewCells.size());
|
---|
[735] | 749 |
|
---|
[997] | 750 | const int range = maxVal - minVal;
|
---|
[735] | 751 | int stepSize = range / intervals;
|
---|
| 752 |
|
---|
[997] | 753 | // set step size to avoid endless loop
|
---|
[735] | 754 | if (!stepSize) stepSize = 1;
|
---|
[979] | 755 |
|
---|
| 756 | Debug << "stepsize: " << stepSize << endl;
|
---|
[997] | 757 |
|
---|
[735] | 758 |
|
---|
| 759 | const float totalRenderCost = mViewCellsTree->GetRoot()->GetRenderCost();
|
---|
| 760 | const float totalVol = GetViewSpaceBox().GetVolume();
|
---|
| 761 |
|
---|
[997] | 762 | int currentPvs = minVal;//(int)ceil(minRenderCost);
|
---|
[735] | 763 |
|
---|
[997] | 764 |
|
---|
[735] | 765 | int i = 0;
|
---|
| 766 | int j = 0;
|
---|
[734] | 767 |
|
---|
[744] | 768 | float volSum = 0;
|
---|
| 769 | int smallerSum = 0;
|
---|
| 770 |
|
---|
[735] | 771 | ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
| 772 |
|
---|
[997] | 773 | for (int j = 0; j < intervals; ++ j)
|
---|
[728] | 774 | {
|
---|
[744] | 775 | float volDif = 0;
|
---|
| 776 | int smallerDif = 0;
|
---|
[736] | 777 |
|
---|
[997] | 778 | while ((i < (int)viewCells.size()) &&
|
---|
[1142] | 779 | (mViewCellsTree->GetPvsSize(viewCells[i]) < currentPvs))
|
---|
[735] | 780 | {
|
---|
[744] | 781 | volDif += viewCells[i]->GetVolume();
|
---|
| 782 | volSum += viewCells[i]->GetVolume();
|
---|
[997] | 783 |
|
---|
[735] | 784 | ++ i;
|
---|
[744] | 785 | ++ smallerDif;
|
---|
| 786 | ++ smallerSum;
|
---|
[735] | 787 | }
|
---|
[736] | 788 |
|
---|
[997] | 789 | if (0 && (i < (int)viewCells.size()))
|
---|
[1142] | 790 | Debug << "new pvs size increase: " << mViewCellsTree->GetPvsSize(viewCells[i])
|
---|
[1121] | 791 | << " " << currentPvs << endl;
|
---|
[997] | 792 |
|
---|
[744] | 793 | const float volRatioDif = volDif / totalVol;
|
---|
| 794 | const float volRatioSum = volSum / totalVol;
|
---|
[728] | 795 |
|
---|
[997] | 796 | outstream << "#Pass\n" << j << endl;
|
---|
| 797 | outstream << "#Pvs\n" << currentPvs << endl;
|
---|
[744] | 798 | outstream << "#ViewCellsDif\n" << smallerDif << endl;
|
---|
| 799 | outstream << "#ViewCellsSum\n" << smallerSum << endl;
|
---|
| 800 | outstream << "#VolumeDif\n" << volRatioDif << endl << endl;
|
---|
| 801 | outstream << "#VolumeSum\n" << volRatioSum << endl << endl;
|
---|
[997] | 802 |
|
---|
| 803 | //if ((i >= (int)viewCells.size()) || (currentPvs >= maxPvs)) break;
|
---|
[729] | 804 |
|
---|
[997] | 805 | //-- increase current pvs size to define next interval
|
---|
| 806 | currentPvs += stepSize;
|
---|
[728] | 807 | }
|
---|
[991] | 808 |
|
---|
[735] | 809 | outstream.close();
|
---|
[728] | 810 | }
|
---|
| 811 |
|
---|
[735] | 812 |
|
---|
[1002] | 813 | bool ViewCellsManager::GetExportPvs() const
|
---|
| 814 | {
|
---|
| 815 | return mExportPvs;
|
---|
| 816 | }
|
---|
| 817 |
|
---|
| 818 |
|
---|
[1259] | 819 | bool ViewCellsManager::AddSampleToPvs(Intersectable *obj,
|
---|
| 820 | const Vector3 &hitPoint,
|
---|
| 821 | ViewCell *vc,
|
---|
| 822 | const float pdf,
|
---|
| 823 | float &contribution) const
|
---|
| 824 | {
|
---|
| 825 | if (!obj) return false;
|
---|
| 826 |
|
---|
| 827 | // potentially visible objects
|
---|
| 828 | return vc->AddPvsSample(obj, pdf, contribution);
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 |
|
---|
[1145] | 832 | void ViewCellsManager::EvalViewCellPartition()
|
---|
[660] | 833 | {
|
---|
| 834 | int samplesPerPass;
|
---|
| 835 | int numSamples;
|
---|
| 836 | int castSamples = 0;
|
---|
| 837 |
|
---|
[664] | 838 | char s[64];
|
---|
| 839 |
|
---|
[1004] | 840 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
|
---|
| 841 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samples", numSamples);
|
---|
[660] | 842 |
|
---|
[664] | 843 | char statsPrefix[100];
|
---|
[1004] | 844 | Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
|
---|
[663] | 845 |
|
---|
[664] | 846 | Debug << "view cell evaluation samples per pass: " << samplesPerPass << endl;
|
---|
| 847 | Debug << "view cell evaluation samples: " << numSamples << endl;
|
---|
| 848 | Debug << "view cell stats prefix: " << statsPrefix << endl;
|
---|
| 849 |
|
---|
[666] | 850 | // should directional sampling be used?
|
---|
[722] | 851 | bool dirSamples = (mEvaluationSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
|
---|
[664] | 852 |
|
---|
[801] | 853 | cout << "reseting pvs ... ";
|
---|
| 854 |
|
---|
[1145] | 855 | const bool startFromZero = true;
|
---|
[801] | 856 |
|
---|
[666] | 857 | // reset pvs and start over from zero
|
---|
| 858 | if (startFromZero)
|
---|
| 859 | {
|
---|
| 860 | mViewCellsTree->ResetPvs();
|
---|
| 861 | }
|
---|
[1145] | 862 | else // start from current sampless
|
---|
[666] | 863 | {
|
---|
[1145] | 864 | // statistics before casting more samples
|
---|
[666] | 865 | cout << "compute new statistics ... ";
|
---|
| 866 | sprintf(s, "-%09d-eval.log", castSamples);
|
---|
| 867 | string fName = string(statsPrefix) + string(s);
|
---|
| 868 |
|
---|
| 869 | mViewCellsTree->ExportStats(fName);
|
---|
| 870 | cout << "finished" << endl;
|
---|
| 871 | }
|
---|
[744] | 872 | cout << "finished" << endl;
|
---|
| 873 |
|
---|
[1178] | 874 | cout << "Evaluating view cell partition ... " << endl;
|
---|
[744] | 875 |
|
---|
[660] | 876 | while (castSamples < numSamples)
|
---|
| 877 | {
|
---|
[664] | 878 | VssRayContainer evaluationSamples;
|
---|
[663] | 879 |
|
---|
[722] | 880 | const int samplingType = mEvaluationSamplingType;
|
---|
[666] | 881 | /* dirSamples ?
|
---|
[664] | 882 | Preprocessor::DIRECTION_BASED_DISTRIBUTION :
|
---|
| 883 | Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
[666] | 884 | */
|
---|
[1004] | 885 | long startTime = GetTime();
|
---|
| 886 |
|
---|
[664] | 887 | //-- construction rays => we use uniform samples for this
|
---|
[1278] | 888 |
|
---|
[1004] | 889 | cout << "casting " << samplesPerPass << " samples ... ";
|
---|
| 890 | Debug << "casting " << samplesPerPass << " samples ... ";
|
---|
| 891 |
|
---|
[664] | 892 | CastPassSamples(samplesPerPass, samplingType, evaluationSamples);
|
---|
| 893 |
|
---|
| 894 | castSamples += samplesPerPass;
|
---|
| 895 |
|
---|
[1004] | 896 | Real timeDiff = TimeDiff(startTime, GetTime());
|
---|
| 897 | Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
| 898 | cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
[664] | 899 |
|
---|
[1004] | 900 | cout << "computing sample contributions of " << (int)evaluationSamples.size() << " samples ... ";
|
---|
| 901 | Debug << "computing sample contributions of " << (int)evaluationSamples.size() << " samples ... ";
|
---|
| 902 |
|
---|
| 903 | startTime = GetTime();
|
---|
| 904 |
|
---|
[664] | 905 | ComputeSampleContributions(evaluationSamples, true, false);
|
---|
| 906 |
|
---|
[1004] | 907 | timeDiff = TimeDiff(startTime, GetTime());
|
---|
| 908 | cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
| 909 | Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
[664] | 910 |
|
---|
[1004] | 911 | startTime = GetTime();
|
---|
[801] | 912 |
|
---|
[1004] | 913 | cout << "compute new statistics ... ";
|
---|
| 914 | Debug << "compute new statistics ... ";
|
---|
| 915 |
|
---|
[801] | 916 | //-- propagate pvs or pvs size information
|
---|
[752] | 917 | ObjectPvs pvs;
|
---|
| 918 | UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), pvs);
|
---|
[664] | 919 |
|
---|
[801] | 920 | //-- output stats
|
---|
[667] | 921 | sprintf(s, "-%09d-eval.log", castSamples);
|
---|
[664] | 922 | string fileName = string(statsPrefix) + string(s);
|
---|
| 923 |
|
---|
| 924 | mViewCellsTree->ExportStats(fileName);
|
---|
| 925 |
|
---|
[1004] | 926 | timeDiff = TimeDiff(startTime, GetTime());
|
---|
| 927 | cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
| 928 | Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
|
---|
[664] | 929 |
|
---|
| 930 | disposeRays(evaluationSamples, NULL);
|
---|
[660] | 931 | }
|
---|
[728] | 932 |
|
---|
[997] | 933 |
|
---|
| 934 | //-- histogram
|
---|
| 935 |
|
---|
[735] | 936 | bool useHisto;
|
---|
[997] | 937 | int histoStepSize;
|
---|
[693] | 938 |
|
---|
[1004] | 939 | Environment::GetSingleton()->GetBoolValue("ViewCells.Evaluation.histogram", useHisto);
|
---|
| 940 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.histoStepSize", histoStepSize);
|
---|
[728] | 941 |
|
---|
[744] | 942 | const int numLeaves = mViewCellsTree->GetNumInitialViewCells(mViewCellsTree->GetRoot());
|
---|
[736] | 943 |
|
---|
| 944 |
|
---|
[735] | 945 | if (useHisto)
|
---|
[734] | 946 | {
|
---|
[735] | 947 | // evaluate view cells in a histogram
|
---|
| 948 | char s[64];
|
---|
| 949 |
|
---|
[997] | 950 | for (int pass = histoStepSize; pass <= numLeaves; pass += histoStepSize)
|
---|
[744] | 951 | {
|
---|
[997] | 952 | string filename;
|
---|
[744] | 953 |
|
---|
[997] | 954 | cout << "computing histogram for " << pass << " view cells" << endl;
|
---|
| 955 | #if 0
|
---|
[744] | 956 | //-- evaluate histogram for render cost
|
---|
| 957 | sprintf(s, "-%09d-histo.log", pass);
|
---|
[997] | 958 | filename = string(statsPrefix) + string(s);
|
---|
[744] | 959 |
|
---|
| 960 | EvalViewCellHistogram(filename, pass);
|
---|
| 961 |
|
---|
[997] | 962 | #endif
|
---|
[744] | 963 | //////////////////////////////////////////
|
---|
[752] | 964 | //-- evaluate histogram for pvs size
|
---|
[744] | 965 |
|
---|
| 966 | cout << "computing pvs histogram for " << pass << " view cells" << endl;
|
---|
| 967 |
|
---|
| 968 | sprintf(s, "-%09d-histo-pvs.log", pass);
|
---|
| 969 | filename = string(statsPrefix) + string(s);
|
---|
| 970 |
|
---|
| 971 | EvalViewCellHistogramForPvsSize(filename, pass);
|
---|
| 972 | }
|
---|
[734] | 973 | }
|
---|
[660] | 974 | }
|
---|
| 975 |
|
---|
[704] | 976 |
|
---|
| 977 | inline float EvalMergeCost(ViewCell *root, ViewCell *candidate)
|
---|
[703] | 978 | {
|
---|
[704] | 979 | return root->GetPvs().GetPvsHomogenity(candidate->GetPvs());
|
---|
| 980 | }
|
---|
[660] | 981 |
|
---|
[703] | 982 |
|
---|
[1145] | 983 | /// Returns index of the best view cells of the neighborhood
|
---|
[704] | 984 | int GetBestViewCellIdx(ViewCell *root, const ViewCellContainer &neighborhood)
|
---|
| 985 | {
|
---|
[705] | 986 | int bestViewCellIdx = 0;
|
---|
[703] | 987 |
|
---|
[704] | 988 | float mergeCost = Limits::Infinity;
|
---|
| 989 | int i = 0;
|
---|
[703] | 990 |
|
---|
[734] | 991 | ViewCellContainer::const_iterator vit, vit_end = neighborhood.end();
|
---|
[703] | 992 |
|
---|
[734] | 993 | for (vit = neighborhood.begin(); vit != vit_end; ++ vit, ++ i)
|
---|
[703] | 994 | {
|
---|
[734] | 995 | const float mc = EvalMergeCost(root, *vit);
|
---|
[705] | 996 |
|
---|
| 997 | if (mc < mergeCost)
|
---|
[704] | 998 | {
|
---|
| 999 | mergeCost = mc;
|
---|
| 1000 | bestViewCellIdx = i;
|
---|
[703] | 1001 | }
|
---|
[704] | 1002 | }
|
---|
[703] | 1003 |
|
---|
[704] | 1004 | return bestViewCellIdx;
|
---|
| 1005 | }
|
---|
[703] | 1006 |
|
---|
| 1007 |
|
---|
[728] | 1008 | void ViewCellsManager::SetMaxFilterSize(const int size)
|
---|
| 1009 | {
|
---|
| 1010 | mMaxFilterSize = size;
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
| 1013 |
|
---|
| 1014 | float ViewCellsManager::EvalRenderCost(Intersectable *obj) const
|
---|
| 1015 | {
|
---|
| 1016 | switch (mRenderCostEvaluationType)
|
---|
| 1017 | {
|
---|
| 1018 | case PER_OBJECT:
|
---|
[752] | 1019 | //cout << "perobject" << endl;
|
---|
| 1020 | return 1.0f;
|
---|
[728] | 1021 |
|
---|
| 1022 | case PER_TRIANGLE:
|
---|
[1145] | 1023 | {//cout << "pertriangle" << endl;
|
---|
[728] | 1024 | // HACK
|
---|
| 1025 | MeshInstance *mi = dynamic_cast<MeshInstance *>(obj);
|
---|
| 1026 |
|
---|
| 1027 | // HACK: assume meshes are triangles
|
---|
| 1028 | if (mi->GetMesh())
|
---|
| 1029 | {
|
---|
[1145] | 1030 | return (float)mi->GetMesh()->mFaces.size();
|
---|
[728] | 1031 | }
|
---|
| 1032 | }
|
---|
| 1033 | default:
|
---|
[752] | 1034 | cout << "default" << endl;
|
---|
| 1035 | return 1.0f;
|
---|
[728] | 1036 | }
|
---|
| 1037 |
|
---|
| 1038 | // should not come here
|
---|
[752] | 1039 | return 0.0f;
|
---|
[728] | 1040 | }
|
---|
| 1041 |
|
---|
| 1042 |
|
---|
[704] | 1043 | ViewCell *ViewCellsManager::ConstructLocalMergeTree(ViewCell *currentViewCell,
|
---|
| 1044 | const ViewCellContainer &viewCells)
|
---|
| 1045 | {
|
---|
| 1046 | ViewCell *root = currentViewCell;
|
---|
| 1047 | ViewCellContainer neighborhood = viewCells;
|
---|
[703] | 1048 |
|
---|
[704] | 1049 | ViewCellContainer::const_iterator it, it_end = neighborhood.end();
|
---|
[703] | 1050 |
|
---|
[704] | 1051 | const int n = min(mMaxFilterSize, (int)neighborhood.size());
|
---|
| 1052 | //-- use priority queue to merge leaf pairs
|
---|
[703] | 1053 |
|
---|
[705] | 1054 | //cout << "neighborhood: " << neighborhood.size() << endl;
|
---|
[704] | 1055 | //const float maxAvgCost = 350;
|
---|
| 1056 | for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
|
---|
[703] | 1057 | {
|
---|
[704] | 1058 | const int bestViewCellIdx = GetBestViewCellIdx(root, neighborhood);
|
---|
[703] | 1059 |
|
---|
[704] | 1060 | ViewCell *bestViewCell = neighborhood[bestViewCellIdx];
|
---|
[706] | 1061 |
|
---|
[704] | 1062 | // remove from vector
|
---|
| 1063 | swap(neighborhood[bestViewCellIdx], neighborhood.back());
|
---|
| 1064 | neighborhood.pop_back();
|
---|
[705] | 1065 |
|
---|
[713] | 1066 | // cout << "vc idx: " << bestViewCellIdx << endl;
|
---|
[704] | 1067 | if (!bestViewCell || !root)
|
---|
| 1068 | cout << "warning!!" << endl;
|
---|
[705] | 1069 |
|
---|
[704] | 1070 | // create new root of the hierarchy
|
---|
| 1071 | root = MergeViewCells(root, bestViewCell);
|
---|
[703] | 1072 | }
|
---|
| 1073 |
|
---|
[704] | 1074 | return root;
|
---|
[703] | 1075 | }
|
---|
| 1076 |
|
---|
[704] | 1077 |
|
---|
[713] | 1078 | struct SortableViewCellEntry {
|
---|
| 1079 |
|
---|
[1174] | 1080 | SortableViewCellEntry() {}
|
---|
| 1081 | SortableViewCellEntry(const float v, ViewCell *cell):mValue(v), mViewCell(cell) {}
|
---|
[713] | 1082 |
|
---|
[1174] | 1083 | float mValue;
|
---|
| 1084 | ViewCell *mViewCell;
|
---|
| 1085 |
|
---|
| 1086 | friend bool operator<(const SortableViewCellEntry &a, const SortableViewCellEntry &b) {
|
---|
| 1087 | return a.mValue < b.mValue;
|
---|
| 1088 | }
|
---|
[713] | 1089 | };
|
---|
| 1090 |
|
---|
[1174] | 1091 |
|
---|
| 1092 | ViewCell * ViewCellsManager::ConstructLocalMergeTree2(ViewCell *currentViewCell,
|
---|
| 1093 | const ViewCellContainer &viewCells)
|
---|
[713] | 1094 | {
|
---|
| 1095 |
|
---|
| 1096 | vector<SortableViewCellEntry> neighborhood(viewCells.size());
|
---|
| 1097 | int i, j;
|
---|
| 1098 | for (i = 0, j = 0; i < viewCells.size(); i++) {
|
---|
| 1099 | if (viewCells[i] != currentViewCell)
|
---|
| 1100 | neighborhood[j++] = SortableViewCellEntry(
|
---|
| 1101 | EvalMergeCost(currentViewCell, viewCells[i]),
|
---|
| 1102 | viewCells[i]);
|
---|
| 1103 | }
|
---|
| 1104 | neighborhood.resize(j);
|
---|
| 1105 |
|
---|
| 1106 | sort(neighborhood.begin(), neighborhood.end());
|
---|
| 1107 |
|
---|
| 1108 | ViewCell *root = currentViewCell;
|
---|
| 1109 |
|
---|
| 1110 | vector<SortableViewCellEntry>::const_iterator it, it_end = neighborhood.end();
|
---|
| 1111 |
|
---|
| 1112 | const int n = min(mMaxFilterSize, (int)neighborhood.size());
|
---|
| 1113 | //-- use priority queue to merge leaf pairs
|
---|
| 1114 |
|
---|
| 1115 | //cout << "neighborhood: " << neighborhood.size() << endl;
|
---|
| 1116 | for (int nMergedViewCells = 0; nMergedViewCells < n; ++ nMergedViewCells)
|
---|
[728] | 1117 | {
|
---|
[713] | 1118 | ViewCell *bestViewCell = neighborhood[nMergedViewCells].mViewCell;
|
---|
| 1119 | //cout <<nMergedViewCells<<":"<<"homogenity=" <<neighborhood[nMergedViewCells].mValue<<endl;
|
---|
| 1120 | // create new root of the hierarchy
|
---|
| 1121 | root = MergeViewCells(root, bestViewCell);
|
---|
| 1122 | // set negative cost so that this view cell gets deleted
|
---|
| 1123 | root->SetMergeCost(-1.0f);
|
---|
[728] | 1124 | }
|
---|
[713] | 1125 |
|
---|
| 1126 | return root;
|
---|
| 1127 | }
|
---|
| 1128 |
|
---|
| 1129 | void
|
---|
| 1130 | ViewCellsManager::DeleteLocalMergeTree(ViewCell *vc
|
---|
| 1131 | ) const
|
---|
| 1132 | {
|
---|
| 1133 | if (!vc->IsLeaf() && vc->GetMergeCost() < 0.0f) {
|
---|
| 1134 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
| 1135 | ViewCellContainer::const_iterator it, it_end = vci->mChildren.end();
|
---|
| 1136 |
|
---|
| 1137 | for (it = vci->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1138 | DeleteLocalMergeTree(*it);
|
---|
| 1139 | vci->mChildren.clear();
|
---|
| 1140 | delete vci;
|
---|
| 1141 | }
|
---|
| 1142 | }
|
---|
| 1143 |
|
---|
[1021] | 1144 |
|
---|
[564] | 1145 | bool ViewCellsManager::CheckValidity(ViewCell *vc,
|
---|
| 1146 | int minPvsSize,
|
---|
[562] | 1147 | int maxPvsSize) const
|
---|
[547] | 1148 | {
|
---|
[569] | 1149 |
|
---|
[1168] | 1150 | if ((vc->GetPvs().CountObjectsInPvs() > maxPvsSize) ||
|
---|
| 1151 | (vc->GetPvs().CountObjectsInPvs() < minPvsSize))
|
---|
[564] | 1152 | {
|
---|
[710] | 1153 | return false;
|
---|
[561] | 1154 | }
|
---|
[570] | 1155 |
|
---|
[710] | 1156 | return true;
|
---|
[547] | 1157 | }
|
---|
| 1158 |
|
---|
| 1159 |
|
---|
[710] | 1160 | int ViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 1161 | ViewCellContainer &viewCells) const
|
---|
| 1162 | {
|
---|
| 1163 | return 0;
|
---|
| 1164 | };
|
---|
| 1165 |
|
---|
[1021] | 1166 |
|
---|
[697] | 1167 | AxisAlignedBox3 ViewCellsManager::GetFilterBBox(const Vector3 &viewPoint,
|
---|
| 1168 | const float width) const
|
---|
| 1169 | {
|
---|
[746] | 1170 | float w = Magnitude(mViewSpaceBox.Size())*width;
|
---|
| 1171 | Vector3 min = viewPoint - w * 0.5f;
|
---|
| 1172 | Vector3 max = viewPoint + w * 0.5f;
|
---|
| 1173 |
|
---|
| 1174 | return AxisAlignedBox3(min, max);
|
---|
[697] | 1175 | }
|
---|
| 1176 |
|
---|
| 1177 |
|
---|
[904] | 1178 | void ViewCellsManager::GetPrVS(const Vector3 &viewPoint,
|
---|
| 1179 | PrVs &prvs,
|
---|
| 1180 | const float filterWidth)
|
---|
[697] | 1181 | {
|
---|
[1112] | 1182 |
|
---|
[746] | 1183 |
|
---|
[1112] | 1184 |
|
---|
| 1185 |
|
---|
[746] | 1186 | ViewCell *currentViewCell = GetViewCell(viewPoint);
|
---|
[697] | 1187 |
|
---|
[1112] | 1188 | if (mMaxFilterSize < 1) {
|
---|
| 1189 | prvs.mViewCell = currentViewCell;
|
---|
| 1190 | return;
|
---|
| 1191 | }
|
---|
| 1192 |
|
---|
| 1193 | const AxisAlignedBox3 box = GetFilterBBox(viewPoint, filterWidth);
|
---|
| 1194 |
|
---|
[746] | 1195 | if (currentViewCell) {
|
---|
[697] | 1196 | ViewCellContainer viewCells;
|
---|
| 1197 | ComputeBoxIntersections(box, viewCells);
|
---|
[1112] | 1198 |
|
---|
| 1199 |
|
---|
[713] | 1200 | ViewCell *root = ConstructLocalMergeTree2(currentViewCell, viewCells);
|
---|
[704] | 1201 | prvs.mViewCell = root;
|
---|
[1112] | 1202 |
|
---|
[746] | 1203 | } else
|
---|
| 1204 | prvs.mViewCell = NULL;
|
---|
| 1205 | //prvs.mPvs = root->GetPvs();
|
---|
[697] | 1206 | }
|
---|
| 1207 |
|
---|
| 1208 |
|
---|
[581] | 1209 | bool ViewCellsManager::ViewCellsTreeConstructed() const
|
---|
| 1210 | {
|
---|
[1002] | 1211 | return (mViewCellsTree && mViewCellsTree->GetRoot());
|
---|
[581] | 1212 | }
|
---|
| 1213 |
|
---|
| 1214 |
|
---|
[564] | 1215 | void ViewCellsManager::SetValidity(ViewCell *vc,
|
---|
| 1216 | int minPvs,
|
---|
[562] | 1217 | int maxPvs) const
|
---|
| 1218 | {
|
---|
| 1219 | vc->SetValid(CheckValidity(vc, minPvs, maxPvs));
|
---|
| 1220 | }
|
---|
| 1221 |
|
---|
[577] | 1222 |
|
---|
[569] | 1223 | void
|
---|
| 1224 | ViewCellsManager::SetValidity(
|
---|
[570] | 1225 | int minPvsSize,
|
---|
[569] | 1226 | int maxPvsSize) const
|
---|
| 1227 | {
|
---|
[991] | 1228 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
[570] | 1229 |
|
---|
[991] | 1230 |
|
---|
| 1231 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1232 | {
|
---|
| 1233 | SetValidity(*it, minPvsSize, maxPvsSize);
|
---|
| 1234 | }
|
---|
[569] | 1235 | }
|
---|
[562] | 1236 |
|
---|
[569] | 1237 | void
|
---|
| 1238 | ViewCellsManager::SetValidityPercentage(
|
---|
| 1239 | const float minValid,
|
---|
| 1240 | const float maxValid
|
---|
| 1241 | )
|
---|
| 1242 | {
|
---|
[997] | 1243 | sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
|
---|
[570] | 1244 |
|
---|
[1151] | 1245 | int start = (int)(mViewCells.size() * minValid);
|
---|
| 1246 | int end = (int)(mViewCells.size() * maxValid);
|
---|
[570] | 1247 |
|
---|
[1151] | 1248 | for (int i = 0; i < (int)mViewCells.size(); ++ i)
|
---|
[997] | 1249 | {
|
---|
| 1250 | mViewCells[i]->SetValid(i >= start && i <= end);
|
---|
| 1251 | }
|
---|
[569] | 1252 | }
|
---|
| 1253 |
|
---|
[1148] | 1254 |
|
---|
[991] | 1255 | int ViewCellsManager::CountValidViewcells() const
|
---|
[569] | 1256 | {
|
---|
[991] | 1257 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1258 | int valid = 0;
|
---|
| 1259 |
|
---|
| 1260 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1261 | {
|
---|
| 1262 | if ((*it)->GetValid())
|
---|
| 1263 | ++ valid;
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 | return valid;
|
---|
[569] | 1267 | }
|
---|
| 1268 |
|
---|
[577] | 1269 |
|
---|
| 1270 | bool ViewCellsManager::LoadViewCellsGeometry(const string filename)
|
---|
[440] | 1271 | {
|
---|
| 1272 | X3dParser parser;
|
---|
[503] | 1273 |
|
---|
[1004] | 1274 | Environment::GetSingleton()->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
|
---|
[503] | 1275 |
|
---|
[440] | 1276 | bool success = parser.ParseFile(filename, *this);
|
---|
| 1277 | Debug << (int)mViewCells.size() << " view cells loaded" << endl;
|
---|
| 1278 |
|
---|
| 1279 | return success;
|
---|
| 1280 | }
|
---|
| 1281 |
|
---|
[485] | 1282 |
|
---|
[487] | 1283 | bool ViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
|
---|
| 1284 | {
|
---|
[542] | 1285 | viewPoint = mViewSpaceBox.GetRandomPoint();
|
---|
[487] | 1286 | return true;
|
---|
| 1287 | }
|
---|
| 1288 |
|
---|
| 1289 |
|
---|
[557] | 1290 | float ViewCellsManager::GetViewSpaceVolume()
|
---|
| 1291 | {
|
---|
[610] | 1292 | return mViewSpaceBox.GetVolume() * (2.0f * sqr((float)M_PI));
|
---|
[557] | 1293 | }
|
---|
| 1294 |
|
---|
| 1295 |
|
---|
[490] | 1296 | bool ViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
|
---|
| 1297 | {
|
---|
[569] | 1298 | if (!ViewCellsConstructed())
|
---|
[837] | 1299 | return mViewSpaceBox.IsInside(viewPoint);
|
---|
| 1300 | else
|
---|
| 1301 | {
|
---|
| 1302 | if (!mViewSpaceBox.IsInside(viewPoint))
|
---|
| 1303 | return false;
|
---|
| 1304 |
|
---|
| 1305 | ViewCell *viewcell = GetViewCell(viewPoint);
|
---|
| 1306 |
|
---|
| 1307 | if (!viewcell || !viewcell->GetValid())
|
---|
| 1308 | return false;
|
---|
[569] | 1309 | }
|
---|
[837] | 1310 |
|
---|
[569] | 1311 | return true;
|
---|
[490] | 1312 | }
|
---|
| 1313 |
|
---|
[501] | 1314 |
|
---|
[563] | 1315 | float
|
---|
| 1316 | ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays,
|
---|
[1155] | 1317 | const bool addRays,
|
---|
[574] | 1318 | const bool storeViewCells
|
---|
[563] | 1319 | )
|
---|
[440] | 1320 | {
|
---|
[563] | 1321 | // view cells not yet constructed
|
---|
| 1322 | if (!ViewCellsConstructed())
|
---|
| 1323 | return 0.0f;
|
---|
[564] | 1324 |
|
---|
[563] | 1325 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
[503] | 1326 |
|
---|
[563] | 1327 | float sum = 0.0f;
|
---|
[1155] | 1328 |
|
---|
[664] | 1329 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 1330 | {
|
---|
[1159] | 1331 | sum += ComputeSampleContribution(*(*it), addRays, storeViewCells);
|
---|
[563] | 1332 | }
|
---|
[664] | 1333 |
|
---|
[563] | 1334 | return sum;
|
---|
[440] | 1335 | }
|
---|
| 1336 |
|
---|
[485] | 1337 |
|
---|
[479] | 1338 | void ViewCellsManager::EvaluateViewCellsStats()
|
---|
| 1339 | {
|
---|
[660] | 1340 | mCurrentViewCellsStats.Reset();
|
---|
[479] | 1341 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1342 |
|
---|
| 1343 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
[487] | 1344 | {
|
---|
[660] | 1345 | mViewCellsTree->UpdateViewCellsStats(*it, mCurrentViewCellsStats);
|
---|
[487] | 1346 | }
|
---|
[479] | 1347 | }
|
---|
| 1348 |
|
---|
[485] | 1349 |
|
---|
[580] | 1350 | void ViewCellsManager::EvaluateRenderStatistics(float &totalRenderCost,
|
---|
[579] | 1351 | float &expectedRenderCost,
|
---|
[580] | 1352 | float &deviation,
|
---|
| 1353 | float &variance,
|
---|
| 1354 | int &totalPvs,
|
---|
| 1355 | float &avgRenderCost)
|
---|
[579] | 1356 | {
|
---|
| 1357 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1358 |
|
---|
| 1359 | //-- compute expected value
|
---|
| 1360 | totalRenderCost = 0;
|
---|
[580] | 1361 | totalPvs = 0;
|
---|
[579] | 1362 |
|
---|
| 1363 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1364 | {
|
---|
| 1365 | ViewCell *vc = *it;
|
---|
[1168] | 1366 | totalRenderCost += vc->GetPvs().CountObjectsInPvs() * vc->GetVolume();
|
---|
| 1367 | totalPvs += (int)vc->GetPvs().CountObjectsInPvs();
|
---|
[579] | 1368 | }
|
---|
| 1369 |
|
---|
[580] | 1370 | // normalize with view space box
|
---|
| 1371 | totalRenderCost /= mViewSpaceBox.GetVolume();
|
---|
[579] | 1372 | expectedRenderCost = totalRenderCost / (float)mViewCells.size();
|
---|
[580] | 1373 | avgRenderCost = totalPvs / (float)mViewCells.size();
|
---|
[579] | 1374 |
|
---|
| 1375 |
|
---|
[580] | 1376 | //-- compute standard defiation
|
---|
[579] | 1377 | variance = 0;
|
---|
[580] | 1378 | deviation = 0;
|
---|
[579] | 1379 |
|
---|
| 1380 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1381 | {
|
---|
| 1382 | ViewCell *vc = *it;
|
---|
| 1383 |
|
---|
[1168] | 1384 | float renderCost = vc->GetPvs().CountObjectsInPvs() * vc->GetVolume();
|
---|
[580] | 1385 | float dev;
|
---|
[579] | 1386 |
|
---|
[580] | 1387 | if (1)
|
---|
[1168] | 1388 | dev = fabs(avgRenderCost - (float)vc->GetPvs().CountObjectsInPvs());
|
---|
[580] | 1389 | else
|
---|
| 1390 | dev = fabs(expectedRenderCost - renderCost);
|
---|
[579] | 1391 |
|
---|
[580] | 1392 | deviation += dev;
|
---|
| 1393 | variance += dev * dev;
|
---|
[579] | 1394 | }
|
---|
[580] | 1395 |
|
---|
[579] | 1396 | variance /= (float)mViewCells.size();
|
---|
[580] | 1397 | deviation /= (float)mViewCells.size();
|
---|
[579] | 1398 | }
|
---|
| 1399 |
|
---|
| 1400 |
|
---|
| 1401 |
|
---|
[440] | 1402 | void ViewCellsManager::AddViewCell(ViewCell *viewCell)
|
---|
| 1403 | {
|
---|
| 1404 | mViewCells.push_back(viewCell);
|
---|
| 1405 | }
|
---|
| 1406 |
|
---|
[485] | 1407 |
|
---|
[478] | 1408 | float ViewCellsManager::GetArea(ViewCell *viewCell) const
|
---|
| 1409 | {
|
---|
| 1410 | return viewCell->GetArea();
|
---|
| 1411 | }
|
---|
| 1412 |
|
---|
| 1413 |
|
---|
| 1414 | float ViewCellsManager::GetVolume(ViewCell *viewCell) const
|
---|
| 1415 | {
|
---|
| 1416 | return viewCell->GetVolume();
|
---|
| 1417 | }
|
---|
| 1418 |
|
---|
[485] | 1419 |
|
---|
[1002] | 1420 | void ViewCellsManager::DeriveViewCellsFromObjects(const ObjectContainer &objects,
|
---|
| 1421 | ViewCellContainer &viewCells,
|
---|
| 1422 | const int maxViewCells) const
|
---|
[440] | 1423 | {
|
---|
| 1424 | // maximal max viewcells
|
---|
[503] | 1425 | int limit = maxViewCells > 0 ?
|
---|
[440] | 1426 | Min((int)objects.size(), maxViewCells) : (int)objects.size();
|
---|
| 1427 |
|
---|
| 1428 | for (int i = 0; i < limit; ++ i)
|
---|
| 1429 | {
|
---|
| 1430 | Intersectable *object = objects[i];
|
---|
[503] | 1431 |
|
---|
[440] | 1432 | // extract the mesh instances
|
---|
| 1433 | if (object->Type() == Intersectable::MESH_INSTANCE)
|
---|
| 1434 | {
|
---|
| 1435 | MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
|
---|
| 1436 |
|
---|
| 1437 | ViewCell *viewCell = GenerateViewCell(inst->GetMesh());
|
---|
| 1438 | viewCells.push_back(viewCell);
|
---|
| 1439 | }
|
---|
[1001] | 1440 | else if (object->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
|
---|
| 1441 | {
|
---|
| 1442 | TransformedMeshInstance *mi = dynamic_cast<TransformedMeshInstance *>(object);
|
---|
| 1443 |
|
---|
| 1444 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
[1002] | 1445 |
|
---|
[1001] | 1446 | // transform mesh
|
---|
[1002] | 1447 | mi->GetTransformedMesh(*mesh);
|
---|
[1001] | 1448 |
|
---|
| 1449 | // create bb + kd tree
|
---|
| 1450 | mesh->Preprocess();
|
---|
| 1451 |
|
---|
| 1452 | ViewCell *viewCell = GenerateViewCell(mi->GetMesh());
|
---|
| 1453 | viewCells.push_back(viewCell);
|
---|
| 1454 |
|
---|
| 1455 | break;
|
---|
| 1456 | }
|
---|
[440] | 1457 | }
|
---|
| 1458 | }
|
---|
| 1459 |
|
---|
[485] | 1460 |
|
---|
[503] | 1461 | ViewCell *ViewCellsManager::ExtrudeViewCell(const Triangle3 &baseTri,
|
---|
[440] | 1462 | const float height) const
|
---|
| 1463 | {
|
---|
| 1464 | // one mesh per view cell
|
---|
[1001] | 1465 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
[503] | 1466 |
|
---|
[440] | 1467 | //-- construct prism
|
---|
| 1468 |
|
---|
[503] | 1469 | // bottom
|
---|
[440] | 1470 | mesh->mFaces.push_back(new Face(2,1,0));
|
---|
| 1471 | // top
|
---|
| 1472 | mesh->mFaces.push_back(new Face(3,4,5));
|
---|
| 1473 | // sides
|
---|
| 1474 | mesh->mFaces.push_back(new Face(1, 4, 3, 0));
|
---|
| 1475 | mesh->mFaces.push_back(new Face(2, 5, 4, 1));
|
---|
| 1476 | mesh->mFaces.push_back(new Face(3, 5, 2, 0));
|
---|
| 1477 |
|
---|
| 1478 | //--- extrude new vertices for top of prism
|
---|
| 1479 | Vector3 triNorm = baseTri.GetNormal();
|
---|
| 1480 |
|
---|
[503] | 1481 | Triangle3 topTri;
|
---|
[440] | 1482 |
|
---|
| 1483 | // add base vertices and calculate top vertices
|
---|
| 1484 | for (int i = 0; i < 3; ++ i)
|
---|
| 1485 | mesh->mVertices.push_back(baseTri.mVertices[i]);
|
---|
[503] | 1486 |
|
---|
| 1487 | // add top vertices
|
---|
[440] | 1488 | for (int i = 0; i < 3; ++ i)
|
---|
| 1489 | mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
|
---|
[503] | 1490 |
|
---|
[440] | 1491 | mesh->Preprocess();
|
---|
| 1492 |
|
---|
| 1493 | return GenerateViewCell(mesh);
|
---|
| 1494 | }
|
---|
| 1495 |
|
---|
[485] | 1496 |
|
---|
[551] | 1497 | void ViewCellsManager::FinalizeViewCells(const bool createMesh)
|
---|
| 1498 | {
|
---|
| 1499 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
[693] | 1500 |
|
---|
| 1501 | // volume and area of the view cells are recomputed and a view cell mesh is created
|
---|
[551] | 1502 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1503 | {
|
---|
| 1504 | Finalize(*it, createMesh);
|
---|
| 1505 | }
|
---|
[555] | 1506 |
|
---|
| 1507 | mTotalAreaValid = false;
|
---|
[551] | 1508 | }
|
---|
| 1509 |
|
---|
| 1510 |
|
---|
| 1511 | void ViewCellsManager::Finalize(ViewCell *viewCell, const bool createMesh)
|
---|
| 1512 | {
|
---|
| 1513 | // implemented in subclasses
|
---|
| 1514 | }
|
---|
| 1515 |
|
---|
[1001] | 1516 |
|
---|
[1168] | 1517 | /** fast way of merging 2 view cells.
|
---|
| 1518 | */
|
---|
[582] | 1519 | ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCell *left, ViewCell *right) const
|
---|
[440] | 1520 | {
|
---|
[580] | 1521 | // generate parent view cell
|
---|
[752] | 1522 | ViewCellInterior *vc = new ViewCellInterior();
|
---|
[710] | 1523 |
|
---|
[752] | 1524 | vc->GetPvs().Clear();
|
---|
[610] | 1525 | vc->GetPvs() = left->GetPvs();
|
---|
[752] | 1526 |
|
---|
| 1527 | // merge pvs of right cell
|
---|
[610] | 1528 | vc->GetPvs().Merge(right->GetPvs());
|
---|
[440] | 1529 |
|
---|
[710] | 1530 | // set only links to child (not from child to parent, maybe not wished!!)
|
---|
| 1531 | vc->mChildren.push_back(left);
|
---|
| 1532 | vc->mChildren.push_back(right);
|
---|
| 1533 |
|
---|
[752] | 1534 | // update pvs size
|
---|
[1168] | 1535 | UpdateScalarPvsSize(vc, vc->GetPvs().CountObjectsInPvs(), vc->GetPvs().GetSize());
|
---|
[752] | 1536 |
|
---|
[440] | 1537 | return vc;
|
---|
| 1538 | }
|
---|
| 1539 |
|
---|
[485] | 1540 |
|
---|
[582] | 1541 | ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCellContainer &children) const
|
---|
| 1542 | {
|
---|
[752] | 1543 | ViewCellInterior *vc = new ViewCellInterior();
|
---|
[582] | 1544 |
|
---|
| 1545 | ViewCellContainer::const_iterator it, it_end = children.end();
|
---|
| 1546 |
|
---|
| 1547 | for (it = children.begin(); it != it_end; ++ it)
|
---|
| 1548 | {
|
---|
| 1549 | // merge pvs
|
---|
[610] | 1550 | vc->GetPvs().Merge((*it)->GetPvs());
|
---|
[710] | 1551 | vc->mChildren.push_back(*it);
|
---|
[582] | 1552 | }
|
---|
| 1553 |
|
---|
| 1554 | return vc;
|
---|
| 1555 | }
|
---|
| 1556 |
|
---|
| 1557 |
|
---|
[480] | 1558 | void ViewCellsManager::SetRenderer(Renderer *renderer)
|
---|
| 1559 | {
|
---|
| 1560 | mRenderer = renderer;
|
---|
| 1561 | }
|
---|
[441] | 1562 |
|
---|
[485] | 1563 |
|
---|
[580] | 1564 | ViewCellsTree *ViewCellsManager::GetViewCellsTree()
|
---|
[440] | 1565 | {
|
---|
[580] | 1566 | return mViewCellsTree;
|
---|
[440] | 1567 | }
|
---|
| 1568 |
|
---|
| 1569 |
|
---|
| 1570 | void ViewCellsManager::SetVisualizationSamples(const int visSamples)
|
---|
| 1571 | {
|
---|
[444] | 1572 | mVisualizationSamples = visSamples;
|
---|
[440] | 1573 | }
|
---|
| 1574 |
|
---|
[485] | 1575 |
|
---|
[440] | 1576 | void ViewCellsManager::SetConstructionSamples(const int constructionSamples)
|
---|
| 1577 | {
|
---|
| 1578 | mConstructionSamples = constructionSamples;
|
---|
| 1579 | }
|
---|
| 1580 |
|
---|
[485] | 1581 |
|
---|
[574] | 1582 | void ViewCellsManager::SetInitialSamples(const int initialSamples)
|
---|
| 1583 | {
|
---|
| 1584 | mInitialSamples = initialSamples;
|
---|
| 1585 | }
|
---|
| 1586 |
|
---|
| 1587 |
|
---|
[440] | 1588 | void ViewCellsManager::SetPostProcessSamples(const int postProcessSamples)
|
---|
| 1589 | {
|
---|
| 1590 | mPostProcessSamples = postProcessSamples;
|
---|
| 1591 | }
|
---|
| 1592 |
|
---|
[485] | 1593 |
|
---|
[440] | 1594 | int ViewCellsManager::GetVisualizationSamples() const
|
---|
| 1595 | {
|
---|
| 1596 | return mVisualizationSamples;
|
---|
| 1597 | }
|
---|
| 1598 |
|
---|
[485] | 1599 |
|
---|
[440] | 1600 | int ViewCellsManager::GetConstructionSamples() const
|
---|
| 1601 | {
|
---|
| 1602 | return mConstructionSamples;
|
---|
| 1603 | }
|
---|
| 1604 |
|
---|
[485] | 1605 |
|
---|
[440] | 1606 | int ViewCellsManager::GetPostProcessSamples() const
|
---|
| 1607 | {
|
---|
| 1608 | return mPostProcessSamples;
|
---|
| 1609 | }
|
---|
| 1610 |
|
---|
[485] | 1611 |
|
---|
[719] | 1612 | void ViewCellsManager::UpdatePvs()
|
---|
| 1613 | {
|
---|
[720] | 1614 | if (mViewCellPvsIsUpdated || !ViewCellsTreeConstructed())
|
---|
[719] | 1615 | return;
|
---|
| 1616 |
|
---|
| 1617 | mViewCellPvsIsUpdated = true;
|
---|
| 1618 |
|
---|
| 1619 | ViewCellContainer leaves;
|
---|
| 1620 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 1621 |
|
---|
| 1622 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 1623 |
|
---|
| 1624 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 1625 | {
|
---|
| 1626 | mViewCellsTree->PropagatePvs(*it);
|
---|
| 1627 | }
|
---|
| 1628 | }
|
---|
| 1629 |
|
---|
| 1630 |
|
---|
[485] | 1631 | void ViewCellsManager::GetPvsStatistics(PvsStatistics &stat)
|
---|
[467] | 1632 | {
|
---|
[1199] | 1633 | // update pvs of view cells tree if necessary
|
---|
| 1634 | UpdatePvs();
|
---|
| 1635 |
|
---|
| 1636 | ViewCellContainer::const_iterator it = mViewCells.begin();
|
---|
| 1637 |
|
---|
| 1638 | stat.viewcells = 0;
|
---|
| 1639 | stat.minPvs = 100000000;
|
---|
| 1640 | stat.maxPvs = 0;
|
---|
| 1641 | stat.avgPvs = 0.0f;
|
---|
| 1642 |
|
---|
| 1643 | for (; it != mViewCells.end(); ++ it)
|
---|
[719] | 1644 | {
|
---|
[1199] | 1645 | ViewCell *viewcell = *it;
|
---|
[1121] | 1646 |
|
---|
[1199] | 1647 | // bool mCountKdPvs = false;
|
---|
| 1648 | const int pvsSize = mViewCellsTree->GetPvsSize(viewcell);
|
---|
[1121] | 1649 |
|
---|
| 1650 |
|
---|
[1199] | 1651 | if (pvsSize < stat.minPvs)
|
---|
| 1652 | stat.minPvs = pvsSize;
|
---|
| 1653 | if (pvsSize > stat.maxPvs)
|
---|
| 1654 | stat.maxPvs = pvsSize;
|
---|
| 1655 |
|
---|
| 1656 | stat.avgPvs += pvsSize;
|
---|
| 1657 |
|
---|
| 1658 | ++ stat.viewcells;
|
---|
[719] | 1659 | }
|
---|
[1199] | 1660 |
|
---|
| 1661 | if (stat.viewcells)
|
---|
| 1662 | stat.avgPvs/=stat.viewcells;
|
---|
[467] | 1663 | }
|
---|
| 1664 |
|
---|
[485] | 1665 |
|
---|
[480] | 1666 | void ViewCellsManager::PrintPvsStatistics(ostream &s)
|
---|
[467] | 1667 | {
|
---|
| 1668 | s<<"############# Viewcell PVS STAT ##################\n";
|
---|
| 1669 | PvsStatistics pvsStat;
|
---|
| 1670 | GetPvsStatistics(pvsStat);
|
---|
| 1671 | s<<"#AVG_PVS\n"<<pvsStat.avgPvs<<endl;
|
---|
| 1672 | s<<"#MAX_PVS\n"<<pvsStat.maxPvs<<endl;
|
---|
| 1673 | s<<"#MIN_PVS\n"<<pvsStat.minPvs<<endl;
|
---|
| 1674 | }
|
---|
| 1675 |
|
---|
[485] | 1676 |
|
---|
[532] | 1677 | int ViewCellsManager::CastBeam(Beam &beam)
|
---|
| 1678 | {
|
---|
| 1679 | return 0;
|
---|
| 1680 | }
|
---|
| 1681 |
|
---|
[547] | 1682 |
|
---|
[468] | 1683 | ViewCellContainer &ViewCellsManager::GetViewCells()
|
---|
| 1684 | {
|
---|
| 1685 | return mViewCells;
|
---|
| 1686 | }
|
---|
| 1687 |
|
---|
[485] | 1688 |
|
---|
[487] | 1689 | void ViewCellsManager::SetViewSpaceBox(const AxisAlignedBox3 &box)
|
---|
| 1690 | {
|
---|
[542] | 1691 | mViewSpaceBox = box;
|
---|
[1074] | 1692 |
|
---|
[1027] | 1693 | // hack: create clip plane relative to new view space box
|
---|
[660] | 1694 | CreateClipPlane();
|
---|
[1027] | 1695 | // the total area of the view space has changed
|
---|
[577] | 1696 | mTotalAreaValid = false;
|
---|
[487] | 1697 | }
|
---|
| 1698 |
|
---|
| 1699 |
|
---|
[660] | 1700 | void ViewCellsManager::CreateClipPlane()
|
---|
[591] | 1701 | {
|
---|
| 1702 | int axis = 0;
|
---|
[667] | 1703 | float pos;
|
---|
[591] | 1704 |
|
---|
[1004] | 1705 | Environment::GetSingleton()->GetFloatValue("ViewCells.Visualization.clipPlanePos", pos);
|
---|
[591] | 1706 |
|
---|
[667] | 1707 | Vector3 point = mViewSpaceBox.Min() + mViewSpaceBox.Size() * pos;
|
---|
| 1708 |
|
---|
[660] | 1709 | if (mUseClipPlaneForViz)
|
---|
[1004] | 1710 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.clipPlaneAxis", axis);
|
---|
[591] | 1711 |
|
---|
[1047] | 1712 | mClipPlane = AxisAlignedPlane(axis, point[axis]);
|
---|
[591] | 1713 | }
|
---|
| 1714 |
|
---|
| 1715 |
|
---|
[519] | 1716 | AxisAlignedBox3 ViewCellsManager::GetViewSpaceBox() const
|
---|
| 1717 | {
|
---|
[542] | 1718 | return mViewSpaceBox;
|
---|
[519] | 1719 | }
|
---|
| 1720 |
|
---|
| 1721 |
|
---|
[480] | 1722 | void ViewCellsManager::ResetViewCells()
|
---|
| 1723 | {
|
---|
[720] | 1724 | // recollect view cells
|
---|
[480] | 1725 | mViewCells.clear();
|
---|
| 1726 | CollectViewCells();
|
---|
[590] | 1727 |
|
---|
[720] | 1728 |
|
---|
| 1729 | // stats are computed once more
|
---|
[660] | 1730 | mCurrentViewCellsStats.Reset();
|
---|
[480] | 1731 | EvaluateViewCellsStats();
|
---|
[590] | 1732 |
|
---|
[520] | 1733 | // has to be recomputed
|
---|
[480] | 1734 | mTotalAreaValid = false;
|
---|
| 1735 | }
|
---|
| 1736 |
|
---|
[485] | 1737 |
|
---|
[547] | 1738 | int ViewCellsManager::GetMaxPvsSize() const
|
---|
| 1739 | {
|
---|
| 1740 | return mMaxPvsSize;
|
---|
| 1741 | }
|
---|
| 1742 |
|
---|
[590] | 1743 |
|
---|
[563] | 1744 | void
|
---|
| 1745 | ViewCellsManager::AddSampleContributions(const VssRayContainer &rays)
|
---|
| 1746 | {
|
---|
| 1747 | if (!ViewCellsConstructed())
|
---|
| 1748 | return;
|
---|
[564] | 1749 |
|
---|
[563] | 1750 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
[564] | 1751 |
|
---|
[563] | 1752 | for (it = rays.begin(); it != it_end; ++ it) {
|
---|
| 1753 | AddSampleContributions(*(*it));
|
---|
| 1754 | }
|
---|
| 1755 | }
|
---|
[547] | 1756 |
|
---|
[581] | 1757 |
|
---|
[562] | 1758 | int ViewCellsManager::GetMinPvsSize() const
|
---|
| 1759 | {
|
---|
| 1760 | return mMinPvsSize;
|
---|
| 1761 | }
|
---|
| 1762 |
|
---|
| 1763 |
|
---|
| 1764 |
|
---|
[561] | 1765 | float ViewCellsManager::GetMaxPvsRatio() const
|
---|
| 1766 | {
|
---|
| 1767 | return mMaxPvsRatio;
|
---|
| 1768 | }
|
---|
| 1769 |
|
---|
| 1770 |
|
---|
[563] | 1771 | void
|
---|
| 1772 | ViewCellsManager::AddSampleContributions(VssRay &ray)
|
---|
[468] | 1773 | {
|
---|
[563] | 1774 | // assumes viewcells have been stored...
|
---|
| 1775 | ViewCellContainer *viewcells = &ray.mViewCells;
|
---|
| 1776 | ViewCellContainer::const_iterator it;
|
---|
| 1777 | for (it = viewcells->begin(); it != viewcells->end(); ++it) {
|
---|
| 1778 | ViewCell *viewcell = *it;
|
---|
[569] | 1779 | if (viewcell->GetValid()) {
|
---|
| 1780 | // if ray not outside of view space
|
---|
| 1781 | viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
|
---|
| 1782 | }
|
---|
[563] | 1783 | }
|
---|
| 1784 | }
|
---|
[503] | 1785 |
|
---|
[574] | 1786 |
|
---|
[1159] | 1787 | float ViewCellsManager::ComputeSampleContribution(VssRay &ray,
|
---|
| 1788 | const bool addRays,
|
---|
| 1789 | const bool storeViewCells)
|
---|
[563] | 1790 | {
|
---|
[570] | 1791 | ViewCellContainer viewcells;
|
---|
[564] | 1792 |
|
---|
[570] | 1793 | ray.mPvsContribution = 0;
|
---|
| 1794 | ray.mRelativePvsContribution = 0.0f;
|
---|
[485] | 1795 |
|
---|
[570] | 1796 | static Ray hray;
|
---|
[564] | 1797 | hray.Init(ray);
|
---|
| 1798 | //hray.mFlags |= Ray::CULL_BACKFACES;
|
---|
| 1799 | //Ray hray(ray);
|
---|
| 1800 |
|
---|
| 1801 | float tmin = 0, tmax = 1.0;
|
---|
| 1802 |
|
---|
| 1803 | if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
|
---|
[565] | 1804 | return 0;
|
---|
[564] | 1805 |
|
---|
| 1806 | Vector3 origin = hray.Extrap(tmin);
|
---|
| 1807 | Vector3 termination = hray.Extrap(tmax);
|
---|
| 1808 |
|
---|
[1291] | 1809 | ViewCell::NewMail();
|
---|
| 1810 |
|
---|
[1006] | 1811 | // traverse the view space subdivision
|
---|
[564] | 1812 | CastLineSegment(origin, termination, viewcells);
|
---|
[664] | 1813 |
|
---|
[574] | 1814 | if (storeViewCells)
|
---|
[1155] | 1815 | { // copy viewcells memory efficiently
|
---|
[570] | 1816 | ray.mViewCells.reserve(viewcells.size());
|
---|
| 1817 | ray.mViewCells = viewcells;
|
---|
| 1818 | }
|
---|
[564] | 1819 |
|
---|
[570] | 1820 | ViewCellContainer::const_iterator it = viewcells.begin();
|
---|
[564] | 1821 |
|
---|
[664] | 1822 | for (; it != viewcells.end(); ++ it)
|
---|
| 1823 | {
|
---|
| 1824 | ViewCell *viewcell = *it;
|
---|
[694] | 1825 |
|
---|
[708] | 1826 | if (viewcell->GetValid())
|
---|
[664] | 1827 | {
|
---|
| 1828 | // if ray not outside of view space
|
---|
| 1829 | float contribution;
|
---|
[1291] | 1830 |
|
---|
| 1831 | if (ray.mTerminationObject)
|
---|
| 1832 | {
|
---|
| 1833 | cout << "f";
|
---|
| 1834 | if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
|
---|
| 1835 | ray.mPdf,
|
---|
| 1836 | contribution))
|
---|
| 1837 | {
|
---|
| 1838 | ++ ray.mPvsContribution;
|
---|
| 1839 | ray.mRelativePvsContribution += contribution;
|
---|
| 1840 | }
|
---|
[664] | 1841 | }
|
---|
[1291] | 1842 |
|
---|
| 1843 | #if SAMPLE_ORIGIN_OBJECTS
|
---|
| 1844 |
|
---|
[840] | 1845 | // for directional sampling it is important to count only contributions
|
---|
[718] | 1846 | // made in one direction!!!
|
---|
| 1847 | // the other contributions of this sample will be counted for the oposite ray!
|
---|
[1291] | 1848 |
|
---|
[666] | 1849 | if (ray.mOriginObject &&
|
---|
| 1850 | viewcell->GetPvs().GetSampleContribution(ray.mOriginObject,
|
---|
| 1851 | ray.mPdf,
|
---|
| 1852 | contribution))
|
---|
| 1853 | {
|
---|
| 1854 | ++ ray.mPvsContribution;
|
---|
| 1855 | ray.mRelativePvsContribution += contribution;
|
---|
| 1856 | }
|
---|
[718] | 1857 | #endif
|
---|
[664] | 1858 | }
|
---|
[569] | 1859 | }
|
---|
[564] | 1860 |
|
---|
[1155] | 1861 | // if addrays is true, sampled entities are stored in the pvs
|
---|
[664] | 1862 | if (addRays)
|
---|
| 1863 | {
|
---|
| 1864 | for (it = viewcells.begin(); it != viewcells.end(); ++ it)
|
---|
| 1865 | {
|
---|
| 1866 | ViewCell *viewcell = *it;
|
---|
| 1867 |
|
---|
[706] | 1868 | if (viewcell->GetValid())
|
---|
[664] | 1869 | {
|
---|
| 1870 | // if ray not outside of view space
|
---|
[718] | 1871 |
|
---|
[1159] | 1872 | // add new object to the pvs
|
---|
[1155] | 1873 | if (ray.mTerminationObject)
|
---|
| 1874 | {
|
---|
[1159] | 1875 | viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
|
---|
| 1876 | }
|
---|
[1166] | 1877 | #if SAMPLE_ORIGIN_OBJECTS
|
---|
[666] | 1878 | if (ray.mOriginObject)
|
---|
[1159] | 1879 | {
|
---|
[666] | 1880 | viewcell->GetPvs().AddSample(ray.mOriginObject, ray.mPdf);
|
---|
[1159] | 1881 | }
|
---|
[718] | 1882 | #endif
|
---|
[664] | 1883 | }
|
---|
| 1884 | }
|
---|
[563] | 1885 | }
|
---|
[570] | 1886 |
|
---|
| 1887 | return ray.mRelativePvsContribution;
|
---|
[468] | 1888 | }
|
---|
| 1889 |
|
---|
| 1890 |
|
---|
[469] | 1891 | void ViewCellsManager::GetRaySets(const VssRayContainer &sourceRays,
|
---|
[485] | 1892 | const int maxSize,
|
---|
[503] | 1893 | VssRayContainer &usedRays,
|
---|
[485] | 1894 | VssRayContainer *savedRays) const
|
---|
[469] | 1895 | {
|
---|
[485] | 1896 | const int limit = min(maxSize, (int)sourceRays.size());
|
---|
[473] | 1897 | const float prop = (float)limit / ((float)sourceRays.size() + Limits::Small);
|
---|
[469] | 1898 |
|
---|
[485] | 1899 | VssRayContainer::const_iterator it, it_end = sourceRays.end();
|
---|
[469] | 1900 | for (it = sourceRays.begin(); it != it_end; ++ it)
|
---|
| 1901 | {
|
---|
[473] | 1902 | if (Random(1.0f) < prop)
|
---|
[485] | 1903 | usedRays.push_back(*it);
|
---|
| 1904 | else if (savedRays)
|
---|
| 1905 | savedRays->push_back(*it);
|
---|
[469] | 1906 | }
|
---|
| 1907 | }
|
---|
| 1908 |
|
---|
[477] | 1909 |
|
---|
[728] | 1910 | float ViewCellsManager::GetRendercost(ViewCell *viewCell) const
|
---|
[605] | 1911 | {
|
---|
[1145] | 1912 | return (float)mViewCellsTree->GetPvsSize(viewCell);
|
---|
[605] | 1913 | }
|
---|
| 1914 |
|
---|
| 1915 |
|
---|
[477] | 1916 | float ViewCellsManager::GetAccVcArea()
|
---|
[470] | 1917 | {
|
---|
| 1918 | // if already computed
|
---|
| 1919 | if (mTotalAreaValid)
|
---|
[728] | 1920 | {
|
---|
[470] | 1921 | return mTotalArea;
|
---|
[728] | 1922 | }
|
---|
[470] | 1923 |
|
---|
| 1924 | mTotalArea = 0;
|
---|
| 1925 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1926 |
|
---|
| 1927 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
[480] | 1928 | {
|
---|
| 1929 | //Debug << "area: " << GetArea(*it);
|
---|
[470] | 1930 | mTotalArea += GetArea(*it);
|
---|
[480] | 1931 | }
|
---|
| 1932 |
|
---|
[470] | 1933 | mTotalAreaValid = true;
|
---|
| 1934 |
|
---|
| 1935 | return mTotalArea;
|
---|
| 1936 | }
|
---|
| 1937 |
|
---|
[482] | 1938 |
|
---|
[475] | 1939 | void ViewCellsManager::PrintStatistics(ostream &s) const
|
---|
| 1940 | {
|
---|
[660] | 1941 | s << mCurrentViewCellsStats << endl;
|
---|
[475] | 1942 | }
|
---|
| 1943 |
|
---|
[477] | 1944 |
|
---|
[508] | 1945 | void ViewCellsManager::CreateUniqueViewCellIds()
|
---|
[482] | 1946 | {
|
---|
[651] | 1947 | if (ViewCellsTreeConstructed())
|
---|
[1284] | 1948 | {
|
---|
[651] | 1949 | mViewCellsTree->CreateUniqueViewCellsIds();
|
---|
[1284] | 1950 | }
|
---|
| 1951 | else // no view cells tree, handle view cells "myself"
|
---|
| 1952 | {
|
---|
[651] | 1953 | for (int i = 0; i < (int)mViewCells.size(); ++ i)
|
---|
[1284] | 1954 | {
|
---|
[651] | 1955 | mViewCells[i]->SetId(i);
|
---|
[1284] | 1956 | }
|
---|
| 1957 | }
|
---|
[508] | 1958 | }
|
---|
| 1959 |
|
---|
| 1960 |
|
---|
| 1961 | void ViewCellsManager::ExportViewCellsForViz(Exporter *exporter) const
|
---|
| 1962 | {
|
---|
[482] | 1963 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1964 |
|
---|
| 1965 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1966 | {
|
---|
[574] | 1967 | if (!mOnlyValidViewCells || (*it)->GetValid())
|
---|
| 1968 | {
|
---|
[801] | 1969 | ExportColor(exporter, *it);
|
---|
| 1970 |
|
---|
[591] | 1971 | ExportViewCellGeometry(exporter, *it,
|
---|
[660] | 1972 | mUseClipPlaneForViz ? &mClipPlane : NULL);
|
---|
[574] | 1973 | }
|
---|
[482] | 1974 | }
|
---|
| 1975 | }
|
---|
| 1976 |
|
---|
| 1977 |
|
---|
[517] | 1978 | void ViewCellsManager::CreateViewCellMeshes()
|
---|
[503] | 1979 | {
|
---|
| 1980 | // convert to meshes
|
---|
| 1981 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 1982 |
|
---|
| 1983 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 1984 | {
|
---|
[517] | 1985 | if (!(*it)->GetMesh())
|
---|
[1001] | 1986 | {
|
---|
[517] | 1987 | CreateMesh(*it);
|
---|
[1001] | 1988 | }
|
---|
[503] | 1989 | }
|
---|
| 1990 | }
|
---|
| 1991 |
|
---|
[508] | 1992 |
|
---|
[944] | 1993 | bool ViewCellsManager::ExportViewCells(const string filename,
|
---|
| 1994 | const bool exportPvs,
|
---|
| 1995 | const ObjectContainer &objects)
|
---|
[564] | 1996 | {
|
---|
[508] | 1997 | return false;
|
---|
| 1998 | }
|
---|
| 1999 |
|
---|
| 2000 |
|
---|
[610] | 2001 | void ViewCellsManager::CollectViewCells(const int n)
|
---|
[508] | 2002 | {
|
---|
[610] | 2003 | mNumActiveViewCells = n;
|
---|
| 2004 | mViewCells.clear();
|
---|
| 2005 | CollectViewCells();
|
---|
| 2006 | }
|
---|
[508] | 2007 |
|
---|
| 2008 |
|
---|
[660] | 2009 | void ViewCellsManager::SetViewCellsActive()
|
---|
| 2010 | {
|
---|
[882] | 2011 | // collect leaf view cells and set the pointers to the currently
|
---|
| 2012 | // active view cells
|
---|
[660] | 2013 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
[944] | 2014 |
|
---|
[660] | 2015 | for (it = mViewCells.begin(); it != it_end; ++ it)
|
---|
| 2016 | {
|
---|
[881] | 2017 | ViewCellContainer leaves;
|
---|
| 2018 | mViewCellsTree->CollectLeaves(*it, leaves);
|
---|
| 2019 |
|
---|
| 2020 | ViewCellContainer::const_iterator lit, lit_end = leaves.end();
|
---|
| 2021 | for (lit = mViewCells.begin(); lit != lit_end; ++ lit)
|
---|
| 2022 | {
|
---|
| 2023 | dynamic_cast<ViewCellLeaf *>(*lit)->SetActiveViewCell(*it);
|
---|
| 2024 | }
|
---|
[660] | 2025 | }
|
---|
| 2026 | }
|
---|
[508] | 2027 |
|
---|
[840] | 2028 |
|
---|
| 2029 | int ViewCellsManager::GetMaxFilterSize() const
|
---|
| 2030 | {
|
---|
| 2031 | return mMaxFilterSize;
|
---|
| 2032 | }
|
---|
| 2033 |
|
---|
[1001] | 2034 |
|
---|
[840] | 2035 | static const bool USE_ASCII = true;
|
---|
| 2036 |
|
---|
[1001] | 2037 |
|
---|
[840] | 2038 | bool ViewCellsManager::ExportBoundingBoxes(const string filename,
|
---|
| 2039 | const ObjectContainer &objects) const
|
---|
| 2040 | {
|
---|
| 2041 | ObjectContainer::const_iterator it, it_end = objects.end();
|
---|
| 2042 |
|
---|
| 2043 | if (USE_ASCII)
|
---|
| 2044 | {
|
---|
| 2045 | ofstream boxesOut(filename.c_str());
|
---|
| 2046 | if (!boxesOut.is_open())
|
---|
| 2047 | return false;
|
---|
| 2048 |
|
---|
| 2049 | for (it = objects.begin(); it != it_end; ++ it)
|
---|
| 2050 | {
|
---|
| 2051 | MeshInstance *mi = dynamic_cast<MeshInstance *>(*it);
|
---|
| 2052 | const AxisAlignedBox3 box = mi->GetBox();
|
---|
| 2053 |
|
---|
| 2054 | boxesOut << mi->GetId() << " "
|
---|
| 2055 | << box.Min().x << " "
|
---|
| 2056 | << box.Min().y << " "
|
---|
| 2057 | << box.Min().z << " "
|
---|
| 2058 | << box.Max().x << " "
|
---|
| 2059 | << box.Max().y << " "
|
---|
| 2060 | << box.Max().z << endl;
|
---|
| 2061 | }
|
---|
| 2062 |
|
---|
| 2063 | boxesOut.close();
|
---|
| 2064 | }
|
---|
| 2065 | else
|
---|
| 2066 | {
|
---|
| 2067 | ofstream boxesOut(filename.c_str(), ios::binary);
|
---|
| 2068 |
|
---|
| 2069 | if (!boxesOut.is_open())
|
---|
| 2070 | return false;
|
---|
| 2071 |
|
---|
| 2072 | for (it = objects.begin(); it != it_end; ++ it)
|
---|
| 2073 | {
|
---|
| 2074 | MeshInstance *mi = dynamic_cast<MeshInstance *>(*it);
|
---|
| 2075 | const AxisAlignedBox3 box = mi->GetBox();
|
---|
| 2076 | Vector3 bmin = box.Min();
|
---|
| 2077 | Vector3 bmax = box.Max();
|
---|
[850] | 2078 | int id = mi->GetId();
|
---|
[840] | 2079 |
|
---|
[850] | 2080 | boxesOut.write(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
[840] | 2081 | boxesOut.write(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
|
---|
| 2082 | boxesOut.write(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
|
---|
| 2083 | }
|
---|
| 2084 |
|
---|
| 2085 | boxesOut.close();
|
---|
| 2086 | }
|
---|
| 2087 |
|
---|
| 2088 | return true;
|
---|
| 2089 | }
|
---|
| 2090 |
|
---|
| 2091 |
|
---|
[850] | 2092 | bool ViewCellsManager::LoadBoundingBoxes(const string filename,
|
---|
| 2093 | IndexedBoundingBoxContainer &boxes) const
|
---|
[840] | 2094 | {
|
---|
[850] | 2095 | Vector3 bmin, bmax;
|
---|
| 2096 | int id;
|
---|
[840] | 2097 |
|
---|
[850] | 2098 | if (USE_ASCII)
|
---|
[840] | 2099 | {
|
---|
[850] | 2100 | ifstream boxesIn(filename.c_str());
|
---|
[840] | 2101 |
|
---|
[850] | 2102 | if (!boxesIn.is_open())
|
---|
[840] | 2103 | {
|
---|
[850] | 2104 | cout << "failed to open file " << filename << endl;
|
---|
| 2105 | return false;
|
---|
[840] | 2106 | }
|
---|
[850] | 2107 |
|
---|
| 2108 | string buf;
|
---|
| 2109 | while (!(getline(boxesIn, buf)).eof())
|
---|
[840] | 2110 | {
|
---|
[850] | 2111 | sscanf(buf.c_str(), "%d %f %f %f %f %f %f",
|
---|
| 2112 | &id, &bmin.x, &bmin.y, &bmin.z,
|
---|
| 2113 | &bmax.x, &bmax.y, &bmax.z);
|
---|
| 2114 |
|
---|
| 2115 | AxisAlignedBox3 box(bmin, bmax);
|
---|
| 2116 | // MeshInstance *mi = new MeshInstance();
|
---|
| 2117 | // HACK: set bounding box to new box
|
---|
| 2118 | //mi->mBox = box;
|
---|
| 2119 |
|
---|
| 2120 | boxes.push_back(IndexedBoundingBox(id, box));
|
---|
[840] | 2121 | }
|
---|
| 2122 |
|
---|
[850] | 2123 | boxesIn.close();
|
---|
[840] | 2124 | }
|
---|
[850] | 2125 | else
|
---|
[840] | 2126 | {
|
---|
[850] | 2127 | ifstream boxesIn(filename.c_str(), ios::binary);
|
---|
[840] | 2128 |
|
---|
[850] | 2129 | if (!boxesIn.is_open())
|
---|
| 2130 | return false;
|
---|
| 2131 |
|
---|
| 2132 | while (1)
|
---|
[840] | 2133 | {
|
---|
[850] | 2134 | boxesIn.read(reinterpret_cast<char *>(&id), sizeof(Vector3));
|
---|
| 2135 | boxesIn.read(reinterpret_cast<char *>(&bmin), sizeof(Vector3));
|
---|
| 2136 | boxesIn.read(reinterpret_cast<char *>(&bmax), sizeof(Vector3));
|
---|
| 2137 |
|
---|
| 2138 | if (boxesIn.eof())
|
---|
| 2139 | break;
|
---|
| 2140 |
|
---|
| 2141 |
|
---|
| 2142 | AxisAlignedBox3 box(bmin, bmax);
|
---|
| 2143 | MeshInstance *mi = new MeshInstance(NULL);
|
---|
| 2144 |
|
---|
| 2145 | // HACK: set bounding box to new box
|
---|
| 2146 | //mi->mBox = box;
|
---|
| 2147 | //boxes.push_back(mi);
|
---|
| 2148 | boxes.push_back(IndexedBoundingBox(id, box));
|
---|
[840] | 2149 | }
|
---|
| 2150 |
|
---|
[850] | 2151 | boxesIn.close();
|
---|
[840] | 2152 | }
|
---|
| 2153 |
|
---|
| 2154 | return true;
|
---|
[850] | 2155 | }
|
---|
[840] | 2156 |
|
---|
[931] | 2157 |
|
---|
| 2158 | float ViewCellsManager::GetFilterWidth()
|
---|
| 2159 | {
|
---|
| 2160 | return mFilterWidth;
|
---|
| 2161 | }
|
---|
| 2162 |
|
---|
| 2163 |
|
---|
| 2164 | float ViewCellsManager::GetAbsFilterWidth()
|
---|
| 2165 | {
|
---|
| 2166 | return Magnitude(mViewSpaceBox.Size()) * mFilterWidth;
|
---|
| 2167 | }
|
---|
| 2168 |
|
---|
| 2169 |
|
---|
[1168] | 2170 | void ViewCellsManager::UpdateScalarPvsSize(ViewCell *vc,
|
---|
| 2171 | const int pvsSize,
|
---|
| 2172 | const int entriesInPvs) const
|
---|
[1002] | 2173 | {
|
---|
| 2174 | vc->mPvsSize = pvsSize;
|
---|
[1160] | 2175 | vc->mEntriesInPvs = entriesInPvs;
|
---|
| 2176 |
|
---|
[1002] | 2177 | vc->mPvsSizeValid = true;
|
---|
| 2178 | }
|
---|
| 2179 |
|
---|
| 2180 |
|
---|
[1021] | 2181 | void
|
---|
| 2182 | ViewCellsManager::ApplyFilter(ViewCell *viewCell,
|
---|
| 2183 | KdTree *kdTree,
|
---|
| 2184 | const float viewSpaceFilterSize,
|
---|
| 2185 | const float spatialFilterSize,
|
---|
| 2186 | ObjectPvs &pvs
|
---|
| 2187 | )
|
---|
| 2188 | {
|
---|
| 2189 | // extend the pvs of the viewcell by pvs of its neighbors
|
---|
| 2190 | // and apply spatial filter by including all neighbors of the objects
|
---|
| 2191 | // in the pvs
|
---|
| 2192 |
|
---|
| 2193 | // get all viewcells intersecting the viewSpaceFilterBox
|
---|
| 2194 | // and compute the pvs union
|
---|
| 2195 |
|
---|
| 2196 | //Vector3 center = viewCell->GetBox().Center();
|
---|
| 2197 | // Vector3 center = m->mBox.Center();
|
---|
| 2198 |
|
---|
| 2199 | // AxisAlignedBox3 box(center - Vector3(viewSpaceFilterSize/2),
|
---|
| 2200 | // center + Vector3(viewSpaceFilterSize/2));
|
---|
| 2201 | if (!ViewCellsConstructed())
|
---|
| 2202 | return;
|
---|
| 2203 |
|
---|
| 2204 | if (viewSpaceFilterSize >= 0.0f) {
|
---|
| 2205 |
|
---|
| 2206 | bool usePrVS = false;
|
---|
| 2207 |
|
---|
| 2208 | if (!usePrVS) {
|
---|
| 2209 | AxisAlignedBox3 box = GetViewCellBox(viewCell);
|
---|
| 2210 | box.Enlarge(Vector3(viewSpaceFilterSize/2));
|
---|
| 2211 |
|
---|
| 2212 | ViewCellContainer viewCells;
|
---|
| 2213 | ComputeBoxIntersections(box, viewCells);
|
---|
| 2214 |
|
---|
| 2215 | // cout<<"box="<<box<<endl;
|
---|
| 2216 | ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
| 2217 |
|
---|
| 2218 | int i;
|
---|
| 2219 | for (i=0; it != it_end; ++ it, ++ i) {
|
---|
| 2220 | //cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
|
---|
| 2221 | pvs.Merge((*it)->GetPvs());
|
---|
| 2222 | }
|
---|
| 2223 | } else {
|
---|
| 2224 | PrVs prvs;
|
---|
| 2225 | AxisAlignedBox3 box = GetViewCellBox(viewCell);
|
---|
| 2226 |
|
---|
| 2227 | // mViewCellsManager->SetMaxFilterSize(1);
|
---|
| 2228 | GetPrVS(box.Center(), prvs, viewSpaceFilterSize);
|
---|
| 2229 | pvs = prvs.mViewCell->GetPvs();
|
---|
| 2230 | DeleteLocalMergeTree(prvs.mViewCell);
|
---|
| 2231 | }
|
---|
| 2232 | } else
|
---|
| 2233 | pvs = viewCell->GetPvs();
|
---|
| 2234 |
|
---|
| 2235 | if (spatialFilterSize >=0.0f)
|
---|
| 2236 | ApplySpatialFilter(kdTree, spatialFilterSize, pvs);
|
---|
| 2237 |
|
---|
| 2238 | }
|
---|
| 2239 |
|
---|
| 2240 |
|
---|
| 2241 |
|
---|
| 2242 | void
|
---|
| 2243 | ViewCellsManager::ApplyFilter(KdTree *kdTree,
|
---|
| 2244 | const float relViewSpaceFilterSize,
|
---|
| 2245 | const float relSpatialFilterSize
|
---|
| 2246 | )
|
---|
| 2247 | {
|
---|
| 2248 |
|
---|
| 2249 | if (!ViewCellsConstructed())
|
---|
| 2250 | return;
|
---|
| 2251 |
|
---|
| 2252 | ViewCellContainer::const_iterator it, it_end = mViewCells.end();
|
---|
| 2253 |
|
---|
| 2254 | ObjectPvs *newPvs;
|
---|
| 2255 | newPvs = new ObjectPvs[mViewCells.size()];
|
---|
| 2256 |
|
---|
| 2257 | float viewSpaceFilterSize = Magnitude(mViewSpaceBox.Size())*relViewSpaceFilterSize;
|
---|
| 2258 | float spatialFilterSize = Magnitude(kdTree->GetBox().Size())*relSpatialFilterSize;
|
---|
| 2259 |
|
---|
| 2260 | int i;
|
---|
| 2261 | for (i=0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
|
---|
| 2262 | ApplyFilter(*it,
|
---|
| 2263 | kdTree,
|
---|
| 2264 | viewSpaceFilterSize,
|
---|
| 2265 | spatialFilterSize,
|
---|
| 2266 | newPvs[i]
|
---|
| 2267 | );
|
---|
| 2268 | }
|
---|
| 2269 |
|
---|
| 2270 | // now replace all pvss
|
---|
| 2271 | for (i = 0, it = mViewCells.begin(); it != it_end; ++ it, ++ i) {
|
---|
| 2272 |
|
---|
| 2273 | ObjectPvs &pvs = (*it)->GetPvs();
|
---|
| 2274 | pvs.Clear();
|
---|
| 2275 | pvs = newPvs[i];
|
---|
| 2276 | newPvs[i].Clear();
|
---|
| 2277 | }
|
---|
| 2278 |
|
---|
| 2279 | delete [] newPvs;
|
---|
| 2280 | }
|
---|
| 2281 |
|
---|
| 2282 |
|
---|
| 2283 |
|
---|
| 2284 |
|
---|
| 2285 | void
|
---|
| 2286 | ViewCellsManager::ApplySpatialFilter(
|
---|
| 2287 | KdTree *kdTree,
|
---|
| 2288 | const float spatialFilterSize,
|
---|
| 2289 | ObjectPvs &pvs
|
---|
| 2290 | )
|
---|
| 2291 | {
|
---|
| 2292 | // now compute a new Pvs by including also objects intersecting the
|
---|
| 2293 | // extended boxes of visible objects
|
---|
| 2294 |
|
---|
| 2295 | Intersectable::NewMail();
|
---|
| 2296 |
|
---|
[1189] | 2297 | ObjectPvsMap::const_iterator oi;
|
---|
[1021] | 2298 |
|
---|
[1189] | 2299 | for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++ oi)
|
---|
| 2300 | {
|
---|
| 2301 | Intersectable *object = (*oi).first;
|
---|
| 2302 | object->Mail();
|
---|
[1021] | 2303 | }
|
---|
| 2304 |
|
---|
| 2305 | ObjectPvs nPvs;
|
---|
[1189] | 2306 | int nPvsSize = 0;
|
---|
[1021] | 2307 | // now go through the pvs again
|
---|
| 2308 | for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++oi) {
|
---|
| 2309 | Intersectable *object = (*oi).first;
|
---|
| 2310 |
|
---|
| 2311 | // Vector3 center = object->GetBox().Center();
|
---|
| 2312 | // AxisAlignedBox3 box(center - Vector3(spatialFilterSize/2),
|
---|
| 2313 | // center + Vector3(spatialFilterSize/2));
|
---|
| 2314 |
|
---|
| 2315 | AxisAlignedBox3 box = object->GetBox();
|
---|
| 2316 | box.Enlarge(Vector3(spatialFilterSize/2));
|
---|
| 2317 |
|
---|
| 2318 | ObjectContainer objects;
|
---|
| 2319 |
|
---|
| 2320 | // $$ warning collect objects takes only unmailed ones!
|
---|
| 2321 | kdTree->CollectObjects(box,
|
---|
| 2322 | objects);
|
---|
| 2323 | // cout<<"collected objects="<<objects.size()<<endl;
|
---|
| 2324 | ObjectContainer::const_iterator noi = objects.begin();
|
---|
| 2325 | for (; noi != objects.end(); ++noi) {
|
---|
| 2326 | Intersectable *o = *noi;
|
---|
| 2327 | // $$ JB warning: pdfs are not correct at this point!
|
---|
| 2328 | nPvs.AddSample(o, Limits::Small);
|
---|
| 2329 | nPvsSize++;
|
---|
| 2330 | }
|
---|
| 2331 | }
|
---|
| 2332 | // cout<<"nPvs size = "<<nPvsSize<<endl;
|
---|
| 2333 | pvs.Merge(nPvs);
|
---|
| 2334 | }
|
---|
| 2335 |
|
---|
| 2336 |
|
---|
| 2337 | void ViewCellsManager::ExportColor(Exporter *exporter, ViewCell *vc) const
|
---|
| 2338 | {
|
---|
| 2339 | const bool vcValid = CheckValidity(vc, mMinPvsSize, mMaxPvsSize);
|
---|
| 2340 |
|
---|
| 2341 | float importance = 0;
|
---|
| 2342 | static Material m;
|
---|
| 2343 |
|
---|
| 2344 | switch (mColorCode)
|
---|
| 2345 | {
|
---|
| 2346 | case 0: // Random
|
---|
| 2347 | {
|
---|
| 2348 | if (vcValid)
|
---|
| 2349 | {
|
---|
| 2350 | m.mDiffuseColor.r = 0.5f + RandomValue(0.0f, 0.5f);
|
---|
| 2351 | m.mDiffuseColor.g = 0.5f + RandomValue(0.0f, 0.5f);
|
---|
| 2352 | m.mDiffuseColor.b = 0.5f + RandomValue(0.f, 0.5f);
|
---|
| 2353 | }
|
---|
| 2354 | else
|
---|
| 2355 | {
|
---|
| 2356 | m.mDiffuseColor.r = 0.0f;
|
---|
| 2357 | m.mDiffuseColor.g = 1.0f;
|
---|
| 2358 | m.mDiffuseColor.b = 0.0f;
|
---|
| 2359 | }
|
---|
| 2360 |
|
---|
| 2361 | exporter->SetForcedMaterial(m);
|
---|
| 2362 | return;
|
---|
| 2363 | }
|
---|
| 2364 |
|
---|
| 2365 | case 1: // pvs
|
---|
| 2366 | {
|
---|
| 2367 | if (mCurrentViewCellsStats.maxPvs)
|
---|
[1121] | 2368 | {
|
---|
| 2369 | importance =
|
---|
[1142] | 2370 | (float)mViewCellsTree->GetPvsSize(vc) /
|
---|
[1121] | 2371 | (float)mCurrentViewCellsStats.maxPvs;
|
---|
| 2372 | }
|
---|
[1021] | 2373 | }
|
---|
| 2374 | break;
|
---|
| 2375 | case 2: // merges
|
---|
| 2376 | {
|
---|
| 2377 | const int lSize = mViewCellsTree->GetNumInitialViewCells(vc);
|
---|
| 2378 | importance = (float)lSize / (float)mCurrentViewCellsStats.maxLeaves;
|
---|
| 2379 | }
|
---|
| 2380 | break;
|
---|
| 2381 | #if 0
|
---|
| 2382 | case 3: // merge tree differene
|
---|
| 2383 | {
|
---|
| 2384 | importance = (float)GetMaxTreeDiff(vc) /
|
---|
| 2385 | (float)(mVspBspTree->GetStatistics().maxDepth * 2);
|
---|
| 2386 |
|
---|
| 2387 | }
|
---|
| 2388 | break;
|
---|
| 2389 | #endif
|
---|
| 2390 | default:
|
---|
| 2391 | break;
|
---|
| 2392 | }
|
---|
| 2393 |
|
---|
| 2394 | // special color code for invalid view cells
|
---|
| 2395 | m.mDiffuseColor.r = importance;
|
---|
| 2396 | m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r;
|
---|
| 2397 | m.mDiffuseColor.b = vcValid ? 1.0f : 0.0f;
|
---|
| 2398 |
|
---|
| 2399 | //Debug << "importance: " << importance << endl;
|
---|
| 2400 | exporter->SetForcedMaterial(m);
|
---|
| 2401 | }
|
---|
| 2402 |
|
---|
| 2403 |
|
---|
[1022] | 2404 | void ViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 2405 | vector<MergeCandidate> &candidates)
|
---|
| 2406 | {
|
---|
| 2407 | // implemented in subclasses
|
---|
| 2408 | }
|
---|
| 2409 |
|
---|
[1047] | 2410 |
|
---|
[1168] | 2411 | void ViewCellsManager::UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs)
|
---|
| 2412 | {
|
---|
| 2413 | // terminate traversal
|
---|
| 2414 | if (root->IsLeaf())
|
---|
| 2415 | {
|
---|
| 2416 | // we assume that pvs is explicitly stored in leaves
|
---|
| 2417 | pvs = root->GetPvs();
|
---|
| 2418 | UpdateScalarPvsSize(root, pvs.CountObjectsInPvs(), pvs.GetSize());
|
---|
| 2419 |
|
---|
| 2420 | return;
|
---|
| 2421 | }
|
---|
| 2422 |
|
---|
| 2423 | //-- interior node => propagate pvs up
|
---|
| 2424 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(root);
|
---|
| 2425 | interior->GetPvs().Clear();
|
---|
| 2426 | pvs.Clear();
|
---|
| 2427 | vector<ObjectPvs> pvsList;
|
---|
| 2428 |
|
---|
| 2429 | ViewCellContainer::const_iterator vit, vit_end = interior->mChildren.end();
|
---|
| 2430 |
|
---|
| 2431 | for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit)
|
---|
| 2432 | {
|
---|
| 2433 | ObjectPvs objPvs;
|
---|
| 2434 |
|
---|
| 2435 | //-- recursivly compute child pvss
|
---|
| 2436 | UpdatePvsForEvaluation(*vit, objPvs);
|
---|
| 2437 |
|
---|
| 2438 | // store pvs in vector
|
---|
| 2439 | pvsList.push_back(objPvs);
|
---|
| 2440 | }
|
---|
| 2441 |
|
---|
| 2442 | #if 1
|
---|
| 2443 | Intersectable::NewMail();
|
---|
| 2444 |
|
---|
| 2445 | //-- faster way of computing pvs:
|
---|
| 2446 | // construct merged pvs by adding
|
---|
| 2447 | // and only those of the next pvs which were not mailed.
|
---|
| 2448 | // note: sumpdf is not correct!!
|
---|
| 2449 | vector<ObjectPvs>::iterator oit = pvsList.begin();
|
---|
| 2450 |
|
---|
| 2451 | for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ oit)
|
---|
| 2452 | {
|
---|
| 2453 | ObjectPvsMap::iterator pit, pit_end = (*oit).mEntries.end();
|
---|
| 2454 |
|
---|
| 2455 | for (pit = (*oit).mEntries.begin(); pit != pit_end; ++ pit)
|
---|
| 2456 | {
|
---|
| 2457 | Intersectable *intersect = (*pit).first;
|
---|
| 2458 |
|
---|
| 2459 | if (!intersect->Mailed())
|
---|
| 2460 | {
|
---|
| 2461 | pvs.AddSample(intersect, (*pit).second.mSumPdf);
|
---|
| 2462 | intersect->Mail();
|
---|
| 2463 | }
|
---|
| 2464 | }
|
---|
| 2465 | }
|
---|
| 2466 |
|
---|
| 2467 | // store pvs in this node
|
---|
| 2468 | if (mViewCellsTree->ViewCellsStorage() == ViewCellsTree::PVS_IN_INTERIORS)
|
---|
| 2469 | {
|
---|
| 2470 | interior->SetPvs(pvs);
|
---|
| 2471 | }
|
---|
| 2472 |
|
---|
| 2473 | // set new pvs size
|
---|
| 2474 | UpdateScalarPvsSize(interior, pvs.CountObjectsInPvs(), pvs.GetSize());
|
---|
| 2475 |
|
---|
| 2476 |
|
---|
[1178] | 2477 | #else // really merge cells: slow put sumPdf is correct
|
---|
[1168] | 2478 | viewCellInterior->GetPvs().Merge(backVc->GetPvs());
|
---|
| 2479 | viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
|
---|
| 2480 | #endif
|
---|
| 2481 | }
|
---|
| 2482 |
|
---|
[1174] | 2483 |
|
---|
| 2484 |
|
---|
[1145] | 2485 | /*******************************************************************/
|
---|
| 2486 | /* BspViewCellsManager implementation */
|
---|
| 2487 | /*******************************************************************/
|
---|
[440] | 2488 |
|
---|
[482] | 2489 |
|
---|
[1264] | 2490 | BspViewCellsManager::BspViewCellsManager(ViewCellsTree *vcTree, BspTree *bspTree):
|
---|
| 2491 | ViewCellsManager(vcTree), mBspTree(bspTree)
|
---|
[440] | 2492 | {
|
---|
[1004] | 2493 | Environment::GetSingleton()->GetIntValue("BspTree.Construction.samples", mInitialSamples);
|
---|
[587] | 2494 | mBspTree->SetViewCellsManager(this);
|
---|
[590] | 2495 | mBspTree->mViewCellsTree = mViewCellsTree;
|
---|
[440] | 2496 | }
|
---|
| 2497 |
|
---|
[462] | 2498 |
|
---|
[440] | 2499 | bool BspViewCellsManager::ViewCellsConstructed() const
|
---|
| 2500 | {
|
---|
| 2501 | return mBspTree->GetRoot() != NULL;
|
---|
| 2502 | }
|
---|
| 2503 |
|
---|
[480] | 2504 |
|
---|
[440] | 2505 | ViewCell *BspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
| 2506 | {
|
---|
| 2507 | return new BspViewCell(mesh);
|
---|
| 2508 | }
|
---|
| 2509 |
|
---|
[480] | 2510 |
|
---|
[574] | 2511 | int BspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
| 2512 | const VssRayContainer &rays)
|
---|
[440] | 2513 | {
|
---|
[441] | 2514 | // if view cells were already constructed
|
---|
[440] | 2515 | if (ViewCellsConstructed())
|
---|
| 2516 | return 0;
|
---|
| 2517 |
|
---|
| 2518 | int sampleContributions = 0;
|
---|
[503] | 2519 |
|
---|
[469] | 2520 | // construct view cells using the collected samples
|
---|
| 2521 | RayContainer constructionRays;
|
---|
| 2522 | VssRayContainer savedRays;
|
---|
[440] | 2523 |
|
---|
[574] | 2524 | const int limit = min(mInitialSamples, (int)rays.size());
|
---|
[440] | 2525 |
|
---|
[469] | 2526 | VssRayContainer::const_iterator it, it_end = rays.end();
|
---|
[448] | 2527 |
|
---|
[473] | 2528 | const float prop = (float)limit / ((float)rays.size() + Limits::Small);
|
---|
| 2529 |
|
---|
[469] | 2530 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
| 2531 | {
|
---|
[473] | 2532 | if (Random(1.0f) < prop)
|
---|
[469] | 2533 | constructionRays.push_back(new Ray(*(*it)));
|
---|
| 2534 | else
|
---|
| 2535 | savedRays.push_back(*it);
|
---|
| 2536 | }
|
---|
[440] | 2537 |
|
---|
[503] | 2538 | if (mViewCells.empty())
|
---|
| 2539 | {
|
---|
| 2540 | // no view cells loaded
|
---|
[587] | 2541 | mBspTree->Construct(objects, constructionRays, &mViewSpaceBox);
|
---|
[469] | 2542 | // collect final view cells
|
---|
| 2543 | mBspTree->CollectViewCells(mViewCells);
|
---|
[503] | 2544 | }
|
---|
[469] | 2545 | else
|
---|
| 2546 | {
|
---|
| 2547 | mBspTree->Construct(mViewCells);
|
---|
| 2548 | }
|
---|
| 2549 |
|
---|
| 2550 | // destroy rays created only for construction
|
---|
| 2551 | CLEAR_CONTAINER(constructionRays);
|
---|
| 2552 |
|
---|
[440] | 2553 | Debug << mBspTree->GetStatistics() << endl;
|
---|
[503] | 2554 |
|
---|
| 2555 | //EvaluateViewCellsStats();
|
---|
[660] | 2556 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
[480] | 2557 |
|
---|
[469] | 2558 | // recast rest of the rays
|
---|
[649] | 2559 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 2560 | ComputeSampleContributions(savedRays, true, false);
|
---|
[469] | 2561 |
|
---|
[719] | 2562 | // real meshes are contructed at this stage
|
---|
| 2563 | if (0)
|
---|
| 2564 | {
|
---|
| 2565 | cout << "finalizing view cells ... ";
|
---|
| 2566 | FinalizeViewCells(true);
|
---|
| 2567 | cout << "finished" << endl;
|
---|
| 2568 | }
|
---|
| 2569 |
|
---|
[440] | 2570 | return sampleContributions;
|
---|
| 2571 | }
|
---|
| 2572 |
|
---|
[503] | 2573 |
|
---|
[480] | 2574 | void BspViewCellsManager::CollectViewCells()
|
---|
| 2575 | {
|
---|
[587] | 2576 | // view cells tree constructed
|
---|
| 2577 | if (!ViewCellsTreeConstructed())
|
---|
[591] | 2578 | {
|
---|
[587] | 2579 | mBspTree->CollectViewCells(mViewCells);
|
---|
| 2580 | }
|
---|
| 2581 | else
|
---|
| 2582 | {
|
---|
| 2583 | // we can use the view cells tree hierarchy to get the right set
|
---|
[591] | 2584 | mViewCellsTree->CollectBestViewCellSet(mViewCells,
|
---|
[600] | 2585 | mNumActiveViewCells);
|
---|
[587] | 2586 | }
|
---|
[480] | 2587 | }
|
---|
[468] | 2588 |
|
---|
[503] | 2589 |
|
---|
[471] | 2590 | float BspViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
[440] | 2591 | {
|
---|
[471] | 2592 | // compute view cell area as subsititute for probability
|
---|
[591] | 2593 | if (1)
|
---|
| 2594 | return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
|
---|
| 2595 | else
|
---|
| 2596 | return GetArea(viewCell) / GetAccVcArea();
|
---|
[466] | 2597 | }
|
---|
[440] | 2598 |
|
---|
[503] | 2599 |
|
---|
[440] | 2600 |
|
---|
[468] | 2601 | int BspViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
| 2602 | const Vector3 &termination,
|
---|
| 2603 | ViewCellContainer &viewcells)
|
---|
[440] | 2604 | {
|
---|
[468] | 2605 | return mBspTree->CastLineSegment(origin, termination, viewcells);
|
---|
| 2606 | }
|
---|
| 2607 |
|
---|
| 2608 |
|
---|
[503] | 2609 | int BspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
[468] | 2610 | const VssRayContainer &rays)
|
---|
| 2611 | {
|
---|
[469] | 2612 | if (!ViewCellsConstructed())
|
---|
[441] | 2613 | {
|
---|
| 2614 | Debug << "view cells not constructed" << endl;
|
---|
| 2615 | return 0;
|
---|
| 2616 | }
|
---|
[590] | 2617 |
|
---|
| 2618 | // view cells already finished before post processing step (i.e. because they were loaded)
|
---|
| 2619 | if (mViewCellsFinished)
|
---|
| 2620 | {
|
---|
| 2621 | FinalizeViewCells(true);
|
---|
| 2622 | EvaluateViewCellsStats();
|
---|
| 2623 |
|
---|
| 2624 | return 0;
|
---|
| 2625 | }
|
---|
| 2626 |
|
---|
[1404] | 2627 | ///////////////////////////////////////
|
---|
[440] | 2628 | //-- post processing of bsp view cells
|
---|
[600] | 2629 |
|
---|
[440] | 2630 | int vcSize = 0;
|
---|
| 2631 | int pvsSize = 0;
|
---|
| 2632 |
|
---|
[591] | 2633 | //-- merge view cells
|
---|
[1264] | 2634 |
|
---|
[649] | 2635 | cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
|
---|
| 2636 | long startTime = GetTime();
|
---|
| 2637 |
|
---|
| 2638 | VssRayContainer postProcessRays;
|
---|
| 2639 | GetRaySets(rays, mPostProcessSamples, postProcessRays);
|
---|
| 2640 |
|
---|
[592] | 2641 | if (mMergeViewCells)
|
---|
[591] | 2642 | {
|
---|
[660] | 2643 | cout << "constructing visibility based merge tree" << endl;
|
---|
[650] | 2644 | mViewCellsTree->ConstructMergeTree(rays, objects);
|
---|
[649] | 2645 | }
|
---|
| 2646 | else
|
---|
| 2647 | {
|
---|
[660] | 2648 | cout << "constructing spatial merge tree" << endl;
|
---|
[752] | 2649 |
|
---|
[649] | 2650 | // create spatial merge hierarchy
|
---|
[752] | 2651 | ViewCell *root = ConstructSpatialMergeTree(mBspTree->GetRoot());
|
---|
| 2652 | mViewCellsTree->SetRoot(root);
|
---|
| 2653 |
|
---|
| 2654 | // compute pvs
|
---|
| 2655 | ObjectPvs pvs;
|
---|
| 2656 | UpdatePvsForEvaluation(root, pvs);
|
---|
[649] | 2657 | }
|
---|
[440] | 2658 |
|
---|
[660] | 2659 | // export statistics after merge
|
---|
[666] | 2660 | if (1)
|
---|
| 2661 | {
|
---|
| 2662 | char mstats[100];
|
---|
[1004] | 2663 | Environment::GetSingleton()->GetStringValue("ViewCells.mergeStats", mstats);
|
---|
[666] | 2664 | mViewCellsTree->ExportStats(mstats);
|
---|
| 2665 | }
|
---|
[650] | 2666 |
|
---|
[649] | 2667 | //-- stats and visualizations
|
---|
| 2668 | cout << "finished" << endl;
|
---|
[650] | 2669 | cout << "merged view cells in "
|
---|
[752] | 2670 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[440] | 2671 |
|
---|
[650] | 2672 | Debug << "Postprocessing: Merged view cells in "
|
---|
[752] | 2673 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl << endl;
|
---|
[649] | 2674 |
|
---|
[503] | 2675 |
|
---|
[660] | 2676 | //-- visualization and statistics
|
---|
[600] | 2677 | // reset view cells and stats
|
---|
[480] | 2678 | ResetViewCells();
|
---|
[660] | 2679 | Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
|
---|
[600] | 2680 |
|
---|
[1121] | 2681 | // save color code
|
---|
| 2682 | const int savedColorCode = mColorCode;
|
---|
[605] | 2683 |
|
---|
[600] | 2684 | //BspLeaf::NewMail();
|
---|
| 2685 | if (1) // export merged view cells
|
---|
| 2686 | {
|
---|
| 2687 | mColorCode = 0;
|
---|
[605] | 2688 |
|
---|
[744] | 2689 | Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl");
|
---|
[605] | 2690 |
|
---|
[600] | 2691 |
|
---|
| 2692 | cout << "exporting view cells after merge ... ";
|
---|
| 2693 |
|
---|
| 2694 | if (exporter)
|
---|
| 2695 | {
|
---|
| 2696 | if (mExportGeometry)
|
---|
| 2697 | exporter->ExportGeometry(objects);
|
---|
| 2698 |
|
---|
| 2699 | //exporter->SetWireframe();
|
---|
| 2700 | exporter->SetFilled();
|
---|
| 2701 | ExportViewCellsForViz(exporter);
|
---|
| 2702 |
|
---|
| 2703 |
|
---|
| 2704 | delete exporter;
|
---|
| 2705 | }
|
---|
| 2706 | cout << "finished" << endl;
|
---|
| 2707 | }
|
---|
| 2708 |
|
---|
[752] | 2709 | if (1) // export merged view cells using pvs color coding
|
---|
[600] | 2710 | {
|
---|
| 2711 | mColorCode = 1;
|
---|
| 2712 |
|
---|
[744] | 2713 | Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
|
---|
[600] | 2714 |
|
---|
| 2715 | cout << "exporting view cells after merge (pvs size) ... ";
|
---|
| 2716 |
|
---|
| 2717 | if (exporter)
|
---|
| 2718 | {
|
---|
| 2719 | //exporter->SetWireframe();
|
---|
[752] | 2720 | //exporter->SetForcedMaterial(RandomMaterial());
|
---|
| 2721 |
|
---|
[600] | 2722 | if (mExportGeometry)
|
---|
| 2723 | exporter->ExportGeometry(objects);
|
---|
| 2724 |
|
---|
| 2725 | //exporter->SetWireframe();
|
---|
| 2726 | exporter->SetFilled();
|
---|
| 2727 | ExportViewCellsForViz(exporter);
|
---|
| 2728 |
|
---|
| 2729 | delete exporter;
|
---|
| 2730 | }
|
---|
| 2731 | cout << "finished" << endl;
|
---|
| 2732 | }
|
---|
[693] | 2733 |
|
---|
[1122] | 2734 | // only for debugging purpose: test if the subdivision is valid
|
---|
| 2735 | if (0) TestSubdivision();
|
---|
[693] | 2736 |
|
---|
[600] | 2737 | mColorCode = savedColorCode;
|
---|
| 2738 |
|
---|
[719] | 2739 | // compute final meshes and volume / area
|
---|
| 2740 | if (1) FinalizeViewCells(true);
|
---|
| 2741 |
|
---|
[587] | 2742 | // write view cells to disc
|
---|
| 2743 | if (mExportViewCells)
|
---|
| 2744 | {
|
---|
[840] | 2745 | char filename[100];
|
---|
[1004] | 2746 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
|
---|
[931] | 2747 | ExportViewCells(filename, mExportPvs, objects);
|
---|
[587] | 2748 | }
|
---|
[840] | 2749 |
|
---|
| 2750 | // export bounding boxes
|
---|
[931] | 2751 | if (0 && mExportBboxesForPvs)
|
---|
[840] | 2752 | {
|
---|
| 2753 | char filename[100];
|
---|
[1004] | 2754 | Environment::GetSingleton()->GetStringValue("ViewCells.boxesFilename", filename);
|
---|
[840] | 2755 | ExportBoundingBoxes(filename, objects);
|
---|
| 2756 | }
|
---|
[587] | 2757 |
|
---|
[840] | 2758 |
|
---|
[650] | 2759 | return 0;
|
---|
[440] | 2760 | }
|
---|
| 2761 |
|
---|
[477] | 2762 |
|
---|
| 2763 | BspViewCellsManager::~BspViewCellsManager()
|
---|
| 2764 | {
|
---|
| 2765 | }
|
---|
| 2766 |
|
---|
| 2767 |
|
---|
[440] | 2768 | int BspViewCellsManager::GetType() const
|
---|
| 2769 | {
|
---|
| 2770 | return BSP;
|
---|
| 2771 | }
|
---|
| 2772 |
|
---|
| 2773 |
|
---|
| 2774 | void BspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
[466] | 2775 | const VssRayContainer &sampleRays)
|
---|
[440] | 2776 | {
|
---|
[469] | 2777 | if (!ViewCellsConstructed())
|
---|
| 2778 | return;
|
---|
[574] | 2779 |
|
---|
[600] | 2780 | int savedColorCode = mColorCode;
|
---|
| 2781 |
|
---|
| 2782 |
|
---|
| 2783 | if (1) // export final view cells
|
---|
[440] | 2784 | {
|
---|
[1020] | 2785 | mColorCode = 1; // hack color code
|
---|
[822] | 2786 | Exporter *exporter = Exporter::GetExporter("final_view_cells.x3d");
|
---|
[600] | 2787 |
|
---|
| 2788 | cout << "exporting view cells after merge (pvs size) ... ";
|
---|
[587] | 2789 |
|
---|
[469] | 2790 | if (exporter)
|
---|
[440] | 2791 | {
|
---|
[600] | 2792 | //exporter->SetWireframe();
|
---|
| 2793 |
|
---|
| 2794 | if (mExportGeometry)
|
---|
| 2795 | exporter->ExportGeometry(objects);
|
---|
[590] | 2796 |
|
---|
[801] | 2797 | //exporter->SetFilled();
|
---|
[1020] | 2798 | const bool b = mUseClipPlaneForViz;
|
---|
[801] | 2799 | mUseClipPlaneForViz = false;
|
---|
[1020] | 2800 |
|
---|
[508] | 2801 | ExportViewCellsForViz(exporter);
|
---|
[1020] | 2802 |
|
---|
[801] | 2803 | mUseClipPlaneForViz = b;
|
---|
[469] | 2804 | delete exporter;
|
---|
[440] | 2805 | }
|
---|
[469] | 2806 | cout << "finished" << endl;
|
---|
[503] | 2807 | }
|
---|
[477] | 2808 |
|
---|
[600] | 2809 | mColorCode = savedColorCode;
|
---|
| 2810 |
|
---|
[469] | 2811 | //-- visualization of the BSP splits
|
---|
| 2812 | bool exportSplits = false;
|
---|
[1004] | 2813 | Environment::GetSingleton()->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
|
---|
[503] | 2814 |
|
---|
[469] | 2815 | if (exportSplits)
|
---|
[440] | 2816 | {
|
---|
[469] | 2817 | cout << "exporting splits ... ";
|
---|
[477] | 2818 | ExportSplits(objects);
|
---|
[469] | 2819 | cout << "finished" << endl;
|
---|
[440] | 2820 | }
|
---|
[477] | 2821 |
|
---|
[600] | 2822 | // export single view cells
|
---|
[477] | 2823 | ExportBspPvs(objects);
|
---|
[440] | 2824 | }
|
---|
| 2825 |
|
---|
| 2826 |
|
---|
[477] | 2827 | void BspViewCellsManager::ExportSplits(const ObjectContainer &objects)
|
---|
[440] | 2828 | {
|
---|
| 2829 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
| 2830 |
|
---|
[503] | 2831 | if (exporter)
|
---|
| 2832 | {
|
---|
[598] | 2833 | //exporter->SetFilled();
|
---|
| 2834 |
|
---|
| 2835 | if (mExportGeometry)
|
---|
| 2836 | exporter->ExportGeometry(objects);
|
---|
| 2837 |
|
---|
[503] | 2838 | Material m;
|
---|
[440] | 2839 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 2840 | exporter->SetForcedMaterial(m);
|
---|
| 2841 | exporter->SetWireframe();
|
---|
[503] | 2842 |
|
---|
[440] | 2843 | exporter->ExportBspSplits(*mBspTree, true);
|
---|
| 2844 |
|
---|
[482] | 2845 | //NOTE: take forced material, else big scenes cannot be viewed
|
---|
[440] | 2846 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 2847 | exporter->SetForcedMaterial(m);
|
---|
[482] | 2848 | //exporter->ResetForcedMaterial();
|
---|
| 2849 |
|
---|
[440] | 2850 | delete exporter;
|
---|
| 2851 | }
|
---|
| 2852 | }
|
---|
| 2853 |
|
---|
[477] | 2854 |
|
---|
| 2855 | void BspViewCellsManager::ExportBspPvs(const ObjectContainer &objects)
|
---|
[440] | 2856 | {
|
---|
[477] | 2857 | const int leafOut = 10;
|
---|
[503] | 2858 |
|
---|
[477] | 2859 | ViewCell::NewMail();
|
---|
[440] | 2860 |
|
---|
[477] | 2861 | //-- some rays for output
|
---|
| 2862 | const int raysOut = min((int)mBspRays.size(), mVisualizationSamples);
|
---|
[503] | 2863 |
|
---|
[477] | 2864 | cout << "visualization using " << mVisualizationSamples << " samples" << endl;
|
---|
| 2865 | Debug << "\nOutput view cells: " << endl;
|
---|
[503] | 2866 |
|
---|
[997] | 2867 | // sort view cells in order to find the largest view cells
|
---|
[587] | 2868 | if (0)
|
---|
[801] | 2869 | stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
|
---|
[587] | 2870 |
|
---|
[503] | 2871 | int limit = min(leafOut, (int)mViewCells.size());
|
---|
| 2872 |
|
---|
[478] | 2873 | for (int i = 0; i < limit; ++ i)
|
---|
[477] | 2874 | {
|
---|
[478] | 2875 | cout << "creating output for view cell " << i << " ... ";
|
---|
| 2876 | VssRayContainer vcRays;
|
---|
| 2877 | Intersectable::NewMail();
|
---|
[582] | 2878 | ViewCell *vc;
|
---|
| 2879 |
|
---|
| 2880 | if (0)
|
---|
| 2881 | vc = mViewCells[i];
|
---|
| 2882 | else
|
---|
| 2883 | vc = mViewCells[Random((int)mViewCells.size())];
|
---|
| 2884 |
|
---|
[478] | 2885 | cout << "creating output for view cell " << i << " ... ";
|
---|
[469] | 2886 |
|
---|
[587] | 2887 | if(0)
|
---|
[477] | 2888 | {
|
---|
[587] | 2889 | // check whether we can add the current ray to the output rays
|
---|
| 2890 | for (int k = 0; k < raysOut; ++ k)
|
---|
[477] | 2891 | {
|
---|
[587] | 2892 | BspRay *ray = mBspRays[k];
|
---|
| 2893 | for (int j = 0; j < (int)ray->intersections.size(); ++ j)
|
---|
| 2894 | {
|
---|
| 2895 | BspLeaf *leaf = ray->intersections[j].mLeaf;
|
---|
| 2896 | if (vc == leaf->GetViewCell())
|
---|
| 2897 | vcRays.push_back(ray->vssRay);
|
---|
| 2898 | }
|
---|
[477] | 2899 | }
|
---|
| 2900 | }
|
---|
[587] | 2901 |
|
---|
[478] | 2902 | //bspLeaves[j]->Mail();
|
---|
| 2903 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
[477] | 2904 |
|
---|
[478] | 2905 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
[503] | 2906 |
|
---|
[478] | 2907 | exporter->SetWireframe();
|
---|
[477] | 2908 |
|
---|
[478] | 2909 | Material m;//= RandomMaterial();
|
---|
| 2910 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 2911 | exporter->SetForcedMaterial(m);
|
---|
[477] | 2912 |
|
---|
[587] | 2913 | ExportViewCellGeometry(exporter, vc);
|
---|
| 2914 |
|
---|
[478] | 2915 | // export rays piercing this view cell
|
---|
| 2916 | exporter->ExportRays(vcRays, RgbColor(0, 1, 0));
|
---|
[580] | 2917 |
|
---|
[478] | 2918 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 2919 | exporter->SetForcedMaterial(m);
|
---|
[440] | 2920 |
|
---|
[478] | 2921 | ObjectPvsMap::const_iterator it,
|
---|
| 2922 | it_end = vc->GetPvs().mEntries.end();
|
---|
[440] | 2923 |
|
---|
[478] | 2924 | exporter->SetFilled();
|
---|
[469] | 2925 |
|
---|
[478] | 2926 | // output PVS of view cell
|
---|
[503] | 2927 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
[478] | 2928 | {
|
---|
| 2929 | Intersectable *intersect = (*it).first;
|
---|
[469] | 2930 |
|
---|
[478] | 2931 | if (!intersect->Mailed())
|
---|
| 2932 | {
|
---|
| 2933 | Material m = RandomMaterial();
|
---|
| 2934 | exporter->SetForcedMaterial(m);
|
---|
| 2935 |
|
---|
| 2936 | exporter->ExportIntersectable(intersect);
|
---|
| 2937 | intersect->Mail();
|
---|
[503] | 2938 | }
|
---|
[477] | 2939 | }
|
---|
[503] | 2940 |
|
---|
[478] | 2941 | DEL_PTR(exporter);
|
---|
| 2942 | cout << "finished" << endl;
|
---|
[477] | 2943 | }
|
---|
| 2944 |
|
---|
| 2945 | Debug << endl;
|
---|
[440] | 2946 | }
|
---|
| 2947 |
|
---|
[441] | 2948 |
|
---|
[693] | 2949 | void BspViewCellsManager::TestSubdivision()
|
---|
| 2950 | {
|
---|
| 2951 | ViewCellContainer leaves;
|
---|
| 2952 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 2953 |
|
---|
| 2954 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 2955 |
|
---|
| 2956 | const float vol = mViewSpaceBox.GetVolume();
|
---|
| 2957 | float subdivVol = 0;
|
---|
| 2958 | float newVol = 0;
|
---|
| 2959 |
|
---|
| 2960 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 2961 | {
|
---|
| 2962 | BspNodeGeometry geom;
|
---|
| 2963 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 2964 | mBspTree->ConstructGeometry(leaf, geom);
|
---|
| 2965 |
|
---|
| 2966 | const float lVol = geom.GetVolume();
|
---|
| 2967 |
|
---|
| 2968 | newVol += lVol;
|
---|
| 2969 | subdivVol += (*it)->GetVolume();
|
---|
[694] | 2970 |
|
---|
[704] | 2971 | float thres = 0.9f;
|
---|
[694] | 2972 | if ((lVol < ((*it)->GetVolume() * thres)) ||
|
---|
| 2973 | (lVol * thres > ((*it)->GetVolume())))
|
---|
| 2974 | Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
|
---|
[693] | 2975 | }
|
---|
| 2976 |
|
---|
| 2977 | Debug << "exact volume: " << vol << endl;
|
---|
| 2978 | Debug << "subdivision volume: " << subdivVol << endl;
|
---|
| 2979 | Debug << "new volume: " << newVol << endl;
|
---|
| 2980 | }
|
---|
| 2981 |
|
---|
| 2982 |
|
---|
[580] | 2983 | void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
[591] | 2984 | ViewCell *vc,
|
---|
[1047] | 2985 | const AxisAlignedPlane *clipPlane) const
|
---|
[482] | 2986 | {
|
---|
[1027] | 2987 | // export mesh if available
|
---|
[482] | 2988 | if (vc->GetMesh())
|
---|
| 2989 | {
|
---|
[587] | 2990 | exporter->ExportMesh(vc->GetMesh());
|
---|
| 2991 | return;
|
---|
[482] | 2992 | }
|
---|
[1027] | 2993 | // otherwise construct from leaves
|
---|
[660] | 2994 | if (clipPlane)
|
---|
[591] | 2995 | {
|
---|
[1047] | 2996 | const Plane3 plane = clipPlane->GetPlane();
|
---|
| 2997 |
|
---|
[726] | 2998 | ViewCellContainer leaves;
|
---|
| 2999 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 3000 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
[591] | 3001 |
|
---|
[726] | 3002 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[681] | 3003 | {
|
---|
[726] | 3004 | BspNodeGeometry geom;
|
---|
| 3005 |
|
---|
[681] | 3006 | BspNodeGeometry front;
|
---|
| 3007 | BspNodeGeometry back;
|
---|
[591] | 3008 |
|
---|
[726] | 3009 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 3010 | mBspTree->ConstructGeometry(leaf, geom);
|
---|
[681] | 3011 |
|
---|
[726] | 3012 | const float eps = 0.00000001f;
|
---|
[1047] | 3013 | const int cf = geom.Side(plane, eps);
|
---|
[726] | 3014 |
|
---|
| 3015 | if (cf == -1)
|
---|
| 3016 | {
|
---|
| 3017 | exporter->ExportPolygons(geom.GetPolys());
|
---|
| 3018 | }
|
---|
| 3019 | else if (cf == 0)
|
---|
| 3020 | {
|
---|
| 3021 | geom.SplitGeometry(front,
|
---|
| 3022 | back,
|
---|
[1047] | 3023 | plane,
|
---|
[726] | 3024 | mViewSpaceBox,
|
---|
| 3025 | eps);
|
---|
| 3026 |
|
---|
| 3027 | //Debug << "geo size: " << geom.Size() << endl;
|
---|
| 3028 | //Debug << "size b: " << back.Size() << " f: " << front.Size() << endl;
|
---|
| 3029 | if (back.Valid())
|
---|
| 3030 | {
|
---|
| 3031 | exporter->ExportPolygons(back.GetPolys());
|
---|
| 3032 | }
|
---|
| 3033 | }
|
---|
[681] | 3034 | }
|
---|
[591] | 3035 | }
|
---|
| 3036 | else
|
---|
| 3037 | {
|
---|
[726] | 3038 | BspNodeGeometry geom;
|
---|
| 3039 | mBspTree->ConstructGeometry(vc, geom);
|
---|
| 3040 |
|
---|
[678] | 3041 | exporter->ExportPolygons(geom.GetPolys());
|
---|
[591] | 3042 | }
|
---|
[482] | 3043 | }
|
---|
| 3044 |
|
---|
[503] | 3045 |
|
---|
| 3046 | void BspViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
| 3047 | {
|
---|
[726] | 3048 | // delete previous mesh
|
---|
| 3049 | ///DEL_PTR(vc->GetMesh());
|
---|
[587] | 3050 | BspNodeGeometry geom;
|
---|
| 3051 | mBspTree->ConstructGeometry(vc, geom);
|
---|
| 3052 |
|
---|
[1001] | 3053 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
[726] | 3054 |
|
---|
[840] | 3055 | IncludeNodeGeomInMesh(geom, *mesh);
|
---|
[587] | 3056 | vc->SetMesh(mesh);
|
---|
[503] | 3057 | }
|
---|
| 3058 |
|
---|
[551] | 3059 |
|
---|
[587] | 3060 | void BspViewCellsManager::Finalize(ViewCell *viewCell,
|
---|
| 3061 | const bool createMesh)
|
---|
[502] | 3062 | {
|
---|
[587] | 3063 | float area = 0;
|
---|
| 3064 | float volume = 0;
|
---|
[572] | 3065 |
|
---|
[587] | 3066 | ViewCellContainer leaves;
|
---|
| 3067 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
| 3068 |
|
---|
| 3069 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 3070 |
|
---|
[693] | 3071 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[587] | 3072 | {
|
---|
| 3073 | BspNodeGeometry geom;
|
---|
| 3074 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 3075 | mBspTree->ConstructGeometry(leaf, geom);
|
---|
| 3076 |
|
---|
[719] | 3077 | const float lVol = geom.GetVolume();
|
---|
| 3078 | const float lArea = geom.GetArea();
|
---|
[693] | 3079 |
|
---|
| 3080 | //(*it)->SetVolume(vol);
|
---|
| 3081 | //(*it)->SetArea(area);
|
---|
| 3082 |
|
---|
| 3083 | area += lArea;
|
---|
| 3084 | volume += lVol;
|
---|
| 3085 |
|
---|
| 3086 | CreateMesh(*it);
|
---|
[587] | 3087 | }
|
---|
| 3088 |
|
---|
| 3089 | viewCell->SetVolume(volume);
|
---|
| 3090 | viewCell->SetArea(area);
|
---|
[502] | 3091 | }
|
---|
| 3092 |
|
---|
[580] | 3093 |
|
---|
[879] | 3094 | ViewCell *BspViewCellsManager::GetViewCell(const Vector3 &point, const bool active) const
|
---|
[580] | 3095 | {
|
---|
[1021] | 3096 | if (!ViewCellsConstructed())
|
---|
[587] | 3097 | return NULL;
|
---|
| 3098 |
|
---|
| 3099 | if (!mViewSpaceBox.IsInside(point))
|
---|
| 3100 | return NULL;
|
---|
| 3101 |
|
---|
| 3102 | return mBspTree->GetViewCell(point);
|
---|
[580] | 3103 | }
|
---|
| 3104 |
|
---|
| 3105 |
|
---|
[587] | 3106 | void BspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 3107 | vector<MergeCandidate> &candidates)
|
---|
| 3108 | {
|
---|
| 3109 | cout << "collecting merge candidates ... " << endl;
|
---|
| 3110 |
|
---|
| 3111 | if (mUseRaysForMerge)
|
---|
| 3112 | {
|
---|
| 3113 | mBspTree->CollectMergeCandidates(rays, candidates);
|
---|
| 3114 | }
|
---|
| 3115 | else
|
---|
| 3116 | {
|
---|
| 3117 | vector<BspLeaf *> leaves;
|
---|
| 3118 | mBspTree->CollectLeaves(leaves);
|
---|
| 3119 | mBspTree->CollectMergeCandidates(leaves, candidates);
|
---|
| 3120 | }
|
---|
| 3121 |
|
---|
| 3122 | cout << "fininshed collecting candidates" << endl;
|
---|
| 3123 | }
|
---|
| 3124 |
|
---|
| 3125 |
|
---|
[440] | 3126 |
|
---|
[1047] | 3127 | bool BspViewCellsManager::ExportViewCells(const string filename,
|
---|
| 3128 | const bool exportPvs,
|
---|
| 3129 | const ObjectContainer &objects)
|
---|
[590] | 3130 | {
|
---|
[1077] | 3131 | #if TODO
|
---|
[590] | 3132 | cout << "exporting view cells to xml ... ";
|
---|
| 3133 | std::ofstream stream;
|
---|
[503] | 3134 |
|
---|
[590] | 3135 | // for output we need unique ids for each view cell
|
---|
| 3136 | CreateUniqueViewCellIds();
|
---|
[503] | 3137 |
|
---|
[590] | 3138 |
|
---|
| 3139 | stream.open(filename.c_str());
|
---|
| 3140 | stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
[975] | 3141 | stream << "<VisibilitySolution>" << endl;
|
---|
[590] | 3142 |
|
---|
| 3143 | //-- the view space bounding box
|
---|
| 3144 | stream << "<ViewSpaceBox"
|
---|
| 3145 | << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
|
---|
| 3146 | << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\" />" << endl;
|
---|
| 3147 |
|
---|
| 3148 | //-- the type of the view cells hierarchy
|
---|
[975] | 3149 |
|
---|
[840] | 3150 | // NOTE: load in vsp bsp here because bsp and vsp bsp can use same tree and vsp bsp is bug free
|
---|
[975] | 3151 | stream << "<Hierarchy name=\"vspBspTree\" >" << endl;
|
---|
[840] | 3152 |
|
---|
[590] | 3153 | //-- load the view cells itself, i.e., the ids and the pvs
|
---|
| 3154 | stream << "<ViewCells>" << endl;
|
---|
[610] | 3155 |
|
---|
[871] | 3156 | mViewCellsTree->Export(stream, exportPvs);
|
---|
[590] | 3157 |
|
---|
| 3158 | stream << "</ViewCells>" << endl;
|
---|
| 3159 |
|
---|
| 3160 | //-- load the hierarchy
|
---|
| 3161 | stream << "<Hierarchy>" << endl;
|
---|
| 3162 | mBspTree->Export(stream);
|
---|
| 3163 | stream << endl << "</Hierarchy>" << endl;
|
---|
| 3164 |
|
---|
[975] | 3165 | stream << "</VisibilitySolution>" << endl;
|
---|
[590] | 3166 | stream.close();
|
---|
| 3167 |
|
---|
| 3168 | cout << "finished" << endl;
|
---|
[955] | 3169 | #endif
|
---|
[590] | 3170 | return true;
|
---|
| 3171 | }
|
---|
| 3172 |
|
---|
| 3173 |
|
---|
[650] | 3174 | ViewCell *BspViewCellsManager::ConstructSpatialMergeTree(BspNode *root)
|
---|
[610] | 3175 | {
|
---|
[752] | 3176 | // terminate recursion
|
---|
[650] | 3177 | if (root->IsLeaf())
|
---|
| 3178 | {
|
---|
[752] | 3179 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(root);
|
---|
| 3180 | leaf->GetViewCell()->SetMergeCost(0.0f);
|
---|
| 3181 | return leaf->GetViewCell();
|
---|
[650] | 3182 | }
|
---|
| 3183 |
|
---|
| 3184 | BspInterior *interior = dynamic_cast<BspInterior *>(root);
|
---|
| 3185 | ViewCellInterior *viewCellInterior = new ViewCellInterior();
|
---|
| 3186 |
|
---|
[752] | 3187 | // evaluate merge cost for priority traversal
|
---|
[650] | 3188 | float mergeCost = 1.0f / (float)root->mTimeStamp;
|
---|
| 3189 | viewCellInterior->SetMergeCost(mergeCost);
|
---|
[649] | 3190 |
|
---|
[650] | 3191 | float volume = 0;
|
---|
[649] | 3192 |
|
---|
[650] | 3193 | BspNode *front = interior->GetFront();
|
---|
| 3194 | BspNode *back = interior->GetBack();
|
---|
[649] | 3195 |
|
---|
[752] | 3196 |
|
---|
| 3197 | //-- recursivly compute child hierarchies
|
---|
[650] | 3198 | ViewCell *backVc = ConstructSpatialMergeTree(back);
|
---|
| 3199 | ViewCell *frontVc = ConstructSpatialMergeTree(front);
|
---|
[610] | 3200 |
|
---|
[752] | 3201 |
|
---|
| 3202 | viewCellInterior->SetupChildLink(backVc);
|
---|
[650] | 3203 | viewCellInterior->SetupChildLink(frontVc);
|
---|
[610] | 3204 |
|
---|
[650] | 3205 | volume += backVc->GetVolume();
|
---|
| 3206 | volume += frontVc->GetVolume();
|
---|
[649] | 3207 |
|
---|
[650] | 3208 | viewCellInterior->SetVolume(volume);
|
---|
[649] | 3209 |
|
---|
[650] | 3210 | return viewCellInterior;
|
---|
[610] | 3211 | }
|
---|
| 3212 |
|
---|
[650] | 3213 |
|
---|
[590] | 3214 | /************************************************************************/
|
---|
| 3215 | /* KdViewCellsManager implementation */
|
---|
| 3216 | /************************************************************************/
|
---|
| 3217 |
|
---|
| 3218 |
|
---|
| 3219 |
|
---|
[1264] | 3220 | KdViewCellsManager::KdViewCellsManager(ViewCellsTree *vcTree, KdTree *kdTree):
|
---|
| 3221 | ViewCellsManager(vcTree), mKdTree(kdTree), mKdPvsDepth(100)
|
---|
[440] | 3222 | {
|
---|
| 3223 | }
|
---|
| 3224 |
|
---|
[605] | 3225 |
|
---|
[471] | 3226 | float KdViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
[468] | 3227 | {
|
---|
[479] | 3228 | // compute view cell area / volume as subsititute for probability
|
---|
[605] | 3229 | if (0)
|
---|
| 3230 | return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
|
---|
| 3231 | else
|
---|
| 3232 | return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
|
---|
[468] | 3233 | }
|
---|
| 3234 |
|
---|
| 3235 |
|
---|
| 3236 |
|
---|
[479] | 3237 |
|
---|
[480] | 3238 | void KdViewCellsManager::CollectViewCells()
|
---|
| 3239 | {
|
---|
| 3240 | //mKdTree->CollectViewCells(mViewCells); TODO
|
---|
| 3241 | }
|
---|
[479] | 3242 |
|
---|
[519] | 3243 |
|
---|
[574] | 3244 | int KdViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
[487] | 3245 | const VssRayContainer &rays)
|
---|
[440] | 3246 | {
|
---|
[441] | 3247 | // if view cells already constructed
|
---|
[440] | 3248 | if (ViewCellsConstructed())
|
---|
| 3249 | return 0;
|
---|
[441] | 3250 |
|
---|
[440] | 3251 | mKdTree->Construct();
|
---|
| 3252 |
|
---|
[480] | 3253 | mTotalAreaValid = false;
|
---|
[469] | 3254 | // create the view cells
|
---|
| 3255 | mKdTree->CreateAndCollectViewCells(mViewCells);
|
---|
[503] | 3256 |
|
---|
[480] | 3257 | // cast rays
|
---|
[574] | 3258 | ComputeSampleContributions(rays, true, false);
|
---|
[480] | 3259 |
|
---|
[479] | 3260 | EvaluateViewCellsStats();
|
---|
[660] | 3261 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
[469] | 3262 |
|
---|
[440] | 3263 | return 0;
|
---|
| 3264 | }
|
---|
| 3265 |
|
---|
[587] | 3266 |
|
---|
[440] | 3267 | bool KdViewCellsManager::ViewCellsConstructed() const
|
---|
| 3268 | {
|
---|
| 3269 | return mKdTree->GetRoot() != NULL;
|
---|
| 3270 | }
|
---|
| 3271 |
|
---|
[1020] | 3272 |
|
---|
[503] | 3273 | int KdViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
[466] | 3274 | const VssRayContainer &rays)
|
---|
[440] | 3275 | {
|
---|
| 3276 | return 0;
|
---|
| 3277 | }
|
---|
| 3278 |
|
---|
[1020] | 3279 |
|
---|
[440] | 3280 | void KdViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
[466] | 3281 | const VssRayContainer &sampleRays)
|
---|
[440] | 3282 | {
|
---|
[441] | 3283 | if (!ViewCellsConstructed())
|
---|
| 3284 | return;
|
---|
| 3285 |
|
---|
[469] | 3286 | // using view cells instead of the kd PVS of objects
|
---|
| 3287 | const bool useViewCells = true;
|
---|
| 3288 | bool exportRays = false;
|
---|
[440] | 3289 |
|
---|
| 3290 | int limit = min(mVisualizationSamples, (int)sampleRays.size());
|
---|
[469] | 3291 | const int pvsOut = min((int)objects.size(), 10);
|
---|
[466] | 3292 | VssRayContainer *rays = new VssRayContainer[pvsOut];
|
---|
[440] | 3293 |
|
---|
[469] | 3294 | if (useViewCells)
|
---|
[440] | 3295 | {
|
---|
[469] | 3296 | const int leafOut = 10;
|
---|
[503] | 3297 |
|
---|
[469] | 3298 | ViewCell::NewMail();
|
---|
[503] | 3299 |
|
---|
[469] | 3300 | //-- some rays for output
|
---|
| 3301 | const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
|
---|
| 3302 | Debug << "visualization using " << raysOut << " samples" << endl;
|
---|
| 3303 |
|
---|
| 3304 | //-- some random view cells and rays for output
|
---|
| 3305 | vector<KdLeaf *> kdLeaves;
|
---|
[503] | 3306 |
|
---|
[469] | 3307 | for (int i = 0; i < leafOut; ++ i)
|
---|
| 3308 | kdLeaves.push_back(dynamic_cast<KdLeaf *>(mKdTree->GetRandomLeaf()));
|
---|
[503] | 3309 |
|
---|
[469] | 3310 | for (int i = 0; i < kdLeaves.size(); ++ i)
|
---|
[440] | 3311 | {
|
---|
[469] | 3312 | KdLeaf *leaf = kdLeaves[i];
|
---|
| 3313 | RayContainer vcRays;
|
---|
[503] | 3314 |
|
---|
[469] | 3315 | cout << "creating output for view cell " << i << " ... ";
|
---|
[503] | 3316 | #if 0
|
---|
[469] | 3317 | // check whether we can add the current ray to the output rays
|
---|
[503] | 3318 | for (int k = 0; k < raysOut; ++ k)
|
---|
[440] | 3319 | {
|
---|
[469] | 3320 | Ray *ray = sampleRays[k];
|
---|
[503] | 3321 |
|
---|
[469] | 3322 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
| 3323 | {
|
---|
| 3324 | BspLeaf *leaf2 = ray->bspIntersections[j].mLeaf;
|
---|
[503] | 3325 |
|
---|
| 3326 | if (leaf->GetViewCell() == leaf2->GetViewCell())
|
---|
[469] | 3327 | {
|
---|
| 3328 | vcRays.push_back(ray);
|
---|
| 3329 | }
|
---|
| 3330 | }
|
---|
[479] | 3331 | }
|
---|
[503] | 3332 | #endif
|
---|
[469] | 3333 | Intersectable::NewMail();
|
---|
[503] | 3334 |
|
---|
[469] | 3335 | ViewCell *vc = leaf->mViewCell;
|
---|
[503] | 3336 |
|
---|
[469] | 3337 | //bspLeaves[j]->Mail();
|
---|
| 3338 | char s[64]; sprintf(s, "kd-pvs%04d.x3d", i);
|
---|
[503] | 3339 |
|
---|
[469] | 3340 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
| 3341 | exporter->SetFilled();
|
---|
| 3342 |
|
---|
| 3343 | exporter->SetWireframe();
|
---|
| 3344 | //exporter->SetFilled();
|
---|
[503] | 3345 |
|
---|
[469] | 3346 | Material m;//= RandomMaterial();
|
---|
| 3347 | m.mDiffuseColor = RgbColor(1, 1, 0);
|
---|
| 3348 | exporter->SetForcedMaterial(m);
|
---|
[503] | 3349 |
|
---|
[469] | 3350 | AxisAlignedBox3 box = mKdTree->GetBox(leaf);
|
---|
| 3351 | exporter->ExportBox(box);
|
---|
| 3352 |
|
---|
| 3353 | // export rays piercing this view cell
|
---|
[503] | 3354 | exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));
|
---|
| 3355 |
|
---|
[469] | 3356 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 3357 | exporter->SetForcedMaterial(m);
|
---|
[503] | 3358 |
|
---|
[469] | 3359 | // exporter->SetWireframe();
|
---|
| 3360 | exporter->SetFilled();
|
---|
[503] | 3361 |
|
---|
[469] | 3362 | ObjectPvsMap::iterator it, it_end = vc->GetPvs().mEntries.end();
|
---|
[752] | 3363 | // -- output PVS of view cell
|
---|
[1189] | 3364 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
[469] | 3365 | {
|
---|
| 3366 | Intersectable *intersect = (*it).first;
|
---|
| 3367 | if (!intersect->Mailed())
|
---|
| 3368 | {
|
---|
| 3369 | exporter->ExportIntersectable(intersect);
|
---|
| 3370 | intersect->Mail();
|
---|
[503] | 3371 | }
|
---|
[440] | 3372 | }
|
---|
[503] | 3373 |
|
---|
[469] | 3374 | DEL_PTR(exporter);
|
---|
| 3375 | cout << "finished" << endl;
|
---|
[440] | 3376 | }
|
---|
[469] | 3377 |
|
---|
| 3378 | DEL_PTR(rays);
|
---|
[440] | 3379 | }
|
---|
[469] | 3380 | else // using kd PVS of objects
|
---|
| 3381 | {
|
---|
| 3382 | for (int i = 0; i < limit; ++ i)
|
---|
| 3383 | {
|
---|
| 3384 | VssRay *ray = sampleRays[i];
|
---|
[503] | 3385 |
|
---|
[469] | 3386 | // check whether we can add this to the rays
|
---|
[503] | 3387 | for (int j = 0; j < pvsOut; j++)
|
---|
[469] | 3388 | {
|
---|
| 3389 | if (objects[j] == ray->mTerminationObject)
|
---|
| 3390 | {
|
---|
| 3391 | rays[j].push_back(ray);
|
---|
| 3392 | }
|
---|
| 3393 | }
|
---|
| 3394 | }
|
---|
[440] | 3395 |
|
---|
[503] | 3396 | if (exportRays)
|
---|
[469] | 3397 | {
|
---|
| 3398 | Exporter *exporter = NULL;
|
---|
| 3399 | exporter = Exporter::GetExporter("sample-rays.x3d");
|
---|
| 3400 | exporter->SetWireframe();
|
---|
| 3401 | exporter->ExportKdTree(*mKdTree);
|
---|
[503] | 3402 |
|
---|
[728] | 3403 | for (i = 0; i < pvsOut; i++)
|
---|
[469] | 3404 | exporter->ExportRays(rays[i], RgbColor(1, 0, 0));
|
---|
| 3405 |
|
---|
| 3406 | exporter->SetFilled();
|
---|
[503] | 3407 |
|
---|
[469] | 3408 | delete exporter;
|
---|
| 3409 | }
|
---|
[440] | 3410 |
|
---|
[503] | 3411 | for (int k=0; k < pvsOut; k++)
|
---|
[469] | 3412 | {
|
---|
| 3413 | Intersectable *object = objects[k];
|
---|
[503] | 3414 | char s[64];
|
---|
[469] | 3415 | sprintf(s, "sample-pvs%04d.x3d", k);
|
---|
[440] | 3416 |
|
---|
[469] | 3417 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
| 3418 | exporter->SetWireframe();
|
---|
[503] | 3419 |
|
---|
[1233] | 3420 | KdPvsMap::iterator kit = object->mKdPvs.mEntries.begin();
|
---|
[469] | 3421 | Intersectable::NewMail();
|
---|
[503] | 3422 |
|
---|
[469] | 3423 | // avoid adding the object to the list
|
---|
| 3424 | object->Mail();
|
---|
| 3425 | ObjectContainer visibleObjects;
|
---|
[503] | 3426 |
|
---|
[1233] | 3427 | for (; kit != object->mKdPvs.mEntries.end(); i++)
|
---|
[469] | 3428 | {
|
---|
[1233] | 3429 | KdNode *node = (*kit).first;
|
---|
[469] | 3430 | exporter->ExportBox(mKdTree->GetBox(node));
|
---|
[503] | 3431 |
|
---|
[469] | 3432 | mKdTree->CollectObjects(node, visibleObjects);
|
---|
| 3433 | }
|
---|
[503] | 3434 |
|
---|
[469] | 3435 | exporter->ExportRays(rays[k], RgbColor(0, 1, 0));
|
---|
| 3436 | exporter->SetFilled();
|
---|
[503] | 3437 |
|
---|
[469] | 3438 | for (int j = 0; j < visibleObjects.size(); j++)
|
---|
| 3439 | exporter->ExportIntersectable(visibleObjects[j]);
|
---|
[503] | 3440 |
|
---|
[469] | 3441 | Material m;
|
---|
| 3442 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 3443 | exporter->SetForcedMaterial(m);
|
---|
| 3444 | exporter->ExportIntersectable(object);
|
---|
[503] | 3445 |
|
---|
[469] | 3446 | delete exporter;
|
---|
[440] | 3447 | }
|
---|
[503] | 3448 | }
|
---|
[440] | 3449 | }
|
---|
| 3450 |
|
---|
[469] | 3451 |
|
---|
[580] | 3452 | ViewCell *KdViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
| 3453 | {
|
---|
| 3454 | return new KdViewCell(mesh);
|
---|
| 3455 | }
|
---|
[482] | 3456 |
|
---|
[580] | 3457 |
|
---|
| 3458 | void KdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
[591] | 3459 | ViewCell *vc,
|
---|
[1047] | 3460 | const AxisAlignedPlane *clipPlane) const
|
---|
[482] | 3461 | {
|
---|
[580] | 3462 | ViewCellContainer leaves;
|
---|
[485] | 3463 |
|
---|
[580] | 3464 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 3465 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 3466 |
|
---|
| 3467 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 3468 | {
|
---|
| 3469 | KdViewCell *kdVc = dynamic_cast<KdViewCell *>(*it);
|
---|
| 3470 |
|
---|
| 3471 | exporter->ExportBox(mKdTree->GetBox(kdVc->mLeaf));
|
---|
| 3472 | }
|
---|
[482] | 3473 | }
|
---|
| 3474 |
|
---|
| 3475 |
|
---|
[440] | 3476 | int KdViewCellsManager::GetType() const
|
---|
| 3477 | {
|
---|
| 3478 | return ViewCellsManager::KD;
|
---|
| 3479 | }
|
---|
| 3480 |
|
---|
| 3481 |
|
---|
[441] | 3482 |
|
---|
[440] | 3483 | KdNode *KdViewCellsManager::GetNodeForPvs(KdLeaf *leaf)
|
---|
| 3484 | {
|
---|
| 3485 | KdNode *node = leaf;
|
---|
| 3486 |
|
---|
| 3487 | while (node->mParent && node->mDepth > mKdPvsDepth)
|
---|
| 3488 | node = node->mParent;
|
---|
[1176] | 3489 |
|
---|
[440] | 3490 | return node;
|
---|
| 3491 | }
|
---|
| 3492 |
|
---|
[469] | 3493 | int KdViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
| 3494 | const Vector3 &termination,
|
---|
| 3495 | ViewCellContainer &viewcells)
|
---|
[466] | 3496 | {
|
---|
[469] | 3497 | return mKdTree->CastLineSegment(origin, termination, viewcells);
|
---|
[466] | 3498 | }
|
---|
| 3499 |
|
---|
[469] | 3500 |
|
---|
[503] | 3501 | void KdViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
| 3502 | {
|
---|
[551] | 3503 | // TODO
|
---|
[503] | 3504 | }
|
---|
| 3505 |
|
---|
[580] | 3506 |
|
---|
| 3507 |
|
---|
| 3508 | void KdViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 3509 | vector<MergeCandidate> &candidates)
|
---|
| 3510 | {
|
---|
| 3511 | // TODO
|
---|
| 3512 | }
|
---|
| 3513 |
|
---|
[1021] | 3514 |
|
---|
| 3515 |
|
---|
[503] | 3516 | /**************************************************************************/
|
---|
| 3517 | /* VspBspViewCellsManager implementation */
|
---|
| 3518 | /**************************************************************************/
|
---|
[482] | 3519 |
|
---|
[503] | 3520 |
|
---|
[1264] | 3521 | VspBspViewCellsManager::VspBspViewCellsManager(ViewCellsTree *vcTree, VspBspTree *vspBspTree):
|
---|
| 3522 | ViewCellsManager(vcTree), mVspBspTree(vspBspTree)
|
---|
[442] | 3523 | {
|
---|
[1004] | 3524 | Environment::GetSingleton()->GetIntValue("VspBspTree.Construction.samples", mInitialSamples);
|
---|
[478] | 3525 | mVspBspTree->SetViewCellsManager(this);
|
---|
[590] | 3526 | mVspBspTree->mViewCellsTree = mViewCellsTree;
|
---|
[442] | 3527 | }
|
---|
| 3528 |
|
---|
[477] | 3529 |
|
---|
[475] | 3530 | VspBspViewCellsManager::~VspBspViewCellsManager()
|
---|
| 3531 | {
|
---|
| 3532 | }
|
---|
| 3533 |
|
---|
[477] | 3534 |
|
---|
[471] | 3535 | float VspBspViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
[468] | 3536 | {
|
---|
[551] | 3537 | if (0 && mVspBspTree->mUseAreaForPvs)
|
---|
[547] | 3538 | return GetArea(viewCell) / GetAccVcArea();
|
---|
| 3539 | else
|
---|
| 3540 | return GetVolume(viewCell) / mViewSpaceBox.GetVolume();
|
---|
[468] | 3541 | }
|
---|
| 3542 |
|
---|
[477] | 3543 |
|
---|
[480] | 3544 | void VspBspViewCellsManager::CollectViewCells()
|
---|
| 3545 | {
|
---|
[581] | 3546 | // view cells tree constructed
|
---|
| 3547 | if (!ViewCellsTreeConstructed())
|
---|
| 3548 | {
|
---|
[590] | 3549 | mVspBspTree->CollectViewCells(mViewCells, false);
|
---|
[581] | 3550 | }
|
---|
| 3551 | else
|
---|
[1264] | 3552 | {
|
---|
| 3553 | // we can use the view cells tree hierarchy to get the right set
|
---|
[600] | 3554 | mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
|
---|
[581] | 3555 | }
|
---|
[480] | 3556 | }
|
---|
| 3557 |
|
---|
| 3558 |
|
---|
[1021] | 3559 | void VspBspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
| 3560 | vector<MergeCandidate> &candidates)
|
---|
| 3561 | {
|
---|
| 3562 | cout << "collecting merge candidates ... " << endl;
|
---|
| 3563 |
|
---|
| 3564 | if (mUseRaysForMerge)
|
---|
| 3565 | {
|
---|
| 3566 | mVspBspTree->CollectMergeCandidates(rays, candidates);
|
---|
| 3567 | }
|
---|
| 3568 | else
|
---|
| 3569 | {
|
---|
| 3570 | vector<BspLeaf *> leaves;
|
---|
| 3571 | mVspBspTree->CollectLeaves(leaves);
|
---|
| 3572 |
|
---|
| 3573 | mVspBspTree->CollectMergeCandidates(leaves, candidates);
|
---|
| 3574 | }
|
---|
| 3575 |
|
---|
| 3576 | cout << "fininshed collecting candidates" << endl;
|
---|
| 3577 | }
|
---|
| 3578 |
|
---|
| 3579 |
|
---|
[442] | 3580 | bool VspBspViewCellsManager::ViewCellsConstructed() const
|
---|
| 3581 | {
|
---|
| 3582 | return mVspBspTree->GetRoot() != NULL;
|
---|
| 3583 | }
|
---|
| 3584 |
|
---|
[462] | 3585 |
|
---|
[442] | 3586 | ViewCell *VspBspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
| 3587 | {
|
---|
| 3588 | return new BspViewCell(mesh);
|
---|
| 3589 | }
|
---|
| 3590 |
|
---|
[569] | 3591 |
|
---|
[574] | 3592 | int VspBspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
[600] | 3593 | const VssRayContainer &rays)
|
---|
[442] | 3594 | {
|
---|
[710] | 3595 | mMaxPvsSize = (int)(mMaxPvsRatio * (float)objects.size());
|
---|
| 3596 |
|
---|
[503] | 3597 | // if view cells were already constructed
|
---|
| 3598 | if (ViewCellsConstructed())
|
---|
| 3599 | return 0;
|
---|
[442] | 3600 |
|
---|
[503] | 3601 | int sampleContributions = 0;
|
---|
| 3602 |
|
---|
| 3603 | VssRayContainer sampleRays;
|
---|
| 3604 |
|
---|
[574] | 3605 | int limit = min (mInitialSamples, (int)rays.size());
|
---|
[503] | 3606 |
|
---|
[469] | 3607 | VssRayContainer constructionRays;
|
---|
| 3608 | VssRayContainer savedRays;
|
---|
[442] | 3609 |
|
---|
[660] | 3610 | Debug << "samples used for vsp bsp subdivision: " << mInitialSamples
|
---|
[664] | 3611 | << ", actual rays: " << (int)rays.size() << endl;
|
---|
[587] | 3612 |
|
---|
[574] | 3613 | GetRaySets(rays, mInitialSamples, constructionRays, &savedRays);
|
---|
[487] | 3614 |
|
---|
[574] | 3615 | Debug << "initial rays: " << (int)constructionRays.size() << endl;
|
---|
[508] | 3616 | Debug << "saved rays: " << (int)savedRays.size() << endl;
|
---|
[485] | 3617 |
|
---|
[1002] | 3618 | long startTime;
|
---|
[564] | 3619 |
|
---|
[667] | 3620 | if (1)
|
---|
[1221] | 3621 | {
|
---|
[667] | 3622 | mVspBspTree->Construct(constructionRays, &mViewSpaceBox);
|
---|
[1221] | 3623 | }
|
---|
[666] | 3624 | else
|
---|
[1221] | 3625 | {
|
---|
[667] | 3626 | mVspBspTree->Construct(rays, &mViewSpaceBox);
|
---|
[1221] | 3627 | }
|
---|
[667] | 3628 |
|
---|
[490] | 3629 | // collapse invalid regions
|
---|
[491] | 3630 | cout << "collapsing invalid tree regions ... ";
|
---|
[1002] | 3631 | startTime = GetTime();
|
---|
| 3632 | const int collapsedLeaves = mVspBspTree->CollapseTree();
|
---|
[587] | 3633 | Debug << "collapsed in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
| 3634 | << " seconds" << endl;
|
---|
| 3635 |
|
---|
[495] | 3636 | cout << "finished" << endl;
|
---|
[503] | 3637 |
|
---|
[1221] | 3638 | //-- stats after construction
|
---|
| 3639 |
|
---|
[660] | 3640 | Debug << mVspBspTree->GetStatistics() << endl;
|
---|
| 3641 |
|
---|
[480] | 3642 | ResetViewCells();
|
---|
[660] | 3643 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
[503] | 3644 |
|
---|
[491] | 3645 |
|
---|
[495] | 3646 | startTime = GetTime();
|
---|
[503] | 3647 |
|
---|
[491] | 3648 | cout << "Computing remaining ray contributions ... ";
|
---|
[605] | 3649 |
|
---|
[469] | 3650 | // recast rest of rays
|
---|
[605] | 3651 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 3652 | ComputeSampleContributions(savedRays, true, false);
|
---|
[752] | 3653 |
|
---|
[491] | 3654 | cout << "finished" << endl;
|
---|
| 3655 |
|
---|
[503] | 3656 | Debug << "Computed remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
[485] | 3657 | << " secs" << endl;
|
---|
[442] | 3658 |
|
---|
[485] | 3659 | cout << "construction finished" << endl;
|
---|
[503] | 3660 |
|
---|
[1077] | 3661 |
|
---|
[719] | 3662 | if (0)
|
---|
| 3663 | {
|
---|
[1077] | 3664 | //-- real meshes are contructed at this stage
|
---|
[719] | 3665 | cout << "finalizing view cells ... ";
|
---|
| 3666 | FinalizeViewCells(true);
|
---|
| 3667 | cout << "finished" << endl;
|
---|
| 3668 | }
|
---|
| 3669 |
|
---|
[442] | 3670 | return sampleContributions;
|
---|
| 3671 | }
|
---|
| 3672 |
|
---|
[489] | 3673 |
|
---|
[503] | 3674 | void VspBspViewCellsManager::MergeViewCells(const VssRayContainer &rays,
|
---|
| 3675 | const ObjectContainer &objects)
|
---|
[475] | 3676 | {
|
---|
[442] | 3677 | int vcSize = 0;
|
---|
| 3678 | int pvsSize = 0;
|
---|
| 3679 |
|
---|
[650] | 3680 | //-- merge view cells
|
---|
| 3681 | cout << "starting merge using " << mPostProcessSamples << " samples ... " << endl;
|
---|
| 3682 | long startTime = GetTime();
|
---|
| 3683 |
|
---|
| 3684 |
|
---|
[597] | 3685 | if (mMergeViewCells)
|
---|
| 3686 | {
|
---|
| 3687 | // TODO: should be done BEFORE the ray casting
|
---|
[710] | 3688 | // compute tree by merging the nodes based on cost heuristics
|
---|
[650] | 3689 | mViewCellsTree->ConstructMergeTree(rays, objects);
|
---|
| 3690 | }
|
---|
| 3691 | else
|
---|
| 3692 | {
|
---|
[710] | 3693 | // compute tree by merging the nodes of the spatial hierarchy
|
---|
| 3694 | ViewCell *root = ConstructSpatialMergeTree(mVspBspTree->GetRoot());
|
---|
| 3695 | mViewCellsTree->SetRoot(root);
|
---|
[752] | 3696 |
|
---|
| 3697 | // compute pvs
|
---|
| 3698 | ObjectPvs pvs;
|
---|
| 3699 | UpdatePvsForEvaluation(root, pvs);
|
---|
[650] | 3700 | }
|
---|
[442] | 3701 |
|
---|
[666] | 3702 | if (1)
|
---|
| 3703 | {
|
---|
| 3704 | char mstats[100];
|
---|
[752] | 3705 | ObjectPvs pvs;
|
---|
| 3706 |
|
---|
[1004] | 3707 | Environment::GetSingleton()->GetStringValue("ViewCells.mergeStats", mstats);
|
---|
[666] | 3708 | mViewCellsTree->ExportStats(mstats);
|
---|
| 3709 | }
|
---|
[485] | 3710 |
|
---|
[650] | 3711 | //-- stats and visualizations
|
---|
| 3712 | cout << "finished merging" << endl;
|
---|
| 3713 | cout << "merged view cells in "
|
---|
| 3714 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
[503] | 3715 |
|
---|
[650] | 3716 | Debug << "Postprocessing: Merged view cells in "
|
---|
| 3717 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
|
---|
| 3718 |
|
---|
| 3719 |
|
---|
[600] | 3720 | int savedColorCode = mColorCode;
|
---|
| 3721 |
|
---|
[660] | 3722 | // get currently active view cell set
|
---|
| 3723 | ResetViewCells();
|
---|
| 3724 | Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
|
---|
| 3725 |
|
---|
[485] | 3726 | //BspLeaf::NewMail();
|
---|
[667] | 3727 | if (1) // export merged view cells
|
---|
[483] | 3728 | {
|
---|
[600] | 3729 | mColorCode = 0;
|
---|
[744] | 3730 | Exporter *exporter = Exporter::GetExporter("merged_view_cells.wrl");
|
---|
[660] | 3731 |
|
---|
[485] | 3732 | cout << "exporting view cells after merge ... ";
|
---|
| 3733 |
|
---|
| 3734 | if (exporter)
|
---|
[483] | 3735 | {
|
---|
[600] | 3736 | if (0)
|
---|
| 3737 | exporter->SetWireframe();
|
---|
| 3738 | else
|
---|
| 3739 | exporter->SetFilled();
|
---|
| 3740 |
|
---|
| 3741 | ExportViewCellsForViz(exporter);
|
---|
| 3742 |
|
---|
[485] | 3743 | if (mExportGeometry)
|
---|
[483] | 3744 | {
|
---|
[485] | 3745 | Material m;
|
---|
| 3746 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 3747 | exporter->SetForcedMaterial(m);
|
---|
| 3748 | exporter->SetFilled();
|
---|
[442] | 3749 |
|
---|
[485] | 3750 | exporter->ExportGeometry(objects);
|
---|
[442] | 3751 | }
|
---|
[485] | 3752 |
|
---|
[600] | 3753 | delete exporter;
|
---|
| 3754 | }
|
---|
| 3755 | cout << "finished" << endl;
|
---|
| 3756 | }
|
---|
| 3757 |
|
---|
[1404] | 3758 | if (1)
|
---|
[600] | 3759 | {
|
---|
[1176] | 3760 | mColorCode = 1; // export merged view cells using pvs coding
|
---|
[744] | 3761 | Exporter *exporter = Exporter::GetExporter("merged_view_cells_pvs.wrl");
|
---|
[1176] | 3762 |
|
---|
[600] | 3763 | cout << "exporting view cells after merge (pvs size) ... ";
|
---|
| 3764 |
|
---|
| 3765 | if (exporter)
|
---|
| 3766 | {
|
---|
[598] | 3767 | if (0)
|
---|
| 3768 | exporter->SetWireframe();
|
---|
| 3769 | else
|
---|
| 3770 | exporter->SetFilled();
|
---|
| 3771 |
|
---|
| 3772 | ExportViewCellsForViz(exporter);
|
---|
| 3773 |
|
---|
[600] | 3774 | if (mExportGeometry)
|
---|
| 3775 | {
|
---|
| 3776 | Material m;
|
---|
| 3777 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 3778 | exporter->SetForcedMaterial(m);
|
---|
| 3779 | exporter->SetFilled();
|
---|
| 3780 |
|
---|
| 3781 | exporter->ExportGeometry(objects);
|
---|
| 3782 | }
|
---|
| 3783 |
|
---|
[485] | 3784 | delete exporter;
|
---|
[442] | 3785 | }
|
---|
[485] | 3786 | cout << "finished" << endl;
|
---|
[442] | 3787 | }
|
---|
[600] | 3788 |
|
---|
| 3789 | mColorCode = savedColorCode;
|
---|
| 3790 |
|
---|
[503] | 3791 | }
|
---|
[475] | 3792 |
|
---|
[503] | 3793 |
|
---|
[609] | 3794 | bool VspBspViewCellsManager::EqualToSpatialNode(ViewCell *viewCell) const
|
---|
[607] | 3795 | {
|
---|
[609] | 3796 | return GetSpatialNode(viewCell) != NULL;
|
---|
[607] | 3797 | }
|
---|
| 3798 |
|
---|
| 3799 |
|
---|
[609] | 3800 | BspNode *VspBspViewCellsManager::GetSpatialNode(ViewCell *viewCell) const
|
---|
[607] | 3801 | {
|
---|
| 3802 | if (!viewCell->IsLeaf())
|
---|
| 3803 | {
|
---|
| 3804 | BspViewCell *bspVc = dynamic_cast<BspViewCell *>(viewCell);
|
---|
| 3805 |
|
---|
[609] | 3806 | return bspVc->mLeaf;
|
---|
| 3807 | }
|
---|
[607] | 3808 | else
|
---|
| 3809 | {
|
---|
| 3810 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(viewCell);
|
---|
| 3811 |
|
---|
| 3812 | // cannot be node of binary tree
|
---|
| 3813 | if (interior->mChildren.size() != 2)
|
---|
[609] | 3814 | return NULL;
|
---|
[607] | 3815 |
|
---|
| 3816 | ViewCell *left = interior->mChildren[0];
|
---|
| 3817 | ViewCell *right = interior->mChildren[1];
|
---|
| 3818 |
|
---|
[609] | 3819 | BspNode *leftNode = GetSpatialNode(left);
|
---|
| 3820 | BspNode *rightNode = GetSpatialNode(right);
|
---|
[607] | 3821 |
|
---|
[609] | 3822 | if (leftNode && rightNode && leftNode->IsSibling(rightNode))
|
---|
[607] | 3823 | {
|
---|
[609] | 3824 | return leftNode->GetParent();
|
---|
[607] | 3825 | }
|
---|
[609] | 3826 | }
|
---|
[607] | 3827 |
|
---|
[609] | 3828 | return NULL;
|
---|
[607] | 3829 | }
|
---|
| 3830 |
|
---|
| 3831 |
|
---|
[564] | 3832 | void VspBspViewCellsManager::RefineViewCells(const VssRayContainer &rays,
|
---|
| 3833 | const ObjectContainer &objects)
|
---|
[503] | 3834 | {
|
---|
[485] | 3835 | Debug << "render time before refine:" << endl;
|
---|
| 3836 | mRenderer->RenderScene();
|
---|
[503] | 3837 | SimulationStatistics ss;
|
---|
[485] | 3838 | dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
| 3839 | Debug << ss << endl;
|
---|
| 3840 |
|
---|
[489] | 3841 | cout << "Refining the merged view cells ... ";
|
---|
[503] | 3842 | long startTime = GetTime();
|
---|
[485] | 3843 |
|
---|
| 3844 | // refining the merged view cells
|
---|
[580] | 3845 | const int refined = mViewCellsTree->RefineViewCells(rays, objects);
|
---|
[485] | 3846 |
|
---|
[442] | 3847 | //-- stats and visualizations
|
---|
| 3848 | cout << "finished" << endl;
|
---|
[485] | 3849 | cout << "refined " << refined << " view cells in "
|
---|
[442] | 3850 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
| 3851 |
|
---|
[485] | 3852 | Debug << "Postprocessing: refined " << refined << " view cells in "
|
---|
[475] | 3853 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl << endl;
|
---|
[503] | 3854 | }
|
---|
[495] | 3855 |
|
---|
[517] | 3856 |
|
---|
[503] | 3857 | int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
| 3858 | const VssRayContainer &rays)
|
---|
[840] | 3859 | {
|
---|
[503] | 3860 | if (!ViewCellsConstructed())
|
---|
| 3861 | {
|
---|
[517] | 3862 | Debug << "postprocess error: no view cells constructed" << endl;
|
---|
[503] | 3863 | return 0;
|
---|
| 3864 | }
|
---|
[520] | 3865 |
|
---|
[564] | 3866 |
|
---|
[609] | 3867 | // view cells already finished before post processing step
|
---|
| 3868 | // (i.e. because they were loaded)
|
---|
[520] | 3869 | if (mViewCellsFinished)
|
---|
[542] | 3870 | {
|
---|
[551] | 3871 | FinalizeViewCells(true);
|
---|
[542] | 3872 | EvaluateViewCellsStats();
|
---|
[577] | 3873 |
|
---|
[517] | 3874 | return 0;
|
---|
[542] | 3875 | }
|
---|
[448] | 3876 |
|
---|
[710] | 3877 | // check if new view cells turned invalid
|
---|
| 3878 | int minPvs, maxPvs;
|
---|
[564] | 3879 |
|
---|
[710] | 3880 | if (0)
|
---|
| 3881 | {
|
---|
| 3882 | minPvs = mMinPvsSize;
|
---|
| 3883 | maxPvs = mMaxPvsSize;
|
---|
| 3884 | }
|
---|
| 3885 | else
|
---|
| 3886 | {
|
---|
| 3887 | minPvs = mPruneEmptyViewCells ? 1 : 0;
|
---|
| 3888 | maxPvs = mMaxPvsSize;
|
---|
| 3889 | }
|
---|
[720] | 3890 |
|
---|
[710] | 3891 | Debug << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
| 3892 | cout << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
| 3893 |
|
---|
| 3894 | SetValidity(minPvs, maxPvs);
|
---|
| 3895 |
|
---|
[574] | 3896 | // update valid view space according to valid view cells
|
---|
[720] | 3897 | if (0) mVspBspTree->ValidateTree();
|
---|
[542] | 3898 |
|
---|
[720] | 3899 | // area has to be recomputed
|
---|
[574] | 3900 | mTotalAreaValid = false;
|
---|
[517] | 3901 | VssRayContainer postProcessRays;
|
---|
[605] | 3902 | GetRaySets(rays, mPostProcessSamples, postProcessRays);
|
---|
| 3903 |
|
---|
[517] | 3904 | Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
|
---|
[564] | 3905 |
|
---|
[517] | 3906 |
|
---|
[552] | 3907 | // should maybe be done here to allow merge working with area or volume
|
---|
| 3908 | // and to correct the rendering statistics
|
---|
[719] | 3909 | if (0) FinalizeViewCells(false);
|
---|
[720] | 3910 |
|
---|
[597] | 3911 | //-- merge the individual view cells
|
---|
| 3912 | MergeViewCells(postProcessRays, objects);
|
---|
[591] | 3913 |
|
---|
[720] | 3914 |
|
---|
| 3915 | // only for debugging purpose: test if the subdivision is valid
|
---|
[1122] | 3916 | if (0) TestSubdivision();
|
---|
[693] | 3917 |
|
---|
[597] | 3918 | //-- refines the merged view cells
|
---|
[720] | 3919 | if (0) RefineViewCells(postProcessRays, objects);
|
---|
[568] | 3920 |
|
---|
[719] | 3921 |
|
---|
[660] | 3922 | //-- render simulation after merge + refine
|
---|
[605] | 3923 | cout << "\nevaluating bsp view cells render time before compress ... ";
|
---|
| 3924 | dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
|
---|
| 3925 | SimulationStatistics ss;
|
---|
| 3926 | dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
[600] | 3927 |
|
---|
[580] | 3928 |
|
---|
[605] | 3929 | cout << " finished" << endl;
|
---|
| 3930 | cout << ss << endl;
|
---|
| 3931 | Debug << ss << endl;
|
---|
[600] | 3932 |
|
---|
| 3933 |
|
---|
| 3934 | //-- compression
|
---|
| 3935 | if (ViewCellsTreeConstructed() && mCompressViewCells)
|
---|
| 3936 | {
|
---|
[1161] | 3937 | int pvsEntries = mViewCellsTree->GetStoredPvsEntriesNum(mViewCellsTree->GetRoot());
|
---|
[600] | 3938 | Debug << "number of entries before compress: " << pvsEntries << endl;
|
---|
| 3939 |
|
---|
[752] | 3940 | mViewCellsTree->SetViewCellsStorage(ViewCellsTree::COMPRESSED);
|
---|
[600] | 3941 |
|
---|
[1161] | 3942 | pvsEntries = mViewCellsTree->GetStoredPvsEntriesNum(mViewCellsTree->GetRoot());
|
---|
[600] | 3943 | Debug << "number of entries after compress: " << pvsEntries << endl;
|
---|
| 3944 | }
|
---|
| 3945 |
|
---|
[660] | 3946 |
|
---|
[517] | 3947 | // collapse sibling leaves that share the same view cell
|
---|
[840] | 3948 | if (0) mVspBspTree->CollapseTree();
|
---|
[586] | 3949 |
|
---|
[574] | 3950 | // recompute view cell list and statistics
|
---|
[520] | 3951 | ResetViewCells();
|
---|
| 3952 |
|
---|
[719] | 3953 | // compute final meshes and volume / area
|
---|
| 3954 | if (1) FinalizeViewCells(true);
|
---|
[517] | 3955 |
|
---|
[508] | 3956 | // write view cells to disc
|
---|
| 3957 | if (mExportViewCells)
|
---|
| 3958 | {
|
---|
[840] | 3959 | char filename[100];
|
---|
[1004] | 3960 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
|
---|
[931] | 3961 | ExportViewCells(filename, mExportPvs, objects);
|
---|
[840] | 3962 | }
|
---|
[517] | 3963 |
|
---|
[840] | 3964 |
|
---|
| 3965 | // export bounding boxes
|
---|
| 3966 | if (mExportBboxesForPvs)
|
---|
| 3967 | {
|
---|
| 3968 | char filename[100];
|
---|
[1004] | 3969 | Environment::GetSingleton()->GetStringValue("ViewCells.boxesFilename", filename);
|
---|
[850] | 3970 |
|
---|
[840] | 3971 | ExportBoundingBoxes(filename, objects);
|
---|
[508] | 3972 | }
|
---|
[503] | 3973 |
|
---|
| 3974 | return 0;
|
---|
[442] | 3975 | }
|
---|
| 3976 |
|
---|
[490] | 3977 |
|
---|
[442] | 3978 | int VspBspViewCellsManager::GetType() const
|
---|
| 3979 | {
|
---|
| 3980 | return VSP_BSP;
|
---|
| 3981 | }
|
---|
| 3982 |
|
---|
| 3983 |
|
---|
[650] | 3984 | ViewCell *VspBspViewCellsManager::ConstructSpatialMergeTree(BspNode *root)
|
---|
[609] | 3985 | {
|
---|
[752] | 3986 | // terminate recursion
|
---|
[650] | 3987 | if (root->IsLeaf())
|
---|
| 3988 | {
|
---|
[752] | 3989 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(root);
|
---|
| 3990 | leaf->GetViewCell()->SetMergeCost(0.0f);
|
---|
| 3991 | return leaf->GetViewCell();
|
---|
[650] | 3992 | }
|
---|
| 3993 |
|
---|
[752] | 3994 |
|
---|
[650] | 3995 | BspInterior *interior = dynamic_cast<BspInterior *>(root);
|
---|
| 3996 | ViewCellInterior *viewCellInterior = new ViewCellInterior();
|
---|
| 3997 |
|
---|
[752] | 3998 | // evaluate merge cost for priority traversal
|
---|
[650] | 3999 | float mergeCost = 1.0f / (float)root->mTimeStamp;
|
---|
| 4000 | viewCellInterior->SetMergeCost(mergeCost);
|
---|
[610] | 4001 |
|
---|
[650] | 4002 | float volume = 0;
|
---|
| 4003 |
|
---|
| 4004 | BspNode *front = interior->GetFront();
|
---|
| 4005 | BspNode *back = interior->GetBack();
|
---|
[649] | 4006 |
|
---|
[752] | 4007 |
|
---|
| 4008 | ObjectPvs frontPvs, backPvs;
|
---|
| 4009 |
|
---|
| 4010 | //-- recursivly compute child hierarchies
|
---|
[650] | 4011 | ViewCell *backVc = ConstructSpatialMergeTree(back);
|
---|
| 4012 | ViewCell *frontVc = ConstructSpatialMergeTree(front);
|
---|
[610] | 4013 |
|
---|
[752] | 4014 |
|
---|
| 4015 | viewCellInterior->SetupChildLink(backVc);
|
---|
[650] | 4016 | viewCellInterior->SetupChildLink(frontVc);
|
---|
| 4017 |
|
---|
| 4018 | volume += backVc->GetVolume();
|
---|
| 4019 | volume += frontVc->GetVolume();
|
---|
| 4020 |
|
---|
| 4021 | viewCellInterior->SetVolume(volume);
|
---|
| 4022 |
|
---|
| 4023 | return viewCellInterior;
|
---|
[609] | 4024 | }
|
---|
| 4025 |
|
---|
| 4026 |
|
---|
[490] | 4027 | bool VspBspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
|
---|
| 4028 | {
|
---|
| 4029 | if (!ViewCellsConstructed())
|
---|
| 4030 | return ViewCellsManager::GetViewPoint(viewPoint);
|
---|
| 4031 |
|
---|
| 4032 | // TODO: set reasonable limit
|
---|
| 4033 | const int limit = 20;
|
---|
| 4034 |
|
---|
| 4035 | for (int i = 0; i < limit; ++ i)
|
---|
| 4036 | {
|
---|
[542] | 4037 | viewPoint = mViewSpaceBox.GetRandomPoint();
|
---|
[490] | 4038 | if (mVspBspTree->ViewPointValid(viewPoint))
|
---|
| 4039 | {
|
---|
| 4040 | return true;
|
---|
| 4041 | }
|
---|
| 4042 | }
|
---|
[997] | 4043 |
|
---|
[490] | 4044 | Debug << "failed to find valid view point, taking " << viewPoint << endl;
|
---|
| 4045 | return false;
|
---|
| 4046 | }
|
---|
| 4047 |
|
---|
| 4048 |
|
---|
| 4049 | bool VspBspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
|
---|
| 4050 | {
|
---|
[569] | 4051 | // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
|
---|
| 4052 | // validy update in preprocessor for all managers)
|
---|
| 4053 | return ViewCellsManager::ViewPointValid(viewPoint);
|
---|
[570] | 4054 |
|
---|
[569] | 4055 | // return mViewSpaceBox.IsInside(viewPoint) &&
|
---|
| 4056 | // mVspBspTree->ViewPointValid(viewPoint);
|
---|
[490] | 4057 | }
|
---|
| 4058 |
|
---|
| 4059 |
|
---|
[442] | 4060 | void VspBspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
[466] | 4061 | const VssRayContainer &sampleRays)
|
---|
[442] | 4062 | {
|
---|
| 4063 | if (!ViewCellsConstructed())
|
---|
| 4064 | return;
|
---|
| 4065 |
|
---|
[485] | 4066 | VssRayContainer visRays;
|
---|
| 4067 | GetRaySets(sampleRays, mVisualizationSamples, visRays);
|
---|
[483] | 4068 |
|
---|
[1077] | 4069 | //-- export final view cell partition
|
---|
| 4070 |
|
---|
| 4071 | if (0)
|
---|
[1047] | 4072 | {
|
---|
| 4073 | // hack pvs
|
---|
[1052] | 4074 | //const int savedColorCode = mColorCode;
|
---|
| 4075 | //mColorCode = 1;
|
---|
[801] | 4076 |
|
---|
[1020] | 4077 | Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
|
---|
[600] | 4078 |
|
---|
[442] | 4079 | if (exporter)
|
---|
| 4080 | {
|
---|
[580] | 4081 | cout << "exporting view cells after post process ... ";
|
---|
| 4082 |
|
---|
[600] | 4083 | if (0)
|
---|
[542] | 4084 | {
|
---|
[1004] | 4085 | // export view space box
|
---|
[542] | 4086 | exporter->SetWireframe();
|
---|
| 4087 | exporter->ExportBox(mViewSpaceBox);
|
---|
| 4088 | exporter->SetFilled();
|
---|
| 4089 | }
|
---|
| 4090 |
|
---|
[1052] | 4091 | Material m;
|
---|
| 4092 |
|
---|
| 4093 | m.mDiffuseColor.r = 0.0f;
|
---|
| 4094 | m.mDiffuseColor.g = 0.5f;
|
---|
| 4095 | m.mDiffuseColor.b = 0.5f;
|
---|
| 4096 |
|
---|
| 4097 | exporter->SetForcedMaterial(m);
|
---|
| 4098 |
|
---|
| 4099 | if (0 && mExportGeometry)
|
---|
[542] | 4100 | {
|
---|
[485] | 4101 | exporter->ExportGeometry(objects);
|
---|
[542] | 4102 | }
|
---|
[489] | 4103 |
|
---|
| 4104 | // export rays
|
---|
[1047] | 4105 | if (0 && mExportRays)
|
---|
[489] | 4106 | {
|
---|
[1052] | 4107 | exporter->ExportRays(visRays, RgbColor(1, 0, 0));
|
---|
[490] | 4108 | }
|
---|
[564] | 4109 |
|
---|
[801] | 4110 | //exporter->SetFilled();
|
---|
[1020] | 4111 |
|
---|
[1006] | 4112 | // HACK: export without clip plane
|
---|
| 4113 | const bool b = mUseClipPlaneForViz;
|
---|
[1052] | 4114 | //mUseClipPlaneForViz = false;
|
---|
[1020] | 4115 |
|
---|
[508] | 4116 | ExportViewCellsForViz(exporter);
|
---|
[1020] | 4117 |
|
---|
[801] | 4118 | mUseClipPlaneForViz = b;
|
---|
[442] | 4119 | delete exporter;
|
---|
[1020] | 4120 |
|
---|
[580] | 4121 | cout << "finished" << endl;
|
---|
[442] | 4122 | }
|
---|
[600] | 4123 |
|
---|
[1052] | 4124 | //mColorCode = savedColorCode;
|
---|
[557] | 4125 | }
|
---|
[503] | 4126 |
|
---|
[1002] | 4127 |
|
---|
[600] | 4128 | if (0)
|
---|
[577] | 4129 | {
|
---|
| 4130 | cout << "exporting depth map ... ";
|
---|
| 4131 |
|
---|
| 4132 | Exporter *exporter = Exporter::GetExporter("depth_map.x3d");
|
---|
| 4133 | if (exporter)
|
---|
| 4134 | {
|
---|
| 4135 | if (1)
|
---|
| 4136 | {
|
---|
| 4137 | exporter->SetWireframe();
|
---|
| 4138 | exporter->ExportBox(mViewSpaceBox);
|
---|
| 4139 | exporter->SetFilled();
|
---|
| 4140 | }
|
---|
| 4141 |
|
---|
| 4142 | if (mExportGeometry)
|
---|
| 4143 | {
|
---|
| 4144 | exporter->ExportGeometry(objects);
|
---|
| 4145 | }
|
---|
| 4146 |
|
---|
[1143] | 4147 | const int maxDepth = mVspBspTree->GetStatistics().maxDepth;
|
---|
[577] | 4148 |
|
---|
| 4149 | ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
|
---|
| 4150 |
|
---|
| 4151 | for (vit = mViewCells.begin(); vit != mViewCells.end(); ++ vit)
|
---|
| 4152 | {
|
---|
[582] | 4153 | ViewCell *vc = *vit;
|
---|
[577] | 4154 |
|
---|
[580] | 4155 | ViewCellContainer leaves;
|
---|
| 4156 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
[577] | 4157 |
|
---|
[580] | 4158 | ViewCellContainer::const_iterator lit, lit_end = leaves.end();
|
---|
| 4159 |
|
---|
| 4160 | for (lit = leaves.begin(); lit != lit_end; ++ lit)
|
---|
[577] | 4161 | {
|
---|
[580] | 4162 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*lit)->mLeaf;
|
---|
[577] | 4163 |
|
---|
| 4164 | Material m;
|
---|
| 4165 |
|
---|
| 4166 | float relDepth = (float)leaf->GetDepth() / (float)maxDepth;
|
---|
| 4167 | m.mDiffuseColor.r = relDepth;
|
---|
| 4168 | m.mDiffuseColor.g = 0.0f;
|
---|
| 4169 | m.mDiffuseColor.b = 1.0f - relDepth;
|
---|
| 4170 |
|
---|
| 4171 | exporter->SetForcedMaterial(m);
|
---|
| 4172 |
|
---|
| 4173 |
|
---|
| 4174 | BspNodeGeometry geom;
|
---|
| 4175 | mVspBspTree->ConstructGeometry(leaf, geom);
|
---|
[678] | 4176 | exporter->ExportPolygons(geom.GetPolys());
|
---|
[577] | 4177 | }
|
---|
| 4178 | }
|
---|
| 4179 |
|
---|
| 4180 | delete exporter;
|
---|
| 4181 | }
|
---|
| 4182 |
|
---|
| 4183 |
|
---|
| 4184 | cout << "finished" << endl;
|
---|
| 4185 | }
|
---|
| 4186 |
|
---|
[442] | 4187 | //-- visualization of the BSP splits
|
---|
| 4188 | bool exportSplits = false;
|
---|
[1004] | 4189 | Environment::GetSingleton()->GetBoolValue("VspBspTree.Visualization.exportSplits", exportSplits);
|
---|
[503] | 4190 |
|
---|
[442] | 4191 | if (exportSplits)
|
---|
| 4192 | {
|
---|
| 4193 | cout << "exporting splits ... ";
|
---|
[485] | 4194 | ExportSplits(objects, visRays);
|
---|
[442] | 4195 | cout << "finished" << endl;
|
---|
| 4196 | }
|
---|
| 4197 |
|
---|
[542] | 4198 | //-- export single view cells
|
---|
[485] | 4199 | ExportBspPvs(objects, visRays);
|
---|
[442] | 4200 | }
|
---|
| 4201 |
|
---|
[503] | 4202 |
|
---|
[485] | 4203 | void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects,
|
---|
| 4204 | const VssRayContainer &rays)
|
---|
[442] | 4205 | {
|
---|
| 4206 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
| 4207 |
|
---|
[503] | 4208 | if (exporter)
|
---|
| 4209 | {
|
---|
| 4210 | Material m;
|
---|
[442] | 4211 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 4212 | exporter->SetForcedMaterial(m);
|
---|
| 4213 | exporter->SetWireframe();
|
---|
[503] | 4214 |
|
---|
[448] | 4215 | exporter->ExportBspSplits(*mVspBspTree, true);
|
---|
[442] | 4216 |
|
---|
| 4217 | // take forced material, else big scenes cannot be viewed
|
---|
| 4218 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 4219 | exporter->SetForcedMaterial(m);
|
---|
| 4220 | exporter->SetFilled();
|
---|
| 4221 |
|
---|
| 4222 | exporter->ResetForcedMaterial();
|
---|
[503] | 4223 |
|
---|
[442] | 4224 | // export rays
|
---|
[485] | 4225 | if (mExportRays)
|
---|
| 4226 | exporter->ExportRays(rays, RgbColor(1, 1, 0));
|
---|
[503] | 4227 |
|
---|
[485] | 4228 | if (mExportGeometry)
|
---|
[442] | 4229 | exporter->ExportGeometry(objects);
|
---|
| 4230 |
|
---|
| 4231 | delete exporter;
|
---|
| 4232 | }
|
---|
| 4233 | }
|
---|
| 4234 |
|
---|
[503] | 4235 |
|
---|
[485] | 4236 | void VspBspViewCellsManager::ExportBspPvs(const ObjectContainer &objects,
|
---|
| 4237 | const VssRayContainer &rays)
|
---|
[442] | 4238 | {
|
---|
[1020] | 4239 | int leafOut;
|
---|
| 4240 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
|
---|
[503] | 4241 |
|
---|
[442] | 4242 | ViewCell::NewMail();
|
---|
[503] | 4243 |
|
---|
[639] | 4244 | cout << "visualization using " << (int)rays.size() << " samples" << endl;
|
---|
| 4245 | Debug << "visualization using " << (int)rays.size() << " samples" << endl;
|
---|
[475] | 4246 | Debug << "\nOutput view cells: " << endl;
|
---|
[503] | 4247 |
|
---|
[694] | 4248 | const bool sortViewCells = true;
|
---|
[1021] | 4249 |
|
---|
[485] | 4250 | // sort view cells to visualize the largest view cells
|
---|
[694] | 4251 | if (sortViewCells)
|
---|
[801] | 4252 | {
|
---|
| 4253 | //stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
|
---|
| 4254 | stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::LargerRenderCost);
|
---|
| 4255 | }
|
---|
| 4256 |
|
---|
[503] | 4257 | int limit = min(leafOut, (int)mViewCells.size());
|
---|
[483] | 4258 |
|
---|
[639] | 4259 | int raysOut = 0;
|
---|
[587] | 4260 |
|
---|
[485] | 4261 | //-- some rays for output
|
---|
[478] | 4262 | for (int i = 0; i < limit; ++ i)
|
---|
[442] | 4263 | {
|
---|
[478] | 4264 | cout << "creating output for view cell " << i << " ... ";
|
---|
[503] | 4265 |
|
---|
[582] | 4266 | ViewCell *vc;
|
---|
[574] | 4267 |
|
---|
[694] | 4268 | if (sortViewCells) // largest view cell pvs first
|
---|
[582] | 4269 | vc = mViewCells[i];
|
---|
[574] | 4270 | else
|
---|
[639] | 4271 | vc = mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 1)];
|
---|
[442] | 4272 |
|
---|
[801] | 4273 | ObjectPvs pvs;
|
---|
| 4274 | mViewCellsTree->GetPvs(vc, pvs);
|
---|
| 4275 |
|
---|
[639] | 4276 | //bspLeaves[j]->Mail();
|
---|
[1020] | 4277 | char s[64]; sprintf(s, "bsp-pvs%04d.wrl", i);
|
---|
[639] | 4278 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
| 4279 |
|
---|
[1142] | 4280 | Debug << i << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc) << endl;
|
---|
[587] | 4281 |
|
---|
[801] | 4282 | //-- export the sample rays
|
---|
[1020] | 4283 | if (mExportRays)
|
---|
[442] | 4284 | {
|
---|
[694] | 4285 | // output rays stored with the view cells during subdivision
|
---|
[1047] | 4286 | if (1)
|
---|
[442] | 4287 | {
|
---|
[639] | 4288 | VssRayContainer vcRays;
|
---|
| 4289 | VssRayContainer collectRays;
|
---|
| 4290 |
|
---|
| 4291 | raysOut = min((int)rays.size(), 100);
|
---|
| 4292 |
|
---|
| 4293 | // collect intial view cells
|
---|
| 4294 | ViewCellContainer leaves;
|
---|
| 4295 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 4296 |
|
---|
| 4297 | ViewCellContainer::const_iterator vit, vit_end = leaves.end();
|
---|
| 4298 |
|
---|
| 4299 | for (vit = leaves.begin(); vit != vit_end; ++ vit)
|
---|
[574] | 4300 | {
|
---|
[639] | 4301 | BspLeaf *vcLeaf = dynamic_cast<BspViewCell *>(*vit)->mLeaf;
|
---|
| 4302 |
|
---|
| 4303 | VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
|
---|
[587] | 4304 |
|
---|
[639] | 4305 | for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 4306 | {
|
---|
| 4307 | collectRays.push_back(*rit);
|
---|
| 4308 | }
|
---|
[574] | 4309 | }
|
---|
[639] | 4310 |
|
---|
| 4311 | VssRayContainer::const_iterator rit, rit_end = collectRays.end();
|
---|
| 4312 |
|
---|
| 4313 | for (rit = collectRays.begin(); rit != rit_end; ++ rit)
|
---|
| 4314 | {
|
---|
| 4315 | float p = RandomValue(0.0f, (float)collectRays.size());
|
---|
| 4316 |
|
---|
| 4317 | if (p < raysOut)
|
---|
| 4318 | vcRays.push_back(*rit);
|
---|
| 4319 | }
|
---|
[801] | 4320 |
|
---|
[1052] | 4321 | //Debug << "#rays: " << (int)vcRays.size() << endl;
|
---|
[1047] | 4322 |
|
---|
[639] | 4323 | //-- export rays piercing this view cell
|
---|
| 4324 | exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
|
---|
[442] | 4325 | }
|
---|
[651] | 4326 |
|
---|
[694] | 4327 | // associate new rays with output view cell
|
---|
[1047] | 4328 | if (0)
|
---|
[639] | 4329 | {
|
---|
| 4330 | VssRayContainer vcRays;
|
---|
| 4331 | raysOut = min((int)rays.size(), mVisualizationSamples);
|
---|
[801] | 4332 |
|
---|
[639] | 4333 | // check whether we can add the current ray to the output rays
|
---|
| 4334 | for (int k = 0; k < raysOut; ++ k)
|
---|
| 4335 | {
|
---|
| 4336 | VssRay *ray = rays[k];
|
---|
| 4337 | for (int j = 0; j < (int)ray->mViewCells.size(); ++ j)
|
---|
| 4338 | {
|
---|
| 4339 | ViewCell *rayvc = ray->mViewCells[j];
|
---|
| 4340 |
|
---|
| 4341 | if (rayvc == vc)
|
---|
| 4342 | vcRays.push_back(ray);
|
---|
| 4343 | }
|
---|
| 4344 | }
|
---|
| 4345 |
|
---|
| 4346 | //-- export rays piercing this view cell
|
---|
| 4347 | exporter->ExportRays(vcRays, RgbColor(1, 1, 0));
|
---|
| 4348 | }
|
---|
| 4349 |
|
---|
[442] | 4350 | }
|
---|
[639] | 4351 |
|
---|
[574] | 4352 |
|
---|
[1020] | 4353 | //-- export view cell geometry
|
---|
[478] | 4354 | exporter->SetWireframe();
|
---|
[442] | 4355 |
|
---|
[478] | 4356 | Material m;//= RandomMaterial();
|
---|
| 4357 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 4358 | exporter->SetForcedMaterial(m);
|
---|
[442] | 4359 |
|
---|
[580] | 4360 | ExportViewCellGeometry(exporter, vc);
|
---|
[639] | 4361 |
|
---|
| 4362 | exporter->SetFilled();
|
---|
[503] | 4363 |
|
---|
[1020] | 4364 |
|
---|
[801] | 4365 | //-- export pvs
|
---|
[574] | 4366 | if (1)
|
---|
| 4367 | {
|
---|
[639] | 4368 | ObjectPvsMap::const_iterator oit,
|
---|
[801] | 4369 | oit_end = pvs.mEntries.end();
|
---|
[483] | 4370 |
|
---|
[801] | 4371 | Intersectable::NewMail();
|
---|
[580] | 4372 |
|
---|
[639] | 4373 | // output PVS of view cell
|
---|
[801] | 4374 | for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
|
---|
[639] | 4375 | {
|
---|
| 4376 | Intersectable *intersect = (*oit).first;
|
---|
[442] | 4377 |
|
---|
[639] | 4378 | if (!intersect->Mailed())
|
---|
| 4379 | {
|
---|
[643] | 4380 | m = RandomMaterial();
|
---|
| 4381 | exporter->SetForcedMaterial(m);
|
---|
[442] | 4382 |
|
---|
[639] | 4383 | exporter->ExportIntersectable(intersect);
|
---|
| 4384 | intersect->Mail();
|
---|
| 4385 | }
|
---|
[503] | 4386 | }
|
---|
[442] | 4387 | }
|
---|
[801] | 4388 |
|
---|
| 4389 | if (0)
|
---|
| 4390 | { // export scene geometry
|
---|
[643] | 4391 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 4392 | exporter->SetForcedMaterial(m);
|
---|
| 4393 |
|
---|
[639] | 4394 | exporter->ExportGeometry(objects);
|
---|
| 4395 | }
|
---|
[503] | 4396 |
|
---|
[478] | 4397 | DEL_PTR(exporter);
|
---|
| 4398 | cout << "finished" << endl;
|
---|
[442] | 4399 | }
|
---|
[475] | 4400 |
|
---|
| 4401 | Debug << endl;
|
---|
[442] | 4402 | }
|
---|
| 4403 |
|
---|
| 4404 |
|
---|
[1021] | 4405 | void VspBspViewCellsManager::TestFilter(const ObjectContainer &objects)
|
---|
[697] | 4406 | {
|
---|
[1021] | 4407 | Exporter *exporter = Exporter::GetExporter("filter.x3d");
|
---|
[697] | 4408 |
|
---|
[1021] | 4409 | Vector3 bsize = mViewSpaceBox.Size();
|
---|
| 4410 | const Vector3 viewPoint(mViewSpaceBox.Center());
|
---|
| 4411 | float w = Magnitude(mViewSpaceBox.Size()) * mFilterWidth;
|
---|
| 4412 | const Vector3 width = Vector3(w);
|
---|
| 4413 |
|
---|
| 4414 | PrVs testPrVs;
|
---|
| 4415 |
|
---|
| 4416 | if (exporter)
|
---|
| 4417 | {
|
---|
| 4418 | ViewCellContainer viewCells;
|
---|
| 4419 |
|
---|
| 4420 | const AxisAlignedBox3 tbox = GetFilterBBox(viewPoint, mFilterWidth);
|
---|
[697] | 4421 |
|
---|
[1021] | 4422 | GetPrVS(viewPoint, testPrVs, GetFilterWidth());
|
---|
[482] | 4423 |
|
---|
[1021] | 4424 | exporter->SetWireframe();
|
---|
[482] | 4425 |
|
---|
[1021] | 4426 | exporter->SetForcedMaterial(RgbColor(1,1,1));
|
---|
| 4427 | exporter->ExportBox(tbox);
|
---|
| 4428 |
|
---|
| 4429 | exporter->SetFilled();
|
---|
[503] | 4430 |
|
---|
[1021] | 4431 | exporter->SetForcedMaterial(RgbColor(0,1,0));
|
---|
| 4432 | ExportViewCellGeometry(exporter, GetViewCell(viewPoint));
|
---|
[564] | 4433 |
|
---|
[1021] | 4434 | //exporter->ResetForcedMaterial();
|
---|
| 4435 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 4436 | ExportViewCellGeometry(exporter, testPrVs.mViewCell);
|
---|
[591] | 4437 |
|
---|
[1021] | 4438 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
| 4439 | exporter->ExportGeometry(objects);
|
---|
[547] | 4440 |
|
---|
[1021] | 4441 | delete exporter;
|
---|
[482] | 4442 | }
|
---|
[1021] | 4443 | }
|
---|
[482] | 4444 |
|
---|
[547] | 4445 |
|
---|
[1021] | 4446 | int VspBspViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 4447 | ViewCellContainer &viewCells) const
|
---|
| 4448 | {
|
---|
| 4449 | return mVspBspTree->ComputeBoxIntersections(box, viewCells);
|
---|
[482] | 4450 | }
|
---|
| 4451 |
|
---|
| 4452 |
|
---|
[1021] | 4453 | int VspBspViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
| 4454 | const Vector3 &termination,
|
---|
| 4455 | ViewCellContainer &viewcells)
|
---|
| 4456 | {
|
---|
| 4457 | return mVspBspTree->CastLineSegment(origin, termination, viewcells);
|
---|
| 4458 | }
|
---|
| 4459 |
|
---|
| 4460 |
|
---|
[979] | 4461 | void VspBspViewCellsManager::VisualizeWithFromPointQueries()
|
---|
| 4462 | {
|
---|
| 4463 | int numSamples;
|
---|
[1161] | 4464 |
|
---|
[1004] | 4465 | Environment::GetSingleton()->GetIntValue("RenderSampler.samples", numSamples);
|
---|
[979] | 4466 | cout << "samples" << numSamples << endl;
|
---|
| 4467 |
|
---|
| 4468 | vector<RenderCostSample> samples;
|
---|
| 4469 |
|
---|
[1145] | 4470 | if (!mPreprocessor->GetRenderer())
|
---|
[979] | 4471 | return;
|
---|
| 4472 |
|
---|
[1145] | 4473 | //start the view point queries
|
---|
[979] | 4474 | long startTime = GetTime();
|
---|
| 4475 | cout << "starting sampling of render cost ... ";
|
---|
| 4476 |
|
---|
[1145] | 4477 | mPreprocessor->GetRenderer()->SampleRenderCost(numSamples, samples, true);
|
---|
[979] | 4478 |
|
---|
| 4479 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
| 4480 |
|
---|
| 4481 |
|
---|
[1161] | 4482 | // for each sample:
|
---|
| 4483 | // find view cells associated with the samples
|
---|
| 4484 | // store the sample pvs with the pvs associated with the view cell
|
---|
| 4485 | // for each view cell:
|
---|
| 4486 | // compute difference point sampled pvs - view cell pvs
|
---|
| 4487 | // export geometry with color coded pvs difference
|
---|
| 4488 |
|
---|
| 4489 | std::map<ViewCell *, ObjectPvs> sampleMap;
|
---|
| 4490 |
|
---|
[979] | 4491 | vector<RenderCostSample>::const_iterator rit, rit_end = samples.end();
|
---|
| 4492 |
|
---|
[1161] | 4493 | for (rit = samples.begin(); rit != rit_end; ++ rit)
|
---|
| 4494 | {
|
---|
| 4495 | RenderCostSample sample = *rit;
|
---|
| 4496 |
|
---|
| 4497 | ViewCell *vc = GetViewCell(sample.mPosition);
|
---|
[991] | 4498 |
|
---|
[1161] | 4499 | std::map<ViewCell *, ObjectPvs>::iterator it = sampleMap.find(vc);
|
---|
| 4500 |
|
---|
| 4501 | if (it == sampleMap.end())
|
---|
| 4502 | {
|
---|
| 4503 | sampleMap[vc] = sample.mPvs;
|
---|
| 4504 | }
|
---|
| 4505 | else
|
---|
| 4506 | {
|
---|
| 4507 | (*it).second.Merge(sample.mPvs);
|
---|
| 4508 | }
|
---|
| 4509 | }
|
---|
| 4510 |
|
---|
| 4511 | // visualize the view cells
|
---|
| 4512 | std::map<ViewCell *, ObjectPvs>::const_iterator vit, vit_end = sampleMap.end();
|
---|
| 4513 |
|
---|
| 4514 | Material m;//= RandomMaterial();
|
---|
| 4515 |
|
---|
| 4516 | for (vit = sampleMap.begin(); vit != vit_end; ++ vit)
|
---|
| 4517 | {
|
---|
| 4518 | ViewCell *vc = (*vit).first;
|
---|
| 4519 |
|
---|
| 4520 | const int pvsVc = mViewCellsTree->GetPvsEntries(vc);
|
---|
[1168] | 4521 | const int pvsPtSamples = (*vit).second.GetSize();
|
---|
[1161] | 4522 |
|
---|
[1168] | 4523 | m.mDiffuseColor.r = (float) (pvsVc - pvsPtSamples);
|
---|
[1161] | 4524 | m.mDiffuseColor.b = 1.0f;
|
---|
| 4525 | //exporter->SetForcedMaterial(m);
|
---|
| 4526 |
|
---|
| 4527 | // ExportViewCellGeometry(exporter, vc, mClipPlane);
|
---|
| 4528 |
|
---|
| 4529 | /* // counting the pvss
|
---|
[979] | 4530 | for (rit = samples.begin(); rit != rit_end; ++ rit)
|
---|
| 4531 | {
|
---|
| 4532 | RenderCostSample sample = *rit;
|
---|
[1001] | 4533 |
|
---|
[991] | 4534 | ViewCell *vc = GetViewCell(sample.mPosition);
|
---|
| 4535 |
|
---|
| 4536 | AxisAlignedBox3 box(sample.mPosition - Vector3(1, 1, 1), sample.mPosition + Vector3(1, 1, 1));
|
---|
| 4537 | Mesh *hMesh = CreateMeshFromBox(box);
|
---|
| 4538 |
|
---|
| 4539 | DEL_PTR(hMesh);
|
---|
[1161] | 4540 |
|
---|
| 4541 | */
|
---|
[979] | 4542 | }
|
---|
| 4543 | }
|
---|
| 4544 |
|
---|
| 4545 |
|
---|
[580] | 4546 | void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
[591] | 4547 | ViewCell *vc,
|
---|
[1047] | 4548 | const AxisAlignedPlane *clipPlane) const
|
---|
[482] | 4549 | {
|
---|
[660] | 4550 | if (clipPlane)
|
---|
[591] | 4551 | {
|
---|
[1047] | 4552 | const Plane3 plane = clipPlane->GetPlane();
|
---|
| 4553 |
|
---|
[591] | 4554 | ViewCellContainer leaves;
|
---|
| 4555 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 4556 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 4557 |
|
---|
| 4558 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 4559 | {
|
---|
| 4560 | BspNodeGeometry geom;
|
---|
| 4561 |
|
---|
| 4562 | BspNodeGeometry front;
|
---|
| 4563 | BspNodeGeometry back;
|
---|
| 4564 |
|
---|
| 4565 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 4566 | mVspBspTree->ConstructGeometry(leaf, geom);
|
---|
| 4567 |
|
---|
[1052] | 4568 | const float eps = 0.0001f;
|
---|
[1047] | 4569 | const int cf = geom.Side(plane, eps);
|
---|
[682] | 4570 |
|
---|
[710] | 4571 | if (cf == -1)
|
---|
[682] | 4572 | {
|
---|
| 4573 | exporter->ExportPolygons(geom.GetPolys());
|
---|
| 4574 | }
|
---|
[710] | 4575 | else if (cf == 0)
|
---|
[684] | 4576 | {
|
---|
[682] | 4577 | geom.SplitGeometry(front,
|
---|
| 4578 | back,
|
---|
[1047] | 4579 | plane,
|
---|
[682] | 4580 | mViewSpaceBox,
|
---|
[710] | 4581 | eps);
|
---|
[682] | 4582 |
|
---|
[694] | 4583 | //Debug << "geo size: " << geom.Size() << endl;
|
---|
| 4584 | //Debug << "size b: " << back.Size() << " f: " << front.Size() << endl;
|
---|
[801] | 4585 |
|
---|
[683] | 4586 | if (back.Valid())
|
---|
| 4587 | {
|
---|
[682] | 4588 | exporter->ExportPolygons(back.GetPolys());
|
---|
[708] | 4589 | }
|
---|
[684] | 4590 | }
|
---|
[591] | 4591 | }
|
---|
| 4592 | }
|
---|
| 4593 | else
|
---|
| 4594 | {
|
---|
[801] | 4595 | // export mesh if available
|
---|
[1001] | 4596 | // TODO: some bug here?
|
---|
| 4597 | if (0 && vc->GetMesh())
|
---|
[801] | 4598 | {
|
---|
| 4599 | exporter->ExportMesh(vc->GetMesh());
|
---|
| 4600 | }
|
---|
| 4601 | else
|
---|
[1001] | 4602 | {
|
---|
[801] | 4603 | BspNodeGeometry geom;
|
---|
| 4604 | mVspBspTree->ConstructGeometry(vc, geom);
|
---|
| 4605 | exporter->ExportPolygons(geom.GetPolys());
|
---|
[1001] | 4606 | }
|
---|
[591] | 4607 | }
|
---|
[482] | 4608 | }
|
---|
| 4609 |
|
---|
| 4610 |
|
---|
| 4611 | int VspBspViewCellsManager::GetMaxTreeDiff(ViewCell *vc) const
|
---|
| 4612 | {
|
---|
[580] | 4613 | ViewCellContainer leaves;
|
---|
| 4614 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
[482] | 4615 |
|
---|
| 4616 | int maxDist = 0;
|
---|
[580] | 4617 |
|
---|
[482] | 4618 | // compute max height difference
|
---|
[580] | 4619 | for (int i = 0; i < (int)leaves.size(); ++ i)
|
---|
[1001] | 4620 | {
|
---|
[580] | 4621 | for (int j = 0; j < (int)leaves.size(); ++ j)
|
---|
[1001] | 4622 | {
|
---|
| 4623 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(leaves[i])->mLeaf;
|
---|
[482] | 4624 |
|
---|
[1001] | 4625 | if (i != j)
|
---|
| 4626 | {
|
---|
| 4627 | BspLeaf *leaf2 =dynamic_cast<BspViewCell *>(leaves[j])->mLeaf;
|
---|
| 4628 |
|
---|
| 4629 | int dist = mVspBspTree->TreeDistance(leaf, leaf2);
|
---|
| 4630 |
|
---|
| 4631 | if (dist > maxDist)
|
---|
| 4632 | maxDist = dist;
|
---|
| 4633 | }
|
---|
[482] | 4634 | }
|
---|
| 4635 | }
|
---|
[580] | 4636 |
|
---|
[492] | 4637 | return maxDist;
|
---|
| 4638 | }
|
---|
[482] | 4639 |
|
---|
[492] | 4640 |
|
---|
[879] | 4641 | ViewCell *VspBspViewCellsManager::GetViewCell(const Vector3 &point, const bool active) const
|
---|
[492] | 4642 | {
|
---|
[1021] | 4643 | if (!ViewCellsConstructed())
|
---|
[508] | 4644 | return NULL;
|
---|
[572] | 4645 |
|
---|
| 4646 | if (!mViewSpaceBox.IsInside(point))
|
---|
| 4647 | return NULL;
|
---|
| 4648 |
|
---|
[879] | 4649 | return mVspBspTree->GetViewCell(point, active);
|
---|
[492] | 4650 | }
|
---|
[503] | 4651 |
|
---|
| 4652 |
|
---|
| 4653 | void VspBspViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
| 4654 | {
|
---|
[726] | 4655 | //if (vc->GetMesh()) delete vc->GetMesh();
|
---|
[503] | 4656 | BspNodeGeometry geom;
|
---|
[590] | 4657 |
|
---|
| 4658 | mVspBspTree->ConstructGeometry(vc, geom);
|
---|
[582] | 4659 |
|
---|
[1001] | 4660 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
[840] | 4661 | IncludeNodeGeomInMesh(geom, *mesh);
|
---|
| 4662 |
|
---|
[503] | 4663 | vc->SetMesh(mesh);
|
---|
| 4664 | }
|
---|
[508] | 4665 |
|
---|
| 4666 |
|
---|
[532] | 4667 | int VspBspViewCellsManager::CastBeam(Beam &beam)
|
---|
| 4668 | {
|
---|
[535] | 4669 | return mVspBspTree->CastBeam(beam);
|
---|
[532] | 4670 | }
|
---|
[551] | 4671 |
|
---|
| 4672 |
|
---|
[587] | 4673 | void VspBspViewCellsManager::Finalize(ViewCell *viewCell,
|
---|
| 4674 | const bool createMesh)
|
---|
[551] | 4675 | {
|
---|
| 4676 | float area = 0;
|
---|
| 4677 | float volume = 0;
|
---|
| 4678 |
|
---|
[580] | 4679 | ViewCellContainer leaves;
|
---|
[582] | 4680 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
[580] | 4681 |
|
---|
| 4682 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 4683 |
|
---|
[693] | 4684 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[551] | 4685 | {
|
---|
| 4686 | BspNodeGeometry geom;
|
---|
[580] | 4687 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
[551] | 4688 | mVspBspTree->ConstructGeometry(leaf, geom);
|
---|
| 4689 |
|
---|
[719] | 4690 | const float lVol = geom.GetVolume();
|
---|
| 4691 | const float lArea = geom.GetArea();
|
---|
[693] | 4692 |
|
---|
| 4693 | //(*it)->SetVolume(vol);
|
---|
| 4694 | //(*it)->SetArea(area);
|
---|
| 4695 |
|
---|
| 4696 | area += lArea;
|
---|
| 4697 | volume += lVol;
|
---|
| 4698 |
|
---|
| 4699 | CreateMesh(*it);
|
---|
[551] | 4700 | }
|
---|
| 4701 |
|
---|
| 4702 | viewCell->SetVolume(volume);
|
---|
| 4703 | viewCell->SetArea(area);
|
---|
[563] | 4704 | }
|
---|
[575] | 4705 |
|
---|
| 4706 |
|
---|
[693] | 4707 | void VspBspViewCellsManager::TestSubdivision()
|
---|
| 4708 | {
|
---|
| 4709 | ViewCellContainer leaves;
|
---|
| 4710 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 4711 |
|
---|
| 4712 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 4713 |
|
---|
| 4714 | const float vol = mViewSpaceBox.GetVolume();
|
---|
| 4715 | float subdivVol = 0;
|
---|
| 4716 | float newVol = 0;
|
---|
| 4717 |
|
---|
| 4718 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 4719 | {
|
---|
| 4720 | BspNodeGeometry geom;
|
---|
| 4721 | BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
| 4722 | mVspBspTree->ConstructGeometry(leaf, geom);
|
---|
| 4723 |
|
---|
| 4724 | const float lVol = geom.GetVolume();
|
---|
| 4725 |
|
---|
| 4726 | newVol += lVol;
|
---|
| 4727 | subdivVol += (*it)->GetVolume();
|
---|
[697] | 4728 |
|
---|
[704] | 4729 | float thres = 0.9f;
|
---|
[697] | 4730 | if ((lVol < ((*it)->GetVolume() * thres)) || (lVol * thres > ((*it)->GetVolume())))
|
---|
| 4731 | Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
|
---|
[693] | 4732 | }
|
---|
| 4733 |
|
---|
| 4734 | Debug << "exact volume: " << vol << endl;
|
---|
| 4735 | Debug << "subdivision volume: " << subdivVol << endl;
|
---|
| 4736 | Debug << "new volume: " << newVol << endl;
|
---|
| 4737 | }
|
---|
| 4738 |
|
---|
| 4739 |
|
---|
[577] | 4740 | void VspBspViewCellsManager::PrepareLoadedViewCells()
|
---|
| 4741 | {
|
---|
| 4742 | // TODO: do I still need this here?
|
---|
[590] | 4743 | if (0)
|
---|
[693] | 4744 | mVspBspTree->RepairViewCellsLeafLists();
|
---|
[577] | 4745 | }
|
---|
[575] | 4746 |
|
---|
[1021] | 4747 |
|
---|
| 4748 |
|
---|
| 4749 | /**************************************************************************/
|
---|
[1047] | 4750 | /* VspOspViewCellsManager implementation */
|
---|
[1021] | 4751 | /**************************************************************************/
|
---|
| 4752 |
|
---|
| 4753 |
|
---|
[1278] | 4754 | VspOspViewCellsManager::VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm)
|
---|
| 4755 | : ViewCellsManager(vcTree), mHierarchyManager(hm)
|
---|
[859] | 4756 | {
|
---|
[1027] | 4757 | Environment::GetSingleton()->GetIntValue("VspTree.Construction.samples", mInitialSamples);
|
---|
[1106] | 4758 |
|
---|
[1278] | 4759 | mHierarchyManager->SetViewCellsManager(this);
|
---|
| 4760 | mHierarchyManager->SetViewCellsTree(mViewCellsTree);
|
---|
[1021] | 4761 | }
|
---|
[859] | 4762 |
|
---|
| 4763 |
|
---|
[1021] | 4764 | VspOspViewCellsManager::~VspOspViewCellsManager()
|
---|
| 4765 | {
|
---|
| 4766 | }
|
---|
| 4767 |
|
---|
| 4768 |
|
---|
| 4769 | float VspOspViewCellsManager::GetProbability(ViewCell *viewCell)
|
---|
| 4770 | {
|
---|
| 4771 | return GetVolume(viewCell) / mViewSpaceBox.GetVolume();
|
---|
| 4772 | }
|
---|
| 4773 |
|
---|
| 4774 |
|
---|
| 4775 | void VspOspViewCellsManager::CollectViewCells()
|
---|
| 4776 | {
|
---|
| 4777 | // view cells tree constructed
|
---|
| 4778 | if (!ViewCellsTreeConstructed())
|
---|
| 4779 | {
|
---|
[1278] | 4780 | mHierarchyManager->GetVspTree()->CollectViewCells(mViewCells, false);
|
---|
[1021] | 4781 | }
|
---|
| 4782 | else
|
---|
| 4783 | { // we can use the view cells tree hierarchy to get the right set
|
---|
| 4784 | mViewCellsTree->CollectBestViewCellSet(mViewCells, mNumActiveViewCells);
|
---|
| 4785 | }
|
---|
| 4786 | }
|
---|
| 4787 |
|
---|
| 4788 |
|
---|
| 4789 | bool VspOspViewCellsManager::ViewCellsConstructed() const
|
---|
| 4790 | {
|
---|
[1278] | 4791 | return mHierarchyManager->GetVspTree()->GetRoot() != NULL;
|
---|
[1021] | 4792 | }
|
---|
| 4793 |
|
---|
| 4794 |
|
---|
| 4795 | ViewCell *VspOspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
| 4796 | {
|
---|
| 4797 | return new VspViewCell(mesh);
|
---|
| 4798 | }
|
---|
| 4799 |
|
---|
| 4800 |
|
---|
| 4801 | int VspOspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
|
---|
| 4802 | const VssRayContainer &rays)
|
---|
| 4803 | {
|
---|
| 4804 | mMaxPvsSize = (int)(mMaxPvsRatio * (float)objects.size());
|
---|
| 4805 |
|
---|
[1074] | 4806 | // skip rest if view cells were already constructed
|
---|
[1021] | 4807 | if (ViewCellsConstructed())
|
---|
| 4808 | return 0;
|
---|
| 4809 |
|
---|
| 4810 | int sampleContributions = 0;
|
---|
| 4811 |
|
---|
| 4812 | VssRayContainer sampleRays;
|
---|
| 4813 |
|
---|
| 4814 | int limit = min (mInitialSamples, (int)rays.size());
|
---|
| 4815 |
|
---|
| 4816 | VssRayContainer constructionRays;
|
---|
| 4817 | VssRayContainer savedRays;
|
---|
| 4818 |
|
---|
| 4819 | Debug << "samples used for vsp bsp subdivision: " << mInitialSamples
|
---|
| 4820 | << ", actual rays: " << (int)rays.size() << endl;
|
---|
| 4821 |
|
---|
| 4822 | GetRaySets(rays, mInitialSamples, constructionRays, &savedRays);
|
---|
| 4823 |
|
---|
| 4824 | Debug << "initial rays: " << (int)constructionRays.size() << endl;
|
---|
| 4825 | Debug << "saved rays: " << (int)savedRays.size() << endl;
|
---|
| 4826 |
|
---|
[1293] | 4827 | mHierarchyManager->Construct(constructionRays, objects, &mViewSpaceBox);
|
---|
[1142] | 4828 |
|
---|
[1178] | 4829 | VssRayContainer::const_iterator vit, vit_end = constructionRays.end();
|
---|
[1180] | 4830 |
|
---|
[1178] | 4831 | for (vit = constructionRays.begin(); vit != vit_end; ++ vit)
|
---|
| 4832 | {
|
---|
| 4833 | storedRays.push_back(new VssRay(*(*vit)));
|
---|
| 4834 | }
|
---|
[1176] | 4835 |
|
---|
[1143] | 4836 | // print subdivision statistics
|
---|
[1313] | 4837 | mHierarchyManager->PrintHierarchyStatistics(Debug);
|
---|
[1021] | 4838 |
|
---|
[1278] | 4839 | if (0)
|
---|
| 4840 | {
|
---|
| 4841 | OUT_STREAM stream("osptree.xml.zip");
|
---|
| 4842 | mHierarchyManager->ExportObjectSpaceHierarchy(stream);
|
---|
| 4843 | }
|
---|
[1201] | 4844 |
|
---|
[1143] | 4845 | // print view cell statistics
|
---|
[1021] | 4846 | ResetViewCells();
|
---|
| 4847 | Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
|
---|
| 4848 |
|
---|
| 4849 |
|
---|
[1286] | 4850 | const long startTime = GetTime();
|
---|
[1021] | 4851 |
|
---|
| 4852 | cout << "Computing remaining ray contributions ... ";
|
---|
| 4853 |
|
---|
| 4854 | // recast rest of rays
|
---|
| 4855 | if (SAMPLE_AFTER_SUBDIVISION)
|
---|
| 4856 | ComputeSampleContributions(savedRays, true, false);
|
---|
| 4857 |
|
---|
| 4858 | cout << "finished" << endl;
|
---|
| 4859 |
|
---|
| 4860 | Debug << "Computed remaining ray contribution in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
| 4861 | << " secs" << endl;
|
---|
| 4862 |
|
---|
| 4863 | cout << "construction finished" << endl;
|
---|
| 4864 |
|
---|
| 4865 | // real meshes are contructed at this stage
|
---|
| 4866 | if (0)
|
---|
| 4867 | {
|
---|
| 4868 | cout << "finalizing view cells ... ";
|
---|
| 4869 | FinalizeViewCells(true);
|
---|
| 4870 | cout << "finished" << endl;
|
---|
| 4871 | }
|
---|
| 4872 |
|
---|
| 4873 | return sampleContributions;
|
---|
| 4874 | }
|
---|
| 4875 |
|
---|
| 4876 |
|
---|
| 4877 | int VspOspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
| 4878 | const VssRayContainer &rays)
|
---|
| 4879 | {
|
---|
| 4880 | if (!ViewCellsConstructed())
|
---|
| 4881 | {
|
---|
| 4882 | Debug << "postprocess error: no view cells constructed" << endl;
|
---|
| 4883 | return 0;
|
---|
| 4884 | }
|
---|
| 4885 |
|
---|
| 4886 |
|
---|
[1027] | 4887 | // take this step only if
|
---|
| 4888 | // view cells already constructed before post processing step
|
---|
| 4889 | // (e.g., because they were loaded)
|
---|
[1021] | 4890 | if (mViewCellsFinished)
|
---|
| 4891 | {
|
---|
| 4892 | FinalizeViewCells(true);
|
---|
| 4893 | EvaluateViewCellsStats();
|
---|
| 4894 |
|
---|
| 4895 | return 0;
|
---|
| 4896 | }
|
---|
| 4897 |
|
---|
| 4898 | // check if new view cells turned invalid
|
---|
| 4899 | int minPvs, maxPvs;
|
---|
| 4900 |
|
---|
| 4901 | if (0)
|
---|
| 4902 | {
|
---|
| 4903 | minPvs = mMinPvsSize;
|
---|
| 4904 | maxPvs = mMaxPvsSize;
|
---|
| 4905 | }
|
---|
| 4906 | else
|
---|
| 4907 | {
|
---|
| 4908 | minPvs = mPruneEmptyViewCells ? 1 : 0;
|
---|
| 4909 | maxPvs = mMaxPvsSize;
|
---|
| 4910 | }
|
---|
| 4911 |
|
---|
| 4912 | Debug << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
| 4913 | cout << "setting validity, min: " << minPvs << " max: " << maxPvs << endl;
|
---|
| 4914 |
|
---|
| 4915 | SetValidity(minPvs, maxPvs);
|
---|
| 4916 |
|
---|
| 4917 |
|
---|
| 4918 | // area has to be recomputed
|
---|
| 4919 | mTotalAreaValid = false;
|
---|
| 4920 | VssRayContainer postProcessRays;
|
---|
| 4921 | GetRaySets(rays, mPostProcessSamples, postProcessRays);
|
---|
| 4922 |
|
---|
| 4923 | Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
|
---|
| 4924 |
|
---|
| 4925 |
|
---|
| 4926 | // should maybe be done here to allow merge working with area or volume
|
---|
| 4927 | // and to correct the rendering statistics
|
---|
| 4928 | if (0) FinalizeViewCells(false);
|
---|
| 4929 |
|
---|
| 4930 | // compute tree by merging the nodes of the spatial hierarchy
|
---|
[1278] | 4931 | ViewCell *root = ConstructSpatialMergeTree(mHierarchyManager->GetVspTree()->GetRoot());
|
---|
[1021] | 4932 | mViewCellsTree->SetRoot(root);
|
---|
| 4933 |
|
---|
| 4934 | // compute pvs
|
---|
| 4935 | ObjectPvs pvs;
|
---|
| 4936 | UpdatePvsForEvaluation(root, pvs);
|
---|
| 4937 |
|
---|
| 4938 |
|
---|
| 4939 | //-- render simulation after merge + refine
|
---|
| 4940 | cout << "\nevaluating bsp view cells render time before compress ... ";
|
---|
| 4941 | dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
|
---|
| 4942 | SimulationStatistics ss;
|
---|
| 4943 | dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
|
---|
[859] | 4944 |
|
---|
| 4945 |
|
---|
[1021] | 4946 | cout << " finished" << endl;
|
---|
| 4947 | cout << ss << endl;
|
---|
| 4948 | Debug << ss << endl;
|
---|
[859] | 4949 |
|
---|
| 4950 |
|
---|
[1021] | 4951 | //-- compression
|
---|
[1262] | 4952 |
|
---|
[1021] | 4953 | if (ViewCellsTreeConstructed() && mCompressViewCells)
|
---|
| 4954 | {
|
---|
[1161] | 4955 | int pvsEntries = mViewCellsTree->GetStoredPvsEntriesNum(mViewCellsTree->GetRoot());
|
---|
[1021] | 4956 | Debug << "number of entries before compress: " << pvsEntries << endl;
|
---|
[859] | 4957 |
|
---|
[1021] | 4958 | mViewCellsTree->SetViewCellsStorage(ViewCellsTree::COMPRESSED);
|
---|
[859] | 4959 |
|
---|
[1161] | 4960 | pvsEntries = mViewCellsTree->GetStoredPvsEntriesNum(mViewCellsTree->GetRoot());
|
---|
[1021] | 4961 | Debug << "number of entries after compress: " << pvsEntries << endl;
|
---|
[859] | 4962 | }
|
---|
[1021] | 4963 |
|
---|
| 4964 | // compute final meshes and volume / area
|
---|
| 4965 | if (1) FinalizeViewCells(true);
|
---|
| 4966 |
|
---|
| 4967 | // write view cells to disc
|
---|
| 4968 | if (mExportViewCells)
|
---|
| 4969 | {
|
---|
| 4970 | char filename[100];
|
---|
| 4971 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", filename);
|
---|
| 4972 | ExportViewCells(filename, mExportPvs, objects);
|
---|
| 4973 | }
|
---|
| 4974 |
|
---|
| 4975 |
|
---|
| 4976 | return 0;
|
---|
[859] | 4977 | }
|
---|
| 4978 |
|
---|
[1021] | 4979 |
|
---|
| 4980 | int VspOspViewCellsManager::GetType() const
|
---|
[884] | 4981 | {
|
---|
[1021] | 4982 | return VSP_OSP;
|
---|
| 4983 | }
|
---|
[859] | 4984 |
|
---|
[1021] | 4985 |
|
---|
| 4986 | ViewCell *VspOspViewCellsManager::ConstructSpatialMergeTree(VspNode *root)
|
---|
| 4987 | {
|
---|
| 4988 | // terminate recursion
|
---|
| 4989 | if (root->IsLeaf())
|
---|
| 4990 | {
|
---|
| 4991 | VspLeaf *leaf = dynamic_cast<VspLeaf *>(root);
|
---|
| 4992 | leaf->GetViewCell()->SetMergeCost(0.0f);
|
---|
| 4993 | return leaf->GetViewCell();
|
---|
| 4994 | }
|
---|
| 4995 |
|
---|
| 4996 | VspInterior *interior = dynamic_cast<VspInterior *>(root);
|
---|
| 4997 | ViewCellInterior *viewCellInterior = new ViewCellInterior();
|
---|
| 4998 |
|
---|
| 4999 | // evaluate merge cost for priority traversal
|
---|
| 5000 | float mergeCost = 1.0f / (float)root->mTimeStamp;
|
---|
| 5001 | viewCellInterior->SetMergeCost(mergeCost);
|
---|
| 5002 |
|
---|
| 5003 | float volume = 0;
|
---|
| 5004 |
|
---|
| 5005 | VspNode *front = interior->GetFront();
|
---|
| 5006 | VspNode *back = interior->GetBack();
|
---|
| 5007 |
|
---|
| 5008 |
|
---|
| 5009 | ObjectPvs frontPvs, backPvs;
|
---|
| 5010 |
|
---|
| 5011 | //-- recursivly compute child hierarchies
|
---|
| 5012 | ViewCell *backVc = ConstructSpatialMergeTree(back);
|
---|
| 5013 | ViewCell *frontVc = ConstructSpatialMergeTree(front);
|
---|
| 5014 |
|
---|
| 5015 |
|
---|
| 5016 | viewCellInterior->SetupChildLink(backVc);
|
---|
| 5017 | viewCellInterior->SetupChildLink(frontVc);
|
---|
| 5018 |
|
---|
| 5019 | volume += backVc->GetVolume();
|
---|
| 5020 | volume += frontVc->GetVolume();
|
---|
| 5021 |
|
---|
| 5022 | viewCellInterior->SetVolume(volume);
|
---|
| 5023 |
|
---|
| 5024 | return viewCellInterior;
|
---|
| 5025 | }
|
---|
| 5026 |
|
---|
| 5027 |
|
---|
| 5028 | bool VspOspViewCellsManager::GetViewPoint(Vector3 &viewPoint) const
|
---|
| 5029 | {
|
---|
| 5030 | if (!ViewCellsConstructed())
|
---|
| 5031 | return ViewCellsManager::GetViewPoint(viewPoint);
|
---|
| 5032 |
|
---|
| 5033 | // TODO: set reasonable limit
|
---|
| 5034 | const int limit = 20;
|
---|
| 5035 |
|
---|
| 5036 | for (int i = 0; i < limit; ++ i)
|
---|
| 5037 | {
|
---|
| 5038 | viewPoint = mViewSpaceBox.GetRandomPoint();
|
---|
[1047] | 5039 |
|
---|
[1278] | 5040 | if (mHierarchyManager->GetVspTree()->ViewPointValid(viewPoint))
|
---|
[1021] | 5041 | {
|
---|
| 5042 | return true;
|
---|
| 5043 | }
|
---|
[904] | 5044 | }
|
---|
[859] | 5045 |
|
---|
[1021] | 5046 | Debug << "failed to find valid view point, taking " << viewPoint << endl;
|
---|
| 5047 | return false;
|
---|
[884] | 5048 | }
|
---|
| 5049 |
|
---|
| 5050 |
|
---|
[1027] | 5051 | void VspOspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
|
---|
| 5052 | ViewCell *vc,
|
---|
[1047] | 5053 | const AxisAlignedPlane *clipPlane) const
|
---|
[1027] | 5054 | {
|
---|
| 5055 | ViewCellContainer leaves;
|
---|
| 5056 |
|
---|
| 5057 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 5058 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 5059 |
|
---|
[1047] | 5060 | Plane3 plane;
|
---|
| 5061 |
|
---|
| 5062 | if (clipPlane)
|
---|
| 5063 | plane = clipPlane->GetPlane();
|
---|
| 5064 |
|
---|
[1027] | 5065 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 5066 | {
|
---|
| 5067 | VspViewCell *vspVc = dynamic_cast<VspViewCell *>(*it);
|
---|
| 5068 | VspLeaf *l = vspVc->mLeaf;
|
---|
| 5069 |
|
---|
[1278] | 5070 | const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(vspVc->mLeaf);
|
---|
[1047] | 5071 |
|
---|
| 5072 | if (clipPlane)
|
---|
| 5073 | {
|
---|
| 5074 | if (box.Side(plane) == -1)
|
---|
[1144] | 5075 | {
|
---|
[1047] | 5076 | exporter->ExportBox(box);
|
---|
[1144] | 5077 | }
|
---|
[1047] | 5078 | else if (box.Side(plane) == 0)
|
---|
| 5079 | {
|
---|
| 5080 | AxisAlignedBox3 fbox, bbox;
|
---|
| 5081 | box.Split(clipPlane->mAxis, clipPlane->mPosition, fbox, bbox);
|
---|
| 5082 |
|
---|
| 5083 | exporter->ExportBox(bbox);
|
---|
| 5084 | }
|
---|
| 5085 | }
|
---|
| 5086 | else
|
---|
| 5087 | {
|
---|
[1027] | 5088 | exporter->ExportBox(box);
|
---|
[1047] | 5089 | }
|
---|
[1027] | 5090 | }
|
---|
| 5091 | }
|
---|
| 5092 |
|
---|
| 5093 |
|
---|
[1021] | 5094 | bool VspOspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
|
---|
| 5095 | {
|
---|
| 5096 | // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
|
---|
| 5097 | // validy update in preprocessor for all managers)
|
---|
| 5098 | return ViewCellsManager::ViewPointValid(viewPoint);
|
---|
[884] | 5099 |
|
---|
[1021] | 5100 | // return mViewSpaceBox.IsInside(viewPoint) &&
|
---|
[1027] | 5101 | // mVspTree->ViewPointValid(viewPoint);
|
---|
[1021] | 5102 | }
|
---|
| 5103 |
|
---|
| 5104 |
|
---|
| 5105 | void VspOspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
| 5106 | const VssRayContainer &sampleRays)
|
---|
[859] | 5107 | {
|
---|
[997] | 5108 | if (!ViewCellsConstructed())
|
---|
| 5109 | return;
|
---|
[1027] | 5110 |
|
---|
[1021] | 5111 | VssRayContainer visRays;
|
---|
| 5112 | GetRaySets(sampleRays, mVisualizationSamples, visRays);
|
---|
[997] | 5113 |
|
---|
[1021] | 5114 | if (1)
|
---|
[1047] | 5115 | {
|
---|
[1106] | 5116 | //-- export final view cells
|
---|
| 5117 |
|
---|
[1047] | 5118 | // hack pvs
|
---|
[1021] | 5119 | const int savedColorCode = mColorCode;
|
---|
[1047] | 5120 | mColorCode = 0;
|
---|
[1021] | 5121 |
|
---|
| 5122 | Exporter *exporter = Exporter::GetExporter("final_view_cells.wrl");
|
---|
| 5123 |
|
---|
| 5124 | if (exporter)
|
---|
| 5125 | {
|
---|
| 5126 | cout << "exporting view cells after post process ... ";
|
---|
[859] | 5127 |
|
---|
[1021] | 5128 | if (mExportGeometry)
|
---|
| 5129 | {
|
---|
[1404] | 5130 | exporter->ExportGeometry(objects, true);
|
---|
[1021] | 5131 | }
|
---|
[859] | 5132 |
|
---|
[1021] | 5133 | // export rays
|
---|
[1072] | 5134 | if (0 && mExportRays)
|
---|
[1021] | 5135 | {
|
---|
| 5136 | exporter->ExportRays(visRays, RgbColor(0, 1, 0));
|
---|
| 5137 | }
|
---|
| 5138 |
|
---|
| 5139 | // HACK: export without clip plane
|
---|
[1052] | 5140 | const bool b = mUseClipPlaneForViz;
|
---|
[1106] | 5141 | if (0) mUseClipPlaneForViz = false;
|
---|
[1021] | 5142 |
|
---|
| 5143 | ExportViewCellsForViz(exporter);
|
---|
| 5144 |
|
---|
[1052] | 5145 | mUseClipPlaneForViz = b;
|
---|
[1021] | 5146 | delete exporter;
|
---|
| 5147 |
|
---|
| 5148 | cout << "finished" << endl;
|
---|
| 5149 | }
|
---|
| 5150 |
|
---|
| 5151 | mColorCode = savedColorCode;
|
---|
| 5152 | }
|
---|
| 5153 |
|
---|
[1287] | 5154 | if (1)
|
---|
| 5155 | {
|
---|
| 5156 | // export final object partition
|
---|
| 5157 | Exporter *exporter = Exporter::GetExporter("final_object_partition.wrl");
|
---|
| 5158 |
|
---|
| 5159 | if (exporter)
|
---|
| 5160 | {
|
---|
| 5161 | cout << "exporting object space hierarchy ... ";
|
---|
| 5162 | mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects);
|
---|
| 5163 |
|
---|
| 5164 | delete exporter;
|
---|
| 5165 | cout << "finished" << endl;
|
---|
| 5166 | }
|
---|
| 5167 | }
|
---|
[1121] | 5168 |
|
---|
[1287] | 5169 | // export pvss of some view cell
|
---|
[1074] | 5170 | ExportPvs(objects, visRays);
|
---|
[859] | 5171 | }
|
---|
| 5172 |
|
---|
[1021] | 5173 |
|
---|
[1074] | 5174 | void VspOspViewCellsManager::ExportPvs(const ObjectContainer &objects,
|
---|
| 5175 | const VssRayContainer &rays)
|
---|
[697] | 5176 | {
|
---|
[1021] | 5177 | int leafOut;
|
---|
| 5178 | Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
|
---|
[580] | 5179 |
|
---|
[1021] | 5180 | ViewCell::NewMail();
|
---|
| 5181 |
|
---|
| 5182 | cout << "visualization using " << (int)rays.size() << " samples" << endl;
|
---|
| 5183 | Debug << "visualization using " << (int)rays.size() << " samples" << endl;
|
---|
| 5184 | Debug << "\nOutput view cells: " << endl;
|
---|
| 5185 |
|
---|
| 5186 | const bool sortViewCells = true;
|
---|
| 5187 |
|
---|
| 5188 | // sort view cells to visualize the largest view cells
|
---|
| 5189 | if (sortViewCells)
|
---|
[697] | 5190 | {
|
---|
[1021] | 5191 | //stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
|
---|
| 5192 | stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::LargerRenderCost);
|
---|
| 5193 | }
|
---|
| 5194 |
|
---|
| 5195 | int limit = min(leafOut, (int)mViewCells.size());
|
---|
| 5196 |
|
---|
| 5197 | int raysOut = 0;
|
---|
| 5198 |
|
---|
[1178] | 5199 |
|
---|
[1021] | 5200 | //-- some rays for output
|
---|
[1178] | 5201 |
|
---|
[1021] | 5202 | for (int i = 0; i < limit; ++ i)
|
---|
| 5203 | {
|
---|
| 5204 | cout << "creating output for view cell " << i << " ... ";
|
---|
| 5205 |
|
---|
| 5206 | ViewCell *vc;
|
---|
[704] | 5207 |
|
---|
[1021] | 5208 | if (sortViewCells) // largest view cell pvs first
|
---|
| 5209 | vc = mViewCells[i];
|
---|
[1074] | 5210 | else // random view cell
|
---|
[1021] | 5211 | vc = mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 1)];
|
---|
[704] | 5212 |
|
---|
[1021] | 5213 | ObjectPvs pvs;
|
---|
| 5214 | mViewCellsTree->GetPvs(vc, pvs);
|
---|
[704] | 5215 |
|
---|
[1021] | 5216 | //bspLeaves[j]->Mail();
|
---|
| 5217 | char s[64]; sprintf(s, "bsp-pvs%04d.wrl", i);
|
---|
| 5218 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
| 5219 |
|
---|
[1142] | 5220 | Debug << i << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc) << endl;
|
---|
[1021] | 5221 |
|
---|
| 5222 | //-- export the sample rays
|
---|
[1074] | 5223 | if (1 || mExportRays)
|
---|
[1021] | 5224 | {
|
---|
| 5225 | // output rays stored with the view cells during subdivision
|
---|
[1047] | 5226 | if (1)
|
---|
[1021] | 5227 | {
|
---|
| 5228 | VssRayContainer vcRays;
|
---|
| 5229 | VssRayContainer collectRays;
|
---|
| 5230 |
|
---|
| 5231 | raysOut = min((int)rays.size(), 100);
|
---|
| 5232 |
|
---|
| 5233 | // collect intial view cells
|
---|
| 5234 | ViewCellContainer leaves;
|
---|
| 5235 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 5236 |
|
---|
| 5237 | ViewCellContainer::const_iterator vit, vit_end = leaves.end();
|
---|
| 5238 |
|
---|
| 5239 | for (vit = leaves.begin(); vit != vit_end; ++ vit)
|
---|
| 5240 | {
|
---|
[1047] | 5241 | VspLeaf *vcLeaf = dynamic_cast<VspViewCell *>(*vit)->mLeaf;
|
---|
[1021] | 5242 |
|
---|
| 5243 | VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
|
---|
| 5244 |
|
---|
| 5245 | for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
| 5246 | {
|
---|
| 5247 | collectRays.push_back(*rit);
|
---|
| 5248 | }
|
---|
| 5249 | }
|
---|
| 5250 |
|
---|
| 5251 | VssRayContainer::const_iterator rit, rit_end = collectRays.end();
|
---|
| 5252 |
|
---|
| 5253 | for (rit = collectRays.begin(); rit != rit_end; ++ rit)
|
---|
| 5254 | {
|
---|
| 5255 | float p = RandomValue(0.0f, (float)collectRays.size());
|
---|
| 5256 |
|
---|
| 5257 | if (p < raysOut)
|
---|
| 5258 | vcRays.push_back(*rit);
|
---|
| 5259 | }
|
---|
| 5260 |
|
---|
| 5261 | //-- export rays piercing this view cell
|
---|
| 5262 | exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
|
---|
| 5263 | }
|
---|
| 5264 |
|
---|
| 5265 | // associate new rays with output view cell
|
---|
[1047] | 5266 | if (0)
|
---|
[1021] | 5267 | {
|
---|
| 5268 | VssRayContainer vcRays;
|
---|
| 5269 | raysOut = min((int)rays.size(), mVisualizationSamples);
|
---|
| 5270 |
|
---|
| 5271 | // check whether we can add the current ray to the output rays
|
---|
| 5272 | for (int k = 0; k < raysOut; ++ k)
|
---|
| 5273 | {
|
---|
| 5274 | VssRay *ray = rays[k];
|
---|
| 5275 | for (int j = 0; j < (int)ray->mViewCells.size(); ++ j)
|
---|
| 5276 | {
|
---|
| 5277 | ViewCell *rayvc = ray->mViewCells[j];
|
---|
| 5278 |
|
---|
| 5279 | if (rayvc == vc)
|
---|
| 5280 | vcRays.push_back(ray);
|
---|
| 5281 | }
|
---|
| 5282 | }
|
---|
| 5283 |
|
---|
| 5284 | //-- export rays piercing this view cell
|
---|
| 5285 | exporter->ExportRays(vcRays, RgbColor(1, 1, 0));
|
---|
| 5286 | }
|
---|
| 5287 | }
|
---|
| 5288 |
|
---|
| 5289 | //-- export view cell geometry
|
---|
[1121] | 5290 |
|
---|
[697] | 5291 | exporter->SetWireframe();
|
---|
| 5292 |
|
---|
[1021] | 5293 | Material m;//= RandomMaterial();
|
---|
| 5294 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 5295 | exporter->SetForcedMaterial(m);
|
---|
| 5296 |
|
---|
| 5297 | ExportViewCellGeometry(exporter, vc);
|
---|
| 5298 |
|
---|
[697] | 5299 | exporter->SetFilled();
|
---|
| 5300 |
|
---|
[1278] | 5301 | #if 0
|
---|
| 5302 | //-- export pvs
|
---|
[697] | 5303 |
|
---|
[1278] | 5304 | Intersectable::NewMail();
|
---|
| 5305 | KdLeaf::NewMail();
|
---|
[1074] | 5306 |
|
---|
[1278] | 5307 | vector<KdLeaf *> kdLeaves;
|
---|
[1074] | 5308 |
|
---|
[1278] | 5309 | ObjectPvsMap::const_iterator oit, oit_end = pvs.mEntries.end();
|
---|
[1021] | 5310 |
|
---|
[1278] | 5311 | for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
|
---|
| 5312 | {
|
---|
| 5313 | Intersectable *obj = (*oit).first;
|
---|
[1144] | 5314 |
|
---|
[1278] | 5315 | if (obj->Type() == Intersectable::KD_INTERSECTABLE)
|
---|
| 5316 | {
|
---|
| 5317 | m.mDiffuseColor = RgbColor(1, 1, 1);
|
---|
[1144] | 5318 | exporter->SetForcedMaterial(m);
|
---|
| 5319 |
|
---|
[1278] | 5320 | // export bounding box of node
|
---|
| 5321 | KdIntersectable *kdObj = dynamic_cast<KdIntersectable *>(obj);
|
---|
| 5322 | AxisAlignedBox3 box = mOspTree->GetBoundingBox(kdObj->GetItem());
|
---|
| 5323 |
|
---|
| 5324 | exporter->SetWireframe();
|
---|
| 5325 | exporter->ExportBox(box);
|
---|
| 5326 | exporter->SetFilled();
|
---|
| 5327 | }
|
---|
| 5328 |
|
---|
| 5329 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 5330 | exporter->SetForcedMaterial(m);
|
---|
| 5331 |
|
---|
| 5332 | // export pvs entry
|
---|
| 5333 | if (!obj->Mailed())
|
---|
| 5334 | {
|
---|
| 5335 | exporter->ExportIntersectable(obj);
|
---|
| 5336 | obj->Mail();
|
---|
| 5337 | }
|
---|
| 5338 | }
|
---|
| 5339 | #endif
|
---|
[1074] | 5340 |
|
---|
[1021] | 5341 | DEL_PTR(exporter);
|
---|
| 5342 | cout << "finished" << endl;
|
---|
[697] | 5343 | }
|
---|
[1021] | 5344 |
|
---|
| 5345 | Debug << endl;
|
---|
[697] | 5346 | }
|
---|
| 5347 |
|
---|
| 5348 |
|
---|
[1021] | 5349 | int VspOspViewCellsManager::ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 5350 | ViewCellContainer &viewCells) const
|
---|
| 5351 | {
|
---|
[1278] | 5352 | return mHierarchyManager->GetVspTree()->ComputeBoxIntersections(box, viewCells);
|
---|
[1021] | 5353 | }
|
---|
[580] | 5354 |
|
---|
[1021] | 5355 |
|
---|
| 5356 | int VspOspViewCellsManager::CastLineSegment(const Vector3 &origin,
|
---|
| 5357 | const Vector3 &termination,
|
---|
| 5358 | ViewCellContainer &viewcells)
|
---|
| 5359 | {
|
---|
[1278] | 5360 | return mHierarchyManager->GetVspTree()->CastLineSegment(origin, termination, viewcells);
|
---|
[1021] | 5361 | }
|
---|
| 5362 |
|
---|
| 5363 |
|
---|
| 5364 | bool VspOspViewCellsManager::ExportViewCells(const string filename,
|
---|
| 5365 | const bool exportPvs,
|
---|
| 5366 | const ObjectContainer &objects)
|
---|
| 5367 | {
|
---|
| 5368 | if (!ViewCellsConstructed() || !ViewCellsTreeConstructed())
|
---|
| 5369 | return false;
|
---|
| 5370 |
|
---|
| 5371 | cout << "exporting view cells to xml ... ";
|
---|
| 5372 |
|
---|
[1201] | 5373 | OUT_STREAM stream(filename.c_str());
|
---|
[1021] | 5374 |
|
---|
| 5375 | // for output we need unique ids for each view cell
|
---|
| 5376 | CreateUniqueViewCellIds();
|
---|
| 5377 |
|
---|
| 5378 | stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
| 5379 | stream << "<VisibilitySolution>" << endl;
|
---|
| 5380 |
|
---|
| 5381 |
|
---|
[1344] | 5382 | //////////////////////////////////////////////////////////////
|
---|
[1021] | 5383 | //-- export bounding boxes
|
---|
[1201] | 5384 |
|
---|
[1021] | 5385 | stream << "<BoundingBoxes>" << endl;
|
---|
| 5386 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 5387 |
|
---|
| 5388 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
[580] | 5389 | {
|
---|
[1344] | 5390 | const AxisAlignedBox3 box = (*oit)->GetBox();
|
---|
[1021] | 5391 |
|
---|
| 5392 | //-- the bounding boxes
|
---|
[1344] | 5393 | stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
|
---|
[1021] | 5394 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
| 5395 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
[580] | 5396 | }
|
---|
[1021] | 5397 |
|
---|
| 5398 | stream << "</BoundingBoxes>" << endl;
|
---|
| 5399 |
|
---|
[1344] | 5400 | ////////////////////////////////////////////////////////////////
|
---|
[1021] | 5401 | //-- export the view cells and the pvs
|
---|
| 5402 |
|
---|
| 5403 | const int numViewCells = mCurrentViewCellsStats.viewCells;
|
---|
| 5404 |
|
---|
| 5405 | stream << "<ViewCells number=\"" << numViewCells << "\" >" << endl;
|
---|
| 5406 | mViewCellsTree->Export(stream, exportPvs);
|
---|
| 5407 | stream << "</ViewCells>" << endl;
|
---|
| 5408 |
|
---|
| 5409 |
|
---|
[1201] | 5410 | //-- export the view space hierarchy
|
---|
[1021] | 5411 |
|
---|
[1262] | 5412 | stream << "<ViewSpaceHierarchy type=\"vsp\""
|
---|
| 5413 | << " min=\"" << mViewSpaceBox.Min().x << " " << mViewSpaceBox.Min().y << " " << mViewSpaceBox.Min().z << "\""
|
---|
[1286] | 5414 | << " max=\"" << mViewSpaceBox.Max().x << " " << mViewSpaceBox.Max().y << " " << mViewSpaceBox.Max().z << "\">" << endl;
|
---|
[1262] | 5415 |
|
---|
[1278] | 5416 | mHierarchyManager->GetVspTree()->Export(stream);
|
---|
[1286] | 5417 | stream << "</ViewSpaceHierarchy>" << endl;
|
---|
[1021] | 5418 |
|
---|
[1201] | 5419 |
|
---|
| 5420 | //-- export the object space partition
|
---|
| 5421 |
|
---|
[1278] | 5422 | mHierarchyManager->ExportObjectSpaceHierarchy(stream);
|
---|
| 5423 |
|
---|
[1201] | 5424 | stream << "</VisibilitySolution>" << endl;
|
---|
[1021] | 5425 | stream.close();
|
---|
[1201] | 5426 |
|
---|
[1021] | 5427 | cout << "finished" << endl;
|
---|
| 5428 |
|
---|
| 5429 | return true;
|
---|
| 5430 | }
|
---|
| 5431 |
|
---|
| 5432 |
|
---|
| 5433 |
|
---|
[1027] | 5434 | ViewCell *VspOspViewCellsManager::GetViewCell(const Vector3 &point,
|
---|
| 5435 | const bool active) const
|
---|
[1021] | 5436 | {
|
---|
| 5437 | if (!ViewCellsConstructed())
|
---|
| 5438 | return NULL;
|
---|
| 5439 |
|
---|
| 5440 | if (!mViewSpaceBox.IsInside(point))
|
---|
| 5441 | return NULL;
|
---|
| 5442 |
|
---|
[1278] | 5443 | return mHierarchyManager->GetVspTree()->GetViewCell(point, active);
|
---|
[1021] | 5444 | }
|
---|
| 5445 |
|
---|
| 5446 |
|
---|
| 5447 | void VspOspViewCellsManager::CreateMesh(ViewCell *vc)
|
---|
| 5448 | {
|
---|
| 5449 | // matt: TODO
|
---|
| 5450 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
[1027] | 5451 |
|
---|
| 5452 | ViewCellContainer leaves;
|
---|
| 5453 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
| 5454 |
|
---|
| 5455 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 5456 |
|
---|
| 5457 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 5458 | {
|
---|
| 5459 | VspLeaf *leaf = dynamic_cast<VspViewCell *>(*it)->mLeaf;
|
---|
[1278] | 5460 | const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(leaf);
|
---|
[1027] | 5461 |
|
---|
| 5462 | IncludeBoxInMesh(box, *mesh);
|
---|
| 5463 | }
|
---|
| 5464 |
|
---|
[1021] | 5465 | vc->SetMesh(mesh);
|
---|
| 5466 | }
|
---|
| 5467 |
|
---|
| 5468 |
|
---|
| 5469 | int VspOspViewCellsManager::CastBeam(Beam &beam)
|
---|
| 5470 | {
|
---|
| 5471 | // matt: TODO
|
---|
| 5472 | return 0;
|
---|
| 5473 | }
|
---|
| 5474 |
|
---|
| 5475 |
|
---|
| 5476 | void VspOspViewCellsManager::Finalize(ViewCell *viewCell,
|
---|
| 5477 | const bool createMesh)
|
---|
| 5478 | {
|
---|
| 5479 | float area = 0;
|
---|
| 5480 | float volume = 0;
|
---|
| 5481 |
|
---|
| 5482 | ViewCellContainer leaves;
|
---|
| 5483 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
| 5484 |
|
---|
| 5485 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
| 5486 |
|
---|
| 5487 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
[580] | 5488 | {
|
---|
[1027] | 5489 | VspLeaf *leaf = dynamic_cast<VspViewCell *>(*it)->mLeaf;
|
---|
| 5490 |
|
---|
[1278] | 5491 | const AxisAlignedBox3 box = mHierarchyManager->GetVspTree()->GetBoundingBox(leaf);
|
---|
[1021] | 5492 |
|
---|
[1027] | 5493 | const float lVol = box.GetVolume();
|
---|
| 5494 | const float lArea = box.SurfaceArea();
|
---|
[1021] | 5495 |
|
---|
| 5496 | //(*it)->SetVolume(vol);
|
---|
| 5497 | //(*it)->SetArea(area);
|
---|
| 5498 |
|
---|
| 5499 | area += lArea;
|
---|
| 5500 | volume += lVol;
|
---|
| 5501 |
|
---|
| 5502 | CreateMesh(*it);
|
---|
[580] | 5503 | }
|
---|
| 5504 |
|
---|
[1021] | 5505 | viewCell->SetVolume(volume);
|
---|
| 5506 | viewCell->SetArea(area);
|
---|
[580] | 5507 | }
|
---|
[1021] | 5508 |
|
---|
[580] | 5509 |
|
---|
[1159] | 5510 | float VspOspViewCellsManager::ComputeSampleContribution(VssRay &ray,
|
---|
| 5511 | const bool addRays,
|
---|
| 5512 | const bool storeViewCells)
|
---|
| 5513 | {
|
---|
| 5514 | ViewCellContainer viewcells;
|
---|
| 5515 |
|
---|
| 5516 | ray.mPvsContribution = 0;
|
---|
| 5517 | ray.mRelativePvsContribution = 0.0f;
|
---|
| 5518 |
|
---|
| 5519 | static Ray hray;
|
---|
| 5520 | hray.Init(ray);
|
---|
| 5521 | //hray.mFlags |= Ray::CULL_BACKFACES;
|
---|
| 5522 | //Ray hray(ray);
|
---|
| 5523 |
|
---|
| 5524 | float tmin = 0, tmax = 1.0;
|
---|
| 5525 |
|
---|
| 5526 | if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
|
---|
| 5527 | return 0;
|
---|
| 5528 |
|
---|
| 5529 | Vector3 origin = hray.Extrap(tmin);
|
---|
| 5530 | Vector3 termination = hray.Extrap(tmax);
|
---|
| 5531 |
|
---|
[1291] | 5532 | ViewCell::NewMail();
|
---|
| 5533 |
|
---|
[1159] | 5534 | // traverse the view space subdivision
|
---|
| 5535 | CastLineSegment(origin, termination, viewcells);
|
---|
| 5536 |
|
---|
| 5537 | if (storeViewCells)
|
---|
[1291] | 5538 | {
|
---|
| 5539 | // copy viewcells memory efficiently
|
---|
[1159] | 5540 | ray.mViewCells.reserve(viewcells.size());
|
---|
| 5541 | ray.mViewCells = viewcells;
|
---|
| 5542 | }
|
---|
| 5543 |
|
---|
| 5544 | ViewCellContainer::const_iterator it = viewcells.begin();
|
---|
| 5545 |
|
---|
| 5546 | for (; it != viewcells.end(); ++ it)
|
---|
| 5547 | {
|
---|
| 5548 | ViewCell *viewcell = *it;
|
---|
| 5549 |
|
---|
| 5550 | if (viewcell->GetValid())
|
---|
| 5551 | {
|
---|
| 5552 | // if ray not outside of view space
|
---|
| 5553 | float contribution;
|
---|
| 5554 |
|
---|
| 5555 | if (ray.mTerminationObject)
|
---|
| 5556 | {
|
---|
[1168] | 5557 | // todo: maybe not correct for kd node pvs
|
---|
[1159] | 5558 | if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
|
---|
| 5559 | ray.mPdf, contribution))
|
---|
| 5560 | {
|
---|
| 5561 | ++ ray.mPvsContribution;
|
---|
| 5562 | }
|
---|
| 5563 |
|
---|
| 5564 | ray.mRelativePvsContribution += contribution;
|
---|
| 5565 | }
|
---|
| 5566 |
|
---|
| 5567 | // for directional sampling it is important to count only contributions
|
---|
| 5568 | // made in one direction!!!
|
---|
| 5569 | // the other contributions of this sample will be counted for the oposite ray!
|
---|
[1166] | 5570 | #if SAMPLE_ORIGIN_OBJECTS
|
---|
[1159] | 5571 | if (ray.mOriginObject &&
|
---|
| 5572 | viewcell->GetPvs().GetSampleContribution(ray.mOriginObject,
|
---|
| 5573 | ray.mPdf,
|
---|
| 5574 | contribution))
|
---|
| 5575 | {
|
---|
| 5576 | ++ ray.mPvsContribution;
|
---|
| 5577 | ray.mRelativePvsContribution += contribution;
|
---|
| 5578 | }
|
---|
| 5579 | #endif
|
---|
| 5580 | }
|
---|
| 5581 | }
|
---|
| 5582 |
|
---|
[1259] | 5583 | if (!addRays)
|
---|
| 5584 | return ray.mRelativePvsContribution;
|
---|
| 5585 |
|
---|
| 5586 | // sampled objects are stored in the pvs
|
---|
| 5587 | for (it = viewcells.begin(); it != viewcells.end(); ++ it)
|
---|
[1159] | 5588 | {
|
---|
[1259] | 5589 | ViewCell *viewCell = *it;
|
---|
[1159] | 5590 |
|
---|
[1259] | 5591 | if (!viewCell->GetValid())
|
---|
| 5592 | break;
|
---|
[1159] | 5593 |
|
---|
[1259] | 5594 | AddSampleToPvs(
|
---|
| 5595 | ray.mTerminationObject,
|
---|
| 5596 | ray.mTermination,
|
---|
| 5597 | viewCell,
|
---|
| 5598 | ray.mPdf,
|
---|
| 5599 | ray.mRelativePvsContribution);
|
---|
| 5600 |
|
---|
[1166] | 5601 | #if SAMPLE_ORIGIN_OBJECTS
|
---|
[1168] | 5602 |
|
---|
[1259] | 5603 | AddSampleToPvs(
|
---|
| 5604 | ray.mOriginObject,
|
---|
| 5605 | ray.mOrigin,
|
---|
| 5606 | viewCell,
|
---|
| 5607 | ray.mPdf,
|
---|
| 5608 | ray.mRelativePvsContribution);
|
---|
| 5609 | #endif
|
---|
[1159] | 5610 | }
|
---|
| 5611 |
|
---|
| 5612 | return ray.mRelativePvsContribution;
|
---|
| 5613 | }
|
---|
| 5614 |
|
---|
| 5615 |
|
---|
[1259] | 5616 | bool VspOspViewCellsManager::AddSampleToPvs(Intersectable *obj,
|
---|
| 5617 | const Vector3 &hitPoint,
|
---|
| 5618 | ViewCell *vc,
|
---|
| 5619 | const float pdf,
|
---|
| 5620 | float &contribution) const
|
---|
| 5621 | {
|
---|
[1278] | 5622 | return mHierarchyManager->AddSampleToPvs(obj, hitPoint, vc, pdf, contribution);
|
---|
[1259] | 5623 | }
|
---|
| 5624 |
|
---|
| 5625 |
|
---|
[1021] | 5626 | void VspOspViewCellsManager::PrepareLoadedViewCells()
|
---|
| 5627 | {
|
---|
| 5628 | // TODO
|
---|
| 5629 | }
|
---|
[580] | 5630 |
|
---|
[1201] | 5631 |
|
---|
| 5632 | ViewCellsManager *VspOspViewCellsManager::LoadViewCells(const string &filename,
|
---|
| 5633 | ObjectContainer *objects,
|
---|
| 5634 | const bool finalizeViewCells,
|
---|
| 5635 | BoundingBoxConverter *bconverter)
|
---|
| 5636 |
|
---|
| 5637 | {
|
---|
| 5638 | ViewCellsManager *vm =
|
---|
| 5639 | ViewCellsManager::LoadViewCells(filename, objects, finalizeViewCells, bconverter);
|
---|
[1278] | 5640 | #if 0
|
---|
| 5641 | // insert scene objects in tree
|
---|
[1201] | 5642 | mOspTree->InsertObjects(mOspTree->GetRoot(), *objects);
|
---|
[1278] | 5643 | #endif
|
---|
[1201] | 5644 | return vm;
|
---|
| 5645 | }
|
---|
| 5646 |
|
---|
| 5647 |
|
---|
[1184] | 5648 | #if TEST_EVALUATION
|
---|
[1178] | 5649 | void VspOspViewCellsManager::EvalViewCellPartition()
|
---|
| 5650 | {
|
---|
[1180] | 5651 | int castSamples = (int)storedRays.size();
|
---|
[1178] | 5652 |
|
---|
| 5653 | char s[64];
|
---|
| 5654 | char statsPrefix[100];
|
---|
[1180] | 5655 |
|
---|
[1178] | 5656 | Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
|
---|
| 5657 |
|
---|
| 5658 | Debug << "view cell stats prefix: " << statsPrefix << endl;
|
---|
| 5659 |
|
---|
| 5660 | // should directional sampling be used?
|
---|
| 5661 | bool dirSamples = (mEvaluationSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
|
---|
| 5662 |
|
---|
| 5663 | cout << "reseting pvs ... ";
|
---|
| 5664 |
|
---|
| 5665 | const bool startFromZero = true;
|
---|
| 5666 |
|
---|
[1184] | 5667 |
|
---|
[1178] | 5668 | // reset pvs and start over from zero
|
---|
| 5669 | if (startFromZero)
|
---|
| 5670 | {
|
---|
| 5671 | mViewCellsTree->ResetPvs();
|
---|
| 5672 | }
|
---|
| 5673 | else // start from current sampless
|
---|
| 5674 | {
|
---|
| 5675 | // statistics before casting more samples
|
---|
| 5676 | cout << "compute new statistics ... ";
|
---|
| 5677 | sprintf(s, "-%09d-eval.log", castSamples);
|
---|
| 5678 | string fName = string(statsPrefix) + string(s);
|
---|
| 5679 |
|
---|
| 5680 | mViewCellsTree->ExportStats(fName);
|
---|
| 5681 | cout << "finished" << endl;
|
---|
| 5682 | }
|
---|
| 5683 | cout << "finished" << endl;
|
---|
| 5684 |
|
---|
| 5685 | cout << "Evaluating view cell partition ... " << endl;
|
---|
| 5686 |
|
---|
| 5687 | VssRayContainer evaluationSamples = storedRays;
|
---|
| 5688 | const int samplingType = mEvaluationSamplingType;
|
---|
| 5689 |
|
---|
| 5690 | cout << "computing sample contributions of " << (int)evaluationSamples.size() << " samples ... ";
|
---|
| 5691 |
|
---|
| 5692 | ComputeSampleContributions(evaluationSamples, true, false);
|
---|
| 5693 |
|
---|
| 5694 | cout << "finished" << endl;
|
---|
| 5695 |
|
---|
| 5696 | cout << "compute new statistics ... ";
|
---|
| 5697 | Debug << "compute new statistics ... ";
|
---|
| 5698 |
|
---|
[1221] | 5699 | // propagate pvs or pvs size information
|
---|
[1178] | 5700 | ObjectPvs pvs;
|
---|
| 5701 | UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), pvs);
|
---|
| 5702 |
|
---|
[1259] | 5703 | ///////////////////////////////////////
|
---|
| 5704 | //-- temporary matt: test render cost
|
---|
[1178] | 5705 |
|
---|
[1259] | 5706 | sprintf(s, "-%09d-eval.log", castSamples);
|
---|
| 5707 | string fileName = string(statsPrefix) + string(s);
|
---|
[1178] | 5708 |
|
---|
[1259] | 5709 | ViewCellContainer leaves;
|
---|
[1184] | 5710 |
|
---|
[1259] | 5711 | mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), leaves);
|
---|
| 5712 | float rc = 0;
|
---|
| 5713 | ViewCellContainer::const_iterator vit, vit_end = leaves.end();
|
---|
| 5714 | for (vit = leaves.begin(); vit != vit_end; ++ vit)
|
---|
| 5715 | {
|
---|
| 5716 | ViewCell *vc = *vit;
|
---|
[1184] | 5717 | int pvs = vc->GetPvs().CountObjectsInPvs();
|
---|
[1259] | 5718 | float vol = vc->GetVolume();
|
---|
| 5719 | rc += pvs * vol;
|
---|
[1184] | 5720 |
|
---|
| 5721 | }
|
---|
| 5722 |
|
---|
[1302] | 5723 | Debug << "\nrendercost hack: " << rc / mHierarchyManager->GetVspTree()->GetBoundingBox().GetVolume() << endl;
|
---|
[1178] | 5724 | mViewCellsTree->ExportStats(fileName);
|
---|
| 5725 | cout << "finished" << endl;
|
---|
| 5726 |
|
---|
| 5727 | disposeRays(evaluationSamples, NULL);
|
---|
| 5728 | }
|
---|
[1179] | 5729 | #endif
|
---|
[580] | 5730 |
|
---|
[860] | 5731 |
|
---|
[878] | 5732 | }
|
---|