[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"
|
---|
[1221] | 15 | #include "Intersectable.h"
|
---|
[1251] | 16 | #ifdef GTP_INTERNAL
|
---|
| 17 | #include "ArchModeler2MLRT.hxx"
|
---|
| 18 | #endif
|
---|
[372] | 19 |
|
---|
[863] | 20 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 21 |
|
---|
[427] | 22 | bool use2dSampling = false;
|
---|
[534] | 23 | bool useViewspacePlane = false;
|
---|
[427] | 24 |
|
---|
[372] | 25 | VssPreprocessor::VssPreprocessor():
|
---|
[1002] | 26 | mVssRays(),
|
---|
| 27 | mViewSpaceBox(NULL)
|
---|
[372] | 28 | {
|
---|
| 29 | // this should increase coherence of the samples
|
---|
[1004] | 30 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
| 31 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
|
---|
| 32 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
|
---|
| 33 | Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
|
---|
| 34 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
[574] | 35 |
|
---|
[1004] | 36 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
| 37 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
| 38 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
|
---|
| 39 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
|
---|
| 40 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.enlargeViewSpace", mEnlargeViewSpace);
|
---|
| 41 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
[694] | 42 |
|
---|
[508] | 43 | useViewspacePlane = mUseViewSpaceBox; //hack
|
---|
[490] | 44 |
|
---|
[675] | 45 |
|
---|
| 46 | Debug << "*********** vss preprocessor options **************" << endl;
|
---|
| 47 | Debug << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
| 48 | Debug << "enlarge view space=" << mEnlargeViewSpace << endl;
|
---|
| 49 | Debug << "*********** end vss preprocessor options **************" << endl;
|
---|
| 50 |
|
---|
[372] | 51 | mStats.open("stats.log");
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[685] | 54 |
|
---|
[372] | 55 | VssPreprocessor::~VssPreprocessor()
|
---|
| 56 | {
|
---|
[674] | 57 | CLEAR_CONTAINER(mVssRays);
|
---|
| 58 | DEL_PTR(mViewSpaceBox);
|
---|
[372] | 59 | }
|
---|
| 60 |
|
---|
[685] | 61 |
|
---|
[372] | 62 | void
|
---|
[574] | 63 | VssPreprocessor::CastRays(
|
---|
| 64 | SimpleRayContainer &rays,
|
---|
| 65 | VssRayContainer &vssRays
|
---|
| 66 | )
|
---|
| 67 | {
|
---|
[1251] | 68 | const AxisAlignedBox3 box = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
|
---|
[1221] | 69 |
|
---|
| 70 | #if 1
|
---|
[1251] | 71 | SimpleRayContainer::const_iterator rit, rit_end = rays.end();
|
---|
| 72 | for (rit = rays.begin(); rit != rit_end; ++ rit)
|
---|
[1221] | 73 | {
|
---|
[1251] | 74 | CastRay((*rit).mOrigin, (*rit).mDirection, 1, vssRays, box);
|
---|
[1221] | 75 | }
|
---|
| 76 | #else
|
---|
| 77 | for (int i = 0; i < rays.size(); i += 16)
|
---|
| 78 | {
|
---|
| 79 | CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays, box);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | #endif
|
---|
[574] | 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
[1221] | 86 | void VssPreprocessor::CastRays16(SimpleRayContainer &rays,
|
---|
| 87 | VssRayContainer &vssRays,
|
---|
| 88 | const AxisAlignedBox3 &sbox)
|
---|
[1251] | 89 | { |
---|
| 90 | #ifdef GTP_INTERNAL |
---|
| 91 | /* |
---|
| 92 | mlrtaStoreRayAS16(const float* pforg, const float* pfdir, int j); |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | // input parameters |
---|
| 96 | const RTVec3f& org = *reinterpret_cast<const RTVec3f*> (pforg); |
---|
| 97 | const RTVec3f& rd = *reinterpret_cast<const RTVec3f*> (pfdir); |
---|
| 98 | |
---|
| 99 | int ri = j/4; |
---|
| 100 | int ci = j%4; |
---|
| 101 | *((float*)&ibp4.ray4X[ri].m_origin.t[0] + ci) = org[0]; |
---|
| 102 | *((float*)&ibp4.ray4X[ri].m_origin.t[1] + ci) = org[1]; |
---|
| 103 | *((float*)&ibp4.ray4X[ri].m_origin.t[2] + ci) = org[2]; |
---|
| 104 | |
---|
| 105 | *((float*)&ibp4.ray4X[ri].m_direction.t[0] + ci) = rd[0]; |
---|
| 106 | *((float*)&ibp4.ray4X[ri].m_direction.t[1] + ci) = rd[1]; |
---|
| 107 | *((float*)&ibp4.ray4X[ri].m_direction.t[2] + ci) = rd[2]; |
---|
| 108 | |
---|
| 109 | mlrtaTraverseGroupAS16(const float* bbmin, const float* bbmax, int* hit_triangles, float* dist);*/ |
---|
| 110 | #endif |
---|
| 111 | } |
---|
[1221] | 112 |
|
---|
| 113 |
|
---|
[376] | 114 | Vector3
|
---|
[382] | 115 | VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
[372] | 116 | {
|
---|
[434] | 117 | AxisAlignedBox3 box;
|
---|
[468] | 118 |
|
---|
[434] | 119 | if (viewSpaceBox)
|
---|
| 120 | box =*viewSpaceBox;
|
---|
[468] | 121 | else
|
---|
[434] | 122 | box = mKdTree->GetBox();
|
---|
[468] | 123 |
|
---|
[434] | 124 | // shrink the box in the y direction
|
---|
| 125 | return box.GetRandomPoint();
|
---|
[372] | 126 | }
|
---|
| 127 |
|
---|
[376] | 128 | Vector3
|
---|
[427] | 129 | VssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
[434] | 130 | AxisAlignedBox3 *viewSpaceBox
|
---|
| 131 | )
|
---|
[372] | 132 | {
|
---|
[434] | 133 | Vector3 point;
|
---|
[600] | 134 | if (!use2dSampling)
|
---|
| 135 | {
|
---|
| 136 | if (0)
|
---|
| 137 | {
|
---|
| 138 | Vector3 normal;
|
---|
| 139 | int i = Random((int)mObjects.size());
|
---|
| 140 | Intersectable *object = mObjects[i];
|
---|
| 141 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 142 | }
|
---|
| 143 | else
|
---|
| 144 | point = mKdTree->GetBox().GetRandomPoint();
|
---|
[534] | 145 | // point = viewpoint + UniformRandomVector();
|
---|
[600] | 146 | }
|
---|
| 147 | else
|
---|
| 148 | {
|
---|
| 149 | AxisAlignedBox3 box;
|
---|
[534] | 150 |
|
---|
[600] | 151 | if (viewSpaceBox)
|
---|
| 152 | box =*viewSpaceBox;
|
---|
| 153 | else
|
---|
| 154 | box = mKdTree->GetBox();
|
---|
[468] | 155 |
|
---|
[600] | 156 | point = box.GetRandomPoint();
|
---|
| 157 | point.y = viewpoint.y;
|
---|
[434] | 158 | }
|
---|
[468] | 159 |
|
---|
[434] | 160 | return point - viewpoint;
|
---|
[372] | 161 | }
|
---|
| 162 |
|
---|
[401] | 163 | int
|
---|
[427] | 164 | VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
|
---|
[434] | 165 | const int desiredSamples,
|
---|
| 166 | SimpleRayContainer &rays
|
---|
| 167 | )
|
---|
[401] | 168 | {
|
---|
[434] | 169 | int num;
|
---|
| 170 | if (0) {
|
---|
| 171 | float minRayContribution;
|
---|
| 172 | float maxRayContribution;
|
---|
| 173 | float avgRayContribution;
|
---|
[468] | 174 |
|
---|
[434] | 175 | vssTree->GetRayContributionStatistics(minRayContribution,
|
---|
| 176 | maxRayContribution,
|
---|
| 177 | avgRayContribution);
|
---|
[468] | 178 |
|
---|
[434] | 179 | cout<<
|
---|
| 180 | "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
|
---|
| 181 | "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
|
---|
| 182 | "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
|
---|
[468] | 183 |
|
---|
[434] | 184 | float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
|
---|
| 185 | num = vssTree->GenerateRays(p, rays);
|
---|
| 186 | } else {
|
---|
[534] | 187 | int leaves = vssTree->stat.Leaves();
|
---|
[434] | 188 | num = vssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
| 189 | }
|
---|
[468] | 190 |
|
---|
[434] | 191 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
[468] | 192 |
|
---|
[434] | 193 | return num;
|
---|
[427] | 194 | }
|
---|
[376] | 195 |
|
---|
| 196 |
|
---|
[427] | 197 | bool
|
---|
| 198 | VssPreprocessor::ExportRays(const char *filename,
|
---|
[434] | 199 | const VssRayContainer &vssRays,
|
---|
| 200 | const int number
|
---|
| 201 | )
|
---|
[427] | 202 | {
|
---|
[434] | 203 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
[468] | 204 |
|
---|
[434] | 205 | Exporter *exporter = NULL;
|
---|
| 206 | exporter = Exporter::GetExporter(filename);
|
---|
[1074] | 207 | exporter->SetWireframe();
|
---|
| 208 | exporter->ExportKdTree(*mKdTree);
|
---|
[434] | 209 | exporter->SetFilled();
|
---|
| 210 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 211 | exporter->SetWireframe();
|
---|
[427] | 212 |
|
---|
[434] | 213 | if (mViewSpaceBox) {
|
---|
[438] | 214 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
[434] | 215 | exporter->ExportBox(*mViewSpaceBox);
|
---|
| 216 | exporter->ResetForcedMaterial();
|
---|
| 217 | }
|
---|
[468] | 218 |
|
---|
[534] | 219 | VssRayContainer rays;
|
---|
| 220 | vssRays.SelectRays(number, rays);
|
---|
| 221 |
|
---|
[685] | 222 | //exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
[468] | 223 |
|
---|
[434] | 224 | delete exporter;
|
---|
[401] | 225 |
|
---|
[434] | 226 | cout<<"done."<<endl<<flush;
|
---|
[427] | 227 |
|
---|
[434] | 228 | return true;
|
---|
[401] | 229 | }
|
---|
| 230 |
|
---|
| 231 |
|
---|
[372] | 232 | bool
|
---|
[434] | 233 | VssPreprocessor::ExportVssTree(char *filename,
|
---|
[438] | 234 | VssTree *tree,
|
---|
| 235 | const Vector3 &dir
|
---|
| 236 | )
|
---|
[434] | 237 | {
|
---|
| 238 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 239 | exporter->SetFilled();
|
---|
| 240 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
[438] | 241 | // exporter->SetWireframe();
|
---|
| 242 | bool result = exporter->ExportVssTree2( *tree, dir );
|
---|
[434] | 243 | delete exporter;
|
---|
| 244 | return result;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | bool
|
---|
[427] | 248 | VssPreprocessor::ExportVssTreeLeaf(char *filename,
|
---|
[434] | 249 | VssTree *tree,
|
---|
| 250 | VssTreeLeaf *leaf)
|
---|
[427] | 251 | {
|
---|
[434] | 252 | Exporter *exporter = NULL;
|
---|
| 253 | exporter = Exporter::GetExporter(filename);
|
---|
| 254 | exporter->SetWireframe();
|
---|
| 255 | exporter->ExportKdTree(*mKdTree);
|
---|
[468] | 256 |
|
---|
[434] | 257 | if (mViewSpaceBox) {
|
---|
| 258 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
| 259 | exporter->ExportBox(*mViewSpaceBox);
|
---|
[427] | 260 | exporter->ResetForcedMaterial();
|
---|
[434] | 261 | }
|
---|
[468] | 262 |
|
---|
[434] | 263 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
| 264 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
| 265 | exporter->ResetForcedMaterial();
|
---|
[468] | 266 |
|
---|
[434] | 267 | VssRayContainer rays[4];
|
---|
| 268 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
| 269 | int k = leaf->rays[i].GetRayClass();
|
---|
| 270 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
| 271 | }
|
---|
[468] | 272 |
|
---|
[434] | 273 | // SOURCE RAY
|
---|
| 274 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
| 275 | // TERMINATION RAY
|
---|
| 276 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
| 277 | // PASSING_RAY
|
---|
| 278 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
| 279 | // CONTAINED_RAY
|
---|
| 280 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
[427] | 281 |
|
---|
[434] | 282 | delete exporter;
|
---|
| 283 | return true;
|
---|
[427] | 284 | }
|
---|
| 285 |
|
---|
| 286 | void
|
---|
| 287 | VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
|
---|
| 288 | {
|
---|
[434] | 289 | vector<VssTreeLeaf *> leaves;
|
---|
| 290 | tree->CollectLeaves(leaves);
|
---|
[427] | 291 |
|
---|
[434] | 292 | int num = 0;
|
---|
| 293 | int i;
|
---|
| 294 | float p = number / (float)leaves.size();
|
---|
| 295 | for (i=0; i < leaves.size(); i++) {
|
---|
| 296 | if (RandomValue(0,1) < p) {
|
---|
| 297 | char filename[64];
|
---|
| 298 | sprintf(filename, "vss-leaf-%04d.x3d", num);
|
---|
| 299 | ExportVssTreeLeaf(filename, tree, leaves[i]);
|
---|
| 300 | num++;
|
---|
[427] | 301 | }
|
---|
[434] | 302 | if (num >= number)
|
---|
| 303 | break;
|
---|
| 304 | }
|
---|
[427] | 305 | }
|
---|
| 306 |
|
---|
[685] | 307 |
|
---|
[535] | 308 | void VssPreprocessor::TestBeamCasting(VssTree *tree,
|
---|
| 309 | ViewCellsManager *vm,
|
---|
| 310 | const ObjectContainer &objects)
|
---|
[530] | 311 | {
|
---|
[540] | 312 | //debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
| 313 | // renderer->resize(640, 480);
|
---|
| 314 | //debuggerWidget->resize(640, 480);
|
---|
| 315 |
|
---|
[531] | 316 | vector<VssTreeLeaf *> leaves;
|
---|
| 317 | tree->CollectLeaves(leaves);
|
---|
[530] | 318 |
|
---|
[535] | 319 | Exporter *exporter = Exporter::GetExporter("shafts.x3d");
|
---|
| 320 |
|
---|
| 321 | exporter->SetWireframe();
|
---|
[540] | 322 | exporter->ExportGeometry(objects);
|
---|
[535] | 323 | exporter->SetFilled();
|
---|
| 324 | //Randomize();
|
---|
[1145] | 325 | // §§matt
|
---|
| 326 | // debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
[540] | 327 |
|
---|
| 328 | /*debuggerWidget->mBeam = beam;
|
---|
| 329 | debuggerWidget->mSourceObject = sourceObj;
|
---|
| 330 | debuggerWidget->mSamples = 10000;
|
---|
| 331 |
|
---|
| 332 | Debug << "showing window" << endl;
|
---|
[1145] | 333 | debuggerWidget->show();
|
---|
[540] | 334 |
|
---|
[1145] | 335 | renderer->makeCurrent();*/
|
---|
[540] | 336 |
|
---|
| 337 | for (int i = 0; i < 10; ++ i)
|
---|
[530] | 338 | {
|
---|
[540] | 339 | Beam beam;
|
---|
| 340 | Intersectable *sourceObj = mObjects[5];
|
---|
| 341 |
|
---|
[531] | 342 | const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
|
---|
| 343 | VssTreeLeaf *leaf = leaves[index];
|
---|
[530] | 344 |
|
---|
[535] | 345 | AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
|
---|
[531] | 346 | AxisAlignedBox3 box = tree->GetBBox(leaf);
|
---|
[532] | 347 |
|
---|
[535] | 348 | beam.Construct(box, dirBox);
|
---|
[532] | 349 |
|
---|
| 350 | // collect kd leaves and view cells
|
---|
| 351 | mKdTree->CastBeam(beam);
|
---|
| 352 | vm->CastBeam(beam);
|
---|
| 353 |
|
---|
[577] | 354 | Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
|
---|
| 355 | << (int)beam.mKdNodes.size() << " kd nodes" << endl;
|
---|
[532] | 356 |
|
---|
[530] | 357 | BeamSampleStatistics stats;
|
---|
[1145] | 358 | // §§matt
|
---|
| 359 | /* renderer->SampleBeamContributions(sourceObj,
|
---|
[589] | 360 | beam,
|
---|
| 361 | 200000,
|
---|
[531] | 362 | stats);
|
---|
[530] | 363 |
|
---|
[540] | 364 | char s[64]; sprintf(s, "shaft%04d.png", i);
|
---|
| 365 |
|
---|
| 366 | QImage image = renderer->toImage();
|
---|
| 367 | image.save(s, "PNG");
|
---|
[532] | 368 | Debug << "beam statistics: " << stats << endl << endl;
|
---|
[1145] | 369 | */
|
---|
[540] | 370 | if (1)
|
---|
| 371 | {
|
---|
| 372 | AxisAlignedBox3 sbox = mSceneGraph->GetBox();
|
---|
| 373 | Vector3 bmin = sbox.Min() - 150.0f;
|
---|
| 374 | Vector3 bmax = sbox.Max() + 150.0f;
|
---|
| 375 | AxisAlignedBox3 vbox(bmin, bmax);
|
---|
[535] | 376 |
|
---|
[540] | 377 | exporter->ExportBeam(beam, vbox);
|
---|
| 378 | }
|
---|
[535] | 379 |
|
---|
[540] | 380 | bool exportViewCells = false;
|
---|
[535] | 381 |
|
---|
| 382 | if (exportViewCells)
|
---|
| 383 | {
|
---|
| 384 | ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
|
---|
| 385 |
|
---|
| 386 | for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
|
---|
| 387 | {
|
---|
| 388 | BspNodeGeometry geom;
|
---|
| 389 | AxisAlignedBox3 vbox;
|
---|
| 390 | vbox.Initialize();
|
---|
[538] | 391 | vbox.Include((*it)->GetMesh());
|
---|
| 392 |
|
---|
| 393 | exporter->SetWireframe();
|
---|
[535] | 394 | exporter->ExportBox(vbox);
|
---|
[538] | 395 | exporter->SetFilled();
|
---|
| 396 | exporter->ExportViewCell(*it);
|
---|
[535] | 397 | }
|
---|
[538] | 398 |
|
---|
| 399 | /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
| 400 |
|
---|
| 401 | for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
|
---|
| 402 | {
|
---|
| 403 | exporter->ExportBox(mKdTree->GetBox((*it)));
|
---|
| 404 | }*/
|
---|
[535] | 405 | }
|
---|
[530] | 406 | }
|
---|
[540] | 407 | /*while (1)
|
---|
| 408 | { debuggerWidget->repaint();
|
---|
| 409 | };*/
|
---|
[535] | 410 | delete exporter;
|
---|
[530] | 411 | }
|
---|
| 412 |
|
---|
[540] | 413 |
|
---|
[434] | 414 | float
|
---|
| 415 | VssPreprocessor::GetAvgPvsSize(VssTree *tree,
|
---|
| 416 | const vector<AxisAlignedBox3> &viewcells
|
---|
| 417 | )
|
---|
| 418 | {
|
---|
| 419 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
| 420 |
|
---|
| 421 | int sum = 0;
|
---|
| 422 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
| 423 | sum += tree->GetPvsSize(*it);
|
---|
[468] | 424 |
|
---|
[434] | 425 | return sum/(float)viewcells.size();
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[427] | 428 | bool
|
---|
[372] | 429 | VssPreprocessor::ComputeVisibility()
|
---|
| 430 | {
|
---|
[579] | 431 | Debug << "type: vss" << endl;
|
---|
[468] | 432 |
|
---|
[372] | 433 | long startTime = GetTime();
|
---|
[468] | 434 |
|
---|
[372] | 435 | int totalSamples = 0;
|
---|
| 436 |
|
---|
| 437 |
|
---|
[1002] | 438 | AxisAlignedBox3 box(mKdTree->GetBox());
|
---|
[534] | 439 |
|
---|
[434] | 440 | if (!useViewspacePlane) {
|
---|
[446] | 441 | float size = 0.05f;
|
---|
[434] | 442 | float s = 0.5f - size;
|
---|
[1002] | 443 | float olds = Magnitude(box.Size());
|
---|
| 444 | box.Enlarge(box.Size()*Vector3(-s));
|
---|
[434] | 445 | Vector3 translation = Vector3(-olds*0.1f, 0, 0);
|
---|
[1002] | 446 | box.SetMin(box.Min() + translation);
|
---|
| 447 | box.SetMax(box.Max() + translation);
|
---|
[434] | 448 | } else {
|
---|
[468] | 449 |
|
---|
[434] | 450 | // sample city like heights
|
---|
[1002] | 451 | box.SetMin(1, box.Min(1) + box.Size(1)*0.2f);
|
---|
| 452 | box.SetMax(1, box.Min(1) + box.Size(1)*0.3f);
|
---|
[434] | 453 | }
|
---|
[427] | 454 |
|
---|
[434] | 455 | if (use2dSampling)
|
---|
[1002] | 456 | box.SetMax(1, box.Min(1));
|
---|
[468] | 457 |
|
---|
[1221] | 458 | cout << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
[579] | 459 |
|
---|
[1076] | 460 |
|
---|
[501] | 461 | if (mUseViewSpaceBox)
|
---|
[487] | 462 | {
|
---|
[1221] | 463 | //mViewSpaceBox = ConstructViewSpaceBox();
|
---|
| 464 |
|
---|
[674] | 465 | if (!mEnlargeViewSpace)
|
---|
[654] | 466 | {
|
---|
[1002] | 467 | mViewSpaceBox = new AxisAlignedBox3(box);
|
---|
[654] | 468 | }
|
---|
[598] | 469 | else
|
---|
| 470 | {
|
---|
[750] | 471 | mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
[677] | 472 |
|
---|
[1052] | 473 | if (0)
|
---|
[750] | 474 | {
|
---|
[1052] | 475 | // HACK: enlarge in y directon
|
---|
[750] | 476 | Vector3 size = mViewSpaceBox->Size();
|
---|
[1052] | 477 |
|
---|
| 478 | size[1] *= 1.25;
|
---|
| 479 | Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
|
---|
| 480 | //Vector3 enlarge(size[0] * 4.0f, 0.0f, 0.0f);
|
---|
[598] | 481 |
|
---|
[1052] | 482 | mViewSpaceBox->Enlarge(enlarge);
|
---|
[750] | 483 | mViewSpaceBox->SetMax(mViewSpaceBox->Max() + enlarge);
|
---|
| 484 | }
|
---|
| 485 | else
|
---|
| 486 | {
|
---|
[1052] | 487 | AxisAlignedBox3 tbox(*mViewSpaceBox);
|
---|
| 488 |
|
---|
| 489 | Vector3 size = mViewSpaceBox->Size();
|
---|
[1264] | 490 | tbox.SetMax(0, mViewSpaceBox->Max(0) + size[0] * 0.5f);
|
---|
[1052] | 491 | tbox.SetMin(0, mViewSpaceBox->Min(0) + size[0]);
|
---|
| 492 | *mViewSpaceBox = tbox;
|
---|
| 493 |
|
---|
[750] | 494 | // $$ JB temporary
|
---|
[1052] | 495 | /*AxisAlignedBox3 tempbox = *mViewSpaceBox;
|
---|
[750] | 496 |
|
---|
[1002] | 497 | float s = tempbox.Size(0);
|
---|
[750] | 498 |
|
---|
[1002] | 499 | tempbox.Scale(0.8f);
|
---|
| 500 | tempbox.SetMax(0, box.Max(0) + s*0.8f);
|
---|
| 501 | tempbox.SetMin(0, box.Min(0) + s*0.8f);
|
---|
[1052] | 502 | *mViewSpaceBox = tempbox;*/
|
---|
[750] | 503 | }
|
---|
[598] | 504 | }
|
---|
| 505 | //Debug << "view space box: " << *mViewSpaceBox << endl;
|
---|
[487] | 506 | }
|
---|
[434] | 507 | else
|
---|
[487] | 508 | {
|
---|
[579] | 509 | mViewSpaceBox = NULL;
|
---|
[487] | 510 | }
|
---|
[534] | 511 |
|
---|
[577] | 512 | AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
|
---|
| 513 |
|
---|
[590] | 514 | mSceneGraph->CollectObjects(&mObjects);
|
---|
| 515 |
|
---|
[1264] | 516 | //-- load view cells from file or reconstruct them
|
---|
[577] | 517 | if (!mLoadViewCells)
|
---|
[574] | 518 | {
|
---|
[577] | 519 | mViewCellsManager->SetViewSpaceBox(vbox);
|
---|
[574] | 520 | // construct view cells using it's own set of samples
|
---|
| 521 | mViewCellsManager->Construct(this);
|
---|
[518] | 522 |
|
---|
[574] | 523 | //-- several visualizations and statistics
|
---|
[587] | 524 | Debug << "view cells construction finished: " << endl;
|
---|
[574] | 525 | mViewCellsManager->PrintStatistics(Debug);
|
---|
| 526 | }
|
---|
[1264] | 527 | #if 1 // test successful view cells loading by exporting them again
|
---|
| 528 | else
|
---|
[651] | 529 | {
|
---|
[590] | 530 | VssRayContainer dummies;
|
---|
| 531 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
[1264] | 532 | mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
[590] | 533 | }
|
---|
[1264] | 534 | #endif
|
---|
[651] | 535 | VssTree *vssTree = NULL;
|
---|
[590] | 536 |
|
---|
[468] | 537 |
|
---|
[490] | 538 | long initialTime = GetTime();
|
---|
[468] | 539 |
|
---|
[490] | 540 | if (mLoadInitialSamples)
|
---|
| 541 | {
|
---|
| 542 | cout << "Loading samples from file ... ";
|
---|
| 543 | LoadSamples(mVssRays, mObjects);
|
---|
| 544 | cout << "finished\n" << endl;
|
---|
| 545 | totalSamples = (int)mVssRays.size();
|
---|
| 546 | }
|
---|
| 547 | else
|
---|
| 548 | {
|
---|
[534] | 549 |
|
---|
| 550 | while (totalSamples < mInitialSamples) {
|
---|
[490] | 551 | int passContributingSamples = 0;
|
---|
| 552 | int passSampleContributions = 0;
|
---|
| 553 | int passSamples = 0;
|
---|
[468] | 554 |
|
---|
[490] | 555 | int index = 0;
|
---|
[468] | 556 |
|
---|
[490] | 557 | int sampleContributions;
|
---|
[468] | 558 |
|
---|
[490] | 559 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
| 560 | for (int k=0; k < s; k++) {
|
---|
| 561 | // changed by matt
|
---|
| 562 | Vector3 viewpoint;
|
---|
[534] | 563 | // viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
[490] | 564 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
| 565 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
[534] | 566 |
|
---|
[1251] | 567 | sampleContributions = CastRay(viewpoint, direction, 1, mVssRays, vbox);
|
---|
[468] | 568 |
|
---|
[490] | 569 | if (sampleContributions) {
|
---|
| 570 | passContributingSamples ++;
|
---|
| 571 | passSampleContributions += sampleContributions;
|
---|
| 572 | }
|
---|
| 573 | passSamples++;
|
---|
| 574 | totalSamples++;
|
---|
| 575 | }
|
---|
[468] | 576 |
|
---|
[490] | 577 | mPass++;
|
---|
| 578 | int pvsSize = 0;
|
---|
| 579 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
| 580 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
[468] | 581 |
|
---|
[490] | 582 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
| 583 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
[651] | 584 | << "#SampleContributions=" << passSampleContributions << " ("
|
---|
[490] | 585 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
| 586 | << pvsSize/(float)mObjects.size() << endl
|
---|
| 587 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
[468] | 588 |
|
---|
[490] | 589 | mStats <<
|
---|
| 590 | "#Pass\n" <<mPass<<endl<<
|
---|
| 591 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
| 592 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
| 593 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
| 594 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
| 595 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
| 596 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
[534] | 600 |
|
---|
| 601 |
|
---|
| 602 |
|
---|
[490] | 603 | }
|
---|
[564] | 604 |
|
---|
| 605 |
|
---|
[490] | 606 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
|
---|
| 607 | Debug << (int)mVssRays.size() << " rays generated in "
|
---|
| 608 | << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
|
---|
[401] | 609 |
|
---|
[490] | 610 | if (mStoreInitialSamples)
|
---|
| 611 | {
|
---|
| 612 | cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
|
---|
[508] | 613 | ExportSamples(mVssRays);
|
---|
[490] | 614 | cout << "finished\n" << endl;
|
---|
[491] | 615 |
|
---|
| 616 | /*VssRayContainer dummyRays;
|
---|
| 617 | LoadSamples(dummyRays, mObjects);
|
---|
| 618 | Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
|
---|
| 619 |
|
---|
| 620 | for (int i = 0; i < (int)mVssRays.size(); ++ i)
|
---|
| 621 | {
|
---|
| 622 | Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
|
---|
| 623 | Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
|
---|
| 624 | }*/
|
---|
[434] | 625 | }
|
---|
[468] | 626 |
|
---|
[534] | 627 |
|
---|
[712] | 628 | //int numExportRays = 2000;
|
---|
| 629 | int numExportRays = 0;
|
---|
[372] | 630 |
|
---|
[434] | 631 | if (numExportRays) {
|
---|
| 632 | char filename[64];
|
---|
| 633 | sprintf(filename, "vss-rays-initial.x3d");
|
---|
| 634 | ExportRays(filename, mVssRays, numExportRays);
|
---|
| 635 | }
|
---|
[468] | 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 |
|
---|
[1074] | 643 | ExportRays("kdtree.x3d", mVssRays, 10);
|
---|
| 644 |
|
---|
[465] | 645 | if (0)
|
---|
| 646 | {
|
---|
| 647 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
| 648 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
| 649 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
| 650 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
| 651 | ExportVssTreeLeaves(vssTree, 10);
|
---|
| 652 | }
|
---|
[430] | 653 |
|
---|
[434] | 654 | // viewcells->UpdatePVS(newVssRays);
|
---|
| 655 | // get viewcells as kd tree boxes
|
---|
| 656 | vector<AxisAlignedBox3> kdViewcells;
|
---|
| 657 | if (0) {
|
---|
| 658 | vector<KdLeaf *> leaves;
|
---|
| 659 | mKdTree->CollectLeaves(leaves);
|
---|
| 660 | vector<KdLeaf *>::const_iterator it;
|
---|
| 661 | int targetLeaves = 50;
|
---|
| 662 | float prob = targetLeaves/(float)leaves.size();
|
---|
| 663 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
| 664 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
| 665 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
[468] | 666 |
|
---|
[434] | 667 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 668 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
[468] | 671 |
|
---|
[434] | 672 | int samples = 0;
|
---|
| 673 | int pass = 0;
|
---|
[448] | 674 |
|
---|
[535] | 675 |
|
---|
[448] | 676 | // cast view cell samples
|
---|
[557] | 677 | while (samples < mVssSamples)
|
---|
| 678 | {
|
---|
| 679 |
|
---|
[434] | 680 | int num = mVssSamplesPerPass;
|
---|
| 681 | SimpleRayContainer rays;
|
---|
| 682 | VssRayContainer vssRays;
|
---|
[468] | 683 |
|
---|
[434] | 684 | if (!mUseImportanceSampling) {
|
---|
| 685 | for (int j=0; j < num; j++) {
|
---|
[487] | 686 | // changed by matt
|
---|
| 687 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
| 688 | Vector3 viewpoint;
|
---|
| 689 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
[434] | 690 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
| 691 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
| 692 | }
|
---|
| 693 | } else {
|
---|
| 694 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
| 695 | }
|
---|
[468] | 696 |
|
---|
[434] | 697 | for (int i=0; i < rays.size(); i++)
|
---|
[1251] | 698 | CastRay(rays[i].mOrigin, rays[i].mDirection, 1, vssRays, vbox);
|
---|
[468] | 699 |
|
---|
[434] | 700 | vssTree->AddRays(vssRays);
|
---|
[468] | 701 |
|
---|
[434] | 702 | if (0) {
|
---|
| 703 | int subdivided = vssTree->UpdateSubdivision();
|
---|
| 704 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
| 705 | }
|
---|
[427] | 706 |
|
---|
[434] | 707 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
| 708 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
[430] | 709 |
|
---|
[517] | 710 | /// compute view cell contribution of rays
|
---|
[574] | 711 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
[517] | 712 |
|
---|
[434] | 713 | if (numExportRays) {
|
---|
| 714 | char filename[64];
|
---|
| 715 | if (mUseImportanceSampling)
|
---|
| 716 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
| 717 | else
|
---|
| 718 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
[468] | 719 |
|
---|
[434] | 720 | ExportRays(filename, vssRays, numExportRays);
|
---|
[401] | 721 | }
|
---|
[386] | 722 |
|
---|
[434] | 723 | samples+=num;
|
---|
| 724 | float pvs = vssTree->GetAvgPvsSize();
|
---|
| 725 | cout<<"*****************************\n";
|
---|
| 726 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
| 727 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
| 728 | cout<<"*****************************\n";
|
---|
[557] | 729 | // if (samples >= mVssSamples) break;
|
---|
| 730 | pass ++;
|
---|
[434] | 731 | }
|
---|
[448] | 732 |
|
---|
[574] | 733 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
| 734 | {
|
---|
| 735 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
[553] | 736 | }
|
---|
| 737 |
|
---|
[1221] | 738 | if (0) Debug << vssTree->stat << endl;
|
---|
[468] | 739 |
|
---|
[574] | 740 | if (0)
|
---|
| 741 | {
|
---|
| 742 | VssRayContainer viewCellRays;
|
---|
[1221] | 743 | // compute rays used for view cells construction
|
---|
[574] | 744 | const int numRays = mViewCellsManager->GetVisualizationSamples();
|
---|
| 745 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
[540] | 746 | }
|
---|
[535] | 747 |
|
---|
[452] | 748 | //-- render simulation after merge
|
---|
[574] | 749 | cout << "\nevaluating bsp view cells render time after sampling ... ";
|
---|
[605] | 750 | Debug << "\nStatistics after sampling: " << endl;
|
---|
| 751 |
|
---|
[468] | 752 | mRenderSimulator->RenderScene();
|
---|
| 753 | SimulationStatistics ss;
|
---|
| 754 | mRenderSimulator->GetStatistics(ss);
|
---|
[474] | 755 |
|
---|
[452] | 756 | cout << " finished" << endl;
|
---|
| 757 | cout << ss << endl;
|
---|
| 758 | Debug << ss << endl;
|
---|
[468] | 759 |
|
---|
[434] | 760 | delete vssTree;
|
---|
[475] | 761 |
|
---|
[434] | 762 | return true;
|
---|
[466] | 763 | }
|
---|
[697] | 764 |
|
---|
[860] | 765 | } |
---|