#include <3dmath.h>
Public Member Functions | |
matrix () | |
Constructor. | |
matrix (float _11, float _12, float _13, float _14, float _21, float _22, float _23, float _24, float _31, float _32, float _33, float _34, float _41, float _42, float _43, float _44) | |
Constructor. | |
matrix (const float *m) | |
Constructor. | |
matrix (const matrix &m) | |
Copy constructor. | |
matrix & | operator= (const matrix &mat) |
Assignment operator. | |
matrix | operator+ (const matrix &c) const |
Add operator. | |
matrix & | operator+= (const matrix &c) |
Add assignment operator. | |
matrix | operator- (const matrix &c) const |
Subtract operator. | |
matrix & | operator-= (const matrix &c) |
Subtract assignment operator. | |
matrix | operator* (const matrix &c) const |
Multiply operator. | |
matrix | operator* (float f) const |
Scalar multiply operator. | |
matrix & | operator*= (const matrix &m) |
Assignment multiply operator. | |
matrix & | operator*= (float f) |
Assignment scalar multiply operator. | |
bool | operator== (const matrix &m) const |
Comparison operator. | |
bool | IsUnit () const |
Test if it is unit matrix. | |
vector3 | ExtractTranslation () const |
Extract position from world transformation matrix. | |
matrix | ExtractRotation () const |
Get matrix preserving rotation (and scale) but with no translation. | |
vector3 | TransformCoord (const vector3 &v) const |
Transform 3D coordinate (assuming w = 1) with transformation matrix. | |
vector3 | TransformNormal (const vector3 &v) const |
Transform 3D direction with transformation matrix. | |
matrix | Inverse () const |
Get inverted matrix (homogeneous). | |
float | Det3x3 () const |
Get 3x3 determinant. | |
bool | InvertThisPrecise () |
matrix | Transpose () const |
Get transposed matrix. | |
matrix & | Translate (const vector3 &v) |
matrix & | Scale (const vector3 &v) |
Scale matrix. | |
void | ToEuler (float &x, float &y, float &z) const |
void | ToAxisAngle (vector3 &axis, float &angle) const |
wxString | ToString () const |
Get string. | |
matrix * | Clone () const |
Get copy of instance. | |
Static Public Member Functions | |
static matrix | ScaleMat (const vector3 &v) |
Get scale matrix. | |
static matrix | Translation (const vector3 &v) |
Get translation matrix. | |
static matrix | RotationX (float angle) |
Get rotation matrix around the X axis. | |
static matrix | RotationY (float angle) |
Get rotation matrix around the Y axis. | |
static matrix | RotationZ (float angle) |
Get rotation matrix around the Z axis. | |
static matrix | RotationYPR (float Yaw, float Pitch, float Roll) |
static matrix | RotationAxis (const vector3 &axis, float angle) |
static matrix | RotationVectors (const vector3 &vecStart, const vector3 &vecTo) |
Construct the rotation matrix that rotates 'vec1' to 'vec2'. | |
Public Attributes | |
union { | |
struct { | |
float _m11 | |
Components. | |
float _m12 | |
float _m13 | |
float _m14 | |
float _m21 | |
float _m22 | |
float _m23 | |
float _m24 | |
float _m31 | |
float _m32 | |
float _m33 | |
float _m34 | |
float _m41 | |
float _m42 | |
float _m43 | |
float _m44 | |
} | |
float _m [16] | |
Components. | |
}; | |
Friends | |
matrix | operator* (float f, const matrix &m) |
Scalar multiply operator. | |
std::ostream & | operator<< (std::ostream &os, const matrix &M) |
Output stream operator. |
Definition at line 422 of file 3dmath.h.
matrix::matrix | ( | ) | [inline] |
matrix::matrix | ( | float | _11, | |
float | _12, | |||
float | _13, | |||
float | _14, | |||
float | _21, | |||
float | _22, | |||
float | _23, | |||
float | _24, | |||
float | _31, | |||
float | _32, | |||
float | _33, | |||
float | _34, | |||
float | _41, | |||
float | _42, | |||
float | _43, | |||
float | _44 | |||
) | [inline] |
matrix::matrix | ( | const float * | m | ) | [inline] |
matrix::matrix | ( | const matrix & | m | ) | [inline] |
Multiply operator.
Definition at line 522 of file 3dmath.h.
00523 { 00524 matrix ret; 00525 ret._m11 = _m11*c._m11+_m12*c._m21+_m13*c._m31+_m14*c._m41; 00526 ret._m12 = _m11*c._m12+_m12*c._m22+_m13*c._m32+_m14*c._m42; 00527 ret._m13 = _m11*c._m13+_m12*c._m23+_m13*c._m33+_m14*c._m43; 00528 ret._m14 = _m11*c._m14+_m12*c._m24+_m13*c._m34+_m14*c._m44; 00529 ret._m21 = _m21*c._m11+_m22*c._m21+_m23*c._m31+_m24*c._m41; 00530 ret._m22 = _m21*c._m12+_m22*c._m22+_m23*c._m32+_m24*c._m42; 00531 ret._m23 = _m21*c._m13+_m22*c._m23+_m23*c._m33+_m24*c._m43; 00532 ret._m24 = _m21*c._m14+_m22*c._m24+_m23*c._m34+_m24*c._m44; 00533 ret._m31 = _m31*c._m11+_m32*c._m21+_m33*c._m31+_m34*c._m41; 00534 ret._m32 = _m31*c._m12+_m32*c._m22+_m33*c._m32+_m34*c._m42; 00535 ret._m33 = _m31*c._m13+_m32*c._m23+_m33*c._m33+_m34*c._m43; 00536 ret._m34 = _m31*c._m14+_m32*c._m24+_m33*c._m34+_m34*c._m44; 00537 ret._m41 = _m41*c._m11+_m42*c._m21+_m43*c._m31+_m44*c._m41; 00538 ret._m42 = _m41*c._m12+_m42*c._m22+_m43*c._m32+_m44*c._m42; 00539 ret._m43 = _m41*c._m13+_m42*c._m23+_m43*c._m33+_m44*c._m43; 00540 ret._m44 = _m41*c._m14+_m42*c._m24+_m43*c._m34+_m44*c._m44; 00541 return ret; 00542 }
matrix matrix::operator* | ( | float | f | ) | const [inline] |
matrix& matrix::operator*= | ( | float | f | ) | [inline] |
bool matrix::operator== | ( | const matrix & | m | ) | const [inline] |
bool matrix::IsUnit | ( | ) | const [inline] |
vector3 matrix::ExtractTranslation | ( | ) | const [inline] |
matrix matrix::ExtractRotation | ( | ) | const [inline] |
Transform 3D coordinate (assuming w = 1) with transformation matrix.
Definition at line 612 of file 3dmath.h.
00613 { 00614 vector3 ret; 00615 ret.x = v.x * _m11 + v.y * _m21 + v.z * _m31 + _m41; 00616 ret.y = v.x * _m12 + v.y * _m22 + v.z * _m32 + _m42; 00617 ret.z = v.x * _m13 + v.y * _m23 + v.z * _m33 + _m43; 00618 // float w = v.x * _m14 + v.y * _m24 + v.z * _m34 + _m44; 00619 // if (w != 1) 00620 // return ret; 00621 return ret; 00622 }
Transform 3D direction with transformation matrix.
Definition at line 625 of file 3dmath.h.
00626 { 00627 vector3 ret; 00628 ret.x = v.x * _m11 + v.y * _m21 + v.z * _m31; 00629 ret.y = v.x * _m12 + v.y * _m22 + v.z * _m32; 00630 ret.z = v.x * _m13 + v.y * _m23 + v.z * _m33; 00631 return ret; 00632 }
matrix matrix::Inverse | ( | ) | const [inline] |
Get inverted matrix (homogeneous).
Definition at line 635 of file 3dmath.h.
00636 { 00637 matrix ret; 00638 00639 ret._m[0] = _m[0]; 00640 ret._m[1] = _m[4]; 00641 ret._m[2] = _m[8]; 00642 ret._m[4] = _m[1]; 00643 ret._m[5] = _m[5]; 00644 ret._m[6] = _m[9]; 00645 ret._m[8] = _m[2]; 00646 ret._m[9] = _m[6]; 00647 ret._m[10] = _m[10]; 00648 00649 ret._m[12] = ret._m[0]*-_m[12]+ret._m[4]*-_m[13]+ret._m[8]*-_m[14]; 00650 ret._m[13] = ret._m[1]*-_m[12]+ret._m[5]*-_m[13]+ret._m[9]*-_m[14]; 00651 ret._m[14] = ret._m[2]*-_m[12]+ret._m[6]*-_m[13]+ret._m[10]*-_m[14]; 00652 00653 ret._m[3] = 0.0f; 00654 ret._m[7] = 0.0f; 00655 ret._m[11] = 0.0f; 00656 ret._m[15] = 1.0f; 00657 00658 return ret; 00659 }
float matrix::Det3x3 | ( | ) | const [inline] |
bool matrix::InvertThisPrecise | ( | ) | [inline] |
Inverse matrix computation gauss_jacobiho method .. from N.R. in C If matrix is regular = computatation successfull = returns true In case of singular matrix returns false
Definition at line 672 of file 3dmath.h.
00673 { 00674 int indxc[4],indxr[4],ipiv[4]; 00675 int i,icol,irow,j,k,l,ll,n; 00676 float big,dum,pivinv,temp; 00677 // satisfy the compiler 00678 icol = irow = 0; 00679 // the size of the matrix 00680 n = 4; 00681 00682 for ( j = 0 ; j < n ; j++) /* zero pivots */ 00683 ipiv[j] = 0; 00684 for ( i = 0; i < n; i++) 00685 { 00686 big = 0.0; 00687 for (j = 0 ; j < n ; j++) 00688 if (ipiv[j] != 1) 00689 for ( k = 0 ; k<n ; k++) 00690 { 00691 if (ipiv[k] == 0) 00692 { 00693 if (fabs(_m[4*k+j]) >= big) 00694 { 00695 big = fabs(_m[4*k+j]); 00696 irow = j; 00697 icol = k; 00698 } 00699 } 00700 else 00701 if (ipiv[k] > 1) 00702 return false; /* singular matrix */ 00703 } 00704 ++(ipiv[icol]); 00705 if (irow != icol) 00706 { 00707 for ( l = 0 ; l<n ; l++) 00708 { 00709 temp = _m[4*l+icol]; 00710 _m[4*l+icol] = _m[4*l+irow]; 00711 _m[4*l+irow] = temp; 00712 } 00713 } 00714 indxr[i] = irow; 00715 indxc[i] = icol; 00716 if (_m[4*icol+icol] == 0.0) 00717 return false; /* singular matrix */ 00718 00719 pivinv = 1.0f / _m[4*icol+icol]; 00720 _m[4*icol+icol] = 1.0 ; 00721 for ( l = 0 ; l<n ; l++) 00722 _m[4*l+icol] = _m[4*l+icol] * pivinv ; 00723 for (ll = 0 ; ll < n ; ll++) 00724 if (ll != icol) 00725 { 00726 dum = _m[4*icol+ll]; 00727 _m[4*icol+ll] = 0.0; 00728 for ( l = 0 ; l<n ; l++) 00729 _m[4*l+ll] = _m[4*l+ll] - _m[4*l+icol] * dum ; 00730 } 00731 } 00732 for ( l = n; l--; ) 00733 { 00734 if (indxr[l] != indxc[l]) 00735 for ( k = 0; k<n ; k++) 00736 { 00737 temp = _m[4*indxr[l]+k]; 00738 _m[4*indxr[l]+k] = _m[4*indxc[l]+k]; 00739 _m[4*indxc[l]+k] = temp; 00740 } 00741 } 00742 return true; // matrix is regular .. inversion has been succesfull 00743 }
matrix matrix::Transpose | ( | ) | const [inline] |
Translation matrix TODO: check multiply order
Definition at line 757 of file 3dmath.h.
00758 { 00759 *this *= Translation(v); 00760 return *this; 00761 }
void matrix::ToEuler | ( | float & | x, | |
float & | y, | |||
float & | z | |||
) | const [inline] |
void matrix::ToAxisAngle | ( | vector3 & | axis, | |
float & | angle | |||
) | const [inline] |
Convert matrix to Axis-Angle angles, used algorithm from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm It works only for pure rotation matrix!
Definition at line 787 of file 3dmath.h.
00788 { 00789 float epsilon = 0.01f; // margin to allow for rounding errors 00790 float epsilon2 = 0.1f; // margin to distinguish between 0 and 180 degrees 00791 // optional check that input is pure rotation, 'isRotationMatrix' is defined at: 00792 // http://www.euclideanspace.com/maths/algebra/matrix/orthogonal/rotation/ 00793 //assert isRotationMatrix(m) : "not valid rotation matrix" ;// for debugging 00794 if ((fabs(_m12-_m21)<EPSILON) && (fabs(_m13-_m31)<EPSILON) && (fabs(_m23-_m32)<EPSILON)) { 00795 // singularity found 00796 // first check for identity matrix which must have +1 for all terms 00797 // in leading diagonaland zero in other terms 00798 if ((fabs(_m12+_m21) < epsilon2) && (fabs(_m13+_m31) < epsilon2) && (fabs(_m23+_m32) < epsilon2) && (fabs(_m11+_m22+_m33-3) < epsilon2)) { 00799 // this singularity is identity matrix so angle = 0 and arbitrary axis 00800 axis=vector3(0, 1, 0); 00801 angle=0; 00802 } 00803 // otherwise this singularity is angle = 180 00804 angle = 3.14159f; 00805 float xx = (_m11+1)/2; 00806 float yy = (_m22+1)/2; 00807 float zz = (_m33+1)/2; 00808 float xy = (_m12+_m21)/4; 00809 float xz = (_m13+_m31)/4; 00810 float yz = (_m23+_m32)/4; 00811 if ((xx > yy) && (xx > zz)) { // _m11 is the largest diagonal term 00812 if (xx< epsilon) { 00813 axis=vector3(0.0f, 0.7071f, 0.7071f); 00814 } else { 00815 xx=sqrt(xx); 00816 axis=vector3(xx, xy/xx, xz/xx); 00817 } 00818 } else if (yy > zz) { // _m22 is the largest diagonal term 00819 if (yy< epsilon) { 00820 axis=vector3(0.7071f, 0.0f, 0.7071f); 00821 } else { 00822 yy=sqrt(yy); 00823 axis=vector3(xy/yy, yy, yz/yy); 00824 } 00825 } else { // _m33 is the largest diagonal term so base result on this 00826 if (zz< epsilon) { 00827 axis=vector3(0.7071f, 0.7071f, 0.0f); 00828 } else { 00829 zz = sqrt(zz); 00830 axis=vector3(xz/zz, yz/zz, zz); 00831 } 00832 } 00833 return;// return 180 deg rotation 00834 } 00835 // as we have reached here there are no singularities so we can handle normally 00836 // float s = sqrt((_m32 - _m23)*(_m32 - _m23) + (_m13 - _m31)*(_m13 - _m31) + (_m21 - _m12)*(_m21 - _m12)); // used to normalise 00837 // if (fabs(s) < 0.001) s=1; 00838 // prevent divide by zero, should not happen if matrix is orthogonal and should be 00839 // caught by singularity test above, but I've left it in just in case 00840 angle = acos(( _m11 + _m22 + _m33 - 1)/2); 00841 axis=vector3(_m32 - _m23, _m13 - _m31, _m21 - _m12).Normalize(); 00842 }
static matrix matrix::RotationX | ( | float | angle | ) | [inline, static] |
static matrix matrix::RotationY | ( | float | angle | ) | [inline, static] |
static matrix matrix::RotationZ | ( | float | angle | ) | [inline, static] |
static matrix matrix::RotationYPR | ( | float | Yaw, | |
float | Pitch, | |||
float | Roll | |||
) | [inline, static] |
Construct a yaw-pitch-roll rotation matrix. Rotate Yaw radians about the XY axis, rotate Pitch radians in the plane defined by the Yaw rotation, and rotate Roll radians about the axis defined by the previous two angles.
Definition at line 907 of file 3dmath.h.
00908 { 00909 matrix M; 00910 float ch = cos(Yaw); 00911 float sh = sin(Yaw); 00912 float cp = cos(Pitch); 00913 float sp = sin(Pitch); 00914 float cr = cos(Roll); 00915 float sr = sin(Roll); 00916 00917 M._m11 = ch * cr + sh * sp * sr; 00918 M._m12 = -ch * sr + sh * sp * cr; 00919 M._m13 = sh * cp; 00920 M._m21 = sr * cp; 00921 M._m22 = cr * cp; 00922 M._m23 = -sp; 00923 M._m31 = -sh * cr - ch * sp * sr; 00924 M._m32 = sr * sh + ch * sp * cr; 00925 M._m33 = ch * cp; 00926 00927 return M; 00928 }
Construct a rotation of a given angle about a given axis. Derived from Eric Haines's SPD (Standard Procedural Database).
Definition at line 933 of file 3dmath.h.
00934 { 00935 matrix M; 00936 float cosine = cos(angle); 00937 float sine = sin(angle); 00938 float one_minus_cosine = 1 - cosine; 00939 00940 M._m11 = axis.x * axis.x + (1.0f - axis.x * axis.x) * cosine; 00941 M._m21 = axis.x * axis.y * one_minus_cosine + axis.z * sine; 00942 M._m31 = axis.x * axis.z * one_minus_cosine - axis.y * sine; 00943 M._m41 = 0; 00944 00945 M._m12 = axis.x * axis.y * one_minus_cosine - axis.z * sine; 00946 M._m22 = axis.y * axis.y + (1.0f - axis.y * axis.y) * cosine; 00947 M._m32 = axis.y * axis.z * one_minus_cosine + axis.x * sine; 00948 M._m42 = 0; 00949 00950 M._m13 = axis.x * axis.z * one_minus_cosine + axis.y * sine; 00951 M._m23 = axis.y * axis.z * one_minus_cosine - axis.x * sine; 00952 M._m33 = axis.z * axis.z + (1.0f - axis.z * axis.z) * cosine; 00953 M._m43 = 0; 00954 00955 M._m14 = 0; 00956 M._m24 = 0; 00957 M._m34 = 0; 00958 M._m44 = 1; 00959 00960 return M; 00961 }
static matrix matrix::RotationVectors | ( | const vector3 & | vecStart, | |
const vector3 & | vecTo | |||
) | [inline, static] |
Construct the rotation matrix that rotates 'vec1' to 'vec2'.
Definition at line 965 of file 3dmath.h.
00966 { 00967 vector3 vec = vecStart.Cross(vecTo); 00968 00969 if (vec.Length() > 1e-6f) 00970 { 00971 // vector exist, compute angle 00972 float angle = acos(vecStart.Dot(vecTo)); 00973 // normalize for sure 00974 vec = vec.Normalize(); 00975 return RotationAxis(vec, angle); 00976 } 00977 00978 // opposite or colinear vectors 00979 matrix ret; 00980 if (vecStart.Dot(vecTo) < 0.0f) 00981 ret *= -1.0f; // opposite vectors 00982 00983 return ret; 00984 }
wxString matrix::ToString | ( | ) | const [inline] |
Get string.
Definition at line 987 of file 3dmath.h.
00988 { 00989 wxString str; 00990 for (int i = 0; i < 4; i++) 00991 { // y 00992 for (int j = 0; j < 4; j++) 00993 { // x 00994 str.Append(wxString::Format(wxT("%.2f "), _m[4*i+j])); 00995 } 00996 str << wxT("\n"); 00997 } 00998 return str; 00999 }
matrix* matrix::Clone | ( | ) | const [inline] |
std::ostream& operator<< | ( | std::ostream & | os, | |
const matrix & | M | |||
) | [friend] |
float matrix::_m11 |
float matrix::_m12 |
float matrix::_m13 |
float matrix::_m14 |
float matrix::_m21 |
float matrix::_m22 |
float matrix::_m23 |
float matrix::_m24 |
float matrix::_m31 |
float matrix::_m32 |
float matrix::_m33 |
float matrix::_m34 |
float matrix::_m41 |
float matrix::_m42 |
float matrix::_m43 |
float matrix::_m44 |
float matrix::_m[16] |
union { ... } |