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