Hermes
normal.h
Go to the documentation of this file.
1 
32 #ifndef HERMES_GEOMETRY_NORMAL_H
33 #define HERMES_GEOMETRY_NORMAL_H
34 
35 #include <iostream>
36 #include <hermes/geometry/vector.h>
37 
38 namespace hermes {
39 
40 // *********************************************************************************************************************
41 // Normal2
42 // *********************************************************************************************************************
45 template<typename T> class Normal2 : public MathElement<T, 2u> {
46  static_assert(std::is_same<T, f32>::value || std::is_same<T, f64>::value ||
47  std::is_same<T, float>::value || std::is_same<T, double>::value,
48  "Normal2 must hold an float type!");
49 public:
50  // *******************************************************************************************************************
51  // CONSTRUCTORS
52  // *******************************************************************************************************************
58  HERMES_DEVICE_CALLABLE Normal2(T _x, T _y) : x(_x), y(_y) {}
62  // *******************************************************************************************************************
63  // OPERATORS
64  // *******************************************************************************************************************
65  // casting
68  HERMES_DEVICE_CALLABLE explicit operator Vector2<T>() const {
69  return Vector2<T>(x, y);
70  }
71  // arithmetic
72  HERMES_DEVICE_CALLABLE Normal2 operator-() const { return Normal2(-x, -y); }
73  HERMES_DEVICE_CALLABLE Normal2 &operator*=(T f) {
74  x *= f;
75  y *= f;
76  return *this;
77  }
78  HERMES_DEVICE_CALLABLE Normal2 &operator/=(T f) {
79  x /= f;
80  y /= f;
81  return *this;
82  }
83  // *******************************************************************************************************************
84  // PUBLIC FIELDS
85  // *******************************************************************************************************************
86  T x{0};
87  T y{0};
88 };
89 
90 // *********************************************************************************************************************
91 // Normal3
92 // *********************************************************************************************************************
95 template<typename T> class Normal3 : MathElement<T, 3u> {
96  static_assert(std::is_same<T, f32>::value || std::is_same<T, f64>::value ||
97  std::is_same<T, float>::value ||
98  std::is_same<T, double>::value,
99  "Normal3 must hold an float type!");
100 public:
101  // *******************************************************************************************************************
102  // STATIC METHODS
103  // *******************************************************************************************************************
104  // *******************************************************************************************************************
105  // CONSTRUCTORS
106  // *******************************************************************************************************************
113  HERMES_DEVICE_CALLABLE Normal3(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {}
116  HERMES_DEVICE_CALLABLE explicit Normal3(const Vector3 <T> &v) : x(v.x), y(v.y), z(v.z) {}
117  // *******************************************************************************************************************
118  // OPERATORS
119  // *******************************************************************************************************************
120  // casting
123  HERMES_DEVICE_CALLABLE explicit operator Vector3<T>() const {
124  return Vector3<T>(x, y, z);
125  }
126  // arithmetic
127  HERMES_DEVICE_CALLABLE Normal3 operator-() const { return Normal3(-x, -y, -z); }
128  HERMES_DEVICE_CALLABLE Normal3 &operator+=(const Vector3<T>& v) {
129  x += v.x;
130  y += v.y;
131  z += v.z;
132  return *this;
133  }
134  HERMES_DEVICE_CALLABLE Normal3 &operator*=(T f) {
135  x *= f;
136  y *= f;
137  z *= f;
138  return *this;
139  }
140  HERMES_DEVICE_CALLABLE Normal3 &operator/=(T f) {
141  x /= f;
142  y /= f;
143  z /= f;
144  return *this;
145  }
146  // boolean
147  HERMES_DEVICE_CALLABLE bool operator!=(const Normal3 &n) const {
148  return n.x != x || n.y != y || n.z != z;
149  }
150  // *******************************************************************************************************************
151  // GEOMETRY
152  // *******************************************************************************************************************
157  return reflect(v, *this);
158  }
163  return project(v, *this);
164  }
169  // hermes::tangential(Vector3<T>(x, y, z), a, b);
170  }
171 
172  T x{0};
173  T y{0};
174  T z{0};
175 };
176 
177 // *********************************************************************************************************************
178 // FUNCTIONS
179 // *********************************************************************************************************************
184 template<typename T>
186  return a - 2 * dot(a, Vector2<T>(n)) * Vector2<T>(n);
187 }
192 template<typename T>
194  return v - dot(v, Vector2<T>(n)) * Vector2<T>(n);
195 }
200 template<typename T>
202  T d = normal.x * normal.x + normal.y * normal.y + normal.z * normal.z;
203  if (d == 0.f)
204  return normal;
205  return Normal3<T>(normal.x / d, normal.y / d, normal.z / d);
206 }
211 template<typename T>
213  return Normal3<T>(std::abs(normal.x), std::abs(normal.y), std::abs(normal.z));
214 }
219 template<typename T>
221  return a - 2 * dot(a, Vector3<T>(n)) * Vector3<T>(n);
222 }
227 template<typename S>
229  return v - dot(v, Vector3<S>(n)) * Vector3<S>(n);
230 }
236 template<typename T>
238  return n.x * v.x + n.y * v.y + n.z * v.z;
239 }
245 template<typename T>
247  return n.x * v.x + n.y * v.y + n.z * v.z;
248 }
254 template<typename T>
256  return (dot(v, n) < 0.f) ? -v : v;
257 }
258 
259 // *********************************************************************************************************************
260 // IO
261 // *********************************************************************************************************************
267 template<typename T>
268 std::ostream &operator<<(std::ostream &os, const Normal2<T> &n) {
269  os << "[Normal3] " << n.x << " " << n.y << std::endl;
270  return os;
271 }
277 template<typename T>
278 std::ostream &operator<<(std::ostream &os, const Normal3<T> &n) {
279  os << "[Normal3] " << n.x << " " << n.y << " " << n.z << std::endl;
280  return os;
281 }
282 
283 // *********************************************************************************************************************
284 // TYPEDEFS
285 // *********************************************************************************************************************
286 using normal2 = Normal2<real_t>;
287 using normal2f = Normal2<float>;
288 using normal2d = Normal2<double>;
289 using normal3 = Normal3<real_t>;
290 using normal3f = Normal3<float>;
291 using normal3d = Normal3<double>;
292 
293 } // namespace hermes
294 
295 #endif
296 
Interface used by all basic geometric entities.
Definition: math_element.h:44
Geometric 2-dimensional normal (nx, ny)
Definition: normal.h:45
HERMES_DEVICE_CALLABLE Normal2()
Default constructor.
Definition: normal.h:54
T y
1-th normal component
Definition: normal.h:87
T x
0-th normal component
Definition: normal.h:86
HERMES_DEVICE_CALLABLE Normal2(T _x, T _y)
Constructs from component values.
Definition: normal.h:58
HERMES_DEVICE_CALLABLE Normal2(const Vector2< T > &v)
Constructs from vector.
Definition: normal.h:61
Geometric 3-dimensional normal (nx, ny, nz)
Definition: normal.h:95
HERMES_DEVICE_CALLABLE Normal3()
Default constructor.
Definition: normal.h:108
T x
0-th normal component
Definition: normal.h:172
HERMES_DEVICE_CALLABLE Vector3< T > reflect(const Vector3< T > &v)
reflects v from this
Definition: normal.h:156
HERMES_DEVICE_CALLABLE Vector3< T > project(const Vector3< T > &v)
projects v on the surface with this normal
Definition: normal.h:162
HERMES_DEVICE_CALLABLE Normal3(T _x, T _y, T _z)
Constructs from component values.
Definition: normal.h:113
T y
1-th normal component
Definition: normal.h:173
T z
2-th normal component
Definition: normal.h:174
HERMES_DEVICE_CALLABLE Normal3(const Vector3< T > &v)
Constructs from vector.
Definition: normal.h:116
HERMES_DEVICE_CALLABLE void tangential(Vector3< T > &a, Vector3< T > &b)
compute the two orthogonal-tangential vectors from this
Definition: normal.h:168
Geometric 2-dimensional vector (x, y)
Definition: vector.h:54
Geometric 3-dimensional vector (x, y, z)
Definition: vector.h:166
T x
0-th component
Definition: vector.h:415
T z
2-th component
Definition: vector.h:417
T y
1-th component
Definition: vector.h:416
#define HERMES_DEVICE_CALLABLE
Specifies that the function can be called from both host and device sides.
Definition: defs.h:45
HERMES_DEVICE_CALLABLE Normal3< T > abs(const Normal3< T > &normal)
Computes absolute normal components.
Definition: normal.h:212
HERMES_DEVICE_CALLABLE Normal3< T > normalize(const Normal3< T > &normal)
Computes normalized copy.
Definition: normal.h:201
HERMES_DEVICE_CALLABLE Vector2< T > project(const Vector2< T > &v, const Normal2< T > &n)
projects v on the surface with normal n
Definition: normal.h:193
HERMES_DEVICE_CALLABLE T dot(const Normal3< T > &n, const Vector3< T > &v)
Computes dot product with vector.
Definition: normal.h:237
HERMES_DEVICE_CALLABLE Vector3< T > faceForward(const Vector3< T > &v, const Normal3< T > &n)
Definition: normal.h:255
HERMES_DEVICE_CALLABLE Vector2< T > reflect(const Vector2< T > &a, const Normal2< T > &n)
reflects a on n
Definition: normal.h:185
Geometric vector classes.