[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;
|
---|
[534] | 17 | bool useViewspacePlane = false;
|
---|
[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 |
|
---|
[534] | 80 |
|
---|
| 81 | if (!mDetectEmptyViewSpace)
|
---|
| 82 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
[499] | 83 |
|
---|
[434] | 84 | if (mKdTree->CastRay(ray)) {
|
---|
| 85 | objectA = ray.intersections[0].mObject;
|
---|
| 86 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
| 87 | } else {
|
---|
| 88 | objectA = NULL;
|
---|
| 89 | // compute intersection with the scene bounding box
|
---|
| 90 | float tmin, tmax;
|
---|
[499] | 91 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 92 | pointA = ray.Extrap(tmax);
|
---|
| 93 | else
|
---|
| 94 | return 0;
|
---|
[434] | 95 | }
|
---|
[386] | 96 |
|
---|
[499] | 97 |
|
---|
[534] | 98 | if (mDetectEmptyViewSpace) {
|
---|
[434] | 99 | SetupRay(ray, pointA, -direction);
|
---|
| 100 | } else
|
---|
| 101 | SetupRay(ray, viewPoint, -direction);
|
---|
[534] | 102 |
|
---|
| 103 | if (!mDetectEmptyViewSpace)
|
---|
| 104 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
| 105 |
|
---|
[434] | 106 | if (mKdTree->CastRay(ray)) {
|
---|
| 107 | objectB = ray.intersections[0].mObject;
|
---|
| 108 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
| 109 | } else {
|
---|
| 110 | objectB = NULL;
|
---|
| 111 | float tmin, tmax;
|
---|
[499] | 112 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
| 113 | pointB = ray.Extrap(tmax);
|
---|
| 114 | else
|
---|
| 115 | return 0;
|
---|
| 116 | }
|
---|
[534] | 117 |
|
---|
[499] | 118 | // if (objectA == NULL && objectB != NULL) {
|
---|
[534] | 119 | if (mDetectEmptyViewSpace) {
|
---|
[499] | 120 | // cast again to ensure that there is no objectA
|
---|
| 121 | SetupRay(ray, pointB, direction);
|
---|
| 122 | if (mKdTree->CastRay(ray)) {
|
---|
| 123 | objectA = ray.intersections[0].mObject;
|
---|
| 124 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
[434] | 125 | }
|
---|
| 126 | }
|
---|
[534] | 127 |
|
---|
[499] | 128 |
|
---|
[434] | 129 | VssRay *vssRay = NULL;
|
---|
[386] | 130 |
|
---|
[499] | 131 | bool validSample = (objectA != objectB);
|
---|
[534] | 132 | if (0 && mDetectEmptyViewSpace) { // consider all samples valid
|
---|
[499] | 133 | // check if the viewpoint lies on the line segment AB
|
---|
[434] | 134 | if (Distance(pointA, pointB) <
|
---|
| 135 | Distance(viewPoint, pointA) + Distance(viewPoint, pointB) - Limits::Small) {
|
---|
| 136 | validSample = false;
|
---|
[386] | 137 | }
|
---|
[434] | 138 | }
|
---|
[499] | 139 |
|
---|
[434] | 140 | if (validSample) {
|
---|
| 141 | if (objectA) {
|
---|
| 142 | vssRay = new VssRay(pointB,
|
---|
| 143 | pointA,
|
---|
| 144 | objectB,
|
---|
[499] | 145 | objectA,
|
---|
| 146 | mPass
|
---|
| 147 | );
|
---|
[434] | 148 | vssRays.push_back(vssRay);
|
---|
| 149 | hits ++;
|
---|
| 150 | }
|
---|
[499] | 151 |
|
---|
[434] | 152 | if (objectB) {
|
---|
| 153 | vssRay = new VssRay(pointA,
|
---|
| 154 | pointB,
|
---|
| 155 | objectA,
|
---|
[499] | 156 | objectB,
|
---|
| 157 | mPass
|
---|
| 158 | );
|
---|
[434] | 159 | vssRays.push_back(vssRay);
|
---|
| 160 | hits ++;
|
---|
[372] | 161 | }
|
---|
[434] | 162 | }
|
---|
[499] | 163 |
|
---|
[434] | 164 | return hits;
|
---|
[372] | 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
[376] | 168 | Vector3
|
---|
[382] | 169 | VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
[372] | 170 | {
|
---|
[434] | 171 | AxisAlignedBox3 box;
|
---|
[468] | 172 |
|
---|
[434] | 173 | if (viewSpaceBox)
|
---|
| 174 | box =*viewSpaceBox;
|
---|
[468] | 175 | else
|
---|
[434] | 176 | box = mKdTree->GetBox();
|
---|
[468] | 177 |
|
---|
[434] | 178 | // shrink the box in the y direction
|
---|
| 179 | return box.GetRandomPoint();
|
---|
[372] | 180 | }
|
---|
| 181 |
|
---|
[376] | 182 | Vector3
|
---|
[427] | 183 | VssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
[434] | 184 | AxisAlignedBox3 *viewSpaceBox
|
---|
| 185 | )
|
---|
[372] | 186 | {
|
---|
[434] | 187 | Vector3 point;
|
---|
| 188 | if (!use2dSampling) {
|
---|
[534] | 189 | if (0) {
|
---|
| 190 | Vector3 normal;
|
---|
| 191 | int i = Random((int)mObjects.size());
|
---|
| 192 | Intersectable *object = mObjects[i];
|
---|
| 193 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 194 | } else
|
---|
| 195 | point = mKdTree->GetBox().GetRandomPoint();
|
---|
| 196 | // point = viewpoint + UniformRandomVector();
|
---|
| 197 |
|
---|
[434] | 198 | } else {
|
---|
| 199 | AxisAlignedBox3 box;
|
---|
[468] | 200 |
|
---|
[434] | 201 | if (viewSpaceBox)
|
---|
| 202 | box =*viewSpaceBox;
|
---|
[468] | 203 | else
|
---|
[434] | 204 | box = mKdTree->GetBox();
|
---|
[468] | 205 |
|
---|
[434] | 206 | point = box.GetRandomPoint();
|
---|
| 207 | point.y = viewpoint.y;
|
---|
| 208 | }
|
---|
[468] | 209 |
|
---|
[434] | 210 | return point - viewpoint;
|
---|
[372] | 211 | }
|
---|
| 212 |
|
---|
[401] | 213 | int
|
---|
[427] | 214 | VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
|
---|
[434] | 215 | const int desiredSamples,
|
---|
| 216 | SimpleRayContainer &rays
|
---|
| 217 | )
|
---|
[401] | 218 | {
|
---|
[434] | 219 | int num;
|
---|
| 220 | if (0) {
|
---|
| 221 | float minRayContribution;
|
---|
| 222 | float maxRayContribution;
|
---|
| 223 | float avgRayContribution;
|
---|
[468] | 224 |
|
---|
[434] | 225 | vssTree->GetRayContributionStatistics(minRayContribution,
|
---|
| 226 | maxRayContribution,
|
---|
| 227 | avgRayContribution);
|
---|
[468] | 228 |
|
---|
[434] | 229 | cout<<
|
---|
| 230 | "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
|
---|
| 231 | "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
|
---|
| 232 | "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
|
---|
[468] | 233 |
|
---|
[434] | 234 | float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
|
---|
| 235 | num = vssTree->GenerateRays(p, rays);
|
---|
| 236 | } else {
|
---|
[534] | 237 | int leaves = vssTree->stat.Leaves();
|
---|
[434] | 238 | num = vssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
| 239 | }
|
---|
[468] | 240 |
|
---|
[434] | 241 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
[468] | 242 |
|
---|
[434] | 243 | return num;
|
---|
[427] | 244 | }
|
---|
[376] | 245 |
|
---|
| 246 |
|
---|
[427] | 247 | bool
|
---|
| 248 | VssPreprocessor::ExportRays(const char *filename,
|
---|
[434] | 249 | const VssRayContainer &vssRays,
|
---|
| 250 | const int number
|
---|
| 251 | )
|
---|
[427] | 252 | {
|
---|
[434] | 253 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
[468] | 254 |
|
---|
[434] | 255 | Exporter *exporter = NULL;
|
---|
| 256 | exporter = Exporter::GetExporter(filename);
|
---|
| 257 | // exporter->SetWireframe();
|
---|
| 258 | // exporter->ExportKdTree(*mKdTree);
|
---|
| 259 | exporter->SetFilled();
|
---|
| 260 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 261 | exporter->SetWireframe();
|
---|
[427] | 262 |
|
---|
[434] | 263 | if (mViewSpaceBox) {
|
---|
[438] | 264 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
[434] | 265 | exporter->ExportBox(*mViewSpaceBox);
|
---|
| 266 | exporter->ResetForcedMaterial();
|
---|
| 267 | }
|
---|
[468] | 268 |
|
---|
[534] | 269 | VssRayContainer rays;
|
---|
| 270 | vssRays.SelectRays(number, rays);
|
---|
| 271 |
|
---|
[434] | 272 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
[468] | 273 |
|
---|
[434] | 274 | delete exporter;
|
---|
[401] | 275 |
|
---|
[434] | 276 | cout<<"done."<<endl<<flush;
|
---|
[427] | 277 |
|
---|
[434] | 278 | return true;
|
---|
[401] | 279 | }
|
---|
| 280 |
|
---|
| 281 |
|
---|
[372] | 282 | bool
|
---|
[434] | 283 | VssPreprocessor::ExportVssTree(char *filename,
|
---|
[438] | 284 | VssTree *tree,
|
---|
| 285 | const Vector3 &dir
|
---|
| 286 | )
|
---|
[434] | 287 | {
|
---|
| 288 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 289 | exporter->SetFilled();
|
---|
| 290 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
[438] | 291 | // exporter->SetWireframe();
|
---|
| 292 | bool result = exporter->ExportVssTree2( *tree, dir );
|
---|
[434] | 293 | delete exporter;
|
---|
| 294 | return result;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | bool
|
---|
[427] | 298 | VssPreprocessor::ExportVssTreeLeaf(char *filename,
|
---|
[434] | 299 | VssTree *tree,
|
---|
| 300 | VssTreeLeaf *leaf)
|
---|
[427] | 301 | {
|
---|
[434] | 302 | Exporter *exporter = NULL;
|
---|
| 303 | exporter = Exporter::GetExporter(filename);
|
---|
| 304 | exporter->SetWireframe();
|
---|
| 305 | exporter->ExportKdTree(*mKdTree);
|
---|
[468] | 306 |
|
---|
[434] | 307 | if (mViewSpaceBox) {
|
---|
| 308 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
| 309 | exporter->ExportBox(*mViewSpaceBox);
|
---|
[427] | 310 | exporter->ResetForcedMaterial();
|
---|
[434] | 311 | }
|
---|
[468] | 312 |
|
---|
[434] | 313 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 314 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
| 315 | exporter->ResetForcedMaterial();
|
---|
[468] | 316 |
|
---|
[434] | 317 | VssRayContainer rays[4];
|
---|
| 318 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
| 319 | int k = leaf->rays[i].GetRayClass();
|
---|
| 320 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
| 321 | }
|
---|
[468] | 322 |
|
---|
[434] | 323 | // SOURCE RAY
|
---|
| 324 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
| 325 | // TERMINATION RAY
|
---|
| 326 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
| 327 | // PASSING_RAY
|
---|
| 328 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
| 329 | // CONTAINED_RAY
|
---|
| 330 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
[427] | 331 |
|
---|
[434] | 332 | delete exporter;
|
---|
| 333 | return true;
|
---|
[427] | 334 | }
|
---|
| 335 |
|
---|
| 336 | void
|
---|
| 337 | VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
|
---|
| 338 | {
|
---|
[434] | 339 | vector<VssTreeLeaf *> leaves;
|
---|
| 340 | tree->CollectLeaves(leaves);
|
---|
[427] | 341 |
|
---|
[434] | 342 | int num = 0;
|
---|
| 343 | int i;
|
---|
| 344 | float p = number / (float)leaves.size();
|
---|
| 345 | for (i=0; i < leaves.size(); i++) {
|
---|
| 346 | if (RandomValue(0,1) < p) {
|
---|
| 347 | char filename[64];
|
---|
| 348 | sprintf(filename, "vss-leaf-%04d.x3d", num);
|
---|
| 349 | ExportVssTreeLeaf(filename, tree, leaves[i]);
|
---|
| 350 | num++;
|
---|
[427] | 351 | }
|
---|
[434] | 352 | if (num >= number)
|
---|
| 353 | break;
|
---|
| 354 | }
|
---|
[427] | 355 | }
|
---|
[535] | 356 | #include "ViewCellBsp.h"
|
---|
[427] | 357 |
|
---|
[535] | 358 | void VssPreprocessor::TestBeamCasting(VssTree *tree,
|
---|
| 359 | ViewCellsManager *vm,
|
---|
| 360 | const ObjectContainer &objects)
|
---|
[530] | 361 | {
|
---|
[540] | 362 | //debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
| 363 | // renderer->resize(640, 480);
|
---|
| 364 | //debuggerWidget->resize(640, 480);
|
---|
| 365 |
|
---|
[531] | 366 | vector<VssTreeLeaf *> leaves;
|
---|
| 367 | tree->CollectLeaves(leaves);
|
---|
[530] | 368 |
|
---|
[535] | 369 | Exporter *exporter = Exporter::GetExporter("shafts.x3d");
|
---|
| 370 |
|
---|
| 371 | exporter->SetWireframe();
|
---|
[540] | 372 | exporter->ExportGeometry(objects);
|
---|
[535] | 373 | exporter->SetFilled();
|
---|
| 374 | //Randomize();
|
---|
[540] | 375 | debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
| 376 |
|
---|
| 377 | /*debuggerWidget->mBeam = beam;
|
---|
| 378 | debuggerWidget->mSourceObject = sourceObj;
|
---|
| 379 | debuggerWidget->mSamples = 10000;
|
---|
| 380 |
|
---|
| 381 | Debug << "showing window" << endl;
|
---|
| 382 | debuggerWidget->show();*/
|
---|
| 383 |
|
---|
| 384 | renderer->makeCurrent();
|
---|
| 385 |
|
---|
| 386 | for (int i = 0; i < 10; ++ i)
|
---|
[530] | 387 | {
|
---|
[540] | 388 | Beam beam;
|
---|
| 389 | Intersectable *sourceObj = mObjects[5];
|
---|
| 390 |
|
---|
[531] | 391 | const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
|
---|
| 392 | VssTreeLeaf *leaf = leaves[index];
|
---|
[530] | 393 |
|
---|
[535] | 394 | AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
|
---|
[531] | 395 | AxisAlignedBox3 box = tree->GetBBox(leaf);
|
---|
[532] | 396 |
|
---|
[535] | 397 | beam.Construct(box, dirBox);
|
---|
[532] | 398 |
|
---|
| 399 | // collect kd leaves and view cells
|
---|
| 400 | mKdTree->CastBeam(beam);
|
---|
| 401 | vm->CastBeam(beam);
|
---|
| 402 |
|
---|
| 403 | Debug << "found " << beam.mViewCells.size() << " view cells and "
|
---|
| 404 | << beam.mKdNodes.size() << " kd nodes" << endl;
|
---|
| 405 |
|
---|
[530] | 406 | BeamSampleStatistics stats;
|
---|
[538] | 407 |
|
---|
[531] | 408 | renderer->SampleBeamContributions(sourceObj, |
---|
| 409 | beam, |
---|
[540] | 410 | 200000, |
---|
[531] | 411 | stats);
|
---|
[530] | 412 |
|
---|
[540] | 413 | char s[64]; sprintf(s, "shaft%04d.png", i);
|
---|
| 414 |
|
---|
| 415 | QImage image = renderer->toImage();
|
---|
| 416 | image.save(s, "PNG");
|
---|
[532] | 417 | Debug << "beam statistics: " << stats << endl << endl;
|
---|
[540] | 418 |
|
---|
| 419 | if (1)
|
---|
| 420 | {
|
---|
| 421 | AxisAlignedBox3 sbox = mSceneGraph->GetBox();
|
---|
| 422 | Vector3 bmin = sbox.Min() - 150.0f;
|
---|
| 423 | Vector3 bmax = sbox.Max() + 150.0f;
|
---|
| 424 | AxisAlignedBox3 vbox(bmin, bmax);
|
---|
[535] | 425 |
|
---|
[540] | 426 | exporter->ExportBeam(beam, vbox);
|
---|
| 427 | }
|
---|
[535] | 428 |
|
---|
[540] | 429 | bool exportViewCells = false;
|
---|
[535] | 430 |
|
---|
| 431 | if (exportViewCells)
|
---|
| 432 | {
|
---|
| 433 | ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
|
---|
| 434 |
|
---|
| 435 | for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
|
---|
| 436 | {
|
---|
| 437 | BspNodeGeometry geom;
|
---|
| 438 | AxisAlignedBox3 vbox;
|
---|
| 439 | vbox.Initialize();
|
---|
[538] | 440 | vbox.Include((*it)->GetMesh());
|
---|
| 441 |
|
---|
| 442 | exporter->SetWireframe();
|
---|
[535] | 443 | exporter->ExportBox(vbox);
|
---|
[538] | 444 | exporter->SetFilled();
|
---|
| 445 | exporter->ExportViewCell(*it);
|
---|
[535] | 446 | }
|
---|
[538] | 447 |
|
---|
| 448 | /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
| 449 |
|
---|
| 450 | for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
|
---|
| 451 | {
|
---|
| 452 | exporter->ExportBox(mKdTree->GetBox((*it)));
|
---|
| 453 | }*/
|
---|
[535] | 454 | }
|
---|
[530] | 455 | }
|
---|
[540] | 456 | /*while (1)
|
---|
| 457 | { debuggerWidget->repaint();
|
---|
| 458 | };*/
|
---|
[535] | 459 | delete exporter;
|
---|
[530] | 460 | }
|
---|
| 461 |
|
---|
[540] | 462 |
|
---|
[434] | 463 | float
|
---|
| 464 | VssPreprocessor::GetAvgPvsSize(VssTree *tree,
|
---|
| 465 | const vector<AxisAlignedBox3> &viewcells
|
---|
| 466 | )
|
---|
| 467 | {
|
---|
| 468 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
| 469 |
|
---|
| 470 | int sum = 0;
|
---|
| 471 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
| 472 | sum += tree->GetPvsSize(*it);
|
---|
[468] | 473 |
|
---|
[434] | 474 | return sum/(float)viewcells.size();
|
---|
| 475 | }
|
---|
| 476 |
|
---|
[427] | 477 | bool
|
---|
[372] | 478 | VssPreprocessor::ComputeVisibility()
|
---|
| 479 | {
|
---|
[468] | 480 |
|
---|
| 481 |
|
---|
[372] | 482 | long startTime = GetTime();
|
---|
[468] | 483 |
|
---|
[372] | 484 | int totalSamples = 0;
|
---|
| 485 |
|
---|
| 486 |
|
---|
[434] | 487 | AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
[534] | 488 |
|
---|
[434] | 489 | if (!useViewspacePlane) {
|
---|
[446] | 490 | float size = 0.05f;
|
---|
[434] | 491 | float s = 0.5f - size;
|
---|
| 492 | float olds = Magnitude(box->Size());
|
---|
| 493 | box->Enlarge(box->Size()*Vector3(-s));
|
---|
| 494 | Vector3 translation = Vector3(-olds*0.1f, 0, 0);
|
---|
| 495 | box->SetMin(box->Min() + translation);
|
---|
| 496 | box->SetMax(box->Max() + translation);
|
---|
| 497 | } else {
|
---|
[468] | 498 |
|
---|
[434] | 499 | // sample city like heights
|
---|
[469] | 500 | box->SetMin(1, box->Min(1) + box->Size(1)*0.2f);
|
---|
| 501 | box->SetMax(1, box->Min(1) + box->Size(1)*0.3f);
|
---|
[434] | 502 | }
|
---|
[427] | 503 |
|
---|
[434] | 504 | if (use2dSampling)
|
---|
| 505 | box->SetMax(1, box->Min(1));
|
---|
[468] | 506 |
|
---|
[534] | 507 | cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
|
---|
[551] | 508 | Debug << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
[501] | 509 | if (mUseViewSpaceBox)
|
---|
[487] | 510 | {
|
---|
[434] | 511 | mViewSpaceBox = box;
|
---|
[487] | 512 | mViewCellsManager->SetViewSpaceBox(*box);
|
---|
| 513 | }
|
---|
[434] | 514 | else
|
---|
[487] | 515 | {
|
---|
[434] | 516 | mViewSpaceBox = NULL;
|
---|
[487] | 517 | mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
|
---|
| 518 | }
|
---|
[534] | 519 |
|
---|
[518] | 520 | //-- load view cells from file if requested
|
---|
| 521 | if (mLoadViewCells)
|
---|
[527] | 522 | {
|
---|
| 523 | // load now because otherwise bounding box not correct
|
---|
[520] | 524 | mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects);
|
---|
[518] | 525 | }
|
---|
| 526 |
|
---|
| 527 |
|
---|
[434] | 528 | VssTree *vssTree = NULL;
|
---|
[401] | 529 |
|
---|
[490] | 530 | mSceneGraph->CollectObjects(&mObjects);
|
---|
[468] | 531 |
|
---|
[490] | 532 | long initialTime = GetTime();
|
---|
[468] | 533 |
|
---|
[490] | 534 | if (mLoadInitialSamples)
|
---|
| 535 | {
|
---|
| 536 | cout << "Loading samples from file ... ";
|
---|
| 537 | LoadSamples(mVssRays, mObjects);
|
---|
| 538 | cout << "finished\n" << endl;
|
---|
| 539 | totalSamples = (int)mVssRays.size();
|
---|
| 540 | }
|
---|
| 541 | else
|
---|
| 542 | {
|
---|
[534] | 543 |
|
---|
| 544 | while (totalSamples < mInitialSamples) {
|
---|
[490] | 545 | int passContributingSamples = 0;
|
---|
| 546 | int passSampleContributions = 0;
|
---|
| 547 | int passSamples = 0;
|
---|
[468] | 548 |
|
---|
[490] | 549 | int index = 0;
|
---|
[468] | 550 |
|
---|
[490] | 551 | int sampleContributions;
|
---|
[468] | 552 |
|
---|
[490] | 553 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
| 554 | for (int k=0; k < s; k++) {
|
---|
| 555 | // changed by matt
|
---|
| 556 | Vector3 viewpoint;
|
---|
[534] | 557 | // viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
[490] | 558 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
| 559 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
[534] | 560 |
|
---|
[490] | 561 | sampleContributions = CastRay(viewpoint, direction, mVssRays);
|
---|
[468] | 562 |
|
---|
[490] | 563 | if (sampleContributions) {
|
---|
| 564 | passContributingSamples ++;
|
---|
| 565 | passSampleContributions += sampleContributions;
|
---|
| 566 | }
|
---|
| 567 | passSamples++;
|
---|
| 568 | totalSamples++;
|
---|
| 569 | }
|
---|
[468] | 570 |
|
---|
[490] | 571 | mPass++;
|
---|
| 572 | int pvsSize = 0;
|
---|
| 573 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
| 574 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
[468] | 575 |
|
---|
[490] | 576 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 577 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
| 578 | << "k #SampleContributions=" << passSampleContributions << " ("
|
---|
| 579 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
| 580 | << pvsSize/(float)mObjects.size() << endl
|
---|
| 581 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
[468] | 582 |
|
---|
[490] | 583 | mStats <<
|
---|
| 584 | "#Pass\n" <<mPass<<endl<<
|
---|
| 585 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
| 586 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
| 587 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
| 588 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
| 589 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
| 590 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
[534] | 594 |
|
---|
| 595 |
|
---|
| 596 |
|
---|
[490] | 597 | }
|
---|
| 598 |
|
---|
| 599 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
|
---|
| 600 | Debug << (int)mVssRays.size() << " rays generated in "
|
---|
| 601 | << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
|
---|
[401] | 602 |
|
---|
[490] | 603 | if (mStoreInitialSamples)
|
---|
| 604 | {
|
---|
| 605 | cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
|
---|
[508] | 606 | ExportSamples(mVssRays);
|
---|
[490] | 607 | cout << "finished\n" << endl;
|
---|
[491] | 608 |
|
---|
| 609 | /*VssRayContainer dummyRays;
|
---|
| 610 | LoadSamples(dummyRays, mObjects);
|
---|
| 611 | Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
|
---|
| 612 |
|
---|
| 613 | for (int i = 0; i < (int)mVssRays.size(); ++ i)
|
---|
| 614 | {
|
---|
| 615 | Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
|
---|
| 616 | Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
|
---|
| 617 | }*/
|
---|
[434] | 618 | }
|
---|
[468] | 619 |
|
---|
[534] | 620 |
|
---|
[540] | 621 | //int numExportRays = 5000;
|
---|
| 622 | int numExportRays = 0;
|
---|
[372] | 623 |
|
---|
[434] | 624 | if (numExportRays) {
|
---|
| 625 | char filename[64];
|
---|
| 626 | sprintf(filename, "vss-rays-initial.x3d");
|
---|
| 627 | ExportRays(filename, mVssRays, numExportRays);
|
---|
| 628 | }
|
---|
[468] | 629 |
|
---|
[518] | 630 | /// compute view cell contribution of rays if view cells manager already constructed
|
---|
| 631 | mViewCellsManager->ComputeSampleContributions(mVssRays);
|
---|
| 632 |
|
---|
[448] | 633 | // construct view cells
|
---|
[517] | 634 | if (!mDelayedViewCellsConstruction)
|
---|
| 635 | mViewCellsManager->Construct(mObjects, mVssRays);
|
---|
| 636 |
|
---|
[434] | 637 | vssTree = new VssTree;
|
---|
| 638 | // viewcells = Construct(mVssRays);
|
---|
[468] | 639 |
|
---|
[434] | 640 | vssTree->Construct(mVssRays, mViewSpaceBox);
|
---|
| 641 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
[468] | 642 |
|
---|
[465] | 643 | if (0)
|
---|
| 644 | {
|
---|
| 645 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
| 646 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
| 647 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
| 648 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
| 649 | ExportVssTreeLeaves(vssTree, 10);
|
---|
| 650 | }
|
---|
[430] | 651 |
|
---|
[434] | 652 | // viewcells->UpdatePVS(newVssRays);
|
---|
| 653 | // get viewcells as kd tree boxes
|
---|
| 654 | vector<AxisAlignedBox3> kdViewcells;
|
---|
| 655 | if (0) {
|
---|
| 656 | vector<KdLeaf *> leaves;
|
---|
| 657 | mKdTree->CollectLeaves(leaves);
|
---|
| 658 | vector<KdLeaf *>::const_iterator it;
|
---|
| 659 | int targetLeaves = 50;
|
---|
| 660 | float prob = targetLeaves/(float)leaves.size();
|
---|
| 661 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
| 662 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
| 663 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
[468] | 664 |
|
---|
[434] | 665 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 666 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
[468] | 669 |
|
---|
[434] | 670 | int samples = 0;
|
---|
| 671 | int pass = 0;
|
---|
[448] | 672 |
|
---|
[535] | 673 |
|
---|
[448] | 674 | // cast view cell samples
|
---|
[434] | 675 | while (1) {
|
---|
| 676 | int num = mVssSamplesPerPass;
|
---|
| 677 | SimpleRayContainer rays;
|
---|
| 678 | VssRayContainer vssRays;
|
---|
[468] | 679 |
|
---|
[434] | 680 | if (!mUseImportanceSampling) {
|
---|
| 681 | for (int j=0; j < num; j++) {
|
---|
[487] | 682 | // changed by matt
|
---|
| 683 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
| 684 | Vector3 viewpoint;
|
---|
| 685 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
[434] | 686 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
| 687 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
| 688 | }
|
---|
| 689 | } else {
|
---|
| 690 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
| 691 | }
|
---|
[468] | 692 |
|
---|
[434] | 693 | for (int i=0; i < rays.size(); i++)
|
---|
| 694 | CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
|
---|
[468] | 695 |
|
---|
[434] | 696 | vssTree->AddRays(vssRays);
|
---|
[468] | 697 |
|
---|
[434] | 698 | if (0) {
|
---|
| 699 | int subdivided = vssTree->UpdateSubdivision();
|
---|
| 700 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
| 701 | }
|
---|
[427] | 702 |
|
---|
[434] | 703 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 704 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
[430] | 705 |
|
---|
[517] | 706 | // construct view cells after vss
|
---|
[554] | 707 | if (!mViewCellsManager->ViewCellsConstructed() &&
|
---|
| 708 | (samples + mInitialSamples >= mViewCellsManager->GetConstructionSamples()))
|
---|
[517] | 709 | {
|
---|
[552] | 710 | Debug << "samples " << samples << " initial " << mInitialSamples << " " << samples + mInitialSamples << endl;
|
---|
[517] | 711 | VssRayContainer constructionRays;
|
---|
[554] | 712 | vssTree->CollectRays(constructionRays, mViewCellsManager->GetConstructionSamples());
|
---|
[552] | 713 |
|
---|
| 714 | Debug << "collected rays: " << constructionRays.size() << endl;
|
---|
| 715 |
|
---|
| 716 | // test if we really have enough rays
|
---|
[554] | 717 | //if (constructionRays.size() >= nSamples)
|
---|
| 718 | mViewCellsManager->Construct(mObjects, constructionRays);
|
---|
[517] | 719 | }
|
---|
| 720 |
|
---|
| 721 | /// compute view cell contribution of rays
|
---|
| 722 | mViewCellsManager->ComputeSampleContributions(vssRays);
|
---|
| 723 |
|
---|
[434] | 724 | if (numExportRays) {
|
---|
| 725 | char filename[64];
|
---|
| 726 | if (mUseImportanceSampling)
|
---|
| 727 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
| 728 | else
|
---|
| 729 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
[468] | 730 |
|
---|
[434] | 731 | ExportRays(filename, vssRays, numExportRays);
|
---|
[401] | 732 | }
|
---|
[386] | 733 |
|
---|
[434] | 734 | samples+=num;
|
---|
| 735 | float pvs = vssTree->GetAvgPvsSize();
|
---|
| 736 | cout<<"*****************************\n";
|
---|
| 737 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
| 738 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
| 739 | cout<<"*****************************\n";
|
---|
| 740 | if (samples >= mVssSamples)
|
---|
| 741 | break;
|
---|
| 742 | pass++;
|
---|
| 743 | }
|
---|
[448] | 744 |
|
---|
[552] | 745 |
|
---|
[553] | 746 | // at the latest create view cells here
|
---|
| 747 | if (!mViewCellsManager->ViewCellsConstructed())
|
---|
| 748 | {
|
---|
| 749 | VssRayContainer constructionRays;
|
---|
| 750 |
|
---|
[554] | 751 | vssTree->CollectRays(constructionRays,
|
---|
| 752 | mViewCellsManager->GetConstructionSamples());
|
---|
[553] | 753 | Debug << "collected rays: " << constructionRays.size() << endl;
|
---|
| 754 |
|
---|
| 755 | mViewCellsManager->Construct(mObjects, constructionRays);
|
---|
| 756 | }
|
---|
| 757 |
|
---|
[535] | 758 | Debug << vssTree->stat << endl;
|
---|
[517] | 759 | VssRayContainer viewCellRays;
|
---|
| 760 |
|
---|
| 761 | // compute rays used for view cells construction
|
---|
| 762 | int numRays = Max(mViewCellsManager->GetPostProcessSamples(),
|
---|
| 763 | mViewCellsManager->GetVisualizationSamples());
|
---|
[468] | 764 |
|
---|
[517] | 765 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
| 766 |
|
---|
| 767 | //-- post process view cells
|
---|
| 768 | mViewCellsManager->PostProcess(mObjects, viewCellRays);
|
---|
[468] | 769 |
|
---|
[540] | 770 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
| 771 | {
|
---|
[535] | 772 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
[540] | 773 | }
|
---|
[535] | 774 |
|
---|
[517] | 775 | //-- several visualizations and statistics
|
---|
| 776 | Debug << "\nview cells after post processing: " << endl;
|
---|
| 777 | mViewCellsManager->PrintStatistics(Debug);
|
---|
[452] | 778 |
|
---|
[517] | 779 | mViewCellsManager->Visualize(mObjects, viewCellRays);
|
---|
| 780 |
|
---|
[452] | 781 | //-- render simulation after merge
|
---|
| 782 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
[468] | 783 | mRenderSimulator->RenderScene();
|
---|
| 784 | SimulationStatistics ss;
|
---|
| 785 | mRenderSimulator->GetStatistics(ss);
|
---|
[474] | 786 |
|
---|
[452] | 787 | cout << " finished" << endl;
|
---|
| 788 | cout << ss << endl;
|
---|
| 789 | Debug << ss << endl;
|
---|
[468] | 790 |
|
---|
[434] | 791 | delete vssTree;
|
---|
[475] | 792 |
|
---|
[434] | 793 | return true;
|
---|
[466] | 794 | }
|
---|