library of assembled shared sources

http://lass.cocamware.com

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

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