emdata_transform.cpp

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #include "emdata.h"
00037 #include "emfft.h"
00038 
00039 #include <cstring>
00040 #include <cstdio>
00041 
00042 #include  "gsl_sf_result.h"
00043 #include  "gsl_sf_bessel.h"
00044 #include <iostream>
00045 #include <algorithm>
00046 #include <vector>
00047 #include <utility>
00048 #include <cmath>
00049 #include "util.h"
00050 
00051 //#ifdef EMAN2_USING_CUDA
00052 //#include "cuda/cuda_processor.h"
00053 //#endif
00054 
00055 using namespace EMAN;
00056 using namespace std;
00057 typedef vector< pair<float,int> > vp;
00058 
00059 EMData *EMData::do_fft() const
00060 {
00061         ENTERFUNC;
00062 
00063         if (is_complex() ) { // ming add 08/17/2010
00064 #ifdef NATIVE_FFT
00065                 LOGERR(" NATIVE_FFT not supported yet.");
00066                 throw ImageFormatException("real image expected. Input image is complex image.");
00067                 exit;
00068 #endif // NATIVE_FFT
00069 
00070                         EMData *temp_in=copy();
00071                         EMData *dat= copy_head();
00072                         int offset;
00073                         if(is_fftpadded()) {
00074                                 offset = is_fftodd() ? 1 : 2;
00075                         }
00076                         else offset=0;
00077                         //printf("offset=%d\n",offset);
00078                         EMfft::complex_to_complex_nd(temp_in->get_data(),dat->get_data(),nx-offset,ny,nz);
00079 
00080                         if(dat->get_ysize()==1 && dat->get_zsize()==1) dat->set_complex_x(true);
00081 
00082                         dat->update();
00083                         delete temp_in;
00084                         EXITFUNC;
00085                         return dat;
00086                 }
00087 
00088         else{
00089         int nxreal = nx;
00090         int offset = 2 - nx%2;
00091         int nx2 = nx + offset;
00092         EMData* dat = copy_head();
00093         dat->set_size(nx2, ny, nz);
00094         //dat->to_zero();  // do not need it, real_to_complex will do it right anyway
00095         if (offset == 1) dat->set_fftodd(true);
00096         else             dat->set_fftodd(false);
00097 
00098         float *d = dat->get_data();
00099         //std::cout<<" do_fft "<<rdata[5]<<"  "<<d[5]<<std::endl;
00100         EMfft::real_to_complex_nd(get_data(), d, nxreal, ny, nz);
00101 
00102         dat->update();
00103         dat->set_fftpad(true);
00104         dat->set_complex(true);
00105         if(dat->get_ysize()==1 && dat->get_zsize()==1) dat->set_complex_x(true);
00106         dat->set_ri(true);
00107 
00108         EXITFUNC;
00109         return dat;
00110         }
00111 }
00112 
00113 EMData *EMData::do_fft_inplace()
00114 {
00115         ENTERFUNC;
00116 
00117         if ( is_complex() ) {
00118                 LOGERR("real image expected. Input image is complex image.");
00119                 throw ImageFormatException("real image expected. Input image is complex image.");
00120         }
00121 
00122         size_t offset;
00123         int nxreal;
00124         get_data(); // Required call if GPU caching is being used. Otherwise harmless
00125         if (!is_fftpadded()) {
00126                 // need to extend the matrix along x
00127                 // meaning nx is the un-fftpadded size
00128                 nxreal = nx;
00129                 offset = 2 - nx%2;
00130                 if (1 == offset) set_fftodd(true);
00131                 else             set_fftodd(false);
00132                 int nxnew = nx + offset;
00133                 set_size(nxnew, ny, nz);
00134                 for (int iz = nz-1; iz >= 0; iz--) {
00135                         for (int iy = ny-1; iy >= 0; iy--) {
00136                                 for (int ix = nxreal-1; ix >= 0; ix--) {
00137                                         size_t oldxpos = ix + (iy + iz*ny)*(size_t)nxreal;
00138                                         size_t newxpos = ix + (iy + iz*ny)*(size_t)nxnew;
00139                                         (*this)(newxpos) = (*this)(oldxpos);
00140                                 }
00141                         }
00142                 }
00143                 set_fftpad(true);
00144         } else {
00145                 offset = is_fftodd() ? 1 : 2;
00146                 nxreal = nx - offset;
00147         }
00148         EMfft::real_to_complex_nd(rdata, rdata, nxreal, ny, nz);
00149 
00150         set_complex(true);
00151         if(ny==1 && nz==1)  set_complex_x(true);
00152         set_ri(true);
00153 
00154         update();
00155 
00156         EXITFUNC;
00157         return this;
00158 }
00159 
00160 #ifdef EMAN2_USING_CUDA
00161 
00162 #include "cuda/cuda_emfft.h"
00163 
00164 EMData *EMData::do_fft_cuda()
00165 {
00166         ENTERFUNC;
00167 
00168         if ( is_complex() ) {
00169                 LOGERR("real image expected. Input image is complex image.");
00170                 throw ImageFormatException("real image expected. Input image is complex image.");
00171         }
00172 
00173         int offset = 2 - nx%2;
00174         EMData* dat = new EMData(0,0,nx+offset,ny,nz,attr_dict);
00175         if(!dat->rw_alloc()) throw ImageFormatException("Couldn't allocate memory.");
00176         //cout << "Doing CUDA FFT " << cudarwdata << endl;
00177         if(cudarwdata == 0){copy_to_cuda();}
00178         cuda_dd_fft_real_to_complex_nd(cudarwdata, dat->cudarwdata, nx, ny,nz, 1);
00179 
00180         if (offset == 1) dat->set_fftodd(true);
00181         else             dat->set_fftodd(false);
00182 
00183         dat->set_fftpad(true);
00184         dat->set_complex(true);
00185         if(dat->get_ysize()==1 && dat->get_zsize()==1) dat->set_complex_x(true);
00186         dat->set_ri(true);
00187 //      dat->gpu_update();
00188 
00189         EXITFUNC;
00190         return dat;
00191 }
00192 
00193 EMData *EMData::do_fft_inplace_cuda()
00194 {
00195         ENTERFUNC;
00196 
00197         if ( is_complex() ) {
00198                 LOGERR("real image expected. Input image is complex image.");
00199                 throw ImageFormatException("real image expected. Input image is complex image.");
00200         }
00201 
00202         int offset = 2 - nx%2;
00203         float* tempcudadata = 0;
00204         cudaError_t error = cudaMalloc((void**)&tempcudadata,(nx + offset)*ny*nz*sizeof(float));
00205         if( error != cudaSuccess) throw ImageFormatException("Couldn't allocate memory.");
00206         
00207         //cout << "Doing CUDA FFT inplace" << cudarwdata << endl;
00208         if(cudarwdata == 0){copy_to_cuda();}
00209         cuda_dd_fft_real_to_complex_nd(cudarwdata, tempcudadata, nx, ny,nz, 1);
00210         // this section is a bit slight of hand it actually does the FFT out of place but this avoids and EMData object creation and detruction...
00211         cudaError_t ferror = cudaFree(cudarwdata);
00212         if ( ferror != cudaSuccess) throw UnexpectedBehaviorException( "CudaFree failed:" + string(cudaGetErrorString(error)));
00213         cudarwdata = tempcudadata;
00214         num_bytes = (nx + offset)*ny*nz*sizeof(float);
00215 
00216         if (offset == 1) set_fftodd(true);
00217         else             set_fftodd(false);
00218 
00219         nx = nx + offset; // don't want to call set_size b/c that will delete my cudadata, remember what I am doing is a bit slignt of hand....
00220         set_fftpad(true);
00221         set_complex(true);
00222         if(get_ysize()==1 && get_zsize()==1) set_complex_x(true);
00223         set_ri(true);
00224 //      dat->gpu_update();
00225         //perhaps is should call update() ????
00226 
00227         EXITFUNC;
00228         return this;
00229 }
00230 
00231 EMData *EMData::do_ift_cuda()
00232 {
00233         ENTERFUNC;
00234 
00235         if (!is_complex()) {
00236                 LOGERR("complex image expected. Input image is real image.");
00237                 throw ImageFormatException("complex image expected. Input image is real image.");
00238         }
00239 
00240         if (!is_ri()) {
00241                 throw ImageFormatException("complex ri expected. Got amplitude/phase.");
00242         }
00243 
00244         int offset = is_fftodd() ? 1 : 2;
00245         EMData* dat = new EMData(0,0,nx-offset,ny,nz);
00246         if(!dat->rw_alloc()) throw ImageFormatException("Couldn't allocate memory.");
00247         
00248         if(cudarwdata == 0){copy_to_cuda();}
00249 
00250         
00251         int ndim = get_ndim();
00252         if ( ndim == 1 ) {
00253                 cuda_dd_fft_complex_to_real_nd(cudarwdata,dat->cudarwdata, nx-offset,1,1,1);
00254         } else if (ndim == 2) {
00255                 cuda_dd_fft_complex_to_real_nd(cudarwdata,dat->cudarwdata, ny,nx-offset,1,1);
00256         } else if (ndim == 3) {
00257                 cuda_dd_fft_complex_to_real_nd(cudarwdata,dat->cudarwdata, nz,ny,nx-offset,1);
00258         } else throw ImageDimensionException("No cuda FFT support of images with dimensions exceeding 3");
00259         
00260         // SCALE the inverse FFT
00261         float scale = 1.0f/static_cast<float>((dat->get_size()));
00262         dat->mult(scale); 
00263 
00264         dat->set_fftpad(false);
00265         dat->set_fftodd(false);
00266         dat->set_complex(false);
00267         if(dat->get_ysize()==1 && dat->get_zsize()==1)  dat->set_complex_x(false);
00268         dat->set_ri(false);
00269 //      dat->gpu_update();
00270 //      dat->update(); // this is a hack to make diaplay work(will fix in future)
00271         
00272         EXITFUNC;
00273         return dat;
00274 }
00275 
00276 /*
00277    FFT in place does not depad, hence this routine is of limited use b/c mem operations on the device are quite SLOW, JFF
00278    use
00279 */
00280 
00281 EMData *EMData::do_ift_inplace_cuda()
00282 {
00283         ENTERFUNC;
00284 
00285         if (!is_complex()) {
00286                 LOGERR("complex image expected. Input image is real image.");
00287                 throw ImageFormatException("complex image expected. Input image is real image.");
00288         }
00289 
00290         if (!is_ri()) {
00291                 LOGWARN("run IFT on AP data, only RI should be used. ");
00292         }
00293 
00294         int offset = is_fftodd() ? 1 : 2;
00295         
00296         if(cudarwdata == 0){copy_to_cuda();}
00297         
00298         int ndim = get_ndim();
00299         if ( ndim == 1 ) {
00300                 cuda_dd_fft_complex_to_real_nd(cudarwdata,cudarwdata, nx-offset,1,1,1);
00301         } else if (ndim == 2) {
00302                 cuda_dd_fft_complex_to_real_nd(cudarwdata,cudarwdata, ny,nx-offset,1,1);
00303         } else if (ndim == 3) {
00304                 cuda_dd_fft_complex_to_real_nd(cudarwdata,cudarwdata, nz,ny,nx-offset,1);
00305         } else throw ImageDimensionException("No cuda FFT support of images with dimensions exceeding 3");
00306 #if defined     FFTW2 || defined FFTW3 //native fft and ACML already done normalization
00307         // SCALE the inverse FFT
00308         int nxo = nx - offset;
00309         float scale = 1.0f / (nxo * ny * nz);
00310         mult(scale); //if we are just doing a CCF, this is a waste!
00311 #endif //FFTW2 || FFTW3
00312 
00313         set_fftpad(true);
00314         set_complex(false);
00315 
00316         if(ny==1 && nz==1) set_complex_x(false);
00317         set_ri(false);
00318         //update();
00319         
00320         EXITFUNC;
00321         return this;
00322 }
00323 
00324 #endif //EMAN2_USING_CUDA
00325 
00326 EMData *EMData::do_ift()
00327 {
00328         ENTERFUNC;
00329 
00330         if (!is_complex()) {
00331                 LOGERR("complex image expected. Input image is real image.");
00332                 throw ImageFormatException("complex image expected. Input image is real image.");
00333         }
00334 
00335         if (!is_ri()) {
00336                 LOGWARN("run IFT on AP data, only RI should be used. Converting.");
00337         }
00338 
00339         get_data(); // Required call if GPU caching is being used. Otherwise harmless
00340         EMData* dat = copy_head();
00341         dat->set_size(nx, ny, nz);
00342         ap2ri();
00343 
00344         float *d = dat->get_data();
00345         int ndim = get_ndim();
00346 
00347         /* Do inplace IFT on a image copy, because the complex to real transform of
00348          * nd will destroy its input array even for out-of-place transforms.
00349          */
00350         memcpy((char *) d, (char *) rdata, nx * ny * nz * sizeof(float));
00351 
00352         int offset = is_fftodd() ? 1 : 2;
00353         //cout << "Sending offset " << offset << " " << nx-offset << endl;
00354         if (ndim == 1) {
00355                 EMfft::complex_to_real_nd(d, d, nx - offset, ny, nz);
00356         } else {
00357                 EMfft::complex_to_real_nd(d, d, nx - offset, ny, nz);
00358 
00359                 size_t row_size = (nx - offset) * sizeof(float);
00360                 for (int i = 1; i < ny * nz; i++) {
00361                         memmove((char *) &d[i * (nx - offset)], (char *) &d[i * nx], row_size);
00362                 }
00363 
00364         }
00365 
00366         dat->set_size(nx - offset, ny, nz);     //remove the padding
00367 #if defined     FFTW2 || defined FFTW3 //native fft and ACML already done normalization
00368         // SCALE the inverse FFT
00369         float scale = 1.0f / ((nx - offset) * ny * nz);
00370         dat->mult(scale);
00371 #endif  //FFTW2 || FFTW3
00372         dat->set_fftodd(false);
00373         dat->set_fftpad(false);
00374         dat->set_complex(false);
00375         if(dat->get_ysize()==1 && dat->get_zsize()==1)  dat->set_complex_x(false);
00376         dat->set_ri(false);
00377         dat->update();
00378 
00379 
00380         EXITFUNC;
00381         return dat;
00382 }
00383 
00384 /*
00385    FFT in place does not depad, return real x-extended image (needs to be depadded before use as PAP does in CCF routines)
00386    use
00387 */
00388 EMData *EMData::do_ift_inplace()
00389 {
00390         ENTERFUNC;
00391 
00392         if (!is_complex()) {
00393                 LOGERR("complex image expected. Input image is real image.");
00394                 throw ImageFormatException("complex image expected. Input image is real image.");
00395         }
00396 
00397         if (!is_ri()) {
00398                 LOGWARN("run IFT on AP data, only RI should be used. ");
00399         }
00400         ap2ri();
00401 
00402         int offset = is_fftodd() ? 1 : 2;
00403         float* data = get_data();
00404         EMfft::complex_to_real_nd(data, data, nx - offset, ny, nz);
00405 
00406 #if defined     FFTW2 || defined FFTW3  //native fft and ACML already done normalization
00407         // SCALE the inverse FFT
00408         int nxo = nx - offset;
00409         float scale = 1.0f / ((size_t)nxo * ny * nz);
00410         mult(scale);
00411 #endif //FFTW2 || FFTW3
00412 
00413         set_fftpad(true);
00414         set_complex(false);
00415         if(ny==1 && nz==1) set_complex_x(false);
00416         set_ri(false);
00417         update();
00418 
00419         EXITFUNC;
00420         return this;
00421 }
00422 #undef rdata
00423 
00424 
00425 std::string EMData::render_ap24(int x0, int y0, int ixsize, int iysize,
00426                                                  int bpl, float scale, int mingray, int maxgray,
00427                                                  float render_min, float render_max,float gamma,int flags)
00428 {
00429         ENTERFUNC;
00430 
00431         int asrgb;
00432         int hist=(flags&2)/2;
00433         int invy=(flags&4)?1:0;
00434 
00435         if (!is_complex()) throw ImageDimensionException("complex only");
00436 
00437         if (get_ndim() != 2) {
00438                 throw ImageDimensionException("2D only");
00439         }
00440 
00441         if (is_complex()) ri2ap();
00442 
00443         if (render_max <= render_min) {
00444                 render_max = render_min + 0.01f;
00445         }
00446 
00447         if (gamma<=0) gamma=1.0;
00448 
00449         // Calculating a full floating point gamma for
00450         // each pixel in the image slows rendering unacceptably
00451         // however, applying a gamma-mapping to an 8 bit colorspace
00452         // has unaccepable accuracy. So, we oversample the 8 bit colorspace
00453         // as a 12 bit colorspace and apply the gamma mapping to that
00454         // This should produce good accuracy for gamma values
00455         // larger than 0.5 (and a high upper limit)
00456         static int smg0=0,smg1=0;       // while this destroys threadsafety in the rendering process
00457         static float sgam=0;            // it is necessary for speed when rendering large numbers of small images
00458         static unsigned char gammamap[4096];
00459         if (gamma!=1.0 && (smg0!=mingray || smg1!=maxgray || sgam!=gamma)) {
00460                 for (int i=0; i<4096; i++) {
00461                         if (mingray<maxgray) gammamap[i]=(unsigned char)(mingray+(maxgray-mingray+0.999)*pow(((float)i/4096.0f),gamma));
00462                         else gammamap[4095-i]=(unsigned char)(mingray+(maxgray-mingray+0.999)*pow(((float)i/4096.0f),gamma));
00463                 }
00464         }
00465         smg0=mingray;   // so we don't recompute the map unless something changes
00466         smg1=maxgray;
00467         sgam=gamma;
00468 
00469         if (flags&8) asrgb=4;
00470         else if (flags&1) asrgb=3;
00471         else throw ImageDimensionException("must set flag 1 or 8");
00472 
00473         std::string ret=std::string();
00474 //      ret.resize(iysize*bpl);
00475         ret.assign(iysize*bpl+hist*1024,char(mingray));
00476         unsigned char *data=(unsigned char *)ret.data();
00477         unsigned int *histd=(unsigned int *)(data+iysize*bpl);
00478         if (hist) {
00479                 for (int i=0; i<256; i++) histd[i]=0;
00480         }
00481 
00482         float rm = render_min;
00483         float inv_scale = 1.0f / scale;
00484         int ysize = iysize;
00485         int xsize = ixsize;
00486 
00487         int ymin = 0;
00488         if (iysize * inv_scale > ny) {
00489                 ymin = (int) (iysize - ny / inv_scale);
00490         }
00491 
00492         float gs = (maxgray - mingray) / (render_max - render_min);
00493         float gs2 = 4095.999f / (render_max - render_min);
00494 //      float gs2 = 1.0 / (render_max - render_min);
00495         if (render_max < render_min) {
00496                 gs = 0;
00497                 rm = FLT_MAX;
00498         }
00499 
00500         int dsx = -1;
00501         int dsy = 0;
00502         int remx = 0;
00503         int remy = 0;
00504         const int scale_n = 100000;
00505 
00506         int addi = 0;
00507         int addr = 0;
00508         if (inv_scale == floor(inv_scale)) {
00509                 dsx = (int) inv_scale;
00510                 dsy = (int) (inv_scale * nx);
00511         }
00512         else {
00513                 addi = (int) floor(inv_scale);
00514                 addr = (int) (scale_n * (inv_scale - floor(inv_scale)));
00515         }
00516 
00517         int xmin = 0;
00518         if (x0 < 0) {
00519                 xmin = (int) (-x0 / inv_scale);
00520                 xsize -= (int) floor(x0 / inv_scale);
00521                 x0 = 0;
00522         }
00523 
00524         if ((xsize - xmin) * inv_scale > (nx - x0)) {
00525                 xsize = (int) ((nx - x0) / inv_scale + xmin);
00526         }
00527         int ymax = ysize - 1;
00528         if (y0 < 0) {
00529                 ymax = (int) (ysize + y0 / inv_scale - 1);
00530                 ymin += (int) floor(y0 / inv_scale);
00531                 y0 = 0;
00532         }
00533 
00534         if (xmin < 0) xmin = 0;
00535         if (ymin < 0) ymin = 0;
00536         if (xsize > ixsize) xsize = ixsize;
00537         if (ymax > iysize) ymax = iysize;
00538 
00539         int lmax = nx * ny - 1;
00540 
00541         int mid=nx*ny/2;
00542         float* image_data = get_data();
00543         if (dsx != -1) {
00544                 int l = y0 * nx;
00545                 for (int j = ymax; j >= ymin; j--) {
00546                         int ll = x0;
00547                         for (int i = xmin; i < xsize; i++) {
00548                                 if (l + ll > lmax || ll >= nx - 2) break;
00549 
00550                                 int k = 0;
00551                                 unsigned char p;
00552                                 int ph;
00553                                 if (ll >= nx / 2) {
00554                                         if (l >= (ny - inv_scale) * nx) k = 2 * (ll - nx / 2) + 2;
00555                                         else k = 2 * (ll - nx / 2) + l + 2 + nx;
00556                                         ph = (int)(image_data[k+1]*768/(2.0*M_PI))+384; // complex phase as integer 0-767
00557                                 }
00558                                 else {
00559                                         k = nx * ny - (l + 2 * ll) - 2;
00560                                         ph = (int)(-image_data[k+1]*768/(2.0*M_PI))+384;        // complex phase as integer 0-767
00561                                 }
00562                                 if (k>=mid) k-=mid;             // These 2 lines handle the Fourier origin being in the corner, not the middle
00563                                 else k+=mid;
00564                                 float t = image_data[k];
00565                                 if (t <= rm)  p = mingray;
00566                                 else if (t >= render_max) p = maxgray;
00567                                 else if (gamma!=1.0) {
00568                                         k=(int)(gs2 * (t-render_min));          // map float value to 0-4096 range
00569                                         p = gammamap[k];                                        // apply gamma using precomputed gamma map
00570                                 }
00571                                 else {
00572                                         p = (unsigned char) (gs * (t - render_min));
00573                                         p += mingray;
00574                                 }
00575                                 if (ph<256) {
00576                                         data[i * asrgb + j * bpl] = p*(255-ph)/256;
00577                                         data[i * asrgb + j * bpl+1] = p*ph/256;
00578                                         data[i * asrgb + j * bpl+2] = 0;
00579                                 }
00580                                 else if (ph<512) {
00581                                         data[i * asrgb + j * bpl+1] = p*(511-ph)/256;
00582                                         data[i * asrgb + j * bpl+2] = p*(ph-256)/256;
00583                                         data[i * asrgb + j * bpl] = 0;
00584                                 }
00585                                 else {
00586                                         data[i * asrgb + j * bpl+2] = p*(767-ph)/256;
00587                                         data[i * asrgb + j * bpl] = p*(ph-512)/256;
00588                                         data[i * asrgb + j * bpl+1] = 0;
00589                                 }
00590                                 if (hist) histd[p]++;
00591                                 ll += dsx;
00592                         }
00593                         l += dsy;
00594                 }
00595         }
00596         else {
00597                 remy = 10;
00598                 int l = y0 * nx;
00599                 for (int j = ymax; j >= ymin; j--) {
00600                         int br = l;
00601                         remx = 10;
00602                         int ll = x0;
00603                         for (int i = xmin; i < xsize - 1; i++) {
00604                                 if (l + ll > lmax || ll >= nx - 2) {
00605                                         break;
00606                                 }
00607                                 int k = 0;
00608                                 unsigned char p;
00609                                 int ph;
00610                                 if (ll >= nx / 2) {
00611                                         if (l >= (ny * nx - nx)) k = 2 * (ll - nx / 2) + 2;
00612                                         else k = 2 * (ll - nx / 2) + l + 2 + nx;
00613                                         ph = (int)(image_data[k+1]*768/(2.0*M_PI))+384; // complex phase as integer 0-767
00614                                 }
00615                                 else {
00616                                         k = nx * ny - (l + 2 * ll) - 2;
00617                                         ph = (int)(-image_data[k+1]*768/(2.0*M_PI))+384;        // complex phase as integer 0-767
00618                                 }
00619                                 if (k>=mid) k-=mid;             // These 2 lines handle the Fourier origin being in the corner, not the middle
00620                                 else k+=mid;
00621 
00622                                 float t = image_data[k];
00623                                 if (t <= rm)
00624                                         p = mingray;
00625                                 else if (t >= render_max) {
00626                                         p = maxgray;
00627                                 }
00628                                 else if (gamma!=1.0) {
00629                                         k=(int)(gs2 * (t-render_min));          // map float value to 0-4096 range
00630                                         p = gammamap[k];                                        // apply gamma using precomputed gamma map
00631                                 }
00632                                 else {
00633                                         p = (unsigned char) (gs * (t - render_min));
00634                                         p += mingray;
00635                                 }
00636                                 if (ph<256) {
00637                                         data[i * asrgb + j * bpl] = p*(255-ph)/256;
00638                                         data[i * asrgb + j * bpl+1] = p*ph/256;
00639                                         data[i * asrgb + j * bpl+2] = 0;
00640                                 }
00641                                 else if (ph<512) {
00642                                         data[i * asrgb + j * bpl+1] = p*(511-ph)/256;
00643                                         data[i * asrgb + j * bpl+2] = p*(ph-256)/256;
00644                                         data[i * asrgb + j * bpl] = 0;
00645                                 }
00646                                 else {
00647                                         data[i * asrgb + j * bpl+2] = p*(767-ph)/256;
00648                                         data[i * asrgb + j * bpl] = p*(ph-512)/256;
00649                                         data[i * asrgb + j * bpl+1] = 0;
00650                                 }
00651                                 if (hist) histd[p]++;
00652                                 ll += addi;
00653                                 remx += addr;
00654                                 if (remx > scale_n) {
00655                                         remx -= scale_n;
00656                                         ll++;
00657                                 }
00658                         }
00659                         l = br + addi * nx;
00660                         remy += addr;
00661                         if (remy > scale_n) {
00662                                 remy -= scale_n;
00663                                 l += nx;
00664                         }
00665                 }
00666         }
00667 
00668         // this replicates r -> g,b
00669         if (asrgb==4) {
00670                 for (int j=ymin*bpl; j<=ymax*bpl; j+=bpl) {
00671                         for (int i=xmin; i<xsize*4; i+=4) {
00672                                 data[i+j+3]=255;
00673                         }
00674                 }
00675         }
00676 
00677         EXITFUNC;
00678 
00679         // ok, ok, not the most efficient place to do this, but it works
00680         if (invy) {
00681                 int x,y;
00682                 char swp;
00683                 for (y=0; y<iysize/2; y++) {
00684                         for (x=0; x<ixsize; x++) {
00685                                 swp=ret[y*bpl+x];
00686                                 ret[y*bpl+x]=ret[(iysize-y-1)*bpl+x];
00687                                 ret[(iysize-y-1)*bpl+x]=swp;
00688                         }
00689                 }
00690         }
00691 
00692     //  return PyString_FromStringAndSize((const char*) data,iysize*bpl);
00693         return ret;
00694 }
00695 
00696 
00697 void EMData::render_amp24( int x0, int y0, int ixsize, int iysize,
00698                                                   int bpl, float scale, int mingray, int maxgray,
00699                                                   float render_min, float render_max, void *ref,
00700                                                   void cmap(void *, int coord, unsigned char *tri))
00701 {
00702         ENTERFUNC;
00703 
00704         if (get_ndim() != 2) {
00705                 throw ImageDimensionException("2D only");
00706         }
00707 
00708         if (is_complex()) {
00709                 ri2ap();
00710         }
00711 
00712         if (render_max <= render_min) {
00713                 render_max = render_min + 0.01f;
00714         }
00715 
00716         std::string ret=std::string();
00717         ret.resize(iysize*bpl);
00718         unsigned char *data=(unsigned char *)ret.data();
00719 
00720         float rm = render_min;
00721         float inv_scale = 1.0f / scale;
00722         int ysize = iysize;
00723         int xsize = ixsize;
00724         const int scale_n = 100000;
00725 
00726         int ymin = 0;
00727         if ( iysize * inv_scale > ny) {
00728                 ymin = (int) (iysize - ny / inv_scale);
00729         }
00730         float gs = (maxgray - mingray) / (render_max - render_min);
00731         if (render_max < render_min) {
00732                 gs = 0;
00733                 rm = FLT_MAX;
00734         }
00735         int dsx = -1;
00736         int dsy = 0;
00737         if (inv_scale == floor(inv_scale)) {
00738                 dsx = (int) inv_scale;
00739                 dsy = (int) (inv_scale * nx);
00740         }
00741         int addi = 0;
00742         int addr = 0;
00743 
00744         if (dsx == -1) {
00745                 addi = (int) floor(inv_scale);
00746                 addr = (int) (scale_n * (inv_scale - floor(inv_scale)));
00747         }
00748 
00749         int remx = 0;
00750         int remy = 0;
00751         int xmin = 0;
00752         if (x0 < 0) {
00753                 xmin = (int) (-x0 / inv_scale);
00754                 xsize -= (int) floor(x0 / inv_scale);
00755                 x0 = 0;
00756         }
00757 
00758         if ((xsize - xmin) * inv_scale > (nx - x0)) {
00759                 xsize = (int) ((nx - x0) / inv_scale + xmin);
00760         }
00761         int ymax = ysize - 1;
00762         if (y0 < 0) {
00763                 ymax = (int) (ysize + y0 / inv_scale - 1);
00764                 ymin += (int) floor(y0 / inv_scale);
00765                 y0 = 0;
00766         }
00767 
00768 
00769         if (xmin < 0) {
00770                 xmin = 0;
00771         }
00772 
00773         if (ymin < 0) {
00774                 ymin = 0;
00775         }
00776         if (xsize > ixsize) {
00777                 xsize = ixsize;
00778         }
00779         if (ymax > iysize) {
00780                 ymax = iysize;
00781         }
00782 
00783         int lmax = nx * ny - 1;
00784         unsigned char tri[3];
00785         float* image_data = get_data();
00786         if (is_complex()) {
00787                 if (dsx != -1) {
00788                         int l = y0 * nx;
00789                         for (int j = ymax; j >= ymin; j--) {
00790                                 int ll = x0;
00791                                 for (int i = xmin; i < xsize; i++, ll += dsx) {
00792                                         if (l + ll > lmax || ll >= nx - 2) {
00793                                                 break;
00794                                         }
00795                                         int kk = 0;
00796                                         if (ll >= nx / 2) {
00797                                                 if (l >= (ny - inv_scale) * nx) {
00798                                                         kk = 2 * (ll - nx / 2) + 2;
00799                                                 }
00800                                                 else {
00801                                                         kk = 2 * (ll - nx / 2) + l + 2 + nx;
00802                                                 }
00803                                         }
00804                                         else {
00805                                                 kk = nx * ny - (l + 2 * ll) - 2;
00806                                         }
00807                                         int k = 0;
00808                                         float t = image_data[kk];
00809                                         if (t <= rm) {
00810                                                 k = mingray;
00811                                         }
00812                                         else if (t >= render_max) {
00813                                                 k = maxgray;
00814                                         }
00815                                         else {
00816                                                 k = (int) (gs * (t - render_min));
00817                                                 k += mingray;
00818                                         }
00819                                         tri[0] = static_cast < unsigned char >(k);
00820                                         cmap(ref, kk, tri);
00821                                         data[i * 3 + j * bpl] = tri[0];
00822                                         data[i * 3 + 1 + j * bpl] = tri[1];
00823                                         data[i * 3 + 2 + j * bpl] = tri[2];
00824                                 }
00825                                 l += dsy;
00826                         }
00827                 }
00828                 else {
00829                         remy = 10;
00830                         for (int j = ymax, l = y0 * nx; j >= ymin; j--) {
00831                                 int br = l;
00832                                 remx = 10;
00833                                 for (int i = xmin, ll = x0; i < xsize - 1; i++) {
00834                                         if (l + ll > lmax || ll >= nx - 2) {
00835                                                 break;
00836                                         }
00837                                         int kk = 0;
00838                                         if (ll >= nx / 2) {
00839                                                 if (l >= (ny * nx - nx)) {
00840                                                         kk = 2 * (ll - nx / 2) + 2;
00841                                                 }
00842                                                 else {
00843                                                         kk = 2 * (ll - nx / 2) + l + 2 + nx;
00844                                                 }
00845                                         }
00846                                         else {
00847                                                 kk = nx * ny - (l + 2 * ll) - 2;
00848                                         }
00849                                         int k = 0;
00850                                         float t = image_data[kk];
00851                                         if (t <= rm) {
00852                                                 k = mingray;
00853                                         }
00854                                         else if (t >= render_max) {
00855                                                 k = maxgray;
00856                                         }
00857                                         else {
00858                                                 k = (int) (gs * (t - render_min));
00859                                                 k += mingray;
00860                                         }
00861                                         tri[0] = static_cast < unsigned char >(k);
00862                                         cmap(ref, kk, tri);
00863                                         data[i * 3 + j * bpl] = tri[0];
00864                                         data[i * 3 + 1 + j * bpl] = tri[1];
00865                                         data[i * 3 + 2 + j * bpl] = tri[2];
00866                                         ll += addi;
00867                                         remx += addr;
00868                                         if (remx > scale_n) {
00869                                                 remx -= scale_n;
00870                                                 ll++;
00871                                         }
00872                                 }
00873                                 l = br + addi * nx;
00874                                 remy += addr;
00875                                 if (remy > scale_n) {
00876                                         remy -= scale_n;
00877                                         l += nx;
00878                                 }
00879                         }
00880                 }
00881         }
00882         else {
00883                 if (dsx != -1) {
00884                         for (int j = ymax, l = x0 + y0 * nx; j >= ymin; j--) {
00885                                 int br = l;
00886                                 for (int i = xmin; i < xsize; i++, l += dsx) {
00887                                         if (l > lmax) {
00888                                                 break;
00889                                         }
00890                                         float t = image_data[l];
00891                                         int k = 0;
00892                                         if (t <= rm) {
00893                                                 k = mingray;
00894                                         }
00895                                         else if (t >= render_max) {
00896                                                 k = maxgray;
00897                                         }
00898                                         else {
00899                                                 k = (int) (gs * (t - render_min));
00900                                                 k += mingray;
00901                                         }
00902                                         tri[0] = static_cast < unsigned char >(k);
00903                                         cmap(ref, l, tri);
00904                                         data[i * 3 + j * bpl] = tri[0];
00905                                         data[i * 3 + 1 + j * bpl] = tri[1];
00906                                         data[i * 3 + 2 + j * bpl] = tri[2];
00907                                 }
00908                                 l = br + dsy;
00909                         }
00910                 }
00911                 else {
00912                         remy = 10;
00913                         for (int j = ymax, l = x0 + y0 * nx; j >= ymin; j--) {
00914                                 int br = l;
00915                                 remx = 10;
00916                                 for (int i = xmin; i < xsize; i++) {
00917                                         if (l > lmax) {
00918                                                 break;
00919                                         }
00920                                         float t = image_data[l];
00921                                         int k = 0;
00922                                         if (t <= rm) {
00923                                                 k = mingray;
00924                                         }
00925                                         else if (t >= render_max) {
00926                                                 k = maxgray;
00927                                         }
00928                                         else {
00929                                                 k = (int) (gs * (t - render_min));
00930                                                 k += mingray;
00931                                         }
00932                                         tri[0] = static_cast < unsigned char >(k);
00933                                         cmap(ref, l, tri);
00934                                         data[i * 3 + j * bpl] = tri[0];
00935                                         data[i * 3 + 1 + j * bpl] = tri[1];
00936                                         data[i * 3 + 2 + j * bpl] = tri[2];
00937                                         l += addi;
00938                                         remx += addr;
00939                                         if (remx > scale_n) {
00940                                                 remx -= scale_n;
00941                                                 l++;
00942                                         }
00943                                 }
00944                                 l = br + addi * nx;
00945                                 remy += addr;
00946                                 if (remy > scale_n) {
00947                                         remy -= scale_n;
00948                                         l += nx;
00949                                 }
00950                         }
00951                 }
00952         }
00953 
00954         EXITFUNC;
00955 }
00956 
00957 void EMData::ap2ri()
00958 {
00959         ENTERFUNC;
00960 
00961         if (!is_complex() || is_ri()) {
00962                 return;
00963         }
00964 
00965 //#ifdef EMAN2_USING_CUDA
00966 //      if (gpu_operation_preferred()) {
00967 //              EMDataForCuda tmp = get_data_struct_for_cuda();
00968 //              emdata_ap2ri(&tmp);
00969 //              set_ri(true);
00970 //              gpu_update();
00971 //              EXITFUNC;
00972 //              return;
00973 //      }
00974 //#endif
00975 
00976         Util::ap2ri(get_data(), (size_t)nx * ny * nz);
00977         set_ri(true);
00978         update();
00979         EXITFUNC;
00980 }
00981 
00982 void EMData::ri2inten()
00983 {
00984         ENTERFUNC;
00985 
00986         if (!is_complex()) return;
00987         if (!is_ri()) ap2ri();
00988 
00989 //#ifdef EMAN2_USING_CUDA
00990 //      if (gpu_operation_preferred()) {
00991 //              EMDataForCuda tmp = get_data_struct_for_cuda();
00992 //              emdata_ri2inten(&tmp);
00993 //              set_attr("is_intensity", int(1));
00994 //              gpu_update();
00995 //              EXITFUNC;
00996 //              return;
00997 //      }
00998 //#endif
00999 
01000         float * data = get_data();
01001         size_t size = (size_t)nx * ny * nz;
01002         for (size_t i = 0; i < size; i += 2) {
01003                 data[i]=data[i]*data[i]+data[i+1]*data[i+1];
01004                 data[i+1]=0;
01005         }
01006 
01007         set_attr("is_intensity", int(1));
01008         update();
01009         EXITFUNC;
01010 }
01011 
01012 
01013 void EMData::ri2ap()
01014 {
01015         ENTERFUNC;
01016 
01017         if (!is_complex() || !is_ri()) {
01018                 return;
01019         }
01020 //#ifdef EMAN2_USING_CUDA
01021 //      if (gpu_operation_preferred()) {
01022 //              EMDataForCuda tmp = get_data_struct_for_cuda();
01023 //              emdata_ri2ap(&tmp);
01024 //              set_ri(false);
01025 //              gpu_update();
01026 //              EXITFUNC;
01027 //              return;
01028 //      }
01029 //#endif
01030 
01031         float * data = get_data();
01032 
01033         size_t size = (size_t)nx * ny * nz;
01034         for (size_t i = 0; i < size; i += 2) {
01035 #ifdef  _WIN32
01036                 float f = (float)_hypot(data[i], data[i + 1]);
01037 #else
01038                 float f = (float)hypot(data[i], data[i + 1]);
01039 #endif
01040                 if (data[i] == 0 && data[i + 1] == 0) {
01041                         data[i + 1] = 0;
01042                 }
01043                 else {
01044                         data[i + 1] = atan2(data[i + 1], data[i]);
01045                 }
01046                 data[i] = f;
01047         }
01048 
01049         set_ri(false);
01050         update();
01051         EXITFUNC;
01052 }
01053 
01054 
01055 float calc_bessel(const int n, const float& x) {
01056         gsl_sf_result result;
01057 //      int success =
01058         gsl_sf_bessel_Jn_e(n,(double)x, &result);
01059         return (float)result.val;
01060 }
01061 
01062 EMData*   EMData::bispecRotTransInvN(int N, int NK)
01063 {
01064 
01065         int EndP = this -> get_xsize(); // length(fTrueVec);
01066         int Mid  = (int) ((1+EndP)/2);
01067         int End = 2*Mid-1;
01068 
01069         int CountxyMax = End*End;
01070 
01071         int   *SortfkInds       = new    int[CountxyMax];
01072         int   *kVecX            = new    int[CountxyMax];
01073         int   *kVecY            = new    int[CountxyMax];
01074         float *fkVecR           = new  float[CountxyMax];
01075         float *fkVecI           = new  float[CountxyMax];
01076         float *absD1fkVec       = new  float[CountxyMax];
01077         float *absD1fkVecSorted = new  float[CountxyMax];
01078 
01079         float *jxjyatan2         = new  float[End*End]; //  jxjyatan2[jy*End + jx]  = atan2(jy+1-Mid , jx +1 -Mid);
01080 
01081         EMData * ThisCopy = new EMData(End,End);
01082 
01083         for (int jx=0; jx <End ; jx++) {
01084                 for (int jy=0; jy <End ; jy++) {
01085                         float ValNow = this -> get_value_at(jx,jy);
01086                         ThisCopy -> set_value_at(jx,jy,ValNow);
01087 //              cout<< " jxM= " << jx+1<<" jyM= " << jy+1<< "ValNow" << ValNow << endl; //    Works
01088         }}
01089 
01090 
01091         EMData* fk = ThisCopy -> do_fft();
01092         fk          ->process_inplace("xform.fourierorigin.tocenter");
01093 
01094 //      EMData* fk
01095         EMData* fkRCopy = new EMData(End,End);
01096         EMData* fkICopy = new EMData(End,End);
01097         EMData* fkCopy  = new EMData(End,End);
01098 
01099 
01100         for  (int jCount= 0; jCount<End*End; jCount++) {
01101 //              jCount = jy*End + jx;
01102                 int jx             = jCount%End ;
01103                 int jy             = (jCount-jx)/End ;
01104                 jxjyatan2[jCount]  = atan2((float)(jy+1-Mid) , (float)(jx +1-Mid));
01105         }
01106 
01107 
01108         for (int kEx= 0; kEx<2*Mid; kEx=kEx+2) { // kEx twice the value of the Fourier
01109                                                 // x variable: EMAN index for real, imag
01110                 int kx    = kEx/2;              // kx  is  the value of the Fourier variable
01111                 int kIx   = kx+Mid-1; // This is the value of the index for a matlab image (-1)
01112                 int kCx   =  -kx ;
01113                 int kCIx  = kCx+ Mid-1 ;
01114                 for (int kEy= 0 ; kEy<End; kEy++) { // This is the value of the EMAN index
01115                         int kIy              =  kEy       ; //  This is the value of the index for a matlab image (-1)
01116                         int ky               =  kEy+1-Mid; // (kEy+ Mid-1)%End - Mid+1 ;  // This is the actual value of the Fourier variable
01117                         float realVal        =  fk -> get_value_at(kEx  ,kEy) ;
01118                         float imagVal        =  fk -> get_value_at(kEx+1,kEy) ;
01119                         float absVal         =  ::sqrt(realVal*realVal+imagVal*imagVal);
01120                         float fkAng          =  atan2(imagVal,realVal);
01121 
01122                         float NewRealVal   ;
01123                         float NewImagVal   ;
01124                         float AngMatlab    ;
01125 
01126                         if (kIx==Mid-1) {
01127 //                              AngMatlab = -fkAng - 2.*M_PI*(kIy+ 1-Mid)*(Mid)/End;
01128 //                      cout<< "i= " << i << " kIx= " << kIx << " kIy=" << kIy << " fkVecR[i] =" << fkVecR[i]<< " fkVecI[i]="  << fkVecI[i] <<"  angle[i]= "  << AngMatlab << endl;
01129                         }
01130 
01131                         if (kIx>Mid-1){
01132 //                      cout<< "i= " << i << " kIx= " << kIx << " kIy=" << kIy << " fkVecR[i] =" << fkVecR[i]<< " fkVecI[i]="  << fkVecI[i] <<"  angle[i]= "  << AngMatlab << endl;
01133                         }
01134 
01135                         AngMatlab = fkAng - 2.0f*M_PI*(kx +ky)*(Mid)/End;
01136                         NewRealVal  =   absVal*cos(AngMatlab);
01137                         NewImagVal  =   absVal*sin(AngMatlab);
01138 
01139 
01140                         fkVecR[kIy+kIx *End] =  NewRealVal ;
01141                         fkVecR[kIy+kCIx*End] =  NewRealVal ;
01142                         fkVecI[kIy+kIx *End] =  NewImagVal ;
01143                         fkVecI[kIy+kCIx*End] = -NewImagVal ;
01144                         absD1fkVec[kIy + kIx  *End] = absVal;
01145                         absD1fkVec[kIy + kCIx *End] = absVal;
01146                         kVecX[kIy+kIx  *End] =  kx      ;
01147                         kVecX[kIy+kCIx *End] =  kCx    ;
01148                         kVecY[kIy+kIx  *End] =  ky     ;
01149                         kVecY[kIy+kCIx *End] =  ky     ;
01150 //                      printf("kx=%d,ky=%d,tempVal =%f+ i %4.2f \n",kx,ky,realVal,imagVal );
01151 //                      cout << "kx = " << kx << "; ky = "<< ky << "; val is" << realVal<<"+ i "<<imagVal<< endl;
01152 
01153 //                      cout << "kIMx = "<< kIx+1 << "; kIMy = "<< kIy+1 <<"; fkAng*9/ 2pi is " << fkAng*9/2/M_PI<<  endl;
01154 //                      cout << "kIMx = "<< kIx+1 << "; kIMy = "<< kIy+1 <<"; absval is " << absVal<<  "; realval is " << NewRealVal<< "; imagval is " << NewImagVal<< endl;
01155                         fkCopy  -> set_value_at(kIx ,kIy, absVal);
01156                         fkCopy  -> set_value_at(kCIx,kIy, absVal);
01157                         fkRCopy -> set_value_at(kIx, kIy, NewRealVal);
01158                         fkRCopy -> set_value_at(kCIx,kIy, NewRealVal);
01159                         fkICopy -> set_value_at(kIx, kIy, NewImagVal);
01160                         fkICopy -> set_value_at(kCIx,kIy,-NewImagVal);
01161 
01162                 }
01163         }
01164         system("rm -f fkCopy.???");
01165         system("rm -f fk?Copy.???");
01166         fkCopy  -> write_image("fkCopy.img");
01167         fkRCopy -> write_image("fkRCopy.img");
01168         fkICopy -> write_image("fkICopy.img");
01169 
01170         cout << "Starting the sort "<< endl;
01171 
01172         vector< pair<float, int> > absInds;
01173         for(int i  = 0; i < CountxyMax; ++i ) {
01174                 pair<float,int> p;
01175                 p = make_pair(absD1fkVec[i],i); // p = make_pair(rand(),i);
01176                 absInds.push_back( p);
01177         }
01178 
01179         std::sort(absInds.begin(),absInds.end());
01180 
01181         for(int i  = 0; i < CountxyMax; ++i ) {
01182                 pair<float,int> p   ;
01183                 p = absInds[i]         ;
01184                 absD1fkVecSorted[CountxyMax-1-i] =  p.first ;
01185                 SortfkInds[CountxyMax-1-i]       =  p.second ;
01186         }
01187 
01188         cout << "Ending the sort "<< endl;
01189 
01190 // float AngsMat[] ={2.8448, -0.3677,-0.2801,-1.0494,-1.7836,-2.5179, 2.9959, 3.0835,-0.1290,-0.8876,2.1829, 2.2705,1.5011,0.7669,0.0327,-0.7366,-0.6489,2.4215,-1.6029,1.4676,1.5552,0.7859,0.0517,-0.6825,-1.4518,-1.3642,1.7063,-1.7845,1.2859,1.3736,0.6043,-0.1299,-0.8642,-1.6335,-1.5459,1.5247,-1.6546,1.4159,1.5036,0.7342,0,-0.7342,-1.5036,-1.4159,1.6546,-1.5247,1.5459,1.6335,0.8642,0.1299,-0.6043,-1.3736,-1.286,1.7846,-1.7063,1.3642,1.4519,0.6825,-0.0517,-0.7859,-1.5553,-1.4676,1.6029,-2.4216,0.649,0.7366,-0.0327,-0.767,-1.5012,-2.2705,-2.1829,0.8877,0.1291,-3.0836,-2.9959,2.5179,1.7837,1.0495,0.2801,0.3677,-2.8449};
01191 
01192 
01193         for(int i  = 0; i < CountxyMax; ++i ) {  // creates a new fkVec
01194                 int Si  = SortfkInds[i];
01195                 int kIx = (int)  Si/End;  kIx = (int)  i/End; // i = kIx*End+kIy
01196 //              int kIy = Si  - kIx*End;  kIy = i  - kIx*End;
01197 //              int iC = (End-1-kIx)*End + (End-1-kIy);
01198 //              if (i<30) { cout<< "i= " << i << " kIx= " << kIx << " kIy=" << kIy << " valAft=" << absD1fkVecSorted[i]<< " valBef="  <<     absD1fkVec[Si] << "  SortfkInds = " << Si <<endl; }// This worked
01199 //              cout<< "i= " << i << " kIx= " << kIx << " kIy=" << kIy << " fkVecR[i] =" << fkVecR[i]<< " fkVecI[i]="  << fkVecI[i] <<"  angle[i]= "  << fkAng << endl;
01200         }
01201         cout<< "Ratio of Last Amplitude to First Amplitude= " << absD1fkVecSorted[NK] /absD1fkVecSorted[0]  << endl;
01202 
01203 //      pause;
01204 
01205 //      for(int i  = 0; i < NK; ++i ) { // Prints out the new fkVec ,  CountxyMax
01206 //              int Si= SortfkInds[i];
01207 //              int kIx = (int)  Si/End; // i = kIx*End+kIy
01208 //              int kIy = Si  - kIx*End;
01209 //              cout << " kIxM= " << kIx+1 << " kIyM=" << kIy+1 << " fkVecAbs=" << ::sqrt(fkVecR[Si]*fkVecR[Si] +  fkVecI[Si]* fkVecI[Si]) << " fkVecAbs=" << absD1fkVecSorted[i] << " kx= " << kVecX[Si] <<  " ky=" << kVecY[Si] <<  endl;
01210 //      }
01211 
01212 //       angEMAN+angMat+angDiff    =0  mod 2 pi
01213 
01214 //      angDiff=  2*pi*((-4):4)*(Mid)/End; angEMAN+angMat+angDiff= integer*2 *pi
01215 //              [  absD1fkVecSorted, SortfkInds] =sort( absD1fkVec,'descend') ;
01216 //      Util::sort_mat(&absD1fkVec[0],&absD1fkVec[Countxy],&SortfkInds[0],&SortfkInds[Countxy]);
01217 
01218 
01219 //      Let radial sampling be 0:0.5:(Mid-1)
01220 
01221  //     int NK=  min(12,CountxyMax) ;
01222 
01223 
01224 
01225         cout << "NK = " << NK << endl;
01226         float frR= 3.0/4.0;
01227         int LradRange= (int) (floor(Mid/frR)) ;
01228 
01229         float *radRange = new float[LradRange]; //= 0:.75:(Mid-1);
01230         radRange[0]=0;
01231         for (int irad=1; irad < LradRange; irad++){
01232                         radRange[irad] = radRange[irad-1] + frR; }
01233 
01234 
01235 
01236          // should equal to (2*Mid-1)
01237         cout << "Starting the calculation of invariants for N= " << N << endl;
01238 
01239 /*      int NMax=5;            */
01240 
01241         EMData* RotTransInv = new EMData();
01242         RotTransInv -> set_size(LradRange,LradRange);
01243 
01244 
01245 //      float  *RotTransInv       = new float[LradRange*LradRange ] ;
01246 //      float  *RotTransInvN      = new float[LradRange*LradRange*(NMax+1) ] ;
01247 
01248 //      for (int N=0 ; N<NMax; N++) {
01249 
01250         for (int jr1=0; jr1 < LradRange ; jr1++ ) { // LradRange
01251                 float r1= radRange[jr1];
01252 //              cout << "Pre jr2 "<< endl;
01253                 for (int jr2=0;  jr2<LradRange;  jr2++ ) { //LradRange
01254                         float r2= radRange[jr2];
01255                         float RotTransInvTemp=0;
01256                         for (int jCountkxy =0; jCountkxy<NK; jCountkxy++){
01257                                 int Countkxy =SortfkInds[jCountkxy] ;   // 1: CountxyMax
01258                                 int kx = kVecX[Countkxy] ;
01259                                 int ky = kVecY[Countkxy] ;
01260                                 float k2 = (float)(kx*kx+ky*ky);
01261                                 if (k2==0) { continue;}
01262                                 float phiK =0;
01263                                 if (k2>0) phiK= jxjyatan2[ (ky+Mid-1)*End + kx+Mid-1];  phiK= atan2((float)ky,(float)kx);
01264 
01265                                 float fkR     = fkVecR[Countkxy] ;
01266                                 float fkI     = fkVecI[Countkxy]  ;
01267 /*                              printf("jCountkxy=%d, Countkxy=%d,absD1fkVec(Countkxy)=%f,\t\t kx=%d, ky=%d \n", jCountkxy, Countkxy, absD1fkVec[Countkxy], kx, ky);*/
01268 
01269                                 for (int jCountqxy =0; jCountqxy<NK; jCountqxy++){
01270                                         int Countqxy =SortfkInds[jCountqxy] ;   // Countqxy is the index for absD1fkVec
01271                                         int qx   = kVecX[Countqxy] ;
01272                                         int qy   = kVecY[Countqxy] ;
01273                                         int q2   = qx*qx+qy*qy;
01274                                         if (q2==0) {continue;}
01275                                         float phiQ =0;
01276                                         if (q2>0) phiQ = jxjyatan2[ (qy+Mid-1)*End + qx+Mid-1];   phiQ=atan2((float)qy,(float)qx);
01277                                         float fqR     = fkVecR[Countqxy]  ;
01278                                         float fqI     = fkVecI[Countqxy]  ;
01279                                         int kCx  = (-kx-qx);
01280                                         int kCy  = (-ky-qy);
01281                                         int kCIx = ((kCx+Mid+2*End)%End);// labels of the image in C
01282                                         int kCIy = ((kCy+Mid+2*End)%End);
01283                                         kCx  = kCIx-Mid; // correct
01284                                         kCy  = kCIy-Mid; // correct
01285                                         int CountCxy = kCIx*End+kCIy;
01286                                         float fCR     = fkVecR[CountCxy];
01287                                         float fCI     = fkVecI[CountCxy];
01288                                         if (jr1+jr2==-1) {
01289                                         printf("jCountqxy=%d , Countqxy=%d, absD1fkVec(Countqxy)=%f,qx=%d, qy=%d \n", jCountqxy, Countqxy, absD1fkVec[Countqxy],qx, qy);
01290                                         printf(" CountCxy=%d,absD1fkVec[CountCxy]=%f,  kCx=%d,     kCy=%d \n",CountCxy, absD1fkVec[CountCxy], kCx, kCy );
01291                                         }
01292                                         for (int p=0; p<NK; p++){
01293 //                                              printf("p=%d, SortfkInds[p]=%d, CountCxy =%d \n", p,SortfkInds[p], CountCxy);
01294                                                 if (SortfkInds[p]==CountCxy){
01295                                                         float Arg1 = 2.0f*M_PI*r1*::sqrt((float) q2)/End;
01296                                                         float Arg2 = 2.0f*M_PI*r2*::sqrt((float) k2)/End;
01297 //                                                      printf("Arg1=%4.2f, Arg2=%4.2f,  \n",Arg1, Arg2 );
01298 //                                                      if (Arg1+ Arg2<15) {
01299                                                                 float bispectemp  = (fkR*(fqR*fCR -fqI*fCI) -fkI*(fqI*fCR  +fqR*fCI))
01300                                                                 * cos(N*(phiK-phiQ+M_PI));
01301                                                                 bispectemp  -= (fkR*(fqR*fCI + fqI*fCR) +fkI*(fqR*fCR - fqI*fCI))
01302                                                                 * sin(N*(phiK-phiQ+M_PI));
01303                                                                 float bess1 = calc_bessel(N, Arg1 );
01304                                                                 float bess2 = calc_bessel(N, Arg2 );
01305 //                      printf("fkr=%4.2f, fqr=%4.2f, bess1=%4.2f,bess2=%4.2f \n",fkR, fqR, bess1, bess2);
01306 /*                      printf("p =%d, SortfkInds[p]=%d, CountCxy=%d, Arg1 =%4.2f, bess1=%4.2f,  \n",
01307                                 p, SortfkInds[p],CountCxy, Arg1, bess1);*/
01308                                                                 RotTransInvTemp   = RotTransInvTemp  + bispectemp  * bess1*bess2 ;
01309 //                                                      }
01310                                                 }
01311                                         }
01312                                 } // jCountqxy
01313                         } // jCountkxy
01314                         RotTransInv -> set_value_at(jr1,jr2, RotTransInvTemp)   ;
01315 /*              RotTransInvN[jr1 + LradRange*jr2+LradRange*LradRange*N] = RotTransInvTemp  ;*/
01316                 } //jr2
01317         } //jr1
01318 // }//N
01319 
01320         return  RotTransInv ;
01321 
01322 
01323 }
01324 
01325 
01326 
01327 /*
01328 // find example
01329 #include <iostream>
01330 #include <algorithm>
01331 #include <vector>
01332 using namespace std;
01333 
01334 int main () {
01335   int myints[] = { 10, 20, 30 ,40 };
01336   int * p;
01337 
01338   // pointer to array element:
01339   p = find(myints,myints+4,30);
01340   ++p;
01341   cout << "The element following 30 is " << *p << endl;
01342 
01343   vector<int> myvector (myints,myints+4);
01344   vector<int>::iterator it;
01345 
01346   // iterator to vector element:
01347   it = find (myvector.begin(), myvector.end(), 30);
01348   ++it;
01349   cout << "The element following 30 is " << *it << endl;
01350 
01351   return 0;
01352 }*/
01353 
01354 EMData*   EMData::bispecRotTransInvDirect(int type)
01355 {
01356 
01357         int EndP = this -> get_xsize(); // length(fTrueVec);
01358         int Mid  = (int) ((1+EndP)/2);
01359         int End = 2*Mid-1;
01360 
01361         int CountxyMax = End*End;
01362 
01363 //      int   *SortfkInds       = new    int[CountxyMax];
01364         int   *kVecX            = new    int[CountxyMax];
01365         int   *kVecY            = new    int[CountxyMax];
01366         float *fkVecR           = new  float[CountxyMax];
01367         float *fkVecI           = new  float[CountxyMax];
01368         float *absD1fkVec       = new  float[CountxyMax];
01369 //      float *absD1fkVecSorted = new  float[CountxyMax];
01370 
01371 
01372         float *jxjyatan2         = new  float[End*End];
01373 
01374 
01375         EMData * ThisCopy = new EMData(End,End);
01376 
01377         for (int jx=0; jx <End ; jx++) {  // create jxjyatan2
01378                 for (int jy=0; jy <End ; jy++) {
01379                         float ValNow = this -> get_value_at(jx,jy);
01380                         ThisCopy -> set_value_at(jx,jy,ValNow);
01381                         jxjyatan2[jy*End + jx]  = atan2((float)(jy+1-Mid) , (float)(jx +1 -Mid));
01382 //              cout<< " jxM= " << jx+1<<" jyM= " << jy+1<< "ValNow" << ValNow << endl; //    Works
01383         }}
01384 
01385 
01386         EMData* fk = ThisCopy -> do_fft();
01387         fk          ->process_inplace("xform.fourierorigin.tocenter");
01388 
01389 //      Create kVecX , kVecy etc
01390 
01391         for (int kEx= 0; kEx<2*Mid; kEx=kEx+2) { // kEx twice the value of the Fourier
01392                                                 // x variable: EMAN index for real, imag
01393                 int kx    = kEx/2;              // kx  is  the value of the Fourier variable
01394                 int kIx   = kx+Mid-1; // This is the value of the index for a matlab image (-1)
01395                 int kCx   = -kx ;
01396                 int kCIx  = kCx+ Mid-1 ;
01397                 for (int kEy= 0 ; kEy<End; kEy++) { // This is the value of the EMAN index
01398                         int kIy              =  kEy       ; //  This is the value of the index for a matlab image (-1)
01399                         int ky               =  kEy+1-Mid; // (kEy+ Mid-1)%End - Mid+1 ;  // This is the actual value of the Fourier variable
01400                         float realVal        =  fk -> get_value_at(kEx  ,kEy) ;
01401                         float imagVal        =  fk -> get_value_at(kEx+1,kEy) ;
01402                         float absVal         =  ::sqrt(realVal*realVal+imagVal*imagVal);
01403                         float fkAng          =  atan2(imagVal,realVal);
01404 
01405                         float NewRealVal   ;
01406                         float NewImagVal   ;
01407                         float AngMatlab    ;
01408 
01409                         if (kIx==Mid-1) {
01410 //                              AngMatlab = -fkAng - 2.*M_PI*(kIy+ 1-Mid)*(Mid)/End;
01411                         }
01412 
01413                         if (kIx>Mid-1){
01414 //                      cout<< "i= " << i << " kIx= " << kIx << " kIy=" << kIy << " fkVecR[i] =" << fkVecR[i]<< " fkVecI[i]="  << fkVecI[i] <<"  angle[i]= "  << AngMatlab << endl;
01415                         }
01416 
01417                         AngMatlab = fkAng - 2.0f*M_PI*(kx +ky)*(Mid)/End;
01418                         NewRealVal  =   absVal*cos(AngMatlab);
01419                         NewImagVal  =   absVal*sin(AngMatlab);
01420 
01421 
01422                         fkVecR[ kIy +kIx *End] =  NewRealVal ;
01423                         fkVecR[(End-1-kIy)+kCIx*End] =  NewRealVal ;
01424                         fkVecI[ kIy +kIx *End] =  NewImagVal ;
01425                         fkVecI[(End-1-kIy)+kCIx*End] = -NewImagVal ;
01426                         absD1fkVec[(End-1-kIy) + kIx  *End] = absVal;
01427                         absD1fkVec[(End-1-kIy) + kCIx *End] = absVal;
01428                         kVecX[kIy+kIx  *End] =  kx      ;
01429                         kVecX[kIy+kCIx *End] =  kCx    ;
01430                         kVecY[kIy+kIx  *End] =  ky     ;
01431                         kVecY[kIy+kCIx *End] =  ky     ;
01432 
01433  //                     cout << " kIxM= " << kIx+1 << " kIy=" << kIy+1 << " fkVecR[i] =" << NewRealVal << " fkVecI[i]="  << NewImagVal <<"  angle[i]= "  << AngMatlab << " Total Index" << kIy+kIx *End << endl;
01434 
01435 //                      printf("kx=%d,ky=%d,tempVal =%f+ i %4.2f \n",kx,ky,realVal,imagVal );
01436 //                      cout << "kx = " << kx << "; ky = "<< ky << "; val is" << realVal<<"+ i "<<imagVal<< endl;
01437 
01438 //                      cout << "kIMx = "<< kIx+1 << "; kIMy = "<< kIy+1 <<"; fkAng*9/ 2pi is " << fkAng*9/2/M_PI<<  endl;
01439 //                      cout << "kIMx = "<< kIx+1 << "; kIMy = "<< kIy+1 <<"; absval is " << absVal<<  "; realval is " << NewRealVal<< "; imagval is " << NewImagVal<< endl;
01440                 }
01441         }
01442 
01443 
01444 //      for (int TotalInd = 0 ;  TotalInd < CountxyMax ; TotalInd++){
01445 //              int kx     = kVecX[TotalInd]; // This is the value of the index for a matlab image (-1)
01446 //              int kIx    = kx+Mid-1; // This is the value of the index for a matlab image (-1)
01447 //              int ky     = kVecY[TotalInd];
01448 //              int kIy    = ky+Mid-1; // This is the value of the index for a matlab image (-1)
01449                 //float fkR  = fkVecR[kIy+kIx *End]  ;
01450                 //float fkI  = fkVecI[kIy+kIx *End]  ;
01451 //      }
01452 
01453         float frR= 3.0/4.0;
01454         frR= 1;
01455         int LradRange= (int) (1+floor(Mid/frR -.1)) ;
01456 
01457         float *radRange = new float[LradRange]; //= 0:.75:(Mid-1);
01458         for (int irad=0; irad < LradRange; irad++){
01459                         radRange[irad] =  frR*irad;
01460 //                      cout << " irad = " << irad << " radRange[irad]= " <<  radRange[irad] <<  " LradRange= " << LradRange << endl;
01461         }
01462 
01463         cout << "Starting the calculation of invariants" << endl;
01464 
01465 
01466         if (type==0) {
01467                 int LthetaRange  = 59;
01468                 float ftR        = (2.0f*M_PI/LthetaRange );
01469                 float *thetaRange = new float[LthetaRange]; //= 0:.75:(Mid-1);
01470 
01471                 for (int ith=0; ith < LthetaRange; ith++){
01472                                 thetaRange[ith] =  ftR*ith; }
01473 
01474                 int TotalVol = LradRange*LradRange*LthetaRange;
01475 
01476                 float *RotTransInv   = new  float[TotalVol];
01477                 float *WeightInv     = new  float[TotalVol];
01478 
01479                 for (int jW=0; jW<TotalVol; jW++) {
01480                         RotTransInv[jW] = 0;
01481                         WeightInv[jW]   = 0;
01482                 }
01483 
01484                 for (int jW=0; jW<TotalVol; jW++) {
01485                         RotTransInv[jW] = 0;
01486                         WeightInv[jW]   = 0;
01487                 }
01488         //      float  *RotTransInv       = new float[LradRange*LradRange ] ;
01489         //      float  *RotTransInvN      = new float[LradRange*LradRange*(NMax+1) ] ;
01490 
01491                 for (int Countkxy =0; Countkxy<CountxyMax; Countkxy++){  // Main Section for type 0
01492                         int kx = kVecX[Countkxy] ;
01493                         int ky = kVecY[Countkxy] ;
01494                         float k2 = ::sqrt((float)(kx*kx+ky*ky));
01495                         float phiK =0;
01496                         if (k2>0)    phiK = jxjyatan2[ (ky+Mid-1)*End + kx+Mid-1]; //  phiK=atan2(ky,kx);
01497                         float fkR     = fkVecR[(ky+Mid-1) + (kx+Mid-1) *End] ;
01498                         float fkI     = fkVecI[(ky+Mid-1) + (kx+Mid-1) *End]  ;
01499         //              printf("Countkxy=%d,\t kx=%d, ky=%d, fkR=%3.2f,fkI=%3.2f \n", Countkxy, kx, ky, fkR, fkI);
01500 
01501                         if ((k2==0)|| (k2>Mid) ) { continue;}
01502 
01503                         for (int Countqxy =0; Countqxy<CountxyMax; Countqxy++){   // This is the innermost loop
01504                                 int qx   = kVecX[Countqxy] ;
01505                                 int qy   = kVecY[Countqxy] ;
01506                                 float q2   = ::sqrt((float)(qx*qx+qy*qy));
01507                                 if ((q2==0)|| (q2>Mid) ) {continue;}
01508                                 float phiQ =0;
01509                                 if (q2>0)   phiQ = jxjyatan2[ (qy+Mid-1)*End + qx+Mid-1]; // phiQ=atan2(qy,qx);
01510                                 float fqR     = fkVecR[(qy+Mid-1) + (qx+Mid-1) *End] ;
01511                                 float fqI     = fkVecI[(qy+Mid-1) + (qx+Mid-1) *End]  ;
01512                                 int kCx  = (-kx-qx);
01513                                 int kCy  = (-ky-qy);
01514                                 int kCIx = ((kCx+Mid+2*End)%End);// labels of the image in C
01515                                 int kCIy = ((kCy+Mid+2*End)%End);
01516                                 kCx  = ((kCIx+End-1)%End)+1-Mid; // correct
01517                                 kCy  = ((kCIy+End-1)%End)+1-Mid ; // correct
01518 
01519 //                              float C2   = ::sqrt((float)(kCx*kCx+ kCy*kCy));
01520                                 int CountCxy  = (kCx+Mid-1)*End+(kCy+Mid-1);
01521                                 float fCR     = fkVecR[CountCxy];
01522                                 float fCI     = fkVecI[CountCxy];
01523         /*                      if (Countkxy==1) {
01524                                         printf(" Countqxy=%d, absD1fkVec(Countqxy)=%f,qx=%d, qy=%d \n", Countqxy, absD1fkVec[Countqxy],qx, qy);
01525                                         printf(" CountCxy=%d, absD1fkVec[CountCxy]=%f,kCx=%d,kCy=%d \n",CountCxy, absD1fkVec[CountCxy], kCx, kCy );
01526                                 }*/
01527 //                              float   phiC = jxjyatan2[ (kCy+Mid-1)*End + kCx+Mid-1];
01528                                 float   phiQK = (4*M_PI+phiQ-phiK);
01529                                 while (phiQK> (2*M_PI)) phiQK -= (2*M_PI);
01530 
01531 
01532 
01533                                 float bispectemp  = (fkR*(fqR*fCR -fqI*fCI) -fkI*(fqI*fCR  +fqR*fCI));
01534 
01535                                 if  ((q2<k2) )  continue;
01536 //                              if  ((q2<k2) || (C2<k2) || (C2<q2))  continue;
01537 
01538         //                              printf(" CountCxy=%d, absD1fkVec[CountCxy]=%f,kCx=%d,kCy=%d \n",CountCxy, absD1fkVec[CountCxy], kCx, kCy );
01539 
01540         //                      up to here, matched perfectly with Matlab
01541 
01542                                 int k2IndLo  = 0; while ((k2>=radRange[k2IndLo+1]) && (k2IndLo+1 < LradRange ) ) k2IndLo +=1;
01543                                 int k2IndHi = k2IndLo;
01544                                 float k2Lo= radRange[k2IndLo];
01545                                 if (k2IndLo+1< LradRange) {
01546                                         k2IndHi   = k2IndLo+1;
01547                                 }
01548 //                              float k2Hi= radRange[k2IndHi];
01549 
01550                                 float kCof =k2-k2Lo;
01551 
01552                                 int q2IndLo  = 0; while ((q2>=radRange[q2IndLo+1]) && (q2IndLo+1 < LradRange ) ) q2IndLo +=1;
01553                                 int q2IndHi=q2IndLo;
01554                                 float q2Lo= radRange[q2IndLo];
01555                                 if (q2IndLo+1 < LradRange)  {
01556                                         q2IndHi   = q2IndLo+1 ;
01557                                 }
01558                                 float qCof = q2-q2Lo;
01559 
01560                                 if ((qCof<0) || (qCof >1) ) {
01561                                         cout<< "Weird! qCof="<< qCof <<  " q2="<< q2 << " q2IndLo="<< q2IndLo << endl ;
01562                                         int x    ;
01563                                         cin >> x ;
01564                                 }
01565 
01566                                 int thetaIndLo = 0; while ((phiQK>=thetaRange[thetaIndLo+1])&& (thetaIndLo+1<LthetaRange)) thetaIndLo +=1;
01567                                 int thetaIndHi = thetaIndLo;
01568 
01569                                 float thetaLo  = thetaRange[thetaIndLo];
01570                                 float thetaHi = thetaLo;
01571                                 float thetaCof = 0;
01572 
01573                                 if (thetaIndLo+1< LthetaRange) {
01574                                         thetaIndHi = thetaIndLo +1;
01575                                 }else{
01576                                         thetaIndHi=0;
01577                                 }
01578 
01579                                 thetaHi    = thetaRange[thetaIndHi];
01580 
01581                                 if (thetaHi==thetaLo) {
01582                                         thetaCof =0 ;
01583                                 } else {
01584                                         thetaCof   = (phiQK-thetaLo)/(thetaHi-thetaLo);
01585                                 }
01586 
01587                                 if ((thetaCof>2*M_PI)  ) {
01588                                         cout<< "Weird! thetaCof="<< thetaCof <<endl ;
01589                                         thetaCof=0;
01590                                 }
01591 
01592 
01593         //                      if ((thetaIndLo>=58) || (k2IndLo >= LradRange-1) || (q2IndLo >= LradRange-1) ) {
01594 
01595 
01596                                 for (int jk =1; jk<=2; jk++){
01597                                 for (int jq =1; jq<=2; jq++){
01598                                 for (int jtheta =1; jtheta<=2; jtheta++){
01599 
01600                                         float Weight = (kCof+(1-2*kCof)*(jk==1))*(qCof+(1-2*qCof)*(jq==1))
01601                                                         * (thetaCof+(1-2*thetaCof)*(jtheta==1));
01602 
01603 
01604                                         int k2Ind      =  k2IndLo*(jk==1)      +   k2IndHi*(jk==2);
01605                                         int q2Ind      =  q2IndLo*(jq==1)      +   q2IndHi*(jq==2);
01606                                         int thetaInd   =  thetaIndLo*(jtheta==1)  + thetaIndHi*(jtheta ==2);
01607                                         int TotalInd   = thetaInd*LradRange*LradRange+q2Ind*LradRange+k2Ind;
01608         /*                              if (TotalInd+1 >=  LthetaRange*LradRange*LradRange) {
01609                                                 cout << "Weird!!! TotalInd="<< TotalInd << " IndMax" << LthetaRange*LradRange*LradRange << " LradRange=" << LradRange << endl;
01610                                                 cout << "k2Ind= "<< k2Ind  << " q2Ind="<< q2Ind  << " thetaInd="<< thetaInd  << " q2IndLo="<< q2IndLo  << " q2IndHi="<< q2IndHi  <<  endl;
01611                                                 cout << "k2=" << k2 << "q2=" << q2 << " phiQK=" << phiQK*180.0/M_PI<< endl;
01612                                         }*/
01613 
01614                                         RotTransInv[TotalInd] += Weight*bispectemp;
01615                                         WeightInv[TotalInd]   +=  Weight;
01616         //                              cout << "k2Ind= "<< k2Ind  << " q2Ind="<< q2Ind  << "Weight=" << Weight << endl;
01617                                 }}}
01618                         } // Countqxy
01619                 } // Countkxy
01620 
01621                 cout << "Finished Main Section " << endl;
01622 
01623         /*              RotTransInvN[jr1 + LradRange*jr2+LradRange*LradRange*N] = RotTransInvTemp  ;*/
01624 
01625                 cout << " LradRange " <<LradRange <<" LthetaRange " << LthetaRange << endl;
01626                 EMData *RotTransInvF  = new  EMData(LradRange,LradRange,LthetaRange);
01627                 EMData *WeightImage   = new  EMData(LradRange,LradRange,LthetaRange);
01628 
01629         //      cout << "FFFFFFF" << endl;
01630         //
01631         //      RotTransInvF -> set_size(LradRange,LradRange,LthetaRange);
01632         //
01633         //      cout << "GGGG" << endl;
01634 
01635                 for (int jtheta =0; jtheta < LthetaRange; jtheta++){    // write out to RotTransInvF
01636                 for (int jq =0; jq<LradRange; jq++){ // LradRange
01637                 for (int jk =0; jk<LradRange ; jk++){// LradRange
01638         //              cout << "Hi There" << endl;
01639                         int TotalInd   = jtheta*LradRange*LradRange+jq*LradRange+jk;
01640                         float Weight = WeightInv[TotalInd];
01641                         WeightImage    -> set_value_at(jk,jq,jtheta,Weight);
01642                         RotTransInvF   -> set_value_at(jk,jq,jtheta,0);
01643                         if (Weight <= 0) continue;
01644                         RotTransInvF -> set_value_at(jk,jq,jtheta,RotTransInv[TotalInd] / Weight);//  include /Weight
01645                         int newjtheta = (LthetaRange- jtheta)%LthetaRange;
01646                         RotTransInvF -> set_value_at(jq,jk,newjtheta,RotTransInv[TotalInd]/Weight );//  include /Weight
01647                                 }
01648                         }
01649                 }
01650 
01651                 cout << " Almost Done " << endl;
01652                 system("rm -f WeightImage.???");
01653                 WeightImage  -> write_image("WeightImage.img");
01654 
01655                 return  RotTransInvF ;
01656         } // Finish type 0
01657 
01658         if (type==1) {
01659                 int TotalVol = LradRange*LradRange;
01660 
01661                 float *RotTransInv   = new  float[TotalVol];
01662                 float *WeightInv     = new  float[TotalVol];
01663 
01664                 for (int jW=0; jW<TotalVol; jW++) {
01665                         RotTransInv[jW] = 0;
01666                         WeightInv[jW]   = 0;
01667                 }
01668 
01669 
01670                 for (int Countkxy =0; Countkxy<CountxyMax; Countkxy++){
01671                         int kx = kVecX[Countkxy] ;
01672                         int ky = kVecY[Countkxy] ;
01673                         float k2 = ::sqrt((float)(kx*kx+ky*ky));
01674                         float fkR     = fkVecR[(ky+Mid-1) + (kx+Mid-1) *End] ;
01675                         float fkI     = fkVecI[(ky+Mid-1) + (kx+Mid-1) *End]  ;
01676         //              printf("Countkxy=%d,\t kx=%d, ky=%d, fkR=%3.2f,fkI=%3.2f \n", Countkxy, kx, ky, fkR, fkI);
01677 
01678                         if ((k2==0)|| (k2>Mid) ) { continue;}
01679 
01680                         for (int Countqxy =0; Countqxy<CountxyMax; Countqxy++){   // This is the innermost loop
01681 
01682 //                      up to here, matched perfectly with Matlab
01683                                 int qx   = kVecX[Countqxy] ;
01684                                 int qy   = kVecY[Countqxy] ;
01685                                 float q2   = ::sqrt((float)(qx*qx+qy*qy));
01686                                 if ((q2==0)|| (q2>Mid) ) {continue;}
01687                                 if  ((q2<k2) )   continue;
01688 
01689                                 float fqR     = fkVecR[(qy+Mid-1) + (qx+Mid-1) *End] ;
01690                                 float fqI     = fkVecI[(qy+Mid-1) + (qx+Mid-1) *End]  ;
01691 
01692                                 int kCx  = (-kx-qx);
01693                                 int kCy  = (-ky-qy);
01694                                 int kCIx = ((kCx+Mid+2*End)%End);// labels of the image in C
01695                                 int kCIy = ((kCy+Mid+2*End)%End);
01696                                 kCx  = ((kCIx+End-1)%End)+1-Mid; // correct
01697                                 kCy  = ((kCIy+End-1)%End)+1-Mid ; // correct
01698 
01699 //                              float C2   = ::sqrt((float)(kCx*kCx+ kCy*kCy));
01700                                 int CountCxy  = (kCx+Mid-1)*End+(kCy+Mid-1);
01701                                 float fCR     = fkVecR[CountCxy];
01702                                 float fCI     = fkVecI[CountCxy];
01703 
01704 
01705                                 float bispectemp  = (fkR*(fqR*fCR -fqI*fCI) -fkI*(fqI*fCR  +fqR*fCI));
01706 
01707 
01708                                 int k2IndLo  = 0; while ((k2>=radRange[k2IndLo+1]) && (k2IndLo+1 < LradRange ) ) k2IndLo +=1;
01709                                 int k2IndHi = k2IndLo;
01710                                 float k2Lo= radRange[k2IndLo];
01711                                 if (k2IndLo+1< LradRange) {
01712                                         k2IndHi   = k2IndLo+1;
01713                                 }
01714 //                              float k2Hi= radRange[k2IndHi];
01715 
01716                                 float kCof =k2-k2Lo;
01717 
01718                                 int q2IndLo  = 0; while ((q2>=radRange[q2IndLo+1]) && (q2IndLo+1 < LradRange ) ) q2IndLo +=1;
01719                                 int q2IndHi=q2IndLo;
01720                                 float q2Lo= radRange[q2IndLo];
01721                                 if (q2IndLo+1 < LradRange)  {
01722                                         q2IndHi   = q2IndLo+1 ;
01723                                 }
01724                                 float qCof = q2-q2Lo;
01725 
01726 
01727                                 for (int jk =1; jk<=2; jk++){
01728                                 for (int jq =1; jq<=2; jq++){
01729 
01730                                         float Weight = (kCof+(1-2*kCof)*(jk==1))*(qCof+(1-2*qCof)*(jq==1));
01731 
01732                                         int k2Ind      =  k2IndLo*(jk==1)      +   k2IndHi*(jk==2);
01733                                         int q2Ind      =  q2IndLo*(jq==1)      +   q2IndHi*(jq==2);
01734                                         int TotalInd   = q2Ind*LradRange+k2Ind;
01735                                         RotTransInv[TotalInd] += Weight*bispectemp;
01736                                         WeightInv[TotalInd]   +=  Weight;
01737         //                              cout << "k2Ind= "<< k2Ind  << " q2Ind="<< q2Ind  << "Weight=" << Weight << endl;
01738                                 }}
01739                         } // Countqxy
01740                 } // Countkxy
01741 
01742 //              cout << "Finished Main Section " << endl;
01743 //              cout << " LradRange " <<LradRange <<  endl;
01744 
01745 
01746                 EMData *RotTransInvF  = new  EMData(LradRange,LradRange);
01747                 EMData *WeightImage   = new  EMData(LradRange,LradRange);
01748 
01749                 for (int jk =0; jk<LradRange ; jk++){// LradRange
01750                 for (int jq =jk; jq<LradRange; jq++){ // LradRange
01751                         int TotalInd      = jq*LradRange+jk;
01752                         int TotalIndBar   = jq*LradRange+jk;
01753                         float Weight = WeightInv[TotalInd] + WeightInv[TotalIndBar];
01754                         if (Weight <=0) continue;
01755                         WeightImage    -> set_value_at(jk,jq,Weight);
01756                         WeightImage    -> set_value_at(jq,jk,Weight);
01757 #ifdef _WIN32
01758                         float ValNow  = pow( (RotTransInv[TotalInd] + RotTransInv[TotalIndBar]) / Weight, 1.0f/3.0f )  ;
01759 #else
01760                         float ValNow  = cbrt( (RotTransInv[TotalInd] + RotTransInv[TotalIndBar]) / Weight )  ;
01761 #endif  //_WIN32
01762                         RotTransInvF -> set_value_at(jk,jq,ValNow);//  include /Weight
01763                         RotTransInvF -> set_value_at(jq,jk,ValNow );//  include /Weight
01764                 }}
01765 
01766                 system("rm -f WeightImage.???");
01767                 WeightImage  -> write_image("WeightImage.img");
01768 
01769                 return  RotTransInvF ;
01770         }
01771         return 0;
01772 }
01773 
01774 
01775 void EMData::insert_clip(const EMData * const block, const IntPoint &origin) {
01776         int nx1 = block->get_xsize();
01777         int ny1 = block->get_ysize();
01778         int nz1 = block->get_zsize();
01779 
01780         Region area(origin[0], origin[1], origin[2],nx1, ny1, nz1);
01781 
01782         //make sure the block fits in EMData 
01783         int x0 = (int) area.origin[0];
01784         x0 = x0 < 0 ? 0 : x0;
01785 
01786         int y0 = (int) area.origin[1];
01787         y0 = y0 < 0 ? 0 : y0;
01788 
01789         int z0 = (int) area.origin[2];
01790         z0 = z0 < 0 ? 0 : z0;
01791 
01792         int x1 = (int) (area.origin[0] + area.size[0]);
01793         x1 = x1 > nx ? nx : x1;
01794 
01795         int y1 = (int) (area.origin[1] + area.size[1]);
01796         y1 = y1 > ny ? ny : y1;
01797 
01798         int z1 = (int) (area.origin[2] + area.size[2]);
01799         z1 = z1 > nz ? nz : z1;
01800         if (z1 <= 0) {
01801                 z1 = 1;
01802         }
01803 
01804         int xd0 = (int) (area.origin[0] < 0 ? -area.origin[0] : 0);
01805         int yd0 = (int) (area.origin[1] < 0 ? -area.origin[1] : 0);
01806         int zd0 = (int) (area.origin[2] < 0 ? -area.origin[2] : 0);
01807 
01808         if (x1 < x0 || y1 < y0 || z1 < z0) return; // out of bounds, this is fine, nothing happens
01809 
01810         size_t clipped_row_size = (x1-x0) * sizeof(float);
01811         int src_secsize =  nx1 * ny1;
01812         int dst_secsize = nx * ny;
01813 
01814 /*
01815 #ifdef EMAN2_USING_CUDA
01816         if(block->cudarwdata){
01817                 // this is VERY slow.....
01818                 float *cudasrc = block->cudarwdata + zd0 * src_secsize + yd0 * nx1 + xd0;
01819                 if(!cudarwdata) rw_alloc();
01820                 float *cudadst = cudarwdata + z0 * dst_secsize + y0 * nx + x0;
01821                 for (int i = z0; i < z1; i++) {
01822                         for (int j = y0; j < y1; j++) {
01823                                 //printf("%x %x %d\n", cudadst, cudasrc, clipped_row_size);
01824                                 cudaMemcpy(cudadst,cudasrc,clipped_row_size,cudaMemcpyDeviceToDevice);
01825                                 cudasrc += nx1;
01826                                 cudadst += nx;
01827                         }
01828                         cudasrc += src_gap;
01829                         cudadst += dst_gap;
01830                 }
01831                 return;
01832         }
01833 #endif
01834 */
01835         float *src = block->get_data() + zd0 * src_secsize + yd0 * nx1 + xd0;
01836         float *dst = get_data() + z0 * dst_secsize + y0 * nx + x0;
01837         
01838         int src_gap = src_secsize - (y1-y0) * nx1;
01839         int dst_gap = dst_secsize - (y1-y0) * nx;
01840         
01841         for (int i = z0; i < z1; i++) {
01842                 for (int j = y0; j < y1; j++) {
01843                         EMUtil::em_memcpy(dst, src, clipped_row_size);
01844                         src += nx1;
01845                         dst += nx;
01846                 }
01847                 src += src_gap;
01848                 dst += dst_gap;
01849         }
01850         
01851 #ifdef EMAN2_USING_CUDA 
01852         if(block->cudarwdata){
01853                 copy_to_cuda(); // copy back to device as padding is faster on the host
01854         }
01855 #endif
01856 
01857         update();
01858         EXITFUNC;
01859 }
01860 
01861 
01862 void EMData::insert_scaled_sum(EMData *block, const FloatPoint &center,
01863                                                    float scale, float)
01864 {
01865         ENTERFUNC;
01866         float * data = get_data();
01867         if (get_ndim()==3) {
01868                 // Start by determining the region to operate on
01869                 int xs=(int)floor(block->get_xsize()*scale/2.0);
01870                 int ys=(int)floor(block->get_ysize()*scale/2.0);
01871                 int zs=(int)floor(block->get_zsize()*scale/2.0);
01872                 int x0=(int)center[0]-xs;
01873                 int x1=(int)center[0]+xs;
01874                 int y0=(int)center[1]-ys;
01875                 int y1=(int)center[1]+ys;
01876                 int z0=(int)center[2]-zs;
01877                 int z1=(int)center[2]+zs;
01878 
01879                 if (x1<0||y1<0||z1<0||x0>get_xsize()||y0>get_ysize()||z0>get_zsize()) return;   // object is completely outside the target volume
01880 
01881                 // make sure we stay inside the volume
01882                 if (x0<0) x0=0;
01883                 if (y0<0) y0=0;
01884                 if (z0<0) z0=0;
01885                 if (x1>=get_xsize()) x1=get_xsize()-1;
01886                 if (y1>=get_ysize()) y1=get_ysize()-1;
01887                 if (z1>=get_zsize()) z1=get_zsize()-1;
01888 
01889                 float bx=block->get_xsize()/2.0f;
01890                 float by=block->get_ysize()/2.0f;
01891                 float bz=block->get_zsize()/2.0f;
01892 
01893                 size_t idx;
01894                 for (int x=x0; x<=x1; x++) {
01895                         for (int y=y0; y<=y1; y++) {
01896                                 for (int z=z0; z<=z1; z++) {
01897                                         idx = x + y * nx + (size_t)z * nx * ny;
01898                                         data[idx] +=
01899                                                 block->sget_value_at_interp((x-center[0])/scale+bx,(y-center[1])/scale+by,(z-center[2])/scale+bz);
01900                                 }
01901                         }
01902                 }
01903                 update();
01904         }
01905         else if (get_ndim()==2) {
01906                 // Start by determining the region to operate on
01907                 int xs=(int)floor(block->get_xsize()*scale/2.0);
01908                 int ys=(int)floor(block->get_ysize()*scale/2.0);
01909                 int x0=(int)center[0]-xs;
01910                 int x1=(int)center[0]+xs;
01911                 int y0=(int)center[1]-ys;
01912                 int y1=(int)center[1]+ys;
01913 
01914                 if (x1<0||y1<0||x0>get_xsize()||y0>get_ysize()) return; // object is completely outside the target volume
01915 
01916                 // make sure we stay inside the volume
01917                 if (x0<0) x0=0;
01918                 if (y0<0) y0=0;
01919                 if (x1>=get_xsize()) x1=get_xsize()-1;
01920                 if (y1>=get_ysize()) y1=get_ysize()-1;
01921 
01922                 float bx=block->get_xsize()/2.0f;
01923                 float by=block->get_ysize()/2.0f;
01924 
01925                 for (int x=x0; x<=x1; x++) {
01926                         for (int y=y0; y<=y1; y++) {
01927                                 data[x + y * nx] +=
01928                                         block->sget_value_at_interp((x-center[0])/scale+bx,(y-center[1])/scale+by);
01929                         }
01930                 }
01931                 update();
01932         }
01933         else {
01934                 LOGERR("insert_scaled_sum supports only 2D and 3D data");
01935                 throw ImageDimensionException("2D/3D only");
01936         }
01937 
01938         EXITFUNC;
01939 }
01940 //                      else if ( m == 0 )
01941 //                      {
01942 //                              if ( n_f == -ny/2 )
01943 //                              {
01944 //                                      t2++;
01945 // //                                   continue;
01946 //                                      for (int y = 0; y < return_slice->get_ysize(); ++y) {
01947 //                                              for (int x = 0; x < return_slice->get_xsize(); ++x) {
01948 //                                                      double cur_val = return_slice->get_value_at(x,y);
01949 //                                                      return_slice->set_value_at(x,y,cur_val+dat[idx]*std::pow(-1.0f,y));
01950 //                                              }
01951 //                                      }
01952 //                                      if (phase > 0.01 ) cout << "foo 2 " << phase << " " << amp << " " << dat[idx] << endl;
01953 //                              }
01954 //                              else
01955 //                              {
01956 //                                      if ( n_f < 1 ) continue;
01957 //                                      t3++;
01958 //                                      for (int y = 0; y < return_slice->get_ysize(); ++y) {
01959 //                                              for (int x = 0; x < return_slice->get_xsize(); ++x) {
01960 //                                                      double cur_val = return_slice->get_value_at(x,y);
01961 //                                                      return_slice->set_value_at(x,y,cur_val+2*amp*cos(ndash*y+phase));
01962 //                                              }
01963 //                                      }
01964 //                              }
01965 //                      }
01966 //                      else if ( n_f == -ny/2 )
01967 //                      {
01968 //                              if ( m == ((nx-2)/2) )
01969 //                              {
01970 //                                      t4++;
01971 //                                      for (int y = 0; y < return_slice->get_ysize(); ++y) {
01972 //                                              for (int x = 0; x < return_slice->get_xsize(); ++x) {
01973 //                                                      double cur_val = return_slice->get_value_at(x,y);
01974 //                                                      return_slice->set_value_at(x,y,cur_val+dat[idx]*std::pow(-1.0f,x+y));
01975 //                                              }
01976 //                                      }
01977 //                                      if (phase > 0.01 ) cout << "foo 4 " << phase << " " << amp << " " << dat[idx] << endl;
01978 //                              }
01979 //                              else
01980 //                              {
01981 //                                      t5++;
01982 //                                      for (int y = 0; y < return_slice->get_ysize(); ++y) {
01983 //                                              for (int x = 0; x < return_slice->get_xsize(); ++x) {
01984 //                                                      double cur_val = return_slice->get_value_at(x,y);
01985 //                                                      return_slice->set_value_at(x,y,cur_val+2*amp*cos(mdash*x+phase));
01986 //                                              }
01987 //                                      }
01988 //                              }
01989 //                      }
01990 //                      else if ( n_f == 0 )
01991 //                      {
01992 //                              if ( m == ((nx-2)/2) )
01993 //                              {
01994 //                                      t6++;
01995 //                                      for (int y = 0; y < return_slice->get_ysize(); ++y) {
01996 //                                              for (int x = 0; x < return_slice->get_xsize(); ++x) {
01997 //                                                      double cur_val = return_slice->get_value_at(x,y);
01998 //                                                      return_slice->set_value_at(x,y,cur_val+dat[idx]*std::pow(-1.0f,x));
01999 //                                              }
02000 //                                      }
02001 //                                      if (phase > 0.01 ) cout << "foo 3 " << phase << " " << amp << " " << dat[idx] << endl;
02002 //                              }
02003 //                              else
02004 //                              {
02005 //                                      t7++;
02006 //                                      for (int y = 0; y < return_slice->get_ysize(); ++y) {
02007 //                                              for (int x = 0; x < return_slice->get_xsize(); ++x) {
02008 //                                                      double cur_val = return_slice->get_value_at(x,y);
02009 //                                                      return_slice->set_value_at(x,y,cur_val+2*amp*cos(mdash*x+M_PI*y + phase));
02010 //                                              }
02011 //                                      }
02012 //                              }
02013 //                      }
02014 //                      else if ( m == ((nx-2)/2) )
02015 //                      {
02016 //                              if ( n_f < 1 ) continue;
02017 //                              t8++;
02018 //                              for (int y = 0; y < return_slice->get_ysize(); ++y) {
02019 //                                      for (int x = 0; x < return_slice->get_xsize(); ++x) {
02020 //                                              double cur_val = return_slice->get_value_at(x,y);
02021 //                                              return_slice->set_value_at(x,y,cur_val+2*amp*cos(ndash*y+M_PI*x+phase));
02022 //                                      }
02023 //                              }
02024 //                      }
02025 // }

Generated on Thu Mar 10 22:56:40 2011 for EMAN2 by  doxygen 1.4.7