RDKit
Open-source cheminformatics and machine learning.
UniformGrid3D.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005-2013 Greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #include <RDGeneral/export.h>
11 #ifndef _UNIFORMGRID3D_H_20050124_1703
12 #define _UNIFORMGRID3D_H_20050124_1703
13 
14 #include "point.h"
16 #include "Grid3D.h"
17 #include <iostream>
18 
19 namespace RDGeom {
21  public:
22  //! \brief ctor
23  /*
24  \param dimX: the x dimension of the grid, in Angstroms
25  \param dimY: the y dimension of the grid, in Angstroms
26  \param dimZ: the z dimension of the grid, in Angstroms
27  \param spacing: the grid spacing, in Angstroms
28  \param valType: the data type of the grid (determines the number of bits
29  per point)
30  \param offset: OPTIONAL: the offset of the grid from (0,0,0), in
31  Angstroms.
32 
33  \b Note: the values of arguments such as \c dimX and \c spacing
34  don't actually need to be in Angstroms, but they should be internally
35  consistent.
36 
37  */
38  UniformGrid3D(double dimX, double dimY, double dimZ, double spacing = 0.5,
41  const RDGeom::Point3D *offset = nullptr) {
42  if (offset == nullptr) {
43  initGrid(dimX, dimY, dimZ, spacing, valType,
44  Point3D(-0.5 * dimX, -0.5 * dimY, -0.5 * dimZ));
45  } else {
46  initGrid(dimX, dimY, dimZ, spacing, valType, *offset);
47  }
48  }
49  //! copy ctor
51  //! construct from a string pickle
52  UniformGrid3D(const std::string &pkl);
53  //! construct from a text pickle
54  UniformGrid3D(const char *pkl, unsigned int);
56 
57  ~UniformGrid3D() override;
58 
59  //! \brief Get the index of the grid point closest to point
60  //!
61  //! \return the integer index, -1 if the specified point is outside the grid
62  int getGridPointIndex(const Point3D &point) const override;
63 
64  //! \brief Get the value at the grid point closest to the specified point
65  //!
66  //! \return the integer value, -1 if the specified index is outside the grid
67  int getVal(const Point3D &point) const override;
68 
69  //! \brief Get the value at a specified grid point
70  //!
71  //! \return the unsigned integer value
72  unsigned int getVal(unsigned int pointId) const override;
73 
74  //! \brief Set the value at the grid point closest to the specified point
75  //!
76  //! doesn't do anything if the point is outside the grid
77  void setVal(const Point3D &point, unsigned int val) override;
78 
79  //! \brief get the location of the specified grid point
80  Point3D getGridPointLoc(unsigned int pointId) const override;
81 
82  //! \brief Set the value at the specified grid point
83  void setVal(unsigned int pointId, unsigned int val) override;
84 
85  //! \brief get the size of the grid (number of grid points)
86  unsigned int getSize() const override { return d_numX * d_numY * d_numZ; }
87 
88  //! \brief set the occupancy for a multi-layered sphere
89  /*!
90  This function encodes the occupancy for a sphere and multiple layers around
91  it
92  \param center location of the sphere center
93  \param radius Radius of the base sphere
94  \param stepSize thickness of each layer on top of the base sphere
95  \param maxNumLayers maximum number of layers, if -1 this is
96  determined by
97  the number of bits used per grid points in the
98  storage
99  \param ignoreOutOfBound if true, ignore if center is outside the grid,
100  otherwise throw
101  an exception
102 
103  */
104  void setSphereOccupancy(const Point3D &center, double radius, double stepSize,
105  int maxNumLayers = -1, bool ignoreOutOfBound = true);
106 
107  //! \brief get the index of the grid point given the x, y, z indices
108  //!
109  //! \return the integer value, -1 if the indices are outside the grid
110  int getGridIndex(unsigned int xi, unsigned int yi, unsigned int zi) const;
111 
112  //! \brief get the x, y, and z indices of a grid-point index
113  //!
114  void getGridIndices(unsigned int idx, unsigned int &xi, unsigned int &yi,
115  unsigned int &zi) const;
116 
117  //! \brief get the number of grid points along x-axis
118  unsigned int getNumX() const { return d_numX; }
119 
120  //! \brief get the number of grid points along y-axis
121  unsigned int getNumY() const { return d_numY; }
122 
123  //! \brief get the number of grid points along z-axis
124  unsigned int getNumZ() const { return d_numZ; }
125 
126  //! \brief get the grid's offset
127  const Point3D &getOffset() const { return d_offSet; }
128 
129  //! \brief get the grid's spacing
130  double getSpacing() const { return d_spacing; }
131 
132  //! \brief return a \b const pointer to our occupancy vector
133  const RDKit::DiscreteValueVect *getOccupancyVect() const override {
134  return dp_storage;
135  }
136 
137  //! \brief returns true if the grid \c other has parameters
138  //! compatible with ours.
139  virtual bool compareParams(const UniformGrid3D &other) const;
140  //! \brief calculates the union between the data on this grid and
141  //! that on \c other.
142  //! This grid is modified.
143  //! NOTE that the grids must have the same parameters.
145  //! \brief calculates the intersection between the data on this grid and
146  //! that on \c other.
147  //! This grid is modified.
148  //! NOTE that the grids must have the same parameters.
150  //! \brief calculates the sum of the data on this grid and
151  //! that on \c other.
152  //! This grid is modified.
153  //! NOTE that the grids must have the same parameters.
155  //! \brief calculates the difference between the data on this grid and
156  //! that on \c other.
157  //! This grid is modified.
158  //! NOTE that the grids must have the same parameters.
160 
161  //! \brief create and return a pickle
162  std::string toString() const;
163 
164  UniformGrid3D operator&(const UniformGrid3D &other) const {
165  PRECONDITION(dp_storage, "bad storage");
166  PRECONDITION(compareParams(other), "mismatched params");
167  UniformGrid3D res(d_numX * d_spacing, d_numY * d_spacing,
168  d_numZ * d_spacing, d_spacing, dp_storage->getValueType(),
169  &d_offSet);
170  return res;
171  }
172 
173  private:
174  //! \brief internal initialization code
175  /*
176  \param dimX: the x dimension of the grid, in Angstroms
177  \param dimY: the y dimension of the grid, in Angstroms
178  \param dimZ: the z dimension of the grid, in Angstroms
179  \param spacing: the grid spacing, in Angstroms
180  \param valType: the data type of the grid (determines the number of bits
181  per point)
182  \param offset: the offset of the grid from (0,0,0), in Angstroms.
183  \param data: (optional) a pointer to a DiscreteValueVect to use, we
184  take
185  ownership of the pointer.
186  */
187  void initGrid(double dimX, double dimY, double dimZ, double spacing,
189  const RDGeom::Point3D &offSet,
190  RDKit::DiscreteValueVect *data = nullptr);
191  unsigned int d_numX, d_numY,
192  d_numZ; //! number of grid points along x, y, z axes
193  double d_spacing; //! grid spacing
194  Point3D d_offSet; //! the grid offset (from the origin)
196  *dp_storage; //! storage for values at each grid point
197 
198  //! \brief construct from a pickle
199  void initFromText(const char *pkl, const unsigned int length);
200 };
201 
202 //! \brief writes the contents of the grid to a stream
203 /*
204  The grid is written in GRD format
205 */
207  std::ostream &outStrm);
208 
209 //! \brief writes the contents of the grid to a named file
210 /*
211  The grid is written in GRD format
212 */
214  const std::string &filename);
215 } // namespace RDGeom
216 
217 #endif
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
Virtual base class for a grid object.
Definition: Grid3D.h:38
unsigned int getNumY() const
get the number of grid points along y-axis
UniformGrid3D operator&(const UniformGrid3D &other) const
int getVal(const Point3D &point) const override
Get the value at the grid point closest to the specified point.
void setSphereOccupancy(const Point3D &center, double radius, double stepSize, int maxNumLayers=-1, bool ignoreOutOfBound=true)
set the occupancy for a multi-layered sphere
UniformGrid3D(const char *pkl, unsigned int)
construct from a text pickle
UniformGrid3D(const std::string &pkl)
construct from a string pickle
const Point3D & getOffset() const
get the grid's offset
int getGridPointIndex(const Point3D &point) const override
Get the index of the grid point closest to point.
Point3D getGridPointLoc(unsigned int pointId) const override
get the location of the specified grid point
void setVal(const Point3D &point, unsigned int val) override
Set the value at the grid point closest to the specified point.
UniformGrid3D & operator=(const UniformGrid3D &other)
UniformGrid3D(double dimX, double dimY, double dimZ, double spacing=0.5, RDKit::DiscreteValueVect::DiscreteValueType valType=RDKit::DiscreteValueVect::TWOBITVALUE, const RDGeom::Point3D *offset=nullptr)
ctor
Definition: UniformGrid3D.h:38
UniformGrid3D & operator&=(const UniformGrid3D &other)
calculates the intersection between the data on this grid and that on other. This grid is modified....
unsigned int getSize() const override
get the size of the grid (number of grid points)
Definition: UniformGrid3D.h:86
double getSpacing() const
get the grid's spacing
~UniformGrid3D() override
UniformGrid3D & operator+=(const UniformGrid3D &other)
calculates the sum of the data on this grid and that on other. This grid is modified....
int getGridIndex(unsigned int xi, unsigned int yi, unsigned int zi) const
get the index of the grid point given the x, y, z indices
unsigned int getNumX() const
get the number of grid points along x-axis
void getGridIndices(unsigned int idx, unsigned int &xi, unsigned int &yi, unsigned int &zi) const
get the x, y, and z indices of a grid-point index
UniformGrid3D & operator|=(const UniformGrid3D &other)
calculates the union between the data on this grid and that on other. This grid is modified....
UniformGrid3D & operator-=(const UniformGrid3D &other)
calculates the difference between the data on this grid and that on other. This grid is modified....
unsigned int getNumZ() const
get the number of grid points along z-axis
unsigned int getVal(unsigned int pointId) const override
Get the value at a specified grid point.
virtual bool compareParams(const UniformGrid3D &other) const
returns true if the grid other has parameters compatible with ours.
UniformGrid3D(const UniformGrid3D &other)
copy ctor
void setVal(unsigned int pointId, unsigned int val) override
Set the value at the specified grid point.
std::string toString() const
create and return a pickle
const RDKit::DiscreteValueVect * getOccupancyVect() const override
return a const pointer to our occupancy vector
a class for efficiently storing vectors of discrete values
DiscreteValueType
used to define the possible range of the values
#define RDKIT_RDGEOMETRYLIB_EXPORT
Definition: export.h:369
RDKIT_RDGEOMETRYLIB_EXPORT void writeGridToFile(const UniformGrid3D &grid, const std::string &filename)
writes the contents of the grid to a named file
RDKIT_RDGEOMETRYLIB_EXPORT void writeGridToStream(const UniformGrid3D &grid, std::ostream &outStrm)
writes the contents of the grid to a stream