Bsoft  2.1.0
Bernard's software package
Bimage_topo.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "linked_list.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
39 Bimage* Bimage::topograph_to_surface(Bimage* psd, long nz, double density, double resolution)
40 {
41  // Density within the surface
42  if ( density < 1e-10 ) density = 1;
43  double voxel_density(density*sampling(0).volume());
44  int local_psd(0);
45  long i, j, xx, yy, zz, slice_size(x*y);
46  double factor(GOLDEN);
47  double mn, mx, zdis, scale;
48 
49  if ( psd == NULL ) {
50  local_psd = 1;
51  psd = copy_header();
52  mn = mx = resolution*0.5;
53  psd->show_minimum(resolution*0.5);
54  psd->show_maximum(resolution*0.5);
55  scale = 1;
56  psd->data_alloc();
57  for ( i=0; i<slice_size; i++ )
58  psd->set(i, psd->show_minimum());
59  } else {
60  mn = psd->minimum();
61  mx = psd->maximum();
62  scale = (psd->show_maximum() - psd->show_minimum())/(mx - mn);
63  for ( i=0; i<slice_size; i++ )
64  psd->set(i, scale*(*psd)[i] + psd->show_minimum());
65  }
66 
67  Bimage* psurf = new Bimage(Float, TSimple, x, y, nz, 1);
68  psurf->sampling(sampling(0));
69 
70  if ( show_minimum() == 0 && show_maximum() == 0 ) {
71  show_minimum(resolution);
72  show_maximum((nz - 1)*image->sampling()[2] - resolution);
73  }
74 
75  double loz(show_minimum()/image->sampling()[2]);
76  scale = (show_maximum()-show_minimum())/(image->sampling()[2]*(max-min));
77  if ( show_minimum() > show_maximum() ) factor *= -1;
78 
79  if ( verbose & VERB_PROCESS ) {
80  cout << "Converting a 2D image into a 3D surface density" << endl;
81  cout << "New z dimension: " << nz << endl;
82  cout << "Dimension scale: " << sampling(0) << " A/pixel" << endl;
83  cout << "Density scale: " << scale << " A/(unit density)" << endl;
84  cout << "Z min & max: " << show_minimum() << " A " << show_maximum() << " A" << endl;
85  cout << "Z thickness: " << show_maximum() - show_minimum() << " A" << endl;
86  cout << "Resolution: " << resolution << " A" << endl;
87  if ( psd )
88  cout << "Sigma min & max: " << psd->show_minimum() << " " << psd->show_maximum() << " A" << endl;
89  cout << "Density within surface: " << density << " Da/A3" << endl << endl;
90  } else if ( verbose & VERB_LABEL )
91  cout << "Converting a 2D image into a 3D surface density" << endl << endl;
92 
93  for ( i=yy=0; yy<y; yy++ ) {
94  for ( xx=0; xx<x; xx++, i++ ) {
95  zdis = loz + scale*((*this)[i]-min);
96  for ( j=i, zz=0; zz<psurf->z; zz++, j+=slice_size )
97  psurf->set(j, voxel_density/(1+exp(factor*(zz-zdis)/(*psd)[i])));
98  }
99  }
100 
101  if ( local_psd ) delete psd;
102 
103  psurf->statistics();
104 
105  return psurf;
106 }
107 
119 Bimage* Bimage::surface_to_topograph(double threshold, int dir)
120 {
121  long i, j, xx, yy, zz, nn;
122 
123  Bimage* p = new Bimage(Float, TSimple, x, y, 1, n);
124 
125  if ( verbose & VERB_PROCESS ) {
126  cout << "Converting a 3D image into a 2D height image" << endl;
127  cout << "Threshold: " << threshold << endl << endl;
128  }
129 
130  for ( i=nn=0; nn<n; nn++ ) {
131  for ( zz=0; zz<z; zz++ ) {
132  for ( yy=0; yy<y; yy++ ) {
133  for ( xx=0; xx<x; xx++, i++ ) {
134  j = (nn*y+yy)*x+xx;
135  if ( !(*p)[j] ) {
136  if ( dir ) {
137  if ( (*this)[i] >= threshold ) p->set(j, zz);
138  } else {
139  if ( (*this)[i] <= threshold ) p->set(j, zz);
140  }
141  }
142  }
143  }
144  }
145  }
146 
147  return p;
148 }
149 
164 Bimage* Bimage::rotate_height(Matrix3 mat, Vector3<double> translate, double threshold)
165 {
166  long i, xx, yy, zz;
167  Vector3<double> old, nuvec;
169 
170  double radial_cutoff = x;
171  if ( radial_cutoff < 1 ) radial_cutoff = x;
172 // double rad_sq = radial_cutoff*radial_cutoff;
173 
174  Vector3<double> oldorigin(image->origin());
175 
176  Vector3<double> nuorigin = oldorigin + translate;
177 
178  Matrix3 matinv = mat.transpose();
179 
180  if ( verbose & VERB_FULL ) {
181  cout << "Rotating around the map center and calculating height:" << endl;
182  cout << "Rotation origin: " << oldorigin << endl;
183  cout << matinv << endl;
184  if ( translate[0] != 0 || translate[1] != 0 || translate[2] != 0 )
185  cout << "Translation: " << translate << endl;
186 // cout << "Radial cutoff: " << radial_cutoff << " pixels" << endl;
187  cout << "Threshold: " << threshold << endl;
188  cout << endl;
189  }
190 
191  Bimage* pheight = new Bimage(Float, TSimple, x, y, 1, 1);
192  pheight->sampling(u[0], u[1], 1);
193  pheight->origin(nuorigin[0], nuorigin[1], 0);
194 
195  double d, dz, bottom(u[2]*z);
196 
197  for ( i=yy=0; yy<y; yy++ ) {
198  nuvec[1] = yy - nuorigin[1];
199  for ( xx=0; xx<x; xx++, i++ ) {
200  nuvec[0] = xx - nuorigin[0];
201  for ( zz=z-1; zz; zz-- ) {
202  nuvec[2] = zz - nuorigin[2];
203  old = mat * nuvec + oldorigin;
204  d = interpolate(old);
205  if ( d > threshold ) {
206  dz = u[2]*zz;
207  if ( (*pheight)[i] <= 0 ) pheight->set(i, dz);
208  if ( bottom > dz ) bottom = dz;
209  }
210  }
211  }
212  }
213 
214  for ( i=0; i<x*y; ++i ) if ( (*pheight)[i] ) pheight->add(i, -bottom);
215 
216  pheight->statistics();
217 
218  return pheight;
219 }
220 
230 Bimage* Bimage::height(View* views, double threshold)
231 {
232  if ( !views ) {
233  error_show("Error in Bimage::height: No views defined!", __FILE__, __LINE__);
234  return NULL;
235  }
236 
238 
239  long i;
240  Vector3<double> translate;
242  u[2] = 1;
243 
244  long nviews = count_list((char *)views);
245  View* vlist = new View[nviews];
246 // vector<View> vlist(nviews);
247  View* v;
248 
249  for ( v=views, i=0; v; v = v->next, i++ ) vlist[i] = *v;
250 
251  if ( verbose & VERB_PROCESS ) {
252  cout << "Calculating height images:" << endl;
253  cout << "Number of images: " << nviews << endl;
254  cout << "Threshold: " << threshold << endl << endl;
255  }
256 
257  Bimage* pheight = new Bimage(Float, TSimple, x, y, 1, nviews);
258  pheight->file_name(file_name());
259  pheight->sampling(u);
260 
261 #ifdef HAVE_GCD
262  dispatch_apply(nviews, dispatch_get_global_queue(0, 0), ^(size_t i){
263 // if ( verbose & VERB_LABEL )
264 // cout << "Projection " << i+1 << ":" << endl;
265  Matrix3 mat = vlist[i].matrix();
266  Bimage* one_height = rotate_height(mat, translate, threshold);
267  one_height->shift_background(0);
268  pheight->replace(i, one_height);
269 // pheight->image[i].origin(one_height->image->origin());
270  pheight->origin(i, one_height->image->origin());
271  pheight->image[i].view(vlist[i]);
272  delete one_height;
273  });
274 #else
275 #pragma omp parallel for
276  for ( long i=0; i<nviews; i++ ) {
277 // if ( verbose & VERB_LABEL )
278 // cout << "Projection " << i+1 << ":" << endl;
279  Matrix3 mat = vlist[i].matrix();
280  Bimage* one_height = rotate_height(mat, translate, threshold);
281  one_height->shift_background(0);
282  pheight->replace(i, one_height);
283 // pheight->image[i].origin(one_height->image->origin());
284  pheight->origin(i, one_height->image->origin());
285  pheight->image[i].view(vlist[i]);
286  delete one_height;
287  }
288 #endif
289 
290  delete[] vlist;
291 
292  pheight->statistics();
293 
294  return pheight;
295 }
Bimage::height
Bimage * height(View *views, double threshold=0)
Calculates a set of height images from a 3D density map.
Definition: Bimage_topo.cpp:230
View::next
View * next
Definition: View.h:28
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Vector3< double >
GOLDEN
#define GOLDEN
Definition: utilities.h:85
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
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
View
Definition: View.h:26
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::maximum
double maximum()
Definition: Bimage.h:542
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bimage::density
double density(long nn, Vector3< double > coord, double radius, double &sigma)
Calculates the density in a sphere around a coordinate in an image.
Definition: Bimage.cpp:3012
Matrix3
Definition: Matrix3.h:18
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::shift_background
int shift_background(double bkg)
Sets the background for each sub-image to a given value.
Definition: Bimage_background.cpp:351
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Bimage::file_name
string & file_name()
Definition: Bimage.h:246
Bimage
General image parameter class.
Definition: Bimage.h:177
volume
T volume(vector< T > v)
Definition: Vector3.h:547
Bimage::rotate_height
Bimage * rotate_height(Matrix3 mat, Vector3< double > translate, double threshold=0)
Rotates a 3D map and calculates the height along the z-axis.
Definition: Bimage_topo.cpp:164
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
Bimage.h
Header file for image class.
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
utilities.h
Header file for general utilities.
Bimage::interpolate
double interpolate(long cc, double xx, double yy=0, double zz=0, long nn=0, double fill=0) const
Interpolates using a given location.
Definition: Bimage.cpp:1414
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
verbose
int verbose
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
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
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
linked_list.h
Header file for generalized linked list functions.
Bimage::show_minimum
double show_minimum()
Definition: Bimage.h:565
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::sampling
vector< Vector3< double > > sampling()
Definition: Bimage.h:529
Bimage::minimum
double minimum()
Definition: Bimage.h:541
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
View::matrix
Matrix3 matrix()
Definition: View.h:153
Bimage::show_maximum
double show_maximum()
Definition: Bimage.h:566
Float
@ Float
Definition: utilities.h:161
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22