library of assembled shared sources

http://lass.cocamware.com

parallelogram_3d_ray_3d.h

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 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_PARALLELOGRAM_3D_RAY_3D_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_PARALLELOGRAM_3D_RAY_3D_H
00045 
00046 #include "prim_common.h"
00047 #include "parallelogram_3d.h"
00048 #include "ray_3d.h"
00049 #include "../num/floating_point_consistency.h"
00050 
00051 namespace lass
00052 {
00053 namespace prim
00054 {
00055 
00056 /** Find the intersection of a ray and a parallelogram by their parameter t on the ray
00057  *  and it's coordinates (u,v) on the parallelogram.
00058  *  @relates lass::prim::Ray3D
00059  *  @relates lass::prim::Parallelogram3D
00060  *
00061  *  A maximum of two possible intersections with @a t > 0.
00062  *
00063  *  @param parallelogram [in] the parallelogram
00064  *  @param ray [in] the ray
00065  *  @param oU [out] the parameter of the intersection point on the parallelogram.
00066  *  @param oV [out] the parameter of the intersection point on the parallelogram.
00067  *  @param t [out] the parameter of the intersection point > @a tMin.
00068  *  @param tMin [in] the minimum t that may be returned as valid intersection.
00069  *  @return @arg rNone: no intersections with @a t > @a tMin found. @a t is not assigned.
00070  *          @arg rOne: a intersection with @a t > @a tMin is found. @a t is assigned.
00071  *  
00072  *  @par Algorithm, based on ray/triangle intersection from:
00073  *  @arg Tomas Möller and Ben Trumbore.
00074  *  "Fast, minimum storage ray-triangle intersection."
00075  *  Journal of Graphics Tools, 2(1):21--28, 1997.
00076  *  http://www.graphics.cornell.edu/pubs/1997/MT97.html
00077  */
00078 template <typename T, class NP, class PP>
00079 Result intersect(
00080         const Parallelogram3D<T>& parallelogram, const Ray3D<T, NP, PP>& ray, 
00081         T& u, T& v, T& t, const T& tMin = T())
00082 {
00083     typedef typename Parallelogram3D<T>::TVector TVector;
00084     typedef typename Parallelogram3D<T>::TValue TValue;
00085     typedef typename Parallelogram3D<T>::TNumTraits TNumTraits;
00086     typedef num::Consistent<T> TConsistent;
00087 
00088     const TVector pvec = cross(ray.direction(), parallelogram.sizeV());
00089     
00090     const TValue det = dot(pvec, parallelogram.sizeU());
00091     if (det == TNumTraits::zero)
00092     {
00093         return rNone;
00094     }
00095     const TValue invDet = num::inv(det);
00096 
00097     const TVector tvec = ray.support() - parallelogram.support();
00098     const TValue uCandidate = dot(tvec, pvec) * invDet;
00099     if (uCandidate < TNumTraits::zero || uCandidate > TNumTraits::one)
00100     {
00101         return rNone;
00102     }
00103 
00104     const TVector qvec = cross(tvec, parallelogram.sizeU());
00105     const TValue vCandidate = dot(ray.direction(), qvec) * invDet;
00106     if (vCandidate < TNumTraits::zero || vCandidate > TNumTraits::one)
00107     {
00108         return rNone;
00109     }
00110 
00111     const TConsistent tCandidate = dot(parallelogram.sizeV(), qvec) * invDet;
00112     if (tCandidate <= tMin)
00113     {
00114         return rNone;
00115     }
00116 
00117     u = uCandidate;
00118     v = vCandidate;
00119     t = tCandidate.value();
00120     return rOne;
00121 }
00122 
00123 
00124 
00125 /** Find the intersection of a ray and a parallelogram by their parameter t on the ray.
00126  *  @relates lass::prim::Ray3D
00127  *  @relates lass::prim::Parallelogram3D
00128  *
00129  *  A maximum of two possible intersections with t > 0.
00130  *
00131  *  @param parallelogram [in] the parallelogram
00132  *  @param ray [in] the ray
00133  *  @param t [out] the parameter of the intersection point > @a tMin.
00134  *  @param tMin [in] the minimum t that may be returned as valid intersection.
00135  *  @return @arg rNone      no intersections with @a t > @a tMin found
00136  *                          @a t is not assigned.
00137  *          @arg rOne       a intersection with @a t > @a tMin is found
00138  *                          @a t is assigned.
00139  */
00140 template <typename T, class NP, class PP> inline
00141 Result intersect(
00142         const Parallelogram3D<T>& parallelogram, const Ray3D<T, NP, PP>& ray, 
00143         T& t, const T& tMin = T())
00144 {
00145     T u;
00146     T v;
00147     return intersect(parallelogram, ray, u, v, t, tMin);
00148 }
00149 
00150 }
00151 
00152 }
00153 
00154 #endif
00155 
00156 // EOF

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