library of assembled shared sources

http://lass.cocamware.com

plane_3d_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_PLANE_3D_PARAMETRIC_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_IMPL_PLANE_3D_PARAMETRIC_INL
00047 
00048 #include "../prim_common.h"
00049 #include "plane_3d_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 Plane3DParametric<T, NP>::Plane3DParametric():
00064     support_(),
00065     directionU_(),
00066     directionV_()
00067 {
00068     LASS_ASSERT(!isValid());
00069 }
00070 
00071 
00072 
00073 /** Construct a plane through three points.
00074  *  - support point S is given by the first point iSupport.
00075  *  - direction vectors U and V are choosen from iSupport to iPointU and iPointV respectively
00076  *    (U = iPointU - iSupport, V = iPointV - iSupport).
00077  */
00078 template<typename T, class NP>
00079 Plane3DParametric<T, NP>::Plane3DParametric(const TPoint& iSupport,
00080                                         const TPoint& iPointU,
00081                                         const TPoint& iPointV):
00082     support_(iSupport),
00083     directionU_(iPointU - iSupport),
00084     directionV_(iPointV - iSupport)
00085 {
00086     NP::normalize(directionU_);
00087     NP::normalize(directionV_);
00088 }
00089 
00090 
00091 
00092 /** construct a plane through a support point and by two direction vectors..
00093  *  - support point S is given by the point iSupport.
00094  *  - direction vectors U and V are given by iDirectionU and iDirectionV.
00095  */
00096 template<typename T, class NP>
00097 Plane3DParametric<T, NP>::Plane3DParametric(const TPoint& iSupport,
00098                                         const TVector& iDirectionU,
00099                                         const TVector& iDirectionV):
00100     support_(iSupport),
00101     directionU_(iDirectionU),
00102     directionV_(iDirectionV)
00103 {
00104     NP::normalize(directionU_);
00105     NP::normalize(directionV_);
00106 }
00107 
00108 
00109 
00110 /** Construct a plane through a support point and by a normal vector.
00111  *  - support point S is given by the point iSupport.
00112  *  - direction vectors U and V are automatically generated so that N == U x V.
00113  */
00114 template<typename T, class NP>
00115 Plane3DParametric<T, NP>::Plane3DParametric(const TVector& iNormal, const TPoint& iSupport):
00116     support_(iSupport)
00117 {
00118     Plane3DImplDetail::generateDirections(iNormal, directionU_, directionV_);
00119     NP::normalize(directionU_);
00120     NP::normalize(directionV_);
00121 }
00122 
00123 
00124 
00125 /** Construct a plane by a cartesian equation N.P + d == 0.
00126  *  - support point S automatically generated so that N.S + d == 0.
00127  *  - direction vectors U and V are automatically generated so that N == U x V.
00128  */
00129 template<typename T, class NP>
00130 Plane3DParametric<T, NP>::Plane3DParametric(const TVector& iNormal, TParam iD):
00131     support_(iNormal * (-iD / iNormal.squaredNorm()))
00132 {
00133     Plane3DImplDetail::generateDirections(iNormal, directionU_, directionV_);
00134     NP::normalize(directionU_);
00135     NP::normalize(directionV_);
00136 }
00137 
00138 
00139 
00140 /** return support point.
00141  */
00142 template<typename T, class NP>
00143 const typename Plane3DParametric<T, NP>::TPoint& Plane3DParametric<T, NP>::support() const
00144 {
00145     return support_;
00146 }
00147 
00148 
00149 
00150 /** return U and V direction vectors
00151  */
00152 template<typename T, class NP>
00153 void Plane3DParametric<T, NP>::getDirections(TVector& oDirectionU, TVector& oDirectionV) const
00154 {
00155     oDirectionU = directionU_;
00156     oDirectionV = directionV_;
00157 }
00158 
00159 
00160 
00161 /** return U direction vector.
00162  */
00163 template<typename T, class NP>
00164 const typename Plane3DParametric<T, NP>::TVector& Plane3DParametric<T, NP>::directionU() const
00165 {
00166     return directionU_;
00167 }
00168 
00169 
00170 
00171 /** return V direction vector.
00172  */
00173 template<typename T, class NP>
00174 const typename Plane3DParametric<T, NP>::TVector& Plane3DParametric<T, NP>::directionV() const
00175 {
00176     return directionV_;
00177 }
00178 
00179 
00180 
00181 /** return reciprocal vectors for U and V direction vectors
00182  */
00183 template<typename T, class NP>
00184 void Plane3DParametric<T, NP>::getReciprocals(TVector& oReciprocalU,
00185                                               TVector& oReciprocalV) const
00186 {
00187     Plane3DImplDetail::generateReciprocal(directionU_, directionV_, oReciprocalU, oReciprocalV);
00188 }
00189 
00190 
00191 
00192 /** return reciprocal for U direction vector.
00193  */
00194 template<typename T, class NP>
00195 const typename Plane3DParametric<T, NP>::TVector Plane3DParametric<T, NP>::reciprocalU() const
00196 {
00197     TVector reciprocalU;
00198     TVector reciprocalV;
00199     getReciprocals(reciprocalU, reciprocalV);
00200     return reciprocalU;
00201 }
00202 
00203 
00204 
00205 /** return reciprocal for V direction vector.
00206  */
00207 template<typename T, class NP>
00208 const typename Plane3DParametric<T, NP>::TVector Plane3DParametric<T, NP>::reciprocalV() const
00209 {
00210     TVector reciprocalU;
00211     TVector reciprocalV;
00212     getReciprocals(reciprocalU, reciprocalV);
00213     return reciprocalV;
00214 }
00215 
00216 
00217 
00218 template<typename T, class NP>
00219 void Plane3DParametric<T, NP>::getCartesian(TVector& oNormal, TReference oD) const
00220 {
00221     Plane3DImplDetail::generateCartesian(support_, directionU_, directionV_, oNormal, oD);
00222     NP::normalizeAndScale(oNormal, oD);
00223 }
00224 
00225 
00226 
00227 template<typename T, class NP>
00228 const typename Plane3DParametric<T, NP>::TVector Plane3DParametric<T, NP>::normal() const
00229 {
00230     TVector normal;
00231     TValue d;
00232     getCartesian(normal, d);
00233     return normal;
00234 }
00235 
00236 
00237 
00238 template<typename T, class NP>
00239 const typename Plane3DParametric<T, NP>::TValue Plane3DParametric<T, NP>::d() const
00240 {
00241     TVector normal;
00242     TValue d;
00243     getCartesian(normal, d);
00244     return d;
00245 }
00246 
00247 
00248 
00249 /** Return value of point in equation.
00250  */
00251 template<typename T, class NP>
00252 const typename Plane3DParametric<T, NP>::TValue
00253 Plane3DParametric<T, NP>::equation(const TPoint& iPoint) const
00254 {
00255     TVector normal;
00256     TValue d;
00257     getCartesian(normal, d);
00258     return dot(iPoint.position(), normal) + d;
00259 }
00260 
00261 
00262 
00263 /** Return value of point in equation.
00264  */
00265 template<typename T, class NP>
00266 const typename Plane3DParametric<T, NP>::TValue
00267 Plane3DParametric<T, NP>::equation(const TPoint& iPoint, TParam iRelativeTolerance) const
00268 {
00269     TVector normal;
00270     TValue d;
00271     getCartesian(normal, d);
00272     const TValue a = dot(iPoint.position(), normal);
00273     return almostEqual(a, -d, iRelativeTolerance) ? TNumTraits::zero : (a + d);
00274 }
00275 
00276 
00277 
00278 /** return the vector that, if added to the PROJECTION of iPoint, you get iPoint again.
00279  *  iPoint == (almost) project(iPoint) + reject(iPoint)
00280  */
00281 template<typename T, class NP>
00282 const typename Plane3DParametric<T, NP>::TVector
00283 Plane3DParametric<T, NP>::reject(const TPoint& iPoint) const
00284 {
00285     TVector normal;
00286     TValue d;
00287     getCartesian(normal, d);
00288     return normal * NP::divideBySquaredNorm(dot(iPoint.position(), normal) + d, normal);
00289 }
00290 
00291 
00292 
00293 /** return the part of iVector that is orthogonal to the plane.
00294  *  it's the vector that, if added to the PROJECTION of iVector, you get iVector again.
00295  *  iVector == (almost) project(iVector) + reject(iVector).
00296  */
00297 template<typename T, class NP>
00298 const typename Plane3DParametric<T, NP>::TVector
00299 Plane3DParametric<T, NP>::reject(const TVector& iVector) const
00300 {
00301     TVector normal;
00302     TValue d;
00303     getCartesian(normal, d);
00304     return normal * NP::divideBySquaredNorm(dot(iVector, normal), normal);
00305 }
00306 
00307 
00308 
00309 /** project a point orthogonally onto the plane
00310  */
00311 template<typename T, class NP>
00312 const typename Plane3DParametric<T, NP>::TPoint
00313 Plane3DParametric<T, NP>::project(const TPoint& iPoint) const
00314 {
00315     return iPoint - reject(iPoint);
00316 }
00317 
00318 
00319 
00320 /** project a vector orthogonally onto the plane
00321  */
00322 template<typename T, class NP>
00323 const typename Plane3DParametric<T, NP>::TVector
00324 Plane3DParametric<T, NP>::project(const TVector& iVector) const
00325 {
00326     return iVector - reject(iVector);
00327 }
00328 
00329 
00330 
00331 /** reflect a point orthogonally into the plane.
00332  */
00333 template<typename T, class NP>
00334 const typename Plane3DParametric<T, NP>::TPoint
00335 Plane3DParametric<T, NP>::reflect(const TPoint& iPoint) const
00336 {
00337     return iPoint - 2 * reject(iPoint);
00338 }
00339 
00340 
00341 
00342 /** reflect a vector orthogonally into the plane
00343  */
00344 template<typename T, class NP>
00345 const typename Plane3DParametric<T, NP>::TVector
00346 Plane3DParametric<T, NP>::reflect(const TVector& iVector) const
00347 {
00348     return iVector - 2 * reject(iVector);
00349 }
00350 
00351 
00352 
00353 /** return point by filling in the parametric equation: P(u, v) = S + u * U + v * V
00354  */
00355 template<typename T, class NP>
00356 const typename Plane3DParametric<T, NP>::TPoint
00357 Plane3DParametric<T, NP>::point(TParam iU, TParam iV) const
00358 {
00359     return point(TIndex(iU, iV));
00360 }
00361 
00362 
00363 
00364 /** return point by filling in the parametric equation: P(u, v) = S + u * U + v * V
00365  */
00366 template<typename T, class NP>
00367 const typename Plane3DParametric<T, NP>::TPoint
00368 Plane3DParametric<T, NP>::point(const TUV& iUV) const
00369 {
00370     return support_ + iUV.x * directionU_ + iUV.y * directionV_;
00371 }
00372 
00373 
00374 
00375 /** return UV pair of parameters
00376  */
00377 template<typename T, class NP>
00378 const typename Plane3DParametric<T, NP>::TUV
00379 Plane3DParametric<T, NP>::uv(const TPoint& iPoint) const
00380 {
00381     TVector reciprocalU;
00382     TVector reciprocalV;
00383     getReciprocals(reciprocalU, reciprocalV);
00384     const TVector relative = iPoint - support_;
00385     return TUV(dot(relative, reciprocalU), dot(relative, reciprocalV));
00386 }
00387 
00388 
00389 
00390 template <typename T, class NP>
00391 void Plane3DParametric<T, NP>::flip()
00392 {
00393     directionV_ = -directionV_;
00394 }
00395 
00396 
00397 
00398 /** return true if plane is a valid plane (no direction vector is zero and they're not
00399  *  colinear.
00400  */
00401 template<typename T, class NP>
00402 const bool Plane3DParametric<T, NP>::isValid() const
00403 {
00404     return !cross(directionU_, directionV_).isZero(); // zero vectors will give zero cross product too.
00405 }
00406 
00407 
00408 
00409 
00410 // --- protected -----------------------------------------------------------------------------------
00411 
00412 
00413 
00414 // --- private -------------------------------------------------------------------------------------
00415 
00416 
00417 
00418 // --- free ----------------------------------------------------------------------------------------
00419 
00420 template<typename T, class NP>
00421 std::ostream& operator<<(std::ostream& ioOStream, const Plane3DParametric<T, NP>& iPlane)
00422 {
00423     LASS_ENFORCE(ioOStream) << "{S=" << iPlane.support() << ", U=" << iPlane.directionU() << ", V="
00424         << iPlane.directionV() << "}";
00425     return ioOStream;
00426 }
00427 
00428 
00429 
00430 }
00431 
00432 }
00433 
00434 }
00435 
00436 #endif
00437 
00438 // EOF

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