library of assembled shared sources

http://lass.cocamware.com

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

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