| Bsoft 2.0.5 Bernard's software package

bsurf.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: bsurf [options] input.tif output.map",
21 "--------------------------------------------",
22 "Calculates a 3D surface from an AFM image.",
23 " ",
24 "Actions:",
25 "-expand 10 Surface expansion: value = new z dimension.",
26 "-threshold 1.6,1 Calculate a 2D height image using this threshold",
27 " and direction: 0=bottom up, 1=top down.",
28 "-invert Invert inside & outside (default not).",
29 " ",
30 "Parameters:",
31 "-verbose 7 Verbosity of output.",
32 "-datatype u Force writing of a new data type.",
33 "-positions 2.5,6.3 Z positions of surface minimum and maximum (angstrom).",
34 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
35 "-sampling 1.5,1.5,1.5 Dimension scaling (default 1,1,1 A/pixel).",
36 "-resolution 0.5,2.3 Upper & lower sigma bounds (default 3.236,3.236).",
37 "-Density 0.74 Density within the surface (default 0.81 Da/A3).",
38 " ",
39 "Input:",
40 "-Stdev sd.map Standard deviation map (default no map).",
41 " ",
42 NULL
43 };
44
45 int main(int argc, char **argv)
46 {
47 /* Initialize variables */
48 DataType nudatatype(Unknown_Type); // Conversion to new type
49 Vector3<double> origin; // New image origin
50 int set_origin(0); // Flag to set origin
51 Vector3<double> sam; // Pixel size
52 long nz(1); // New z-dimension set to preclude expansion
53 double threshold(-1e37); // Threshold for height image
54 int dir(0); // Direction for height image
55 double density(0.74); // Density within the surface
56 double resmin(1+sqrt(5.0)); // Resolution minimum = 2 phi = 3.236
57 double resmax(1+sqrt(5.0)); // Resolution maximum = 2 phi = 3.236
58 double posmin(0), posmax(0); // No min and max for surface expansion
59 int setinvert(0); // Don't invert inside-outside
60 Bstring sdfile; // Standard deviation file name
61
62 int optind;
63 Boption* option = get_option_list(use, argc, argv, optind);
64 Boption* curropt;
65 for ( curropt = option; curropt; curropt = curropt->next ) {
66 if ( curropt->tag == "expand" )
67 if ( curropt->values(nz, dir) < 1 )
68 cerr << "-expand: A z dimension must be specified!" << endl;
69 if ( curropt->tag == "threshold" )
70 if ( ( threshold = curropt->value.real() ) < -1e36 )
71 cerr << "-threshold: A threshold must be specified!" << endl;
72 if ( curropt->tag == "invert" )
73 setinvert = 1;
74 if ( curropt->tag == "datatype" )
75 nudatatype = curropt->datatype();
76 if ( curropt->tag == "origin" ) {
77 if ( curropt->value[0] == 'c' ) {
78 set_origin = 2;
79 } else {
80 origin = curropt->origin();
81 set_origin = 1;
82 }
83 }
84 if ( curropt->tag == "sampling" )
85 sam = curropt->sampling();
86 if ( curropt->tag == "positions" )
87 if ( curropt->values(posmin, posmax) < 2 )
88 cerr << "-positions: Minimum and maximum positions must be specified!" << endl;
89 if ( curropt->tag == "resolution" ) {
90 if ( curropt->values(resmin, resmax) < 1 )
91 cerr << "-resolution: A resolution must be specified!" << endl;
92 if ( resmax < resmin ) resmax = resmin;
93 }
94 if ( curropt->tag == "Density" ) {
95 if ( ( density = curropt->value.real() ) < 0.001 )
96 cerr << "-Density: A density must be specified!" << endl;
97 if ( density < 1e-10 ) density = 0.74;
98 }
99 if ( curropt->tag == "Stdev" )
100 sdfile = curropt->filename();
101 }
102 option_kill(option);
103
104 double ti = timer_start();
105
106 // Read the input file if a file name is given
107 Bimage* p = read_img(argv[optind++], 1, -1);
108 Bimage* psd = NULL;
109 Bimage* pnu = NULL;
110
111 p->change_type(Float);
112 if ( sam.volume() ) p->sampling(sam);
113
114 // Expand the image to a 3D surface density
115 if ( nz > 1 ) {
116 p->show_minimum(posmin); // Positions in angstrom
117 p->show_maximum(posmax);
118
119 // Read the standard deviation file if given
120 if ( sdfile.length() ) {
121 psd = read_img(sdfile, 1, -1);
122
123 psd->show_minimum(0.5*resmin);
124 psd->show_maximum(0.5*resmax);
125
126 // Convert all data to floating point and check the size
127 psd->change_type(Float);
128 if ( p->size() != psd->size() ) {
129 cerr << "Error: Dimensions of the standard deviation file " << sdfile << " incorrect!" << endl;
130 bexit(-1);
131 }
132 }
133
134 // Calculate the surface
135 pnu = p->topograph_to_surface(psd, nz, density, resmin);
136 delete p;
137 p = pnu;
138 }
139
140 if ( threshold > -1e36 && p->sizeZ() > 1 ) {
141 pnu = p->surface_to_topograph(threshold, dir);
142 delete p;
143 p = pnu;
144 }
145
146 // Invert if requested
147 if ( setinvert ) p->invert();
148
149 if ( set_origin ) {
150 if ( set_origin == 2 ) p->origin(p->size()/2);
151 else p->origin(origin);
152 }
153
154 // Write an output file if a file name is given
155 if ( argc > optind && strspn(argv[optind],"-") != 1 ) {
156 p->change_type(nudatatype);
157 p->label(get_cmd_line(argc, argv));
158 write_img(argv[optind], p, 0);
159 }
160
161 delete p;
162 delete psd;
163
164 if ( verbose & VERB_TIME )
165 timer_report(ti);
166
167 bexit(0);
168 }
169
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bstring::real
double real()
Definition: Bstring.cpp:545
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3108
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bstring::length
long length() const
Definition: Bstring.cpp:263
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
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
timer.h
Utilities for timing functions.
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
main
int main(int argc, char **argv)
Definition: bsurf.cpp:45
Bimage::topograph_to_surface
Bimage * topograph_to_surface(Bimage *psd, long nz, double density, double resolution)
Converts a 2D AFM image to a 3D density map.
Definition: Bimage_topo.cpp:39
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
verbose
int verbose
Bimage::show_maximum
double show_maximum()
Definition: Bimage.h:559
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
use
const char * use[]
Definition: bsurf.cpp:18
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
Bimage::surface_to_topograph
Bimage * surface_to_topograph(double threshold, int dir=0)
Converts a 3D image to a 2D height image.
Definition: Bimage_topo.cpp:119
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bimage::show_minimum
double show_minimum()
Definition: Bimage.h:558
Vector3::volume
double volume()
Definition: Vector3.h:235
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563