| 1 | tim | 741 | /********************************************************************** | 
| 2 |  |  | obutil.h - Various utility methods. | 
| 3 |  |  |  | 
| 4 |  |  | Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. | 
| 5 |  |  | Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison | 
| 6 |  |  |  | 
| 7 |  |  | This file is part of the Open Babel project. | 
| 8 |  |  | For more information, see <http://openbabel.sourceforge.net/> | 
| 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 "babelconfig.hpp" | 
| 24 |  |  |  | 
| 25 |  |  | #if HAVE_IOSTREAM | 
| 26 |  |  | #include <iostream> | 
| 27 |  |  | #elif HAVE_IOSTREAM_H | 
| 28 |  |  | #include <iostream.h> | 
| 29 |  |  | #endif | 
| 30 |  |  |  | 
| 31 |  |  | #if TIME_WITH_SYS_TIME | 
| 32 |  |  | #include <sys/time.h> | 
| 33 |  |  | #include <time.h> | 
| 34 |  |  | #else | 
| 35 |  |  | #if HAVE_SYS_TIME_H | 
| 36 |  |  | #include <sys/time.h> | 
| 37 |  |  | #else | 
| 38 |  |  | #include <time.h> | 
| 39 |  |  | #endif | 
| 40 |  |  | #endif | 
| 41 |  |  |  | 
| 42 |  |  | #include <string> | 
| 43 |  |  |  | 
| 44 |  |  | namespace OpenBabel | 
| 45 |  |  | { | 
| 46 |  |  |  | 
| 47 |  |  | // class introduction in obutil.cpp | 
| 48 |  |  | class OBAPI OBStopwatch | 
| 49 |  |  | { | 
| 50 |  |  | #if HAVE_CLOCK_T | 
| 51 |  |  | clock_t start, stop; | 
| 52 |  |  | #else | 
| 53 |  |  |  | 
| 54 |  |  | timeval start; | 
| 55 |  |  | timeval stop; | 
| 56 |  |  | #endif | 
| 57 |  |  |  | 
| 58 |  |  | public: | 
| 59 |  |  | #if HAVE_CLOCK_T | 
| 60 |  |  |  | 
| 61 |  |  | void  Start() | 
| 62 |  |  | { | 
| 63 |  |  | start= clock(); | 
| 64 |  |  | } | 
| 65 |  |  | double Lap() | 
| 66 |  |  | { | 
| 67 |  |  | stop= clock(); | 
| 68 |  |  | return((double)(stop - start) / CLOCKS_PER_SEC); | 
| 69 |  |  | } | 
| 70 |  |  | #else | 
| 71 |  |  | void Start() | 
| 72 |  |  | { | 
| 73 |  |  | gettimeofday(&start,(struct timezone *)NULL); | 
| 74 |  |  | } | 
| 75 |  |  | double Lap() | 
| 76 |  |  | { | 
| 77 |  |  | gettimeofday(&stop,(struct timezone *)NULL); | 
| 78 |  |  | return((stop.tv_sec - start.tv_sec) | 
| 79 |  |  | + (double)(stop.tv_usec - start.tv_usec)/1000000.0); | 
| 80 |  |  | } | 
| 81 |  |  | #endif | 
| 82 |  |  | double Elapsed() | 
| 83 |  |  | { | 
| 84 |  |  | return(Lap()); | 
| 85 |  |  | } | 
| 86 |  |  | }; | 
| 87 |  |  |  | 
| 88 |  |  |  | 
| 89 |  |  | //! sqrt lookup table - given a distance squared returns distance | 
| 90 |  |  | class OBAPI OBSqrtTbl | 
| 91 |  |  | { | 
| 92 |  |  | double _max,_incr,*_tbl; | 
| 93 |  |  | public: | 
| 94 |  |  | OBSqrtTbl() | 
| 95 |  |  | { | 
| 96 |  |  | _tbl=NULL; | 
| 97 |  |  | _max = _incr = 0.0; | 
| 98 |  |  | } | 
| 99 |  |  | OBSqrtTbl(double max,double incr) | 
| 100 |  |  | { | 
| 101 |  |  | Init(max,incr); | 
| 102 |  |  | } | 
| 103 |  |  | ~OBSqrtTbl() | 
| 104 |  |  | { | 
| 105 |  |  | if (_tbl) | 
| 106 |  |  | { | 
| 107 |  |  | delete [] _tbl; | 
| 108 |  |  | _tbl = NULL; | 
| 109 |  |  | } | 
| 110 |  |  | } | 
| 111 |  |  | double Sqrt(double d2) const | 
| 112 |  |  | { | 
| 113 |  |  | if (_tbl) | 
| 114 |  |  | return((d2 < _max) ? _tbl[(int)(d2*_incr)]:sqrt(d2)); | 
| 115 |  |  | else | 
| 116 |  |  | return 0.0; | 
| 117 |  |  | } | 
| 118 |  |  | void Init(double max,double incr) | 
| 119 |  |  | { | 
| 120 |  |  | int i; | 
| 121 |  |  | double r; | 
| 122 |  |  | _max = max*max; | 
| 123 |  |  | _incr = incr; | 
| 124 |  |  | //array size needs to be large enough to account for fp error | 
| 125 |  |  | _tbl = new double [(unsigned int)((_max/_incr)+10)]; | 
| 126 |  |  | for (r = (_incr/2.0),i=0;r <= _max;r += _incr,i++) | 
| 127 |  |  | _tbl[i] = sqrt(r); | 
| 128 |  |  |  | 
| 129 |  |  | _incr = 1/_incr; | 
| 130 |  |  | } | 
| 131 |  |  | }; | 
| 132 |  |  |  | 
| 133 |  |  |  | 
| 134 |  |  |  | 
| 135 |  |  | //****************************************** | 
| 136 |  |  | //*** Stuff for random number generation *** | 
| 137 |  |  | //****************************************** | 
| 138 |  |  |  | 
| 139 |  |  | //! Used for internal random number generation OBRandom (unless the system random generaor is used) | 
| 140 |  |  | typedef struct | 
| 141 |  |  | { | 
| 142 |  |  | unsigned int hi; | 
| 143 |  |  | unsigned int lo; | 
| 144 |  |  | } | 
| 145 |  |  | DoubleType; | 
| 146 |  |  |  | 
| 147 |  |  | OBAPI void DoubleMultiply( unsigned int,unsigned int,DoubleType*); | 
| 148 |  |  | OBAPI void DoubleAdd( DoubleType*,unsigned int); | 
| 149 |  |  | OBAPI unsigned int DoubleModulus( DoubleType*,unsigned int); | 
| 150 |  |  |  | 
| 151 |  |  | //! Random number generator | 
| 152 |  |  | class OBAPI OBRandom | 
| 153 |  |  | { | 
| 154 |  |  | DoubleType d; | 
| 155 |  |  | unsigned int m,a,c; | 
| 156 |  |  | unsigned int p; | 
| 157 |  |  | unsigned int i; | 
| 158 |  |  | unsigned int x; | 
| 159 |  |  | bool OBRandomUseSysRand; | 
| 160 |  |  |  | 
| 161 |  |  | public: | 
| 162 |  |  | OBRandom(bool useSys= false); | 
| 163 |  |  | void Seed(int seed) | 
| 164 |  |  | { | 
| 165 |  |  | x = seed; | 
| 166 |  |  | } | 
| 167 |  |  | void TimeSeed(); | 
| 168 |  |  | int NextInt(); | 
| 169 |  |  | double NextFloat(); | 
| 170 |  |  | }; | 
| 171 |  |  |  | 
| 172 |  |  | //***RMS helper methods***/ | 
| 173 |  |  | OBAPI void  rotate_coords(double*,double m[3][3],int); | 
| 174 |  |  | OBAPI double calc_rms(double*,double*,unsigned int); | 
| 175 |  |  |  | 
| 176 |  |  | //! \name  String conversion utilities | 
| 177 |  |  | //@{ | 
| 178 |  |  | // Documentation in obutil.cpp | 
| 179 |  |  | OBAPI void ToUpper(std::string&); | 
| 180 |  |  | OBAPI void ToUpper(char*); | 
| 181 |  |  | OBAPI void ToLower(std::string&); | 
| 182 |  |  | OBAPI void ToLower(char *); | 
| 183 |  |  | //! "Clean" the supplied atom type | 
| 184 |  |  | OBAPI void CleanAtomType(char*); | 
| 185 |  |  | //@} | 
| 186 |  |  |  | 
| 187 |  |  | //! Comparison -- returns true if first parameter less than second | 
| 188 |  |  | OBAPI bool OBCompareInt(const int &,const int &); | 
| 189 |  |  | //! Comparison -- returns true if first parameter less than second | 
| 190 |  |  | OBAPI bool OBCompareUnsigned(const unsigned int &,const unsigned int &); | 
| 191 |  |  | //! Safe comparison for floats/doubles: true if a and b are closer than epsilon | 
| 192 |  |  | OBAPI bool IsNear(const double &, const double &, const double epsilon=2e-6); | 
| 193 |  |  | //! Safe comparison for floats/doubles: true if a is less than epsilon | 
| 194 |  |  | OBAPI bool IsNearZero(const double &, const double epsilon=2e-6); | 
| 195 |  |  |  | 
| 196 |  |  | //******************triple template************************* | 
| 197 |  |  | //! \brief A 3-element templated, based on the design of the STL pair<> | 
| 198 |  |  | template <class T1, class T2, class T3> | 
| 199 |  |  | struct triple | 
| 200 |  |  | { | 
| 201 |  |  | //type names for the values | 
| 202 |  |  | typedef T1 first_type; | 
| 203 |  |  | typedef T2 second_type; | 
| 204 |  |  | typedef T3 third_type; | 
| 205 |  |  |  | 
| 206 |  |  | //member | 
| 207 |  |  | T1 first; | 
| 208 |  |  | T2 second; | 
| 209 |  |  | T3 third; | 
| 210 |  |  |  | 
| 211 |  |  | /** Default constructor | 
| 212 |  |  | *    T1() and T2() and T3() force initialization for built in types | 
| 213 |  |  | **/ | 
| 214 |  |  | triple(): | 
| 215 |  |  | first(T1()),second(T2()),third(T3()) | 
| 216 |  |  | {} | 
| 217 |  |  |  | 
| 218 |  |  | //! Constructor for 3 values | 
| 219 |  |  | triple(const T1 &a, const T2 &b, const T3 &c): | 
| 220 |  |  | first(a), second(b), third(c) | 
| 221 |  |  | {} | 
| 222 |  |  |  | 
| 223 |  |  | //! Copy constructor with implicit conversions | 
| 224 |  |  | template<class U, class V, class W> | 
| 225 |  |  | triple(const triple<U,V,W> &t): | 
| 226 |  |  | first(t.first), second(t.second), third(t.third) | 
| 227 |  |  | {} | 
| 228 |  |  |  | 
| 229 |  |  | }; | 
| 230 |  |  |  | 
| 231 |  |  | //**************quad template******************** | 
| 232 |  |  | //! \brief A 4-element templated, based on the design of the STL pair<> | 
| 233 |  |  | template <class T1, class T2, class T3, class T4> | 
| 234 |  |  | struct quad | 
| 235 |  |  | { | 
| 236 |  |  | //type names for the values | 
| 237 |  |  | typedef T1 first_type; | 
| 238 |  |  | typedef T2 second_type; | 
| 239 |  |  | typedef T3 third_type; | 
| 240 |  |  | typedef T4 fourth_type; | 
| 241 |  |  |  | 
| 242 |  |  | //member | 
| 243 |  |  | T1 first; | 
| 244 |  |  | T2 second; | 
| 245 |  |  | T3 third; | 
| 246 |  |  | T4 fourth; | 
| 247 |  |  |  | 
| 248 |  |  | /*! default constructor | 
| 249 |  |  | *   T1() and T2() and T3() force initialization for built in types | 
| 250 |  |  | */ | 
| 251 |  |  | quad(): | 
| 252 |  |  | first(T1()),second(T2()),third(T3()),fourth(T4()) | 
| 253 |  |  | {} | 
| 254 |  |  |  | 
| 255 |  |  | //! constructor for 3 values | 
| 256 |  |  | quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d): | 
| 257 |  |  | first(a), second(b), third(c), fourth(d) | 
| 258 |  |  | {} | 
| 259 |  |  |  | 
| 260 |  |  | //! copy constructor with implicit conversions | 
| 261 |  |  | template<class U, class V, class W, class X> | 
| 262 |  |  | quad(const quad<U,V,W,X> &q): | 
| 263 |  |  | first(q.first), second(q.second), third(q.third), fourth(q.fourth) | 
| 264 |  |  | {} | 
| 265 |  |  |  | 
| 266 |  |  | }; | 
| 267 |  |  |  | 
| 268 |  |  | } // end namespace OpenBabel | 
| 269 |  |  |  | 
| 270 |  |  | #endif // OBUTIL_H | 
| 271 |  |  |  | 
| 272 |  |  | //! \file obutil.h | 
| 273 |  |  | //! \brief Various utility methods. |