library of assembled shared sources

http://lass.cocamware.com

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

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