Xmipp  v3.23.11-Nereus
image_assignment_tilt_pair.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: Jose Luis Vilas (jlvilas@cnb.csic.es)
3  *
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 
27 #include "delaunay/delaunay.h"
28 #include "core/geometry.h"
29 #include "core/matrix1d.h"
30 #include "core/matrix2d.h"
33 #include "core/xmipp_funcs.h"
34 
36 {
37  fnuntilt = getParam("--untiltcoor");
38  fntilt = getParam("--tiltcoor");
39  fnmic = getParam("--tiltmicsize");
40  fndir = getParam("--odir");
41  mshift = getDoubleParam("--maxshift");
42  tiltest = getDoubleParam("--tiltangle");
43  particle_size = getDoubleParam("--particlesize");
44  thr = getDoubleParam("--threshold");
45 }
46 
48 {
49  //usage
50  addUsageLine("Validate a 3D reconstruction from its projections attending to directionality and spread of the angular assignments from a given significant value");
51  //params
52 
53  addParamsLine(" [--untiltcoor <md_file=\"\">] : Untilt coordinates");
54  addParamsLine(" [--tiltcoor <md_file=\"\">] : Tilt coordinates");
55  addParamsLine(" [--tiltmicsize <img_file=\"\">] : Tilt micrography");
56  addParamsLine(" [--odir <outputDir=\".\">] : Output directory");
57  addParamsLine(" [--maxshift <s=1000>] : Maximum shift");
58  addParamsLine(" [--tiltangle <s=-1>] : Tilt angle estimation, the method will look for the assignment in the interval of [tiltangle-15º, til_angle+15º]");
59  addParamsLine(" [--particlesize <p=100>] : Particle size");
60  addParamsLine(" [--threshold <d=0.3>] : If the distance between two points is lesser than threshold*particlesize, they will be the same point");
61 }
62 
63 void ProgassignmentTiltPair::search_affine_transform(float u1x, float u1y, float u2x, float u2y, float u3x, float u3y, float t1x,
64  float t1y, float t2x, float t2y, float t3x, float t3y,
65  const Matrix1D<double> &ux, const Matrix1D<double> &uy, size_t Xdim, size_t Ydim, struct Delaunay_T &delaunay_tilt,
66  int &bestInliers, Matrix2D<double> &A_coarse, Matrix1D<double> &T_coarse, bool contingency, int thrs)
67 {
68  double estimator, dist;
69  struct Point_T t_dist, t_closest;
71  Matrix2D<double> invW;
72  Matrix2D<double> A_matrix;
73 
74  A_matrix.initZeros(2,2);
75 
76  for (int i=0; i<6; i++)
77  {
78  if (i==0){
79  def_affinity(u1x, u1y, u2x, u2y, u3x, u3y, t1x, t1y, t2x, t2y, t3x, t3y, A_matrix, T, invW);}
80  if (i==1)
81  def_affinity(u1x, u1y, u2x, u2y, u3x, u3y, t1x, t1y, t3x, t3y, t2x, t2y, A_matrix, T, invW);
82  if (i==2)
83  def_affinity(u1x, u1y, u2x, u2y, u3x, u3y, t2x, t2y, t1x, t1y, t3x, t3y, A_matrix, T, invW);
84  if (i==3)
85  def_affinity(u1x, u1y, u2x, u2y, u3x, u3y, t2x, t2y, t3x, t3y, t1x, t1y, A_matrix, T, invW);
86  if (i==4)
87  def_affinity(u1x, u1y, u2x, u2y, u3x, u3y, t3x, t3y, t1x, t1y, t2x, t2y, A_matrix, T, invW);
88  if (i==5)
89  def_affinity(u1x, u1y, u2x, u2y, u3x, u3y, t3x, t3y, t2x, t2y, t1x, t1y, A_matrix, T, invW);
90 
91  double trace_A = MAT_ELEM(A_matrix, 0, 0) + MAT_ELEM(A_matrix, 1, 1);
92  double det_A = MAT_ELEM(A_matrix, 0, 0)*MAT_ELEM(A_matrix, 1, 1) - MAT_ELEM(A_matrix, 0, 1)*MAT_ELEM(A_matrix, 1, 0);
93 
94 
95 // if ( fabs(det_A - cos_tilt_max)>0.1)
96 // continue;
97 
98  double discriminant_A = 0.25*trace_A*trace_A - det_A;
99 
100  if (discriminant_A < DBL_EPSILON)
101  continue;
102 
103  double sqrt_aux = sqrt(discriminant_A);
104  double Eig_A1 = trace_A/2 + sqrt_aux;
105  double Eig_A2 = trace_A/2 - sqrt_aux;
106 
107  if (Eig_A1<0.34 || Eig_A2<0.34 || fabs(Eig_A1-1)>0.05)
108  continue;
109 
110  Matrix1D<double> u(2), dist_vec, dist_vec2, t_test(2);
111  size_t len_u=VEC_XSIZE(ux);
112 
113  dist_vec.initConstant(len_u, -1);
114  dist_vec2.initConstant(len_u, 10);
115 
116  for (size_t tt=0; tt<len_u; tt++)
117  {
118  VEC_ELEM(u,0) = VEC_ELEM(ux,tt);
119  VEC_ELEM(u,1) = VEC_ELEM(uy,tt);
120  t_test = A_matrix*u + T;
121 
122 
123  if (VEC_ELEM(t_test,0)<0 || VEC_ELEM(t_test,0)>Xdim || VEC_ELEM(t_test,1)<0 || VEC_ELEM(t_test,1)>Ydim)
124  continue;
125 
126  t_dist.x = VEC_ELEM(t_test,0);
127  t_dist.y = VEC_ELEM(t_test,1);
128 
129  if (!select_Closest_Point(&delaunay_tilt, &t_dist, &t_closest, &dist) )
130  {
131 
132  write_DCEL(delaunay_tilt.dcel, 0, "tiltdata_dcel.txt");
133  //std::cerr << "WARNING IN TRIANGULATION OR CLOSEST NEIGHBOUR" << std::endl;
134  //printf("Maybe the warning involves the tilt coordinates ( %f , %f ) \n", t_dist.x, t_dist.y);
135  continue;
136  }
137  VEC_ELEM(dist_vec,tt) = dist;
138  }
139 
140  int counter = 0;
141  size_t inliers2 = 0;
142  double dist2 = 0;
143 
144  for (size_t kk=0; kk<len_u; kk++)
145  {
146  if (VEC_ELEM(dist_vec,kk)>-1)
147  {
148  counter = counter + 1;
149  if (VEC_ELEM(dist_vec,kk) < thr*particle_size)
150  {
151  dist2 = VEC_ELEM(dist_vec,kk) + dist2;
152  inliers2 = inliers2 + 1;
153  }
154  }
155  }
156 
157  if (counter == 0)
158  continue;
159 
160  if (inliers2 > bestInliers)
161  {
162  estimator = dist2/inliers2;
163  bestInliers = inliers2;
164  if (contingency)
165  if (bestInliers>0.3*thrs)
166  break;
167 
168  A_coarse = A_matrix;
169  T_coarse = T;
170  std::cout << bestInliers << std::endl;
171  }
172  else if ((inliers2 == bestInliers) && (dist2/inliers2 < estimator) )
173  {
174  estimator = dist2/inliers2;
175  A_coarse = A_matrix;
176  T_coarse = T;
177  }
178  }
179 }
180 
181 void ProgassignmentTiltPair::findMaximumMinimum(const float u1, const float u2, const float u3, double &u_max, double &u_min)
182 {
183 if (u1 > u2)
184 {
185  if (u1 > u3)
186  u_max = u1;
187  else
188  u_max = u3;
189 
190  if (u2 > u3)
191  u_min = u3;
192  else
193  u_min = u2;
194 }
195 else
196 {
197  if (u2 > u3)
198  u_max = u2;
199  else
200  u_max = u3;
201 
202  if (u1 > u3)
203  u_min = u3;
204  else
205  u_min = u1;
206 }
207 }
208 
209 bool ProgassignmentTiltPair::checkwindow(const float t1, const float t2, const float t3,
210  const double u_max, const double u_min)
211 {
212  double window_p, window_m;
213  window_p = u_max + 1.2*mshift;
214  window_m = u_min - 1.2*mshift;
215 
216 
217  if (((t1<window_p) && (t2<window_p) && (t3<window_p)) && ((t1>window_m) && (t2>window_m) && (t3>window_m)))
218  return true;
219  else
220  return false;
221 
222 }
223 
224 
226 {
227  std::cout << "Starting..." << std::endl;
228 
229  //LOAD METADATA and TRIANGULATIONS
230  MetaDataVec md_untilt, md_tilt, mduntilt, mdtilt;
231  size_t Ndim, Zdim, Ydim , Xdim, objId;
232 
233  getImageSize(fnmic, Xdim, Ydim, Zdim, Ndim);
234 
235  bool exist1, exist2;
236  bool flag_assign = true;
237 
238  exist1 = fnuntilt.existsTrim();
239  exist2 = fntilt.existsTrim();
240 
241  if ((exist1 && exist2) == true)
242  {
243  md_untilt.read(fnuntilt);
244  md_tilt.read(fntilt);
245 
246  int x, y, len_u=0, len_t=0;
247 
248  //storing untilted points and creating Delaunay triangulation
249  struct Delaunay_T delaunay_untilt;
250  Matrix1D<double> ux(md_untilt.size()), uy(md_untilt.size()), tx(md_tilt.size()), ty(md_tilt.size());
251  init_Delaunay( &delaunay_untilt, md_untilt.size());
252  for (size_t objId : md_untilt.ids())
253  {
254  md_untilt.getValue(MDL_XCOOR, x, objId);
255  md_untilt.getValue(MDL_YCOOR, y, objId);
256 
257  VEC_ELEM(ux,len_u) = x;
258  VEC_ELEM(uy,len_u) = y;
259 
260  insert_Point( &delaunay_untilt, x, y);
261  len_u = len_u +1;
262  }
263  create_Delaunay_Triangulation( &delaunay_untilt, 0);
264  int tri_number_untilt = get_Number_Real_Faces(delaunay_untilt.dcel);
265 
266  //storing tilted points and creating Delaunay triangulation
267  struct Delaunay_T delaunay_tilt;
268  init_Delaunay( &delaunay_tilt, md_tilt.size());
269  for (size_t objId : md_tilt.ids())
270  {
271  md_tilt.getValue(MDL_XCOOR, x, objId);
272  md_tilt.getValue(MDL_YCOOR, y, objId);
273 
274  VEC_ELEM(tx,len_t) = x;
275  VEC_ELEM(ty,len_t) = y;
276 
277  insert_Point( &delaunay_tilt, x, y);
278  len_t = len_t +1;
279  }
280  int thrs;
281  if (len_u<len_t)
282  {
283  thrs = len_u;
284  }
285  else
286  {
287  thrs = len_t;
288  }
289 
290  if (len_u == 0 || len_t == 0)
291  {
292  flag_assign = false;
293  }
294 
295  if (flag_assign == true)
296  {
297  create_Delaunay_Triangulation( &delaunay_tilt, 1);
298  int tri_number_tilt = get_Number_Real_Faces(delaunay_tilt.dcel);
300 
301  //DETERMINING TRIANGLES AREAS
302  struct Point_T p, q, r, u1, u2, u3, t1, t2, t3;
303  MultidimArray<double> trig_untilt_area(tri_number_untilt+1), trig_tilt_area(tri_number_tilt+1), sortedArea;
304  Matrix1D<double> trig_untilt_area_1D(tri_number_untilt+1), trig_tilt_area_1D(tri_number_tilt+1);
305 
306  for (int i=1; i<tri_number_untilt+1 ;i++)
307  {
308  get_Face_Points(&delaunay_untilt, i, &p, &q, &r); //i is the triangle number
309  A1D_ELEM(trig_untilt_area,i-1) = triangle_area(p.x, p.y, q.x, q.y, r.x, r.y);
310  }
311 
312  for (int i=1; i<tri_number_tilt+1 ;i++)
313  {
314  get_Face_Points(&delaunay_tilt, i, &p, &q, &r);
315  A1D_ELEM(trig_tilt_area,i-1) = triangle_area(p.x, p.y, q.x, q.y, r.x, r.y);
316  }
317 
318  trig_untilt_area.sort(sortedArea);
319 
320  double threshold_area = sortedArea( round((tri_number_untilt+1)*0.2) );
322 
323  //DEFINING PARAMETERS FOR SEARCH_AFFINE_TRANSFORM
324  int bestInliers=0, bestInliers_con=0;
325  Matrix2D<double> A_coarse;
326  Matrix1D<double> T_coarse, def_T, t_test(2), u(2), dist_vec, dist_vec2;
327 
328  struct Point_T t_dist, t_closest;
330 
331  //TILT ANGLE ESTIMATION
332  double tilt_max = (tiltest + 15)*PI/180, tilt_min = (tiltest - 15)*PI/180, cos_tilt_max, cos_tilt_min;
333 
334  if (tiltest == -1)
335  {
336  cos_tilt_max = 0;
337  cos_tilt_min = 1;
338  }
339  else
340  {
341  cos_tilt_max= cos(tilt_max);
342  cos_tilt_min= cos(tilt_min);
343  }
345 
346 
347  if (verbose==1)
348  {
349  std::cerr << "Exploring triangle matching" << std::endl;
350  init_progress_bar(tri_number_untilt);
351  }
352 
353 
355  if (verbose==1)
356  std::cerr << "Coarse Phase" << std::endl;
357 
358  for (int k=0; k<tri_number_untilt; k++)
359  {
360  if (trig_untilt_area(k) < threshold_area)
361  continue;
362 
363  for (int j=0; j<tri_number_tilt; j++)
364  {
365  //std::cout << "Iteration k = " << k << " Iteration j = " << j << std::endl;
366  if (trig_untilt_area(k) < trig_tilt_area(j))
367  continue;
368 
369  if ( (trig_untilt_area(k)*cos_tilt_min < trig_tilt_area(j)) || (trig_untilt_area(k)*cos_tilt_max > trig_tilt_area(j)) )
370  continue;
371 
372  get_Face_Points(&delaunay_untilt, k, &u1, &u2, &u3);
373  get_Face_Points(&delaunay_tilt, j, &t1, &t2, &t3);
374 
375 
376  //New Idea
377  double ux_max, uy_max, ux_min, uy_min;
378  findMaximumMinimum(u1.x, u2.x, u3.x, ux_max, ux_min);
379  findMaximumMinimum(u1.y, u2.y, u3.y, uy_max, uy_min);
380 
381  if (!checkwindow(t1.x, t2.x, t3.x, ux_max, ux_min))
382  continue;
383  if (!checkwindow(t1.y, t2.y, t3.y, uy_max, uy_min))
384  continue;
385 
386  search_affine_transform(u1.x, u1.y, u2.x, u2.y, u3.x, u3.y, t1.x,
387  t1.y, t2.x, t2.y, t3.x, t3.y,
388  ux, uy, Xdim, Ydim, delaunay_tilt, bestInliers,
389  A_coarse, T_coarse, false, thrs);
390  }
391  if (verbose==1 && k%100==0)
392  progress_bar(k);
393  }
394  std::cout << "Coarse Inliers = " << bestInliers << std::endl;
395 
396 
398  if (verbose==1)
399  std::cerr << "Refinement Phase" << std::endl;
400 
401  dist_vec.initConstant(len_u, -1);
402  Matrix1D<double> t_clo(2*bestInliers), X;
403  Matrix2D<double> invref, invW2;
404  //PseudoInverseHelper pseudoInverter;
405  invref.initZeros(2*bestInliers,6);
406  invW2.initZeros(2*bestInliers,6);
407  int count = 0;
408 
410  for (int k=0; k<len_u; k++)
411  {
412  VEC_ELEM(u,0) = VEC_ELEM(ux,k);
413  VEC_ELEM(u,1) = VEC_ELEM(uy,k);
414  t_test = A_coarse*u + T_coarse;
415  t_dist.x = VEC_ELEM(t_test,0);
416  t_dist.y = VEC_ELEM(t_test,1);
417  double dist;
418  if (!select_Closest_Point(&delaunay_tilt, &t_dist, &t_closest, &dist) )
419  {
420  //std::cerr << "WARNING IN TRIANGULATION OR CLOSEST NEIGHBOUR (in Coarse Phase)" << std::endl;
421  //std::cout << "Maybe the warning involves the tilt coordinates " << "(" << t_dist.x << ", " << t_dist.y << ")" << std::endl;
422  continue;
423  }
424  if (dist<thr*particle_size)
425  {
426  VEC_ELEM(t_clo,count) = t_closest.x;
427  VEC_ELEM(t_clo,count+bestInliers) = t_closest.y;
428  MAT_ELEM(invW2,count,0) = VEC_ELEM(u,0);
429  MAT_ELEM(invW2,count,1) = VEC_ELEM(u,1);
430  MAT_ELEM(invW2,count,4) = 1;
431 
432  MAT_ELEM(invW2, count+bestInliers, 2) = VEC_ELEM(u,0);
433  MAT_ELEM(invW2, count+bestInliers, 3) = VEC_ELEM(u,1);
434  MAT_ELEM(invW2, count+bestInliers, 5) = 1;
435 
436  count = count + 1;
437  }
438 
439  }
440 
441 
442  Matrix2D<double> def_A, A_con;
443  Matrix1D<double> T_con;
444  T_con.initZeros(2);
445  A_con.initZeros(2,2);
446  def_A.initZeros(2,2);
447  def_T.initZeros(2);
448  bool flag = false;
449 
450 
451  if ((count >= bestInliers) && (count >= 0.2*thrs))
452  {
453  //std::cout << "Count > bestInliers" << std::endl;
454  h.A=invW2;
455 
456  h.b=t_clo;
457  solveLinearSystem(h,X);
458  MAT_ELEM(def_A,0,0) = VEC_ELEM(X,0);
459  MAT_ELEM(def_A,0,1) = VEC_ELEM(X,1);
460  MAT_ELEM(def_A,1,0) = VEC_ELEM(X,2);
461  MAT_ELEM(def_A,1,1) = VEC_ELEM(X,3);
462  VEC_ELEM(def_T,0) = VEC_ELEM(X,4);
463  VEC_ELEM(def_T,1) = VEC_ELEM(X,5);
464  std::cout << "Best fitting is gotten using refinement phase" << std::endl;
465  std::cout << "Refinement Inliers = " << count << std::endl;
466  std::cout << "Coarse Inliers = " << bestInliers << std::endl;
467  std::cout << "A" << def_A << std::endl;
468  std::cout << "---------------------------" << std::endl;
469  std::cout << "T" << def_T << std::endl;
470  flag = true;
471  }
472  if (((count <= bestInliers) && (bestInliers >= 0.2*thrs)) && !flag)
473  {
474  std::cout << "Best fitting is gotten using coarse phase" << std::endl;
475  std::cout << "Coarse Inliers = " << bestInliers << std::endl;
476  std::cout << "Refinement Inliers = " << count << std::endl;
477  def_A = A_coarse;
478  def_T = T_coarse;
479  std::cout << "A" << def_A << std::endl;
480  std::cout << "---------------------------" << std::endl;
481  std::cout << "T" << def_T << std::endl;
482  flag = true;
483  }
484  if (((count <=bestInliers || count >=bestInliers) && ((count < 0.2*thrs) || (bestInliers < 0.2*thrs))) && !flag)
485  {
486  std::cerr << " Matching not found" << std::endl;
487  std::cerr << " Starting contingency method" << std::endl;
488  std::cerr << " This phase can take a long time" << std::endl;
489  //Defining triangles in untilted space
490  double window=4*particle_size;
491  double window_t=window *(1-0.34); //0.34 (approx 0.3) is cos(70), tilts higher than 70 degrees are not considered
492  std::vector<Matrix1D<double> > allTrianglesU;
493  Matrix1D<double> triangleu(7);
494  for (int k = 0; k< len_u; k++)
495  {
496  VEC_ELEM(triangleu,0)=VEC_ELEM(ux,k);
497  VEC_ELEM(triangleu,1)=VEC_ELEM(uy,k);
498  for (int j=k+1; j<len_u; j++)
499  {
500  if ( ( (VEC_ELEM(ux,j)>VEC_ELEM(ux,k)+window) || (VEC_ELEM(ux,j)<VEC_ELEM(ux,k)-window)) ||
501  ( (VEC_ELEM(uy,j)>VEC_ELEM(uy,k)+window) || (VEC_ELEM(uy,j)<VEC_ELEM(uy,k)-window)) )
502  continue;
503  VEC_ELEM(triangleu,2)=VEC_ELEM(ux,j);
504  VEC_ELEM(triangleu,3)=VEC_ELEM(uy,j);
505  for (int l=j+1; l<len_u; l++)
506  {
507  if ( ( (VEC_ELEM(ux,l)>VEC_ELEM(ux,k)+window) || (VEC_ELEM(ux,l)<VEC_ELEM(ux,k)-window)) ||
508  ( (VEC_ELEM(uy,l)>VEC_ELEM(uy,k)+window) || (VEC_ELEM(uy,l)<VEC_ELEM(uy,k)-window)) )
509  continue;
510 
511  VEC_ELEM(triangleu,6) = triangle_area(VEC_ELEM(ux,k), VEC_ELEM(uy,k), VEC_ELEM(ux,j), VEC_ELEM(uy,j), VEC_ELEM(ux,l), VEC_ELEM(uy,l));
512  if (VEC_ELEM(triangleu,6) < threshold_area)
513  continue;
514 
515  VEC_ELEM(triangleu,4) = VEC_ELEM(ux,l);
516  VEC_ELEM(triangleu,5) = VEC_ELEM(uy,l);
517  allTrianglesU.push_back(triangleu);
518  }
519  }
520  }
521  //Defining triangles in tilted space
522  std::vector<Matrix1D<double> > allTrianglesT;
523  Matrix1D<double> trianglet(7);
524  for (int k = 0; k< len_t; k++)
525  {
526  VEC_ELEM(trianglet,0)=VEC_ELEM(tx,k);
527  VEC_ELEM(trianglet,1)=VEC_ELEM(ty,k);
528  for (int j=k+1; j<len_t; j++)
529  {
530  if ( ( (VEC_ELEM(tx,j)>VEC_ELEM(tx,k)+window_t) || (VEC_ELEM(tx,j)<VEC_ELEM(tx,k)-window_t)) ||
531  ( (VEC_ELEM(ty,j)>VEC_ELEM(ty,k)+window_t) || (VEC_ELEM(ty,j)<VEC_ELEM(ty,k)-window_t)) )
532  continue;
533  VEC_ELEM(trianglet,2)=VEC_ELEM(tx,j);
534  VEC_ELEM(trianglet,3)=VEC_ELEM(ty,j);
535  for (int l=j+1; l<len_t; l++)
536  {
537  if ( ( (VEC_ELEM(tx,l)>VEC_ELEM(tx,k)+window_t) || (VEC_ELEM(tx,l)<VEC_ELEM(tx,k)-window_t)) ||
538  ( (VEC_ELEM(ty,l)>VEC_ELEM(ty,k)+window_t) || (VEC_ELEM(ty,l)<VEC_ELEM(ty,k)-window_t)) )
539  continue;
540  VEC_ELEM(trianglet,6) = triangle_area(VEC_ELEM(tx,k), VEC_ELEM(ty,k), VEC_ELEM(tx,j), VEC_ELEM(ty,j), VEC_ELEM(tx,l), VEC_ELEM(ty,l));
541  VEC_ELEM(trianglet,4) = VEC_ELEM(tx,l);
542  VEC_ELEM(trianglet,5) = VEC_ELEM(ty,l);
543  allTrianglesT.push_back(trianglet);
544  }
545  }
546  }
547 
548  //Searching the affine application
549 
550  for (size_t ku=0; ku<allTrianglesU.size(); ku++)
551  {
552 
553  const Matrix1D<double> &triangleU=allTrianglesU[ku];
554 
555  for (size_t kt=0; kt<allTrianglesT.size(); kt++)
556  {
557  const Matrix1D<double> &triangleT=allTrianglesT[kt];
558 
559  if (VEC_ELEM(triangleU,6)<VEC_ELEM(triangleT,6))
560  continue;
561 
562  search_affine_transform(VEC_ELEM(triangleU,0), VEC_ELEM(triangleU,1), VEC_ELEM(triangleU,2), VEC_ELEM(triangleU,3),
563  VEC_ELEM(triangleU,4), VEC_ELEM(triangleU,5), VEC_ELEM(triangleT,0), VEC_ELEM(triangleT,1),
564  VEC_ELEM(triangleT,2), VEC_ELEM(triangleT,3), VEC_ELEM(triangleT,4), VEC_ELEM(triangleT,5),
565  ux, uy, Xdim, Ydim, delaunay_tilt, bestInliers_con,
566  A_con, T_con, true, thrs);
567  }
568  }
569  std::cout << "Contingency Inliers = " << bestInliers_con << std::endl;
570  if ((bestInliers_con > bestInliers) && (bestInliers_con>count))
571  {
572  std::cout << "Best fitting is gotten using contingency phase" << std::endl;
573  std::cout << "Contingency Inliers = " << bestInliers_con << std::endl;
574  std::cout << "Refinement Inliers = " << count << std::endl;
575  def_A = A_con;
576  def_T = T_con;
577  std::cout << "A" << def_A << std::endl;
578  std::cout << "---------------------------" << std::endl;
579  std::cout << "T" << def_T << std::endl;
580  }
581 // else
582 // {
583 // def_A = A_coarse;
584 // def_T = T_coarse;
585 // }
586  }
587 
589 
590  for (int k=0; k<len_u; k++)
591  {
592  VEC_ELEM(u,0) = VEC_ELEM(ux,k);
593  VEC_ELEM(u,1) = VEC_ELEM(uy,k);
594  t_test = def_A*u + def_T;
595  t_dist.x = VEC_ELEM(t_test,0);
596  t_dist.y = VEC_ELEM(t_test,1);
597  double dist;
598  if (!select_Closest_Point(&delaunay_tilt, &t_dist, &t_closest, &dist) )
599  {
600  //std::cerr << "WARNING IN TRIANGULATION OR CLOSEST NEIGHBOUR (in writing phase)" << std::endl;
601  //std::cout << "Maybe the warning involves the tilt coordinates " << "(" << t_dist.x << ", " << t_dist.y << ")" << std::endl;
602  continue;
603  }
604 
605  if (dist<thr*particle_size)
606  {
607  objId = mduntilt.addObject();
608  mduntilt.setValue(MDL_XCOOR,(int) VEC_ELEM(u,0),objId);
609  mduntilt.setValue(MDL_YCOOR,(int) VEC_ELEM(u,1),objId);
610 
611  objId = mdtilt.addObject();
612  mdtilt.setValue(MDL_XCOOR,(int) t_closest.x,objId);
613  mdtilt.setValue(MDL_YCOOR,(int) t_closest.y,objId);
614  }
615  }
616  delete_Delaunay(&delaunay_untilt);
617  delete_Delaunay(&delaunay_tilt);
618  }
619  }
620  else
621  {
622  std::cout << "WARNING: input pos. file does not exist, therefore output files are empty" << std::endl;
623  }
624  if (flag_assign == false)
625  {
626  std::cout << "WARNING: input pos file is empty." << std::endl;
627  }
628 
629  mduntilt.write((String)"particles@"+fndir+'/'+fnuntilt.getBaseName() + ".pos" );
630  mdtilt.write((String)"particles@"+fndir+'/'+fntilt.getBaseName() + ".pos" );
632 
633 }
void init_progress_bar(long total)
#define DBL_EPSILON
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
double getDoubleParam(const char *param, int arg=0)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
Definition: point.h:12
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
void def_affinity(double u1x, double u1y, double u2x, double u2y, double u3x, double u3y, double t1x, double t1y, double t2x, double t2y, double t3x, double t3y, Matrix2D< double > &A, Matrix1D< double > &T, Matrix2D< double > &invW)
Definition: geometry.cpp:441
void sqrt(Image< double > &op)
void delete_Delaunay(struct Delaunay_T *delaunay)
Definition: delaunay.cpp:123
static double * y
#define A1D_ELEM(v, i)
void getImageSize(const MetaData &md, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim, MDLabel image_label)
int get_Face_Points(struct Delaunay_T *delaunay, int face_ID, struct Point_T *p, struct Point_T *q, struct Point_T *r)
Definition: delaunay.cpp:638
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
POINT_T x
Definition: point.h:14
virtual IdIteratorProxy< false > ids()
bool existsTrim() const
doublereal * x
size_t size() const override
#define i
void search_affine_transform(float u1x, float u1y, float u2x, float u2y, float u3x, float u3y, float t1x, float t1y, float t2x, float t2y, float t3x, float t3y, const Matrix1D< double > &ux, const Matrix1D< double > &uy, size_t Xdim, size_t Ydim, struct Delaunay_T &delaunay_tilt, int &bestInliers, Matrix2D< double > &A_coarse, Matrix1D< double > &T_coarse, bool contingency, int thrs)
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
int insert_Point(struct Delaunay_T *delaunay, double x, double y)
Definition: delaunay.cpp:142
double triangle_area(double x1, double y1, double x2, double y2, double x3, double y3)
Definition: geometry.cpp:511
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void findMaximumMinimum(const float u1, const float u2, const float u3, double &u_max, double &u_min)
struct DCEL_T * dcel
Definition: delaunay.h:17
long flag
Matrix1D< double > b
const char * getParam(const char *param, int arg=0)
bool setValue(const MDObject &mdValueIn, size_t id)
bool select_Closest_Point(struct Delaunay_T *delaunay, struct Point_T *p, struct Point_T *q, double *lowest_Distance)
Definition: delaunay.cpp:873
size_t addObject() override
Matrix2D< double > A
X component (int)
void progress_bar(long rlen)
int write_DCEL(struct DCEL_T *dcel, int type, const char *fileName)
Definition: dcel.cpp:301
int verbose
Verbosity level.
void initZeros()
Definition: matrix1d.h:592
void solveLinearSystem(PseudoInverseHelper &h, Matrix1D< double > &result)
#define j
POINT_T y
Definition: point.h:15
int get_Number_Real_Faces(struct DCEL_T *dcel)
Definition: dcel.cpp:1568
bool getValue(MDObject &mdValueOut, size_t id) const override
int round(double x)
Definition: ap.cpp:7245
std::string String
Definition: xmipp_strings.h:34
void initZeros()
Definition: matrix2d.h:626
int init_Delaunay(struct Delaunay_T *delaunay, int nPoints)
Definition: delaunay.cpp:67
doublereal * u
int create_Delaunay_Triangulation(struct Delaunay_T *delaunay, int createVoronoi)
Definition: delaunay.cpp:188
Y component (int)
void addUsageLine(const char *line, bool verbatim=false)
#define PI
Definition: tools.h:43
void initConstant(T val)
Definition: matrix1d.cpp:83
FileName getBaseName() const
bool checkwindow(const float t1, const float t2, const float t3, const double u_max, const double u_min)
void addParamsLine(const String &line)