Nori  24
vector.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_VECTOR_H)
20 #define __NORI_VECTOR_H
21 
22 #include <nori/common.h>
23 
24 NORI_NAMESPACE_BEGIN
25 
26 /* ===================================================================
27  This file contains a few templates and specializations, which
28  provide 2/3D points, vectors, and normals over different
29  underlying data types. Points, vectors, and normals are distinct
30  in Nori, because they transform differently under homogeneous
31  coordinate transformations.
32  * =================================================================== */
33 
37 template <typename _Scalar, int _Dimension> struct TVector : public Eigen::Matrix<_Scalar, _Dimension, 1> {
38 public:
39  enum {
40  Dimension = _Dimension
41  };
42 
43  typedef _Scalar Scalar;
44  typedef Eigen::Matrix<Scalar, Dimension, 1> Base;
47 
49  TVector(Scalar value = (Scalar) 0) { Base::setConstant(value); }
50 
52  TVector(Scalar x, Scalar y) : Base(x, y) { }
53 
55  TVector(Scalar x, Scalar y, Scalar z) : Base(x, y, z) { }
56 
58  TVector(Scalar x, Scalar y, Scalar z, Scalar w) : Base(x, y, z, w) { }
59 
61  template <typename Derived> TVector(const Eigen::MatrixBase<Derived>& p)
62  : Base(p) { }
63 
65  template <typename Derived> TVector &operator=(const Eigen::MatrixBase<Derived>& p) {
66  this->Base::operator=(p);
67  return *this;
68  }
69 
71  std::string toString() const {
72  std::string result;
73  for (size_t i=0; i<Dimension; ++i) {
74  result += std::to_string(this->coeff(i));
75  if (i+1 < Dimension)
76  result += ", ";
77  }
78  return "[" + result + "]";
79  }
80 };
81 
85 template <typename _Scalar, int _Dimension> struct TPoint : public Eigen::Matrix<_Scalar, _Dimension, 1> {
86 public:
87  enum {
88  Dimension = _Dimension
89  };
90 
91  typedef _Scalar Scalar;
92  typedef Eigen::Matrix<Scalar, Dimension, 1> Base;
95 
97  TPoint(Scalar value = (Scalar) 0) { Base::setConstant(value); }
98 
100  TPoint(Scalar x, Scalar y) : Base(x, y) { }
101 
103  TPoint(Scalar x, Scalar y, Scalar z) : Base(x, y, z) { }
104 
106  TPoint(Scalar x, Scalar y, Scalar z, Scalar w) : Base(x, y, z, w) { }
107 
109  template <typename Derived> TPoint(const Eigen::MatrixBase<Derived>& p)
110  : Base(p) { }
111 
113  template <typename Derived> TPoint &operator=(const Eigen::MatrixBase<Derived>& p) {
114  this->Base::operator=(p);
115  return *this;
116  }
117 
119  std::string toString() const {
120  std::string result;
121  for (size_t i=0; i<Dimension; ++i) {
122  result += std::to_string(this->coeff(i));
123  if (i+1 < Dimension)
124  result += ", ";
125  }
126  return "[" + result + "]";
127  }
128 };
129 
133 struct Normal3f : public Eigen::Matrix<float, 3, 1> {
134 public:
135  enum {
136  Dimension = 3
137  };
138 
139  typedef float Scalar;
140  typedef Eigen::Matrix<Scalar, Dimension, 1> Base;
143 
144 
146  Normal3f(Scalar value = 0.0f) { Base::setConstant(value); }
147 
149  Normal3f(Scalar x, Scalar y, Scalar z) : Base(x, y, z) { }
150 
152  template <typename Derived> Normal3f(const Eigen::MatrixBase<Derived>& p)
153  : Base(p) { }
154 
156  template <typename Derived> Normal3f &operator=(const Eigen::MatrixBase<Derived>& p) {
157  this->Base::operator=(p);
158  return *this;
159  }
160 
162  std::string toString() const {
163  return tfm::format("[%f, %f, %f]", coeff(0), coeff(1), coeff(2));
164  }
165 };
166 
168 extern void coordinateSystem(const Vector3f &a, Vector3f &b, Vector3f &c);
169 
170 NORI_NAMESPACE_END
171 
172 #endif /* __NORI_VECTOR_H */
173 
3-dimensional surface normal representation
Definition: vector.h:133
Normal3f & operator=(const Eigen::MatrixBase< Derived > &p)
Assign a normal from MatrixBase (needed to play nice with Eigen)
Definition: vector.h:156
std::string toString() const
Return a human-readable string summary.
Definition: vector.h:162
Normal3f(Scalar value=0.0f)
Create a new normal with constant component vlaues.
Definition: vector.h:146
Normal3f(Scalar x, Scalar y, Scalar z)
Create a new 3D normal.
Definition: vector.h:149
Normal3f(const Eigen::MatrixBase< Derived > &p)
Construct a normal from MatrixBase (needed to play nice with Eigen)
Definition: vector.h:152
Generic N-dimensional point data structure based on Eigen::Matrix.
Definition: vector.h:85
TPoint & operator=(const Eigen::MatrixBase< Derived > &p)
Assign a point from MatrixBase (needed to play nice with Eigen)
Definition: vector.h:113
std::string toString() const
Return a human-readable string summary.
Definition: vector.h:119
TPoint(Scalar value=(Scalar) 0)
Create a new point with constant component vlaues.
Definition: vector.h:97
TPoint(Scalar x, Scalar y, Scalar z)
Create a new 3D point (type error if Dimension != 3)
Definition: vector.h:103
TPoint(Scalar x, Scalar y)
Create a new 2D point (type error if Dimension != 2)
Definition: vector.h:100
TPoint(Scalar x, Scalar y, Scalar z, Scalar w)
Create a new 4D point (type error if Dimension != 4)
Definition: vector.h:106
TPoint(const Eigen::MatrixBase< Derived > &p)
Construct a point from MatrixBase (needed to play nice with Eigen)
Definition: vector.h:109
Generic N-dimensional vector data structure based on Eigen::Matrix.
Definition: vector.h:37
TVector(Scalar x, Scalar y, Scalar z)
Create a new 3D vector (type error if Dimension != 3)
Definition: vector.h:55
std::string toString() const
Return a human-readable string summary.
Definition: vector.h:71
TVector(Scalar x, Scalar y, Scalar z, Scalar w)
Create a new 4D vector (type error if Dimension != 4)
Definition: vector.h:58
TVector & operator=(const Eigen::MatrixBase< Derived > &p)
Assign a vector from MatrixBase (needed to play nice with Eigen)
Definition: vector.h:65
TVector(Scalar x, Scalar y)
Create a new 2D vector (type error if Dimension != 2)
Definition: vector.h:52
TVector(const Eigen::MatrixBase< Derived > &p)
Construct a vector from MatrixBase (needed to play nice with Eigen)
Definition: vector.h:61
TVector(Scalar value=(Scalar) 0)
Create a new vector with constant component vlaues.
Definition: vector.h:49