UniformGrid3D.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005-2006 Rational Discovery LLC
00003 //
00004 //   @@ All Rights Reserved @@
00005 //  This file is part of the RDKit.
00006 //  The contents are covered by the terms of the BSD license
00007 //  which is included in the file license.txt, found at the root
00008 //  of the RDKit source tree.
00009 //
00010 #ifndef _UNIFORMGRID3D_H_20050124_1703
00011 #define _UNIFORMGRID3D_H_20050124_1703
00012 
00013 #include "point.h"
00014 #include <DataStructs/DiscreteValueVect.h>
00015 #include "Grid3D.h"
00016 #include <iostream>
00017 namespace RDGeom {
00018   class UniformGrid3D : public Grid3D {
00019   
00020   public:
00021        
00022     //! \brief ctor
00023     /*
00024         \param dimX:    the x dimension of the grid, in Angstroms
00025         \param dimY:    the y dimension of the grid, in Angstroms
00026         \param dimZ:    the z dimension of the grid, in Angstroms
00027         \param spacing: the grid spacing, in Angstroms
00028         \param valType: the data type of the grid (determines the number of bits
00029                         per point)
00030         \param offset:  OPTIONAL: the offset of the grid from (0,0,0), in Angstroms.
00031 
00032         \b Note: the values of arguments such as \c dimX and \c spacing
00033         don't actually need to be in Angstroms, but they should be internally
00034         consistent.
00035         
00036     */
00037     UniformGrid3D(double dimX, double dimY, double dimZ, double spacing=0.5,
00038                   RDKit::DiscreteValueVect::DiscreteValueType valType=RDKit::DiscreteValueVect::TWOBITVALUE,
00039                   const RDGeom::Point3D *offset=0){
00040       if (offset == 0) {
00041         initGrid(dimX, dimY, dimZ, spacing, valType,
00042                  Point3D(-0.5*dimX, -0.5*dimY, -0.5*dimZ));
00043       } else {
00044         initGrid(dimX, dimY, dimZ, spacing, valType, *offset);
00045       }
00046     }
00047     //! copy ctor
00048     UniformGrid3D(const UniformGrid3D &other);
00049     //! construct from a string pickle
00050     UniformGrid3D(const std::string pkl);
00051     //! construct from a text pickle
00052     UniformGrid3D(const char *pkl,unsigned int);
00053 
00054     ~UniformGrid3D();
00055 
00056     //! \brief Get the index of the grid point closest to point
00057     //!
00058     //! \return the integer index, -1 if the specified point is outside the grid
00059     int getGridPointIndex(const Point3D &point) const;
00060 
00061     //! \brief Get the value at the grid point closest to the specified point
00062     //!
00063     //! \return the integer value, -1 if the specified index is outside the grid
00064     int getVal(const Point3D &point) const;
00065 
00066     //! \brief Get the value at a specified grid point
00067     //!
00068     //! \return the unsigned integer value
00069     unsigned int getVal(unsigned int pointId) const;
00070 
00071     //! \brief Set the value at the grid point closest to the specified point
00072     //!
00073     //! doesn't do anything if the point is outside the grid 
00074     void setVal(const Point3D &point, unsigned int val);
00075 
00076     //! \brief get the location of the specified grid point
00077     Point3D getGridPointLoc(unsigned int pointId) const;
00078 
00079     //! \brief Set the value at the specified grid point 
00080     void setVal(unsigned int pointId, unsigned int val);
00081 
00082     //! \brief get the size of the grid (number of grid points)
00083     unsigned int getSize() const { return d_numX*d_numY*d_numZ; };
00084 
00085     //! \brief set the occupancy for a multi-layered sphere
00086     /*!
00087       This function encodes the occupancy for a sphere and multiple layers around it
00088       \param center              location of the sphere center
00089       \param radius              Radius of the base sphere
00090       \param stepSize            thickness of each layer on top of the base sphere
00091       \param maxNumLayers        maximum number of layers, if -1 this is determined by
00092                                  the number of bits used per grid points in the storage
00093       \param ignoreOutOfBound    if true, ignore if center is outside the grid, otherwise throw
00094                                  an exception
00095       
00096     */
00097     void setSphereOccupancy(const Point3D & center, double radius, 
00098                             double stepSize, int maxNumLayers=-1, 
00099                             bool ignoreOutOfBound=true);
00100 
00101     //! \brief get the index of the grid point given the x, y, z indices
00102     //!
00103     //! \return the integer value, -1 if the indices are outside the grid
00104     int getGridIndex(unsigned int xi, unsigned int yi, unsigned int zi) const;
00105 
00106     //! \brief get the number of grid points along x-axis
00107     unsigned int getNumX() const { return d_numX; };
00108 
00109     //! \brief get the number of grid points along y-axis
00110     unsigned int getNumY() const { return d_numY; };
00111 
00112     //! \brief get the number of grid points along z-axis
00113     unsigned int getNumZ() const { return d_numZ; };
00114 
00115     //! \brief get the grid's offset
00116     const Point3D &getOffset() const { return d_offSet; };
00117 
00118     //! \brief get the grid's spacing
00119     double getSpacing() const { return d_spacing; };
00120 
00121     //! \brief return a \b const pointer to our occupancy vector
00122     const RDKit::DiscreteValueVect *getOccupancyVect() const { return dp_storage;} ;
00123 
00124     //! \brief returns true if the grid \c other has parameters
00125     //!        compatible with ours.
00126     virtual bool compareParams(const UniformGrid3D &other) const;
00127     //! \brief calculates the union between the data on this grid and
00128     //!  that on \c other.
00129     //!  This grid is modified.
00130     //!  NOTE that the grids must have the same parameters.   
00131     UniformGrid3D &operator|=(const UniformGrid3D &other);
00132     //! \brief calculates the intersection between the data on this grid and
00133     //!  that on \c other.
00134     //!  This grid is modified.
00135     //!  NOTE that the grids must have the same parameters.   
00136     UniformGrid3D &operator&=(const UniformGrid3D &other);
00137     //! \brief calculates the sum of the data on this grid and
00138     //!  that on \c other.
00139     //!  This grid is modified.
00140     //!  NOTE that the grids must have the same parameters.   
00141     UniformGrid3D &operator+=(const UniformGrid3D &other);
00142     //! \brief calculates the difference between the data on this grid and
00143     //!  that on \c other.
00144     //!  This grid is modified.
00145     //!  NOTE that the grids must have the same parameters.   
00146     UniformGrid3D &operator-=(const UniformGrid3D &other);
00147 
00148     //! \brief create and return a pickle
00149     std::string toString() const;
00150 
00151     UniformGrid3D operator& (const UniformGrid3D &other) const{
00152       PRECONDITION(dp_storage,"bad storage");
00153       PRECONDITION(compareParams(other),"mismatched params");
00154       UniformGrid3D res(d_numX*d_spacing,d_numY*d_spacing,d_numZ*d_spacing,
00155                         d_spacing,dp_storage->getValueType(),&d_offSet);
00156       return res;
00157     };
00158     
00159   private:
00160     //! \brief internal initialization code
00161     /*
00162         \param dimX:    the x dimension of the grid, in Angstroms
00163         \param dimY:    the y dimension of the grid, in Angstroms
00164         \param dimZ:    the z dimension of the grid, in Angstroms
00165         \param spacing: the grid spacing, in Angstroms
00166         \param valType: the data type of the grid (determines the number of bits
00167                         per point)
00168         \param offset:  the offset of the grid from (0,0,0), in Angstroms.
00169         \param data:    (optional) a pointer to a DiscreteValueVect to use, we take 
00170                         ownership of the pointer.
00171     */
00172     void initGrid(double dimX, double dimY, double dimZ, double spacing,
00173                   RDKit::DiscreteValueVect::DiscreteValueType valType,
00174                   const RDGeom::Point3D &offSet,RDKit::DiscreteValueVect *data=0);
00175     unsigned int d_numX, d_numY, d_numZ; //! number of grid points along x, y, z axes
00176     double d_spacing; //! grid spacing
00177     Point3D d_offSet; //! the grid offset (from the origin)
00178     RDKit::DiscreteValueVect *dp_storage; //! storage for values at each grid point
00179 
00180     //! \brief construct from a pickle
00181     void initFromText(const char *pkl,const unsigned int length);
00182 
00183   };
00184 
00185   //! \brief writes the contents of the grid to a stream
00186   /*
00187     The grid is written in GRD format
00188   */
00189   void writeGridToStream(const UniformGrid3D &grid, std::ostream &outStrm);
00190 
00191   //! \brief writes the contents of the grid to a named file
00192   /*
00193     The grid is written in GRD format
00194   */
00195   void writeGridToFile(const UniformGrid3D &grid, std::string filename);
00196 
00197 }
00198 
00199 #endif
00200