Loading...
Searching...
No Matches
obutil.h
Go to the documentation of this file.
/**********************************************************************
obutil.h - Various utility methods.
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifndef OB_UTIL_H
#define OB_UTIL_H
#include <openbabel/babelconfig.h>
#include <string>
#include <iosfwd>
#if TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#include <math.h>
#include <ctime>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
namespace OpenBabel
{
// class introduction in obutil.cpp
class OBAPI OBStopwatch
{
#if HAVE_CLOCK_T
clock_t start;
clock_t stop;
#else
timeval start;
timeval stop;
#endif
public:
#if HAVE_CLOCK_T
void Start()
{
start= clock();
}
double Lap()
{
stop= clock();
return((stop - start) / (double) CLOCKS_PER_SEC);
}
#else
void Start()
{
gettimeofday(&start, nullptr);
}
double Lap()
{
gettimeofday(&stop, nullptr);
return((stop.tv_sec - start.tv_sec)
+ (stop.tv_usec - start.tv_usec)/1000000.0);
}
#endif
double Elapsed()
{
return(Lap());
}
};
class OBAPI OBSqrtTbl
{
double _max,_incr,*_tbl;
public:
OBSqrtTbl():
_max(0.0), _incr(0.0), _tbl(nullptr)
{ }
OBSqrtTbl(const double max, const double incr):
_max(max*max), _incr(incr), _tbl(nullptr)
{
Init(max,incr);
}
~OBSqrtTbl()
{
if (_tbl)
{
delete [] _tbl;
_tbl = nullptr;
}
}
double Sqrt(double d2) const
{
if (_tbl)
return((d2 < _max) ? _tbl[static_cast<int>(d2*_incr)]:sqrt(d2));
else
return 0.0;
}
void Init(double max,double incr)
{
// parameters are potentially unneeded, but let's do this until we can
// deprecate them
_max = max * max;
_incr = incr;
//array size needs to be large enough to account for fp error
int i;
double r;
_tbl = new double [static_cast<int>((_max/_incr)+10)];
for (r = (_incr/2.0),i=0;r <= _max;r += _incr,++i)
_tbl[i] = sqrt(r);
_incr = 1/_incr;
}
};
//***RMS helper methods***/
#ifndef __KCC
extern "C" {
OBAPI void rotate_coords(double*,double m[3][3],unsigned);
OBAPI double calc_rms(double*,double*,unsigned int);
}
#else
OBAPI void rotate_coords(double*,double m[3][3],unsigned);
OBAPI double calc_rms(double*,double*,unsigned int);
#endif
#ifndef SWIG
// Documentation in obutil.cpp
OBAPI void ToUpper(std::string&);
OBAPI void ToUpper(char*);
OBAPI void ToLower(std::string&);
OBAPI void ToLower(char *);
OBAPI void InvertCase(std::string&, int);
OBAPI void InvertCase(char *);
OBAPI void CleanAtomType(char*);
OBAPI bool OBCompareInt(const int &a,const int &b);
OBAPI bool OBCompareUnsigned(const unsigned int &a,const unsigned int &b);
OBAPI bool IsNear(const double &, const double &, const double epsilon=2e-6);
OBAPI bool IsNearZero(const double &, const double epsilon=2e-6);
OBAPI bool IsNan(const double &);
OBAPI inline bool IsNegligible(const double & a, const double & b,
const double precision = 1e-11)
{
return( fabs(a) <= precision * fabs(b) );
}
OBAPI inline bool IsApprox(const double & a, const double & b,
const double precision = 1e-11)
{
return( fabs(a - b) <= precision * std::min<const double>( fabs(a), fabs(b) ) );
}
OBAPI inline bool IsApprox_pos(const double &a, const double &b,
const double precision = 1e-11)
{
return( fabs(a - b) <= precision * std::min<const double>( a, b ) );
}
OBAPI bool CanBeSquared(const double &);
OBAPI bool SafeOpen(std::ifstream &fs, const char *filename);
OBAPI bool SafeOpen(std::ofstream &fs, const char *filename);
#endif
// (end part to be skipped by SWIG)
//******************triple template*************************
template <class T1, class T2, class T3>
struct triple
{
//type names for the values
typedef T1 first_type;
typedef T2 second_type;
typedef T3 third_type;
//member
T1 first;
T2 second;
T3 third;
triple():
first(T1()),second(T2()),third(T3())
{}
triple(const T1 &a, const T2 &b, const T3 &c):
first(a), second(b), third(c)
{}
template<class U, class V, class W>
triple(const triple<U,V,W> &t):
first(t.first), second(t.second), third(t.third)
{}
};
//**************quad template********************
template <class T1, class T2, class T3, class T4>
struct quad
{
//type names for the values
typedef T1 first_type;
typedef T2 second_type;
typedef T3 third_type;
typedef T4 fourth_type;
//member
T1 first;
T2 second;
T3 third;
T4 fourth;
quad():
first(T1()),second(T2()),third(T3()),fourth(T4())
{}
quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d):
first(a), second(b), third(c), fourth(d)
{}
template<class U, class V, class W, class X>
quad(const quad<U,V,W,X> &q):
first(q.first), second(q.second), third(q.third), fourth(q.fourth)
{}
};
} // end namespace OpenBabel
#endif // OBUTIL_H
OpenBabel::OBSqrtTbl::Init
void Init(double max, double incr)
Initialize the square root lookup table.
Definition obutil.h:135
OpenBabel::OBSqrtTbl::OBSqrtTbl
OBSqrtTbl(const double max, const double incr)
Create a square root table to handle up to the square root of max (e.g., if you want the square root ...
Definition obutil.h:110
OpenBabel::OBSqrtTbl::Sqrt
double Sqrt(double d2) const
Fast square root calculation using a lookup table.
Definition obutil.h:125
OpenBabel::OBSqrtTbl::~OBSqrtTbl
~OBSqrtTbl()
Definition obutil.h:115
OpenBabel::OBSqrtTbl::OBSqrtTbl
OBSqrtTbl()
Definition obutil.h:103
OpenBabel::OBStopwatch
Stopwatch class used for timing length of execution.
Definition obutil.h:51
OpenBabel::OBStopwatch::Start
void Start()
Mark the start of "stopwatch" timing.
Definition obutil.h:76
OpenBabel::OBStopwatch::Lap
double Lap()
Definition obutil.h:81
OpenBabel::OBStopwatch::Elapsed
double Elapsed()
Definition obutil.h:90
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
OpenBabel::SafeOpen
bool SafeOpen(std::ifstream &fs, const char *filename)
Definition obutil.cpp:295
OpenBabel::IsNan
bool IsNan(const double &)
Comparison for nan (not a number).
Definition obutil.cpp:92
OpenBabel::calc_rms
double calc_rms(double *, double *, unsigned int)
Calculate the RMS deviation between the first N coordinates of *r and *f.
Definition obutil.cpp:173
OpenBabel::IsNearZero
bool IsNearZero(const double &, const double epsilon=2e-6)
Comparison for doubles: returns fabs(a) < epsilon.
Definition obutil.cpp:86
OpenBabel::IsNear
bool IsNear(const double &, const double &, const double epsilon=2e-6)
Comparison for doubles: returns fabs(a - b) < epsilon.
Definition obutil.cpp:80
OpenBabel::OBCompareInt
bool OBCompareInt(const int &a, const int &b)
Definition obutil.cpp:68
OpenBabel::IsApprox
bool IsApprox(const double &a, const double &b, const double precision=1e-11)
Definition obutil.h:227
OpenBabel::InvertCase
void InvertCase(std::string &, int)
OpenBabel::ToLower
void ToLower(std::string &)
Shift the supplied string to lowercase.
Definition obutil.cpp:379
OpenBabel::CanBeSquared
bool CanBeSquared(const double &)
Tests whether its argument can be squared without triggering an overflow or underflow.
Definition obutil.cpp:99
OpenBabel::ToUpper
void ToUpper(std::string &)
Shift the supplied string to uppercase.
Definition obutil.cpp:359
OpenBabel::IsApprox_pos
bool IsApprox_pos(const double &a, const double &b, const double precision=1e-11)
Same as IsApprox(), but only for positive numbers. Faster.
Definition obutil.h:233
OpenBabel::OBCompareUnsigned
bool OBCompareUnsigned(const unsigned int &a, const unsigned int &b)
Definition obutil.cpp:74
OpenBabel::IsNegligible
bool IsNegligible(const double &a, const double &b, const double precision=1e-11)
Definition obutil.h:205
OpenBabel::CleanAtomType
void CleanAtomType(char *)
"Clean" the supplied atom type
Definition obutil.cpp:427
OpenBabel::rotate_coords
void rotate_coords(double *, double m[3][3], unsigned)
OpenBabel::quad::fourth_type
T4 fourth_type
Definition obutil.h:294
OpenBabel::quad::quad
quad()
Definition obutil.h:305
OpenBabel::quad::first_type
T1 first_type
Definition obutil.h:291
OpenBabel::quad::third_type
T3 third_type
Definition obutil.h:293
OpenBabel::quad::fourth
T4 fourth
Definition obutil.h:300
OpenBabel::quad::second_type
T2 second_type
Definition obutil.h:292
OpenBabel::quad::second
T2 second
Definition obutil.h:298
OpenBabel::quad::third
T3 third
Definition obutil.h:299
OpenBabel::quad::quad
quad(const T1 &a, const T2 &b, const T3 &c, const T4 &d)
constructor for 3 values
Definition obutil.h:310
OpenBabel::quad::first
T1 first
Definition obutil.h:297
OpenBabel::quad::quad
quad(const quad< U, V, W, X > &q)
copy constructor with implicit conversions
Definition obutil.h:316
OpenBabel::triple::first_type
T1 first_type
Definition obutil.h:255
OpenBabel::triple::third_type
T3 third_type
Definition obutil.h:257
OpenBabel::triple::second_type
T2 second_type
Definition obutil.h:256
OpenBabel::triple::triple
triple()
Definition obutil.h:267
OpenBabel::triple::second
T2 second
Definition obutil.h:261
OpenBabel::triple::triple
triple(const T1 &a, const T2 &b, const T3 &c)
Constructor for 3 values.
Definition obutil.h:272
OpenBabel::triple::third
T3 third
Definition obutil.h:262
OpenBabel::triple::first
T1 first
Definition obutil.h:260
OpenBabel::triple::triple
triple(const triple< U, V, W > &t)
Copy constructor with implicit conversions.
Definition obutil.h:278