EMAN2
Public Member Functions | Static Public Member Functions | Private Attributes
EMAN::Matrix4 Class Reference

#include <vecmath.h>

List of all members.

Public Member Functions

 Matrix4 ()
 Matrix4 (const Vector4 &row0, const Vector4 &row1, const Vector4 &row2, const Vector4 &row3)
 Matrix4 (const Vector3 &row0, const Vector3 &row1, const Vector3 &row2)
 Matrix4 (const Matrix4 &m)
Matrix4operator= (const Matrix4 &m)
int index (int row, int col) const
const double & operator() (int row, int col) const
double & operator() (int row, int col)
Vector4 row (int r) const
Vector4 column (int c) const
Matrix4operator*= (const Matrix4 &m)
Matrix4operator+= (const Matrix4 &m)
Matrix4operator-= (const Matrix4 &m)
Matrix4 transpose () const
Matrix4 operator+ (const Matrix4 &m) const
Matrix4 operator- (const Matrix4 &m) const
Vector3 operator* (const Vector3 &v) const
Point3 operator* (const Point3 &p) const
Vector4 operator* (const Vector4 &v) const
Matrix4 operator* (const Matrix4 &b) const
Matrix4 inverse () const
bool operator== (const Matrix4 &m) const
bool approxEqual (const Matrix4 &m, double eps=1e-12) const
void print () const

Static Public Member Functions

static Matrix4 identity ()
static Matrix4 translation (const Point3 &p)
static Matrix4 translation (const Vector3 &v)
static Matrix4 rotation (const Vector3 &u, const Vector3 &v, const Vector3 &w)
static Matrix4 rotation (const Vector3 &axis, double angle)
static Matrix4 xrotation (double angle)
static Matrix4 yrotation (double angle)
static Matrix4 zrotation (double angle)
static Matrix4 scaling (const Vector3 &s)
static Matrix4 scaling (double x, double y, double z)
static Matrix4 scaling (double scale)

Private Attributes

double mat [16]

Detailed Description

Definition at line 695 of file vecmath.h.


Constructor & Destructor Documentation

EMAN::Matrix4::Matrix4 ( ) [inline]

Definition at line 697 of file vecmath.h.

References index(), and mat.

Referenced by identity(), rotation(), scaling(), translation(), xrotation(), yrotation(), and zrotation().

                      {
                for ( int i = 0; i < 4; i++ )
                    for ( int j = 0; j < 4; j++ )
                        mat[ index(i,j) ] = (i == j) ? 1.0 : 0.0;
            }
EMAN::Matrix4::Matrix4 ( const Vector4 row0,
const Vector4 row1,
const Vector4 row2,
const Vector4 row3 
) [inline]

Definition at line 703 of file vecmath.h.

References index(), and mat.

                                                                                                        {
                for ( int i = 0; i < 4; i++ ) {
                    mat[ index( 0, i ) ] = row0[i];
                    mat[ index( 1, i ) ] = row1[i];
                    mat[ index( 2, i ) ] = row2[i];
                    mat[ index( 3, i ) ] = row3[i];
                }
            }
EMAN::Matrix4::Matrix4 ( const Vector3 row0,
const Vector3 row1,
const Vector3 row2 
) [inline]

Definition at line 712 of file vecmath.h.

References index(), and mat.

                                                                                    {
                for ( int i = 0; i < 3; i++ ) {
                    mat[ index( 0, i ) ] = row0[i];
                    mat[ index( 1, i ) ] = row1[i];
                    mat[ index( 2, i ) ] = row2[i];
                    mat[ index(i,3) ] = 0.0;
                    mat[ index(3,i) ] = 0.0;
                }
                mat[ index(3,3) ] = 1.0;
            }
EMAN::Matrix4::Matrix4 ( const Matrix4 m) [inline]

Definition at line 723 of file vecmath.h.

                                      {
                (*this) = m;
            }

Member Function Documentation

bool EMAN::Matrix4::approxEqual ( const Matrix4 m,
double  eps = 1e-12 
) const [inline]

Definition at line 829 of file vecmath.h.

References EMAN::isZero(), and mat.

                                                                           {
                for ( int i = 0; i < 16; i++ )
                    if ( isZero( mat[i] - m.mat[i], eps ) )
                        return false;
                return true;
            }
Vector4 EMAN::Matrix4::column ( int  c) const [inline]

Definition at line 742 of file vecmath.h.

References index(), and mat.

                                        {
                return Vector4( mat[index(0,c)], mat[index(1,c)], mat[index(2,c)], mat[index(3,c)] );
            }
static Matrix4 EMAN::Matrix4::identity ( ) [inline, static]

Definition at line 843 of file vecmath.h.

References Matrix4().

Referenced by inverse().

                                      {
                return Matrix4(Vector4(1, 0, 0, 0),
                               Vector4(0, 1, 0, 0),
                               Vector4(0, 0, 1, 0),
                               Vector4(0, 0, 0, 1));
            }
int EMAN::Matrix4::index ( int  row,
int  col 
) const [inline]

Definition at line 733 of file vecmath.h.

References Assert, and row().

Referenced by column(), Matrix4(), operator()(), and row().

{ Assert( row >= 0 && row < 4 ); Assert( col >= 0 && col < 4 ); return col * 4 + row; }
Matrix4 EMAN::Matrix4::inverse ( ) const [inline]

Definition at line 960 of file vecmath.h.

References Assert, b, identity(), and EMAN::isZero().

                                              {
            // Compute inverse using Gauss-Jordan elimination; caller is responsible
            // for ensuring that the matrix isn't singular.
            Matrix4 a(*this);
            Matrix4 b(Matrix4::identity());
            int i, j;
            int p;
        
            for (j = 0; j < 4; j++) {
                p = j;
                for (i = j + 1; i < 4; i++) {
                    if (fabs(a(i,j)) > fabs(a(p,j)))
                        p = i;
                }
                // Swap rows p and j
                if ( p != j ) {
                    for ( i = 0; i < 4; i++ ) {
                        const double ta = a(p,i);
                        a(p,i) = a(j,i);
                        a(j,i) = ta;
        
                        const double tb = b(p,i);
                        b(p,i) = b(j,i);
                        b(j,i) = tb;
                    }
                }
        
                const double s = a(j,j);  // if s == 0, the matrix is singular
                Assert( isZero( s ) == false );
                for ( i = 0; i < 4; i++ ) {
                    a(j,i) *= (1.0 / s);
                    b(j,i) *= (1.0 / s);
                }
                // Eliminate off-diagonal elements
                for (i = 0; i < 4; i++) {
                    if (i != j) {
                        for ( int k = 0; k < 4; k++ ) {
                            b(i,k) -= a(i,j) * b(j,k);
                            a(i,k) -= a(i,j) * a(j,k);
                        }
                    }
                }
            }
            return b;
        }
double& EMAN::Matrix4::operator() ( int  row,
int  col 
) [inline]

Definition at line 736 of file vecmath.h.

References index(), and mat.

{ return mat[ index(row,col) ]; }
const double& EMAN::Matrix4::operator() ( int  row,
int  col 
) const [inline]

Definition at line 735 of file vecmath.h.

References index(), and mat.

{ return mat[ index(row,col) ]; }
Matrix4 EMAN::Matrix4::operator* ( const Matrix4 b) const [inline]

Definition at line 808 of file vecmath.h.

References b.

                                                            {
                Matrix4 matRet;
                for ( int i = 0; i < 4; i++ ) {
                    for ( int j = 0; j < 4; j++ ) {
                        matRet(i,j) = 0.0;
                        for ( int k = 0; k < 4; k++ )
                            matRet(i,j) += (*this)(i,k) * b(k,j);
                    }
                }
                return matRet;
            }
Vector3 EMAN::Matrix4::operator* ( const Vector3 v) const [inline]

Definition at line 786 of file vecmath.h.

                                                      {
                return Vector3((*this)(0,0) * v[0] + (*this)(0,1) * v[1] + (*this)(0,2) * v[2],
                               (*this)(1,0) * v[0] + (*this)(1,1) * v[1] + (*this)(1,2) * v[2],
                               (*this)(2,0) * v[0] + (*this)(2,1) * v[1] + (*this)(2,2) * v[2]);
            }
Point3 EMAN::Matrix4::operator* ( const Point3 p) const [inline]

Definition at line 792 of file vecmath.h.

References Assert, and EMAN::isZero().

                                                     {
                const Point3 pt((*this)(0,0) * p[0] + (*this)(0,1) * p[1] + (*this)(0,2) * p[2] + (*this)(0,3),
                                (*this)(1,0) * p[0] + (*this)(1,1) * p[1] + (*this)(1,2) * p[2] + (*this)(1,3),
                                (*this)(2,0) * p[0] + (*this)(2,1) * p[1] + (*this)(2,2) * p[2] + (*this)(2,3));
                const double w = (*this)(3,0) * p[0] + (*this)(3,1) * p[1] + (*this)(3,2) * p[2] + (*this)(3,3);
                Assert( isZero( w ) == false );
                return Point3( pt[0] / w, pt[1] / w, pt[2] / w );
            }
Vector4 EMAN::Matrix4::operator* ( const Vector4 v) const [inline]

Definition at line 801 of file vecmath.h.

                                                      {
                return Vector4((*this)(0,0) * v[0] + (*this)(0,1) * v[1] + (*this)(0,2) * v[2] + (*this)(0,3) * v[3],
                               (*this)(1,0) * v[0] + (*this)(1,1) * v[1] + (*this)(1,2) * v[2] + (*this)(1,3) * v[3],
                               (*this)(2,0) * v[0] + (*this)(2,1) * v[1] + (*this)(2,2) * v[2] + (*this)(2,3) * v[3],
                               (*this)(3,0) * v[0] + (*this)(3,1) * v[1] + (*this)(3,2) * v[2] + (*this)(3,3) * v[3]);
            }
Matrix4& EMAN::Matrix4::operator*= ( const Matrix4 m) [inline]

Definition at line 746 of file vecmath.h.

                                                       {
                const Matrix4 matRet = (*this) * m;
                (*this) = matRet;
                return *this;
            }
Matrix4 EMAN::Matrix4::operator+ ( const Matrix4 m) const [inline]

Definition at line 772 of file vecmath.h.

References mat.

                                                       {
                Matrix4 matRet;
                for ( int i = 0; i < 16; i++ )
                    matRet.mat[i] = mat[i] + m.mat[i];
                return matRet;
            }
Matrix4& EMAN::Matrix4::operator+= ( const Matrix4 m) [inline]

Definition at line 752 of file vecmath.h.

                                                       {
                const Matrix4 matRet = (*this) + m;
                (*this) = matRet;
                return *this;
            }
Matrix4 EMAN::Matrix4::operator- ( const Matrix4 m) const [inline]

Definition at line 779 of file vecmath.h.

References mat.

                                                       {
                Matrix4 matRet;
                for ( int i = 0; i < 16; i++ )
                    matRet.mat[i] = mat[i] - m.mat[i];
                return matRet;
            }
Matrix4& EMAN::Matrix4::operator-= ( const Matrix4 m) [inline]

Definition at line 758 of file vecmath.h.

                                                       {
                const Matrix4 matRet = (*this) - m;
                (*this) = matRet;
                return *this;
            }
Matrix4& EMAN::Matrix4::operator= ( const Matrix4 m) [inline]

Definition at line 727 of file vecmath.h.

References mat.

                                                 {
                memcpy( &mat[0], &m.mat[0], sizeof(double) * 16 );
                return *this;
            }
bool EMAN::Matrix4::operator== ( const Matrix4 m) const [inline]

Definition at line 822 of file vecmath.h.

References mat.

                                                      {
                for ( int i = 0; i < 16; i++ )
                    if ( mat[i] != m.mat[i] )
                        return false;
                return true;
            }
void EMAN::Matrix4::print ( ) const [inline]

Definition at line 836 of file vecmath.h.

                               {
                std::cout << "( " << (*this)(0,0) << ", " << (*this)(0,1) << ", " << (*this)(0,2) << ", " << (*this)(0,3) << "\n";
                std::cout << "  " << (*this)(1,0) << ", " << (*this)(1,1) << ", " << (*this)(1,2) << ", " << (*this)(1,3) << "\n";
                std::cout << "  " << (*this)(2,0) << ", " << (*this)(2,1) << ", " << (*this)(2,2) << ", " << (*this)(2,3) << "\n";
                std::cout << "  " << (*this)(3,0) << ", " << (*this)(3,1) << ", " << (*this)(3,2) << ", " << (*this)(3,3) << ")\n";
            }
static Matrix4 EMAN::Matrix4::rotation ( const Vector3 axis,
double  angle 
) [inline, static]

Definition at line 871 of file vecmath.h.

References Matrix4(), EMAN::Vector3::normalize(), and t.

                                                                       {
                Vector3 a = axis;
                a.normalize();
                const double c = cos(angle);
                const double s = sin(angle);
                const double t = 1 - c;
        
                return Matrix4(Vector4(t * a[0] * a[0] + c,
                                       t * a[0] * a[1] - s * a[2],
                                       t * a[0] * a[2] + s * a[1],
                                       0),
                               Vector4(t * a[0] * a[1] + s * a[2],
                                       t * a[1] * a[1] + c,
                                       t * a[1] * a[2] - s * a[0],
                                       0),
                               Vector4(t * a[0] * a[2] - s * a[1],
                                       t * a[1] * a[2] + s * a[0],
                                       t * a[2] * a[2] + c,
                                       0),
                               Vector4(0, 0, 0, 1));
            }
static Matrix4 EMAN::Matrix4::rotation ( const Vector3 u,
const Vector3 v,
const Vector3 w 
) [inline, static]

Definition at line 864 of file vecmath.h.

References Matrix4().

                                                                                          {
                return Matrix4(Vector4(u[0], u[1], u[2], 0),
                               Vector4(v[0], v[1], v[2], 0),
                               Vector4(w[0], w[1], w[2], 0),
                               Vector4(0  , 0  , 0  , 1));
            }
Vector4 EMAN::Matrix4::row ( int  r) const [inline]

Definition at line 738 of file vecmath.h.

References index(), and mat.

Referenced by index(), and EMAN::operator<<().

                                     {
                return Vector4( mat[index(r,0)], mat[index(r,1)], mat[index(r,2)], mat[index(r,3)] );
            }
static Matrix4 EMAN::Matrix4::scaling ( double  x,
double  y,
double  z 
) [inline, static]

Definition at line 930 of file vecmath.h.

References Matrix4().

                                                                  {
                return Matrix4(Vector4(x, 0, 0, 0),
                               Vector4(0, y, 0, 0),
                               Vector4(0, 0, z, 0),
                               Vector4(0, 0, 0, 1));
            }
static Matrix4 EMAN::Matrix4::scaling ( const Vector3 s) [inline, static]

Definition at line 923 of file vecmath.h.

References Matrix4().

Referenced by scaling().

                                                     {
                return Matrix4(Vector4(s[0], 0  , 0  , 0),
                               Vector4(0  , s[1], 0  , 0),
                               Vector4(0  , 0  , s[2], 0),
                               Vector4(0  , 0  , 0  , 1));
            }
static Matrix4 EMAN::Matrix4::scaling ( double  scale) [inline, static]

Definition at line 937 of file vecmath.h.

References scaling().

                                                 {
                return scaling(Vector3(scale, scale, scale));
            }
static Matrix4 EMAN::Matrix4::translation ( const Vector3 v) [inline, static]

Definition at line 857 of file vecmath.h.

References Matrix4().

                                                         {
                return Matrix4(Vector4(1, 0, 0, v[0]),
                               Vector4(0, 1, 0, v[1]),
                               Vector4(0, 0, 1, v[2]),
                               Vector4(0, 0, 0, 1));
            }
static Matrix4 EMAN::Matrix4::translation ( const Point3 p) [inline, static]

Definition at line 850 of file vecmath.h.

References Matrix4().

                                                        {
                return Matrix4(Vector4(1, 0, 0, p[0]),
                               Vector4(0, 1, 0, p[1]),
                               Vector4(0, 0, 1, p[2]),
                               Vector4(0, 0, 0, 1));
            }
Matrix4 EMAN::Matrix4::transpose ( ) const [inline]

Definition at line 764 of file vecmath.h.

                                      {
                Matrix4 matRet;
                for ( int i = 0; i < 4; i++ )
                    for ( int j = 0; j < 4; j++ )
                        matRet(i,j) = (*this)(j,i);
                return matRet;
            }
static Matrix4 EMAN::Matrix4::xrotation ( double  angle) [inline, static]

Definition at line 893 of file vecmath.h.

References Matrix4().

                                                   {
                const double c = cos(angle);
                const double s = sin(angle);
        
                return Matrix4(Vector4(1, 0,  0, 0),
                               Vector4(0, c, -s, 0),
                               Vector4(0, s,  c, 0),
                               Vector4(0, 0,  0, 1));
            }
static Matrix4 EMAN::Matrix4::yrotation ( double  angle) [inline, static]

Definition at line 903 of file vecmath.h.

References Matrix4().

                                                   {
                double c = cos(angle);
                double s = sin(angle);
        
                return Matrix4(Vector4( c, 0, s, 0),
                               Vector4( 0, 1, 0, 0),
                               Vector4(-s, 0, c, 0),
                               Vector4( 0, 0, 0, 1));
            }
static Matrix4 EMAN::Matrix4::zrotation ( double  angle) [inline, static]

Definition at line 913 of file vecmath.h.

References Matrix4().

                                                   {
                const double c = cos(angle);
                const double s = sin(angle);
        
                return Matrix4(Vector4(c, -s, 0, 0),
                               Vector4(s,  c, 0, 0),
                               Vector4(0,  0, 1, 0),
                               Vector4(0,  0, 0, 1));
            }

Member Data Documentation

double EMAN::Matrix4::mat[16] [private]

The documentation for this class was generated from the following file: