library of assembled shared sources

http://lass.cocamware.com

line_2d.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
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 /** Return on what side a point is located.
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 /** Return signed distance of point to line.
00114  *  negative value means point is in the back.
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 /** Return signed distance of point to line.
00126  *  negative value means point is in the back.
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 /** Return on what side a point is located.
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 /** Return signed distance of point to line.
00149  *  negative value means point is in the back.
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 /** Return signed distance of point to line.
00161  *  negative value means point is in the back.
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 // --- protected -----------------------------------------------------------------------------------
00173 
00174 
00175 
00176 // --- private -------------------------------------------------------------------------------------
00177 
00178 
00179 
00180 // --- free ----------------------------------------------------------------------------------------
00181 
00182 /** absolute distance between point and line.
00183  *  @relates Line2D
00184  *  @param iA   point
00185  *  @param iB   line
00186  *  @return absolute distance.
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 /** absolute distance between two lines
00197  *  @relates Line2D
00198  *  @param iA   line A
00199  *  @param iB   line B
00200  *  @return absolute distance.
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())); // parallel
00212     }
00213     else
00214     {
00215         return TNumTraits::zero; // intersecting
00216     }
00217 }
00218 
00219 
00220 
00221 /** intersection of two lines
00222  *  @relates Line2D
00223  *  @param iA   line A
00224  *  @param iB   line B
00225  *  @param oTa  parameter of intersection point on line A
00226  *  @param oTb  parameter of intersection point on line B
00227  *  @return @arg rNone      the lines don't intersect, they have no points in common.
00228  *                          @a oTa and @a oTb are not assigned.
00229  *          @arg rOne       both lines have exactly one point in common.
00230  *                          @a oTa and @a oTb contain parameters of intersection point.
00231  *          @arg rInfinite  the lines are coincident, they have all points in common.
00232  *                          @a oTa and @a oTb are not assigned.
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; // parallel
00255         case sSurface:
00256             return rInfinite; // coincident
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; // intersecting
00267     }
00268 }
00269 
00270 
00271 
00272 /** intersection of two lines
00273  *  @relates Line2D
00274  *  @param iA   line A
00275  *  @param iB   line B
00276  *  @param oPoint   intersection point
00277  *  @return @arg rNone      the lines don't intersect, they have no points in common.
00278  *                          @a oPoint is not assigned.
00279  *          @arg rOne       both lines have exactly one point in common.
00280  *                          @a oPoint contains intersection point.
00281  *          @arg rInfinite  the lines are coincident, they have all points in common.
00282  *                          @a oPoint is not assigned.
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(); // take average for more stable point.
00294     }
00295     return result;
00296 }
00297 
00298 
00299 
00300 /** @relates Line2D
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 /** @relates Line2D
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 /** @relates Line2D
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 // EOF

Generated on Mon Nov 10 14:20:09 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo