Bsoft  2.1.0
Bernard's software package
tcltk_bmarker.cpp
Go to the documentation of this file.
1 
9 // Tk must be included before anything else to remedy symbol conflicts
10 #include <tk.h>
11 
12 #include "tcltk_bmarker.h"
13 #include "mg_img_proc.h"
14 #include "mg_extract.h"
15 #include "mg_tomography.h"
16 #include "mg_tomo_track.h"
17 #include "rwmg.h"
18 #include "mg_tomography.h"
19 #include "linked_list.h"
20 #include "timer.h"
21 #include "utilities.h"
22 
23 #include <sys/stat.h>
24 
25 // Declaration of global variables
26 extern int verbose; // Level of output to the screen
27 extern Bimage* imglist;
28 
29 // Internal function prototypes
30 Tcl_Obj* marker_count(Bproject* project);
31 Tcl_Obj* marker_count(Bfield* field);
32 Tcl_Obj* marker_count(Bmicrograph* mg);
33 Tcl_Obj* marker_count(Breconstruction* rec);
34 Tcl_Obj* marker_ids(Bproject* project, double fom_cut);
35 Tcl_Obj* marker_ids(Bfield* field, double fom_cut);
36 Tcl_Obj* marker_ids(Bmicrograph* mg, double fom_cut);
37 Tcl_Obj* marker_ids(Breconstruction* rec, double fom_cut);
38 Tcl_Obj* marker_list(Bproject* project);
39 Tcl_Obj* marker_list(Bfield* field);
40 Tcl_Obj* marker_list(Bmicrograph* mg);
41 Tcl_Obj* marker_list(Breconstruction* rec);
42 Tcl_Obj* marker_location(Bmarker* mark);
44 Tcl_Obj* marker_residual(Bmarker* mark);
45 Tcl_Obj* marker_fom(Bmarker* mark);
46 Tcl_Obj* marker_fom_maximum(Bmarker* mark);
47 Tcl_Obj* marker_select(Bmarker* marklist, double rad, int objc, Tcl_Obj *CONST objv[]);
48 Tcl_Obj* marker_select_rectangle(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[]);
49 Tcl_Obj* marker_get_selection_flag(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[]);
50 Tcl_Obj* marker_set_selection_flag(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[]);
51 Tcl_Obj* marker_clear_selection_flag(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[]);
52 Tcl_Obj* marker_move(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[]);
53 Tcl_Obj* marker_create(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[]);
54 Tcl_Obj* marker_create(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[]);
55 Tcl_Obj* marker_center(Bmarker* mark, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
57 Tcl_Obj* marker_find(Bmicrograph* mg, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
58 Tcl_Obj* marker_snap(Bmicrograph* mg, Bimage* p, long mid, int objc, Tcl_Obj *CONST objv[]);
59 Tcl_Obj* marker_extract(Bmicrograph* mg, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
60 Tcl_Obj* marker_extract(Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
61 Tcl_Obj* marker_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[]);
62 Tcl_Obj* marker_delete(Bfield* field, int objc, Tcl_Obj *CONST objv[]);
63 Tcl_Obj* marker_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[]);
64 Tcl_Obj* marker_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[]);
65 Tcl_Obj* marker_edge(Bmarker* mark, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
66 Tcl_Obj* marker_accept(Bmarker* mark, int objc, Tcl_Obj *CONST objv[]);
67 
68 Tcl_Obj* do_tomo_thickness(Bproject* project, int objc, Tcl_Obj *CONST objv[])
69 {
70  Tcl_Obj* returnObj = Tcl_NewObj();
71 
72  int adjust_tilt(0);
73  double lambda(2260);
74  Bstring psfile;
75 
76  if ( objc > 2 ) Tcl_GetDoubleFromObj(NULL, objv[2], &lambda); // Proportionality parameter
77  if ( objc > 3 ) Tcl_GetIntFromObj(NULL, objv[3], &adjust_tilt); // Tilt ddjustment flag
78  if ( objc > 4 ) psfile = Tcl_GetStringFromObj(objv[4], NULL); // Postscript file name
79 
80 // cout << "Lambda = " << lambda << tab << adjust_tilt << endl;
81 
83  if ( !plot ) return returnObj;
84 
85  double tlr = project_fit_intensities(project, plot, adjust_tilt);
86  double thickness = lambda*tlr;
87 
88 // cout << "tlr = " << tlr << endl;
89 
90  Bstring txt;
91  txt = Bstring(thickness, "Thickness: %lg");
92  plot->page(0).add_text(txt);
93 
94  if ( psfile.length() )
95  ps_plot(psfile, plot);
96 
97  Tcl_SetDoubleObj(returnObj, thickness);
98 
99  return returnObj;
100 }
101 
102 int do_tomo_transfer_seed(Bproject* project, int objc, Tcl_Obj *CONST objv[])
103 {
104  double rot_start(-180), rot_end(180); // Rotation search limits
105  double rot_step(0); // Rotation search step size for seed transfer
106  double hi_res(0), lo_res(1000); // Default resolution range
107  double shift_limit(-1); // Micrograph shift search limit
108  int refine(0);
109 
110  if ( objc > 2 ) Tcl_GetDoubleFromObj(NULL, objv[2], &rot_start);
111  if ( objc > 3 ) Tcl_GetDoubleFromObj(NULL, objv[3], &rot_end);
112  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &rot_step);
113  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &hi_res);
114  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &lo_res);
115  if ( objc > 7 ) Tcl_GetDoubleFromObj(NULL, objv[7], &shift_limit);
116  if ( objc > 8 ) Tcl_GetIntFromObj(NULL, objv[8], &refine);
117 
118  rot_step *= M_PI/180.0;
119  rot_start *= M_PI/180.0;
120  rot_end *= M_PI/180.0;
121 
122  if ( rot_step > 0 ) {
123  project_transfer_seed(project, rot_start, rot_end, rot_step, hi_res, lo_res, shift_limit);
124  if ( refine )
125  project_refine_markers(project, hi_res, lo_res);
126  }
127 
128  return 0;
129 }
130 
131 int do_tomo_findaxis(Bproject* project, int objc, Tcl_Obj *CONST objv[])
132 {
133  double axis(0), axis_start(-180), axis_end(180), axis_step(1);
134  double hi_res(0), lo_res(1000), shift_limit(-1);
135 
136  if ( objc > 2 ) Tcl_GetDoubleFromObj(NULL, objv[2], &axis); // Target tilt axis
137  if ( objc > 3 ) Tcl_GetDoubleFromObj(NULL, objv[3], &axis_start); // Starting angle to test
138  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &axis_end); // Ending angle to test
139  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &axis_step); // Angular step size
140  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &hi_res); // High resolution limit
141  if ( objc > 7 ) Tcl_GetDoubleFromObj(NULL, objv[7], &lo_res); // Low resolution limit
142  if ( objc > 8 ) Tcl_GetDoubleFromObj(NULL, objv[8], &shift_limit); // Maximum shift limit
143 
144  axis *= M_PI/180.0;
145  axis_start *= M_PI/180.0;
146  axis_end *= M_PI/180.0;
147  axis_step *= M_PI/180.0;
148 
149  project_find_tilt_axis(project, axis, axis_start, axis_end, axis_step, hi_res, lo_res, shift_limit);
150 
151  return 0;
152 }
153 
154 int do_tomo_track(Bproject* project, int objc, Tcl_Obj *CONST objv[])
155 {
156  int iter(5), thickness(500), cc_type(0), recenter(0);
157  double hi_res(0), lo_res(500), shift_limit(-1);
158  Bstring paramfile("track.star");
159 
160  if ( objc > 2 ) Tcl_GetIntFromObj(NULL, objv[2], &iter); // Maximum number of iterations
161  if ( objc > 3 ) Tcl_GetDoubleFromObj(NULL, objv[3], &hi_res); // High resolution limit
162  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &lo_res); // Low resolution limit
163  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &shift_limit); // Maximum shift limit
164  if ( objc > 6 ) Tcl_GetIntFromObj(NULL, objv[6], &thickness); // Maximum thickness
165  if ( objc > 7 ) Tcl_GetIntFromObj(NULL, objv[7], &cc_type); // Flag to cross correlate
166  if ( objc > 8 ) Tcl_GetIntFromObj(NULL, objv[8], &recenter); // Flag to recenter z coordinates
167 
168  ofstream out("track.log");
169  streambuf *coutbuf = cout.rdbuf(); //save old buf
170  cout.rdbuf(out.rdbuf()); //redirect std::cout to out!
171 
172  project_track_markers(project, hi_res, lo_res, shift_limit, thickness, cc_type, iter, 1, recenter, paramfile);
173 
174  cout.rdbuf(coutbuf); //reset to standard output again
175 
176  return 0;
177 }
178 
179 int do_tomo_refine(Bproject* project, int objc, Tcl_Obj *CONST objv[])
180 {
181 // int ref_view(0), ref_ori(0), ref_scale(0);
182  int ref_iter(1);
183  double hi_res(20), lo_res(1000), ref_tol(1e-4);
184  Bstring refop;
185 
189 
190  /* Type of operations */
191  if ( objc > 2 ) refop = Tcl_GetStringFromObj(objv[2], NULL);
192 
193  if ( refop[0] == 'm' ) {
194  if ( objc > 3 ) Tcl_GetDoubleFromObj(NULL, objv[3], &hi_res); // High resolution limit
195  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &lo_res); // Low resolution limit
196  project_refine_markers(project, hi_res, lo_res);
197 // } else if ( refop[0] == 'z' ) {
198 // project_refine_z(project);
199  } else {
200 // if ( objc > 3 ) Tcl_GetIntFromObj(NULL, objv[3], &ref_view); /* Do views */
201 // if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &ref_ori); /* Do origins */
202 // if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &ref_scale); /* Do scales */
203 // project_refine(project, ref_view, ref_ori, ref_scale);
204  if ( objc > 3 ) Tcl_GetIntFromObj(NULL, objv[3], &ref_iter); // Iterations
205  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &ref_tol); // Stopping condition
206  project_refine(project, ref_iter, ref_tol, refop);
207  }
208 
212 
213  return 0;
214 }
215 
216 int do_tomo_refine_one(Bproject* project, int objc, Tcl_Obj *CONST objv[])
217 {
218  int id(0);
219  double hi_res(20), lo_res(1000);
220 
221  /* Type of operations */
222  if ( objc > 2 ) Tcl_GetIntFromObj(NULL, objv[2], &id);
223  if ( id < 1 ) return 0;
224 
225  if ( objc > 3 ) Tcl_GetDoubleFromObj(NULL, objv[3], &hi_res); /* High resolution limit */
226  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &lo_res); /* Low resolution limit */
227  project_refine_one_marker(project, id, hi_res, lo_res);
228 
231 
232  return 0;
233 }
234 
235 
236 
237 /*
238 @brief Functions for tomographic marker manipulation.
239  Marker interface syntax:
240  Bmg marker [item] [action] [params]
241  Actions:
242  count counts the number of markers
243  ids [f] returns the identifiers of markers selected above the given FOM cutoff
244  list returns a list of markers
245  location [id] returns the location and error vector of a marker
246  z_range returns the maximum difference in z coordinates
247  residual [id] returns marker residual or residual of all markers
248  fom [id] returns the FOM of a marker
249  select [x] [y] [z] returns the identifier at the given location
250  selectrect [x1] [y1] [x2] [y2] returns a list of identifiers within the rectangle
251  getflag [id] sets the selection flag of a marker
252  setflag [id] [s] sets the selection flag of a marker
253  clearflag [id] clears the selection flag of a marker
254  move [id] [dx] [dy] [dz] updates a marker location by the given vector
255  create [x] [y] [z] creates a new marker at the given location
256  renumber renumbers markers
257  center centers markers
258  generate generates missing markers from an existing seed
259  find [r] finds markers
260  snap [id] snaps a marker to a low density center
261  extract [fn] extracts markers
262  delete [all|fom|id] [f] deletes markers
263  edge [e] sets FOM based on edge parameter
264  accept [id] accept model location for marker
265  Parameters:
266  id identifier
267  x x location
268  y y location
269  z z location
270  f FOM cutoff
271  s selection flag
272  dx change in x
273  dy change in y
274  dz change in z
275  r marker radius
276  fn new file name
277  e edge for marker selection
278 */
279 Tcl_Obj* do_marker(Bproject* project, int objc, Tcl_Obj *CONST objv[])
280 {
281  Tcl_Obj* returnObj = NULL;
282 
283  Bstring item = Tcl_GetStringFromObj(objv[2], NULL);
284  if ( verbose & VERB_DEBUG )
285  cout << "DEBUG do_marker: Item: " << item << " (" << item.length() << ")" << endl;
286 
287  Bstring id = item.post(':');
288  id = id.remove(' ');
289  item = item.pre(':');
290 
291  Bfield* field = NULL;
292  Bmicrograph* mg = NULL;
293  Breconstruction* rec = NULL;
294  Bmarker* mark = NULL;
295  Bimage* p = NULL;
296  Bstring filename, filename2, base, delete_what;
297  int mid(-1);
298  double radius(0), fom_cut(0);
299 
300  if ( item.contains("Field") ) {
301  for ( field = project->field; field && field->id != id; field = field->next ) ;
302  if ( field )
303  filename = field->mg->fmg;
304  else
305  cerr << "Error in do_marker: Field " << id << " not found!" << endl;
306  } else if ( item.contains("Micrograph") ) {
307  for ( field = project->field; field; field = field->next ) {
308  for ( mg = field->mg; mg && mg->id != id; mg = mg->next ) ;
309  if ( mg ) break;
310  }
311  if ( mg ) {
312  if ( mg->fmg.length() )
313  filename = mg->fmg;
314  else if ( mg->fframe.length() )
315  filename = mg->fframe;
316  mark = mg->mark;
317  radius = mg->mark_radius;
318  } else
319  cerr << "Error in do_marker: Micrograph " << id << " not found!" << endl;
320  } else if ( item.contains("Reconstruction") ) {
321  for ( rec = project->rec; rec && rec->id != id; rec = rec->next ) ;
322  if ( rec ) {
323  filename = rec->frec;
324  mark = rec->mark;
325  radius = rec->mark_radius;
326  } else
327  cerr << "Error in do_marker: Reconstruction " << id << " not found!" << endl;
328  }
329 
330  if ( filename.length() ) {
331  base = filename.base();
332  for ( p = imglist; p; p = p->next ) {
333  filename2 = p->file_name();
334  if ( filename2.base() == base ) break;
335  }
336  }
337 
338  Bstring action = Tcl_GetStringFromObj(objv[3], NULL);
339  if ( verbose & VERB_DEBUG )
340  cout << "DEBUG do_marker: Action: " << action << " (" << action.length() << ")" << endl;
341 
342  if ( action == "count" ) {
343  if ( mg ) returnObj = marker_count(mg);
344  else if ( field ) returnObj = marker_count(field);
345  else if ( rec ) returnObj = marker_count(rec);
346  else returnObj = marker_count(project);
347  } else if ( action == "ids" ) {
348  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &fom_cut);
349  if ( mg ) returnObj = marker_ids(mg, fom_cut);
350  else if ( field ) returnObj = marker_ids(field, fom_cut);
351  else if ( rec ) returnObj = marker_ids(rec, fom_cut);
352  else returnObj = marker_ids(project, fom_cut);
353  } else if ( action == "list" ) {
354  if ( mg ) returnObj = marker_list(mg);
355  else if ( field ) returnObj = marker_list(field);
356  else if ( rec ) returnObj = marker_list(rec);
357  else returnObj = marker_list(project);
358  } else if ( action == "location" ) {
359  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &mid);
360  for ( ; mark && mark->id != mid; mark = mark->next ) ;
361  if ( mark ) returnObj = marker_location(mark);
362  } else if ( action == "z_range" ) {
363  rec = project->rec;
364  if ( rec && rec->mark ) {
365  Vector3<double> mr = marker_range(rec->mark);
366  mr *= rec->voxel_size;
367  returnObj = Tcl_NewDoubleObj(mr[2]);
368  }
369  } else if ( action == "residual" ) {
370  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &mid);
371  if ( mid < 0 ) {
372  returnObj = marker_residual(project);
373  } else {
374  for ( ; mark && mark->id != mid; mark = mark->next ) ;
375  if ( mark ) returnObj = marker_residual(mark);
376  }
377  } else if ( action == "fom" ) {
378  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &mid);
379  for ( ; mark && mark->id != mid; mark = mark->next ) ;
380  returnObj = marker_fom(mark);
381  } else if ( action == "fom_maximum" ) {
382  returnObj = marker_fom_maximum(mark);
383  } else if ( action == "select" ) {
384  if ( mark ) returnObj = marker_select(mark, radius, objc, objv);
385  } else if ( action == "selectrect" ) {
386  if ( mark ) returnObj = marker_select_rectangle(mark, objc, objv);
387  } else if ( action == "getflag" ) {
388  if ( mark ) returnObj = marker_get_selection_flag(mark, objc, objv);
389  } else if ( action == "setflag" ) {
390  if ( mark ) returnObj = marker_set_selection_flag(mark, objc, objv);
391  } else if ( action == "clearflag" ) {
392  if ( mark ) returnObj = marker_clear_selection_flag(mark, objc, objv);
393  } else if ( action == "move" ) {
394  if ( mark ) returnObj = marker_move(mark, objc, objv);
395  } else if ( action == "create" ) {
396  if ( mg ) returnObj = marker_create(mg, objc, objv);
397  else if ( rec ) returnObj = marker_create(rec, objc, objv);
398  } else if ( action == "renumber" ) {
399  if ( mark ) markers_renumber(mark);
401  } else if ( action == "center" ) {
402  if ( mark ) returnObj = marker_center(mark, p, objc, objv);
403  } else if ( action == "generate" ) {
404  returnObj = marker_generate_from_seed(project);
405  } else if ( action == "find" ) {
406  if ( mg ) returnObj = marker_find(mg, p, objc, objv);
407  } else if ( action == "snap" ) {
408  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &mid);
409  if ( mg ) returnObj = marker_snap(mg, p, mid, objc, objv);
410  } else if ( action == "extract" ) {
411  if ( mg ) returnObj = marker_extract(mg, p, objc, objv);
412  else if ( rec ) returnObj = marker_extract(rec, p, objc, objv);
413  } else if ( action == "delete" ) {
414  if ( mg ) returnObj = marker_delete(mg, objc, objv);
415  else if ( field ) returnObj = marker_delete(field, objc, objv);
416  else if ( rec ) returnObj = marker_delete(rec, objc, objv);
417  else returnObj = marker_delete(project, objc, objv);
418  } else if ( action == "edge" ) {
419  if ( mark ) returnObj = marker_edge(mark, p, objc, objv);
420  } else if ( action == "accept" ) {
421  if ( mark ) returnObj = marker_accept(mark, objc, objv);
422  } else {
423  cerr << "Error: Action " << action << " not recognized!" << endl;
424  }
425 
426  if ( !returnObj ) {
427  returnObj = Tcl_NewObj();
428  Tcl_SetIntObj(returnObj, 0);
429  }
430 
431  return returnObj;
432 }
433 
435 {
436  Tcl_Obj* returnObj = Tcl_NewObj();
437 
438  int nmark(0);
439  Bfield* field;
440  Bmicrograph* mg;
441  Breconstruction* rec;
442  Bmarker* mark;
443 
444  if ( project->select < 1 ) {
445  for ( field = project->field; field; field = field->next )
446  for ( mg = field->mg; mg; mg = mg->next )
447  for ( mark = mg->mark; mark; mark = mark->next ) nmark++;
448  } else {
449  for ( rec = project->rec; rec; rec = rec->next )
450  for ( mark = rec->mark; mark; mark = mark->next ) nmark++;
451  }
452 
453  Tcl_SetIntObj(returnObj, nmark);
454 
455  return returnObj;
456 }
457 
458 Tcl_Obj* marker_count(Bfield* field)
459 {
460  Tcl_Obj* returnObj = Tcl_NewObj();
461 
462  int nmark(0);
463  Bmicrograph* mg;
464  Bmarker* mark;
465 
466  for ( mg = field->mg; mg; mg = mg->next )
467  for ( mark = mg->mark; mark; mark = mark->next ) nmark++;
468 
469  Tcl_SetIntObj(returnObj, nmark);
470 
471  return returnObj;
472 }
473 
475 {
476  Tcl_Obj* returnObj = Tcl_NewObj();
477 
478  int nmark(0);
479  Bmarker* mark;
480 
481  for ( mark = mg->mark; mark; mark = mark->next ) nmark++;
482 
483  Tcl_SetIntObj(returnObj, nmark);
484 
485  return returnObj;
486 }
487 
489 {
490  Tcl_Obj* returnObj = Tcl_NewObj();
491 
492  int nmark(0);
493  Bmarker* mark;
494 
495  for ( mark = rec->mark; mark; mark = mark->next ) nmark++;
496 
497  Tcl_SetIntObj(returnObj, nmark);
498 
499  return returnObj;
500 }
501 
502 Tcl_Obj* marker_ids(Bproject* project, double fom_cut)
503 {
504  Tcl_Obj* returnObj = Tcl_NewObj();
505 
506  char string[MAXLINELEN] = "";
507  Bfield* field = project->field;
508  Bmicrograph* mg = NULL;
509  Breconstruction* rec = project->rec;
510  Bmarker* mark = NULL;
511 
512  if ( project->select < 1 ) {
513  if ( field ) for ( mg = field->mg; mg && !mg->mark; mg = mg->next ) ;
514  if ( mg ) {
515  for ( mark = mg->mark; mark; mark = mark->next ) if ( mark->fom >= fom_cut ) {
516  sprintf(string, " %d", mark->id);
517  Tcl_AppendToObj(returnObj, string, strlen(string));
518  }
519  }
520  } else {
521  if ( rec ) {
522  for ( mark = rec->mark; mark; mark = mark->next ) if ( mark->fom >= fom_cut ) {
523  sprintf(string, " %d", mark->id);
524  Tcl_AppendToObj(returnObj, string, strlen(string));
525  }
526  }
527  }
528 
529  return returnObj;
530 }
531 
532 Tcl_Obj* marker_ids(Bfield* field, double fom_cut)
533 {
534  Tcl_Obj* returnObj = Tcl_NewObj();
535 
536  char string[MAXLINELEN] = "";
537  Bmicrograph* mg = NULL;
538  Bmarker* mark = NULL;
539 
540  for ( mg = field->mg; mg && !mg->mark; mg = mg->next ) ;
541  if ( mg ) {
542  for ( mark = mg->mark; mark; mark = mark->next ) if ( mark->fom >= fom_cut ) {
543  sprintf(string, " %d", mark->id);
544  Tcl_AppendToObj(returnObj, string, strlen(string));
545  }
546  }
547 
548  return returnObj;
549 }
550 
551 Tcl_Obj* marker_ids(Bmicrograph* mg, double fom_cut)
552 {
553  Tcl_Obj* returnObj = Tcl_NewObj();
554 
555  char string[MAXLINELEN] = "";
556  Bmarker* mark = NULL;
557 
558  for ( mark = mg->mark; mark; mark = mark->next ) if ( mark->fom >= fom_cut ) {
559  sprintf(string, " %d", mark->id);
560  Tcl_AppendToObj(returnObj, string, strlen(string));
561  }
562 
563  return returnObj;
564 }
565 
566 Tcl_Obj* marker_ids(Breconstruction* rec, double fom_cut)
567 {
568  Tcl_Obj* returnObj = Tcl_NewObj();
569 
570  char string[MAXLINELEN] = "";
571  Bmarker* mark = NULL;
572 
573  for ( mark = rec->mark; mark; mark = mark->next ) if ( mark->fom >= fom_cut ) {
574  sprintf(string, " %d", mark->id);
575  Tcl_AppendToObj(returnObj, string, strlen(string));
576  }
577 
578  return returnObj;
579 }
580 
582 {
583  Tcl_Obj* returnObj = Tcl_NewObj();
584 
585  char string[MAXLINELEN] = "";
586  Bfield* field;
587  Bmicrograph* mg = NULL;
588  Breconstruction* rec = project->rec;
589  Bmarker* mark = NULL;
590 
591  if ( project->select < 1 ) {
592  for ( field = project->field; field; field = field->next )
593  for ( mg = field->mg; mg; mg = mg->next )
594  for ( mark = mg->mark; mark; mark = mark->next ) {
595  sprintf(string, " %d %d %20.16f %20.16f %d", mg->img_num, mark->id, mark->res, mark->fom, mark->sel);
596  Tcl_AppendToObj(returnObj, string, strlen(string));
597  }
598  } else {
599  if ( rec ) {
600  for ( mark = rec->mark; mark; mark = mark->next ) {
601  sprintf(string, " 0 %d %20.16f %20.16f %d", mark->id, mark->res, mark->fom, mark->sel);
602  Tcl_AppendToObj(returnObj, string, strlen(string));
603  }
604  }
605  }
606 
607  return returnObj;
608 }
609 
610 Tcl_Obj* marker_list(Bfield* field)
611 {
612  Tcl_Obj* returnObj = Tcl_NewObj();
613 
614  char string[MAXLINELEN] = "";
615  Bmicrograph* mg = NULL;
616  Bmarker* mark = NULL;
617 
618  for ( mg = field->mg; mg; mg = mg->next )
619  for ( mark = mg->mark; mark; mark = mark->next ) {
620  sprintf(string, " %d %d %20.16f %20.16f %d", mg->img_num, mark->id, mark->res, mark->fom, mark->sel);
621  Tcl_AppendToObj(returnObj, string, strlen(string));
622  }
623 
624  return returnObj;
625 }
626 
628 {
629  Tcl_Obj* returnObj = Tcl_NewObj();
630 
631  char string[MAXLINELEN] = "";
632  Bmarker* mark = NULL;
633 
634  for ( mark = mg->mark; mark; mark = mark->next ) {
635  sprintf(string, " %d %d %20.16f %20.16f %d", mg->img_num, mark->id, mark->res, mark->fom, mark->sel);
636  Tcl_AppendToObj(returnObj, string, strlen(string));
637  }
638 
639  return returnObj;
640 }
641 
643 {
644  Tcl_Obj* returnObj = Tcl_NewObj();
645 
646  char string[MAXLINELEN] = "";
647  Bmarker* mark = NULL;
648 
649  for ( mark = rec->mark; mark; mark = mark->next ) {
650  sprintf(string, " 0 %d %20.16f %20.16f %d", mark->id, mark->res, mark->fom, mark->sel);
651  Tcl_AppendToObj(returnObj, string, strlen(string));
652  }
653 
654  return returnObj;
655 }
656 
657 Tcl_Obj* marker_location(Bmarker* mark)
658 {
659  Tcl_Obj* returnObj = Tcl_NewObj();
660 
661  char string[MAXLINELEN] = "";
662 
663  if ( mark ) {
664  sprintf(string, "%f %f %f %f %f %f",
665  mark->loc[0], mark->loc[1], mark->loc[2],
666  mark->err[0], mark->err[1], mark->err[2]);
667  Tcl_SetStringObj(returnObj, string, strlen(string));
668  }
669 
670  return returnObj;
671 }
672 
674 {
675  Tcl_Obj* returnObj = Tcl_NewObj();
676 
677  double res = project_tomo_residuals(project, 0);
678 
679  Tcl_SetDoubleObj(returnObj, res);
680 
681  return returnObj;
682 }
683 
684 Tcl_Obj* marker_residual(Bmarker* mark)
685 {
686  Tcl_Obj* returnObj = Tcl_NewObj();
687 
688  double res(0);
689 
690  if ( mark ) res = mark->res;
691 
692  Tcl_SetDoubleObj(returnObj, res);
693 
694  return returnObj;
695 }
696 
697 Tcl_Obj* marker_fom(Bmarker* mark)
698 {
699  Tcl_Obj* returnObj = Tcl_NewObj();
700 
701  double fom(0);
702 
703  if ( mark ) fom = mark->fom;
704 
705  Tcl_SetDoubleObj(returnObj, fom);
706 
707  return returnObj;
708 }
709 
711 {
712  Tcl_Obj* returnObj = Tcl_NewObj();
713 
714  double fom_max(0);
715 
716  for ( ; mark; mark = mark->next )
717  if ( fom_max < mark->fom ) fom_max = mark->fom;
718 
719  Tcl_SetDoubleObj(returnObj, fom_max);
720 
721  return returnObj;
722 }
723 
724 Tcl_Obj* marker_select(Bmarker* marklist, double radius, int objc, Tcl_Obj *CONST objv[])
725 {
726  Tcl_Obj* returnObj = Tcl_NewObj();
727 
728  int id(0), i(-1);
729  double x(0), y(0), z(0);
730 
731  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &x);
732  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &y);
733  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &z);
734 
735  double d, dmin;
736  Vector3<float> loc(x, y, z);
737  Bmarker* mark;
738 
739  if ( marklist ) {
740  dmin = 2*radius;
741  for ( mark = marklist; mark; mark = mark->next ) {
742  d = loc.distance(mark->loc);
743  if ( dmin > d ) {
744  dmin = d;
745  i = mark->id;
746  }
747  }
748  if ( dmin <= radius ) id = i;
749  }
750 
751  Tcl_SetIntObj(returnObj, id);
752 
753  return returnObj;
754 }
755 
756 Tcl_Obj* marker_select_rectangle(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[])
757 {
758  Tcl_Obj* returnObj = Tcl_NewObj();
759 
760  double x1(0), y1(0), x2(0), y2(0);
761 
762  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &x1);
763  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &y1);
764  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &x2);
765  if ( objc > 7 ) Tcl_GetDoubleFromObj(NULL, objv[7], &y2);
766 
767  if ( x2 < x1 ) swap(x1, x2);
768  if ( y2 < y1 ) swap(y1, y2);
769 
770  char string[MAXLINELEN] = "";
771  Bmarker* mark;
772 
773  if ( marklist ) {
774  for ( mark = marklist; mark; mark = mark->next ) {
775  if ( mark->loc[0] >= x1 && mark->loc[0] <= x2 &&
776  mark->loc[1] >= y1 && mark->loc[1] <= y2 ) {
777  sprintf(string, " %d", mark->id);
778  Tcl_AppendToObj(returnObj, string, strlen(string));
779  }
780  }
781  }
782 
783  return returnObj;
784 }
785 
786 Tcl_Obj* marker_get_selection_flag(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[])
787 {
788  Tcl_Obj* returnObj = Tcl_NewObj();
789 
790  int id(0), sel(-1);
791 
792  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
793 
794  Bmarker* mark;
795 
796  for ( mark = marklist; mark && mark->id != id; mark = mark->next ) ;
797 
798  if ( mark ) sel = mark->sel;
799 
800  Tcl_SetIntObj(returnObj, sel);
801 
802  return returnObj;
803 }
804 
805 Tcl_Obj* marker_set_selection_flag(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[])
806 {
807  Tcl_Obj* returnObj = Tcl_NewObj();
808 
809  int id(0), sel(1), found(0);
810 
811  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
812  if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &sel);
813 
814  Bmarker* mark;
815 
816  for ( mark = marklist; mark && mark->id != id; mark = mark->next ) ;
817 
818  if ( mark ) mark->sel = found = sel;
819 
820  Tcl_SetIntObj(returnObj, found);
821 
822  return returnObj;
823 }
824 
825 Tcl_Obj* marker_clear_selection_flag(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[])
826 {
827  Tcl_Obj* returnObj = Tcl_NewObj();
828 
829  int id(0), found(0);
830 
831  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
832 
833  Bmarker* mark;
834 
835  for ( mark = marklist; mark && mark->id != id; mark = mark->next ) ;
836 
837  if ( mark ) {
838  mark->sel = 0;
839  found = 1;
840  }
841 
842  Tcl_SetIntObj(returnObj, found);
843 
844  return returnObj;
845 }
846 
847 Tcl_Obj* marker_move(Bmarker* marklist, int objc, Tcl_Obj *CONST objv[])
848 {
849  Tcl_Obj* returnObj = Tcl_NewObj();
850 
851  int id(0);
852  double dx(0), dy(0), dz(0);
853 
854  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
855  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &dx);
856  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &dy);
857  if ( objc > 7 ) Tcl_GetDoubleFromObj(NULL, objv[7], &dz);
858 
859  Bmarker* mark;
860  Vector3<float> d = Vector3<float>(dx, dy, dz);
861 
862  if ( marklist ) {
863  for ( mark = marklist; mark && mark->id != id; mark = mark->next ) ;
864  if ( mark ) {
865  mark->loc += d;
866  mark->err -= d;
867  }
868  }
869 
870  Tcl_SetIntObj(returnObj, id);
871 
872  return returnObj;
873 }
874 
875 int marker_create(Bmarker* mark, int objc, Tcl_Obj *CONST objv[])
876 {
877 // Tcl_Obj* returnObj = Tcl_NewObj();
878 
879  double x, y, z;
880 
881  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &x);
882  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &y);
883  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &z);
884 
885  if ( mark ) {
886  mark->loc = Vector3<float>(x, y, z);
887  mark->fom = 1;
888  mark->sel = 1;
889  }
890 
891  return 0;
892 }
893 
894 Tcl_Obj* marker_create(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[])
895 {
896  Tcl_Obj* returnObj = Tcl_NewObj();
897 
898  int id(0);
899 
900  Bmarker* mark = NULL;
901 
902  if ( mg ) {
903  for ( mark = mg->mark; mark; mark = mark->next )
904  if ( id < mark->id ) id = mark->id;
905  mark = (Bmarker *) add_item((char **) &mg->mark, sizeof(Bmarker));
906  }
907 
908  if ( mark ) {
909  mark->id = ++id;
910  mark->img_num = mg->img_num;
911  marker_create(mark, objc, objv);
912  }
913 
914  Tcl_SetIntObj(returnObj, id);
915 
916  return returnObj;
917 }
918 
919 Tcl_Obj* marker_create(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[])
920 {
921  Tcl_Obj* returnObj = Tcl_NewObj();
922 
923  int id(0);
924 
925  Bmarker* mark = NULL;
926 
927  if ( rec ) {
928  for ( mark = rec->mark; mark; mark = mark->next )
929  if ( id < mark->id ) id = mark->id;
930  mark = (Bmarker *) add_item((char **) &rec->mark, sizeof(Bmarker));
931  }
932 
933  if ( mark ) {
934  mark->id = ++id;
935  mark->img_num = 0;
936  marker_create(mark, objc, objv);
937  }
938 
939  Tcl_SetIntObj(returnObj, id);
940 
941  return returnObj;
942 }
943 
944 Tcl_Obj* marker_center(Bmarker* mark, Bimage* p, int objc, Tcl_Obj *CONST objv[])
945 {
946  Tcl_Obj* returnObj = Tcl_NewObj();
947 
948  Vector3<float> center(p->sizeX()/2, p->sizeY()/2, p->sizeZ()/2);
949 
950  if ( mark ) markers_center(mark, center);
951 
952  return returnObj;
953 }
954 
956 {
957  Tcl_Obj* returnObj = Tcl_NewObj();
958 
959  if ( !project ) return returnObj;
960  if ( !project->field ) return returnObj;
961  if ( !project->field->mg ) return returnObj;
962 
963  if ( project->field->mg->origin[0] < 1 || project->field->mg->origin[1] < 1 )
965 
967 
968 // if ( !project->rec || !project->rec->mark )
970 
972 
973  return returnObj;
974 }
975 
976 Tcl_Obj* marker_find(Bmicrograph* mg, Bimage* p, int objc, Tcl_Obj *CONST objv[])
977 {
978  Tcl_Obj* returnObj = Tcl_NewObj();
979 
980  double r(0);
981 
982  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &r);
983 
984  Bmarker* mark = NULL;
985 
986  if ( mg ) {
987  mark = img_find_gold_particles(p, mg->img_num, r, 0, 0);
988  if ( mark ) {
989  if ( !mg->mark ) mg->mark = mark;
990  else markers_add(&mg->mark, mark, 2*mg->mark_radius, 2);
991  }
992  }
993 
994  int nmark = count_list((char *) mg->mark);
995 
996  Tcl_SetIntObj(returnObj, nmark);
997 
998  return returnObj;
999 }
1000 
1001 Tcl_Obj* marker_snap(Bmicrograph* mg, Bimage* p, long mid, int objc, Tcl_Obj *CONST objv[])
1002 {
1003  Tcl_Obj* returnObj = Tcl_NewObj();
1004 
1005  if ( mg ) {
1006  Bmarker* mark = NULL;
1007  for ( mark = mg->mark; mark && mark->id != mid; mark = mark->next ) ;
1008  if ( mark ) {
1009  long i(0);
1010  double v, avg(0), wcoor(0);
1011  Vector3<float> acoor;
1012  Vector3<long> size = Vector3<long>(6*mg->mark_radius, 6*mg->mark_radius, 1);
1013  Vector3<long> coor = mark->loc - size/2;
1014  Bimage* pm = p->extract(mg->img_num, coor, size, 0, 0);
1015  for ( i=0; i<pm->image_size(); ++i ) avg += (*pm)[i];
1016  avg /= pm->image_size();
1017  for ( i=0; i<pm->image_size(); ++i ) {
1018  v = (*pm)[i];
1019  if ( (*pm)[i] < avg ) {
1020  acoor += pm->coordinates(i) * v;
1021  wcoor += v;
1022  }
1023  }
1024  acoor /= wcoor;
1025  mark->loc = acoor + coor;
1026  delete pm;
1027  }
1028  }
1029 
1030  Tcl_SetDoubleObj(returnObj, 0);
1031 
1032  return returnObj;
1033 }
1034 
1035 Tcl_Obj* marker_extract(Bmicrograph* mg, Bimage* p, int objc, Tcl_Obj *CONST objv[])
1036 {
1037  Tcl_Obj* returnObj = Tcl_NewObj();
1038 
1039  if ( !p ) return returnObj;
1040 
1041  Bstring filename;
1042  float box_radius(0);
1043  Bimage* part = NULL;
1044 
1045  /* Particle image file name */
1046  if ( objc > 4 ) filename = Tcl_GetStringFromObj(objv[4], NULL);
1047 
1048  if ( filename.length() ) {
1049  if ( mg ) {
1050  part = micrograph_extract_gold(mg, p, box_radius);
1051  if ( part ) {
1052  mg->fpart = filename;
1053  write_img(filename, part, 0);
1054  delete part;
1055  }
1056  }
1057  }
1058 
1059  return returnObj;
1060 }
1061 
1062 Tcl_Obj* marker_extract(Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[])
1063 {
1064  Tcl_Obj* returnObj = Tcl_NewObj();
1065 
1066  if ( !p ) return returnObj;
1067 
1068  Bstring filename;
1069  float box_radius(0);
1070  Bimage* part = NULL;
1071 
1072  /* Particle image file name */
1073  if ( objc > 4 ) filename = Tcl_GetStringFromObj(objv[4], NULL);
1074 
1075  if ( filename.length() ) {
1076  if ( rec ) {
1077  part = reconstruction_extract_gold(rec, p, box_radius);
1078  if ( part ) {
1079  rec->fpart = filename;
1080  write_img(filename, part, 0);
1081  delete part;
1082  }
1083  }
1084  }
1085 
1086  return returnObj;
1087 }
1088 
1089 Tcl_Obj* marker_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[])
1090 {
1091  Tcl_Obj* returnObj = Tcl_NewObj();
1092 
1093  int id(0);
1094  double fom_cut(0);
1095 
1096  Bstring action = Tcl_GetStringFromObj(objv[4], NULL);
1097 
1098  Bfield* field;
1099  Bmicrograph* mg = NULL;
1100  Breconstruction* rec;
1101  Bmarker* mark;
1102 
1103  if ( action == "all" ) {
1104  for ( field = project->field; field; field = field->next ) {
1105  for ( mg = field->mg; mg; mg = mg->next ) {
1106  kill_list((char *) mg->mark, sizeof(Bmarker));
1107  mg->mark = NULL;
1108  }
1109  }
1110  for ( rec = project->rec; rec; rec = rec->next ) {
1111  kill_list((char *) rec->mark, sizeof(Bmarker));
1112  rec->mark = NULL;
1113  }
1114  } else if ( action == "fom" ) {
1115  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &fom_cut);
1116  for ( field = project->field; field; field = field->next ) {
1117  for ( mg = field->mg; mg; mg = mg->next ) {
1118  for ( mark = mg->mark; mark; mark = mark->next )
1119  if ( mark->fom < fom_cut ) mark->sel = 0;
1120  else if ( mark->sel < 1 ) mark->sel = 1;
1122  }
1123  }
1124  for ( rec = project->rec; rec; rec = rec->next ) {
1125  for ( mark = rec->mark; mark; mark = mark->next )
1126  if ( mark->fom < fom_cut ) mark->sel = 0;
1127  else if ( mark->sel < 1 ) mark->sel = 1;
1129  }
1130  } else {
1131  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
1132  if ( id > 0 ) { // Delete all markers with this id for both micrographs and reconstructions
1133  for ( field = project->field; field; field = field->next ) {
1134  for ( mg = field->mg; mg; mg = mg->next ) {
1135  for ( mark = mg->mark; mark && mark->id != id; mark = mark->next ) ;
1136  if ( mark )
1137  remove_item((char **)&mg->mark, (char *)mark, sizeof(Bmarker));
1138  }
1139  }
1140  for ( rec = project->rec; rec; rec = rec->next ) {
1141  for ( mark = rec->mark; mark && mark->id != id; mark = mark->next ) ;
1142  if ( mark )
1143  remove_item((char **)&rec->mark, (char *)mark, sizeof(Bmarker));
1144  }
1145  }
1146  }
1147 
1148  Tcl_SetIntObj(returnObj, id);
1149 
1150  return returnObj;
1151 }
1152 
1153 Tcl_Obj* marker_delete(Bfield* field, int objc, Tcl_Obj *CONST objv[])
1154 {
1155  Tcl_Obj* returnObj = Tcl_NewObj();
1156 
1157  if ( !field ) return returnObj;
1158 
1159  int id(0);
1160  double fom_cut(0);
1161 
1162  Bstring action = Tcl_GetStringFromObj(objv[4], NULL);
1163 
1164  Bmicrograph* mg = NULL;
1165  Bmarker* mark;
1166 
1167  if ( action == "all" ) {
1168  for ( mg = field->mg; mg; mg = mg->next ) {
1169  kill_list((char *) mg->mark, sizeof(Bmarker));
1170  mg->mark = NULL;
1171  }
1172  } else if ( action == "fom" ) {
1173  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &fom_cut);
1174  for ( mg = field->mg; mg; mg = mg->next ) {
1175  for ( mark = mg->mark; mark; mark = mark->next )
1176  if ( mark->fom < fom_cut ) mark->sel = 0;
1177  else if ( mark->sel < 1 ) mark->sel = 1;
1179  }
1180  } else {
1181  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
1182  if ( id > 0 ) {
1183  for ( mg = field->mg; mg; mg = mg->next ) {
1184  for ( mark = mg->mark; mark && mark->id != id; mark = mark->next ) ;
1185  if ( mark )
1186  remove_item((char **)&mg->mark, (char *)mark, sizeof(Bmarker));
1187  }
1188  }
1189  }
1190 
1191  Tcl_SetIntObj(returnObj, id);
1192 
1193  return returnObj;
1194 }
1195 
1196 Tcl_Obj* marker_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[])
1197 {
1198  Tcl_Obj* returnObj = Tcl_NewObj();
1199 
1200  if ( !mg ) return returnObj;
1201 
1202  int id(0);
1203  double fom_cut(0);
1204 
1205  Bstring action = Tcl_GetStringFromObj(objv[4], NULL);
1206 
1207  Bmarker* mark;
1208 
1209  if ( action == "all" ) {
1210  kill_list((char *) mg->mark, sizeof(Bmarker));
1211  mg->mark = NULL;
1212  } else if ( action == "fom" ) {
1213  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &fom_cut);
1214  for ( mark = mg->mark; mark; mark = mark->next )
1215  if ( mark->fom < fom_cut ) mark->sel = 0;
1216  else if ( mark->sel < 1 ) mark->sel = 1;
1217 // mark = mg->mark;
1218 // mg->mark = markers_copy_selected(mark);
1219 // kill_list((char *) mark, sizeof(Bmarker));
1221  } else {
1222  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
1223  if ( id > 0 ) {
1224  for ( mark = mg->mark; mark && mark->id != id; mark = mark->next ) ;
1225  if ( mark ) remove_item((char **)&mg->mark, (char *)mark, sizeof(Bmarker));
1226  }
1227  }
1228 
1229  Tcl_SetIntObj(returnObj, id);
1230 
1231  return returnObj;
1232 }
1233 
1234 Tcl_Obj* marker_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[])
1235 {
1236  Tcl_Obj* returnObj = Tcl_NewObj();
1237 
1238  if ( !rec ) return returnObj;
1239 
1240  int id(0);
1241  double fom_cut(0);
1242 
1243  Bstring action = Tcl_GetStringFromObj(objv[4], NULL);
1244 
1245  Bmarker* mark;
1246 
1247  if ( action == "all" ) {
1248  kill_list((char *) rec->mark, sizeof(Bmarker));
1249  rec->mark = NULL;
1250  } else if ( action == "fom" ) {
1251  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &fom_cut);
1252  for ( mark = rec->mark; mark; mark = mark->next )
1253  if ( mark->fom < fom_cut ) mark->sel = 0;
1254  else if ( mark->sel < 1 ) mark->sel = 1;
1256  } else {
1257  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
1258  if ( id > 0 ) {
1259  for ( mark = rec->mark; mark && mark->id != id; mark = mark->next ) ;
1260  if ( mark ) remove_item((char **)&rec->mark, (char *)mark, sizeof(Bmarker));
1261  }
1262  }
1263 
1264  Tcl_SetIntObj(returnObj, id);
1265 
1266  return returnObj;
1267 }
1268 
1269 Tcl_Obj* marker_edge(Bmarker* mark, Bimage* p, int objc, Tcl_Obj *CONST objv[])
1270 {
1271  Tcl_Obj* returnObj = Tcl_NewObj();
1272 
1273  int edge(0);
1274 
1275  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &edge);
1276 
1277  float xmin, xmax, ymin, ymax;
1278 
1279  if ( mark ) {
1280  xmin = edge;
1281  xmax = p->sizeX() - edge;
1282  ymin = edge;
1283  ymax = p->sizeY() - edge;
1284  for ( ; mark && mark->id; mark = mark->next ) {
1285  if ( mark->loc[0] < xmin || mark->loc[0] > xmax || mark->loc[1] < ymin || mark->loc[1] > ymax )
1286  mark->fom = -fabs(mark->fom);
1287  else
1288  mark->fom = fabs(mark->fom);
1289  }
1290  }
1291 
1292  return returnObj;
1293 }
1294 
1295 Tcl_Obj* marker_accept(Bmarker* mark, int objc, Tcl_Obj *CONST objv[])
1296 {
1297  Tcl_Obj* returnObj = Tcl_NewObj();
1298 
1299  int id(0);
1300 
1301  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &id);
1302 
1303  for ( ; mark && mark->id != id; mark = mark->next ) ;
1304 
1305  if ( mark ) {
1306  mark->loc += mark->err;
1307  mark->err = 0;
1308  }
1309 
1310  return returnObj;
1311 }
1312 
1313 
1314 
Bimage::coordinates
void coordinates(long i, long &nx, long &ny, long &nz)
Definition: Bimage.h:429
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
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:310
Bmarker::fom
float fom
Definition: marker.h:28
do_tomo_thickness
Tcl_Obj * do_tomo_thickness(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:68
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::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Breconstruction::fpart
Bstring fpart
Definition: mg_processing.h:291
Bstring::remove
Bstring remove(const char c)
Definition: Bstring.cpp:473
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:129
markers_add
long markers_add(Bmarker **mark, Bmarker *mark2, double mindist, int sel)
Adds a set of markers to another.
Definition: marker.cpp:170
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
project_check_markers
int project_check_markers(Bproject *project, int flags)
Checks if the markers in a project parameter structure fall within micrographs.
Definition: mg_tomography.cpp:2108
marker_set_selection_flag
Tcl_Obj * marker_set_selection_flag(Bmarker *marklist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:805
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bproject::select
int select
Definition: mg_processing.h:342
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bmarker::img_num
int img_num
Definition: marker.h:24
Bstring
Definition: Bstring.h:29
project_calculate_model
int project_calculate_model(Bproject *project)
Calculates the z-coordinates of the marker model.
Definition: mg_tomography.cpp:1083
Bimage::next
Bimage * next
Definition: Bimage.h:179
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
marker_ids
Tcl_Obj * marker_ids(Bproject *project, double fom_cut)
Definition: tcltk_bmarker.cpp:502
marker_generate_from_seed
Tcl_Obj * marker_generate_from_seed(Bproject *project)
Definition: tcltk_bmarker.cpp:955
Bproject::field
Bfield * field
Definition: mg_processing.h:346
base
string base(string &filename)
Definition: string_util.cpp:28
project_sort_markers_by_id
int project_sort_markers_by_id(Bproject *project)
Sorts markers for each micrograph by ID number.
Definition: mg_tomography.cpp:844
marker_delete
Tcl_Obj * marker_delete(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:1089
project_set_nominal_mg_origins
int project_set_nominal_mg_origins(Bproject *project)
Sets micrograph origins to the centers of the micrographs.
Definition: mg_img_proc.cpp:770
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
do_tomo_refine_one
int do_tomo_refine_one(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:216
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
marker_get_selection_flag
Tcl_Obj * marker_get_selection_flag(Bmarker *marklist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:786
marker_snap
Tcl_Obj * marker_snap(Bmicrograph *mg, Bimage *p, long mid, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:1001
marker_count
Tcl_Obj * marker_count(Bproject *project)
Definition: tcltk_bmarker.cpp:434
Bplot
Definition: Bplot.h:146
Bfield::id
Bstring id
Definition: mg_processing.h:262
Bmicrograph::fpart
Bstring fpart
Definition: mg_processing.h:208
marker_range
Vector3< double > marker_range(Bmarker *markers)
Calculating the marker location range.
Definition: marker.cpp:100
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Breconstruction::mark_radius
double mark_radius
Definition: mg_processing.h:304
Bfield
Definition: mg_processing.h:260
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bmarker::res
float res
Definition: marker.h:27
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
marker_fom
Tcl_Obj * marker_fom(Bmarker *mark)
Definition: tcltk_bmarker.cpp:697
Bmicrograph::fframe
Bstring fframe
Definition: mg_processing.h:207
marker_center
Tcl_Obj * marker_center(Bmarker *mark, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:944
Bmarker
Definition: marker.h:21
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
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
do_tomo_track
int do_tomo_track(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:154
project
Bproject * project
Definition: bshow.cpp:33
rwmg.h
Header file for reading and writing micrograph parameters.
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
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bmarker::next
Bmarker * next
Definition: marker.h:22
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
marker_select
Tcl_Obj * marker_select(Bmarker *marklist, double rad, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:724
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
Bmarker::id
int id
Definition: marker.h:23
Bimage::image_size
long image_size()
Definition: Bimage.h:290
project_tomo_residuals
double project_tomo_residuals(Bproject *project, int show)
Calculates the residuals for an alignment solution.
Definition: mg_tomography.cpp:868
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
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
project_fit_intensities
double project_fit_intensities(Bproject *project, Bplot *plot=NULL, int flag=0)
Determines the thickness to mean free path ratio from a tilt series.
Definition: mg_tomography.cpp:619
marker_find
Tcl_Obj * marker_find(Bmicrograph *mg, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:976
do_marker
Tcl_Obj * do_marker(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:279
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
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
imglist
Bimage * imglist
Definition: bshow.cpp:32
project_renumber_markers
long project_renumber_markers(Bproject *project)
Renumbers the markers in a project hierarchy.
Definition: mg_tomography.cpp:1732
project_intensity_plot
Bplot * project_intensity_plot(Bproject *project)
Transfers the micrograph intensities for a field into a plot.
Definition: mg_tomography.cpp:515
remove_item
char * remove_item(char **list, char *item, unsigned long size)
Finds the given item and deletes it from the linked list.
Definition: linked_list.cpp:114
markers_renumber
long markers_renumber(Bmarker *mark)
Renumbers a set of markers.
Definition: marker.cpp:1200
mg_img_proc.h
Header file for image processing from micrograph structures.
marker_create
Tcl_Obj * marker_create(Bmicrograph *mg, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:894
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:238
Bmarker::loc
Vector3< float > loc
Definition: marker.h:25
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
mg_extract.h
Header file for functions to extract particles from micrographs.
verbose
int verbose
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
marker_move
Tcl_Obj * marker_move(Bmarker *marklist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:847
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
marker_clear_selection_flag
Tcl_Obj * marker_clear_selection_flag(Bmarker *marklist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:825
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
project_refine
double project_refine(Bproject *project, int iter, double tol, Bstring refop)
Refines all the alignment parameters.
Definition: mg_tomo_track.cpp:870
do_tomo_findaxis
int do_tomo_findaxis(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:131
Bfield::next
Bfield * next
Definition: mg_processing.h:261
Bmicrograph::mark
Bmarker * mark
Definition: mg_processing.h:245
tcltk_bmarker.h
A shared object to manage markers in micrograph parameter files in TCL/Tk.
img_find_gold_particles
Bmarker * img_find_gold_particles(Bimage *p, int img_select, double radius, long edge, double cutoff)
Finds gold particles in micrograph images.
Definition: mg_tomography.cpp:228
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
Bmarker::sel
int sel
Definition: marker.h: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
linked_list.h
Header file for generalized linked list functions.
do_tomo_transfer_seed
int do_tomo_transfer_seed(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:102
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
marker_list
Tcl_Obj * marker_list(Bproject *project)
Definition: tcltk_bmarker.cpp:581
reconstruction_extract_gold
Bimage * reconstruction_extract_gold(Breconstruction *rec, Bimage *p, double radius)
Extracts gold particle images from a reconstruction.
Definition: mg_extract.cpp:683
Bstring::base
Bstring base()
Definition: Bstring.cpp:516
marker_edge
Tcl_Obj * marker_edge(Bmarker *mark, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:1269
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
marker_residual
Tcl_Obj * marker_residual(Bproject *project)
Definition: tcltk_bmarker.cpp:673
do_tomo_refine
int do_tomo_refine(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:179
marker_fom_maximum
Tcl_Obj * marker_fom_maximum(Bmarker *mark)
Definition: tcltk_bmarker.cpp:710
marker_accept
Tcl_Obj * marker_accept(Bmarker *mark, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:1295
marker_extract
Tcl_Obj * marker_extract(Bmicrograph *mg, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:1035
project_generate_markers
int project_generate_markers(Bproject *project)
Generates missing micrograph and model markers.
Definition: mg_tomography.cpp:1204
marker_location
Tcl_Obj * marker_location(Bmarker *mark)
Definition: tcltk_bmarker.cpp:657
micrograph_extract_gold
Bimage * micrograph_extract_gold(Bmicrograph *mg, Bimage *p, double radius)
Extracts gold particle images from a micrograph.
Definition: mg_extract.cpp:665
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
markers_delete_non_selected
long markers_delete_non_selected(Bmarker **mark)
Deletes markers not selected.
Definition: marker.cpp:209
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
marker_select_rectangle
Tcl_Obj * marker_select_rectangle(Bmarker *marklist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmarker.cpp:756
mg_tomography.h
Header file for single particle image processing in Bsoft.
markers_center
long markers_center(Bmarker *mark, Vector3< double > center)
Centers a set of markers.
Definition: marker.cpp:1226
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284