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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_SEGMENT_2D_INL
00044 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_SEGMENT_2D_INL
00045
00046 #include "line_segment_2d.h"
00047 #include "point_2dh.h"
00048 #include "../num/floating_point_consistency.h"
00049
00050 namespace lass
00051 {
00052 namespace prim
00053 {
00054
00055 template <typename T, class PP>
00056 LineSegment2D<T, PP>::LineSegment2D():
00057 tail_(),
00058 head_()
00059 {
00060 LASS_ASSERT(tail_.isZero());
00061 LASS_ASSERT(head_.isZero());
00062 }
00063
00064
00065
00066 template <typename T, class PP>
00067 LineSegment2D<T, PP>::LineSegment2D(const TPoint& tail, const TPoint& head):
00068 tail_(tail),
00069 head_(head)
00070 {
00071 }
00072
00073
00074
00075 template <typename T, class PP> inline
00076 const typename LineSegment2D<T, PP>::TPoint&
00077 LineSegment2D<T, PP>::tail() const
00078 {
00079 return tail_;
00080 }
00081
00082
00083
00084 template <typename T, class PP> inline
00085 typename LineSegment2D<T, PP>::TPoint&
00086 LineSegment2D<T, PP>::tail()
00087 {
00088 return tail_;
00089 }
00090
00091
00092
00093 template <typename T, class PP> inline
00094 const typename LineSegment2D<T, PP>::TPoint&
00095 LineSegment2D<T, PP>::head() const
00096 {
00097 return head_;
00098 }
00099
00100
00101
00102 template <typename T, class PP> inline
00103 typename LineSegment2D<T, PP>::TPoint&
00104 LineSegment2D<T, PP>::head()
00105 {
00106 return head_;
00107 }
00108
00109
00110
00111
00112
00113
00114 template <typename T, class PP>
00115 const typename LineSegment2D<T, PP>::TPoint
00116 LineSegment2D<T, PP>::point(TParam t) const
00117 {
00118 TParameterPolicy::enforceRange(t, TNumTraits::zero, TNumTraits::one);
00119 return tail_ + t * vector();
00120 }
00121
00122
00123
00124
00125
00126
00127 template <typename T, class PP>
00128 const typename LineSegment2D<T, PP>::TValue
00129 LineSegment2D<T, PP>::t(const TPoint& point) const
00130 {
00131 const TVector v = vector();
00132 const TValue t1 = dot(point - tail_, v);
00133 const TValue t2 = -dot(point - head_, v);
00134 const TValue t = std::max(t1,t2) / (t1 + t2);
00135 return t1 > t2 ? t : TNumTraits::one - t;
00136 }
00137
00138
00139
00140
00141
00142 template <typename T, class PP>
00143 const typename LineSegment2D<T, PP>::TVector
00144 LineSegment2D<T, PP>::vector() const
00145 {
00146 return head_ - tail_;
00147 }
00148
00149
00150
00151
00152
00153 template <typename T, class PP>
00154 const typename LineSegment2D<T, PP>::TValue
00155 LineSegment2D<T, PP>::length() const
00156 {
00157 const TVector v = vector();
00158 return v.norm();
00159 }
00160
00161
00162
00163 template <typename T, class PP>
00164 T squaredDistance(const LineSegment2D<T, PP>& segment, const Point2D<T>& point)
00165 {
00166 typedef typename LineSegment2D<T, PP>::TVector TVector;
00167 typedef typename LineSegment2D<T, PP>::TValue TValue;
00168
00169 const TVector edge = segment.vector();
00170 const TVector v = point - segment.tail();
00171 const TValue t = dot(v, edge);
00172 const TValue tMax = dot(edge, edge);
00173 if (t <= 0)
00174 {
00175 return v.squaredNorm();
00176 }
00177 if (t >= tMax)
00178 {
00179 return squaredDistance(segment.head(), point);
00180 }
00181 return (v - edge * (t / tMax)).squaredNorm();
00182 }
00183
00184
00185
00186 template <typename T, class PP>
00187 T distance(const LineSegment2D<T, PP>& segment, const Point2D<T>& point)
00188 {
00189 return num::sqrt(squaredDistance(segment, point));
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 template <typename T, class PPa, class PPb>
00208 Result intersect(const LineSegment2D<T, PPa>& a, const LineSegment2D<T, PPb>& b, T& tA, T& tB)
00209 {
00210 typedef typename LineSegment2D<T, PPa>::TVector TVector;
00211 typedef typename LineSegment2D<T, PPa>::TValue TValue;
00212 typedef typename LineSegment2D<T, PPa>::TNumTraits TNumTraits;
00213 typedef num::Consistent<TValue> TConsistent;
00214
00215 const TVector dirA(a.vector());
00216 const TVector dirB(b.vector());
00217
00218 const TValue denominator = -perpDot(dirA, dirB);
00219 if (denominator == TNumTraits::zero)
00220 {
00221 const TValue tTail = a.t(b.tail());
00222 const TValue tHead = b.t(b.head());
00223 if ((tTail < TNumTraits::zero && tHead < TNumTraits::zero) ||
00224 (tTail > TNumTraits::one && tHead > TNumTraits::one))
00225 {
00226 return rNone;
00227 }
00228 else
00229 {
00230
00231
00232 if (doubleTriangleArea(a.tail(), a.head(), b.tail()) == TNumTraits::zero)
00233 {
00234 return rInfinite;
00235 }
00236 else
00237 {
00238 return rNone;
00239 }
00240 }
00241 }
00242 else
00243 {
00244 const TVector difference = b.tail() - a.tail();
00245 const TConsistent candidateA = -perpDot(difference, dirB) / denominator;
00246 const TConsistent candidateB = perpDot(dirA, difference) / denominator;
00247 if (candidateA < TNumTraits::zero || candidateA > TNumTraits::one ||
00248 candidateB < TNumTraits::zero || candidateB > TNumTraits::one)
00249 {
00250 return rNone;
00251 }
00252 else
00253 {
00254 tA = candidateA.value();
00255 tB = candidateB.value();
00256 return rOne;
00257 }
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 template <typename T, class PPa, class PPb>
00276 Result intersect(const LineSegment2D<T, PPa>& a, const LineSegment2D<T, PPb>& b, Point2D<T>& point)
00277 {
00278 T tA;
00279 T tB;
00280 const Result result = intersect(a, b, tA, tB);
00281 if (result == rOne)
00282 {
00283 Point2DH<T> intersection(a.point(tA));
00284 intersection += b.point(tB);
00285 point = intersection.affine();
00286 }
00287 return result;
00288 }
00289
00290
00291
00292
00293 template <typename T, class PPa, class PPb> bool operator==(const LineSegment2D<T, PPa>& a, const LineSegment2D<T, PPb>& b)
00294 {
00295 return a.tail()==b.tail() && a.head()==b.head();
00296 }
00297
00298
00299
00300 template <typename T, class PPa, class PPb> bool operator!=(const LineSegment2D<T, PPa>& a, const LineSegment2D<T, PPb>& b)
00301 {
00302 return !(a==b);
00303 }
00304
00305
00306
00307
00308 template<typename T, class PP>
00309 std::ostream& operator<<(std::ostream& stream, const LineSegment2D<T, PP>& segment)
00310 {
00311 LASS_ENFORCE_STREAM(stream) << "{T=" << segment.tail() << ", H=" << segment.head() << "}";
00312 return stream;
00313 }
00314
00315
00316
00317
00318
00319 template<typename T, class PP>
00320 io::XmlOStream& operator<<(io::XmlOStream& stream, const LineSegment2D<T, PP>& segment)
00321 {
00322 LASS_ENFORCE_STREAM(stream)
00323 << "<LineSegment2D>\n"
00324 << "<tail>" << segment.tail() << "</tail>\n"
00325 << "<head>" << segment.head() << "</head>\n"
00326 << "</LineSegment2D>\n";
00327
00328 return stream;
00329 }
00330
00331
00332
00333
00334 template<typename T, class PP>
00335 lass::io::MatlabOStream& operator<<(lass::io::MatlabOStream& stream, const LineSegment2D<T, PP>& segment)
00336 {
00337 LASS_ENFORCE_STREAM(stream) << "lasthandle = line(";
00338 stream << "[" << segment.tail().x << "," << segment.head().x << "],";
00339 stream << "[" << segment.tail().y << "," << segment.head().y << "],";
00340 stream << "'Color'," << stream.color() << ");\n";
00341 return stream;
00342 }
00343
00344
00345 }
00346
00347 }
00348
00349 #endif