Bsoft  2.1.0
Bernard's software package
Bimage_polar.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "simplex.h"
11 #include "utilities.h"
12 #include "Matrix.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
33 Bimage* Bimage::radial(long minrad, long maxrad, double rad_step, int wrap)
34 {
35  return radial(minrad, maxrad, rad_step, 1, 0, NULL, wrap);
36 }
37 
55 Bimage* Bimage::radial(long minrad, long maxrad, double rad_step, Bimage* pmask, int wrap)
56 {
57  return radial(minrad, maxrad, rad_step, 1, 0, pmask, wrap);
58 }
59 
79 Bimage* Bimage::radial(long minrad, long maxrad, double rad_step,
80  double ellipticity, double angle, Bimage* pmask, int wrap)
81 {
82  long i, j, nn, xx, yy, zz, ir, nrad(0);
83  double dx, dy, dz, x2, y2, z2, a, r, f, f1;
84 
85  if ( rad_step < 0.1 ) rad_step = 1;
86 
87  Vector3<long> h(size()/2);
89  Vector3<double> scale(1/rad_step, 1/rad_step, 1/rad_step);
90  if ( z < 2 ) scale[2] = 1;
91 
92  if ( maxrad < minrad ) swap(minrad, maxrad);
93  if ( maxrad < 1 ) maxrad = size().max()/(2*rad_step);
94  else if ( maxrad > size().max()/(2*rad_step) ) maxrad = size().max()/(2*rad_step);
95 
96  Bimage* prad = new Bimage(Float, TSimple, maxrad, 1, 1, n);
97  prad->sampling(rad_step*image->sampling()[0], 1, 1);
98 
99 // float* num = new float[maxrad];
100  vector<float> num(maxrad);
101 
102  if ( pmask )
103  if ( pmask->data_type() != UCharacter || pmask->minimum() != 0 || pmask->maximum() != 1 )
104  pmask->to_mask();
105 
106  if ( verbose & VERB_PROCESS ) {
107  cout << "Calculating a radial profile:" << endl;
108  cout << "Minimum and maximum pixels: " << minrad << " " << maxrad << endl;
109  cout << "Radial step size: " << rad_step << endl;
110  cout << "Minimum and maximum radius: " << minrad*image->sampling()[0] << " " << maxrad*image->sampling()[0] << " A" << endl;
111  cout << "Radial step size: " << rad_step*image->sampling()[0] << " A" << endl;
112  if ( pmask )
113  cout << "Mask file: " << pmask->file_name() << endl;
114  }
115 
116  if ( verbose & VERB_PROCESS )
117  cout << "Image\toriX\toriY\toriZ" << endl;
118  for ( i=nn=0; nn<n; nn++ ) {
119  for ( j=0; j<maxrad; j++ ) num[j] = 0;
120  if ( x > 1 ) origin[0] = image[nn].origin()[0];
121  if ( y > 1 ) origin[1] = image[nn].origin()[1];
122  if ( z > 1 ) origin[2] = image[nn].origin()[2];
123 // if ( (vector3_origin_to_shift(origin, size())).length() < x/10 ) wrap = 1;
124 // else wrap = 0;
125  if ( verbose & VERB_PROCESS )
126  cout << nn+1 << tab << origin << endl;
127  dz = -origin[2];
128  if ( wrap && dz < -h[2] ) dz += z;
129  for ( zz=0; zz<z; zz++, dz++ ) {
130  if ( wrap && dz > h[2] ) dz -= z;
131  z2 = dz * scale[2];
132  z2 *= z2;
133  dy = -origin[1];
134  if ( wrap && dy < -h[1] ) dy += y;
135  for ( yy=0; yy<y; yy++, dy++ ) {
136  if ( wrap && dy > h[1] ) dy -= y;
137  y2 = dy * scale[1];
138  y2 *= y2;
139  dx = -origin[0];
140  if ( wrap && dx < -h[0] ) dx += x;
141  for ( xx=0; xx<x; xx++, dx++, i++ ) if ( ( !pmask ) || ( pmask && (*pmask)[i] ) ) {
142  if ( wrap && dx > h[0] ) dx -= x;
143  x2 = dx * scale[0];
144  x2 *= x2;
145  r = sqrt(x2 + y2 + z2);
146  if ( fabs(ellipticity - 1) > 0.001 ) {
147  a = atan2(dy, dx) - angle;
148  r *= sqrt((ellipticity*ellipticity+1)/
149  (2*(ellipticity*ellipticity*cos(a)*cos(a)+
150  sin(a)*sin(a))));
151  }
152  ir = (long) r;
153  if ( ( ir >= minrad ) && ( ir < maxrad ) ) {
154  f = r - ir;
155  f1 = 1 - f;
156  j = ir + nn*maxrad; // Add for multiple images
157  num[ir] += f1;
158  prad->add(j, f1*(*this)[i]);
159  ir++;
160  if ( ir < maxrad ) {
161  j++;
162  num[ir] += f;
163  prad->add(j, f*(*this)[i]);
164  }
165  nrad++;
166  }
167  }
168  }
169  }
170  for ( j=0, ir = nn*maxrad; j<maxrad; j++, ir++ ) {
171  if ( num[j] ) {
172  prad->set(ir, (*prad)[ir]/num[j]);
173  }
174  }
175  prad->image[nn].origin(0.0,0.0,0.0);
176  }
177 
178 // delete[] num;
179 
180  if ( verbose & VERB_PROCESS )
181  cout << "Pixels in radial averages: " << nrad << endl;
182 
183  if ( verbose & VERB_DEBUG )
184  cout << "DEBUG Bimage:radial: prad size = " << prad->sizeX() << endl;
185 
186  prad->statistics();
187 
188  return prad;
189 }
190 
210  double rad_end, double rad_step,
211  double spherical_fraction, Bsymmetry* sym)
212 {
213  if ( rad_start < 0 ) rad_start = 0;
214  if ( rad_start >= z/2 ) rad_start = z/2;
215  if ( rad_end <= 0 ) rad_end = z/2;
216  if ( rad_end >= z/2 ) rad_end = z/2 - 1;
217  if ( rad_end < rad_start ) rad_end = rad_start;
218  if ( rad_step <= 0 ) rad_step = 1;
219 
220  if ( set_radial_distances(spherical_fraction, sym) < 0 )
221  return NULL;
222 
223  long i, j, nn, ir;
224  long nr = (long) ((rad_end - rad_start)/rad_step + 1);
225  double r, f, f1;
226 
227  if ( verbose ) {
228  cout << "Calculating radial profiles:" << endl;
229  cout << "Radial range: " << rad_start << " - " << rad_end << endl;
230  cout << "Radial step size: " << rad_step << endl;
231  cout << "Profile size: " << nr << endl << endl;
232  }
233 
234  Bimage* prad = new Bimage(Float, TSimple, nr, 1, 1, n);
235  prad->sampling(sampling(0)[0] * rad_step, 1, 1);
236 
237  vector<double> num(nr*n);
238 
239  for ( i=nn=0; nn<n; ++nn ) {
240  for ( j=0; j<image_size(); ++j, ++i ) {
241  r = ((*next)[i] - rad_start)/rad_step;
242  ir = (long)r;
243  f = r - ir;
244  f1 = 1 - f;
245  ir += nn*nr;
246  if ( ir < nr ) {
247  prad->add(ir, f1*(*this)[i]);
248  num[ir] += f1;
249  ir++;
250  if ( ir < nr ) {
251  prad->add(ir, f*(*this)[i]);
252  num[ir] += f;
253  }
254  }
255  }
256  }
257 
258  for ( i=0; i<prad->data_size(); ++i )
259  if ( num[i] ) prad->set(i, (*prad)[i]/num[i]);
260 
261  return prad;
262 }
263 
264 /*
265 @brief Least squares function for the radial_fit function.
266 @param &simp a simplex structure.
267 @return double the least squares R value.
268 
269  A radial profile is fitted to a reference profile using an iterative
270  simplex down-hill method. The equations solved are:
271  xref = x*m
272  yref(xref) = y(x)*a + b
273  where m is the dimension scaling (or magnification).
274  a is the amplitude scaling.
275  b is the amplitude shift.
276  The least squares function is:
277  sum(weight*(yref(xref) - y(x)*a - b)^2)
278  R = sqrt(---------------------------------------)
279  sum(weight)
280 **/
281 double radial_R(Bsimplex& simp)
282 {
283  vector<double>& y = simp.dependent_values();
284  vector<double>& yref = simp.independent_values();
285 
286  int i, iref, n(0);
287  double fraction, xref, df, R(0), sum(0), weight;
288 
289  for ( i=0; i<simp.points(); i++ ) {
290  xref = simp.parameter(0)*i;
291  iref = (int) xref;
292  if ( iref >= 0 && iref < simp.points() - 1 ) {
293  fraction = xref - iref;
294  df = (1.0 - fraction)*yref[iref] + fraction*yref[iref + 1]
295  - simp.parameter(1)*y[i] - simp.parameter(2);
296 // weight = i*(fabs(yref[iref]) + fabs(y[i]));
297  weight = i;
298 // weight = fabs(y[i]);
299  sum += weight;
300  R += df*df*weight;
301  n++;
302  }
303  }
304  if ( sum && n > 0.2*simp.points() ) R = sqrt(R/sum);
305  else R = 1e30; // Out of range
306 
307  if ( verbose & VERB_DEBUG )
308  cout << "DEBUG radial_R: " << simp.parameter(0) << " " <<
309  simp.parameter(1) << " " << simp.parameter(2) << " R=" << R << endl;
310 
311  return R;
312 }
313 
329 {
330  long i, npoint(x);
331  if ( x < pref->sizeX() ) npoint = pref->sizeX();
332 
333  // 3 parameters: Dimension scaling, amplitude scaling, amplitude shift
334  vector<double> ax(npoint), fx(npoint);
335  for ( i=0; i<npoint; i++ ) ax[i] = (*pref)[i];
336  for ( i=0; i<npoint; i++ ) fx[i] = (*this)[i];
337 
338  Bsimplex simp(1, 3, 0, npoint, ax, fx);
339 
340  // Dimension scaling estimated from the input units
341  simp.parameter(0, image->sampling()[0]/pref->sampling(0)[0]);
342 
343  // Amplitude scaling estimated from the standard deviations
344  simp.parameter(1, 1);
345  if ( std != 0 && pref->standard_deviation() != 0 )
346  simp.parameter(1, pref->standard_deviation()/std);
347  else if ( max - min > 0 && pref->maximum() - pref->minimum() > 0 )
348  simp.parameter(1, (pref->maximum() - pref->minimum())/(max - min));
349 
350  // Amplitude shift estimated from the averages
351  simp.parameter(2, pref->average() - avg*simp.parameter(1));
352 // if ( simp.parameter(2] < 1e-6 && max - min > 0 && pref->maximum() - pref->minimum() > 0 )
353 // simp.parameter(2, (pref->maximum() - pref->minimum() + max - min)/2.0;
354 
355  // Parameter limits
356  simp.limits(0, 0.8*simp.parameter(0), 1.2*simp.parameter(0));
357  simp.limits(1, 0.1*simp.parameter(1), 10*simp.parameter(1));
358  simp.limits(2, simp.parameter(2) - 10*pref->standard_deviation(), simp.parameter(2) + 10*pref->standard_deviation());
359 
360  if ( verbose & VERB_PROCESS ) {
361  cout << "Radial fit initial parameters:" << endl;
362  cout << "Dimension scale: " << simp.parameter(0) << endl;
363  cout << "Amplitude scale and shift: " << simp.parameter(1) << " " << simp.parameter(2) << endl;
364  }
365 
366  // Do the downhill simplex iterative fit
367  double R = simp.run(10000, 1e-12*pref->standard_deviation(), radial_R) / pref->standard_deviation();
368 
369  int idat;
370  double fraction, xdat, f;
371 
372  if ( verbose & VERB_PROCESS ) {
373  cout << "Radial fit final parameters:" << endl;
374  cout << "Dimension scale: " << simp.parameter(0) << endl;
375  cout << "Relative dimension scale: " << simp.parameter(0)*pref->sampling(0)[0]/image->sampling()[0] << endl;
376  cout << "Amplitude scale and shift: " << simp.parameter(1) << " " << simp.parameter(2) << endl;
377  cout << "R: " << R << endl << endl;
378  cout << "Radial averages:" << endl;
379  cout << "Radius(A) " << file_name() << " " << pref->file_name() << endl;
380  }
381 
382  if ( verbose & VERB_PROCESS ) {
383  for ( i=0; i<pref->sizeX(); i++ ) {
384  xdat = i/simp.parameter(0);
385  idat = (int) xdat;
386  if ( idat >= 0 && idat < x - 1 ) {
387  fraction = xdat - idat;
388  f = simp.parameter(1)*((1.0 - fraction)*(*this)[idat]
389  + fraction*(*this)[idat+1]) + simp.parameter(2);
390  } else {
391  f = 0;
392  }
393  cout << pref->sampling(0)[0]*i << " " << f << " " << (*pref)[i] << endl;
394  }
395  }
396 
397  double* result = new double[simp.parameters()];
398  for ( i=0; i<simp.parameters(); i++ ) result[i] = simp.parameter(i);
399 
400  return result;
401 }
402 
413 {
414  if ( nusize.volume() < 1 ) return NULL;
415 
416  Bimage* p = copy_header();
417  p->data_type(Float);
418  p->size(nusize);
419  if ( p->sizeX() < 2 ) origin[0] = 0;
420  if ( p->sizeY() < 2 ) origin[1] = 0;
421  if ( p->sizeZ() < 2 ) origin[2] = 0;
422  p->data_alloc();
423 
424  long i, nn, xx, yy, zz, ir, nexp(0);
425  double r, f, v;
426  Vector3<double> d;
427 
428  if ( verbose ) {
429  cout << "Calculating a full image from a radial profile:" << endl;
430  cout << "Size: " << p->size() << endl;
431  cout << "Origin: " << origin << endl;
432  }
433 
434  for ( nn=i=0; nn<n; nn++ ) {
435  for ( zz=0; zz<p->sizeZ(); zz++ ) {
436  d[2] = zz - origin[2];
437  for ( yy=0; yy<p->sizeY(); yy++ ) {
438  d[1] = yy - origin[1];
439  for ( xx=0; xx<p->sizeX(); xx++, i++ ) {
440  d[0] = xx - origin[0];
441  r = d.length();
442  ir = (long) r;
443  v = (*p)[i];
444  if ( ir < x ) {
445  f = 1 - r + ir;
446  v += f*(*this)[ir];
447  ir++;
448  f = 1 - f;
449  if ( ir < x ) v += f*(*this)[ir];
450  else v += f*background(nn);
451  nexp++;
452  } else {
453  v = background(nn);
454  }
455  p->set(i, v);
456  }
457  }
458  }
459  p->image[nn].origin(origin);
460  }
461 
462  if ( verbose )
463  cout << "Pixels generated: " << nexp << endl << endl;
464 
465  p->statistics();
466 
467  return p;
468 }
469 
470 
501 Bimage* Bimage::cartesian_to_spherical(long nannuli, long nphi, long ntheta)
502 {
503  long j, nn, ann, phi, theta;
504  double maxrad, scale;
505 
506  maxrad = size().max()/2;
507 
508  if ( nannuli < 1 ) nannuli = (long) maxrad;
509  if ( nphi < 1 ) nphi = 90;
510  if ( y < 2 ) nphi = 1;
511  if ( ntheta < 1 ) ntheta = 50;
512  if ( z < 2 ) ntheta = 1;
513 
514  double dannulus(maxrad*1.0/nannuli); // Step size in each dimension
515  double dphi(TWOPI/nphi);
516  double dtheta(M_PI/ntheta);
517  double cos_phi, sin_phi, cos_theta, sin_theta;
518  Vector3<double> coor;
520 
521  Bimage* psph = new Bimage(Float, TSimple, nannuli, nphi, ntheta, n);
522  psph->sampling(dannulus*u[0], dphi*180.0/M_PI, dtheta*180.0/M_PI);
523  u = psph->image->sampling();
524 
525  if ( verbose & VERB_PROCESS ) {
526  cout << "Spherical image size: " << psph->size() << endl;
527  cout << "Step sizes: " << u[0] << " A\t"
528  << u[1] << " degree\t" << u[2] << " degree" << endl;
529  }
530 
531  // Interpolate cartesian points to get spherical voxel values
532  if ( verbose & VERB_FULL )
533  cout << "Image\tOrigin" << endl;
534  for ( nn=0; nn<n; nn++ ) {
535  if ( verbose & VERB_FULL )
536  cout << nn+1 << tab << image[nn].origin() << endl;
537  for ( theta=0; theta<ntheta; theta++ ) {
538  cos_theta = dannulus*cos(dtheta*theta);
539  sin_theta = dannulus*sin(dtheta*theta);
540  for ( phi=0; phi<nphi; phi++ ) {
541  cos_phi = cos(dphi*phi);
542  sin_phi = -sin(dphi*phi);
543  for ( ann=0; ann<nannuli; ann++ ) {
544  j = psph->index(ann, phi, theta, nn);
545  scale = ann*sin_theta;
546  coor = Vector3<double>(scale*cos_phi, scale*sin_phi, ann*cos_theta);
547  coor += image[nn].origin();
548  psph->set(j, interpolate(coor, nn, 0));
549  }
550  }
551  }
552  psph->image[nn].origin(0.0, nphi/2, ntheta/2);
553  }
554 
555  if ( verbose & VERB_PROCESS ) cout << endl;
556 
557  if ( verbose & VERB_DEBUG )
558  cout << "DEBUG Bimage::cartesian_to_spherical: Calculating statistics" << endl;
559 
560  statistics();
561 
562  return psph;
563 }
564 
592 Bimage* Bimage::cartesian_to_cylindrical(long nannuli, long nphi, int flag)
593 {
594  long j, nn, zz, ann, phi;
595  double maxrad;
596 
597  maxrad = x/2; // Maximum radius taken from largest x and y dimension
598  if ( maxrad < y/2 ) maxrad = y/2;
599 
600  if ( nannuli < 1 ) nannuli = (long) maxrad;
601  if ( nphi < 1 ) nphi = 90;
602  if ( y < 2 ) nphi = 1;
603 
604  double dannulus(maxrad*1.0/nannuli); // Step size in each dimension
605  double dphi(TWOPI/nphi);
606  double cos_phi, sin_phi;
607  Vector3<double> coor;
609 
610  Bimage* pcyl;
611 
612  switch ( flag ) {
613  case 0:
614  pcyl = new Bimage(Float, TSimple, nannuli, nphi, z, n);
615  pcyl->sampling(dannulus*u[0], dphi*180.0/M_PI, u[2]);
616  break;
617  case 1:
618  pcyl = new Bimage(Float, TSimple, nphi, nannuli, z, n);
619  pcyl->sampling(dphi*180.0/M_PI, dannulus*u[0], u[2]);
620  break;
621  case 2:
622  pcyl = new Bimage(Float, TSimple, z, nannuli, nphi, n);
623  pcyl->sampling(u[2], dannulus*u[0], dphi*180.0/M_PI);
624  break;
625  default:
626  cerr << "Bimage::cartesian_to_cylindrical: flag = " << flag << " not supported!" << endl;
627  return NULL;
628  }
629 
630  if ( verbose & VERB_PROCESS ) {
631  cout << "Cylindrical image size: " << pcyl->size() << endl;
632  cout << "Step sizes: " << dannulus*image->sampling()[0] << " A\t"
633  << dphi*180.0/M_PI << " degree" << endl;
634  }
635 
636  // Interpolate cartesian points to get spherical voxel values
637  if ( verbose & VERB_FULL )
638  cout << "Image\tOrigin" << endl;
639  for ( nn=0; nn<n; nn++ ) {
640  if ( verbose & VERB_FULL )
641  cout << nn+1 << tab << image[nn].origin() << endl;
642  for ( zz=0; zz<z; zz++ ) {
643  coor[2] = zz;
644  for ( phi=0; phi<nphi; phi++ ) {
645  cos_phi = cos(dphi*phi);
646  sin_phi = sin(dphi*phi);
647  for ( ann=0; ann<nannuli; ann++ ) {
648  switch ( flag ) {
649  case 0: j = pcyl->index(ann, phi, zz, nn); break;
650  case 1: j = pcyl->index(phi, ann, zz, nn); break;
651  case 2: j = pcyl->index(zz, ann, phi, nn); break;
652  default: j=0;
653  }
654  coor[0] = ann*cos_phi + image[nn].origin()[0];
655  coor[1] = ann*sin_phi + image[nn].origin()[1];
656  pcyl->set(j, interpolate(coor, nn, 0));
657  }
658  }
659  }
660  if ( flag < 1 )
661 // pcyl->image[nn].origin(0, nphi/2, image[nn].origin()[2]);
662  pcyl->image[nn].origin(0.0, nphi/2, image[nn].origin()[2]);
663  else
664 // pcyl->image[nn].origin(nphi/2, 0, image[nn].origin()[2]);
665  pcyl->image[nn].origin(nphi/2, 0.0, image[nn].origin()[2]);
666  }
667 
668  if ( verbose & VERB_PROCESS ) cout << endl;
669 
670  if ( verbose & VERB_DEBUG )
671  cout << "DEBUG Bimage::cartesian_to_cylindrical: Calculating statistics" << endl;
672 
673  statistics();
674 
675  return pcyl;
676 }
677 
699 Bimage* Bimage::polar_transform(long nangles, long ann_min, long ann_max,
700  long dann, long zmin, long zmax, long zinc)
701 {
703 
704  if ( dann < 1 ) dann = 1;
705  if ( ann_min >= x/2 ) ann_min = x/2;
706  if ( ann_max < 1 || ann_max >= x/2 ) ann_max = x/2;
707  if ( ann_max >= y/2 ) ann_max = y/2;
708  if ( ann_min > ann_max ) swap(ann_min, ann_max);
709  if ( ann_min == ann_max ) ann_max = ann_min + dann;
710 
711  if ( zinc < 1 ) zinc = 1;
712  if ( zmin > zmax ) swap(zmin, zmax);
713  if ( zmin == zmax ) zmax = zmin + zinc;
714  if ( zmax >= z ) zmax = z - 1;
715 
716  long i, ann, ang, r, zz, iz;
717  long nannuli((ann_max - ann_min)/dann + 1);
718  long nz((zmax - zmin)/zinc + 1);
719  double a, cosa, sina;
720  double dang(M_PI*2.0/nangles);
721  Vector3<double> coor;
722 
723  Bimage* p = new Bimage(Float, TSimple, nangles, nannuli, nz, 1);
724 
725  if ( verbose & VERB_PROCESS ) {
726  cout << "Calculating a polar image from " << file_name() << endl;
727  cout << "Image size: " << nangles << " x " << nannuli << " x " << nz << endl;
728  cout << "Angular step size: " << dang*180/M_PI << " degrees" << endl;
729  cout << "Annular range: " << ann_min << " - " << ann_max << " pixels" << endl;
730  cout << "Annular step size: " << dann << " pixels" << endl;
731  cout << "Z range: " << zmin << " - " << zmax << " pixels" << endl;
732  cout << "Z step size: " << zinc << " pixels" << endl << endl;
733  } else if ( verbose & VERB_LABEL )
734  cout << "Calculating a polar image" << endl << endl;
735 
736  for ( i=0, zz=zmin; zz<=zmax; zz+=zinc ) {
737  for ( ann=ann_min; ann<=ann_max; ann+=dann ) {
738  for ( ang=0; ang<nangles; ang++, i++ ) {
739  a = dang*ang;
740  cosa = cos(a);
741  sina = sin(a);
742  for ( iz=zz; iz<zz+zinc; iz++ ) {
743  for ( r=ann; r<ann+dann; r++ ) {
744  coor[0] = r*cosa + image->origin()[0];
745  coor[1] = r*sina + image->origin()[1];
746  coor[2] = iz;
747  p->add(i, interpolate(coor));
748  }
749  }
750  }
751  }
752  }
753 
754  if ( verbose & VERB_DEBUG )
755  cout << "DEBUG Bimage::polar_transform: Calculating statistics" << endl;
756 
757  p->statistics();
758 
759  return p;
760 }
761 
774 Bimage* Bimage::polar_power_spectrum(double resolution, long num_angle)
775 {
776  check_resolution(resolution);
777 
778  long max_rad = (long) (x*image->sampling()[0]/resolution);
779  if ( max_rad > x/2 ) max_rad = x/2;
780  long max_ang = num_angle*max_rad;
781  Bimage* pps = new Bimage(Double, TSimple, max_ang, max_rad, 1, n);
782 
783  long j, nn, r, a;
784  double ang, xx, yy, fac;
785 
786  if ( verbose & VERB_FULL )
787  cout << "Calculating a radial power spectrum: " << max_rad << " annuli x " << max_ang << " angles" << endl;
788 
789  for ( nn=0; nn<n; nn++ ) {
790  for ( r=1; r<max_rad; r++ ) {
791  max_ang = num_angle*r;
792  fac = TWOPI/max_ang;
793  for ( a=0; a<max_ang; a++ ) {
794  j = (nn*pps->sizeY() + r)*pps->sizeX() + a;
795  ang = a*fac;
796  xx = r*cos(ang);
797  yy = r*sin(ang);
798  pps->set(j, interpolate_wrap(xx, yy, 0, nn));
799  }
800  }
801  }
802 
803  return pps;
804 }
805 
815 {
816  long i, is, isx, xx, yy, zz;
817  double sum;
818  Complex<float>* data = new Complex<float>[x];
819 
820 // cout << "Doing 1D transforms" << endl;
821 
822  for ( is=zz=0; zz<z; zz++ ) {
823  for ( yy=0; yy<y; yy++, is++ ) {
824  isx = is*x;
825  for ( xx=0, i=isx; xx<x; xx++, i++ )
826  data[xx] = Complex<float>((*this)[i], 0);
827  fftw(plan, data);
828  set(isx, 0); // Zero order
829  for ( xx=1, i=isx+1, sum=0; xx<x; xx++, i++ ) {
830  set(i, data[xx].power());
831  sum += (*this)[i];
832  }
833  if ( sum ) for ( xx=1, i=isx+1; xx<x; xx++, i++ )
834  set(i, (*this)[i]/sum);
835  }
836  }
837 
838  delete[] data;
839 
840  return 0;
841 }
842 
857 {
859 
860  long i, j, nn, xx, yy, zz;
861  long rz;
862  double dx2, dy2, dz, dz2, d2, oldz, fraction;
863 
864  float* shell = new float[datasize];
865 
866  if ( verbose & ( VERB_PROCESS | VERB_LABEL ) )
867  cout << "Converting to radial shells" << endl << endl;
868 
869  // Interpolate cartesian points to get spherical voxel values
870  if ( verbose & VERB_FULL )
871  cout << "Image\tOrigin" << endl;
872  for ( j=nn=0; nn<n; nn++ ) {
873  if ( verbose & VERB_FULL )
874  cout << nn+1 << tab << image[nn].origin() << endl;
875  for ( zz=0; zz<z; zz++ ) {
876  dz = zz - image[nn].origin()[2];
877  dz2 = dz*dz;
878  for ( yy=0; yy<y; yy++ ) {
879  dy2 = yy - image[nn].origin()[1];
880  dy2 *= dy2;
881  for ( xx=0; xx<x; xx++, j++ ) {
882  dx2 = xx - image[nn].origin()[0];
883  dx2 *= dx2;
884  d2 = dz2 - dy2 - dx2;
885  if ( d2 >= 0 ) {
886  if ( dz < 0 ) oldz = image[nn].origin()[2] - sqrt(d2);
887  else oldz = image[nn].origin()[2] + sqrt(d2);
888  rz = (long) oldz;
889  if ( rz >= 0 && rz < z ) {
890  fraction = 1 + rz - oldz;
891  i = index(0,xx,yy,rz,nn);
892  shell[j] = fraction*(*this)[i];
893  rz++;
894  if ( rz < z ) {
895  fraction = 1 - fraction;
896  i += y*x;
897  shell[j] += fraction*(*this)[i];
898  }
899  }
900  }
901  }
902  }
903  }
904  }
905 
906  if ( verbose & VERB_FULL ) cout << "" << endl;
907 
908  data_assign((unsigned char *) shell);
909 
910  statistics();
911 
912  return 0;
913 }
914 
929 {
931 
932  long i, j, nn, xx, yy, zz;
933  long ry;
934  double dx, dy, dx2, dy2, d2, oldy, fraction;
935 
936  float* shell = new float[datasize];
937 
938  if ( verbose & ( VERB_PROCESS | VERB_LABEL ) )
939  cout << "Converting to cylindrical shells" << endl << endl;
940 
941  // Interpolate cartesian points to get cylindrical voxel values
942  if ( verbose & VERB_FULL )
943  cout << "Image\tOrigin" << endl;
944  for ( j=nn=0; nn<n; nn++ ) {
945  if ( verbose & VERB_FULL )
946  cout << nn+1 << tab << image[nn].origin() << endl;
947  for ( zz=0; zz<z; zz++ ) {
948 // dz = zz - image[nn].origin()[2];
949 // dz2 = dz*dz;
950  for ( yy=0; yy<y; yy++ ) {
951  dy = yy - image[nn].origin()[1];
952  dy2 = dy*dy;
953  for ( xx=0; xx<x; xx++, j++ ) {
954  dx = xx - image[nn].origin()[0];
955  dx2 = dx*dx;
956  d2 = dy2 - dx2;
957  if ( dy2 >= dx2 ) {
958  if ( dy < 0 ) oldy = image[nn].origin()[1] - sqrt(d2);
959  else oldy = image[nn].origin()[1] + sqrt(d2);
960  ry = (long) oldy;
961  if ( ry >= 0 && ry < y ) {
962  fraction = 1 + ry - oldy;
963  i = index(0,xx,ry,zz,nn);
964  shell[j] = fraction*(*this)[i];
965  ry++;
966  if ( ry < y ) {
967  fraction = 1 - fraction;
968  i += x;
969  shell[j] += fraction*(*this)[i];
970  }
971  }
972  }
973  }
974  }
975  }
976  }
977 
978  if ( verbose & VERB_FULL ) cout << endl;
979 
980  data_assign((unsigned char *) shell);
981 
982  statistics();
983 
984  return 0;
985 }
986 
998 {
999  double r, rmax(0);
1000  Vector3<double> v(coor);
1001  v.normalize();
1002 
1003  for ( auto it = vs.begin(); it != vs.end(); ++it ) {
1004  r = v.scalar(*it);
1005  if ( rmax < r ) rmax = r;
1006  }
1007 
1008  return rmax;
1009 }
1010 
1021 int Bimage::set_radial_distances(double spherical_fraction, Bsymmetry* sym)
1022 {
1023  change_type(Float);
1024 
1025  long i, xx, yy, zz, nn;
1026  double r, rf;
1027  Vector3<double> coor;
1028 
1029  vector<Vector3<double>> vs = symmetry_get_axes(sym);
1030  if ( vs.size() < 1 ) return -1;
1031 
1032  if ( !sym || vs.size() < 2 ) spherical_fraction = 1;
1033 
1034  if ( verbose ) {
1035  cout << "Calculating radial distances:" << endl;
1036  if ( sym )
1037  cout << "Symmetry: " << sym->label << " (" << vs.size() << ")" << endl;
1038  cout << "Spherical fraction: " << spherical_fraction << endl;
1039  }
1040 
1041  next = new Bimage(Float, TSimple, size(), n);
1042 
1043  for ( i=nn=0; nn<n; nn++ ) {
1044  for ( zz=0; zz<z; zz++ ) {
1045  coor[2] = zz - image[nn].origin()[2];
1046  for ( yy=0; yy<y; yy++ ) {
1047  coor[1] = yy - image[nn].origin()[1];
1048  for ( xx=0; xx<x; xx++, i++ ) {
1049  coor[0] = xx - image[nn].origin()[0];
1050  r = coor.length();
1051  rf = 1;
1052  if ( vs.size() > 1 ) {
1053  rf = radius_wrt_sym_axes(coor, vs);
1054  rf = rf + spherical_fraction*(1 - rf);
1055  r = r*rf;
1056  }
1057  next->set(i, r);
1058  }
1059  }
1060  }
1061  }
1062 
1063  return 0;
1064 }
1065 
1080 int Bimage::radial_section(Bimage* prad, long nn,
1081  long zr, double rad_start, double rad_step, double fill)
1082 {
1083  long j, j1, xx, yy, zz, zj;
1084  long slice_size = x*y;
1085  long image_size = slice_size*z;
1086  long i = (long) (nn*prad->size().volume() + zr*slice_size);
1087  double r = rad_step*zr + rad_start;
1088  double dr, drp;
1089 
1090  for ( yy=0; yy<y; yy++ ) {
1091  for ( xx=0; xx<prad->sizeX(); xx++, i++ ) {
1092  j = nn*image_size + yy*x + xx;
1093  for ( zz=0, zj=z+1, drp=1; zz<z && zj>=z; zz++, j+=slice_size ) {
1094  dr = (*next)[j] - r;
1095  if ( dr > 0 && drp <= 0 ) zj = zz;
1096  drp = dr;
1097  }
1098  if ( zj < z ) {
1099  j1 = nn*image_size + zj*slice_size + yy*x + xx;
1100  j = j1 - slice_size;
1101  dr = ((*next)[j1] - r)/((*next)[j1] - (*next)[j]);
1102  prad->set(i, dr*(*this)[j]);
1103  dr = 1 - dr;
1104  prad->add(i, dr*(*this)[j1]);
1105  } else {
1106  prad->set(i, fill);
1107  }
1108  }
1109  }
1110 
1111  return 0;
1112 }
1113 
1134 Bimage* Bimage::radial_sections(double rad_start, double rad_end,
1135  double rad_step, double spherical_fraction,
1136  Bsymmetry* sym, int fill_type, double fill)
1137 {
1138  if ( rad_start < 0 ) rad_start = 0;
1139  if ( rad_start >= z/2 ) rad_start = z/2;
1140  if ( rad_end <= 0 ) rad_end = z/2;
1141  if ( rad_end >= z/2 ) rad_end = z/2 - 1;
1142  if ( rad_end < rad_start ) rad_end = rad_start;
1143  if ( rad_step <= 0 ) rad_step = 1;
1144 
1145  if ( fill_type == FILL_AVERAGE ) fill = avg;
1146  if ( fill_type == FILL_BACKGROUND ) {
1147  if ( fabs(background(long(0))) < 1e-6 )
1149  fill = background(long(0));
1150  }
1151 
1152  if ( set_radial_distances(spherical_fraction, sym) < 0 )
1153  return NULL;
1154 
1155  long nn, nz = (long) ((rad_end - rad_start)/rad_step + 1);
1156 
1157  if ( verbose ) {
1158  cout << "Calculating radial shells:" << endl;
1159  cout << "Radial range: " << rad_start << " - " << rad_end << endl;
1160  cout << "Radial step size: " << rad_step << endl;
1161  cout << "New image size: " << x << " x " << y << " x " << nz << " x " << n << endl << endl;
1162  }
1163 
1164  Bimage* prad = new Bimage(Float, TSimple, x, y, nz, n);
1165  prad->sampling(sampling(0)[0], sampling(0)[1], sampling(0)[2] * rad_step);
1166 
1167  for ( nn=0; nn<n; nn++ ) {
1168  prad->image[nn] = image[nn];
1169  prad->image[nn].origin(prad->image[nn].origin()[0], prad->image[nn].origin()[1], -rad_start);
1170 #ifdef HAVE_GCD
1171  dispatch_apply(nz, dispatch_get_global_queue(0, 0), ^(size_t zz){
1172  radial_section(prad, nn, zz, rad_start, rad_step, fill);
1173  });
1174 #else
1175 #pragma omp parallel for
1176  for ( long zz=0; zz<nz; zz++ )
1177  radial_section(prad, nn, zz, rad_start, rad_step, fill);
1178 #endif
1179  }
1180 
1181  prad->statistics();
1182 
1183  return prad;
1184 }
1185 
1193 Bimage* Bimage::radial_coverage(double threshold, double rad_step)
1194 {
1195  if ( rad_step <= 0 ) rad_step = 1;
1196 
1197  to_mask(threshold);
1198 
1199  Bimage* prad = radial(0, x/2, rad_step);
1200 
1201  if ( verbose ) {
1202  cout << "Calculating radial shell coverage:" << endl;
1203  cout << "Threshold: " << threshold << endl;
1204  cout << "Radial step size: " << rad_step << endl;
1205  cout << "Radius\tCoverage" << endl;
1206  }
1207 
1208  long i, xx, nn;
1209 
1210  for ( i=nn=0; nn<n; nn++ ) {
1211  for ( xx=0; xx<prad->x; ++xx, ++i )
1212  cout << xx << tab << (*prad)[i] << endl;
1213  }
1214 
1215  delete prad;
1216 
1217  return 0;
1218 }
1219 
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
UCharacter
@ UCharacter
Definition: utilities.h:153
Complex
Definition: Complex.h:23
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bimage::cylindrical_shells
int cylindrical_shells()
Calculates an image with slices representing cylindrical shell projections.
Definition: Bimage_polar.cpp:928
Bimage::shell
int shell(Vector3< double > center, double minrad, double maxrad, double width, int fill_type=0, double fill=0)
Fills a shell within an image with a uniform value.
Definition: Bimage_edit.cpp:546
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::sum
void sum(long m, Bimage **p)
Sums an array of images with their FOM blocks.
Definition: Bimage_combine.cpp:23
Bimage::line_powerspectra
int line_powerspectra(fft_plan plan)
Calculates the 1D power spectrum of each line in an image.
Definition: Bimage_polar.cpp:814
Bimage::radial
Bimage * radial(long minrad, long maxrad, double rad_step=1, int wrap=0)
Calculates the radial average of an image.
Definition: Bimage_polar.cpp:33
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
simplex.h
Nelder and Mead downhill simplex method for generalized parameter fitting.
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bsimplex::dependent_values
vector< double > & dependent_values()
Definition: simplex.h:85
Vector3::scalar
double scalar(const Vector3 &v) const
Definition: Vector3.h:249
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bsymmetry
Definition: symmetry.h:38
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Bsimplex
Structure used in the downhill simplex method.
Definition: simplex.h:44
Bimage::radial_shells
int radial_shells()
Calculates an image with slices representing radial shell projections.
Definition: Bimage_polar.cpp:856
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Bimage::cartesian_to_cylindrical
Bimage * cartesian_to_cylindrical(long nannuli, long nphi, int flag=0)
Calculates an image with cylindrical coordinates.
Definition: Bimage_polar.cpp:592
Bimage::radial_fit
double * radial_fit(Bimage *pref)
Fits a radial profile to a reference radial profile.
Definition: Bimage_polar.cpp:328
verbose
int verbose
Bimage::radial_coverage
Bimage * radial_coverage(double threshold, double rad_step=1)
Calculates the coverage in each radial shell.
Definition: Bimage_polar.cpp:1193
Bsimplex::independent_values
vector< double > & independent_values()
Definition: simplex.h:84
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Bsimplex::points
long points()
Definition: simplex.h:66
Vector3::normalize
double normalize()
Definition: Vector3.h:238
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::file_name
string & file_name()
Definition: Bimage.h:246
Bimage::radial_symmetry_adjusted
Bimage * radial_symmetry_adjusted(double rad_start, double rad_end, double rad_step, double spherical_fraction, Bsymmetry *sym)
Calculates the symmetry-adjusted radial average of an image.
Definition: Bimage_polar.cpp:209
Bimage::image_size
long image_size()
Definition: Bimage.h:290
Bimage::radial_to_full
Bimage * radial_to_full(Vector3< long > nusize, Vector3< double > origin)
Generates a full 2D or 3D image from a radial profile in a 1D image.
Definition: Bimage_polar.cpp:412
symmetry_get_axes
vector< Vector3< double > > symmetry_get_axes(Bsymmetry *sym)
Get all symmetry axes.
Definition: symmetry.cpp:378
Bimage::data_size
long data_size()
Definition: Bimage.h:270
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
fftw
int fftw(fft_plan plan, Complex< float > *a)
Fast Fourier transforms a data array.
Definition: fft.cpp:106
Bsimplex::run
double run(long maxcycles, double tolerance, double(funk)(Bsimplex &))
Definition: simplex.cpp:76
Bimage::interpolate_wrap
double interpolate_wrap(long cc, double xx, double yy=0, double zz=0, long nn=0) const
Interpolates using a given location with wrapping.
Definition: Bimage.cpp:1524
Bimage::power
void power(double v)
Calculates the power of an image.
Definition: Bimage.cpp:3291
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::radial_sections
Bimage * radial_sections(double rad_start, double rad_end, double rad_step, double spherical_fraction, Bsymmetry *sym, int fill_type=FILL_USER, double fill=0)
Calculates an image with slices giving the radial section projections.
Definition: Bimage_polar.cpp:1134
Bsimplex::parameter
void parameter(long i, double p)
Definition: simplex.h:71
Bimage.h
Header file for image class.
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
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.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bimage::check_resolution
void check_resolution(double &resolution)
Checks that the resolution falls within reasonable limits.
Definition: Bimage.cpp:635
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
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Vector3::volume
double volume()
Definition: Vector3.h:237
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
Bimage::polar_transform
Bimage * polar_transform(long nangles, long ann_min, long ann_max, long dann, long zmin, long zmax, long zinc)
Calculates an image with cylindrical coordinates by integration.
Definition: Bimage_polar.cpp:699
Matrix.h
Generalized matrix class.
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::sampling
vector< Vector3< double > > sampling()
Definition: Bimage.h:529
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:875
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::minimum
double minimum()
Definition: Bimage.h:541
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bimage::sizeX
long sizeX() const
Definition: Bimage.h:393
Bsimplex::parameters
long parameters()
Definition: simplex.h:67
radius_wrt_sym_axes
double radius_wrt_sym_axes(Vector3< double > coor, vector< Vector3< double >> vs)
Calculates the radius relative to the closest symmetry axis.
Definition: Bimage_polar.cpp:997
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Double
@ Double
Definition: utilities.h:162
Vector3::length
double length()
Definition: Vector3.h:222
radial_R
double radial_R(Bsimplex &simp)
Definition: Bimage_polar.cpp:281
Bsimplex::limits
void limits(long i, double vlo, double vhi)
Definition: simplex.h:77
Float
@ Float
Definition: utilities.h:161
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::cartesian_to_spherical
Bimage * cartesian_to_spherical(long nannuli, long nphi, long ntheta)
Calculates an image with spherical coordinates.
Definition: Bimage_polar.cpp:501
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::to_mask
long to_mask()
Definition: Bimage.h:1313
Bimage::polar_power_spectrum
Bimage * polar_power_spectrum(double resolution, long num_angle)
Calculates the polar power spectrum of a 2D transform amplitude or intensity image.
Definition: Bimage_polar.cpp:774
TWOPI
#define TWOPI
Definition: utilities.h:72
Vector3::max
Type max()
Definition: Vector3.h:175