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

lineI.H

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 "IOstreams.H"
00028 
00029 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 template<class Point, class PointRef>
00037 inline line<Point, PointRef>::line(const Point& start, const Point& end)
00038 :
00039     a_(start),
00040     b_(end)
00041 {}
00042 
00043 
00044 template<class Point, class PointRef>
00045 inline line<Point, PointRef>::line(Istream& is)
00046 {
00047     // Read beginning of line point pair
00048     is.readBegin("line");
00049 
00050     is >> a_ >> b_;
00051 
00052     // Read end of line point pair
00053     is.readEnd("line");
00054 
00055     // Check state of Istream
00056     is.check("line::line(Istream& is)");
00057 }
00058 
00059 
00060 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00061 
00062 template<class Point, class PointRef>
00063 inline PointRef line<Point, PointRef>::start() const
00064 {
00065     return a_;
00066 }
00067 
00068 template<class Point, class PointRef>
00069 inline PointRef line<Point, PointRef>::end() const
00070 {
00071     return b_;
00072 }
00073 
00074 
00075 template<class Point, class PointRef>
00076 inline Point line<Point, PointRef>::centre() const
00077 {
00078     return 0.5*(a_ + b_);
00079 }
00080 
00081 
00082 template<class Point, class PointRef>
00083 inline scalar line<Point, PointRef>::mag() const
00084 {
00085     return ::Foam::mag(vec());
00086 }
00087 
00088 
00089 template<class Point, class PointRef>
00090 inline Point line<Point, PointRef>::vec() const
00091 {
00092     return b_ - a_;
00093 }
00094 
00095 
00096 template<class Point, class PointRef>
00097 PointHit<Point> line<Point, PointRef>::nearestDist(const Point& p) const
00098 {
00099     Point v = vec();
00100 
00101     Point w(p - a_);
00102 
00103     scalar c1 = v & w;
00104 
00105     if (c1 <= 0)
00106     {
00107         return PointHit<Point>(false, a_, Foam::mag(p - a_), true);
00108     }
00109 
00110     scalar c2 = v & v;
00111 
00112     if (c2 <= c1)
00113     {
00114         return PointHit<Point>(false, b_, Foam::mag(p - b_), true);
00115     }
00116 
00117     scalar b = c1/c2;
00118 
00119     Point pb(a_ + b*v);
00120 
00121     return PointHit<Point>(true, pb, Foam::mag(p - pb), false);
00122 }
00123 
00124 
00125 template<class Point, class PointRef>
00126 scalar line<Point, PointRef>::nearestDist
00127 (
00128     const line<Point, const Point&>& edge,
00129     Point& thisPt,
00130     Point& edgePt
00131 ) const
00132 {
00133     // From Mathworld Line-Line distance/(Gellert et al. 1989, p. 538).
00134     Point a(end() - start());
00135     Point b(edge.end() - edge.start());
00136     Point c(edge.start() - start());
00137 
00138     Point crossab = a ^ b;
00139     scalar magCrossSqr = magSqr(crossab);
00140 
00141     if (magCrossSqr > VSMALL)
00142     {
00143         scalar s = ((c ^ b) & crossab)/magCrossSqr;
00144         scalar t = ((c ^ a) & crossab)/magCrossSqr;
00145 
00146         // Check for end points outside of range 0..1
00147         if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
00148         {
00149             // Both inside range 0..1
00150             thisPt = start() + a*s;
00151             edgePt = edge.start() + b*t;
00152         }
00153         else
00154         {
00155             // Do brute force. Distance of everything to everything.
00156             // Can quite possibly be improved!
00157 
00158             // From edge endpoints to *this
00159             PointHit<Point> this0(nearestDist(edge.start()));
00160             PointHit<Point> this1(nearestDist(edge.end()));
00161             scalar thisDist = min(this0.distance(), this1.distance());
00162 
00163             // From *this to edge
00164             PointHit<Point> edge0(edge.nearestDist(start()));
00165             PointHit<Point> edge1(edge.nearestDist(end()));
00166             scalar edgeDist = min(edge0.distance(), edge1.distance());
00167 
00168             if (thisDist < edgeDist)
00169             {
00170                 if (this0.distance() < this1.distance())
00171                 {
00172                     thisPt = this0.rawPoint();
00173                     edgePt = edge.start();
00174                 }
00175                 else
00176                 {
00177                     thisPt = this1.rawPoint();
00178                     edgePt = edge.end();
00179                 }
00180             }
00181             else
00182             {
00183                 if (edge0.distance() < edge1.distance())
00184                 {
00185                     thisPt = start();
00186                     edgePt = edge0.rawPoint();
00187                 }
00188                 else
00189                 {
00190                     thisPt = end();
00191                     edgePt = edge1.rawPoint();
00192                 }
00193             }
00194         }
00195     }
00196     else
00197     {
00198         // Parallel lines. Find overlap of both lines by projecting onto
00199         // direction vector (now equal for both lines).
00200 
00201         scalar edge0 = edge.start() & a;
00202         scalar edge1 = edge.end() & a;
00203         bool edgeOrder = edge0 < edge1;
00204 
00205         scalar minEdge = (edgeOrder ? edge0 : edge1);
00206         scalar maxEdge = (edgeOrder ? edge1 : edge0);
00207         const Point& minEdgePt = (edgeOrder ? edge.start() : edge.end());
00208         const Point& maxEdgePt = (edgeOrder ? edge.end() : edge.start());
00209 
00210         scalar this0 = start() & a;
00211         scalar this1 = end() & a;
00212         bool thisOrder = this0 < this1;
00213 
00214         scalar minThis = min(this0, this1);
00215         scalar maxThis = max(this1, this0);
00216         const Point& minThisPt = (thisOrder ? start() : end());
00217         const Point& maxThisPt = (thisOrder ? end() : start());
00218 
00219         if (maxEdge < minThis)
00220         {
00221             // edge completely below *this
00222             edgePt = maxEdgePt;
00223             thisPt = minThisPt;
00224         }
00225         else if (maxEdge < maxThis)
00226         {
00227             // maxEdge inside interval of *this
00228             edgePt = maxEdgePt;
00229             thisPt = nearestDist(edgePt).rawPoint();
00230         }
00231         else
00232         {
00233             // maxEdge outside. Check if minEdge inside.
00234             if (minEdge < minThis)
00235             {
00236                 // Edge completely envelops this. Take any this point and
00237                 // determine nearest on edge.
00238                 thisPt = minThisPt;
00239                 edgePt = edge.nearestDist(thisPt).rawPoint();
00240             }
00241             else if (minEdge < maxThis)
00242             {
00243                 // minEdge inside this interval.
00244                 edgePt = minEdgePt;
00245                 thisPt = nearestDist(edgePt).rawPoint();
00246             }
00247             else
00248             {
00249                 // minEdge outside this interval
00250                 edgePt = minEdgePt;
00251                 thisPt = maxThisPt;
00252             }
00253         }
00254     }
00255 
00256     return Foam::mag(thisPt - edgePt);
00257 }
00258 
00259 
00260 // * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * * //
00261 
00262 template<class Point, class PointRef>
00263 inline Istream& operator>>(Istream& is, line<Point, PointRef>& l)
00264 {
00265     // Read beginning of line point pair
00266     is.readBegin("line");
00267 
00268     is >> l.a_ >> l.b_;
00269 
00270     // Read end of line point pair
00271     is.readEnd("line");
00272 
00273     // Check state of Ostream
00274     is.check("Istream& operator>>(Istream&, line&)");
00275 
00276     return is;
00277 }
00278 
00279 
00280 template<class Point, class PointRef>
00281 inline Ostream& operator<<(Ostream& os, const line<Point, PointRef>& l)
00282 {
00283     os << token::BEGIN_LIST << l.a_ << token::SPACE << l.b_ << token::END_LIST;
00284     return os;
00285 }
00286 
00287 
00288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00289 
00290 } // End namespace Foam
00291 
00292 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd