Bsoft  2.1.0
Bernard's software package
bsymcomp.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 // Usage assistance
18 const char* use[] = {
19 " ",
20 "Usage: bsymcomp [options] input.img output.img",
21 "----------------------------------------------",
22 "Finds the best orientation that fits a symmetrized template.",
23 "The output is the rotated input image.",
24 " ",
25 "Parameters:",
26 "-verbose 1 Verbosity of output",
27 "-datatype u Force writing of a new data type.",
28 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; default from input file; a single value can be given).",
29 "-origin 110,50,44 Origin for rotations of the template (default taken from template image).",
30 "-angles 8.8,2.5,5.2 Step size for alpha, theta and phi, one value sets all (default 45 degrees).",
31 "-shift 0.5,3,-2.3 Shift to apply before symmetrization.",
32 "-symmetry C5 Point group symmetry.",
33 "-fill 0.02 Fill value (default image background).",
34 " ",
35 "Input:",
36 "-Template image.map Template with symmetry to search for.",
37 " ",
38 NULL
39 };
40 
41 
42 int main(int argc, char** argv)
43 {
44  // Initializing variables
45  DataType nudatatype(Unknown_Type); // Conversion to new type
46  Vector3<double> sam; // Units for the three axes (A/pixel)
47  Vector3<double> origin; // Template origin
48  int set_origin(0); // Flag to set origin
49  double alpha_step(M_PI_4); // Angular step size for alpha
50  double theta_step(M_PI_4); // Angular step size for theta
51  double phi_step(M_PI_4); // Angular step size for phi
52  Vector3<double> shift; // Shift to apply before symmetrization
53  int fill_type(FILL_BACKGROUND); // Use background
54  double fill(0); // Default fill is zero
55  Bsymmetry* sym = NULL; // Point group
56  Bstring template_file; // Symmmetric template
57 
58  int i, optind;
59  Boption* option = get_option_list(use, argc, argv, optind);
60  Boption* curropt;
61  for ( curropt = option; curropt; curropt = curropt->next ) {
62  if ( curropt->tag == "datatype" )
63  nudatatype = curropt->datatype();
64  if ( curropt->tag == "sampling" )
65  sam = curropt->scale();
66  if ( curropt->tag == "origin" ) {
67  if ( curropt->value[0] == 'c' ) {
68  set_origin = 2;
69  } else {
70  origin = curropt->origin();
71  set_origin = 1;
72  }
73  }
74  if ( curropt->tag == "angles" ) {
75  if ( ( i = curropt->values(alpha_step, theta_step, phi_step) ) < 1 )
76  cerr << "-angles: An angle step size must be specified" << endl;
77  else {
78  if ( i < 2 ) phi_step = theta_step = alpha_step;
79  else if ( i < 3 ) phi_step = theta_step;
80  alpha_step *= M_PI/180.0;
81  theta_step *= M_PI/180.0;
82  phi_step *= M_PI/180.0;
83  }
84  }
85  if ( curropt->tag == "shift" )
86  shift = curropt->vector3();
87  if ( curropt->tag == "symmetry" )
88  sym = curropt->symmetry();
89  if ( curropt->tag == "fill" )
90  fill = curropt->fill(fill_type);
91  if ( curropt->tag == "Template" )
92  template_file = curropt->filename();
93  }
94  option_kill(option);
95 
96  double ti = timer_start();
97 
98  // Read image file
99  Bimage* p = read_img(argv[optind++], 1, 0);
100 
101  if ( !p ) {
102  cerr << "Error: No input file read!" << endl;
103  bexit(-1);
104  }
105 
106  Bimage* ptemp = NULL;
107 
108  if ( template_file.length() )
109  ptemp = read_img(template_file, 1, 0);
110 
111  if ( !ptemp ) {
112  cerr << "Error: No template file read!" << endl;
113  bexit(-1);
114  }
115 
116  if ( nudatatype == Unknown_Type )
117  nudatatype = p->data_type(); // Preserve the old type
118 
119  if ( sam.volume() > 0 ) {
120  p->sampling(sam);
121  ptemp->sampling(sam);
122  }
123 
124  if ( set_origin ) {
125  p->origin(origin);
126  }
127  if ( set_origin ) {
128  if ( set_origin == 2 ) {
129  p->origin(p->size()/2);
130  ptemp->origin(ptemp->size()/2);
131  } else {
132  p->origin(origin);
133  ptemp->origin(origin);
134  }
135  }
136 
137  if ( fill_type == FILL_AVERAGE ) fill = p->average();
138  if ( fill_type == FILL_BACKGROUND ) {
139  if ( !p->background(long(0)) ) p->calculate_background();
140  fill = p->background(long(0));
141  }
142 
143  p->background(fill);
144 
145  Bimage* prot = p->find_symmetric_view(ptemp, sym,
146  phi_step, theta_step, alpha_step, shift);
147 
148  // Write an output file if a name is given
149  if ( optind < argc ) {
150  prot->change_type(nudatatype);
151  write_img(argv[optind], prot, 0);
152  }
153 
154  delete p;
155  delete ptemp;
156  delete prot;
157  kill_symmetry(sym);
158 
159  if ( verbose & VERB_TIME )
160  timer_report(ti);
161 
162  return 0;
163 }
164 
Boption
A class for a command line input option.
Definition: options.h:26
Boption::scale
Vector3< double > scale()
Definition: options.h:190
M_PI_4
#define M_PI_4
Definition: utilities.h:69
Bstring
Definition: Bstring.h:29
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
options.h
Header file for option handlers.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bsymmetry
Definition: symmetry.h:38
use
const char * use[]
Definition: bsymcomp.cpp:18
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:240
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Boption::vector3
Vector3< double > vector3()
Definition: options.h:148
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
Bstring::length
long length() const
Definition: Bstring.cpp:263
main
int main(int argc, char **argv)
Definition: bsymcomp.cpp:42
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::find_symmetric_view
Bimage * find_symmetric_view(Bimage *ptemp, Bsymmetry *sym, double phi_step, double theta_step, double alpha_step, Vector3< double > shift)
Finds the view that on symmetrizing fits best to a symmetric template.
Definition: Bimage_symmetry.cpp:1042
verbose
int verbose