SpinParser  1.0
Geometry.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 #include <cstring>
11 #include <cmath>
12 
13 namespace geometry
14 {
15  #pragma region vector
16 
21  template <typename T> struct Vec3
22  {
26  Vec3() {};
27 
33  Vec3(const T &xyz) : x(xyz), y(xyz), z(xyz) {};
34 
42  Vec3(const T &x, const T &y, const T &z) : x(x), y(y), z(z) {};
43 
44  T x;
45  T y;
46  T z;
47 
53  T norm() const
54  {
55  return std::sqrt(x * x + y * y + z * z);
56  }
57 
64  {
65  T n = norm();
66  x /= n;
67  y /= n;
68  z /= n;
69  return *this;
70  }
71  };
72 
81  template <typename T> T dot(const Vec3<T> &v, const Vec3<T> &w)
82  {
83  return v.x * w.x + v.y * w.y + v.z * w.z;
84  }
85 
94  template <typename T> Vec3<T> cross(const Vec3<T> &v, const Vec3<T> &w)
95  {
96  return Vec3<T>(v.y * w.z - v.z * w.y, v.z * w.x - v.x * w.z, v.x * w.y - v.y * w.x);
97  }
98 
107  template <typename T> Vec3<T> operator*(const T lhs, const Vec3<T> &rhs)
108  {
109  return Vec3<T>(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
110  }
111 
120  template <typename T> Vec3<T> operator+(const Vec3<T> &lhs, const Vec3<T> &rhs)
121  {
122  return Vec3<T>(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
123  }
124 
133  template <typename T> Vec3<T> operator-(const Vec3<T> &lhs, const Vec3<T> &rhs)
134  {
135  return Vec3<T>(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
136  }
137 
145  template <typename T> Vec3<T> operator-(const Vec3<T> &rhs)
146  {
147  return Vec3<T>(-rhs.x, -rhs.y, -rhs.z);
148  }
149 
158  template <typename T> bool operator==(const Vec3<T> &lhs, const Vec3<T> &rhs)
159  {
160  return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
161  }
162 
168  template <typename T> struct Vec4
169  {
173  Vec4() {};
174 
180  Vec4(const T &xyz) : x(xyz), y(xyz), z(xyz), w(1) {};
181 
189  Vec4(const T &x, const T &y, const T &z) : x(x), y(y), z(z), w(1) {};
190 
199  Vec4(const T &x, const T &y, const T &z, const T &w) : x(x), y(y), z(z), w(w) {};
200 
201  T x;
202  T y;
203  T z;
204  T w;
205 
211  operator Vec3<T>() const
212  {
213  return Vec3<T>(x, y, z);
214  }
215  };
216  #pragma endregion
217 
218  #pragma region matrix
219 
224  template <typename T> struct Mat3
225  {
229  Mat3() {};
230 
236  Mat3(const T &m)
237  {
238  data[0][0] = m;
239  data[0][1] = m;
240  data[0][2] = m;
241  data[1][0] = m;
242  data[1][1] = m;
243  data[1][2] = m;
244  data[2][0] = m;
245  data[2][1] = m;
246  data[2][2] = m;
247  };
248 
256  Mat3(const Vec3<T> &a0, const Vec3<T> &a1, const Vec3<T> &a2)
257  {
258  data[0][0] = a0.x;
259  data[0][1] = a1.x;
260  data[0][2] = a2.x;
261  data[1][0] = a0.y;
262  data[1][1] = a1.y;
263  data[1][2] = a2.y;
264  data[2][0] = a0.z;
265  data[2][1] = a1.z;
266  data[2][2] = a2.z;
267  }
268 
282  Mat3(const T &m00, const T &m01, const T &m02, const T &m10, const T &m11, const T &m12, const T &m20, const T &m21, const T &m22)
283  {
284  data[0][0] = m00;
285  data[0][1] = m01;
286  data[0][2] = m02;
287  data[1][0] = m10;
288  data[1][1] = m11;
289  data[1][2] = m12;
290  data[2][0] = m20;
291  data[2][1] = m21;
292  data[2][2] = m22;
293  }
294 
295  T data[3][3];
296 
302  static Mat3<T> identity()
303  {
304  return Mat3<T>(1, 0, 0, 0, 1, 0, 0, 0, 1);
305  }
306 
312  T determinant() const
313  {
314  return -data[0][2] * data[1][1] * data[2][0] + data[0][1] * data[1][2] * data[2][0] + data[0][2] * data[1][0] * data[2][1] - data[0][0] * data[1][2] * data[2][1] - data[0][1] * data[1][0] * data[2][2] + data[0][0] * data[1][1] * data[2][2];
315  }
316 
322  Mat3<T> inverse() const
323  {
324  return (1 / this->determinant()) * Mat3<T>(-data[1][2] * data[2][1] + data[1][1] * data[2][2], data[0][2] * data[2][1] - data[0][1] * data[2][2], -data[0][2] * data[1][1] + data[0][1] * data[1][2], data[1][2] * data[2][0] - data[1][0] * data[2][2], -data[0][2] * data[2][0] + data[0][0] * data[2][2], data[0][2] * data[1][0] - data[0][0] * data[1][2], -data[1][1] * data[2][0] + data[1][0] * data[2][1], data[0][1] * data[2][0] - data[0][0] * data[2][1], -data[0][1] * data[1][0] + data[0][0] * data[1][1]);
325  }
326  };
327 
336  template <typename T> Mat3<T> operator*(const T &lhs, const Mat3<T> &rhs)
337  {
338  Mat3<T> m(T(0));
339  for (int i = 0; i < 3; ++i)
340  {
341  for (int j = 0; j < 3; ++j)
342  {
343  m.data[i][j] = lhs * rhs.data[i][j];
344  }
345  }
346  return m;
347  }
348 
354  template <typename T> struct Mat4
355  {
359  Mat4() {};
360 
364  Mat4(const T &m)
365  {
366  data[0][0] = m;
367  data[0][1] = m;
368  data[0][2] = m;
369  data[0][3] = m;
370  data[1][0] = m;
371  data[1][1] = m;
372  data[1][2] = m;
373  data[1][3] = m;
374  data[2][0] = m;
375  data[2][1] = m;
376  data[2][2] = m;
377  data[2][3] = m;
378  data[3][0] = m;
379  data[3][1] = m;
380  data[3][2] = m;
381  data[3][3] = m;
382  };
383 
404  Mat4(const T &m00, const T &m01, const T &m02, const T &m03, const T &m10, const T &m11, const T &m12, const T &m13, const T &m20, const T &m21, const T &m22, const T &m23, const T &m30, const T &m31, const T &m32, const T &m33)
405  {
406  data[0][0] = m00;
407  data[0][1] = m01;
408  data[0][2] = m02;
409  data[0][3] = m03;
410  data[1][0] = m10;
411  data[1][1] = m11;
412  data[1][2] = m12;
413  data[1][3] = m13;
414  data[2][0] = m20;
415  data[2][1] = m21;
416  data[2][2] = m22;
417  data[2][3] = m23;
418  data[3][0] = m30;
419  data[3][1] = m31;
420  data[3][2] = m32;
421  data[3][3] = m33;
422  }
423 
424  T data[4][4];
425 
431  static Mat4<T> identity()
432  {
433  return Mat4<T>(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
434  }
435 
442  {
443  return Mat4<T>(-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1);
444  }
445 
452  static Mat4<T> translation(const Vec3<T> &v)
453  {
454  return Mat4<T>(1, 0, 0, v.x, 0, 1, 0, v.y, 0, 0, 1, v.z, 0, 0, 0, 1);
455  }
456 
464  static Mat4<T> rotation(const Vec3<T> &axis, T angle)
465  {
466  Vec3<T> a = axis;
467  a.normalize();
468  return Mat4<T>(
469  a.x * a.x * (1 - cos(angle)) + cos(angle),
470  a.x * a.y * (1 - cos(angle)) - a.z * sin(angle),
471  a.x * a.z * (1 - cos(angle)) + a.y * sin(angle),
472  0,
473 
474  a.y * a.x * (1 - cos(angle)) + a.z * sin(angle),
475  a.y * a.y * (1 - cos(angle)) + cos(angle),
476  a.y * a.z * (1 - cos(angle)) - a.x * sin(angle),
477  0,
478 
479  a.z * a.x * (1 - cos(angle)) - a.y * sin(angle),
480  a.z * a.y * (1 - cos(angle)) + a.x * sin(angle),
481  a.z * a.z * (1 - cos(angle)) + cos(angle),
482  0,
483 
484  0,
485  0,
486  0,
487  1);
488  }
489 
498  static Mat4<T> rotation(const Vec3<T> &axis, const Vec3<T> &point, const T &angle)
499  {
500  return translation(point) * rotation(axis, angle) * translation(-point);
501  }
502  };
503 
512  template <typename T> Mat4<T> operator*(const Mat4<T> &lhs, const Mat4<T> &rhs)
513  {
514  Mat4<T> m(T(0));
515  for (int i = 0; i < 4; ++i)
516  {
517  for (int j = 0; j < 4; ++j)
518  {
519  for (int k = 0; k < 4; ++k) m.data[i][j] += lhs.data[i][k] * rhs.data[k][j];
520  }
521  }
522  return m;
523  }
524  #pragma endregion
525 
526  #pragma region matrixvectoroperations
527 
535  template <typename T> Vec3<T> operator*(const Mat3<T> &lhs, const Vec3<T> &rhs)
536  {
537  return Vec3<T>(
538  lhs.data[0][0] * rhs.x + lhs.data[0][1] * rhs.y + lhs.data[0][2] * rhs.z,
539  lhs.data[1][0] * rhs.x + lhs.data[1][1] * rhs.y + lhs.data[1][2] * rhs.z,
540  lhs.data[2][0] * rhs.x + lhs.data[2][1] * rhs.y + lhs.data[2][2] * rhs.z
541  );
542  }
543 
552  template <typename T> Vec3<T> operator*(const Mat4<T> &lhs, const Vec3<T> &rhs)
553  {
554  return Vec3<T>(
555  lhs.data[0][0] * rhs.x + lhs.data[0][1] * rhs.y + lhs.data[0][2] * rhs.z + lhs.data[0][3],
556  lhs.data[1][0] * rhs.x + lhs.data[1][1] * rhs.y + lhs.data[1][2] * rhs.z + lhs.data[1][3],
557  lhs.data[2][0] * rhs.x + lhs.data[2][1] * rhs.y + lhs.data[2][2] * rhs.z + lhs.data[2][3]
558  );
559  }
560  #pragma endregion
561 }
geometry::Mat4::inversion
static Mat4< T > inversion()
Generate a spatial inversion operation, acting on a three-dimensional vector with fourth entry of uni...
Definition: Geometry.hpp:441
geometry::Vec4::Vec4
Vec4(const T &xyz)
Construct a new Vec4 object and initialize the x, y, and z components. The w component is set to unit...
Definition: Geometry.hpp:180
geometry::operator==
bool operator==(const Vec3< T > &lhs, const Vec3< T > &rhs)
Comparison of two Vec3 objects.
Definition: Geometry.hpp:158
geometry::Vec4::Vec4
Vec4()
Construct a new Vec4 object with uninitialized components.
Definition: Geometry.hpp:173
geometry::Vec4::y
T y
y Component.
Definition: Geometry.hpp:202
geometry::Mat3::Mat3
Mat3(const Vec3< T > &a0, const Vec3< T > &a1, const Vec3< T > &a2)
Construct a new Mat3 object and initialize the column vectors.
Definition: Geometry.hpp:256
geometry::Vec3::Vec3
Vec3()
Construct a new Vec3 object with uninitialized values.
Definition: Geometry.hpp:26
geometry::cross
Vec3< T > cross(const Vec3< T > &v, const Vec3< T > &w)
Compute the cross producdt of two Vec3 objects.
Definition: Geometry.hpp:94
geometry::dot
T dot(const Vec3< T > &v, const Vec3< T > &w)
Compute the dot product of two Vec3 objects.
Definition: Geometry.hpp:81
geometry::Mat3::Mat3
Mat3(const T &m)
Construct a new Mat3 and initialize all entries with the same value.
Definition: Geometry.hpp:236
geometry::Mat3::determinant
T determinant() const
Calculate the determinant of a Mat3 object.
Definition: Geometry.hpp:312
geometry::Vec3
Three-dimensional vector.
Definition: Geometry.hpp:21
geometry::Mat3::Mat3
Mat3()
Construct a new Mat3 with uninitialized values.
Definition: Geometry.hpp:229
geometry::Mat4::Mat4
Mat4(const T &m00, const T &m01, const T &m02, const T &m03, const T &m10, const T &m11, const T &m12, const T &m13, const T &m20, const T &m21, const T &m22, const T &m23, const T &m30, const T &m31, const T &m32, const T &m33)
Construct a new Mat 4 object and initialize entires.
Definition: Geometry.hpp:404
geometry::Mat4::Mat4
Mat4(const T &m)
Construct a new Mat4 object and initialize all entries with the same value.
Definition: Geometry.hpp:364
geometry::operator*
Vec3< T > operator*(const T lhs, const Vec3< T > &rhs)
Scalar left-multiplication of a Vec3 object.
Definition: Geometry.hpp:107
geometry::Vec3::z
T z
z Component.
Definition: Geometry.hpp:46
geometry::Vec4::Vec4
Vec4(const T &x, const T &y, const T &z)
Construct a new Vec4 object and initialize the x, y, and z components. The w component is set to unit...
Definition: Geometry.hpp:189
geometry::Mat3::Mat3
Mat3(const T &m00, const T &m01, const T &m02, const T &m10, const T &m11, const T &m12, const T &m20, const T &m21, const T &m22)
Construct a new Mat3 object and initialize entries.
Definition: Geometry.hpp:282
geometry::Mat4::Mat4
Mat4()
Construct a new Mat4 object with uninitialized entries.
Definition: Geometry.hpp:359
geometry::Mat3
3x3 dimensional matrix.
Definition: Geometry.hpp:224
geometry::Vec4
Four-dimensional vector.
Definition: Geometry.hpp:168
geometry::Vec4::w
T w
w Component.
Definition: Geometry.hpp:204
geometry::Mat4::identity
static Mat4< T > identity()
Generate a 4x4 identity matrix.
Definition: Geometry.hpp:431
geometry::Vec3::y
T y
y Component.
Definition: Geometry.hpp:45
geometry::Mat4::rotation
static Mat4< T > rotation(const Vec3< T > &axis, const Vec3< T > &point, const T &angle)
Generate a spatial rotation around a specific rotation center and axis by a specific angle,...
Definition: Geometry.hpp:498
geometry::Mat4::data
T data[4][4]
Matrix entries in row-major format.
Definition: Geometry.hpp:424
geometry::Vec3::Vec3
Vec3(const T &xyz)
Construct a new Vec3 object and initialize all numbers with the same value.
Definition: Geometry.hpp:33
geometry::Vec4::z
T z
z Component.
Definition: Geometry.hpp:203
geometry::Vec3::x
T x
x Component.
Definition: Geometry.hpp:42
geometry::Mat4::rotation
static Mat4< T > rotation(const Vec3< T > &axis, T angle)
Generate a spatial rotation around a specific axis by a specific angle, acting on a three-dimensional...
Definition: Geometry.hpp:464
geometry::operator+
Vec3< T > operator+(const Vec3< T > &lhs, const Vec3< T > &rhs)
Addition of two Vec3 objects.
Definition: Geometry.hpp:120
geometry::Mat4
4x4 dimensional matrix.
Definition: Geometry.hpp:354
geometry::Vec4::x
T x
x Component.
Definition: Geometry.hpp:199
geometry::Mat3::data
T data[3][3]
Matrix entries in row-major order.
Definition: Geometry.hpp:295
geometry::Vec3::norm
T norm() const
Compute the Euclidean norm of a Vec3 object.
Definition: Geometry.hpp:53
geometry::operator-
Vec3< T > operator-(const Vec3< T > &lhs, const Vec3< T > &rhs)
Subtraction of two Vec3 objects.
Definition: Geometry.hpp:133
geometry::Mat3::identity
static Mat3< T > identity()
Generate a 3x3 identity matrix.
Definition: Geometry.hpp:302
geometry::Mat3::inverse
Mat3< T > inverse() const
Calculate the inverse of a matrix.
Definition: Geometry.hpp:322
geometry::Vec3::normalize
Vec3 & normalize()
Normalize the Vec3 object with respect to its Euclidean norm.
Definition: Geometry.hpp:63
geometry::Mat4::translation
static Mat4< T > translation(const Vec3< T > &v)
Generate a spatial translation operation, acting on a three-dimensional vector with fourth entry of u...
Definition: Geometry.hpp:452