17 namespace artec {
namespace sdk {
namespace base
20 template<
typename Type =
double >
29 Type _10, Type _11, Type _12, Type _13,
30 Type _20, Type _21, Type _22, Type _23,
31 Type _30, Type _31, Type _32, Type _33)
33 m[0][0] = _00;
m[0][1] = _01;
m[0][2] = _02;
m[0][3] = _03;
34 m[1][0] = _10;
m[1][1] = _11;
m[1][2] = _12;
m[1][3] = _13;
35 m[2][0] = _20;
m[2][1] = _21;
m[2][2] = _22;
m[2][3] = _23;
36 m[3][0] = _30;
m[3][1] = _31;
m[3][2] = _32;
m[3][3] = _33;
41 template<
typename Type2, std::
size_t sizeOfArray >
42 explicit Matrix4x4(
const Type2 (&values)[sizeOfArray])
44 int c_assert[
size_ == sizeOfArray ? 1 : -1];
46 for(
int i = 0; i <
size_; i++)
51 template<
typename Type2 >
52 Matrix4x4(
const Type2 * values,
int rows2,
int cols2)
54 for (
int matrixRow = 0; matrixRow <
rows_; ++matrixRow) {
55 for (
int matrixCol = 0; matrixCol <
cols_; ++matrixCol) {
56 if (matrixCol < cols2 && matrixRow < rows2)
57 m[matrixRow][matrixCol] = values[matrixRow * cols2 + matrixCol];
58 else if (matrixCol == matrixRow)
59 m[matrixRow][matrixCol] = Type(1);
61 m[matrixRow][matrixCol] = Type(0);
71 template<
int rows2,
int cols2,
typename Type2 >
75 template<
typename Type2 >
78 for(
int i = 0; i <
size_; i++)
data[i] = (Type)second.
data[i];
84 template<
int rows2,
int cols2,
typename Type2 >
87 for (
int matrixRow = 0; matrixRow <
rows_; ++matrixRow) {
88 for (
int matrixCol = 0; matrixCol <
cols_; ++matrixCol) {
89 if (matrixCol < cols2 && matrixRow < rows2)
90 m[matrixRow][matrixCol] = second.
m[matrixRow][matrixCol];
91 else if (matrixCol == matrixRow)
92 m[matrixRow][matrixCol] = Type(1);
94 m[matrixRow][matrixCol] = Type(0);
111 template <
int rows,
int cols>
114 return toGenericMatrix<rows, cols, Type>();
117 template <
int rows,
int cols,
typename Type2>
121 for (
int matrixRow = 0; matrixRow < rows; ++matrixRow) {
122 for (
int matrixCol = 0; matrixCol < cols; ++matrixCol) {
123 if (matrixRow < 4 && matrixCol < 4)
124 result.
m[matrixRow][matrixCol] =
m[matrixRow][matrixCol];
125 else if (matrixCol == matrixRow)
126 result.
m[matrixRow][matrixCol] = Type2(1);
128 result.
m[matrixRow][matrixCol] = Type2(0);
138 for(
int i = 0; i <
size_; i++)
167 *
this = *
this * other;
198 for(
int i = 0; i <
rows_; i++)
199 for(
int j = 0; j <
cols_; j++)
202 for(
int k = 0; k <
cols_; k++)
203 sum +=
m[i][k]*mat.
m[k][j];
210 template<
int cols2 >
214 for(
int i = 0; i <
rows_; i++)
215 for(
int j = 0; j < cols2; j++)
218 for(
int k = 0; k <
cols_; k++)
219 sum +=
m[i][k]*mat.
m[k][j];
228 template <
typename Type2>
235 for(
int i = 0; i < 4; i++)
238 for(
int j = 0; j < 4; j++)
239 sum +=
m[i][j]*point.
data[j];
241 res.
data[i] = (Type2)sum;
255 template <
typename Type2>
262 for(
int i = 0; i < 3; i++)
265 for(
int j = 0; j < 3; j++)
266 sum +=
m[i][j]*point.
data[j];
269 res.
data[i] = (Type2)sum;
279 for(
int i = 0; i <
size_; i++)
data[i] += val;
285 for(
int i = 0; i <
size_; i++)
data[i] -= val;
291 for(
int i = 0; i <
size_; i++)
data[i] *= val;
297 for(
int i = 0; i <
size_; i++)
data[i] /= val;
337 for(
int i = 0; i <
size_; i++)
346 return !(*
this ==
m);
370 for(
int x = 0; x <
rows_; x++)
371 for(
int y = 0; y <
cols_; y++)
373 m[x][y] = x != y ? Type() : Type(1.0);
384 if (
m[0][0] != Type(1.0) ||
m[0][1] != Type() ||
m[0][2] != Type() ||
m[0][3] != Type() )
387 if (
m[1][0] != Type() ||
m[1][1] != Type(1.0) ||
m[1][2] != Type() ||
m[1][3] != Type() )
390 if (
m[2][0] != Type() ||
m[2][1] != Type() ||
m[2][2] != Type(1.0) ||
m[2][3] != Type() )
393 if (
m[3][0] != Type() ||
m[3][1] != Type() ||
m[3][2] != Type() ||
m[3][3] != Type(1.0) )
404 for(
size_t i = 0; i <
rows_; i++)
405 for(
size_t j = 0; j <
cols_; j++)
406 res.
m[i][j] =
m[j][i];
420 for(
size_t j = 0; j < 3; j++)
421 m4sum +=
m.m[3][j] * point.
data[j];
424 for(
size_t j = 0; j < 3; j++)
425 pr.
data[j] = Type(pr.
data[j]/m4sum);
437 Type c = std::cos(angle);
438 Type s = std::sin(angle);
440 r.
m[1][1] = r.
m[2][2] = c;
441 r.
m[1][2] = -s; r.
m[2][1] = s;
442 r.
m[0][0] = r.
m[3][3] = 1;
451 Type c = std::cos(angle);
452 Type s = std::sin(angle);
454 r.
m[0][0] = r.
m[2][2] = c;
455 r.
m[0][2] = s; r.
m[2][0] = -s;
456 r.
m[1][1] = r.
m[3][3] = 1;
465 Type c = std::cos(angle);
466 Type s = std::sin(angle);
468 r.
m[0][0] = r.
m[1][1] = c;
469 r.
m[0][1] = -s; r.
m[1][0] = s;
470 r.
m[2][2] = r.
m[3][3] = 1;
480 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
489 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
498 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
509 Type c = std::cos(angle);
510 Type s = std::sin(angle);
512 r.
m[0][0] = c+(1-c)*direction.
x*direction.
x;
513 r.
m[0][1] = (1-c)*direction.
y*direction.
x-s*direction.
z;
514 r.
m[0][2] = (1-c)*direction.
z*direction.
x+s*direction.
y;
516 r.
m[1][0] = (1-c)*direction.
x*direction.
y+s*direction.
z;
517 r.
m[1][1] = c+(1-c)*direction.
y*direction.
y;
518 r.
m[1][2] = (1-c)*direction.
z*direction.
y-s*direction.
x;
520 r.
m[2][0] = (1-c)*direction.
x*direction.
z-s*direction.
y;
521 r.
m[2][1] = (1-c)*direction.
y*direction.
z+s*direction.
x;
522 r.
m[2][2] = c+(1-c)*direction.
z*direction.
z;
524 r.
m[0][3] = r.
m[1][3] = r.
m[2][3] = 0;
525 r.
m[3][0] = r.
m[3][1] = r.
m[3][2] = 0; r.
m[3][3] = 1;
536 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
547 t.
m[0][3] = direction.
x; t.
m[1][3] = direction.
y; t.
m[2][3] = direction.
z;
560 s.
m[0][0] = s.
m[1][1] = s.
m[2][2] = factor;
570 s.
m[0][3] = t.
x; s.
m[1][3] = t.
y; s.
m[2][3] = t.
z;
590 i.
m[0][3] = it.
x; i.
m[1][3] = it.
y; i.
m[2][3] = it.
z;
591 i.
m[3][0] = i.
m[3][1] = i.
m[3][2] = 0; i.
m[3][3] = 1;
600 res.
m[0][0] = 2.0f * nearVal / (right - left);
602 res.
m[0][2] = (right + left) / (right - left);
605 res.
m[1][1] = 2.0f * nearVal / (top - bottom);
606 res.
m[1][2] = (top + bottom) / (top - bottom);
610 res.
m[2][2] = -(farVal + nearVal) / (farVal - nearVal);
611 res.
m[2][3] = -2.0f * farVal * nearVal / (farVal - nearVal);
622 float f = 1.0f / tan(fovy * 0.5f);
623 float dneg = zNear - zFar;
624 return Matrix4x4( f / aspect, 0.0f, 0.0f, 0.0f,
626 0.0f, 0.0f, (zFar + zNear)/dneg, 2.0f*zNear*zFar/dneg,
627 0.0f, 0.0f, -1.0f, 0.0f );
631 static Matrix4x4 ortho(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
634 res.
m[0][0] = 2.0f / (right - left);
637 res.
m[0][3] = -(right + left) / (right - left);
639 res.
m[1][1] = 2.0f / (top - bottom);
641 res.
m[1][3] = -(top + bottom) / (top - bottom);
644 res.
m[2][2] = -2.0f / (farVal - nearVal);
645 res.
m[2][3] = -(farVal + nearVal) / (farVal - nearVal);
656 operator const Type * ()
const {
return data; }
690 template<
int rows,
typename Type >
694 for(
int i = 0; i < rows; i++)
695 for(
int j = 0; j < 4; j++)
698 for(
int k = 0; k < 4; k++)
699 sum += m1.
m[i][k]*m2.
m[k][j];
707 template<
typename Type >
718 for(
int i = 0; i < size; i++)
722 for(j = i; (j < size) && (
isZero(work[j*size+i])); j++) {};
733 for(
int k = 0; k < size; k++)
735 Type tmp = result[i*size+k]; result[i*size+k] = result[j*size+k]; result[j*size+k] = tmp;
736 tmp = work[i*size+k]; work[i*size+k] = work[j*size+k]; work[j*size+k] = tmp;
739 Type d = 1/work[i*size+i];
740 for(j = 0; j < size; j++)
742 result[i*size+j] *= d;
746 for(j = i+1; j < size; j++)
749 for(
int k = 0; k < size; k++)
751 result[j*size+k] -= result[i*size+k] * d;
752 work[j*size+k] -= work[i*size+k] * d;
757 for(
int i = size-1; i > 0; i--)
758 for(
int j = 0; j < i; j++)
760 Type d = work[j*size+i];
761 for(
int k = 0; k < size; k++)
763 result[j*size+k] -= result[i*size+k] * d;
764 work[j*size+k] -= work[i*size+k] * d;
775 template <
typename Type >
inline
778 bool invertable =
false;
779 result = matrix.
inverted(&invertable);
785 template <
typename Type >
inline
792 throw std::runtime_error(
"artec::sdk::base::invert: Try to invert singular matrix");
795 template <
typename Type >
inline
802 template <
typename Type >
inline
Point3< Type2 > operator*(const Point3< Type2 > &point) const
Special case of 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()
Make current matrix to identity.
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)
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 operator-(const Type &val) const
Matrix4x4< double > Matrix4x4D
Matrix4x4 operator*(const Type &val) const
bool operator!=(const Matrix4x4 &m) const
check matrices are not equal
Matrix4x4 operator*(const Matrix4x4 &mat) const
static Matrix4x4 translation(const Point3< Type > &direction)
Translations.
Matrix4x4 & operator+=(const Type &val)
Matrix4x4 & operator+=(const Matrix4x4 &mat)
void fill(const Type value)
Fill 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 operator==(const Matrix4x4 &m) const
check matrices are equal
bool isIdentity() const
Check if matrix is an identity matrix.
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
Matrix4x4 & operator-=(const Type &val)
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 types which are not exact.
GenericMatrix< rows, cols, Type2 > toGenericMatrix() const
Matrix4x4 transposed() const
Return transposed matrix copy.
static Matrix4x4 scale(Type factor, const Point3< Type > ¢er)
Matrix4x4 operator-() const
Unary minus.
static Matrix4x4 rotationY(Type angle, const Point3< Type > ¢er)
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 size_ conversion.
GenericMatrix< 3, 4, double > Matrix3x4D
Matrix4x4(const Matrix4x4< Type2 > &second)
Copying constructor with 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.
Matrix4x4 operator+(const Type &val) const
Type & operator()(int row, int col)
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