Open Babel  3.0
matrix3x3.h
Go to the documentation of this file.
1 /**********************************************************************
2 matrix3x3.cpp - Handle 3D Rotation matrix.
3 
4 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
6 Some portions Copyright (C) 2006 by Benoit Jacob
7 
8 This file is part of the Open Babel project.
9 For more information, see <http://openbabel.org/>
10 
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation version 2 of the License.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 ***********************************************************************/
20 
21 #ifndef OB_MATRIX3x3_H
22 #define OB_MATRIX3x3_H
23 
24 #include <ostream>
25 
26 #include <openbabel/math/vector3.h> // includes rand.h, which includes <math.h>
27 #include <openbabel/oberror.h>
28 
29 #ifndef RAD_TO_DEG
30 #define RAD_TO_DEG (180.0/M_PI)
31 #endif
32 
33 #ifndef DEG_TO_RAD
34 #define DEG_TO_RAD (M_PI/180.0)
35 #endif
36 
37 namespace OpenBabel
38 {
39  class OBRandom; // class introduction in rand.h
40 
41  // class introduction in matrix3x3.cpp
42  class OBAPI matrix3x3
43  {
45 
47  double ele[3][3];
48 
49  public:
51  matrix3x3(void)
52  {
53  // Loops are typically unrolled and/or vectorized
54  for (unsigned int i = 0; i < 3; ++i)
55  for (unsigned int j = 0; j < 3; ++j)
56  ele[i][j] = 0.0;
57  }
58 
60  matrix3x3(double s)
61  {
62  // Loops are typically unrolled and/or vectorized
63  for (unsigned int i = 0; i < 3; ++i)
64  for (unsigned int j = 0; j < 3; ++j)
65  ele[i][j] = 0.0;
66 
67  for (unsigned int i = 0; i < 3; ++i)
68  ele[i][i] = s;
69  }
70 
72  matrix3x3(vector3 row1,vector3 row2,vector3 row3)
73  {
74  ele[0][0] = row1.x();
75  ele[0][1] = row1.y();
76  ele[0][2] = row1.z();
77  ele[1][0] = row2.x();
78  ele[1][1] = row2.y();
79  ele[1][2] = row2.z();
80  ele[2][0] = row3.x();
81  ele[2][1] = row3.y();
82  ele[2][2] = row3.z();
83  }
84 
86 
87  matrix3x3(double d[3][3])
88  {
89  // Loops are typically unrolled and/or vectorized
90  for (unsigned int i = 0; i < 3; ++i)
91  for (unsigned int j = 0; j < 3; ++j)
92  ele[i][j] = d[i][j];
93 
94  // We could also potentially use memcpy here
95  }
96 
99 
101 
104  void GetArray(double *m)
105  {
106  for (unsigned int i = 0; i < 3; ++i)
107  for (unsigned int j = 0; j < 3; ++j)
108  m[3*i+j] = ele[i][j];
109  }
110 
113  const double & operator() (int row, int column ) const
114  {
115  return ele[row][column];
116  }
117 
120  double & operator() (int row, int column )
121  {
122  return ele[row][column];
123  }
124 
126  matrix3x3 inverse(void) const
127 #ifdef OB_OLD_MATH_CHECKS
128  throw(OBError)
129 #endif
130  ;
131 
133  matrix3x3 transpose(void) const;
134 
136  double determinant() const;
137 
139  bool isSymmetric(void) const;
140 
142 
156  bool isOrthogonal(void) const
157  {
158  return (*this * transpose()).isUnitMatrix();
159  };
160 
162  bool isDiagonal(void) const;
163 
165  bool isUnitMatrix(void) const;
166 
168 
171  double Get(int row,int column) const
172  {
173 #ifdef OB_OLD_MATH_CHECKS
174  if (row >= 0 && row <= 2 && column >= 0 && column <= 2)
175  return(ele[row][column]);
176  else
177  return 0.0f;
178 #else
179  return(ele[row][column]);
180 #endif
181  }
182 
184 
187  void Set(int row,int column, double v)
188  {
189 #ifdef OB_OLD_MATH_CHECKS
190  if (row >= 0 && row <= 2 && column >= 0 && column <= 2)
191  ele[row][column]= v;
192 #else
193  ele[row][column]= v;
194 #endif
195  }
196 
198 
200  void SetColumn(int column, const vector3 &v)
201 #ifdef OB_OLD_MATH_CHECKS
202  throw(OBError)
203 #endif
204  ;
205 
207 
209  void SetRow(int row, const vector3 &v)
210 #ifdef OB_OLD_MATH_CHECKS
211  throw(OBError)
212 #endif
213  ;
214 
216 
218  vector3 GetColumn(unsigned int col) const
219 #ifdef OB_OLD_MATH_CHECKS
220  throw(OBError)
221 #endif
222  ;
223 
225 
227  vector3 GetRow(unsigned int row) const
228 #ifdef OB_OLD_MATH_CHECKS
229  throw(OBError)
230 #endif
231  ;
232 
234  matrix3x3 &operator*=(const double &c)
235  {
236  for( int i = 0; i < 3; i++ )
237  for( int j = 0; j < 3; j++ )
238  ele[i][j] *= c;
239  return *this;
240  }
241 
243  matrix3x3 &operator/=(const double &c)
244  {
245  return( (*this) *= ( 1.0 / c ) );
246  }
247 
250  void SetupRotMat(double x, double y, double z);
251 
253  void PlaneReflection(const vector3 &norm);
254 
257  void RotAboutAxisByAngle(const vector3 &axis, const double angle);
258 
264  void FillOrth(double alpha, double beta, double gamma,
265  double a, double b, double c);
266 
268  matrix3x3 findEigenvectorsIfSymmetric(vector3 &eigenvals) const
269 #ifdef OB_OLD_MATH_CHECKS
270  throw(OBError)
271 #endif
272  ;
273 
275  friend OBAPI vector3 operator *(const matrix3x3 &,const vector3 &);
276 
278  friend OBAPI matrix3x3 operator *(const matrix3x3 &,const matrix3x3 &);
279 
281  friend OBAPI std::ostream& operator<< ( std::ostream&, const matrix3x3 & ) ;
282 
284  static void jacobi(unsigned int n, double *a, double *d, double *v);
285  };
286 
287 #ifndef SWIG
288  OBAPI vector3 center_coords(double*,int);
289 #endif
290 }
291 
292 #endif // OB_MATRIX3x3_H
293 
matrix3x3(double d[3][3])
Constructs a matrix from a 3x3-array of doubles.
Definition: matrix3x3.h:87
Handle 3D coordinates.
matrix3x3 & operator/=(const double &c)
Divides all entries of the matrix by a scalar c.
Definition: matrix3x3.h:243
matrix3x3(double s)
Constructs s times the unit matrix.
Definition: matrix3x3.h:60
matrix3x3 & operator*=(const double &c)
Multiplies all entries of the matrix by a scalar c.
Definition: matrix3x3.h:234
double Get(int row, int column) const
Access function.
Definition: matrix3x3.h:171
void GetArray(double *m)
Access function.
Definition: matrix3x3.h:104
Customizable error handling and logging – store a message, including the method yielding the error...
Definition: oberror.h:52
const double & x() const
Access function to get the x-coordinate of the vector.
Definition: vector3.h:237
~matrix3x3()
Destructor.
Definition: matrix3x3.h:98
matrix3x3(void)
Constructs the zero-matrix.
Definition: matrix3x3.h:51
const double & y() const
Access function to get the y-coordinate of the vector.
Definition: vector3.h:242
void Set(int row, int column, double v)
Access function.
Definition: matrix3x3.h:187
Represents a vector in 3-dimensional real space.
Definition: vector3.h:44
Represents a real 3x3 matrix.
Definition: matrix3x3.h:42
bool isOrthogonal(void) const
Checks if a matrix is orthogonal.
Definition: matrix3x3.h:156
std::ostream & operator<<(std::ostream &, const vector3 &)
Prints a representation of the vector as a row vector of the form "<0.1,1,2>".
Definition: vector3.cpp:109
vector3 center_coords(double *, int)
Handle error messages, warnings, notices, etc.
vector3 operator*(const double &c, const vector3 &v)
Multiplication with a scalar.
Definition: vector3.h:330
const double & z() const
Access function to get the z-coordinate of the vector.
Definition: vector3.h:247
matrix3x3(vector3 row1, vector3 row2, vector3 row3)
Constructs a matrix from row vectors.
Definition: matrix3x3.h:72
Global namespace for all Open Babel code.
Definition: alias.h:22