NRE_Matrix4x4.hpp
1 
10  #pragma once
11 
12  #include <Utility/String/NRE_String.hpp>
13  #include <Utility/Interfaces/Stringable/NRE_Stringable.hpp>
14  #include <type_traits>
15 
16  #include "../../NRE_Math.hpp"
17  #include "../../NRE_Unit.hpp"
18 
19  #include "../../Curve/Bezier/4D/NRE_BezierCurve4D.hpp"
20 
25  namespace NRE {
30  namespace Math {
31 
32  template<class> class Vector2D;
33  template<class> class Vector3D;
34  template<class> class Vector4D;
35  template<class> class Segment4D;
36  template<class> class Plane;
37  template<class> class Matrix3x3;
38  template<class> class Quaternion;
39 
44  template <class T>
45  class Matrix4x4 : public Utility::Stringable<Matrix4x4<T>> {
46  private: //Fields
47  Vector4D<T> data[4];
49  public: // Methods
50  //## Constructor ##//
54  constexpr Matrix4x4() = default;
74  template <class A, class B, class C, class D,
75  class E, class F, class G, class H,
76  class I, class J, class K, class L,
77  class M, class N, class O, class P>
78  constexpr Matrix4x4(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p);
83  template <class K>
84  constexpr Matrix4x4(K value);
92  template <class K, class L, class N, class M>
93  constexpr Matrix4x4(Vector4D<K> const& l1, Vector4D<L> const& l2, Vector4D<N> const& l3, Vector4D<M> const& l4);
94 
95  //## Copy-Constructor ##//
100  constexpr Matrix4x4(Matrix4x4 const& m) = default;
101 
102  //## Move-Constructor ##//
107  constexpr Matrix4x4(Matrix4x4 && m) = default;
108 
109  //## Convertor ##//
114  template <class K>
115  constexpr Matrix4x4(Matrix3x3<K> const& m);
120  template <class K>
121  constexpr Matrix4x4(Matrix4x4<K> const& m);
126  template <class K>
127  constexpr Matrix4x4(Quaternion<K> const& q);
128 
129  //## Deconstructor ##//
133  ~Matrix4x4() = default;
134 
135  //## Getter ##//
139  constexpr Vector4D<T> const& getL1() const;
143  constexpr Vector4D<T> const& getL2() const;
147  constexpr Vector4D<T> const& getL3() const;
151  constexpr Vector4D<T> const& getL4() const;
155  constexpr Vector4D<T> getC1() const;
159  constexpr Vector4D<T> getC2() const;
163  constexpr Vector4D<T> getC3() const;
167  constexpr Vector4D<T> getC4() const;
171  constexpr long double getDeterminant() const;
172 
173  //## Setter ##//
178  template <class K>
179  constexpr void setL1(Vector4D<K> const& l1);
184  template <class K>
185  constexpr void setL2(Vector4D<K> const& l2);
190  template <class K>
191  constexpr void setL3(Vector4D<K> const& l3);
196  template <class K>
197  constexpr void setL4(Vector4D<K> const& l4);
202  template <class K>
203  constexpr void setC1(Vector4D<K> const& c1);
208  template <class K>
209  constexpr void setC2(Vector4D<K> const& c2);
214  template <class K>
215  constexpr void setC3(Vector4D<K> const& c3);
220  template <class K>
221  constexpr void setC4(Vector4D<K> const& c4);
226  constexpr Matrix4x4& setIdentity();
227 
228  //## Methods ##//
233  constexpr Matrix4x4& transpose();
238  constexpr Matrix4x4& inverse();
244  template <class K>
245  constexpr Matrix4x4& translate(Vector3D<K> const& u);
251  template <class K>
252  constexpr Matrix4x4& scale(Vector3D<K> const& u);
258  template <class K, typename = UseIfArithmetic<K>>
259  constexpr Matrix4x4& stretchX(K u) {
260  Matrix4x4<T> tmp = IDENTITY;
261  tmp[0][0] = static_cast <T> (u);
262  return *this *= tmp;
263  }
269  template <class K, typename = UseIfArithmetic<K>>
270  constexpr Matrix4x4& stretchY(K u) {
271  Matrix4x4<T> tmp = IDENTITY;
272  tmp[1][1] = static_cast <T> (u);
273  return *this *= tmp;
274  }
280  template <class K, typename = UseIfArithmetic<K>>
281  constexpr Matrix4x4& stretchZ(K u) {
282  Matrix4x4<T> tmp = IDENTITY;
283  tmp[2][2] = static_cast <T> (u);
284  return *this *= tmp;
285  }
292  template <class K>
293  constexpr Matrix4x4& rotate(Angle angle, Vector3D<K> const& axis);
301  template <class K, class L, typename = UseIfArithmetic<K>>
302  constexpr Matrix4x4& perspective(Angle fov, K ratio, Vector2D<L> const& z) {
303  setIdentity();
304 
305  T f = static_cast <T> (tan(fov / 2.0));
306  data[0][0] = static_cast <T> (1.0) / (static_cast <T> (ratio) * f);
307  data[1][1] = static_cast <T> (1.0) / f;
308  data[2][2] = static_cast <T> (-(z.getY() + z.getX()) / (z.getY() - z.getX()));
309  data[2][3] = static_cast <T> (-(static_cast <L> (2.0) * z.getY() * z.getX()) / (z.getY() - z.getX()));
310  data[3][2] = -1;
311  data[3][3] = 0;
312 
313  return *this;
314  }
322  template <class K, class L, class N>
323  constexpr Matrix4x4& ortho(Vector2D<K> const& h, Vector2D<L> const& v, Vector2D<N> const& z);
330  template <class K, class L>
331  constexpr Matrix4x4& ortho2D(Vector2D<K> const& h, Vector2D<L> const& v);
339  template <class K, class L, class N>
340  constexpr Matrix4x4& lookAt(Vector3D<K> const& eye, Vector3D<L> const& center, Vector3D<N> const& up);
344  constexpr const T* value() const;
345 
346  //## Access Operator ##//
353  constexpr Vector4D<T>& operator [](std::size_t index);
360  constexpr Vector4D<T> const& operator [](std::size_t index) const;
361 
362  //## Assignment Operator ##//
368  constexpr Matrix4x4& operator =(Matrix4x4 const& m) = default;
374  constexpr Matrix4x4& operator =(Matrix4x4 && m) = default;
380  template <class K>
381  constexpr Matrix4x4& operator =(Matrix4x4<K> const& m);
387  template <class K>
388  constexpr Matrix4x4& operator =(Matrix4x4<K> && m);
389 
390  //## Shortcut Operator ##//
396  template <class K>
397  constexpr Matrix4x4& operator +=(Matrix4x4<K> const& m);
403  template <class K>
404  constexpr Matrix4x4& operator -=(Matrix4x4<K> const& m);
410  template <class K>
411  constexpr Matrix4x4& operator *=(K k);
417  template <class K>
418  constexpr Matrix4x4& operator *=(Matrix4x4<K> const& m);
424  template <class K>
425  constexpr Matrix4x4& operator /=(K k);
431  template <class K>
432  constexpr Matrix4x4& operator /=(Matrix4x4<K> const& m);
433 
434  //## Arithmetic Operator ##//
440  template <class K>
447  template <class K>
454  template <class K>
455  constexpr Matrix4x4<std::common_type_t<T, K>> operator *(K k) const;
461  template <class K>
468  template <class K>
469  constexpr Vector4D<K> operator *(Vector4D<K> const& u) const;
475  template <class K>
476  constexpr Segment4D<K> operator *(Segment4D<K> const& s) const;
482  constexpr BezierCurve4D operator *(BezierCurve4D const& c) const;
488  template <class K>
489  constexpr Matrix4x4<std::common_type_t<T, K>> operator /(K k) const;
495  template <class K>
497 
498  //## Comparison Operator ##//
504  template <class K>
505  constexpr bool operator ==(Matrix4x4<K> const& m) const;
511  template <class K>
512  constexpr bool operator !=(Matrix4x4<K> const& m) const;
513 
514  //## Stream Operator ##//
519  Utility::String toString() const;
520 
521  public :
522  static constexpr Matrix4x4 IDENTITY = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
523  };
524  }
525  }
526 
527  #include "NRE_Matrix4x4.tpp"
A cartesian 2D vector.
Definition: NRE_Matrix3x3.hpp:32
constexpr Vector4D< T > getC1() const
constexpr Vector4D< T > const & getL3() const
constexpr Matrix4x4 & scale(Vector3D< K > const &u)
constexpr Matrix4x4 & lookAt(Vector3D< K > const &eye, Vector3D< L > const &center, Vector3D< N > const &up)
A 4D homogeneous segment.
Definition: NRE_Matrix4x4.hpp:35
static constexpr Matrix4x4 IDENTITY
Definition: NRE_Matrix4x4.hpp:522
constexpr void setL1(Vector4D< K > const &l1)
constexpr Vector4D< T > & operator[](std::size_t index)
constexpr Vector4D< T > const & getL4() const
constexpr Vector4D< T > getC3() const
constexpr Vector4D< T > getC4() const
4x4 generic matrix
Definition: NRE_Matrix3x3.hpp:35
3x3 generic matrix
Definition: NRE_Matrix3x3.hpp:42
constexpr Matrix4x4< std::common_type_t< T, K > > operator-(Matrix4x4< K > const &m) const
constexpr const T * value() const
constexpr void setL2(Vector4D< K > const &l2)
constexpr Matrix4x4()=default
constexpr long double getDeterminant() const
Utility::String toString() const
constexpr Matrix4x4 & ortho2D(Vector2D< K > const &h, Vector2D< L > const &v)
constexpr Matrix4x4 & stretchX(K u)
Definition: NRE_Matrix4x4.hpp:259
Math&#39;s API.
constexpr UAcceleration G
Definition: NRE_Unit.hpp:321
constexpr Matrix4x4 & ortho(Vector2D< K > const &h, Vector2D< L > const &v, Vector2D< N > const &z)
constexpr Matrix4x4< std::common_type_t< T, K > > operator*(K k) const
constexpr Matrix4x4 & operator+=(Matrix4x4< K > const &m)
constexpr void setC3(Vector4D< K > const &c3)
A cartesian 3D vector, 2D homogeneous.
Definition: NRE_Matrix3x3.hpp:33
constexpr void setC1(Vector4D< K > const &c1)
constexpr Vector4D< T > getC2() const
constexpr Matrix4x4 & operator/=(K k)
A cartesian plane with a point and a vector.
Definition: NRE_Matrix4x4.hpp:36
constexpr void setC4(Vector4D< K > const &c4)
constexpr Matrix4x4< std::common_type_t< T, K > > operator/(K k) const
constexpr Matrix4x4< std::common_type_t< T, K > > operator+(Matrix4x4< K > const &m) const
constexpr Matrix4x4 & translate(Vector3D< K > const &u)
constexpr Matrix4x4 & operator-=(Matrix4x4< K > const &m)
constexpr Matrix4x4 & transpose()
constexpr Matrix4x4 & operator*=(K k)
constexpr void setL4(Vector4D< K > const &l4)
The NearlyRealEngine&#39;s global namespace.
constexpr Vector4D< T > const & getL2() const
A cartesian 4D vector, 3D homogeneous.
Definition: NRE_Matrix4x4.hpp:34
constexpr bool operator==(Matrix4x4< K > const &m) const
constexpr T getX() const
double tan(Angle const &u)
Definition: NRE_Unit.hpp:591
constexpr Matrix4x4 & stretchZ(K u)
Definition: NRE_Matrix4x4.hpp:281
constexpr Vector4D< T > const & getL1() const
constexpr Matrix4x4 & rotate(Angle angle, Vector3D< K > const &axis)
constexpr Matrix4x4 & perspective(Angle fov, K ratio, Vector2D< L > const &z)
Definition: NRE_Matrix4x4.hpp:302
constexpr void setL3(Vector4D< K > const &l3)
constexpr Matrix4x4 & setIdentity()
constexpr Matrix4x4 & operator=(Matrix4x4 const &m)=default
constexpr T getY() const
A quaternion represented by a 3D rotation axis and an angle.
Definition: NRE_Matrix4x4.hpp:38
Represent a 3D homogeneous Bezier curves with a set of control points.
Definition: NRE_BezierCurve4D.hpp:32
constexpr void setC2(Vector4D< K > const &c2)
constexpr Matrix4x4 & stretchY(K u)
Definition: NRE_Matrix4x4.hpp:270
constexpr bool operator!=(Matrix4x4< K > const &m) const
constexpr Matrix4x4 & inverse()