IMP logo
IMP Reference Guide  develop.d36d3deffb,2026/11/09
The Integrative Modeling Platform
PolarResamplingParameters.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/PolarResamplingParameters.h
3  * \brief Functions related with rotations in em2d
4  * Copyright 2007-2026 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_POLAR_RESAMPLING_PARAMETERS_H
8 #define IMPEM2D_POLAR_RESAMPLING_PARAMETERS_H
9 
10 #include <IMP/em2d/em2d_config.h>
11 #include "IMP/em2d/Matrix.h"
12 #include "IMP/em2d/FFToperations.h"
13 #include "IMP/algebra/constants.h"
14 #include "IMP/exception.h"
15 #include "IMP/log.h"
16 #include "IMP/log_macros.h"
17 #include <cereal/access.hpp>
18 #include <IMP/constants.h>
19 
20 IMPEM2D_BEGIN_NAMESPACE
21 
22 //! Class to manage the parameters required for the polar resampling in the
23 //! rotational_alignment function
24 class IMPEM2DEXPORT PolarResamplingParameters {
25 
26  public:
28  parameters_set_ = false;
29  };
30 
31  //! Compute the parameters for a polar resampling from the dimensions of
32  //! a matrix
33  PolarResamplingParameters(unsigned int rows, unsigned int cols) {
34  setup(rows, cols);
35  }
36 
37  //! Compute the parameters for a polar resampling getting the dimensions from
38  //! the matrix
39  /*!
40  \param[in] m Matrix that is going to be resampled
41  */
43  setup(m.rows(), m.cols());
44  }
45 
47 
48  //! Initialize the internal parameters to generate all the values
49  //! The class uses a number of radius values for resampling that is
50  //! optimal to perform FFT during the rotational alignment. The first guess
51  //! is half the rows and columns
52  void setup(unsigned int rows, unsigned int cols) {
53  starting_radius_ = 5.0;
54  n_angles_ = 0;
55  matrix_rows_ = rows;
56  matrix_cols_ = cols;
57  ending_radius_ =
58  std::min(rows / 2., cols / 2.); // maximum radius, half the size
59  n_rings_ = get_optimal_dft_size((int)ending_radius_);
60  radius_step_ =
61  (ending_radius_ - starting_radius_) / (static_cast<double>(n_rings_));
62  parameters_set_ = true;
63  IMP_LOG_VERBOSE("PolarResamplingParameters setup. Input matrix: "
64  << rows << " x " << cols << " Starting radius= "
65  << starting_radius_ << " Ending radius= " << ending_radius_
66  << " Rings= " << n_rings_ << std::endl);
67  }
68 
69  //! Gets the initial radius of the resampling
70  double get_starting_radius() const {
71  get_is_setup();
72  return starting_radius_;
73  }
74 
75  //! Gets the largest radius
76  double get_ending_radius() const {
77  get_is_setup();
78  return ending_radius_;
79  }
80 
81  //! Gets the current radius employed for the ring in consideration)
82  double get_radius(unsigned int n_ring) const {
83  get_is_setup();
84  IMP_USAGE_CHECK(n_ring <= n_rings_,
85  "PolarResamplingParameters: Requested ring is above the "
86  "maximum number");
87  return starting_radius_ + n_ring * radius_step_;
88  }
89 
90  //! Get the number of rings (that is, the number of radius values considered)
91  unsigned int get_number_of_rings() const {
92  get_is_setup();
93  return n_rings_;
94  }
95 
96  //! You give an approximated number of values that you want to
97  //! use for the resampling, and the function computes the optimal number
98  //! for an FFT based on this approximated number
99  void set_estimated_number_of_angles(unsigned int aprox_value) {
100  n_angles_ = get_optimal_dft_size(aprox_value);
101  angle_step_ = (2 * PI) / static_cast<double>(n_angles_);
102  }
103 
104  //! Gets the number of points that are sampled for the angles
105  //! remember that this number is usually different to the approximated value
106  //! that you provide to the function set_estimated_number_of_angles()
107  unsigned int get_number_of_angles() const { return n_angles_; }
108 
109  //! get the angular step used
110  double get_angle_step() const { return angle_step_; }
111 
112  //! Get the step for the radius coordinate
113  double get_radius_step() const {
114  if (get_is_setup() == false) {
115  IMP_THROW("trying to get radius_step before initializing",
117  }
118  return radius_step_;
119  }
120 
121  //! After the number of radius and angles values are set, this function
122  //! Builds a map of resampling coordinates. This map is very useful for
123  //! speeding up the resampling (see do_resample_polar())
124  void create_maps_for_resampling();
125 
126 #ifndef SWIG
127  //! Get the map of the (row, column) coordinates of each sample
128  /** The coordinates are stored as pairs of floats.
129  \note For internal use.
130  */
131  void get_resampling_map(internal::RowMajorArray<float> &m1) const {
132  m1 = polar_map_;
133  }
134 #endif
135 
136  void show(std::ostream &out) const {
137  out << "starting_radius = " << starting_radius_ << std::endl;
138  out << "ending_radius = " << ending_radius_ << std::endl;
139  out << "n_rings = " << n_rings_ << std::endl;
140  }
141 
142  bool get_is_setup() const {
143  if (parameters_set_) return true;
144  return false;
145  }
146 
147  protected:
148  // Map of the (row, column) coordinates of each sample (pairs of floats)
149  internal::RowMajorArray<float> polar_map_;
150  double starting_radius_; // starting radius for the polar resampling
151  double ending_radius_; // ending radius for the polar resampling
152  // Number of sampling points for radius and angles
153  unsigned int n_rings_, n_angles_, matrix_rows_, matrix_cols_;
154  bool parameters_set_;
155  double radius_step_, angle_step_;
156 
157 private:
158  friend class cereal::access;
159 
160  template<class Archive> void serialize(Archive &ar) {
161  ar(polar_map_, starting_radius_,
162  ending_radius_, n_rings_, n_angles_, matrix_rows_, matrix_cols_,
163  parameters_set_, radius_step_, angle_step_);
164  }
165 };
166 
168 
169 IMPEM2D_END_NAMESPACE
170 
171 #endif /* IMPEM2D_POLAR_RESAMPLING_PARAMETERS_H */
void get_resampling_map(internal::RowMajorArray< float > &m1) const
Get the map of the (row, column) coordinates of each sample.
Various useful constants.
void set_estimated_number_of_angles(unsigned int aprox_value)
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:83
Exception definitions and assertions.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
double get_ending_radius() const
Gets the largest radius.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
double get_angle_step() const
get the angular step used
Logging and error reporting support.
double get_radius_step() const
Get the step for the radius coordinate.
double get_starting_radius() const
Gets the initial radius of the resampling.
Matrices used to store image data Copyright 2007-2026 IMP Inventors. All rights reserved.
unsigned int get_number_of_rings() const
Get the number of rings (that is, the number of radius values considered)
Various useful constants.
int get_optimal_dft_size(int n)
Get the smallest size >= n that can be transformed efficiently by FFT.
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
void setup(unsigned int rows, unsigned int cols)
double get_radius(unsigned int n_ring) const
Gets the current radius employed for the ring in consideration)
PolarResamplingParameters(unsigned int rows, unsigned int cols)
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
Logging and error reporting support.
Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > DoubleMatrix
Matrix of doubles, used to store the data of an image.
Definition: Matrix.h:28
Operations involving FFT Copyright 2007-2026 IMP Inventors. All rights reserved.
An exception for an invalid value being passed to IMP.
Definition: exception.h:136