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 "meshSearch.H"
00028 #include "polyMesh.H"
00029 #include "indexedOctree.H"
00030 #include "DynamicList.H"
00031 #include "demandDrivenData.H"
00032 #include "treeDataCell.H"
00033 #include "treeDataFace.H"
00034 #include "treeDataPoint.H"
00035
00036
00037
00038 defineTypeNameAndDebug(Foam::meshSearch, 0);
00039
00040 Foam::scalar Foam::meshSearch::tol_ = 1E-3;
00041
00042
00043
00044
00045 bool Foam::meshSearch::findNearer
00046 (
00047 const point& sample,
00048 const pointField& points,
00049 label& nearestI,
00050 scalar& nearestDistSqr
00051 )
00052 {
00053 bool nearer = false;
00054
00055 forAll(points, pointI)
00056 {
00057 scalar distSqr = magSqr(points[pointI] - sample);
00058
00059 if (distSqr < nearestDistSqr)
00060 {
00061 nearestDistSqr = distSqr;
00062 nearestI = pointI;
00063 nearer = true;
00064 }
00065 }
00066
00067 return nearer;
00068 }
00069
00070
00071 bool Foam::meshSearch::findNearer
00072 (
00073 const point& sample,
00074 const pointField& points,
00075 const labelList& indices,
00076 label& nearestI,
00077 scalar& nearestDistSqr
00078 )
00079 {
00080 bool nearer = false;
00081
00082 forAll(indices, i)
00083 {
00084 label pointI = indices[i];
00085
00086 scalar distSqr = magSqr(points[pointI] - sample);
00087
00088 if (distSqr < nearestDistSqr)
00089 {
00090 nearestDistSqr = distSqr;
00091 nearestI = pointI;
00092 nearer = true;
00093 }
00094 }
00095
00096 return nearer;
00097 }
00098
00099
00100
00101 Foam::label Foam::meshSearch::findNearestCellTree(const point& location) const
00102 {
00103 const indexedOctree<treeDataPoint>& tree = cellCentreTree();
00104
00105 scalar span = tree.bb().mag();
00106
00107 pointIndexHit info = tree.findNearest(location, Foam::sqr(span));
00108
00109 if (!info.hit())
00110 {
00111 info = tree.findNearest(location, Foam::sqr(GREAT));
00112 }
00113 return info.index();
00114 }
00115
00116
00117
00118 Foam::label Foam::meshSearch::findNearestCellLinear(const point& location) const
00119 {
00120 const vectorField& centres = mesh_.cellCentres();
00121
00122 label nearestIndex = 0;
00123 scalar minProximity = magSqr(centres[nearestIndex] - location);
00124
00125 findNearer
00126 (
00127 location,
00128 centres,
00129 nearestIndex,
00130 minProximity
00131 );
00132
00133 return nearestIndex;
00134 }
00135
00136
00137
00138 Foam::label Foam::meshSearch::findNearestCellWalk
00139 (
00140 const point& location,
00141 const label seedCellI
00142 ) const
00143 {
00144 if (seedCellI < 0)
00145 {
00146 FatalErrorIn
00147 (
00148 "meshSearch::findNearestCellWalk(const point&, const label)"
00149 ) << "illegal seedCell:" << seedCellI << exit(FatalError);
00150 }
00151
00152
00153
00154 label curCellI = seedCellI;
00155 scalar distanceSqr = magSqr(mesh_.cellCentres()[curCellI] - location);
00156
00157 bool closer;
00158
00159 do
00160 {
00161
00162 closer = findNearer
00163 (
00164 location,
00165 mesh_.cellCentres(),
00166 mesh_.cellCells()[curCellI],
00167 curCellI,
00168 distanceSqr
00169 );
00170 } while (closer);
00171
00172 return curCellI;
00173 }
00174
00175
00176
00177 Foam::label Foam::meshSearch::findNearestFaceTree(const point& location) const
00178 {
00179
00180 const indexedOctree<treeDataPoint>& tree = cellCentreTree();
00181
00182 scalar span = tree.bb().mag();
00183
00184
00185 pointIndexHit info = tree.findNearest(location, Foam::sqr(span));
00186
00187 if (!info.hit())
00188 {
00189
00190 info = tree.findNearest(location, Foam::sqr(GREAT));
00191 }
00192
00193
00194
00195 const vectorField& centres = mesh_.faceCentres();
00196 const cell& ownFaces = mesh_.cells()[info.index()];
00197
00198 label nearestFaceI = ownFaces[0];
00199 scalar minProximity = magSqr(centres[nearestFaceI] - location);
00200
00201 findNearer
00202 (
00203 location,
00204 centres,
00205 ownFaces,
00206 nearestFaceI,
00207 minProximity
00208 );
00209
00210 return nearestFaceI;
00211 }
00212
00213
00214
00215 Foam::label Foam::meshSearch::findNearestFaceLinear(const point& location) const
00216 {
00217 const vectorField& centres = mesh_.faceCentres();
00218
00219 label nearestFaceI = 0;
00220 scalar minProximity = magSqr(centres[nearestFaceI] - location);
00221
00222 findNearer
00223 (
00224 location,
00225 centres,
00226 nearestFaceI,
00227 minProximity
00228 );
00229
00230 return nearestFaceI;
00231 }
00232
00233
00234
00235 Foam::label Foam::meshSearch::findNearestFaceWalk
00236 (
00237 const point& location,
00238 const label seedFaceI
00239 ) const
00240 {
00241 if (seedFaceI < 0)
00242 {
00243 FatalErrorIn
00244 (
00245 "meshSearch::findNearestFaceWalk(const point&, const label)"
00246 ) << "illegal seedFace:" << seedFaceI << exit(FatalError);
00247 }
00248
00249 const vectorField& centres = mesh_.faceCentres();
00250
00251
00252
00253
00254 label curFaceI = seedFaceI;
00255 scalar distanceSqr = magSqr(centres[curFaceI] - location);
00256
00257 while (true)
00258 {
00259 label betterFaceI = curFaceI;
00260
00261 findNearer
00262 (
00263 location,
00264 centres,
00265 mesh_.cells()[mesh_.faceOwner()[curFaceI]],
00266 betterFaceI,
00267 distanceSqr
00268 );
00269
00270 if (mesh_.isInternalFace(curFaceI))
00271 {
00272 findNearer
00273 (
00274 location,
00275 centres,
00276 mesh_.cells()[mesh_.faceNeighbour()[curFaceI]],
00277 betterFaceI,
00278 distanceSqr
00279 );
00280 }
00281
00282 if (betterFaceI == curFaceI)
00283 {
00284 break;
00285 }
00286
00287 curFaceI = betterFaceI;
00288 }
00289
00290 return curFaceI;
00291 }
00292
00293
00294 Foam::label Foam::meshSearch::findCellLinear(const point& location) const
00295 {
00296 bool cellFound = false;
00297 label n = 0;
00298
00299 label cellI = -1;
00300
00301 while ((!cellFound) && (n < mesh_.nCells()))
00302 {
00303 if (pointInCell(location, n))
00304 {
00305 cellFound = true;
00306 cellI = n;
00307 }
00308 else
00309 {
00310 n++;
00311 }
00312 }
00313 if (cellFound)
00314 {
00315 return cellI;
00316 }
00317 else
00318 {
00319 return -1;
00320 }
00321 }
00322
00323
00324 Foam::label Foam::meshSearch::findNearestBoundaryFaceWalk
00325 (
00326 const point& location,
00327 const label seedFaceI
00328 ) const
00329 {
00330 if (seedFaceI < 0)
00331 {
00332 FatalErrorIn
00333 (
00334 "meshSearch::findNearestBoundaryFaceWalk"
00335 "(const point&, const label)"
00336 ) << "illegal seedFace:" << seedFaceI << exit(FatalError);
00337 }
00338
00339
00340
00341 label curFaceI = seedFaceI;
00342
00343 const face& f = mesh_.faces()[curFaceI];
00344
00345 scalar minDist = f.nearestPoint
00346 (
00347 location,
00348 mesh_.points()
00349 ).distance();
00350
00351 bool closer;
00352
00353 do
00354 {
00355 closer = false;
00356
00357
00358
00359
00360 label lastFaceI = curFaceI;
00361
00362 const labelList& myEdges = mesh_.faceEdges()[curFaceI];
00363
00364 forAll (myEdges, myEdgeI)
00365 {
00366 const labelList& neighbours = mesh_.edgeFaces()[myEdges[myEdgeI]];
00367
00368
00369
00370
00371 forAll(neighbours, nI)
00372 {
00373 label faceI = neighbours[nI];
00374
00375 if
00376 (
00377 (faceI >= mesh_.nInternalFaces())
00378 && (faceI != lastFaceI)
00379 )
00380 {
00381 const face& f = mesh_.faces()[faceI];
00382
00383 pointHit curHit = f.nearestPoint
00384 (
00385 location,
00386 mesh_.points()
00387 );
00388
00389
00390 if (curHit.distance() < minDist)
00391 {
00392 minDist = curHit.distance();
00393 curFaceI = faceI;
00394 closer = true;
00395 }
00396 }
00397 }
00398 }
00399 } while (closer);
00400
00401 return curFaceI;
00402 }
00403
00404
00405 Foam::vector Foam::meshSearch::offset
00406 (
00407 const point& bPoint,
00408 const label bFaceI,
00409 const vector& dir
00410 ) const
00411 {
00412
00413 label ownerCellI = mesh_.faceOwner()[bFaceI];
00414
00415 const point& c = mesh_.cellCentres()[ownerCellI];
00416
00417
00418 scalar typDim = mag(c - bPoint);
00419
00420 return tol_*typDim*dir;
00421 }
00422
00423
00424
00425
00426
00427 Foam::meshSearch::meshSearch(const polyMesh& mesh, const bool faceDecomp)
00428 :
00429 mesh_(mesh),
00430 faceDecomp_(faceDecomp),
00431 cloud_(mesh_, IDLList<passiveParticle>()),
00432 boundaryTreePtr_(NULL),
00433 cellTreePtr_(NULL),
00434 cellCentreTreePtr_(NULL)
00435 {}
00436
00437
00438
00439
00440 Foam::meshSearch::~meshSearch()
00441 {
00442 clearOut();
00443 }
00444
00445
00446
00447
00448 const Foam::indexedOctree<Foam::treeDataFace>& Foam::meshSearch::boundaryTree()
00449 const
00450 {
00451 if (!boundaryTreePtr_)
00452 {
00453
00454
00455
00456
00457
00458 labelList bndFaces(mesh_.nFaces()-mesh_.nInternalFaces());
00459 forAll(bndFaces, i)
00460 {
00461 bndFaces[i] = mesh_.nInternalFaces() + i;
00462 }
00463
00464 treeBoundBox overallBb(mesh_.points());
00465 Random rndGen(123456);
00466 overallBb.extend(rndGen, 1E-4);
00467 overallBb.min() -= point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
00468 overallBb.max() += point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
00469
00470 boundaryTreePtr_ = new indexedOctree<treeDataFace>
00471 (
00472 treeDataFace
00473 (
00474 false,
00475 mesh_,
00476 bndFaces
00477 ),
00478 overallBb,
00479 8,
00480 10,
00481 3.0
00482 );
00483 }
00484
00485 return *boundaryTreePtr_;
00486 }
00487
00488
00489 const Foam::indexedOctree<Foam::treeDataCell>& Foam::meshSearch::cellTree()
00490 const
00491 {
00492 if (!cellTreePtr_)
00493 {
00494
00495
00496
00497
00498 treeBoundBox overallBb(mesh_.points());
00499 Random rndGen(123456);
00500 overallBb.extend(rndGen, 1E-4);
00501 overallBb.min() -= point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
00502 overallBb.max() += point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
00503
00504 cellTreePtr_ = new indexedOctree<treeDataCell>
00505 (
00506 treeDataCell
00507 (
00508 false,
00509 mesh_
00510 ),
00511 overallBb,
00512 8,
00513 10,
00514 3.0
00515 );
00516 }
00517
00518 return *cellTreePtr_;
00519
00520 }
00521
00522
00523 const Foam::indexedOctree<Foam::treeDataPoint>&
00524 Foam::meshSearch::cellCentreTree() const
00525 {
00526 if (!cellCentreTreePtr_)
00527 {
00528
00529
00530
00531
00532 treeBoundBox overallBb(mesh_.cellCentres());
00533 Random rndGen(123456);
00534 overallBb.extend(rndGen, 1E-4);
00535 overallBb.min() -= point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
00536 overallBb.max() += point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
00537
00538 cellCentreTreePtr_ = new indexedOctree<treeDataPoint>
00539 (
00540 treeDataPoint(mesh_.cellCentres()),
00541 overallBb,
00542 10,
00543 10.0,
00544 100.0
00545 );
00546 }
00547
00548 return *cellCentreTreePtr_;
00549 }
00550
00551
00552
00553
00554
00555
00556 bool Foam::meshSearch::pointInCell(const point& p, label cellI) const
00557 {
00558 if (faceDecomp_)
00559 {
00560 const point& ctr = mesh_.cellCentres()[cellI];
00561
00562 vector dir(p - ctr);
00563 scalar magDir = mag(dir);
00564
00565
00566
00567 const labelList& cFaces = mesh_.cells()[cellI];
00568
00569
00570
00571 scalar oldTol = intersection::setPlanarTol(0.0);
00572
00573 forAll(cFaces, i)
00574 {
00575 label faceI = cFaces[i];
00576
00577 const face& f = mesh_.faces()[faceI];
00578
00579 forAll(f, fp)
00580 {
00581 pointHit inter = f.ray
00582 (
00583 ctr,
00584 dir,
00585 mesh_.points(),
00586 intersection::HALF_RAY,
00587 intersection::VECTOR
00588 );
00589
00590 if (inter.hit())
00591 {
00592 scalar dist = inter.distance();
00593
00594 if (dist < magDir)
00595 {
00596
00597 intersection::setPlanarTol(oldTol);
00598
00599 return false;
00600 }
00601 }
00602 }
00603 }
00604
00605 intersection::setPlanarTol(oldTol);
00606
00607
00608 return true;
00609 }
00610 else
00611 {
00612 const labelList& f = mesh_.cells()[cellI];
00613 const labelList& owner = mesh_.faceOwner();
00614 const vectorField& cf = mesh_.faceCentres();
00615 const vectorField& Sf = mesh_.faceAreas();
00616
00617 forAll(f, facei)
00618 {
00619 label nFace = f[facei];
00620 vector proj = p - cf[nFace];
00621 vector normal = Sf[nFace];
00622 if (owner[nFace] == cellI)
00623 {
00624 if ((normal & proj) > 0)
00625 {
00626 return false;
00627 }
00628 }
00629 else
00630 {
00631 if ((normal & proj) < 0)
00632 {
00633 return false;
00634 }
00635 }
00636 }
00637
00638 return true;
00639 }
00640 }
00641
00642
00643 Foam::label Foam::meshSearch::findNearestCell
00644 (
00645 const point& location,
00646 const label seedCellI,
00647 const bool useTreeSearch
00648 ) const
00649 {
00650 if (seedCellI == -1)
00651 {
00652 if (useTreeSearch)
00653 {
00654 return findNearestCellTree(location);
00655 }
00656 else
00657 {
00658 return findNearestCellLinear(location);
00659 }
00660 }
00661 else
00662 {
00663 return findNearestCellWalk(location, seedCellI);
00664 }
00665 }
00666
00667
00668 Foam::label Foam::meshSearch::findNearestFace
00669 (
00670 const point& location,
00671 const label seedFaceI,
00672 const bool useTreeSearch
00673 ) const
00674 {
00675 if (seedFaceI == -1)
00676 {
00677 if (useTreeSearch)
00678 {
00679 return findNearestFaceTree(location);
00680 }
00681 else
00682 {
00683 return findNearestFaceLinear(location);
00684 }
00685 }
00686 else
00687 {
00688 return findNearestFaceWalk(location, seedFaceI);
00689 }
00690 }
00691
00692
00693 Foam::label Foam::meshSearch::findCell
00694 (
00695 const point& location,
00696 const label seedCellI,
00697 const bool useTreeSearch
00698 ) const
00699 {
00700
00701 label nearCellI = findNearestCell(location, seedCellI, useTreeSearch);
00702
00703 if (debug)
00704 {
00705 Pout<< "findCell : nearCellI:" << nearCellI
00706 << " ctr:" << mesh_.cellCentres()[nearCellI]
00707 << endl;
00708 }
00709
00710 if (pointInCell(location, nearCellI))
00711 {
00712 return nearCellI;
00713 }
00714 else
00715 {
00716 if (useTreeSearch)
00717 {
00718
00719 point curPoint = mesh_.cellCentres()[nearCellI];
00720
00721 vector edgeVec = location - curPoint;
00722 edgeVec /= mag(edgeVec);
00723
00724 do
00725 {
00726
00727 passiveParticle tracker(cloud_, curPoint, nearCellI);
00728
00729 if (debug)
00730 {
00731 Pout<< "findCell : tracked from curPoint:" << curPoint
00732 << " nearCellI:" << nearCellI;
00733 }
00734
00735 tracker.track(location);
00736
00737 if (debug)
00738 {
00739 Pout<< " to " << tracker.position()
00740 << " need:" << location
00741 << " onB:" << tracker.onBoundary()
00742 << " cell:" << tracker.cell()
00743 << " face:" << tracker.face() << endl;
00744 }
00745
00746 if (!tracker.onBoundary())
00747 {
00748
00749 return tracker.cell();
00750 }
00751
00752
00753 scalar typDim = sqrt(mag(mesh_.faceAreas()[tracker.face()]));
00754
00755 if ((mag(tracker.position() - location)/typDim) < SMALL)
00756 {
00757 return tracker.cell();
00758 }
00759
00760
00761
00762
00763
00764 curPoint =
00765 tracker.position()
00766 + offset(tracker.position(), tracker.face(), edgeVec);
00767
00768 if (debug)
00769 {
00770 Pout<< "Searching for next boundary from curPoint:"
00771 << curPoint
00772 << " to location:" << location << endl;
00773 }
00774 pointIndexHit curHit = intersection(curPoint, location);
00775 if (debug)
00776 {
00777 Pout<< "Returned from line search with ";
00778 curHit.write(Pout);
00779 Pout<< endl;
00780 }
00781
00782 if (!curHit.hit())
00783 {
00784 return -1;
00785 }
00786 else
00787 {
00788 nearCellI = mesh_.faceOwner()[curHit.index()];
00789 curPoint =
00790 curHit.hitPoint()
00791 + offset(curHit.hitPoint(), curHit.index(), edgeVec);
00792 }
00793 }
00794 while(true);
00795 }
00796 else
00797 {
00798 return findCellLinear(location);
00799 }
00800 }
00801 return -1;
00802 }
00803
00804
00805 Foam::label Foam::meshSearch::findNearestBoundaryFace
00806 (
00807 const point& location,
00808 const label seedFaceI,
00809 const bool useTreeSearch
00810 ) const
00811 {
00812 if (seedFaceI == -1)
00813 {
00814 if (useTreeSearch)
00815 {
00816 const indexedOctree<treeDataFace>& tree = boundaryTree();
00817
00818 scalar span = tree.bb().mag();
00819
00820 pointIndexHit info = boundaryTree().findNearest
00821 (
00822 location,
00823 Foam::sqr(span)
00824 );
00825
00826 if (!info.hit())
00827 {
00828 info = boundaryTree().findNearest
00829 (
00830 location,
00831 Foam::sqr(GREAT)
00832 );
00833 }
00834
00835 return tree.shapes().faceLabels()[info.index()];
00836 }
00837 else
00838 {
00839 scalar minDist = GREAT;
00840
00841 label minFaceI = -1;
00842
00843 for
00844 (
00845 label faceI = mesh_.nInternalFaces();
00846 faceI < mesh_.nFaces();
00847 faceI++
00848 )
00849 {
00850 const face& f = mesh_.faces()[faceI];
00851
00852 pointHit curHit =
00853 f.nearestPoint
00854 (
00855 location,
00856 mesh_.points()
00857 );
00858
00859 if (curHit.distance() < minDist)
00860 {
00861 minDist = curHit.distance();
00862 minFaceI = faceI;
00863 }
00864 }
00865 return minFaceI;
00866 }
00867 }
00868 else
00869 {
00870 return findNearestBoundaryFaceWalk(location, seedFaceI);
00871 }
00872 }
00873
00874
00875 Foam::pointIndexHit Foam::meshSearch::intersection
00876 (
00877 const point& pStart,
00878 const point& pEnd
00879 ) const
00880 {
00881 pointIndexHit curHit = boundaryTree().findLine(pStart, pEnd);
00882
00883 if (curHit.hit())
00884 {
00885
00886 curHit.setIndex(boundaryTree().shapes().faceLabels()[curHit.index()]);
00887 }
00888 return curHit;
00889 }
00890
00891
00892 Foam::List<Foam::pointIndexHit> Foam::meshSearch::intersections
00893 (
00894 const point& pStart,
00895 const point& pEnd
00896 ) const
00897 {
00898 DynamicList<pointIndexHit> hits;
00899
00900 vector edgeVec = pEnd - pStart;
00901 edgeVec /= mag(edgeVec);
00902
00903 point pt = pStart;
00904
00905 pointIndexHit bHit;
00906 do
00907 {
00908 bHit = intersection(pt, pEnd);
00909
00910 if (bHit.hit())
00911 {
00912 hits.append(bHit);
00913
00914 const vector& area = mesh_.faceAreas()[bHit.index()];
00915
00916 scalar typDim = Foam::sqrt(mag(area));
00917
00918 if ((mag(bHit.hitPoint() - pEnd)/typDim) < SMALL)
00919 {
00920 break;
00921 }
00922
00923
00924
00925
00926 pt =
00927 bHit.hitPoint()
00928 + offset(bHit.hitPoint(), bHit.index(), edgeVec);
00929 }
00930
00931 } while(bHit.hit());
00932
00933
00934 hits.shrink();
00935
00936 return hits;
00937 }
00938
00939
00940 bool Foam::meshSearch::isInside(const point& p) const
00941 {
00942 return
00943 boundaryTree().getVolumeType(p)
00944 == indexedOctree<treeDataFace>::INSIDE;
00945 }
00946
00947
00948
00949 void Foam::meshSearch::clearOut()
00950 {
00951 deleteDemandDrivenData(boundaryTreePtr_);
00952 deleteDemandDrivenData(cellTreePtr_);
00953 deleteDemandDrivenData(cellCentreTreePtr_);
00954 }
00955
00956
00957 void Foam::meshSearch::correct()
00958 {
00959 clearOut();
00960 }
00961
00962
00963