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