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
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_2D_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_2D_INL
00047
00048
00049
00050
00051 #include "line_2d.h"
00052
00053
00054
00055 namespace lass
00056 {
00057
00058 namespace prim
00059 {
00060
00061 template<typename T, typename EP, typename NP>
00062 Line2D<T, EP, NP>::Line2D():
00063 TImpl()
00064 {
00065 }
00066
00067
00068
00069
00070 template<typename T, typename EP, typename NP>
00071 Line2D<T, EP, NP>::Line2D(const TPoint& iSupport, const TPoint& iPoint):
00072 TImpl(iSupport, iPoint)
00073 {
00074 }
00075
00076
00077
00078 template<typename T, typename EP, typename NP>
00079 Line2D<T, EP, NP>::Line2D(const TPoint& iSupport, const TVector& iDirection):
00080 TImpl(iSupport, iDirection)
00081 {
00082 }
00083
00084
00085
00086 template<typename T, typename EP, typename NP>
00087 Line2D<T, EP, NP>::Line2D(const TVector& iNormal, const TPoint& iSupport):
00088 TImpl(iNormal, iSupport)
00089 {
00090 }
00091
00092
00093
00094 template<typename T, typename EP, typename NP>
00095 Line2D<T, EP, NP>::Line2D(const TVector& iNormal, TParam iD):
00096 TImpl(iNormal, iD)
00097 {
00098 }
00099
00100
00101
00102
00103
00104 template<typename T, typename EP, typename NP>
00105 const Side Line2D<T, EP, NP>::classify(const TPoint& iPoint) const
00106 {
00107 const TValue eq = this->equation(iPoint);
00108 return eq > TNumTraits::zero ? sFront : (eq < TNumTraits::zero ? sBack : sSurface);
00109 }
00110
00111
00112
00113
00114
00115
00116 template<typename T, typename EP, typename NP>
00117 const typename Line2D<T, EP, NP>::TValue
00118 Line2D<T, EP, NP>::signedDistance(const TPoint& iPoint) const
00119 {
00120 return NP::divideByNorm(this->equation(iPoint), this->normal());
00121 }
00122
00123
00124
00125
00126
00127
00128 template<typename T, typename EP, typename NP>
00129 const typename Line2D<T, EP, NP>::TValue
00130 Line2D<T, EP, NP>::squaredDistance(const TPoint& iPoint) const
00131 {
00132 return num::sqr(this->signedDistance(iPoint));
00133 }
00134
00135
00136
00137
00138
00139 template<typename T, typename EP, typename NP>
00140 const Side Line2D<T, EP, NP>::classify(const TPoint& iPoint, TParam iRelativeTolerance) const
00141 {
00142 const TValue eq = this->equation(iPoint, iRelativeTolerance);
00143 return eq > TNumTraits::zero ? sFront : (eq < TNumTraits::zero ? sBack : sSurface);
00144 }
00145
00146
00147
00148
00149
00150
00151 template<typename T, typename EP, typename NP>
00152 const typename Line2D<T, EP, NP>::TValue
00153 Line2D<T, EP, NP>::signedDistance(const TPoint& iPoint, TParam iRelativeTolerance) const
00154 {
00155 return NP::divideByNorm(this->equation(iPoint, iRelativeTolerance), this->normal());
00156 }
00157
00158
00159
00160
00161
00162
00163 template<typename T, typename EP, typename NP>
00164 const typename Line2D<T, EP, NP>::TValue
00165 Line2D<T, EP, NP>::squaredDistance(const TPoint& iPoint, TParam iRelativeTolerance) const
00166 {
00167 return num::sqr(this->signedDistance(iPoint, iRelativeTolerance));
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 template <typename T, class EP, class NP>
00189 T distance(const Point2D<T>& iA, const Line2D<T, EP, NP>& iB)
00190 {
00191 return num::abs(iB.signedDistance(iA));
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 template <typename T, class EPa, class NPa, class EPb, class NPb>
00203 T distance(const Line2D<T, EPa, NPa>& iA, const Line2D<T, EPb, NPb>& iB)
00204 {
00205 LASS_ASSERT(iA.isValid() && iB.isValid());
00206
00207 typedef typename Line2D<T, EPa, NPa>::TNumTraits TNumTraits;
00208
00209 if (perpDot(iA.direction(), iB.direction()) == TNumTraits::zero)
00210 {
00211 return num::abs(iA.signedDistance(iB.support()));
00212 }
00213 else
00214 {
00215 return TNumTraits::zero;
00216 }
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 template <typename T, class EPa, class NPa, class EPb, class NPb>
00235 Result intersect(const Line2D<T, EPa, NPa>& iA, const Line2D<T, EPb, NPb>& iB,
00236 T& oTa, T& oTb)
00237 {
00238 LASS_ASSERT(iA.isValid() && iB.isValid());
00239
00240 typedef typename Line2D<T, EPa, NPa>::TVector TVector;
00241 typedef typename Line2D<T, EPa, NPa>::TValue TValue;
00242 typedef typename Line2D<T, EPa, NPa>::TNumTraits TNumTraits;
00243
00244 const TVector dirA(iA.direction());
00245 const TVector dirB(iB.direction());
00246
00247 const TValue denominator = -perpDot(dirA, dirB);
00248 if (denominator == TNumTraits::zero)
00249 {
00250 switch (iA.classify(iB.support()))
00251 {
00252 case sFront:
00253 case sBack:
00254 return rNone;
00255 case sSurface:
00256 return rInfinite;
00257 default:
00258 return rInvalid;
00259 }
00260 }
00261 else
00262 {
00263 const TVector difference = iB.support() - iA.support();
00264 oTa = -perpDot(difference, dirB) / denominator;
00265 oTb = perpDot(dirA, difference) / denominator;
00266 return rOne;
00267 }
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 template <typename T, class EPa, class NPa, class EPb, class NPb>
00285 Result intersect(const Line2D<T, EPa, NPa>& iA, const Line2D<T, EPb, NPb>& iB,
00286 Point2D<T>& oPoint)
00287 {
00288 T tA;
00289 T tB;
00290 Result result = intersect(iA, iB, tA, tB);
00291 if (result == rOne)
00292 {
00293 oPoint = (iA.point(tA) + iB.point(tB)).affine();
00294 }
00295 return result;
00296 }
00297
00298
00299
00300
00301
00302 template <typename T>
00303 io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Line2D<T, Cartesian>& iLine)
00304 {
00305 LASS_ENFORCE_STREAM(ioOStream)
00306 << "<Line2D>\n"
00307 << "<normal>" << iLine.normal() << "</normal>\n"
00308 << "<d>" << iLine.d() << "</d>\n"
00309 << "</Line2D>\n";
00310
00311 return ioOStream;
00312 }
00313
00314
00315
00316
00317
00318 template <typename T>
00319 io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Line2D<T, Parametric>& iLine)
00320 {
00321 LASS_ENFORCE_STREAM(ioOStream)
00322 << "<Line2D>\n"
00323 << "<support>" << iLine.support() << "</support>\n"
00324 << "<direction>" << iLine.direction() << "</direction>\n"
00325 << "</Line2D>\n";
00326
00327 return ioOStream;
00328 }
00329
00330
00331
00332
00333
00334 template <typename T>
00335 io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Line2D<T, Combined>& iLine)
00336 {
00337 LASS_ENFORCE_STREAM(ioOStream)
00338 << "<Line2D>\n"
00339 << "<support>" << iLine.support() << "</support>\n"
00340 << "<direction>" << iLine.direction() << "</direction>\n"
00341 << "<normal>" << iLine.normal() << "</normal>\n"
00342 << "<d>" << iLine.d() << "</d>\n"
00343 << "</Line2D>\n";
00344
00345 return ioOStream;
00346 }
00347
00348
00349
00350 }
00351
00352 }
00353
00354 #endif
00355
00356