Coverage Report

Created: 2025-08-26 06:55

/src/openbabel/include/openbabel/grid.h
Line
Count
Source (jump to first uncovered line)
1
/**********************************************************************
2
grid.h - Handle grids of values.
3
4
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5
Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
6
Some Portions Copyright (C) 2008 by Marcus D. Hanwell
7
8
This file is part of the Open Babel project.
9
For more information, see <http://openbabel.org/>
10
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published by
13
the Free Software Foundation version 2 of the License.
14
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
GNU General Public License for more details.
19
***********************************************************************/
20
21
#ifndef OB_GRID_H
22
#define OB_GRID_H
23
24
#include <openbabel/babelconfig.h>
25
#include <openbabel/math/vector3.h>
26
#include <openbabel/base.h>
27
28
// Necessary evil for 2.x series -- should use OBMol* below
29
#include <openbabel/mol.h>
30
31
#include <iosfwd>
32
#include <algorithm>
33
#include <vector>
34
#include <string>
35
36
namespace OpenBabel
37
{
38
39
  // Forward declaration
40
  class OBMol;
41
42
  //! \class OBGrid grid.h <openbabel/grid.h>
43
  //! \brief A base grid class
44
 class OBAPI OBGrid: public OBBase
45
  {
46
  protected:
47
    double _xmin,_xmax,_ymin,_ymax,_zmin,_zmax; //!< the min/max axes in XYZ axes (i.e., the box)
48
49
  public:
50
0
    OBGrid() {}
51
52
    //! \brief Initialize the grid based on a box around the molecule @p box
53
    //! Subclasses should overload this method -- this only tracks the
54
    //! dimension of the box itself
55
    virtual void Init(OBMol &box);
56
57
    //! \return the minimum x point of the grid
58
0
    double GetXmin() const    { return(_xmin);    }
59
    //! \return the minimum y point of the grid
60
0
    double GetYmin() const    { return(_ymin);    }
61
    //! \return the minimum z point of the grid
62
0
    double GetZmin() const    { return(_zmin);    }
63
    //! \return the maximum x point of the grid
64
0
    double GetXmax() const    { return(_xmax);    }
65
    //! \return the maximum y point of the grid
66
0
    double GetYmax() const    { return(_ymax);    }
67
    //! \return the maximum z point of the grid
68
0
    double GetZmax() const    { return(_zmax);    }
69
70
    //! \return whether the supplied XYZ coordinates fall within the box
71
    bool PointIsInBox(double x,double y,double z)
72
0
    {
73
0
      return (x>=_xmin) && (x<=_xmax) &&
74
0
        (y>=_ymin) && (y<=_ymax) &&
75
0
        (z>=_zmin) && (z<=_zmax);
76
0
    }
77
    //! \return true if the point falls within the box
78
    bool PointIsInBox(double *c)
79
0
    {
80
0
      return (c[0]>=_xmin) && (c[0]<=_xmax) &&
81
0
        (c[1]>=_ymin) && (c[1]<=_ymax) &&
82
0
        (c[2]>=_zmin) && (c[2]<=_zmax);
83
0
    }
84
85
    //! \return true if the point falls within the box
86
    bool PointIsInBox(vector3 v)
87
0
    {
88
0
      return (v.x() >= _xmin) && (v.x() <=_xmax) &&
89
0
      (v.y()>=_ymin) && (v.y()<=_ymax) &&
90
0
      (v.z()>=_zmin) && (v.z()<=_zmax);
91
0
    }
92
  };
93
94
  //! \class OBFloatGrid grid.h <openbabel/grid.h>
95
  //! \brief Handle double precision floating point 3D grids (e.g., charge density around an OBMol)
96
  //!
97
  //! Supports input/output and base functionality for simple 3D discrete grids
98
  //! of some function -- typically around a molecule. Typically you will want
99
  //! to use OBGridData which uses OBFloatGrid to store its data.
100
  //! \sa OBGridData
101
 class OBAPI OBFloatGrid: public OBGrid
102
  {
103
  protected:
104
    std::vector<double> _values;   //!< floating point values
105
    OB_DEPRECATED int   *_ival;    //!< for integer values \deprecated
106
    double _midz,_midx,_midy; //!< center of grid in world coordinates
107
    int _ydim,_xdim,_zdim;    //!< grid dimensions
108
    double _spacing,_inv_spa; //!< spacing between grid points and its inverse
109
    double _halfSpace;        //!< half of the grid spacing
110
    //! Three axes (i.e., translation vectors like a unit cell)
111
    vector3 _xAxis, _yAxis, _zAxis;
112
113
  public:
114
115
0
    OBFloatGrid() : _ival(nullptr), _halfSpace(0.0) {}
116
    ~OBFloatGrid()
117
0
    {
118
0
      if (_ival) delete [] _ival;
119
0
    }
120
121
    //! Initialize the grid using this molecule as a box (plus a padding)
122
    //! with the supplied spacing between points.
123
    void Init(OBMol &box,double spacing, double pad=0.0);
124
125
    //! \return the minimum point in the grid.
126
0
    vector3 GetMin() { return vector3(_xmin, _ymin, _zmin); }
127
128
    //! Get the minimum point in the grid.
129
    //! \deprecated Will be removed.
130
    //! Use \sa GetMin()
131
    OB_DEPRECATED_MSG("Use GetMin() instead")
132
    void GetMin(double *a)
133
0
    {
134
0
      a[0]=_xmin;
135
0
      a[1]=_ymin;
136
0
      a[2]=_zmin;
137
0
    }
138
139
    //! \return the minimum point in the grid.
140
0
    vector3 GetMax() { return vector3(_xmax, _ymax, _zmax); }
141
142
    //! Get the maximum point in the grid.
143
    //! \deprecated Will be removed.
144
    //! \sa GetMax()
145
    OB_DEPRECATED_MSG("Use GetMax() instead")
146
    void GetMax(double *a)
147
0
    {
148
0
      a[0]=_xmax;
149
0
      a[1]=_ymax;
150
0
      a[2]=_zmax;
151
0
    }
152
153
    //! \return The grid spacing.
154
0
    double GetSpacing() const { return(_spacing); }
155
    //! Get the grid spacing.
156
    //! \deprecated Will be removed.
157
    //! \sa GetSpacing()
158
    OB_DEPRECATED_MSG("Use GetSpacing() instead")
159
    void GetSpacing(double &s)
160
0
    {
161
0
      s=_spacing;
162
0
    }
163
    //! \return Inverse of the grid spacing.
164
0
    double GetScale() const   { return(_inv_spa); }
165
    //! \return Half of the spacing between grid points.
166
0
    double GetHalfSpace() const {return(_halfSpace);}
167
    //! \return Size of the grid in the x dimension.
168
0
    int GetXdim() const       { return(_xdim);    }
169
    //! \return Size of the grid in the y dimension.
170
0
    int GetYdim() const       { return(_ydim);    }
171
    //! \return Size of the grid in the z dimension.
172
0
    int GetZdim() const       { return(_zdim);    }
173
    //! Get the x, y and z dimensions (must pass an double[3] at least).
174
    //! \deprecated May be removed in future.
175
    //! \sa GetXdim() \sa GetYdim() \sa GetZdim()
176
    OB_DEPRECATED_MSG("Use GetXdim(), GetYdim() or GetZdim() instead")
177
    void GetDim(int *a)
178
0
    {
179
0
      a[0]=_xdim;
180
0
      a[1]=_ydim;
181
0
      a[2]=_zdim;
182
0
    }
183
184
    //! \return Position of the center of the grid.
185
    vector3 GetMidpointVector()
186
0
    {
187
0
      vector3 v;
188
0
      v.Set(_midx,_midy,_midz);
189
0
      return(v);
190
0
    }
191
192
    //! \return X axis direction.
193
    vector3 GetXAxis() const
194
0
    {
195
0
      return _xAxis;
196
0
    }
197
198
    //! \return Y axis direction.
199
    vector3 GetYAxis() const
200
0
    {
201
0
      return _yAxis;
202
0
    }
203
204
    //! \return Z axis direction.
205
    vector3 GetZAxis() const
206
0
    {
207
0
      return _zAxis;
208
0
    }
209
210
    //! Sets the number of points in the x, y and z directions.
211
    void SetNumberOfPoints(int nx, int ny, int nz);
212
213
    //! Set the direction of the x axis.
214
    void SetXAxis(vector3);
215
    //! Set the direction of the y axis.
216
    void SetYAxis(vector3);
217
    //! Set the direction of the z axis.
218
    void SetZAxis(vector3);
219
220
    //! Set the limits (i.e., the origin point and the axes)
221
    //! NOTE: You must set the number of points first,
222
    //!       with SetNumberOfPoints
223
    //!       so the grid spacing can be calculated
224
    void SetLimits(const vector3& origin, const vector3& x, const vector3& y,
225
                   const vector3& z);
226
    //! \deprecated Will be removed.
227
    //! \sa SetLimits(const vector3& origin, const vector3& x, const vector3& y, const vector3& z)
228
    OB_DEPRECATED_MSG("Use vector version instead")
229
    void SetLimits(const double origin[3], const double x[3], const double y[3],
230
                   const double z[3]);
231
232
    //! Get a copy of the vector that stores the points in the grid.
233
    std::vector<double> GetDataVector();
234
    //! Set the values in the grid to those in the vector passed. Note that the
235
    //! vector must be of the same dimensions as the grid based on the values
236
    //! given in SetNumberOfPoints(int nx, int ny, int nz).
237
    void SetVals(const std::vector<double> & vals);
238
239
    //! \return Pointer to the first element of the grid point data stored as a
240
    //! one dimensional array.
241
    //! \deprecated Will be removed.
242
    //! \sa GetDataVector()
243
    OB_DEPRECATED_MSG("Use GetDataVector instead")
244
0
    double *GetVals()    {        return(&_values[0]);    }
245
246
    //! \return Value at the point in the grid specified by i, j and k.
247
    double GetValue(int i, int j, int k)
248
0
    {
249
0
      if (i*_ydim*_zdim + j*_zdim + k > _xdim*_ydim*_zdim)
250
0
        return 0.0;
251
0
      else
252
0
        return _values[i*_ydim*_zdim + j*_zdim + k];
253
0
    }
254
255
    //! \deprecated Will be removed.
256
    //! \sa SetVals(const std::vector<double> & vals)
257
    OB_DEPRECATED_MSG("Use vector version instead")
258
    void SetVals(double *ptr)
259
0
    {
260
0
     for (int i = 0; i < _xdim*_ydim*_zdim; ++i)
261
0
       _values[i] = ptr[i];
262
0
    }
263
264
    //! Set the value at the grid point specified by i, j and k to val.
265
    bool SetValue(int i, int j, int k, double val)
266
0
    {
267
0
      if (i*_ydim*_zdim + j*_zdim + k > _xdim*_ydim*_zdim)
268
0
        return false;
269
270
0
      _values[i*_ydim*_zdim + j*_zdim + k] = val;
271
0
      return true;
272
0
    }
273
274
    //! \return Position of the center of the grid.
275
    vector3 Center()
276
0
    {
277
0
      return vector3(_midx,_midy,_midz);
278
0
    }
279
280
    friend std::ostream& operator<< ( std::ostream&, const OBFloatGrid& ) ;
281
    friend std::istream& operator>> ( std::istream&,OBFloatGrid& ) ;
282
283
    //! \return the value at the given point (rounding as needed)
284
    double Inject(double x,double y,double z);
285
    void IndexToCoords(int idx, double &x, double &y, double &z);
286
    void CoordsToIndex(int*,double*);
287
    int CoordsToIndex(double x, double y, double z);
288
    //! \return the interpolated value for the given point
289
    double Interpolate(double,double,double);
290
    //! \return the interpolated value for the given point and set the dx, dy, dz derivatives
291
    double InterpolateDerivatives(double,double,double,double *derivatives);
292
  };
293
294
#ifndef OBPolarGrid
295
#define OBPolarGrid 0x01 /* polar interactions? */
296
#endif //OBPolarGrid
297
298
#ifndef OBLipoGrid
299
#define OBLipoGrid 0x02 /* lipophilicity? */
300
#endif //OBLipoGrid
301
302
  //! \class OBProxGrid grid.h <openbabel/grid.h>
303
  //! \brief A grid for determining the proximity of a given point to atoms in an OBMol
304
  //! \deprecated May be removed in the future, since docking is not a key feature
305
 class OBAPI OB_DEPRECATED OBProxGrid: public OBGrid
306
  {
307
  protected:
308
    int _gridtype;
309
    int _nxinc,_nyinc,_nzinc,_maxinc;
310
    double _inc;
311
    std::vector<std::vector<int> > cell;
312
313
  public:
314
315
    OBProxGrid(int gridtype=0)
316
0
      {
317
0
        _gridtype=gridtype;
318
0
      }
319
    ~OBProxGrid()
320
0
      {}
321
    void Setup(OBMol &mol,OBMol &box, double cutoff,double resolution = 0.5);
322
    void Setup(OBMol &mol,OBMol &box, double cutoff,
323
               std::vector<bool> &use,double resolution = 0.5);
324
    std::vector<int> *GetProxVector(double,double,double);
325
    std::vector<int> *GetProxVector(double*);
326
327
    bool LipoGrid()
328
0
    {
329
0
      return((_gridtype&OBLipoGrid) ? true : false);
330
0
    }
331
    bool PolarGrid()
332
0
    {
333
0
      return(_gridtype&OBPolarGrid);
334
0
    }
335
    void SetGridType(int gridtype)
336
0
    {
337
0
      _gridtype = gridtype;
338
0
    }
339
  };
340
341
  // scoring function used: PLP = Piecewise Linear Potential or ChemScore algorithm
342
  typedef enum { Undefined = -1, PLP, ChemScore } score_t;
343
344
  //! \class OBScoreGrid grid.h <openbabel/grid.h>
345
  //! \brief A base class for scoring docking interactions between multiple molecules
346
  //! \deprecated Will disappear in future versions. Use your own code.
347
  class OBAPI OB_DEPRECATED OBScoreGrid
348
  {
349
  protected:
350
    score_t gridtype;
351
    bool verbose;
352
353
  public:
354
355
    double score;
356
357
0
    OBScoreGrid(void)                 {  verbose = false;      }
358
0
    virtual ~OBScoreGrid(void) {}
359
360
0
    void    SetVerbose(bool v)        {      verbose = v;      }
361
0
    void    SetType(score_t type)     {      gridtype = type;  }
362
0
    score_t GetType(void)             {    return gridtype;    }
363
364
0
    virtual void   Clear(void)        { }
365
0
    virtual double  Eval(double *)    {       return -1;       }
366
0
    virtual double  Eval(OBMol &mol){return Eval(mol.GetCoordinates());}
367
0
    virtual void   Init(OBMol &, OBMol &, std::string &, double){}
368
0
    virtual void   Setup(OBMol &) {}
369
0
    virtual void   Setup(OBMol &, std::vector<int> &){}
370
0
    virtual void   Setup(std::vector<int> &) {}
371
0
    virtual void   Config(std::string) {}
372
0
    virtual bool   Read(std::string)   {      return false;    }
373
0
    virtual bool   Write(std::string)  {      return false;    }
374
0
    virtual vector3 Center()           {      return VZero;    }
375
0
    virtual vector3 CenterMol(OBMol &) {      return VZero;    }
376
  };
377
378
} // end namespace OpenBabel
379
380
#endif // OB_GRID_H
381
382
//! \file grid.h
383
//! \brief Handle grids of values.