Bsoft  2.1.0
Bernard's software package
mg_tomo_track.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "mg_tomography.h"
11 #include "mg_tomo_track.h"
12 #include "mg_select.h"
13 #include "mg_img_proc.h"
14 #include "linked_list.h"
15 #include "qsort_functions.h"
16 #include "random_numbers.h"
17 #include "utilities.h"
18 #include "timer.h"
19 
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22 
23 // Internal function prototypes
25  Bimage* pgold, double thickness, int cc_type, int iz, double* z);
26 int mg_z_matrix_update(Bmarker* model, int nmg, int nmark, double* z, double* w, int recenter);
27 
28 
29 double fom_from_distance(double distance, double sigma)
30 {
31  double r = distance/sigma;
32 
33  double fom = exp(-0.5*r*r);
34 
35  return fom;
36 }
37 
39 {
40  for ( ; m1 && m2; m1 = m1->next, m2 = m2->next )
41  cout << m1->id << tab << m1->loc << tab << m2->loc << endl;
42 
43  return 0;
44 }
45 
60  double radius, Bimage* pmark, Bmarker* mark)
61 {
62  double outrad = sqrt(2.0)*radius;
63  Bmarker* m;
64  Vector3<double> loc;
65 
66  Bimage* p = new Bimage(Float, TSimple, size, 1);
67  p->origin(p->size()/2);
68 
69  if ( pmark ) {
70  p->fill(pmark->background(long(0)));
71  for ( m = mark; m; m = m->next ) if ( m->sel ) {
72  loc = m->loc - offset;
73  p->place(0, pmark, loc, outrad, 1, 0, 1);
74  }
75  } else {
76  for ( m = mark; m; m = m->next ) if ( m->sel ) {
77  loc = m->loc - offset;
78  p->sphere(loc, outrad, 2, FILL_USER, 1);
79  p->sphere(loc, radius, 2, FILL_USER, -1);
80  }
81  }
82 
83  p->statistics();
84 
85 // write_img("mark_proj.map", p);
86 
87  return p;
88 }
89 
91  double axis_angle, Bimage* pgold, double hi_res, double lo_res,
92  double shift_limit, fft_plan planf, fft_plan planb)
93 {
94  if ( verbose & VERB_DEBUG )
95  cout << "DEBUG mg_find_tilt_axis: axis_angle=" << axis_angle << endl;
96 
97  double cc(0);
98  Bmicrograph* mg = micrograph_copy(mgp);
99 
100  mg->matrix = Matrix3(mg->tilt_angle, axis_angle);
101 // mg->matrix = mg->matrix.transpose();
102 // cout << mg->matrix << endl;
103 
104  mg_marker_update(mg, mg_ref->mark, mg_ref->origin, 1);
105 // marker_show(mg_ref->mark, mg->mark);
106 
107  cc = mg_marker_shift(mg, pgold, hi_res, lo_res, shift_limit, planf, planb);
108 
109  micrograph_kill(mg);
110 
111  mg = micrograph_copy(mgn);
112 
113  mg->matrix = Matrix3(mg->tilt_angle, axis_angle);
114 // mg->matrix = mg->matrix.transpose();
115 // cout << mg->matrix << endl;
116 
117  mg_marker_update(mg, mg_ref->mark, mg_ref->origin, 1);
118 // marker_show(mg_ref->mark, mg->mark);
119 
120  cc += mg_marker_shift(mg, pgold, hi_res, lo_res, shift_limit, planf, planb);
121  cc /= 2;
122 
123  micrograph_kill(mg);
124 
125  if ( verbose & VERB_DEBUG )
126  cout << "DEBUG mg_find_tilt_axis: cc=" << cc << endl;
127 
128  return cc;
129 }
130 
155  double axis_start, double axis_end, double axis_step,
156  double hi_res, double lo_res, double shift_limit)
157 {
158  if ( !project->field ) return -1;
159 
160  double a(0), abest(0), ccbest(-1);
161  Bmicrograph* mg;
163  Bmicrograph* mgp = mg_ref;
164  Bmicrograph* mgn = mg_ref;
165 
166  if ( !mg_ref || !mg_ref->mark ) {
167  cerr << "Error: No zero-tilt markers specified!" << endl;
168  return 0;
169  }
170 
171  for ( mg = project->field->mg; mg; mg = mg->next )
172  if ( a < fabs(mg->tilt_angle) ) a = fabs(mg->tilt_angle);
173 
174  if ( a < tilt ) {
175  cerr << "Error: The micrograph tilt angles are smaller than the requested tilt!" << endl;
176  return 0;
177  }
178 
179  // Choose micrographs close to the given tilt angle
180  for ( mg = project->field->mg; mg; mg = mg->next ) {
181  if ( fabs(mg->tilt_angle - tilt) < fabs(mgp->tilt_angle - tilt) ) mgp = mg;
182  if ( fabs(mg->tilt_angle + tilt) < fabs(mgn->tilt_angle + tilt) ) mgn = mg;
183  }
184 
185  if ( mgp && mgp->tilt_angle <= 0.05 ) {
186  cerr << "Error: The postive test tilt is too small!" << endl;
187  return 0;
188  }
189 
190  if ( mgn && mgn->tilt_angle >= -0.05 ) {
191  cerr << "Error: The negative test tilt is too large!" << endl;
192  return 0;
193  }
194 
195  if ( verbose & VERB_DEBUG )
196  cout << "DEBUG project_find_tilt_axis: mg_ref=" << mg_ref->id << " mgp=" << mgp->id << " mgn=" << mgn->id << endl;
197 
198  kill_list((char *) mgp->mark, sizeof(Bmarker));
199  kill_list((char *) mgn->mark, sizeof(Bmarker));
200 
201  // Generate a composite gold particle image for cross-correlation
202  int cc_size = (int) (4*mg_ref->mark_radius);
203  if ( cc_size < 30 ) cc_size = 30;
204  Vector3<long> gsize(cc_size, cc_size, 1);
205  Bimage* pgold = mg_composite_particle(mg_ref, gsize);
206 
207  if ( verbose ) {
208  cout << "Finding the tilt axis angle:" << endl;
209  cout << "Test tilt angle: " << tilt*180/M_PI << endl;
210  cout << "Micrograph tilt angles: " << mgn->tilt_angle*180/M_PI
211  << " " << mgp->tilt_angle*180/M_PI << endl;
212  cout << "Micrographs: " << mgn->id << " " << mgp->id << endl;
213  cout << "Axis start, end and step: " << axis_start*180/M_PI << " "
214  << axis_end*180/M_PI << " " << axis_step*180/M_PI << endl;
215  cout << "Resolution limits: " << hi_res << " - " << lo_res << endl << endl;
216  }
217 
218  long i, na(0);
219  for ( a = axis_start; a < axis_end + 0.1*axis_step; a += axis_step ) na++;
220 
221  double* ar = new double[na];
222  double* cc = new double[na];
223 
224  for ( i=0, a = axis_start; a < axis_end + 0.1*axis_step; a += axis_step, i++ ) {
225  ar[i] = a;
226  cc[i] = 0;
227  }
228 
229  Vector3<long> size = micrograph_get_size(mg_ref);
230 
231  fft_plan planf = fft_setup_plan(size, FFTW_FORWARD, 1);
232  fft_plan planb = fft_setup_plan(size, FFTW_BACKWARD, 1);
233 // fft_plan planf = fft_setup_plan(size, FFTW_FORWARD, 0);
234 // fft_plan planb = fft_setup_plan(size, FFTW_BACKWARD, 0);
235 
236 #ifdef HAVE_GCD
237  dispatch_apply(na, dispatch_get_global_queue(0, 0), ^(size_t i){
238  cc[i] = mg_find_tilt_axis(mgp, mgn, mg_ref, ar[i], pgold, hi_res, lo_res, shift_limit, planf, planb);
239  });
240 #else
241 #pragma omp parallel for
242  for ( i=0; i<na; i++ ) {
243  cc[i] = mg_find_tilt_axis(mgp, mgn, mg_ref, ar[i], pgold, hi_res, lo_res, shift_limit, planf, planb);
244  }
245 #endif
246 
249 
250  if ( verbose )
251  cout << "Angle\tCC" << endl;
252  for ( i=0; i<na; i++ ) {
253  if ( ccbest < cc[i] ) {
254  ccbest = cc[i];
255  abest = ar[i];
256  }
257  if ( verbose )
258  cout << ar[i]*180.0/M_PI << tab << cc[i] << endl;
259  }
260 
261  delete[] ar;
262  delete[] cc;
263  delete pgold;
264 
265  if ( verbose ) {
266  cout << "Best tilt axis angle: " << abest*180.0/M_PI << " degrees" << endl;
267  cout << "Alternative tilt axis angle: " << angle_set_negPI_to_PI(abest-M_PI)*180.0/M_PI << " degrees" << endl;
268  cout << "Best correlation coefficient: " << ccbest << endl << endl;
269  }
270 
271  kill_list((char *) mgp->mark, sizeof(Bmarker));
272  kill_list((char *) mgn->mark, sizeof(Bmarker));
273  mgp->mark = NULL;
274  mgn->mark = NULL;
275 
276  for ( mg = project->field->mg; mg; mg = mg->next )
277  mg->tilt_axis = abest;
278 
279  return ccbest;
280 }
281 
282 
314 int project_track_markers(Bproject* project, double hi_res, double lo_res,
315  double shift_limit, double thickness, int max_cycle,
316  double target, int cc_type, int recenter, Bstring paramfile)
317 {
318  if ( max_cycle > 20 ) max_cycle = 20;
319 
320  int i, j, nmark, n, iref(0);
321  Bfield* field = project->field;
322  Bmicrograph* mg = NULL;
324  Breconstruction* rec = project->rec;
325  Bmarker* model = rec->mark;
326  Bmarker* mark = NULL;
327  Bmarker* mark2 = NULL;
328  Bmarker* model2 = NULL;
329 
330  if ( mg_ref->mark_radius < 1 )
331  return error_show("Error: Cannot track markers with zero radius reference!", __FILE__, __LINE__);
332 
333  if ( target < 0.1 ) target = mg_ref->mark_radius/10;
334 
335  if ( !model )
336  return error_show("No seed markers found!", __FILE__, __LINE__);
337 
338  if ( verbose ) {
339  cout << "Tracking markers:" << endl;
340  cout << "Maximum number of iterations: " << max_cycle << endl;
341  cout << "Marker radius: " << mg_ref->mark_radius << endl;
342  cout << "Model origin: " << rec->origin << endl << endl;
343  }
344 
345  if ( paramfile.length() < 1 ) paramfile = "btrack_param.star";
346  Bstring filename;
347 
348  // Generate a composite gold particle image for cross-correlation
349  int cc_size = (int) (4*mg_ref->mark_radius);
350  if ( cc_size < 30 ) cc_size = 30;
351  Vector3<long> gsize(cc_size, cc_size, 1);
352  Bimage* pgold = mg_composite_particle(mg_ref, gsize);
353  write_img("marker_track_ref.map", pgold, 0);
354 
355  for ( nmark=0, mark = model; mark; mark = mark->next, nmark++ ) ;
356  model2 = markers_copy(model);
357 
358  int nmg = field_count_micrographs(field);
359  int cycle, iz;
360  double R(100);
361 
362  double* z = new double[nmark*nmg];
363  double* w = new double[nmg];
364  Bmicrograph** mglist = new Bmicrograph*[nmg];
365 
366  for ( i=0; i<nmg*nmark; i++ ) z[i] = -1e30;
367 
368  for ( i=0, mg = field->mg; mg; mg = mg->next, i++ ) {
369  mglist[i] = mg;
370  if ( mg == mg_ref ) iref = i;
371  w[i] = 1/(1 + exp(0.2*(0.35 - fabs(mg->tilt_angle))));
372  }
373 
374  int imax = nmg - iref;
375  if ( imax < iref ) imax = iref;
376 
377  Vector3<long> size = micrograph_get_size(mg_ref);
378 
379 // fft_plan planf = fft_setup_plan(size, FFTW_FORWARD, 1);
380 // fft_plan planb = fft_setup_plan(size, FFTW_BACKWARD, 1);
381  fft_plan planf = fft_setup_plan(size, FFTW_FORWARD, 0);
382  fft_plan planb = fft_setup_plan(size, FFTW_BACKWARD, 0);
383 
384  for ( cycle = 0; R > target && cycle < max_cycle; cycle++ ) {
385 
386  for ( mark=model, mark2=model2; mark; mark=mark->next, mark2=mark2->next )
387  mark2->loc = mark->loc;
388 
389  for ( i=1; i<=imax; i++ ) {
390  cout << "Processing images " << iref-i << " and " << iref+i << endl;
391  iz = iref - i;
392  if ( iz >= 0 ) {
393  mg = mglist[iz];
394  if ( mg->select ) {
395  mg_marker_update(mg, model, rec->origin, 1);
396  mg_marker_shift(mg, pgold, hi_res, lo_res, shift_limit, planf, planb);
397  mg_marker_z_search(mg, model, rec->origin, pgold, thickness, cc_type, iz*nmark, z);
398  }
399  }
400 // bexit(0);
401  iz = iref + i;
402  if ( iz < nmg ) {
403  mg = mglist[iz];
404  if ( mg->select ) {
405  mg_marker_update(mg, model, rec->origin, 1);
406  mg_marker_shift(mg, pgold, hi_res, lo_res, shift_limit, planf, planb);
407  mg_marker_z_search(mg, model, rec->origin, pgold, thickness, cc_type, iz*nmark, z);
408  }
409  }
410  mg_z_matrix_update(model, nmg, nmark, z, w, recenter);
411  }
412 
413  for ( mg = field->mg; mg; mg = mg->next )
414  mg_marker_update(mg, model, rec->origin, 1);
415 
416  R = 0;
417  for ( n=0, mark=model, mark2=model2; mark; mark=mark->next, mark2=mark2->next ) if ( mark2->sel ) {
418  R += (mark->loc - mark2->loc).length2();
419  n++;
420  }
421  R = sqrt(R/n);
422  cout << "Cycle " << cycle+1 << ":\tAverage change in positions = " << R << " (" << n << ")" << endl;
423 
424  if ( max_cycle > 1 ) {
425  filename = paramfile.pre_rev('.') + Bstring(cycle+1, "_%02d.") + paramfile.post_rev('.');
426  write_project(filename, project);
427  }
428  }
429 
432 
433  kill_list((char *) model2, sizeof(Bmarker));
434 
435  cout << "Marker z coordinate matrix:\nMicrograph\tw";
436  for ( j=0; j<nmark; j++ ) cout << tab << j+1;
437  cout << endl;
438  for ( i=0, mg=field->mg; i<nmg; i++, mg=mg->next ) if ( mg != mg_ref && mg->select ) {
439  cout << mg->id << ":\t" << w[i];
440  for ( j=0; j<nmark; j++ ) cout << tab << z[i*nmark+j];
441  cout << endl;
442  }
443 
444  delete[] z;
445  delete[] w;
446  delete[] mglist;
447  delete pgold;
448 
449  if ( verbose ) {
450  cout << "Marker\tx\ty\tz\tResidual" << endl;
451  for ( mark = model; mark; mark = mark->next )
452  cout << mark->id << tab << mark->loc[0] << tab << mark->loc[1] << tab << mark->loc[2] << tab << mark->res << endl;
453  cout << endl;
454  }
455 
456  return 0;
457 }
458 
490 int project_track_markers_dual(Bproject* project, double hi_res, double lo_res,
491  double shift_limit, double thickness, int max_cycle,
492  double target, int cc_type, int recenter, Bstring paramfile)
493 {
494  if ( max_cycle > 20 ) max_cycle = 20;
495  if ( thickness < 100 ) thickness = 1000;
496 
497  long i, j, k, h, nmark, n, jref(0);
498  Bstring filename;
499  Bfield* field = project->field;
500  Bmicrograph* mg = NULL;
501  Bmicrograph* mgr = NULL;
503  Breconstruction* rec = project->rec;
504  Bmarker* model = rec->mark;
505  Bmarker* mark = NULL;
506  Bmarker* mark2 = NULL;
507  Bmarker* model2 = NULL;
508 
509  if ( mg_ref->mark_radius < 1 )
510  return error_show("Error: Cannot track markers with zero radius reference!", __FILE__, __LINE__);
511 
512  if ( target < 0.1 ) target = mg_ref->mark_radius/10;
513 
514  if ( !model )
515  return error_show("No seed markers found!", __FILE__, __LINE__);
516 
517  if ( verbose ) {
518  cout << "Tracking markers:" << endl;
519  cout << "Maximum number of iterations: " << max_cycle << endl;
520  cout << "Marker radius: " << mg_ref->mark_radius << endl;
521  cout << "Model origin: " << rec->origin << endl << endl;
522  }
523 
524  if ( paramfile.length() < 1 ) paramfile = "btrack_param.star";
525 
526  // Generate a composite gold particle image for cross-correlation
527  long cc_size = (long) (4*mg_ref->mark_radius);
528  if ( cc_size < 30 ) cc_size = 30;
529  Vector3<long> gsize(cc_size, cc_size, 1);
530  Bimage* pgold = mg_composite_particle(mg_ref, gsize);
531  write_img("marker_track_ref.map", pgold, 0);
532 
533  for ( nmark=0, mark = model; mark; mark = mark->next, nmark++ ) ;
534  model2 = markers_copy(model);
535 
537  long cycle;
538  double R(100);
539 
540  int* seq = new int[nmg];
541  double* z = new double[nmark*nmg];
542  double* w = new double[nmg];
543  Bmicrograph** mglist = new Bmicrograph*[nmg];
544 
545  for ( i=0; i<nmg*nmark; i++ ) z[i] = -1e30;
546 
547  if ( verbose )
548  cout << "Sequence:" << endl;
549  for ( i=k=0, field = project->field; field; field = field->next ) {
551  for ( n=0, mg = field->mg; mg; mg = mg->next, n++ )
552  if ( mg == mgr ) jref = n;
553  for ( j=1, h=0, mg = field->mg; mg; mg = mg->next, i++ ) {
554  mglist[i] = mg;
555  seq[i] = -1;
556  if ( jref + j < n && h ) seq[i] = k + jref + j;
557  else if ( jref - j >= 0 && h == 0 ) seq[i] = k + jref - j;
558  if ( h ) j++;
559  h = 1 - h;
560  w[i] = 1/(1 + exp(0.2*(0.35 - fabs(mg->tilt_angle))));
561  if ( verbose )
562  cout << " " << seq[i];
563  }
564  k += n;
565  }
566 
567  if ( verbose )
568  cout << endl;
569 
570  Vector3<long> size = micrograph_get_size(mg_ref);
571 
572 // fft_plan planf = fft_setup_plan(size, FFTW_FORWARD, 1);
573 // fft_plan planb = fft_setup_plan(size, FFTW_BACKWARD, 1);
574  fft_plan planf = fft_setup_plan(size, FFTW_FORWARD, 0);
575  fft_plan planb = fft_setup_plan(size, FFTW_BACKWARD, 0);
576 
577  for ( cycle = 0; R > target && cycle < max_cycle; cycle++ ) {
578 
579  for ( mark=model, mark2=model2; mark; mark=mark->next, mark2=mark2->next )
580  mark2->loc = mark->loc;
581 
582  for ( i=0; i<nmg; i++ ) if ( seq[i] >= 0 ) {
583  cout << "Processing image " << seq[i] << endl;
584  mg = mglist[seq[i]];
585  if ( mg->select ) {
586  mg_marker_update(mg, model, rec->origin, 1);
587  mg_marker_shift(mg, pgold, hi_res, lo_res, shift_limit, planf, planb);
588  mg_marker_z_search(mg, model, rec->origin, pgold, thickness, cc_type, seq[i]*nmark, z);
589  mg_z_matrix_update(model, nmg, nmark, z, w, recenter);
590  }
591  }
592 
593  for ( field = project->field; field; field = field->next )
594  for ( mg = field->mg; mg; mg = mg->next )
595  mg_marker_update(mg, model, rec->origin, 1);
596 
597  R = 0;
598  for ( n=0, mark=model, mark2=model2; mark; mark=mark->next, mark2=mark2->next ) if ( mark2->sel ) {
599  R += (mark->loc - mark2->loc).length2();
600  n++;
601  }
602  R = sqrt(R/n);
603  cout << "Cycle " << cycle+1 << ":\tAverage change in positions = " << R << " (" << n << ")" << endl;
604 
605  if ( max_cycle > 1 ) {
606  filename = paramfile.pre_rev('.') + Bstring(cycle+1, "_%02d.") + paramfile.post_rev('.');
607  write_project(filename, project);
608  }
609  }
610 
613 
614  kill_list((char *) model2, sizeof(Bmarker));
615 
616  if ( verbose & VERB_PROCESS ) {
617  cout << "Marker z coordinate matrix:\nMicrograph\tw";
618  for ( j=0; j<nmark; j++ ) cout << tab << j+1;
619  cout << endl;
620  for ( i=0, field = project->field; field; field = field->next ) {
621  for ( mg = field->mg; mg; i++, mg = mg->next ) if ( mg != mg_ref && mg->select ) {
622  cout << mg->id << ":\t" << w[i];
623  for ( j=0; j<nmark; j++ ) cout << tab << z[i*nmark+j];
624  cout << endl;
625  }
626  }
627  }
628 
629  delete[] seq;
630  delete[] z;
631  delete[] w;
632  delete[] mglist;
633  delete pgold;
634 
635  if ( verbose ) {
636  cout << "Marker\tx\ty\tz\tResidual" << endl;
637  for ( mark = model; mark; mark = mark->next )
638  cout << mark->id << tab << mark->loc[0] << tab << mark->loc[1] << tab << mark->loc[2] << tab << mark->res << endl;
639  cout << endl;
640  }
641 
642  return 0;
643 }
644 
645 double marker_refine(Bmarker* mark, Bmarker* marklist, Bimage* p, Bimage* pgold,
646  double radius, double hi_res, double lo_res)
647 {
648  if ( !p->within_boundaries(mark->loc) ) return 0;
649 
650  Vector3<long> extsize = pgold->size() * 2;
651  if ( pgold->sizeZ() < 2 ) extsize[2] = 1;
652  Vector3<double> extori(extsize[0]/2, extsize[1]/2, extsize[2]/2);
653 
654  Bimage* pmark = img_marker_projection(extsize, (mark->loc - extori),
655  radius, pgold, marklist);
656 
657  Bimage* pex = p->extract(0, (mark->loc - extori), extsize);
658  pex->origin(extori);
659 
660  double cc(0);
661  mark->err = pex->find_shift(pmark, NULL, hi_res, lo_res, pex->sizeX()/4.0, 0.1, 1, cc);
662 
663  delete pmark;
664  delete pex;
665 
666  mark->loc += mark->err;
667  mark->res = mark->err.length();
668  mark->fom = cc;
669  if ( mark->fom < 0.0001 ) mark->fom = 0.0001;
670 
671  return cc;
672 }
673 
685 int project_refine_markers(Bproject* project, double hi_res, double lo_res)
686 {
687  int i, nmg, nmark, nms(0), nmsi;
688 
689  Bfield* field = project->field;
690  Bmicrograph* mg;
691  Bmicrograph* mg_ref = field_find_zero_tilt_mg(field);
692  Bmarker* mark = NULL;
693  Bimage* p = NULL;
694 
695  if ( !mg_ref->mark )
696  return error_show("No markers found!", __FILE__, __LINE__);
697 
698  int cc_size = (int) (6*mg_ref->mark_radius);
699  if ( cc_size < 40 ) cc_size = 40;
700  Vector3<long> gsize(cc_size, cc_size, 1);
701  Bimage* pgold = mg_composite_particle(mg_ref, gsize);
702  write_img("marker_refine_ref.map", pgold, 0);
703 
704  for ( nmark=0, mark = mg_ref->mark; mark; mark = mark->next ) nmark++;
705  double mg_shift, avg_shift, mg_fom, avg_fom, cc;
706  double* mark_shift = new double[nmark];
707  double* mark_fom = new double[nmark];
708 
709  for ( i=0; i<nmark; i++ ) mark_shift[i] = mark_fom[i] = 0;
710 
711  if ( verbose )
712  cout << "Image\tShift\tCC" << endl;
713  for ( nmg=0, field = project->field; field; field = field->next ) if ( field->select ) {
714  for ( mg = field->mg; mg; mg = mg->next, nmg++ ) if ( mg->select && mg->mark ) {
715  p = read_img(mg->fmg, 1, mg->img_num);
716  if ( !p )
717  return error_show("project_refine_markers", __FILE__, __LINE__);
718  nmsi = 0;
719  mg_shift = mg_fom = 0;
720  for ( i=0, mark = mg->mark; mark; mark = mark->next, i++ ) if ( mark->sel > 0 ) {
721  cc = marker_refine(mark, mg->mark, p, pgold, mg->mark_radius, hi_res, lo_res);
722  mark_shift[i] += mark->res;
723  mg_shift += mark->res;
724  mark_fom[i] += cc;
725  mg_fom += cc;
726  nmsi++;
727  }
728  delete p;
729  nms += nmsi;
730  if ( verbose )
731  cout << mg->id << tab << mg_shift/nmsi << tab << mg_fom/nmsi << endl;
732  }
733  }
734 
735  if ( verbose )
736  cout << "\nMarker\tShift\tCC" << endl;
737  for ( avg_shift=avg_fom=0, i=0, mark=mg_ref->mark; i<nmark; i++, mark=mark->next ) {
738  mark_shift[i] /= nmg;
739  mark_fom[i] /= nmg;
740  avg_shift += mark_shift[i];
741  avg_fom += mark_fom[i];
742  if ( verbose )
743  cout << mark->id << tab << mark_shift[i] << tab << mark_fom[i] << endl;
744  }
745 
746  if ( verbose ) {
747  cout << endl << "Average shift: " << avg_shift/nms << endl;
748  cout << "Average CC: " << avg_fom/nms << endl;
749  }
750 
751  delete[] mark_shift;
752  delete[] mark_fom;
753  delete pgold;
754 
755  return 0;
756 }
757 
770 int project_refine_one_marker(Bproject* project, int id, double hi_res, double lo_res)
771 {
772  int nmg, nms(0);
773 
774  Bfield* field = project->field;
775  Bmicrograph* mg;
776  Bmicrograph* mg_ref = field_find_zero_tilt_mg(field);
777  Bmarker* mark = NULL;
778  Bimage* p = NULL;
779 
780  if ( !mg_ref->mark )
781  return error_show("No markers found!", __FILE__, __LINE__);
782 
783  int cc_size = (int) (6*mg_ref->mark_radius);
784  if ( cc_size < 40 ) cc_size = 40;
785  Vector3<long> gsize(cc_size, cc_size, 1);
786  Bimage* pgold = mg_composite_particle(mg_ref, gsize);
787  write_img("marker_refine_ref.map", pgold, 0);
788 
789  double cc;
790  double mark_shift(0);
791  double mark_fom(0);
792 
793  for ( nmg=0, field = project->field; field; field = field->next ) if ( field->select ) {
794  for ( mg = field->mg; mg; mg = mg->next, nmg++ ) if ( mg->select && mg->mark ) {
795  p = read_img(mg->fmg, 1, mg->img_num);
796  if ( !p )
797  return error_show("project_refine_one_marker", __FILE__, __LINE__);
798  for ( mark = mg->mark; mark && mark->id != id; mark = mark->next ) ;
799  if ( mark && mark->sel > 0 ) {
800  cc = marker_refine(mark, mg->mark, p, pgold, mg->mark_radius, hi_res, lo_res);
801  mark_shift += mark->res;
802  mark_fom += cc;
803  nms++;
804  }
805  delete p;
806  }
807  }
808 
809  if ( verbose ) {
810  cout << "Marker " << id << ":" << endl;
811  cout << "Average shift: " << mark_shift/nms << endl;
812  cout << "Average CC: " << mark_fom/nms << endl << endl;
813  }
814 
815  delete pgold;
816 
817  return 0;
818 }
819 
820 
833 int mg_refine_markers(Bmicrograph* mg, Bimage* pgold, double hi_res, double lo_res)
834 {
835  if ( !mg->mark )
836  return error_show("No markers found!", __FILE__, __LINE__);
837 
838  int nmark, nms(0);
839  Bmarker* mark = NULL;
840  Bimage* p = NULL;
841 
842  double mg_shift, mg_fom, cc;
843 
844  p = read_img(mg->fmg, 1, mg->img_num);
845  if ( !p )
846  return error_show("mg_refine_markers", __FILE__, __LINE__);
847  mg_shift = mg_fom = 0;
848  for ( nmark=0, mark = mg->mark; mark; mark = mark->next, nmark++ ) if ( mark->sel > 0 ) {
849  cc = marker_refine(mark, mg->mark, p, pgold, mg->mark_radius, hi_res, lo_res);
850  mg_shift += mark->res;
851  mg_fom += cc;
852  nms++;
853  }
854  delete p;
855 
856  if ( verbose & VERB_FULL )
857  cout << mg->id << tab << mg_shift/nms << tab << mg_fom/nms << endl;
858 
859  return 0;
860 }
861 
870 double project_refine(Bproject* project, int iter, double tol, Bstring refop)
871 {
872  long i, j;
873  double R = 1e30, dR = 1e30;
874 
875  if ( verbose ) {
876  cout << "Refining model and orientation parameters:" << endl;
877  cout << "Model origin: " << project->rec->origin << endl;
878  cout << "Sequence of refinements: " << refop << endl << endl;
879  }
880 
882 
883  if ( verbose )
884  cout << "Cycle\tR\tdR" << endl;
885  for ( i = 1; i <= iter && R > 0.1 && fabs(dR) > tol; i++ ) {
886  dR = R;
887  for ( j=0; j<refop.length(); j++ ) {
888  switch ( refop[j] ) {
889  case 'z': project_refine_z(project); break;
890  case 'v': project_refine(project, 1, 0, 0); break;
891  case 'o': project_refine(project, 0, 1, 0); break;
892  case 's': project_refine(project, 0, 0, 1); break;
893  case 'a': project_refine(project, 1, 1, 1); break;
894  }
895  }
897  dR -= R;
898  if ( verbose )
899  cout << i << tab << R << tab << dR << endl;
900  }
901 
903 
904  if ( verbose )
905  cout << endl;
906 
907  return R;
908 }
909 
911 {
912  Bfield* field = project->field;
913  Bmicrograph* mg = field->mg;
914  Bmarker* m;
915  Vector3<double> oriref = project->rec->origin;
916  Vector3<double> loc, rloc;
917  Matrix3 mat(1);
918  long n(0);
919  double d, R(0), w(0);
920 
921  for ( field = project->field; field; field = field->next ) if ( field->select ) {
922  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
923  mat = mg->matrix.transpose(); // Handedness
924  for ( m = mg->mark; m && m->id != mark->id; m = m->next ) ;
925  if ( m && m->id == mark->id && m->sel > 0 ) {
926  loc = m->loc - mg->origin;
927  rloc = mark->loc - oriref;
928 // d = loc[0] - rloc[0]*mat[0] - rloc[1]*mat[1] - rloc[2]*mat[2];
929  d = loc[0] - rloc.scalar(mat[0]);
930  R += d*d*mark->fom;
931 // d = loc[1] - rloc[0]*mat[3] - rloc[1]*mat[4] - rloc[2]*mat[5];
932  d = loc[1] - rloc.scalar(mat[1]);
933  R += d*d*mark->fom;
934  n++;
935  w += mark->fom;
936  }
937  }
938  }
939 
940  if ( w ) R = sqrt(R/(2*w));
941 
942  return R;
943 }
944 
951 {
952  Bmarker* mr;
954  long n(0);
955  double oldz, zinc, d, Rz, Rp, Rm, R(0);
956 
957  if ( !model )
958  return error_show("No markers found!", __FILE__, __LINE__);
959 
960  if ( verbose & VERB_FULL )
961  cout << "Marker\tOld z\tNew z\tR" << endl;
962  for ( mr = model; mr; mr = mr->next ) if ( mr->sel ) {
963  oldz = mr->loc[2];
964  Rz = tomo_z_residual(project, mr);
965  for ( zinc = 1; zinc > 0.001; ) {
966  mr->loc[2] += zinc;
967  Rp = tomo_z_residual(project, mr);
968  if ( Rz > Rp ) {
969  Rz = Rp;
970  } else {
971  mr->loc[2] -= 2*zinc;
972  Rm = tomo_z_residual(project, mr);
973  if ( Rz > Rm ) {
974  Rz = Rm;
975  } else {
976  mr->loc[2] += zinc;
977  zinc *= 0.7;
978  }
979  }
980  }
981  if ( verbose & VERB_FULL )
982  cout << mr->id << tab << oldz << tab << mr->loc[2] << tab << Rz << endl;
983  d = mr->loc[2] - oldz;
984  R += d*d;
985  n++;
986  }
987 
988  R = sqrt(R/n);
989 
990  if ( verbose & VERB_PROCESS )
991  cout << "RMS change in z: " << R << endl;
992 
993  return R;
994 }
995 
1007 double project_refine(Bproject* project, int do_view, int do_origin, int do_scale)
1008 {
1009  if ( ( do_origin < 1 ) && ( do_scale < 1 ) && ( do_view < 1 ) ) return 0;
1010 
1011  Bfield* field;
1012  Bmicrograph* mg;
1013  Breconstruction* rec = project->rec;
1014  double att(0.001), beta(100);
1015  int accept, ncyc(0), n(0), nacc;
1016  double irm = 1.0/get_rand_max();
1017  double R, pR, bR(0), d;
1018  Bmarker* model = rec->mark;
1019 
1020  View v, vt, bv;
1021  Vector3<double> ori, oritemp, oribest;
1022  Vector3<double> scale, scaletemp, scalebest;
1023 
1024  random_seed();
1025 
1026  if ( verbose & VERB_PROCESS )
1027  cout << "Image\tdx\tdy\tsx\tsy\tvx\tvy\tvz\tva\tR" << endl;
1028  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1029  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
1030  v = View(mg->matrix);
1031  ori = mg->origin;
1032  scale = mg->scale;
1033  v.normalize();
1034  bR = mg_tomo_residuals(mg, model, rec->origin);
1035  n = nacc = ncyc = 0;
1036  R = pR = bR = 1e30;
1037  while ( ncyc < 1000 && bR > 0.001 ) {
1038  oritemp = ori;
1039  scaletemp = scale;
1040  vt = v;
1041  if ( do_view ) {
1042  vt[0] = v[0] + att*(random()*irm*2.0 - 1);
1043  vt[1] = v[1] + att*(random()*irm*2.0 - 1);
1044  vt[2] = v[2] + att*(random()*irm*2.0 - 1);
1045  if ( vt[2] < 0 ) vt[2] = -vt[2]; // Is this good ?????
1046  vt[3] = v.angle() + att*(random()*irm*2.0 - 1);
1047  vt.normalize();
1048  mg->matrix = vt.matrix();
1049  }
1050  if ( do_origin ) {
1051  oritemp = ori + vector3_xy_random_gaussian(0.0, 1.0);
1052  mg->origin = oritemp;
1053  }
1054  if ( do_scale ) {
1055  scaletemp = scale + vector3_xy_random_gaussian(0.0, 0.001);
1056  mg->scale = scaletemp;
1057  }
1058  R = mg_tomo_residuals(mg, model, rec->origin);
1059  if ( bR > R ) {
1060  bR = R;
1061  bv = vt;
1062  oribest = oritemp;
1063  scalebest = scaletemp;
1064  ncyc = 0;
1065  }
1066  accept = 1;
1067  if ( R > pR ) {
1068  d = random()*irm;
1069  if ( exp(beta*(pR-R)) < d ) accept = 0;
1070  }
1071  if ( accept ) {
1072  pR = R;
1073  v = vt;
1074  ori = oritemp;
1075  scale = scaletemp;
1076  nacc++;
1077  } else {
1078  R = pR;
1079  vt = v;
1080  oritemp = ori;
1081  scaletemp = scale;
1082  }
1083  ncyc++;
1084  n++;
1085  }
1086  mg->matrix = bv.matrix();
1087  mg->origin = oribest;
1088  mg->scale = scalebest;
1089  if ( verbose & VERB_PROCESS )
1090  cout << mg->id << tab << bv[0] << tab << bv[1] << tab << bv[2] << tab <<
1091  bv.angle()*180/M_PI << tab << acos(bv[2])*180/M_PI << tab << bR << tab << nacc*100.0/n << endl;
1092  }
1093  }
1094 
1095  return bR;
1096 }
1097 
1114 double mg_marker_shift(Bmicrograph* mg, Bimage* pgold, double hi_res,
1115  double lo_res, double shift_limit, fft_plan planf, fft_plan planb)
1116 {
1117  if ( !mg->mark )
1118  return error_show("No markers found!", __FILE__, __LINE__);
1119 
1120  if ( verbose & VERB_DEBUG )
1121  cout << "DEBUG mg_marker_shift: mg=" << mg->id << endl;
1122 
1123  Bmarker* mark = NULL;
1124 
1125  Bimage* p = read_img(mg->fmg, 1, mg->img_num);
1126  if ( !p )
1127  return error_show("mg_marker_shift", __FILE__, __LINE__);
1128 
1129  p->origin(mg->origin);
1130 
1131  Vector3<double> offset;
1132 
1133  if ( shift_limit < 0 ) shift_limit = 0.2*p->sizeX();
1134 
1135  Bimage* pmark = img_marker_projection(p->size(), offset, mg->mark_radius, pgold, mg->mark);
1136 
1137  double cc(0);
1138  Vector3<double> shift = p->find_shift(pmark, NULL, hi_res, lo_res, shift_limit, 0, 1, planf, planb, cc);
1139 
1140 // write_img("pmark.pif", pmark);
1141  delete pmark;
1142  delete p;
1143 
1144  mg->origin += shift;
1145  for ( mark = mg->mark; mark; mark = mark->next ) if ( mark->sel )
1146  mark->loc += shift;
1147 
1148  if ( verbose & VERB_PROCESS )
1149  cout << "Image " << mg->img_num << " origin:\t" << mg->origin[0] << tab << mg->origin[1] << tab << "CC: " << cc << endl;
1150 
1151  return cc;
1152 }
1153 
1154 double marker_fom(Bimage* p, Vector3<double> loc, double radius)
1155 {
1156  double sf, s;
1157  double f = p->density(0, loc, radius, sf);
1158  double d = p->density(0, loc, 2*radius, s);
1159 
1160 // return fabs(f/d - 1);
1161 // return sf/s - 1;
1162  return f - d;
1163 }
1164 
1166  Bimage* pgold, double thickness, int cc_type, int i, double* z, fft_plan planf, fft_plan planb)
1167 {
1168  Bmarker* mark;
1169 
1170  for ( mark = mg->mark; mark && mark->id != modmark->id; mark = mark->next ) ;
1171 
1172  if ( !mark || !mark->sel ) return 0;
1173 
1174  Vector3<double> mloc = modmark->loc - oriref;
1175 
1176  z[i] = mloc[2];
1177  mark->fom = 0;
1178 
1179  double cc(-1);
1180  double dz(1/sin(fabs(mg->tilt_angle))); // Approximately one pixel in xy
1181  double z_range(thickness/mg->pixel_size[0]); // Thickness in voxels
1182  double z_start = mloc[2] - z_range;
1183  double z_end = mloc[2] + z_range;
1184  Matrix3 mat = mg->matrix;
1185  Vector3<long> gsize(pgold->size());
1186  Vector3<double> gori(pgold->image->origin());
1187  Vector3<double> extori;
1188  Bimage* pmark;
1189  Bimage* pc;
1190 
1191  if ( z_range > 100*dz ) z_range = 100*dz;
1192 
1193  for ( mloc[2] = z_start; mloc[2] <= z_end; mloc[2] += dz ) {
1194  mark->loc = mg_location_from_3D_model(mloc, mat, mg->origin);
1195  if ( mark->loc[0] > gori[0] && mark->loc[0] < p->sizeX() - gori[0] &&
1196  mark->loc[1] > gori[1] && mark->loc[1] < p->sizeY() - gori[1] ) {
1197  extori = mark->loc - gori;
1198  pmark = p->extract(0, extori, gsize);
1199  pmark->origin(pmark->size()/2);
1200  if ( cc_type < 1 ) {
1201  cc = pmark->correlate(pgold, 0, pmark->sizeX()/2.0, NULL, 0);
1202 // cc = pmark->correlate(pgold);
1203  } else {
1204  pc = pmark->cross_correlate(pgold, 4*pmark->sampling(0)[0], 0, planf, planb);
1205  cc = pc->maximum();
1206  delete pc;
1207  }
1208 // write_img("m.map", pmark);
1209  delete pmark;
1210  if ( mark->fom < cc ) {
1211  mark->fom = cc;
1212  z[i] = mloc[2];
1213  }
1214  }
1215  }
1216 
1217 // mark->fom = marker_fom(p, mark->loc, mg->mark_radius)/pgold->image->FOM();
1218  if ( mark->fom < 0.0001 ) mark->fom = 0.0001;
1219  modmark->fom = mark->fom;
1220 
1221  if ( verbose & VERB_FULL )
1222  cout << " " << z[i];
1223 
1224  return cc;
1225 }
1226 
1227 /*
1228 @brief Refines the z-coordinate of a global marker model.
1229 @param *mg micrograph.
1230 @param *model marker model.
1231 @param oriref reference origin.
1232 @param *pgold marker reference image.
1233 @param thickness estimated tomogram thickness (angstrom).
1234 @param cc_type indicates type of correlation: 0=real space, 1=cross correlation
1235 @param iz starting index in the z-matrix.
1236 @param *z z-matrix.
1237 @return int 0.
1238 **/
1240  Bimage* pgold, double thickness, int cc_type, int iz, double* z)
1241 {
1242  if ( fabs(mg->tilt_angle) < 0.001 ) {
1243  cerr << "Warning: micrograph " << mg->id << " tilt angle is too small! (" << mg->tilt_angle << ")" << endl;
1244  return -1;
1245  }
1246 
1247  if ( thickness < 1000 ) thickness = 1000; // Minimum thickness = 1000 angstrom (0.1 um)
1248  if ( thickness > 100000 ) thickness = 100000; // Maximum thickness = 100000 angstrom (10 um)
1249 
1250  if ( !mg->mark )
1251  return error_show("No markers found!", __FILE__, __LINE__);
1252 
1253  Bmarker* modmark = NULL;
1254 
1255  Bimage* p = read_img(mg->fmg, 1, mg->img_num);
1256  if ( !p )
1257  return error_show("mg_marker_z_search", __FILE__, __LINE__);
1258 
1259  long i, cc_size = (long) (4*mg->mark_radius);
1260  if ( cc_size < 30 ) cc_size = 30;
1261  double dz(1/sin(fabs(mg->tilt_angle))); // Approximately one pixel in xy
1262  Vector3<long> gsize;
1263 
1264  int local_gold_particle(0);
1265  if ( pgold ) {
1266  gsize = pgold->size();
1267  } else {
1268  local_gold_particle = 1;
1269  gsize = Vector3<long>(cc_size, cc_size, 1);
1270  pgold = img_gold_particle(gsize, mg->mark_radius);
1271  }
1272 
1273  p->sampling(mg->pixel_size);
1274  pgold->sampling(p->sampling(0));
1275 
1276  long nmark;
1277  for ( nmark=0, modmark = model; modmark; modmark = modmark->next, nmark++ ) ;
1278 
1279  Bmarker** marr = new Bmarker*[nmark];
1280  for ( i=0, modmark = model; i<nmark && modmark; i++, modmark = modmark->next )
1281  marr[i] = modmark;
1282 
1283  fft_plan planf = fft_setup_plan(gsize, FFTW_FORWARD, 1);
1284  fft_plan planb = fft_setup_plan(gsize, FFTW_BACKWARD, 1);
1285 
1286  if ( verbose )
1287  cout << "Image " << mg->img_num << " z search: " << dz << endl;
1288 #ifdef HAVE_GCD
1289  dispatch_apply(nmark, dispatch_get_global_queue(0, 0), ^(size_t i){
1290  mg_marker_z_search_one(mg, p, marr[i], oriref, pgold, thickness, cc_type, i+iz, z, planf, planb);
1291  });
1292 #else
1293 #pragma omp parallel for
1294  for ( i=0; i<nmark; i++ ) {
1295  mg_marker_z_search_one(mg, p, marr[i], oriref, pgold, thickness, cc_type, i+iz, z, planf, planb);
1296  }
1297 #endif
1298 
1299  if ( verbose & VERB_FULL )
1300  cout << endl;
1301 
1304 
1305  delete p;
1306  delete[] marr;
1307 
1308  if ( local_gold_particle ) delete pgold;
1309 
1310  return 0;
1311 }
1312 
1313 /*
1314 @brief Updates the z-coordinate of a global marker model.
1315 @param model reference marker list.
1316 @param nmg number of micrographs.
1317 @param nmark number of markers.
1318 @param *z z-matrix.
1319 @param *w array for micrograph weights.
1320 @param recenter flag to recenter z coordinates.
1321 @return int 0.
1322 
1323  Calculates average and deviation of the z-coordinate of a global
1324  marker model over all of the available z measurements.
1325  The ultimate z-value accepted is the median.
1326 **/
1327 int mg_z_matrix_update(Bmarker* model, int nmg, int nmark, double* z, double* w, int recenter)
1328 {
1329  if ( !model )
1330  return error_show("No markers found!", __FILE__, __LINE__);
1331 
1332  int i, j, k, n, m;
1333  double tw, avg_z, std_z, med_z, avg_all_z(0);
1334  double* za = new double[nmg];
1335  Bmarker* mark;
1336 
1337  for ( i=0; i<nmg; i++ ) za[i] = 0;
1338 
1339  if ( verbose )
1340  cout << "Marker\tZmed\tZavg\tZstd\tFOM\tCount" << endl;
1341  for ( i=m=0, mark = model; mark; i++, mark = mark->next ) if ( mark->sel ) {
1342  for ( avg_z=std_z=tw=0, j=n=0; j<nmg; j++ ) {
1343  k = j*nmark + i;
1344  if ( z[k] > -1e10 ) {
1345  avg_z += w[j]*z[k];
1346  std_z += w[j]*z[k]*z[k];
1347  za[n] = z[k];
1348  tw += w[j];
1349  n++;
1350  }
1351  }
1352  med_z = 0;
1353  if ( n ) {
1354  avg_z /= tw;
1355  std_z = std_z/tw - avg_z*avg_z;
1356  if ( std_z > 0 ) std_z = sqrt(std_z);
1357  else std_z = 0;
1358  qsort((void *) za, n, sizeof(double), QsortLargeToSmallDouble);
1359  med_z = za[n/2];
1360 // mark->loc.z = avg_z;
1361 // mark->loc[2] = med_z;
1362  mark->loc[2] = 0.5*(med_z + mark->loc[2]);
1363 // mark->loc[2] = 0.5*(med_z + avg_z);
1364  mark->res = std_z;
1365 // mark->fom = fom_from_distance(std_z, 10);
1366 // if ( mark->fom < 0.0001 ) mark->fom = 0.0001;
1367 // mark->fom = 1/(1 + std_z + fabs(avg_z - med_z));
1368  }
1369  avg_all_z += mark->loc[2];
1370  m++;
1371  if ( verbose )
1372  cout << setprecision(2) << mark->id << tab << med_z << tab <<
1373  avg_z << tab << std_z << tab << setprecision(5) << mark->fom << tab << n << endl;
1374  }
1375 // if ( verbose )
1376 // cout << endl;
1377 
1378  delete[] za;
1379 
1380  avg_all_z /= m;
1381 
1382  if ( recenter ) {
1383  if ( verbose )
1384  cout << "Recentering z coordinates: " << avg_all_z << endl;
1385 
1386  if ( verbose & VERB_FULL )
1387  cout << "Marker\tz\tFOM" << endl;
1388  for ( mark = model; mark; mark = mark->next ) {
1389  if ( mark->sel ) mark->loc[2] -= avg_all_z;
1390  else mark->loc[2] = mark->fom = 0;
1391  if ( verbose & VERB_FULL )
1392  cout << mark->id << tab << mark->loc[2] << tab << mark->fom << endl;
1393  }
1394  }
1395 
1396  if ( verbose )
1397  cout << endl;
1398 
1399  return 0;
1400 }
1401 
1422  double rot_start, double rot_end, double rot_step,
1423  double hi_res, double lo_res, double shift_limit)
1424 {
1425  Vector3<double> origin, bestshift;
1426  double cc(-1), bestcc(-1), bestangle(0);
1427  Bfield* field1 = project->field;
1428  Bfield* field2 = field1->next;
1429 
1430  Bmicrograph* mg1 = field_find_zero_tilt_mg(field1);
1431  if ( !mg1->mark ) for ( mg1 = field1->mg; mg1 && !mg1->mark; mg1 = mg1->next ) ;
1432 
1433  if ( !mg1 ) {
1434  cerr << "Error: No markers defined for the first tilt series!" << endl;
1435  return -1;
1436  }
1437 
1438  if ( hi_res < 2*mg1->pixel_size[0] ) hi_res = 2*mg1->pixel_size[0];
1439  if ( lo_res < 2*hi_res ) lo_res = 100*hi_res;
1440  if ( shift_limit < 10 ) shift_limit = 10;
1441 
1442  Bmicrograph* mg2 = field_find_zero_tilt_mg(field2);
1443 
1444  if ( !mg2 ) {
1445  cerr << "Error: No micrographs found in the second tilt series!" << endl;
1446  return -1;
1447  }
1448 
1449  if ( mg1->matrix.determinant() < 0.5 )
1451 
1452  mg2->mark_radius = mg1->mark_radius;
1453 
1454  // Generate a composite gold particle image for cross-correlation
1455  int cc_size = (int) (4*mg1->mark_radius);
1456  if ( cc_size < 30 ) cc_size = 30;
1457  Vector3<long> gsize(cc_size, cc_size, 1);
1458  Bimage* pgold = mg_composite_particle(mg1, gsize);
1459 
1460  Transform t;
1461  t.origin = mg1->origin;
1462 
1463  if ( verbose ) {
1464  cout << "Finding the best angle to transform the first seed to the second" << endl;
1465  cout << "Micrograph 1: " << mg1->id << endl;
1466  cout << "Micrograph 2: " << mg2->id << endl;
1467  cout << "Resolution: " << hi_res << " - " << lo_res << endl;
1468  cout << "Shift limit: " << shift_limit << endl;
1469  }
1470 
1471  Vector3<long> size = micrograph_get_size(mg1);
1472 
1473  fft_plan planf = fft_setup_plan(size[0], size[1], 1, FFTW_FORWARD, 1);
1474  fft_plan planb = fft_setup_plan(size[0], size[1], 1, FFTW_BACKWARD, 1);
1475 
1476  if ( verbose )
1477  cout << "Angle\tOriX\tOriY\tCC" << endl;
1478  for ( t.angle = rot_start; t.angle <= rot_end; t.angle += rot_step ) {
1479  kill_list((char *) mg2->mark, sizeof(Bmarker));
1480  mg2->mark = markers_copy(mg1->mark);
1481  marker_transform(mg2->mark, t);
1482  mg2->origin = mg1->origin;
1483  cc = mg_marker_shift(mg2, pgold, hi_res, lo_res, shift_limit, planf, planb);
1484  if ( bestcc < cc ) {
1485  bestcc = cc;
1486  bestangle = t.angle;
1487  bestshift = mg2->origin - mg1->origin;
1488  }
1489  if ( verbose )
1490  cout << t.angle*180.0/M_PI << tab << mg2->origin[0] << tab << mg2->origin[1] << tab << cc << endl;
1491  }
1492 
1495 
1496  t.angle = bestangle;
1497  t.trans = bestshift;
1498  kill_list((char *) mg2->mark, sizeof(Bmarker));
1499  mg2->mark = markers_copy(mg1->mark);
1500  int nmark = marker_transform(mg2->mark, t);
1501 
1502  if ( verbose ) {
1503  cout << "Best angle: " << bestangle*180.0/M_PI << " degrees" << endl;
1504  cout << "Best shift: " << bestshift[0] << " " << bestshift[1] << endl;
1505  cout << "Correlation coefficient: " << bestcc << endl << endl;
1506  }
1507 
1508  field2->origin = t * mg1->origin;
1509 
1510 // t = marker_find_transform(mg2->mark, mg1->mark, mg1->origin);
1511 // cout << "axis=" << t.axis*180/M_PI << tab << "angle=" << t.angle*180/M_PI << endl;
1512 
1513  field2->matrix = Matrix3(t.axis, t.angle);
1514  cout << "Field origin: " << field2->origin << endl;
1515  cout << "Field matrix: " << field2->matrix << endl;
1516  for ( mg2 = field2->mg; mg2; mg2 = mg2->next ) {
1517  mg2->origin = field2->origin;
1518 // mg2->tilt_axis = mg1->tilt_axis;
1519 // mg2->matrix = mg2->matrix * field2->matrix.transpose();
1520  mg2->matrix = field2->matrix.transpose() * mg2->matrix;
1521  }
1522 
1523 // if ( project->rec && project->rec->mark )
1524 // marker_transform(project->rec->mark, t);
1525 
1526  delete pgold;
1527 
1528  return nmark;
1529 }
1530 
1545 {
1546  Breconstruction* rec1 = project->rec;
1547  Breconstruction* rec2 = project->rec->next;
1548  Bmarker* mark1 = rec1->mark;
1549  Bmarker* mark2 = rec2->mark;
1550 
1551  Transform t = marker_find_transform(mark2, mark1, rec2->origin);
1552 
1553  Matrix3 mat = Matrix3(t.axis, t.angle);
1554 
1555  if ( verbose ) {
1556  cout << "Marker set 2 transformation:" << endl;
1557  cout << "Axis: " << t.axis << endl;
1558  cout << "Rotation angle: " << t.angle*180.0/M_PI << " degrees" << endl;
1559  cout << "Scale: " << t.scale << endl;
1560  cout << "Origin: " << t.origin << endl;
1561  cout << "Translation: " << t.trans << endl;
1562  matrix3_show_hp(mat);
1563  }
1564 
1565  mat = t.scale * mat;
1566 
1567  long n;
1568  double d, R(0);
1569  Vector3<double> loc, shift_avg;
1570  Vector3<double> shift = t.origin*t.scale + t.trans;
1571 
1572  // Transform the markers to agree with the eventual reconstruction
1573  for ( mark2 = rec2->mark; mark2; mark2 = mark2->next ) {
1574  loc = mark2->loc - rec2->origin;
1575  loc = mat * loc;
1576  loc += shift;
1577  mark2->loc = loc;
1578  }
1579 
1580  for ( n=0, mark1 = rec1->mark; mark1; mark1 = mark1->next ) if ( mark1->sel ) {
1581  for ( mark2 = rec2->mark; mark2 && mark2->id != mark1->id; mark2 = mark2->next ) ;
1582  if ( mark2 && mark2->sel ) {
1583  loc = mark1->loc - mark2->loc;
1584  shift_avg += loc;
1585  n++;
1586  }
1587  }
1588 
1589  if ( n ) {
1590  shift_avg /= n;
1591  for ( mark2 = rec2->mark; mark2; mark2 = mark2->next ) mark2->loc += shift_avg;
1592  }
1593 
1594  if ( verbose )
1595  cout << "Marker\tResidual" << endl;
1596  for ( n=0, mark1 = rec1->mark; mark1; mark1 = mark1->next ) if ( mark1->sel ) {
1597  for ( mark2 = rec2->mark; mark2 && mark2->id != mark1->id; mark2 = mark2->next ) ;
1598  if ( mark2 && mark2->sel ) {
1599  loc = mark1->loc - mark2->loc;
1600  d = loc.length();
1601  R += d*d;
1602  n++;
1603  if ( verbose )
1604  cout << mark1->id << tab << d << endl;
1605  }
1606  }
1607 
1608  if ( n ) R = sqrt(R/n);
1609 
1610  if ( verbose ) {
1611  cout << "Markers selected: " << n << endl;
1612  cout << "Additional shift: " << shift_avg << endl;
1613  cout << "Residual: " << R << endl << endl;
1614  }
1615 
1616  Bmicrograph* mg;
1617  Matrix3 tmat = mat.transpose();
1618 
1619  t.trans += shift_avg;
1620  cout << "Reconstruction origin = " << rec2->origin << endl;
1621 
1622  // Adjust the micrograph matrices and origins
1623  for ( mg = project->field->next->mg; mg; mg = mg->next ) {
1624  mg->matrix = mat * mg->matrix;
1625  tmat = mg->matrix.transpose();
1626  mg->origin -= tmat * t.trans;
1627 // cout << mg->id << tab << mg->origin << endl;
1628  mg->origin[2] = 0;
1629  }
1630 
1631  return n;
1632 }
1633 
1634 
1645 {
1646  int n(0);
1647  double d, R(0);
1648  Breconstruction* rec1 = project->rec;
1649  Breconstruction* rec2 = project->rec->next;
1650  Bmarker* mark1 = NULL;
1651  Bmarker* mark2 = NULL;
1652 
1653  cout << "Z Coordinates Compare" << endl << endl;
1654  cout << "Marker\tz1\tz2\tz1-z2" << endl;
1655  for ( mark1 = rec1->mark; mark1; mark1 = mark1->next ) if ( mark1->sel ) {
1656  for ( mark2 = rec2->mark; mark2 && mark2->id != mark1->id; mark2 = mark2->next ) ;
1657  if ( mark2 && mark2->sel ) {
1658  d = mark1->loc[2] - mark2->loc[2];
1659  R += d*d;
1660  n++;
1661  cout << mark1->id << tab << mark1->loc[2] << tab << mark2->loc[2] << tab << d << endl;
1662  }
1663  }
1664 
1665  if ( n ) R = sqrt(R/n);
1666 
1667  cout << "Z RMSD: " << R << " (" << n << ")" << endl << endl;
1668 
1669  cout << "Origin1: " << rec1->origin[0] << " " << rec1->origin[1] << endl;
1670  cout << "Origin2: " << rec2->origin[0] << " " << rec2->origin[1] << endl;
1671  cout << "Marker\tx1\ty1\tz1\tMarker\tx2\ty2\tz2" << endl;
1672  for ( mark1 = rec1->mark; mark1; mark1 = mark1->next ) if ( mark1->sel ) {
1673  for ( mark2 = rec2->mark; mark2 && mark2->id != mark1->id; mark2 = mark2->next ) ;
1674  if ( mark2 && mark2->sel ) {
1675  cout << mark1->id << tab << mark1->loc[0] << tab << mark1->loc[1] << tab << mark1->loc[2] << tab <<
1676  mark2->id << tab << mark2->loc[0] << tab << mark2->loc[1] << tab << mark2->loc[2] << endl;
1677  }
1678  }
1679 
1680  cout << endl;
1681 
1682  return R;
1683 }
1684 
qsort_functions.h
Header file for utility functions for use in qsort calls.
field_find_low_tilt_mg_with_markers
Bmicrograph * field_find_low_tilt_mg_with_markers(Bfield *field)
Finds the micrograph closest to a zero degree tilt in a series with markers.
Definition: mg_select.cpp:312
Bmicrograph::scale
Vector3< double > scale
Definition: mg_processing.h:224
length2
double length2(vector< T > v)
Definition: Vector3.h:519
QsortLargeToSmallDouble
int QsortLargeToSmallDouble(const void *x, const void *y)
Utility function for sorting double precision floating point values in qsort.
Definition: qsort_functions.cpp:59
project_mg_tilt_to_matrix
int project_mg_tilt_to_matrix(Bproject *project)
Sets the matrix from the tilt and axis angles for each micrograph.
Definition: mg_tomography.cpp:2177
Bmicrograph::id
Bstring id
Definition: mg_processing.h:203
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:310
Bmarker::fom
float fom
Definition: marker.h:28
marker_transform
int marker_transform(Bmarker *markers, Transform t)
Transforming markers.
Definition: marker.cpp:276
field_count_micrographs
long field_count_micrographs(Bfield *field)
Counts all the micrographs in the field-of-view structure.
Definition: mg_select.cpp:671
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
fom_from_distance
double fom_from_distance(double distance, double sigma)
Definition: mg_tomo_track.cpp:29
project_refine_markers
int project_refine_markers(Bproject *project, double hi_res, double lo_res)
Refines marker positions in a tomographic series.
Definition: mg_tomo_track.cpp:685
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
img_gold_particle
Bimage * img_gold_particle(Vector3< long > size, double radius)
Generates an image representing a gold particle.
Definition: mg_tomography.cpp:188
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
vector3_xy_random_gaussian
Vector3< double > vector3_xy_random_gaussian(double avg, double std)
Generates a random vector within a random gaussian-distributed length in the xy plane.
Definition: random_numbers.cpp:522
Bimage::cross_correlate
Bimage * cross_correlate(Bimage *p, double hires, double lores, fft_plan planf, fft_plan planb)
Calculates a cross-correlation map by Fast Fourier transformation.
Definition: Bimage_correlate.cpp:363
Bstring
Definition: Bstring.h:29
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bproject::field
Bfield * field
Definition: mg_processing.h:346
micrograph_kill
int micrograph_kill(Bmicrograph *mg)
Frees a whole micrograph parameter structure.
Definition: mg_processing.cpp:493
markers_copy
Bmarker * markers_copy(Bmarker *mark)
Copies a set of markers.
Definition: marker.cpp:142
marker_find_transform
Transform marker_find_transform(Bmarker *markers1, Bmarker *markers2, Vector3< double > origin)
Determines the rotation matrix between two sets of markers.
Definition: marker.cpp:396
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:220
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
random_numbers.h
Header file for functions for creating random images.
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
mg_tomo_track.h
Functions to track fiducial markers in a tomographic series of images.
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:212
mg_select.h
Header file for micrograph processing.
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bfield::origin
Vector3< double > origin
Definition: mg_processing.h:263
mg_composite_particle
Bimage * mg_composite_particle(Bmicrograph *mg, Vector3< long > size)
Generates an average particle image from a micrograph.
Definition: mg_tomography.cpp:1454
Bimage::background
double background(long nn)
Definition: Bimage.h:549
FILL_USER
#define FILL_USER
Definition: utilities.h:176
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
View
Definition: View.h:26
project_transfer_seed
int project_transfer_seed(Bproject *project, double rot_start, double rot_end, double rot_step, double hi_res, double lo_res, double shift_limit)
Transfers the seed markers from the first to the second project.
Definition: mg_tomo_track.cpp:1421
Bimage::find_shift
int find_shift(Bimage *pref, Bimage *pmask, double hires, double lores, double radius, double sigma, int refine_flag)
Calculates a cross-correlation map to find the shift for the pair of images.
Definition: Bimage_correlate.cpp:929
Bfield
Definition: mg_processing.h:260
Vector3::scalar
double scalar(const Vector3 &v) const
Definition: Vector3.h:249
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Transform::origin
Vector3< double > origin
Definition: Transform.h:27
mg_find_tilt_axis
double mg_find_tilt_axis(Bmicrograph *mgp, Bmicrograph *mgn, Bmicrograph *mg_ref, double axis_angle, Bimage *pgold, double hi_res, double lo_res, double shift_limit, fft_plan planf, fft_plan planb)
Definition: mg_tomo_track.cpp:90
micrograph_get_size
Vector3< long > micrograph_get_size(Bmicrograph *mg)
Gets the size of a micrograph.
Definition: mg_img_proc.cpp:712
Bmarker::res
float res
Definition: marker.h:27
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
planf
fft_plan planf
Definition: bico.cpp:24
Bmarker
Definition: marker.h:21
verbose
int verbose
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:296
Bimage::density
double density(long nn, Vector3< double > coord, double radius, double &sigma)
Calculates the density in a sphere around a coordinate in an image.
Definition: Bimage.cpp:3012
project_calculate_angles
int project_calculate_angles(Bproject *project)
Calculates tilt and tilt axis angles from matrices in a project parameter structure.
Definition: mg_tomography.cpp:1966
project
Bproject * project
Definition: bshow.cpp:33
Matrix3
Definition: Matrix3.h:18
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
mg_tomo_residuals
double mg_tomo_residuals(Bmicrograph *mg, Bmarker *model, Vector3< double > oriref)
Calculates the residuals for an alignment solution of one micrograph.
Definition: mg_tomography.cpp:1006
TSimple
@ TSimple
Definition: Bimage.h:59
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
mg_location_from_3D_model
Vector3< double > mg_location_from_3D_model(Vector3< double > loc, Matrix3 mat, Vector3< double > ori)
Calculates the 2D location of a marker from a 3D model.
Definition: mg_tomography.cpp:140
Bmarker::next
Bmarker * next
Definition: marker.h:22
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Bimage::correlate
double correlate(Bimage *p)
Calculates correlation coefficient between two images.
Definition: Bimage_correlate.cpp:36
Bmarker::id
int id
Definition: marker.h:23
project_tomo_residuals
double project_tomo_residuals(Bproject *project, int show)
Calculates the residuals for an alignment solution.
Definition: mg_tomography.cpp:868
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:219
project_dual_zcompare
double project_dual_zcompare(Bproject *project)
Lists two sets of Z coordinates for comparison.
Definition: mg_tomo_track.cpp:1644
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
Bmarker::err
Vector3< float > err
Definition: marker.h:26
View::normalize
double normalize()
Definition: View.h:109
distance
double distance(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:538
Bimage::place
int place(long nn, Bimage *p, Vector3< double > loc, double radius=0, double scale=1, double shift=0, int operation=0)
Places a small image into a large image.
Definition: Bimage_combine.cpp:176
mg_img_proc.h
Header file for image processing from micrograph structures.
Transform::scale
Vector3< double > scale
Definition: Transform.h:31
Bimage.h
Header file for image class.
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:238
Bmarker::loc
Vector3< float > loc
Definition: marker.h:25
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
project_track_markers
int project_track_markers(Bproject *project, double hi_res, double lo_res, double shift_limit, double thickness, int max_cycle, double target, int cc_type, int recenter, Bstring paramfile)
Aligns markers in a tomographic series.
Definition: mg_tomo_track.cpp:314
Bfield::matrix
Matrix3 matrix
Definition: mg_processing.h:264
Breconstruction
Definition: mg_processing.h:283
fft_setup_plan
fft_plan fft_setup_plan(long x, long y, long z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
Definition: fft.cpp:31
tomo_z_residual
double tomo_z_residual(Bproject *project, Bmarker *mark)
Definition: mg_tomo_track.cpp:910
marker_fom
double marker_fom(Bimage *p, Vector3< double > loc, double radius)
Definition: mg_tomo_track.cpp:1154
Bstring::length
long length() const
Definition: Bstring.cpp:263
img_marker_projection
Bimage * img_marker_projection(Vector3< long > size, Vector3< double > offset, double radius, Bimage *pmark, Bmarker *mark)
Generates a projection image of the global marker model.
Definition: mg_tomo_track.cpp:59
Bmicrograph::matrix
Matrix3 matrix
Definition: mg_processing.h:225
mg_marker_z_search
int mg_marker_z_search(Bmicrograph *mg, Bmarker *model, Vector3< double > oriref, Bimage *pgold, double thickness, int cc_type, int iz, double *z)
Definition: mg_tomo_track.cpp:1239
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:41
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
field_find_zero_tilt_mg
Bmicrograph * field_find_zero_tilt_mg(Bfield *field)
Finds the micrograph closest to a zero degree tilt in a series.
Definition: mg_select.cpp:281
Bfield::select
long select
Definition: mg_processing.h:265
project_refine_z
double project_refine_z(Bproject *project)
Refines the z coordinates of the marker model.
Definition: mg_tomo_track.cpp:950
model
Bmodel * model
Definition: bshow.cpp:34
Transform::trans
Vector3< double > trans
Definition: Transform.h:30
Bfield::next
Bfield * next
Definition: mg_processing.h:261
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
Bmicrograph::mark
Bmarker * mark
Definition: mg_processing.h:245
project_find_tilt_axis
double project_find_tilt_axis(Bproject *project, double tilt, double axis_start, double axis_end, double axis_step, double hi_res, double lo_res, double shift_limit)
Finds the tilt axis for a tomographic tilt series.
Definition: mg_tomo_track.cpp:154
mg_marker_shift
double mg_marker_shift(Bmicrograph *mg, Bimage *pgold, double hi_res, double lo_res, double shift_limit, fft_plan planf, fft_plan planb)
Finds the shift of a micrograph wrt the reference using markers.
Definition: mg_tomo_track.cpp:1114
Matrix3::determinant
double determinant()
Definition: Matrix3.h:260
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
beta
double beta(double volt)
Calculates the relative electron velocity.
Definition: ctf.cpp:500
Bmarker::sel
int sel
Definition: marker.h:29
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
linked_list.h
Header file for generalized linked list functions.
Bmicrograph::select
long select
Definition: mg_processing.h:204
mg_z_matrix_update
int mg_z_matrix_update(Bmarker *model, int nmg, int nmark, double *z, double *w, int recenter)
Definition: mg_tomo_track.cpp:1327
project_refine
double project_refine(Bproject *project, int iter, double tol, Bstring refop)
Refines all the alignment parameters.
Definition: mg_tomo_track.cpp:870
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
project_track_markers_dual
int project_track_markers_dual(Bproject *project, double hi_res, double lo_res, double shift_limit, double thickness, int max_cycle, double target, int cc_type, int recenter, Bstring paramfile)
Aligns markers in a tomographic series.
Definition: mg_tomo_track.cpp:490
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
irm
double irm(1.0L/get_rand_max())
marker_show
int marker_show(Bmarker *m1, Bmarker *m2)
Definition: mg_tomo_track.cpp:38
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
planb
fft_plan planb
Definition: bico.cpp:24
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Transform::angle
double angle
Definition: Transform.h:29
mg_marker_z_search_one
double mg_marker_z_search_one(Bmicrograph *mg, Bimage *p, Bmarker *modmark, Vector3< double > oriref, Bimage *pgold, double thickness, int cc_type, int i, double *z, fft_plan planf, fft_plan planb)
Definition: mg_tomo_track.cpp:1165
View::matrix
Matrix3 matrix()
Definition: View.h:153
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
mg_refine_markers
int mg_refine_markers(Bmicrograph *mg, Bimage *pgold, double hi_res, double lo_res)
Refines marker positions in a tomographic micrograph.
Definition: mg_tomo_track.cpp:833
Transform
Definition: Transform.h:24
Vector3::length
double length()
Definition: Vector3.h:222
Bimage::within_boundaries
bool within_boundaries(Vector3< double > loc)
Definition: Bimage.h:461
Float
@ Float
Definition: utilities.h:161
marker_refine
double marker_refine(Bmarker *mark, Bmarker *marklist, Bimage *p, Bimage *pgold, double radius, double hi_res, double lo_res)
Definition: mg_tomo_track.cpp:645
View::angle
double angle()
Definition: View.h:98
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
mg_marker_update
int mg_marker_update(Bmicrograph *mg, Bmarker *model, Vector3< double > oriref, int update_location)
Calculates the predicted positions of markers from the reference.
Definition: mg_tomography.cpp:60
project_refine_one_marker
int project_refine_one_marker(Bproject *project, int id, double hi_res, double lo_res)
Refines marker positions in a tomographic series.
Definition: mg_tomo_track.cpp:770
project_count_micrographs
long project_count_micrographs(Bproject *project)
Counts all the micrographs in the project structure.
Definition: mg_select.cpp:350
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
project_transform_dual
int project_transform_dual(Bproject *project)
Calculates the transformation of the second tilt series to fit the first.
Definition: mg_tomo_track.cpp:1544
micrograph_copy
Bmicrograph * micrograph_copy(Bmicrograph *mg)
Copies a micrograph.
Definition: mg_processing.cpp:735
Bimage::sphere
int sphere(Vector3< double > center, double radius, double width=0, int fill_type=0, double fill=0)
Fills a sphere within an image with a uniform value.
Definition: Bimage_edit.cpp:440
mg_tomography.h
Header file for single particle image processing in Bsoft.
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284
matrix3_show_hp
int matrix3_show_hp(Matrix3 &mat)
Print out a matrix.
Definition: Matrix3.cpp:31