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