[176] | 1 | #include "SceneGraph.h" |
---|
| 2 | #include "KdTree.h" |
---|
[65] | 3 | #include "SamplingPreprocessor.h" |
---|
[176] | 4 | #include "X3dExporter.h" |
---|
[177] | 5 | #include "Environment.h" |
---|
[191] | 6 | #include "MutualVisibility.h" |
---|
[256] | 7 | #include "Polygon3.h" |
---|
[308] | 8 | #include "ViewCell.h" |
---|
[191] | 9 | |
---|
[331] | 10 | SamplingPreprocessor::SamplingPreprocessor(): mPass(0), mSampleRays(NULL) |
---|
[176] | 11 | { |
---|
| 12 | // this should increase coherence of the samples |
---|
[177] | 13 | environment->GetIntValue("Sampling.samplesPerPass", mSamplesPerPass); |
---|
| 14 | environment->GetIntValue("Sampling.totalSamples", mTotalSamples); |
---|
[329] | 15 | environment->GetIntValue("BspTree.Construction.samples", mBspConstructionSamples); |
---|
[367] | 16 | environment->GetIntValue("BspTree.PostProcessing.samples", mPostProcessSamples); |
---|
[177] | 17 | mKdPvsDepth = 100; |
---|
[245] | 18 | mStats.open("stats.log"); |
---|
[65] | 19 | |
---|
[176] | 20 | } |
---|
| 21 | |
---|
[329] | 22 | SamplingPreprocessor::~SamplingPreprocessor() |
---|
| 23 | { |
---|
[331] | 24 | CLEAR_CONTAINER(mSampleRays); |
---|
[329] | 25 | } |
---|
| 26 | |
---|
[176] | 27 | void |
---|
[335] | 28 | SamplingPreprocessor::SetupRay(Ray &ray, |
---|
[339] | 29 | const Vector3 &point, |
---|
| 30 | const Vector3 &direction, |
---|
| 31 | const int type) |
---|
[176] | 32 | { |
---|
| 33 | ray.intersections.clear(); |
---|
[362] | 34 | ray.kdLeaves.clear(); |
---|
[176] | 35 | ray.meshes.clear(); |
---|
[370] | 36 | ray.bspIntersections.clear(); |
---|
[308] | 37 | |
---|
[176] | 38 | // cout<<point<<" "<<direction<<endl; |
---|
[335] | 39 | ray.Init(point, direction, type); |
---|
[176] | 40 | } |
---|
| 41 | |
---|
| 42 | KdNode * |
---|
| 43 | SamplingPreprocessor::GetNodeForPvs(KdLeaf *leaf) |
---|
| 44 | { |
---|
| 45 | KdNode *node = leaf; |
---|
| 46 | while (node->mParent && node->mDepth > mKdPvsDepth) |
---|
| 47 | node = node->mParent; |
---|
| 48 | return node; |
---|
| 49 | } |
---|
| 50 | |
---|
[329] | 51 | bool |
---|
| 52 | SamplingPreprocessor::BuildBspTree() |
---|
| 53 | { |
---|
| 54 | // delete old tree |
---|
| 55 | DEL_PTR(mBspTree); |
---|
| 56 | mBspTree = new BspTree(&mUnbounded); |
---|
| 57 | ObjectContainer objects; |
---|
[349] | 58 | |
---|
[329] | 59 | switch (BspTree::sConstructionMethod) |
---|
| 60 | { |
---|
| 61 | case BspTree::FROM_INPUT_VIEW_CELLS: |
---|
[332] | 62 | mBspTree->SetGenerateViewCells(false); |
---|
[329] | 63 | mBspTree->Construct(mViewCells); |
---|
| 64 | break; |
---|
| 65 | case BspTree::FROM_SCENE_GEOMETRY: |
---|
| 66 | DeleteViewCells(); // we generate new view cells |
---|
[332] | 67 | mBspTree->SetGenerateViewCells(true); |
---|
[329] | 68 | mSceneGraph->CollectObjects(&objects); |
---|
[332] | 69 | mBspTree->Construct(objects); |
---|
[329] | 70 | break; |
---|
| 71 | case BspTree::FROM_RAYS: |
---|
| 72 | DeleteViewCells(); // we generate new view cells |
---|
[332] | 73 | mBspTree->SetGenerateViewCells(true); |
---|
| 74 | mBspTree->Construct(mSampleRays); |
---|
[329] | 75 | break; |
---|
| 76 | default: |
---|
| 77 | Debug << "Error: Method not available\n"; |
---|
| 78 | break; |
---|
| 79 | } |
---|
| 80 | |
---|
[331] | 81 | |
---|
[329] | 82 | return true; |
---|
| 83 | } |
---|
| 84 | |
---|
[176] | 85 | int |
---|
[369] | 86 | SamplingPreprocessor::AddNodeSamples(const Ray &ray, |
---|
| 87 | Intersectable *sObject, |
---|
| 88 | Intersectable *tObject |
---|
[256] | 89 | ) |
---|
[176] | 90 | { |
---|
| 91 | int contributingSamples = 0; |
---|
[191] | 92 | int j; |
---|
[369] | 93 | int objects = 0; |
---|
| 94 | if (sObject) |
---|
| 95 | objects++; |
---|
| 96 | if (tObject) |
---|
| 97 | objects++; |
---|
| 98 | |
---|
| 99 | if (objects) { |
---|
| 100 | for (j=0; j < ray.kdLeaves.size(); j++) { |
---|
| 101 | KdNode *node = GetNodeForPvs( ray.kdLeaves[j] ); |
---|
| 102 | if (sObject) |
---|
| 103 | contributingSamples += sObject->mKdPvs.AddSample(node); |
---|
| 104 | if (tObject) |
---|
| 105 | contributingSamples += tObject->mKdPvs.AddSample(node); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | for (j=1; j < ((int)ray.kdLeaves.size() - 1); j++) { |
---|
| 110 | ray.kdLeaves[j]->AddPassingRay2(ray, |
---|
| 111 | objects, |
---|
| 112 | ray.kdLeaves.size() |
---|
| 113 | ); |
---|
[176] | 114 | } |
---|
[191] | 115 | |
---|
[176] | 116 | return contributingSamples; |
---|
| 117 | } |
---|
| 118 | |
---|
[191] | 119 | |
---|
[308] | 120 | int SamplingPreprocessor::AddObjectSamples(Intersectable *obj, const Ray &ray) |
---|
| 121 | { |
---|
| 122 | int contributingSamples = 0; |
---|
| 123 | int j; |
---|
[311] | 124 | |
---|
[321] | 125 | // object can be seen from the view cell => add to view cell pvs |
---|
[370] | 126 | for (j=0; j < ray.bspIntersections.size(); ++ j) |
---|
| 127 | { |
---|
| 128 | BspLeaf *leaf = ray.bspIntersections[j].mLeaf; |
---|
| 129 | // if ray not in unbounded space |
---|
| 130 | if (leaf->GetViewCell() != &mUnbounded) |
---|
[362] | 131 | contributingSamples += |
---|
[370] | 132 | leaf->GetViewCell()->GetPvs().AddSample(obj); |
---|
[308] | 133 | } |
---|
[311] | 134 | |
---|
[321] | 135 | // rays passing through this viewcell |
---|
[311] | 136 | if (mPass > 1) |
---|
[370] | 137 | for (j=1; j < ((int)ray.bspIntersections.size() - 1); ++ j) |
---|
[311] | 138 | { |
---|
[370] | 139 | BspLeaf *leaf = ray.bspIntersections[j].mLeaf; |
---|
[366] | 140 | |
---|
| 141 | if (leaf->GetViewCell() != &mUnbounded) |
---|
| 142 | leaf->GetViewCell()-> |
---|
[362] | 143 | AddPassingRay(ray, contributingSamples ? 1 : 0); |
---|
[311] | 144 | } |
---|
| 145 | |
---|
[308] | 146 | return contributingSamples; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
[191] | 150 | void |
---|
| 151 | SamplingPreprocessor::HoleSamplingPass() |
---|
| 152 | { |
---|
| 153 | vector<KdLeaf *> leaves; |
---|
| 154 | mKdTree->CollectLeaves(leaves); |
---|
| 155 | |
---|
| 156 | // go through all the leaves and evaluate their passing contribution |
---|
| 157 | for (int i=0 ; i < leaves.size(); i++) { |
---|
| 158 | KdLeaf *leaf = leaves[i]; |
---|
| 159 | cout<<leaf->mPassingRays<<endl; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
[256] | 163 | |
---|
| 164 | int |
---|
[339] | 165 | SamplingPreprocessor::CastRay(Intersectable *object, |
---|
[369] | 166 | Ray &ray |
---|
[354] | 167 | ) |
---|
[256] | 168 | { |
---|
[308] | 169 | int sampleContributions = 0; |
---|
| 170 | |
---|
[340] | 171 | long t1 = GetRealTime(); |
---|
[321] | 172 | // cast ray to KD tree to find intersection with other objects |
---|
| 173 | mKdTree->CastRay(ray); |
---|
[340] | 174 | long t2 = GetRealTime(); |
---|
[339] | 175 | |
---|
[369] | 176 | if (0 && object && object->GetId() > 2197) { |
---|
[340] | 177 | object->Describe(cout)<<endl; |
---|
| 178 | cout<<ray<<endl; |
---|
| 179 | } |
---|
[339] | 180 | |
---|
[366] | 181 | if (ViewCell::sHierarchy == ViewCell::BSP) |
---|
[349] | 182 | { |
---|
| 183 | // cast ray to BSP tree to get intersection with view cells |
---|
| 184 | if (mBspTree) |
---|
[329] | 185 | { |
---|
[349] | 186 | mBspTree->CastRay(ray); |
---|
[354] | 187 | |
---|
[369] | 188 | if (object) |
---|
[354] | 189 | sampleContributions += AddObjectSamples(object, ray); |
---|
| 190 | |
---|
[349] | 191 | if (!ray.intersections.empty()) // second intersection found |
---|
[354] | 192 | { |
---|
| 193 | sampleContributions += |
---|
| 194 | AddObjectSamples(ray.intersections[0].mObject, ray); |
---|
| 195 | } |
---|
[310] | 196 | } |
---|
[349] | 197 | } |
---|
[310] | 198 | else |
---|
[339] | 199 | { |
---|
[362] | 200 | if (ray.kdLeaves.size()) { |
---|
[369] | 201 | Intersectable *terminator = |
---|
| 202 | ray.intersections.size() ? ray.intersections[0].mObject: NULL; |
---|
| 203 | |
---|
| 204 | sampleContributions += AddNodeSamples(ray, |
---|
| 205 | object, |
---|
| 206 | terminator); |
---|
| 207 | } |
---|
| 208 | } |
---|
[308] | 209 | |
---|
[256] | 210 | return sampleContributions; |
---|
| 211 | } |
---|
| 212 | |
---|
[245] | 213 | // void |
---|
| 214 | // SamplingPreprocessor::AvsGenerateRandomRay(Ray &ray) |
---|
| 215 | // { |
---|
| 216 | // int objId = RandomValue(0, mObjects.size()); |
---|
| 217 | // Intersectable *object = objects[objId]; |
---|
| 218 | // object->GetRandomSurfacePoint(point, normal); |
---|
| 219 | // direction = UniformRandomVector(normal); |
---|
| 220 | // SetupRay(ray, point, direction); |
---|
| 221 | // } |
---|
| 222 | |
---|
| 223 | // void |
---|
| 224 | // SamplingPreprocessor::AvsHandleRay(Ray &ray) |
---|
| 225 | // { |
---|
| 226 | // int sampleContributions = 0; |
---|
| 227 | |
---|
| 228 | // mKdTree->CastRay(ray); |
---|
| 229 | |
---|
| 230 | // if (ray.leaves.size()) { |
---|
| 231 | // sampleContributions += AddNodeSamples(object, ray, pass); |
---|
| 232 | |
---|
| 233 | // if (ray.intersections.size()) { |
---|
| 234 | // sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass); |
---|
| 235 | // } |
---|
| 236 | // } |
---|
| 237 | // } |
---|
| 238 | |
---|
| 239 | // void |
---|
| 240 | // SamplingPreprocessor::AvsBorderSampling(Ray &ray) |
---|
| 241 | // { |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | // } |
---|
| 245 | |
---|
| 246 | // void |
---|
| 247 | // SamplingPreprocessor::AvsPass() |
---|
| 248 | // { |
---|
| 249 | // Ray ray; |
---|
| 250 | // while (1) { |
---|
| 251 | // AvsGenerateRay(ray); |
---|
| 252 | // HandleRay(ray); |
---|
| 253 | // while ( !mRayQueue.empty() ) { |
---|
| 254 | // Ray ray = mRayQueue.pop(); |
---|
| 255 | // mRayQueue.pop(); |
---|
| 256 | // AdaptiveBorderSampling(ray); |
---|
| 257 | // } |
---|
| 258 | // } |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | // } |
---|
| 263 | |
---|
| 264 | |
---|
[333] | 265 | int |
---|
[256] | 266 | SamplingPreprocessor::CastEdgeSamples( |
---|
| 267 | Intersectable *object, |
---|
| 268 | const Vector3 &point, |
---|
[333] | 269 | MeshInstance *mi, |
---|
[256] | 270 | const int samples |
---|
| 271 | ) |
---|
| 272 | { |
---|
| 273 | Ray ray; |
---|
[333] | 274 | int maxTries = samples*10; |
---|
[256] | 275 | int i; |
---|
[333] | 276 | int rays = 0; |
---|
| 277 | int edgeSamplesContributions = 0; |
---|
| 278 | for (i=0; i < maxTries && rays < samples; i++) { |
---|
[256] | 279 | // pickup a random face of each mesh |
---|
[333] | 280 | Mesh *mesh = mi->GetMesh(); |
---|
| 281 | int face = RandomValue(0, mesh->mFaces.size()-1); |
---|
[256] | 282 | |
---|
[333] | 283 | Polygon3 poly(mesh->mFaces[face], mesh); |
---|
[256] | 284 | poly.Scale(1.001); |
---|
| 285 | // now extend a random edge of the face |
---|
| 286 | int edge = RandomValue(0, poly.mVertices.size()-1); |
---|
| 287 | float t = RandomValue(0.0f,1.0f); |
---|
| 288 | Vector3 target = t*poly.mVertices[edge] + (1.0f-t)*poly.mVertices[(edge + 1)% |
---|
| 289 | poly.mVertices.size()]; |
---|
[335] | 290 | SetupRay(ray, point, target - point, Ray::LOCAL_RAY); |
---|
[333] | 291 | if (!mesh->CastRay(ray, mi)) { |
---|
| 292 | // the rays which intersect the mesh have been discarded since they are not tangent |
---|
| 293 | // to the mesh |
---|
| 294 | rays++; |
---|
[369] | 295 | edgeSamplesContributions += CastRay(object, ray); |
---|
[333] | 296 | } |
---|
[256] | 297 | } |
---|
[333] | 298 | return edgeSamplesContributions; |
---|
[256] | 299 | } |
---|
[245] | 300 | |
---|
[354] | 301 | KdNode * |
---|
| 302 | SamplingPreprocessor::GetNodeToSample(Intersectable *object) |
---|
| 303 | { |
---|
| 304 | int pvsSize = object->mKdPvs.GetSize(); |
---|
| 305 | KdNode *nodeToSample = NULL; |
---|
| 306 | |
---|
| 307 | bool samplePvsBoundary = false; |
---|
| 308 | if (pvsSize && samplePvsBoundary) { |
---|
| 309 | // this samples the nodes from the boundary of the current PVS |
---|
| 310 | // mail all nodes from the pvs |
---|
| 311 | Intersectable::NewMail(); |
---|
| 312 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); |
---|
| 313 | |
---|
| 314 | for (; i != object->mKdPvs.mEntries.end(); i++) { |
---|
| 315 | KdNode *node = (*i).first; |
---|
| 316 | node->Mail(); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | int maxTries = 2*pvsSize; |
---|
| 320 | Debug << "Finding random neighbour" << endl; |
---|
| 321 | for (int tries = 0; tries < 10; tries++) { |
---|
| 322 | int index = RandomValue(0, pvsSize - 1); |
---|
| 323 | KdPvsData data; |
---|
| 324 | KdNode *node; |
---|
| 325 | object->mKdPvs.GetData(index, node, data); |
---|
| 326 | nodeToSample = mKdTree->FindRandomNeighbor(node, true); |
---|
| 327 | if (nodeToSample) |
---|
| 328 | break; |
---|
| 329 | } |
---|
| 330 | } else { |
---|
| 331 | // just pickup a random node |
---|
| 332 | // nodeToSample = mKdTree->GetRandomLeaf(Plane3(normal, point)); |
---|
| 333 | nodeToSample = mKdTree->GetRandomLeaf(); |
---|
| 334 | } |
---|
| 335 | return nodeToSample; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | void |
---|
| 339 | SamplingPreprocessor::VerifyVisibility(Intersectable *object) |
---|
| 340 | { |
---|
| 341 | // mail all nodes from the pvs |
---|
| 342 | Intersectable::NewMail(); |
---|
| 343 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); |
---|
| 344 | for (; i != object->mKdPvs.mEntries.end(); i++) { |
---|
| 345 | KdNode *node = (*i).first; |
---|
| 346 | node->Mail(); |
---|
| 347 | } |
---|
| 348 | Debug << "Get all neighbours from PVS" << endl; |
---|
| 349 | vector<KdNode *> invisibleNeighbors; |
---|
| 350 | // get all neighbors of all PVS nodes |
---|
| 351 | i = object->mKdPvs.mEntries.begin(); |
---|
| 352 | for (; i != object->mKdPvs.mEntries.end(); i++) { |
---|
| 353 | KdNode *node = (*i).first; |
---|
| 354 | mKdTree->FindNeighbors(node, invisibleNeighbors, true); |
---|
| 355 | AxisAlignedBox3 box = object->GetBox(); |
---|
| 356 | for (int j=0; j < invisibleNeighbors.size(); j++) { |
---|
| 357 | int visibility = ComputeBoxVisibility(mSceneGraph, |
---|
[359] | 358 | mKdTree, |
---|
| 359 | box, |
---|
| 360 | mKdTree->GetBox(invisibleNeighbors[j]), |
---|
| 361 | 1e-6f); |
---|
[354] | 362 | // exit(0); |
---|
| 363 | } |
---|
| 364 | // now rank all the neighbors according to probability that a new |
---|
| 365 | // sample creates some contribution |
---|
| 366 | } |
---|
| 367 | } |
---|
| 368 | |
---|
[162] | 369 | bool |
---|
| 370 | SamplingPreprocessor::ComputeVisibility() |
---|
| 371 | { |
---|
[245] | 372 | |
---|
[176] | 373 | // pickup an object |
---|
| 374 | ObjectContainer objects; |
---|
| 375 | |
---|
| 376 | mSceneGraph->CollectObjects(&objects); |
---|
| 377 | |
---|
| 378 | Vector3 point, normal, direction; |
---|
| 379 | Ray ray; |
---|
| 380 | |
---|
| 381 | long startTime = GetTime(); |
---|
| 382 | |
---|
| 383 | int i; |
---|
| 384 | int totalSamples = 0; |
---|
| 385 | |
---|
| 386 | int pvsOut = Min((int)objects.size(), 10); |
---|
[313] | 387 | |
---|
[331] | 388 | vector<Ray> rays[10]; |
---|
[321] | 389 | |
---|
[256] | 390 | while (totalSamples < mTotalSamples) { |
---|
| 391 | int passContributingSamples = 0; |
---|
| 392 | int passSampleContributions = 0; |
---|
| 393 | int passSamples = 0; |
---|
| 394 | int index = 0; |
---|
[366] | 395 | |
---|
[354] | 396 | int reverseSamples = 0; |
---|
[367] | 397 | |
---|
[349] | 398 | |
---|
[366] | 399 | //cout << "totalSamples: " << totalSamples << endl; |
---|
[331] | 400 | |
---|
[329] | 401 | for (i = 0; i < objects.size(); i++) { |
---|
[331] | 402 | |
---|
[256] | 403 | KdNode *nodeToSample = NULL; |
---|
| 404 | Intersectable *object = objects[i]; |
---|
[308] | 405 | |
---|
[312] | 406 | int pvsSize = 0; |
---|
[366] | 407 | if (ViewCell::sHierarchy == ViewCell::KD) |
---|
[312] | 408 | pvsSize = object->mKdPvs.GetSize(); |
---|
| 409 | |
---|
[256] | 410 | |
---|
| 411 | if (0 && pvsSize && mPass == 1000 ) { |
---|
[354] | 412 | VerifyVisibility(object); |
---|
[256] | 413 | } |
---|
| 414 | |
---|
[349] | 415 | int faceIndex = object->GetRandomSurfacePoint(point, normal); |
---|
[331] | 416 | |
---|
[256] | 417 | bool viewcellSample = true; |
---|
| 418 | int sampleContributions; |
---|
[340] | 419 | bool debug = false; //(object->GetId() >= 2199); |
---|
[256] | 420 | if (viewcellSample) { |
---|
[354] | 421 | //mKdTree->GetRandomLeaf(Plane3(normal, point)); |
---|
| 422 | |
---|
[365] | 423 | nodeToSample = GetNodeToSample(object); |
---|
| 424 | |
---|
[354] | 425 | for (int k=0; k < mSamplesPerPass; k++) { |
---|
| 426 | bool reverseSample = false; |
---|
[369] | 427 | |
---|
[355] | 428 | |
---|
[256] | 429 | if (nodeToSample) { |
---|
[355] | 430 | AxisAlignedBox3 box = mKdTree->GetBox(nodeToSample); |
---|
| 431 | Vector3 pointToSample = box.GetRandomPoint(); |
---|
| 432 | // pointToSample.y = 0.9*box.Min().y + 0.1*box.Max().y; |
---|
| 433 | if (object->GetRandomVisibleSurfacePoint( point, normal, pointToSample, 3 )) { |
---|
[354] | 434 | direction = pointToSample - point; |
---|
| 435 | } else { |
---|
| 436 | reverseSamples++; |
---|
| 437 | reverseSample = true; |
---|
| 438 | direction = point - pointToSample; |
---|
| 439 | point = pointToSample; |
---|
[256] | 440 | } |
---|
| 441 | } |
---|
| 442 | else { |
---|
| 443 | direction = UniformRandomVector(normal); |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | // construct a ray |
---|
[350] | 447 | SetupRay(ray, point, direction, Ray::LOCAL_RAY); |
---|
[354] | 448 | |
---|
[369] | 449 | sampleContributions = CastRay(reverseSample ? NULL : object, ray); |
---|
[363] | 450 | |
---|
[331] | 451 | //-- CORR matt: put block inside loop |
---|
| 452 | if (sampleContributions) { |
---|
[350] | 453 | passContributingSamples ++; |
---|
[331] | 454 | passSampleContributions += sampleContributions; |
---|
| 455 | } |
---|
| 456 | |
---|
[330] | 457 | if ( i < pvsOut ) |
---|
| 458 | rays[i].push_back(ray); |
---|
| 459 | |
---|
| 460 | if (!ray.intersections.empty()) { |
---|
| 461 | // check whether we can add this to the rays |
---|
| 462 | for (int j = 0; j < pvsOut; j++) { |
---|
| 463 | if (objects[j] == ray.intersections[0].mObject) { |
---|
| 464 | rays[j].push_back(ray); |
---|
| 465 | } |
---|
| 466 | } |
---|
| 467 | } |
---|
[331] | 468 | //------------------- |
---|
[366] | 469 | if (ViewCell::sHierarchy == ViewCell::BSP) |
---|
[313] | 470 | { |
---|
[367] | 471 | ProcessBspViewCells(ray, |
---|
[362] | 472 | object, |
---|
| 473 | faceIndex, |
---|
| 474 | passContributingSamples, |
---|
| 475 | passSampleContributions); |
---|
[313] | 476 | } |
---|
[256] | 477 | } |
---|
| 478 | } else { |
---|
| 479 | // edge samples |
---|
| 480 | // get random visible mesh |
---|
| 481 | // object->GetRandomVisibleMesh(Plane3(normal, point)); |
---|
| 482 | } |
---|
[331] | 483 | |
---|
| 484 | // CORR matt: must add all samples |
---|
[330] | 485 | passSamples += mSamplesPerPass; |
---|
[256] | 486 | } |
---|
[330] | 487 | |
---|
[331] | 488 | totalSamples += passSamples; |
---|
[256] | 489 | |
---|
| 490 | // if (pass>10) |
---|
| 491 | // HoleSamplingPass(); |
---|
[191] | 492 | |
---|
[256] | 493 | mPass++; |
---|
[308] | 494 | |
---|
[256] | 495 | int pvsSize = 0; |
---|
[309] | 496 | |
---|
[366] | 497 | if (ViewCell::sHierarchy == ViewCell::BSP) { |
---|
[309] | 498 | for (i=0; i < mViewCells.size(); i++) { |
---|
| 499 | ViewCell *vc = mViewCells[i]; |
---|
| 500 | pvsSize += vc->GetPvs().GetSize(); |
---|
| 501 | } |
---|
[313] | 502 | } else { |
---|
[309] | 503 | for (i=0; i < objects.size(); i++) { |
---|
| 504 | Intersectable *object = objects[i]; |
---|
| 505 | pvsSize += object->mKdPvs.GetSize(); |
---|
| 506 | } |
---|
[308] | 507 | } |
---|
[309] | 508 | |
---|
[331] | 509 | float avgRayContrib = (passContributingSamples > 0) ? |
---|
| 510 | passSampleContributions/(float)passContributingSamples : 0; |
---|
| 511 | |
---|
| 512 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; |
---|
[256] | 513 | cout << "#TotalSamples=" << totalSamples/1000 |
---|
| 514 | << "k #SampleContributions=" << passSampleContributions << " (" |
---|
| 515 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" |
---|
| 516 | << pvsSize/(float)objects.size() << endl |
---|
[354] | 517 | << "avg ray contrib=" << avgRayContrib << endl |
---|
| 518 | << "reverse samples [%]" << reverseSamples/(float)passSamples*100.0f << endl; |
---|
| 519 | |
---|
[256] | 520 | mStats << |
---|
| 521 | "#Pass\n" <<mPass<<endl<< |
---|
| 522 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< |
---|
| 523 | "#TotalSamples\n" << totalSamples<< endl<< |
---|
| 524 | "#SampleContributions\n" << passSampleContributions << endl << |
---|
| 525 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << |
---|
| 526 | "#AvgPVS\n"<< pvsSize/(float)objects.size() << endl << |
---|
[331] | 527 | "#AvgRayContrib\n" << avgRayContrib << endl; |
---|
[256] | 528 | } |
---|
[349] | 529 | |
---|
[366] | 530 | if (ViewCell::sHierarchy == ViewCell::KD) |
---|
[309] | 531 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; |
---|
| 532 | |
---|
[362] | 533 | // merge or subdivide view cells |
---|
| 534 | if (mBspTree) |
---|
| 535 | { |
---|
[367] | 536 | cout << "starting post processing using " << mSampleRays.size() << " samples ... "; |
---|
[312] | 537 | |
---|
[366] | 538 | Debug << "original pvs size: " << mBspTree->CountViewCellPvs() << endl; |
---|
[242] | 539 | |
---|
[366] | 540 | long startTime = GetTime(); |
---|
| 541 | int merged = PostprocessViewCells(mSampleRays); |
---|
| 542 | |
---|
| 543 | cout << "finished" << endl; |
---|
| 544 | cout << "merged " << merged << " view cells in " |
---|
| 545 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl; |
---|
[218] | 546 | |
---|
[366] | 547 | //-- recount pvs |
---|
| 548 | Debug << "merged pvs size: " << mBspTree->CountViewCellPvs() << endl; |
---|
[349] | 549 | |
---|
[362] | 550 | } |
---|
[360] | 551 | |
---|
[362] | 552 | // HoleSamplingPass(); |
---|
| 553 | if (0) { |
---|
| 554 | Exporter *exporter = Exporter::GetExporter("ray-density.x3d"); |
---|
| 555 | exporter->SetExportRayDensity(true); |
---|
| 556 | exporter->ExportKdTree(*mKdTree); |
---|
[360] | 557 | |
---|
[366] | 558 | if (mBspTree && (ViewCell::sHierarchy == ViewCell::BSP)) |
---|
[362] | 559 | exporter->ExportBspTree(*mBspTree); |
---|
[360] | 560 | |
---|
[362] | 561 | delete exporter; |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | bool exportRays = false; |
---|
| 565 | if (exportRays) { |
---|
| 566 | Exporter *exporter = NULL; |
---|
| 567 | exporter = Exporter::GetExporter("sample-rays.x3d"); |
---|
| 568 | exporter->SetWireframe(); |
---|
| 569 | exporter->ExportKdTree(*mKdTree); |
---|
| 570 | exporter->ExportBspTree(*mBspTree); |
---|
[361] | 571 | |
---|
[362] | 572 | for (i=0; i < pvsOut; i++) |
---|
| 573 | exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0)); |
---|
| 574 | exporter->SetFilled(); |
---|
| 575 | |
---|
| 576 | delete exporter; |
---|
| 577 | } |
---|
[360] | 578 | |
---|
[362] | 579 | //-- several visualizations and statistics |
---|
| 580 | if (1) { |
---|
[366] | 581 | if (mBspTree && (ViewCell::sHierarchy == ViewCell::BSP)) |
---|
[362] | 582 | { |
---|
| 583 | bool exportSplits = false; |
---|
| 584 | environment->GetBoolValue("BspTree.exportSplits", exportSplits); |
---|
[361] | 585 | |
---|
[362] | 586 | // export the bsp splits |
---|
| 587 | if (exportSplits)
|
---|
| 588 | ExportSplits(objects);
|
---|
[331] | 589 | |
---|
[362] | 590 | ExportBspPvs(objects); |
---|
| 591 | } |
---|
[322] | 592 | for (int k=0; k < pvsOut; k++) { |
---|
[369] | 593 | Intersectable *object = objects[k]; |
---|
| 594 | char s[64]; |
---|
| 595 | sprintf(s, "sample-pvs%04d.x3d", k); |
---|
| 596 | Exporter *exporter = Exporter::GetExporter(s); |
---|
| 597 | exporter->SetWireframe(); |
---|
| 598 | |
---|
| 599 | |
---|
| 600 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); |
---|
| 601 | Intersectable::NewMail(); |
---|
| 602 | |
---|
| 603 | // avoid adding the object to the list |
---|
| 604 | object->Mail(); |
---|
| 605 | ObjectContainer visibleObjects; |
---|
| 606 | |
---|
| 607 | for (; i != object->mKdPvs.mEntries.end(); i++) |
---|
| 608 | { |
---|
| 609 | KdNode *node = (*i).first; |
---|
| 610 | exporter->ExportBox(mKdTree->GetBox(node)); |
---|
| 611 | mKdTree->CollectObjects(node, visibleObjects); |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0)); |
---|
| 615 | exporter->SetFilled(); |
---|
| 616 | |
---|
| 617 | for (int j = 0; j < visibleObjects.size(); j++) |
---|
| 618 | exporter->ExportIntersectable(visibleObjects[j]); |
---|
| 619 | |
---|
| 620 | |
---|
| 621 | Material m; |
---|
| 622 | m.mDiffuseColor = RgbColor(1, 0, 0); |
---|
| 623 | exporter->SetForcedMaterial(m); |
---|
| 624 | exporter->ExportIntersectable(object); |
---|
| 625 | |
---|
| 626 | delete exporter; |
---|
| 627 | } |
---|
[176] | 628 | } |
---|
| 629 | |
---|
[162] | 630 | return true; |
---|
[333] | 631 | } |
---|
[349] | 632 | |
---|
[367] | 633 | bool SamplingPreprocessor::ProcessBspViewCells(Ray &ray, |
---|
[362] | 634 | Intersectable *object, |
---|
| 635 | int faceIndex, |
---|
| 636 | int &contributingSamples, |
---|
| 637 | int &sampleContributions) |
---|
| 638 | { |
---|
| 639 | // save rays for bsp tree construction |
---|
[367] | 640 | if ((BspTree::sConstructionMethod == BspTree::FROM_RAYS) && |
---|
| 641 | (mSampleRays.size() < mBspConstructionSamples)) |
---|
[362] | 642 | { |
---|
| 643 | // also add origin to sample in order to extract it as input polygons |
---|
| 644 | MeshInstance *mi = dynamic_cast<MeshInstance *>(object); |
---|
| 645 | ray.sourceObject = Ray::Intersection(0.0, mi, faceIndex); |
---|
| 646 | |
---|
| 647 | mSampleRays.push_back(new Ray(ray)); |
---|
| 648 | |
---|
| 649 | return false; |
---|
| 650 | } |
---|
| 651 | |
---|
[367] | 652 | if (!mBspTree) // construct BSP tree using the samples |
---|
[362] | 653 | { |
---|
[367] | 654 | cout << "building bsp tree from " << mSampleRays.size() << " samples " << endl; |
---|
[362] | 655 | BuildBspTree(); |
---|
| 656 | |
---|
[367] | 657 | // add contributions of saved samples to PVS |
---|
[362] | 658 | contributingSamples += mBspTree->GetStat().contributingSamples; |
---|
| 659 | sampleContributions += mBspTree->GetStat().sampleContributions; |
---|
| 660 | |
---|
| 661 | BspTreeStatistics(Debug); |
---|
[367] | 662 | |
---|
[362] | 663 | if (0) Export("vc_bsptree.x3d", false, false, true); |
---|
[367] | 664 | |
---|
| 665 | // throw away samples because BSP leaves not stored in order |
---|
| 666 | // Need ordered rays for post processing => collect new rays |
---|
| 667 | CLEAR_CONTAINER(mSampleRays); |
---|
[362] | 668 | } |
---|
[367] | 669 | |
---|
| 670 | if ((int)mSampleRays.size() < mPostProcessSamples) |
---|
| 671 | { |
---|
| 672 | mSampleRays.push_back(new Ray(ray)); |
---|
| 673 | } |
---|
[362] | 674 | |
---|
| 675 | return true; |
---|
| 676 | }
|
---|
[349] | 677 |
|
---|
[362] | 678 | // merge or subdivide view cells |
---|
| 679 | int SamplingPreprocessor::PostprocessViewCells(const RayContainer &rays) |
---|
| 680 | { |
---|
| 681 | int merged = 0; |
---|
| 682 | RayContainer::const_iterator rit, rit_end = rays.end(); |
---|
[370] | 683 | vector<Ray::BspIntersection>::const_iterator iit; |
---|
[366] | 684 | |
---|
[362] | 685 | for (rit = rays.begin(); rit != rays.end(); ++ rit) |
---|
| 686 | { |
---|
| 687 | // traverse leaves stored in the rays and compare and merge consecutive |
---|
| 688 | // leaves (i.e., the neighbors in the tree) |
---|
[370] | 689 | if ((*rit)->bspIntersections.empty()) |
---|
[366] | 690 | continue; |
---|
| 691 | |
---|
[370] | 692 | iit = (*rit)->bspIntersections.begin(); |
---|
[362] | 693 | |
---|
[370] | 694 | BspLeaf *previousLeaf = (*iit).mLeaf; |
---|
| 695 | ++ iit; |
---|
[362] | 696 | |
---|
[370] | 697 | for (; iit != (*rit)->bspIntersections.end(); ++ iit) |
---|
[362] | 698 | { |
---|
[370] | 699 | BspLeaf *leaf = (*iit).mLeaf; |
---|
[367] | 700 | |
---|
[362] | 701 | if (mBspTree->ShouldMerge(leaf, previousLeaf)) |
---|
| 702 | { |
---|
| 703 | mBspTree->MergeViewCells(leaf, previousLeaf); |
---|
| 704 | ++ merged; |
---|
| 705 | } |
---|
| 706 | previousLeaf = leaf; |
---|
| 707 | } |
---|
| 708 | } |
---|
[366] | 709 | |
---|
[362] | 710 | return merged; |
---|
| 711 | }
|
---|
| 712 |
|
---|
[349] | 713 | void SamplingPreprocessor::ExportSplits(const ObjectContainer &objects)
|
---|
| 714 | {
|
---|
| 715 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
| 716 | |
---|
| 717 | if (exporter) |
---|
| 718 | { |
---|
| 719 | cout << "exporting splits ... "; |
---|
| 720 | |
---|
| 721 | Material m;
|
---|
| 722 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
| 723 | exporter->SetForcedMaterial(m);
|
---|
| 724 | exporter->SetWireframe();
|
---|
| 725 | exporter->ExportBspSplits(*mBspTree);
|
---|
| 726 | |
---|
| 727 | // take forced material, else big scenes cannot be viewed
|
---|
| 728 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
| 729 | exporter->SetForcedMaterial(m);
|
---|
| 730 | exporter->SetFilled();
|
---|
| 731 |
|
---|
| 732 | exporter->ResetForcedMaterial();
|
---|
| 733 |
|
---|
| 734 | // export rays
|
---|
| 735 | if (0)
|
---|
| 736 | {
|
---|
| 737 | RayContainer outRays;
|
---|
| 738 |
|
---|
| 739 | for (int i = 0; i < mSampleRays.size(); ++ i)
|
---|
| 740 | {
|
---|
| 741 | // only rays piercing geometry
|
---|
| 742 | if (!mSampleRays[i]->intersections.empty())
|
---|
| 743 | outRays.push_back(mSampleRays[i]);
|
---|
| 744 | }
|
---|
| 745 | if (BspTree::sConstructionMethod == BspTree::FROM_RAYS)
|
---|
| 746 | {
|
---|
| 747 | // export rays
|
---|
| 748 | exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
|
---|
| 749 | }
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | // export scene geometry
|
---|
[352] | 753 | if (0)
|
---|
[349] | 754 | {
|
---|
| 755 | Material m;//= RandomMaterial(); |
---|
| 756 | m.mDiffuseColor = RgbColor(0, 1, 0); |
---|
| 757 | exporter->SetForcedMaterial(m); |
---|
| 758 | exporter->SetWireframe(); |
---|
| 759 | |
---|
| 760 | for (int j = 0; j < objects.size(); ++ j) |
---|
| 761 | exporter->ExportIntersectable(objects[j]); |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | delete exporter; |
---|
| 765 | |
---|
| 766 | cout << "finished" << endl; |
---|
| 767 | } |
---|
[354] | 768 | } |
---|
[362] | 769 | |
---|
| 770 | void SamplingPreprocessor::ExportBspPvs(const ObjectContainer &objects) |
---|
| 771 | { |
---|
| 772 | //-- some random view cells and rays for output |
---|
| 773 | const int leafOut = 10; |
---|
| 774 | |
---|
| 775 | vector<Ray> vcRays[leafOut]; |
---|
| 776 | vector<BspLeaf *> bspLeaves; |
---|
| 777 | |
---|
| 778 | for (int i = 0; i < leafOut; ++ i) |
---|
| 779 | bspLeaves.push_back(mBspTree->GetRandomLeaf()); |
---|
| 780 | |
---|
| 781 | const int raysOut = min((int)mSampleRays.size(), 20000); |
---|
| 782 | |
---|
| 783 | // check whether we can add the current ray to the output rays |
---|
| 784 | for (int k = 0; k < raysOut; ++ k) |
---|
| 785 | { |
---|
| 786 | Ray *ray = mSampleRays[k]; |
---|
| 787 | |
---|
[370] | 788 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j) |
---|
[362] | 789 | { |
---|
[370] | 790 | BspLeaf *leaf = ray->bspIntersections[j].mLeaf; |
---|
| 791 | |
---|
[362] | 792 | for (int i = 0; i < (int)bspLeaves.size(); ++ i) |
---|
| 793 | { |
---|
[370] | 794 | if (bspLeaves[i]->GetViewCell() == leaf->GetViewCell()) |
---|
[362] | 795 | { |
---|
| 796 | vcRays[i].push_back(*ray); |
---|
| 797 | } |
---|
| 798 | } |
---|
| 799 | } |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | ViewCell::NewMail(); |
---|
| 803 | |
---|
| 804 | for (int i = 0; i < bspLeaves.size(); ++ i) |
---|
| 805 | { |
---|
| 806 | Intersectable::NewMail(); |
---|
| 807 | |
---|
| 808 | ViewCell *vc = bspLeaves[i]->GetViewCell(); |
---|
| 809 | |
---|
| 810 | //bspLeaves[j]->Mail(); |
---|
| 811 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i); |
---|
| 812 | |
---|
| 813 | Exporter *exporter = Exporter::GetExporter(s); |
---|
| 814 | exporter->SetFilled(); |
---|
| 815 | |
---|
| 816 | ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin(); |
---|
| 817 | |
---|
[366] | 818 | exporter->SetWireframe(); |
---|
| 819 | |
---|
[362] | 820 | Material m;//= RandomMaterial(); |
---|
| 821 | m.mDiffuseColor = RgbColor(0, 1, 0); |
---|
| 822 | exporter->SetForcedMaterial(m); |
---|
| 823 | |
---|
| 824 | if (vc->GetMesh()) |
---|
| 825 | exporter->ExportViewCell(vc); |
---|
| 826 | else |
---|
| 827 | { |
---|
| 828 | PolygonContainer cell; |
---|
[366] | 829 | // export view cell |
---|
| 830 | mBspTree->ConstructGeometry(bspLeaves[i]->GetViewCell(), cell); |
---|
[362] | 831 | exporter->ExportPolygons(cell); |
---|
| 832 | CLEAR_CONTAINER(cell); |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() |
---|
| 836 | << ", piercing rays=" << (int)vcRays[i].size() << endl; |
---|
| 837 | |
---|
| 838 | // export view cells |
---|
| 839 | if (0) |
---|
| 840 | { |
---|
| 841 | m.mDiffuseColor = RgbColor(1, 0, 1); |
---|
| 842 | exporter->SetForcedMaterial(m); |
---|
| 843 | exporter->ExportViewCells(mViewCells); |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | // export rays piercing this view cell |
---|
| 847 | exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0)); |
---|
| 848 | |
---|
| 849 | m.mDiffuseColor = RgbColor(1, 0, 0); |
---|
| 850 | exporter->SetForcedMaterial(m); |
---|
| 851 | |
---|
| 852 | // output PVS of view cell |
---|
| 853 | for (; it != vc->GetPvs().mEntries.end(); ++ it) |
---|
| 854 | { |
---|
| 855 | Intersectable *intersect = (*it).first; |
---|
| 856 | if (!intersect->Mailed()) |
---|
| 857 | { |
---|
| 858 | exporter->ExportIntersectable(intersect); |
---|
| 859 | intersect->Mail(); |
---|
| 860 | } |
---|
| 861 | } |
---|
| 862 | |
---|
| 863 | // output rest of the objects |
---|
| 864 | if (0) |
---|
| 865 | { |
---|
| 866 | Material m;//= RandomMaterial(); |
---|
| 867 | m.mDiffuseColor = RgbColor(0, 0, 1); |
---|
| 868 | exporter->SetForcedMaterial(m); |
---|
| 869 | |
---|
| 870 | for (int j = 0; j < objects.size(); ++ j) |
---|
| 871 | if (!objects[j]->Mailed()) |
---|
| 872 | { |
---|
| 873 | //if (j == 2198)m.mDiffuseColor = RgbColor(1, 0, 1); |
---|
| 874 | //else m.mDiffuseColor = RgbColor(1, 1, 0); |
---|
| 875 | exporter->SetForcedMaterial(m); |
---|
| 876 | exporter->ExportIntersectable(objects[j]); |
---|
| 877 | objects[j]->Mail(); |
---|
| 878 | } |
---|
| 879 | } |
---|
| 880 | DEL_PTR(exporter); |
---|
| 881 | } |
---|
[365] | 882 | } |
---|