grid.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 grid.h - Handle grids of values.
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_GRID_H
00021 #define OB_GRID_H
00022 
00023 #include <openbabel/babelconfig.h>
00024 
00025 #include <iosfwd>
00026 #include <algorithm>
00027 #include <vector>
00028 #include <string>
00029 
00030 namespace OpenBabel
00031 {
00032 
00033   // Forward declaration
00034   class OBMol;
00035 
00038  class OBAPI OBGrid: public OBBase
00039   {
00040   protected: 
00041     double _xmin,_xmax,_ymin,_ymax,_zmin,_zmax; 
00042 
00043   public:
00044     OBGrid() {}
00045 
00049     virtual void Init(OBMol &box);
00050 
00052     double GetXmin() const    { return(_xmin);    }
00054     double GetYmin() const    { return(_ymin);    }
00056     double GetZmin() const    { return(_zmin);    }
00058     double GetXmax() const    { return(_xmax);    }
00060     double GetYmax() const    { return(_ymax);    }
00062     double GetZmax() const    { return(_zmax);    }
00063 
00065     bool PointIsInBox(double x,double y,double z)
00066     {
00067       return (x>=_xmin) && (x<=_xmax) &&
00068         (y>=_ymin) && (y<=_ymax) &&
00069         (z>=_zmin) && (z<=_zmax);
00070     }
00072     bool PointIsInBox(double *c)
00073     {
00074       return (c[0]>=_xmin) && (c[0]<=_xmax) &&
00075         (c[1]>=_ymin) && (c[1]<=_ymax) &&
00076         (c[2]>=_zmin) && (c[2]<=_zmax);
00077     }
00078   };
00079 
00085  class OBAPI OBFloatGrid: public OBGrid
00086   {
00087   protected:
00088     double *_val;             
00089     int   *_ival;             
00090     double _midz,_midx,_midy; 
00091     int _ydim,_xdim,_zdim;    
00092     double _spacing,_inv_spa; 
00093     double _halfSpace;        
00094 
00095   public:
00096 
00097   OBFloatGrid() : _val(NULL), _ival(NULL), _halfSpace(0.0) {}
00098     ~OBFloatGrid()
00099       {
00100         if (_ival) delete [] _ival;
00101         if (_val)  delete [] _val;
00102       }
00105     void Init(OBMol &box,double spacing, double pad= 0.0);
00106 
00107     void GetMin(double *a)
00108     {
00109       a[0]=_xmin;
00110       a[1]=_ymin;
00111       a[2]=_zmin;
00112     }
00113     void GetMax(double *a)
00114     {
00115       a[0]=_xmax;
00116       a[1]=_ymax;
00117       a[2]=_zmax;
00118     }
00119 
00120     double GetSpacing() const { return(_spacing); }
00121     void GetSpacing(double &s)
00122     {
00123       s=_spacing;
00124     }
00125     double GetScale() const   { return(_inv_spa); }
00126     double GetHalfSpace() const {return(_halfSpace);}
00127     int GetXdim() const       { return(_xdim);    }
00128     int GetYdim() const       { return(_ydim);    }
00129     int GetZdim() const       { return(_zdim);    }
00130     void GetDim(int *a)
00131     {
00132       a[0]=_xdim;
00133       a[1]=_ydim;
00134       a[2]=_zdim;
00135     }
00136     vector3 GetMidpointVector()
00137     {
00138       vector3 v;
00139       v.Set(_midx,_midy,_midz);
00140       return(v);
00141     }
00142     double *GetVals()    {        return(_val);    }
00143     void SetVals(double *ptr)    {  _val = ptr;    }
00144     vector3 Center()
00145     {
00146       return vector3(_midx,_midy,_midz);
00147     }
00148 
00149     friend std::ostream& ".">operator<< ( std::ostream&, const OBFloatGrid& ) ;
00150     friend std::istream& operator>> ( std::istream&,OBFloatGrid& ) ;
00151 
00153     double Inject(double x,double y,double z);
00154 
00155     void IndexToCoords(int idx, double &x, double &y, double &z);
00156     void CoordsToIndex(int*,double*);
00157     int CoordsToIndex(double &x, double &y, double &z);
00159     double Interpolate(double,double,double);
00161     double InterpolateDerivatives(double,double,double,double *derivatives);
00162   };
00163 
00164 #ifndef OBPolarGrid
00165 #define OBPolarGrid 0x01 /* polar interactions? */
00166 #endif //OBPolarGrid
00167 
00168 #ifndef OBLipoGrid
00169 #define OBLipoGrid 0x02 /* lipophilicity? */
00170 #endif //OBLipoGrid
00171 
00175  class OBAPI OBProxGrid: public OBGrid
00176   {
00177   protected:
00178     int _gridtype;
00179     int _nxinc,_nyinc,_nzinc,_maxinc;
00180     double _inc;
00181     std::vector<std::vector<int> > cell;
00182     
00183   public:
00184 
00185     OBProxGrid(int gridtype=0)
00186       {
00187         _gridtype=gridtype;
00188       }
00189     ~OBProxGrid()
00190       {}
00191     void Setup(OBMol &mol,OBMol &box, double cutoff,double resolution = 0.5);
00192     void Setup(OBMol &mol,OBMol &box, double cutoff,
00193                std::vector<bool> &use,double resolution = 0.5);
00194     std::vector<int> *GetProxVector(double,double,double);
00195     std::vector<int> *GetProxVector(double*);
00196 
00197     bool LipoGrid()
00198     {
00199       return((_gridtype&OBLipoGrid) ? true : false);
00200     }
00201     bool PolarGrid()
00202     {
00203       return(_gridtype&OBPolarGrid);
00204     }
00205     void SetGridType(int gridtype)
00206     {
00207       _gridtype = gridtype;
00208     }
00209   };
00210 
00211   // scoring function used: PLP = Piecewise Linear Potential or ChemScore algorithm
00212   typedef enum { Undefined = -1, PLP, ChemScore } score_t;
00213 
00217   class OBAPI OBScoreGrid
00218   {
00219   protected:
00220     score_t gridtype;
00221     bool verbose;
00222 
00223   public:
00224 
00225     double score;
00226 
00227     OBScoreGrid(void)                 {  verbose = false;      }
00228     virtual ~OBScoreGrid(void) {}
00229 
00230     void    SetVerbose(bool v)        {      verbose = v;      }
00231     void    SetType(score_t type)     {      gridtype = type;  }
00232     score_t GetType(void)             {    return gridtype;    }
00233 
00234     virtual void   Clear(void)        { }
00235     virtual double  Eval(double *)    {       return -1;       }
00236     virtual double  Eval(OBMol &mol){return Eval(mol.GetCoordinates());}
00237     virtual void   Init(OBMol &, OBMol &, std::string &, double){}
00238     virtual void   Setup(OBMol &) {}
00239     virtual void   Setup(OBMol &, std::vector<int> &){}
00240     virtual void   Setup(std::vector<int> &) {}
00241     virtual void   Config(std::string) {}
00242     virtual bool   Read(std::string)   {      return false;    }
00243     virtual bool   Write(std::string)  {      return false;    }
00244     virtual vector3 Center()           {      return VZero;    }
00245     virtual vector3 CenterMol(OBMol &) {      return VZero;    }
00246   };
00247 
00248 } // end namespace OpenBabel
00249 
00250 #endif // OB_GRID_H
00251