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