matrix3x3.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 matrix3x3.cpp - Handle 3D Rotation matrix.
00003  
00004 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
00005 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
00006 Some portions Copyright (C) 2006 by Benoit Jacob
00007 
00008 This file is part of the Open Babel project.
00009 For more information, see <http://openbabel.sourceforge.net/>
00010  
00011 This program is free software; you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation version 2 of the License.
00014  
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 ***********************************************************************/
00020 
00021 #ifndef OB_MATRIX3x3_H
00022 #define OB_MATRIX3x3_H
00023 
00024 #include <ostream>
00025 
00026 #include <openbabel/math/vector3.h> // includes rand.h, which includes <math.h>
00027 #include <openbabel/oberror.h>
00028 
00029 #ifndef RAD_TO_DEG
00030 #define RAD_TO_DEG (180.0/M_PI)
00031 #endif
00032 
00033 #ifndef DEG_TO_RAD
00034 #define DEG_TO_RAD (M_PI/180.0)
00035 #endif
00036 
00037 namespace OpenBabel
00038 {
00039   class OBRandom; // class introduction in rand.h
00040 
00041   // class introduction in matrix3x3.cpp
00042   class OBAPI matrix3x3
00043     {
00045 
00047       double ele[3][3];
00048 
00049     public:
00051       matrix3x3(void)
00052         {
00053           ele[0][0] = 0.0;
00054           ele[0][1] = 0.0;
00055           ele[0][2] = 0.0;
00056           ele[1][0] = 0.0;
00057           ele[1][1] = 0.0;
00058           ele[1][2] = 0.0;
00059           ele[2][0] = 0.0;
00060           ele[2][1] = 0.0;
00061           ele[2][2] = 0.0;
00062         }
00063 
00065       matrix3x3(double s)
00066         {
00067           ele[0][0] = s;
00068           ele[0][1] = 0.0;
00069           ele[0][2] = 0.0;
00070           ele[1][0] = 0.0;
00071           ele[1][1] = s;
00072           ele[1][2] = 0.0;
00073           ele[2][0] = 0.0;
00074           ele[2][1] = 0.0;
00075           ele[2][2] = s;
00076         }
00077 
00079       matrix3x3(vector3 row1,vector3 row2,vector3 row3)
00080         {
00081           ele[0][0] = row1.x();
00082           ele[0][1] = row1.y();
00083           ele[0][2] = row1.z();
00084           ele[1][0] = row2.x();
00085           ele[1][1] = row2.y();
00086           ele[1][2] = row2.z();
00087           ele[2][0] = row3.x();
00088           ele[2][1] = row3.y();
00089           ele[2][2] = row3.z();
00090         }
00091 
00093 
00094       matrix3x3(double d[3][3])
00095         {
00096           ele[0][0] = d[0][0];
00097           ele[0][1] = d[0][1];
00098           ele[0][2] = d[0][2];
00099           ele[1][0] = d[1][0];
00100           ele[1][1] = d[1][1];
00101           ele[1][2] = d[1][2];
00102           ele[2][0] = d[2][0];
00103           ele[2][1] = d[2][1];
00104           ele[2][2] = d[2][2];
00105         }
00106 
00108 
00111       void GetArray(double *m)
00112         {
00113           m[0] = ele[0][0];
00114           m[1] = ele[0][1];
00115           m[2] = ele[0][2];
00116           m[3] = ele[1][0];
00117           m[4] = ele[1][1];
00118           m[5] = ele[1][2];
00119           m[6] = ele[2][0];
00120           m[7] = ele[2][1];
00121           m[8] = ele[2][2];
00122         }
00123 
00126       const double & operator() (int row, int column ) const
00127       {
00128         return ele[row][column];
00129       }
00130 
00133       double & operator() (int row, int column )
00134       {
00135         return ele[row][column];
00136       }
00137 
00139       matrix3x3 inverse(void) const
00140 #ifdef OB_OLD_MATH_CHECKS
00141   throw(OBError)
00142 #endif
00143       ;
00144 
00146       matrix3x3 transpose(void) const;
00147 
00149       void randomRotation(OBRandom &rnd);
00150 
00152       double determinant() const;
00153 
00155       bool isSymmetric(void) const;
00156 
00158 
00172       bool isOrthogonal(void) const
00173         {
00174           return (*this * transpose()).isUnitMatrix();
00175         };
00176 
00178       bool isDiagonal(void) const;
00179 
00181       bool isUnitMatrix(void) const;
00182 
00184 
00187       double Get(int row,int column) const
00188         {
00189 #ifdef OB_OLD_MATH_CHECKS
00190           if (row >= 0 && row <= 2 && column >= 0 && column <= 2)
00191             return(ele[row][column]);
00192           else
00193             return 0.0f;
00194 #else
00195           return(ele[row][column]);
00196 #endif
00197         }
00198 
00200 
00203       void Set(int row,int column, double v)
00204         {
00205 #ifdef OB_OLD_MATH_CHECKS
00206           if (row >= 0 && row <= 2 && column >= 0 && column <= 2)
00207             ele[row][column]= v;
00208 #else
00209           ele[row][column]= v;
00210 #endif
00211         }
00212 
00214 
00216       void SetColumn(int column, const vector3 &v)
00217 #ifdef OB_OLD_MATH_CHECKS
00218   throw(OBError)
00219 #endif
00220       ;
00221 
00223 
00225       void SetRow(int row, const vector3 &v)
00226 #ifdef OB_OLD_MATH_CHECKS
00227   throw(OBError)
00228 #endif
00229       ;
00230 
00232 
00234       vector3 GetColumn(unsigned int col) const
00235 #ifdef OB_OLD_MATH_CHECKS
00236   throw(OBError)
00237 #endif
00238       ;
00239 
00241 
00243       vector3 GetRow(unsigned int row) const
00244 #ifdef OB_OLD_MATH_CHECKS
00245   throw(OBError)
00246 #endif
00247       ;
00248 
00250       matrix3x3 &operator*=(const double &c)
00251       {
00252         for( int i = 0; i < 3; i++ )
00253           for( int j = 0; j < 3; j++ )
00254             ele[i][j] *= c;
00255         return *this;
00256       }
00257 
00259       matrix3x3 &operator/=(const double &c)
00260       {
00261         return( (*this) *= ( 1.0 / c ) );
00262       }
00263 
00266       void SetupRotMat(double x, double y, double z);
00267 
00269       void PlaneReflection(const vector3 &norm);
00270 
00273       void RotAboutAxisByAngle(const vector3 &axis, const double angle);
00274 
00280       void FillOrth(double alpha, double beta, double gamma,
00281                     double a, double b, double c);
00282 
00284       matrix3x3 findEigenvectorsIfSymmetric(vector3 &eigenvals) const
00285 #ifdef OB_OLD_MATH_CHECKS
00286   throw(OBError)
00287 #endif
00288       ;
00289 
00291       friend OBAPI vector3 operator *(const matrix3x3 &,const vector3 &);
00292 
00294       friend OBAPI matrix3x3 operator *(const matrix3x3 &,const matrix3x3 &);
00295 
00297       friend OBAPI std::ostream& ".">operator<< ( std::ostream&, const matrix3x3 & ) ;
00298 
00300       static void jacobi(unsigned int n, double *a, double *d, double *v);
00301     };
00302 
00303 #ifndef SWIG
00304   OBAPI vector3 center_coords(double*,int);
00305 #endif
00306 }
00307 
00308 #endif // OB_MATRIX3x3_H
00309