library of assembled shared sources

http://lass.cocamware.com

ray_2d.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_RAY_2D_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_RAY_2D_INL
00047 
00048 
00049 
00050 
00051 #include "ray_2d.h"
00052 
00053 
00054 
00055 namespace lass
00056 {
00057 
00058 namespace prim
00059 {
00060 
00061 template <typename T, class NP, class PP>
00062 Ray2D<T, NP, PP>::Ray2D():
00063     support_(),
00064     direction_()
00065 {
00066     LASS_ASSERT(support_.isZero());
00067     LASS_ASSERT(direction_.isZero());
00068 }
00069 
00070 
00071 
00072 template <typename T, class NP, class PP>
00073 Ray2D<T, NP, PP>::Ray2D(const TPoint& iSupport, const TVector& iDirection):
00074     support_(iSupport),
00075     direction_(iDirection)
00076 {
00077     NP::normalize(direction_);
00078 }
00079 
00080 
00081 
00082 template <typename T, class NP, class PP>
00083 Ray2D<T, NP, PP>::Ray2D(const TPoint& iSupport, const TPoint& iLookAt):
00084     support_(iSupport),
00085     direction_(iLookAt - iSupport)
00086 {
00087     NP::normalize(direction_);
00088 }
00089 
00090 
00091 
00092 /** return origin of ray.
00093  *  the origin is also the support point of the ray.
00094  */
00095 template <typename T, class NP, class PP>
00096 const typename Ray2D<T, NP, PP>::TPoint&
00097 Ray2D<T, NP, PP>::support() const
00098 {
00099     return support_;
00100 }
00101 
00102 
00103 
00104 /** access origin of ray
00105  *  the origin is also the support point of the ray.
00106  */
00107 template <typename T, class NP, class PP>
00108 typename Ray2D<T, NP, PP>::TPoint&
00109 Ray2D<T, NP, PP>::support()
00110 {
00111     return support_;
00112 }
00113 
00114 
00115 
00116 /** Return direction of ray.
00117  */
00118 template <typename T, class NP, class PP>
00119 const typename Ray2D<T, NP, PP>::TVector&
00120 Ray2D<T, NP, PP>::direction() const
00121 {
00122     return direction_;
00123 }
00124 
00125 
00126 
00127 /** Set direction and normalize it if that's the policy
00128  */
00129 template <typename T, class NP, class PP>
00130 void Ray2D<T, NP, PP>::setDirection(const TVector& iDirection)
00131 {
00132     direction_ = iDirection;
00133     NP::normalize(direction_);
00134 }
00135 
00136 
00137 
00138 /** Set direction from origin to look-at point.
00139  */
00140 template <typename T, class NP, class PP>
00141 void Ray2D<T, NP, PP>::lookAt(const TPoint& iLookAt)
00142 {
00143     direction_ = iLookAt - support_;
00144     NP::normalize(direction_);
00145 }
00146 
00147 
00148 
00149 /** Return point on ray by it's parameter.
00150  *  @exception throw an error if parameter is out of range t >= 0, if Bounded is
00151  *             used as @c ParameterPolicy.
00152  *  @return origin + t * direction
00153  */
00154 template <typename T, class NP, class PP>
00155 const typename Ray2D<T, NP, PP>::TPoint
00156 Ray2D<T, NP, PP>::point(TParam iT) const
00157 {
00158     TParameterPolicy::enforceRange(iT, TNumTraits::zero);
00159     return support_ + iT * direction_;
00160 }
00161 
00162 
00163 
00164 /** Return parameter of point on the ray that is the projection of the given point.
00165  *  @warning it can return a (invalid) negative number even if you've used a bounded parameter policy.
00166  */
00167 template <typename T, class NP, class PP>
00168 const typename Ray2D<T, NP, PP>::TValue
00169 Ray2D<T, NP, PP>::t(const TPoint& iPoint) const
00170 {
00171     return NP::divideBySquaredNorm(dot(iPoint - support_, direction_), direction_);
00172 }
00173 
00174 
00175 
00176 /** Project vector on the axis of the ray
00177  */
00178 template <typename T, class NP, class PP>
00179 const typename Ray2D<T, NP, PP>::TVector
00180 Ray2D<T, NP, PP>::project(const TVector& iVector) const
00181 {
00182     return direction_ * NP::divideBySquaredNorm(dot(iVector, direction_), direction_);
00183 }
00184 
00185 
00186 
00187 /** Reject vector against the axis of the ray
00188  */
00189 template <typename T, class NP, class PP>
00190 const typename Ray2D<T, NP, PP>::TVector
00191 Ray2D<T, NP, PP>::reject(const TVector& iVector) const
00192 {
00193     return iVector - project(iVector);
00194 }
00195 
00196 
00197 
00198 /** Reflect vector against the axis of the ray
00199  */
00200 template <typename T, class NP, class PP>
00201 const typename Ray2D<T, NP, PP>::TVector
00202 Ray2D<T, NP, PP>::reflect(const TVector& iVector) const
00203 {
00204     return 2 * project(iVector) - iVector;
00205 }
00206 
00207 
00208 
00209 
00210 /** Project point on the axis of the ray
00211  */
00212 template <typename T, class NP, class PP>
00213 const typename Ray2D<T, NP, PP>::TPoint
00214 Ray2D<T, NP, PP>::project(const TPoint& iPoint) const
00215 {
00216     return support_ + project(iPoint - support_);
00217 }
00218 
00219 
00220 
00221 /** Reject point against the axis of the ray
00222  */
00223 template <typename T, class NP, class PP>
00224 const typename Ray2D<T, NP, PP>::TVector
00225 Ray2D<T, NP, PP>::reject(const TPoint& iPoint) const
00226 {
00227     return reject(iPoint - support_);
00228 }
00229 
00230 
00231 
00232 /** Reject point against the axis of the ray
00233  */
00234 template <typename T, class NP, class PP>
00235 const typename Ray2D<T, NP, PP>::TPoint
00236 Ray2D<T, NP, PP>::reflect(const TPoint& iPoint) const
00237 {
00238     return support_ + reflect(iPoint - support_);
00239 }
00240 
00241 
00242 
00243 /** Return on what side a point is located.
00244  */
00245 template <typename T, class NP, class PP>
00246 const Side Ray2D<T, NP, PP>::classify(const TPoint& iPoint) const
00247 {
00248     T surf = doubleTriangleArea(support_,support_+direction_,iPoint);
00249     if (surf>T(0))
00250         return sLeft;
00251     if (surf<T(0))
00252         return sRight;
00253     return sSurface;
00254 }
00255 
00256 
00257 /** Return true if ray is valid (direction isn't a zero vector).
00258  */
00259 template <typename T, class NP, class PP>
00260 const bool Ray2D<T, NP, PP>::isValid() const
00261 {
00262     return !direction_.isZero();
00263 }
00264 
00265 
00266 
00267 /** @relates lass::prim::Ray2D
00268  */
00269 template<typename T, class NP>
00270 std::ostream& operator<<(std::ostream& ioOStream, const Ray2D<T, NP>& iRay)
00271 {
00272     LASS_ENFORCE(ioOStream) << "{O=" << iRay.origin() << ", D=" << iRay.direction() << "}";
00273     return ioOStream;
00274 }
00275 
00276 
00277 
00278 /** @relates lass::prim::Ray2D
00279  */
00280 template<typename T, class NP>
00281 io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const Ray2D<T, NP>& iRay)
00282 {
00283     LASS_ENFORCE_STREAM(ioOStream)
00284         << "<Ray2D>\n"
00285         << "<support>" << iRay.support() << "</support>\n"
00286         << "<direction>" << iRay.direction() << "</direction>\n"
00287         << "</Ray2D>\n";
00288     return ioOStream;
00289 }
00290 
00291 }
00292 
00293 }
00294 
00295 #endif
00296 
00297 // --- END OF FILE ------------------------------------------------------------------------------

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