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

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