00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "octreeDataFace.H"
00028 #include "labelList.H"
00029 #include "polyMesh.H"
00030 #include "octree.H"
00031 #include "polyPatch.H"
00032 #include "triangleFuncs.H"
00033 #include "linePointRef.H"
00034
00035
00036
00037 defineTypeNameAndDebug(Foam::octreeDataFace, 0);
00038
00039 Foam::scalar Foam::octreeDataFace::tol(1E-6);
00040
00041
00042
00043
00044 void Foam::octreeDataFace::calcBb()
00045 {
00046 allBb_.setSize(meshFaces_.size());
00047 allBb_ = treeBoundBox::invertedBox;
00048
00049 forAll (meshFaces_, i)
00050 {
00051
00052 treeBoundBox& myBb = allBb_[i];
00053
00054 const face& f = mesh_.faces()[meshFaces_[i]];
00055
00056 forAll(f, faceVertexI)
00057 {
00058 const point& coord = mesh_.points()[f[faceVertexI]];
00059
00060 myBb.min() = min(myBb.min(), coord);
00061 myBb.max() = max(myBb.max(), coord);
00062 }
00063 }
00064 }
00065
00066
00067
00068
00069
00070 Foam::octreeDataFace::octreeDataFace
00071 (
00072 const primitiveMesh& mesh,
00073 const labelList& meshFaces,
00074 const treeBoundBoxList& allBb
00075 )
00076 :
00077 mesh_(mesh),
00078 meshFaces_(meshFaces),
00079 allBb_(allBb)
00080 {}
00081
00082
00083
00084 Foam::octreeDataFace::octreeDataFace
00085 (
00086 const primitiveMesh& mesh,
00087 const labelList& meshFaces
00088 )
00089 :
00090 mesh_(mesh),
00091 meshFaces_(meshFaces),
00092 allBb_(meshFaces_.size())
00093 {
00094
00095 calcBb();
00096 }
00097
00098
00099
00100 Foam::octreeDataFace::octreeDataFace
00101 (
00102 const primitiveMesh& mesh,
00103 const UList<const labelList*>& meshFaceListPtrs,
00104 const UList<const treeBoundBoxList*>& bbListPtrs
00105 )
00106 :
00107 mesh_(mesh),
00108 meshFaces_(0),
00109 allBb_(0)
00110 {
00111 label faceI = 0;
00112
00113 forAll(meshFaceListPtrs, listI)
00114 {
00115 faceI += meshFaceListPtrs[listI]->size();
00116 }
00117
00118 meshFaces_.setSize(faceI);
00119 allBb_.setSize(faceI);
00120
00121 faceI = 0;
00122
00123 forAll(meshFaceListPtrs, listI)
00124 {
00125 const labelList& meshFaces = *meshFaceListPtrs[listI];
00126 const treeBoundBoxList& allBb = *bbListPtrs[listI];
00127
00128 forAll(meshFaces, meshFaceI)
00129 {
00130 meshFaces_[faceI] = meshFaces[meshFaceI];
00131 allBb_[faceI] = allBb[meshFaceI];
00132 faceI++;
00133 }
00134 }
00135 }
00136
00137
00138
00139 Foam::octreeDataFace::octreeDataFace
00140 (
00141 const primitiveMesh& mesh,
00142 const UList<const labelList*>& meshFaceListPtrs
00143 )
00144 :
00145 mesh_(mesh),
00146 meshFaces_(0)
00147 {
00148 label faceI = 0;
00149
00150 forAll(meshFaceListPtrs, listI)
00151 {
00152 faceI += meshFaceListPtrs[listI]->size();
00153 }
00154
00155 meshFaces_.setSize(faceI);
00156
00157 faceI = 0;
00158
00159 forAll(meshFaceListPtrs, listI)
00160 {
00161 const labelList& meshFaces = *meshFaceListPtrs[listI];
00162
00163 forAll(meshFaces, meshFaceI)
00164 {
00165 meshFaces_[faceI++] = meshFaces[meshFaceI];
00166 }
00167 }
00168
00169
00170 calcBb();
00171 }
00172
00173
00174
00175 Foam::octreeDataFace::octreeDataFace(const polyPatch& patch)
00176 :
00177 mesh_(patch.boundaryMesh().mesh()),
00178 meshFaces_(patch.size())
00179 {
00180 forAll(patch, patchFaceI)
00181 {
00182 meshFaces_[patchFaceI] = patch.start() + patchFaceI;
00183 }
00184
00185
00186 calcBb();
00187 }
00188
00189
00190
00191 Foam::octreeDataFace::octreeDataFace(const primitiveMesh& mesh)
00192 :
00193 mesh_(mesh),
00194 meshFaces_(0),
00195 allBb_(0)
00196 {
00197
00198 meshFaces_.setSize(mesh_.nFaces() - mesh_.nInternalFaces());
00199
00200
00201 label boundaryFaceI = 0;
00202
00203 for (label faceI = mesh_.nInternalFaces(); faceI < mesh_.nFaces(); faceI++)
00204 {
00205 meshFaces_[boundaryFaceI++] = faceI;
00206 }
00207
00208
00209 calcBb();
00210 }
00211
00212
00213
00214 Foam::octreeDataFace::octreeDataFace(const octreeDataFace& shapes)
00215 :
00216 mesh_(shapes.mesh()),
00217 meshFaces_(shapes.meshFaces()),
00218 allBb_(shapes.allBb())
00219 {}
00220
00221
00222
00223
00224 Foam::octreeDataFace::~octreeDataFace()
00225 {}
00226
00227
00228
00229
00230 Foam::label Foam::octreeDataFace::getSampleType
00231 (
00232 const octree<octreeDataFace>& oc,
00233 const point& sample
00234 ) const
00235 {
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 treeBoundBox tightest(treeBoundBox::greatBox);
00247 scalar tightestDist(treeBoundBox::great);
00248
00249 label index = oc.findNearest(sample, tightest, tightestDist);
00250
00251 if (index == -1)
00252 {
00253 FatalErrorIn
00254 (
00255 "octreeDataFace::getSampleType"
00256 "(octree<octreeDataFace>&, const point&)"
00257 ) << "Could not find " << sample << " in octree."
00258 << abort(FatalError);
00259 }
00260
00261
00262
00263 label faceI = meshFaces_[index];
00264
00265 if (debug & 2)
00266 {
00267 Pout<< "getSampleType : sample:" << sample
00268 << " nearest face:" << faceI;
00269 }
00270
00271 const face& f = mesh_.faces()[faceI];
00272
00273 const pointField& points = mesh_.points();
00274
00275 pointHit curHit = f.nearestPoint(sample, points);
00276
00277
00278
00279
00280
00281 if (curHit.hit())
00282 {
00283
00284
00285 if (debug & 2)
00286 {
00287 Pout<< " -> face hit:" << curHit.hitPoint()
00288 << " comparing to face normal " << mesh_.faceAreas()[faceI]
00289 << endl;
00290 }
00291 return octree<octreeDataFace>::getVolType
00292 (
00293 mesh_.faceAreas()[faceI],
00294 sample - curHit.hitPoint()
00295 );
00296 }
00297
00298 if (debug & 2)
00299 {
00300 Pout<< " -> face miss:" << curHit.missPoint();
00301 }
00302
00303
00304
00305
00306
00307
00308 scalar typDim = sqrt(mag(mesh_.faceAreas()[faceI])) + VSMALL;
00309
00310 forAll(f, fp)
00311 {
00312 if ((mag(points[f[fp]] - curHit.missPoint())/typDim) < tol)
00313 {
00314
00315
00316
00317
00318 const labelList& myFaces = mesh_.pointFaces()[f[fp]];
00319
00320 vector pointNormal(vector::zero);
00321
00322 forAll(myFaces, myFaceI)
00323 {
00324 if (myFaces[myFaceI] >= mesh_.nInternalFaces())
00325 {
00326 vector n = mesh_.faceAreas()[myFaces[myFaceI]];
00327 n /= mag(n) + VSMALL;
00328
00329 pointNormal += n;
00330 }
00331 }
00332
00333 if (debug & 2)
00334 {
00335 Pout<< " -> face point hit :" << points[f[fp]]
00336 << " point normal:" << pointNormal
00337 << " distance:"
00338 << mag(points[f[fp]] - curHit.missPoint())/typDim
00339 << endl;
00340 }
00341 return octree<octreeDataFace>::getVolType
00342 (
00343 pointNormal,
00344 sample - curHit.missPoint()
00345 );
00346 }
00347 }
00348 if ((mag(mesh_.faceCentres()[faceI] - curHit.missPoint())/typDim) < tol)
00349 {
00350
00351
00352
00353 if (debug & 2)
00354 {
00355 Pout<< " -> centre hit:" << mesh_.faceCentres()[faceI]
00356 << " distance:"
00357 << mag(mesh_.faceCentres()[faceI] - curHit.missPoint())/typDim
00358 << endl;
00359 }
00360
00361 return octree<octreeDataFace>::getVolType
00362 (
00363 mesh_.faceAreas()[faceI],
00364 sample - curHit.missPoint()
00365 );
00366 }
00367
00368
00369
00370
00371
00372
00373 const labelList& myEdges = mesh_.faceEdges()[faceI];
00374
00375 forAll(myEdges, myEdgeI)
00376 {
00377 const edge& e = mesh_.edges()[myEdges[myEdgeI]];
00378
00379 pointHit edgeHit = line<point, const point&>
00380 (
00381 points[e.start()],
00382 points[e.end()]
00383 ).nearestDist(sample);
00384
00385
00386 if ((mag(edgeHit.rawPoint() - curHit.missPoint())/typDim) < tol)
00387 {
00388
00389
00390
00391
00392 const labelList& myFaces = mesh_.edgeFaces()[myEdges[myEdgeI]];
00393
00394 vector edgeNormal(vector::zero);
00395
00396 forAll(myFaces, myFaceI)
00397 {
00398 if (myFaces[myFaceI] >= mesh_.nInternalFaces())
00399 {
00400 vector n = mesh_.faceAreas()[myFaces[myFaceI]];
00401 n /= mag(n) + VSMALL;
00402
00403 edgeNormal += n;
00404 }
00405 }
00406
00407 if (debug & 2)
00408 {
00409 Pout<< " -> real edge hit point:" << edgeHit.rawPoint()
00410 << " comparing to edge normal:" << edgeNormal
00411 << endl;
00412 }
00413
00414
00415
00416 return octree<octreeDataFace>::getVolType
00417 (
00418 edgeNormal,
00419 sample - curHit.missPoint()
00420 );
00421 }
00422 }
00423
00424
00425
00426
00427
00428
00429 forAll(f, fp)
00430 {
00431 pointHit edgeHit =
00432 line<point, const point&>
00433 (
00434 points[f[fp]],
00435 mesh_.faceCentres()[faceI]
00436 ).nearestDist(sample);
00437
00438 if ((mag(edgeHit.rawPoint() - curHit.missPoint())/typDim) < tol)
00439 {
00440
00441
00442
00443 const label fpPrev = f.rcIndex(fp);
00444 const label fpNext = f.fcIndex(fp);
00445
00446 vector e = points[f[fp]] - mesh_.faceCentres()[faceI];
00447 vector ePrev = points[f[fpPrev]] - mesh_.faceCentres()[faceI];
00448 vector eNext = points[f[fpNext]] - mesh_.faceCentres()[faceI];
00449
00450 vector nLeft = ePrev ^ e;
00451 nLeft /= mag(nLeft) + VSMALL;
00452
00453 vector nRight = e ^ eNext;
00454 nRight /= mag(nRight) + VSMALL;
00455
00456 if (debug & 2)
00457 {
00458 Pout<< " -> internal edge hit point:" << edgeHit.rawPoint()
00459 << " comparing to edge normal "
00460 << 0.5*(nLeft + nRight)
00461 << endl;
00462 }
00463
00464
00465
00466 return octree<octreeDataFace>::getVolType
00467 (
00468 0.5*(nLeft + nRight),
00469 sample - curHit.missPoint()
00470 );
00471 }
00472 }
00473
00474 if (debug & 2)
00475 {
00476 Pout<< "Did not find sample " << sample
00477 << " anywhere related to nearest face " << faceI << endl
00478 << "Face:";
00479
00480 forAll(f, fp)
00481 {
00482 Pout<< " vertex:" << f[fp] << " coord:" << points[f[fp]]
00483 << endl;
00484 }
00485 }
00486
00487
00488
00489
00490
00491
00492 return octree<octreeDataFace>::UNKNOWN;
00493 }
00494
00495
00496 bool Foam::octreeDataFace::overlaps
00497 (
00498 const label index,
00499 const treeBoundBox& sampleBb
00500 ) const
00501 {
00502
00503
00504
00505
00506
00507 if (!sampleBb.overlaps(allBb_[index]))
00508 {
00509 return false;
00510 }
00511
00512
00513 label faceI = meshFaces_[index];
00514
00515 const face& f = mesh_.faces()[faceI];
00516
00517 const pointField& points = mesh_.points();
00518
00519 forAll(f, fp)
00520 {
00521 if (sampleBb.contains(points[f[fp]]))
00522 {
00523 return true;
00524 }
00525 }
00526
00527
00528
00529 const point& fc = mesh_.faceCentres()[faceI];
00530
00531 forAll(f, fp)
00532 {
00533 const label fp1 = f.fcIndex(fp);
00534
00535 bool triIntersects = triangleFuncs::intersectBb
00536 (
00537 points[f[fp]],
00538 points[f[fp1]],
00539 fc,
00540 sampleBb
00541 );
00542
00543 if (triIntersects)
00544 {
00545 return true;
00546 }
00547 }
00548 return false;
00549 }
00550
00551
00552 bool Foam::octreeDataFace::contains(const label, const point&) const
00553 {
00554 notImplemented
00555 (
00556 "octreeDataFace::contains(const label, const point&)"
00557 );
00558 return false;
00559 }
00560
00561
00562 bool Foam::octreeDataFace::intersects
00563 (
00564 const label index,
00565 const point& start,
00566 const point& end,
00567 point& intersectionPoint
00568 ) const
00569 {
00570 label faceI = meshFaces_[index];
00571
00572 const face& f = mesh_.faces()[faceI];
00573
00574 const vector dir(end - start);
00575
00576
00577 scalar oldTol = intersection::setPlanarTol(0.0);
00578
00579 pointHit inter = f.ray
00580 (
00581 start,
00582 dir,
00583 mesh_.points(),
00584 intersection::HALF_RAY,
00585 intersection::VECTOR
00586 );
00587
00588 intersection::setPlanarTol(oldTol);
00589
00590 if (inter.hit() && inter.distance() <= mag(dir))
00591 {
00592 intersectionPoint = inter.hitPoint();
00593
00594 return true;
00595 }
00596 else
00597 {
00598 return false;
00599 }
00600 }
00601
00602
00603 bool Foam::octreeDataFace::findTightest
00604 (
00605 const label index,
00606 const point& sample,
00607 treeBoundBox& tightest
00608 ) const
00609 {
00610
00611 point myNear, myFar;
00612 allBb_[index].calcExtremities(sample, myNear, myFar);
00613
00614 const point dist = myFar - sample;
00615 scalar myFarDist = mag(dist);
00616
00617 point tightestNear, tightestFar;
00618 tightest.calcExtremities(sample, tightestNear, tightestFar);
00619
00620 scalar tightestFarDist = mag(tightestFar - sample);
00621
00622 if (tightestFarDist < myFarDist)
00623 {
00624
00625 return false;
00626 }
00627 else
00628 {
00629
00630 const point dist2(fabs(dist.x()), fabs(dist.y()), fabs(dist.z()));
00631
00632 tightest.min() = sample - dist2;
00633 tightest.max() = sample + dist2;
00634
00635 return true;
00636 }
00637 }
00638
00639
00640
00641 Foam::scalar Foam::octreeDataFace::calcSign
00642 (
00643 const label index,
00644 const point& sample,
00645 point& n
00646 ) const
00647 {
00648 label faceI = meshFaces_[index];
00649
00650 n = mesh_.faceAreas()[faceI];
00651
00652 n /= mag(n) + VSMALL;
00653
00654 vector vec = sample - mesh_.faceCentres()[faceI];
00655
00656 vec /= mag(vec) + VSMALL;
00657
00658 return n & vec;
00659 }
00660
00661
00662
00663 Foam::scalar Foam::octreeDataFace::calcNearest
00664 (
00665 const label index,
00666 const point& sample,
00667 point& nearest
00668 ) const
00669 {
00670 label faceI = meshFaces_[index];
00671
00672 const face& f = mesh_.faces()[faceI];
00673
00674 pointHit nearHit = f.nearestPoint(sample, mesh_.points());
00675
00676 nearest = nearHit.rawPoint();
00677
00678 if (debug & 1)
00679 {
00680 const point& ctr = mesh_.faceCentres()[faceI];
00681
00682 scalar sign = mesh_.faceAreas()[faceI] & (sample - nearest);
00683
00684 Pout<< "octreeDataFace::calcNearest : "
00685 << "sample:" << sample
00686 << " index:" << index
00687 << " faceI:" << faceI
00688 << " ctr:" << ctr
00689 << " sign:" << sign
00690 << " nearest point:" << nearest
00691 << " distance to face:" << nearHit.distance()
00692 << endl;
00693 }
00694 return nearHit.distance();
00695 }
00696
00697
00698
00699 Foam::scalar Foam::octreeDataFace::calcNearest
00700 (
00701 const label index,
00702 const linePointRef& ln,
00703 point& linePt,
00704 point& shapePt
00705 ) const
00706 {
00707 notImplemented
00708 (
00709 "octreeDataFace::calcNearest(const label, const linePointRef&"
00710 ", point&, point&)"
00711 );
00712 return GREAT;
00713 }
00714
00715
00716 void Foam::octreeDataFace::write(Ostream& os, const label index) const
00717 {
00718 os << meshFaces_[index] << " " << allBb_[index];
00719 }
00720
00721
00722