Matrix.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2005 Imetric 3D GmbH                                    *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #ifndef BASE_MATRIX_H
00025 #define BASE_MATRIX_H
00026 
00027 #include <cassert>
00028 #include <cmath>
00029 #include <cstdio>
00030 #include <string>
00031 
00032 #include "Vector3D.h"
00033 #include <float.h>
00034 
00035 namespace Base {
00036 
00040 class BaseExport Matrix4D
00041 {
00042   typedef float_traits<double> traits_type;
00043 
00044 public:
00046   Matrix4D(void);
00048   Matrix4D (float a11, float a12, float a13, float a14, 
00049             float a21, float a22, float a23, float a24,
00050             float a31, float a32, float a33, float a34,
00051             float a41, float a42, float a43, float a44 );
00052   Matrix4D (double a11, double a12, double a13, double a14, 
00053             double a21, double a22, double a23, double a24,
00054             double a31, double a32, double a33, double a34,
00055             double a41, double a42, double a43, double a44 );
00057   Matrix4D (const Matrix4D& rclMtrx);
00059   Matrix4D (const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
00060   Matrix4D (const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
00062   ~Matrix4D () {};
00063 
00066 
00067   inline Matrix4D  operator +  (const Matrix4D& rclMtrx) const;
00068   inline Matrix4D& operator += (const Matrix4D& rclMtrx);
00070   inline Matrix4D  operator -  (const Matrix4D& rclMtrx) const;
00071   inline Matrix4D& operator -= (const Matrix4D& rclMtrx);
00073   inline Matrix4D& operator *= (const Matrix4D& rclMtrx);
00075   inline Matrix4D& operator =  (const Matrix4D& rclMtrx);
00077   inline Matrix4D  operator *  (const Matrix4D& rclMtrx) const;
00079   inline Vector3f  operator *  (const Vector3f& rclVct) const;
00080   inline Vector3d  operator *  (const Vector3d& rclVct) const;
00082   inline bool      operator != (const Matrix4D& rclMtrx) const;
00084   inline bool      operator == (const Matrix4D& rclMtrx) const;
00086   inline double*   operator [] (unsigned short usNdx);
00088   inline const double*    operator[] (unsigned short usNdx) const;
00090   double determinant() const;
00092 
00094   void getGLMatrix  (double dMtrx[16]) const;
00096   void setGLMatrix  (const double dMtrx[16]);
00097 
00098   unsigned long getMemSpace (void);
00099 
00102 
00103   void setToUnity        (void);
00105   void move         (float x, float y, float z)
00106   { move(Vector3f(x,y,z)); }
00108   void move         (const Vector3f& rclVct);
00109   void move         (const Vector3d& rclVct);
00111   void scale        (float x, float y, float z)
00112   { scale(Vector3f(x,y,z)); }
00114   void scale        (const Vector3f& rclVct);
00115   void scale        (const Vector3d& rclVct);
00117   void rotX         (double fAngle);
00119   void rotY         (double fAngle);
00121   void rotZ         (double fAngle);
00123   void rotLine      (const Vector3f& rclVct, float fAngle);
00124   void rotLine      (const Vector3d& rclVct, double fAngle);
00126   void rotLine      (const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
00127   void rotLine      (const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
00129   bool toAxisAngle (Vector3f& rclBase, Vector3f& rclDir, float& fAngle, float& fTranslation) const;
00131   void transform    (const Vector3f& rclVct, const Matrix4D& rclMtrx);
00132   void transform    (const Vector3d& rclVct, const Matrix4D& rclMtrx);
00133   void inverse      (void);
00134   void inverseGauss (void);
00135   void transpose    (void);
00137 
00138   void Print        (void) const;
00140   std::string toString(void) const;
00142   void fromString (const std::string &str);
00143   
00144 private:
00145   double  dMtrx4D[4][4];
00146 };
00147 
00148 inline Matrix4D Matrix4D::operator  +  (const Matrix4D& rclMtrx) const
00149 {
00150   Matrix4D  clMat;
00151   short     iz, is;
00152 
00153   for (iz = 0; iz < 4; iz++) {
00154     for (is = 0; is < 4; is++) {
00155       clMat.dMtrx4D[iz][is] = dMtrx4D[iz][is] + rclMtrx[iz][is];
00156     }
00157   }
00158 
00159   return clMat;
00160 }
00161 
00162 inline Matrix4D& Matrix4D::operator += (const Matrix4D& rclMtrx)
00163 {
00164   short     iz, is;
00165 
00166   for (iz = 0; iz < 4; iz++) {
00167     for (is = 0; is < 4; is++) {
00168       dMtrx4D[iz][is] += rclMtrx[iz][is];
00169     }
00170   }
00171 
00172   return *this;
00173 }
00174 
00175 inline Matrix4D Matrix4D::operator  -  (const Matrix4D& rclMtrx) const
00176 {
00177   Matrix4D  clMat;
00178   short     iz, is;
00179 
00180   for (iz = 0; iz < 4; iz++) {
00181     for (is = 0; is < 4; is++) {
00182       clMat.dMtrx4D[iz][is] = dMtrx4D[iz][is] - rclMtrx[iz][is];
00183     }
00184   }
00185 
00186   return clMat;
00187 }
00188 
00189 inline Matrix4D& Matrix4D::operator -= (const Matrix4D& rclMtrx)
00190 {
00191   short     iz, is;
00192 
00193   for (iz = 0; iz < 4; iz++) {
00194     for (is = 0; is < 4; is++) {
00195       dMtrx4D[iz][is] -= rclMtrx[iz][is];
00196     }
00197   }
00198 
00199   return *this;
00200 }
00201 
00202 inline Matrix4D& Matrix4D::operator *= (const Matrix4D& rclMtrx)
00203 {
00204   Matrix4D  clMat;
00205   short     ie, iz, is;
00206 
00207   for (iz = 0; iz < 4; iz++)
00208     for (is = 0; is < 4; is++) {
00209       clMat.dMtrx4D[iz][is] = 0;
00210       for (ie = 0; ie < 4; ie++)
00211         clMat.dMtrx4D[iz][is] += dMtrx4D[iz][ie] * 
00212                           rclMtrx.dMtrx4D[ie][is];
00213     }
00214 
00215   (*this) = clMat;
00216  
00217   return *this;
00218 }
00219 
00220 inline Matrix4D Matrix4D::operator * (const Matrix4D& rclMtrx) const
00221 {
00222   Matrix4D  clMat;
00223   short     ie, iz, is;
00224 
00225   for (iz = 0; iz < 4; iz++)
00226     for (is = 0; is < 4; is++) {
00227       clMat.dMtrx4D[iz][is] = 0;
00228       for (ie = 0; ie < 4; ie++)
00229         clMat.dMtrx4D[iz][is] += dMtrx4D[iz][ie] * 
00230                           rclMtrx.dMtrx4D[ie][is];
00231     }
00232 
00233   return clMat;
00234 }
00235 
00236 inline Matrix4D& Matrix4D::operator= (const Matrix4D& rclMtrx)
00237 {
00238   short iz, is;
00239 
00240   for (iz = 0; iz < 4; iz++) {
00241     for (is = 0; is < 4; is++) {
00242       dMtrx4D[iz][is] = rclMtrx.dMtrx4D[iz][is];
00243     }
00244   }
00245   
00246   return *this;
00247 }
00248 
00249 inline Vector3f Matrix4D::operator* (const Vector3f& rclVct) const
00250 {
00251   return Vector3f((float)(dMtrx4D[0][0]*rclVct.x + dMtrx4D[0][1]*rclVct.y +
00252                           dMtrx4D[0][2]*rclVct.z + dMtrx4D[0][3]),
00253                   (float)(dMtrx4D[1][0]*rclVct.x + dMtrx4D[1][1]*rclVct.y + 
00254                           dMtrx4D[1][2]*rclVct.z + dMtrx4D[1][3]),
00255                   (float)(dMtrx4D[2][0]*rclVct.x + dMtrx4D[2][1]*rclVct.y + 
00256                           dMtrx4D[2][2]*rclVct.z + dMtrx4D[2][3]));
00257 }
00258 
00259 inline Vector3d Matrix4D::operator* (const Vector3d& rclVct) const
00260 {
00261   return Vector3d((dMtrx4D[0][0]*rclVct.x + dMtrx4D[0][1]*rclVct.y +
00262                    dMtrx4D[0][2]*rclVct.z + dMtrx4D[0][3]),
00263                   (dMtrx4D[1][0]*rclVct.x + dMtrx4D[1][1]*rclVct.y + 
00264                    dMtrx4D[1][2]*rclVct.z + dMtrx4D[1][3]),
00265                   (dMtrx4D[2][0]*rclVct.x + dMtrx4D[2][1]*rclVct.y + 
00266                    dMtrx4D[2][2]*rclVct.z + dMtrx4D[2][3]));
00267 }
00268 
00269 inline bool Matrix4D::operator== (const Matrix4D& rclMtrx) const
00270 {
00271   short     iz, is;
00272 
00273   for (iz = 0; iz < 4; iz++)
00274     for (is = 0; is < 4; is++) 
00275       if (fabs(dMtrx4D[iz][is] - rclMtrx.dMtrx4D[iz][is]) > traits_type::epsilon())
00276         return false;
00277 
00278   return true;
00279 }
00280 
00281 inline bool Matrix4D::operator!= (const Matrix4D& rclMtrx) const
00282 {
00283   return !( (*this) == rclMtrx );
00284 }
00285 
00286 inline Vector3f& operator*= (Vector3f& rclVect,
00287                               const Matrix4D& rclMtrx)
00288 {
00289   rclVect = rclMtrx * rclVect;
00290   return rclVect;
00291 }
00292 
00293 inline double* Matrix4D::operator[] (unsigned short usNdx)
00294 {
00295   return dMtrx4D[usNdx];
00296 }
00297 
00298 inline const double* Matrix4D::operator[] (unsigned short usNdx) const
00299 {
00300   return dMtrx4D[usNdx];
00301 }
00302 
00303 } // namespace Base
00304 
00305 
00306 #endif // BASE_MATRIX_H 
00307 
00308 

Generated on Wed Nov 23 19:00:21 2011 for FreeCAD by  doxygen 1.6.1