[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"
|
---|
[447] | 14 |
|
---|
[549] | 15 | static bool useViewSpaceBox = false;
|
---|
[447] | 16 | static bool use2dSampling = false;
|
---|
[516] | 17 |
|
---|
| 18 |
|
---|
| 19 | // not supported anymore!
|
---|
[463] | 20 | static bool fromBoxVisibility = false;
|
---|
[447] | 21 |
|
---|
[463] | 22 |
|
---|
[447] | 23 | RssPreprocessor::RssPreprocessor():
|
---|
| 24 | mPass(0),
|
---|
| 25 | mVssRays()
|
---|
| 26 | {
|
---|
| 27 | // this should increase coherence of the samples
|
---|
| 28 | environment->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 29 | environment->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples);
|
---|
| 30 | environment->GetIntValue("RssPreprocessor.vssSamples", mRssSamples);
|
---|
| 31 | environment->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass);
|
---|
| 32 | environment->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
[464] | 33 |
|
---|
| 34 | environment->GetBoolValue("RssPreprocessor.Export.pvs", mExportPvs);
|
---|
| 35 | environment->GetBoolValue("RssPreprocessor.Export.rssTree", mExportRssTree);
|
---|
| 36 | environment->GetBoolValue("RssPreprocessor.Export.rays", mExportRays);
|
---|
| 37 | environment->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays);
|
---|
| 38 | environment->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells);
|
---|
[492] | 39 | environment->GetBoolValue("RssPreprocessor.objectBasedSampling", mObjectBasedSampling);
|
---|
| 40 | environment->GetBoolValue("RssPreprocessor.directionalSampling", mDirectionalSampling);
|
---|
[464] | 41 |
|
---|
[490] | 42 | environment->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
| 43 | environment->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
[563] | 44 | environment->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
|
---|
[464] | 45 |
|
---|
[447] | 46 | mStats.open("stats.log");
|
---|
[563] | 47 | mRssTree = NULL;
|
---|
| 48 | }
|
---|
[492] | 49 |
|
---|
[563] | 50 |
|
---|
| 51 | bool
|
---|
| 52 | RssPreprocessor::GenerateRays(
|
---|
| 53 | const int number,
|
---|
| 54 | const int sampleType,
|
---|
| 55 | SimpleRayContainer &rays
|
---|
| 56 | )
|
---|
| 57 | {
|
---|
| 58 | bool result = false;
|
---|
| 59 |
|
---|
| 60 | switch (sampleType) {
|
---|
| 61 | case RSS_BASED_DISTRIBUTION:
|
---|
| 62 | case RSS_SILHOUETTE_BASED_DISTRIBUTION:
|
---|
| 63 | if (mRssTree) {
|
---|
| 64 | GenerateImportanceRays(mRssTree, number, rays);
|
---|
| 65 | result = true;
|
---|
[567] | 66 | // rays.NormalizePdf();
|
---|
[563] | 67 | }
|
---|
| 68 | break;
|
---|
| 69 | default:
|
---|
| 70 | result = Preprocessor::GenerateRays(number, sampleType, rays);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | return result;
|
---|
[447] | 74 | }
|
---|
| 75 |
|
---|
[563] | 76 | void
|
---|
| 77 | RssPreprocessor::CastRays(
|
---|
| 78 | SimpleRayContainer &rays,
|
---|
| 79 | VssRayContainer &vssRays
|
---|
| 80 | )
|
---|
| 81 | {
|
---|
| 82 | for (int i=0; i < rays.size(); i++)
|
---|
| 83 | CastRay(rays[i].mOrigin, rays[i].mDirection, rays[i].mPdf, vssRays);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
[447] | 87 | RssPreprocessor::~RssPreprocessor()
|
---|
| 88 | {
|
---|
[492] | 89 | // mVssRays get deleted in the tree
|
---|
| 90 | // CLEAR_CONTAINER(mVssRays);
|
---|
[447] | 91 | }
|
---|
| 92 |
|
---|
| 93 | void
|
---|
| 94 | RssPreprocessor::SetupRay(Ray &ray,
|
---|
| 95 | const Vector3 &point,
|
---|
| 96 | const Vector3 &direction
|
---|
| 97 | )
|
---|
| 98 | {
|
---|
| 99 | ray.intersections.clear();
|
---|
| 100 | // do not store anything else then intersections at the ray
|
---|
| 101 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | int
|
---|
| 105 | RssPreprocessor::CastRay(
|
---|
| 106 | Vector3 &viewPoint,
|
---|
| 107 | Vector3 &direction,
|
---|
[537] | 108 | const float probability,
|
---|
[447] | 109 | VssRayContainer &vssRays
|
---|
| 110 | )
|
---|
| 111 | {
|
---|
| 112 | int hits = 0;
|
---|
| 113 | static Ray ray;
|
---|
| 114 | AxisAlignedBox3 box = mKdTree->GetBox();
|
---|
| 115 |
|
---|
| 116 | AxisAlignedBox3 sbox = box;
|
---|
| 117 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
| 118 | if (!sbox.IsInside(viewPoint))
|
---|
| 119 | return 0;
|
---|
| 120 |
|
---|
| 121 | SetupRay(ray, viewPoint, direction);
|
---|
[549] | 122 |
|
---|
| 123 | if (!mDetectEmptyViewSpace)
|
---|
| 124 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[556] | 125 | else
|
---|
| 126 | ray.mFlags |= Ray::CULL_BACKFACES;
|
---|
[549] | 127 |
|
---|
[556] | 128 |
|
---|
[447] | 129 | // cast ray to KD tree to find intersection with other objects
|
---|
| 130 | Intersectable *objectA, *objectB;
|
---|
| 131 | Vector3 pointA, pointB;
|
---|
| 132 | float bsize = Magnitude(box.Size());
|
---|
[492] | 133 |
|
---|
[447] | 134 |
|
---|
| 135 | if (mKdTree->CastRay(ray)) {
|
---|
| 136 | objectA = ray.intersections[0].mObject;
|
---|
| 137 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
| 138 | } else {
|
---|
| 139 | objectA = NULL;
|
---|
| 140 | // compute intersection with the scene bounding box
|
---|
| 141 | float tmin, tmax;
|
---|
[492] | 142 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 143 | pointA = ray.Extrap(tmax);
|
---|
| 144 | else
|
---|
| 145 | return 0;
|
---|
[447] | 146 | }
|
---|
| 147 |
|
---|
[549] | 148 |
|
---|
| 149 | if (mDetectEmptyViewSpace) {
|
---|
[447] | 150 | SetupRay(ray, pointA, -direction);
|
---|
| 151 | } else
|
---|
| 152 | SetupRay(ray, viewPoint, -direction);
|
---|
| 153 |
|
---|
[549] | 154 | if (!mDetectEmptyViewSpace)
|
---|
| 155 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[556] | 156 | else
|
---|
| 157 | ray.mFlags |= Ray::CULL_BACKFACES;
|
---|
[549] | 158 |
|
---|
[447] | 159 | if (mKdTree->CastRay(ray)) {
|
---|
| 160 | objectB = ray.intersections[0].mObject;
|
---|
| 161 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
| 162 | } else {
|
---|
| 163 | objectB = NULL;
|
---|
| 164 | float tmin, tmax;
|
---|
[492] | 165 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 166 | pointB = ray.Extrap(tmax);
|
---|
| 167 | else
|
---|
| 168 | return 0;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | // if (objectA == NULL && objectB != NULL) {
|
---|
[549] | 172 | if (mDetectEmptyViewSpace) {
|
---|
[492] | 173 | // cast again to ensure that there is no objectA
|
---|
| 174 | SetupRay(ray, pointB, direction);
|
---|
[556] | 175 | ray.mFlags |= Ray::CULL_BACKFACES;
|
---|
[492] | 176 | if (mKdTree->CastRay(ray)) {
|
---|
| 177 | objectA = ray.intersections[0].mObject;
|
---|
| 178 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
[447] | 179 | }
|
---|
| 180 | }
|
---|
[492] | 181 |
|
---|
| 182 |
|
---|
[447] | 183 | VssRay *vssRay = NULL;
|
---|
| 184 |
|
---|
[492] | 185 | bool validSample = (objectA != objectB);
|
---|
[447] | 186 | if (validSample) {
|
---|
| 187 | if (objectA) {
|
---|
| 188 | vssRay = new VssRay(pointB,
|
---|
| 189 | pointA,
|
---|
| 190 | objectB,
|
---|
[464] | 191 | objectA,
|
---|
[537] | 192 | mPass,
|
---|
| 193 | probability
|
---|
[464] | 194 | );
|
---|
[447] | 195 | vssRays.push_back(vssRay);
|
---|
| 196 | hits ++;
|
---|
[576] | 197 |
|
---|
[447] | 198 | }
|
---|
| 199 |
|
---|
| 200 | if (objectB) {
|
---|
| 201 | vssRay = new VssRay(pointA,
|
---|
| 202 | pointB,
|
---|
| 203 | objectA,
|
---|
[464] | 204 | objectB,
|
---|
[537] | 205 | mPass,
|
---|
| 206 | probability
|
---|
[464] | 207 | );
|
---|
[447] | 208 | vssRays.push_back(vssRay);
|
---|
| 209 | hits ++;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
[576] | 212 |
|
---|
[447] | 213 | return hits;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
[576] | 217 | void
|
---|
| 218 | RssPreprocessor::ExportObjectRays(VssRayContainer &rays,
|
---|
| 219 | const int objectId)
|
---|
| 220 | {
|
---|
| 221 | ObjectContainer::const_iterator oi;
|
---|
[447] | 222 |
|
---|
[576] | 223 | Intersectable *object = NULL;
|
---|
| 224 | for (oi = mObjects.begin(); oi != mObjects.end(); ++oi)
|
---|
| 225 | if (objectId == (*oi)->GetId()) {
|
---|
| 226 | object = *oi;
|
---|
| 227 | break;
|
---|
| 228 | }
|
---|
[447] | 229 |
|
---|
[576] | 230 | if (object == NULL)
|
---|
| 231 | return;
|
---|
| 232 |
|
---|
| 233 | VssRayContainer selectedRays;
|
---|
| 234 | VssRayContainer::const_iterator it= rays.begin(), it_end = rays.end();
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 | for (; it != it_end; ++it) {
|
---|
| 238 | if ((*it)->mTerminationObject == object)
|
---|
| 239 | selectedRays.push_back(*it);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 | Exporter *exporter = Exporter::GetExporter("object-rays.x3d");
|
---|
| 244 | // exporter->SetWireframe();
|
---|
| 245 | // exporter->ExportKdTree(*mKdTree);
|
---|
| 246 | exporter->SetFilled();
|
---|
| 247 | exporter->ExportIntersectable(object);
|
---|
| 248 | exporter->ExportRays(selectedRays, RgbColor(1, 0, 0));
|
---|
| 249 |
|
---|
| 250 | delete exporter;
|
---|
| 251 |
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 |
|
---|
[447] | 255 | int
|
---|
| 256 | RssPreprocessor::GenerateImportanceRays(RssTree *rssTree,
|
---|
| 257 | const int desiredSamples,
|
---|
| 258 | SimpleRayContainer &rays
|
---|
| 259 | )
|
---|
| 260 | {
|
---|
| 261 | int num;
|
---|
[463] | 262 |
|
---|
[492] | 263 | rssTree->UpdateTreeStatistics();
|
---|
[463] | 264 |
|
---|
[447] | 265 | cout<<
|
---|
[492] | 266 | "#RSS_AVG_PVS_SIZE\n"<<rssTree->stat.avgPvsSize<<endl<<
|
---|
| 267 | "#RSS_AVG_RAYS\n"<<rssTree->stat.avgRays<<endl<<
|
---|
| 268 | "#RSS_AVG_RAY_CONTRIB\n"<<rssTree->stat.avgRayContribution<<endl<<
|
---|
| 269 | "#RSS_AVG_PVS_ENTROPY\n"<<rssTree->stat.avgPvsEntropy<<endl<<
|
---|
| 270 | "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<rssTree->stat.avgRayLengthEntropy<<endl<<
|
---|
| 271 | "#RSS_AVG_IMPORTANCE\n"<<rssTree->stat.avgImportance<<endl;
|
---|
[447] | 272 |
|
---|
| 273 | if (0) {
|
---|
[492] | 274 | float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves());
|
---|
[447] | 275 | num = rssTree->GenerateRays(p, rays);
|
---|
| 276 | } else {
|
---|
[459] | 277 | int leaves = rssTree->stat.Leaves()/1;
|
---|
[447] | 278 | num = rssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | return num;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 |
|
---|
| 286 | bool
|
---|
| 287 | RssPreprocessor::ExportRays(const char *filename,
|
---|
| 288 | const VssRayContainer &vssRays,
|
---|
| 289 | const int number
|
---|
| 290 | )
|
---|
| 291 | {
|
---|
| 292 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
| 293 |
|
---|
| 294 | Exporter *exporter = NULL;
|
---|
| 295 | exporter = Exporter::GetExporter(filename);
|
---|
| 296 | // exporter->SetWireframe();
|
---|
| 297 | // exporter->ExportKdTree(*mKdTree);
|
---|
| 298 | exporter->SetFilled();
|
---|
[556] | 299 | // $$JB temporarily do not export the scene
|
---|
[563] | 300 | if (0)
|
---|
[549] | 301 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
[447] | 302 | exporter->SetWireframe();
|
---|
| 303 |
|
---|
| 304 | if (mViewSpaceBox) {
|
---|
| 305 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
| 306 | exporter->ExportBox(*mViewSpaceBox);
|
---|
| 307 | exporter->ResetForcedMaterial();
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[492] | 310 | VssRayContainer rays;
|
---|
| 311 |
|
---|
| 312 | vssRays.SelectRays( number, rays);
|
---|
[447] | 313 |
|
---|
| 314 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
| 315 |
|
---|
| 316 | delete exporter;
|
---|
| 317 |
|
---|
| 318 | cout<<"done."<<endl<<flush;
|
---|
| 319 |
|
---|
| 320 | return true;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 |
|
---|
| 324 | bool
|
---|
| 325 | RssPreprocessor::ExportRssTree(char *filename,
|
---|
| 326 | RssTree *tree,
|
---|
| 327 | const Vector3 &dir
|
---|
| 328 | )
|
---|
| 329 | {
|
---|
| 330 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 331 | exporter->SetFilled();
|
---|
| 332 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 333 | // exporter->SetWireframe();
|
---|
| 334 | bool result = exporter->ExportRssTree2( *tree, dir );
|
---|
| 335 | delete exporter;
|
---|
| 336 | return result;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | bool
|
---|
| 340 | RssPreprocessor::ExportRssTreeLeaf(char *filename,
|
---|
| 341 | RssTree *tree,
|
---|
| 342 | RssTreeLeaf *leaf)
|
---|
| 343 | {
|
---|
| 344 | Exporter *exporter = NULL;
|
---|
| 345 | exporter = Exporter::GetExporter(filename);
|
---|
| 346 | exporter->SetWireframe();
|
---|
| 347 | exporter->ExportKdTree(*mKdTree);
|
---|
| 348 |
|
---|
| 349 | if (mViewSpaceBox) {
|
---|
| 350 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
| 351 | exporter->ExportBox(*mViewSpaceBox);
|
---|
| 352 | exporter->ResetForcedMaterial();
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 356 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
| 357 | exporter->ResetForcedMaterial();
|
---|
| 358 |
|
---|
| 359 | VssRayContainer rays[4];
|
---|
| 360 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
| 361 | int k = leaf->rays[i].GetRayClass();
|
---|
| 362 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | // SOURCE RAY
|
---|
| 366 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
| 367 | // TERMINATION RAY
|
---|
| 368 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
| 369 | // PASSING_RAY
|
---|
| 370 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
| 371 | // CONTAINED_RAY
|
---|
| 372 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
| 373 |
|
---|
| 374 | delete exporter;
|
---|
| 375 | return true;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | void
|
---|
| 379 | RssPreprocessor::ExportRssTreeLeaves(RssTree *tree, const int number)
|
---|
| 380 | {
|
---|
| 381 | vector<RssTreeLeaf *> leaves;
|
---|
| 382 | tree->CollectLeaves(leaves);
|
---|
| 383 |
|
---|
| 384 | int num = 0;
|
---|
| 385 | int i;
|
---|
| 386 | float p = number / (float)leaves.size();
|
---|
| 387 | for (i=0; i < leaves.size(); i++) {
|
---|
| 388 | if (RandomValue(0,1) < p) {
|
---|
| 389 | char filename[64];
|
---|
| 390 | sprintf(filename, "rss-leaf-%04d.x3d", num);
|
---|
| 391 | ExportRssTreeLeaf(filename, tree, leaves[i]);
|
---|
| 392 | num++;
|
---|
| 393 | }
|
---|
| 394 | if (num >= number)
|
---|
| 395 | break;
|
---|
| 396 | }
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 |
|
---|
| 400 | float
|
---|
| 401 | RssPreprocessor::GetAvgPvsSize(RssTree *tree,
|
---|
| 402 | const vector<AxisAlignedBox3> &viewcells
|
---|
| 403 | )
|
---|
| 404 | {
|
---|
| 405 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
| 406 |
|
---|
| 407 | int sum = 0;
|
---|
| 408 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
| 409 | sum += tree->GetPvsSize(*it);
|
---|
| 410 |
|
---|
| 411 | return sum/(float)viewcells.size();
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 |
|
---|
| 415 | void
|
---|
| 416 | RssPreprocessor::ExportPvs(char *filename,
|
---|
| 417 | RssTree *rssTree
|
---|
| 418 | )
|
---|
| 419 | {
|
---|
| 420 | ObjectContainer pvs;
|
---|
| 421 | if (rssTree->CollectRootPvs(pvs)) {
|
---|
| 422 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 423 | exporter->SetFilled();
|
---|
| 424 | exporter->ExportGeometry(pvs);
|
---|
[459] | 425 | exporter->SetWireframe();
|
---|
| 426 | exporter->ExportBox(rssTree->bbox);
|
---|
| 427 | exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
|
---|
[447] | 428 | delete exporter;
|
---|
| 429 | }
|
---|
| 430 | }
|
---|
| 431 |
|
---|
[492] | 432 |
|
---|
| 433 | void
|
---|
| 434 | RssPreprocessor::ComputeRenderError()
|
---|
| 435 | {
|
---|
| 436 | // compute rendering error
|
---|
| 437 | if (renderer) {
|
---|
[496] | 438 | // emit EvalPvsStat();
|
---|
| 439 | // QMutex mutex;
|
---|
| 440 | // mutex.lock();
|
---|
| 441 | // renderer->mRenderingFinished.wait(&mutex);
|
---|
| 442 | // mutex.unlock();
|
---|
[556] | 443 |
|
---|
[496] | 444 | renderer->EvalPvsStat();
|
---|
[492] | 445 | mStats <<
|
---|
| 446 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
| 447 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
| 448 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl;
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
| 451 |
|
---|
[563] | 452 |
|
---|
| 453 |
|
---|
[447] | 454 | bool
|
---|
| 455 | RssPreprocessor::ComputeVisibility()
|
---|
| 456 | {
|
---|
[556] | 457 |
|
---|
[579] | 458 | Debug << "type: rss" << endl;
|
---|
| 459 |
|
---|
[464] | 460 | cout<<"Rss Preprocessor started\n"<<flush;
|
---|
[567] | 461 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
[492] | 462 |
|
---|
| 463 | Randomize(0);
|
---|
| 464 |
|
---|
[447] | 465 |
|
---|
| 466 | long startTime = GetTime();
|
---|
| 467 |
|
---|
| 468 | int totalSamples = 0;
|
---|
| 469 |
|
---|
[563] | 470 | mViewSpaceBox = NULL;
|
---|
[572] | 471 |
|
---|
[577] | 472 | // if not already loaded, construct view cells from file
|
---|
| 473 | if (!mLoadViewCells) {
|
---|
| 474 |
|
---|
| 475 | mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
|
---|
| 476 |
|
---|
[574] | 477 | // construct view cells using it's own set of samples
|
---|
| 478 | mViewCellsManager->Construct(this);
|
---|
| 479 |
|
---|
| 480 | //-- several visualizations and statistics
|
---|
| 481 | Debug << "view cells after construction: " << endl;
|
---|
| 482 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 483 | }
|
---|
[577] | 484 |
|
---|
| 485 |
|
---|
[492] | 486 | int rssPass = 0;
|
---|
| 487 | int rssSamples = 0;
|
---|
[563] | 488 |
|
---|
[492] | 489 | if (mLoadInitialSamples) {
|
---|
| 490 | cout << "Loading samples from file ... ";
|
---|
| 491 | LoadSamples(mVssRays, mObjects);
|
---|
| 492 | cout << "finished\n" << endl;
|
---|
| 493 | } else {
|
---|
[563] | 494 | SimpleRayContainer rays;
|
---|
[537] | 495 |
|
---|
[576] | 496 | GenerateRays(mInitialSamples/4, SPATIAL_BOX_BASED_DISTRIBUTION, rays);
|
---|
| 497 | GenerateRays(mInitialSamples/4, OBJECT_BASED_DISTRIBUTION, rays);
|
---|
| 498 | GenerateRays(mInitialSamples/4, DIRECTION_BASED_DISTRIBUTION, rays);
|
---|
| 499 | GenerateRays(mInitialSamples/4, OBJECT_DIRECTION_BASED_DISTRIBUTION, rays);
|
---|
[492] | 500 |
|
---|
[563] | 501 | CastRays(rays, mVssRays);
|
---|
[576] | 502 |
|
---|
| 503 | ExportObjectRays(mVssRays, 1546);
|
---|
[563] | 504 | }
|
---|
| 505 |
|
---|
| 506 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
|
---|
| 507 |
|
---|
| 508 | rssSamples += (int)mVssRays.size();
|
---|
| 509 | totalSamples += mInitialSamples;
|
---|
| 510 | mPass++;
|
---|
| 511 |
|
---|
| 512 | if (mExportRays) {
|
---|
[577] | 513 |
|
---|
[563] | 514 | char filename[64];
|
---|
| 515 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
| 516 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
[577] | 517 |
|
---|
[563] | 518 | }
|
---|
| 519 |
|
---|
| 520 | if (mStoreInitialSamples) {
|
---|
| 521 | cout << "Writing samples to file ... ";
|
---|
| 522 | ExportSamples(mVssRays);
|
---|
| 523 | cout << "finished\n" << endl;
|
---|
| 524 | }
|
---|
[492] | 525 |
|
---|
[563] | 526 | if (mUseViewcells) {
|
---|
| 527 |
|
---|
| 528 | // evaluate contributions of the intitial rays
|
---|
[574] | 529 | mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
[492] | 530 |
|
---|
| 531 |
|
---|
[563] | 532 | mStats <<
|
---|
| 533 | "#Pass\n" <<mPass<<endl<<
|
---|
| 534 | "#RssPass\n" <<rssPass<<endl<<
|
---|
| 535 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
| 536 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
| 537 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
[569] | 538 |
|
---|
| 539 | {
|
---|
| 540 | VssRayContainer contributingRays;
|
---|
| 541 | mVssRays.GetContributingRays(contributingRays, mPass);
|
---|
| 542 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
| 543 | }
|
---|
[563] | 544 |
|
---|
| 545 | mVssRays.PrintStatistics(mStats);
|
---|
| 546 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
[502] | 547 |
|
---|
[572] | 548 | // viewcells->UpdatePVS(newVssRays);
|
---|
| 549 | Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
| 550 | // cull viewcells with PVS > median (0.5f)
|
---|
| 551 | mViewCellsManager->SetValidityPercentage(0, 0.5f);
|
---|
| 552 | Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
[563] | 553 |
|
---|
| 554 | ComputeRenderError();
|
---|
| 555 | }
|
---|
[492] | 556 |
|
---|
| 557 | rssPass++;
|
---|
| 558 |
|
---|
[563] | 559 | mRssTree = new RssTree;
|
---|
| 560 | mRssTree->SetPass(mPass);
|
---|
[492] | 561 |
|
---|
[527] | 562 | /// compute view cell contribution of rays if view cells manager already constructed
|
---|
[574] | 563 | mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
[527] | 564 |
|
---|
[467] | 565 | if (mUseImportanceSampling) {
|
---|
| 566 |
|
---|
[563] | 567 | mRssTree->Construct(mObjects, mVssRays);
|
---|
[467] | 568 |
|
---|
[563] | 569 | mRssTree->stat.Print(mStats);
|
---|
| 570 | cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
|
---|
| 571 |
|
---|
[467] | 572 | if (mExportRssTree) {
|
---|
[563] | 573 | ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
|
---|
| 574 | ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
|
---|
| 575 | ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
|
---|
| 576 | ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
|
---|
| 577 | ExportRssTreeLeaves(mRssTree, 10);
|
---|
[467] | 578 | }
|
---|
| 579 |
|
---|
| 580 | if (mExportPvs) {
|
---|
[563] | 581 | ExportPvs("rss-pvs-initial.x3d", mRssTree);
|
---|
[467] | 582 | }
|
---|
[459] | 583 | }
|
---|
[467] | 584 |
|
---|
[466] | 585 |
|
---|
[447] | 586 | while (1) {
|
---|
| 587 | SimpleRayContainer rays;
|
---|
| 588 | VssRayContainer vssRays;
|
---|
[563] | 589 | int castRays = 0;
|
---|
| 590 | if (mUseImportanceSampling) {
|
---|
| 591 | VssRayContainer tmpVssRays;
|
---|
[567] | 592 |
|
---|
[576] | 593 | float ratios[] = {0.8f,0.1f,0.1f};
|
---|
[567] | 594 |
|
---|
[563] | 595 |
|
---|
[567] | 596 |
|
---|
| 597 | GenerateRays(mRssSamplesPerPass*ratios[0], RSS_BASED_DISTRIBUTION, rays);
|
---|
[563] | 598 | CastRays(rays, tmpVssRays);
|
---|
| 599 | castRays += rays.size();
|
---|
[574] | 600 | float c1 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
|
---|
[563] | 601 | mStats<<"#RssRelContrib"<<endl<<c1/rays.size()<<endl;
|
---|
[492] | 602 |
|
---|
[563] | 603 | vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
|
---|
| 604 | rays.clear();
|
---|
| 605 | tmpVssRays.clear();
|
---|
[492] | 606 |
|
---|
[567] | 607 | if (ratios[1]!=0.0f) {
|
---|
| 608 | GenerateRays(mRssSamplesPerPass*ratios[1], SPATIAL_BOX_BASED_DISTRIBUTION, rays);
|
---|
| 609 | CastRays(rays, tmpVssRays);
|
---|
| 610 | castRays += rays.size();
|
---|
[574] | 611 | float c2 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
|
---|
[567] | 612 | mStats<<"#SpatialRelContrib"<<endl<<c2/rays.size()<<endl;
|
---|
| 613 |
|
---|
| 614 | vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
|
---|
| 615 |
|
---|
| 616 | rays.clear();
|
---|
| 617 | tmpVssRays.clear();
|
---|
| 618 | }
|
---|
[563] | 619 |
|
---|
| 620 |
|
---|
[567] | 621 | if (ratios[2]!=0.0f) {
|
---|
| 622 | GenerateRays(mRssSamplesPerPass*ratios[2], DIRECTION_BASED_DISTRIBUTION, rays);
|
---|
| 623 | CastRays(rays, tmpVssRays);
|
---|
| 624 | castRays += rays.size();
|
---|
[574] | 625 | float c3 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
|
---|
[567] | 626 | mStats<<"#DirectionalRelContrib"<<endl<<c3/rays.size()<<endl;
|
---|
| 627 |
|
---|
| 628 | vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
|
---|
| 629 |
|
---|
| 630 | rays.clear();
|
---|
| 631 | tmpVssRays.clear();
|
---|
| 632 | }
|
---|
[563] | 633 |
|
---|
| 634 | // add contributions of all rays at once...
|
---|
| 635 | mViewCellsManager->AddSampleContributions(vssRays);
|
---|
| 636 |
|
---|
| 637 | }
|
---|
| 638 | else {
|
---|
| 639 | int rayType = SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
| 640 | if (mObjectBasedSampling)
|
---|
| 641 | rayType = OBJECT_BASED_DISTRIBUTION;
|
---|
| 642 | else
|
---|
| 643 | if (mDirectionalSampling)
|
---|
| 644 | rayType = DIRECTION_BASED_DISTRIBUTION;
|
---|
| 645 |
|
---|
| 646 | GenerateRays(mRssSamplesPerPass, rayType, rays);
|
---|
| 647 | CastRays(rays, vssRays);
|
---|
| 648 | castRays += rays.size();
|
---|
| 649 | if (mUseViewcells) {
|
---|
| 650 | /// compute view cell contribution of rays
|
---|
[574] | 651 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
[447] | 652 | }
|
---|
[563] | 653 |
|
---|
| 654 |
|
---|
[447] | 655 | }
|
---|
[563] | 656 | totalSamples += castRays;
|
---|
[503] | 657 | rssSamples += (int)vssRays.size();
|
---|
[467] | 658 |
|
---|
[563] | 659 | cout<<"Generated "<<castRays<<" rays, progress "<<totalSamples/((float) mRssSamples +
|
---|
| 660 | mInitialSamples)<<"%\n";
|
---|
[492] | 661 |
|
---|
| 662 |
|
---|
[467] | 663 | mStats <<
|
---|
| 664 | "#Pass\n" <<mPass<<endl<<
|
---|
| 665 | "#RssPass\n" <<rssPass<<endl<<
|
---|
| 666 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
| 667 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
| 668 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
| 669 |
|
---|
[492] | 670 |
|
---|
[466] | 671 | if (mUseViewcells) {
|
---|
[467] | 672 | vssRays.PrintStatistics(mStats);
|
---|
| 673 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
[466] | 674 | }
|
---|
[576] | 675 |
|
---|
| 676 | if (0 && mPass > 0) {
|
---|
| 677 | if (mUseImportanceSampling)
|
---|
| 678 | renderer->mSnapPrefix.sprintf("snap/i-%02d-", mPass);
|
---|
| 679 | else
|
---|
| 680 | renderer->mSnapPrefix.sprintf("snap/r-%02d-", mPass);
|
---|
| 681 | renderer->mSnapErrorFrames = true;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
[492] | 684 | ComputeRenderError();
|
---|
| 685 |
|
---|
| 686 | // epxort rays before adding them to the tree -> some of them can be deleted
|
---|
| 687 |
|
---|
| 688 | if (mExportRays) {
|
---|
| 689 | char filename[64];
|
---|
| 690 | if (mUseImportanceSampling)
|
---|
| 691 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
| 692 | else
|
---|
| 693 | sprintf(filename, "rss-rays-%04d.x3d", rssPass);
|
---|
| 694 |
|
---|
| 695 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
| 696 |
|
---|
| 697 | // now export all contributing rays
|
---|
| 698 | VssRayContainer contributingRays;
|
---|
| 699 | vssRays.GetContributingRays(contributingRays, mPass);
|
---|
[508] | 700 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
[492] | 701 | sprintf(filename, "rss-crays-%04d.x3d", rssPass);
|
---|
| 702 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 |
|
---|
[467] | 706 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
| 707 | // already when adding into the tree
|
---|
| 708 | // do not add those rays which have too low or no contribution....
|
---|
[466] | 709 |
|
---|
[467] | 710 | if (mUseImportanceSampling) {
|
---|
[563] | 711 | mRssTree->AddRays(vssRays);
|
---|
[467] | 712 |
|
---|
| 713 | if (mUpdateSubdivision) {
|
---|
[492] | 714 | int updatePasses = 1;
|
---|
| 715 | if (mPass % updatePasses == 0) {
|
---|
[563] | 716 | int subdivided = mRssTree->UpdateSubdivision();
|
---|
[492] | 717 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
[563] | 718 | cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
|
---|
[492] | 719 | }
|
---|
[467] | 720 | }
|
---|
[447] | 721 | }
|
---|
[467] | 722 |
|
---|
[464] | 723 | if (mExportPvs) {
|
---|
[447] | 724 | char filename[64];
|
---|
[467] | 725 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
[563] | 726 | ExportPvs(filename, mRssTree);
|
---|
[447] | 727 | }
|
---|
[492] | 728 |
|
---|
[563] | 729 |
|
---|
[492] | 730 | if (!mUseImportanceSampling)
|
---|
| 731 | CLEAR_CONTAINER(vssRays);
|
---|
[563] | 732 | // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
|
---|
[492] | 733 |
|
---|
[467] | 734 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
[447] | 735 | break;
|
---|
| 736 |
|
---|
| 737 |
|
---|
[467] | 738 | rssPass++;
|
---|
| 739 | mPass++;
|
---|
[563] | 740 | mRssTree->SetPass(mPass);
|
---|
[447] | 741 | }
|
---|
| 742 |
|
---|
[464] | 743 | if (mUseViewcells) {
|
---|
[466] | 744 |
|
---|
[574] | 745 | if(0)
|
---|
| 746 | {
|
---|
| 747 | VssRayContainer selectedRays;
|
---|
| 748 | int desired = mViewCellsManager->GetVisualizationSamples();
|
---|
| 749 |
|
---|
| 750 | mVssRays.SelectRays(desired, selectedRays);
|
---|
| 751 |
|
---|
| 752 | mViewCellsManager->Visualize(mObjects, selectedRays);
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 | // view cells after sampling
|
---|
| 756 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 757 |
|
---|
| 758 | //-- render simulation after merge
|
---|
| 759 | cout << "\nevaluating bsp view cells render time after sampling ... ";
|
---|
| 760 |
|
---|
| 761 | mRenderSimulator->RenderScene();
|
---|
| 762 | SimulationStatistics ss;
|
---|
| 763 | mRenderSimulator->GetStatistics(ss);
|
---|
| 764 |
|
---|
| 765 | cout << " finished" << endl;
|
---|
| 766 | cout << ss << endl;
|
---|
| 767 | Debug << ss << endl;
|
---|
[556] | 768 |
|
---|
[466] | 769 | }
|
---|
[563] | 770 |
|
---|
| 771 | Debug<<"Deleting RSS tree...\n";
|
---|
| 772 | delete mRssTree;
|
---|
| 773 | Debug<<"Done.\n";
|
---|
[466] | 774 |
|
---|
[447] | 775 |
|
---|
| 776 | return true;
|
---|
| 777 | }
|
---|
| 778 |
|
---|