Open Babel  3.0
obutil.h
Go to the documentation of this file.
1 /**********************************************************************
2 obutil.h - Various utility methods.
3 
4 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
6 
7 This file is part of the Open Babel project.
8 For more information, see <http://openbabel.org/>
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation version 2 of the License.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 ***********************************************************************/
19 
20 #ifndef OB_UTIL_H
21 #define OB_UTIL_H
22 
23 #include <openbabel/babelconfig.h>
24 
25 #include <string>
26 #include <iosfwd>
27 
28 #if TIME_WITH_SYS_TIME
29 #include <sys/time.h>
30 #include <time.h>
31 #else
32 #if HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #else
35 #include <time.h>
36 #endif
37 #endif
38 
39 #include <math.h>
40 
41 #ifndef M_PI
42 #define M_PI 3.14159265358979323846
43 #endif
44 
45 namespace OpenBabel
46 {
47 
48  // class introduction in obutil.cpp
49  class OBAPI OBStopwatch
50  {
51 #if HAVE_CLOCK_T
52  clock_t start;
53  clock_t stop;
54 #else
55  timeval start;
56  timeval stop;
57 #endif
58 
59  public:
60 #if HAVE_CLOCK_T
61 
63  void Start()
64  {
65  start= clock();
66  }
68  double Lap()
69  {
70  stop= clock();
71  return((stop - start) / (double) CLOCKS_PER_SEC);
72  }
73 #else
74  void Start()
76  {
77  gettimeofday(&start, NULL);
78  }
80  double Lap()
81  {
82  gettimeofday(&stop, NULL);
83  return((stop.tv_sec - start.tv_sec)
84  + (stop.tv_usec - start.tv_usec)/1000000.0);
85  }
86 #endif
87 
89  double Elapsed()
90  {
91  return(Lap());
92  }
93  };
94 
95 
98  class OBAPI OBSqrtTbl
99  {
100  double _max,_incr,*_tbl;
101  public:
103  _max(0.0), _incr(0.0), _tbl(NULL)
104  { }
109  OBSqrtTbl(const double max, const double incr):
110  _max(max*max), _incr(incr), _tbl(NULL)
111  {
112  Init(max,incr);
113  }
115  {
116  if (_tbl)
117  {
118  delete [] _tbl;
119  _tbl = NULL;
120  }
121  }
124  double Sqrt(double d2) const
125  {
126  if (_tbl)
127  return((d2 < _max) ? _tbl[static_cast<int>(d2*_incr)]:sqrt(d2));
128  else
129  return 0.0;
130  }
134  void Init(double max,double incr)
135  {
136  // parameters are potentially unneeded, but let's do this until we can
137  // deprecate them
138  _max = max * max;
139  _incr = incr;
140 
141  //array size needs to be large enough to account for fp error
142  int i;
143  double r;
144  _tbl = new double [static_cast<int>((_max/_incr)+10)];
145  for (r = (_incr/2.0),i=0;r <= _max;r += _incr,++i)
146  _tbl[i] = sqrt(r);
147 
148  _incr = 1/_incr;
149  }
150  };
151 
152  //***RMS helper methods***/
153 #ifndef __KCC
154  extern "C" {
155  OBAPI void rotate_coords(double*,double m[3][3],unsigned);
156  OBAPI double calc_rms(double*,double*,unsigned int);
157  }
158 #else
159  OBAPI void rotate_coords(double*,double m[3][3],unsigned);
160  OBAPI double calc_rms(double*,double*,unsigned int);
161 #endif
162 
163 #ifndef SWIG
164 
166  // Documentation in obutil.cpp
167  OBAPI void ToUpper(std::string&);
168  OBAPI void ToUpper(char*);
169  OBAPI void ToLower(std::string&);
170  OBAPI void ToLower(char *);
171  OBAPI void InvertCase(std::string&, int);
172  OBAPI void InvertCase(char *);
174  OBAPI void CleanAtomType(char*);
176 
179  OBAPI bool OBCompareInt(const int &a,const int &b);
182  OBAPI bool OBCompareUnsigned(const unsigned int &a,const unsigned int &b);
189  OBAPI bool IsNear(const double &, const double &, const double epsilon=2e-6);
196  OBAPI bool IsNearZero(const double &, const double epsilon=2e-6);
197  OBAPI bool IsNan(const double &);
204  OBAPI inline bool IsNegligible(const double & a, const double & b,
205  const double precision = 1e-11)
206  {
207  return( fabs(a) <= precision * fabs(b) );
208  }
226  OBAPI inline bool IsApprox(const double & a, const double & b,
227  const double precision = 1e-11)
228  {
229  return( fabs(a - b) <= precision * std::min<const double>( fabs(a), fabs(b) ) );
230  }
232  OBAPI inline bool IsApprox_pos(const double &a, const double &b,
233  const double precision = 1e-11)
234  {
235  return( fabs(a - b) <= precision * std::min<const double>( a, b ) );
236  }
240  OBAPI bool CanBeSquared(const double &);
241 
242  OBAPI bool SafeOpen(std::ifstream &fs, const char *filename);
243  OBAPI bool SafeOpen(std::ofstream &fs, const char *filename);
244 #endif
245  // (end part to be skipped by SWIG)
246 
247  //******************triple template*************************
250  template <class T1, class T2, class T3>
251  struct triple
252  {
253  //type names for the values
254  typedef T1 first_type;
255  typedef T2 second_type;
256  typedef T3 third_type;
257 
258  //member
259  T1 first;
260  T2 second;
261  T3 third;
262 
267  first(T1()),second(T2()),third(T3())
268  {}
269 
271  triple(const T1 &a, const T2 &b, const T3 &c):
272  first(a), second(b), third(c)
273  {}
274 
276  template<class U, class V, class W>
278  first(t.first), second(t.second), third(t.third)
279  {}
280 
281  };
282 
283  //**************quad template********************
286  template <class T1, class T2, class T3, class T4>
287  struct quad
288  {
289  //type names for the values
290  typedef T1 first_type;
291  typedef T2 second_type;
292  typedef T3 third_type;
293  typedef T4 fourth_type;
294 
295  //member
296  T1 first;
297  T2 second;
298  T3 third;
299  T4 fourth;
300 
304  quad():
305  first(T1()),second(T2()),third(T3()),fourth(T4())
306  {}
307 
309  quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d):
310  first(a), second(b), third(c), fourth(d)
311  {}
312 
314  template<class U, class V, class W, class X>
315  quad(const quad<U,V,W,X> &q):
316  first(q.first), second(q.second), third(q.third), fourth(q.fourth)
317  {}
318 
319  };
320 
321 } // end namespace OpenBabel
322 
323 #endif // OBUTIL_H
324 
T3 third_type
Definition: obutil.h:292
bool IsNear(const double &, const double &, const double epsilon=2e-6)
Comparison for doubles: returns fabs(a - b) < epsilon.
Definition: obutil.cpp:80
T2 second_type
Definition: obutil.h:255
~OBSqrtTbl()
Definition: obutil.h:114
bool OBCompareInt(const int &a, const int &b)
Definition: obutil.cpp:68
bool IsNan(const double &)
Comparison for nan (not a number)
Definition: obutil.cpp:92
quad(const quad< U, V, W, X > &q)
copy constructor with implicit conversions
Definition: obutil.h:315
T1 first
Definition: obutil.h:259
T2 second
Definition: obutil.h:297
T4 fourth
Definition: obutil.h:299
OBSqrtTbl()
Definition: obutil.h:102
quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d)
constructor for 3 values
Definition: obutil.h:309
T2 second
Definition: obutil.h:260
A 4-element templated, based on the design of the STL pair<>
Definition: obutil.h:287
T3 third_type
Definition: obutil.h:256
T3 third
Definition: obutil.h:261
bool IsApprox(const double &a, const double &b, const double precision=1e-11)
Definition: obutil.h:226
double Sqrt(double d2) const
Fast square root calculation using a lookup table.
Definition: obutil.h:124
triple()
Definition: obutil.h:266
A 3-element templated, based on the design of the STL pair<>
Definition: obutil.h:251
void rotate_coords(double *, double m[3][3], unsigned)
Square Root lookup table - given a distance squared returns distance.
Definition: obutil.h:98
bool SafeOpen(std::ifstream &fs, const char *filename)
Definition: obutil.cpp:295
double calc_rms(double *, double *, unsigned int)
Calculate the RMS deviation between the first N coordinates of *r and *f.
Definition: obutil.cpp:173
double Elapsed()
Definition: obutil.h:89
bool IsNearZero(const double &, const double epsilon=2e-6)
Comparison for doubles: returns fabs(a) < epsilon.
Definition: obutil.cpp:86
void Init(double max, double incr)
Initialize the square root lookup table.
Definition: obutil.h:134
OBSqrtTbl(const double max, const double incr)
Create a square root table to handle up to the square root of max (e.g., if you want the square root ...
Definition: obutil.h:109
bool IsNegligible(const double &a, const double &b, const double precision=1e-11)
Definition: obutil.h:204
double Lap()
Definition: obutil.h:80
quad()
Definition: obutil.h:304
triple(const T1 &a, const T2 &b, const T3 &c)
Constructor for 3 values.
Definition: obutil.h:271
T1 first_type
Definition: obutil.h:290
T3 third
Definition: obutil.h:298
bool IsApprox_pos(const double &a, const double &b, const double precision=1e-11)
Same as IsApprox(), but only for positive numbers. Faster.
Definition: obutil.h:232
void CleanAtomType(char *)
"Clean" the supplied atom type
Definition: obutil.cpp:427
T1 first
Definition: obutil.h:296
Stopwatch class used for timing length of execution.
Definition: obutil.h:49
void ToLower(std::string &)
Shift the supplied string to lowercase.
Definition: obutil.cpp:379
void InvertCase(std::string &, int)
T4 fourth_type
Definition: obutil.h:293
T2 second_type
Definition: obutil.h:291
triple(const triple< U, V, W > &t)
Copy constructor with implicit conversions.
Definition: obutil.h:277
bool CanBeSquared(const double &)
Tests whether its argument can be squared without triggering an overflow or underflow.
Definition: obutil.cpp:99
T1 first_type
Definition: obutil.h:254
bool OBCompareUnsigned(const unsigned int &a, const unsigned int &b)
Definition: obutil.cpp:74
Global namespace for all Open Babel code.
Definition: alias.h:22
void ToUpper(std::string &)
Shift the supplied string to uppercase.
Definition: obutil.cpp:359