lineI.H
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
00027 #include "IOstreams.H"
00028
00029
00030
00031 namespace Foam
00032 {
00033
00034
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
00048 is.readBegin("line");
00049
00050 is >> a_ >> b_;
00051
00052
00053 is.readEnd("line");
00054
00055
00056 is.check("line::line(Istream& is)");
00057 }
00058
00059
00060
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
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
00147 if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
00148 {
00149
00150 thisPt = start() + a*s;
00151 edgePt = edge.start() + b*t;
00152 }
00153 else
00154 {
00155
00156
00157
00158
00159 PointHit<Point> this0(nearestDist(edge.start()));
00160 PointHit<Point> this1(nearestDist(edge.end()));
00161 scalar thisDist = min(this0.distance(), this1.distance());
00162
00163
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
00199
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
00222 edgePt = maxEdgePt;
00223 thisPt = minThisPt;
00224 }
00225 else if (maxEdge < maxThis)
00226 {
00227
00228 edgePt = maxEdgePt;
00229 thisPt = nearestDist(edgePt).rawPoint();
00230 }
00231 else
00232 {
00233
00234 if (minEdge < minThis)
00235 {
00236
00237
00238 thisPt = minThisPt;
00239 edgePt = edge.nearestDist(thisPt).rawPoint();
00240 }
00241 else if (minEdge < maxThis)
00242 {
00243
00244 edgePt = minEdgePt;
00245 thisPt = nearestDist(edgePt).rawPoint();
00246 }
00247 else
00248 {
00249
00250 edgePt = minEdgePt;
00251 thisPt = maxThisPt;
00252 }
00253 }
00254 }
00255
00256 return Foam::mag(thisPt - edgePt);
00257 }
00258
00259
00260
00261
00262 template<class Point, class PointRef>
00263 inline Istream& operator>>(Istream& is, line<Point, PointRef>& l)
00264 {
00265
00266 is.readBegin("line");
00267
00268 is >> l.a_ >> l.b_;
00269
00270
00271 is.readEnd("line");
00272
00273
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 }
00291
00292
Copyright © 2000-2009 OpenCFD Ltd