[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 |
|
---|
[863] | 19 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 20 |
|
---|
[427] | 21 | bool use2dSampling = false;
|
---|
[534] | 22 | bool useViewspacePlane = false;
|
---|
[427] | 23 |
|
---|
[372] | 24 | VssPreprocessor::VssPreprocessor():
|
---|
[1486] | 25 | mVssRays()
|
---|
[372] | 26 | {
|
---|
| 27 | // this should increase coherence of the samples
|
---|
[1004] | 28 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 29 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
|
---|
| 30 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
|
---|
| 31 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
|
---|
| 32 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
[574] | 33 |
|
---|
[1004] | 34 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
| 35 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
| 36 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
|
---|
| 37 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
|
---|
| 38 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.enlargeViewSpace", mEnlargeViewSpace);
|
---|
| 39 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
[694] | 40 |
|
---|
[508] | 41 | useViewspacePlane = mUseViewSpaceBox; //hack
|
---|
[490] | 42 |
|
---|
[1486] | 43 | mViewSpaceBox.Initialize();
|
---|
[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));
|
---|
| 160 | exporter->ExportBox(mViewSpaceBox);
|
---|
| 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));
|
---|
| 203 | exporter->ExportBox(mViewSpaceBox);
|
---|
| 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 |
|
---|
[372] | 375 | long startTime = GetTime();
|
---|
[468] | 376 |
|
---|
[372] | 377 | int totalSamples = 0;
|
---|
| 378 |
|
---|
| 379 |
|
---|
[1002] | 380 | AxisAlignedBox3 box(mKdTree->GetBox());
|
---|
[534] | 381 |
|
---|
[434] | 382 | if (!useViewspacePlane) {
|
---|
[446] | 383 | float size = 0.05f;
|
---|
[434] | 384 | float s = 0.5f - size;
|
---|
[1002] | 385 | float olds = Magnitude(box.Size());
|
---|
| 386 | box.Enlarge(box.Size()*Vector3(-s));
|
---|
[434] | 387 | Vector3 translation = Vector3(-olds*0.1f, 0, 0);
|
---|
[1002] | 388 | box.SetMin(box.Min() + translation);
|
---|
| 389 | box.SetMax(box.Max() + translation);
|
---|
[434] | 390 | } else {
|
---|
[468] | 391 |
|
---|
[434] | 392 | // sample city like heights
|
---|
[1002] | 393 | box.SetMin(1, box.Min(1) + box.Size(1)*0.2f);
|
---|
| 394 | box.SetMax(1, box.Min(1) + box.Size(1)*0.3f);
|
---|
[434] | 395 | }
|
---|
[427] | 396 |
|
---|
[434] | 397 | if (use2dSampling)
|
---|
[1002] | 398 | box.SetMax(1, box.Min(1));
|
---|
[468] | 399 |
|
---|
[1221] | 400 | cout << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
[579] | 401 |
|
---|
[1076] | 402 |
|
---|
[501] | 403 | if (mUseViewSpaceBox)
|
---|
[487] | 404 | {
|
---|
[674] | 405 | if (!mEnlargeViewSpace)
|
---|
[654] | 406 | {
|
---|
[1486] | 407 | mViewSpaceBox = AxisAlignedBox3(box);
|
---|
[654] | 408 | }
|
---|
[598] | 409 | else
|
---|
| 410 | {
|
---|
[1486] | 411 | mViewSpaceBox = AxisAlignedBox3(mKdTree->GetBox());
|
---|
[677] | 412 |
|
---|
[1052] | 413 | if (0)
|
---|
[750] | 414 | {
|
---|
[1052] | 415 | // HACK: enlarge in y directon
|
---|
[1486] | 416 | Vector3 size = mViewSpaceBox.Size();
|
---|
[1052] | 417 |
|
---|
| 418 | size[1] *= 1.25;
|
---|
| 419 | Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
|
---|
| 420 | //Vector3 enlarge(size[0] * 4.0f, 0.0f, 0.0f);
|
---|
[598] | 421 |
|
---|
[1486] | 422 | mViewSpaceBox.Enlarge(enlarge);
|
---|
| 423 | mViewSpaceBox.SetMax(mViewSpaceBox.Max() + enlarge);
|
---|
[750] | 424 | }
|
---|
| 425 | else
|
---|
| 426 | {
|
---|
[1486] | 427 | AxisAlignedBox3 tbox(mViewSpaceBox);
|
---|
[1052] | 428 |
|
---|
[1486] | 429 | Vector3 size = mViewSpaceBox.Size();
|
---|
| 430 | tbox.SetMax(0, mViewSpaceBox.Max(0) + size[0] * 0.5f);
|
---|
| 431 | tbox.SetMin(0, mViewSpaceBox.Min(0) + size[0]);
|
---|
| 432 | mViewSpaceBox = tbox;
|
---|
[750] | 433 | }
|
---|
[598] | 434 | }
|
---|
[487] | 435 | }
|
---|
[434] | 436 | else
|
---|
[487] | 437 | {
|
---|
[1486] | 438 | mViewSpaceBox = AxisAlignedBox3(mKdTree->GetBox());
|
---|
[487] | 439 | }
|
---|
[534] | 440 |
|
---|
[590] | 441 | mSceneGraph->CollectObjects(&mObjects);
|
---|
| 442 |
|
---|
[577] | 443 | if (!mLoadViewCells)
|
---|
[574] | 444 | {
|
---|
[1486] | 445 | //-- generate new view cells from the scratch
|
---|
[1404] | 446 | //-- for construction the manager uses it's own set of samples
|
---|
[1486] | 447 | ConstructViewCells(mViewSpaceBox);
|
---|
[574] | 448 | }
|
---|
[1418] | 449 | #if 0
|
---|
[1264] | 450 | else
|
---|
[651] | 451 | {
|
---|
[1404] | 452 | //-- load view cells from file
|
---|
| 453 | //-- test successful view cells loading by exporting them again
|
---|
| 454 | VssRayContainer dummies;
|
---|
[590] | 455 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
[1264] | 456 | mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
[590] | 457 | }
|
---|
[1264] | 458 | #endif
|
---|
[1404] | 459 |
|
---|
[1311] | 460 | VssTree *vssTree = NULL;
|
---|
[1404] | 461 | const long initialTime = GetTime();
|
---|
[468] | 462 |
|
---|
[490] | 463 | if (mLoadInitialSamples)
|
---|
| 464 | {
|
---|
| 465 | cout << "Loading samples from file ... ";
|
---|
| 466 | LoadSamples(mVssRays, mObjects);
|
---|
| 467 | cout << "finished\n" << endl;
|
---|
| 468 | totalSamples = (int)mVssRays.size();
|
---|
| 469 | }
|
---|
| 470 | else
|
---|
| 471 | {
|
---|
[534] | 472 | while (totalSamples < mInitialSamples) {
|
---|
[490] | 473 | int passContributingSamples = 0;
|
---|
| 474 | int passSampleContributions = 0;
|
---|
| 475 | int passSamples = 0;
|
---|
[468] | 476 |
|
---|
[490] | 477 | int index = 0;
|
---|
[468] | 478 |
|
---|
[490] | 479 | int sampleContributions;
|
---|
[468] | 480 |
|
---|
[490] | 481 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
[1486] | 482 | for (int k=0; k < s; k++)
|
---|
| 483 | {
|
---|
[490] | 484 | Vector3 viewpoint;
|
---|
[1486] | 485 |
|
---|
[490] | 486 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
[1528] | 487 | const Vector3 direction = GetDirection(viewpoint, &mViewSpaceBox);
|
---|
[534] | 488 |
|
---|
[1528] | 489 | const SimpleRay sray(viewpoint, direction);
|
---|
| 490 | sampleContributions = mRayCaster->CastRay(sray, mVssRays, mViewSpaceBox, true);
|
---|
[468] | 491 |
|
---|
[490] | 492 | if (sampleContributions) {
|
---|
| 493 | passContributingSamples ++;
|
---|
| 494 | passSampleContributions += sampleContributions;
|
---|
| 495 | }
|
---|
| 496 | passSamples++;
|
---|
| 497 | totalSamples++;
|
---|
| 498 | }
|
---|
[468] | 499 |
|
---|
[490] | 500 | mPass++;
|
---|
| 501 | int pvsSize = 0;
|
---|
| 502 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
| 503 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
[468] | 504 |
|
---|
[490] | 505 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 506 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
[651] | 507 | << "#SampleContributions=" << passSampleContributions << " ("
|
---|
[490] | 508 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
| 509 | << pvsSize/(float)mObjects.size() << endl
|
---|
| 510 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
[468] | 511 |
|
---|
[490] | 512 | mStats <<
|
---|
| 513 | "#Pass\n" <<mPass<<endl<<
|
---|
| 514 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
| 515 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
| 516 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
| 517 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
| 518 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
| 519 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
[534] | 523 |
|
---|
| 524 |
|
---|
| 525 |
|
---|
[490] | 526 | }
|
---|
[564] | 527 |
|
---|
| 528 |
|
---|
[490] | 529 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
|
---|
| 530 | Debug << (int)mVssRays.size() << " rays generated in "
|
---|
| 531 | << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
|
---|
[401] | 532 |
|
---|
[490] | 533 | if (mStoreInitialSamples)
|
---|
| 534 | {
|
---|
| 535 | cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
|
---|
[508] | 536 | ExportSamples(mVssRays);
|
---|
[490] | 537 | cout << "finished\n" << endl;
|
---|
[491] | 538 |
|
---|
| 539 | /*VssRayContainer dummyRays;
|
---|
| 540 | LoadSamples(dummyRays, mObjects);
|
---|
| 541 | Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
|
---|
| 542 |
|
---|
| 543 | for (int i = 0; i < (int)mVssRays.size(); ++ i)
|
---|
| 544 | {
|
---|
| 545 | Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
|
---|
| 546 | Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
|
---|
| 547 | }*/
|
---|
[434] | 548 | }
|
---|
[468] | 549 |
|
---|
[534] | 550 |
|
---|
[712] | 551 | //int numExportRays = 2000;
|
---|
| 552 | int numExportRays = 0;
|
---|
[372] | 553 |
|
---|
[434] | 554 | if (numExportRays) {
|
---|
| 555 | char filename[64];
|
---|
| 556 | sprintf(filename, "vss-rays-initial.x3d");
|
---|
| 557 | ExportRays(filename, mVssRays, numExportRays);
|
---|
| 558 | }
|
---|
[468] | 559 |
|
---|
[434] | 560 | vssTree = new VssTree;
|
---|
| 561 | // viewcells = Construct(mVssRays);
|
---|
[468] | 562 |
|
---|
[1486] | 563 | vssTree->Construct(mVssRays, NULL);
|
---|
[434] | 564 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
[468] | 565 |
|
---|
[1414] | 566 | if (0) ExportRays("kdtree.x3d", mVssRays, 10);
|
---|
[1074] | 567 |
|
---|
[465] | 568 | if (0)
|
---|
| 569 | {
|
---|
| 570 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
| 571 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
| 572 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
| 573 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
| 574 | ExportVssTreeLeaves(vssTree, 10);
|
---|
| 575 | }
|
---|
[430] | 576 |
|
---|
[434] | 577 | // viewcells->UpdatePVS(newVssRays);
|
---|
| 578 | // get viewcells as kd tree boxes
|
---|
| 579 | vector<AxisAlignedBox3> kdViewcells;
|
---|
| 580 | if (0) {
|
---|
| 581 | vector<KdLeaf *> leaves;
|
---|
| 582 | mKdTree->CollectLeaves(leaves);
|
---|
| 583 | vector<KdLeaf *>::const_iterator it;
|
---|
| 584 | int targetLeaves = 50;
|
---|
| 585 | float prob = targetLeaves/(float)leaves.size();
|
---|
| 586 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
| 587 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
| 588 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
[468] | 589 |
|
---|
[434] | 590 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 591 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[468] | 594 |
|
---|
[434] | 595 | int samples = 0;
|
---|
| 596 | int pass = 0;
|
---|
[448] | 597 |
|
---|
[535] | 598 |
|
---|
[448] | 599 | // cast view cell samples
|
---|
[557] | 600 | while (samples < mVssSamples)
|
---|
| 601 | {
|
---|
| 602 |
|
---|
[434] | 603 | int num = mVssSamplesPerPass;
|
---|
| 604 | SimpleRayContainer rays;
|
---|
| 605 | VssRayContainer vssRays;
|
---|
[468] | 606 |
|
---|
[434] | 607 | if (!mUseImportanceSampling) {
|
---|
| 608 | for (int j=0; j < num; j++) {
|
---|
[1486] | 609 | Vector3 viewpoint;
|
---|
[487] | 610 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
[1486] | 611 | Vector3 direction = GetDirection(viewpoint, NULL);
|
---|
[434] | 612 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
| 613 | }
|
---|
| 614 | } else {
|
---|
| 615 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
| 616 | }
|
---|
[468] | 617 |
|
---|
[1528] | 618 | CastRays(rays, vssRays, true);
|
---|
[434] | 619 | vssTree->AddRays(vssRays);
|
---|
[468] | 620 |
|
---|
[434] | 621 | if (0) {
|
---|
| 622 | int subdivided = vssTree->UpdateSubdivision();
|
---|
| 623 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
| 624 | }
|
---|
[427] | 625 |
|
---|
[434] | 626 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 627 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
[430] | 628 |
|
---|
[517] | 629 | /// compute view cell contribution of rays
|
---|
[574] | 630 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
[517] | 631 |
|
---|
[434] | 632 | if (numExportRays) {
|
---|
| 633 | char filename[64];
|
---|
| 634 | if (mUseImportanceSampling)
|
---|
| 635 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
| 636 | else
|
---|
| 637 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
[468] | 638 |
|
---|
[434] | 639 | ExportRays(filename, vssRays, numExportRays);
|
---|
[401] | 640 | }
|
---|
[386] | 641 |
|
---|
[434] | 642 | samples+=num;
|
---|
| 643 | float pvs = vssTree->GetAvgPvsSize();
|
---|
| 644 | cout<<"*****************************\n";
|
---|
| 645 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
| 646 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
| 647 | cout<<"*****************************\n";
|
---|
[557] | 648 | // if (samples >= mVssSamples) break;
|
---|
| 649 | pass ++;
|
---|
[434] | 650 | }
|
---|
[448] | 651 |
|
---|
[574] | 652 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
| 653 | {
|
---|
| 654 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
[553] | 655 | }
|
---|
| 656 |
|
---|
[1221] | 657 | if (0) Debug << vssTree->stat << endl;
|
---|
[468] | 658 |
|
---|
[574] | 659 | if (0)
|
---|
| 660 | {
|
---|
[1414] | 661 | VssRayContainer viewCellRays;
|
---|
| 662 | // compute rays used for view cells construction
|
---|
| 663 | const int numRays = mViewCellsManager->GetVisualizationSamples();
|
---|
| 664 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
[540] | 665 | }
|
---|
[535] | 666 |
|
---|
[605] | 667 |
|
---|
[1486] | 668 | ////////////////////
|
---|
| 669 | //-- render simulation after construction
|
---|
[1414] | 670 |
|
---|
[468] | 671 | mRenderSimulator->RenderScene();
|
---|
| 672 | SimulationStatistics ss;
|
---|
| 673 | mRenderSimulator->GetStatistics(ss);
|
---|
[1414] | 674 | Debug << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
| 675 | cout << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
[468] | 676 |
|
---|
[434] | 677 | delete vssTree;
|
---|
[475] | 678 |
|
---|
[434] | 679 | return true;
|
---|
[466] | 680 | }
|
---|
[697] | 681 |
|
---|
[1281] | 682 | } |
---|