1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36 scalar line<point2D, const point2D&>::nearestDist
38 const line<point2D, const point2D&>& e,
43 vector2D u = end()-start();
44 vector2D v = e.end()-e.start();
45 vector2D w = start()-e.start();
49 if (Foam::mag(d) > VSMALL)
51 scalar s = v.perp(w) / d;
57 else if (s >= (1-SMALL))
67 scalar t = u.perp(w) / d;
73 else if (t >= (1-SMALL))
79 edgePt = e.start()+t*v;
84 // Parallel lines. Find overlap of both lines by projecting onto
85 // direction vector (now equal for both lines).
87 scalar edge0 = e.start() & u;
88 scalar edge1 = e.end() & u;
89 bool edgeOrder = edge0 < edge1;
91 scalar minEdge = (edgeOrder ? edge0 : edge1);
92 scalar maxEdge = (edgeOrder ? edge1 : edge0);
93 const point2D& minEdgePt = (edgeOrder ? e.start() : e.end());
94 const point2D& maxEdgePt = (edgeOrder ? e.end() : e.start());
96 scalar this0 = start() & u;
97 scalar this1 = end() & u;
98 bool thisOrder = this0 < this1;
100 scalar minThis = min(this0, this1);
101 scalar maxThis = max(this1, this0);
102 const point2D& minThisPt = (thisOrder ? start() : end());
103 const point2D& maxThisPt = (thisOrder ? end() : start());
105 if (maxEdge < minThis)
107 // edge completely below *this
111 else if (maxEdge < maxThis)
113 // maxEdge inside interval of *this
115 thisPt = nearestDist(edgePt).rawPoint();
119 // maxEdge outside. Check if minEdge inside.
120 if (minEdge < minThis)
122 // Edge completely envelops this. Take any this point and
123 // determine nearest on edge.
125 edgePt = e.nearestDist(thisPt).rawPoint();
127 else if (minEdge < maxThis)
129 // minEdge inside this interval.
131 thisPt = nearestDist(edgePt).rawPoint();
135 // minEdge outside this interval
142 return Foam::mag(thisPt - edgePt);
146 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
148 } // End namespace Foam
150 // ************************************************************************* //