Xmipp  v3.23.11-Nereus
Modules | Classes | Functions | Variables
Fourier projection
Collaboration diagram for Fourier projection:

Modules

 Projections (2D Image + Euler angles)
 

Classes

class  Projection
 

Functions

void Projection::reset (int Ydim, int Xdim)
 
void Projection::setAngles (double _rot, double _tilt, double _psi)
 
void Projection::read (const FileName &fn, const bool only_apply_shifts=false, DataMode datamode=DATA, MDRow *row=nullptr)
 
void Projection::assign (const Projection &P)
 
 FourierProjector::FourierProjector (double paddFactor, double maxFreq, int degree)
 
 FourierProjector::FourierProjector (MultidimArray< double > &V, double paddFactor, double maxFreq, int BSplinedegree)
 
void FourierProjector::project (double rot, double tilt, double psi, const MultidimArray< double > *ctf=nullptr)
 
void FourierProjector::updateVolume (MultidimArray< double > &V)
 
void FourierProjector::produceSideInfo ()
 Prepare the Spline coefficients and projection space. More...
 
void FourierProjector::produceSideInfoProjection ()
 Prepare projection space. More...
 

Variables

Matrix1D< double > Projection::direction
 
Matrix2D< double > Projection::euler
 
Matrix2D< double > Projection::eulert
 
double FourierProjector::paddingFactor
 Padding factor. More...
 
double FourierProjector::maxFrequency
 Maximum Frequency for pixels. More...
 
double FourierProjector::BSplineDeg
 The order of B-Spline for interpolation. More...
 
FourierTransformer FourierProjector::transformer2D
 
MultidimArray< double > * FourierProjector::volume
 
MultidimArray< double > FourierProjector::VfourierRealCoefs
 
MultidimArray< double > FourierProjector::VfourierImagCoefs
 
MultidimArray< std::complex< double > > FourierProjector::projectionFourier
 
Image< double > FourierProjector::projection
 
MultidimArray< double > FourierProjector::phaseShiftImgB
 
MultidimArray< double > FourierProjector::phaseShiftImgA
 
int FourierProjector::volumeSize
 
int FourierProjector::volumePaddedSize
 
Matrix2D< double > FourierProjector::E
 

Detailed Description

Function Documentation

◆ assign()

void Projection::assign ( const Projection P)

Another function for assignment.

Definition at line 63 of file fourier_projection.cpp.

64 {
65  *this = P;
66 }

◆ FourierProjector() [1/2]

FourierProjector::FourierProjector ( double  paddFactor,
double  maxFreq,
int  degree 
)

Definition at line 68 of file fourier_projection.cpp.

69 {
70  paddingFactor = paddFactor;
71  maxFrequency = maxFreq;
72  BSplineDeg = degree;
73  volume = nullptr;
74 }
double maxFrequency
Maximum Frequency for pixels.
double BSplineDeg
The order of B-Spline for interpolation.
MultidimArray< double > * volume
double paddingFactor
Padding factor.

◆ FourierProjector() [2/2]

FourierProjector::FourierProjector ( MultidimArray< double > &  V,
double  paddFactor,
double  maxFreq,
int  BSplinedegree 
)

Definition at line 76 of file fourier_projection.cpp.

77 {
78  paddingFactor = paddFactor;
79  maxFrequency = maxFreq;
80  BSplineDeg = degree;
81  updateVolume(V);
82 }
void updateVolume(MultidimArray< double > &V)
double maxFrequency
Maximum Frequency for pixels.
double BSplineDeg
The order of B-Spline for interpolation.
double paddingFactor
Padding factor.

◆ produceSideInfo()

void FourierProjector::produceSideInfo ( )

Prepare the Spline coefficients and projection space.

Definition at line 247 of file fourier_projection.cpp.

248 {
249  // Zero padding
250  MultidimArray<double> Vpadded;
251  auto paddedDim=(int)(paddingFactor*volumeSize);
252  volume->window(Vpadded,FIRST_XMIPP_INDEX(paddedDim),FIRST_XMIPP_INDEX(paddedDim),FIRST_XMIPP_INDEX(paddedDim),
253  LAST_XMIPP_INDEX(paddedDim),LAST_XMIPP_INDEX(paddedDim),LAST_XMIPP_INDEX(paddedDim));
254  volume->clear();
255  // Make Fourier transform, shift the volume origin to the volume center and center it
257  FourierTransformer transformer3D;
258  transformer3D.completeFourierTransform(Vpadded,Vfourier);
259  ShiftFFT(Vfourier, FIRST_XMIPP_INDEX(XSIZE(Vpadded)), FIRST_XMIPP_INDEX(YSIZE(Vpadded)), FIRST_XMIPP_INDEX(ZSIZE(Vpadded)));
260  CenterFFT(Vfourier,true);
261  Vfourier.setXmippOrigin();
262 
263  // Compensate for the Fourier normalization factor
264  double K=(double)(XSIZE(Vpadded)*XSIZE(Vpadded)*XSIZE(Vpadded))/(double)(volumeSize*volumeSize);
266  DIRECT_MULTIDIM_ELEM(Vfourier,n)*=K;
267  Vpadded.clear();
268  // Compute Bspline coefficients
270  {
271  MultidimArray< double > VfourierRealAux;
272  MultidimArray< double > VfourierImagAux;
273  Complex2RealImag(Vfourier, VfourierRealAux, VfourierImagAux);
274  Vfourier.clear();
276 
277  // Release memory as soon as you can
278  VfourierRealAux.clear();
279 
280  // Remove all those coefficients we are sure we will not use during the projections
282  int idxMax=maxFrequency*XSIZE(VfourierRealCoefs)+10; // +10 is a safety guard
283  idxMax=std::min(FINISHINGX(VfourierRealCoefs),idxMax);
284  int idxMin=std::max(-idxMax,STARTINGX(VfourierRealCoefs));
285  VfourierRealCoefs.selfWindow(idxMin,idxMin,idxMin,idxMax,idxMax,idxMax);
286 
288  VfourierImagAux.clear();
289  VfourierImagCoefs.selfWindow(idxMin,idxMin,idxMin,idxMax,idxMax,idxMax);
290  }
291  else {
294  }
295 
297 }
void selfWindow(int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T init_value=0)
#define YSIZE(v)
void min(Image< double > &op1, const Image< double > &op2)
#define FINISHINGX(v)
void ShiftFFT(MultidimArray< std::complex< double > > &v, double xshift)
Definition: xmipp_fft.cpp:311
#define STARTINGX(v)
void produceSideInfoProjection()
Prepare projection space.
void CenterFFT(MultidimArray< T > &v, bool forward)
Definition: xmipp_fft.h:291
MultidimArray< double > VfourierRealCoefs
void window(MultidimArray< T1 > &result, int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T1 init_value=0) const
double maxFrequency
Maximum Frequency for pixels.
void produceSplineCoefficients(int SplineDegree, MultidimArray< double > &coeffs, const MultidimArray< T > &V1)
MultidimArray< double > VfourierImagCoefs
#define XSIZE(v)
void max(Image< double > &op1, const Image< double > &op2)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define ZSIZE(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
double BSplineDeg
The order of B-Spline for interpolation.
#define FIRST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:439
void completeFourierTransform(T &v, T1 &V)
Definition: xmipp_fftw.h:315
constexpr int K
MultidimArray< double > * volume
#define LAST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:448
int * n
void Complex2RealImag(const MultidimArray< std::complex< double > > &in, MultidimArray< double > &real, MultidimArray< double > &imag)
Definition: xmipp_fft.cpp:103
double paddingFactor
Padding factor.

◆ produceSideInfoProjection()

void FourierProjector::produceSideInfoProjection ( )

Prepare projection space.

Definition at line 299 of file fourier_projection.cpp.

300 {
301  // Allocate memory for the 2D Fourier transform
302  projection().initZeros(volumeSize,volumeSize);
303  projection().setXmippOrigin();
305 
306  // Calculate phase shift terms
309  double shift=-FIRST_XMIPP_INDEX(volumeSize);
310  double xxshift = -2 * PI * shift / volumeSize;
311  for (size_t i=0; i<YSIZE(projectionFourier); ++i)
312  {
313  double phasey=(double)(i) * xxshift;
314  for (size_t j=0; j<XSIZE(projectionFourier); ++j)
315  {
316  // Phase shift to move the origin of the image to the corner
317  double dotp = (double)(j) * xxshift + phasey;
318  //sincos(dotp,&DIRECT_A2D_ELEM(phaseShiftImgB,i,j),&DIRECT_A2D_ELEM(phaseShiftImgA,i,j));
319  DIRECT_A2D_ELEM(phaseShiftImgB,i,j) = sin(dotp);
320  DIRECT_A2D_ELEM(phaseShiftImgA,i,j) = cos(dotp);
321  }
322  }
323 }
#define YSIZE(v)
#define DIRECT_A2D_ELEM(v, i, j)
MultidimArray< double > phaseShiftImgB
MultidimArray< double > phaseShiftImgA
#define i
FourierTransformer transformer2D
#define XSIZE(v)
void FourierTransform(T &v, T1 &V, bool getCopy=true)
Definition: xmipp_fftw.h:166
MultidimArray< std::complex< double > > projectionFourier
#define j
#define FIRST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:439
Image< double > projection
void initZeros(const MultidimArray< T1 > &op)
#define PI
Definition: tools.h:43

◆ project()

void FourierProjector::project ( double  rot,
double  tilt,
double  psi,
const MultidimArray< double > *  ctf = nullptr 
)

This method gets the volume's Fourier and the Euler's angles as the inputs and interpolates the related projection

Definition at line 91 of file fourier_projection.cpp.

92 {
93  double freqy;
94  double freqx;
95  std::complex< double > f;
96  Euler_angles2matrix(rot,tilt,psi,E);
98  double maxFreq2=maxFrequency*maxFrequency;
99  auto Xdim=(int)XSIZE(VfourierRealCoefs);
100  auto Ydim=(int)YSIZE(VfourierRealCoefs);
101  auto Zdim=(int)ZSIZE(VfourierRealCoefs);
102 
103  for (size_t i=0; i<YSIZE(projectionFourier); ++i)
104  {
106  double freqy2=freqy*freqy;
107 
108  double freqYvol_X=MAT_ELEM(E,1,0)*freqy;
109  double freqYvol_Y=MAT_ELEM(E,1,1)*freqy;
110  double freqYvol_Z=MAT_ELEM(E,1,2)*freqy;
111  for (size_t j=0; j<XSIZE(projectionFourier); ++j)
112  {
113  // The frequency of pairs (i,j) in 2D
115 
116  // Do not consider pixels with high frequency
117  if ((freqy2+freqx*freqx)>maxFreq2)
118  continue;
119 
120  // Compute corresponding frequency in the volume
121  double freqvol_X=freqYvol_X+MAT_ELEM(E,0,0)*freqx;
122  double freqvol_Y=freqYvol_Y+MAT_ELEM(E,0,1)*freqx;
123  double freqvol_Z=freqYvol_Z+MAT_ELEM(E,0,2)*freqx;
124 
125  double c;
126  double d;
128  {
129  // 0 order interpolation
130  // Compute corresponding index in the volume
131  auto kVolume=(int)round(freqvol_Z*volumePaddedSize);
132  auto iVolume=(int)round(freqvol_Y*volumePaddedSize);
133  auto jVolume=(int)round(freqvol_X*volumePaddedSize);
134  c = A3D_ELEM(VfourierRealCoefs,kVolume,iVolume,jVolume);
135  d = A3D_ELEM(VfourierImagCoefs,kVolume,iVolume,jVolume);
136  }
138  {
139  // B-spline linear interpolation
140  double kVolume=freqvol_Z*volumePaddedSize;
141  double iVolume=freqvol_Y*volumePaddedSize;
142  double jVolume=freqvol_X*volumePaddedSize;
143  c=VfourierRealCoefs.interpolatedElement3D(jVolume,iVolume,kVolume);
144  d=VfourierImagCoefs.interpolatedElement3D(jVolume,iVolume,kVolume);
145  }
146  else
147  {
148  // B-spline cubic interpolation
149  double kVolume=freqvol_Z*volumePaddedSize;
150  double iVolume=freqvol_Y*volumePaddedSize;
151  double jVolume=freqvol_X*volumePaddedSize;
152 
153  // Commented for speed-up, the corresponding code is below
154  // c=VfourierRealCoefs.interpolatedElementBSpline3D(jVolume,iVolume,kVolume);
155  // d=VfourierImagCoefs.interpolatedElementBSpline3D(jVolume,iVolume,kVolume);
156 
157  // The code below is a replicate for speed reasons of interpolatedElementBSpline3D
158  double z=kVolume;
159  double y=iVolume;
160  double x=jVolume;
161 
162  // Logical to physical
166 
167  auto l1 = (int)ceil(x - 2);
168  int l2 = l1 + 3;
169 
170  auto m1 = (int)ceil(y - 2);
171  int m2 = m1 + 3;
172 
173  auto n1 = (int)ceil(z - 2);
174  int n2 = n1 + 3;
175 
176  c = d = 0.0;
177  double aux;
178  for (int nn = n1; nn <= n2; nn++)
179  {
180  int equivalent_nn=nn;
181  if (nn<0)
182  equivalent_nn=-nn-1;
183  else if (nn>=Zdim)
184  equivalent_nn=2*Zdim-nn-1;
185  double yxsumRe = 0.0;
186  double yxsumIm = 0.0;
187  for (int m = m1; m <= m2; m++)
188  {
189  int equivalent_m=m;
190  if (m<0)
191  equivalent_m=-m-1;
192  else if (m>=Ydim)
193  equivalent_m=2*Ydim-m-1;
194  double xsumRe = 0.0;
195  double xsumIm = 0.0;
196  for (int l = l1; l <= l2; l++)
197  {
198  double xminusl = x - (double) l;
199  int equivalent_l=l;
200  if (l<0)
201  equivalent_l=-l-1;
202  else if (l>=Xdim)
203  equivalent_l=2*Xdim-l-1;
204  auto CoeffRe = (double) DIRECT_A3D_ELEM(VfourierRealCoefs,equivalent_nn,equivalent_m,equivalent_l);
205  auto CoeffIm = (double) DIRECT_A3D_ELEM(VfourierImagCoefs,equivalent_nn,equivalent_m,equivalent_l);
206  BSPLINE03(aux,xminusl);
207  xsumRe += CoeffRe * aux;
208  xsumIm += CoeffIm * aux;
209  }
210 
211  double yminusm = y - (double) m;
212  BSPLINE03(aux,yminusm);
213  yxsumRe += xsumRe * aux;
214  yxsumIm += xsumIm * aux;
215  }
216 
217  double zminusn = z - (double) nn;
218  BSPLINE03(aux,zminusn);
219  c += yxsumRe * aux;
220  d += yxsumIm * aux;
221  }
222  }
223  // Phase shift to move the origin of the image to the corner
226  if (ctf!=nullptr)
227  {
228  double ctfij=DIRECT_A2D_ELEM(*ctf,i,j);
229  a*=ctfij;
230  b*=ctfij;
231  }
232 
233  // Multiply Fourier coefficient in volume times phase shift
234  double ac = a * c;
235  double bd = b * d;
236  double ab_cd = (a + b) * (c + d);
237 
238  // And store the multiplication
239  auto *ptrI_ij=(double *)&DIRECT_A2D_ELEM(projectionFourier,i,j);
240  *ptrI_ij = ac - bd;
241  *(ptrI_ij+1) = ab_cd - ac - bd;
242  }
243  }
245 }
#define YSIZE(v)
void inverseFourierTransform()
Definition: xmipp_fftw.cpp:329
void Euler_angles2matrix(T alpha, T beta, T gamma, Matrix2D< T > &A, bool homogeneous)
Definition: geometry.cpp:624
doublereal * c
static double * y
#define DIRECT_A2D_ELEM(v, i, j)
MultidimArray< double > phaseShiftImgB
MultidimArray< double > phaseShiftImgA
#define STARTINGX(v)
doublereal * x
#define i
doublereal * d
#define STARTINGY(v)
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define A3D_ELEM(V, k, i, j)
MultidimArray< double > VfourierRealCoefs
doublereal * b
double maxFrequency
Maximum Frequency for pixels.
FourierTransformer transformer2D
double * f
MultidimArray< double > VfourierImagCoefs
#define XSIZE(v)
#define ZSIZE(v)
double z
T interpolatedElement3D(double x, double y, double z, T outside_value=(T) 0) const
double BSplineDeg
The order of B-Spline for interpolation.
Matrix2D< double > E
#define DIRECT_A3D_ELEM(v, k, i, j)
__device__ float FFT_IDX2DIGFREQ(int idx, int size)
MultidimArray< std::complex< double > > projectionFourier
#define j
int m
int round(double x)
Definition: ap.cpp:7245
double psi(const double x)
void initZeros(const MultidimArray< T1 > &op)
#define STARTINGZ(v)
doublereal * a
#define BSPLINE03(y, x)
Definition: kernel.h:83

◆ read()

void Projection::read ( const FileName fn,
const bool  only_apply_shifts = false,
DataMode  datamode = DATA,
MDRow row = nullptr 
)

Read a Projection from file.

When a projection is read, the Euler matrices and perpendicular direction is computed and stored in the Projection structure.

Definition at line 50 of file fourier_projection.cpp.

52 {
53  Image<double>::read(fn, datamode);
54  if (row != nullptr)
55  applyGeo(*row, only_apply_shifts);
60 }
Matrix2D< double > eulert
double psi(const size_t n=0) const
void Euler_angles2matrix(T alpha, T beta, T gamma, Matrix2D< T > &A, bool homogeneous)
Definition: geometry.cpp:624
Matrix2D< double > euler
Matrix2D< T > transpose() const
Definition: matrix2d.cpp:1314
double rot(const size_t n=0) const
void selfTranspose()
Definition: matrix1d.h:944
double tilt(const size_t n=0) const
Matrix1D< double > direction
void applyGeo(const MDRow &row, bool only_apply_shifts=false, bool wrap=xmipp_transformation::WRAP) override
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
void getRow(size_t i, Matrix1D< T > &v) const
Definition: matrix2d.cpp:871

◆ reset()

void Projection::reset ( int  Ydim,
int  Xdim 
)

Init_zeros and move origin to center.

This function initialises the projection plane with 0's, and then moves the logical origin of the image to the physical center of the image (using the Xmipp conception of image center).

Definition at line 33 of file fourier_projection.cpp.

34 {
35  data.initZeros(Ydim, Xdim);
37 }
MultidimArray< double > data
Definition: xmipp_image.h:55
void initZeros(const MultidimArray< T1 > &op)

◆ setAngles()

void Projection::setAngles ( double  _rot,
double  _tilt,
double  _psi 
)

Set Euler angles for this projection.

The Euler angles are stored in the Xmipp header, then the pass matrices (Universe <—> Projection coordinate system) are computed, and the vector perpendicular to this projection plane is also calculated.

Definition at line 40 of file fourier_projection.cpp.

41 {
42  setEulerAngles(_rot, _tilt, _psi);
43  Euler_angles2matrix(_rot, _tilt, _psi, euler);
47 }
Matrix2D< double > eulert
void Euler_angles2matrix(T alpha, T beta, T gamma, Matrix2D< T > &A, bool homogeneous)
Definition: geometry.cpp:624
Matrix2D< double > euler
Matrix2D< T > transpose() const
Definition: matrix2d.cpp:1314
void selfTranspose()
Definition: matrix1d.h:944
Matrix1D< double > direction
void setEulerAngles(double rot, double tilt, double psi, const size_t n=0)
void getRow(size_t i, Matrix1D< T > &v) const
Definition: matrix2d.cpp:871

◆ updateVolume()

void FourierProjector::updateVolume ( MultidimArray< double > &  V)

Update volume

Definition at line 84 of file fourier_projection.cpp.

85 {
86  volume = &V;
89 }
#define XSIZE(v)
void produceSideInfo()
Prepare the Spline coefficients and projection space.
MultidimArray< double > * volume

Variable Documentation

◆ BSplineDeg

double FourierProjector::BSplineDeg

The order of B-Spline for interpolation.

Definition at line 119 of file fourier_projection.h.

◆ direction

Matrix1D< double > Projection::direction

Vector perpendicular to the projection plane. It is calculated as a function of rot and tilt.

Definition at line 62 of file fourier_projection.h.

◆ E

Matrix2D<double> FourierProjector::E

Definition at line 149 of file fourier_projection.h.

◆ euler

Matrix2D< double > Projection::euler

Matrix used to pass from the Universal coordinate system to the projection coordinate system.

Rp = euler * Ru

Definition at line 71 of file fourier_projection.h.

◆ eulert

Matrix2D< double > Projection::eulert

Just the opposite.

Ru = eulert * Rp.

Definition at line 79 of file fourier_projection.h.

◆ maxFrequency

double FourierProjector::maxFrequency

Maximum Frequency for pixels.

Definition at line 117 of file fourier_projection.h.

◆ paddingFactor

double FourierProjector::paddingFactor

Padding factor.

Definition at line 115 of file fourier_projection.h.

◆ phaseShiftImgA

MultidimArray<double> FourierProjector::phaseShiftImgA

Definition at line 140 of file fourier_projection.h.

◆ phaseShiftImgB

MultidimArray<double> FourierProjector::phaseShiftImgB

Definition at line 139 of file fourier_projection.h.

◆ projection

Image<double> FourierProjector::projection

Definition at line 136 of file fourier_projection.h.

◆ projectionFourier

MultidimArray< std::complex<double> > FourierProjector::projectionFourier

Definition at line 133 of file fourier_projection.h.

◆ transformer2D

FourierTransformer FourierProjector::transformer2D

Definition at line 123 of file fourier_projection.h.

◆ VfourierImagCoefs

MultidimArray< double > FourierProjector::VfourierImagCoefs

Definition at line 130 of file fourier_projection.h.

◆ VfourierRealCoefs

MultidimArray< double > FourierProjector::VfourierRealCoefs

Definition at line 129 of file fourier_projection.h.

◆ volume

MultidimArray<double>* FourierProjector::volume

Definition at line 126 of file fourier_projection.h.

◆ volumePaddedSize

int FourierProjector::volumePaddedSize

Definition at line 146 of file fourier_projection.h.

◆ volumeSize

int FourierProjector::volumeSize

Definition at line 143 of file fourier_projection.h.