IMP logo
IMP Reference Guide  2.5.0
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-2015 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)
44  double voxelsize, double mass = 1.0);
45 
46  //! Generates the mask
47  /*!
48  \param[in] KP Kernel parameters to employ. See the EM module
49  \param[in] params Kernel parameters associated with radius to employ
50  \param[in] mass Mass to give to the mask
51  */
52  void create(const em::KernelParameters &KP,
54  double mass = 1.0);
55 #endif
56 
57  //! Adds the values of the mask to a matrix at the given pixel
58  /*!
59  \param[out] m matrix where the values of the mask are added.
60  \param[in] v pixel where to apply the values. Currently used as integer.
61  */
62  void apply(cv::Mat &m, const algebra::Vector2D &v);
63 
64  void show(std::ostream &out = std::cout) const;
65 
66  ~ProjectionMask();
67 
68  protected:
69  int dim_; // dimension of the mask
70  double sq_pixelsize_; // Used to save multiplications
71  cv::Mat data_; // actual matrix with the mask
72 };
73 
75 
76 // Place a matrix in another
77 // Very ugly but very fast projecting function
78 
79 /*!
80  \param[in] mask matrix to place in m
81  \param[in] m matrix
82  \param[in] v Pixel of the matrix dest where the center of m is put.
83 */
84 inline void do_place(cv::Mat &mask, cv::Mat &m, const algebra::Vector2D &v) {
85 
86  // v is the vector of coordinates respect to the center of the matrix m
87  int vi = algebra::get_rounded(v[0]);
88  int vj = algebra::get_rounded(v[1]);
89 
90  // Centers for the matrix
91  int center[2];
92  center[0] = static_cast<int>(0.5 * m.rows);
93  center[1] = static_cast<int>(0.5 * m.cols);
94 
95  int start[2], end[2];
96  start[0] = -center[0];
97  start[1] = -center[1];
98  end[0] = m.rows - 1 - center[0];
99  end[1] = m.cols - 1 - center[1];
100 
101  // Check range: If the vector is outside the matrix, don't do anything.
102  if (vi < start[0] || vi > end[0]) return;
103  if (vj < start[1] || vj > end[1]) return;
104 
105  // Centers for the mask
106  int mcenter[2];
107  mcenter[0] = static_cast<int>(0.5 * mask.rows);
108  mcenter[1] = static_cast<int>(0.5 * mask.cols);
109 
110  int mstart[2], mend[2];
111  mstart[0] = -mcenter[0];
112  mstart[1] = -mcenter[1];
113  mend[0] = mask.rows - 1 - mcenter[0];
114  mend[1] = mask.cols - 1 - mcenter[1];
115 
116  // Get the admissible range for the mask
117  int start_i = std::max(start[0] - vi, mstart[0]);
118  int start_j = std::max(start[1] - vj, mstart[1]);
119  int end_i = std::min(end[0] - vi, mend[0]);
120  int end_j = std::min(end[1] - vj, mend[1]);
121 
122  int row = vi + center[0];
123  int col = vj + center[1];
124 
125  for (int i = start_i; i <= end_i; ++i) {
126  int p = i + row;
127  for (int j = start_j; j <= end_j; ++j) {
128  m.at<double>(p, j + col) +=
129  mask.at<double>(i + mcenter[0], j + mcenter[1]);
130  }
131  }
132 }
133 
134 //! Management of projection masks
135 class IMPEM2DEXPORT MasksManager {
136  public:
137  MasksManager() {
138  is_setup_ = false;
139  };
140 
141  MasksManager(double resolution, double pixelsize) {
142  setup_kernel(resolution, pixelsize);
143  }
144 
145  //! Initializes the kernel
146  inline void setup_kernel(double resolution, double pixelsize) {
147  kernel_params_ = em::KernelParameters((float)resolution);
148  pixelsize_ = pixelsize;
149  is_setup_ = true;
150  }
151 
152  //! Generates all the masks for a set of particles. This is the function
153  //! you typically want to use
154  void create_masks(const ParticlesTemp &ps);
155 
156  //! Creates the adequate mask for a particle of given radius
157  /*!
158  \param[in] radius of the particle
159  \param[in] mass of the particle
160  */
161  void create_mask(double radius, double mass);
162 
163  //! Returns the adequate mask for a particle of given radius
164  ProjectionMaskPtr find_mask(double radius);
165 
166  void show(std::ostream &out = std::cout) const;
167 
168  unsigned int get_number_of_masks() const { return radii2mask_.size(); }
169 
170  ~MasksManager();
171 
172  protected:
173  // A map to store the masks
174  std::map<double, ProjectionMaskPtr> radii2mask_;
175  // Kernel Params for the particles
176  em::KernelParameters kernel_params_;
177  // Pixel size for the masks
178  double pixelsize_;
179  bool is_setup_;
180 };
181 
183 
184 IMPEM2D_END_NAMESPACE
185 
186 #endif /* IMPEM2D_PROJECTION_MASK_H */
A decorator for particles with mass.
VectorD< 2 > Vector2D
Definition: VectorD.h:391
Calculates kernel parameters as a function of a specific radius.
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-2015 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...
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.
Management of projection masks.
Decorator for OpenCV matrix to use relative coordinates Copyright 2007-2015 IMP Inventors. All rights reserved.