obutil.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 obutil.h - Various utility methods.
00003  
00004 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
00005 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
00006  
00007 This file is part of the Open Babel project.
00008 For more information, see <http://openbabel.sourceforge.net/>
00009  
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation version 2 of the License.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 ***********************************************************************/
00019 
00020 #ifndef OB_UTIL_H
00021 #define OB_UTIL_H
00022 
00023 #include <openbabel/babelconfig.h>
00024 
00025 #include <string>
00026 #include <iosfwd>
00027 
00028 #if TIME_WITH_SYS_TIME
00029 #include <sys/time.h>
00030 #include <time.h>
00031 #else
00032 #if HAVE_SYS_TIME_H
00033 #include <sys/time.h>
00034 #else
00035 #include <time.h>
00036 #endif
00037 #endif
00038 
00039 #include <math.h>
00040 
00041 #ifndef M_PI
00042 #define M_PI 3.14159265358979323846
00043 #endif
00044 
00045 // For backwards compatibility. Will be removed in the future
00046 #include <openbabel/rand.h>
00047 
00048 namespace OpenBabel
00049 {
00050 
00051   // class introduction in obutil.cpp
00052   class OBAPI OBStopwatch
00053   {
00054 #if HAVE_CLOCK_T
00055     clock_t start; 
00056     clock_t stop;  
00057 #else
00058     timeval start; 
00059     timeval stop;  
00060 #endif
00061 
00062   public:
00063 #if HAVE_CLOCK_T
00064 
00066     void  Start()
00067     {
00068       start= clock();
00069     }
00071     double Lap()
00072     {
00073       stop= clock();
00074       return((stop - start) / CLOCKS_PER_SEC);
00075     }
00076 #else
00078     void Start()
00079     {
00080       gettimeofday(&start, NULL);
00081     }
00083     double Lap()
00084     {
00085       gettimeofday(&stop, NULL);
00086       return((stop.tv_sec - start.tv_sec)
00087              + (stop.tv_usec - start.tv_usec)/1000000.0);
00088     }
00089 #endif
00090 
00092     double Elapsed()
00093     {
00094       return(Lap());
00095     }
00096   };
00097 
00098 
00101   class OBAPI OBSqrtTbl
00102   {
00103     double _max,_incr,*_tbl;
00104   public:
00105   OBSqrtTbl():
00106     _max(0.0), _incr(0.0),  _tbl(NULL)
00107       { }
00112   OBSqrtTbl(const double max, const double incr):
00113     _max(max*max), _incr(incr), _tbl(NULL)
00114       {
00115         Init(max,incr);
00116       }
00117     ~OBSqrtTbl()
00118       {
00119         if (_tbl)
00120           {
00121             delete [] _tbl;
00122             _tbl = NULL;
00123           }
00124       }
00127     double Sqrt(double d2) const
00128     {
00129       if (_tbl)
00130         return((d2 < _max) ? _tbl[static_cast<int>(d2*_incr)]:sqrt(d2));
00131       else
00132         return 0.0;
00133     }
00137     void Init(double max,double incr)
00138     {
00139       // parameters are potentially unneeded, but let's do this until we can 
00140       // deprecate them
00141       _max = max * max;
00142       _incr = incr;
00143 
00144       //array size needs to be large enough to account for fp error
00145       int i;
00146       double r;
00147       _tbl = new double [static_cast<int>((_max/_incr)+10)];
00148       for (r = (_incr/2.0),i=0;r <= _max;r += _incr,++i)
00149         _tbl[i] = sqrt(r);
00150 
00151       _incr = 1/_incr;
00152     }
00153   };
00154 
00155   //***RMS helper methods***/
00156 #ifndef SWIG
00157   OBAPI void  rotate_coords(double*,double m[3][3],int);
00158   OBAPI double calc_rms(double*,double*,unsigned int);
00159 
00161 
00162   // Documentation in obutil.cpp
00163   OBAPI void ToUpper(std::string&);
00164   OBAPI void ToUpper(char*);
00165   OBAPI void ToLower(std::string&);
00166   OBAPI void ToLower(char *);
00167   OBAPI void InvertCase(std::string&, int);
00168   OBAPI void InvertCase(char *);
00170   OBAPI void CleanAtomType(char*);
00172 
00175   OBAPI bool OBCompareInt(const int &a,const int &b);
00178   OBAPI bool OBCompareUnsigned(const unsigned int &a,const unsigned int &b);
00185   OBAPI bool IsNear(const double &, const double &, const double epsilon=2e-6);
00192   OBAPI bool IsNearZero(const double &, const double epsilon=2e-6);
00193   OBAPI bool IsNan(const double &);
00200   OBAPI inline bool IsNegligible(const double & a, const double & b,
00201                                  const double precision = 1e-11)
00202   {
00203     return( fabs(a) <= precision * fabs(b) );
00204   }
00222   OBAPI inline bool IsApprox(const double & a, const double & b,
00223                              const double precision = 1e-11)
00224   {
00225     return( fabs(a - b) <= precision * std::min( fabs(a), fabs(b) ) );
00226   }
00228   OBAPI inline bool IsApprox_pos(const double &a, const double &b,
00229                                  const double precision = 1e-11)
00230   {
00231     return( fabs(a - b) <= precision * std::min( a, b ) );
00232   }
00236   OBAPI bool CanBeSquared(const double &);
00237 
00238 #endif
00239   // (end part to be skipped by SWIG)
00240 
00241   //******************triple template*************************
00244   template <class T1, class T2, class T3>
00245     struct triple
00246     {
00247       //type names for the values
00248       typedef T1 first_type;
00249       typedef T2 second_type;
00250       typedef T3 third_type;
00251 
00252       //member
00253       T1 first;
00254       T2 second;
00255       T3 third;
00256   
00260     triple():
00261       first(T1()),second(T2()),third(T3())
00262       {}
00263 
00265     triple(const T1 &a, const T2 &b, const T3 &c):
00266       first(a), second(b), third(c)
00267       {}
00268 
00270       template<class U, class V, class W>
00271         triple(const triple<U,V,W> &t):
00272         first(t.first), second(t.second), third(t.third)
00273       {}
00274 
00275     };
00276 
00277   //**************quad template********************
00280   template <class T1, class T2, class T3, class T4>
00281     struct quad
00282     {
00283       //type names for the values
00284       typedef T1 first_type;
00285       typedef T2 second_type;
00286       typedef T3 third_type;
00287       typedef T4 fourth_type;
00288 
00289       //member
00290       T1 first;
00291       T2 second;
00292       T3 third;
00293       T4 fourth;
00294 
00298     quad():
00299       first(T1()),second(T2()),third(T3()),fourth(T4())
00300       {}
00301 
00303     quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d):
00304       first(a), second(b), third(c), fourth(d)
00305       {}
00306 
00308       template<class U, class V, class W, class X>
00309         quad(const quad<U,V,W,X> &q):
00310         first(q.first), second(q.second), third(q.third), fourth(q.fourth)
00311       {}
00312 
00313     };
00314 
00315 } // end namespace OpenBabel
00316 
00317 #endif // OBUTIL_H
00318