IMP logo
IMP Reference Guide  2.6.2
The Integrative Modeling Platform
ProjectionMask.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/ProjectionMask.h
3  * \brief projection masks
4  * Copyright 2007-2016 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/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 //! Mask that contains the projection of a given particles. This matrices
35 //! speed up projecting because the only have to be computed once for a model
36 class IMPEM2DEXPORT ProjectionMask {
37 #ifdef SWIG
38  ProjectionMask() {}
39 #endif
40  public:
41 #if !defined(DOXYGEN) && !defined(SWIG)
42  ProjectionMask(const em::KernelParameters& kparams,
43  double voxelsize, double mass = 1.0);
44 
45  //! Generates the mask
46  /*!
47  \param[in] KP Kernel parameters to employ. See the EM module
48  \param[in] mass Mass to give to the mask
49  */
50  void create(const em::KernelParameters& kparams,
51  double mass = 1.0);
52 #endif
53 
54  //! Adds the values of the mask to a matrix at the given pixel
55  /*!
56  \param[out] m matrix where the values of the mask are added.
57  \param[in] v pixel where to apply the values. Currently used as integer.
58  */
59  void apply(cv::Mat &m, const algebra::Vector2D &v);
60 
61  void show(std::ostream &out = std::cout) const;
62 
63  ~ProjectionMask();
64 
65  protected:
66  int dim_; // dimension of the mask
67  double sq_pixelsize_; // Used to save multiplications
68  cv::Mat data_; // actual matrix with the mask
69 };
70 
72 
73 // Place a matrix in another
74 // Very ugly but very fast projecting function
75 
76 /*!
77  \param[in] mask matrix to place in m
78  \param[in] m matrix
79  \param[in] v Pixel of the matrix dest where the center of m is put.
80 */
81 inline void do_place(cv::Mat &mask, cv::Mat &m, const algebra::Vector2D &v) {
82 
83  // v is the vector of coordinates respect to the center of the matrix m
84  int vi = algebra::get_rounded(v[0]);
85  int vj = algebra::get_rounded(v[1]);
86 
87  // Centers for the matrix
88  int center[2];
89  center[0] = static_cast<int>(0.5 * m.rows);
90  center[1] = static_cast<int>(0.5 * m.cols);
91 
92  int start[2], end[2];
93  start[0] = -center[0];
94  start[1] = -center[1];
95  end[0] = m.rows - 1 - center[0];
96  end[1] = m.cols - 1 - center[1];
97 
98  // Check range: If the vector is outside the matrix, don't do anything.
99  if (vi < start[0] || vi > end[0]) return;
100  if (vj < start[1] || vj > end[1]) return;
101 
102  // Centers for the mask
103  int mcenter[2];
104  mcenter[0] = static_cast<int>(0.5 * mask.rows);
105  mcenter[1] = static_cast<int>(0.5 * mask.cols);
106 
107  int mstart[2], mend[2];
108  mstart[0] = -mcenter[0];
109  mstart[1] = -mcenter[1];
110  mend[0] = mask.rows - 1 - mcenter[0];
111  mend[1] = mask.cols - 1 - mcenter[1];
112 
113  // Get the admissible range for the mask
114  int start_i = std::max(start[0] - vi, mstart[0]);
115  int start_j = std::max(start[1] - vj, mstart[1]);
116  int end_i = std::min(end[0] - vi, mend[0]);
117  int end_j = std::min(end[1] - vj, mend[1]);
118 
119  int row = vi + center[0];
120  int col = vj + center[1];
121 
122  for (int i = start_i; i <= end_i; ++i) {
123  int p = i + row;
124  for (int j = start_j; j <= end_j; ++j) {
125  m.at<double>(p, j + col) +=
126  mask.at<double>(i + mcenter[0], j + mcenter[1]);
127  }
128  }
129 }
130 
131 //! Management of projection masks
132 class IMPEM2DEXPORT MasksManager {
133  public:
134  MasksManager() {
135  is_setup_ = false;
136  };
137 
138  MasksManager(double resolution, double pixelsize) {
139  setup_kernel(resolution, pixelsize);
140  }
141 
142  //! Initializes the kernel
143  inline void setup_kernel(double resolution, double pixelsize) {
144  kernel_params_ = em::KernelParameters((float)resolution);
145  pixelsize_ = pixelsize;
146  is_setup_ = true;
147  }
148 
149  //! Generates all the masks for a set of particles. This is the function
150  //! you typically want to use
151  void create_masks(const ParticlesTemp &ps);
152 
153  //! Creates the adequate mask for a particle of given mass
154  /*!
155  \param[in] mass of the particle
156  */
157  void create_mask(double mass);
158 
159  //! Returns the adequate mask for a particle of given mass
160  ProjectionMaskPtr find_mask(double mass);
161 
162  void show(std::ostream &out = std::cout) const;
163 
164  unsigned int get_number_of_masks() const { return mass2mask_.size(); }
165 
166  ~MasksManager();
167 
168  protected:
169  // A map to store the masks
170  std::map<double, ProjectionMaskPtr> mass2mask_;
171  // Kernel Params for the particles
172  em::KernelParameters kernel_params_;
173  // Pixel size for the masks
174  double pixelsize_;
175  bool is_setup_;
176 };
177 
179 
180 IMPEM2D_END_NAMESPACE
181 
182 #endif /* IMPEM2D_PROJECTION_MASK_H */
A decorator for particles with mass.
VectorD< 2 > Vector2D
Definition: VectorD.h:391
Calculates and stores Gaussian kernel parameters.
void setup_kernel(double resolution, double pixelsize)
Initializes the kernel.
Simple 2D vector class.
Calculates and stores Gaussian kernel parameters.
Interface with OpenCV Copyright 2007-2016 IMP Inventors. All rights reserved.
Exception definitions and assertions.
An approximation of the exponential function.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
int get_rounded(const T &x)
Rounds a number to next integer.
Stores and converts spherical coordinates.
Simple 3D rotation class.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
Simple 3D vector class.
void do_place(cv::Mat &mask, cv::Mat &m, const algebra::Vector2D &v)
Decorator for a sphere-like particle.
Management of projection masks.
Decorator for OpenCV matrix to use relative coordinates Copyright 2007-2016 IMP Inventors. All rights reserved.