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