Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #include <OpenFOAM/line.H>
00027 
00028 
00029 
00030 namespace Foam
00031 {
00032 
00033 
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         
00085         
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             
00108             edgePt = maxEdgePt;
00109             thisPt = minThisPt;
00110         }
00111         else if (maxEdge < maxThis)
00112         {
00113             
00114             edgePt = maxEdgePt;
00115             thisPt = nearestDist(edgePt).rawPoint();
00116         }
00117         else
00118         {
00119             
00120             if (minEdge < minThis)
00121             {
00122                 
00123                 
00124                 thisPt = minThisPt;
00125                 edgePt = e.nearestDist(thisPt).rawPoint();
00126             }
00127             else if (minEdge < maxThis)
00128             {
00129                 
00130                 edgePt = minEdgePt;
00131                 thisPt = nearestDist(edgePt).rawPoint();
00132             }
00133             else
00134             {
00135                 
00136                 edgePt = minEdgePt;
00137                 thisPt = maxThisPt;
00138             }
00139         }
00140     }
00141 
00142     return Foam::mag(thisPt - edgePt);
00143 }
00144 
00145 
00146 
00147 
00148 } 
00149 
00150