Add support for Slice to Matrix, SquareMatrix, Vector

This commit is contained in:
Julian Kent
2019-09-16 16:11:08 +02:00
parent af1378cc55
commit 82d565f4d9
8 changed files with 77 additions and 20 deletions
+21 -17
View File
@@ -24,6 +24,12 @@ namespace matrix
template <typename Type, size_t M>
class Vector;
template<typename Type, size_t M, size_t N>
class Matrix;
template <typename Type, size_t P, size_t Q, size_t M, size_t N>
class Slice;
template<typename Type, size_t M, size_t N>
class Matrix
{
@@ -50,6 +56,17 @@ public:
memcpy(_data, other._data, sizeof(_data));
}
template<size_t P, size_t Q>
Matrix(const Slice<Type, M, N, P, Q>& in_slice)
{
Matrix<Type, M, N>& self = *this;
for (size_t i = 0; i < M; i++) {
for (size_t j = 0; j < N; j++) {
self(i, j) = in_slice(i, j);
}
}
}
/**
* Accessors/ Assignment etc.
*/
@@ -353,27 +370,15 @@ public:
}
template<size_t P, size_t Q>
Matrix<Type, P, Q> slice(size_t x0, size_t y0) const
const Slice<Type, P, Q, M, N> slice(size_t x0, size_t y0) const
{
const Matrix<Type, M, N> &self = *this;
Matrix<Type, P, Q> res; //default constructed
for (size_t i = 0; i < P; i++) {
for (size_t j = 0; j < Q; j++) {
res(i, j) = self(i + x0, j + y0);
}
}
return res;
return Slice<Type, P, Q, M, N>(x0, y0, this);
}
template<size_t P, size_t Q>
void set(const Matrix<Type, P, Q> &m, size_t x0, size_t y0)
Slice<Type, P, Q, M, N> slice(size_t x0, size_t y0)
{
Matrix<Type, M, N> &self = *this;
for (size_t i = 0; i < P; i++) {
for (size_t j = 0; j < Q; j++) {
self(i + x0, j + y0) = m(i, j);
}
}
return Slice<Type, P, Q, M, N>(x0, y0, this);
}
void setRow(size_t i, const Matrix<Type, N, 1> &row)
@@ -517,7 +522,6 @@ public:
}
return result;
}
};
template<typename Type, size_t M, size_t N>