38#include "gsl/gsl_sf_result.h"
39#include "gsl/gsl_sf_bessel.h"
53typedef vector< pair<float,int> >
vp;
60 return fftcache->copy();
66 LOGERR(
" NATIVE_FFT does not support complex to complex.");
77 EMfft::complex_to_complex_nd(temp_in->get_data(),dat->get_data(),
nx-offset,
ny,
nz);
79 if(dat->get_ysize()==1 && dat->get_zsize()==1) dat->set_complex_x(
true);
88 int offset = 2 -
nx%2;
89 int nx2 =
nx + offset;
91 dat->set_size(nx2,
ny,
nz);
93 if (offset == 1) dat->set_fftodd(
true);
94 else dat->set_fftodd(
false);
96 float *d = dat->get_data();
98 EMfft::real_to_complex_nd(
get_data(), d, nxreal,
ny,
nz);
101 dat->set_fftpad(
true);
102 dat->set_complex(
true);
103 dat->set_attr(
"is_intensity",
false);
104 if(dat->get_ysize()==1 && dat->get_zsize()==1) dat->set_complex_x(
true);
111 fftcache=dat->copy();
123 LOGERR(
"real image expected. Input image is complex image.");
137 int nxnew =
nx + offset;
139 for (
int iz =
nz-1; iz >= 0; iz--) {
140 for (
int iy =
ny-1; iy >= 0; iy--) {
141 for (
int ix = nxreal-1; ix >= 0; ix--) {
142 size_t oldxpos = ix + (iy + iz*
ny)*(
size_t)nxreal;
143 size_t newxpos = ix + (iy + iz*
ny)*(
size_t)nxnew;
144 (*this)(newxpos) = (*
this)(oldxpos);
151 nxreal =
nx - offset;
165#ifdef EMAN2_USING_CUDA
169EMData *EMData::do_fft_cuda()
174 LOGERR(
"real image expected. Input image is complex image.");
178 int offset = 2 -
nx%2;
182 if(cudarwdata == 0){copy_to_cuda();}
185 if (offset == 1) dat->set_fftodd(
true);
186 else dat->set_fftodd(
false);
188 dat->set_fftpad(
true);
189 dat->set_complex(
true);
190 if(dat->get_ysize()==1 && dat->get_zsize()==1) dat->set_complex_x(
true);
198void EMData::do_fft_inplace_cuda()
203 LOGERR(
"real image expected. Input image is complex image.");
207 int offset = 2 -
nx%2;
208 float* tempcudadata = 0;
209 cudaError_t error = cudaMalloc((
void**)&tempcudadata,(
nx + offset)*
ny*
nz*
sizeof(
float));
213 if(cudarwdata == 0){copy_to_cuda();}
216 cudaError_t ferror = cudaFree(cudarwdata);
218 cudarwdata = tempcudadata;
219 num_bytes = (
nx + offset)*
ny*
nz*
sizeof(
float);
235EMData *EMData::do_ift_cuda()
240 LOGERR(
"complex image expected. Input image is real image.");
252 if(cudarwdata == 0){copy_to_cuda();}
258 }
else if (ndim == 2) {
260 }
else if (ndim == 3) {
265 float scale = 1.0f/
static_cast<float>((dat->get_size()));
268 dat->set_fftpad(
false);
269 dat->set_fftodd(
false);
270 dat->set_complex(
false);
271 if(dat->get_ysize()==1 && dat->get_zsize()==1) dat->set_complex_x(
false);
285void EMData::do_ift_inplace_cuda()
290 LOGERR(
"complex image expected. Input image is real image.");
295 LOGWARN(
"run IFT on AP data, only RI should be used. ");
300 if(cudarwdata == 0){copy_to_cuda();}
305 }
else if (ndim == 2) {
307 }
else if (ndim == 3) {
312 int nxo =
nx - offset;
335 LOGERR(
"complex image expected. Input image is real image.");
340 LOGWARN(
"run IFT on AP data, only RI should be used. Converting.");
345 dat->set_size(
nx,
ny,
nz);
348 float *d = dat->get_data();
354 memcpy((
char *) d, (
char *)
rdata, (
size_t)
nx *
ny *
nz *
sizeof(
float));
359 EMfft::complex_to_real_nd(d, d,
nx - offset,
ny,
nz);
361 EMfft::complex_to_real_nd(d, d,
nx - offset,
ny,
nz);
363 size_t row_size = (
nx - offset) *
sizeof(
float);
364 for (
size_t i = 1; i < (size_t)
ny *
nz; i++) {
365 memmove((
char *) &d[i * (
nx - offset)], (
char *) &d[i *
nx], row_size);
369 dat->set_size(
nx - offset,
ny,
nz);
375 dat->set_fftodd(
false);
376 dat->set_fftpad(
false);
377 dat->set_complex(
false);
378 if(dat->get_ysize()==1 && dat->get_zsize()==1) dat->set_complex_x(
false);
380 dat->set_attr(
"is_intensity",
false);
397 LOGERR(
"complex image expected. Input image is real image.");
402 LOGWARN(
"run IFT on AP data, only RI should be used. ");
408 EMfft::complex_to_real_nd(data, data,
nx - offset,
ny,
nz);
412 int nxo =
nx - offset;
413 float scale = 1.0f / ((size_t)nxo *
ny *
nz);
430 int bpl,
float scale,
int mingray,
int maxgray,
431 float render_min,
float render_max,
float gamma,
int flags)
436 int hist=(
flags&2)/2;
437 int invy=(
flags&4)?1:0;
447 if (render_max <= render_min) {
448 render_max = render_min + 0.01f;
451 if (gamma<=0) gamma=1.0;
460 static int smg0=0,smg1=0;
462 static unsigned char gammamap[4096];
463 if (gamma!=1.0 && (smg0!=mingray || smg1!=maxgray || sgam!=gamma)) {
464 for (
int i=0; i<4096; i++) {
465 if (mingray<maxgray) gammamap[i]=(
unsigned char)(mingray+(maxgray-mingray+0.999)*pow(((
float)i/4096.0f),gamma));
466 else gammamap[4095-i]=(
unsigned char)(mingray+(maxgray-mingray+0.999)*pow(((
float)i/4096.0f),gamma));
473 if (
flags&8) asrgb=4;
474 else if (
flags&1) asrgb=3;
479 ret.assign(iysize*bpl+hist*1024,
char(mingray));
480 unsigned char *data=(
unsigned char *)ret.data();
481 unsigned int *histd=(
unsigned int *)(data+iysize*bpl);
483 for (
int i=0; i<256; i++) histd[i]=0;
486 float rm = render_min;
487 float inv_scale = 1.0f /
scale;
492 if (iysize * inv_scale >
ny) {
493 ymin = (int) (iysize -
ny / inv_scale);
496 float gs = (maxgray - mingray) / (render_max - render_min);
497 float gs2 = 4095.999f / (render_max - render_min);
499 if (render_max < render_min) {
508 const int scale_n = 100000;
512 if (inv_scale == floor(inv_scale)) {
513 dsx = (int) inv_scale;
514 dsy = (int) (inv_scale *
nx);
517 addi = (int) floor(inv_scale);
518 addr = (int) (scale_n * (inv_scale - floor(inv_scale)));
523 xmin = (int) (-x0 / inv_scale);
524 xsize -= (int) floor(x0 / inv_scale);
528 if ((xsize - xmin) * inv_scale > (
nx - x0)) {
529 xsize = (int) ((
nx - x0) / inv_scale + xmin);
531 int ymax = ysize - 1;
533 ymax = (int) (ysize + y0 / inv_scale - 1);
534 ymin += (int) floor(y0 / inv_scale);
538 if (xmin < 0) xmin = 0;
539 if (ymin < 0) ymin = 0;
540 if (xsize > ixsize) xsize = ixsize;
541 if (ymax > iysize) ymax = iysize;
543 int lmax =
nx *
ny - 1;
549 for (
int j = ymax; j >= ymin; j--) {
551 for (
int i = xmin; i < xsize; i++) {
552 if (l + ll > lmax || ll >=
nx - 2)
break;
558 if (l >= (
ny - inv_scale) *
nx) k = 2 * (ll -
nx / 2) + 2;
559 else k = 2 * (ll -
nx / 2) + l + 2 +
nx;
562 ph = (int)(image_data[k+1]*768/(2.0*M_PI))+384;
565 k =
nx *
ny - (l + 2 * ll) - 2;
566 ph = (int)(-image_data[k+1]*768/(2.0*M_PI))+384;
570 float t = image_data[k];
571 if (t <= rm) p = mingray;
572 else if (t >= render_max) p = maxgray;
573 else if (gamma!=1.0) {
574 k=(int)(gs2 * (t-render_min));
578 p = (
unsigned char) (gs * (t - render_min));
582 data[i * asrgb + j * bpl] = p*(255-ph)/256;
583 data[i * asrgb + j * bpl+1] = p*ph/256;
584 data[i * asrgb + j * bpl+2] = 0;
587 data[i * asrgb + j * bpl+1] = p*(511-ph)/256;
588 data[i * asrgb + j * bpl+2] = p*(ph-256)/256;
589 data[i * asrgb + j * bpl] = 0;
592 data[i * asrgb + j * bpl+2] = p*(767-ph)/256;
593 data[i * asrgb + j * bpl] = p*(ph-512)/256;
594 data[i * asrgb + j * bpl+1] = 0;
596 if (hist) histd[p]++;
605 for (
int j = ymax; j >= ymin; j--) {
609 for (
int i = xmin; i < xsize - 1; i++) {
610 if (l + ll > lmax || ll >=
nx - 2) {
617 if (l >= (
ny *
nx -
nx)) k = 2 * (ll -
nx / 2) + 2;
618 else k = 2 * (ll -
nx / 2) + l + 2 +
nx;
621 ph = (int)(image_data[k+1]*768/(2.0*M_PI))+384;
624 k =
nx *
ny - (l + 2 * ll) - 2;
627 ph = (int)(-image_data[k+1]*768/(2.0*M_PI))+384;
630 float t = image_data[k];
633 else if (t >= render_max) {
636 else if (gamma!=1.0) {
637 k=(int)(gs2 * (t-render_min));
641 p = (
unsigned char) (gs * (t - render_min));
645 data[i * asrgb + j * bpl] = p*(255-ph)/256;
646 data[i * asrgb + j * bpl+1] = p*ph/256;
647 data[i * asrgb + j * bpl+2] = 0;
650 data[i * asrgb + j * bpl+1] = p*(511-ph)/256;
651 data[i * asrgb + j * bpl+2] = p*(ph-256)/256;
652 data[i * asrgb + j * bpl] = 0;
655 data[i * asrgb + j * bpl+2] = p*(767-ph)/256;
656 data[i * asrgb + j * bpl] = p*(ph-512)/256;
657 data[i * asrgb + j * bpl+1] = 0;
659 if (hist) histd[p]++;
662 if (remx > scale_n) {
669 if (remy > scale_n) {
678 for (
int j=ymin*bpl; j<=ymax*bpl; j+=bpl) {
679 for (
int i=xmin; i<xsize*4; i+=4) {
691 for (
y=0;
y<iysize/2;
y++) {
692 for (
x=0;
x<ixsize;
x++) {
694 ret[
y*bpl+
x]=ret[(iysize-
y-1)*bpl+
x];
695 ret[(iysize-
y-1)*bpl+
x]=swp;
706 int bpl,
float scale,
int mingray,
int maxgray,
707 float render_min,
float render_max,
void *ref,
708 void cmap(
void *,
int coord,
unsigned char *tri))
720 if (render_max <= render_min) {
721 render_max = render_min + 0.01f;
724 std::string ret=std::string();
725 ret.resize(iysize*bpl);
726 unsigned char *data=(
unsigned char *)ret.data();
728 float rm = render_min;
729 float inv_scale = 1.0f /
scale;
732 const int scale_n = 100000;
735 if ( iysize * inv_scale >
ny) {
736 ymin = (int) (iysize -
ny / inv_scale);
738 float gs = (maxgray - mingray) / (render_max - render_min);
739 if (render_max < render_min) {
745 if (inv_scale == floor(inv_scale)) {
746 dsx = (int) inv_scale;
747 dsy = (int) (inv_scale *
nx);
753 addi = (int) floor(inv_scale);
754 addr = (int) (scale_n * (inv_scale - floor(inv_scale)));
761 xmin = (int) (-x0 / inv_scale);
762 xsize -= (int) floor(x0 / inv_scale);
766 if ((xsize - xmin) * inv_scale > (
nx - x0)) {
767 xsize = (int) ((
nx - x0) / inv_scale + xmin);
769 int ymax = ysize - 1;
771 ymax = (int) (ysize + y0 / inv_scale - 1);
772 ymin += (int) floor(y0 / inv_scale);
784 if (xsize > ixsize) {
791 int lmax =
nx *
ny - 1;
792 unsigned char tri[3];
797 for (
int j = ymax; j >= ymin; j--) {
799 for (
int i = xmin; i < xsize; i++, ll += dsx) {
800 if (l + ll > lmax || ll >=
nx - 2) {
805 if (l >= (
ny - inv_scale) *
nx) {
806 kk = 2 * (ll -
nx / 2) + 2;
809 kk = 2 * (ll -
nx / 2) + l + 2 +
nx;
813 kk =
nx *
ny - (l + 2 * ll) - 2;
816 float t = image_data[kk];
820 else if (t >= render_max) {
824 k = (int) (gs * (t - render_min));
827 tri[0] =
static_cast < unsigned char >(k);
829 data[i * 3 + j * bpl] = tri[0];
830 data[i * 3 + 1 + j * bpl] = tri[1];
831 data[i * 3 + 2 + j * bpl] = tri[2];
838 for (
int j = ymax, l = y0 *
nx; j >= ymin; j--) {
841 for (
int i = xmin, ll = x0; i < xsize - 1; i++) {
842 if (l + ll > lmax || ll >=
nx - 2) {
847 if (l >= (
ny *
nx -
nx)) {
848 kk = 2 * (ll -
nx / 2) + 2;
851 kk = 2 * (ll -
nx / 2) + l + 2 +
nx;
855 kk =
nx *
ny - (l + 2 * ll) - 2;
858 float t = image_data[kk];
862 else if (t >= render_max) {
866 k = (int) (gs * (t - render_min));
869 tri[0] =
static_cast < unsigned char >(k);
871 data[i * 3 + j * bpl] = tri[0];
872 data[i * 3 + 1 + j * bpl] = tri[1];
873 data[i * 3 + 2 + j * bpl] = tri[2];
876 if (remx > scale_n) {
883 if (remy > scale_n) {
892 for (
int j = ymax, l = x0 + y0 *
nx; j >= ymin; j--) {
894 for (
int i = xmin; i < xsize; i++, l += dsx) {
898 float t = image_data[l];
903 else if (t >= render_max) {
907 k = (int) (gs * (t - render_min));
910 tri[0] =
static_cast < unsigned char >(k);
912 data[i * 3 + j * bpl] = tri[0];
913 data[i * 3 + 1 + j * bpl] = tri[1];
914 data[i * 3 + 2 + j * bpl] = tri[2];
921 for (
int j = ymax, l = x0 + y0 *
nx; j >= ymin; j--) {
924 for (
int i = xmin; i < xsize; i++) {
928 float t = image_data[l];
933 else if (t >= render_max) {
937 k = (int) (gs * (t - render_min));
940 tri[0] =
static_cast < unsigned char >(k);
942 data[i * 3 + j * bpl] = tri[0];
943 data[i * 3 + 1 + j * bpl] = tri[1];
944 data[i * 3 + 2 + j * bpl] = tri[2];
947 if (remx > scale_n) {
954 if (remy > scale_n) {
987 size_t size = (size_t)
nx *
ny *
nz;
988 for (
size_t i = 0; i < size; i += 2) {
989 data[i]=data[i]*data[i]+data[i+1]*data[i+1];
1009 size_t size = (size_t)
nx *
ny *
nz;
1010 for (
size_t i = 0; i < size; i += 2) {
1011 float f = (float)hypot(data[i], data[i + 1]);
1012 if (data[i] == 0 && data[i + 1] == 0) {
1016 data[i + 1] = atan2(data[i + 1], data[i]);
1028 gsl_sf_result result;
1030 gsl_sf_bessel_Jn_e(n,(
double)
x, &result);
1031 return (
float)result.val;
1038 int Mid = (int) ((1+EndP)/2);
1041 int CountxyMax = End*End;
1043 int *SortfkInds =
new int[CountxyMax];
1044 int *kVecX =
new int[CountxyMax];
1045 int *kVecY =
new int[CountxyMax];
1046 float *fkVecR =
new float[CountxyMax];
1047 float *fkVecI =
new float[CountxyMax];
1048 float *absD1fkVec =
new float[CountxyMax];
1049 float *absD1fkVecSorted =
new float[CountxyMax];
1051 float *jxjyatan2 =
new float[End*End];
1055 for (
int jx=0; jx <End ; jx++) {
1056 for (
int jy=0; jy <End ; jy++) {
1064 fk ->process_inplace(
"xform.fourierorigin.tocenter");
1072 for (
int jCount= 0; jCount<End*End; jCount++) {
1074 int jx = jCount%End ;
1075 int jy = (jCount-jx)/End ;
1076 jxjyatan2[jCount] = atan2((
float)(jy+1-Mid) , (
float)(jx +1-Mid));
1080 for (
int kEx= 0; kEx<2*Mid; kEx=kEx+2) {
1085 int kCIx = kCx+ Mid-1 ;
1086 for (
int kEy= 0 ; kEy<End; kEy++) {
1091 float absVal =
::sqrt(realVal*realVal+imagVal*imagVal);
1092 float fkAng = atan2(imagVal,realVal);
1107 AngMatlab = fkAng - 2.0f*M_PI*(kx +ky)*(Mid)/End;
1108 NewRealVal = absVal*cos(AngMatlab);
1109 NewImagVal = absVal*sin(AngMatlab);
1112 fkVecR[kIy+kIx *End] = NewRealVal ;
1113 fkVecR[kIy+kCIx*End] = NewRealVal ;
1114 fkVecI[kIy+kIx *End] = NewImagVal ;
1115 fkVecI[kIy+kCIx*End] = -NewImagVal ;
1116 absD1fkVec[kIy + kIx *End] = absVal;
1117 absD1fkVec[kIy + kCIx *End] = absVal;
1118 kVecX[kIy+kIx *End] = kx ;
1119 kVecX[kIy+kCIx *End] = kCx ;
1120 kVecY[kIy+kIx *End] = ky ;
1121 kVecY[kIy+kCIx *End] = ky ;
1137 _unlink(
"fkCopy.???");
1138 _unlink(
"fk?Copy.???");
1141 rslt = system(
"rm -f fkCopy.???"); rslt++;
1142 rslt = system(
"rm -f fk?Copy.???"); rslt++;
1148 cout <<
"Starting the sort "<< endl;
1150 vector< pair<float, int> > absInds;
1151 for(
int i = 0; i < CountxyMax; ++i ) {
1153 p = make_pair(absD1fkVec[i],i);
1154 absInds.push_back( p);
1157 std::sort(absInds.begin(),absInds.end());
1159 for(
int i = 0; i < CountxyMax; ++i ) {
1162 absD1fkVecSorted[CountxyMax-1-i] = p.first ;
1163 SortfkInds[CountxyMax-1-i] = p.second ;
1166 cout <<
"Ending the sort "<< endl;
1171 for(
int i = 0; i < CountxyMax; ++i ) {
1179 cout<<
"Ratio of Last Amplitude to First Amplitude= " << absD1fkVecSorted[NK] /absD1fkVecSorted[0] << endl;
1203 cout <<
"NK = " << NK << endl;
1205 int LradRange= (int) (floor(Mid/frR)) ;
1207 float *radRange =
new float[LradRange];
1209 for (
int irad=1; irad < LradRange; irad++){
1210 radRange[irad] = radRange[irad-1] + frR; }
1215 cout <<
"Starting the calculation of invariants for N= " << N << endl;
1220 RotTransInv ->
set_size(LradRange,LradRange);
1228 for (
int jr1=0; jr1 < LradRange ; jr1++ ) {
1229 float r1= radRange[jr1];
1231 for (
int jr2=0; jr2<LradRange; jr2++ ) {
1232 float r2= radRange[jr2];
1233 float RotTransInvTemp=0;
1234 for (
int jCountkxy =0; jCountkxy<NK; jCountkxy++){
1235 int Countkxy =SortfkInds[jCountkxy] ;
1236 int kx = kVecX[Countkxy] ;
1237 int ky = kVecY[Countkxy] ;
1238 float k2 = (float)(kx*kx+ky*ky);
1239 if (k2==0) {
continue;}
1241 if (k2>0) phiK= jxjyatan2[ (ky+Mid-1)*End + kx+Mid-1]; phiK= atan2((
float)ky,(
float)kx);
1243 float fkR = fkVecR[Countkxy] ;
1244 float fkI = fkVecI[Countkxy] ;
1247 for (
int jCountqxy =0; jCountqxy<NK; jCountqxy++){
1248 int Countqxy =SortfkInds[jCountqxy] ;
1249 int qx = kVecX[Countqxy] ;
1250 int qy = kVecY[Countqxy] ;
1251 int q2 = qx*qx+qy*qy;
1252 if (q2==0) {
continue;}
1254 if (q2>0) phiQ = jxjyatan2[ (qy+Mid-1)*End + qx+Mid-1]; phiQ=atan2((
float)qy,(
float)qx);
1255 float fqR = fkVecR[Countqxy] ;
1256 float fqI = fkVecI[Countqxy] ;
1259 int kCIx = ((kCx+Mid+2*End)%End);
1260 int kCIy = ((kCy+Mid+2*End)%End);
1263 int CountCxy = kCIx*End+kCIy;
1264 float fCR = fkVecR[CountCxy];
1265 float fCI = fkVecI[CountCxy];
1267 printf(
"jCountqxy=%d , Countqxy=%d, absD1fkVec(Countqxy)=%f,qx=%d, qy=%d \n", jCountqxy, Countqxy, absD1fkVec[Countqxy],qx, qy);
1268 printf(
" CountCxy=%d,absD1fkVec[CountCxy]=%f, kCx=%d, kCy=%d \n",CountCxy, absD1fkVec[CountCxy], kCx, kCy );
1270 for (
int p=0; p<NK; p++){
1272 if (SortfkInds[p]==CountCxy){
1273 float Arg1 = 2.0f*M_PI*r1*
::sqrt((
float) q2)/End;
1274 float Arg2 = 2.0f*M_PI*r2*
::sqrt((
float) k2)/End;
1277 float bispectemp = (fkR*(fqR*fCR -fqI*fCI) -fkI*(fqI*fCR +fqR*fCI))
1278 * cos(N*(phiK-phiQ+M_PI));
1279 bispectemp -= (fkR*(fqR*fCI + fqI*fCR) +fkI*(fqR*fCR - fqI*fCI))
1280 * sin(N*(phiK-phiQ+M_PI));
1286 RotTransInvTemp = RotTransInvTemp + bispectemp * bess1*bess2 ;
1292 RotTransInv ->
set_value_at(jr1,jr2, RotTransInvTemp) ;
1298 return RotTransInv ;
1336 int Mid = (int) ((1+EndP)/2);
1339 int CountxyMax = End*End;
1342 int *kVecX =
new int[CountxyMax];
1343 int *kVecY =
new int[CountxyMax];
1344 float *fkVecR =
new float[CountxyMax];
1345 float *fkVecI =
new float[CountxyMax];
1346 float *absD1fkVec =
new float[CountxyMax];
1350 float *jxjyatan2 =
new float[End*End];
1355 for (
int jx=0; jx <End ; jx++) {
1356 for (
int jy=0; jy <End ; jy++) {
1359 jxjyatan2[jy*End + jx] = atan2((
float)(jy+1-Mid) , (
float)(jx +1 -Mid));
1365 fk ->process_inplace(
"xform.fourierorigin.tocenter");
1369 for (
int kEx= 0; kEx<2*Mid; kEx=kEx+2) {
1374 int kCIx = kCx+ Mid-1 ;
1375 for (
int kEy= 0 ; kEy<End; kEy++) {
1380 float absVal =
::sqrt(realVal*realVal+imagVal*imagVal);
1381 float fkAng = atan2(imagVal,realVal);
1395 AngMatlab = fkAng - 2.0f*M_PI*(kx +ky)*(Mid)/End;
1396 NewRealVal = absVal*cos(AngMatlab);
1397 NewImagVal = absVal*sin(AngMatlab);
1400 fkVecR[ kIy +kIx *End] = NewRealVal ;
1401 fkVecR[(End-1-kIy)+kCIx*End] = NewRealVal ;
1402 fkVecI[ kIy +kIx *End] = NewImagVal ;
1403 fkVecI[(End-1-kIy)+kCIx*End] = -NewImagVal ;
1404 absD1fkVec[(End-1-kIy) + kIx *End] = absVal;
1405 absD1fkVec[(End-1-kIy) + kCIx *End] = absVal;
1406 kVecX[kIy+kIx *End] = kx ;
1407 kVecX[kIy+kCIx *End] = kCx ;
1408 kVecY[kIy+kIx *End] = ky ;
1409 kVecY[kIy+kCIx *End] = ky ;
1433 int LradRange= (int) (1+floor(Mid/frR -.1)) ;
1435 float *radRange =
new float[LradRange];
1436 for (
int irad=0; irad < LradRange; irad++){
1437 radRange[irad] = frR*irad;
1441 cout <<
"Starting the calculation of invariants" << endl;
1445 int LthetaRange = 59;
1446 float ftR = (2.0f*M_PI/LthetaRange );
1447 float *thetaRange =
new float[LthetaRange];
1449 for (
int ith=0; ith < LthetaRange; ith++){
1450 thetaRange[ith] = ftR*ith; }
1452 int TotalVol = LradRange*LradRange*LthetaRange;
1454 float *RotTransInv =
new float[TotalVol];
1455 float *WeightInv =
new float[TotalVol];
1457 for (
int jW=0; jW<TotalVol; jW++) {
1458 RotTransInv[jW] = 0;
1462 for (
int jW=0; jW<TotalVol; jW++) {
1463 RotTransInv[jW] = 0;
1469 for (
int Countkxy =0; Countkxy<CountxyMax; Countkxy++){
1470 int kx = kVecX[Countkxy] ;
1471 int ky = kVecY[Countkxy] ;
1472 float k2 =
::sqrt((
float)(kx*kx+ky*ky));
1474 if (k2>0) phiK = jxjyatan2[ (ky+Mid-1)*End + kx+Mid-1];
1475 float fkR = fkVecR[(ky+Mid-1) + (kx+Mid-1) *End] ;
1476 float fkI = fkVecI[(ky+Mid-1) + (kx+Mid-1) *End] ;
1479 if ((k2==0)|| (k2>Mid) ) {
continue;}
1481 for (
int Countqxy =0; Countqxy<CountxyMax; Countqxy++){
1482 int qx = kVecX[Countqxy] ;
1483 int qy = kVecY[Countqxy] ;
1484 float q2 =
::sqrt((
float)(qx*qx+qy*qy));
1485 if ((q2==0)|| (q2>Mid) ) {
continue;}
1487 if (q2>0) phiQ = jxjyatan2[ (qy+Mid-1)*End + qx+Mid-1];
1488 float fqR = fkVecR[(qy+Mid-1) + (qx+Mid-1) *End] ;
1489 float fqI = fkVecI[(qy+Mid-1) + (qx+Mid-1) *End] ;
1492 int kCIx = ((kCx+Mid+2*End)%End);
1493 int kCIy = ((kCy+Mid+2*End)%End);
1494 kCx = ((kCIx+End-1)%End)+1-Mid;
1495 kCy = ((kCIy+End-1)%End)+1-Mid ;
1498 int CountCxy = (kCx+Mid-1)*End+(kCy+Mid-1);
1499 float fCR = fkVecR[CountCxy];
1500 float fCI = fkVecI[CountCxy];
1506 float phiQK = (4*M_PI+phiQ-phiK);
1507 while (phiQK> (2*M_PI)) phiQK -= (2*M_PI);
1511 float bispectemp = (fkR*(fqR*fCR -fqI*fCI) -fkI*(fqI*fCR +fqR*fCI));
1513 if ((q2<k2) )
continue;
1520 int k2IndLo = 0;
while ((k2>=radRange[k2IndLo+1]) && (k2IndLo+1 < LradRange ) ) k2IndLo +=1;
1521 int k2IndHi = k2IndLo;
1522 float k2Lo= radRange[k2IndLo];
1523 if (k2IndLo+1< LradRange) {
1524 k2IndHi = k2IndLo+1;
1528 float kCof =k2-k2Lo;
1530 int q2IndLo = 0;
while ((q2>=radRange[q2IndLo+1]) && (q2IndLo+1 < LradRange ) ) q2IndLo +=1;
1531 int q2IndHi=q2IndLo;
1532 float q2Lo= radRange[q2IndLo];
1533 if (q2IndLo+1 < LradRange) {
1534 q2IndHi = q2IndLo+1 ;
1536 float qCof = q2-q2Lo;
1538 if ((qCof<0) || (qCof >1) ) {
1539 cout<<
"Weird! qCof="<< qCof <<
" q2="<< q2 <<
" q2IndLo="<< q2IndLo << endl ;
1544 int thetaIndLo = 0;
while ((phiQK>=thetaRange[thetaIndLo+1])&& (thetaIndLo+1<LthetaRange)) thetaIndLo +=1;
1545 int thetaIndHi = thetaIndLo;
1547 float thetaLo = thetaRange[thetaIndLo];
1548 float thetaHi = thetaLo;
1551 if (thetaIndLo+1< LthetaRange) {
1552 thetaIndHi = thetaIndLo +1;
1557 thetaHi = thetaRange[thetaIndHi];
1559 if (thetaHi==thetaLo) {
1562 thetaCof = (phiQK-thetaLo)/(thetaHi-thetaLo);
1565 if ((thetaCof>2*M_PI) ) {
1566 cout<<
"Weird! thetaCof="<< thetaCof <<endl ;
1574 for (
int jk =1; jk<=2; jk++){
1575 for (
int jq =1; jq<=2; jq++){
1576 for (
int jtheta =1; jtheta<=2; jtheta++){
1578 float Weight = (kCof+(1-2*kCof)*(jk==1))*(qCof+(1-2*qCof)*(jq==1))
1579 * (thetaCof+(1-2*thetaCof)*(jtheta==1));
1582 int k2Ind = k2IndLo*(jk==1) + k2IndHi*(jk==2);
1583 int q2Ind = q2IndLo*(jq==1) + q2IndHi*(jq==2);
1584 int thetaInd = thetaIndLo*(jtheta==1) + thetaIndHi*(jtheta ==2);
1585 int TotalInd = thetaInd*LradRange*LradRange+q2Ind*LradRange+k2Ind;
1592 RotTransInv[TotalInd] += Weight*bispectemp;
1593 WeightInv[TotalInd] += Weight;
1599 cout <<
"Finished Main Section " << endl;
1603 cout <<
" LradRange " <<LradRange <<
" LthetaRange " << LthetaRange << endl;
1604 EMData *RotTransInvF =
new EMData(LradRange,LradRange,LthetaRange);
1605 EMData *WeightImage =
new EMData(LradRange,LradRange,LthetaRange);
1613 for (
int jtheta =0; jtheta < LthetaRange; jtheta++){
1614 for (
int jq =0; jq<LradRange; jq++){
1615 for (
int jk =0; jk<LradRange ; jk++){
1617 int TotalInd = jtheta*LradRange*LradRange+jq*LradRange+jk;
1618 float Weight = WeightInv[TotalInd];
1621 if (Weight <= 0)
continue;
1622 RotTransInvF ->
set_value_at(jk,jq,jtheta,RotTransInv[TotalInd] / Weight);
1623 int newjtheta = (LthetaRange- jtheta)%LthetaRange;
1624 RotTransInvF ->
set_value_at(jq,jk,newjtheta,RotTransInv[TotalInd]/Weight );
1629 cout <<
" Almost Done " << endl;
1631 _unlink(
"WeightImage.???");
1634 rslt = system(
"rm -f WeightImage.???"); rslt++;
1638 return RotTransInvF ;
1642 int TotalVol = LradRange*LradRange;
1644 float *RotTransInv =
new float[TotalVol];
1645 float *WeightInv =
new float[TotalVol];
1647 for (
int jW=0; jW<TotalVol; jW++) {
1648 RotTransInv[jW] = 0;
1653 for (
int Countkxy =0; Countkxy<CountxyMax; Countkxy++){
1654 int kx = kVecX[Countkxy] ;
1655 int ky = kVecY[Countkxy] ;
1656 float k2 =
::sqrt((
float)(kx*kx+ky*ky));
1657 float fkR = fkVecR[(ky+Mid-1) + (kx+Mid-1) *End] ;
1658 float fkI = fkVecI[(ky+Mid-1) + (kx+Mid-1) *End] ;
1661 if ((k2==0)|| (k2>Mid) ) {
continue;}
1663 for (
int Countqxy =0; Countqxy<CountxyMax; Countqxy++){
1666 int qx = kVecX[Countqxy] ;
1667 int qy = kVecY[Countqxy] ;
1668 float q2 =
::sqrt((
float)(qx*qx+qy*qy));
1669 if ((q2==0)|| (q2>Mid) ) {
continue;}
1670 if ((q2<k2) )
continue;
1672 float fqR = fkVecR[(qy+Mid-1) + (qx+Mid-1) *End] ;
1673 float fqI = fkVecI[(qy+Mid-1) + (qx+Mid-1) *End] ;
1677 int kCIx = ((kCx+Mid+2*End)%End);
1678 int kCIy = ((kCy+Mid+2*End)%End);
1679 kCx = ((kCIx+End-1)%End)+1-Mid;
1680 kCy = ((kCIy+End-1)%End)+1-Mid ;
1683 int CountCxy = (kCx+Mid-1)*End+(kCy+Mid-1);
1684 float fCR = fkVecR[CountCxy];
1685 float fCI = fkVecI[CountCxy];
1688 float bispectemp = (fkR*(fqR*fCR -fqI*fCI) -fkI*(fqI*fCR +fqR*fCI));
1691 int k2IndLo = 0;
while ((k2>=radRange[k2IndLo+1]) && (k2IndLo+1 < LradRange ) ) k2IndLo +=1;
1692 int k2IndHi = k2IndLo;
1693 float k2Lo= radRange[k2IndLo];
1694 if (k2IndLo+1< LradRange) {
1695 k2IndHi = k2IndLo+1;
1699 float kCof =k2-k2Lo;
1701 int q2IndLo = 0;
while ((q2>=radRange[q2IndLo+1]) && (q2IndLo+1 < LradRange ) ) q2IndLo +=1;
1702 int q2IndHi=q2IndLo;
1703 float q2Lo= radRange[q2IndLo];
1704 if (q2IndLo+1 < LradRange) {
1705 q2IndHi = q2IndLo+1 ;
1707 float qCof = q2-q2Lo;
1710 for (
int jk =1; jk<=2; jk++){
1711 for (
int jq =1; jq<=2; jq++){
1713 float Weight = (kCof+(1-2*kCof)*(jk==1))*(qCof+(1-2*qCof)*(jq==1));
1715 int k2Ind = k2IndLo*(jk==1) + k2IndHi*(jk==2);
1716 int q2Ind = q2IndLo*(jq==1) + q2IndHi*(jq==2);
1717 int TotalInd = q2Ind*LradRange+k2Ind;
1718 RotTransInv[TotalInd] += Weight*bispectemp;
1719 WeightInv[TotalInd] += Weight;
1729 EMData *RotTransInvF =
new EMData(LradRange,LradRange);
1732 for (
int jk =0; jk<LradRange ; jk++){
1733 for (
int jq =jk; jq<LradRange; jq++){
1734 int TotalInd = jq*LradRange+jk;
1735 int TotalIndBar = jq*LradRange+jk;
1736 float Weight = WeightInv[TotalInd] + WeightInv[TotalIndBar];
1737 if (Weight <=0)
continue;
1740 float ValNow = cbrt( (RotTransInv[TotalInd] + RotTransInv[TotalIndBar]) / Weight ) ;
1746 _unlink(
"WeightImage.???");
1749 rslt = system(
"rm -f WeightImage.???"); rslt++;
1753 return RotTransInvF ;
1760 int nx1 = block->get_xsize();
1761 int ny1 = block->get_ysize();
1762 int nz1 = block->get_zsize();
1764 Region area(origin[0], origin[1], origin[2],nx1, ny1, nz1);
1767 int x0 = (int) area.origin[0];
1768 x0 = x0 < 0 ? 0 : x0;
1770 int y0 = (
int) area.origin[1];
1771 y0 = y0 < 0 ? 0 : y0;
1773 int z0 = (int) area.origin[2];
1774 z0 = z0 < 0 ? 0 : z0;
1776 int x1 = (
int) (area.origin[0] + area.size[0]);
1777 x1 = x1 >
nx ?
nx : x1;
1779 int y1 = (int) (area.origin[1] + area.size[1]);
1780 y1 = y1 >
ny ?
ny : y1;
1782 int z1 = (int) (area.origin[2] + area.size[2]);
1783 z1 = z1 >
nz ?
nz : z1;
1788 int xd0 = (int) (area.origin[0] < 0 ? -area.origin[0] : 0);
1789 int yd0 = (int) (area.origin[1] < 0 ? -area.origin[1] : 0);
1790 int zd0 = (int) (area.origin[2] < 0 ? -area.origin[2] : 0);
1792 if (x1 < x0 || y1 < y0 || z1 < z0)
return;
1794 size_t clipped_row_size = (x1-x0) *
sizeof(
float);
1795 size_t src_secsize = (size_t)(nx1 * ny1);
1796 size_t dst_secsize = (size_t)(
nx *
ny);
1819 float *src = block->get_data() + (size_t)zd0 * (
size_t)src_secsize + (size_t)yd0 * (
size_t)nx1 + (size_t)xd0;
1820 float *dst =
get_data() + (size_t)z0 * (
size_t)dst_secsize + (size_t)y0 * (
size_t)
nx + (size_t)x0;
1822 size_t src_gap = src_secsize - (y1-y0) * nx1;
1823 size_t dst_gap = dst_secsize - (y1-y0) *
nx;
1825 for (
int i = z0; i < z1; i++) {
1826 for (
int j = y0; j < y1; j++) {
1835#ifdef EMAN2_USING_CUDA
1836 if(block->cudarwdata){
1847 float scale,
float mult_factor)
1853 int xs=(int)floor(block->get_xsize()*
scale/2.0);
1854 int ys=(int)floor(block->get_ysize()*
scale/2.0);
1855 int zs=(int)floor(block->get_zsize()*
scale/2.0);
1856 int x0=(int)center[0]-xs;
1857 int x1=(int)center[0]+xs;
1858 int y0=(int)center[1]-ys;
1859 int y1=(int)center[1]+ys;
1860 int z0=(int)center[2]-zs;
1861 int z1=(int)center[2]+zs;
1873 float bx=block->get_xsize()/2.0f;
1874 float by=block->get_ysize()/2.0f;
1875 float bz=block->get_zsize()/2.0f;
1878 for (
int x=x0;
x<=x1;
x++) {
1879 for (
int y=y0;
y<=y1;
y++) {
1880 for (
int z=z0; z<=z1; z++) {
1881 idx =
x +
y *
nx + (size_t)z *
nx *
ny;
1884 mult_factor*block->sget_value_at((
x-center[0])+bx,(
y-center[1])+by,(z-center[2])+bz);
1887 mult_factor*block->sget_value_at_interp((
x-center[0])/
scale+bx,(
y-center[1])/
scale+by,(z-center[2])/
scale+bz);
1896 int xs=(int)floor(block->get_xsize()*
scale/2.0);
1897 int ys=(int)floor(block->get_ysize()*
scale/2.0);
1898 int x0=(int)center[0]-xs;
1899 int x1=(int)center[0]+xs;
1900 int y0=(int)center[1]-ys;
1901 int y1=(int)center[1]+ys;
1911 float bx=block->get_xsize()/2.0f;
1912 float by=block->get_ysize()/2.0f;
1914 for (
int x=x0;
x<=x1;
x++) {
1915 for (
int y=y0;
y<=y1;
y++) {
1917 mult_factor*block->sget_value_at_interp((
x-center[0])/
scale+bx,(
y-center[1])/
scale+by);
1923 LOGERR(
"insert_scaled_sum supports only 2D and 3D data");
EMData stores an image's data and defines core image processing routines.
void scale(float scale_factor)
scale the image by a factor.
EMData()
For all image I/O.
float * rdata
image real data
Dict attr_dict
to store all image header info
int flags
CTF data All CTF data become attribute ctf(vector<float>) in attr_dict –Grant Tang.
static void em_memcpy(void *dst, const void *const src, const size_t size)
FloatPoint defines a float-coordinate point in a 1D/2D/3D space.
IntPoint defines an integer-coordinate point in a 1D/2D/3D space.
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
static void ap2ri(float *data, size_t n)
convert complex data array from Amplitude/Phase format into Real/Imaginary format.
int cuda_dd_fft_real_to_complex_nd(float *real_data, float *complex_data, int nx, int ny, int nz, int batch)
int cuda_dd_fft_complex_to_real_nd(float *complex_data, float *real_data, int nx, int ny, int nz, int batch)
EMData * copy() const
This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata....
void mult(int n)
multiply an integer number to each pixel value of the image.
void set_value_at(Vec3i loc, float val)
set_value_at with Vec3i
EMData * sqrt() const
return square root of current image
float get_value_at(int x, int y, int z) const
Get the pixel density value at coordinates (x,y,z).
EMData * copy_head() const
Make an image with a copy of the current image's header.
void write_image(const string &filename, int img_index=0, EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN, bool header_only=false, const Region *region=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)
write the header and data out to an image.
#define ImageFormatException(desc)
#define ImageDimensionException(desc)
#define UnexpectedBehaviorException(desc)
void insert_clip(const EMData *const block, const IntPoint &origin)
Insert a clip into this image.