FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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