35 return radial(minrad, maxrad, rad_step, 1, 0, NULL, wrap);
57 return radial(minrad, maxrad, rad_step, 1, 0, pmask, wrap);
80 double ellipticity,
double angle,
Bimage* pmask,
int wrap)
82 long i, j, nn, xx, yy, zz, ir, nrad(0);
83 double dx, dy, dz, x2, y2, z2, a, r, f, f1;
85 if ( rad_step < 0.1 ) rad_step = 1;
90 if ( z < 2 ) scale[2] = 1;
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);
100 vector<float> num(maxrad);
107 cout <<
"Calculating a radial profile:" << endl;
108 cout <<
"Minimum and maximum pixels: " << minrad <<
" " << maxrad << endl;
109 cout <<
"Radial step size: " << rad_step << endl;
111 cout <<
"Radial step size: " << rad_step*
image->
sampling()[0] <<
" A" << endl;
113 cout <<
"Mask file: " << pmask->
file_name() << endl;
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;
128 if ( wrap && dz < -h[2] ) dz += z;
129 for ( zz=0; zz<z; zz++, dz++ ) {
130 if ( wrap && dz > h[2] ) dz -= z;
134 if ( wrap && dy < -h[1] ) dy += y;
135 for ( yy=0; yy<y; yy++, dy++ ) {
136 if ( wrap && dy > h[1] ) dy -= y;
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;
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)+
153 if ( ( ir >= minrad ) && ( ir < maxrad ) ) {
158 prad->
add(j, f1*(*
this)[i]);
163 prad->
add(j, f*(*
this)[i]);
170 for ( j=0, ir = nn*maxrad; j<maxrad; j++, ir++ ) {
172 prad->
set(ir, (*prad)[ir]/num[j]);
181 cout <<
"Pixels in radial averages: " << nrad << endl;
184 cout <<
"DEBUG Bimage:radial: prad size = " << prad->
sizeX() << endl;
210 double rad_end,
double rad_step,
211 double spherical_fraction,
Bsymmetry* sym)
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;
220 if ( set_radial_distances(spherical_fraction, sym) < 0 )
224 long nr = (long) ((rad_end - rad_start)/rad_step + 1);
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;
237 vector<double> num(nr*n);
239 for ( i=nn=0; nn<n; ++nn ) {
241 r = ((*next)[i] - rad_start)/rad_step;
247 prad->
add(ir, f1*(*
this)[i]);
251 prad->
add(ir, f*(*
this)[i]);
259 if ( num[i] ) prad->
set(i, (*prad)[i]/num[i]);
287 double fraction, xref, df, R(0), sum(0), weight;
289 for ( i=0; i<simp.
points(); i++ ) {
292 if ( iref >= 0 && iref < simp.
points() - 1 ) {
293 fraction = xref - iref;
294 df = (1.0 - fraction)*yref[iref] + fraction*yref[iref + 1]
304 if ( sum && n > 0.2*simp.
points() ) R = sqrt(R/sum);
308 cout <<
"DEBUG radial_R: " << simp.
parameter(0) <<
" " <<
331 if ( x < pref->
sizeX() ) npoint = pref->
sizeX();
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];
338 Bsimplex simp(1, 3, 0, npoint, ax, fx);
347 else if ( max - min > 0 && pref->
maximum() - pref->
minimum() > 0 )
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;
370 double fraction, xdat, f;
373 cout <<
"Radial fit final parameters:" << endl;
374 cout <<
"Dimension scale: " << simp.
parameter(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;
383 for ( i=0; i<pref->
sizeX(); i++ ) {
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);
393 cout << pref->
sampling(0)[0]*i <<
" " << f <<
" " << (*pref)[i] << endl;
397 double* result =
new double[simp.
parameters()];
414 if ( nusize.
volume() < 1 )
return NULL;
424 long i, nn, xx, yy, zz, ir, nexp(0);
429 cout <<
"Calculating a full image from a radial profile:" << endl;
430 cout <<
"Size: " << p->
size() << endl;
431 cout <<
"Origin: " <<
origin << endl;
434 for ( nn=i=0; nn<n; nn++ ) {
435 for ( zz=0; zz<p->
sizeZ(); zz++ ) {
437 for ( yy=0; yy<p->
sizeY(); yy++ ) {
439 for ( xx=0; xx<p->
sizeX(); xx++, i++ ) {
449 if ( ir < x ) v += f*(*this)[ir];
463 cout <<
"Pixels generated: " << nexp << endl << endl;
503 long j, nn, ann, phi, theta;
504 double maxrad, scale;
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;
514 double dannulus(maxrad*1.0/nannuli);
515 double dphi(
TWOPI/nphi);
516 double dtheta(
M_PI/ntheta);
517 double cos_phi, sin_phi, cos_theta, sin_theta;
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;
533 cout <<
"Image\tOrigin" << endl;
534 for ( nn=0; nn<n; nn++ ) {
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;
558 cout <<
"DEBUG Bimage::cartesian_to_spherical: Calculating statistics" << endl;
594 long j, nn, zz, ann, phi;
598 if ( maxrad < y/2 ) maxrad = y/2;
600 if ( nannuli < 1 ) nannuli = (long) maxrad;
601 if ( nphi < 1 ) nphi = 90;
602 if ( y < 2 ) nphi = 1;
604 double dannulus(maxrad*1.0/nannuli);
605 double dphi(
TWOPI/nphi);
606 double cos_phi, sin_phi;
626 cerr <<
"Bimage::cartesian_to_cylindrical: flag = " << flag <<
" not supported!" << endl;
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;
638 cout <<
"Image\tOrigin" << endl;
639 for ( nn=0; nn<n; nn++ ) {
642 for ( zz=0; zz<z; 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++ ) {
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;
671 cout <<
"DEBUG Bimage::cartesian_to_cylindrical: Calculating statistics" << endl;
700 long dann,
long zmin,
long zmax,
long zinc)
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;
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;
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);
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;
734 cout <<
"Calculating a polar image" << endl << endl;
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++ ) {
742 for ( iz=zz; iz<zz+zinc; iz++ ) {
743 for ( r=ann; r<ann+dann; r++ ) {
755 cout <<
"DEBUG Bimage::polar_transform: Calculating statistics" << endl;
779 if ( max_rad > x/2 ) max_rad = x/2;
780 long max_ang = num_angle*max_rad;
784 double ang, xx, yy, fac;
787 cout <<
"Calculating a radial power spectrum: " << max_rad <<
" annuli x " << max_ang <<
" angles" << endl;
789 for ( nn=0; nn<n; nn++ ) {
790 for ( r=1; r<max_rad; r++ ) {
791 max_ang = num_angle*r;
793 for ( a=0; a<max_ang; a++ ) {
816 long i, is, isx, xx, yy, zz;
822 for ( is=zz=0; zz<z; zz++ ) {
823 for ( yy=0; yy<y; yy++, is++ ) {
825 for ( xx=0, i=isx; xx<x; xx++, i++ )
829 for ( xx=1, i=isx+1,
sum=0; xx<x; xx++, i++ ) {
833 if (
sum )
for ( xx=1, i=isx+1; xx<x; xx++, i++ )
860 long i, j, nn, xx, yy, zz;
862 double dx2, dy2, dz, dz2, d2, oldz, fraction;
864 float*
shell =
new float[datasize];
867 cout <<
"Converting to radial shells" << endl << endl;
871 cout <<
"Image\tOrigin" << endl;
872 for ( j=nn=0; nn<n; nn++ ) {
875 for ( zz=0; zz<z; zz++ ) {
878 for ( yy=0; yy<y; yy++ ) {
881 for ( xx=0; xx<x; xx++, j++ ) {
884 d2 = dz2 - dy2 - dx2;
886 if ( dz < 0 ) oldz =
image[nn].
origin()[2] - sqrt(d2);
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];
895 fraction = 1 - fraction;
897 shell[j] += fraction*(*this)[i];
932 long i, j, nn, xx, yy, zz;
934 double dx, dy, dx2, dy2, d2, oldy, fraction;
936 float*
shell =
new float[datasize];
939 cout <<
"Converting to cylindrical shells" << endl << endl;
943 cout <<
"Image\tOrigin" << endl;
944 for ( j=nn=0; nn<n; nn++ ) {
947 for ( zz=0; zz<z; zz++ ) {
950 for ( yy=0; yy<y; yy++ ) {
953 for ( xx=0; xx<x; xx++, j++ ) {
958 if ( dy < 0 ) oldy =
image[nn].
origin()[1] - sqrt(d2);
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];
967 fraction = 1 - fraction;
969 shell[j] += fraction*(*this)[i];
1003 for (
auto it = vs.begin(); it != vs.end(); ++it ) {
1005 if ( rmax < r ) rmax = r;
1021 int Bimage::set_radial_distances(
double spherical_fraction,
Bsymmetry* sym)
1025 long i, xx, yy, zz, nn;
1030 if ( vs.size() < 1 )
return -1;
1032 if ( !sym || vs.size() < 2 ) spherical_fraction = 1;
1035 cout <<
"Calculating radial distances:" << endl;
1037 cout <<
"Symmetry: " << sym->
label <<
" (" << vs.size() <<
")" << endl;
1038 cout <<
"Spherical fraction: " << spherical_fraction << endl;
1043 for ( i=nn=0; nn<n; nn++ ) {
1044 for ( zz=0; zz<z; zz++ ) {
1046 for ( yy=0; yy<y; yy++ ) {
1048 for ( xx=0; xx<x; xx++, i++ ) {
1052 if ( vs.size() > 1 ) {
1054 rf = rf + spherical_fraction*(1 - rf);
1080 int Bimage::radial_section(
Bimage* prad,
long nn,
1081 long zr,
double rad_start,
double rad_step,
double fill)
1083 long j, j1, xx, yy, zz, zj;
1084 long slice_size = x*y;
1086 long i = (long) (nn*prad->
size().
volume() + zr*slice_size);
1087 double r = rad_step*zr + rad_start;
1090 for ( yy=0; yy<y; yy++ ) {
1091 for ( xx=0; xx<prad->
sizeX(); xx++, i++ ) {
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;
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]);
1104 prad->
add(i, dr*(*
this)[j1]);
1135 double rad_step,
double spherical_fraction,
1136 Bsymmetry* sym,
int fill_type,
double fill)
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;
1152 if ( set_radial_distances(spherical_fraction, sym) < 0 )
1155 long nn, nz = (long) ((rad_end - rad_start)/rad_step + 1);
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;
1167 for ( nn=0; nn<n; nn++ ) {
1171 dispatch_apply(nz, dispatch_get_global_queue(0, 0), ^(
size_t zz){
1172 radial_section(prad, nn, zz, rad_start, rad_step,
fill);
1175 #pragma omp parallel for
1176 for (
long zz=0; zz<nz; zz++ )
1177 radial_section(prad, nn, zz, rad_start, rad_step,
fill);
1195 if ( rad_step <= 0 ) rad_step = 1;
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;
1210 for ( i=nn=0; nn<n; nn++ ) {
1211 for ( xx=0; xx<prad->x; ++xx, ++i )
1212 cout << xx <<
tab << (*prad)[i] << endl;