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