Xmipp  v3.23.11-Nereus
refinement.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Roberto Marabini
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include "refinement.h"
27 #include "core/multidim_array.h"
29 
30 template <class T>
32  MultidimArray<T> const &mat2,
33  MultidimArray<T> & mat_temp,
34  double &shift_X, double &shift_Y,
35  double const max_step,
36  CorrelationAux &aux)
37 {
38 
39  //WRAP is OK for crystal but may be not for single particles
40 
41  //calculate correlation matrix
42  correlation_matrix(mat1, mat2, mat_temp, aux);
43  mat_temp.setXmippOrigin();
44 
45  //search for the maximun inside selfWindow "selfWindow"
46  auto max_step_int = (int)ROUND(max_step);
47  int imax=0, jmax=0;
48 
49  MultidimArray<int> window(2*max_step_int + 1, 2*max_step_int + 1);
50  window.setXmippOrigin();
51 
52  T temp_max = -100000000;
53 
54  //******* search for the maximun with pixel acuraccy **************/
56  {
57  if (temp_max < mat_temp(i, j))
58  {
59  temp_max = mat_temp(i, j);
60  imax = i;
61  jmax = j;
62  }
63  }
64  // #define DEBUG_calculate_and_find_correlation_max_mat
65 #ifdef DEBUG_calculate_and_find_correlation_max_mat
66  std::cout << "\n imax, jmax: " << imax << " " << jmax << std::endl;
67  std::cout.flush();
68 #endif
69 
70  /******* Calculate the gravity centre of the corelation ****/
71  /******* in a neighborhood such as maximum/sqrt(2) > value ****/
72  int n_max;
73  int stop_loop;
74  int i, j, i_actual, j_actual;
75  int radius;
76  stop_loop = false;
77  n_max = 0;
78  while (!stop_loop)
79  {
80  n_max ++;
81  radius = n_max * n_max;
82  for (i = -n_max; i <= n_max; i++)
83  for (j = -n_max; j <= n_max; j++)
84  {
85  i_actual = i + imax;
86  j_actual = j + jmax;
87  if ((i*i + j*j) > radius)
88  continue;
89  if (!mat_temp.outside(i_actual, j_actual))
90  {
91  if (temp_max / 1.414 > mat_temp(i_actual, j_actual))
92  stop_loop = true;
93  }
94  else
95  {
96  stop_loop = true;
97  std::cout << "\nWarning(calculate_and_find_correlation_max_mat)"
98  << "\n some points neede to determine the maxima"
99  << "\n are not available" << std::endl;
100  }
101  }
102  }
103 
104  // #define DEBUG_calculate_and_find_correlation_max_mat
105 #ifdef DEBUG_calculate_and_find_correlation_max_mat
106  std::cout << "\n n_max, temp_max: " << n_max << " " << temp_max << std::endl;
107  std::cout.flush();
108 #endif
109 
110  /*** We have the neighborhood => looking for the gravity centre ***/
111 
112  double jj_max ;
113  double ii_max ;
114  double sum_corr ;
115  //n_max and radius has been calculated above
116  jj_max = ii_max = sum_corr = 0.;
117  for (i = -n_max; i <= n_max; i++)
118  for (j = -n_max; j <= n_max; j++)
119  {
120  i_actual = i + imax;
121  j_actual = j + jmax;
122  if ((i*i + j*j) > radius)
123  continue;
124  if (!mat_temp.outside(i_actual, j_actual))
125  {
126  ii_max += i_actual * mat_temp(i_actual, j_actual);
127  jj_max += j_actual * mat_temp(i_actual, j_actual);
128  sum_corr += mat_temp(i_actual, j_actual);
129  }
130  }
131  shift_X = jj_max / sum_corr; /***This is the gravity centre ***/
132  shift_Y = ii_max / sum_corr;
133 
134  // #define DEBUG_calculate_and_find_correlation_max_mat
135 #ifdef DEBUG_calculate_and_find_correlation_max_mat
136  std::cout << "\n shift_XX " << shift_X << std::endl;
137  std::cout << "\n shift_Y " << shift_Y << std::endl;
138  std::cout << "\n jj_max " << jj_max << std::endl;
139  std::cout << "\n ii_max " << ii_max << std::endl;
140  std::cout << "\n sum_corr " << sum_corr << std::endl;
141  std::cout.flush();
142 #endif
143 
144 }
145 #undef DEBUG_calculate_and_find_correlation_max_mat
146 
147 
148 //-------------------------------------------------------------------------
149 /* Correlate two projections and find the maximum of the correlation matrix -- */
150 /* If the maximum if moved further away than max_step returns 0 */
152  Projection const &proj2,
153  Projection &proj_temp,
154  double &shift_X, double &shift_Y,
155  double const max_step,
156  int ref_trans_after, int imagen_no,
157  CorrelationAux &aux)
158 {
159 // #define DEBUG_calculate_and_find_correlation_max_proj
160 #ifdef DEBUG_calculate_and_find_correlation_max_proj
161  std::cout << "\n (cal_find_corr_proj) imagen_no: " << imagen_no << std::endl;
162 #endif
163 
164 
165  proj_temp().resize(proj1());
167  IMGMATRIX(proj2),
168  IMGMATRIX(proj_temp),
169  shift_X, shift_Y, max_step,
170  aux);
171 
172 }//calculate_and_find_correlation_max end
173 #undef DEBUG_calculate_and_find_correlation_max_proj
void calculate_and_find_correlation_max_proj(Projection const &proj1, Projection const &proj2, Projection &proj_temp, double &shift_X, double &shift_Y, double const max_step, int ref_trans_after, int imagen_no, CorrelationAux &aux)
Definition: refinement.cpp:151
void calculate_and_find_correlation_max_mat(MultidimArray< T > const &mat1, MultidimArray< T > const &mat2, MultidimArray< T > &mat_temp, double &shift_X, double &shift_Y, double const max_step, CorrelationAux &aux)
Definition: refinement.cpp:31
void correlation_matrix(const MultidimArray< double > &m1, const MultidimArray< double > &m2, MultidimArray< double > &R, CorrelationAux &aux, bool center)
Definition: xmipp_fftw.cpp:869
#define IMGMATRIX(I)
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
#define ROUND(x)
Definition: xmipp_macros.h:210
#define j
bool outside(int k, int i, int j) const