Nori  24
transform.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_TRANSFORM_H)
20 #define __NORI_TRANSFORM_H
21 
22 #include <nori/common.h>
23 #include <nori/ray.h>
24 
25 NORI_NAMESPACE_BEGIN
26 
35 struct Transform {
36 public:
38  Transform() :
39  m_transform(Eigen::Matrix4f::Identity()),
40  m_inverse(Eigen::Matrix4f::Identity()) { }
41 
43  Transform(const Eigen::Matrix4f &trafo);
44 
46  Transform(const Eigen::Matrix4f &trafo, const Eigen::Matrix4f &inv)
47  : m_transform(trafo), m_inverse(inv) { }
48 
50  const Eigen::Matrix4f &getMatrix() const {
51  return m_transform;
52  }
53 
55  const Eigen::Matrix4f &getInverseMatrix() const {
56  return m_inverse;
57  }
58 
60  Transform inverse() const {
61  return Transform(m_inverse, m_transform);
62  }
63 
65  Transform operator*(const Transform &t) const;
66 
68  Vector3f operator*(const Vector3f &v) const {
69  return m_transform.topLeftCorner<3,3>() * v;
70  }
71 
73  Normal3f operator*(const Normal3f &n) const {
74  return m_inverse.topLeftCorner<3, 3>().transpose() * n;
75  }
76 
78  Point3f operator*(const Point3f &p) const {
79  Vector4f result = m_transform * Vector4f(p[0], p[1], p[2], 1.0f);
80  return result.head<3>() / result.w();
81  }
82 
84  Ray3f operator*(const Ray3f &r) const {
85  return Ray3f(
86  operator*(r.o),
87  operator*(r.d),
88  r.mint, r.maxt
89  );
90  }
91 
93  std::string toString() const;
94 private:
95  Eigen::Matrix4f m_transform;
96  Eigen::Matrix4f m_inverse;
97 };
98 
99 NORI_NAMESPACE_END
100 
101 #endif /* __NORI_TRANSFORM_H */
3-dimensional surface normal representation
Definition: vector.h:133
VectorType d
Ray direction.
Definition: ray.h:44
Scalar mint
Minimum position on the ray segment.
Definition: ray.h:46
Scalar maxt
Maximum position on the ray segment.
Definition: ray.h:47
PointType o
Ray origin.
Definition: ray.h:43
Homogeneous coordinate transformation.
Definition: transform.h:35
Point3f operator*(const Point3f &p) const
Transform a point by an arbitrary matrix in homogeneous coordinates.
Definition: transform.h:78
Transform operator*(const Transform &t) const
Concatenate with another transform.
Definition: common.cpp:246
std::string toString() const
Return a string representation.
Definition: common.cpp:240
Ray3f operator*(const Ray3f &r) const
Apply the homogeneous transformation to a ray.
Definition: transform.h:84
Normal3f operator*(const Normal3f &n) const
Apply the homogeneous transformation to a 3D normal.
Definition: transform.h:73
const Eigen::Matrix4f & getInverseMatrix() const
Return the inverse of the underlying matrix.
Definition: transform.h:55
Transform()
Create the identity transform.
Definition: transform.h:38
Transform inverse() const
Return the inverse transformation.
Definition: transform.h:60
const Eigen::Matrix4f & getMatrix() const
Return the underlying matrix.
Definition: transform.h:50
Transform(const Eigen::Matrix4f &trafo, const Eigen::Matrix4f &inv)
Create a new transform instance for the given matrix and its inverse.
Definition: transform.h:46
Vector3f operator*(const Vector3f &v) const
Apply the homogeneous transformation to a 3D vector.
Definition: transform.h:68