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