-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinAlgOperators.inl
218 lines (191 loc) · 8.72 KB
/
linAlgOperators.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// #####################
// ### vec op scalar ###
// #####################
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator+( const linAlg::vec_t< val_T, numCoords_T >& vecLhs, const val_T& scalRhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = vecLhs[i] + scalRhs;
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator-( const linAlg::vec_t< val_T, numCoords_T >& vecLhs, const val_T& scalRhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = vecLhs[i] - scalRhs;
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator*( const linAlg::vec_t< val_T, numCoords_T >& vecLhs, const val_T& scalRhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = vecLhs[i] * scalRhs;
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator/( const linAlg::vec_t< val_T, numCoords_T >& vecLhs, const val_T& scalRhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = vecLhs[i] / scalRhs;
}
return result;
}
// #####################
// ### scalar op vec ###
// #####################
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator+( const val_T& scalLhs, const linAlg::vec_t< val_T, numCoords_T >& vecRhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = scalLhs + vecRhs[i];
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator-( const val_T& scalLhs, const linAlg::vec_t< val_T, numCoords_T >& vecRhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = scalLhs - vecRhs[i];
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator*( const val_T& scalLhs, const linAlg::vec_t< val_T, numCoords_T >& vecRhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = scalLhs * vecRhs[i];
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator/( const val_T& scalLhs, const linAlg::vec_t< val_T, numCoords_T >& vecRhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = scalLhs / vecRhs[i];
}
return result;
}
// ##################
// ### vec op vec ###
// ##################
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator+( const linAlg::vec_t< val_T, numCoords_T >& lhs, const linAlg::vec_t< val_T, numCoords_T >& rhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = lhs[i] + rhs[i];
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator-( const linAlg::vec_t< val_T, numCoords_T >& lhs, const linAlg::vec_t< val_T, numCoords_T >& rhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = lhs[i] - rhs[i];
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator*( const linAlg::vec_t< val_T, numCoords_T >& lhs, const linAlg::vec_t< val_T, numCoords_T >& rhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = lhs[i] * rhs[i];
}
return result;
}
template<typename val_T, std::size_t numCoords_T>
static inline linAlg::vec_t< val_T, numCoords_T > operator/( const linAlg::vec_t< val_T, numCoords_T >& lhs, const linAlg::vec_t< val_T, numCoords_T >& rhs ) {
linAlg::vec_t< val_T, numCoords_T > result;
for (std::size_t i = 0; i < numCoords_T; i++) {
result[i] = lhs[i] / rhs[i];
}
return result;
}
// ##################
// ### mat op vec ###
// ##################
// for matrix: inner array stores the rows vectors, outer array says how many row vectors we have
// vector = matrix * vector
template<typename val_T, std::size_t numColumns_T, std::size_t numRows_T>
static linAlg::vec_t<val_T, numRows_T> operator*( const linAlg::mat_t<val_T, numRows_T, numColumns_T >& matrix,
const linAlg::vec_t<val_T, numColumns_T>& vector ) {
linAlg::vec_t<val_T, numRows_T> result;
// result[0] = linAlg::dot( matrix[0], vector );
// result[1] = linAlg::dot( matrix[1], vector );
// result[2] = linAlg::dot( matrix[2], vector );
for (std::size_t row = 0; row < numRows_T; row++) {
result[row] = linAlg::dot( matrix[row], vector );
}
return result;
}
// ##################
// ### mat op mat ###
// ##################
// matrix = matrix * matrix
// inner array stores the rows vectors, outer array says how many row vectors we have
// "inner" index - the one closer to the var name - accesses the row, the outer "last" indexing parentheses index the column within that row
template<typename val_T, std::size_t rowsL_T, std::size_t colsL_rowsR_T, std::size_t colsR_T>
static linAlg::mat_t<val_T, rowsL_T, colsR_T > operator* ( const linAlg::mat_t< val_T, rowsL_T, colsL_rowsR_T >& matrixLhs,
const linAlg::mat_t< val_T, colsL_rowsR_T, colsR_T >& matrixRhs ) {
linAlg::mat_t< val_T, rowsL_T, colsR_T > result;
for (std::size_t col = 0; col < colsR_T; col++) { // left columns
for (std::size_t row = 0; row < rowsL_T; row++) { // right rows
val_T accum{ 0 };
for (std::size_t k = 0; k < colsL_rowsR_T; k++) { // dot of left row (over columns) and right columns ( over rows )
accum += matrixLhs[row][k] * matrixRhs[k][col];
}
result[row][col] = accum;
}
}
return result;
}
// e.g., in 3D, non-perspective transformations can be represented by 3x4 matrices => treat them as 3x3 rotations and fix up translational part
template<typename val_T, std::size_t rowsL_T>
static linAlg::mat_t<val_T, rowsL_T, rowsL_T + 1 > operator* ( const linAlg::mat_t< val_T, rowsL_T, rowsL_T + 1 >& matrixLhs,
const linAlg::mat_t< val_T, rowsL_T, rowsL_T + 1 >& matrixRhs ) {
constexpr std::size_t colsR_T = rowsL_T + 1;
constexpr std::size_t squareDim = rowsL_T;
linAlg::mat_t< val_T, rowsL_T, colsR_T > result;
for (std::size_t col = 0; col < squareDim; col++) { // left columns => only rowsL_T number of columns to iterate over
for (std::size_t row = 0; row < squareDim; row++) { // right rows
val_T accum{ 0 };
for (std::size_t k = 0; k < squareDim; k++) { // dot of left row (over columns) and right columns ( over rows )
accum += matrixLhs[row][k] * matrixRhs[k][col];
}
result[row][col] = accum;
}
}
// fix-up for last column (translations)
constexpr std::size_t lastColumnIdx = colsR_T - 1;
for (std::size_t row = 0; row < rowsL_T; row++) {
val_T accum{ 0 };
for (std::size_t k = 0; k < squareDim; k++) {
accum += matrixLhs[row][k] * matrixRhs[k][lastColumnIdx];
}
result[row][lastColumnIdx] = accum + matrixLhs[row][lastColumnIdx];
}
return result;
}
// #######################
// ### equality checks ###
// #######################
template<typename val_T, std::size_t numCoords_T>
static bool operator==( const linAlg::vec_t<val_T, numCoords_T>& vectorLhs,
const linAlg::vec_t<val_T, numCoords_T>& vectorRhs ) {
for (std::size_t coord = 0; coord < numCoords_T; coord++) {
if (vectorLhs[coord] != vectorRhs[coord]) { return false; }
}
return true;
}
template<typename val_T, std::size_t rows_T, std::size_t cols_T>
static bool operator== ( const linAlg::mat_t< val_T, rows_T, cols_T >& matrixLhs,
const linAlg::mat_t< val_T, rows_T, cols_T >& matrixRhs ) {
for (std::size_t col = 0; col < cols_T; col++) {
for (std::size_t row = 0; row < rows_T; row++) {
if (matrixLhs[row][col] != matrixRhs[row][col]) { return false; }
}
}
return true;
}