12 #ifndef _GENERICMATRIX_H_
13 #define _GENERICMATRIX_H_
21 namespace artec {
namespace sdk {
namespace base
26 #pragma warning(disable: 4201)
28 template<
int rows,
int cols,
typename Type =
double >
37 template<
typename Type2, std::
size_t sizeOfArray >
40 int c_assert[(rows * cols == sizeOfArray) ? 1 : -1];
42 for(
int i = 0; i < size_; i++)
46 template<
typename Type2 >
49 for (
int matrixRow = 0; matrixRow < rows; ++matrixRow) {
50 for (
int matrixCol = 0; matrixCol < cols; ++matrixCol) {
51 if (matrixCol < cols2 && matrixRow < rows2)
52 m[matrixRow][matrixCol] =
static_cast<Type
>(values[matrixRow * cols2 + matrixCol]);
53 else if (matrixCol == matrixRow)
54 m[matrixRow][matrixCol] = Type(1);
56 m[matrixRow][matrixCol] = Type(0);
65 template<
typename Type2 >
68 for(
int i = 0; i < size_; i++)
data[i] = (Type)second.
data[i];
72 template<
int rows2,
int cols2,
typename Type2 >
77 template<
int rows2,
int cols2 >
80 for (
int matrixRow = 0; matrixRow < rows; ++matrixRow) {
81 for (
int matrixCol = 0; matrixCol < cols; ++matrixCol) {
82 if (matrixCol < cols2 && matrixRow < rows2)
83 m[matrixRow][matrixCol] = second.
m[matrixRow][matrixCol];
84 else if (matrixCol == matrixRow)
85 m[matrixRow][matrixCol] = Type(1);
87 m[matrixRow][matrixCol] = Type(0);
95 memcpy(
data, second.
data, size_*
sizeof(Type));
103 for(
int i = 0; i < size_; i++)
112 for(
int i = 0; i < size_; i++)
data[i] += mat.
data[i];
117 for(
int i = 0; i < size_; i++)
data[i] -= mat.
data[i];
122 *
this = *
this * other;
129 for(
int i = 0; i < size_; i++) m.
data[i] = m.
data[i] + mat.
data[i];
136 for(
int i = 0; i < size_; i++) res.
data[i] = res.
data[i] - mat.
data[i];
142 template<
int cols2 >
146 for(
int i = 0; i < rows; i++)
147 for(
int j = 0; j < cols2; j++)
150 for(
int k = 0; k < cols; k++)
151 sum +=
m[i][k]*mat.
m[k][j];
162 for(
int i = 0; i < size_; i++)
data[i] *= val;
167 for(
int i = 0; i < size_; i++)
data[i] /= val;
174 for(
int i = 0; i < size_; i++) m.
data[i] = m.
data[i] * val;
180 for(
int i = 0; i < size_; i++) m.
data[i] = m.
data[i] / val;
189 for(
int i = 0; i < size_; i++) m.
data[i] = -m.
data[i];
196 for(
int i = 0; i < size_; i++)
205 return !(*
this ==
m);
223 for(
int x = 0; x < rows; x++)
224 for(
int y = 0; y < cols; y++)
226 m[x][y] = x != y ? Type() : Type(1.0);
232 for(
int x = 0; x < rows; x++)
233 for(
int y = 0; y < cols; y++)
235 Type value = x != y ? Type() : Type(1.0);
236 if (
m[x][y] != value)
245 for(
size_t i = 0; i < rows; i++)
246 for(
size_t j = 0; j < cols; j++)
247 res.
m[j][i] =
m[i][j];
254 operator Type * () {
return data; }
255 operator const Type * ()
const {
return data; }
258 int size()
const {
return size_; }
273 static int const size_;
276 template<
int rows,
int cols,
typename Type >
int const GenericMatrix<rows, cols, Type>::size_ = rows * cols;
278 template <
typename Type,
typename Type2 >
inline
283 for(
int i = 0; i < 3; i++)
286 for(
int j = 0; j < 3; j++)
287 sum += matrix.
m[i][j]*point.
data[j];
289 res.
data[i] = (Type2)sum;
295 template<
int rows,
int cols,
typename Type >
305 template <
typename Type,
int size >
inline
314 for(
int i = 0; i < size; i++)
318 for(j = i; (j < size) && (
isZero(work[j*size+i])); j++) {};
325 for(
int k = 0; k < size; k++)
327 Type tmp = result[i*size+k]; result[i*size+k] = result[j*size+k]; result[j*size+k] = tmp;
328 tmp = work[i*size+k]; work[i*size+k] = work[j*size+k]; work[j*size+k] = tmp;
331 Type d = 1/work[i*size+i];
332 for(j = 0; j < size; j++)
334 result[i*size+j] *= d;
338 for(j = i+1; j < size; j++)
341 for(
int k = 0; k < size; k++)
343 result[j*size+k] -= result[i*size+k] * d;
344 work[j*size+k] -= work[i*size+k] * d;
349 for(
int i = size-1; i > 0; i--)
350 for(
int j = 0; j < i; j++)
352 Type d = work[j*size+i];
353 for(
int k = 0; k < size; k++)
355 result[j*size+k] -= result[i*size+k] * d;
356 work[j*size+k] -= work[i*size+k] * d;
365 template <
int size,
typename Type >
inline
372 throw std::runtime_error(
"artec::sdk::base::invert: Try to invert singular matrix");
376 template<
int rows,
int cols,
typename MatType,
typename Type>
379 if (vec.size() != rows * cols)
380 throw std::runtime_error(
"invalid matrix size");
384 template<
int rows,
int cols,
typename Type>
387 if (vec.size() != rows * cols)
388 throw std::runtime_error(
"invalid matrix size");
396 #endif //_GENERICMATRIX_H_
GenericMatrix & operator-=(const GenericMatrix &mat)
void setToIdentity()
Convert current matrix to identity one.
GenericMatrix & operator*=(const GenericMatrix &other)
bool isIdentity() const
Check whether the matrix is an identity one.
Point3< Type2 > operator*(const GenericMatrix< 3, 3, Type > &matrix, const Point3< Type2 > &point)
GenericMatrix & operator/=(const Type &val)
GenericMatrix operator-(const GenericMatrix &mat) const
GenericMatrix< cols, rows, Type > transposed() const
Return transposed matrix copy.
const Type & operator()(int row, int col) const
GenericMatrix operator/(const Type &val) const
GenericMatrix & operator=(const GenericMatrix &second)
GenericMatrix(const GenericMatrix< rows, cols, Type2 > &second)
Copying constructor with type conversion.
bool invert(const GenericMatrix< size, size, Type > &matrix, GenericMatrix< size, size, Type > &result)
Inversion It returns "false", if the matrix is singular and can't be inverted, otherwise the value is...
void fill(const Type &value)
Populate matrix with value.
GenericMatrix operator*(const Type &val) const
GenericMatrix< rows, cols2, Type > operator*(const GenericMatrix< cols, cols2, Type > &mat) const
Matrix-matrix multiplication.
GenericMatrix & operator*=(const Type &val)
GenericMatrix< rows, cols, MatType > vectorToMatrix(const std::vector< Type > &vec)
Type & operator()(int row, int col)
3-dimensional point class.
GenericMatrix(const GenericMatrix< rows2, cols2, Type2 > &second)
Copying constructor with size conversion.
GenericMatrix(const Type2 *values, int rows2, int cols2)
bool operator==(const GenericMatrix &other) const
Check whether the matrices are equal.
bool operator!=(const GenericMatrix &m) const
Check matrices whether the are not equal.
Matrix of unspecified size.
static GenericMatrix< rows, cols, Type > identity()
Identity matrix.
int size() const
Return the number of elements in the matrix.
GenericMatrix & operator=(const GenericMatrix< rows2, cols2, Type > &second)
GenericMatrix(const Type2(&values)[sizeOfArray])
GenericMatrix operator+(const GenericMatrix &mat) const
GenericMatrix operator-() const
Unary minus.
bool isZero(const Tf &some)
Equality expression for the types which are not exact.
GenericMatrix(const GenericMatrix &second)
Copying constructor.
GenericMatrix & operator+=(const GenericMatrix &mat)