7 #ifndef BALL_MATHS_MATRIX44_H
8 #define BALL_MATHS_MATRIX44_H
10 #ifndef BALL_COMMON_EXCEPTION_H
18 #ifndef BALL_MATHS_ANGLE_H
22 #ifndef BALL_MATHS_VECTOR3_H
26 #ifndef BALL_MATHS_VECTOR4_H
60 std::ostream& operator << (std::ostream& s, const TMatrix4x4<T>& m)
90 TMatrix4x4(const T* ptr);
98 TMatrix4x4(const T ptr[4][4]);
105 TMatrix4x4(const TMatrix4x4& m)
126 (const T&
m11, const T&
m12, const T&
m13, const T&
m14,
136 virtual ~TMatrix4x4()
157 void set(
const T* ptr);
164 void set(
const T ptr[4][4]);
185 (
const T&
m11,
const T&
m12,
const T&
m13,
const T&
m14,
188 const T&
m41,
const T&
m42,
const T&
m43,
const T&
m44);
215 void get(T* ptr)
const;
222 void get(T ptr[4][4])
const;
286 void set(
const T& t = (T)1);
468 void translate(
const T &x,
const T &y,
const T &z);
492 void scale(
const T& x_scale,
const T& y_scale,
const T& z_scale);
509 void setScale(
const T& x_scale,
const T& y_scale,
const T& z_scale);
557 void rotate(
const TAngle<T>& phi,
const T& axis_x,
const T& axis_y,
const T& axis_z);
664 void dump(std::ostream& s = std::cout,
Size depth = 0)
const;
726 *ptr++ = &
m11; *ptr++ = &
m12; *ptr++ = &
m13; *ptr++ = &
m14;
727 *ptr++ = &
m21; *ptr++ = &
m22; *ptr++ = &
m23; *ptr++ = &
m24;
728 *ptr++ = &
m31; *ptr++ = &
m32; *ptr++ = &
m33; *ptr++ = &
m34;
729 *ptr++ = &
m41; *ptr++ = &
m42; *ptr++ = &
m43; *ptr = &
m44;
737 template <
typename T>
739 : m11(0), m12(0), m13(0), m14(0),
740 m21(0), m22(0), m23(0), m24(0),
741 m31(0), m32(0), m33(0), m34(0),
742 m41(0), m42(0), m43(0), m44(0)
747 template <
typename T>
755 m11 = *ptr++; m12 = *ptr++; m13 = *ptr++; m14 = *ptr++;
756 m21 = *ptr++; m22 = *ptr++; m23 = *ptr++; m24 = *ptr++;
757 m31 = *ptr++; m32 = *ptr++; m33 = *ptr++; m34 = *ptr++;
758 m41 = *ptr++; m42 = *ptr++; m43 = *ptr++; m44 = *ptr;
760 initializeComponentPointers_();
763 template <
typename T>
771 const T *ptr = *array_ptr;
773 m11 = *ptr++; m12 = *ptr++; m13 = *ptr++; m14 = *ptr++;
774 m21 = *ptr++; m22 = *ptr++; m23 = *ptr++; m24 = *ptr++;
775 m31 = *ptr++; m32 = *ptr++; m33 = *ptr++; m34 = *ptr++;
776 m41 = *ptr++; m42 = *ptr++; m43 = *ptr++; m44 = *ptr;
778 initializeComponentPointers_();
781 template <
typename T>
783 : m11(m.m11), m12(m.m12), m13(m.m13), m14(m.m14),
784 m21(m.m21), m22(m.m22), m23(m.m23), m24(m.m24),
785 m31(m.m31), m32(m.m32), m33(m.m33), m34(m.m34),
786 m41(m.m41), m42(m.m42), m43(m.m43), m44(m.m44)
792 template <
typename T>
796 : m11(col1.
x), m12(col1.
y), m13(col1.
z), m14(col1.
h),
797 m21(col2.
x), m22(col2.
y), m23(col2.
z), m24(col2.
h),
798 m31(col3.
x), m32(col3.
y), m33(col3.
z), m34(col3.
h),
799 m41(col4.
x), m42(col4.
y), m43(col4.
z), m44(col4.
h)
801 initializeComponentPointers_();
804 template <
typename T>
806 (
const T& m11,
const T& m12,
const T& m13,
const T& m14,
807 const T& m21,
const T& m22,
const T& m23,
const T& m24,
808 const T& m31,
const T& m32,
const T& m33,
const T& m34,
809 const T& m41,
const T& m42,
const T& m43,
const T& m44)
810 : m11(m11), m12(m12), m13(m13), m14(m14),
811 m21(m21), m22(m22), m23(m23), m24(m24),
812 m31(m31), m32(m32), m33(m33), m34(m34),
813 m41(m41), m42(m42), m43(m43), m44(m44)
815 initializeComponentPointers_();
818 template <
typename T>
824 template <
typename T>
832 m11 = *ptr++; m12 = *ptr++; m13 = *ptr++; m14 = *ptr++;
833 m21 = *ptr++; m22 = *ptr++; m23 = *ptr++; m24 = *ptr++;
834 m31 = *ptr++; m32 = *ptr++; m33 = *ptr++; m34 = *ptr++;
835 m41 = *ptr++; m42 = *ptr++; m43 = *ptr++; m44 = *ptr;
838 template <
typename T>
846 const T *ptr = *array_ptr;
848 m11 = *ptr++; m12 = *ptr++; m13 = *ptr++; m14 = *ptr++;
849 m21 = *ptr++; m22 = *ptr++; m23 = *ptr++; m24 = *ptr++;
850 m31 = *ptr++; m32 = *ptr++; m33 = *ptr++; m34 = *ptr++;
851 m41 = *ptr++; m42 = *ptr++; m43 = *ptr++; m44 = *ptr;
854 template <
typename T>
863 template <
typename T>
868 m11 = col1.
x; m12 = col1.
y; m13 = col1.
z; m14 = col1.
h;
869 m21 = col2.
x; m22 = col2.
y; m23 = col2.
z; m24 = col2.
h;
870 m31 = col3.
x; m32 = col3.
y; m33 = col3.
z; m34 = col3.
h;
871 m41 = col4.
x; m42 = col4.
y; m43 = col4.
z; m44 = col4.
h;
874 template <
typename T>
876 (
const T& c11,
const T& c12,
const T& c13,
const T& c14,
877 const T& c21,
const T& c22,
const T& c23,
const T& c24,
878 const T& c31,
const T& c32,
const T& c33,
const T& c34,
879 const T& c41,
const T& c42,
const T& c43,
const T& c44)
881 m11 = c11; m12 = c12; m13 = c13; m14 = c14;
882 m21 = c21; m22 = c22; m23 = c23; m24 = c24;
883 m31 = c31; m32 = c32; m33 = c33; m34 = c34;
884 m41 = c41; m42 = c42; m43 = c43; m44 = c44;
887 template <
typename T>
895 template <
typename T>
903 template <
typename T>
911 template <
typename T>
919 *ptr++ = m11; *ptr++ = m12; *ptr++ = m13; *ptr++ = m14;
920 *ptr++ = m21; *ptr++ = m22; *ptr++ = m23; *ptr++ = m24;
921 *ptr++ = m31; *ptr++ = m32; *ptr++ = m33; *ptr++ = m34;
922 *ptr++ = m41; *ptr++ = m42; *ptr++ = m43; *ptr = m44;
925 template <
typename T>
935 *ptr++ = m11; *ptr++ = m12; *ptr++ = m13; *ptr++ = m14;
936 *ptr++ = m21; *ptr++ = m22; *ptr++ = m23; *ptr++ = m24;
937 *ptr++ = m31; *ptr++ = m32; *ptr++ = m33; *ptr++ = m34;
938 *ptr++ = m41; *ptr++ = m42; *ptr++ = m43; *ptr = m44;
941 template <
typename T>
947 template <
typename T>
952 col1.
x = m11; col1.
y = m12; col1.
z = m13; col1.
h = m14;
953 col2.
x = m21; col2.
y = m22; col2.
z = m23; col2.
h = m24;
954 col3.
x = m31; col3.
y = m32; col3.
z = m33; col3.
h = m34;
955 col4.
x = m41; col4.
y = m42; col4.
z = m43; col4.
h = m44;
958 template <
typename T>
960 (T& c11, T& c12, T& c13, T& c14,
961 T& c21, T& c22, T& c23, T& c24,
962 T& c31, T& c32, T& c33, T& c34,
963 T& c41, T& c42, T& c43, T& c44)
const
965 c11 = m11; c12 = m12; c13 = m13; c14 = m14;
966 c21 = m21; c22 = m22; c23 = m23; c24 = m24;
967 c31 = m31; c32 = m32; c33 = m33; c34 = m34;
968 c41 = m41; c42 = m42; c43 = m43; c44 = m44;
971 template <
typename T>
975 return (m11 + m22 + m33 + m44);
978 template <
typename T>
992 template <
typename T>
996 m12 = m13 = m14 = m21 = m23 = m24 = m31 = m32 = m34 = m41 = m42 = m43 = 0;
997 m11 = m22 = m33 = m44 = (T)1;
999 template <
typename T>
1012 template <
typename T>
1015 m11 = m12 = m13 = m14
1016 = m21 = m22 = m23 = m24
1017 = m31 = m32 = m33 = m34
1018 = m41 = m42 = m43 = m44
1022 template <
typename T>
1025 T tmp = m11; m11 = m.
m11; m.
m11 = tmp;
1026 tmp = m12; m12 = m.
m12; m.
m12 = tmp;
1027 tmp = m13; m13 = m.
m13; m.
m13 = tmp;
1028 tmp = m14; m14 = m.
m14; m.
m14 = tmp;
1029 tmp = m21; m21 = m.
m21; m.
m21 = tmp;
1030 tmp = m22; m22 = m.
m22; m.
m22 = tmp;
1031 tmp = m23; m23 = m.
m23; m.
m23 = tmp;
1032 tmp = m24; m24 = m.
m24; m.
m24 = tmp;
1033 tmp = m31; m31 = m.
m31; m.
m31 = tmp;
1034 tmp = m32; m32 = m.
m32; m.
m32 = tmp;
1035 tmp = m33; m33 = m.
m33; m.
m33 = tmp;
1036 tmp = m34; m34 = m.
m34; m.
m34 = tmp;
1037 tmp = m41; m41 = m.
m41; m.
m41 = tmp;
1038 tmp = m42; m42 = m.
m42; m.
m42 = tmp;
1039 tmp = m43; m43 = m.
m43; m.
m43 = tmp;
1040 tmp = m44; m44 = m.
m44; m.
m44 = tmp;
1043 template <
typename T>
1071 template <
typename T>
1080 const T* ptr = comp_ptr_[4 * row];
1081 return TVector4<T> (ptr[0], ptr[1], ptr[2], ptr[3]);
1084 template <
typename T>
1092 const T* ptr = comp_ptr_[col];
1094 return TVector4<T> (ptr[0], ptr[4], ptr[8], ptr[12]);
1098 template <
typename T>
1107 T* ptr = comp_ptr_[4 * row];
1109 ptr[0] = row_value.
x;
1110 ptr[1] = row_value.
y;
1111 ptr[2] = row_value.
z;
1112 ptr[3] = row_value.
h;
1115 template <
typename T>
1124 T* ptr = comp_ptr_[col];
1126 ptr[0] = col_value.
x;
1127 ptr[4] = col_value.
y;
1128 ptr[8] = col_value.
z;
1129 ptr[12] = col_value.
h;
1132 template <
typename T>
1148 template <
typename T>
1154 template <
typename T>
1158 if ((row > 3) || (col > 3))
1163 return *comp_ptr_[4 * row + col];
1166 template <
typename T>
1170 if ((row > 3) || (col > 3))
1175 return *comp_ptr_[4 * row + col];
1178 template <
typename T>
1186 return *comp_ptr_[position];
1189 template <
typename T>
1197 return *comp_ptr_[position];
1200 template <
typename T>
1207 template <
typename T>
1212 (-m11, -m12, -m13, -m14,
1213 -m21, -m22, -m23, -m24,
1214 -m31, -m32, -m33, -m34,
1215 -m41, -m42, -m43, -m44);
1218 template <
typename T>
1228 template <
typename T>
1251 template <
typename T>
1261 template <
typename T>
1284 template <
typename T>
1288 (m11 * scalar, m12 * scalar, m13 * scalar, m14 * scalar,
1289 m21 * scalar, m22 * scalar, m23 * scalar, m24 * scalar,
1290 m31 * scalar, m32 * scalar, m33 * scalar, m34 * scalar,
1291 m41 * scalar, m42 * scalar, m43 * scalar, m44 * scalar);
1294 template <
typename T>
1298 (scalar * m.
m11, scalar * m.
m12, scalar * m.
m13, scalar * m.
m14,
1299 scalar * m.
m21, scalar * m.
m22, scalar * m.
m23, scalar * m.
m24,
1300 scalar * m.
m31, scalar * m.
m32, scalar * m.
m33, scalar * m.
m34,
1301 scalar * m.
m41, scalar * m.
m42, scalar * m.
m43, scalar * m.
m44);
1304 template <
typename T>
1327 template <
typename T>
1331 (matrix.
m11 * vector.
x + matrix.
m12 * vector.
y + matrix.
m13 * vector.
z + matrix.
m14,
1332 matrix.
m21 * vector.
x + matrix.
m22 * vector.
y + matrix.
m23 * vector.
z + matrix.
m24,
1333 matrix.
m31 * vector.
x + matrix.
m32 * vector.
y + matrix.
m33 * vector.
z + matrix.
m34);
1336 template <
typename T>
1345 return (*
this * ((T)1 / scalar));
1348 template <
typename T>
1357 return (*
this *= (T)1 / scalar);
1360 template <
typename T>
1364 (m11 * m.
m11 + m12 * m.
m21 + m13 * m.
m31 + m14 * m.
m41,
1382 m41 * m.
m14 + m42 * m.
m24 + m43 * m.
m34 + m44 * m.
m44);
1385 template <
typename T>
1388 set(m11 * m.
m11 + m12 * m.
m21 + m13 * m.
m31 + m14 * m.
m41,
1406 m41 * m.
m14 + m42 * m.
m24 + m43 * m.
m34 + m44 * m.
m44);
1411 template <
typename T>
1415 (m11 * v.
x + m12 * v.
y + m13 * v.
z + m14 * v.
h,
1416 m21 * v.
x + m22 * v.
y + m23 * v.
z + m24 * v.
h,
1417 m31 * v.
x + m32 * v.
y + m33 * v.
z + m34 * v.
h,
1418 m41 * v.
x + m42 * v.
y + m43 * v.
z + m44 * v.
h);
1421 template <
typename T>
1435 { m11, m12, m13, m14 },
1436 { m21, m22, m23, m24 },
1437 { m31, m32, m33, m34 },
1438 { m41, m42, m43, m44 }
1442 T scale, sum_of_squares, sigma, tau;
1459 sum_of_squares = (T)0;
1461 sum_of_squares += a[i][k]*a[i][k];
1464 sigma = (a[
k][
k] >= 0) ? sqrt(sum_of_squares) : -sqrt(sum_of_squares);
1467 c[
k] = sigma*a[
k][
k];
1468 d[
k] = -scale*sigma;
1470 for (j = k+1; j<4; j++)
1473 sum_of_squares = (T)0;
1474 for (i = k; i<4; i++)
1475 sum_of_squares += a[i][k] * a[i][j];
1477 tau = sum_of_squares / c[
k];
1481 a[i][j] -= tau*a[i][k];
1499 result[0][0] = 1; result[0][1] = 0; result[0][2] = 0; result[0][3] = 0;
1500 result[1][0] = 0; result[1][1] = 1; result[1][2] = 0; result[1][3] = 0;
1501 result[2][0] = 0; result[2][1] = 0; result[2][2] = 1; result[2][3] = 0;
1502 result[3][0] = 0; result[3][1] = 0; result[3][2] = 0; result[3][3] = 1;
1509 sum_of_squares = (T)0;
1511 sum_of_squares += a[i][j]*result[i][k];
1513 tau = sum_of_squares / c[j];
1516 result[i][k] -= tau*a[i][j];
1520 result[3][
k] /= d[3];
1521 for (i=2; i>=0; i--)
1523 sum_of_squares = (T)0;
1524 for (j=i+1; j<4; j++)
1525 sum_of_squares += a[i][j] * result[j][k];
1527 result[i][
k] = (result[i][
k] - sum_of_squares) / d[i];
1532 inverse.
m11 = *k_ptr++;
1533 inverse.
m12 = *k_ptr++;
1534 inverse.
m13 = *k_ptr++;
1535 inverse.
m14 = *k_ptr++;
1536 inverse.
m21 = *k_ptr++;
1537 inverse.
m22 = *k_ptr++;
1538 inverse.
m23 = *k_ptr++;
1539 inverse.
m24 = *k_ptr++;
1540 inverse.
m31 = *k_ptr++;
1541 inverse.
m32 = *k_ptr++;
1542 inverse.
m33 = *k_ptr++;
1543 inverse.
m34 = *k_ptr++;
1544 inverse.
m41 = *k_ptr++;
1545 inverse.
m42 = *k_ptr++;
1546 inverse.
m43 = *k_ptr++;
1547 inverse.
m44 = *k_ptr;
1552 template <
typename T>
1555 return invert(*
this);
1558 template <
typename T>
1567 { m11, m12, m13, m14 },
1568 { m21, m22, m23, m24 },
1569 { m31, m32, m33, m34 },
1570 { m41, m42, m43, m44 }
1574 for (i = 0; i < 4; i++)
1576 for (j = 0; j < 3; j++)
1578 for (k = 0; k < 3; k++)
1581 matrix[j + 1][(k < i) ? k : k + 1];
1585 determinant += matrix[0][i] * (T)(i / 2.0 == (i >> 1) ? 1 : -1)
1586 * (submatrix[0][0] * submatrix[1][1] * submatrix[2][2]
1587 + submatrix[0][1] * submatrix[1][2] * submatrix[2][0]
1588 + submatrix[0][2] * submatrix[1][0] * submatrix[2][1]
1589 - submatrix[0][2] * submatrix[1][1] * submatrix[2][0]
1590 - submatrix[0][0] * submatrix[1][2] * submatrix[2][1]
1591 - submatrix[0][1] * submatrix[1][0] * submatrix[2][2]);
1597 template <
typename T>
1600 m14 += m11 * x + m12 * y + m13 * z;
1601 m24 += m21 * x + m22 * y + m23 * z;
1602 m34 += m31 * x + m32 * y + m33 * z;
1603 m44 += m41 * x + m42 * y + m43 * z;
1606 template <
typename T>
1609 m14 += m11 * v.
x + m12 * v.
y + m13 * v.
z;
1610 m24 += m21 * v.
x + m22 * v.
y + m23 * v.
z;
1611 m34 += m31 * v.
x + m32 * v.
y + m33 * v.
z;
1612 m44 += m41 * v.
x + m42 * v.
y + m43 * v.
z;
1615 template <
typename T>
1618 m11 = m22 = m33 = m44 = 1;
1623 m41 = m42 = m43 = 0;
1630 template <
typename T>
1633 m11 = m22 = m33 = m44 = 1;
1638 m41 = m42 = m43 = 0;
1645 template <
typename T>
1664 template <
typename T>
1683 template <
typename T>
1702 template <
typename T>
1713 m41 = m42 = m43 = 0;
1716 template <
typename T>
1727 m41 = m42 = m43 = 0;
1730 template <
typename T>
1741 m41 = m42 = m43 = 0;
1744 template <
typename T>
1754 template <
typename T>
1765 m22 = m33 = cos(phi);
1766 m23 = -(m32 = sin(phi));
1769 template <
typename T>
1779 template <
typename T>
1790 m11 = m33 = cos(phi);
1791 m31 = -(m13 = sin(phi));
1794 template <
typename T>
1804 template <
typename T>
1809 m13 = m14 = m23 = m24 = m31 =
1810 m32 = m34 = m41 = m42 = m43 = 0;
1812 m11 = m22 = cos(phi);
1813 m12 = -(m21 = sin(phi));
1816 template <
typename T>
1820 rotate(phi, v.
x, v.
y, v.
z);
1823 template <
typename T>
1827 rotate(phi, v.
x, v.
y, v.
z);
1886 template <
typename T>
1889 T xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
1894 double sin_angle = sin(phi);
1895 double cos_angle = cos(phi);
1901 T mag = sqrt(xx + yy + zz);
1905 m12 = m13 = m14 = m21 = m23 = m24 = m31 = m32 = m34 = m41 = m42 = m43 = 0;
1906 m11 = m22 = m33 = m44 = (T)1;
1923 xs = (T) (x * sin_angle);
1924 ys = (T) (y * sin_angle);
1925 zs = (T) (z * sin_angle);
1926 one_c = (T) (1 - cos_angle);
1928 m11 = (T)( (one_c * xx) + cos_angle );
1929 m12 = (one_c * xy) - zs;
1930 m13 = (one_c * zx) + ys;
1933 m21 = (one_c * xy) + zs;
1934 m22 = (T) ((one_c * yy) + cos_angle);
1935 m23 = (one_c * yz) - xs;
1938 m31 = (one_c * zx) - ys;
1939 m32 = (one_c * yz) + xs;
1940 m33 = (T) ((one_c * zz) + cos_angle);
1949 template <
typename T>
1952 m12 = m13 = m14 = m21 = m23 = m24 = m31 = m32 = m34 = m41 = m42 = m43 = 0;
1953 m11 = m22 = m33 = m44 = (T)1;
1954 rotate(phi, x, y, z);
1957 template <
typename T>
1961 m12 = m13 = m14 = m21 = m23 = m24 = m31 = m32 = m34 = m41 = m42 = m43 = 0;
1962 m11 = m22 = m33 = m44 = (T)1;
1963 rotate(phi, v.
x, v.
y, v.
z);
1966 template <
typename T>
1970 m12 = m13 = m14 = m21 = m23 = m24 = m31 = m32 = m34 = m41 = m42 = m43 = 0;
1971 m11 = m22 = m33 = m44 = (T)1;
1972 rotate(phi, v.
x, v.
y, v.
z);
1975 template <
typename T>
1997 template <
typename T>
2019 template <
typename T>
2041 template <
typename T>
2048 template <
typename T>
2055 template <
typename T>
2058 return ( m12 == m21 && m13 == m31
2059 && m14 == m41 && m23 == m32
2060 && m24 == m42 && m34 == m43);
2063 template <
typename T>
2066 return ( m12 == (T)0
2074 template <
typename T>
2077 return ( m21 == (T)0
2085 template <
typename T>
2089 return ( m12 == (T)0
2103 template <
typename T>
2106 T **ptr = (T **)comp_ptr_;
2108 return ( *ptr++ == &m11
2126 template <
typename T>
2139 template <
typename T>
2140 std::ostream& operator << (std::ostream& s, const TMatrix4x4<T>& m)
2142 s <<
'/' << std::setw(14) << m.m11 <<
' ' << std::setw(14) << m.m12 <<
' ' << std::setw(14) << m.m13 <<
' ' << std::setw(14) << m.m14 <<
" \\" << std::endl
2143 <<
'|' << std::setw(14) << m.m21 <<
' ' << std::setw(14) << m.m22 <<
' ' << std::setw(14) << m.m23 <<
' ' << std::setw(14) << m.m24 <<
" |" << std::endl
2144 <<
'|' << std::setw(14) << m.m31 <<
' ' << std::setw(14) << m.m32 <<
' ' << std::setw(14) << m.m33 <<
' ' << std::setw(14) << m.m34 <<
" |" << std::endl
2145 <<
'\\' << std::setw(14) << m.m41 <<
' ' << std::setw(14) << m.m42 <<
' ' << std::setw(14) << m.m43 <<
' ' << std::setw(14) << m.m44 <<
" /" << std::endl;
2150 template <
typename T>
2158 s << m11 <<
" " << m12 <<
" " << m13 <<
" " << m14 << std::endl;
2161 s << m21 <<
" " << m22 <<
" " << m23 <<
" " << m24 << std::endl;
2164 s << m31 <<
" " << m32 <<
" " << m33 <<
" " << m34 << std::endl;
2167 s << m41 <<
" " << m42 <<
" " << m43 <<
" " << m44 << std::endl;
2173 template <
typename T>
2177 template <
typename T>
2188 #endif // BALL_MATHS_MATRIX44_H