| 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 |
gezelter |
751 |
#include "config.h" |
| 24 |
tim |
741 |
|
| 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 |
gezelter |
1081 |
#ifndef SWIG |
| 174 |
tim |
741 |
OBAPI void rotate_coords(double*,double m[3][3],int); |
| 175 |
gezelter |
1081 |
#endif |
| 176 |
tim |
741 |
OBAPI double calc_rms(double*,double*,unsigned int); |
| 177 |
|
|
|
| 178 |
|
|
//! \name String conversion utilities |
| 179 |
|
|
//@{ |
| 180 |
|
|
// Documentation in obutil.cpp |
| 181 |
|
|
OBAPI void ToUpper(std::string&); |
| 182 |
|
|
OBAPI void ToUpper(char*); |
| 183 |
|
|
OBAPI void ToLower(std::string&); |
| 184 |
|
|
OBAPI void ToLower(char *); |
| 185 |
|
|
//! "Clean" the supplied atom type |
| 186 |
|
|
OBAPI void CleanAtomType(char*); |
| 187 |
|
|
//@} |
| 188 |
|
|
|
| 189 |
|
|
//! Comparison -- returns true if first parameter less than second |
| 190 |
|
|
OBAPI bool OBCompareInt(const int &,const int &); |
| 191 |
|
|
//! Comparison -- returns true if first parameter less than second |
| 192 |
|
|
OBAPI bool OBCompareUnsigned(const unsigned int &,const unsigned int &); |
| 193 |
|
|
//! Safe comparison for floats/doubles: true if a and b are closer than epsilon |
| 194 |
|
|
OBAPI bool IsNear(const double &, const double &, const double epsilon=2e-6); |
| 195 |
|
|
//! Safe comparison for floats/doubles: true if a is less than epsilon |
| 196 |
|
|
OBAPI bool IsNearZero(const double &, const double epsilon=2e-6); |
| 197 |
|
|
|
| 198 |
|
|
//******************triple template************************* |
| 199 |
|
|
//! \brief A 3-element templated, based on the design of the STL pair<> |
| 200 |
|
|
template <class T1, class T2, class T3> |
| 201 |
|
|
struct triple |
| 202 |
|
|
{ |
| 203 |
|
|
//type names for the values |
| 204 |
|
|
typedef T1 first_type; |
| 205 |
|
|
typedef T2 second_type; |
| 206 |
|
|
typedef T3 third_type; |
| 207 |
|
|
|
| 208 |
|
|
//member |
| 209 |
|
|
T1 first; |
| 210 |
|
|
T2 second; |
| 211 |
|
|
T3 third; |
| 212 |
|
|
|
| 213 |
|
|
/** Default constructor |
| 214 |
|
|
* T1() and T2() and T3() force initialization for built in types |
| 215 |
|
|
**/ |
| 216 |
|
|
triple(): |
| 217 |
|
|
first(T1()),second(T2()),third(T3()) |
| 218 |
|
|
{} |
| 219 |
|
|
|
| 220 |
|
|
//! Constructor for 3 values |
| 221 |
|
|
triple(const T1 &a, const T2 &b, const T3 &c): |
| 222 |
|
|
first(a), second(b), third(c) |
| 223 |
|
|
{} |
| 224 |
|
|
|
| 225 |
|
|
//! Copy constructor with implicit conversions |
| 226 |
|
|
template<class U, class V, class W> |
| 227 |
|
|
triple(const triple<U,V,W> &t): |
| 228 |
|
|
first(t.first), second(t.second), third(t.third) |
| 229 |
|
|
{} |
| 230 |
|
|
|
| 231 |
|
|
}; |
| 232 |
|
|
|
| 233 |
|
|
//**************quad template******************** |
| 234 |
|
|
//! \brief A 4-element templated, based on the design of the STL pair<> |
| 235 |
|
|
template <class T1, class T2, class T3, class T4> |
| 236 |
|
|
struct quad |
| 237 |
|
|
{ |
| 238 |
|
|
//type names for the values |
| 239 |
|
|
typedef T1 first_type; |
| 240 |
|
|
typedef T2 second_type; |
| 241 |
|
|
typedef T3 third_type; |
| 242 |
|
|
typedef T4 fourth_type; |
| 243 |
|
|
|
| 244 |
|
|
//member |
| 245 |
|
|
T1 first; |
| 246 |
|
|
T2 second; |
| 247 |
|
|
T3 third; |
| 248 |
|
|
T4 fourth; |
| 249 |
|
|
|
| 250 |
|
|
/*! default constructor |
| 251 |
|
|
* T1() and T2() and T3() force initialization for built in types |
| 252 |
|
|
*/ |
| 253 |
|
|
quad(): |
| 254 |
|
|
first(T1()),second(T2()),third(T3()),fourth(T4()) |
| 255 |
|
|
{} |
| 256 |
|
|
|
| 257 |
|
|
//! constructor for 3 values |
| 258 |
|
|
quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d): |
| 259 |
|
|
first(a), second(b), third(c), fourth(d) |
| 260 |
|
|
{} |
| 261 |
|
|
|
| 262 |
|
|
//! copy constructor with implicit conversions |
| 263 |
|
|
template<class U, class V, class W, class X> |
| 264 |
|
|
quad(const quad<U,V,W,X> &q): |
| 265 |
|
|
first(q.first), second(q.second), third(q.third), fourth(q.fourth) |
| 266 |
|
|
{} |
| 267 |
|
|
|
| 268 |
|
|
}; |
| 269 |
|
|
|
| 270 |
|
|
} // end namespace OpenBabel |
| 271 |
|
|
|
| 272 |
|
|
#endif // OBUTIL_H |
| 273 |
|
|
|
| 274 |
|
|
//! \file obutil.h |
| 275 |
|
|
//! \brief Various utility methods. |