[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 | }
|
---|
[447] | 524 |
|
---|
| 525 |
|
---|
| 526 | RssTree *rssTree = NULL;
|
---|
[492] | 527 | int rssPass = 0;
|
---|
| 528 | int rssSamples = 0;
|
---|
[447] | 529 |
|
---|
[492] | 530 | #if 0
|
---|
| 531 | if (mLoadInitialSamples) {
|
---|
| 532 | cout << "Loading samples from file ... ";
|
---|
| 533 | LoadSamples(mVssRays, mObjects);
|
---|
| 534 | cout << "finished\n" << endl;
|
---|
| 535 | } else {
|
---|
| 536 | #endif
|
---|
[490] | 537 | while (totalSamples < mInitialSamples) {
|
---|
[492] | 538 | int passContributingSamples = 0;
|
---|
| 539 | int passSampleContributions = 0;
|
---|
| 540 | int passSamples = 0;
|
---|
| 541 | int index = 0;
|
---|
| 542 |
|
---|
| 543 | int sampleContributions;
|
---|
| 544 |
|
---|
| 545 | //int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
| 546 | int s = mInitialSamples;
|
---|
| 547 |
|
---|
| 548 | for (int k=0; k < s; k++) {
|
---|
| 549 |
|
---|
| 550 |
|
---|
[490] | 551 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
| 552 | Vector3 viewpoint;
|
---|
[504] | 553 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
| 554 | // viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
[492] | 555 | Vector3 direction = InitialGetDirection(viewpoint, mViewSpaceBox);
|
---|
| 556 |
|
---|
[490] | 557 | sampleContributions = CastRay(viewpoint, direction, mVssRays);
|
---|
[492] | 558 |
|
---|
| 559 |
|
---|
[490] | 560 | //-- CORR matt: put block inside loop
|
---|
| 561 | if (sampleContributions) {
|
---|
[492] | 562 | passContributingSamples ++;
|
---|
| 563 | passSampleContributions += sampleContributions;
|
---|
[490] | 564 | }
|
---|
| 565 | passSamples++;
|
---|
| 566 | totalSamples++;
|
---|
[492] | 567 | }
|
---|
| 568 |
|
---|
| 569 |
|
---|
| 570 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
[490] | 571 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
[492] | 572 |
|
---|
| 573 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 574 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
| 575 | << "k #SampleContributions=" << passSampleContributions << " ("
|
---|
| 576 | << 100*passContributingSamples/(float)passSamples<<"%)"
|
---|
| 577 | << "avgcontrib=" << avgRayContrib << endl;
|
---|
| 578 |
|
---|
| 579 | mPass++;
|
---|
[490] | 580 | }
|
---|
[492] | 581 |
|
---|
[490] | 582 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
[492] | 583 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
|
---|
| 584 |
|
---|
[508] | 585 | rssSamples += (int)mVssRays.size();
|
---|
[492] | 586 |
|
---|
| 587 |
|
---|
| 588 | if (mExportRays) {
|
---|
| 589 | char filename[64];
|
---|
| 590 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
| 591 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | if (mStoreInitialSamples)
|
---|
| 595 | {
|
---|
| 596 | cout << "Writing samples to file ... ";
|
---|
[508] | 597 | ExportSamples(mVssRays);
|
---|
[492] | 598 | cout << "finished\n" << endl;
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | if (mUseViewcells) {
|
---|
| 602 | // construct view cells
|
---|
| 603 | mViewCellsManager->Construct(mObjects, mVssRays);
|
---|
| 604 | // evaluate contributions of the intitial rays
|
---|
| 605 | mViewCellsManager->ComputeSampleContributions(mVssRays);
|
---|
| 606 |
|
---|
| 607 |
|
---|
| 608 | mStats <<
|
---|
| 609 | "#Pass\n" <<mPass<<endl<<
|
---|
| 610 | "#RssPass\n" <<rssPass<<endl<<
|
---|
| 611 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
| 612 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
| 613 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
| 614 |
|
---|
[490] | 615 |
|
---|
[502] | 616 | mVssRays.PrintStatistics(mStats);
|
---|
| 617 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
| 618 |
|
---|
| 619 | VssRayContainer selectedRays;
|
---|
| 620 | int desired = Max(
|
---|
| 621 | mViewCellsManager->GetPostProcessSamples(),
|
---|
| 622 | mViewCellsManager->GetVisualizationSamples());
|
---|
| 623 |
|
---|
| 624 | mVssRays.SelectRays(desired, selectedRays);
|
---|
| 625 |
|
---|
| 626 | //-- post process view cells
|
---|
| 627 | mViewCellsManager->PostProcess(mObjects, selectedRays);
|
---|
| 628 |
|
---|
| 629 | //-- several visualizations and statistics
|
---|
| 630 | Debug << "view cells after post processing: " << endl;
|
---|
| 631 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 632 |
|
---|
| 633 | if (1)
|
---|
| 634 | mViewCellsManager->Visualize(mObjects, selectedRays);
|
---|
[447] | 635 |
|
---|
[502] | 636 | ComputeRenderError();
|
---|
| 637 |
|
---|
| 638 | }
|
---|
[492] | 639 |
|
---|
[447] | 640 |
|
---|
[492] | 641 |
|
---|
| 642 | rssPass++;
|
---|
| 643 |
|
---|
[467] | 644 | rssTree = new RssTree;
|
---|
[492] | 645 | rssTree->SetPass(mPass);
|
---|
| 646 |
|
---|
[467] | 647 | if (mUseImportanceSampling) {
|
---|
| 648 |
|
---|
[516] | 649 | // if (fromBoxVisibility)
|
---|
| 650 | // rssTree->Construct(mObjects, mVssRays, mViewSpaceBox);
|
---|
| 651 | // else
|
---|
| 652 | rssTree->Construct(mObjects, mVssRays);
|
---|
[492] | 653 |
|
---|
| 654 | rssTree->stat.Print(mStats);
|
---|
[467] | 655 | cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
|
---|
| 656 |
|
---|
| 657 | if (mExportRssTree) {
|
---|
| 658 | ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));
|
---|
| 659 | ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));
|
---|
| 660 | ExportRssTree("rss-tree-101.x3d", rssTree, Vector3(1,0,1));
|
---|
| 661 | ExportRssTree("rss-tree-101m.x3d", rssTree, Vector3(-1,0,-1));
|
---|
| 662 | ExportRssTreeLeaves(rssTree, 10);
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | if (mExportPvs) {
|
---|
| 666 | ExportPvs("rss-pvs-initial.x3d", rssTree);
|
---|
| 667 | }
|
---|
[459] | 668 | }
|
---|
[467] | 669 |
|
---|
[447] | 670 | // viewcells->UpdatePVS(newVssRays);
|
---|
[466] | 671 |
|
---|
[492] | 672 |
|
---|
[447] | 673 | while (1) {
|
---|
| 674 | int num = mRssSamplesPerPass;
|
---|
| 675 | SimpleRayContainer rays;
|
---|
| 676 | VssRayContainer vssRays;
|
---|
[492] | 677 |
|
---|
| 678 |
|
---|
[447] | 679 | if (!mUseImportanceSampling) {
|
---|
| 680 | for (int j=0; j < num; j++) {
|
---|
[487] | 681 | //changed by matt
|
---|
| 682 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
| 683 | Vector3 viewpoint;
|
---|
| 684 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
[447] | 685 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
| 686 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
| 687 | }
|
---|
| 688 | } else {
|
---|
| 689 | num = GenerateImportanceRays(rssTree, num, rays);
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 |
|
---|
| 693 | for (int i=0; i < rays.size(); i++)
|
---|
| 694 | CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
|
---|
[467] | 695 |
|
---|
[466] | 696 |
|
---|
[503] | 697 | totalSamples += (int)rays.size();
|
---|
| 698 | rssSamples += (int)vssRays.size();
|
---|
[467] | 699 |
|
---|
[492] | 700 |
|
---|
| 701 |
|
---|
[467] | 702 | mStats <<
|
---|
| 703 | "#Pass\n" <<mPass<<endl<<
|
---|
| 704 | "#RssPass\n" <<rssPass<<endl<<
|
---|
| 705 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
| 706 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
| 707 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
| 708 |
|
---|
[492] | 709 |
|
---|
[466] | 710 | if (mUseViewcells) {
|
---|
| 711 |
|
---|
| 712 | /// compute view cell contribution of rays
|
---|
[467] | 713 | mViewCellsManager->ComputeSampleContributions(vssRays);
|
---|
[466] | 714 |
|
---|
[467] | 715 | vssRays.PrintStatistics(mStats);
|
---|
| 716 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
[466] | 717 | }
|
---|
| 718 |
|
---|
[492] | 719 |
|
---|
| 720 | ComputeRenderError();
|
---|
| 721 |
|
---|
| 722 | // epxort rays before adding them to the tree -> some of them can be deleted
|
---|
| 723 |
|
---|
| 724 | if (mExportRays) {
|
---|
| 725 | char filename[64];
|
---|
| 726 | if (mUseImportanceSampling)
|
---|
| 727 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
| 728 | else
|
---|
| 729 | sprintf(filename, "rss-rays-%04d.x3d", rssPass);
|
---|
| 730 |
|
---|
| 731 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
| 732 |
|
---|
| 733 | // now export all contributing rays
|
---|
| 734 | VssRayContainer contributingRays;
|
---|
| 735 | vssRays.GetContributingRays(contributingRays, mPass);
|
---|
[508] | 736 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
[492] | 737 | sprintf(filename, "rss-crays-%04d.x3d", rssPass);
|
---|
| 738 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 |
|
---|
[467] | 742 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
| 743 | // already when adding into the tree
|
---|
| 744 | // do not add those rays which have too low or no contribution....
|
---|
[466] | 745 |
|
---|
[467] | 746 | if (mUseImportanceSampling) {
|
---|
| 747 | rssTree->AddRays(vssRays);
|
---|
| 748 |
|
---|
| 749 | if (0) {
|
---|
| 750 | cout<<"############# Rss PVS STAT ##################\n";
|
---|
| 751 | cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl;
|
---|
| 752 | cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl;
|
---|
| 753 | }
|
---|
| 754 |
|
---|
[492] | 755 |
|
---|
[467] | 756 | if (mUpdateSubdivision) {
|
---|
[492] | 757 | int updatePasses = 1;
|
---|
| 758 | if (mPass % updatePasses == 0) {
|
---|
| 759 | int subdivided = rssTree->UpdateSubdivision();
|
---|
| 760 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
| 761 | cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl;
|
---|
| 762 | }
|
---|
[467] | 763 | }
|
---|
[447] | 764 | }
|
---|
[467] | 765 |
|
---|
[447] | 766 |
|
---|
[463] | 767 |
|
---|
[464] | 768 | if (mExportPvs) {
|
---|
[447] | 769 | char filename[64];
|
---|
[467] | 770 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
[447] | 771 | ExportPvs(filename, rssTree);
|
---|
| 772 | }
|
---|
| 773 |
|
---|
[492] | 774 |
|
---|
| 775 | if (!mUseImportanceSampling)
|
---|
| 776 | CLEAR_CONTAINER(vssRays);
|
---|
| 777 |
|
---|
[467] | 778 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
[447] | 779 | break;
|
---|
| 780 |
|
---|
| 781 |
|
---|
[467] | 782 | rssPass++;
|
---|
| 783 | mPass++;
|
---|
[492] | 784 | rssTree->SetPass(mPass);
|
---|
[447] | 785 | }
|
---|
| 786 |
|
---|
[464] | 787 | if (mUseViewcells) {
|
---|
[466] | 788 |
|
---|
| 789 |
|
---|
| 790 | //-- render simulation after merge
|
---|
| 791 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
[463] | 792 |
|
---|
[468] | 793 | mRenderSimulator->RenderScene();
|
---|
| 794 | SimulationStatistics ss;
|
---|
| 795 | mRenderSimulator->GetStatistics(ss);
|
---|
| 796 |
|
---|
[466] | 797 | cout << " finished" << endl;
|
---|
| 798 | cout << ss << endl;
|
---|
| 799 | Debug << ss << endl;
|
---|
| 800 |
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[447] | 803 | delete rssTree;
|
---|
| 804 |
|
---|
| 805 | return true;
|
---|
| 806 | }
|
---|
| 807 |
|
---|