- Timestamp:
- 08/22/05 21:07:44 (19 years ago)
- Location:
- trunk/VUT/GtpVisibilityPreprocessor/src
- Files:
-
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/Intersectable.h
r224 r245 20 20 static void NewMail() { mailID++; } 21 21 bool Mailed() const { return mailbox == mailID; } 22 22 int IncMail() { return ++mailbox - mailID; } 23 23 24 virtual AxisAlignedBox3 GetBox() = 0; 24 25 -
trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
r241 r245 596 596 entp = extp; 597 597 mint = maxt; 598 if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f) 599 break; 600 598 601 RayTraversalData &s = tStack.top(); 599 602 node = s.mNode; … … 709 712 710 713 return neighbors.size(); 714 } 715 716 // Find random neighbor which was not mailed 717 KdNode * 718 KdTree::GetRandomLeaf(const Plane3 &plane) 719 { 720 stack<KdNode *> nodeStack; 721 722 nodeStack.push(mRoot); 723 724 int mask = rand(); 725 726 while (!nodeStack.empty()) { 727 KdNode *node = nodeStack.top(); 728 nodeStack.pop(); 729 if (node->IsLeaf()) { 730 return node; 731 } else { 732 KdInterior *interior = (KdInterior *)node; 733 KdNode *next; 734 if (GetBox(interior->mBack).Side(plane) < 0) 735 next = interior->mFront; 736 else 737 if (GetBox(interior->mFront).Side(plane) < 0) 738 next = interior->mBack; 739 else { 740 // random decision 741 if (mask&1) 742 next = interior->mBack; 743 else 744 next = interior->mFront; 745 mask = mask>>1; 746 } 747 nodeStack.push(next); 748 } 749 } 750 751 752 return NULL; 711 753 } 712 754 -
trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h
r191 r245 307 307 ); 308 308 309 310 KdNode * 311 KdTree::GetRandomLeaf(const Plane3 &halfspace); 312 309 313 int 310 314 FindNeighbors(KdNode *n, -
trunk/VUT/GtpVisibilityPreprocessor/src/Makefile
r209 r245 1 1 ############################################################################# 2 2 # Makefile for building: preprocessor 3 # Generated by qmake (1.07a) (Qt 3.3.2) on: Thu Aug 04 20:06:0620053 # Generated by qmake (1.07a) (Qt 3.3.2) on: Mon Aug 22 21:02:28 2005 4 4 # Project: preprocessor.pro 5 5 # Template: app … … 65 65 Triangle3.cpp \ 66 66 Rectangle3.cpp \ 67 Plane3.cpp 67 Plane3.cpp \ 68 Polygon3.cpp \ 69 ViewCell.cpp \ 70 ViewCellBsp.cpp 68 71 OBJECTS = Preprocessor.obj \ 69 72 SamplingPreprocessor.obj \ … … 91 94 Triangle3.obj \ 92 95 Rectangle3.obj \ 93 Plane3.obj 96 Plane3.obj \ 97 Polygon3.obj \ 98 ViewCell.obj \ 99 ViewCellBsp.obj 94 100 FORMS = 95 101 UICDECLS = … … 179 185 -$(DEL_FILE) Rectangle3.obj 180 186 -$(DEL_FILE) Plane3.obj 187 -$(DEL_FILE) Polygon3.obj 188 -$(DEL_FILE) ViewCell.obj 189 -$(DEL_FILE) ViewCellBsp.obj 181 190 182 191 … … 195 204 X3dParser.h \ 196 205 Preprocessor.h \ 206 ViewCell.h \ 207 Environment.h \ 197 208 Containers.h \ 198 209 AxisAlignedBox3.h \ … … 311 322 Mesh.h \ 312 323 KdTree.h \ 324 ViewCellBsp.h \ 325 ViewCell.h \ 326 Polygon3.h \ 313 327 Containers.h \ 314 328 AxisAlignedBox3.h \ … … 486 500 Mesh.h \ 487 501 Triangle3.h \ 502 SceneGraph.h \ 488 503 Containers.h \ 489 504 Pvs.h \ … … 511 526 Plane3.obj: Plane3.cpp \ 512 527 Plane3.h \ 513 Vector3.h \ 514 common.h \ 528 Matrix4x4.h \ 529 Vector3.h \ 530 common.h \ 531 532 533 Polygon3.obj: Polygon3.cpp \ 534 Polygon3.h \ 535 Mesh.h \ 536 Containers.h \ 537 Intersectable.h \ 538 Plane3.h \ 539 Matrix4x4.h \ 540 AxisAlignedBox3.h \ 541 Material.h \ 542 Pvs.h \ 543 Rectangle3.h \ 544 Vector3.h \ 545 common.h \ 546 547 548 ViewCell.obj: ViewCell.cpp \ 549 ViewCell.h \ 550 Mesh.h \ 551 MeshKdTree.h \ 552 Triangle3.h \ 553 Intersectable.h \ 554 Containers.h \ 555 AxisAlignedBox3.h \ 556 Pvs.h \ 557 Rectangle3.h \ 558 Matrix4x4.h \ 559 Vector3.h \ 560 Plane3.h \ 561 common.h \ 562 Material.h \ 563 Ray.h \ 564 565 566 ViewCellBsp.obj: ViewCellBsp.cpp \ 567 Plane3.h \ 568 ViewCellBsp.h \ 569 Mesh.h \ 570 common.h \ 571 ViewCell.h \ 572 Environment.h \ 573 Polygon3.h \ 574 Ray.h \ 575 AxisAlignedBox3.h \ 576 Vector3.h \ 577 Containers.h \ 578 Intersectable.h \ 579 Matrix4x4.h \ 580 Material.h \ 581 Pvs.h \ 582 Rectangle3.h \ 515 583 516 584 -
trunk/VUT/GtpVisibilityPreprocessor/src/Matrix4x4.cpp
r209 r245 8 8 9 9 Matrix4x4::Matrix4x4(const Vector3 &a, const Vector3 &b, const Vector3 &c) 10 { 11 // first index is column [x], the second is row [y] 12 x[0][0] = a.x; 13 x[1][0] = b.x; 14 x[2][0] = c.x; 15 x[3][0] = 0.0f; 16 17 x[0][1] = a.y; 18 x[1][1] = b.y; 19 x[2][1] = c.y; 20 x[3][1] = 0.0f; 21 22 x[0][2] = a.z; 23 x[1][2] = b.z; 24 x[2][2] = c.z; 25 x[3][2] = 0.0f; 26 27 x[0][3] = 0.0f; 28 x[1][3] = 0.0f; 29 x[2][3] = 0.0f; 30 x[3][3] = 1.0f; 31 32 } 33 34 void 35 Matrix4x4::SetColumns(const Vector3 &a, const Vector3 &b, const Vector3 &c) 10 36 { 11 37 // first index is column [x], the second is row [y] … … 22 48 x[0][2] = c.x; 23 49 x[1][2] = c.y; 24 x[2][2] = c.z;25 x[3][2] = 0.0f;26 27 x[0][3] = 0.0f;28 x[1][3] = 0.0f;29 x[2][3] = 0.0f;30 x[3][3] = 1.0f;31 }32 33 void34 Matrix4x4::SetColumns(const Vector3 &a, const Vector3 &b, const Vector3 &c)35 {36 // first index is column [x], the second is row [y]37 x[0][0] = a.x;38 x[1][0] = b.x;39 x[2][0] = c.x;40 x[3][0] = 0.0f;41 42 x[0][1] = a.y;43 x[1][1] = b.y;44 x[2][1] = c.y;45 x[3][1] = 0.0f;46 47 x[0][2] = a.z;48 x[1][2] = b.z;49 50 x[2][2] = c.z; 50 51 x[3][2] = 0.0f; -
trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp
r209 r245 68 68 nearestFace = faceIndex; 69 69 hit++; 70 break; 71 case Ray::LINE_SEGMENT: 72 if (t <= 1.0f) { 73 ray.intersections.push_back(Ray::Intersection(t, instance, faceIndex)); 74 hit++; 75 } 70 76 break; 71 77 } -
trunk/VUT/GtpVisibilityPreprocessor/src/MeshKdTree.cpp
r177 r245 369 369 entp = extp; 370 370 mint = maxt; 371 372 if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f) 373 break; 374 371 375 RayTraversalData &s = tStack.top(); 372 376 node = s.mNode; -
trunk/VUT/GtpVisibilityPreprocessor/src/MutualVisibility.cpp
r223 r245 9 9 #include "Mesh.h" 10 10 #include "Triangle3.h" 11 #include "SceneGraph.h" 11 12 12 13 void … … 26 27 { 27 28 Vector3 origin, direction; 28 GetRay(rayIndex, origin, direction); 29 return origin + direction*mSamples[rayIndex].mIntersections[depth].mT; 29 Ray ray; 30 GetRaySegment(rayIndex, ray); 31 if (depth >= mSamples[rayIndex].mIntersections.size()) { 32 cerr<<"depth of sample out of limits"<<endl; 33 exit(1); 34 } 35 return ray.Extrap(mSamples[rayIndex].mIntersections[depth].mT); 36 } 37 38 void 39 RayShaft::GetRaySegment(const int i, Ray &ray) const 40 { 41 Vector3 origin, direction; 42 GetRay(i, origin, direction); 43 ray.Init(origin, direction, Ray::LINE_SEGMENT); 44 if ( mSamples[i].IsValid() ) { 45 origin = ray.Extrap(mSamples[i].mMinT); 46 direction = ray.Extrap(mSamples[i].mMaxT) - origin; 47 ray.Init(origin, direction, Ray::LINE_SEGMENT); 48 } 30 49 } 31 50 … … 98 117 99 118 float 100 MutualVisibilitySampler::GetSpatialAngle(const RayShaft &s ample,119 MutualVisibilitySampler::GetSpatialAngle(const RayShaft &shaft, 101 120 const Vector3 &point 102 121 ) 103 122 { 104 const int sampleIndices[] [2]={105 (0,1,2),106 (0,2,3)123 const int sampleIndices[]={ 124 0,1,2, 125 0,2,3 107 126 }; 108 127 109 128 float sum = 0.0f; 110 int i; 111 129 int i, j=0; 130 131 if (!shaft.IsValid()) 132 return 2.0f*mSolidAngleThreshold; 133 112 134 for (i=0; i < 2; i++) { 113 Triangle3 triangle(s ample.GetIntersectionPoint(sampleIndices[i][0], 0),114 s ample.GetIntersectionPoint(sampleIndices[i][1], 0),115 s ample.GetIntersectionPoint(sampleIndices[i][2], 0));135 Triangle3 triangle(shaft.GetIntersectionPoint(sampleIndices[j++], 0), 136 shaft.GetIntersectionPoint(sampleIndices[j++], 0), 137 shaft.GetIntersectionPoint(sampleIndices[j++], 0)); 116 138 sum += triangle.GetSpatialAngle(point); 117 139 } … … 122 144 123 145 void 124 MutualVisibilitySampler::ComputeError(RayShaft &s ample)146 MutualVisibilitySampler::ComputeError(RayShaft &shaft) 125 147 { 126 148 // evaluate minimal error which can be achieved by more precise evaluation 127 149 // if this is above the threshold do not proceed further 128 150 129 float maxAngle = GetSpatialAngle(sample, 130 sample.mSource.GetCenter()); 131 132 for (int i=0; i < 3; i++) { 133 float angle = GetSpatialAngle(sample, 134 sample.mSource.mVertices[i]); 151 // check whether the samples pierce the same mesh 152 Intersectable::NewMail(); 153 int i, j; 154 for (i=0; i < 4; i++) { 155 RaySample *sample = &shaft.mSamples[i]; 156 for (j=0; j < sample->mIntersections.size(); j++) 157 sample->mIntersections[j].mObject->Mail(); 158 } 159 160 for (i=0; i < 4; i++) { 161 RaySample *sample = &shaft.mSamples[i]; 162 for (j=0; j < sample->mIntersections.size(); j++) { 163 if (sample->mIntersections[j].mObject->IncMail() == 4) { 164 cerr<<"T"; 165 shaft.mError = 0.0f; 166 return; 167 } 168 } 169 } 170 171 172 float maxAngle = GetSpatialAngle(shaft, 173 shaft.mTarget.GetCenter()); 174 175 for (i=0; i < 3; i++) { 176 float angle = GetSpatialAngle(shaft, 177 shaft.mTarget.mVertices[i]); 135 178 if (angle > maxAngle) 136 179 maxAngle = angle; 137 180 } 138 139 sample.mError = maxAngle; 140 } 141 181 182 maxAngle = MAX_FLOAT; 183 shaft.mError = maxAngle; 184 } 185 186 187 void 188 MutualVisibilitySampler::ConstructInitialSamples2( 189 const AxisAlignedBox3 &source, 190 const AxisAlignedBox3 &target, 191 vector<RayShaft *> &samples 192 ) 193 { 194 // get all rectangles potentially visible from the source box 195 int i; 196 int sourceMask = 0; 197 for (i=0; i < 8; i++) 198 sourceMask |= source.GetFaceVisibilityMask(target.GetVertex(i)); 199 200 // now for each visble source face find all visible target faces 201 for (i=0; i < 6; i++) { 202 Rectangle3 sourceFace = source.GetFace(i); 203 if ( sourceMask &(1<<i) ) { 204 int mask = target.GetFaceVisibilityMask(sourceFace); 205 // construct triangle samples for all visible rectangles 206 for (int j=0; j < 6; j++) 207 if (mask & (1<<j)) { 208 AddInitialSamples2(sourceFace, target.GetFace(j), samples); 209 } 210 } 211 } 212 } 213 214 void 215 MutualVisibilitySampler::ConstructInitialSamples3( 216 const AxisAlignedBox3 &source, 217 const AxisAlignedBox3 &target, 218 vector<RayShaft *> &samples 219 ) 220 { 221 // get all rectangles potentially visible from the source box 222 int i; 223 int sourceMask = 0; 224 for (i=0; i < 8; i++) 225 sourceMask |= source.GetFaceVisibilityMask(target.GetVertex(i)); 226 227 // now for each visble source face find all visible target faces 228 int face; 229 for (face=0; face < 6; face++) { 230 if ( sourceMask & (1<<face) ) { 231 Rectangle3 sourceRect = source.GetFace(face); 232 233 Vector3 targetCenter = target.Center(); 234 235 Vector3 normal = sourceRect.GetNormal(); 236 237 Plane3 sourcePlane(normal, sourceRect.GetVertex(0)); 238 239 Plane3 targetPlane(normal, target.GetVertex(0)); 240 241 int i; 242 for (i=1; i < 8; i++) { 243 Vector3 v = target.GetVertex(i); 244 if (targetPlane.Distance(v) < 0) 245 targetPlane = Plane3(normal, v); 246 } 247 248 Vector3 xBasis = Normalize(sourceRect.GetVertex(1) - sourceRect.GetVertex(0)); 249 Vector3 yBasis = Normalize(sourceRect.GetVertex(3) - sourceRect.GetVertex(0)); 250 251 // cast rays between the centers of the boxes 252 Vector3 targetRCenter = targetPlane.FindIntersection(sourceRect.GetCenter(), 253 targetCenter); 254 255 Rectangle3 targetRect; 256 257 float targetDist[4]; 258 targetDist[0] = targetDist[1] = targetDist[2] = targetDist[3] = 0.0f; 259 260 // cast rays between corresponding vertices of the boxes 261 int j; 262 int intersections=0; 263 for (i=0; i < 4; i++) 264 for (j=0; j < 8; j++) { 265 Vector3 v; 266 Vector3 diff; 267 bool coplanar; 268 float dist; 269 270 v = targetPlane.FindIntersection(sourceRect.GetVertex(i), 271 target.GetVertex(j), 272 NULL, 273 &coplanar); 274 if (!coplanar) { 275 // evaluate target and 276 diff = targetRCenter - v; 277 dist = DotProd(diff, xBasis); 278 if (dist < targetDist[0]) 279 targetDist[0] = dist; 280 if (dist > targetDist[1]) 281 targetDist[1] = dist; 282 283 dist = DotProd(diff, yBasis); 284 285 if (dist < targetDist[2]) 286 targetDist[2] = dist; 287 if (dist > targetDist[3]) 288 targetDist[3] = dist; 289 intersections++; 290 } 291 } 292 293 if (intersections>=4) { 294 targetRect.mVertices[0] = targetRCenter + targetDist[0]*xBasis + targetDist[2]*yBasis; 295 targetRect.mVertices[1] = targetRCenter + targetDist[1]*xBasis + targetDist[2]*yBasis; 296 targetRect.mVertices[2] = targetRCenter + targetDist[1]*xBasis + targetDist[3]*yBasis; 297 targetRect.mVertices[3] = targetRCenter + targetDist[0]*xBasis + targetDist[3]*yBasis; 298 299 // cout<<sourceRect<<targetRect<<endl; 300 AddInitialSamples(sourceRect, targetRect, samples); 301 } 302 } 303 } 304 } 142 305 143 306 void … … 173 336 xBasis.Normalize(); 174 337 else { 175 xBasis = CrossProd(Vector3(0,0,1), normal);338 xBasis = Normalize(CrossProd(Vector3(0,0,1), normal)); 176 339 } 177 340 … … 188 351 Rectangle3 targetRect; 189 352 190 353 354 if (0) { 191 355 float scale = Magnitude(source.Size())*0.7f; 192 356 sourceRect.mVertices[0] = sourceRCenter - scale*xBasis - scale*yBasis; … … 200 364 targetRect.mVertices[2] = targetRCenter + scale*xBasis + scale*yBasis; 201 365 targetRect.mVertices[3] = targetRCenter - scale*xBasis + scale*yBasis; 202 366 } 367 368 369 float sourceDist[4]; 370 float targetDist[4]; 371 sourceDist[0] = sourceDist[1] = sourceDist[2] = sourceDist[3] = 0.0f; 372 targetDist[0] = targetDist[1] = targetDist[2] = targetDist[3] = 0.0f; 373 374 // cast rays between corresponding vertices of the boxes 375 int j; 376 for (i=0; i < 8; i++) 377 for (j=0; j < 8; j++) { 378 Vector3 v; 379 Vector3 diff; 380 bool coplanar; 381 float dist; 382 v = sourcePlane.FindIntersection(source.GetVertex(i), 383 target.GetVertex(j), 384 NULL, 385 &coplanar); 386 if (!coplanar) { 387 // evaluate source and 388 diff = sourceRCenter - v; 389 dist = DotProd(diff, xBasis); 390 if (dist < sourceDist[0]) 391 sourceDist[0] = dist; 392 if (dist > sourceDist[1]) 393 sourceDist[1] = dist; 394 395 dist = DotProd(diff, yBasis); 396 if (dist < sourceDist[2]) 397 sourceDist[2] = dist; 398 if (dist > sourceDist[3]) 399 sourceDist[3] = dist; 400 } 401 402 v = targetPlane.FindIntersection(source.GetVertex(i), 403 target.GetVertex(j), 404 NULL, 405 &coplanar); 406 if (!coplanar) { 407 // evaluate target and 408 diff = targetRCenter - v; 409 dist = DotProd(diff, xBasis); 410 if (dist < targetDist[0]) 411 targetDist[0] = dist; 412 if (dist > targetDist[1]) 413 targetDist[1] = dist; 414 dist = DotProd(diff, yBasis); 415 if (dist < targetDist[2]) 416 targetDist[2] = dist; 417 if (dist > targetDist[3]) 418 targetDist[3] = dist; 419 } 420 } 421 422 sourceRect.mVertices[0] = sourceRCenter + sourceDist[0]*xBasis + sourceDist[2]*yBasis; 423 sourceRect.mVertices[1] = sourceRCenter + sourceDist[1]*xBasis + sourceDist[2]*yBasis; 424 sourceRect.mVertices[2] = sourceRCenter + sourceDist[1]*xBasis + sourceDist[3]*yBasis; 425 sourceRect.mVertices[3] = sourceRCenter + sourceDist[0]*xBasis + sourceDist[3]*yBasis; 426 427 targetRect.mVertices[0] = targetRCenter + targetDist[0]*xBasis + targetDist[2]*yBasis; 428 targetRect.mVertices[1] = targetRCenter + targetDist[1]*xBasis + targetDist[2]*yBasis; 429 targetRect.mVertices[2] = targetRCenter + targetDist[1]*xBasis + targetDist[3]*yBasis; 430 targetRect.mVertices[3] = targetRCenter + targetDist[0]*xBasis + targetDist[3]*yBasis; 431 432 433 cout<<sourceRect<<targetRect<<endl; 203 434 AddInitialSamples(sourceRect, targetRect, samples); 204 205 // Plane3 bottomPlane(xBasis, source.Center());206 // Plane3 sidePlane(yBasis, source.Center());207 208 // // cast rays between corresponding vertices of the boxes209 // for (i=0; i < 8; i++) {210 // Vector3 v;211 // bool coplanar;212 // v = sourcePlane.FindIntersection(source.GetVertex(i),213 // target.GetVertex(i),214 // NULL,215 // &coplanar);216 // if (!coplanar) {217 // // evaluate source and218 219 // }220 // }221 222 // AddInitialSamples(sourceRectangle, targetRectangle, samples);223 435 } 224 436 … … 236 448 } 237 449 238 239 void 240 MutualVisibilitySampler::ExportSamples(vector<RayShaft *> &samples) 450 void 451 MutualVisibilitySampler::AddInitialSamples2( 452 const Rectangle3 &sourceRect, 453 const Rectangle3 &targetRect, 454 vector<RayShaft *> &samples 455 ) 456 { 457 // align the rectangles properly 458 Rectangle3 sRect, tRect; 459 if (DotProd(sourceRect.GetNormal(), targetRect.GetNormal()) < -0.99f) { 460 int i; 461 for (i=0; i < 4; i++) { 462 tRect.mVertices[i] = targetRect.mVertices[( 7 - i )%4]; 463 } 464 465 RayShaft *sample = new RayShaft(sourceRect, 466 tRect); 467 samples.push_back(sample); 468 } 469 470 return; 471 472 float minDist = MAX_FLOAT; 473 int startI; 474 int startJ; 475 int i, j; 476 477 478 for (i=0; i < 4; i++) { 479 for (j=0; j < 4; j++) { 480 float dist = Distance(sourceRect.mVertices[i], targetRect.mVertices[j]); 481 if (dist < minDist) { 482 minDist = dist; 483 startI = i; 484 startJ = j; 485 } 486 } 487 } 488 for (i=0; i < 4; i++) { 489 sRect.mVertices[i] = sourceRect.mVertices[(startI + i )%4]; 490 tRect.mVertices[i] = targetRect.mVertices[(4 + startJ - i )%4]; 491 } 492 493 RayShaft *sample = new RayShaft(sRect, 494 tRect); 495 samples.push_back(sample); 496 } 497 498 499 void 500 MutualVisibilitySampler::ExportShafts(vector<RayShaft *> &shafts, 501 const bool singleFile) 241 502 { 242 503 static int id = 0; … … 244 505 if (id > 20) 245 506 return; 246 247 for (int i=0; i < samples.size(); i++) { 248 sprintf(filename, "samples%04d-%02d.x3d", id++, i); 249 Exporter *exporter = Exporter::GetExporter(filename); 250 507 508 Exporter *exporter = NULL; 509 for (int i=0; i < shafts.size(); i++) { 510 if (!exporter) { 511 if (singleFile) 512 sprintf(filename, "shafts-single-%04d.x3d", id++); 513 else 514 sprintf(filename, "shafts%04d-%02d.x3d", id++, i); 515 exporter = Exporter::GetExporter(filename); 516 } 517 251 518 exporter->SetWireframe(); 519 // exporter->ExportScene(mSceneGraph->mRoot); 520 252 521 exporter->ExportBox(mSource); 253 522 exporter->ExportBox(mTarget); … … 256 525 257 526 258 RayShaft *s ample = samples[i];527 RayShaft *shaft = shafts[i]; 259 528 Mesh *mesh = new Mesh; 260 mesh->AddRectangle(s ample->mSource);261 mesh->AddRectangle(s ample->mTarget);529 mesh->AddRectangle(shaft->mSource); 530 mesh->AddRectangle(shaft->mTarget); 262 531 vector<Ray> rays; 263 532 for (int j=0; j < 4; j++) { 264 Vector3 origin, direction; 265 sample->GetRay(j, origin, direction); 266 Ray ray(origin, direction, Ray::LINE_SEGMENT); 533 Ray ray; 534 shaft->GetRaySegment(j, ray); 267 535 rays.push_back(ray); 268 536 } … … 273 541 exporter->ExportIntersectable(&mi); 274 542 exporter->ExportRays(rays, -1.0f, m.mDiffuseColor); 543 if (!singleFile) { 544 delete exporter; 545 exporter = NULL; 546 } 547 } 548 if (exporter) 275 549 delete exporter; 276 }550 277 551 } 278 552 … … 283 557 int i; 284 558 285 for (i=0; i < 4; i++) { 286 Vector3 origin, direction; 287 shaft.GetRay(i, origin, direction); 288 // determine intersections with the boxes 289 ray.Init(origin, direction, Ray::LINE_SEGMENT); 290 float stmin, stmax, ttmin, ttmax; 291 if ( mSource.GetMinMaxT(ray, &stmin, &stmax) && mTarget.GetMinMaxT(ray, &ttmin, &ttmax) ) { 292 shaft.mSamples[i].mMinT = stmax; 293 shaft.mSamples[i].mMaxT = ttmin; 294 origin = ray.Extrap(stmax); 295 direction = ray.Extrap(ttmin) - origin; 296 // reinit the ray 559 for (i=0; i < 4; i++) 560 if (!shaft.mSamples[i].IsProcessed()) { 561 Vector3 origin, direction; 562 shaft.GetRay(i, origin, direction); 563 // determine intersections with the boxes 297 564 ray.Init(origin, direction, Ray::LINE_SEGMENT); 298 if (!mKdTree->CastRay(ray)) 299 return VISIBLE; 300 } else { 301 shaft.mSamples[i].mMaxT = -1.0; 302 } 303 } 565 float stmin, stmax = 0.0f, ttmin=1.0f, ttmax; 566 bool valid = true; 567 568 if (mUseBoxes) { 569 if (mSource.GetMinMaxT(ray, &stmin, &stmax) && 570 mTarget.GetMinMaxT(ray, &ttmin, &ttmax)) { 571 shaft.mSamples[i].mMinT = stmax; 572 shaft.mSamples[i].mMaxT = ttmin; 573 origin = ray.Extrap(stmax); 574 direction = ray.Extrap(ttmin) - origin; 575 // reinit the ray 576 ray.Init(origin, direction, Ray::LINE_SEGMENT); 577 } else 578 valid = false; 579 } else { 580 shaft.mSamples[i].mMinT = 0.0f; 581 shaft.mSamples[i].mMaxT = 1.0f; 582 } 583 if (valid) { 584 if (!mKdTree->CastRay(ray)) { 585 cerr<<"V"<<endl; 586 return VISIBLE; 587 } 588 shaft.mSamples[i].mIntersections = ray.intersections; 589 cerr<<"I"; 590 } else { 591 cerr<<"X"; 592 shaft.mSamples[i].SetInvalid(); 593 } 594 } 304 595 return INVISIBLE; 305 596 } … … 308 599 MutualVisibilitySampler::ComputeVisibility() 309 600 { 601 int result = INVISIBLE; 310 602 311 603 vector<RayShaft *> shafts; 312 ConstructInitialSamples(mSource, mTarget, shafts); 313 ExportSamples(shafts); 604 ConstructInitialSamples3(mSource, mTarget, shafts); 605 606 if (1) 607 ExportShafts(shafts, false); 314 608 315 609 stack<RayShaft *> shaftStack; 316 610 317 318 611 for (int i=0; i < shafts.size(); i++) 612 shaftStack.push(shafts[i]); 613 614 shafts.clear(); 319 615 Ray ray; 320 // now process the shafts as long as we have something to do 616 617 // now process the shafts as long as we have something to do 321 618 while (!shaftStack.empty()) { 322 619 RayShaft *shaft = shaftStack.top(); … … 326 623 // int triangleSplitEdge = SetupExtremalRay(sample, source, ray); 327 624 328 if (CastRays(*shaft) == VISIBLE) 329 return VISIBLE; 330 331 // // generate 2 new samples 332 // RayShaft newSamples[2]; 333 // sample.Split(triangleSplitEdge, ray, newSamples[0], newSamples[1]); 334 // for (i=0; i < 2; i++) { 335 // newSamples[i].ComputeError(); 336 // if (newSamples[i].mError > solidAngleThreshold) { 337 // sampleStack.push(newSamples[i]); 338 // } 339 // } 340 // } 341 } 342 343 for (int i=0; i < shafts.size(); i++) 625 if (CastRays(*shaft) == VISIBLE) { 626 result = VISIBLE; 627 break; 628 } 629 630 // compute error .... 631 ComputeError(*shaft); 632 cout<<shaft->mDepth<<"|"; 633 if (shaft->IsValid()) 634 shafts.push_back(shaft); 635 636 if (shaft->mDepth < 10 && 637 shaft->mError > mSolidAngleThreshold) { 638 639 // generate 2 new samples 640 RayShaft *newSamples[2]; 641 newSamples[0] = new RayShaft; 642 newSamples[1] = new RayShaft; 643 644 // chose what to split 645 bool splitSource = shaft->mSource.GetArea() > shaft->mTarget.GetArea(); 646 int axis; 647 if (splitSource) { 648 axis = shaft->mSource.DominantAxis(); 649 } else { 650 axis = shaft->mTarget.DominantAxis(); 651 } 652 653 PerformSplit(*shaft, splitSource, axis, *newSamples[0], *newSamples[1]); 654 // delete shaft; 655 shaftStack.push(newSamples[0]); 656 shaftStack.push(newSamples[1]); 657 } else { 658 // store a terminal shaft 659 } 660 661 } 662 663 while (!shaftStack.empty()) { 664 RayShaft *shaft = shaftStack.top(); 665 shaftStack.pop(); 666 delete shaft; 667 } 668 669 if (0) 670 ExportShafts(shafts, true); 671 672 for (i=0; i < shafts.size(); i++) { 344 673 delete shafts[i]; 345 return INVISIBLE; 346 } 347 348 MutualVisibilitySampler::MutualVisibilitySampler(KdTree *kdTree, 674 } 675 676 return result; 677 } 678 679 MutualVisibilitySampler::MutualVisibilitySampler(SceneGraph *sceneGraph, 680 KdTree *kdTree, 349 681 AxisAlignedBox3 &source, 350 682 AxisAlignedBox3 &target, 351 683 const float solidAngleThreshold) 352 684 { 685 mSceneGraph = sceneGraph; 353 686 mKdTree = kdTree; 354 687 mSource = source; 355 688 mTarget = target; 689 mUseBoxes = true; 356 690 mSolidAngleThreshold = solidAngleThreshold; 357 691 } … … 359 693 360 694 int 361 ComputeBoxVisibility(KdTree *kdTree, 695 ComputeBoxVisibility(SceneGraph *sceneGraph, 696 KdTree *kdTree, 362 697 AxisAlignedBox3 &source, 363 698 AxisAlignedBox3 &target, 364 699 const float solidAngleThreshold) 365 700 { 366 MutualVisibilitySampler sampler( kdTree, source, target, solidAngleThreshold);701 MutualVisibilitySampler sampler(sceneGraph, kdTree, source, target, solidAngleThreshold); 367 702 368 703 -
trunk/VUT/GtpVisibilityPreprocessor/src/MutualVisibility.h
r223 r245 7 7 class Intersectable; 8 8 class AxisAlignedBox3; 9 class SceneGraph; 9 10 10 struct SimpleRay {11 Vector3 mOrigin;12 Vector3 mDirection;13 };14 11 15 12 struct RaySample { … … 17 14 float mMinT; 18 15 float mMaxT; 16 17 RaySample():mMinT(-1.0f), mMaxT(-1.0f) {} 19 18 /// intersections of the sample with the scene 20 19 vector<Ray::Intersection> mIntersections; 21 bool IsValid() const { return mMaxT > 0.0; } 20 void SetInvalid() { 21 mMinT = 0.0f; 22 mMaxT = -1.0f; 23 } 24 bool IsValid() const { return mMaxT > 0.0f; } 25 bool IsProcessed() const { return mMinT != mMaxT; } 22 26 }; 23 27 … … 37 41 void ComputeError(); 38 42 39 RayShaft 43 RayShaft() {} 40 44 41 45 RayShaft ( … … 45 49 Init(source, target); 46 50 } 47 51 52 bool IsValid() const { return 53 mSamples[0].IsValid() && 54 mSamples[1].IsValid() && 55 mSamples[2].IsValid() && 56 mSamples[3].IsValid(); 57 } 58 48 59 // initial triangle sample 49 60 void Init( … … 58 69 Vector3 &origin, 59 70 Vector3 &direction) const; 71 72 void 73 GetRaySegment(const int i, Ray &ray) const; 74 60 75 }; 61 76 … … 65 80 class MutualVisibilitySampler { 66 81 public: 82 SceneGraph *mSceneGraph; 67 83 KdTree *mKdTree; 68 84 AxisAlignedBox3 mSource; 69 85 AxisAlignedBox3 mTarget; 70 86 float mSolidAngleThreshold; 87 bool mUseBoxes; 71 88 72 73 MutualVisibilitySampler(KdTree *kdTree,89 MutualVisibilitySampler(SceneGraph *sceneGraph, 90 KdTree *kdTree, 74 91 AxisAlignedBox3 &source, 75 92 AxisAlignedBox3 &target, … … 86 103 87 104 void 105 ConstructInitialSamples2( 106 const AxisAlignedBox3 &source, 107 const AxisAlignedBox3 &target, 108 vector<RayShaft *> &samples 109 ); 110 111 void 112 ConstructInitialSamples3( 113 const AxisAlignedBox3 &source, 114 const AxisAlignedBox3 &target, 115 vector<RayShaft *> &samples 116 ); 117 118 void 88 119 AddInitialSamples( 89 120 const Rectangle3 &sourceRect, … … 91 122 vector<RayShaft *> &samples 92 123 ); 124 125 void 126 AddInitialSamples2( 127 const Rectangle3 &sourceRect, 128 const Rectangle3 &targetRect, 129 vector<RayShaft *> &samples 130 ); 93 131 94 132 // the split sample method contains a methodology to create new samples … … 126 164 127 165 void 128 ExportS amples(vector<RayShaft *> &samples);166 ExportShafts(vector<RayShaft *> &samples, const bool singleFile); 129 167 130 168 … … 132 170 133 171 int 134 ComputeBoxVisibility(KdTree *kdTree, 172 ComputeBoxVisibility(SceneGraph *sceneGraph, 173 KdTree *kdTree, 135 174 AxisAlignedBox3 &source, 136 175 AxisAlignedBox3 &target, -
trunk/VUT/GtpVisibilityPreprocessor/src/Plane3.h
r241 r245 43 43 ) const 44 44 { 45 const Vector3 v = b - a; // line from A to B 46 float dv = DotProd(mNormal, v); 47 48 if (signum(dv) == 0) 49 { 50 if (coplanar) 51 (*coplanar) = true; 52 53 return a; 54 } 55 56 float u = - Distance(a) / dv; // TODO: could be done more efficiently 57 //Debug << "t: " << u << ", b - a: " << v << ", norm: " << mNormal << ", dist(a): " << - Distance(a) << ", dv: " << dv << endl; 58 if (coplanar) 59 (*coplanar) = false; 45 const Vector3 v = b - a; // line from A to B 46 float dv = DotProd(mNormal, v); 47 48 if (signum(dv) == 0) 49 { 50 if (coplanar) 51 (*coplanar) = true; 60 52 61 if (t) 62 (*t) = u; 63 64 return a + u * b - u * a; // NOTE: gives better precision than calclulating a + u * v 65 //return a + (u * v); 53 return a; 54 } 55 56 float u = - Distance(a) / dv; // TODO: could be done more efficiently 57 //Debug << "t: " << u << ", b - a: " << v << ", norm: " << mNormal << ", dist(a): " << - Distance(a) << ", dv: " << dv << endl; 58 if (coplanar) 59 (*coplanar) = false; 60 61 if (t) 62 (*t) = u; 63 64 return a + u * b - u * a; // NOTE: gives better precision than calclulating a + u * v 65 //return a + (u * v); 66 66 } 67 67 68 68 friend bool 69 69 PlaneIntersection(const Plane3 &a, const Plane3 &b, const Plane3 &c, Vector3 &result); -
trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
r209 r245 86 86 87 87 // Inititalize the ray again when already constructed 88 void Init(const Vector3 &wherefrom, const Vector3 &whichdir, 88 void Init(const Vector3 &wherefrom, 89 const Vector3 &whichdir, 89 90 const int _type, 90 91 bool dirNormalized = false) { 91 92 loc = wherefrom; 92 dir = (dirNormalized ) ? whichdir: Normalize(whichdir) ;93 dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ; 93 94 mType = _type; 94 95 depth = 0; -
trunk/VUT/GtpVisibilityPreprocessor/src/Rectangle3.h
r223 r245 1 1 #ifndef __RECTANGLE3_H 2 2 #define __RECTANGLE3_H 3 3 #include <iostream> 4 using namespace std; 4 5 #include "Vector3.h" 5 6 … … 11 12 Vector3 mVertices[4]; 12 13 14 Vector3 GetVertex(const int i) const { return mVertices[i]; } 13 15 Rectangle3() {} 14 16 … … 23 25 } 24 26 25 Vector3 GetNormal() {27 Vector3 GetNormal() const { 26 28 return Normalize(CrossProd(mVertices[0]-mVertices[1], 27 29 mVertices[2]-mVertices[1] … … 29 31 } 30 32 31 Vector3 GetCenter() {33 Vector3 GetCenter() const { 32 34 return (mVertices[0] + mVertices[1] + mVertices[2] + mVertices[3])/4.0f; 33 35 } 34 36 35 37 int DominantAxis() const { 38 if (SqrMagnitude(mVertices[0] - mVertices[1]) > SqrMagnitude(mVertices[0] - mVertices[3])) 39 return 0; 40 else 41 return 1; 42 } 43 44 float GetArea() const { 45 Vector3 v = CrossProd(mVertices[3], mVertices[0]); 46 47 for (int i=0; i < 3; i++) 48 v += CrossProd(mVertices[i], mVertices[i+1]); 49 50 return 0.5f*abs(DotProd(GetNormal(), v)); 51 } 52 36 53 void 37 54 Split(const int axis, … … 39 56 Rectangle3 &r2 40 57 ) const; 41 58 59 friend ostream& operator<< (ostream &s, const Rectangle3 &r) { 60 return s<<"Rectangle3:"<<r.mVertices[0]<<r.mVertices[1]<<r.mVertices[2]<<r.mVertices[3]; 61 } 62 42 63 }; 43 64 -
trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
r242 r245 12 12 environment->GetIntValue("Sampling.totalSamples", mTotalSamples); 13 13 mKdPvsDepth = 100; 14 mStats.open("stats.log"); 14 15 15 16 } … … 68 69 } 69 70 71 // void 72 // SamplingPreprocessor::AvsGenerateRandomRay(Ray &ray) 73 // { 74 // int objId = RandomValue(0, mObjects.size()); 75 // Intersectable *object = objects[objId]; 76 // object->GetRandomSurfacePoint(point, normal); 77 // direction = UniformRandomVector(normal); 78 // SetupRay(ray, point, direction); 79 // } 80 81 // void 82 // SamplingPreprocessor::AvsHandleRay(Ray &ray) 83 // { 84 // int sampleContributions = 0; 85 86 // mKdTree->CastRay(ray); 87 88 // if (ray.leaves.size()) { 89 // sampleContributions += AddNodeSamples(object, ray, pass); 90 91 // if (ray.intersections.size()) { 92 // sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass); 93 // } 94 // } 95 // } 96 97 // void 98 // SamplingPreprocessor::AvsBorderSampling(Ray &ray) 99 // { 100 101 102 // } 103 104 // void 105 // SamplingPreprocessor::AvsPass() 106 // { 107 // Ray ray; 108 // while (1) { 109 // AvsGenerateRay(ray); 110 // HandleRay(ray); 111 // while ( !mRayQueue.empty() ) { 112 // Ray ray = mRayQueue.pop(); 113 // mRayQueue.pop(); 114 // AdaptiveBorderSampling(ray); 115 // } 116 // } 117 118 119 120 // } 121 122 123 70 124 bool 71 125 SamplingPreprocessor::ComputeVisibility() 72 126 { 73 127 74 128 // pickup an object 75 129 ObjectContainer objects; … … 100 154 KdNode *nodeToSample = NULL; 101 155 Intersectable *object = objects[i]; 102 156 103 157 int pvsSize = object->mKdPvs.GetSize(); 104 158 159 160 105 161 if (0 && pvsSize) { 106 162 // mail all nodes from the pvs … … 125 181 } 126 182 } 127 128 129 if (pvsSize && pass == 50 ) { 183 184 if (0 && pvsSize && pass == 1000 ) { 130 185 // mail all nodes from the pvs 131 186 Intersectable::NewMail(); … … 144 199 AxisAlignedBox3 box = object->GetBox(); 145 200 for (int j=0; j < invisibleNeighbors.size(); j++) { 146 int visibility = ComputeBoxVisibility(mKdTree, 201 int visibility = ComputeBoxVisibility(mSceneGraph, 202 mKdTree, 147 203 box, 148 204 mKdTree->GetBox(invisibleNeighbors[j]), 149 1 .0f);150 205 1e-6f); 206 // exit(0); 151 207 } 152 208 // now rank all the neighbors according to probability that a new … … 155 211 } 156 212 157 213 object->GetRandomSurfacePoint(point, normal); 214 nodeToSample = mKdTree->GetRandomLeaf(Plane3(normal, point)); 215 158 216 for (int k=0; k < mSamplesPerPass; k++) { 159 object->GetRandomSurfacePoint(point, normal); 160 217 161 218 if (nodeToSample) { 162 219 int maxTries = 5; 163 220 164 221 for (int tries = 0; tries < maxTries; tries++) { 165 222 direction = mKdTree->GetBox(nodeToSample).GetRandomPoint() - point; 166 223 167 224 if (DotProd(direction, normal) > Limits::Small) 168 225 break; 169 226 } 170 227 171 228 if (tries == maxTries) 172 229 direction = UniformRandomVector(normal); 173 } 174 else 230 } 231 else { 175 232 direction = UniformRandomVector(normal); 176 233 } 234 177 235 // construct a ray 178 236 SetupRay(ray, point, direction); 179 237 mKdTree->CastRay(ray); 180 181 if ( i < pvsOut)238 239 if ( i < pvsOut ) 182 240 rays[i].push_back(ray); 183 241 184 242 int sampleContributions = 0; 185 186 243 187 244 if (ray.leaves.size()) { 188 245 sampleContributions += AddNodeSamples(object, ray, pass); 189 246 190 247 if (ray.intersections.size()) { 191 248 sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass); … … 222 279 } 223 280 224 cout << " pass " << pass<<" : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;225 cout << "# totalSamples=" << totalSamples/1000226 << "k # sampleContributions=" << passSampleContributions << " ("281 cout << "#Pass " << pass<<" : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 282 cout << "#TotalSamples=" << totalSamples/1000 283 << "k #SampleContributions=" << passSampleContributions << " (" 227 284 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" 228 285 << pvsSize/(float)objects.size() << endl 229 286 << "avg ray contrib=" << passSampleContributions/(float)passContributingSamples << endl; 287 288 289 mStats << 290 "#Pass\n" <<pass<<endl<< 291 "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< 292 "#TotalSamples\n" << totalSamples<< endl<< 293 "#SampleContributions\n" << passSampleContributions << endl << 294 "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << 295 "#AvgPVS\n"<< pvsSize/(float)objects.size() << endl << 296 "#AvgRayContrib\n" << passSampleContributions/(float)passContributingSamples << endl; 230 297 } 231 298 -
trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h
r191 r245 2 2 #define _SamplingPreprocessor_H__ 3 3 4 #include <fstream> 5 using namespace std; 6 4 7 #include "Preprocessor.h" 5 8 9 struct SimpleRay { 10 Vector3 mOrigin; 11 Vector3 mDirection; 12 }; 6 13 7 14 /** Sampling based visibility preprocessing. The implementation is based on heuristical … … 12 19 int mTotalSamples; 13 20 int mKdPvsDepth; 14 21 ofstream mStats; 22 ObjectContainer mObjects; 23 15 24 SamplingPreprocessor(); 16 25 -
trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
r239 r245 59 59 { 60 60 // maximal max viewcells 61 int limit = maxViewCells > 0 ? std::min((int)objects.size(), maxViewCells) : (int)objects.size();61 int limit = maxViewCells > 0 ? Min((int)objects.size(), maxViewCells) : (int)objects.size(); 62 62 63 63 for (int i = 0; i < limit; ++i) -
trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
r242 r245 350 350 ObjectContainer::const_iterator it, it_end = objects.end(); 351 351 352 int limit = (maxObjects > 0) ? min((int)objects.size(), maxObjects) : (int)objects.size();352 int limit = (maxObjects > 0) ? Min((int)objects.size(), maxObjects) : (int)objects.size(); 353 353 354 354 // initialise bounding box … … 463 463 Plane3 *bestPlane = NULL; 464 464 465 int limit = min((int)polygons->size(), maxTests);465 int limit = Min((int)polygons->size(), maxTests); 466 466 467 467 for (int i = 0; i < limit; ++i) -
trunk/VUT/GtpVisibilityPreprocessor/src/default.env
r209 r245 8 8 # filename glasgow1.x3d 9 9 # filename vienna.x3d 10 filename ../data/atlanta/atlanta2.x3d10 # filename ../data/atlanta/atlanta2.x3d 11 11 # filename ../data/soda/soda.dat 12 #filename ../data/soda/soda5.dat12 filename ../data/soda/soda5.dat 13 13 14 14 } … … 54 54 55 55 Sampling { 56 totalSamples 100000057 samplesPerPass 556 totalSamples 50000000 57 samplesPerPass 20 58 58 } -
trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro
r209 r245 16 16 17 17 # RELEASE CONFIG 18 #CONFIG += windows warn_on thread 18 #CONFIG += windows warn_on thread release 19 19 20 20 # DEPENDPATH = ../../include … … 32 32 Matrix4x4.cpp Vector3.cpp AxisAlignedBox3.cpp Ray.cpp main.cpp Mesh.cpp \ 33 33 Exporter.cpp Camera.cpp X3dParser.cpp MeshKdTree.cpp Pvs.cpp \ 34 MutualVisibility.cpp Triangle3.cpp Rectangle3.cpp Plane3.cpp 34 MutualVisibility.cpp Triangle3.cpp Rectangle3.cpp Plane3.cpp Polygon3.cpp \ 35 ViewCell.cpp ViewCellBsp.cpp 35 36 36 37
Note: See TracChangeset
for help on using the changeset viewer.