Lugdunum  0.1.0
Vector.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <lug/Math/Matrix.hpp>
5 
6 namespace lug {
7 namespace Math {
8 
9 // TODO: Redefine all operations on Matrix for Vector
10 
11 template <uint8_t Rows, typename T = float>
12 class Vector : public Matrix<Rows, 1, T> {
13 public:
15 
16  constexpr Vector() = default;
17 
18  explicit constexpr Vector(T value);
19  Vector(std::initializer_list<T> list);
20 
21  // Convert from matrix (we want non explicit conversion)
22  Vector(const BaseMatrix& matrix);
23  Vector(BaseMatrix&& matrix);
24 
25  Vector(const Vector<Rows - 1, T>& vector, T value = 0);
26  Vector(const Vector<Rows + 1, T>& vector);
27 
28  Vector(const Vector<Rows, T>& vector) = default;
29  Vector(Vector<Rows, T>&& vector) = default;
30 
31  Vector<Rows, T>& operator=(const Vector<Rows, T>& vector) = default;
32  Vector<Rows, T>& operator=(Vector<Rows, T>&& vector) = default;
33 
34  ~Vector() = default;
35 
37 
40 
41 #define DEFINE_ACCESS(name, minimum_rows) \
42  template <bool EnableBool = true, typename = typename std::enable_if<(Rows >= minimum_rows) && EnableBool>::type> \
43  const T& name() const { \
44  return (*this)(minimum_rows - 1); \
45  } \
46  \
47  template <bool EnableBool = true, typename = typename std::enable_if<(Rows >= minimum_rows) && EnableBool>::type> \
48  T& name() { \
49  return (*this)(minimum_rows - 1); \
50  }
51 
52  DEFINE_ACCESS(x, 1)
53  DEFINE_ACCESS(r, 1)
54  DEFINE_ACCESS(width, 1)
55 
56  DEFINE_ACCESS(y, 2)
57  DEFINE_ACCESS(g, 2)
58  DEFINE_ACCESS(height, 2)
59 
60  DEFINE_ACCESS(z, 3)
61  DEFINE_ACCESS(b, 3)
62 
63  DEFINE_ACCESS(w, 4)
64  DEFINE_ACCESS(a, 4)
65 
66 #undef DEFINE_ACCESS
67 
68  constexpr T length() const;
69  constexpr T squaredLength() const;
70  void normalize();
71 };
72 
73 template <typename T>
74 constexpr Vector<3, T> cross(const Vector<3, T>& lhs, const Vector<3, T>& rhs);
75 
76 template <uint8_t Rows, typename T>
77 constexpr T dot(const Vector<Rows, T>& lhs, const Vector<Rows, T>& rhs);
78 
79 template <uint8_t Rows, uint8_t Columns, typename T>
80 constexpr Matrix<Rows, Columns, T> outer(const Vector<Rows, T>& lhs, const Vector<Columns, T>& rhs);
81 
82 template <uint8_t Rows, typename T>
83 constexpr Vector<Rows, T> normalize(const Vector<Rows, T>& lhs);
84 
85 #define DEFINE_LENGTH_VECTOR(length) \
86  template <typename T = float> \
87  using Vec##length = Vector<length, T>; \
88  \
89  template class LUG_MATH_API Vector<length, float>; \
90  using Vec##length##f = Vec##length<float>; \
91  \
92  template class LUG_MATH_API Vector<length, double>; \
93  using Vec##length##d = Vec##length<double>; \
94  \
95  template class LUG_MATH_API Vector<length, int32_t>; \
96  using Vec##length##i = Vec##length<int32_t>; \
97  \
98  template class LUG_MATH_API Vector<length, uint32_t>; \
99  using Vec##length##u = Vec##length<uint32_t>;
100 
104 
105 #undef DEFINE_LENGTH_VECTOR
106 
107 template <uint8_t Rows, typename T>
108 Vector<Rows, T> operator*(const Vector<Rows, T>& lhs, const Vector<Rows, T>& rhs);
109 
110 template <uint8_t Rows, typename T>
111 Vector<Rows, T> operator/(const Vector<Rows, T>& lhs, const Vector<Rows, T>& rhs);
112 
113 template <uint8_t Rows, typename T>
114 Vector<Rows, T> operator*(const Vector<Rows, T>& lhs, const Matrix<Rows, Rows, T>& rhs);
115 
116 template <uint8_t Rows, typename T>
117 Vector<Rows, T> operator*(const Matrix<Rows, Rows, T>& lhs, const Vector<Rows, T>& rhs);
118 
119 // Special case Mat4x4 * Vec3
120 template <typename T>
121 Vector<3, T> operator*(const Vector<3, T>& lhs, const Matrix<4, 4, T>& rhs);
122 
123 template <typename T>
124 Vector<3, T> operator*(const Matrix<4, 4, T>& lhs, const Vector<3, T>& rhs);
125 
126 #include <lug/Math/Vector.inl>
127 
128 } // Math
129 } // lug
Vector< Rows, T > operator*=(const Matrix< Rows, Rows, T > &rhs)
Definition: Vector.inl:30
#define DEFINE_ACCESS(name, minimum_rows)
Definition: Vector.hpp:41
constexpr Vector< 3, T > cross(const Vector< 3, T > &lhs, const Vector< 3, T > &rhs)
Vector< Rows, T > & operator=(const Vector< Rows, T > &vector)=default
constexpr T squaredLength() const
Definition: Vector.inl:62
void normalize()
Definition: Vector.inl:67
constexpr Matrix< Rows, Columns, T > outer(const Vector< Rows, T > &lhs, const Vector< Columns, T > &rhs)
constexpr T length() const
Definition: Vector.inl:57
Vector< Rows, T > operator/=(const Vector< Rows, T > &rhs)
Definition: Vector.inl:51
#define DEFINE_LENGTH_VECTOR(length)
Definition: Vector.hpp:85
Matrix< Rows, Columns, T > operator/(const Matrix< Rows, Columns, T > &lhs, T rhs)
constexpr Vector()=default
Matrix< Rows, Columns, T > operator*(const Matrix< Rows, Columns, T > &lhs, T rhs)
T dot(const Quaternion< T > &lhs, const Quaternion< T > &rhs)