| Bsoft 2.0.5 Bernard's software package

tcltk_bfil.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_bfil.h"
13 #include "mg_extract.h"
14 #include "mg_helix.h"
15 #include "rwmg.h"
16 #include "linked_list.h"
17 #include "timer.h"
18 #include "utilities.h"
19
20 #include <sys/stat.h>
21
22 // Declaration of global variables
23 extern int verbose; // Level of output to the screen
24 extern Bimage* imglist;
25
26 // Internal function prototypes
27 Tcl_Obj* filament_count(Bproject* project);
28 Tcl_Obj* filament_count(Bfield* field);
29 Tcl_Obj* filament_count(Bmicrograph* mg);
30 Tcl_Obj* filament_count(Breconstruction* rec);
31 Tcl_Obj* filament_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[]);
32 Tcl_Obj* filament_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[]);
33 Tcl_Obj* filament_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[]);
34 Tcl_Obj* filament_extract(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
35 Tcl_Obj* filament_profile(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
36 Tcl_Obj* filament_center(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
37 Tcl_Obj* filament_to_particles(Bproject* project, int objc, Tcl_Obj *CONST objv[]);
38
39 Tcl_Obj* node_count(Bproject* project);
40 Tcl_Obj* node_count(Bfield* field);
41 Tcl_Obj* node_count(Bmicrograph* mg);
42 Tcl_Obj* node_count(Breconstruction* rec);
43 Tcl_Obj* node_ids(Bfilament* fillist);
44 Tcl_Obj* node_location(Bfilament* fillist, int objc, Tcl_Obj *CONST objv[]);
45 Tcl_Obj* node_select(Bfilament* fillist, double radius, int objc, Tcl_Obj *CONST objv[]);
46 Tcl_Obj* node_move(Bfilament* fillist, int objc, Tcl_Obj *CONST objv[]);
47 Tcl_Obj* node_create(Bfilament** fillist, int objc, Tcl_Obj *CONST objv[]);
48 Tcl_Obj* node_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[]);
49 Tcl_Obj* node_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[]);
50 Tcl_Obj* node_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[]);
51
52
53 /*
54 @brief Functions for filament picking.
55 Filament interface syntax:
56 Bmg filament [item] [action] [params]
57 Actions:
58 count counts the number of filaments
59 delete [all|mg|rec|fid] deletes filaments
60 extract [f] [w] [ax] [sf] [p] extracts filaments from a micrograph
61 profile [fid] [nid] [w] plots a filament profile
62 center [w] centers nodes based on profiles
63 toparticles [w] [b] [r] [a] generates particles along filaments
64 Parameters:
65 fid filament identifier
66 nid node identifier
67 f file name
68 w filament width
69 ax extracted filament axis
70 sf split flag
71 p filament path
72 b boxing interval
73 r helical rise
74 a helical rotation angle
75 */
76 Tcl_Obj* do_filament(Bproject* project, int objc, Tcl_Obj *CONST objv[])
77 {
78 Tcl_Obj* returnObj = NULL;
79
80 Bstring item = Tcl_GetStringFromObj(objv[2], NULL);
81 if ( verbose & VERB_DEBUG )
82 cout << "DEBUG do_filament: Item: " << item << " (" << item.length() << ")" << endl;
83
84 Bstring id = item.post(':');
85 id = id.remove(' ');
86 item = item.pre(':');
87
88 if ( !item.contains("Field") && !item.contains("Micrograph") && !item.contains("Reconstruction") ) {
89 cerr << "Error in do_filament: Item " << item << " must be a micrograph or reconstruction!" << endl;
90 returnObj = Tcl_NewObj();
91 return returnObj;
92 }
93
94 Bfield* field = NULL;
95 Bmicrograph* mg = NULL;
96 Breconstruction* rec = NULL;
97 Bimage* p = NULL;
98 Bstring filename, filename2, base, delete_what;
99
100 if ( item.contains("Field") ) {
101 for ( field = project->field; field && field->id != id; field = field->next ) ;
102 if ( field ) {
103 mg = field->mg;
104 filename = field->mg->fmg;
105 } else
106 cerr << "Error in do_filament: Field " << id << " not found!" << endl;
107 } else if ( item.contains("Micrograph") ) {
108 for ( field = project->field; field; field = field->next ) {
109 for ( mg = field->mg; mg && mg->id != id; mg = mg->next ) ;
110 if ( mg ) break;
111 }
112 if ( mg ) {
113 if ( mg->fmg.length() )
114 filename = mg->fmg;
115 else if ( mg->fframe.length() )
116 filename = mg->fframe;
117 } else
118 cerr << "Error in do_filament: Micrograph " << id << " not found!" << endl;
119 } else if ( item.contains("Reconstruction") ) {
120 for ( rec = project->rec; rec && rec->id != id; rec = rec->next ) ;
121 if ( rec ) {
122 filename = rec->frec;
123 } else
124 cerr << "Error in do_filament: Reconstruction " << id << " not found!" << endl;
125 }
126
127 if ( filename.length() ) {
128 base = filename.base();
129 for ( p = imglist; p; p = p->next ) {
130 filename2 = p->file_name();
131 if ( filename2.base() == base ) break;
132 }
133 }
134
135 Bstring action = Tcl_GetStringFromObj(objv[3], NULL);
136 if ( verbose & VERB_DEBUG )
137 cout << "DEBUG do_filament: Action: " << action << " (" << action.length() << ")" << endl;
138
139 if ( action == "count" ) {
140 if ( mg ) returnObj = filament_count(mg);
141 else if ( field ) returnObj = filament_count(field);
142 else if ( rec ) returnObj = filament_count(rec);
143 else returnObj = filament_count(project);
144 } else if ( action == "delete" ) {
145 if ( objc > 4 ) delete_what = Tcl_GetStringFromObj(objv[4], NULL);
146 if ( delete_what == "all" || delete_what == "mg" || delete_what == "rec" )
147 returnObj = filament_delete(project, objc, objv);
148 else if ( mg ) returnObj = filament_delete(mg, objc, objv);
149 else if ( rec ) returnObj = filament_delete(rec, objc, objv);
150 else returnObj = filament_delete(project, objc, objv);
151 } else if ( action == "extract" ) {
152 if ( p ) returnObj = filament_extract(mg, rec, p, objc, objv);
153 } else if ( action == "profile" ) {
154 if ( p ) returnObj = filament_profile(mg, rec, p, objc, objv);
155 } else if ( action == "center" ) {
156 if ( p ) returnObj = filament_center(mg, rec, p, objc, objv);
157 } else if ( action == "toparticles" ) {
158 returnObj = filament_to_particles(project, objc, objv);
159 } else {
160 cerr << "Error: Action " << action << " not recognized!" << endl;
161 }
162
163 if ( !returnObj ) {
164 returnObj = Tcl_NewObj();
165 Tcl_SetIntObj(returnObj, 0);
166 }
167
168 return returnObj;
169 }
170
171 Tcl_Obj* filament_count(Bproject* project)
172 {
173 Tcl_Obj* returnObj = Tcl_NewObj();
174
175 int nfil(0);
176 Bfield* field = project->field;
177 Bmicrograph* mg;
178 Breconstruction* rec;
179
180 if ( project->select < 1 ) {
181 for ( field = project->field; field; field = field->next )
182 for ( mg = field->mg; mg; mg = mg->next )
183 if ( mg->fil )
184 nfil += count_list((char *) mg->fil);
185 } else {
186 for ( rec = project->rec; rec; rec = rec->next )
187 if ( rec->fil )
188 nfil += count_list((char *) rec->fil);
189 }
190
191 Tcl_SetIntObj(returnObj, nfil);
192
193 return returnObj;
194 }
195
196 Tcl_Obj* filament_count(Bfield* field)
197 {
198 Tcl_Obj* returnObj = Tcl_NewObj();
199
200 int nfil(0);
201 Bmicrograph* mg;
202
203 if ( field ) {
204 for ( mg = field->mg; mg; mg = mg->next )
205 if ( mg->fil )
206 nfil += count_list((char *) mg->fil);
207 }
208
209 Tcl_SetIntObj(returnObj, nfil);
210
211 return returnObj;
212 }
213
214 Tcl_Obj* filament_count(Bmicrograph* mg)
215 {
216 Tcl_Obj* returnObj = Tcl_NewObj();
217
218 int nfil(0);
219
220 if ( mg && mg->fil )
221 nfil = count_list((char *) mg->fil);
222
223 Tcl_SetIntObj(returnObj, nfil);
224
225 return returnObj;
226 }
227
228 Tcl_Obj* filament_count(Breconstruction* rec)
229 {
230 Tcl_Obj* returnObj = Tcl_NewObj();
231
232 int nfil(0);
233
234 if ( rec && rec->fil )
235 nfil = count_list((char *) rec->fil);
236
237 Tcl_SetIntObj(returnObj, nfil);
238
239 return returnObj;
240 }
241
242 Tcl_Obj* filament_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[])
243 {
244 Tcl_Obj* returnObj = Tcl_NewObj();
245
246 int fid(0), n(-1), all(0);
247
248 Bstring action = Tcl_GetStringFromObj(objv[4], NULL);
249
250 char string[64] = "";
251 Bfield* field = project->field;
252 Bmicrograph* mg = NULL;
253 Breconstruction* rec;
254 Bfilament* fil = NULL;
255
256 if ( action == "all" || action == "mg" ) {
257 for ( field = project->field; field; field = field->next ) {
258 for ( mg = field->mg; mg; mg = mg->next ) {
259 filament_kill(mg->fil);
260 mg->fil = NULL;
261 }
262 }
263 all = 1;
264 }
265
266 if ( action == "all" || action == "rec" ) {
267 for ( rec = project->rec; rec; rec = rec->next ) {
268 filament_kill(rec->fil);
269 rec->fil = NULL;
270 }
271 all = 1;
272 }
273
274 if ( !all ) {
275 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
276 // if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &n);
277 }
278
279 if ( fid > 0 ) {
280 if ( project->select < 1 ) {
281 if ( n < 0 ) {
282 if ( field ) for ( mg = field->mg; mg; mg = mg->next ) {
283 for ( fil = mg->fil; fil && fil->id != fid; fil = fil->next ) ;
284 if ( fil ) {
285 kill_list((char *)fil->node, sizeof(Bfilnode));
286 remove_item((char **)&mg->fil, (char *)fil, sizeof(Bfilament));
287 }
288 }
289 } else {
290 if ( field ) for ( mg = field->mg; mg && mg->img_num != n; mg = mg->next ) ;
291 if ( mg ) {
292 for ( fil = mg->fil; fil && fil->id != fid; fil = fil->next ) ;
293 if ( fil ) {
294 kill_list((char *)fil->node, sizeof(Bfilnode));
295 remove_item((char **)&mg->fil, (char *)fil, sizeof(Bfilament));
296 }
297 }
298 }
299 } else {
300 for ( rec = project->rec; rec; rec = rec->next ) {
301 for ( fil = rec->fil; fil && fil->id != fid; fil = fil->next ) ;
302 if ( fil ) {
303 kill_list((char *)fil->node, sizeof(Bfilnode));
304 remove_item((char **)&rec->fil, (char *)fil, sizeof(Bfilament));
305 }
306 }
307 }
308 }
309
310 sprintf(string, "%d", fid);
311 Tcl_SetStringObj(returnObj, string, strlen(string));
312
313 return returnObj;
314 }
315
316 Tcl_Obj* filament_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[])
317 {
318 Tcl_Obj* returnObj = Tcl_NewObj();
319
320 int fid(0);
321 Bfilament* fil = NULL;
322
323 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
324
325 if ( mg && fid > 0 ) {
326 for ( fil = mg->fil; fil && fil->id != fid; fil = fil->next ) ;
327 if ( fil ) {
328 kill_list((char *)fil->node, sizeof(Bfilnode));
329 remove_item((char **)&mg->fil, (char *)fil, sizeof(Bfilament));
330 }
331 }
332
333 Tcl_SetIntObj(returnObj, fid);
334
335 return returnObj;
336 }
337
338 Tcl_Obj* filament_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[])
339 {
340 Tcl_Obj* returnObj = Tcl_NewObj();
341
342 int fid(0);
343 Bfilament* fil = NULL;
344
345 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
346
347 if ( rec && fid > 0 ) {
348 for ( fil = rec->fil; fil && fil->id != fid; fil = fil->next ) ;
349 if ( fil ) {
350 kill_list((char *)fil->node, sizeof(Bfilnode));
351 remove_item((char **)&rec->fil, (char *)fil, sizeof(Bfilament));
352 }
353 }
354
355 Tcl_SetIntObj(returnObj, fid);
356
357 return returnObj;
358 }
359
360 int filament_renumber(Bfilament* fil)
361 {
362 int nnode(0);
363 Bfilnode* fnode = NULL;
364
365 for ( fnode = fil->node; fnode; fnode = fnode->next ) fnode->id = ++nnode;
366
367 return nnode;
368 }
369
370 Tcl_Obj* filament_extract(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[])
371 {
372 Tcl_Obj* returnObj = Tcl_NewObj();
373
374 if ( !p ) return returnObj;
375
376 Bstring filename, filpath;
377 int nfil(0), split(0), axis(0);
378 double filament_width(10);
379
380 if ( objc > 4 ) filename = Tcl_GetStringFromObj(objv[4], NULL);
381 if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &filament_width);
382 if ( objc > 6 ) Tcl_GetIntFromObj(NULL, objv[6], &axis); /* Axis selection: x=1, y=2, z=3 */
383 if ( objc > 7 ) Tcl_GetIntFromObj(NULL, objv[7], &split); /* Flag to split particles into individual images */
384 if ( objc > 8 ) filpath = Tcl_GetStringFromObj(objv[8], NULL);
385
386 // cout << filename << "\t" << filament_width << "\t" << axis << "\t" << split << endl;
387
388 if ( filename.length() ) {
389 if ( mg ) {
390 if ( split ) {
391 filament_setup_filenames(mg->fil, filename, filpath);
392 mg->ffil = 0;
393 } else {
394 if ( filpath.length() ) {
395 if ( filename.contains("/") ) filename = filename.post_rev('/');
396 filename = filpath + "/" + filename;
397 }
398 mg->ffil = filename;
399 }
400 nfil = micrograph_extract_filaments(mg, p, filament_width, axis);
401 } else if ( rec ) {
402 if ( split ) {
403 filament_setup_filenames(rec->fil, filename, filpath);
404 rec->ffil = 0;
405 } else {
406 if ( filpath.length() ) {
407 if ( filename.contains("/") ) filename = filename.post_rev('/');
408 filename = filpath + "/" + filename;
409 }
410 rec->ffil = filename;
411 }
412 nfil = reconstruction_extract_filaments(rec, p, filament_width, axis);
413 }
414 }
415
416 Tcl_SetIntObj(returnObj, nfil);
417
418 return returnObj;
419 }
420
421 Tcl_Obj* filament_profile(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[])
422 {
423 Tcl_Obj* returnObj = Tcl_NewObj();
424
425 if ( !p ) return returnObj;
426
427 int fid(0), nid(0);
428 double filament_width(10);
429
430 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
431 if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
432 if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &filament_width);
433
434 long i, n(0), img_num(0);
435 double* profile = NULL;
436 char string[MAXLINELEN] = "";
437 Bfilament* fil = NULL;
438
439 if ( mg ) {
440 fil = mg->fil;
441 img_num = mg->img_num;
442 } else if ( rec ) fil = rec->fil;
443
444 if ( fil ) {
445 for ( ; fil && fil->id != fid; fil = fil->next ) ;
446 if ( fil ) {
447 profile = filament_profile(fil->node, p, img_num, nid, filament_width, n);
448 sprintf(string, "%ld", n);
449 Tcl_SetStringObj(returnObj, string, strlen(string));
450 for ( i=0; i<2*n; i++ ) {
451 sprintf(string, " %lf", profile[i]);
452 Tcl_AppendToObj(returnObj, string, strlen(string));
453 }
454 delete[] profile;
455 }
456 }
457
458 return returnObj;
459 }
460
461 Tcl_Obj* filament_center(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[])
462 {
463 Tcl_Obj* returnObj = Tcl_NewObj();
464
465 if ( !p ) return returnObj;
466
467 int filament_width(40);
468
469 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &filament_width);
470
471 long n(0), img_num(0);
472 double cc(0);
473 Bfilament* fil = NULL;
474
475 if ( mg ) {
476 fil = mg->fil;
477 img_num = mg->img_num;
478 } else if ( rec ) fil = rec->fil;
479
480 if ( fil ) {
481 cc += filaments_center(fil, p, img_num, filament_width);
482 n++;
483 }
484
485 if ( n ) cc /= n;
486
487 Tcl_SetDoubleObj(returnObj, cc);
488
489 return returnObj;
490 }
491
492 Tcl_Obj* filament_to_particles(Bproject* project, int objc, Tcl_Obj *CONST objv[])
493 {
494 Tcl_Obj* returnObj = Tcl_NewObj();
495
496 int nfil(0);
497 double filament_width(10), boxing_interval(0), rise(1e30), angle(0);
498
499 if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &filament_width);
500 if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &boxing_interval);
501 if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &rise); /* Helical rise */
502 if ( objc > 7 ) Tcl_GetDoubleFromObj(NULL, objv[7], &angle); /* Helical angle */
503
504 Vector3<int> box_size((int)filament_width, (int)filament_width, (int)filament_width);
505 nfil = project_filaments_to_particles(project, box_size, boxing_interval, rise, angle);
506
507 Tcl_SetIntObj(returnObj, nfil);
508
509 return returnObj;
510 }
511
512 /*
513 Filament node interface syntax:
514 Bmg node [item] [action] [params]
515 Actions:
516 count counts the number of filament nodes
517 ids returns the identifiers of filaments and nodes
518 location [fid] [nid] returns the location of a node
519 select [x] [y] [z] returns the filament and node identifiers at the given location
520 move [fid] [nid] [dx] [dy] [dz] updates a node location by the given vector
521 create [x] [y] [z] [fid] [nid] creates a new node at the given location and after the given node
522 delete [all|mg|rec|fid] [nid] deletes nodes
523 Parameters:
524 fid filament identifier
525 nid node identifier
526 x x location
527 y y location
528 z z location
529 dx change in x
530 dy change in y
531 dz change in z
532 */
533 Tcl_Obj* do_node(Bproject* project, int objc, Tcl_Obj *CONST objv[])
534 {
535 Tcl_Obj* returnObj = NULL;
536
537 Bstring item = Tcl_GetStringFromObj(objv[2], NULL);
538 if ( verbose & VERB_DEBUG )
539 cout << "DEBUG do_node: Item: " << item << " (" << item.length() << ")" << endl;
540
541 Bstring id = item.post(':');
542 id = id.remove(' ');
543 item = item.pre(':');
544
545 if ( !item.contains("Field") && !item.contains("Micrograph") && !item.contains("Reconstruction") ) {
546 cerr << "Error in do_node: Item " << item << " must be a micrograph or reconstruction!" << endl;
547 returnObj = Tcl_NewObj();
548 return returnObj;
549 }
550
551
552 double radius(10);
553 Bfield* field = NULL;
554 Bmicrograph* mg = NULL;
555 Breconstruction* rec = NULL;
556 Bfilament* fil = NULL;
557 Bstring delete_what;
558
559 if ( item.contains("Field") ) {
560 for ( field = project->field; field && field->id != id; field = field->next ) ;
561 } else if ( item.contains("Micrograph") ) {
562 for ( field = project->field; field; field = field->next ) {
563 for ( mg = field->mg; mg && mg->id != id; mg = mg->next ) ;
564 if ( mg ) break;
565 }
566 if ( mg ) {
567 fil = mg->fil;
568 radius = mg->fil_node_radius;
569 } else cerr << "Error in do_node: Micrograph " << id << " not found!" << endl;
570 } else if ( item.contains("Reconstruction") ) {
571 for ( rec = project->rec; rec && rec->id != id; rec = rec->next ) ;
572 if ( rec ) {
573 fil = rec->fil;
574 radius = rec->fil_node_radius;
575 } else cerr << "Error in do_node: Reconstruction " << id << " not found!" << endl;
576 }
577
578 Bstring action = Tcl_GetStringFromObj(objv[3], NULL);
579 if ( verbose & VERB_DEBUG )
580 cout << "DEBUG do_node: Action: " << action << " (" << action.length() << ")" << endl;
581
582 if ( action == "count" ) {
583 if ( mg ) returnObj = node_count(mg);
584 else if ( field ) returnObj = node_count(field);
585 else if ( rec ) returnObj = node_count(rec);
586 else returnObj = node_count(project);
587 } else if ( action == "ids" ) {
588 if ( fil ) returnObj = node_ids(fil);
589 } else if ( action == "location" ) {
590 if ( fil ) returnObj = node_location(fil, objc, objv);
591 } else if ( action == "select" ) {
592 if ( fil ) returnObj = node_select(fil, radius, objc, objv);
593 } else if ( action == "move" ) {
594 if ( fil ) returnObj = node_move(fil, objc, objv);
595 } else if ( action == "create" ) {
596 if ( mg ) returnObj = node_create(&mg->fil, objc, objv);
597 else if ( rec ) returnObj = node_create(&rec->fil, objc, objv);
598 } else if ( action == "delete" ) {
599 if ( objc > 4 ) delete_what = Tcl_GetStringFromObj(objv[4], NULL);
600 if ( delete_what == "all" || delete_what == "mg" || delete_what == "rec" )
601 returnObj = node_delete(project, objc, objv);
602 else if ( mg ) returnObj = node_delete(mg, objc, objv);
603 else if ( rec ) returnObj = node_delete(rec, objc, objv);
604 else returnObj = node_delete(project, objc, objv);
605 } else {
606 cerr << "Error: Action " << action << " not recognized!" << endl;
607 }
608
609 if ( !returnObj ) {
610 returnObj = Tcl_NewObj(); // Return object should be empty in some cases
611 // Tcl_SetIntObj(returnObj, 0);
612 }
613
614 return returnObj;
615 }
616
617 Tcl_Obj* node_count(Bproject* project)
618 {
619 Tcl_Obj* returnObj = Tcl_NewObj();
620
621 int nnode(0);
622 Bfield* field = project->field;
623 Bmicrograph* mg = NULL;
624 Breconstruction* rec;
625 Bfilament* fil = NULL;
626
627 if ( project->select < 1 ) {
628 for ( field = project->field; field; field = field->next )
629 for ( mg = field->mg; mg; mg = mg->next )
630 for ( fil = mg->fil; fil; fil = fil->next )
631 if ( fil->node ) nnode += count_list((char *) fil->node);
632 } else {
633 for ( rec = project->rec; rec; rec = rec->next )
634 for ( fil = rec->fil; fil; fil = fil->next )
635 if ( fil->node ) nnode += count_list((char *) fil->node);
636 }
637
638 Tcl_SetIntObj(returnObj, nnode);
639
640 return returnObj;
641 }
642
643 Tcl_Obj* node_count(Bfield* field)
644 {
645 Tcl_Obj* returnObj = Tcl_NewObj();
646
647 int nnode(0);
648 Bmicrograph* mg = NULL;
649 Bfilament* fil = NULL;
650
651 if ( field )
652 for ( mg = field->mg; mg; mg = mg->next )
653 for ( fil = mg->fil; fil; fil = fil->next )
654 if ( fil->node ) nnode += count_list((char *) fil->node);
655
656 Tcl_SetIntObj(returnObj, nnode);
657
658 return returnObj;
659 }
660
661 Tcl_Obj* node_count(Bmicrograph* mg)
662 {
663 Tcl_Obj* returnObj = Tcl_NewObj();
664
665 int nnode(0);
666 Bfilament* fil = NULL;
667
668 if ( mg && mg->fil )
669 for ( fil = mg->fil; fil; fil = fil->next )
670 if ( fil->node ) nnode += count_list((char *) fil->node);
671
672 Tcl_SetIntObj(returnObj, nnode);
673
674 return returnObj;
675 }
676
677 Tcl_Obj* node_count(Breconstruction* rec)
678 {
679 Tcl_Obj* returnObj = Tcl_NewObj();
680
681 int nnode(0);
682 Bfilament* fil = NULL;
683
684 if ( rec && rec->fil )
685 for ( fil = rec->fil; fil; fil = fil->next )
686 if ( fil->node ) nnode += count_list((char *) fil->node);
687
688 Tcl_SetIntObj(returnObj, nnode);
689
690 return returnObj;
691 }
692
693 Tcl_Obj* node_ids(Bfilament* fillist)
694 {
695 Tcl_Obj* returnObj = Tcl_NewObj();
696
697 char string[MAXLINELEN] = "";
698 Bfilament* fil = NULL;
699 Bfilnode* fnode = NULL;
700
701 if ( fillist ) {
702 for ( fil = fillist; fil; fil = fil->next ) {
703 for ( fnode = fil->node; fnode; fnode = fnode->next ) {
704 sprintf(string, " %d %d", fil->id, fnode->id);
705 Tcl_AppendToObj(returnObj, string, strlen(string));
706 }
707 }
708 }
709
710 return returnObj;
711 }
712
713 Tcl_Obj* node_location(Bfilament* fillist, int objc, Tcl_Obj *CONST objv[])
714 {
715 Tcl_Obj* returnObj = Tcl_NewObj();
716
717 int fid(0), nid(0);
718 char string[MAXLINELEN] = "";
719 Bfilament* fil = NULL;
720 Bfilnode* fnode = NULL;
721
722 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
723 if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
724
725 if ( fillist ) {
726 for ( fil = fillist; fil && fil->id != fid; fil = fil->next ) ;
727 if ( fil )
728 for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next ) ;
729 if ( fnode ) {
730 sprintf(string, "%f %f %f", fnode->loc[0], fnode->loc[1], fnode->loc[2]);
731 Tcl_SetStringObj(returnObj, string, strlen(string));
732 }
733 }
734
735 return returnObj;
736 }
737
738 Tcl_Obj* node_select(Bfilament* fillist, double radius, int objc, Tcl_Obj *CONST objv[])
739 {
740 Tcl_Obj* returnObj = Tcl_NewObj();
741
742 int fid(0), nid(0);
743 double x(0), y(0), z(0);
744
745 if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &x);
746 if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &y);
747 if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &z);
748
749 double d, dmin;
750 char string[64] = "";
751 Vector3<float> loc(x, y, z);
752 Bfilament* fil = NULL;
753 Bfilnode* fnode = NULL;
754
755 // cout << "Selection coordinates: " << loc << endl;
756 if ( fillist ) {
757 dmin = 2*radius;
758 for ( fil = fillist; fil; fil = fil->next ) {
759 for ( fnode = fil->node; fnode; fnode = fnode->next ) {
760 d = loc.distance(fnode->loc);
761 if ( dmin > d ) {
762 dmin = d;
763 fid = fil->id;
764 nid = fnode->id;
765 }
766 }
767 }
768 if ( dmin <= radius ) {
769 sprintf(string, "%d %d", fid, nid);
770 Tcl_SetStringObj(returnObj, string, strlen(string));
771 }
772 }
773
774 return returnObj;
775 }
776
777 Tcl_Obj* node_move(Bfilament* fillist, int objc, Tcl_Obj *CONST objv[])
778 {
779 Tcl_Obj* returnObj = Tcl_NewObj();
780
781 int fid(0), nid(0);
782 double dx(0), dy(0), dz(0);
783
784 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
785 if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
786 if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &dx);
787 if ( objc > 7 ) Tcl_GetDoubleFromObj(NULL, objv[7], &dy);
788 if ( objc > 8 ) Tcl_GetDoubleFromObj(NULL, objv[8], &dz);
789
790 Vector3<float> d = Vector3<float>(dx, dy, dz);
791 char string[64] = "";
792 Bfilament* fil = NULL;
793 Bfilnode* fnode = NULL;
794
795 if ( fillist ) {
796 for ( fil = fillist; fil && fil->id != fid; fil = fil->next ) ;
797 if ( fil )
798 for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next ) ;
799 if ( fnode ) {
800 fnode->loc += d;
801 sprintf(string, "%d %d", fid, nid);
802 Tcl_SetStringObj(returnObj, string, strlen(string));
803 }
804 }
805
806 return returnObj;
807 }
808
809 Tcl_Obj* node_create(Bfilament** fillist, int objc, Tcl_Obj *CONST objv[])
810 {
811 Tcl_Obj* returnObj = Tcl_NewObj();
812
813 int fid(0), nid(0), fid_max(0), nid_max(0);
814 double x(0), y(0), z(0);
815
816 if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &x);
817 if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &y);
818 if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &z);
819 if ( objc > 7 ) Tcl_GetIntFromObj(NULL, objv[7], &fid); // Filament
820 if ( objc > 8 ) Tcl_GetIntFromObj(NULL, objv[8], &nid); // Node
821
822 char string[64] = "";
823 Vector3<float> loc(x, y, z);
824 Bfilament* fil = NULL;
825 Bfilnode* fnode = NULL;
826 Bfilnode* fnode2 = NULL;
827
828 if ( !(*fillist) ) {
829 fid = nid = 1;
830 fil = filament_add(fillist, fid);
831 }
832
833 for ( fil = *fillist; fil && fil->id != fid; fil = fil->next )
834 if ( fid_max < fil->id ) fid_max = fil->id;
835
836 if ( !fil ) {
837 fil = filament_add(fillist, ++fid_max);
838 nid = 1;
839 }
840
841 for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next )
842 if ( nid_max < nid ) nid_max = nid;
843
844 if ( !fnode ) {
845 nid = ++nid_max;
846 fnode = filament_node_add(&fil->node, nid);
847 } else {
848 fnode2 = fnode->next;
849 fnode->next = NULL;
850 fnode = filament_node_add(&fnode, ++nid);
851 fnode->next = fnode2;
852 for ( ; fnode2; fnode2 = fnode2->next ) fnode2->id = ++nid;
853 }
854
855 fnode->loc = loc;
856
857 sprintf(string, "%d %d", fil->id, fnode->id);
858 Tcl_SetStringObj(returnObj, string, strlen(string));
859
860 return returnObj;
861 }
862
863 Tcl_Obj* node_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[])
864 {
865 Tcl_Obj* returnObj = Tcl_NewObj();
866
867 Bstring action = Tcl_GetStringFromObj(objv[4], NULL);
868
869 Bfield* field = project->field;
870 Bmicrograph* mg = NULL;
871 Breconstruction* rec;
872
873 if ( action == "all" || action == "mg" ) {
874 for ( field = project->field; field; field = field->next ) {
875 for ( mg = field->mg; mg; mg = mg->next ) {
876 filament_kill(mg->fil);
877 mg->fil = NULL;
878 }
879 }
880 }
881
882 if ( action == "all" || action == "rec" ) {
883 for ( rec = project->rec; rec; rec = rec->next ) {
884 filament_kill(rec->fil);
885 rec->fil = NULL;
886 }
887 }
888
889 // cout << "Deleting all" << endl;
890
891 return returnObj;
892 }
893
894 Tcl_Obj* node_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[])
895 {
896 Tcl_Obj* returnObj = Tcl_NewObj();
897
898 int fid(0), nid(0);
899 char string[64] = "";
900 Bfilament* fil = NULL;
901 Bfilnode* fnode = NULL;
902
903 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
904 if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
905
906 if ( mg ) {
907 for ( fil = mg->fil; fil && fil->id != fid; fil = fil->next ) ;
908 if ( fil ) {
909 for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next ) ;
910 if ( fnode )
911 remove_item((char **)&fil->node, (char *)fnode, sizeof(Bfilnode));
912 filament_renumber(fil);
913 if ( !fil->node )
914 remove_item((char **)&mg->fil, (char *)fil, sizeof(Bfilament));
915 }
916 }
917
918 sprintf(string, "%d %d", fid, nid);
919 Tcl_SetStringObj(returnObj, string, strlen(string));
920
921 return returnObj;
922 }
923
924
925 Tcl_Obj* node_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[])
926 {
927 Tcl_Obj* returnObj = Tcl_NewObj();
928
929 int fid(0), nid(0);
930 char string[64] = "";
931 Bfilament* fil = NULL;
932 Bfilnode* fnode = NULL;
933
934 if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
935 if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
936
937 if ( rec ) {
938 for ( fil = rec->fil; fil && fil->id != fid; fil = fil->next ) ;
939 if ( fil ) {
940 for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next ) ;
941 if ( fnode )
942 remove_item((char **)&fil->node, (char *)fnode, sizeof(Bfilnode));
943 filament_renumber(fil);
944 if ( !fil->node )
945 remove_item((char **)&rec->fil, (char *)fil, sizeof(Bfilament));
946 }
947 }
948
949 sprintf(string, "%d %d", fid, nid);
950 Tcl_SetStringObj(returnObj, string, strlen(string));
951
952 return returnObj;
953 }
954
955
956
Breconstruction::fil
Bfilament * fil
Definition: mg_processing.h:305
filament_node_add
Bfilnode * filament_node_add(Bfilnode **fnode, int pid)
Adds a filament node parameter structure to a linked list.
Definition: mg_processing.cpp:655
rwmg.h
Header file for reading and writing micrograph parameters.
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:394
imglist
Bimage * imglist
Definition: bshow.cpp:30
project_filaments_to_particles
int project_filaments_to_particles(Bproject *project, Vector3< long > box_size, double boxing_interval, double rise, double angle)
Converts filaments to sets of particles.
Definition: mg_extract.cpp:914
Bmicrograph::id
Bstring id
Definition: mg_processing.h:202
node_select
Tcl_Obj * node_select(Bfilament *fillist, double radius, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:738
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:211
node_move
Tcl_Obj * node_move(Bfilament *fillist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:777
Bfilnode
Definition: mg_processing.h:115
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Breconstruction
Definition: mg_processing.h:280
filaments_center
double filaments_center(Bfilament *fillist, Bimage *p, long img_num, int filament_width)
Centers filament nodes in a micrograph.
Definition: mg_helix.cpp:190
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
filament_delete
Tcl_Obj * filament_delete(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:242
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bstring::remove
Bstring remove(const char c)
Definition: Bstring.cpp:465
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
node_create
Tcl_Obj * node_create(Bfilament **fillist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:809
node_location
Tcl_Obj * node_location(Bfilament *fillist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:713
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
Bmicrograph::fil
Bfilament * fil
Definition: mg_processing.h:240
filament_profile
Tcl_Obj * filament_profile(Bmicrograph *mg, Breconstruction *rec, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:421
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Bmicrograph::ffil
Bstring ffil
Definition: mg_processing.h:208
Bfield::next
Bfield * next
Definition: mg_processing.h:258
Bfilament::id
int id
Definition: mg_processing.h:131
timer.h
Utilities for timing functions.
Bfilament::node
Bfilnode * node
Definition: mg_processing.h:132
Bimage
General image parameter class.
Definition: Bimage.h:163
Bfilament::next
Bfilament * next
Definition: mg_processing.h:129
filament_center
Tcl_Obj * filament_center(Bmicrograph *mg, Breconstruction *rec, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:461
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Bfield
Definition: mg_processing.h:257
linked_list.h
Header file for generalized linked list functions.
Bfilnode::id
int id
Definition: mg_processing.h:117
base
string base(string &filename)
Definition: string_util.cpp:28
filament_extract
Tcl_Obj * filament_extract(Bmicrograph *mg, Breconstruction *rec, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:370
Bmicrograph::fframe
Bstring fframe
Definition: mg_processing.h:206
Bfield::id
Bstring id
Definition: mg_processing.h:259
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:381
Bproject::field
Bfield * field
Definition: mg_processing.h:343
do_filament
Tcl_Obj * do_filament(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:76
filament_count
Tcl_Obj * filament_count(Bproject *project)
Definition: tcltk_bfil.cpp:171
node_count
Tcl_Obj * node_count(Bproject *project)
Definition: tcltk_bfil.cpp:617
Bmicrograph
Definition: mg_processing.h:200
mg_extract.h
Header file for functions to extract particles from micrographs.
mg_helix.h
Header file for functions to process helical data.
Breconstruction::fil_node_radius
double fil_node_radius
Definition: mg_processing.h:298
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
do_node
Tcl_Obj * do_node(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:533
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
filament_to_particles
Tcl_Obj * filament_to_particles(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:492
Bimage::next
Bimage * next
Definition: Bimage.h:165
Bstring::base
Bstring base()
Definition: Bstring.cpp:508
Bstring
Definition: Bstring.h:29
Bproject::select
int select
Definition: mg_processing.h:339
filament_kill
int filament_kill(Bfilament *fil)
Frees a list of filaments.
Definition: mg_processing.cpp:1347
reconstruction_extract_filaments
long reconstruction_extract_filaments(Breconstruction *rec, Bimage *p, double width, int axis)
Extracts filament images from a micrograph.
Definition: mg_extract.cpp:809
filament_add
Bfilament * filament_add(Bfilament **fil, int pid)
Adds a filament parameter structure to a linked list.
Definition: mg_processing.cpp:625
Breconstruction::ffil
Bstring ffil
Definition: mg_processing.h:289
Vector3< int >
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:205
Bmicrograph::fil_node_radius
double fil_node_radius
Definition: mg_processing.h:232
Bfilnode::next
Bfilnode * next
Definition: mg_processing.h:116
node_ids
Tcl_Obj * node_ids(Bfilament *fillist)
Definition: tcltk_bfil.cpp:693
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:287
node_delete
Tcl_Obj * node_delete(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:863
filament_setup_filenames
int filament_setup_filenames(Bfilament *fil, Bstring filename, Bstring filpath)
Sets up individual filament file names for extraction.
Definition: mg_extract.cpp:85
verbose
int verbose
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
Bfilnode::loc
Vector3< double > loc
Definition: mg_processing.h:118
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:281
Bproject
Definition: mg_processing.h:333
micrograph_extract_filaments
long micrograph_extract_filaments(Bmicrograph *mg, Bimage *p, double width, int axis)
Extracts filament images from a micrograph.
Definition: mg_extract.cpp:783
Breconstruction::id
Bstring id
Definition: mg_processing.h:282
Bfilament
Definition: mg_processing.h:128
tcltk_bfil.h
filament_renumber
int filament_renumber(Bfilament *fil)
Definition: tcltk_bfil.cpp:360
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201