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