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 = 0) {
42  if (offset == 0) {
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
50  UniformGrid3D(const UniformGrid3D &other);
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);
55  UniformGrid3D &operator=(const UniformGrid3D &other);
56 
57  ~UniformGrid3D();
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;
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;
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;
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);
78 
79  //! \brief get the location of the specified grid point
80  Point3D getGridPointLoc(unsigned int pointId) const;
81 
82  //! \brief Set the value at the specified grid point
83  void setVal(unsigned int pointId, unsigned int val);
84 
85  //! \brief get the size of the grid (number of grid points)
86  unsigned int getSize() const { 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
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.
144  UniformGrid3D &operator|=(const UniformGrid3D &other);
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.
149  UniformGrid3D &operator&=(const UniformGrid3D &other);
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.
154  UniformGrid3D &operator+=(const UniformGrid3D &other);
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.
159  UniformGrid3D &operator-=(const UniformGrid3D &other);
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 = 0);
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 */
206 RDKIT_RDGEOMETRYLIB_EXPORT void writeGridToStream(const UniformGrid3D &grid,
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 */
213 RDKIT_RDGEOMETRYLIB_EXPORT void writeGridToFile(const UniformGrid3D &grid,
214  const std::string &filename);
215 } // namespace RDGeom
216 
217 #endif
RDKit::DiscreteValueVect::DiscreteValueType
DiscreteValueType
used to define the possible range of the values
Definition: DiscreteValueVect.h:29
point.h
DiscreteValueVect.h
RDGeom
Definition: TorsionAngleM6.h:20
RDGeom::Point3D
Definition: point.h:46
RDGeom::UniformGrid3D::getOccupancyVect
const RDKit::DiscreteValueVect * getOccupancyVect() const
return a const pointer to our occupancy vector
Definition: UniformGrid3D.h:133
RDKIT_RDGEOMETRYLIB_EXPORT
#define RDKIT_RDGEOMETRYLIB_EXPORT
Definition: export.h:515
RDGeom::UniformGrid3D::getNumX
unsigned int getNumX() const
get the number of grid points along x-axis
Definition: UniformGrid3D.h:118
RDGeom::UniformGrid3D::UniformGrid3D
UniformGrid3D(double dimX, double dimY, double dimZ, double spacing=0.5, RDKit::DiscreteValueVect::DiscreteValueType valType=RDKit::DiscreteValueVect::TWOBITVALUE, const RDGeom::Point3D *offset=0)
ctor
Definition: UniformGrid3D.h:38
RDKit::DiscreteValueVect
a class for efficiently storing vectors of discrete values
Definition: DiscreteValueVect.h:24
RDGeom::Grid3D
Virtual base class for a grid object.
Definition: Grid3D.h:37
RDGeom::UniformGrid3D::operator&
UniformGrid3D operator&(const UniformGrid3D &other) const
Definition: UniformGrid3D.h:164
RDGeom::UniformGrid3D::getSpacing
double getSpacing() const
get the grid's spacing
Definition: UniformGrid3D.h:130
Grid3D.h
RDGeom::UniformGrid3D::getOffset
const Point3D & getOffset() const
get the grid's offset
Definition: UniformGrid3D.h:127
PRECONDITION
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
RDGeom::UniformGrid3D::getNumZ
unsigned int getNumZ() const
get the number of grid points along z-axis
Definition: UniformGrid3D.h:124
RDKit::DiscreteValueVect::TWOBITVALUE
@ TWOBITVALUE
Definition: DiscreteValueVect.h:31
RDGeom::UniformGrid3D::getSize
unsigned int getSize() const
get the size of the grid (number of grid points)
Definition: UniformGrid3D.h:86
RDGeom::writeGridToFile
RDKIT_RDGEOMETRYLIB_EXPORT void writeGridToFile(const UniformGrid3D &grid, const std::string &filename)
writes the contents of the grid to a named file
RDGeom::writeGridToStream
RDKIT_RDGEOMETRYLIB_EXPORT void writeGridToStream(const UniformGrid3D &grid, std::ostream &outStrm)
writes the contents of the grid to a stream
RDGeom::UniformGrid3D
Definition: UniformGrid3D.h:20
RDGeom::UniformGrid3D::getNumY
unsigned int getNumY() const
get the number of grid points along y-axis
Definition: UniformGrid3D.h:121
export.h