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