[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"
|
---|
[1883] | 17 | #include "SamplingStrategy.h"
|
---|
[2116] | 18 | #include "ViewCellBsp.h"
|
---|
[372] | 19 |
|
---|
[1521] | 20 |
|
---|
[863] | 21 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 22 |
|
---|
[1577] | 23 |
|
---|
[427] | 24 | bool use2dSampling = false;
|
---|
[534] | 25 | bool useViewspacePlane = false;
|
---|
[427] | 26 |
|
---|
[372] | 27 | VssPreprocessor::VssPreprocessor():
|
---|
[1486] | 28 | mVssRays()
|
---|
[372] | 29 | {
|
---|
| 30 | // this should increase coherence of the samples
|
---|
[1966] | 31 | // Environment::GetSingleton()->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 32 | // Environment::GetSingleton()->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
|
---|
| 33 | // Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
|
---|
| 34 | // Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
|
---|
[1004] | 35 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
[574] | 36 |
|
---|
[1004] | 37 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
| 38 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
| 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 | }
|
---|
| 51 |
|
---|
[685] | 52 |
|
---|
[372] | 53 | VssPreprocessor::~VssPreprocessor()
|
---|
| 54 | {
|
---|
[674] | 55 | CLEAR_CONTAINER(mVssRays);
|
---|
[372] | 56 | }
|
---|
| 57 |
|
---|
[685] | 58 |
|
---|
[376] | 59 | Vector3
|
---|
[382] | 60 | VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
[372] | 61 | {
|
---|
[434] | 62 | AxisAlignedBox3 box;
|
---|
[468] | 63 |
|
---|
[434] | 64 | if (viewSpaceBox)
|
---|
| 65 | box =*viewSpaceBox;
|
---|
[468] | 66 | else
|
---|
[434] | 67 | box = mKdTree->GetBox();
|
---|
[468] | 68 |
|
---|
[434] | 69 | // shrink the box in the y direction
|
---|
| 70 | return box.GetRandomPoint();
|
---|
[372] | 71 | }
|
---|
| 72 |
|
---|
[376] | 73 | Vector3
|
---|
[427] | 74 | VssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
[434] | 75 | AxisAlignedBox3 *viewSpaceBox
|
---|
| 76 | )
|
---|
[372] | 77 | {
|
---|
[434] | 78 | Vector3 point;
|
---|
[600] | 79 | if (!use2dSampling)
|
---|
| 80 | {
|
---|
| 81 | if (0)
|
---|
| 82 | {
|
---|
| 83 | Vector3 normal;
|
---|
| 84 | int i = Random((int)mObjects.size());
|
---|
| 85 | Intersectable *object = mObjects[i];
|
---|
| 86 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 87 | }
|
---|
| 88 | else
|
---|
| 89 | point = mKdTree->GetBox().GetRandomPoint();
|
---|
[534] | 90 | // point = viewpoint + UniformRandomVector();
|
---|
[600] | 91 | }
|
---|
| 92 | else
|
---|
| 93 | {
|
---|
| 94 | AxisAlignedBox3 box;
|
---|
[534] | 95 |
|
---|
[600] | 96 | if (viewSpaceBox)
|
---|
| 97 | box =*viewSpaceBox;
|
---|
| 98 | else
|
---|
| 99 | box = mKdTree->GetBox();
|
---|
[468] | 100 |
|
---|
[600] | 101 | point = box.GetRandomPoint();
|
---|
| 102 | point.y = viewpoint.y;
|
---|
[434] | 103 | }
|
---|
[468] | 104 |
|
---|
[434] | 105 | return point - viewpoint;
|
---|
[372] | 106 | }
|
---|
| 107 |
|
---|
[401] | 108 | int
|
---|
[427] | 109 | VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
|
---|
[434] | 110 | const int desiredSamples,
|
---|
| 111 | SimpleRayContainer &rays
|
---|
| 112 | )
|
---|
[401] | 113 | {
|
---|
[434] | 114 | int num;
|
---|
| 115 | if (0) {
|
---|
| 116 | float minRayContribution;
|
---|
| 117 | float maxRayContribution;
|
---|
| 118 | float avgRayContribution;
|
---|
[468] | 119 |
|
---|
[434] | 120 | vssTree->GetRayContributionStatistics(minRayContribution,
|
---|
| 121 | maxRayContribution,
|
---|
| 122 | avgRayContribution);
|
---|
[468] | 123 |
|
---|
[434] | 124 | cout<<
|
---|
| 125 | "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
|
---|
| 126 | "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
|
---|
| 127 | "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
|
---|
[468] | 128 |
|
---|
[434] | 129 | float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
|
---|
| 130 | num = vssTree->GenerateRays(p, rays);
|
---|
| 131 | } else {
|
---|
[534] | 132 | int leaves = vssTree->stat.Leaves();
|
---|
[434] | 133 | num = vssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
| 134 | }
|
---|
[468] | 135 |
|
---|
[434] | 136 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
[468] | 137 |
|
---|
[434] | 138 | return num;
|
---|
[427] | 139 | }
|
---|
[376] | 140 |
|
---|
| 141 |
|
---|
[427] | 142 | bool
|
---|
| 143 | VssPreprocessor::ExportRays(const char *filename,
|
---|
[434] | 144 | const VssRayContainer &vssRays,
|
---|
[2199] | 145 | const int number,
|
---|
| 146 | const bool exportScene
|
---|
[434] | 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 |
|
---|
[2283] | 285 | const int index = (int)RandomValue(0, (Real)((float)leaves.size() - 0.5f));
|
---|
[531] | 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 |
|
---|
[1723] | 378 | //mSceneGraph->CollectObjects(&mObjects);
|
---|
[372] | 379 |
|
---|
[1563] | 380 | if (!mLoadViewCells)
|
---|
| 381 | {
|
---|
| 382 | //-- generate new view cells from the scratch
|
---|
| 383 | //-- for construction the manager uses it's own set of samples
|
---|
| 384 | ConstructViewCells();
|
---|
| 385 | }
|
---|
| 386 | #if 0
|
---|
| 387 | else
|
---|
| 388 | {
|
---|
| 389 | //-- load view cells from file
|
---|
| 390 | //-- test successful view cells loading by exporting them again
|
---|
| 391 | VssRayContainer dummies;
|
---|
| 392 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
| 393 | mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
| 394 | }
|
---|
| 395 | #endif
|
---|
[579] | 396 |
|
---|
[1563] | 397 | VssTree *vssTree = NULL;
|
---|
| 398 | const long initialTime = GetTime();
|
---|
[677] | 399 |
|
---|
[1563] | 400 | if (mLoadInitialSamples)
|
---|
| 401 | {
|
---|
| 402 | cout << "Loading samples from file ... ";
|
---|
| 403 | LoadSamples(mVssRays, mObjects);
|
---|
| 404 | cout << "finished\n" << endl;
|
---|
| 405 | totalSamples = (int)mVssRays.size();
|
---|
| 406 | }
|
---|
| 407 | else
|
---|
| 408 | {
|
---|
[2065] | 409 | while (totalSamples < mInitialSamples)
|
---|
| 410 | {
|
---|
[1563] | 411 | int passContributingSamples = 0;
|
---|
| 412 | int passSampleContributions = 0;
|
---|
| 413 | int passSamples = 0;
|
---|
[598] | 414 |
|
---|
[1563] | 415 | int index = 0;
|
---|
[1052] | 416 |
|
---|
[1563] | 417 | int sampleContributions;
|
---|
[590] | 418 |
|
---|
[1563] | 419 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
| 420 | for (int k=0; k < s; k++)
|
---|
| 421 | {
|
---|
| 422 | Vector3 viewpoint;
|
---|
[1404] | 423 |
|
---|
[1563] | 424 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
[2199] | 425 | AxisAlignedBox3 vspBox = mViewCellsManager->GetViewSpaceBox();
|
---|
| 426 | const Vector3 direction = GetDirection(viewpoint, &vspBox);
|
---|
[468] | 427 |
|
---|
[1883] | 428 | const SimpleRay sray(viewpoint, direction,
|
---|
| 429 | SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION,
|
---|
| 430 | 1.0f);
|
---|
| 431 | sampleContributions = mRayCaster->CastRay(sray,
|
---|
| 432 | mVssRays,
|
---|
| 433 | mViewCellsManager->GetViewSpaceBox(),
|
---|
| 434 | true);
|
---|
[468] | 435 |
|
---|
[1563] | 436 | if (sampleContributions) {
|
---|
| 437 | passContributingSamples ++;
|
---|
| 438 | passSampleContributions += sampleContributions;
|
---|
| 439 | }
|
---|
| 440 | passSamples++;
|
---|
| 441 | totalSamples++;
|
---|
| 442 | }
|
---|
[468] | 443 |
|
---|
[1563] | 444 | mPass++;
|
---|
| 445 | int pvsSize = 0;
|
---|
| 446 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
| 447 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
[468] | 448 |
|
---|
[1563] | 449 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 450 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
| 451 | << "#SampleContributions=" << passSampleContributions << " ("
|
---|
| 452 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
| 453 | << pvsSize/(float)mObjects.size() << endl
|
---|
| 454 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
[534] | 455 |
|
---|
[1563] | 456 | mStats <<
|
---|
| 457 | "#Pass\n" <<mPass<<endl<<
|
---|
| 458 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
| 459 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
| 460 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
| 461 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
| 462 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
| 463 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
[490] | 464 | }
|
---|
[468] | 465 |
|
---|
[1563] | 466 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
[468] | 467 |
|
---|
| 468 |
|
---|
[534] | 469 |
|
---|
[1563] | 470 | }
|
---|
[534] | 471 |
|
---|
[564] | 472 |
|
---|
[1563] | 473 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
|
---|
| 474 | Debug << (int)mVssRays.size() << " rays generated in "
|
---|
| 475 | << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
|
---|
[401] | 476 |
|
---|
[1563] | 477 | if (mStoreInitialSamples)
|
---|
| 478 | {
|
---|
| 479 | cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
|
---|
| 480 | ExportSamples(mVssRays);
|
---|
| 481 | cout << "finished\n" << endl;
|
---|
| 482 | }
|
---|
[468] | 483 |
|
---|
[372] | 484 |
|
---|
[1563] | 485 | //int numExportRays = 2000;
|
---|
| 486 | int numExportRays = 0;
|
---|
[468] | 487 |
|
---|
[1563] | 488 | if (numExportRays) {
|
---|
| 489 | char filename[64];
|
---|
| 490 | sprintf(filename, "vss-rays-initial.x3d");
|
---|
| 491 | ExportRays(filename, mVssRays, numExportRays);
|
---|
| 492 | }
|
---|
[468] | 493 |
|
---|
[1563] | 494 | vssTree = new VssTree;
|
---|
| 495 | // viewcells = Construct(mVssRays);
|
---|
[468] | 496 |
|
---|
[1563] | 497 | vssTree->Construct(mVssRays, NULL);
|
---|
| 498 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
[1074] | 499 |
|
---|
[1563] | 500 | if (0) ExportRays("kdtree.x3d", mVssRays, 10);
|
---|
[430] | 501 |
|
---|
[1563] | 502 | if (0)
|
---|
| 503 | {
|
---|
| 504 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
| 505 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
| 506 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
| 507 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
| 508 | ExportVssTreeLeaves(vssTree, 10);
|
---|
| 509 | }
|
---|
[468] | 510 |
|
---|
[1563] | 511 | // viewcells->UpdatePVS(newVssRays);
|
---|
| 512 | // get viewcells as kd tree boxes
|
---|
| 513 | vector<AxisAlignedBox3> kdViewcells;
|
---|
[2065] | 514 | if (0)
|
---|
| 515 | {
|
---|
[1563] | 516 | vector<KdLeaf *> leaves;
|
---|
| 517 | mKdTree->CollectLeaves(leaves);
|
---|
| 518 | vector<KdLeaf *>::const_iterator it;
|
---|
| 519 | int targetLeaves = 50;
|
---|
| 520 | float prob = targetLeaves/(float)leaves.size();
|
---|
| 521 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
| 522 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
| 523 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
[434] | 524 |
|
---|
[1563] | 525 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 526 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
| 527 | }
|
---|
[468] | 528 |
|
---|
[448] | 529 |
|
---|
[1563] | 530 | int samples = 0;
|
---|
| 531 | int pass = 0;
|
---|
[468] | 532 |
|
---|
| 533 |
|
---|
[1563] | 534 | // cast view cell samples
|
---|
| 535 | while (samples < mVssSamples)
|
---|
| 536 | {
|
---|
| 537 | int num = mVssSamplesPerPass;
|
---|
| 538 | SimpleRayContainer rays;
|
---|
| 539 | VssRayContainer vssRays;
|
---|
[427] | 540 |
|
---|
[2065] | 541 | if (!mUseImportanceSampling)
|
---|
| 542 | {
|
---|
[1563] | 543 | for (int j=0; j < num; j++) {
|
---|
| 544 | Vector3 viewpoint;
|
---|
| 545 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
| 546 | Vector3 direction = GetDirection(viewpoint, NULL);
|
---|
[1883] | 547 | rays.push_back(SimpleRay(viewpoint, direction,
|
---|
| 548 | SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION,
|
---|
| 549 | 1.0f)
|
---|
| 550 | );
|
---|
[1563] | 551 | }
|
---|
[2065] | 552 | }
|
---|
| 553 | else
|
---|
| 554 | {
|
---|
[1563] | 555 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
| 556 | }
|
---|
[430] | 557 |
|
---|
[1563] | 558 | CastRays(rays, vssRays, true);
|
---|
| 559 | vssTree->AddRays(vssRays);
|
---|
[468] | 560 |
|
---|
[2065] | 561 | if (0)
|
---|
| 562 | {
|
---|
[1563] | 563 | int subdivided = vssTree->UpdateSubdivision();
|
---|
| 564 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 568 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
| 569 |
|
---|
| 570 | /// compute view cell contribution of rays
|
---|
| 571 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
| 572 |
|
---|
| 573 | if (numExportRays) {
|
---|
| 574 | char filename[64];
|
---|
| 575 | if (mUseImportanceSampling)
|
---|
| 576 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
| 577 | else
|
---|
| 578 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
| 579 |
|
---|
| 580 | ExportRays(filename, vssRays, numExportRays);
|
---|
| 581 | }
|
---|
| 582 |
|
---|
[2065] | 583 | samples += num;
|
---|
| 584 |
|
---|
[1563] | 585 | float pvs = vssTree->GetAvgPvsSize();
|
---|
| 586 | cout<<"*****************************\n";
|
---|
| 587 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
| 588 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
| 589 | cout<<"*****************************\n";
|
---|
| 590 | // if (samples >= mVssSamples) break;
|
---|
| 591 | pass ++;
|
---|
[401] | 592 | }
|
---|
[386] | 593 |
|
---|
[1563] | 594 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
| 595 | {
|
---|
| 596 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
| 597 | }
|
---|
[448] | 598 |
|
---|
[1563] | 599 | if (0) Debug << vssTree->stat << endl;
|
---|
[553] | 600 |
|
---|
[1563] | 601 | if (0)
|
---|
| 602 | {
|
---|
| 603 | VssRayContainer viewCellRays;
|
---|
| 604 | // compute rays used for view cells construction
|
---|
| 605 | const int numRays = mViewCellsManager->GetVisualizationSamples();
|
---|
| 606 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
| 607 | }
|
---|
[468] | 608 |
|
---|
[535] | 609 |
|
---|
[1563] | 610 | ////////////////////
|
---|
| 611 | //-- render simulation after construction
|
---|
[605] | 612 |
|
---|
[1563] | 613 | mRenderSimulator->RenderScene();
|
---|
| 614 | SimulationStatistics ss;
|
---|
| 615 | mRenderSimulator->GetStatistics(ss);
|
---|
| 616 | Debug << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
| 617 | cout << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
[1414] | 618 |
|
---|
[1563] | 619 | delete vssTree;
|
---|
[468] | 620 |
|
---|
[1563] | 621 | return true;
|
---|
[466] | 622 | }
|
---|
[697] | 623 |
|
---|
[2063] | 624 |
|
---|
| 625 | void VssPreprocessor::DeterminePvsObjects(VssRayContainer &rays)
|
---|
| 626 | {
|
---|
[2187] | 627 | // store higher order object
|
---|
[2198] | 628 | mViewCellsManager->DeterminePvsObjects(rays, !mViewCellsManager->ViewCellsConstructed());
|
---|
[1743] | 629 | }
|
---|
[2063] | 630 |
|
---|
| 631 |
|
---|
| 632 | }
|
---|