Nori
|
00001 /* 00002 This file is part of Nori, a simple educational ray tracer 00003 00004 Copyright (c) 2012 by Wenzel Jakob and Steve Marschner. 00005 00006 Nori is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License Version 3 00008 as published by the Free Software Foundation. 00009 00010 Nori is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 #if !defined(__RAY_H) 00020 #define __RAY_H 00021 00022 #include <nori/vector.h> 00023 00024 NORI_NAMESPACE_BEGIN 00025 00026 /** 00027 * \brief Simple n-dimensional ray segment data structure 00028 * 00029 * Along with the ray origin and direction, this data structure additionally 00030 * stores a ray segment [mint, maxt] (whose entries may include positive/negative 00031 * infinity), as well as the componentwise reciprocals of the ray direction. 00032 * That is just done for convenience, as these values are frequently required. 00033 * 00034 * \remark Important: be careful when changing the ray direction. You must 00035 * call \ref update() to compute the componentwise reciprocals as well, or Nori's 00036 * ray-triangle intersection code will go haywire. 00037 */ 00038 template <typename _PointType, typename _VectorType> struct TRay { 00039 typedef _PointType PointType; 00040 typedef _VectorType VectorType; 00041 typedef typename PointType::Scalar Scalar; 00042 00043 PointType o; ///< Ray origin 00044 VectorType d; ///< Ray direction 00045 VectorType dRcp; ///< Componentwise reciprocals of the ray direction 00046 Scalar mint; ///< Minimum position on the ray segment 00047 Scalar maxt; ///< Maximum position on the ray segment 00048 00049 /// Construct a new ray 00050 inline TRay() : mint(Epsilon), 00051 maxt(std::numeric_limits<Scalar>::infinity()) { } 00052 00053 /// Construct a new ray 00054 inline TRay(const PointType &o, const VectorType &d) : o(o), d(d), 00055 mint(Epsilon), maxt(std::numeric_limits<Scalar>::infinity()) { 00056 update(); 00057 } 00058 00059 /// Construct a new ray 00060 inline TRay(const PointType &o, const VectorType &d, 00061 Scalar mint, Scalar maxt) : o(o), d(d), mint(mint), maxt(maxt) { 00062 update(); 00063 } 00064 00065 /// Copy constructor 00066 inline TRay(const TRay &ray) 00067 : o(ray.o), d(ray.d), dRcp(ray.dRcp), 00068 mint(ray.mint), maxt(ray.maxt) { } 00069 00070 /// Copy a ray, but change the covered segment of the copy 00071 inline TRay(const TRay &ray, Scalar mint, Scalar maxt) 00072 : o(ray.o), d(ray.d), dRcp(ray.dRcp), mint(mint), maxt(maxt) { } 00073 00074 /// Update the reciprocal ray directions after changing 'd' 00075 inline void update() { 00076 dRcp = d.cwiseInverse(); 00077 } 00078 00079 /// Return the position of a point along the ray 00080 inline PointType operator() (Scalar t) const { return o + t * d; } 00081 00082 /// Return a ray that points into the opposite direction 00083 inline Ray3f reverse() const { 00084 Ray3f result; 00085 result.o = o; result.d = -d; result.dRcp = -dRcp; 00086 result.mint = mint; result.maxt = maxt; 00087 return result; 00088 } 00089 00090 /// Return a human-readable string summary of this ray 00091 inline QString toString() const { 00092 return QString( 00093 "Ray[\n" 00094 " o = %1,\n" 00095 " d = %2,\n" 00096 " mint = %3,\n" 00097 " maxt = %4\n" 00098 "]") 00099 .arg(o.toString()) 00100 .arg(d.toString()) 00101 .arg(mint) 00102 .arg(maxt); 00103 } 00104 }; 00105 00106 NORI_NAMESPACE_END 00107 00108 #endif /* __RAY_H */ 00109