library of assembled shared sources

http://lass.cocamware.com

line_2d_cartesian.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_IMPL_LINE_2D_CARTESIAN_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_IMPL_LINE_2D_CARTESIAN_INL
00047 
00048 #include "../prim_common.h"
00049 #include "line_2d_cartesian.h"
00050 
00051 namespace lass
00052 {
00053 namespace prim
00054 {
00055 namespace impl
00056 {
00057 
00058 // --- public --------------------------------------------------------------------------------------
00059 
00060 /** initializes to an invalid state.
00061  */
00062 template<typename T, class NP>
00063 Line2DCartesian<T, NP>::Line2DCartesian():
00064     normal_(),
00065     d_(TNumTraits::zero)
00066 {
00067     LASS_ASSERT(!isValid());
00068 }
00069 
00070 
00071 
00072 /** Construct a line through three points.
00073  *  - support point S is given by the first point iSupport.
00074  *  - direction vector U is choosen from iSupport to iPointU and iPointV respectively
00075  *    (U = iPoint - iSupport).
00076  *  - normal vector N is given by the perp of U.
00077  *  - value d is choosen so that the support point is indeed a point of the line.
00078  */
00079 template<typename T, class NP>
00080 Line2DCartesian<T, NP>::Line2DCartesian(const TPoint& iSupport, const TPoint& iPoint):
00081     normal_((iPoint - iSupport).perp())
00082 {
00083     NP::normalize(normal_);
00084     d_ = -dot(normal_, iSupport.position());
00085 }
00086 
00087 
00088 
00089 /** construct a line through a support point and by two direction vectors..
00090  *  - support point S is given by the point iSupport.
00091  *  - direction vector U is given by iDir.
00092  *  - normal vector N is given by the perp of N.
00093  *  - value d is choosen so that the support point is indeed a point of the line.
00094  */
00095 template<typename T, class NP>
00096 Line2DCartesian<T, NP>::Line2DCartesian(const TPoint& iSupport, const TVector& iDirection):
00097     normal_(iDirection.perp())
00098 {
00099     NP::normalize(normal_);
00100     d_ = -dot(normal_, iSupport.position());
00101 }
00102 
00103 
00104 
00105 /** Construct a line through a support point and by a normal vector.
00106  *  - support point S is given by the point iSupport.
00107  *  - normal vector N is given by the vector iNormal.
00108  *  - direction vector U is automatically generated so that N is perp of U.
00109  *  - value d is choosen so that the support point is indeed a point of the line.
00110  */
00111 template<typename T, class NP>
00112 Line2DCartesian<T, NP>::Line2DCartesian(const TVector& iNormal, const TPoint& iSupport):
00113     normal_(iNormal)
00114 {
00115     NP::normalize(normal_);
00116     d_ = -dot(normal_, iSupport.position());
00117 }
00118 
00119 
00120 
00121 /** Construct a line by a cartesian equation N.P + d == 0.
00122  *  - normal vector N is given by the vector iNormal.
00123  *  - value d is given by the value iD.
00124  *  - support point S automatically generated so that N.S + d == 0.
00125  *  - direction vector U is automatically generated so that N is perp of U.
00126  */
00127 template<typename T, class NP>
00128 Line2DCartesian<T, NP>::Line2DCartesian(const TVector& iNormal, TParam iD):
00129     normal_(iNormal),
00130     d_(iD)
00131 {
00132     NP::normalizeAndScale(normal_, d_);
00133 }
00134 
00135 
00136 
00137 /** return @b generated support point.
00138  *  @return -d * N
00139  */
00140 template<typename T, class NP>
00141 const typename Line2DCartesian<T, NP>::TPoint Line2DCartesian<T, NP>::support() const
00142 {
00143     return TPoint(normal_ * NP::divideBySquaredNorm(-d_, normal_));
00144 }
00145 
00146 
00147 
00148 /** return @b generated direction vector.
00149  */
00150 template<typename T, class NP>
00151 const typename Line2DCartesian<T, NP>::TVector Line2DCartesian<T, NP>::direction() const
00152 {
00153     return -normal_.perp();
00154 }
00155 
00156 
00157 
00158 template<typename T, class NP>
00159 void Line2DCartesian<T, NP>::getCartesian(TVector& oNormal, TReference oD) const
00160 {
00161     oNormal = normal_;
00162     oD = d_;
00163 }
00164 
00165 
00166 
00167 template<typename T, class NP>
00168 const typename Line2DCartesian<T, NP>::TVector& Line2DCartesian<T, NP>::normal() const
00169 {
00170     return normal_;
00171 }
00172 
00173 
00174 
00175 template<typename T, class NP>
00176 typename Line2DCartesian<T, NP>::TParam Line2DCartesian<T, NP>::d() const
00177 {
00178     return d_;
00179 }
00180 
00181 
00182 
00183 /** Return value of point in equation.
00184  */
00185 template<typename T, class NP>
00186 const typename Line2DCartesian<T, NP>::TValue
00187 Line2DCartesian<T, NP>::equation(const TPoint& iPoint) const
00188 {
00189     return dot(iPoint.position(), normal_) + d_;
00190 }
00191 
00192 
00193 
00194 /** Return value of point in equation, snapped to zero by @a iRelativeTolerance
00195  */
00196 template<typename T, class NP>
00197 const typename Line2DCartesian<T, NP>::TValue
00198 Line2DCartesian<T, NP>::equation(const TPoint& iPoint, TParam iRelativeTolerance) const
00199 {
00200     const TValue d = dot(iPoint.position(), normal_);
00201     return num::almostEqual(d, -d_, iRelativeTolerance) ? TNumTraits::zero : (d + d_);
00202 }
00203 
00204 
00205 
00206 /** return the vector that, if added to the PROJECTION of iPoint, you get iPoint again.
00207  *  iPoint == (almost) project(iPoint) + reject(iPoint)
00208  */
00209 template<typename T, class NP>
00210 const typename Line2DCartesian<T, NP>::TVector
00211 Line2DCartesian<T, NP>::reject(const TPoint& iPoint) const
00212 {
00213     return normal_ * NP::divideBySquaredNorm(equation(iPoint), normal_);
00214 }
00215 
00216 
00217 
00218 /** return the part of iVector that is orthogonal to the line.
00219  *  it's the vector that, if added to the PROJECTION of iVector, you get iVector again.
00220  *  iVector == (almost) project(iVector) + reject(iVector).
00221  */
00222 template<typename T, class NP>
00223 const typename Line2DCartesian<T, NP>::TVector
00224 Line2DCartesian<T, NP>::reject(const TVector& iVector) const
00225 {
00226     return normal_ * NP::divideBySquaredNorm(dot(normal_, iVector), normal_);
00227 }
00228 
00229 
00230 
00231 /** project a point orthogonally onto the line
00232  */
00233 template<typename T, class NP>
00234 const typename Line2DCartesian<T, NP>::TPoint
00235 Line2DCartesian<T, NP>::project(const TPoint& iPoint) const
00236 {
00237     return iPoint - reject(iPoint);
00238 }
00239 
00240 
00241 
00242 /** project a vector orthogonally onto the line
00243  */
00244 template<typename T, class NP>
00245 const typename Line2DCartesian<T, NP>::TVector
00246 Line2DCartesian<T, NP>::project(const TVector& iVector) const
00247 {
00248     return iVector - reject(iVector);
00249 }
00250 
00251 
00252 
00253 /** reflect a point orthogonally into the line.
00254  */
00255 template<typename T, class NP>
00256 const typename Line2DCartesian<T, NP>::TPoint
00257 Line2DCartesian<T, NP>::reflect(const TPoint& iPoint) const
00258 {
00259     return iPoint - T(2) * reject(iPoint);
00260 }
00261 
00262 
00263 
00264 /** reflect a vector orthogonally into the line
00265  */
00266 template<typename T, class NP>
00267 const typename Line2DCartesian<T, NP>::TVector
00268 Line2DCartesian<T, NP>::reflect(const TVector& iVector) const
00269 {
00270     return iVector - T(2) * reject(iVector);
00271 }
00272 
00273 
00274 
00275 /** return point by filling in parameter in @b generated parametric equation
00276  */
00277 template<typename T, class NP>
00278 const typename Line2DCartesian<T, NP>::TPoint
00279 Line2DCartesian<T, NP>::point(TParam iT) const
00280 {
00281     return support() + iT * direction();
00282 }
00283 
00284 
00285 
00286 /** return parameter along @b generated paremetric equation.
00287  */
00288 template<typename T, class NP>
00289 const typename Line2DCartesian<T, NP>::TValue Line2DCartesian<T, NP>::t(const TPoint& iPoint) const
00290 {
00291     const TVector dir = direction();
00292     return NP::divideBySquaredNorm(dot(dir, iPoint - support()), dir);
00293 }
00294 
00295 
00296 
00297 template <typename T, class NP>
00298 void Line2DCartesian<T, NP>::flip()
00299 {
00300     normal_ = -normal_;
00301     d_ = -d_;
00302 }
00303 
00304 
00305 
00306 /** return true if line is a valid line (no normal or direction vectors that are zero).
00307  */
00308 template<typename T, class NP>
00309 const bool Line2DCartesian<T, NP>::isValid() const
00310 {
00311     return !normal_.isZero();
00312 }
00313 
00314 
00315 
00316 
00317 // --- protected -----------------------------------------------------------------------------------
00318 
00319 
00320 
00321 // --- private -------------------------------------------------------------------------------------
00322 
00323 
00324 
00325 // --- free ----------------------------------------------------------------------------------------
00326 
00327 template<typename T, class NP>
00328 std::ostream& operator<<(std::ostream& ioOStream, const Line2DCartesian<T, NP>& iLine)
00329 {
00330     LASS_ENFORCE(ioOStream) << "{N=" << iLine.normal() << ", d=" << iLine.d() << "}";
00331     return ioOStream;
00332 }
00333 
00334 
00335 
00336 }
00337 
00338 }
00339 
00340 }
00341 
00342 #endif
00343 
00344 // EOF

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