[372] | 1 | #include "SceneGraph.h"
|
---|
| 2 | #include "KdTree.h"
|
---|
| 3 | #include "VssPreprocessor.h"
|
---|
| 4 | #include "X3dExporter.h"
|
---|
| 5 | #include "Environment.h"
|
---|
| 6 | #include "MutualVisibility.h"
|
---|
| 7 | #include "Polygon3.h"
|
---|
| 8 | #include "ViewCell.h"
|
---|
[376] | 9 | #include "VssRay.h"
|
---|
[382] | 10 | #include "VssTree.h"
|
---|
[439] | 11 | #include "ViewCellsManager.h"
|
---|
[452] | 12 | #include "RenderSimulator.h"
|
---|
[531] | 13 | #include "Beam.h"
|
---|
| 14 | #include "GlRenderer.h"
|
---|
[1221] | 15 | #include "Intersectable.h"
|
---|
[1521] | 16 | #include "RayCaster.h"
|
---|
[372] | 17 |
|
---|
[1521] | 18 |
|
---|
[1577] | 19 |
|
---|
[863] | 20 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 21 |
|
---|
[1577] | 22 |
|
---|
[427] | 23 | bool use2dSampling = false;
|
---|
[534] | 24 | bool useViewspacePlane = false;
|
---|
[427] | 25 |
|
---|
[372] | 26 | VssPreprocessor::VssPreprocessor():
|
---|
[1486] | 27 | mVssRays()
|
---|
[372] | 28 | {
|
---|
| 29 | // this should increase coherence of the samples
|
---|
[1004] | 30 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 31 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
|
---|
| 32 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
|
---|
| 33 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
|
---|
| 34 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
[574] | 35 |
|
---|
[1004] | 36 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
| 37 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
| 38 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
|
---|
| 39 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
|
---|
| 40 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.enlargeViewSpace", mEnlargeViewSpace);
|
---|
| 41 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
[694] | 42 |
|
---|
[508] | 43 | useViewspacePlane = mUseViewSpaceBox; //hack
|
---|
[675] | 44 |
|
---|
| 45 | Debug << "*********** vss preprocessor options **************" << endl;
|
---|
| 46 | Debug << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
| 47 | Debug << "enlarge view space=" << mEnlargeViewSpace << endl;
|
---|
| 48 | Debug << "*********** end vss preprocessor options **************" << endl;
|
---|
| 49 |
|
---|
[372] | 50 | mStats.open("stats.log");
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[685] | 53 |
|
---|
[372] | 54 | VssPreprocessor::~VssPreprocessor()
|
---|
| 55 | {
|
---|
[674] | 56 | CLEAR_CONTAINER(mVssRays);
|
---|
[372] | 57 | }
|
---|
| 58 |
|
---|
[685] | 59 |
|
---|
[376] | 60 | Vector3
|
---|
[382] | 61 | VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
[372] | 62 | {
|
---|
[434] | 63 | AxisAlignedBox3 box;
|
---|
[468] | 64 |
|
---|
[434] | 65 | if (viewSpaceBox)
|
---|
| 66 | box =*viewSpaceBox;
|
---|
[468] | 67 | else
|
---|
[434] | 68 | box = mKdTree->GetBox();
|
---|
[468] | 69 |
|
---|
[434] | 70 | // shrink the box in the y direction
|
---|
| 71 | return box.GetRandomPoint();
|
---|
[372] | 72 | }
|
---|
| 73 |
|
---|
[376] | 74 | Vector3
|
---|
[427] | 75 | VssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
[434] | 76 | AxisAlignedBox3 *viewSpaceBox
|
---|
| 77 | )
|
---|
[372] | 78 | {
|
---|
[434] | 79 | Vector3 point;
|
---|
[600] | 80 | if (!use2dSampling)
|
---|
| 81 | {
|
---|
| 82 | if (0)
|
---|
| 83 | {
|
---|
| 84 | Vector3 normal;
|
---|
| 85 | int i = Random((int)mObjects.size());
|
---|
| 86 | Intersectable *object = mObjects[i];
|
---|
| 87 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 88 | }
|
---|
| 89 | else
|
---|
| 90 | point = mKdTree->GetBox().GetRandomPoint();
|
---|
[534] | 91 | // point = viewpoint + UniformRandomVector();
|
---|
[600] | 92 | }
|
---|
| 93 | else
|
---|
| 94 | {
|
---|
| 95 | AxisAlignedBox3 box;
|
---|
[534] | 96 |
|
---|
[600] | 97 | if (viewSpaceBox)
|
---|
| 98 | box =*viewSpaceBox;
|
---|
| 99 | else
|
---|
| 100 | box = mKdTree->GetBox();
|
---|
[468] | 101 |
|
---|
[600] | 102 | point = box.GetRandomPoint();
|
---|
| 103 | point.y = viewpoint.y;
|
---|
[434] | 104 | }
|
---|
[468] | 105 |
|
---|
[434] | 106 | return point - viewpoint;
|
---|
[372] | 107 | }
|
---|
| 108 |
|
---|
[401] | 109 | int
|
---|
[427] | 110 | VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
|
---|
[434] | 111 | const int desiredSamples,
|
---|
| 112 | SimpleRayContainer &rays
|
---|
| 113 | )
|
---|
[401] | 114 | {
|
---|
[434] | 115 | int num;
|
---|
| 116 | if (0) {
|
---|
| 117 | float minRayContribution;
|
---|
| 118 | float maxRayContribution;
|
---|
| 119 | float avgRayContribution;
|
---|
[468] | 120 |
|
---|
[434] | 121 | vssTree->GetRayContributionStatistics(minRayContribution,
|
---|
| 122 | maxRayContribution,
|
---|
| 123 | avgRayContribution);
|
---|
[468] | 124 |
|
---|
[434] | 125 | cout<<
|
---|
| 126 | "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
|
---|
| 127 | "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
|
---|
| 128 | "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
|
---|
[468] | 129 |
|
---|
[434] | 130 | float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
|
---|
| 131 | num = vssTree->GenerateRays(p, rays);
|
---|
| 132 | } else {
|
---|
[534] | 133 | int leaves = vssTree->stat.Leaves();
|
---|
[434] | 134 | num = vssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
| 135 | }
|
---|
[468] | 136 |
|
---|
[434] | 137 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
[468] | 138 |
|
---|
[434] | 139 | return num;
|
---|
[427] | 140 | }
|
---|
[376] | 141 |
|
---|
| 142 |
|
---|
[427] | 143 | bool
|
---|
| 144 | VssPreprocessor::ExportRays(const char *filename,
|
---|
[434] | 145 | const VssRayContainer &vssRays,
|
---|
| 146 | const int number
|
---|
| 147 | )
|
---|
[427] | 148 | {
|
---|
[434] | 149 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
[468] | 150 |
|
---|
[434] | 151 | Exporter *exporter = NULL;
|
---|
| 152 | exporter = Exporter::GetExporter(filename);
|
---|
[1074] | 153 | exporter->SetWireframe();
|
---|
| 154 | exporter->ExportKdTree(*mKdTree);
|
---|
[434] | 155 | exporter->SetFilled();
|
---|
[1328] | 156 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
[434] | 157 | exporter->SetWireframe();
|
---|
[427] | 158 |
|
---|
[1486] | 159 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
[1563] | 160 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
[1486] | 161 | exporter->ResetForcedMaterial();
|
---|
[468] | 162 |
|
---|
[1486] | 163 |
|
---|
[534] | 164 | VssRayContainer rays;
|
---|
| 165 | vssRays.SelectRays(number, rays);
|
---|
| 166 |
|
---|
[685] | 167 | //exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
[468] | 168 |
|
---|
[434] | 169 | delete exporter;
|
---|
[401] | 170 |
|
---|
[434] | 171 | cout<<"done."<<endl<<flush;
|
---|
[427] | 172 |
|
---|
[434] | 173 | return true;
|
---|
[401] | 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
[372] | 177 | bool
|
---|
[434] | 178 | VssPreprocessor::ExportVssTree(char *filename,
|
---|
[438] | 179 | VssTree *tree,
|
---|
| 180 | const Vector3 &dir
|
---|
| 181 | )
|
---|
[434] | 182 | {
|
---|
| 183 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 184 | exporter->SetFilled();
|
---|
[1328] | 185 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
[438] | 186 | // exporter->SetWireframe();
|
---|
| 187 | bool result = exporter->ExportVssTree2( *tree, dir );
|
---|
[434] | 188 | delete exporter;
|
---|
| 189 | return result;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | bool
|
---|
[427] | 193 | VssPreprocessor::ExportVssTreeLeaf(char *filename,
|
---|
[434] | 194 | VssTree *tree,
|
---|
| 195 | VssTreeLeaf *leaf)
|
---|
[427] | 196 | {
|
---|
[434] | 197 | Exporter *exporter = NULL;
|
---|
| 198 | exporter = Exporter::GetExporter(filename);
|
---|
| 199 | exporter->SetWireframe();
|
---|
| 200 | exporter->ExportKdTree(*mKdTree);
|
---|
[468] | 201 |
|
---|
[1486] | 202 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
[1563] | 203 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
[1486] | 204 | exporter->ResetForcedMaterial();
|
---|
[468] | 205 |
|
---|
[434] | 206 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 207 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
| 208 | exporter->ResetForcedMaterial();
|
---|
[468] | 209 |
|
---|
[434] | 210 | VssRayContainer rays[4];
|
---|
| 211 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
| 212 | int k = leaf->rays[i].GetRayClass();
|
---|
| 213 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
| 214 | }
|
---|
[468] | 215 |
|
---|
[434] | 216 | // SOURCE RAY
|
---|
| 217 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
| 218 | // TERMINATION RAY
|
---|
| 219 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
| 220 | // PASSING_RAY
|
---|
| 221 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
| 222 | // CONTAINED_RAY
|
---|
| 223 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
[427] | 224 |
|
---|
[434] | 225 | delete exporter;
|
---|
| 226 | return true;
|
---|
[427] | 227 | }
|
---|
| 228 |
|
---|
| 229 | void
|
---|
| 230 | VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
|
---|
| 231 | {
|
---|
[434] | 232 | vector<VssTreeLeaf *> leaves;
|
---|
| 233 | tree->CollectLeaves(leaves);
|
---|
[427] | 234 |
|
---|
[434] | 235 | int num = 0;
|
---|
| 236 | int i;
|
---|
| 237 | float p = number / (float)leaves.size();
|
---|
| 238 | for (i=0; i < leaves.size(); i++) {
|
---|
| 239 | if (RandomValue(0,1) < p) {
|
---|
| 240 | char filename[64];
|
---|
| 241 | sprintf(filename, "vss-leaf-%04d.x3d", num);
|
---|
| 242 | ExportVssTreeLeaf(filename, tree, leaves[i]);
|
---|
| 243 | num++;
|
---|
[427] | 244 | }
|
---|
[434] | 245 | if (num >= number)
|
---|
| 246 | break;
|
---|
| 247 | }
|
---|
[427] | 248 | }
|
---|
| 249 |
|
---|
[685] | 250 |
|
---|
[535] | 251 | void VssPreprocessor::TestBeamCasting(VssTree *tree,
|
---|
| 252 | ViewCellsManager *vm,
|
---|
| 253 | const ObjectContainer &objects)
|
---|
[530] | 254 | {
|
---|
[540] | 255 | //debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
| 256 | // renderer->resize(640, 480);
|
---|
| 257 | //debuggerWidget->resize(640, 480);
|
---|
| 258 |
|
---|
[531] | 259 | vector<VssTreeLeaf *> leaves;
|
---|
| 260 | tree->CollectLeaves(leaves);
|
---|
[530] | 261 |
|
---|
[535] | 262 | Exporter *exporter = Exporter::GetExporter("shafts.x3d");
|
---|
| 263 |
|
---|
| 264 | exporter->SetWireframe();
|
---|
[540] | 265 | exporter->ExportGeometry(objects);
|
---|
[535] | 266 | exporter->SetFilled();
|
---|
| 267 | //Randomize();
|
---|
[1145] | 268 | // §§matt
|
---|
| 269 | // debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
[540] | 270 |
|
---|
| 271 | /*debuggerWidget->mBeam = beam;
|
---|
| 272 | debuggerWidget->mSourceObject = sourceObj;
|
---|
| 273 | debuggerWidget->mSamples = 10000;
|
---|
| 274 |
|
---|
| 275 | Debug << "showing window" << endl;
|
---|
[1145] | 276 | debuggerWidget->show();
|
---|
[540] | 277 |
|
---|
[1145] | 278 | renderer->makeCurrent();*/
|
---|
[540] | 279 |
|
---|
| 280 | for (int i = 0; i < 10; ++ i)
|
---|
[530] | 281 | {
|
---|
[540] | 282 | Beam beam;
|
---|
| 283 | Intersectable *sourceObj = mObjects[5];
|
---|
| 284 |
|
---|
[531] | 285 | const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
|
---|
| 286 | VssTreeLeaf *leaf = leaves[index];
|
---|
[530] | 287 |
|
---|
[535] | 288 | AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
|
---|
[531] | 289 | AxisAlignedBox3 box = tree->GetBBox(leaf);
|
---|
[532] | 290 |
|
---|
[535] | 291 | beam.Construct(box, dirBox);
|
---|
[532] | 292 |
|
---|
| 293 | // collect kd leaves and view cells
|
---|
| 294 | mKdTree->CastBeam(beam);
|
---|
| 295 | vm->CastBeam(beam);
|
---|
| 296 |
|
---|
[577] | 297 | Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
|
---|
| 298 | << (int)beam.mKdNodes.size() << " kd nodes" << endl;
|
---|
[532] | 299 |
|
---|
[530] | 300 | BeamSampleStatistics stats;
|
---|
[1145] | 301 | // §§matt
|
---|
| 302 | /* renderer->SampleBeamContributions(sourceObj,
|
---|
[589] | 303 | beam,
|
---|
| 304 | 200000,
|
---|
[531] | 305 | stats);
|
---|
[530] | 306 |
|
---|
[540] | 307 | char s[64]; sprintf(s, "shaft%04d.png", i);
|
---|
| 308 |
|
---|
| 309 | QImage image = renderer->toImage();
|
---|
| 310 | image.save(s, "PNG");
|
---|
[532] | 311 | Debug << "beam statistics: " << stats << endl << endl;
|
---|
[1145] | 312 | */
|
---|
[540] | 313 | if (1)
|
---|
| 314 | {
|
---|
| 315 | AxisAlignedBox3 sbox = mSceneGraph->GetBox();
|
---|
| 316 | Vector3 bmin = sbox.Min() - 150.0f;
|
---|
| 317 | Vector3 bmax = sbox.Max() + 150.0f;
|
---|
| 318 | AxisAlignedBox3 vbox(bmin, bmax);
|
---|
[535] | 319 |
|
---|
[540] | 320 | exporter->ExportBeam(beam, vbox);
|
---|
| 321 | }
|
---|
[535] | 322 |
|
---|
[540] | 323 | bool exportViewCells = false;
|
---|
[535] | 324 | if (exportViewCells)
|
---|
| 325 | {
|
---|
| 326 | ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
|
---|
| 327 |
|
---|
| 328 | for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
|
---|
| 329 | {
|
---|
| 330 | BspNodeGeometry geom;
|
---|
| 331 | AxisAlignedBox3 vbox;
|
---|
| 332 | vbox.Initialize();
|
---|
[538] | 333 | vbox.Include((*it)->GetMesh());
|
---|
| 334 |
|
---|
| 335 | exporter->SetWireframe();
|
---|
[535] | 336 | exporter->ExportBox(vbox);
|
---|
[538] | 337 | exporter->SetFilled();
|
---|
| 338 | exporter->ExportViewCell(*it);
|
---|
[535] | 339 | }
|
---|
[538] | 340 |
|
---|
| 341 | /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
| 342 |
|
---|
| 343 | for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
|
---|
| 344 | {
|
---|
| 345 | exporter->ExportBox(mKdTree->GetBox((*it)));
|
---|
| 346 | }*/
|
---|
[535] | 347 | }
|
---|
[530] | 348 | }
|
---|
[540] | 349 | /*while (1)
|
---|
| 350 | { debuggerWidget->repaint();
|
---|
| 351 | };*/
|
---|
[535] | 352 | delete exporter;
|
---|
[530] | 353 | }
|
---|
| 354 |
|
---|
[540] | 355 |
|
---|
[434] | 356 | float
|
---|
| 357 | VssPreprocessor::GetAvgPvsSize(VssTree *tree,
|
---|
| 358 | const vector<AxisAlignedBox3> &viewcells
|
---|
| 359 | )
|
---|
| 360 | {
|
---|
| 361 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
| 362 |
|
---|
| 363 | int sum = 0;
|
---|
| 364 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
| 365 | sum += tree->GetPvsSize(*it);
|
---|
[468] | 366 |
|
---|
[434] | 367 | return sum/(float)viewcells.size();
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[427] | 370 | bool
|
---|
[372] | 371 | VssPreprocessor::ComputeVisibility()
|
---|
| 372 | {
|
---|
[579] | 373 | Debug << "type: vss" << endl;
|
---|
[468] | 374 |
|
---|
[1563] | 375 | const long startTime = GetTime();
|
---|
| 376 | int totalSamples = 0;
|
---|
[468] | 377 |
|
---|
[1563] | 378 | if (mUseViewSpaceBox)
|
---|
| 379 | {
|
---|
| 380 | AxisAlignedBox3 viewSpaceBox;
|
---|
| 381 | viewSpaceBox.Initialize();
|
---|
[372] | 382 |
|
---|
[1563] | 383 | viewSpaceBox = AxisAlignedBox3(mKdTree->GetBox());
|
---|
| 384 | AxisAlignedBox3 tbox(mViewCellsManager->GetViewSpaceBox());
|
---|
[372] | 385 |
|
---|
[1563] | 386 | const Vector3 size = tbox.Size();
|
---|
| 387 | viewSpaceBox.SetMax(0, tbox.Max(0) + size[0] * 0.5f);
|
---|
| 388 | viewSpaceBox.SetMin(0, tbox.Min(0) + size[0]);
|
---|
| 389 |
|
---|
| 390 | // change the view space of the view cells manager
|
---|
| 391 | mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
|
---|
| 392 | }
|
---|
[468] | 393 |
|
---|
[427] | 394 |
|
---|
[1563] | 395 | mSceneGraph->CollectObjects(&mObjects);
|
---|
[468] | 396 |
|
---|
[1563] | 397 | if (!mLoadViewCells)
|
---|
| 398 | {
|
---|
| 399 | //-- generate new view cells from the scratch
|
---|
| 400 | //-- for construction the manager uses it's own set of samples
|
---|
| 401 | ConstructViewCells();
|
---|
| 402 | }
|
---|
| 403 | #if 0
|
---|
| 404 | else
|
---|
| 405 | {
|
---|
| 406 | //-- load view cells from file
|
---|
| 407 | //-- test successful view cells loading by exporting them again
|
---|
| 408 | VssRayContainer dummies;
|
---|
| 409 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
| 410 | mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
| 411 | }
|
---|
| 412 | #endif
|
---|
[579] | 413 |
|
---|
[1563] | 414 | VssTree *vssTree = NULL;
|
---|
| 415 | const long initialTime = GetTime();
|
---|
[677] | 416 |
|
---|
[1563] | 417 | if (mLoadInitialSamples)
|
---|
| 418 | {
|
---|
| 419 | cout << "Loading samples from file ... ";
|
---|
| 420 | LoadSamples(mVssRays, mObjects);
|
---|
| 421 | cout << "finished\n" << endl;
|
---|
| 422 | totalSamples = (int)mVssRays.size();
|
---|
| 423 | }
|
---|
| 424 | else
|
---|
| 425 | {
|
---|
| 426 | while (totalSamples < mInitialSamples) {
|
---|
| 427 | int passContributingSamples = 0;
|
---|
| 428 | int passSampleContributions = 0;
|
---|
| 429 | int passSamples = 0;
|
---|
[598] | 430 |
|
---|
[1563] | 431 | int index = 0;
|
---|
[1052] | 432 |
|
---|
[1563] | 433 | int sampleContributions;
|
---|
[590] | 434 |
|
---|
[1563] | 435 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
| 436 | for (int k=0; k < s; k++)
|
---|
| 437 | {
|
---|
| 438 | Vector3 viewpoint;
|
---|
[1404] | 439 |
|
---|
[1563] | 440 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
| 441 | const Vector3 direction = GetDirection(viewpoint, &mViewCellsManager->GetViewSpaceBox());
|
---|
[468] | 442 |
|
---|
[1563] | 443 | const SimpleRay sray(viewpoint, direction);
|
---|
| 444 | sampleContributions = mRayCaster->CastRay(sray, mVssRays, mViewCellsManager->GetViewSpaceBox(), true);
|
---|
[468] | 445 |
|
---|
[1563] | 446 | if (sampleContributions) {
|
---|
| 447 | passContributingSamples ++;
|
---|
| 448 | passSampleContributions += sampleContributions;
|
---|
| 449 | }
|
---|
| 450 | passSamples++;
|
---|
| 451 | totalSamples++;
|
---|
| 452 | }
|
---|
[468] | 453 |
|
---|
[1563] | 454 | mPass++;
|
---|
| 455 | int pvsSize = 0;
|
---|
| 456 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
| 457 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
[468] | 458 |
|
---|
[1563] | 459 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 460 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
| 461 | << "#SampleContributions=" << passSampleContributions << " ("
|
---|
| 462 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
| 463 | << pvsSize/(float)mObjects.size() << endl
|
---|
| 464 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
[534] | 465 |
|
---|
[1563] | 466 | mStats <<
|
---|
| 467 | "#Pass\n" <<mPass<<endl<<
|
---|
| 468 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
| 469 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
| 470 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
| 471 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
| 472 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
| 473 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
[490] | 474 | }
|
---|
[468] | 475 |
|
---|
[1563] | 476 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
[468] | 477 |
|
---|
| 478 |
|
---|
[534] | 479 |
|
---|
[1563] | 480 | }
|
---|
[534] | 481 |
|
---|
[564] | 482 |
|
---|
[1563] | 483 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
|
---|
| 484 | Debug << (int)mVssRays.size() << " rays generated in "
|
---|
| 485 | << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
|
---|
[401] | 486 |
|
---|
[1563] | 487 | if (mStoreInitialSamples)
|
---|
| 488 | {
|
---|
| 489 | cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
|
---|
| 490 | ExportSamples(mVssRays);
|
---|
| 491 | cout << "finished\n" << endl;
|
---|
[491] | 492 |
|
---|
[1563] | 493 | /*VssRayContainer dummyRays;
|
---|
| 494 | LoadSamples(dummyRays, mObjects);
|
---|
| 495 | Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
|
---|
[491] | 496 |
|
---|
[1563] | 497 | for (int i = 0; i < (int)mVssRays.size(); ++ i)
|
---|
| 498 | {
|
---|
| 499 | Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
|
---|
| 500 | Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
|
---|
| 501 | }*/
|
---|
| 502 | }
|
---|
[468] | 503 |
|
---|
[372] | 504 |
|
---|
[1563] | 505 | //int numExportRays = 2000;
|
---|
| 506 | int numExportRays = 0;
|
---|
[468] | 507 |
|
---|
[1563] | 508 | if (numExportRays) {
|
---|
| 509 | char filename[64];
|
---|
| 510 | sprintf(filename, "vss-rays-initial.x3d");
|
---|
| 511 | ExportRays(filename, mVssRays, numExportRays);
|
---|
| 512 | }
|
---|
[468] | 513 |
|
---|
[1563] | 514 | vssTree = new VssTree;
|
---|
| 515 | // viewcells = Construct(mVssRays);
|
---|
[468] | 516 |
|
---|
[1563] | 517 | vssTree->Construct(mVssRays, NULL);
|
---|
| 518 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
[1074] | 519 |
|
---|
[1563] | 520 | if (0) ExportRays("kdtree.x3d", mVssRays, 10);
|
---|
[430] | 521 |
|
---|
[1563] | 522 | if (0)
|
---|
| 523 | {
|
---|
| 524 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
| 525 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
| 526 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
| 527 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
| 528 | ExportVssTreeLeaves(vssTree, 10);
|
---|
| 529 | }
|
---|
[468] | 530 |
|
---|
[1563] | 531 | // viewcells->UpdatePVS(newVssRays);
|
---|
| 532 | // get viewcells as kd tree boxes
|
---|
| 533 | vector<AxisAlignedBox3> kdViewcells;
|
---|
| 534 | if (0) {
|
---|
| 535 | vector<KdLeaf *> leaves;
|
---|
| 536 | mKdTree->CollectLeaves(leaves);
|
---|
| 537 | vector<KdLeaf *>::const_iterator it;
|
---|
| 538 | int targetLeaves = 50;
|
---|
| 539 | float prob = targetLeaves/(float)leaves.size();
|
---|
| 540 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
| 541 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
| 542 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
[434] | 543 |
|
---|
[1563] | 544 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 545 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
| 546 | }
|
---|
[468] | 547 |
|
---|
[448] | 548 |
|
---|
[1563] | 549 | int samples = 0;
|
---|
| 550 | int pass = 0;
|
---|
[468] | 551 |
|
---|
| 552 |
|
---|
[1563] | 553 | // cast view cell samples
|
---|
| 554 | while (samples < mVssSamples)
|
---|
| 555 | {
|
---|
[468] | 556 |
|
---|
[1563] | 557 | int num = mVssSamplesPerPass;
|
---|
| 558 | SimpleRayContainer rays;
|
---|
| 559 | VssRayContainer vssRays;
|
---|
[427] | 560 |
|
---|
[1563] | 561 | if (!mUseImportanceSampling) {
|
---|
| 562 | for (int j=0; j < num; j++) {
|
---|
| 563 | Vector3 viewpoint;
|
---|
| 564 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
| 565 | Vector3 direction = GetDirection(viewpoint, NULL);
|
---|
| 566 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
| 567 | }
|
---|
| 568 | } else {
|
---|
| 569 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
| 570 | }
|
---|
[430] | 571 |
|
---|
[1563] | 572 | CastRays(rays, vssRays, true);
|
---|
| 573 | vssTree->AddRays(vssRays);
|
---|
[468] | 574 |
|
---|
[1563] | 575 | if (0) {
|
---|
| 576 | int subdivided = vssTree->UpdateSubdivision();
|
---|
| 577 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 581 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
| 582 |
|
---|
| 583 | /// compute view cell contribution of rays
|
---|
| 584 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
| 585 |
|
---|
| 586 | if (numExportRays) {
|
---|
| 587 | char filename[64];
|
---|
| 588 | if (mUseImportanceSampling)
|
---|
| 589 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
| 590 | else
|
---|
| 591 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
| 592 |
|
---|
| 593 | ExportRays(filename, vssRays, numExportRays);
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | samples+=num;
|
---|
| 597 | float pvs = vssTree->GetAvgPvsSize();
|
---|
| 598 | cout<<"*****************************\n";
|
---|
| 599 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
| 600 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
| 601 | cout<<"*****************************\n";
|
---|
| 602 | // if (samples >= mVssSamples) break;
|
---|
| 603 | pass ++;
|
---|
[401] | 604 | }
|
---|
[386] | 605 |
|
---|
[1563] | 606 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
| 607 | {
|
---|
| 608 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
| 609 | }
|
---|
[448] | 610 |
|
---|
[1563] | 611 | if (0) Debug << vssTree->stat << endl;
|
---|
[553] | 612 |
|
---|
[1563] | 613 | if (0)
|
---|
| 614 | {
|
---|
| 615 | VssRayContainer viewCellRays;
|
---|
| 616 | // compute rays used for view cells construction
|
---|
| 617 | const int numRays = mViewCellsManager->GetVisualizationSamples();
|
---|
| 618 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
| 619 | }
|
---|
[468] | 620 |
|
---|
[535] | 621 |
|
---|
[1563] | 622 | ////////////////////
|
---|
| 623 | //-- render simulation after construction
|
---|
[605] | 624 |
|
---|
[1563] | 625 | mRenderSimulator->RenderScene();
|
---|
| 626 | SimulationStatistics ss;
|
---|
| 627 | mRenderSimulator->GetStatistics(ss);
|
---|
| 628 | Debug << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
| 629 | cout << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
[1414] | 630 |
|
---|
[1563] | 631 | delete vssTree;
|
---|
[468] | 632 |
|
---|
[1563] | 633 | return true;
|
---|
[466] | 634 | }
|
---|
[697] | 635 |
|
---|
[1281] | 636 | } |
---|