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