Mathter
A configurable 3D math library for game developers.
Rotation2DBuilder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include "../Matrix/MatrixImpl.hpp"
5 #include "../Vector.hpp"
6 
7 
8 namespace mathter {
9 
10 
11 template <class T>
13 public:
14  Rotation2DBuilder(const T& angle) : angle(angle) {}
16 
17  template <class U, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
20  Set(m);
21  return m;
22  }
23 
24  template <class U, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
27  Set(m);
28  return m;
29  }
30 
31  template <class U, eMatrixLayout Layout, bool MPacked>
34  Set(m);
35  return m;
36  }
37 
38  template <class U, eMatrixLayout Layout, bool MPacked>
41  Set(m);
42  return m;
43  }
44 
45 private:
46  template <class U, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
48  T C = cos(angle);
49  T S = sin(angle);
50 
51  auto elem = [&m](int i, int j) -> T& {
52  return Order == eMatrixOrder::FOLLOW_VECTOR ? m(i, j) : m(j, i);
53  };
54 
55  // Indices according to follow vector order
56  elem(0, 0) = C;
57  elem(0, 1) = S;
58  elem(1, 0) = -S;
59  elem(1, 1) = C;
60 
61  // Rest
62  for (int j = 0; j < m.ColumnCount(); ++j) {
63  for (int i = (j < 2 ? 2 : 0); i < m.RowCount(); ++i) {
64  m(i, j) = T(j == i);
65  }
66  }
67  }
68 
69  const T angle;
70 };
71 
72 
75 template <class T>
76 auto Rotation(const T& angle) {
77  return Rotation2DBuilder{ angle };
78 }
79 
80 
81 } // namespace mathter
auto Rotation(const T &angle)
Creates a 2D rotation matrix.
Definition: Rotation2DBuilder.hpp:76
Definition: Approx.hpp:11
Rotation2DBuilder & operator=(const Rotation2DBuilder &)=delete
Definition: Rotation2DBuilder.hpp:12
constexpr int ColumnCount() const
Returns the number of columns of the matrix.
Definition: MatrixImpl.hpp:27
Definition: Definitions.hpp:63
Rotation2DBuilder(const T &angle)
Definition: Rotation2DBuilder.hpp:14
constexpr int RowCount() const
Returns the number of rows of the matrix.
Definition: MatrixImpl.hpp:31