43 if ( density < 1e-10 ) density = 1;
45 double voxel_density = density*sampling.
volume();
47 unsigned long i, j, x, y, z, slice_size = p->
sizeX()*p->
sizeY();
49 double min, max, zdis, scale;
55 min = max = resolution*0.5;
60 for ( i=0; i<slice_size; i++ )
67 for ( i=0; i<slice_size; i++ )
82 cout <<
"Converting a 2D image into a 3D surface density" << endl;
83 cout <<
"New z dimension: " << p->
sizeZ() << endl;
84 cout <<
"Dimension scale: " << sampling <<
" A/pixel" << endl;
85 cout <<
"Density scale: " << scale <<
" A/(unit density)" << endl;
88 cout <<
"Resolution: " << resolution <<
" A" << endl;
91 cout <<
"Density within surface: " << density <<
" Da/A3" << endl << endl;
93 cout <<
"Converting a 2D image into a 3D surface density" << endl << endl;
95 for ( i=y=0; y<p->
sizeY(); y++ ) {
96 for ( x=0; x<p->
sizeX(); x++, i++ ) {
97 zdis = loz + scale*((*p)[i]-p->
minimum());
98 for ( j=i, z=0; z<p->
sizeZ(); z++, j+=slice_size )
99 dens[j] = voxel_density/(1+exp(factor*(z-zdis)/(*psd)[i]));
105 if ( local_psd )
delete psd;
127 unsigned long i, j, x, y, z, n;
132 cout <<
"Converting a 3D image into a 2D height image" << endl;
133 cout <<
"Threshold: " << threshold << endl << endl;
136 for ( i=n=0; n<p->
images(); n++ ) {
137 for ( z=0; z<p->
sizeZ(); z++ ) {
138 for ( y=0; y<p->
sizeY(); y++ ) {
139 for ( x=0; x<p->
sizeX(); x++, i++ ) {
141 if ( !(*ph)[j] && (*p)[i] >= threshold ) ph->
set(j, z);
168 unsigned long i, x, y;
169 double dx, dy, dis, dis2;
171 if ( size.
x() < 4*radius/sampling.
x() ) size[0] = (
int) (4*radius/sampling.
x());
172 if ( size.
y() < 4*radius/sampling.
y() ) size[1] = (
int) (4*radius/sampling.
y());
173 if ( size.
z() < 4*radius/sampling.
z() ) size[2] = (
int) (4*radius/sampling.
z());
176 cout <<
"Generating a 2D tip image" << endl;
177 cout <<
"Tip dimensions: " << size << endl;
178 cout <<
"Tip radius: " << radius <<
" A" << endl << endl;
180 cout <<
"Generating a 2D tip image" << endl;
187 radius /= sampling.
x();
188 double radcut = radius/sqrt(2.0);
189 double rad2 = radius*radius;
193 for ( i=y=0; y<p->
sizeY(); y++ ) {
195 for ( x=0; x<p->
sizeX(); x++, i++ ) {
197 dis2 = dx*dx + dy*dy;
200 p->
set(i, max - (2*radcut - sqrt(rad2 - dis2)));
202 p->
set(i, max - dis);
219 float* force,
double spring,
double modulus,
double thick)
221 unsigned long i, ti, di, x, y, z, tx, ty, tz, hiz;
222 long dx, dy, dz, zshift;
224 double fraction, overlap_volume(1);
228 double proportion = modulus/(spring*thick);
230 float* shift =
new float[p->
sizeZ()];
233 x = ii - y*p->
sizeX();
234 for ( z=0; z<p->
sizeZ(); z++ ) {
236 if ( overlap_volume > 1e-30 ) {
240 if ( hiz > p->
sizeZ() - z ) hiz = p->
sizeZ() - z;
241 for ( ti=ty=0; ty<ptip->
sizeY(); ty++ ) {
243 while ( dy < 0 ) dy += p->
sizeY();
245 for ( tx=0; tx<ptip->
sizeX(); tx++, ti++ ) {
247 while ( dx < 0 ) dx += p->
sizeX();
249 di = dy*p->
sizeX()+dx;
250 for ( tz=0; tz<hiz; tz++ ) {
252 overlap_volume += (*p)[dz*ss+di]*(*ptip)[tz*tss+ti];
257 shift[z] = overlap_volume*scale*proportion;
261 for ( z=0; z<p->
sizeZ(); z++ ) {
263 while ( zshift*p->
sampling().z() < shift[z+zshift] &&
264 zshift+z < p->
sizeZ() ) zshift++;
265 if ( zshift > 0 ) zshift--;
266 fraction = (zshift*p->
sampling().z() - shift[z+zshift])/
267 (zshift*p->
sampling().z() - shift[z+zshift] -
268 (zshift+1)*p->
sampling().z() + shift[z+zshift+1]);
269 if ( !isfinite(fraction) )
270 cout <<
"Problem:\t" << x <<
tab << y <<
tab << z <<
tab <<
272 shift[z+zshift] <<
tab << shift[z+zshift+1] << endl;
274 force[i] = spring*(zshift + fraction)*p->
sampling().z();
313 unsigned long i, ii, z;
318 cout <<
"Simulating an AFM experiment" << endl;
319 cout <<
"Spring constant: " << spring <<
" N/m" << endl;
320 cout <<
"Bulk modulus: " << modulus <<
" N/m2" << endl;
321 cout <<
"Specimen thickness: " << thick <<
" A" << endl << endl;
323 cout <<
"Simulating an AFM experiment" << endl << endl;
332 dispatch_apply(ss, dispatch_get_global_queue(0, 0), ^(
size_t i){
336 #pragma omp parallel for
337 for (
long i=0; i<ss; i++ )
342 for ( i=0; i<ss*p->
sizeZ(); i++ )
345 Bstring title(
"AFM simulation");
346 int ncol(3), nv(p->
sizeZ());
349 plot->page(0).title(title);
350 plot->page(0).columns(ncol);
351 for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
352 plot->page(0).column(0).label(
"Distance");
353 plot->page(0).column(0).axis(1);
354 plot->page(0).column(1).type(2);
355 plot->page(0).column(1).label(
"Deflection");
356 plot->page(0).column(1).axis(4);
357 plot->page(0).column(2).type(2);
358 plot->page(0).column(2).label(
"Force");
359 plot->page(0).column(2).axis(3);
364 for ( z=0; z<nv; z++ ) {
365 for ( i=0, ii=z*ss, avg_force=0; i<ss; i++, ii++ )
366 avg_force += force[ii];
369 (*plot)[z+nv] = avg_force/spring;
370 (*plot)[z+2*nv] = avg_force;
374 txt =
"Cantilever spring constant: " +
Bstring(spring/100,
"%lg N/m");
375 plot->page(0).add_text(txt);
376 txt =
"Bulk modulus: " +
Bstring(modulus*1e8,
"%lg N/m2");
377 plot->page(0).add_text(txt);
378 txt =
"Sample thickness: " +
Bstring(thick,
"%lg A");
379 plot->page(0).add_text(txt);
381 if (
verbose & VERB_PROCESS ) {
382 cout <<
"Distance(A)\tDeflection(A)\tForce(pN)" << endl;
383 for ( z=0; z<nv; z++ )
384 cout << setw(12) << (*plot)[z] <<
tab << setw(12)
385 << (*plot)[z+nv] <<
tab << (*plot)[z+2*nv] << endl;
402 unsigned long i, j, x, y, z, n;
404 double denom, scale, min, max;
406 n = (
unsigned long) (pf->
maximum()/force_step);
413 cout <<
"Converting an AFM force map into " << ph->
images() <<
" height images" << endl;
414 cout <<
"Force(pN)\tHeight_min(A)\tHeight_range(A)" << endl;
416 cout <<
"Converting an AFM force map into " << ph->
images() <<
" height images" << endl << endl;
418 for ( n=0; n<ph->
images(); n++ ) {
421 for ( y=0; y<ph->
sizeY(); y++ ) {
422 for ( x=0; x<ph->
sizeX(); x++ ) {
424 while ( z < pf->sizeZ() && (*pf)[(z*pf->
sizeY() + y)*pf->
sizeX() + x] > force_step*(n+1) )
426 denom = (*pf)[((z-1)*pf->
sizeY() + y)*pf->
sizeX() + x] -
432 (force_step*(n+1) - (*pf)[(z*pf->sizeY() + y)*pf->sizeX() + x])/denom);
433 if ( min > (*ph)[i] ) min = (*ph)[i];
434 if ( max < (*ph)[i] ) max = (*ph)[i];
439 cout << setw(12) << force_step*(n+1) <<
tab << setw(12) << min <<
tab << scale << endl;
440 for ( i=0, j=n*ss; i<ss; i++, j++ )
441 ph->
set(j, (*ph)[j] - min);
448 cout <<
"DEBUG afm_force_to_height: Done!" << endl << endl;
void change_type(char letter)
Get the data type indicated by a single letter code.
Bplot * afm_simulate(Bimage *ptip, Bimage *p, double spring, double modulus, double thick)
Simulates and AFM experiment.
Header file for general utilities.
CompoundType compound_type()
Vector3< size_t > size() const
size_t statistics()
Calculates the statistics for an image.
Utilities for timing functions.
General image parameter class.
unsigned char * data_alloc()
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Bimage * afm_generate_tip(Vector3< int > size, Vector3< float > sampling, double radius, double resolution)
Generates a rounded conical AFM tip.
void origin(double x, double y, double z)
void set(size_t j, double v)
Sets a single value at the given index.
Bimage * afm_force_to_height(Bimage *pf, double force_step)
Converts an AFM force map into a set of 2D height images at different constant force values...
Bimage * img_3D_to_height(Bimage *p, double threshold)
Converts a 3D image to a 2D height image.
int img_to_surface(Bimage *p, Bimage *psd, int nz, double density, double resolution)
Converts a 2D AFM image to a 3D density map.
int afm_simulate_at_xy(Bimage *ptip, Bimage *p, long ii, float *force, double spring, double modulus, double thick)
Header file for AFM functions.
ostream & tab(ostream &out)