Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | List of all members
ProgassignmentTiltPair Class Reference

#include <image_assignment_tilt_pair.h>

Inheritance diagram for ProgassignmentTiltPair:
Inheritance graph
[legend]
Collaboration diagram for ProgassignmentTiltPair:
Collaboration graph
[legend]

Public Member Functions

void defineParams ()
 
void readParams ()
 
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)
 
void findMaximumMinimum (const float u1, const float u2, const float u3, double &u_max, double &u_min)
 
bool checkwindow (const float t1, const float t2, const float t3, const double u_max, const double u_min)
 
void run ()
 
- Public Member Functions inherited from XmippProgram
const char * getParam (const char *param, int arg=0)
 
const char * getParam (const char *param, const char *subparam, int arg=0)
 
int getIntParam (const char *param, int arg=0)
 
int getIntParam (const char *param, const char *subparam, int arg=0)
 
double getDoubleParam (const char *param, int arg=0)
 
double getDoubleParam (const char *param, const char *subparam, int arg=0)
 
float getFloatParam (const char *param, int arg=0)
 
float getFloatParam (const char *param, const char *subparam, int arg=0)
 
void getListParam (const char *param, StringVector &list)
 
int getCountParam (const char *param)
 
bool checkParam (const char *param)
 
bool existsParam (const char *param)
 
void addParamsLine (const String &line)
 
void addParamsLine (const char *line)
 
ParamDefgetParamDef (const char *param) const
 
virtual void quit (int exit_code=0) const
 
virtual int tryRun ()
 
void initProgress (size_t total, size_t stepBin=60)
 
void setProgress (size_t value=0)
 
void endProgress ()
 
void processDefaultComment (const char *param, const char *left)
 
void setDefaultComment (const char *param, const char *comment)
 
virtual void initComments ()
 
void setProgramName (const char *name)
 
void addUsageLine (const char *line, bool verbatim=false)
 
void clearUsage ()
 
void addExampleLine (const char *example, bool verbatim=true)
 
void addSeeAlsoLine (const char *seeAlso)
 
void addKeywords (const char *keywords)
 
const char * name () const
 
virtual void usage (int verb=0) const
 
virtual void usage (const String &param, int verb=2)
 
int version () const
 
virtual void show () const
 
virtual void read (int argc, const char **argv, bool reportErrors=true)
 
virtual void read (int argc, char **argv, bool reportErrors=true)
 
void read (const String &argumentsLine)
 
 XmippProgram ()
 
 XmippProgram (int argc, const char **argv)
 
virtual ~XmippProgram ()
 

Public Attributes

FileName fntilt
 
FileName fnuntilt
 
FileName fndir
 
FileName fnmic
 
int mshift
 
double particle_size
 
double thr
 
double tiltest
 
MetaDataVec mdPartial
 
- Public Attributes inherited from XmippProgram
bool doRun
 
bool runWithoutArgs
 
int verbose
 Verbosity level. More...
 
int debug
 

Additional Inherited Members

- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 
- Protected Attributes inherited from XmippProgram
int errorCode
 
ProgramDefprogDef
 Program definition and arguments parser. More...
 
std::map< String, CommentListdefaultComments
 
int argc
 Original command line arguments. More...
 
const char ** argv
 

Detailed Description

Definition at line 41 of file image_assignment_tilt_pair.h.

Member Function Documentation

◆ checkwindow()

bool ProgassignmentTiltPair::checkwindow ( const float  t1,
const float  t2,
const float  t3,
const double  u_max,
const double  u_min 
)

Definition at line 209 of file image_assignment_tilt_pair.cpp.

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 }

◆ defineParams()

void ProgassignmentTiltPair::defineParams ( )
virtual

Function in which the param of each Program are defined.

Reimplemented from XmippProgram.

Definition at line 47 of file image_assignment_tilt_pair.cpp.

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 }
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)

◆ findMaximumMinimum()

void ProgassignmentTiltPair::findMaximumMinimum ( const float  u1,
const float  u2,
const float  u3,
double &  u_max,
double &  u_min 
)

Definition at line 181 of file image_assignment_tilt_pair.cpp.

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 }

◆ readParams()

void ProgassignmentTiltPair::readParams ( )
virtual

Function in which each program will read parameters that it need. If some error occurs the usage will be printed out.

Reimplemented from XmippProgram.

Definition at line 35 of file image_assignment_tilt_pair.cpp.

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 }
double getDoubleParam(const char *param, int arg=0)
const char * getParam(const char *param, int arg=0)

◆ run()

void ProgassignmentTiltPair::run ( )
virtual

This function will be start running the program. it also should be implemented by derived classes.

Reimplemented from XmippProgram.

Definition at line 225 of file image_assignment_tilt_pair.cpp.

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 VEC_ELEM(v, i)
Definition: matrix1d.h:245
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
Definition: point.h:12
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
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)
long flag
Matrix1D< double > b
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 verbose
Verbosity level.
void initZeros()
Definition: matrix1d.h:592
void solveLinearSystem(PseudoInverseHelper &h, Matrix1D< double > &result)
#define j
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)
#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)

◆ search_affine_transform()

void ProgassignmentTiltPair::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 
)

Definition at line 63 of file image_assignment_tilt_pair.cpp.

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 }
#define DBL_EPSILON
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
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)
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
struct DCEL_T * dcel
Definition: delaunay.h:17
bool select_Closest_Point(struct Delaunay_T *delaunay, struct Point_T *p, struct Point_T *q, double *lowest_Distance)
Definition: delaunay.cpp:873
int write_DCEL(struct DCEL_T *dcel, int type, const char *fileName)
Definition: dcel.cpp:301
void initZeros()
Definition: matrix2d.h:626
doublereal * u
void initConstant(T val)
Definition: matrix1d.cpp:83

Member Data Documentation

◆ fndir

FileName ProgassignmentTiltPair::fndir

Definition at line 47 of file image_assignment_tilt_pair.h.

◆ fnmic

FileName ProgassignmentTiltPair::fnmic

Definition at line 47 of file image_assignment_tilt_pair.h.

◆ fntilt

FileName ProgassignmentTiltPair::fntilt

Filenames

Definition at line 47 of file image_assignment_tilt_pair.h.

◆ fnuntilt

FileName ProgassignmentTiltPair::fnuntilt

Definition at line 47 of file image_assignment_tilt_pair.h.

◆ mdPartial

MetaDataVec ProgassignmentTiltPair::mdPartial

Definition at line 61 of file image_assignment_tilt_pair.h.

◆ mshift

int ProgassignmentTiltPair::mshift

Maximum shift

Definition at line 50 of file image_assignment_tilt_pair.h.

◆ particle_size

double ProgassignmentTiltPair::particle_size

Particle size

Definition at line 53 of file image_assignment_tilt_pair.h.

◆ thr

double ProgassignmentTiltPair::thr

threshold

Definition at line 56 of file image_assignment_tilt_pair.h.

◆ tiltest

double ProgassignmentTiltPair::tiltest

Tilt estimation angle

Definition at line 59 of file image_assignment_tilt_pair.h.


The documentation for this class was generated from the following files: