17 namespace artec {
namespace sdk {
namespace base
22 #pragma warning(disable: 4201)
24 template<
typename Type =
double >
34 Type _10, Type _11, Type _12, Type _13,
35 Type _20, Type _21, Type _22, Type _23,
36 Type _30, Type _31, Type _32, Type _33)
38 m[0][0] = _00;
m[0][1] = _01;
m[0][2] = _02;
m[0][3] = _03;
39 m[1][0] = _10;
m[1][1] = _11;
m[1][2] = _12;
m[1][3] = _13;
40 m[2][0] = _20;
m[2][1] = _21;
m[2][2] = _22;
m[2][3] = _23;
41 m[3][0] = _30;
m[3][1] = _31;
m[3][2] = _32;
m[3][3] = _33;
44 template<
typename Type2, std::
size_t sizeOfArray >
45 explicit Matrix4x4(
const Type2 (&values)[sizeOfArray])
47 int c_assert[
size_ == sizeOfArray ? 1 : -1];
49 for(
int i = 0; i <
size_; i++)
53 template <
typename It>
57 for(; i <
size() && begin != end; ++begin, ++i)
63 for(; i <
size(); ++i)
70 template<
typename Type2 >
71 Matrix4x4(
const Type2 * values,
int rows2,
int cols2)
73 for (
int matrixRow = 0; matrixRow <
rows_; ++matrixRow) {
74 for (
int matrixCol = 0; matrixCol <
cols_; ++matrixCol) {
75 if (matrixCol < cols2 && matrixRow < rows2)
76 m[matrixRow][matrixCol] = values[matrixRow * cols2 + matrixCol];
77 else if (matrixCol == matrixRow)
78 m[matrixRow][matrixCol] = Type(1);
80 m[matrixRow][matrixCol] = Type(0);
89 template<
int rows2,
int cols2,
typename Type2 >
93 template<
typename Type2 >
96 for(
int i = 0; i < size_; i++) data[i] = static_cast<Type>(second[i]);
101 template<
int rows2,
int cols2,
typename Type2 >
104 for (
int matrixRow = 0; matrixRow <
rows_; ++matrixRow) {
105 for (
int matrixCol = 0; matrixCol <
cols_; ++matrixCol) {
106 if (matrixCol < cols2 && matrixRow < rows2)
107 m[matrixRow][matrixCol] = second.
m[matrixRow][matrixCol];
108 else if (matrixCol == matrixRow)
109 m[matrixRow][matrixCol] = Type(1);
111 m[matrixRow][matrixCol] = Type(0);
126 template <
int rows,
int cols>
129 return toGenericMatrix<rows, cols, Type>();
132 template <
int rows,
int cols,
typename Type2>
136 for (
int matrixRow = 0; matrixRow < rows; ++matrixRow) {
137 for (
int matrixCol = 0; matrixCol < cols; ++matrixCol) {
138 if (matrixRow < 4 && matrixCol < 4)
139 result.
m[matrixRow][matrixCol] =
m[matrixRow][matrixCol];
140 else if (matrixCol == matrixRow)
141 result.
m[matrixRow][matrixCol] = Type2(1);
143 result.
m[matrixRow][matrixCol] = Type2(0);
153 for(
int i = 0; i <
size_; i++)
172 *
this = *
this * other;
196 for(
int i = 0; i <
rows_; i++)
197 for(
int j = 0; j <
cols_; j++)
200 for(
int k = 0; k <
cols_; k++)
201 sum +=
m[i][k]*mat.
m[k][j];
208 template<
int cols2 >
212 for(
int i = 0; i <
rows_; i++)
213 for(
int j = 0; j < cols2; j++)
216 for(
int k = 0; k <
cols_; k++)
217 sum +=
m[i][k]*mat.
m[k][j];
226 template <
typename Type2>
230 for(
int i = 0; i < 4; i++)
233 for(
int j = 0; j < 4; j++)
234 sum +=
m[i][j]*point.
data[j];
236 res.
data[i] = (Type2)sum;
243 template <
typename Type2>
247 for(
int i = 0; i < 3; i++)
250 for(
int j = 0; j < 3; j++)
251 sum +=
m[i][j]*point.
data[j];
254 res.
data[i] = (Type2)sum;
264 for(
int i = 0; i <
size_; i++)
data[i] *= val;
269 for(
int i = 0; i <
size_; i++)
data[i] /= val;
298 for (
int i = 0; i <
size_; i++)
311 return !(*
this == other);
334 for(
int x = 0; x <
rows_; x++)
335 for(
int y = 0; y <
cols_; y++)
337 m[x][y] = x != y ? Type() : Type(1.0);
344 if (
m[0][0] != Type(1.0) ||
m[0][1] != Type() ||
m[0][2] != Type() ||
m[0][3] != Type() )
347 if (
m[1][0] != Type() ||
m[1][1] != Type(1.0) ||
m[1][2] != Type() ||
m[1][3] != Type() )
350 if (
m[2][0] != Type() ||
m[2][1] != Type() ||
m[2][2] != Type(1.0) ||
m[2][3] != Type() )
353 if (
m[3][0] != Type() ||
m[3][1] != Type() ||
m[3][2] != Type() ||
m[3][3] != Type(1.0) )
363 for(
size_t i = 0; i <
rows_; i++)
364 for(
size_t j = 0; j <
cols_; j++)
365 res.
m[i][j] =
m[j][i];
379 for(
size_t j = 0; j < 3; j++)
380 m4sum +=
m.m[3][j] * point.
data[j];
383 for(
size_t j = 0; j < 3; j++)
384 pr.
data[j] = Type(pr.
data[j]/m4sum);
396 Type c = std::cos(angle);
397 Type s = std::sin(angle);
399 r.
m[1][1] = r.
m[2][2] = c;
400 r.
m[1][2] = -s; r.
m[2][1] = s;
401 r.
m[0][0] = r.
m[3][3] = 1;
410 Type c = std::cos(angle);
411 Type s = std::sin(angle);
413 r.
m[0][0] = r.
m[2][2] = c;
414 r.
m[0][2] = s; r.
m[2][0] = -s;
415 r.
m[1][1] = r.
m[3][3] = 1;
424 Type c = std::cos(angle);
425 Type s = std::sin(angle);
427 r.
m[0][0] = r.
m[1][1] = c;
428 r.
m[0][1] = -s; r.
m[1][0] = s;
429 r.
m[2][2] = r.
m[3][3] = 1;
439 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
448 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
457 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
468 Type c = std::cos(angle);
469 Type s = std::sin(angle);
471 r.
m[0][0] = c+(1-c)*direction.
x*direction.
x;
472 r.
m[0][1] = (1-c)*direction.
y*direction.
x-s*direction.
z;
473 r.
m[0][2] = (1-c)*direction.
z*direction.
x+s*direction.
y;
475 r.
m[1][0] = (1-c)*direction.
x*direction.
y+s*direction.
z;
476 r.
m[1][1] = c+(1-c)*direction.
y*direction.
y;
477 r.
m[1][2] = (1-c)*direction.
z*direction.
y-s*direction.
x;
479 r.
m[2][0] = (1-c)*direction.
x*direction.
z-s*direction.
y;
480 r.
m[2][1] = (1-c)*direction.
y*direction.
z+s*direction.
x;
481 r.
m[2][2] = c+(1-c)*direction.
z*direction.
z;
483 r.
m[0][3] = r.
m[1][3] = r.
m[2][3] = 0;
484 r.
m[3][0] = r.
m[3][1] = r.
m[3][2] = 0; r.
m[3][3] = 1;
495 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
506 t.
m[0][3] = direction.
x; t.
m[1][3] = direction.
y; t.
m[2][3] = direction.
z;
518 s.
m[0][0] = s.
m[1][1] = s.
m[2][2] = factor;
528 s.
m[0][3] = t.
x; s.
m[1][3] = t.
y; s.
m[2][3] = t.
z;
548 i.
m[0][3] = it.
x; i.
m[1][3] = it.
y; i.
m[2][3] = it.
z;
549 i.
m[3][0] = i.
m[3][1] = i.
m[3][2] = 0; i.
m[3][3] = 1;
558 res.
m[0][0] = 2.0f * nearVal / (right - left);
560 res.
m[0][2] = (right + left) / (right - left);
563 res.
m[1][1] = 2.0f * nearVal / (top - bottom);
564 res.
m[1][2] = (top + bottom) / (top - bottom);
568 res.
m[2][2] = -(farVal + nearVal) / (farVal - nearVal);
569 res.
m[2][3] = -2.0f * farVal * nearVal / (farVal - nearVal);
580 float f = 1.0f / tan(fovy * 0.5f);
581 float dneg = zNear - zFar;
582 return Matrix4x4( f / aspect, 0.0f, 0.0f, 0.0f,
584 0.0f, 0.0f, (zFar + zNear)/dneg, 2.0f*zNear*zFar/dneg,
585 0.0f, 0.0f, -1.0f, 0.0f );
589 static Matrix4x4 ortho(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
592 res.
m[0][0] = 2.0f / (right - left);
595 res.
m[0][3] = -(right + left) / (right - left);
597 res.
m[1][1] = 2.0f / (top - bottom);
599 res.
m[1][3] = -(top + bottom) / (top - bottom);
602 res.
m[2][2] = -2.0f / (farVal - nearVal);
603 res.
m[2][3] = -(farVal + nearVal) / (farVal - nearVal);
613 operator const Type * ()
const {
return data; }
654 template<
int rows,
typename Type >
658 for(
int i = 0; i < rows; i++)
659 for(
int j = 0; j < 4; j++)
662 for(
int k = 0; k < 4; k++)
663 sum += m1.
m[i][k]*m2.
m[k][j];
671 template<
typename Type >
682 for(
int i = 0; i < size; i++)
686 for(j = i; (j < size) && (
isZero(work[j*size+i])); j++) {};
697 for(
int k = 0; k < size; k++)
699 Type tmp = result[i*size+k]; result[i*size+k] = result[j*size+k]; result[j*size+k] = tmp;
700 tmp = work[i*size+k]; work[i*size+k] = work[j*size+k]; work[j*size+k] = tmp;
703 Type d = 1/work[i*size+i];
704 for(j = 0; j < size; j++)
706 result[i*size+j] *= d;
710 for(j = i+1; j < size; j++)
713 for(
int k = 0; k < size; k++)
715 result[j*size+k] -= result[i*size+k] * d;
716 work[j*size+k] -= work[i*size+k] * d;
721 for(
int i = size-1; i > 0; i--)
722 for(
int j = 0; j < i; j++)
724 Type d = work[j*size+i];
725 for(
int k = 0; k < size; k++)
727 result[j*size+k] -= result[i*size+k] * d;
728 work[j*size+k] -= work[i*size+k] * d;
738 template <
typename Type >
inline
741 bool invertable =
false;
742 result = matrix.
inverted(&invertable);
748 template <
typename Type >
inline
755 throw std::runtime_error(
"artec::sdk::base::invert: Try to invert singular matrix");
758 template <
typename Type >
inline
765 template <
typename Type >
inline
Point3< Type2 > operator*(const Point3< Type2 > &point) const
Special case of the matrix-point multiplication: geometric transformation.
static Matrix4x4 perspective(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
Get perspective projection matrix.
Matrix4x4(const Type2 *values, int rows2, int cols2)
Matrix4x4 & operator/=(const Type &val)
GenericMatrix< rows, cols, Type > toGenericMatrix() const
Matrix4x4 & operator-=(const Matrix4x4 &mat)
GenericMatrix< 2, 2, double > Matrix2x2D
Matrix4x4 operator-(const Matrix4x4 &mat) const
Matrix4x4< Type > inverted(bool *invertible=0) const
Inversion.
static Matrix4x4 rotationX(Type angle, const Point3< Type > ¢er)
Point3< Type2 > operator*(const GenericMatrix< 3, 3, Type > &matrix, const Point3< Type2 > &point)
static Matrix4x4 rotationZ(Type angle)
Matrix4x4 & operator*=(const Type &val)
static Matrix4x4 rotationY(Type angle)
GenericMatrix< 3, 3, double > Matrix3x3D
void setToIdentity()
Turn current matrix to identity.
4-dimensional point class.
Matrix4x4(Type _00, Type _01, Type _02, Type _03, Type _10, Type _11, Type _12, Type _13, Type _20, Type _21, Type _22, Type _23, Type _30, Type _31, Type _32, Type _33)
GenericMatrix< 2, 3, float > Matrix2x3F
Matrix4x4(const Type2(&values)[sizeOfArray])
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...
Matrix4x4 operator=(const GenericMatrix< rows2, cols2, Type2 > &second)
Point3< Type > project(const Point3< Type > &point) const
Matrix4x4 & operator*=(const Matrix4x4 &other)
static Matrix4x4 scale(Type factor)
static Matrix4x4 inverseMotion(const Matrix4x4 &matrix)
Inverse transformation matrix.
static Matrix4x4 perspectiveFov(float fovy, float aspect, float zNear, float zFar)
Get perspective projection matrix.
static Matrix4x4 rotationX(Type angle)
Matrix4x4< double > Matrix4x4D
Matrix4x4 operator*(const Type &val) const
2-dimensional point class.
bool operator!=(const Matrix4x4 &other) const
Check whether the matrices are not equal.
Matrix4x4 operator*(const Matrix4x4 &mat) const
const Type & operator[](int index) const
Plain data element access operator Returns const reference to the element in the inner data array...
static Matrix4x4 translation(const Point3< Type > &direction)
Translations.
3-dimensional point class.
int size() const
Returns the number of elements in the matrix.
Matrix of unspecified size.
Matrix4x4 & operator+=(const Matrix4x4 &mat)
void fill(const Type value)
Fill in the matrix with value.
GenericMatrix< 2, 2, float > Matrix2x2F
static Matrix4x4 rotationZ(Type angle, const Point3< Type > ¢er)
GenericMatrix< 2, 3, double > Matrix2x3D
Matrix4x4< float > Matrix4x4F
bool isIdentity() const
Check if the matrix is an identity one.
GenericMatrix< 3, 3, float > Matrix3x3F
const Type & operator()(int row, int col) const
Matrix4x4(const Matrix4x4 &second)
Copying constructor.
GenericMatrix< 3, 4, float > Matrix3x4F
Point4< Type2 > operator*(const Point4< Type2 > &point) const
Matrix-point multiplication.
Matrix4x4 operator+(const Matrix4x4 &mat) const
static Matrix4x4 rotation(Type angle, const Point3< Type > &direction)
Important: Direction is a unit vector, don't forget to normalize it!
bool isZero(const Tf &some)
Equality expression for the types which are not exact.
GenericMatrix< rows, cols, Type2 > toGenericMatrix() const
Matrix4x4 transposed() const
Return copy of transposed matrix.
static Matrix4x4 scale(Type factor, const Point3< Type > ¢er)
Matrix4x4 operator-() const
Unary minus.
static Matrix4x4 rotationY(Type angle, const Point3< Type > ¢er)
const Type * getData() const
GenericMatrix< 2, 4, float > Matrix2x4F
Matrix4x4 & operator=(const Matrix4x4 &second)
Point2< Type > project(const Point3< Type > &point, const GenericMatrix< 3, 4, Type > &m)
Matrix4x4(const GenericMatrix< rows2, cols2, Type2 > &second)
Copying constructor with the size_ conversion.
GenericMatrix< 3, 4, double > Matrix3x4D
Matrix4x4(const Matrix4x4< Type2 > &second)
Copying constructor with the type conversion.
static Matrix4x4 ortho(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
Get orthogonal matrix.
GenericMatrix< 4, cols2, Type > operator*(const GenericMatrix< 4, cols2, Type > &mat) const
static Matrix4x4 identity()
Identity matrix.
Type & operator()(int row, int col)
Matrix4x4(It begin, It end)
bool operator==(const Matrix4x4 &other) const
Check whether the matrices are equal.
GenericMatrix< 2, 4, double > Matrix2x4D
static Matrix4x4 rotation(Type angle, const Point3< Type > &direction, const Point3< Type > ¢er)
Important: Direction is a unit vector, don't forget to normalize it!
Matrix4x4 operator/(const Type &val) const