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

line.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "line.H"
00028 
00029 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033 
00034 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00035 
00036 template<>
00037 scalar line<point2D, const point2D&>::nearestDist
00038 (
00039     const line<point2D, const point2D&>& e,
00040     point2D& thisPt,
00041     point2D& edgePt
00042 ) const
00043 {
00044     vector2D u = end()-start();
00045     vector2D v = e.end()-e.start();
00046     vector2D w = start()-e.start();
00047 
00048     scalar d = u.perp(v);
00049 
00050     if (Foam::mag(d) > VSMALL)
00051     {
00052         scalar s = v.perp(w) / d;
00053 
00054         if (s <= SMALL)
00055         {
00056             thisPt = start();
00057         }
00058         else if (s >= (1-SMALL))
00059         {
00060             thisPt = end();
00061         }
00062         else
00063         {
00064             thisPt = start()+s*u;
00065         }
00066 
00067 
00068         scalar t = u.perp(w) / d;
00069 
00070         if (t <= SMALL)
00071         {
00072             edgePt = e.start();
00073         }
00074         else if (t >= (1-SMALL))
00075         {
00076             edgePt = e.end();
00077         }
00078         else
00079         {
00080             edgePt = e.start()+t*v;
00081         }
00082     }
00083     else
00084     {
00085         // Parallel lines. Find overlap of both lines by projecting onto
00086         // direction vector (now equal for both lines).
00087 
00088         scalar edge0 = e.start() & u;
00089         scalar edge1 = e.end() & u;
00090         bool edgeOrder = edge0 < edge1;
00091 
00092         scalar minEdge = (edgeOrder ? edge0 : edge1);
00093         scalar maxEdge = (edgeOrder ? edge1 : edge0);
00094         const point2D& minEdgePt = (edgeOrder ? e.start() : e.end());
00095         const point2D& maxEdgePt = (edgeOrder ? e.end() : e.start());
00096 
00097         scalar this0 = start() & u;
00098         scalar this1 = end() & u;
00099         bool thisOrder = this0 < this1;
00100 
00101         scalar minThis = min(this0, this1);
00102         scalar maxThis = max(this1, this0);
00103         const point2D& minThisPt = (thisOrder ? start() : end());
00104         const point2D& maxThisPt = (thisOrder ? end() : start());
00105 
00106         if (maxEdge < minThis)
00107         {
00108             // edge completely below *this
00109             edgePt = maxEdgePt;
00110             thisPt = minThisPt;
00111         }
00112         else if (maxEdge < maxThis)
00113         {
00114             // maxEdge inside interval of *this
00115             edgePt = maxEdgePt;
00116             thisPt = nearestDist(edgePt).rawPoint();
00117         }
00118         else
00119         {
00120             // maxEdge outside. Check if minEdge inside.
00121             if (minEdge < minThis)
00122             {
00123                 // Edge completely envelops this. Take any this point and
00124                 // determine nearest on edge.
00125                 thisPt = minThisPt;
00126                 edgePt = e.nearestDist(thisPt).rawPoint();
00127             }
00128             else if (minEdge < maxThis)
00129             {
00130                 // minEdge inside this interval.
00131                 edgePt = minEdgePt;
00132                 thisPt = nearestDist(edgePt).rawPoint();
00133             }
00134             else
00135             {
00136                 // minEdge outside this interval
00137                 edgePt = minEdgePt;
00138                 thisPt = maxThisPt;
00139             }
00140         }
00141     }
00142 
00143     return Foam::mag(thisPt - edgePt);
00144 }
00145 
00146 
00147 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00148 
00149 } // End namespace Foam
00150 
00151 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd