library of assembled shared sources

http://lass.cocamware.com

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

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