Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ray.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_RAY_H_)
21 #define __MITSUBA_CORE_RAY_H_
22 
23 #include <mitsuba/mitsuba.h>
24 
26 
27 /** \brief Simple n-dimensional ray data structure with
28  * minimum / maximum extent information.
29  *
30  * The somewhat peculiar ordering of the attributes is due
31  * to alignment purposes and should not be changed.
32  *
33  * \ingroup libcore
34  * \ingroup libpython
35 */
36 template <typename _PointType, typename _VectorType> struct TRay {
37  typedef _PointType PointType;
38  typedef _VectorType VectorType;
39  typedef typename PointType::Scalar Scalar;
40 
41  /* The somewhat peculiar ordering of the attributes is for
42  alignment purposes in the 3D case and should not be changed. */
43 
44  PointType o; ///< Ray origin
45  Scalar mint; ///< Minimum range for intersection tests
46  VectorType d; ///< Ray direction
47  Scalar maxt; ///< Maximum range for intersection tests
48  VectorType dRcp; ///< Componentwise reciprocals of the ray direction
49  Float time; ///< Time value associated with this ray
50 
51  /// Construct a new ray
52  inline TRay() : mint(Epsilon),
53  maxt(std::numeric_limits<Scalar>::infinity()), time(0) {
54  }
55 
56  /// Copy constructor (1)
57  inline TRay(const TRay &ray)
58  : o(ray.o), mint(ray.mint), d(ray.d), maxt(ray.maxt),
59  dRcp(ray.dRcp), time(ray.time) {
60  }
61 
62  /// Copy constructor (2)
63  inline TRay(const TRay &ray, Scalar mint, Scalar maxt)
64  : o(ray.o), mint(mint), d(ray.d), maxt(maxt),
65  dRcp(ray.dRcp), time(ray.time) { }
66 
67  /// Construct a new ray, while not specifying a direction yet
68  inline TRay(const PointType &o, Scalar time) : o(o), mint(Epsilon),
69  maxt(std::numeric_limits<Scalar>::infinity()), time(time) { }
70 
71  /// Construct a new ray
72  inline TRay(const PointType &o, const VectorType &d, Scalar time)
73  : o(o), mint(Epsilon), d(d),
74  maxt(std::numeric_limits<Scalar>::infinity()), time(time) {
75 #ifdef MTS_DEBUG_FP
76  bool state = disableFPExceptions();
77 #endif
78  for (int i=0; i<3; ++i)
79  dRcp[i] = (Scalar) 1 / d[i];
80 #ifdef MTS_DEBUG_FP
81  restoreFPExceptions(state);
82 #endif
83  }
84 
85  /// Construct a new ray
86  inline TRay(const PointType &o, const VectorType &d, Scalar mint, Scalar maxt,
87  Scalar time) : o(o), mint(mint), d(d), maxt(maxt), time(time) {
88 #ifdef MTS_DEBUG_FP
89  bool state = disableFPExceptions();
90 #endif
91  for (int i=0; i<3; ++i)
92  dRcp[i] = (Scalar) 1 / d[i];
93 #ifdef MTS_DEBUG_FP
94  restoreFPExceptions(state);
95 #endif
96  }
97 
98  /// Set the origin
99  inline void setOrigin(const PointType &pos) { o = pos; }
100 
101  /// Set the time
102  inline void setTime(Scalar tval) { time = tval; }
103 
104  /// Set the direction and update the reciprocal
105  inline void setDirection(const VectorType &dir) {
106  d = dir;
107 #ifdef MTS_DEBUG_FP
108  bool state = disableFPExceptions();
109 #endif
110  for (int i=0; i<3; ++i)
111  dRcp[i] = (Scalar) 1 / dir[i];
112 #ifdef MTS_DEBUG_FP
113  restoreFPExceptions(state);
114 #endif
115  }
116 
117  /**
118  * \brief Return the position of a point along the ray
119  *
120  * \remark In the Python bindings, this operator is
121  * exposed as a function named \c eval -- i.e.
122  * position lookups should be written as \c ray.eval(t)
123  */
124  inline PointType operator() (Scalar t) const { return o + t * d; }
125 
126  /// Return a string representation of this ray
127  inline std::string toString() const {
128  std::ostringstream oss;
129  oss << "Ray[origin=" << o.toString() << ", direction="
130  << d.toString() << ", mint=" << mint
131  << ", maxt=" << maxt << ", time=" << time << "]";
132  return oss.str();
133  }
134 };
135 
136 /** \brief %Ray differential -- enhances the basic ray class with
137  information about the rays of adjacent pixels on the view plane
138  \ingroup libcore
139 */
140 struct RayDifferential : public Ray {
141  Point rxOrigin, ryOrigin;
142  Vector rxDirection, ryDirection;
144 
146  : hasDifferentials(false) {
147  }
148 
149  inline RayDifferential(const Point &p, const Vector &d, Float time)
150  : Ray(p, d, time), hasDifferentials(false) {
151  }
152 
153  inline explicit RayDifferential(const Ray &ray)
154  : Ray(ray), hasDifferentials(false) {
155  }
156 
157  inline RayDifferential(const RayDifferential &ray)
158  : Ray(ray), rxOrigin(ray.rxOrigin), ryOrigin(ray.ryOrigin),
159  rxDirection(ray.rxDirection), ryDirection(ray.ryDirection),
160  hasDifferentials(ray.hasDifferentials) {
161  }
162 
163  void scaleDifferential(Float amount) {
164  rxOrigin = o + (rxOrigin - o) * amount;
165  ryOrigin = o + (ryOrigin - o) * amount;
166  rxDirection = d + (rxDirection - d) * amount;
167  ryDirection = d + (ryDirection - d) * amount;
168  }
169 
170  void scaleDifferentialUV(const Vector2 amountUV) {
171  rxOrigin = o + (rxOrigin - o) * amountUV.x;
172  ryOrigin = o + (ryOrigin - o) * amountUV.y;
173  rxDirection = d + (rxDirection - d) * amountUV.x;
174  ryDirection = d + (ryDirection - d) * amountUV.y;
175  }
176 
177  inline void operator=(const RayDifferential &ray) {
178  o = ray.o;
179  mint = ray.mint;
180  d = ray.d;
181  maxt = ray.maxt;
182 #ifdef MTS_DEBUG_FP
183  bool state = disableFPExceptions();
184 #endif
185  dRcp = ray.dRcp;
186 #ifdef MTS_DEBUG_FP
187  restoreFPExceptions(state);
188 #endif
189  hasDifferentials = ray.hasDifferentials;
190  rxOrigin = ray.rxOrigin;
191  ryOrigin = ray.ryOrigin;
192  rxDirection = ray.rxDirection;
193  ryDirection = ray.ryDirection;
194  }
195 
196  inline void operator=(const Ray &ray) {
197  o = ray.o;
198  mint = ray.mint;
199  d = ray.d;
200  maxt = ray.maxt;
201 #ifdef MTS_DEBUG_FP
202  bool state = disableFPExceptions();
203 #endif
204  dRcp = ray.dRcp;
205 #ifdef MTS_DEBUG_FP
206  restoreFPExceptions(state);
207 #endif
208  hasDifferentials = false;
209  }
210 
211  /// Return a string representation of this ray
212  inline std::string toString() const {
213  std::ostringstream oss;
214  oss << "RayDifferential[" << endl
215  << " origin = " << o.toString() << "," << endl
216  << " direction = " << d.toString() << "," << endl
217  << " mint = " << mint << "," << endl
218  << " maxt = " << maxt << "," << endl
219  << " time = " << time << "," << endl
220  << " hasDifferentials = " << hasDifferentials << "," << endl
221  << " rxOrigin = " << rxOrigin.toString() << "," << endl
222  << " ryOrigin = " << ryOrigin.toString() << "," << endl
223  << " rxDirection = " << rxDirection.toString() << "," << endl
224  << " ryDirection = " << ryDirection.toString() << endl
225  << "]" << endl;
226  return oss.str();
227  }
228 };
229 
231 
232 #endif /* __MITSUBA_CORE_RAY_H_ */
void scaleDifferential(Float amount)
Definition: ray.h:163
#define Epsilon
Definition: constants.h:28
Float time
Time value associated with this ray.
Definition: ray.h:49
Scalar maxt
Maximum range for intersection tests.
Definition: ray.h:47
RayDifferential(const Ray &ray)
Definition: ray.h:153
TRay(const PointType &o, const VectorType &d, Scalar mint, Scalar maxt, Scalar time)
Construct a new ray.
Definition: ray.h:86
void operator=(const Ray &ray)
Definition: ray.h:196
std::string toString() const
Return a string representation of this ray.
Definition: ray.h:212
bool hasDifferentials
Definition: ray.h:143
MTS_EXPORT_CORE void restoreFPExceptions(bool state)
Restore floating point exceptions to the specified state.
_PointType PointType
Definition: ray.h:37
RayDifferential(const Point &p, const Vector &d, Float time)
Definition: ray.h:149
RayDifferential(const RayDifferential &ray)
Definition: ray.h:157
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
VectorType d
Ray direction.
Definition: ray.h:46
PointType o
Ray origin.
Definition: ray.h:44
Ray differential – enhances the basic ray class with information about the rays of adjacent pixels on...
Definition: ray.h:140
Simple n-dimensional ray data structure with minimum / maximum extent information.
Definition: ray.h:36
RayDifferential()
Definition: ray.h:145
Vector rxDirection
Definition: ray.h:142
TRay()
Construct a new ray.
Definition: ray.h:52
PointType::Scalar Scalar
Definition: ray.h:39
Point ryOrigin
Definition: ray.h:141
Point rxOrigin
Definition: ray.h:141
Definition: fwd.h:65
void scaleDifferentialUV(const Vector2 amountUV)
Definition: ray.h:170
Definition: fwd.h:96
Vector ryDirection
Definition: ray.h:142
void operator=(const RayDifferential &ray)
Definition: ray.h:177
std::string toString() const
Return a string representation of this ray.
Definition: ray.h:127
TRay(const PointType &o, const VectorType &d, Scalar time)
Construct a new ray.
Definition: ray.h:72
void setDirection(const VectorType &dir)
Set the direction and update the reciprocal.
Definition: ray.h:105
void setTime(Scalar tval)
Set the time.
Definition: ray.h:102
TRay(const PointType &o, Scalar time)
Construct a new ray, while not specifying a direction yet.
Definition: ray.h:68
Definition: fwd.h:100
TRay(const TRay &ray)
Copy constructor (1)
Definition: ray.h:57
TRay(const TRay &ray, Scalar mint, Scalar maxt)
Copy constructor (2)
Definition: ray.h:63
void setOrigin(const PointType &pos)
Set the origin.
Definition: ray.h:99
_VectorType VectorType
Definition: ray.h:38
Definition: fwd.h:95
#define MTS_NAMESPACE_END
Definition: platform.h:138
VectorType dRcp
Componentwise reciprocals of the ray direction.
Definition: ray.h:48
Scalar mint
Minimum range for intersection tests.
Definition: ray.h:45
MTS_EXPORT_CORE bool disableFPExceptions()
Disable floating point exceptions.