obutil.h Source File

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

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.org/>
    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
    00077
    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 __KCC
    00157   extern "C" {
    00158   OBAPI void  rotate_coords(double*,double m[3][3],unsigned);
    00159   OBAPI double calc_rms(double*,double*,unsigned int);
    00160   }
    00161 #else
    00162   OBAPI void  rotate_coords(double*,double m[3][3],unsigned);
    00163   OBAPI double calc_rms(double*,double*,unsigned int);
    00164 #endif
    00165
    00166 #ifndef SWIG
    00167
    00168
    00169   // Documentation in obutil.cpp
    00170   OBAPI void ToUpper(std::string&);
    00171   OBAPI void ToUpper(char*);
    00172   OBAPI void ToLower(std::string&);
    00173   OBAPI void ToLower(char *);
    00174   OBAPI void InvertCase(std::string&, int);
    00175   OBAPI void InvertCase(char *);
    00177   OBAPI void CleanAtomType(char*);
    00179
    00182   OBAPI bool OBCompareInt(const int &a,const int &b);
    00185   OBAPI bool OBCompareUnsigned(const unsigned int &a,const unsigned int &b);
    00192   OBAPI bool IsNear(const double &, const double &, const double epsilon=2e-6);
    00199   OBAPI bool IsNearZero(const double &, const double epsilon=2e-6);
    00200   OBAPI bool IsNan(const double &);
    00207   OBAPI inline bool IsNegligible(const double & a, const double & b,
    00208                                  const double precision = 1e-11)
    00209   {
    00210     return( fabs(a) <= precision * fabs(b) );
    00211   }
    00229   OBAPI inline bool IsApprox(const double & a, const double & b,
    00230                              const double precision = 1e-11)
    00231   {
    00232     return( fabs(a - b) <= precision * std::min<const double>( fabs(a), fabs(b) ) );
    00233   }
    00235   OBAPI inline bool IsApprox_pos(const double &a, const double &b,
    00236                                  const double precision = 1e-11)
    00237   {
    00238     return( fabs(a - b) <= precision * std::min<const double>( a, b ) );
    00239   }
    00243   OBAPI bool CanBeSquared(const double &);
    00244
    00245   OBAPI bool SafeOpen(std::ifstream &fs, const char *filename);
    00246   OBAPI bool SafeOpen(std::ofstream &fs, const char *filename);
    00247 #endif
    00248   // (end part to be skipped by SWIG)
    00249
    00250   //******************triple template*************************
    00253   template <class T1, class T2, class T3>
    00254     struct triple
    00255     {
    00256       //type names for the values
    00257       typedef T1 first_type;
    00258       typedef T2 second_type;
    00259       typedef T3 third_type;
    00260
    00261       //member
    00262       T1 first;
    00263       T2 second;
    00264       T3 third;
    00265
    00269     triple():
    00270       first(T1()),second(T2()),third(T3())
    00271       {}
    00272
    00274     triple(const T1 &a, const T2 &b, const T3 &c):
    00275       first(a), second(b), third(c)
    00276       {}
    00277
    00279       template<class U, class V, class W>
    00280         triple(const triple<U,V,W> &t):
    00281         first(t.first), second(t.second), third(t.third)
    00282       {}
    00283
    00284     };
    00285
    00286   //**************quad template********************
    00289   template <class T1, class T2, class T3, class T4>
    00290     struct quad
    00291     {
    00292       //type names for the values
    00293       typedef T1 first_type;
    00294       typedef T2 second_type;
    00295       typedef T3 third_type;
    00296       typedef T4 fourth_type;
    00297
    00298       //member
    00299       T1 first;
    00300       T2 second;
    00301       T3 third;
    00302       T4 fourth;
    00303
    00307     quad():
    00308       first(T1()),second(T2()),third(T3()),fourth(T4())
    00309       {}
    00310
    00312     quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d):
    00313       first(a), second(b), third(c), fourth(d)
    00314       {}
    00315
    00317       template<class U, class V, class W, class X>
    00318         quad(const quad<U,V,W,X> &q):
    00319         first(q.first), second(q.second), third(q.third), fourth(q.fourth)
    00320       {}
    00321
    00322     };
    00323
    00324 } // end namespace OpenBabel
    00325
    00326 #endif // OBUTIL_H
    00327

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines