[447] | 1 | #include "SceneGraph.h"
|
---|
| 2 | #include "KdTree.h"
|
---|
| 3 | #include "RssPreprocessor.h"
|
---|
| 4 | #include "X3dExporter.h"
|
---|
| 5 | #include "Environment.h"
|
---|
| 6 | #include "MutualVisibility.h"
|
---|
| 7 | #include "Polygon3.h"
|
---|
| 8 | #include "ViewCell.h"
|
---|
| 9 | #include "VssRay.h"
|
---|
| 10 | #include "RssTree.h"
|
---|
[451] | 11 | #include "ViewCellsManager.h"
|
---|
[452] | 12 | #include "RenderSimulator.h"
|
---|
[492] | 13 | #include "GlRenderer.h"
|
---|
[1520] | 14 | #include "SamplingStrategy.h"
|
---|
[1867] | 15 | #include "PreprocessorThread.h"
|
---|
[447] | 16 |
|
---|
[1520] | 17 |
|
---|
[863] | 18 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 19 |
|
---|
| 20 |
|
---|
[1883] | 21 |
|
---|
[1876] | 22 | const bool pruneInvalidRays = false;
|
---|
[1867] | 23 |
|
---|
[1876] | 24 |
|
---|
[1199] | 25 | #define ADD_RAYS_IN_PLACE 1
|
---|
[1112] | 26 |
|
---|
[447] | 27 | RssPreprocessor::RssPreprocessor():
|
---|
| 28 | mVssRays()
|
---|
| 29 | {
|
---|
| 30 | // this should increase coherence of the samples
|
---|
[1966] | 31 | // Environment::GetSingleton()->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 32 | //Environment::GetSingleton()->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples);
|
---|
| 33 | //Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamples", mRssSamples);
|
---|
| 34 | //Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass);
|
---|
[1004] | 35 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
[464] | 36 |
|
---|
[1785] | 37 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useRssTree",
|
---|
| 38 | mUseRssTree);
|
---|
| 39 |
|
---|
[1004] | 40 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.pvs", mExportPvs);
|
---|
| 41 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.rssTree", mExportRssTree);
|
---|
[1926] | 42 | //Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.rays", mExportRays);
|
---|
| 43 | //Environment::GetSingleton()->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays);
|
---|
[1004] | 44 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells);
|
---|
| 45 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.objectBasedSampling", mObjectBasedSampling);
|
---|
| 46 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.directionalSampling", mDirectionalSampling);
|
---|
[464] | 47 |
|
---|
[1004] | 48 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
[1785] | 49 |
|
---|
[1004] | 50 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
[1785] | 51 |
|
---|
[1004] | 52 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
|
---|
[464] | 53 |
|
---|
[563] | 54 | mRssTree = NULL;
|
---|
| 55 | }
|
---|
[492] | 56 |
|
---|
[563] | 57 |
|
---|
[1765] | 58 | int
|
---|
[563] | 59 | RssPreprocessor::GenerateRays(
|
---|
| 60 | const int number,
|
---|
[1867] | 61 | SamplingStrategy &strategy,
|
---|
| 62 | SimpleRayContainer &rays)
|
---|
[563] | 63 | {
|
---|
[1765] | 64 | int result = 0;
|
---|
[1715] | 65 |
|
---|
| 66 | Debug<<"Generate rays...\n"<<flush;
|
---|
[563] | 67 |
|
---|
[1867] | 68 | switch (strategy.mType) {
|
---|
[1520] | 69 | case SamplingStrategy::RSS_BASED_DISTRIBUTION:
|
---|
| 70 | case SamplingStrategy::RSS_SILHOUETTE_BASED_DISTRIBUTION:
|
---|
[1877] | 71 | if (mRssTree) {
|
---|
| 72 | result = GenerateImportanceRays(mRssTree, number, rays);
|
---|
| 73 | }
|
---|
| 74 | break;
|
---|
| 75 |
|
---|
[563] | 76 | default:
|
---|
[1867] | 77 | result = Preprocessor::GenerateRays(number, strategy, rays);
|
---|
[563] | 78 | }
|
---|
[1867] | 79 |
|
---|
[1112] | 80 | // rays.NormalizePdf();
|
---|
[1715] | 81 | Debug<<"done.\n"<<flush;
|
---|
| 82 |
|
---|
[563] | 83 | return result;
|
---|
[447] | 84 | }
|
---|
| 85 |
|
---|
[563] | 86 |
|
---|
| 87 |
|
---|
[1715] | 88 |
|
---|
[447] | 89 | RssPreprocessor::~RssPreprocessor()
|
---|
| 90 | {
|
---|
[492] | 91 | // mVssRays get deleted in the tree
|
---|
| 92 | // CLEAR_CONTAINER(mVssRays);
|
---|
[447] | 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
[576] | 96 | void
|
---|
| 97 | RssPreprocessor::ExportObjectRays(VssRayContainer &rays,
|
---|
| 98 | const int objectId)
|
---|
| 99 | {
|
---|
| 100 | ObjectContainer::const_iterator oi;
|
---|
[447] | 101 |
|
---|
[576] | 102 | Intersectable *object = NULL;
|
---|
| 103 | for (oi = mObjects.begin(); oi != mObjects.end(); ++oi)
|
---|
| 104 | if (objectId == (*oi)->GetId()) {
|
---|
| 105 | object = *oi;
|
---|
| 106 | break;
|
---|
| 107 | }
|
---|
[447] | 108 |
|
---|
[576] | 109 | if (object == NULL)
|
---|
| 110 | return;
|
---|
| 111 |
|
---|
| 112 | VssRayContainer selectedRays;
|
---|
| 113 | VssRayContainer::const_iterator it= rays.begin(), it_end = rays.end();
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | for (; it != it_end; ++it) {
|
---|
| 117 | if ((*it)->mTerminationObject == object)
|
---|
| 118 | selectedRays.push_back(*it);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 | Exporter *exporter = Exporter::GetExporter("object-rays.x3d");
|
---|
| 123 | // exporter->SetWireframe();
|
---|
| 124 | // exporter->ExportKdTree(*mKdTree);
|
---|
| 125 | exporter->SetFilled();
|
---|
| 126 | exporter->ExportIntersectable(object);
|
---|
| 127 | exporter->ExportRays(selectedRays, RgbColor(1, 0, 0));
|
---|
| 128 |
|
---|
| 129 | delete exporter;
|
---|
| 130 |
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
[447] | 134 | int
|
---|
| 135 | RssPreprocessor::GenerateImportanceRays(RssTree *rssTree,
|
---|
| 136 | const int desiredSamples,
|
---|
| 137 | SimpleRayContainer &rays
|
---|
| 138 | )
|
---|
| 139 | {
|
---|
| 140 | int num;
|
---|
[463] | 141 |
|
---|
[492] | 142 | rssTree->UpdateTreeStatistics();
|
---|
[463] | 143 |
|
---|
[447] | 144 | cout<<
|
---|
[492] | 145 | "#RSS_AVG_PVS_SIZE\n"<<rssTree->stat.avgPvsSize<<endl<<
|
---|
| 146 | "#RSS_AVG_RAYS\n"<<rssTree->stat.avgRays<<endl<<
|
---|
| 147 | "#RSS_AVG_RAY_CONTRIB\n"<<rssTree->stat.avgRayContribution<<endl<<
|
---|
| 148 | "#RSS_AVG_PVS_ENTROPY\n"<<rssTree->stat.avgPvsEntropy<<endl<<
|
---|
| 149 | "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<rssTree->stat.avgRayLengthEntropy<<endl<<
|
---|
| 150 | "#RSS_AVG_IMPORTANCE\n"<<rssTree->stat.avgImportance<<endl;
|
---|
[447] | 151 |
|
---|
| 152 | if (0) {
|
---|
[492] | 153 | float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves());
|
---|
[447] | 154 | num = rssTree->GenerateRays(p, rays);
|
---|
| 155 | } else {
|
---|
[459] | 156 | int leaves = rssTree->stat.Leaves()/1;
|
---|
[1112] | 157 |
|
---|
[447] | 158 | num = rssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | return num;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 |
|
---|
[1112] | 168 |
|
---|
[447] | 169 | bool
|
---|
| 170 | RssPreprocessor::ExportRssTree(char *filename,
|
---|
| 171 | RssTree *tree,
|
---|
| 172 | const Vector3 &dir
|
---|
| 173 | )
|
---|
| 174 | {
|
---|
| 175 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 176 | exporter->SetFilled();
|
---|
[1328] | 177 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
[447] | 178 | // exporter->SetWireframe();
|
---|
| 179 | bool result = exporter->ExportRssTree2( *tree, dir );
|
---|
| 180 | delete exporter;
|
---|
| 181 | return result;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | bool
|
---|
| 185 | RssPreprocessor::ExportRssTreeLeaf(char *filename,
|
---|
| 186 | RssTree *tree,
|
---|
| 187 | RssTreeLeaf *leaf)
|
---|
| 188 | {
|
---|
| 189 | Exporter *exporter = NULL;
|
---|
| 190 | exporter = Exporter::GetExporter(filename);
|
---|
| 191 | exporter->SetWireframe();
|
---|
| 192 | exporter->ExportKdTree(*mKdTree);
|
---|
| 193 |
|
---|
| 194 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 195 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
| 196 | exporter->ResetForcedMaterial();
|
---|
| 197 |
|
---|
| 198 | VssRayContainer rays[4];
|
---|
| 199 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
| 200 | int k = leaf->rays[i].GetRayClass();
|
---|
| 201 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | // SOURCE RAY
|
---|
| 205 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
| 206 | // TERMINATION RAY
|
---|
| 207 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
| 208 | // PASSING_RAY
|
---|
| 209 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
| 210 | // CONTAINED_RAY
|
---|
| 211 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
| 212 |
|
---|
| 213 | delete exporter;
|
---|
| 214 | return true;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | void
|
---|
| 218 | RssPreprocessor::ExportRssTreeLeaves(RssTree *tree, const int number)
|
---|
| 219 | {
|
---|
| 220 | vector<RssTreeLeaf *> leaves;
|
---|
| 221 | tree->CollectLeaves(leaves);
|
---|
| 222 |
|
---|
| 223 | int num = 0;
|
---|
| 224 | int i;
|
---|
| 225 | float p = number / (float)leaves.size();
|
---|
| 226 | for (i=0; i < leaves.size(); i++) {
|
---|
| 227 | if (RandomValue(0,1) < p) {
|
---|
| 228 | char filename[64];
|
---|
| 229 | sprintf(filename, "rss-leaf-%04d.x3d", num);
|
---|
| 230 | ExportRssTreeLeaf(filename, tree, leaves[i]);
|
---|
| 231 | num++;
|
---|
| 232 | }
|
---|
| 233 | if (num >= number)
|
---|
| 234 | break;
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 | float
|
---|
| 240 | RssPreprocessor::GetAvgPvsSize(RssTree *tree,
|
---|
| 241 | const vector<AxisAlignedBox3> &viewcells
|
---|
| 242 | )
|
---|
| 243 | {
|
---|
| 244 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
| 245 |
|
---|
| 246 | int sum = 0;
|
---|
| 247 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
| 248 | sum += tree->GetPvsSize(*it);
|
---|
| 249 |
|
---|
| 250 | return sum/(float)viewcells.size();
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 | void
|
---|
| 255 | RssPreprocessor::ExportPvs(char *filename,
|
---|
| 256 | RssTree *rssTree
|
---|
| 257 | )
|
---|
| 258 | {
|
---|
| 259 | ObjectContainer pvs;
|
---|
| 260 | if (rssTree->CollectRootPvs(pvs)) {
|
---|
| 261 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 262 | exporter->SetFilled();
|
---|
| 263 | exporter->ExportGeometry(pvs);
|
---|
[459] | 264 | exporter->SetWireframe();
|
---|
| 265 | exporter->ExportBox(rssTree->bbox);
|
---|
| 266 | exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
|
---|
[447] | 267 | delete exporter;
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[492] | 271 |
|
---|
[1785] | 272 |
|
---|
[1112] | 273 | void
|
---|
[1785] | 274 | NormalizeRatios(vector<SamplingStrategy *> &distributions)
|
---|
[1771] | 275 | {
|
---|
| 276 | int i;
|
---|
| 277 | float sumRatios = 0.0f;
|
---|
| 278 |
|
---|
| 279 | for (i=0; i < distributions.size(); i++)
|
---|
| 280 | sumRatios += distributions[i]->mRatio;
|
---|
| 281 |
|
---|
| 282 |
|
---|
| 283 | if (sumRatios == 0.0f) {
|
---|
| 284 | for (i=0; i < distributions.size(); i++) {
|
---|
| 285 | distributions[i]->mRatio = 1.0f;
|
---|
| 286 | sumRatios += 1.0f;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | for (i=0 ; i < distributions.size(); i++)
|
---|
| 291 | distributions[i]->mRatio/=sumRatios;
|
---|
| 292 |
|
---|
| 293 | #define MIN_RATIO 0.1f
|
---|
| 294 |
|
---|
| 295 | for (i = 0; i < distributions.size(); i++)
|
---|
| 296 | if (distributions[i]->mRatio < MIN_RATIO)
|
---|
| 297 | distributions[i]->mRatio = MIN_RATIO;
|
---|
| 298 |
|
---|
| 299 |
|
---|
| 300 | sumRatios = 0.0f;
|
---|
| 301 | for (i=0; i < distributions.size(); i++)
|
---|
| 302 | sumRatios += distributions[i]->mRatio;
|
---|
| 303 |
|
---|
| 304 | for (i=0 ; i < distributions.size(); i++)
|
---|
| 305 | distributions[i]->mRatio/=sumRatios;
|
---|
| 306 |
|
---|
[1785] | 307 |
|
---|
| 308 | cout<<"New ratios: ";
|
---|
| 309 | for (i=0 ; i < distributions.size(); i++)
|
---|
| 310 | cout<<distributions[i]->mRatio<<" ";
|
---|
| 311 | cout<<endl;
|
---|
[1771] | 312 | }
|
---|
| 313 |
|
---|
| 314 | void
|
---|
[1785] | 315 | EvaluateRatios(vector<SamplingStrategy *> &distributions)
|
---|
[1112] | 316 | {
|
---|
[1785] | 317 | // now evaluate the ratios for the next pass
|
---|
[1867] | 318 | #define TIME_WEIGHT 0.0f
|
---|
[1785] | 319 | for (int i=0; i < distributions.size(); i++) {
|
---|
| 320 | distributions[i]->mRatio = distributions[i]->mContribution/
|
---|
[1877] | 321 | (Limits::Small +
|
---|
| 322 | (TIME_WEIGHT*distributions[i]->mTime +
|
---|
| 323 | (1 - TIME_WEIGHT)*distributions[i]->mRays)
|
---|
| 324 | );
|
---|
[1715] | 325 |
|
---|
[1785] | 326 | #if 1
|
---|
| 327 | distributions[i]->mRatio = sqr(distributions[i]->mRatio);
|
---|
[1715] | 328 | #endif
|
---|
[1785] | 329 | }
|
---|
[1112] | 330 | }
|
---|
[563] | 331 |
|
---|
[1785] | 332 |
|
---|
| 333 |
|
---|
[447] | 334 | bool
|
---|
| 335 | RssPreprocessor::ComputeVisibility()
|
---|
| 336 | {
|
---|
[556] | 337 |
|
---|
[811] | 338 | Debug << "type: rss" << endl;
|
---|
[579] | 339 |
|
---|
[464] | 340 | cout<<"Rss Preprocessor started\n"<<flush;
|
---|
[567] | 341 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
[492] | 342 |
|
---|
| 343 | Randomize(0);
|
---|
[1867] | 344 |
|
---|
[1891] | 345 | if (0) {
|
---|
[1867] | 346 | Exporter *exporter = Exporter::GetExporter("samples.wrl");
|
---|
| 347 | exporter->SetFilled();
|
---|
| 348 | Halton<4> halton;
|
---|
| 349 |
|
---|
[1877] | 350 | // for (int i=1; i < 7; i++)
|
---|
| 351 | // cout<<i<<" prime= "<<FindPrime(i)<<endl;
|
---|
[1867] | 352 |
|
---|
| 353 | Material mA, mB;
|
---|
| 354 | mA.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 355 | mB.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 356 |
|
---|
| 357 | for (int i=0; i < 10000; i++) {
|
---|
| 358 | float r[4];
|
---|
| 359 | halton.GetNext(r);
|
---|
| 360 | Vector3 v1 = UniformRandomVector(r[0], r[1]);
|
---|
| 361 | Vector3 v2 = UniformRandomVector(r[2], r[3]);
|
---|
| 362 |
|
---|
| 363 | const float size =0.002f;
|
---|
| 364 | AxisAlignedBox3 box(v1-Vector3(size), v1+Vector3(size));
|
---|
| 365 | exporter->SetForcedMaterial(mA);
|
---|
| 366 | exporter->ExportBox(box);
|
---|
| 367 | AxisAlignedBox3 box2(v2-Vector3(size), v2+Vector3(size));
|
---|
| 368 |
|
---|
| 369 | exporter->SetForcedMaterial(mB);
|
---|
| 370 | exporter->ExportBox(box2);
|
---|
| 371 | }
|
---|
| 372 | delete exporter;
|
---|
| 373 | }
|
---|
[492] | 374 |
|
---|
[1926] | 375 | if (!GetThread())
|
---|
| 376 | cerr << "!!no thread available!" << endl;
|
---|
| 377 |
|
---|
[1867] | 378 | cout<<"COMPUTATION THREAD="<<GetThread()->GetCurrentThreadId()<<endl<<flush;
|
---|
| 379 |
|
---|
[1785] | 380 | // use ray buffer to export ray animation
|
---|
[1877] | 381 | const bool useRayBuffer = false;
|
---|
[1694] | 382 |
|
---|
[1112] | 383 | vector<VssRayContainer> rayBuffer(50);
|
---|
| 384 |
|
---|
[447] | 385 | long startTime = GetTime();
|
---|
[1292] | 386 | long lastTime;
|
---|
| 387 | float totalTime;
|
---|
[447] | 388 |
|
---|
| 389 | int totalSamples = 0;
|
---|
| 390 |
|
---|
[577] | 391 | // if not already loaded, construct view cells from file
|
---|
[1112] | 392 | if (!mLoadViewCells)
|
---|
| 393 | {
|
---|
[1563] | 394 | // construct view cells using it's own set of samples
|
---|
| 395 | mViewCellsManager->Construct(this);
|
---|
| 396 |
|
---|
| 397 | //-- several visualizations and statistics
|
---|
| 398 | Debug << "view cells construction finished: " << endl;
|
---|
| 399 | mViewCellsManager->PrintStatistics(Debug);
|
---|
[574] | 400 | }
|
---|
[1563] | 401 |
|
---|
[1112] | 402 |
|
---|
[492] | 403 | int rssPass = 0;
|
---|
| 404 | int rssSamples = 0;
|
---|
[1737] | 405 |
|
---|
[1877] | 406 | // now decode distribution string
|
---|
| 407 | char buff[1024];
|
---|
| 408 | Environment::GetSingleton()->GetStringValue("RssPreprocessor.distributions",
|
---|
| 409 | buff);
|
---|
[563] | 410 |
|
---|
[1877] | 411 | char *curr = buff;
|
---|
| 412 | mUseRssTree = false;
|
---|
[1891] | 413 |
|
---|
[1877] | 414 | while (1) {
|
---|
| 415 | char *e = strchr(curr,'+');
|
---|
| 416 | if (e!=NULL) {
|
---|
| 417 | *e=0;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | if (strcmp(curr, "rss")==0) {
|
---|
| 421 | mDistributions.push_back(new RssBasedDistribution(*this));
|
---|
| 422 | mUseRssTree = true;
|
---|
| 423 | } else
|
---|
| 424 | if (strcmp(curr, "object")==0) {
|
---|
| 425 | mDistributions.push_back(new ObjectBasedDistribution(*this));
|
---|
| 426 | } else
|
---|
| 427 | if (strcmp(curr, "spatial")==0) {
|
---|
| 428 | mDistributions.push_back(new SpatialBoxBasedDistribution(*this));
|
---|
| 429 | } else
|
---|
| 430 | if (strcmp(curr, "global")==0) {
|
---|
| 431 | mDistributions.push_back(new GlobalLinesDistribution(*this));
|
---|
| 432 | } else
|
---|
| 433 | if (strcmp(curr, "direction")==0) {
|
---|
| 434 | mDistributions.push_back(new DirectionBasedDistribution(*this));
|
---|
| 435 | } else
|
---|
| 436 | if (strcmp(curr, "object_direction")==0) {
|
---|
| 437 | mDistributions.push_back(new ObjectDirectionBasedDistribution(*this));
|
---|
| 438 | } else
|
---|
| 439 | if (strcmp(curr, "reverse_object")==0) {
|
---|
| 440 | mDistributions.push_back(new ReverseObjectBasedDistribution(*this));
|
---|
| 441 | } else
|
---|
| 442 | if (strcmp(curr, "reverse_viewspace_border")==0) {
|
---|
| 443 | mDistributions.push_back(new ReverseViewSpaceBorderBasedDistribution(*this));
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | if (e==NULL)
|
---|
| 447 | break;
|
---|
| 448 | curr = e+1;
|
---|
[1867] | 449 | }
|
---|
[1884] | 450 |
|
---|
| 451 | mMixtureDistribution = new MixtureDistribution(*this);
|
---|
| 452 | mMixtureDistribution->mDistributions = mDistributions;
|
---|
| 453 | mMixtureDistribution->Init();
|
---|
| 454 | bool oldGenerator = false;
|
---|
[1891] | 455 |
|
---|
| 456 | const int batch = 100000;
|
---|
[1867] | 457 |
|
---|
[492] | 458 | if (mLoadInitialSamples) {
|
---|
| 459 | cout << "Loading samples from file ... ";
|
---|
| 460 | LoadSamples(mVssRays, mObjects);
|
---|
| 461 | cout << "finished\n" << endl;
|
---|
| 462 | } else {
|
---|
[563] | 463 | SimpleRayContainer rays;
|
---|
[1737] | 464 |
|
---|
[1884] | 465 | if (oldGenerator) {
|
---|
| 466 |
|
---|
| 467 | cout<<"Generating initial rays..."<<endl<<flush;
|
---|
| 468 |
|
---|
| 469 | int count = 0;
|
---|
| 470 | int i;
|
---|
| 471 |
|
---|
| 472 | // Generate initial samples
|
---|
| 473 | for (i=0; i < mDistributions.size(); i++)
|
---|
| 474 | if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
|
---|
| 475 | count++;
|
---|
| 476 |
|
---|
| 477 | for (i=0; i < mDistributions.size(); i++)
|
---|
| 478 | if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
|
---|
| 479 | GenerateRays(mInitialSamples/count,
|
---|
| 480 | *mDistributions[i],
|
---|
| 481 | rays);
|
---|
| 482 |
|
---|
| 483 | cout<<"Casting initial rays..."<<endl<<flush;
|
---|
| 484 | CastRays(rays, mVssRays, true, pruneInvalidRays);
|
---|
| 485 |
|
---|
| 486 | ExportObjectRays(mVssRays, 1546);
|
---|
| 487 |
|
---|
| 488 | cout<<"Computing sample contributions..."<<endl<<flush;
|
---|
| 489 | // evaluate contributions of the intitial rays
|
---|
| 490 | mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
| 491 | cout<<"done.\n"<<flush;
|
---|
| 492 |
|
---|
| 493 | } else {
|
---|
[2726] | 494 | int invalidSamples = 0;
|
---|
[1884] | 495 | for (int i=0; i < mInitialSamples; i+=batch) {
|
---|
| 496 | rays.clear();
|
---|
| 497 | mVssRays.clear();
|
---|
[2726] | 498 | mMixtureDistribution->GenerateSamples(batch, rays, invalidSamples);
|
---|
[1884] | 499 | CastRays(rays, mVssRays, true, pruneInvalidRays);
|
---|
| 500 | mMixtureDistribution->ComputeContributions(mVssRays);
|
---|
| 501 | }
|
---|
| 502 | }
|
---|
[563] | 503 | }
|
---|
| 504 |
|
---|
| 505 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
|
---|
| 506 |
|
---|
| 507 | rssSamples += (int)mVssRays.size();
|
---|
| 508 | totalSamples += mInitialSamples;
|
---|
| 509 | mPass++;
|
---|
| 510 |
|
---|
| 511 | if (mExportRays) {
|
---|
[577] | 512 |
|
---|
[563] | 513 | char filename[64];
|
---|
| 514 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
| 515 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
[1112] | 516 |
|
---|
[1694] | 517 | if (useRayBuffer)
|
---|
| 518 | mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
|
---|
[577] | 519 |
|
---|
[563] | 520 | }
|
---|
| 521 |
|
---|
| 522 | if (mStoreInitialSamples) {
|
---|
| 523 | cout << "Writing samples to file ... ";
|
---|
| 524 | ExportSamples(mVssRays);
|
---|
| 525 | cout << "finished\n" << endl;
|
---|
| 526 | }
|
---|
[492] | 527 |
|
---|
[1877] | 528 |
|
---|
| 529 | long time = GetTime();
|
---|
| 530 | totalTime = TimeDiff(startTime, time)*1e-3f;
|
---|
| 531 | lastTime = time;
|
---|
| 532 |
|
---|
| 533 | mStats <<
|
---|
| 534 | "#Pass\n" <<mPass<<endl<<
|
---|
| 535 | "#RssPass\n" <<rssPass<<endl<<
|
---|
| 536 | "#Time\n" << totalTime <<endl<<
|
---|
| 537 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
| 538 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
| 539 |
|
---|
| 540 | {
|
---|
| 541 | VssRayContainer contributingRays;
|
---|
| 542 | mVssRays.GetContributingRays(contributingRays, 0);
|
---|
| 543 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
| 544 | if (mExportRays) {
|
---|
| 545 | char filename[64];
|
---|
| 546 | sprintf(filename, "rss-crays-%04d.x3d", 0);
|
---|
| 547 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
[569] | 548 | }
|
---|
[1877] | 549 | }
|
---|
| 550 |
|
---|
| 551 |
|
---|
| 552 | // viewcells->UpdatePVS(newVssRays);
|
---|
| 553 | Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
| 554 | // cull viewcells with PVS > median (0.5f)
|
---|
| 555 | //mViewCellsManager->SetValidityPercentage(0, 0.5f);
|
---|
| 556 | // mViewCellsManager->SetValidityPercentage(0, 1.0f);
|
---|
| 557 | Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
| 558 |
|
---|
| 559 | mVssRays.PrintStatistics(mStats);
|
---|
| 560 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 561 |
|
---|
| 562 |
|
---|
| 563 | if (0) {
|
---|
| 564 | char str[64];
|
---|
| 565 | sprintf(str, "tmp/v-");
|
---|
[563] | 566 |
|
---|
[1877] | 567 | // visualization
|
---|
| 568 | const bool exportRays = true;
|
---|
| 569 | const bool exportPvs = true;
|
---|
| 570 | ObjectContainer objects;
|
---|
| 571 | mViewCellsManager->ExportSingleViewCells(objects,
|
---|
| 572 | 1000,
|
---|
| 573 | false,
|
---|
| 574 | exportPvs,
|
---|
| 575 | exportRays,
|
---|
| 576 | 10000,
|
---|
| 577 | str);
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | if (renderer) {
|
---|
[563] | 581 | ComputeRenderError();
|
---|
| 582 | }
|
---|
[492] | 583 |
|
---|
| 584 | rssPass++;
|
---|
[1824] | 585 |
|
---|
[1876] | 586 | // mDistributions.resize(1);
|
---|
[1824] | 587 |
|
---|
[563] | 588 | mRssTree = new RssTree;
|
---|
| 589 | mRssTree->SetPass(mPass);
|
---|
[492] | 590 |
|
---|
[527] | 591 | /// compute view cell contribution of rays if view cells manager already constructed
|
---|
[752] | 592 | // mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
[527] | 593 |
|
---|
[1877] | 594 | if (mUseRssTree) {
|
---|
| 595 | mRssTree->Construct(mObjects, mVssRays);
|
---|
[467] | 596 |
|
---|
[1877] | 597 | mRssTree->stat.Print(mStats);
|
---|
| 598 | cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
|
---|
| 599 |
|
---|
| 600 | if (mExportRssTree) {
|
---|
| 601 | ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
|
---|
| 602 | ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
|
---|
| 603 | ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
|
---|
| 604 | ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
|
---|
| 605 | ExportRssTreeLeaves(mRssTree, 10);
|
---|
[467] | 606 | }
|
---|
[459] | 607 | }
|
---|
[1877] | 608 |
|
---|
| 609 | if (mExportPvs) {
|
---|
| 610 | ExportPvs("rss-pvs-initial.x3d", mRssTree);
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 |
|
---|
[1867] | 614 | // keep only rss
|
---|
[1876] | 615 | // mDistributions.resize(1);
|
---|
[1867] | 616 |
|
---|
[447] | 617 | while (1) {
|
---|
[1715] | 618 |
|
---|
[1283] | 619 | static SimpleRayContainer rays;
|
---|
| 620 | static VssRayContainer vssRays;
|
---|
| 621 | static VssRayContainer tmpVssRays;
|
---|
[1715] | 622 |
|
---|
[1877] | 623 | cout<<"H1"<<endl<<flush;
|
---|
[1715] | 624 |
|
---|
[1877] | 625 | //rays.reserve((int)(1.1f*mRssSamplesPerPass));
|
---|
| 626 |
|
---|
| 627 | cout<<"H10"<<endl<<flush;
|
---|
| 628 |
|
---|
[1283] | 629 | rays.clear();
|
---|
| 630 | vssRays.clear();
|
---|
| 631 | tmpVssRays.clear();
|
---|
[1877] | 632 |
|
---|
[563] | 633 | int castRays = 0;
|
---|
[567] | 634 |
|
---|
[1877] | 635 | cout<<"H11"<<endl<<flush;
|
---|
[567] | 636 |
|
---|
[1877] | 637 | NormalizeRatios(mDistributions);
|
---|
| 638 |
|
---|
| 639 | long t1;
|
---|
| 640 | cout<<"H2"<<endl<<flush;
|
---|
| 641 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
| 642 | t1 = GetTime();
|
---|
| 643 | rays.clear();
|
---|
| 644 | tmpVssRays.clear();
|
---|
| 645 | if (mDistributions[i]->mRatio != 0) {
|
---|
| 646 | GenerateRays(int(mRssSamplesPerPass*mDistributions[i]->mRatio),
|
---|
| 647 | *mDistributions[i],
|
---|
| 648 | rays);
|
---|
| 649 |
|
---|
[2105] | 650 | #if 0
|
---|
[1877] | 651 | rays.NormalizePdf((float)rays.size());
|
---|
[2105] | 652 | #endif
|
---|
[1877] | 653 | CastRays(rays, tmpVssRays, true, pruneInvalidRays);
|
---|
| 654 | castRays += (int)rays.size();
|
---|
| 655 |
|
---|
| 656 | cout<<"Computing sample contributions..."<<endl<<flush;
|
---|
| 657 |
|
---|
[1112] | 658 | #if ADD_RAYS_IN_PLACE
|
---|
[1877] | 659 | float contribution =
|
---|
| 660 | mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
|
---|
[1112] | 661 | #else
|
---|
[1877] | 662 | float contribution =
|
---|
| 663 | mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
|
---|
[1112] | 664 | #endif
|
---|
[1877] | 665 | mDistributions[i]->mContribution = contribution;
|
---|
| 666 | mDistributions[i]->mTotalContribution += contribution;
|
---|
| 667 |
|
---|
[752] | 668 | cout<<"done."<<endl;
|
---|
[447] | 669 | }
|
---|
[563] | 670 |
|
---|
[1877] | 671 | mDistributions[i]->mTime = TimeDiff(t1, GetTime());
|
---|
| 672 | // mDistributions[i]->mRays = (int)rays.size();
|
---|
| 673 | mDistributions[i]->mRays = (int)tmpVssRays.size();
|
---|
| 674 | mDistributions[i]->mTotalRays = (int)tmpVssRays.size();
|
---|
| 675 |
|
---|
| 676 | // mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
|
---|
| 677 | vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
|
---|
[447] | 678 | }
|
---|
[1877] | 679 |
|
---|
| 680 | EvaluateRatios(mDistributions);
|
---|
| 681 |
|
---|
| 682 | // add contributions of all rays at once...
|
---|
| 683 | #if !ADD_RAYS_IN_PLACE
|
---|
| 684 | mViewCellsManager->AddSampleContributions(vssRays);
|
---|
| 685 | #endif
|
---|
| 686 |
|
---|
| 687 |
|
---|
[563] | 688 | totalSamples += castRays;
|
---|
[503] | 689 | rssSamples += (int)vssRays.size();
|
---|
[1877] | 690 |
|
---|
[884] | 691 | cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
|
---|
| 692 | mInitialSamples)<<"%\n";
|
---|
[492] | 693 |
|
---|
[1877] | 694 |
|
---|
[1292] | 695 | long time = GetTime();
|
---|
| 696 | totalTime += TimeDiff(lastTime, time);
|
---|
| 697 | lastTime = time;
|
---|
[492] | 698 |
|
---|
[467] | 699 | mStats <<
|
---|
| 700 | "#Pass\n" <<mPass<<endl<<
|
---|
| 701 | "#RssPass\n" <<rssPass<<endl<<
|
---|
| 702 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
| 703 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
| 704 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
[1877] | 705 |
|
---|
| 706 |
|
---|
| 707 | Debug<<"Print statistics...\n"<<flush;
|
---|
| 708 | vssRays.PrintStatistics(mStats);
|
---|
| 709 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 710 | Debug<<"done.\n"<<flush;
|
---|
| 711 |
|
---|
[1785] | 712 | if (renderer && mPass > 0) {
|
---|
[1877] | 713 | Debug<<"Computing render errror..."<<endl<<flush;
|
---|
[1785] | 714 | char buf[100];
|
---|
[1877] | 715 | sprintf(buf, "snap/i-%02d-", mPass);
|
---|
[1785] | 716 |
|
---|
[1877] | 717 | renderer->SetSnapPrefix(buf);
|
---|
| 718 |
|
---|
[1151] | 719 | renderer->SetSnapErrorFrames(true);
|
---|
[1877] | 720 | ComputeRenderError();
|
---|
| 721 | Debug<<"done."<<endl<<flush;
|
---|
[576] | 722 | }
|
---|
[1785] | 723 |
|
---|
[492] | 724 | // epxort rays before adding them to the tree -> some of them can be deleted
|
---|
[1877] | 725 |
|
---|
[492] | 726 | if (mExportRays) {
|
---|
[1877] | 727 | Debug<<"Exporting rays..."<<endl<<flush;
|
---|
[492] | 728 | char filename[64];
|
---|
[1877] | 729 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
[492] | 730 |
|
---|
[1694] | 731 | if (useRayBuffer)
|
---|
| 732 | vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
|
---|
[1112] | 733 |
|
---|
[492] | 734 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
[1112] | 735 |
|
---|
[492] | 736 | // now export all contributing rays
|
---|
| 737 | VssRayContainer contributingRays;
|
---|
| 738 | vssRays.GetContributingRays(contributingRays, mPass);
|
---|
[508] | 739 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
[492] | 740 | sprintf(filename, "rss-crays-%04d.x3d", rssPass);
|
---|
| 741 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
[1877] | 742 |
|
---|
| 743 | Debug<<"done."<<endl<<flush;
|
---|
[492] | 744 | }
|
---|
| 745 |
|
---|
| 746 |
|
---|
[467] | 747 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
| 748 | // already when adding into the tree
|
---|
| 749 | // do not add those rays which have too low or no contribution....
|
---|
[1877] | 750 | if (mUseRssTree) {
|
---|
[1715] | 751 | Debug<<"Adding rays...\n"<<flush;
|
---|
[563] | 752 | mRssTree->AddRays(vssRays);
|
---|
[1715] | 753 | Debug<<"done.\n"<<flush;
|
---|
[467] | 754 | if (mUpdateSubdivision) {
|
---|
[492] | 755 | int updatePasses = 1;
|
---|
| 756 | if (mPass % updatePasses == 0) {
|
---|
[1715] | 757 | Debug<<"Updating rss tree...\n"<<flush;
|
---|
[563] | 758 | int subdivided = mRssTree->UpdateSubdivision();
|
---|
[1715] | 759 | Debug<<"done.\n"<<flush;
|
---|
[492] | 760 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
[563] | 761 | cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
|
---|
[492] | 762 | }
|
---|
[467] | 763 | }
|
---|
[447] | 764 | }
|
---|
[467] | 765 |
|
---|
[464] | 766 | if (mExportPvs) {
|
---|
[447] | 767 | char filename[64];
|
---|
[467] | 768 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
[563] | 769 | ExportPvs(filename, mRssTree);
|
---|
[447] | 770 | }
|
---|
[492] | 771 |
|
---|
[563] | 772 |
|
---|
[1877] | 773 | if (!mUseRssTree)
|
---|
[492] | 774 | CLEAR_CONTAINER(vssRays);
|
---|
[563] | 775 | // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
|
---|
[1785] | 776 |
|
---|
[467] | 777 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
[447] | 778 | break;
|
---|
| 779 |
|
---|
[1613] | 780 |
|
---|
[467] | 781 | rssPass++;
|
---|
| 782 | mPass++;
|
---|
[563] | 783 | mRssTree->SetPass(mPass);
|
---|
[447] | 784 | }
|
---|
| 785 |
|
---|
[1877] | 786 | if(0) {
|
---|
| 787 | VssRayContainer selectedRays;
|
---|
| 788 | int desired = mViewCellsManager->GetVisualizationSamples();
|
---|
[556] | 789 |
|
---|
[1877] | 790 | mVssRays.SelectRays(desired, selectedRays);
|
---|
[1785] | 791 |
|
---|
[1877] | 792 | mViewCellsManager->Visualize(mObjects, selectedRays);
|
---|
[466] | 793 | }
|
---|
[1877] | 794 |
|
---|
| 795 | // view cells after sampling
|
---|
| 796 | mViewCellsManager->PrintStatistics(Debug);
|
---|
[1112] | 797 |
|
---|
[1877] | 798 | EvalViewCellHistogram();
|
---|
| 799 |
|
---|
| 800 | //-- render simulation after merge
|
---|
| 801 | cout << "\nEvaluating view cells render time after sampling ... ";
|
---|
| 802 |
|
---|
| 803 | mRenderSimulator->RenderScene();
|
---|
| 804 | SimulationStatistics ss;
|
---|
| 805 | mRenderSimulator->GetStatistics(ss);
|
---|
| 806 |
|
---|
| 807 | cout << " finished" << endl;
|
---|
| 808 | cout << ss << endl;
|
---|
| 809 | Debug << ss << endl;
|
---|
| 810 |
|
---|
| 811 | if (useRayBuffer && mExportRays) {
|
---|
[1112] | 812 | char filename[64];
|
---|
| 813 | sprintf(filename, "rss-rays-i.x3d");
|
---|
| 814 |
|
---|
| 815 | rayBuffer.resize(mPass);
|
---|
| 816 | ExportRayAnimation(filename, rayBuffer);
|
---|
| 817 | }
|
---|
[1877] | 818 |
|
---|
| 819 |
|
---|
[1581] | 820 | // do not delete rss tree now - can be used for visualization..
|
---|
| 821 | #if 0
|
---|
[563] | 822 | Debug<<"Deleting RSS tree...\n";
|
---|
| 823 | delete mRssTree;
|
---|
| 824 | Debug<<"Done.\n";
|
---|
[1581] | 825 | #endif
|
---|
[466] | 826 |
|
---|
[447] | 827 |
|
---|
| 828 | return true;
|
---|
| 829 | }
|
---|
| 830 |
|
---|
[871] | 831 | }
|
---|