[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"
|
---|
[447] | 13 |
|
---|
[467] | 14 | static bool useViewSpaceBox = false;
|
---|
[447] | 15 | static bool use2dSampling = false;
|
---|
[463] | 16 | static bool fromBoxVisibility = false;
|
---|
[447] | 17 |
|
---|
[463] | 18 |
|
---|
[447] | 19 | RssPreprocessor::RssPreprocessor():
|
---|
| 20 | mPass(0),
|
---|
| 21 | mVssRays()
|
---|
| 22 | {
|
---|
| 23 | // this should increase coherence of the samples
|
---|
| 24 | environment->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 25 | environment->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples);
|
---|
| 26 | environment->GetIntValue("RssPreprocessor.vssSamples", mRssSamples);
|
---|
| 27 | environment->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass);
|
---|
| 28 | environment->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
| 29 | environment->GetIntValue("BspTree.Construction.samples", mBspConstructionSamples);
|
---|
[464] | 30 |
|
---|
| 31 | environment->GetBoolValue("RssPreprocessor.Export.pvs", mExportPvs);
|
---|
| 32 | environment->GetBoolValue("RssPreprocessor.Export.rssTree", mExportRssTree);
|
---|
| 33 | environment->GetBoolValue("RssPreprocessor.Export.rays", mExportRays);
|
---|
| 34 | environment->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays);
|
---|
| 35 | environment->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells);
|
---|
| 36 |
|
---|
| 37 | environment->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
|
---|
| 38 |
|
---|
[447] | 39 | mStats.open("stats.log");
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | RssPreprocessor::~RssPreprocessor()
|
---|
| 43 | {
|
---|
| 44 | CLEAR_CONTAINER(mVssRays);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | void
|
---|
| 48 | RssPreprocessor::SetupRay(Ray &ray,
|
---|
| 49 | const Vector3 &point,
|
---|
| 50 | const Vector3 &direction
|
---|
| 51 | )
|
---|
| 52 | {
|
---|
| 53 | ray.intersections.clear();
|
---|
| 54 | // do not store anything else then intersections at the ray
|
---|
| 55 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | int
|
---|
| 59 | RssPreprocessor::CastRay(
|
---|
| 60 | Vector3 &viewPoint,
|
---|
| 61 | Vector3 &direction,
|
---|
| 62 | VssRayContainer &vssRays
|
---|
| 63 | )
|
---|
| 64 | {
|
---|
| 65 | int hits = 0;
|
---|
| 66 | static Ray ray;
|
---|
| 67 | AxisAlignedBox3 box = mKdTree->GetBox();
|
---|
| 68 |
|
---|
| 69 | AxisAlignedBox3 sbox = box;
|
---|
| 70 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
| 71 | if (!sbox.IsInside(viewPoint))
|
---|
| 72 | return 0;
|
---|
| 73 |
|
---|
| 74 | SetupRay(ray, viewPoint, direction);
|
---|
| 75 | // cast ray to KD tree to find intersection with other objects
|
---|
| 76 | Intersectable *objectA, *objectB;
|
---|
| 77 | Vector3 pointA, pointB;
|
---|
| 78 | float bsize = Magnitude(box.Size());
|
---|
| 79 |
|
---|
| 80 | if (mKdTree->CastRay(ray)) {
|
---|
| 81 | objectA = ray.intersections[0].mObject;
|
---|
| 82 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
| 83 | } else {
|
---|
| 84 | objectA = NULL;
|
---|
| 85 | // compute intersection with the scene bounding box
|
---|
| 86 | float tmin, tmax;
|
---|
| 87 | box.ComputeMinMaxT(ray, &tmin, &tmax);
|
---|
| 88 | if (tmax > bsize) {
|
---|
| 89 | //cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl;
|
---|
| 90 | //cerr<<"ray"<<ray<<endl;
|
---|
| 91 | }
|
---|
| 92 | pointA = ray.Extrap(tmax);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | bool detectEmptyViewSpace = true;
|
---|
| 96 |
|
---|
| 97 | if (detectEmptyViewSpace) {
|
---|
| 98 | SetupRay(ray, pointA, -direction);
|
---|
| 99 | } else
|
---|
| 100 | SetupRay(ray, viewPoint, -direction);
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | if (mKdTree->CastRay(ray)) {
|
---|
| 104 | objectB = ray.intersections[0].mObject;
|
---|
| 105 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
| 106 | } else {
|
---|
| 107 | objectB = NULL;
|
---|
| 108 | float tmin, tmax;
|
---|
| 109 | box.ComputeMinMaxT(ray, &tmin, &tmax);
|
---|
| 110 | if (tmax > bsize) {
|
---|
| 111 | //cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl;
|
---|
| 112 | //cerr<<"ray"<<ray<<endl;
|
---|
| 113 | }
|
---|
| 114 | pointB = ray.Extrap(tmax);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | VssRay *vssRay = NULL;
|
---|
| 118 |
|
---|
| 119 | bool validSample = true;
|
---|
| 120 | if (detectEmptyViewSpace) {
|
---|
| 121 | // check if the viewpoint lies on the line segment AB
|
---|
| 122 | if (Distance(pointA, pointB) <
|
---|
| 123 | Distance(viewPoint, pointA) + Distance(viewPoint, pointB) - Limits::Small) {
|
---|
| 124 | validSample = false;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | if (validSample) {
|
---|
| 129 | if (objectA) {
|
---|
| 130 | vssRay = new VssRay(pointB,
|
---|
| 131 | pointA,
|
---|
| 132 | objectB,
|
---|
[464] | 133 | objectA,
|
---|
| 134 | mPass
|
---|
| 135 | );
|
---|
[447] | 136 | vssRays.push_back(vssRay);
|
---|
| 137 | hits ++;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | if (objectB) {
|
---|
| 141 | vssRay = new VssRay(pointA,
|
---|
| 142 | pointB,
|
---|
| 143 | objectA,
|
---|
[464] | 144 | objectB,
|
---|
| 145 | mPass
|
---|
| 146 | );
|
---|
[447] | 147 | vssRays.push_back(vssRay);
|
---|
| 148 | hits ++;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | return hits;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | Vector3
|
---|
| 157 | RssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
| 158 | {
|
---|
| 159 | AxisAlignedBox3 box;
|
---|
| 160 |
|
---|
| 161 | if (viewSpaceBox)
|
---|
| 162 | box =*viewSpaceBox;
|
---|
| 163 | else
|
---|
| 164 | box = mKdTree->GetBox();
|
---|
| 165 |
|
---|
| 166 | // shrink the box in the y direction
|
---|
| 167 | return box.GetRandomPoint();
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | Vector3
|
---|
| 171 | RssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
| 172 | AxisAlignedBox3 *viewSpaceBox
|
---|
| 173 | )
|
---|
| 174 | {
|
---|
| 175 | Vector3 point;
|
---|
| 176 | if (!use2dSampling) {
|
---|
| 177 | Vector3 normal;
|
---|
| 178 | int i = RandomValue(0, mObjects.size()-1);
|
---|
| 179 | Intersectable *object = mObjects[i];
|
---|
| 180 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 181 | } else {
|
---|
| 182 | AxisAlignedBox3 box;
|
---|
| 183 |
|
---|
| 184 | if (viewSpaceBox)
|
---|
| 185 | box =*viewSpaceBox;
|
---|
| 186 | else
|
---|
| 187 | box = mKdTree->GetBox();
|
---|
| 188 |
|
---|
| 189 | point = box.GetRandomPoint();
|
---|
| 190 | point.y = viewpoint.y;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | return point - viewpoint;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | int
|
---|
| 197 | RssPreprocessor::GenerateImportanceRays(RssTree *rssTree,
|
---|
| 198 | const int desiredSamples,
|
---|
| 199 | SimpleRayContainer &rays
|
---|
| 200 | )
|
---|
| 201 | {
|
---|
| 202 | int num;
|
---|
[463] | 203 |
|
---|
| 204 |
|
---|
[447] | 205 |
|
---|
| 206 | float avgPvsSize;
|
---|
| 207 | float avgRayContribution;
|
---|
| 208 | float avgPvsEntropy;
|
---|
| 209 | float avgRayLengthEntropy;
|
---|
| 210 | float avgImportance;
|
---|
[463] | 211 | float avgRays;
|
---|
[447] | 212 | rssTree->GetTreeStatistics(
|
---|
| 213 | avgPvsSize,
|
---|
[463] | 214 | avgRays,
|
---|
[447] | 215 | avgRayContribution,
|
---|
| 216 | avgPvsEntropy,
|
---|
| 217 | avgRayLengthEntropy,
|
---|
| 218 | avgImportance);
|
---|
| 219 |
|
---|
| 220 | cout<<
|
---|
[467] | 221 | "#RSS_AVG_PVS_SIZE\n"<<avgPvsSize<<endl<<
|
---|
| 222 | "#RSS_AVG_RAYS\n"<<avgRays<<endl<<
|
---|
| 223 | "#RSS_AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl<<
|
---|
| 224 | "#RSS_AVG_PVS_ENTROPY\n"<<avgPvsEntropy<<endl<<
|
---|
| 225 | "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<avgRayLengthEntropy<<endl<<
|
---|
| 226 | "#RSS_AVG_IMPORTANCE\n"<<avgImportance<<endl;
|
---|
[447] | 227 |
|
---|
| 228 | if (0) {
|
---|
| 229 | float p = desiredSamples/(float)(avgRayContribution*rssTree->stat.Leaves());
|
---|
| 230 | num = rssTree->GenerateRays(p, rays);
|
---|
| 231 | } else {
|
---|
[459] | 232 | int leaves = rssTree->stat.Leaves()/1;
|
---|
[447] | 233 | num = rssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
| 237 |
|
---|
| 238 | return num;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | bool
|
---|
| 243 | RssPreprocessor::ExportRays(const char *filename,
|
---|
| 244 | const VssRayContainer &vssRays,
|
---|
| 245 | const int number
|
---|
| 246 | )
|
---|
| 247 | {
|
---|
| 248 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
| 249 |
|
---|
| 250 | float prob = number/(float)vssRays.size();
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | Exporter *exporter = NULL;
|
---|
| 254 | exporter = Exporter::GetExporter(filename);
|
---|
| 255 | // exporter->SetWireframe();
|
---|
| 256 | // exporter->ExportKdTree(*mKdTree);
|
---|
| 257 | exporter->SetFilled();
|
---|
| 258 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 259 | exporter->SetWireframe();
|
---|
| 260 |
|
---|
| 261 | if (mViewSpaceBox) {
|
---|
| 262 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
| 263 | exporter->ExportBox(*mViewSpaceBox);
|
---|
| 264 | exporter->ResetForcedMaterial();
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | VssRayContainer rays; for (int i=0; i < vssRays.size(); i++)
|
---|
| 268 | if (RandomValue(0,1) < prob)
|
---|
| 269 | rays.push_back(vssRays[i]);
|
---|
| 270 |
|
---|
| 271 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
| 272 |
|
---|
| 273 | delete exporter;
|
---|
| 274 |
|
---|
| 275 | cout<<"done."<<endl<<flush;
|
---|
| 276 |
|
---|
| 277 | return true;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 |
|
---|
| 281 | bool
|
---|
| 282 | RssPreprocessor::ExportRssTree(char *filename,
|
---|
| 283 | RssTree *tree,
|
---|
| 284 | const Vector3 &dir
|
---|
| 285 | )
|
---|
| 286 | {
|
---|
| 287 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 288 | exporter->SetFilled();
|
---|
| 289 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 290 | // exporter->SetWireframe();
|
---|
| 291 | bool result = exporter->ExportRssTree2( *tree, dir );
|
---|
| 292 | delete exporter;
|
---|
| 293 | return result;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | bool
|
---|
| 297 | RssPreprocessor::ExportRssTreeLeaf(char *filename,
|
---|
| 298 | RssTree *tree,
|
---|
| 299 | RssTreeLeaf *leaf)
|
---|
| 300 | {
|
---|
| 301 | Exporter *exporter = NULL;
|
---|
| 302 | exporter = Exporter::GetExporter(filename);
|
---|
| 303 | exporter->SetWireframe();
|
---|
| 304 | exporter->ExportKdTree(*mKdTree);
|
---|
| 305 |
|
---|
| 306 | if (mViewSpaceBox) {
|
---|
| 307 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
| 308 | exporter->ExportBox(*mViewSpaceBox);
|
---|
| 309 | exporter->ResetForcedMaterial();
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 313 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
| 314 | exporter->ResetForcedMaterial();
|
---|
| 315 |
|
---|
| 316 | VssRayContainer rays[4];
|
---|
| 317 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
| 318 | int k = leaf->rays[i].GetRayClass();
|
---|
| 319 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | // SOURCE RAY
|
---|
| 323 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
| 324 | // TERMINATION RAY
|
---|
| 325 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
| 326 | // PASSING_RAY
|
---|
| 327 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
| 328 | // CONTAINED_RAY
|
---|
| 329 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
| 330 |
|
---|
| 331 | delete exporter;
|
---|
| 332 | return true;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | void
|
---|
| 336 | RssPreprocessor::ExportRssTreeLeaves(RssTree *tree, const int number)
|
---|
| 337 | {
|
---|
| 338 | vector<RssTreeLeaf *> leaves;
|
---|
| 339 | tree->CollectLeaves(leaves);
|
---|
| 340 |
|
---|
| 341 | int num = 0;
|
---|
| 342 | int i;
|
---|
| 343 | float p = number / (float)leaves.size();
|
---|
| 344 | for (i=0; i < leaves.size(); i++) {
|
---|
| 345 | if (RandomValue(0,1) < p) {
|
---|
| 346 | char filename[64];
|
---|
| 347 | sprintf(filename, "rss-leaf-%04d.x3d", num);
|
---|
| 348 | ExportRssTreeLeaf(filename, tree, leaves[i]);
|
---|
| 349 | num++;
|
---|
| 350 | }
|
---|
| 351 | if (num >= number)
|
---|
| 352 | break;
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 |
|
---|
| 357 | float
|
---|
| 358 | RssPreprocessor::GetAvgPvsSize(RssTree *tree,
|
---|
| 359 | const vector<AxisAlignedBox3> &viewcells
|
---|
| 360 | )
|
---|
| 361 | {
|
---|
| 362 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
| 363 |
|
---|
| 364 | int sum = 0;
|
---|
| 365 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
| 366 | sum += tree->GetPvsSize(*it);
|
---|
| 367 |
|
---|
| 368 | return sum/(float)viewcells.size();
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 |
|
---|
| 372 | void
|
---|
| 373 | RssPreprocessor::ExportPvs(char *filename,
|
---|
| 374 | RssTree *rssTree
|
---|
| 375 | )
|
---|
| 376 | {
|
---|
| 377 | ObjectContainer pvs;
|
---|
| 378 | if (rssTree->CollectRootPvs(pvs)) {
|
---|
| 379 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 380 | exporter->SetFilled();
|
---|
| 381 | exporter->ExportGeometry(pvs);
|
---|
[459] | 382 | exporter->SetWireframe();
|
---|
| 383 | exporter->ExportBox(rssTree->bbox);
|
---|
| 384 | exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
|
---|
[447] | 385 | delete exporter;
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | bool
|
---|
| 390 | RssPreprocessor::ComputeVisibility()
|
---|
| 391 | {
|
---|
[464] | 392 | cout<<"Rss Preprocessor started\n"<<flush;
|
---|
| 393 | cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
[447] | 394 | mSceneGraph->CollectObjects(&mObjects);
|
---|
| 395 |
|
---|
| 396 | long startTime = GetTime();
|
---|
| 397 |
|
---|
| 398 | int totalSamples = 0;
|
---|
| 399 |
|
---|
[451] | 400 | /// Rays used for post processing and visualizations.
|
---|
| 401 | RayContainer storedRays;
|
---|
[447] | 402 |
|
---|
| 403 | AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
| 404 |
|
---|
| 405 | if (fromBoxVisibility) {
|
---|
[459] | 406 | float m = box->Min(1);
|
---|
| 407 | float bsize = box->Size(1);
|
---|
| 408 |
|
---|
| 409 | float size = 0.02f;
|
---|
[447] | 410 | float s = 0.5f - size;
|
---|
| 411 | float olds = Magnitude(box->Size());
|
---|
| 412 | box->Enlarge(box->Size()*Vector3(-s));
|
---|
[459] | 413 | // Vector3 translation = Vector3(-olds*0.2f, 0, 0);
|
---|
| 414 | Vector3 translation = Vector3(-0.05*olds, 0, 0);
|
---|
[447] | 415 | box->SetMin(box->Min() + translation);
|
---|
| 416 | box->SetMax(box->Max() + translation);
|
---|
[459] | 417 |
|
---|
| 418 | box->SetMin(1, m + bsize*0.1);
|
---|
| 419 | box->SetMax(1, m + bsize*0.6);
|
---|
| 420 |
|
---|
| 421 |
|
---|
[447] | 422 | } else {
|
---|
| 423 |
|
---|
| 424 | // sample city like heights
|
---|
[459] | 425 | float m = box->Min(1);
|
---|
| 426 | float bsize = box->Size(1);
|
---|
| 427 | box->SetMin(1, m + bsize*0.2);
|
---|
| 428 | box->SetMax(1, m + bsize*0.3);
|
---|
[447] | 429 | }
|
---|
| 430 |
|
---|
| 431 | if (use2dSampling)
|
---|
| 432 | box->SetMax(1, box->Min(1));
|
---|
| 433 |
|
---|
| 434 | if (useViewSpaceBox)
|
---|
| 435 | mViewSpaceBox = box;
|
---|
| 436 | else
|
---|
| 437 | mViewSpaceBox = NULL;
|
---|
| 438 |
|
---|
| 439 |
|
---|
| 440 | RssTree *rssTree = NULL;
|
---|
| 441 |
|
---|
| 442 | while (totalSamples < mInitialSamples) {
|
---|
| 443 | int passContributingSamples = 0;
|
---|
| 444 | int passSampleContributions = 0;
|
---|
| 445 | int passSamples = 0;
|
---|
| 446 | int index = 0;
|
---|
| 447 |
|
---|
| 448 | int sampleContributions;
|
---|
| 449 |
|
---|
| 450 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
| 451 | for (int k=0; k < s; k++) {
|
---|
| 452 |
|
---|
| 453 | Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
| 454 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
| 455 |
|
---|
[452] | 456 | sampleContributions = CastRay(viewpoint, direction, mVssRays);
|
---|
[447] | 457 |
|
---|
| 458 |
|
---|
| 459 | //-- CORR matt: put block inside loop
|
---|
| 460 | if (sampleContributions) {
|
---|
| 461 | passContributingSamples ++;
|
---|
| 462 | passSampleContributions += sampleContributions;
|
---|
| 463 | }
|
---|
| 464 | passSamples++;
|
---|
| 465 | totalSamples++;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 |
|
---|
| 469 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
| 470 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
| 471 |
|
---|
| 472 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 473 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
| 474 | << "k #SampleContributions=" << passSampleContributions << " ("
|
---|
[467] | 475 | << 100*passContributingSamples/(float)passSamples<<"%)"
|
---|
| 476 | << "avgcontrib=" << avgRayContrib << endl;
|
---|
[447] | 477 |
|
---|
| 478 | mStats <<
|
---|
| 479 | "#Pass\n" <<mPass<<endl<<
|
---|
| 480 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
| 481 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
| 482 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
| 483 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
| 484 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
[467] | 485 |
|
---|
| 486 | mPass++;
|
---|
| 487 |
|
---|
[447] | 488 | }
|
---|
| 489 |
|
---|
| 490 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
| 491 | cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush;
|
---|
| 492 |
|
---|
| 493 |
|
---|
[464] | 494 | if (mExportRays) {
|
---|
[447] | 495 | char filename[64];
|
---|
| 496 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
[464] | 497 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
[447] | 498 | }
|
---|
| 499 |
|
---|
[467] | 500 | if (mUseViewcells) {
|
---|
[463] | 501 | // construct view cells
|
---|
| 502 | mViewCellsManager->Construct(mObjects, mVssRays, mViewSpaceBox);
|
---|
[447] | 503 |
|
---|
[467] | 504 | VssRayContainer selectedRays;
|
---|
| 505 | int desired = Max(
|
---|
| 506 | mViewCellsManager->GetPostProcessSamples(),
|
---|
| 507 | mViewCellsManager->GetVisualizationSamples());
|
---|
| 508 | float p = desired/(float)mVssRays.size();
|
---|
| 509 | // rssTree->CollectRays(storedRays, desired);
|
---|
| 510 | for (int i=0; i < mVssRays.size(); i++) {
|
---|
| 511 | if (Random(1.0f) < p)
|
---|
| 512 | selectedRays.push_back(mVssRays[i]);
|
---|
| 513 | }
|
---|
| 514 | //-- post process view cells
|
---|
| 515 | mViewCellsManager->PostProcess(mObjects, selectedRays);
|
---|
| 516 |
|
---|
| 517 | //-- several visualizations and statistics
|
---|
| 518 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 519 |
|
---|
| 520 | if (1)
|
---|
| 521 | mViewCellsManager->Visualize(mObjects, selectedRays);
|
---|
[447] | 522 |
|
---|
[467] | 523 | CLEAR_CONTAINER(storedRays);
|
---|
[463] | 524 | }
|
---|
[447] | 525 |
|
---|
[467] | 526 |
|
---|
| 527 | rssTree = new RssTree;
|
---|
| 528 |
|
---|
| 529 | if (mUseImportanceSampling) {
|
---|
| 530 |
|
---|
| 531 | if (fromBoxVisibility)
|
---|
| 532 | rssTree->Construct(mVssRays, mViewSpaceBox);
|
---|
| 533 | else
|
---|
| 534 | rssTree->Construct(mVssRays, NULL);
|
---|
| 535 | cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
|
---|
| 536 |
|
---|
| 537 | if (mExportRssTree) {
|
---|
| 538 | ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));
|
---|
| 539 | ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));
|
---|
| 540 | ExportRssTree("rss-tree-101.x3d", rssTree, Vector3(1,0,1));
|
---|
| 541 | ExportRssTree("rss-tree-101m.x3d", rssTree, Vector3(-1,0,-1));
|
---|
| 542 | ExportRssTreeLeaves(rssTree, 10);
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | if (mExportPvs) {
|
---|
| 546 | ExportPvs("rss-pvs-initial.x3d", rssTree);
|
---|
| 547 | }
|
---|
[459] | 548 | }
|
---|
[467] | 549 |
|
---|
[447] | 550 | // viewcells->UpdatePVS(newVssRays);
|
---|
[466] | 551 |
|
---|
[467] | 552 | int rssPass = 0;
|
---|
| 553 | int rssSamples = 0;
|
---|
[447] | 554 | while (1) {
|
---|
| 555 | int num = mRssSamplesPerPass;
|
---|
| 556 | SimpleRayContainer rays;
|
---|
| 557 | VssRayContainer vssRays;
|
---|
| 558 |
|
---|
| 559 | if (!mUseImportanceSampling) {
|
---|
| 560 | for (int j=0; j < num; j++) {
|
---|
| 561 | Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
| 562 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
| 563 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
| 564 | }
|
---|
| 565 | } else {
|
---|
| 566 | num = GenerateImportanceRays(rssTree, num, rays);
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 |
|
---|
| 570 | for (int i=0; i < rays.size(); i++)
|
---|
| 571 | CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
|
---|
[467] | 572 |
|
---|
[466] | 573 |
|
---|
[467] | 574 | totalSamples+=num;
|
---|
| 575 | rssSamples+=num;
|
---|
| 576 |
|
---|
| 577 | mStats <<
|
---|
| 578 | "#Pass\n" <<mPass<<endl<<
|
---|
| 579 | "#RssPass\n" <<rssPass<<endl<<
|
---|
| 580 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
| 581 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
| 582 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
| 583 |
|
---|
[466] | 584 | if (mUseViewcells) {
|
---|
| 585 |
|
---|
| 586 | /// compute view cell contribution of rays
|
---|
[467] | 587 | mViewCellsManager->ComputeSampleContributions(vssRays);
|
---|
[466] | 588 |
|
---|
[467] | 589 | vssRays.PrintStatistics(mStats);
|
---|
| 590 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
[466] | 591 | }
|
---|
| 592 |
|
---|
[467] | 593 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
| 594 | // already when adding into the tree
|
---|
| 595 | // do not add those rays which have too low or no contribution....
|
---|
[466] | 596 |
|
---|
[467] | 597 | if (mUseImportanceSampling) {
|
---|
| 598 | rssTree->AddRays(vssRays);
|
---|
| 599 |
|
---|
| 600 | if (0) {
|
---|
| 601 | cout<<"############# Rss PVS STAT ##################\n";
|
---|
| 602 | cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl;
|
---|
| 603 | cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | if (mUpdateSubdivision) {
|
---|
| 607 | int subdivided = rssTree->UpdateSubdivision();
|
---|
| 608 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
| 609 | cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl;
|
---|
| 610 | }
|
---|
[447] | 611 | }
|
---|
[467] | 612 |
|
---|
[464] | 613 | if (mExportRays) {
|
---|
[447] | 614 | char filename[64];
|
---|
| 615 | if (mUseImportanceSampling)
|
---|
[467] | 616 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
[447] | 617 | else
|
---|
[467] | 618 | sprintf(filename, "rss-rays-%04d.x3d", rssPass);
|
---|
[447] | 619 |
|
---|
[464] | 620 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
[447] | 621 | }
|
---|
| 622 |
|
---|
[463] | 623 |
|
---|
[464] | 624 | if (mExportPvs) {
|
---|
[447] | 625 | char filename[64];
|
---|
[467] | 626 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
[447] | 627 | ExportPvs(filename, rssTree);
|
---|
| 628 | }
|
---|
| 629 |
|
---|
[467] | 630 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
[447] | 631 | break;
|
---|
| 632 |
|
---|
| 633 |
|
---|
[467] | 634 | rssPass++;
|
---|
| 635 | mPass++;
|
---|
[447] | 636 | }
|
---|
| 637 |
|
---|
[464] | 638 | if (mUseViewcells) {
|
---|
[466] | 639 |
|
---|
| 640 |
|
---|
| 641 | //-- render simulation after merge
|
---|
| 642 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
[463] | 643 |
|
---|
[468] | 644 | mRenderSimulator->RenderScene();
|
---|
| 645 | SimulationStatistics ss;
|
---|
| 646 | mRenderSimulator->GetStatistics(ss);
|
---|
| 647 |
|
---|
[466] | 648 | cout << " finished" << endl;
|
---|
| 649 | cout << ss << endl;
|
---|
| 650 | Debug << ss << endl;
|
---|
| 651 |
|
---|
| 652 | }
|
---|
| 653 |
|
---|
[447] | 654 | delete rssTree;
|
---|
| 655 |
|
---|
| 656 | return true;
|
---|
| 657 | }
|
---|
| 658 |
|
---|