Mathter
A configurable 3D math library for game developers.
MatrixVectorArithmetic.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "MatrixImpl.hpp"
4 
5 namespace mathter {
6 
7 
8 //------------------------------------------------------------------------------
9 // Matrix-vector arithmetic
10 //------------------------------------------------------------------------------
11 
12 #define MATHTER_VECMAT_ARRAY_1 result = vec(0) * mat.stripes[0];
13 #define MATHTER_VECMAT_ARRAY_2 MATHTER_VECMAT_ARRAY_1 result += vec(1) * mat.stripes[1];
14 #define MATHTER_VECMAT_ARRAY_3 MATHTER_VECMAT_ARRAY_2 result += vec(2) * mat.stripes[2];
15 #define MATHTER_VECMAT_ARRAY_4 MATHTER_VECMAT_ARRAY_3 result += vec(3) * mat.stripes[3];
16 #define MATHTER_VECMAT_UNROLL(S) \
17  if (result.Dimension() == S) { \
18  MATHTER_MATMUL_EXPAND(MATHTER_VECMAT_ARRAY_##S) \
19  return result; \
20  }
21 
22 #define MATHTER_VECMAT_DOT_ARRAY_1 result(0) = Dot(vec, mat.stripes[0]);
23 #define MATHTER_VECMAT_DOT_ARRAY_2 MATHTER_VECMAT_DOT_ARRAY_1 result(1) = Dot(vec, mat.stripes[1]);
24 #define MATHTER_VECMAT_DOT_ARRAY_3 MATHTER_VECMAT_DOT_ARRAY_2 result(2) = Dot(vec, mat.stripes[2]);
25 #define MATHTER_VECMAT_DOT_ARRAY_4 MATHTER_VECMAT_DOT_ARRAY_3 result(3) = Dot(vec, mat.stripes[3]);
26 #define MATHTER_VECMAT_DOT_UNROLL(S) \
27  if (result.Dimension() == S) { \
28  MATHTER_MATMUL_EXPAND(MATHTER_VECMAT_DOT_ARRAY_##S) \
29  return result; \
30  }
31 
32 
33 // v*M
34 template <class Vt, class Mt, int Vd, int Mcol, eMatrixOrder Morder, bool Packed, class Rt>
37 
42 
43  result = vec(0) * mat.stripes[0];
44  for (int i = 1; i < Vd; ++i) {
45  result += vec(i) * mat.stripes[i];
46  }
47  return result;
48 }
49 
50 template <class Vt, class Mt, int Vd, int Mcol, eMatrixOrder Morder, bool Packed, class Rt>
53 
58 
59  for (int i = 0; i < Vd; ++i) {
60  result(i) = Dot(vec, mat.stripes[i]);
61  }
62  return result;
63 }
64 
65 template <class Vt, class Mt, int Vd, int Mcol, eMatrixOrder Morder, eMatrixLayout Mlayout, bool Packed>
67  using Rt = traits::MatMulElemT<Vt, Mt>;
68  return operator*<Vt, Mt, Vd, Mcol, Morder, Packed, Rt>(vec, mat);
69 }
70 
71 
72 // (v|1)*M
73 template <class Vt, class Mt, int Vd, eMatrixLayout Mlayout, eMatrixOrder Morder, bool Packed, class Rt = traits::MatMulElemT<Vt, Mt>>
75  return (vec | Vt(1)) * mat;
76 }
77 
78 template <class Vt, class Mt, int Vd, eMatrixLayout Mlayout, eMatrixOrder Morder, bool Packed, class Rt = traits::MatMulElemT<Vt, Mt>>
80  auto res = (vec | Vt(1)) * mat;
81  res /= res(res.Dimension() - 1);
82  return (Vector<Rt, Vd, Packed>)res;
83 }
84 
85 // M*v
86 template <class Vt, class Mt, int Vd, int Mrow, eMatrixOrder Morder, bool Packed, class Rt>
89 
94 
95  for (int i = 0; i < Mrow; ++i) {
96  result(i) = Dot(vec, mat.stripes[i]);
97  }
98  return result;
99 }
100 
101 template <class Vt, class Mt, int Vd, int Mrow, eMatrixOrder Morder, bool Packed, class Rt>
104 
109 
110  result = vec(0) * mat.stripes[0];
111  for (int i = 1; i < Vd; ++i) {
112  result += vec(i) * mat.stripes[i];
113  }
114  return result;
115 }
116 
117 template <class Vt, class Mt, int Vd, int Mrow, eMatrixOrder Morder, eMatrixLayout Mlayout, bool Packed>
119  using Rt = traits::MatMulElemT<Vt, Mt>;
120  return operator*<Vt, Mt, Vd, Mrow, Morder, Packed, Rt>(mat, vec);
121 }
122 
123 
124 
125 // M*(v|1)
126 template <class Vt, class Mt, int Vd, eMatrixLayout Mlayout, eMatrixOrder Morder, bool Packed, class Rt = traits::MatMulElemT<Vt, Mt>>
128  return mat * (vec | Vt(1));
129 }
130 
131 template <class Vt, class Mt, int Vd, eMatrixLayout Mlayout, eMatrixOrder Morder, bool Packed, class Rt = traits::MatMulElemT<Vt, Mt>>
133  auto res = (vec | Vt(1)) * mat;
134  res /= res(res.Dimension() - 1);
135  return (Vector<Rt, Vd, Packed>)res;
136 }
137 
138 // v*=M
139 template <class Vt, class Mt, int Vd, eMatrixOrder Morder, eMatrixLayout Layout, bool Packed>
141  vec = operator*<Vt, Mt, Vd, Vd, Morder, Packed, Vt>(vec, mat);
142  return vec;
143 }
144 
145 
146 } // namespace mathter
auto operator*(const Matrix< T, Rows1, Match, Order1, eMatrixLayout::ROW_MAJOR, Packed > &lhs, const Matrix< U, Match, Columns2, Order2, eMatrixLayout::ROW_MAJOR, Packed > &rhs) -> Matrix< V, Rows1, Columns2, Order1, eMatrixLayout::ROW_MAJOR, Packed >
Definition: MatrixArithmetic.hpp:78
#define MATHTER_VECMAT_DOT_UNROLL(S)
Definition: MatrixVectorArithmetic.hpp:26
Represents a vector in N-dimensional space.
Definition: Definitions.hpp:57
T Dot(const Vector< T, Dim, Packed > &lhs, const Vector< T, Dim, Packed > &rhs)
Calculates the scalar product (dot product) of the two arguments.
Definition: VectorFunction.hpp:111
std::array< Vector< T, StripeDim, Packed >, StripeCount > stripes
Definition: MatrixImpl.hpp:46
Definition: Approx.hpp:11
#define MATHTER_VECMAT_UNROLL(S)
Definition: MatrixVectorArithmetic.hpp:16
Definition: Definitions.hpp:63
Matrix< T1, Dim, Dim, Order1, Layout1, Packed > & operator*=(Matrix< T1, Dim, Dim, Order1, Layout1, Packed > &lhs, const Matrix< T2, Dim, Dim, Order2, Layout2, Packed > &rhs)
Definition: MatrixArithmetic.hpp:198
decltype(T() *U()+T()+U()) MatMulElemT
Definition: Traits.hpp:60