Wm4Matrix4.h

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.1 (2006/08/19)
00016 
00017 #ifndef WM4MATRIX4_H
00018 #define WM4MATRIX4_H
00019 
00020 // Matrix operations are applied on the left.  For example, given a matrix M
00021 // and a vector V, matrix-times-vector is M*V.  That is, V is treated as a
00022 // column vector.  Some graphics APIs use V*M where V is treated as a row
00023 // vector.  In this context the "M" matrix is really a transpose of the M as
00024 // represented in Wild Magic.  Similarly, to apply two matrix operations M0
00025 // and M1, in that order, you compute M1*M0 so that the transform of a vector
00026 // is (M1*M0)*V = M1*(M0*V).  Some graphics APIs use M0*M1, but again these
00027 // matrices are the transpose of those as represented in Wild Magic.  You
00028 // must therefore be careful about how you interface the transformation code
00029 // with graphics APIS.
00030 //
00031 // For memory organization it might seem natural to chose Real[N][N] for the
00032 // matrix storage, but this can be a problem on a platform/console that
00033 // chooses to store the data in column-major rather than row-major format.
00034 // To avoid potential portability problems, the matrix is stored as Real[N*N]
00035 // and organized in row-major order.  That is, the entry of the matrix in row
00036 // r (0 <= r < N) and column c (0 <= c < N) is stored at index i = c+N*r
00037 // (0 <= i < N*N).
00038 
00039 #include "Wm4FoundationLIB.h"
00040 #include "Wm4Plane3.h"
00041 #include "Wm4Vector4.h"
00042 
00043 namespace Wm4
00044 {
00045 
00046 template <class Real>
00047 class Matrix4
00048 {
00049 public:
00050     // If bZero is true, create the zero matrix.  Otherwise, create the
00051     // identity matrix.
00052     Matrix4 (bool bZero = true);
00053 
00054     // copy constructor
00055     Matrix4 (const Matrix4& rkM);
00056 
00057     // input Mrc is in row r, column c.
00058     Matrix4 (Real fM00, Real fM01, Real fM02, Real fM03,
00059              Real fM10, Real fM11, Real fM12, Real fM13,
00060              Real fM20, Real fM21, Real fM22, Real fM23,
00061              Real fM30, Real fM31, Real fM32, Real fM33);
00062 
00063     // Create a matrix from an array of numbers.  The input array is
00064     // interpreted based on the Boolean input as
00065     //   true:  entry[0..15]={m00,m01,m02,m03,m10,m11,m12,m13,m20,m21,m22,
00066     //                        m23,m30,m31,m32,m33} [row major]
00067     //   false: entry[0..15]={m00,m10,m20,m30,m01,m11,m21,m31,m02,m12,m22,
00068     //                        m32,m03,m13,m23,m33} [col major]
00069     Matrix4 (const Real afEntry[16], bool bRowMajor);
00070 
00071     void MakeZero ();
00072     void MakeIdentity ();
00073 
00074     // member access
00075     operator const Real* () const;
00076     operator Real* ();
00077     const Real* operator[] (int iRow) const;
00078     Real* operator[] (int iRow);
00079     Real operator() (int iRow, int iCol) const;
00080     Real& operator() (int iRow, int iCol);
00081     void SetRow (int iRow, const Vector4<Real>& rkV);
00082     Vector4<Real> GetRow (int iRow) const;
00083     void SetColumn (int iCol, const Vector4<Real>& rkV);
00084     Vector4<Real> GetColumn (int iCol) const;
00085     void GetColumnMajor (Real* afCMajor) const;
00086 
00087     // assignment
00088     Matrix4& operator= (const Matrix4& rkM);
00089 
00090     // comparison
00091     bool operator== (const Matrix4& rkM) const;
00092     bool operator!= (const Matrix4& rkM) const;
00093     bool operator<  (const Matrix4& rkM) const;
00094     bool operator<= (const Matrix4& rkM) const;
00095     bool operator>  (const Matrix4& rkM) const;
00096     bool operator>= (const Matrix4& rkM) const;
00097 
00098     // arithmetic operations
00099     Matrix4 operator+ (const Matrix4& rkM) const;
00100     Matrix4 operator- (const Matrix4& rkM) const;
00101     Matrix4 operator* (const Matrix4& rkM) const;
00102     Matrix4 operator* (Real fScalar) const;
00103     Matrix4 operator/ (Real fScalar) const;
00104     Matrix4 operator- () const;
00105 
00106     // arithmetic updates
00107     Matrix4& operator+= (const Matrix4& rkM);
00108     Matrix4& operator-= (const Matrix4& rkM);
00109     Matrix4& operator*= (Real fScalar);
00110     Matrix4& operator/= (Real fScalar);
00111 
00112     // matrix times vector
00113     Vector4<Real> operator* (const Vector4<Real>& rkV) const;  // M * v
00114 
00115     // other operations
00116     Matrix4 Transpose () const;  // M^T
00117     Matrix4 TransposeTimes (const Matrix4& rkM) const;  // this^T * M
00118     Matrix4 TimesTranspose (const Matrix4& rkM) const;  // this * M^T
00119     Matrix4 Inverse () const;
00120     Matrix4 Adjoint () const;
00121     Real Determinant () const;
00122     Real QForm (const Vector4<Real>& rkU,
00123         const Vector4<Real>& rkV) const;  // u^T*M*v
00124 
00125     // projection matrices onto a specified plane
00126     void MakeObliqueProjection (const Vector3<Real>& rkNormal,
00127         const Vector3<Real>& rkPoint, const Vector3<Real>& rkDirection);
00128     void MakePerspectiveProjection (const Vector3<Real>& rkNormal,
00129         const Vector3<Real>& rkPoint, const Vector3<Real>& rkEye);
00130 
00131     // reflection matrix through a specified plane
00132     void MakeReflection (const Vector3<Real>& rkNormal,
00133         const Vector3<Real>& rkPoint);
00134 
00135     // special matrices
00136     WM4_FOUNDATION_ITEM static const Matrix4 ZERO;
00137     WM4_FOUNDATION_ITEM static const Matrix4 IDENTITY;
00138 
00139 private:
00140     // support for comparisons
00141     int CompareArrays (const Matrix4& rkM) const;
00142 
00143     Real m_afEntry[16];
00144 };
00145 
00146 // c * M
00147 template <class Real>
00148 Matrix4<Real> operator* (Real fScalar, const Matrix4<Real>& rkM);
00149 
00150 // v^T * M
00151 template <class Real>
00152 Vector4<Real> operator* (const Vector4<Real>& rkV, const Matrix4<Real>& rkM);
00153 
00154 } //namespace Wm4
00155 
00156 #include "Wm4Matrix4.inl"
00157 
00158 namespace Wm4
00159 {
00160 typedef Matrix4<float> Matrix4f;
00161 typedef Matrix4<double> Matrix4d;
00162 }
00163 
00164 #endif

Generated on Wed Nov 23 19:01:05 2011 for FreeCAD by  doxygen 1.6.1