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