46#ifdef EMAN2_USING_CUDA
106 ret->set_size(
nx,
ny,
nz);
127 if (abs(
x)>=
nx/2 || abs(
y)>
ny/2)
return std::complex<float>(0,0);
135 if (abs(
x)>=
nx/2 || abs(
y)>
ny/2 || abs(z)>
nz/2)
return std::complex<float>(0,0);
139 return std::complex<float>(
rdata[idx],-
rdata[idx+1]);
143 return std::complex<float>(
rdata[idx],
rdata[idx+1]);
147 if (abs(
x)>=
nx/2 || abs(
y)>
ny/2 || abs(z)>
nz/2)
return nxyz;
149 return -
x*2+(
y<=0?-
y:
ny-
y)*(
size_t)
nx+(z<=0?-z:
nz-z)*(
size_t)
nxy;
154size_t EMData::get_complex_index(
int x,
int y,
int z,
const int &subx0,
const int &suby0,
const int &subz0,
const int &fullnx,
const int &fullny,
const int &fullnz)
const {
155if (abs(
x)>=fullnx/2 || abs(
y)>fullny/2 || abs(z)>fullnz/2)
return nxyz;
165if (
x<subx0||
y<suby0||z<subz0||x>=subx0+
nx||
y>=suby0+
ny||z>=subz0+
nz)
return nxyz;
167return (
x-subx0)*2+(
y-suby0)*(
size_t)
nx+(z-subz0)*(
size_t)
nx*(size_t)
ny;
172 if (abs(
x)>=
nx/2 || abs(
y)>
ny/2)
return;
187 else if (
x==
nx/2-1) {
209if (abs(
x)>=
nx/2 || abs(
y)>
ny/2 || abs(z)>
nz/2)
return;
217 rdata[0]=(float)val.real();
222 size_t idx=(
y<=0?-
y:
ny-
y)*(
size_t)
nx+(z<=0?-z:
nz-z)*(
size_t)
nxy;
223 rdata[idx]=(float)val.real();
224 rdata[idx+1]=(float)-val.imag();
233 size_t idx=
nx-2+(
y<=0?-
y:
ny-
y)*(
size_t)
nx+(z<=0?-z:
nz-z)*(
size_t)
nxy;
234 rdata[idx]=(float)val.real();
235 rdata[idx+1]=(float)-val.imag();
238 idx=-
x*2+(
y<=0?-
y:
ny-
y)*(
size_t)
nx+(z<=0?-z:
nz-z)*(
size_t)
nxy;
239 rdata[idx]=(float)val.real();
240 rdata[idx+1]=-(float)val.imag();
245rdata[idx]=(float)val.real();
246rdata[idx+1]=(float)val.imag();
252if (abs(
x)>=
nx/2 || abs(
y)>
ny/2 || abs(z)>
nz/2)
return nxyz;
259 rdata[0]+=(float)val.real();
264 size_t idx=(
y<=0?-
y:
ny-
y)*(
size_t)
nx+(z<=0?-z:
nz-z)*(
size_t)
nxy;
266 rdata[idx]+=(float)val.real();
267 rdata[idx+1]+=(float)-val.imag();
271 rdata[
nx-2]+=(float)val.real();
276 size_t idx=
nx-2+(
y<=0?-
y:
ny-
y)*(
size_t)
nx+(z<=0?-z:
nz-z)*(
size_t)
nxy;
278 rdata[idx]+=(float)val.real();
279 rdata[idx+1]+=(float)-val.imag();
282 idx=-
x*2+(
y<=0?-
y:
ny-
y)*(
size_t)
nx+(z<=0?-z:
nz-z)*(
size_t)
nxy;
284 rdata[idx]+=(float)val.real();
285 rdata[idx+1]+=-(float)val.imag();
291rdata[idx]+=(float)val.real();
292rdata[idx+1]+=(float)val.imag();
297size_t EMData::add_complex_at(
int x,
int y,
int z,
const int &subx0,
const int &suby0,
const int &subz0,
const int &fullnx,
const int &fullny,
const int &fullnz,
const std::complex<float> &val) {
298if (abs(
x)>=fullnx/2 || abs(
y)>fullny/2 || abs(z)>fullnz/2)
return nxyz;
321if (
x<subx0||
y<suby0||z<subz0||x>=subx0+
nx||
y>=suby0+
ny||z>=subz0+
nz)
return nxyz;
323size_t idx=(
x-subx0)*2+(
y-suby0)*(size_t)
nx+(z-subz0)*(size_t)
nx*
ny;
324rdata[idx]+=(float)val.real();
325rdata[idx+1]+=cc*(float)val.imag();
341 for (
size_t i = 0; i < size; i++) {
342 if (data[i]) data[i] += f;
346 for (
size_t i = 0; i < size; i++) {
358 size_t size = (size_t)
nx*
ny*
nz;
361 for(
size_t i=0; i<size; i+=2)
363 if (data[i]) data[i] += f;
368 for(
size_t i=0; i<size; i+=2)
388 if (
nx != image.get_xsize() ||
ny != image.get_ysize() ||
nz != image.get_zsize()) {
391 else if( (
is_real()^image.is_real()) ==
true )
397 const float *src_data = image.get_data();
401 for (
size_t i = 0; i < size; i++) {
402 data[i] += src_data[i];
413 if (
nx != image.get_xsize() ||
ny != image.get_ysize() ||
nz != image.get_zsize()) {
416 else if( this->
is_complex() || image.is_complex() )
422 const float *src_data = image.get_data();
426 for (
size_t i = 0; i < size; i++) {
427 data[i] += src_data[i]*src_data[i];
438 if (
nx != image.get_xsize() ||
ny != image.get_ysize() ||
nz != image.get_zsize()) {
441 else if( this->
is_complex() || image.is_complex() )
447 const float *src_data = image.get_data();
451 for (
size_t i = 0; i < size; i++) {
452 data[i] -= src_data[i]*src_data[i];
464#ifdef EMAN2_USING_CUDA
465 if (EMData::usecuda == 1 && cudarwdata) {
479 for (
size_t i = 0; i < size; i++) {
490 for(
size_t i=0; i<size; i+=2 )
511 if (
nx != em.get_xsize() ||
ny != em.get_ysize() ||
nz != em.get_zsize()) {
514 else if( (
is_real()^em.is_real()) ==
true )
519 const float *src_data = em.get_data();
523 for (
size_t i = 0; i < size; i++) {
524 data[i] -= src_data[i];
541#ifdef EMAN2_USING_CUDA
542 if (EMData::usecuda == 1 && cudarwdata) {
551 for (
size_t i = 0; i < size; i++) {
564 if (
nx != em.get_xsize() ||
ny != em.get_ysize() ||
nz != em.get_zsize()) {
567 else if( (
is_real()^em.is_real()) ==
true )
573 const float *src_data = em.get_data();
576 if(
is_real() || prevent_complex_multiplication )
578 for (
size_t i = 0; i < size; i++) {
579 data[i] *= src_data[i];
592 typedef std::complex<float> comp;
593 const float *src_data = em.get_data();
595 for(
size_t i = 0; i <
nxyz; i+=2 )
597 comp c_src( src_data[i], src_data[i+1] );
598 comp c_rdat( data[i], data[i+1] );
599 comp c_result = c_src * c_rdat;
600 data[i] = c_result.real();
601 data[i+1] = c_result.imag();
614 const float *src_data = em.get_data();
616 size_t i_radius = radius;
626 }
else if ( ndim == 2 ) {
631 int s_nx = em.get_xsize();
632 int s_nxy = s_nx*em.get_ysize();
634 size_t r_size =
nxyz;
635 int s_size = s_nxy*em.get_zsize();
638 for (
size_t k = 0; k < k_radius; ++k ) {
639 for (
size_t j = 0; j < j_radius; j++) {
640 for (
size_t i = 0; i < i_radius; i++) {
641 int r_idx = k*
nxy + j*
nx + i;
642 int s_idx = k*s_nxy + j*s_nx + i;
643 data[r_idx] *= src_data[s_idx];
644 data[r_size-r_idx-1] *= src_data[s_size-s_idx-1];
669 if (
nx != em.get_xsize() ||
ny != em.get_ysize() ||
nz != em.get_zsize()) {
672 else if( (
is_real()^em.is_real()) ==
true )
677 const float *src_data = em.get_data();
683 for (
size_t i = 0; i < size; i++) {
684 if(src_data[i] != 0) {
685 data[i] /= src_data[i];
688 if (data[i]==0)
continue;
695 typedef std::complex<float> comp;
696 for(
size_t i = 0; i < size; i+=2 )
698 comp c_src( src_data[i], src_data[i+1] );
699 comp c_rdat( data[i], data[i+1] );
700 comp c_result = c_rdat / c_src;
701 data[i] = c_result.real();
702 data[i+1] = c_result.imag();
715 if (
nx != em.get_xsize() ||
ny != em.get_ysize() ||
nz != em.get_zsize()) {
718 if( !
is_real() || !em.is_real() )
739 float r = -dot_cmp.
cmp(
this, with);
754 ret->set_size(
nx, 1, 1);
755 memcpy(ret->get_data(),
get_data() +
nx * row_index,
nx *
sizeof(
float));
769 if (d->get_ndim() != 1) {
774 float *src = d->get_data();
775 memcpy(dst +
nx * row_index, src,
nx *
sizeof(
float));
789 ret->set_size(
ny, 1, 1);
790 float *dst = ret->get_data();
793 for (
int i = 0; i <
ny; i++) {
794 dst[i] = src[i *
nx + col_index];
810 if (d->get_ndim() != 1) {
815 float *src = d->get_data();
817 for (
int i = 0; i <
ny; i++) {
818 dst[i *
nx + n] = src[i];
827 if (
x < 0)
x =
nx +
x;
833 if (
x < 0)
x =
nx +
x;
834 if (
y < 0)
y =
ny +
y;
842#ifdef EMAN2_USING_CUDA
843 if(EMData::usecuda == 1 && cudarwdata){
852 if (lx < 0) lx =
nx + lx;
853 if (ly < 0) ly =
ny + ly;
854 if (lz < 0) lz =
nz + lz;
861 if (
x < 0)
x =
nx -
x;
867 if (
x < 0)
x =
nx -
x;
868 if (
y < 0)
y =
ny -
y;
878 if (lx < 0) lx =
nx + lx;
879 if (ly < 0) ly =
ny + ly;
880 if (lz < 0) lz =
nz + lz;
887 if (x < 0 || x >=
nx || y < 0 || y >=
ny || z < 0 || z >=
nz) {
890 return get_data()[(size_t)
x + (
size_t)
y * (size_t)
nx + (
size_t)z * (size_t)
nxy];
896 if (x < 0 || x >=
nx || y < 0 || y >=
ny) {
959 std::complex<float> p[9];
960 for (
int y=0,i=0;
y<3;
y++) {
961 for (
int x=0;
x<3;
x++,i++) {
987 xx -
x, yy -
y, zz - z);
1019 LOGERR(
"divided by zero");
1022 *
this *= (1.0f / n);
1072 for(
int i=1; i<n; i++ ) {
1093 float * new_data = r->get_data();
1096 for (
size_t i = 0; i < size; ++i) {
1123 float * new_data = r->get_data();
1126 for (
size_t i = 0; i < size; ++i) {
1153 float * new_data = r->get_data();
1156 for (
size_t i = 0; i < size; ++i) {
1189 throw InvalidCallException(
"This image is in amplitude/phase format, this function call require a complex image in real/imaginary format.");
1194 e->set_size(
nx/2,
ny,
nz);
1195 float * edata = e->get_data();
1198 for(
int i=0; i<
nx; ++i )
1200 for(
int j=0; j<
ny; ++j )
1202 for(
int k=0; k<
nz; ++k )
1207 idx1 = i/2+j*(
nx/2)+k*(
nx/2)*
ny;
1209 edata[idx1] = data[idx2];
1216 e->set_complex(
false);
1217 if(e->get_ysize()==1 && e->get_zsize()==1) {
1218 e->set_complex_x(
false);
1234 throw InvalidCallException(
"No imaginary part for a real image, this function call require a complex image.");
1238 throw InvalidCallException(
"This image is in amplitude/phase format, this function call require a complex image in real/imaginary format.");
1243 e->set_size(
nx/2,
ny,
nz);
1244 float * edata = e->get_data();
1246 for(
int i=0; i<
nx; i++ ) {
1247 for(
int j=0; j<
ny; j++ ) {
1248 for(
int k=0; k<
nz; k++ ) {
1251 edata[i/2+j*(
nx/2)+k*(
nx/2)*
ny] = data[i+j*
nx+k*
nx*
ny];
1258 e->set_complex(
false);
1259 if(e->get_ysize()==1 && e->get_zsize()==1) {
1260 e->set_complex_x(
false);
1280 float *edata = e->get_data();
1283 for(
int i=0; i<
nx; ++i ) {
1284 for(
int j=0; j<
ny; ++j ) {
1285 for(
int k=0; k<
nz; ++k ) {
1287 edata[idx] = std::abs(data[idx]);
1296 throw InvalidCallException(
"This image is in amplitude/phase format, this function call require a complex image in real/imaginary format.");
1301 e->set_size(
nx/2,
ny,
nz);
1302 float * edata = e->get_data();
1305 for(
int i=0; i<
nx; ++i )
1307 for(
int j=0; j<
ny; ++j )
1309 for(
int k=0; k<
nz; ++k )
1313 idx1 = i/2+j*(
nx/2)+k*(
nx/2)*
ny;
1317 std::sqrt(data[idx2]*data[idx2]+data[idx2+1]*data[idx2+1]);
1324 e->set_complex(
false);
1325 if(e->get_ysize()==1 && e->get_zsize()==1) {
1326 e->set_complex_x(
false);
1342 throw InvalidCallException(
"No imaginary part for a real image, this function call require a complex image.");
1346 throw InvalidCallException(
"This image is in real/imaginary format, this function call require a complex image in amplitude/phase format.");
1352 e->set_size(
nx/2,
ny,
nz);
1353 float * edata = e->get_data();
1356 for(
int i=0; i<
nx; ++i )
1358 for(
int j=0; j<
ny; ++j )
1360 for(
int k=0; k<
nz; ++k )
1364 idx1 = i/2+j*(
nx/2)+k*(
nx/2)*
ny;
1367 edata[idx1] = data[idx2];
1374 e->set_complex(
false);
1375 if(e->get_ysize()==1 && e->get_zsize()==1) {
1376 e->set_complex_x(
false);
1392 throw InvalidCallException(
"No imaginary part for a real image, this function call require a complex image.");
1397 throw InvalidCallException(
"This image is in real/imaginary format, this function call require a complex image in amplitude/phase format.");
1403 e->set_size(
nx/2,
ny,
nz);
1404 float * edata = e->get_data();
1407 for(
int i=0; i<
nx; ++i ) {
1408 for(
int j=0; j<
ny; ++j ) {
1409 for(
int k=0; k<
nz; ++k ) {
1411 idx1 = i/2+j*(
nx/2)+k*(
nx/2)*
ny;
1414 edata[idx1] = data[idx2];
1421 e->set_complex(
false);
1422 if(e->get_ysize()==1 && e->get_zsize()==1) {
1423 e->set_complex_x(
false);
1443 e->set_size(
nx*2,
ny,
nz);
1445 for(
int k=0; k<
nz; ++k ) {
1446 for(
int j=0; j<
ny; ++j ) {
1447 for(
int i=0; i<
nx; ++i ) {
1448 (*e)(i*2,j,k) = (*
this)(i,j,k);
1449 (*e)(i*2+1,j,k) = img;
1454 e->set_complex(
true);
1455 if(e->get_ysize()==1 && e->get_zsize()==1) {
1456 e->set_complex_x(
true);
1502#ifdef EMAN2_USING_CUDA
1503 if(EMData::usecuda == 1 && cudarwdata){
1514 std::fill(data,data+
get_size(),value);
Use dot product of 2 same-size images to do the comparison.
float cmp(EMData *image, EMData *with) const
To compare 'image' with another image passed in through its parameters.
EMData stores an image's data and defines core image processing routines.
EMData * rot_fp
This is a cached rotational footprint, can save much time.
EMData()
For all image I/O.
float * supp
supplementary data array
float * rdata
image real data
Dict attr_dict
to store all image header info
int flags
CTF data All CTF data become attribute ctf(vector<float>) in attr_dict –Grant Tang.
Vec3f all_translation
translation from the original location
static void em_free(void *data)
static int fast_floor(float x)
A fast way to calculate a floor, which is largest integral value not greater than argument.
static std::complex< float > gauss3_interpolate_cmplx(std::complex< float > *p, float t, float u)
Calculate 3x3 Gaussian interpolation.
static std::complex< float > gauss_interpolate_cmplx(std::complex< float > p1, std::complex< float > p2, std::complex< float > p3, std::complex< float > p4, float t, float u)
Calculate 2x2 Gaussian interpolation.
static std::complex< float > bilinear_interpolate_cmplx(std::complex< float > p1, std::complex< float > p2, std::complex< float > p3, std::complex< float > p4, float t, float u)
Calculate bilinear interpolation.
static float trilinear_interpolate(float p1, float p2, float p3, float p4, float p5, float p6, float p7, float p8, float t, float u, float v)
Calculate trilinear interpolation.
static int get_min(int f1, int f2)
Get the minimum of 2 numbers.
static float bilinear_interpolate(float p1, float p2, float p3, float p4, float t, float u)
Calculate bilinear interpolation.
float get_value_at_wrap_cuda(float *data, int tx, int ty, int tz, int nx, int ny, int nz)
void emdata_processor_mult(float *data, const float &mult, const int nx, const int ny, const int nz)
void subtract_cuda(float *data, float f, const int nx, const int ny, const int nz)
void to_value_cuda(float *data, const float value, const int nx, const int ny, const int nz)
void addsquare(const EMData &image)
add the squared value of each pixel from a same-size image to this image.
float sget_value_at_interp(float x, float y) const
Get pixel density value at interpolation of (x,y).
void set_value_at_index(size_t i, float v)
Set the pixel density value at index.
std::complex< float > get_complex_at_interp(float x, float y) const
Gets bilinear interpolated complex values.
EMData * copy() const
This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata....
size_t add_complex_at(const int &x, const int &y, const int &z, const std::complex< float > &val)
Add complex<float> value at x,y,z.
void to_zero()
Set all the pixel value = 0.
EMData & operator+=(float n)
size_t get_complex_index(const int &x, const int &y, const int &z) const
Get complex<float> index for coords x,y,z.
EMData * get_col(int col_index) const
Get one column of a 2D images.
EMData * log() const
return natural logarithm image for a image
void mult(int n)
multiply an integer number to each pixel value of the image.
EMData * absi() const
For a real image, it returns a same size image with abs() of each pixel.
void to_value(const float &value)
set all the pixel values to a value.
EMData * power(int n) const
return a image to the power of n
float get_value_at_wrap(int x, int y, int z) const
Get the pixel density value at coordinates (x,y,z).
void add(float f, int keepzero=0)
add a number to each pixel value of the image.
void sub(float f)
subtract a float number to each pixel value of the image.
EMData * sqrt() const
return square root of current image
void to_one()
set all the pixel values = 1.
EMData * real2complex(float img=0.0f) const
create a complex image from a real image, this complex image is in real/imaginary format
float sget_value_at(Vec3i v)
Vec3i version of save routines below.
EMData * log10() const
return base 10 logarithm image for a image
float dot(EMData *with)
Dot product 2 images.
EMData * amplitude() const
return amplitude part of a complex image as a real image format
void set_row(const EMData *data, int row_index)
Set one row of a 1D/2D image.
void mult_ri(const EMData &image)
multiply each complex RI pixel of this image with each pixel of some other same-size image.
EMData * imag() const
return imaginary part of a complex image as a real image format.
std::complex< float > get_complex_at_3ginterp(float x, float y) const
Gets 3x3 Gaussian interpolated complex values note that with Gaussian interpolation,...
float get_value_at_index(size_t i) const
Get the pixel density value at index i.
void set_complex_at(const int &x, const int &y, const std::complex< float > &val)
Set complex<float> value at x,y.
void free_memory()
Free memory associated with this EMData Called in destructor and in assignment operator.
EMData * real() const
return real part of a complex image as a real image format, if this image is a real image,...
void mult_complex_efficient(const EMData &em, const int radius)
std::complex< float > get_complex_at(const int &x, const int &y) const
Get complex<float> value at x,y.
void subsquare(const EMData &image)
subtract the squared value of each pixel from a same-size image to this image.
EMData & operator*=(float n)
void update_min(const EMData &image)
Replaces the value of each pixel with the minimum of the current value and the value in the provided ...
EMData * phase() const
return phase part of a complex image as a real image format
void div(float f)
make each pixel value divided by a float number.
EMData * copy_head() const
Make an image with a copy of the current image's header.
std::complex< float > get_complex_at_ginterp(float x, float y) const
Gets 2x2 Gaussian interpolated complex values note that with Gaussian interpolation,...
void free_rdata()
Free rdata memory associated with this EMData Called in CUDA.
void set_col(const EMData *data, int col_index)
Set one column of a 2D image.
EMData & operator/=(float n)
EMData & operator-=(float n)
EMData * get_row(int row_index) const
Get one row of a 1D/2D image.
#define InvalidValueException(val, desc)
#define ImageFormatException(desc)
#define ImageDimensionException(desc)
#define InvalidCallException(desc)
#define NullPointerException(desc)