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;
248 template <
typename Type2>
255 for(
int i = 0; i < 3; i++)
258 for(
int j = 0; j < 3; j++)
259 sum +=
m[i][j]*point.
data[j];
262 res.
data[i] = (Type2)sum;
272 for(
int i = 0; i <
size_; i++)
data[i] *= val;
278 for(
int i = 0; i <
size_; i++)
data[i] /= val;
311 for(
int i = 0; i <
size_; i++)
320 return !(*
this ==
m);
344 for(
int x = 0; x <
rows_; x++)
345 for(
int y = 0; y <
cols_; y++)
347 m[x][y] = x != y ? Type() : Type(1.0);
358 if (
m[0][0] != Type(1.0) ||
m[0][1] != Type() ||
m[0][2] != Type() ||
m[0][3] != Type() )
361 if (
m[1][0] != Type() ||
m[1][1] != Type(1.0) ||
m[1][2] != Type() ||
m[1][3] != Type() )
364 if (
m[2][0] != Type() ||
m[2][1] != Type() ||
m[2][2] != Type(1.0) ||
m[2][3] != Type() )
367 if (
m[3][0] != Type() ||
m[3][1] != Type() ||
m[3][2] != Type() ||
m[3][3] != Type(1.0) )
378 for(
size_t i = 0; i <
rows_; i++)
379 for(
size_t j = 0; j <
cols_; j++)
380 res.
m[i][j] =
m[j][i];
394 for(
size_t j = 0; j < 3; j++)
395 m4sum +=
m.m[3][j] * point.
data[j];
398 for(
size_t j = 0; j < 3; j++)
399 pr.
data[j] = Type(pr.
data[j]/m4sum);
411 Type c = std::cos(angle);
412 Type s = std::sin(angle);
414 r.
m[1][1] = r.
m[2][2] = c;
415 r.
m[1][2] = -s; r.
m[2][1] = s;
416 r.
m[0][0] = r.
m[3][3] = 1;
425 Type c = std::cos(angle);
426 Type s = std::sin(angle);
428 r.
m[0][0] = r.
m[2][2] = c;
429 r.
m[0][2] = s; r.
m[2][0] = -s;
430 r.
m[1][1] = r.
m[3][3] = 1;
439 Type c = std::cos(angle);
440 Type s = std::sin(angle);
442 r.
m[0][0] = r.
m[1][1] = c;
443 r.
m[0][1] = -s; r.
m[1][0] = s;
444 r.
m[2][2] = r.
m[3][3] = 1;
454 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
463 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
472 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
483 Type c = std::cos(angle);
484 Type s = std::sin(angle);
486 r.
m[0][0] = c+(1-c)*direction.
x*direction.
x;
487 r.
m[0][1] = (1-c)*direction.
y*direction.
x-s*direction.
z;
488 r.
m[0][2] = (1-c)*direction.
z*direction.
x+s*direction.
y;
490 r.
m[1][0] = (1-c)*direction.
x*direction.
y+s*direction.
z;
491 r.
m[1][1] = c+(1-c)*direction.
y*direction.
y;
492 r.
m[1][2] = (1-c)*direction.
z*direction.
y-s*direction.
x;
494 r.
m[2][0] = (1-c)*direction.
x*direction.
z-s*direction.
y;
495 r.
m[2][1] = (1-c)*direction.
y*direction.
z+s*direction.
x;
496 r.
m[2][2] = c+(1-c)*direction.
z*direction.
z;
498 r.
m[0][3] = r.
m[1][3] = r.
m[2][3] = 0;
499 r.
m[3][0] = r.
m[3][1] = r.
m[3][2] = 0; r.
m[3][3] = 1;
510 r.
m[0][3] = t.
x; r.
m[1][3] = t.
y; r.
m[2][3] = t.
z;
521 t.
m[0][3] = direction.
x; t.
m[1][3] = direction.
y; t.
m[2][3] = direction.
z;
534 s.
m[0][0] = s.
m[1][1] = s.
m[2][2] = factor;
544 s.
m[0][3] = t.
x; s.
m[1][3] = t.
y; s.
m[2][3] = t.
z;
564 i.
m[0][3] = it.
x; i.
m[1][3] = it.
y; i.
m[2][3] = it.
z;
565 i.
m[3][0] = i.
m[3][1] = i.
m[3][2] = 0; i.
m[3][3] = 1;
574 res.
m[0][0] = 2.0f * nearVal / (right - left);
576 res.
m[0][2] = (right + left) / (right - left);
579 res.
m[1][1] = 2.0f * nearVal / (top - bottom);
580 res.
m[1][2] = (top + bottom) / (top - bottom);
584 res.
m[2][2] = -(farVal + nearVal) / (farVal - nearVal);
585 res.
m[2][3] = -2.0f * farVal * nearVal / (farVal - nearVal);
596 float f = 1.0f / tan(fovy * 0.5f);
597 float dneg = zNear - zFar;
598 return Matrix4x4( f / aspect, 0.0f, 0.0f, 0.0f,
600 0.0f, 0.0f, (zFar + zNear)/dneg, 2.0f*zNear*zFar/dneg,
601 0.0f, 0.0f, -1.0f, 0.0f );
605 static Matrix4x4 ortho(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
608 res.
m[0][0] = 2.0f / (right - left);
611 res.
m[0][3] = -(right + left) / (right - left);
613 res.
m[1][1] = 2.0f / (top - bottom);
615 res.
m[1][3] = -(top + bottom) / (top - bottom);
618 res.
m[2][2] = -2.0f / (farVal - nearVal);
619 res.
m[2][3] = -(farVal + nearVal) / (farVal - nearVal);
630 operator const Type * ()
const {
return data; }
664 template<
int rows,
typename Type >
668 for(
int i = 0; i < rows; i++)
669 for(
int j = 0; j < 4; j++)
672 for(
int k = 0; k < 4; k++)
673 sum += m1.
m[i][k]*m2.
m[k][j];
681 template<
typename Type >
692 for(
int i = 0; i < size; i++)
696 for(j = i; (j < size) && (
isZero(work[j*size+i])); j++) {};
707 for(
int k = 0; k < size; k++)
709 Type tmp = result[i*size+k]; result[i*size+k] = result[j*size+k]; result[j*size+k] = tmp;
710 tmp = work[i*size+k]; work[i*size+k] = work[j*size+k]; work[j*size+k] = tmp;
713 Type d = 1/work[i*size+i];
714 for(j = 0; j < size; j++)
716 result[i*size+j] *= d;
720 for(j = i+1; j < size; j++)
723 for(
int k = 0; k < size; k++)
725 result[j*size+k] -= result[i*size+k] * d;
726 work[j*size+k] -= work[i*size+k] * d;
731 for(
int i = size-1; i > 0; i--)
732 for(
int j = 0; j < i; j++)
734 Type d = work[j*size+i];
735 for(
int k = 0; k < size; k++)
737 result[j*size+k] -= result[i*size+k] * d;
738 work[j*size+k] -= work[i*size+k] * d;
749 template <
typename Type >
inline
752 bool invertable =
false;
753 result = matrix.
inverted(&invertable);
759 template <
typename Type >
inline
766 throw std::runtime_error(
"artec::sdk::base::invert: Try to invert singular matrix");
769 template <
typename Type >
inline
776 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< 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 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
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.
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