OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

PrimitivePatchProjectPoints.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     For every point on the patch find the closest face on the target side.
00027     Return a target face label for each patch point
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "boolList.H"
00032 #include "PointHit.H"
00033 #include "objectHit.H"
00034 #include "bandCompression.H"
00035 
00036 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00037 
00038 template
00039 <
00040     class Face,
00041     template<class> class FaceList,
00042     class PointField,
00043     class PointType
00044 >
00045 
00046 template <class ToPatch>
00047 Foam::List<Foam::objectHit>
00048 Foam::PrimitivePatch<Face, FaceList, PointField, PointType>::
00049 projectPoints
00050 (
00051     const ToPatch& targetPatch,
00052     const Field<PointType>& projectionDirection,
00053     const intersection::algorithm alg,
00054     const intersection::direction dir
00055 ) const
00056 {
00057     // The current patch is slave, i.e. it is being projected onto the target
00058 
00059     if (projectionDirection.size() != nPoints())
00060     {
00061         FatalErrorIn
00062         (
00063             "PrimitivePatch<Face, FaceList, PointField, PointType>::"
00064             "projectPoints(const PrimitivePatch& "
00065             ", const Field<PointType>&) const"
00066         )   << "Projection direction field does not correspond to "
00067             << "patch points." << endl
00068             << "Size: " << projectionDirection.size()
00069             << " Number of points: " << nPoints()
00070             << abort(FatalError);
00071     }
00072 
00073     const labelList& slavePointOrder = localPointOrder();
00074 
00075     const labelList& slaveMeshPoints = meshPoints();
00076 
00077     // Result
00078     List<objectHit> result(nPoints());
00079 
00080     const labelListList& masterFaceFaces = targetPatch.faceFaces();
00081 
00082     const ToPatch& masterFaces = targetPatch;
00083 
00084     const Field<PointType>& masterPoints = targetPatch.points();
00085 
00086     // Estimate face centre of target side
00087     Field<PointType> masterFaceCentres(targetPatch.size());
00088 
00089     forAll (masterFaceCentres, faceI)
00090     {
00091         masterFaceCentres[faceI] =
00092             average(masterFaces[faceI].points(masterPoints));
00093     }
00094 
00095     // Algorithm:
00096     // Loop through all points of the slave side. For every point find the
00097     // radius for the current contact face. If the contact point falls inside
00098     // the face and the radius is smaller than for all neighbouring faces,
00099     // the contact is found. If not, visit the neighbour closest to the
00100     // calculated contact point. If a single master face is visited more than
00101     // twice, initiate n-squared search.
00102 
00103     label curFace = 0;
00104     label nNSquaredSearches = 0;
00105 
00106     forAll (slavePointOrder, pointI)
00107     {
00108         // Pick up slave point and direction
00109         const label curLocalPointLabel = slavePointOrder[pointI];
00110 
00111         const PointType& curPoint =
00112             points_[slaveMeshPoints[curLocalPointLabel]];
00113 
00114         const PointType& curProjectionDir =
00115             projectionDirection[curLocalPointLabel];
00116 
00117         bool closer;
00118 
00119         boolList visitedTargetFace(targetPatch.size(), false);
00120         bool doNSquaredSearch = false;
00121 
00122         bool foundEligible = false;
00123 
00124         scalar sqrDistance = GREAT;
00125 
00126         // Force the full search for the first point to ensure good
00127         // starting face
00128         if (pointI == 0)
00129         {
00130             doNSquaredSearch = true;
00131         }
00132         else
00133         {
00134             do
00135             {
00136                 closer = false;
00137                 doNSquaredSearch = false;
00138 
00139                 // Calculate intersection with curFace
00140                 PointHit<PointType> curHit =
00141                     masterFaces[curFace].ray
00142                     (
00143                         curPoint,
00144                         curProjectionDir,
00145                         masterPoints,
00146                         alg,
00147                         dir
00148                     );
00149 
00150                 visitedTargetFace[curFace] = true;
00151 
00152                 if (curHit.hit())
00153                 {
00154                     result[curLocalPointLabel] = objectHit(true, curFace);
00155 
00156                     break;
00157                 }
00158                 else
00159                 {
00160                     // If a new miss is eligible, it is closer than
00161                     // any previous eligible miss (due to surface walk)
00162 
00163                     // Only grab the miss if it is eligible
00164                     if (curHit.eligibleMiss())
00165                     {
00166                         foundEligible = true;
00167                         result[curLocalPointLabel] = objectHit(false, curFace);
00168                     }
00169 
00170                     // Find the next likely face for intersection
00171 
00172                     // Calculate the miss point on the plane of the
00173                     // face.  This is cooked (illogical!) for fastest
00174                     // surface walk.
00175                     //
00176                     PointType missPlanePoint =
00177                         curPoint + curProjectionDir*curHit.distance();
00178 
00179                     const labelList& masterNbrs = masterFaceFaces[curFace];
00180 
00181                     sqrDistance =
00182                         magSqr(missPlanePoint - masterFaceCentres[curFace]);
00183 
00184                     forAll (masterNbrs, nbrI)
00185                     {
00186                         if
00187                         (
00188                             magSqr
00189                             (
00190                                 missPlanePoint
00191                               - masterFaceCentres[masterNbrs[nbrI]]
00192                             )
00193                          <= sqrDistance
00194                         )
00195                         {
00196                             closer = true;
00197                             curFace = masterNbrs[nbrI];
00198                         }
00199                     }
00200 
00201                     if (visitedTargetFace[curFace])
00202                     {
00203                         // This face has already been visited.
00204                         // Execute n-squared search
00205                         doNSquaredSearch = true;
00206                         break;
00207                     }
00208                 }
00209 
00210                 if (debug) Info<< ".";
00211             } while (closer);
00212         }
00213 
00214         if
00215         (
00216             doNSquaredSearch || !foundEligible
00217         )
00218         {
00219             nNSquaredSearches++;
00220 
00221             if (debug)
00222             {
00223                 Info<< "p " << curLocalPointLabel << ": ";
00224             }
00225 
00226             result[curLocalPointLabel] = objectHit(false, -1);
00227             scalar minDistance = GREAT;
00228 
00229             forAll (masterFaces, faceI)
00230             {
00231                 PointHit<PointType> curHit =
00232                     masterFaces[faceI].ray
00233                     (
00234                         curPoint,
00235                         curProjectionDir,
00236                         masterPoints,
00237                         alg,
00238                         dir
00239                     );
00240 
00241                 if (curHit.hit())
00242                 {
00243                     result[curLocalPointLabel] = objectHit(true, faceI);
00244                     curFace = faceI;
00245 
00246                     break;
00247                 }
00248                 else if (curHit.eligibleMiss())
00249                 {
00250                     // Calculate min distance
00251                     scalar missDist =
00252                         Foam::mag(curHit.missPoint() - curPoint);
00253 
00254                     if (missDist < minDistance)
00255                     {
00256                         minDistance = missDist;
00257 
00258                         result[curLocalPointLabel] = objectHit(false, faceI);
00259                         curFace = faceI;
00260                     }
00261                 }
00262             }
00263 
00264             if (debug)
00265             {
00266                 Info << result[curLocalPointLabel] << nl;
00267             }
00268         }
00269         else
00270         {
00271             if (debug) Info<< "x";
00272         }
00273     }
00274 
00275     if (debug)
00276     {
00277         Info<< nl << "Executed " << nNSquaredSearches
00278             << " n-squared searches out of total of "
00279             << nPoints() << endl;
00280     }
00281 
00282     return result;
00283 }
00284 
00285 
00286 template
00287 <
00288     class Face,
00289     template<class> class FaceList,
00290     class PointField,
00291     class PointType
00292 >
00293 
00294 template <class ToPatch>
00295 Foam::List<Foam::objectHit>
00296 Foam::PrimitivePatch<Face, FaceList, PointField, PointType>::
00297 projectFaceCentres
00298 (
00299     const ToPatch& targetPatch,
00300     const Field<PointType>& projectionDirection,
00301     const intersection::algorithm alg,
00302     const intersection::direction dir
00303 ) const
00304 {
00305     // The current patch is slave, i.e. it is being projected onto the target
00306 
00307     if (projectionDirection.size() != this->size())
00308     {
00309         FatalErrorIn
00310         (
00311             "labelList PrimitivePatch<Face, FaceList, PointField, PointType>::"
00312             "projectFaceCentres(const PrimitivePatch& "
00313             ", const Field<PointType>&) const"
00314         )   << "Projection direction field does not correspond to patch faces."
00315             << endl << "Size: " << projectionDirection.size()
00316             << " Number of points: " << this->size()
00317             << abort(FatalError);
00318     }
00319 
00320     labelList slaveFaceOrder = bandCompression(faceFaces());
00321 
00322     // calculate master face centres
00323     Field<PointType> masterFaceCentres(targetPatch.size());
00324 
00325     const labelListList& masterFaceFaces = targetPatch.faceFaces();
00326 
00327     const ToPatch& masterFaces = targetPatch;
00328 
00329     const typename ToPatch::PointFieldType& masterPoints = targetPatch.points();
00330 
00331     forAll (masterFaceCentres, faceI)
00332     {
00333         masterFaceCentres[faceI] =
00334             masterFaces[faceI].centre(masterPoints);
00335     }
00336 
00337     // Result
00338     List<objectHit> result(this->size());
00339 
00340     const PrimitivePatch<Face, FaceList, PointField, PointType>& slaveFaces = *this;
00341     const PointField& slaveGlobalPoints = points();
00342 
00343     // Algorithm:
00344     // Loop through all points of the slave side. For every point find the
00345     // radius for the current contact face. If the contact point falls inside
00346     // the face and the radius is smaller than for all neighbouring faces,
00347     // the contact is found. If not, visit the neighbour closest to the
00348     // calculated contact point. If a single master face is visited more than
00349     // twice, initiate n-squared search.
00350 
00351     label curFace = 0;
00352     label nNSquaredSearches = 0;
00353 
00354     forAll (slaveFaceOrder, faceI)
00355     {
00356         // pick up slave point and direction
00357         const label curLocalFaceLabel = slaveFaceOrder[faceI];
00358 
00359         const point& curFaceCentre =
00360             slaveFaces[curLocalFaceLabel].centre(slaveGlobalPoints);
00361 
00362         const vector& curProjectionDir =
00363             projectionDirection[curLocalFaceLabel];
00364 
00365         bool closer;
00366 
00367         boolList visitedTargetFace(targetPatch.size(), false);
00368         bool doNSquaredSearch = false;
00369 
00370         bool foundEligible = false;
00371 
00372         scalar sqrDistance = GREAT;
00373 
00374         // Force the full search for the first point to ensure good
00375         // starting face
00376         if (faceI == 0)
00377         {
00378             doNSquaredSearch = true;
00379         }
00380         else
00381         {
00382             do
00383             {
00384                 closer = false;
00385                 doNSquaredSearch = false;
00386 
00387                 // Calculate intersection with curFace
00388                 PointHit<PointType> curHit =
00389                     masterFaces[curFace].ray
00390                     (
00391                         curFaceCentre,
00392                         curProjectionDir,
00393                         masterPoints,
00394                         alg,
00395                         dir
00396                     );
00397 
00398                 visitedTargetFace[curFace] = true;
00399 
00400                 if (curHit.hit())
00401                 {
00402                     result[curLocalFaceLabel] = objectHit(true, curFace);
00403 
00404                     break;
00405                 }
00406                 else
00407                 {
00408                     // If a new miss is eligible, it is closer than
00409                     // any previous eligible miss (due to surface walk)
00410 
00411                     // Only grab the miss if it is eligible
00412                     if (curHit.eligibleMiss())
00413                     {
00414                         foundEligible = true;
00415                         result[curLocalFaceLabel] = objectHit(false, curFace);
00416                     }
00417 
00418                     // Find the next likely face for intersection
00419 
00420                     // Calculate the miss point.  This is
00421                     // cooked (illogical!) for fastest surface walk.
00422                     //
00423                     PointType missPlanePoint =
00424                         curFaceCentre + curProjectionDir*curHit.distance();
00425 
00426                     sqrDistance =
00427                         magSqr(missPlanePoint - masterFaceCentres[curFace]);
00428 
00429                     const labelList& masterNbrs = masterFaceFaces[curFace];
00430 
00431                     forAll (masterNbrs, nbrI)
00432                     {
00433                         if
00434                         (
00435                             magSqr
00436                             (
00437                                 missPlanePoint
00438                               - masterFaceCentres[masterNbrs[nbrI]]
00439                             )
00440                          <= sqrDistance
00441                         )
00442                         {
00443                             closer = true;
00444                             curFace = masterNbrs[nbrI];
00445                         }
00446                     }
00447 
00448                     if (visitedTargetFace[curFace])
00449                     {
00450                         // This face has already been visited.
00451                         // Execute n-squared search
00452                         doNSquaredSearch = true;
00453                         break;
00454                     }
00455                 }
00456 
00457                 if (debug) Info<< ".";
00458             } while (closer);
00459         }
00460 
00461         if (doNSquaredSearch || !foundEligible)
00462         {
00463             nNSquaredSearches++;
00464 
00465             if (debug)
00466             {
00467                 Info<< "p " << curLocalFaceLabel << ": ";
00468             }
00469 
00470             result[curLocalFaceLabel] = objectHit(false, -1);
00471             scalar minDistance = GREAT;
00472 
00473             forAll (masterFaces, faceI)
00474             {
00475                 PointHit<PointType> curHit =
00476                     masterFaces[faceI].ray
00477                     (
00478                         curFaceCentre,
00479                         curProjectionDir,
00480                         masterPoints,
00481                         alg,
00482                         dir
00483                     );
00484 
00485                 if (curHit.hit())
00486                 {
00487                     result[curLocalFaceLabel] = objectHit(true, faceI);
00488                     curFace = faceI;
00489 
00490                     break;
00491                 }
00492                 else if (curHit.eligibleMiss())
00493                 {
00494                     // Calculate min distance
00495                     scalar missDist =
00496                         Foam::mag(curHit.missPoint() - curFaceCentre);
00497 
00498                     if (missDist < minDistance)
00499                     {
00500                         minDistance = missDist;
00501 
00502                         result[curLocalFaceLabel] = objectHit(false, faceI);
00503                         curFace = faceI;
00504                     }
00505                 }
00506             }
00507 
00508             if (debug)
00509             {
00510                 Info << result[curLocalFaceLabel] << nl;
00511             }
00512         }
00513         else
00514         {
00515             if (debug) Info<< "x";
00516         }
00517     }
00518 
00519     if (debug)
00520     {
00521         Info<< nl << "Executed " << nNSquaredSearches
00522             << " n-squared searches out of total of "
00523             << this->size() << endl;
00524     }
00525 
00526     return result;
00527 }
00528 
00529 
00530 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd