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-2005 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 "babelconfig.h"
00024 
00025 #if HAVE_IOSTREAM
00026 #include <iostream>
00027 #elif HAVE_IOSTREAM_H
00028 #include <iostream.h>
00029 #endif
00030 
00031 #if TIME_WITH_SYS_TIME
00032 #include <sys/time.h>
00033 #include <time.h>
00034 #else
00035 #if HAVE_SYS_TIME_H
00036 #include <sys/time.h>
00037 #else
00038 #include <time.h>
00039 #endif
00040 #endif
00041 
00042 #include <string>
00043 
00044 namespace OpenBabel
00045 {
00046 
00047 // class introduction in obutil.cpp
00048 class OBAPI OBStopwatch
00049 {
00050 #if HAVE_CLOCK_T
00051     clock_t start, stop;
00052 #else
00053 
00054     timeval start;
00055     timeval stop;
00056 #endif
00057 
00058 public:
00059 #if HAVE_CLOCK_T
00060 
00061     void  Start()
00062     {
00063         start= clock();
00064     }
00065     double Lap()
00066     {
00067         stop= clock();
00068         return((double)(stop - start) / CLOCKS_PER_SEC);
00069     }
00070 #else
00071     void Start()
00072     {
00073         gettimeofday(&start,(struct timezone *)NULL);
00074     }
00075     double Lap()
00076     {
00077         gettimeofday(&stop,(struct timezone *)NULL);
00078         return((stop.tv_sec - start.tv_sec)
00079                + (double)(stop.tv_usec - start.tv_usec)/1000000.0);
00080     }
00081 #endif
00082     double Elapsed()
00083     {
00084         return(Lap());
00085     }
00086 };
00087 
00088 
00090 class OBAPI OBSqrtTbl
00091 {
00092     double _max,_incr,*_tbl;
00093 public:
00094     OBSqrtTbl()
00095     {
00096         _tbl=NULL;
00097         _max = _incr = 0.0;
00098     }
00099     OBSqrtTbl(double max,double incr)
00100     {
00101         Init(max,incr);
00102     }
00103     ~OBSqrtTbl()
00104     {
00105         if (_tbl)
00106         {
00107             delete [] _tbl;
00108             _tbl = NULL;
00109         }
00110     }
00111     double Sqrt(double d2) const
00112     {
00113         if (_tbl)
00114             return((d2 < _max) ? _tbl[(int)(d2*_incr)]:sqrt(d2));
00115         else
00116             return 0.0;
00117     }
00118     void Init(double max,double incr)
00119     {
00120         int i;
00121         double r;
00122         _max = max*max;
00123         _incr = incr;
00124         //array size needs to be large enough to account for fp error
00125         _tbl = new double [(unsigned int)((_max/_incr)+10)];
00126         for (r = (_incr/2.0),i=0;r <= _max;r += _incr,i++)
00127             _tbl[i] = sqrt(r);
00128 
00129         _incr = 1/_incr;
00130     }
00131 };
00132 
00133 
00134 
00135 //******************************************
00136 //*** Stuff for random number generation ***
00137 //******************************************
00138 
00140 typedef struct
00141 {
00142     unsigned int hi;
00143     unsigned int lo;
00144 }
00145 DoubleType;
00146 
00147 OBAPI void DoubleMultiply( unsigned int,unsigned int,DoubleType*);
00148 OBAPI void DoubleAdd( DoubleType*,unsigned int);
00149 OBAPI unsigned int DoubleModulus( DoubleType*,unsigned int);
00150 
00152 class OBAPI OBRandom
00153 {
00154     DoubleType d;
00155     unsigned int m,a,c;
00156     unsigned int p;
00157     unsigned int i;
00158     unsigned int x;
00159     bool OBRandomUseSysRand;
00160 
00161 public:
00162     OBRandom(bool useSys= false);
00163     void Seed(int seed)
00164     {
00165         x = seed;
00166     }
00167     void TimeSeed();
00168     int NextInt();
00169     double NextFloat();
00170 };
00171 
00172 //***RMS helper methods***/
00173 #ifndef SWIG
00174 OBAPI void  rotate_coords(double*,double m[3][3],int);
00175 #endif
00176 OBAPI double calc_rms(double*,double*,unsigned int);
00177 
00179 
00180 // Documentation in obutil.cpp
00181 OBAPI void ToUpper(std::string&);
00182 OBAPI void ToUpper(char*);
00183 OBAPI void ToLower(std::string&);
00184 OBAPI void ToLower(char *);
00186 OBAPI void CleanAtomType(char*);
00188 
00190 OBAPI bool OBCompareInt(const int &,const int &);
00192 OBAPI bool OBCompareUnsigned(const unsigned int &,const unsigned int &);
00194 OBAPI bool IsNear(const double &, const double &, const double epsilon=2e-6);
00196 OBAPI bool IsNearZero(const double &, const double epsilon=2e-6);
00197 
00198 //******************triple template*************************
00200 template <class T1, class T2, class T3>
00201 struct triple
00202 {
00203     //type names for the values
00204     typedef T1 first_type;
00205     typedef T2 second_type;
00206     typedef T3 third_type;
00207 
00208     //member
00209     T1 first;
00210     T2 second;
00211     T3 third;
00212   
00216   triple():
00217     first(T1()),second(T2()),third(T3())
00218   {}
00219 
00221   triple(const T1 &a, const T2 &b, const T3 &c):
00222     first(a), second(b), third(c)
00223   {}
00224 
00226   template<class U, class V, class W>
00227     triple(const triple<U,V,W> &t):
00228       first(t.first), second(t.second), third(t.third)
00229   {}
00230 
00231 };
00232 
00233 //**************quad template********************
00235 template <class T1, class T2, class T3, class T4>
00236 struct quad
00237 {
00238     //type names for the values
00239     typedef T1 first_type;
00240     typedef T2 second_type;
00241     typedef T3 third_type;
00242     typedef T4 fourth_type;
00243 
00244     //member
00245     T1 first;
00246     T2 second;
00247     T3 third;
00248     T4 fourth;
00249 
00253   quad():
00254     first(T1()),second(T2()),third(T3()),fourth(T4())
00255   {}
00256 
00258   quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d):
00259     first(a), second(b), third(c), fourth(d)
00260   {}
00261 
00263   template<class U, class V, class W, class X>
00264     quad(const quad<U,V,W,X> &q):
00265       first(q.first), second(q.second), third(q.third), fourth(q.fourth)
00266   {}
00267 
00268 };
00269 
00270 } // end namespace OpenBabel
00271 
00272 #endif // OBUTIL_H
00273