IMP logo
IMP Reference Guide  develop.8b7eb21230,2024/06/24
The Integrative Modeling Platform
project.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/project.h
3  * \brief Generation of projections from models or density maps
4  * Copyright 2007-2022 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_PROJECT_H
8 #define IMPEM2D_PROJECT_H
9 
10 #include <IMP/em2d/em2d_config.h>
13 #include "IMP/em2d/Image.h"
15 #include "IMP/em2d/FFToperations.h"
17 #include "IMP/algebra/Vector3D.h"
18 #include "IMP/algebra/Rotation3D.h"
20 #include "IMP/core/XYZR.h"
21 #include "IMP/Pointer.h"
22 #include "IMP.h"
23 #include <complex>
24 #include <algorithm>
25 #include <fstream>
26 #include <cereal/access.hpp>
27 
28 IMPEM2D_BEGIN_NAMESPACE
29 
30 //! Parameters needed for the core projection routine
31 class IMPEM2DEXPORT ProjectingParameters {
32 private:
33  friend class cereal::access;
34 
35  template<class Archive> void serialize(Archive &ar) {
36  ar(pixel_size, resolution);
37  }
38 
39  public:
40  double pixel_size, resolution;
41 
43 
44  /**
45  *
46  * @param ps Pixel size of the image to generate when projection
47  * @param res Resolution used to downsample an atomic model before projecting
48  */
49  ProjectingParameters(double ps, double res)
50  : pixel_size(ps), resolution(res) {};
51 
52  /**
53  * Shows information about the class
54  * @param out Stream used to show the information
55  */
56  void show(std::ostream &out = std::cout) const {
57  out << "ProjectingParameters " << pixel_size << " " << resolution
58  << std::endl;
59  };
60 };
62 
63 //! Parameters given as options to the get_projections() functions.
64 /** This class augments ProjectingParameters adding values that are options
65  for the projecting functions, not core parameters.
66  */
67 class IMPEM2DEXPORT ProjectingOptions : public ProjectingParameters {
68 
69  /**
70  * Sets the default values for the options: Don't save matching images,
71  * normalize the projections, and clear the data matrix after projecting
72  */
73  void init_defaults() {
74  save_images = false;
75  normalize = true;
76  clear_matrix_before_projecting = true;
77  }
78 
79  public:
80 #ifndef SWIG
81  Pointer<ImageReaderWriter> srw; // Writer used to save the images
82 #endif
83  bool save_images; // Save images after projecting
84  bool normalize; // Normalize the projection after generating it
85  bool clear_matrix_before_projecting; // Set the matrix to zeros
86 
87  /**
88  * Constructor. Calls init_defaults()
89  */
90  ProjectingOptions() { init_defaults(); }
91 
92  /**
93  * The arguments passed to the constructor are the same as the arguments to
94  * to the constructor of ProjectingParameters class.
95  * @param ps
96  * @param res
97  */
98  ProjectingOptions(double ps, double res) : ProjectingParameters(ps, res) {
99  init_defaults();
100  }
101 
102  ProjectingOptions(double ps, double res, ImageReaderWriter *irw)
103  : ProjectingParameters(ps, res), srw(irw) {
104  init_defaults();
105  }
106 
107  /**
108  * Shows information about the class
109  * @param out Stream used to show the information
110  */
111  void show(std::ostream &out = std::cout) const {
112  out << "ProjectingOptions " << pixel_size << " " << resolution << std::endl;
113  };
114 };
116 
117 //! Generates projections from particles
118 /**
119  \param[in] ps particles to project
120  \param[in] vs set of spherical vectors with the directions of projection
121  \param[in] rows size of the images
122  \param[in] cols
123  \param[in] options Options for control the projecting
124  \param[in] names names of the images
125 */
126 IMPEM2DEXPORT em2d::Images get_projections(
127  const ParticlesTemp &ps, const algebra::SphericalVector3Ds &vs,
128  int rows, int cols, const ProjectingOptions &options,
129  Strings names = Strings());
130 
131 //! Generates projections from particles
132 /**
133  `registration_values` describes registration values with the parameters of
134  the projections to generate.
135 
136  \note See the function get_projections() for the rest of the parameters
137 */
138 IMPEM2DEXPORT em2d::Images get_projections(
139  const ParticlesTemp &ps,
140  const RegistrationResults &registration_values, int rows, int cols,
141  const ProjectingOptions &options, Strings names = Strings());
142 
143 //! Generates a projection from particles
144 /**
145  \param[in] img
146  \param[in] ps particles to project
147  \param[in] reg Registration value with the parameters of the projection
148  \param[in] options
149  \param[in] masks Precomputed masks for projecting the particles. Very useful
150  for speeding the projection procedure if they are given.
151  If nullptr, they are computed
152  \param[in] name
153  \return img the projection will be stored here
154  \note See the function get_projections() for the rest of the parameters
155 */
156 IMPEM2DEXPORT void get_projection(em2d::Image *img,
157  const ParticlesTemp &ps,
158  const RegistrationResult &reg,
159  const ProjectingOptions &options,
160  MasksManagerPtr masks = MasksManagerPtr(),
161  String name = "");
162 
163 //! Projects a set of particles. This is the core function that others call
164 /**
165  \param[in] ps particles to project
166  \param[in] m2
167  \param[in] R rotation to apply to the particles (respect to the centroid)
168  \param[in] translation Translation to apply after rotation
169  \param[in] options
170  \param[in] masks
171  \note See the function get_projection() for the rest of the parameters
172 */
173 IMPEM2DEXPORT void do_project_particles(const ParticlesTemp &ps,
174  cv::Mat &m2,
175  const algebra::Rotation3D &R,
176  const algebra::Vector3D &translation,
177  const ProjectingOptions &options,
178  MasksManagerPtr masks);
179 
180 /** This function is slightly different than the other ones.
181  Only generates evenly distributed projections and determines the size of
182  the images that encloses the particles. Should not be used unless this is
183  exactly what you want.
184 */
185 IMPEM2DEXPORT Images
187  unsigned int n,
188  const ProjectingOptions &options);
189 
190 //! Project the points contained in Vector3Ds to gen vectors in 2D
191 /**
192  \param[in] ps the points
193  \param[in] R Rotation to apply to the points to project them in the Z axis
194  \param[in] translation translation to apply to the points
195  \return A set of Vector2D with the projected points
196 */
198  const algebra::Vector3Ds &ps, const algebra::Rotation3D &R,
199  const algebra::Vector3D &translation);
200 
201 //! Project the points contained in Vector3Ds
202 /**
203  \param[in] ps the points
204  \param[in] R Rotation to apply to the points to project them in the Z axis
205  \param[in] translation translation to apply to the points
206  \param[in] center Center point used for the rotation around it
207  \return A set of Vector2D with the projected points
208 */
210  const algebra::Vector3Ds &ps, const algebra::Rotation3D &R,
211  const algebra::Vector3D &translation, const algebra::Vector3D &center);
212 
213 //! Get an automatic size for an image that contains the particles
214 /**
215  slack is the number of pixels left as border
216 */
217 IMPEM2DEXPORT unsigned int get_enclosing_image_size(
218  const ParticlesTemp &ps, double pixel_size, unsigned int slack);
219 
220 IMPEM2D_END_NAMESPACE
221 
222 #endif /* IMPEM2D_PROJECT_H */
Images create_evenly_distributed_projections(const ParticlesTemp &ps, unsigned int n, const ProjectingOptions &options)
projection masks Copyright 2007-2023 IMP Inventors. All rights reserved.
IMP::Vector< String > Strings
Standard way to pass a bunch of String values.
Definition: types.h:50
Include all non-deprecated headers in IMP.
Virtual class for reader/writers of images.
void do_project_particles(const ParticlesTemp &ps, cv::Mat &m2, const algebra::Rotation3D &R, const algebra::Vector3D &translation, const ProjectingOptions &options, MasksManagerPtr masks)
Projects a set of particles. This is the core function that others call.
Virtual class for reader/writers of images Copyright 2007-2022 IMP Inventors. All rights reserved...
Parameters given as options to the get_projections() functions.
Definition: project.h:67
Interface with OpenCV Copyright 2007-2022 IMP Inventors. All rights reserved.
ProjectingOptions(double ps, double res)
Definition: project.h:98
A more IMP-like version of the std::vector.
Definition: Vector.h:50
IMP images for Electron Microscopy using openCV matrices Copyright 2007-2022 IMP Inventors. All rights reserved.
A smart pointer to a reference counted object.
Definition: Pointer.h:87
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
ProjectingParameters(double ps, double res)
Definition: project.h:49
Stores and converts spherical coordinates.
algebra::Vector2Ds do_project_vectors(const algebra::Vector3Ds &ps, const algebra::Rotation3D &R, const algebra::Vector3D &translation, const algebra::Vector3D &center)
Project the points contained in Vector3Ds.
void save_images(Images images, const Strings &names, const ImageReaderWriter *rw)
Simple 3D rotation class.
Parameters needed for the core projection routine.
Definition: project.h:31
3D rotation class.
Definition: Rotation3D.h:52
unsigned int get_enclosing_image_size(const ParticlesTemp &ps, double pixel_size, unsigned int slack)
Get an automatic size for an image that contains the particles.
A nullptr-initialized pointer to an IMP Object.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
Simple 3D vector class.
Class to manage registration results.
void show(std::ostream &out=std::cout) const
Definition: project.h:56
void show(std::ostream &out=std::cout) const
Definition: project.h:111
em2d::Images get_projections(const ParticlesTemp &ps, const RegistrationResults &registration_values, int rows, int cols, const ProjectingOptions &options, Strings names=Strings())
Generates projections from particles.
Operations involving FFT Copyright 2007-2022 IMP Inventors. All rights reserved.
Registration results class Copyright 2007-2022 IMP Inventors. All rights reserved.
Decorator for a sphere-like particle.
std::string String
Basic string value.
Definition: types.h:43
2D Electron Microscopy images in IMP
Definition: Image.h:27
void get_projection(em2d::Image *img, const ParticlesTemp &ps, const RegistrationResult &reg, const ProjectingOptions &options, MasksManagerPtr masks=MasksManagerPtr(), String name="")
Generates a projection from particles.