Nori  23
ray.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob
5 
6  Nori 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  Nori 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 #if !defined(__NORI_RAY_H)
20 #define __NORI_RAY_H
21 
22 #include <nori/vector.h>
23 
24 NORI_NAMESPACE_BEGIN
25 
38 template <typename _PointType, typename _VectorType> struct TRay {
39  typedef _PointType PointType;
40  typedef _VectorType VectorType;
41  typedef typename PointType::Scalar Scalar;
42 
43  PointType o;
44  VectorType d;
45  VectorType dRcp;
46  Scalar mint;
47  Scalar maxt;
48 
50  TRay() : mint(Epsilon),
51  maxt(std::numeric_limits<Scalar>::infinity()) { }
52 
54  TRay(const PointType &o, const VectorType &d) : o(o), d(d),
55  mint(Epsilon), maxt(std::numeric_limits<Scalar>::infinity()) {
56  update();
57  }
58 
60  TRay(const PointType &o, const VectorType &d,
61  Scalar mint, Scalar maxt) : o(o), d(d), mint(mint), maxt(maxt) {
62  update();
63  }
64 
66  TRay(const TRay &ray)
67  : o(ray.o), d(ray.d), dRcp(ray.dRcp),
68  mint(ray.mint), maxt(ray.maxt) { }
69 
71  TRay(const TRay &ray, Scalar mint, Scalar maxt)
72  : o(ray.o), d(ray.d), dRcp(ray.dRcp), mint(mint), maxt(maxt) { }
73 
75  void update() {
76  dRcp = d.cwiseInverse();
77  }
78 
80  PointType operator() (Scalar t) const { return o + t * d; }
81 
83  TRay reverse() const {
84  TRay result;
85  result.o = o; result.d = -d; result.dRcp = -dRcp;
86  result.mint = mint; result.maxt = maxt;
87  return result;
88  }
89 
91  std::string toString() const {
92  return tfm::format(
93  "Ray[\n"
94  " o = %s,\n"
95  " d = %s,\n"
96  " mint = %f,\n"
97  " maxt = %f\n"
98  "]", o.toString(), d.toString(), mint, maxt);
99  }
100 };
101 
102 NORI_NAMESPACE_END
103 
104 #endif /* __NORI_RAY_H */
105 
Simple n-dimensional ray segment data structure.
Definition: ray.h:38
VectorType d
Ray direction.
Definition: ray.h:44
Scalar mint
Minimum position on the ray segment.
Definition: ray.h:46
std::string toString() const
Return a human-readable string summary of this ray.
Definition: ray.h:91
TRay(const TRay &ray, Scalar mint, Scalar maxt)
Copy a ray, but change the covered segment of the copy.
Definition: ray.h:71
TRay(const TRay &ray)
Copy constructor.
Definition: ray.h:66
void update()
Update the reciprocal ray directions after changing 'd'.
Definition: ray.h:75
PointType operator()(Scalar t) const
Return the position of a point along the ray.
Definition: ray.h:80
Scalar maxt
Maximum position on the ray segment.
Definition: ray.h:47
TRay(const PointType &o, const VectorType &d)
Construct a new ray.
Definition: ray.h:54
TRay(const PointType &o, const VectorType &d, Scalar mint, Scalar maxt)
Construct a new ray.
Definition: ray.h:60
TRay reverse() const
Return a ray that points into the opposite direction.
Definition: ray.h:83
VectorType dRcp
Componentwise reciprocals of the ray direction.
Definition: ray.h:45
TRay()
Construct a new ray.
Definition: ray.h:50
PointType o
Ray origin.
Definition: ray.h:43