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" 15 #include <DataStructs/DiscreteValueVect.h> 16 #include "Grid3D.h" 17 #include <iostream> 18 19 namespace RDGeom { 20 class RDKIT_RDGEOMETRYLIB_EXPORT UniformGrid3D : public Grid3D { 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, 39 RDKit::DiscreteValueVect::DiscreteValueType valType = 40 RDKit::DiscreteValueVect::TWOBITVALUE, 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 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) getSize()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 ¢er, 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 getNumX()118 unsigned int getNumX() const { return d_numX; }; 119 120 //! \brief get the number of grid points along y-axis getNumY()121 unsigned int getNumY() const { return d_numY; }; 122 123 //! \brief get the number of grid points along z-axis getNumZ()124 unsigned int getNumZ() const { return d_numZ; }; 125 126 //! \brief get the grid's offset getOffset()127 const Point3D &getOffset() const { return d_offSet; }; 128 129 //! \brief get the grid's spacing getSpacing()130 double getSpacing() const { return d_spacing; }; 131 132 //! \brief return a \b const pointer to our occupancy vector getOccupancyVect()133 const RDKit::DiscreteValueVect *getOccupancyVect() const { 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, 188 RDKit::DiscreteValueVect::DiscreteValueType valType, 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) 195 RDKit::DiscreteValueVect 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 218