IMP  2.1.0
The Integrative Modeling Platform
ProjectionMask.h
Go to the documentation of this file.
1 /**
2  * \file ProjectionMask.h
3  * \brief projection masks
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_PROJECTION_MASK_H
8 #define IMPEM2D_PROJECTION_MASK_H
9 
10 #include "IMP/em2d/em2d_config.h"
12 #include "IMP/em2d/CenteredMat.h"
13 #include "IMP/atom/Mass.h"
14 #include "IMP/em/exp.h"
15 #include "IMP/algebra/Vector3D.h"
16 #include "IMP/algebra/Vector2D.h"
17 #include "IMP/algebra/Rotation3D.h"
20 #include "IMP/core/XYZR.h"
21 #include "IMP/Particle.h"
22 #include "IMP/base/exception.h"
23 #include <complex>
24 #include <boost/shared_ptr.hpp>
25 
26 IMPEM2D_BEGIN_NAMESPACE
27 
28 class ProjectionMask;
29 class MasksManager;
30 
31 typedef boost::shared_ptr<ProjectionMask> ProjectionMaskPtr;
32 typedef boost::shared_ptr<MasksManager> MasksManagerPtr;
33 
34 
35 //! Mask that contains the projection of a given particles. This matrices
36 //! speed up projecting because the only have to be computed once for a model
37 class IMPEM2DEXPORT ProjectionMask {
38 #ifdef SWIG
39  ProjectionMask(){}
40 #endif
41 public:
42 #if !defined(DOXYGEN) && !defined(SWIG)
45  double voxelsize,
46  double mass =1.0);
47 
48  //! Generates the mask
49  /*!
50  \param[in] KP Kernel parameteres to employ. See the EM module
51  \param[in] params Kernel parameteres associated with radius to employ
52  \param[in] mass Mass to give to the mask
53  */
54  void create(const em::KernelParameters &KP,
56  double mass = 1.0);
57 #endif
58 
59  //! Adds the values of the mask to a matrix at the given pixel
60  /*!
61  \param[out] m matrix where the values of the mask are added.
62  \param[in] v pixel where to apply the values. Currently used as integer.
63  */
64  void apply(cv::Mat &m,
65  const algebra::Vector2D &v);
66 
67  void show(std::ostream &out = std::cout) const;
68 
69  ~ProjectionMask();
70 
71 protected:
72  int dim_; // dimension of the mask
73  double sq_pixelsize_; // Used to save multiplications
74  cv::Mat data_; // actual matrix with the mask
75 };
76 
78 
79 
80 
81 
82 // Place a matrix in another
83 // Very ugly but very fast projecting function
84 
85 /*!
86  \param[in] mask matrix to place in m
87  \param[in] m matrix
88  \param[in] v Pixel of the matrix dest where the center of m is put.
89 */
90 inline
91 void do_place(cv::Mat &mask, cv::Mat &m,
92  const algebra::Vector2D &v) {
93 
94  // v is the vector of coordinates respect to the center of the matrix m
95  int vi= algebra::get_rounded(v[0]);
96  int vj= algebra::get_rounded(v[1]);
97 
98  // Centers for the matrix
99  int center[2];
100  center[0] = static_cast<int>(0.5*m.rows);
101  center[1] = static_cast<int>(0.5*m.cols);
102 
103  int start[2], end[2];
104  start[0] = -center[0];
105  start[1] = -center[1];
106  end[0]=m.rows - 1 - center[0];
107  end[1]=m.cols - 1 - center[1];
108 
109  // Check range: If the vector is outside the matrix, don't do anything.
110  if(vi < start[0] || vi > end[0]) return;
111  if(vj < start[1] || vj > end[1]) return;
112 
113 
114  // Centers for the mask
115  int mcenter[2];
116  mcenter[0] = static_cast<int>(0.5*mask.rows);
117  mcenter[1] = static_cast<int>(0.5*mask.cols);
118 
119  int mstart[2], mend[2];
120  mstart[0] = -mcenter[0];
121  mstart[1] = -mcenter[1];
122  mend[0] = mask.rows - 1 - mcenter[0];
123  mend[1] = mask.cols - 1 - mcenter[1];
124 
125  // Get the admisible range for the mask
126  int start_i = std::max(start[0] - vi, mstart[0]);
127  int start_j = std::max(start[1] - vj, mstart[1]);
128  int end_i = std::min(end[0] - vi, mend[0]);
129  int end_j = std::min(end[1] - vj, mend[1]);
130 
131 
132  int row = vi+center[0];
133  int col = vj+center[1];
134 
135  for(int i = start_i; i <= end_i; ++i) {
136  int p = i+row;
137  for(int j = start_j; j <= end_j; ++j) {
138  m.at<double>(p, j+col) += mask.at<double>(i+mcenter[0], j+mcenter[1]);
139  }
140  }
141 }
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 //! Manage of projection masks
157 class IMPEM2DEXPORT MasksManager {
158 public:
159  MasksManager() {
160  is_setup_ = false;
161  };
162 
163  MasksManager(double resolution,double pixelsize) {
164  setup_kernel(resolution,pixelsize);
165  }
166 
167  //! Initializes the kernel
168  inline void setup_kernel(double resolution,double pixelsize) {
169  kernel_params_= em::KernelParameters((float)resolution);
170  pixelsize_=pixelsize;
171  is_setup_ = true;
172  }
173 
174  //! Generates all the masks for a set of particles. This is the function
175  //! you typically want to use
176  void create_masks(const kernel::ParticlesTemp &ps);
177 
178  //! Creates the adequate mask for a particle of given radius
179  /*!
180  \param[in] radius of the particle
181  \param[in] mass of the particle
182  */
183  void create_mask(double radius, double mass);
184 
185  //! Returns the adequate mask for a particle of given radius
186  ProjectionMaskPtr find_mask(double radius);
187 
188  void show(std::ostream &out = std::cout) const;
189 
190  unsigned int get_number_of_masks() const {
191  return radii2mask_.size();
192  }
193 
194  ~MasksManager();
195 
196 
197 protected:
198  // A map to store the masks
199  std::map <double,ProjectionMaskPtr > radii2mask_;
200  // Kernel Params for the particles
201  em::KernelParameters kernel_params_;
202  // Pixel size for the masks
203  double pixelsize_;
204  bool is_setup_;
205 };
206 
207 
209 
210 
211 IMPEM2D_END_NAMESPACE
212 
213 #endif /* IMPEM2D_PROJECTION_MASK_H */
A decorator for particles with mass.
Calculates kernel parameters as a function of a specific radius.
void setup_kernel(double resolution, double pixelsize)
Initializes the kernel.
Simple 2D vector class.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Calculates and stores gaussian kernel parameters.
inteface with OpenCV Copyright 2007-2013 IMP Inventors. All rights reserved.
An approximation of the exponential function.
int get_rounded(const T &x)
Rounds a number to next integer.
Stores and converts spherical coordinates.
Simple 3D rotation class.
Exception definitions and assertions.
Import IMP/kernel/Particle.h in the namespace.
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
Simple 3D vector class.
void do_place(cv::Mat &mask, cv::Mat &m, const algebra::Vector2D &v)
Decorator for a sphere-like particle.
Manage of projection masks.
Decorator for OpenCV matrix to use relative coordinates Copyright 2007-2013 IMP Inventors. All rights reserved.