library of assembled shared sources

http://lass.cocamware.com

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

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