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<const double>( 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<const double>( a, b ) ); 00232 } 00236 OBAPI bool CanBeSquared(const double &); 00237 00238 OBAPI bool SafeOpen(std::ifstream &fs, const char *filename); 00239 OBAPI bool SafeOpen(std::ofstream &fs, const char *filename); 00240 #endif 00241 // (end part to be skipped by SWIG) 00242 00243 //******************triple template************************* 00246 template <class T1, class T2, class T3> 00247 struct triple 00248 { 00249 //type names for the values 00250 typedef T1 first_type; 00251 typedef T2 second_type; 00252 typedef T3 third_type; 00253 00254 //member 00255 T1 first; 00256 T2 second; 00257 T3 third; 00258 00262 triple(): 00263 first(T1()),second(T2()),third(T3()) 00264 {} 00265 00267 triple(const T1 &a, const T2 &b, const T3 &c): 00268 first(a), second(b), third(c) 00269 {} 00270 00272 template<class U, class V, class W> 00273 triple(const triple<U,V,W> &t): 00274 first(t.first), second(t.second), third(t.third) 00275 {} 00276 00277 }; 00278 00279 //**************quad template******************** 00282 template <class T1, class T2, class T3, class T4> 00283 struct quad 00284 { 00285 //type names for the values 00286 typedef T1 first_type; 00287 typedef T2 second_type; 00288 typedef T3 third_type; 00289 typedef T4 fourth_type; 00290 00291 //member 00292 T1 first; 00293 T2 second; 00294 T3 third; 00295 T4 fourth; 00296 00300 quad(): 00301 first(T1()),second(T2()),third(T3()),fourth(T4()) 00302 {} 00303 00305 quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d): 00306 first(a), second(b), third(c), fourth(d) 00307 {} 00308 00310 template<class U, class V, class W, class X> 00311 quad(const quad<U,V,W,X> &q): 00312 first(q.first), second(q.second), third(q.third), fourth(q.fourth) 00313 {} 00314 00315 }; 00316 00317 } // end namespace OpenBabel 00318 00319 #endif // OBUTIL_H 00320
This file is part of the documentation for Open Babel, version 2.2.0.