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

faceIntersection.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     Return intersection of a line with the face
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "face.H"
00031 #include "pointHit.H"
00032 #include "triPointRef.H"
00033 #include "line.H"
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 // Return potential intersection with face with a ray starting
00038 // at p, direction n (does not need to be normalized)
00039 // Does face-center decomposition and returns triangle intersection
00040 // point closest to p.
00041 
00042 // In case of miss the point is the nearest point intersection of the
00043 // face plane and the ray and the distance is the distance between the
00044 // intersection point and the nearest point on the face
00045 
00046 Foam::pointHit Foam::face::ray
00047 (
00048     const point& p,
00049     const vector& n,
00050     const pointField& meshPoints,
00051     const intersection::algorithm alg,
00052     const intersection::direction dir
00053 ) const
00054 {
00055     point ctr = Foam::average(points(meshPoints));
00056 
00057     scalar nearestHitDist = GREAT;
00058 
00059     scalar nearestMissDist = GREAT;
00060     bool eligible = false;
00061 
00062     // Initialize to miss, distance = GREAT
00063     pointHit nearest(p);
00064 
00065     const labelList& f = *this;
00066 
00067     label nPoints = size();
00068 
00069     point nextPoint = ctr;
00070 
00071     for (label pI = 0; pI < nPoints; pI++)
00072     {
00073         nextPoint = meshPoints[f[fcIndex(pI)]];
00074 
00075         // Note: for best accuracy, centre point always comes last
00076         //
00077         pointHit curHit = triPointRef
00078         (
00079             meshPoints[f[pI]],
00080             nextPoint,
00081             ctr
00082         ).ray(p, n, alg, dir);
00083 
00084         if (curHit.hit())
00085         {
00086             if (Foam::mag(curHit.distance()) < Foam::mag(nearestHitDist))
00087             {
00088                 nearestHitDist = curHit.distance();
00089                 nearest.setHit();
00090                 nearest.setPoint(curHit.hitPoint());
00091             }
00092         }
00093         else if (!nearest.hit())
00094         {
00095             // Miss and no hit yet. Update miss statistics.
00096             if (curHit.eligibleMiss())
00097             {
00098                 eligible = true;
00099 
00100                 // Miss distance is the distance between the plane intersection
00101                 // point and the nearest point of the triangle
00102                 scalar missDist =
00103                     Foam::mag
00104                     (
00105                         p + curHit.distance()*n
00106                       - curHit.missPoint()
00107                     );
00108 
00109                 if (missDist < nearestMissDist)
00110                 {
00111                     nearestMissDist = missDist;
00112                     nearest.setDistance(curHit.distance());
00113                     nearest.setPoint(curHit.missPoint());
00114                 }
00115             }
00116         }
00117     }
00118 
00119     if (nearest.hit())
00120     {
00121         nearest.setDistance(nearestHitDist);
00122     }
00123     else
00124     {
00125         // Haven't hit a single face triangle
00126         nearest.setMiss(eligible);
00127     }
00128 
00129     return nearest;
00130 }
00131 
00132 
00133 Foam::pointHit Foam::face::intersection
00134 (
00135     const point& p,
00136     const vector& q,
00137     const point& ctr,
00138     const pointField& meshPoints,
00139     const intersection::algorithm alg,
00140     const scalar tol
00141 ) const
00142 {
00143     scalar nearestHitDist = VGREAT;
00144 
00145     // Initialize to miss, distance = GREAT
00146     pointHit nearest(p);
00147 
00148     const labelList& f = *this;
00149 
00150     forAll(f, pI)
00151     {
00152         // Note: for best accuracy, centre point always comes last
00153         pointHit curHit = triPointRef
00154         (
00155             meshPoints[f[pI]],
00156             meshPoints[f[fcIndex(pI)]],
00157             ctr
00158         ).intersection(p, q, alg, tol);
00159 
00160         if (curHit.hit())
00161         {
00162             if (Foam::mag(curHit.distance()) < nearestHitDist)
00163             {
00164                 nearestHitDist = Foam::mag(curHit.distance());
00165                 nearest.setHit();
00166                 nearest.setPoint(curHit.hitPoint());
00167             }
00168         }
00169     }
00170 
00171     if (nearest.hit())
00172     {
00173         nearest.setDistance(nearestHitDist);
00174     }
00175 
00176     return nearest;
00177 }
00178 
00179 
00180 Foam::pointHit Foam::face::nearestPoint
00181 (
00182     const point& p,
00183     const pointField& meshPoints
00184 ) const
00185 {
00186     const face& f = *this;
00187     point ctr = centre(meshPoints);
00188 
00189     // Initialize to miss, distance=GREAT
00190     pointHit nearest(p);
00191 
00192     label nPoints = f.size();
00193 
00194     point nextPoint = ctr;
00195 
00196     for (label pI = 0; pI < nPoints; pI++)
00197     {
00198         nextPoint = meshPoints[f[fcIndex(pI)]];
00199 
00200         // Note: for best accuracy, centre point always comes last
00201         //
00202         triPointRef tri
00203         (
00204             meshPoints[f[pI]],
00205             nextPoint,
00206             ctr
00207         );
00208 
00209         pointHit curHit = tri.nearestPoint(p);
00210 
00211         if (Foam::mag(curHit.distance()) < Foam::mag(nearest.distance()))
00212         {
00213             nearest.setDistance(curHit.distance());
00214 
00215             if (curHit.hit())
00216             {
00217                 nearest.setHit();
00218                 nearest.setPoint(curHit.hitPoint());
00219             }
00220             else
00221             {
00222                 // In nearest point, miss is always eligible
00223                 nearest.setMiss(true);
00224                 nearest.setPoint(curHit.missPoint());
00225             }
00226         }
00227     }
00228 
00229     return nearest;
00230 }
00231 
00232 
00233 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd