EMAN2
Classes | Public Member Functions | Static Public Member Functions | Static Private Member Functions
EMAN::Util Class Reference

Util is a collection of utility functions. More...

#include <util.h>

List of all members.

Classes

class  FakeKaiserBessel
class  Gaussian
 Gaussian function class. More...
class  KaiserBessel
 1-D Kaiser-Bessel window function class. More...
class  sincBlackman
struct  tmpstruct

Public Member Functions

double angle3 (double x1, double y1, double z1, double x2, double y2, double z2)
 Given 2 vectors, it will compute the angle between them in radians.

Static Public Member Functions

static int coveig (int n, float *covmat, float *eigval, float *eigvec)
 For those util function developed by Pawel's group.
static Dict coveig_for_py (int ncov, const vector< float > &covmatpy)
 same function than Util::coveig but wrapped to use directly in python code
static void WTF (EMData *PROJ, vector< float > SS, float SNR, int K)
static void WTM (EMData *PROJ, vector< float > SS, int DIAMETER, int NUMP)
static Dict CANG (float PHI, float THETA, float PSI)
static void BPCQ (EMData *B, EMData *CUBE, const int radius)
static vector< float > infomask (EMData *Vol, EMData *mask, bool)
static vector< float > helixshiftali (vector< EMData * > ctx, vector< vector< float > > pcoords, int nsegms, float maxincline, int kang, int search_rng, int nxc)
static vector< float > curhelixshiftali (vector< EMData * > ctx, vector< vector< float > > pcoords, int nsegms, int search_rng, int nx, int ny)
static void colreverse (float *beg, float *end, int nx)
static void slicereverse (float *beg, float *end, int nx, int ny)
static void cyclicshift (EMData *image, Dict params)
 Performs inplace integer cyclic shift as specified by the "dx","dy","dz" parameters on a 3d volume.
static Dict im_diff (EMData *V1, EMData *V2, EMData *mask=0)
static EMDataTwoDTestFunc (int Size, float p, float q, float a, float b, int flag=0, float alphaDeg=0)
 Creates a Two D Test Pattern.
static void spline_mat (float *x, float *y, int n, float *xq, float *yq, int m)
 Given a tabulated function y of x (n unordered points), and Given the values of the m values xq to be interpolated This routine returns the interpolated array yq, PRB This function is called by splint.
static void spline (float *x, float *y, int n, float yp1, float ypn, float *y2)
 Given a tabulated function y of x (unordered), and Given the values of the first derivatives at the end points This routine returns an array y2, that contains the second derivatives of the function at the tabulated points.
static void splint (float *xa, float *ya, float *y2a, int n, float *xq, float *yq, int m)
 Given the arrays xa(ordered, ya of length n, which tabulate a function and given the array y2a which is the output of spline and an unordered array xq, this routine returns a cubic-spline interpolated array yq.
static void Radialize (int *PermMatTr, float *kValsSorted, float *weightofkvalsSorted, int Size, int *SizeReturned)
 list the sorted lengths of the integer lattice sites of a square sided image of size Size.
static vector< float > even_angles (float delta, float t1=0, float t2=90, float p1=0, float p2=359.999)
 Compute a vector containing quasi-evenly spaced Euler angles.
static float quadri (float x, float y, int nx, int ny, float *image)
 Quadratic interpolation (2D).
static float quadri_background (float x, float y, int nx, int ny, float *image, int xnew, int ynew)
 Quadratic interpolation (2D).
static float get_pixel_conv_new (int nx, int ny, int nz, float delx, float dely, float delz, float *data, Util::KaiserBessel &kb)
static float get_pixel_conv_new_background (int nx, int ny, int nz, float delx, float dely, float delz, float *data, Util::KaiserBessel &kb, int xnew, int ynew)
static std::complex< float > extractpoint2 (int nx, int ny, float nuxnew, float nuynew, EMData *fimage, Util::KaiserBessel &kb)
static float bilinear (float xold, float yold, int nsam, int nrow, float *xim)
static float triquad (float r, float s, float t, float *fdata)
 Quadratic interpolation (3D)
static EMDataPolar2D (EMData *image, vector< int > numr, string mode)
static EMDataPolar2Dm (EMData *image, float cns2, float cnr2, vector< int > numr, string cmode)
static void alrl_ms (float *xim, int nsam, int nrow, float cns2, float cnr2, int *numr, float *circ, int lcirc, int nring, char mode)
static EMDataPolar2Dmi (EMData *image, float cns2, float cnr2, vector< int > numr, string cmode, Util::KaiserBessel &kb)
static void fftr_q (float *xcmplx, int nv)
static void fftr_d (double *xcmplx, int nv)
static void fftc_q (float *br, float *bi, int ln, int ks)
static void fftc_d (double *br, double *bi, int ln, int ks)
static void Frngs (EMData *circ, vector< int > numr)
 This function conducts the Single Precision Fourier Transform for a set of rings.
static void Normalize_ring (EMData *ring, const vector< int > &numr)
static void Frngs_inv (EMData *circ, vector< int > numr)
 This function conducts the Single Precision Inverse Fourier Transform for a set of rings.
static void Applyws (EMData *circ, vector< int > numr, vector< float > wr)
 This is a copy of Applyws routine from alignment.py.
static Dict Crosrng_e (EMData *circ1, EMData *circ2, vector< int > numr, int neg)
static Dict Crosrng_ew (EMData *circ1, EMData *circ2, vector< int > numr, vector< float > w, int neg)
static Dict Crosrng_ms (EMData *circ1, EMData *circ2, vector< int > numr)
static Dict Crosrng_ms_delta (EMData *circ1, EMData *circ2, vector< int > numr, float delta_start, float delta)
static Dict Crosrng_sm_psi (EMData *circ1, EMData *circ2, vector< int > numr, float psi, int flag, float psimax)
 checks either straight or mirrored position depending on flag input - fourier transforms of rings!! circ1 already multiplied by weights!
static Dict Crosrng_psi (EMData *circ1, EMData *circ2, vector< int > numr, float psi, float psimax)
 checks both straight & mirrored position input - fourier transforms of rings!! circ1 already multiplied by weights!
static Dict Crosrng_ns (EMData *circ1, EMData *circ2, vector< int > numr)
 checks both straight & mirrored positions Find only positions within +/-pis_max of 0 and 180 (for helcal use) input - fourier transforms of rings! circ1 already multiplied by weights!
static EMDataCrosrng_msg (EMData *circ1, EMData *circ2, vector< int > numr)
 checks both straight & mirrored positions input - fourier transforms of rings!! circ1 already multiplied by weights! returns EM object with 1D ccf
static void Crosrng_msg_vec (EMData *circ1, EMData *circ2, vector< int > numr, float *q, float *t)
 checks both straight & mirrored positions input - fourier transforms of rings!! circ1 already multiplied by weights! returns EM object with 1D ccf
static EMDataCrosrng_msg_s (EMData *circ1, EMData *circ2, vector< int > numr)
 This program is half of the Crosrng_msg.
static EMDataCrosrng_msg_m (EMData *circ1, EMData *circ2, vector< int > numr)
 This program is half of the Crosrng_msg.
static vector< float > Crosrng_msg_vec_p (EMData *circ1, EMData *circ2, vector< int > numr)
static void prb1d (double *b, int npoint, float *pos)
static void update_fav (EMData *ave, EMData *dat, float tot, int mirror, vector< int > numr)
static void sub_fav (EMData *ave, EMData *dat, float tot, int mirror, vector< int > numr)
static float ener (EMData *ave, vector< int > numr)
static float ener_tot (const vector< EMData * > &data, vector< int > numr, vector< float > tot)
static Dict min_dist_real (EMData *image, const vector< EMData * > &data)
 k-means helper
static Dict min_dist_four (EMData *image, const vector< EMData * > &data)
 helper function for k-means
static int k_means_cont_table_ (int *group1, int *group2, int *stb, long int s1, long int s2, int flag)
 helper to create the contengency table for partition matching (k-means) flag define is the list of stable obj must be store to stb, but the size st must be know before.
static void initial_prune (vector< vector< int * > > &Parts, int *dimClasses, int nParts, int K, int T)
 initial_prune removes all classes C from Parts where there does not exist ANY feasible matching containing class C which has weight gt T.
static bool explore (vector< vector< int * > > &Parts, int *dimClasses, int nParts, int K, int T, int partref, int *curintx, int size_curintx, int *next, int size_next, int depth)
 Each class in Parts has its dummy variable set to 0 or 1.
static int generatesubmax (int *argParts, int *Indices, int *dimClasses, int nParts, int K, int T, int n_guesses, int LARGEST_CLASS)
 make an intelligent "guess" at the largest weight of all possible feasible matches.
static void search2 (int *argParts, int *Indices, int *dimClasses, int nParts, int K, int T, int *matchlist, int *costlist, int J)
 return the weight of the largest weighted feasible match, along with the match in the (preallocated) argument curmax.
static void explore2 (int *argParts, int *Indices, int *dimClasses, int nParts, int K, int T, int *curintx, int size_curintx, int *next, int size_next, int depth, int J, int *matchlist, int *costlist, int *curbranch)
static bool sanitycheck (int *argParts, int *Indices, int *dimClasses, int nParts, int K, int T, int *output)
 First element of output is total cost of the matches in the output Second element of output is the total number of matches in output So output has 2+(*(output+1))nParts elements.
static vector< int > bb_enumerateMPI_ (int *argParts, int *dimClasses, int nParts, int K, int T, int n_guesses, int LARGEST_CLASS, int J, int max_branching, float stmult, int branchfunc, int LIM)
 K is the number of classes in each partition (should be the same for all partitions) the first element of each class is its original index in the partition, and second is dummy var MPI: if nTop <= 0, then initial prune is called, and the pruned partitions are returned in a 1D array.
static int * branchMPI (int *argParts, int *Indices, int *dimClasses, int nParts, int K, int T, int curlevel, int n_guesses, int LARGEST_CLASS, int J, int max_branching, float stmult, int branchfunc, int LIM)
 same as branch except the nFirst (=Levels[0]) possibilites for the first match are already chosen firstmatches stores the matches and corresponding cost, where each match is preceded by its cost....
static int branch_factor_0 (int *costlist, int *matchlist, int J, int T, int nParts, int curlevel, int max_branching, int LIM)
static int branch_factor_2 (int *costlist, int *matchlist, int J, int T, int nParts, int curlevel, int max_branching, int LIM)
static int branch_factor_3 (int *costlist, int *matchlist, int J, int T, int nParts, int curlevel, int max_branching, int K, int LIM)
static int branch_factor_4 (int *costlist, int *matchlist, int J, int T, int nParts, int curlevel, int max_branching, float stmult)
static vector< double > cml_weights (const vector< float > &cml)
static vector< int > cml_line_insino (vector< float > Rot, int i_prj, int n_prj)
 2009-03-25 15:35:53 JB.
static vector< int > cml_line_insino_all (vector< float > Rot, vector< int > seq, int n_prj, int n_lines)
 2009-03-30 15:35:07 JB.
static vector< double > cml_init_rot (vector< float > Ori)
 2009-03-25 15:35:05 JB.
static vector< float > cml_update_rot (vector< float > Rot, int iprj, float nph, float th, float nps)
 2009-03-25 15:35:37 JB.
static vector< double > cml_line_in3d (vector< float > Ori, vector< int > seq, int nprj, int nlines)
 2009-03-26 10:46:14 JB.
static vector< double > cml_spin_psi (const vector< EMData * > &data, vector< int > com, vector< float > weights, int iprj, vector< int > iw, int n_psi, int d_psi, int n_prj)
 2009-03-26 11:37:53 JB.
static vector< double > cml_spin_psi_now (const vector< EMData * > &data, vector< int > com, int iprj, vector< int > iw, int n_psi, int d_psi, int n_prj)
static double cml_disc (const vector< EMData * > &data, vector< int > com, vector< int > seq, vector< float > weights, int n_lines)
 2009-03-30 15:44:05 JB.
static void set_line (EMData *img, int posline, EMData *line, int offset, int length)
 This function drop a line (line) to an 2D image (img).
static void cml_prepare_line (EMData *sino, EMData *line, int ilf, int ihf, int pos_line, int nblines)
 This function prepare the line from sinogram by cutting off some frequencies, and creating the mirror part (complexe conjugate of the first part).
static EMDatadecimate (EMData *img, int x_step, int y_step=1, int z_step=1)
static EMDatawindow (EMData *img, int new_nx, int new_ny=1, int new_nz=1, int x_offset=0, int y_offset=0, int z_offset=0)
static EMDatapad (EMData *img, int new_nx, int new_ny=1, int new_nz=1, int x_offset=0, int y_offset=0, int z_offset=0, const char *params="average")
static vector< float > histogram (EMData *image, EMData *mask, int nbins=128, float hmin=0.0f, float hmax=0.0f)
static Dict histc (EMData *ref, EMData *img, EMData *mask)
static float hist_comp_freq (float PA, float PB, size_t size_img, int hist_len, EMData *img, vector< float > ref_freq_hist, EMData *mask, float ref_h_diff, float ref_h_min)
static float tf (float dzz, float ak, float voltage=300.0f, float cs=2.0f, float wgh=0.1f, float b_factor=0.0f, float sign=-1.0f)
static EMDatacompress_image_mask (EMData *image, EMData *mask)
static EMDatareconstitute_image_mask (EMData *image, EMData *mask)
 Recreates a n-d image using its compressed 1-D form and the mask.
static vector< float > merge_peaks (vector< float > peak1, vector< float > peak2, float p_size)
static vector< float > pw_extract (vector< float >pw, int n, int iswi, float ps)
static vector< float > call_cl1 (long int *k, long int *n, float *ps, long int *iswi, float *pw, float *q2, double *q, double *x, double *res, double *cu, double *s, long int *iu)
static vector< float > lsfit (long int *ks, long int *n, long int *klm2d, long int *iswi, float *q1, double *q, double *x, double *res, double *cu, double *s, long int *iu)
static void cl1 (long int *k, long int *l, long int *m, long int *n, long int *klm2d, double *q, double *x, double *res, double *cu, long int *iu, double *s)
static float eval (char *images, EMData *img, vector< int > S, int N, int K, int size)
static vector< double > vrdg (const vector< float > &ph, const vector< float > &th)
static void hsortd (double *theta, double *phi, int *key, int len, int option)
static void voronoidiag (double *theta, double *phi, double *weight, int n)
static void voronoi (double *phi, double *theta, double *weight, int nt)
static void disorder2 (double *x, double *y, int *key, int len)
static void ang_to_xyz (double *x, double *y, double *z, int len)
static void flip23 (double *x, double *y, double *z, int *key, int k, int len)
static bool cmp1 (tmpstruct tmp1, tmpstruct tmp2)
static bool cmp2 (tmpstruct tmp1, tmpstruct tmp2)
static int trmsh3_ (int *n0, double *tol, double *x, double *y, double *z__, int *n, int *list, int *lptr, int *lend, int *lnew, int *indx, int *lcnt, int *near__, int *next, double *dist, int *ier)
static double areav_ (int *k, int *n, double *x, double *y, double *z__, int *list, int *lptr, int *lend, int *ier)
static EMDatamadn_scalar (EMData *img, EMData *img1, float scalar)
static EMDatamult_scalar (EMData *img, float scalar)
static EMDataaddn_img (EMData *img, EMData *img1)
static EMDatasubn_img (EMData *img, EMData *img1)
static EMDatamuln_img (EMData *img, EMData *img1)
static EMDatadivn_img (EMData *img, EMData *img1)
static EMDatasquaren_img (EMData *img)
static EMDatadivn_filter (EMData *img, EMData *img1)
static void mad_scalar (EMData *img, EMData *img1, float scalar)
static void mul_scalar (EMData *img, float scalar)
static void add_img (EMData *img, EMData *img1)
static void add_img_abs (EMData *img, EMData *img1)
static void add_img2 (EMData *img, EMData *img1)
static void sub_img (EMData *img, EMData *img1)
static void mul_img (EMData *img, EMData *img1)
static void div_img (EMData *img, EMData *img1)
static void square_img (EMData *img)
static void div_filter (EMData *img, EMData *img1)
static void set_freq (EMData *freqvol, EMData *temp, EMData *mask, float cutoff, float freq)
static EMDatapack_complex_to_real (EMData *img)
static vector< float > multiref_polar_ali_2d (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, string mode, vector< int >numr, float cnx, float cny)
 formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation
static vector< float > multiref_polar_ali_2d_peaklist (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, string mode, vector< int >numr, float cnx, float cny)
static vector< float > multiref_polar_ali_2d_peaklist_local (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, string mode, vector< int >numr, float cnx, float cny)
static vector< int > assign_groups (std::string matrix_address, int nref, int nima)
static void getvec (float phi, float theta, float &x, float &y, float &z, int option=0)
static float ang_diff (float v11, float v12, float v13, float v21, float v22, float v23, int &mirror)
static int nearest_ang (const vector< float > &vecref, float x, float y, float z)
static vector< int > assign_projangles (const vector< float > &projangles, const vector< float > &refangles)
static vector< int > nearestk_to_refdir (const vector< float > &projangles, const vector< float > &refangles, const int howmany)
static vector< int > group_proj_by_phitheta (const vector< float > &projangles, const vector< float > &ref_ang, const int img_per_grp)
static vector< float > multiref_polar_ali_2d_delta (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, string mode, vector< int >numr, float cnx, float cny, float delta_start, float delta)
 formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation
static vector< float > multiref_polar_ali_2d_nom (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, string mode, vector< int >numr, float cnx, float cny)
 formerly known as apnq DO NOT CONSIDER MIRROR Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation
static vector< float > multiref_polar_ali_2d_local (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, string mode, vector< int >numr, float cnx, float cny)
 formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation
static vector< float > shc (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, string mode, vector< int >numr, float cnx, float cny, string sym)
static vector< float > shc_multipeaks (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, string mode, vector< int >numr, float cnx, float cny, int max_peaks_count)
static vector< float > multiref_polar_ali_helical (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float psi_max, string mode, vector< int >numr, float cnx, float cny, int ynumber=-1)
 formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation Search for peaks only within +/-psi_max from 0 and 180 (helical)
static vector< float > multiref_polar_ali_helical_local (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, float psi_max, string mode, vector< int >numr, float cnx, float cny, int ynumber=-1, float yrnglocal=-1.0)
static vector< float > multiref_polar_ali_helical_90 (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float psi_max, string mode, vector< int >numr, float cnx, float cny, int ynumber=-1)
static vector< float > multiref_polar_ali_helical_90_local (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, float psi_max, string mode, vector< int >numr, float cnx, float cny, int ynumber=-1, float yrnglocal=-1.0)
static vector< float > multiref_polar_ali_helicon_local (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, float psi_max, string mode, vector< int >numr, float cnx, float cny, int ynumber=-1, float yrnglocal=-1.0)
 Next two for helicon.
static vector< float > multiref_polar_ali_helicon_90_local (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, float psi_max, string mode, vector< int >numr, float cnx, float cny, int ynumber=-1, float yrnglocal=-1.0)
static vector< float > multiref_polar_ali_2d_local_psi (EMData *image, const vector< EMData * > &crefim, float xrng, float yrng, float step, float ant, float psi_max, string mode, vector< int >numr, float cnx, float cny)
 formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation
static void multiref_peaks_ali2d (EMData *image, EMData *crefim, float xrng, float yrng, float step, string mode, vector< int >numr, float cnx, float cny, EMData *peaks, EMData *peakm)
 Determine shift and rotation between image and one reference image (crefim, weights have to be applied) using quadratic interpolation, return a list of peaks PAP 07/21/08.
static void multiref_peaks_compress_ali2d (EMData *image, EMData *crefim, float xrng, float yrng, float step, string mode, vector< int >numr, float cnx, float cny, EMData *peaks, EMData *peakm, EMData *peaks_compress, EMData *peakm_compress)
 Determine shift and rotation between image and one reference image (crefim, weights have to be applied) using quadratic interpolation, return a list of peaks PAP 07/21/08.
static vector< float > ali2d_ccf_list (EMData *image, EMData *crefim, float xrng, float yrng, float step, string mode, vector< int >numr, float cnx, float cny, double T)
 Determine shift and rotation between image and one reference image (crefim, weights have to be applied) using quadratic interpolation.
static vector< float > twoD_fine_ali (EMData *image, EMData *refim, EMData *mask, float ang, float sxs, float sys)
static vector< float > twoD_fine_ali_G (EMData *image, EMData *refim, EMData *mask, Util::KaiserBessel &kb, float ang, float sxs, float sys)
static vector< float > twoD_to_3D_ali (EMData *volft, Util::KaiserBessel &kb, EMData *refim, EMData *mask, float phi, float theta, float psi, float sxs, float sxy)
static vector< float > twoD_fine_ali_SD (EMData *image, EMData *refim, EMData *mask, float ang, float sxs, float sys)
static float ccc_images (EMData *, EMData *, EMData *, float, float, float)
static vector< float > twoD_fine_ali_SD_G (EMData *image, EMData *refim, EMData *mask, Util::KaiserBessel &kb, float ang, float sxs, float sys)
static float ccc_images_G (EMData *image, EMData *refim, EMData *mask, Util::KaiserBessel &kb, float ang, float sx, float sy)
static float local_inner_product (EMData *image1, EMData *image2, int lx, int ly, int lz, int w)
static EMDatamove_points (EMData *img, float qprob, int ri, int ro)
static EMDataget_biggest_cluster (EMData *mg)
static EMDatactf_img (int nx, int ny, int nz, float dz, float ps, float voltage, float cs, float wgh, float b_factor, float dza, float azz, float sign)
static EMDatactf_rimg (int nx, int ny, int nz, float dz, float ps, float voltage, float cs, float wgh, float b_factor, float dza, float azz, float sign)
static EMDatactf2_rimg (int nx, int ny, int nz, float dz, float ps, float voltage, float cs, float wgh, float b_factor, float dza, float azz, float sign)
static int mono (int k1, int k2)
static int nint180 (float arg)
static double mean (double *x, int n)
static double var (double *x, int n)
static vector< float > multi_align_error (vector< float > args, vector< float > all_ali_params, int d)
static double multi_align_error_func (double *x, vector< float > all_ali_params, int nima, int num_ali, int d)
static vector< double > multi_align_error_func2 (double *x, vector< float > all_ali_params, int nima, int num_ali, int d)
static void multi_align_error_dfunc (double *x, vector< float > all_ali_params, int nima, int num_ali, double *g, int d)
static vector< float > cluster_pairwise (EMData *d, int K, float T, float F)
static vector< float > cluster_equalsize (EMData *d)
static vector< float > vareas (EMData *d)
static EMDataget_slice (EMData *vol, int dim, int index)
 This function returns a 2-D slice from a 3-D EMData object dim denotes the slice is perpendicular to which dimension 1 for x-dimension, 2 for y-dimension and 3 for z-dimension.
static void image_mutation (EMData *img, float mutation_rate)
static void array_mutation (float *list, int len_list, float mutation_rate, float min_val, float max_val, int K, int is_mirror)
 The purpose of this function is to convert a list to grey code and mutate them and convert them back.
static vector< float > list_mutation (vector< float > list, float mutation_rate, float min_val, float max_val, int K, int is_mirror)
static float restrict1 (float x, int nx)
static Dict get_transform_params (EMData *image, string xform, string convention)
 This function returns parameters from Transform object as a Dict object.
static void constrained_helix_exhaustive (vector< EMData * > data, vector< EMData * > fdata, vector< EMData * > refproj, vector< EMData * > rotproj, vector< float > dp_dphi_rise_delta, vector< int > nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc, bool FindPsi, float psi_max, vector< EMData * > crefim, vector< int > numr, int maxrin, string mode, int cnx, int cny)
static std::vector< float > diff_between_matrix_of_3D_parameters_angles (std::vector< float > all_params, std::vector< float > rotations)
static std::vector< int > max_clique (std::vector< int > edges)
 Calculates max_clique in undirected graph, input: edges coded as pair of integers (integers correspond to vertices, must be >=0) Returns: vertices (integers) belonging to max clique.
static int MUTEX_INIT (MUTEX *mutex)
static int MUTEX_LOCK (MUTEX *mutex)
static int MUTEX_UNLOCK (MUTEX *mutex)
static void ap2ri (float *data, size_t n)
 convert complex data array from Amplitude/Phase format into Real/Imaginary format.
static void flip_complex_phase (float *data, size_t n)
 flip the phase of a complex data array.
static void rotate_phase_origin (float *data, size_t nx, size_t ny, size_t nz)
 rotate data vertically by ny/2, to make the mrc phase origin compliance with EMAN1 and TNF reader
static int file_lock_wait (FILE *file)
 lock a file.
static bool check_file_by_magic (const void *first_block, const char *magic)
 check whether a file starts with certain magic string.
static bool is_file_exist (const string &filename)
 check whether a file exists or not
static void flip_image (float *data, size_t nx, size_t ny)
 Vertically flip the data of a 2D real image.
static vector< EMData * > svdcmp (const vector< EMData * > &data, int nvec)
 Perform singular value decomposition on a set of images.
static string str_to_lower (const string &s)
 Return a lower case version of the argument string.
static bool sstrncmp (const char *s1, const char *s2)
 Safe string compare.
static string int2str (int n)
 Get a string format of an integer, e.g.
static string get_line_from_string (char **str)
 Extract a single line from a multi-line string.
static bool get_str_float (const char *str, const char *float_var, float *p_val)
 Extract the float value from a variable=value string with format like "XYZ=1.1", where 'str' is "XYZ=1.1"; 'float_var' is "XYZ="; 'p_val' points to float number 1.1.
static bool get_str_float (const char *str, const char *float_var, float *p_v1, float *p_v2)
 Extract the float values from a variable=value1,value2 string with format like "XYZ=1.1,1.2", where 'str' is "XYZ=1.1,1.2"; 'float_var' is "XYZ="; 'p_v1' points to 1.1; 'p_v2' points to 1.2.
static bool get_str_float (const char *str, const char *float_var, int *p_nvalues, float *p_v1, float *p_v2)
 Extract number of values and the float values, if any, from a string whose format is either "variable=value1,value2 " or "variable".
static bool get_str_int (const char *str, const char *int_var, int *p_val)
 Extract the int value from a variable=value string with format like "XYZ=1", where 'str' is "XYZ=1"; 'int_var' is "XYZ="; 'p_val' points to float number 1.
static bool get_str_int (const char *str, const char *int_var, int *p_v1, int *p_v2)
 Extract the int value from a variable=value1,value2 string with format like "XYZ=1,2", where 'str' is "XYZ=1,2"; 'int_var' is "XYZ="; 'p_val' points to float number 1.
static bool get_str_int (const char *str, const char *int_var, int *p_nvalues, int *p_v1, int *p_v2)
 Extract number of values and the int values, if any, from a string whose format is either "variable=value1,value2 " or "variable".
static string change_filename_ext (const string &old_filename, const string &new_ext)
 Change a file's extension and return the new filename.
static string remove_filename_ext (const string &filename)
 Remove a filename's extension and return the new filename.
static string get_filename_ext (const string &filename)
 Get a filename's extension.
static string sbasename (const string &filename)
 Get a filename's basename.
static void calc_least_square_fit (size_t nitems, const float *data_x, const float *data_y, float *p_slope, float *p_intercept, bool ignore_zero, float absmax=0)
 calculate the least square fit value.
static Vec3f calc_bilinear_least_square (const vector< float > &points)
 calculate bilinear least-square fit, z = a + b x + c y Takes a set of x,y,z vectors and produces an a,b,c vector does not accept error bars on z or return goodness of fit
static void save_data (const vector< float > &x_array, const vector< float > &y_array, const string &filename)
 Save (x y) data array into a file.
static void save_data (float x0, float dx, const vector< float > &y_array, const string &filename)
 Save x, y data into a file.
static void save_data (float x0, float dx, float *y_array, size_t array_size, const string &filename)
 Save x, y data into a file.
static void sort_mat (float *left, float *right, int *leftPerm, int *rightPerm)
 does a sort as in Matlab.
static unsigned long long get_randnum_seed ()
 Get the seed for Randnum class.
static void set_randnum_seed (unsigned long long seed)
 Set the seed for Randnum class.
static int get_irand (int low, int high)
 Get an integer random number between low and high, [low, high].
static float get_frand (int low, int high)
 Get a float random number between low and high, [low, high)
static float get_frand (float low, float high)
 Get a float random number between low and high, [low, high)
static float get_frand (double low, double high)
 Get a float random number between low and high, [low, high)
static float get_gauss_rand (float mean, float sigma)
 Get a Gaussian random number.
static int round (float x)
 Get ceiling round of a float number x.
static int round (double x)
 Get ceiling round of a float number x.
static float linear_interpolate (float p1, float p2, float t)
 Calculate linear interpolation.
static float bilinear_interpolate (float p1, float p2, float p3, 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 void find_max (const float *data, size_t nitems, float *p_max_val, int *p_max_index=0)
 Find the maximum value and (optional) its index in an array.
static void find_min_and_max (const float *data, size_t nitems, float *p_max_val, float *p_min_val, int *p_max_index=0, int *p_min_index=0)
 Find the maximum value and (optional) its index, minimum value and (optional) its index in an array.
static Dict get_stats (const vector< float > &data)
 Get the mean, standard deviation, skewness and kurtosis of the input data.
static Dict get_stats_cstyle (const vector< float > &data)
 Performs the same calculations as in get_stats, but uses a single pass, optimized c approach Should perform better than get_stats.
static int calc_best_fft_size (int low)
 Search the best FFT size with good primes.
static vector< float > nonconvex (const vector< float > &curve, int first=3)
 Returns a non-convex version of a curve.
static vector< float > windowdot (const vector< float > &curveA, const vector< float > &curveB, int window, int normA)
 Computes a windowed dot product between curve A and curve B.
static EMDatacalc_bessel (const int n, const float &x)
static int square (int n)
 Calculate a number's square.
static float square (float x)
 Calculate a number's square.
static float square (double x)
 Calculate a number's square.
static float square_sum (float x, float y)
 Calcuate (x*x + y*y).
static float hypot2 (float x, float y)
 Euclidean distance function in 2D: f(x,y) = sqrt(x*x + y*y);.
static int hypot3sq (int x, int y, int z)
 Euclidean distance function squared in 3D: f(x,y,z) = (x*x + y*y + z*z);.
static float hypot3sq (float x, float y, float z)
 Euclidean distance function squared in 3D: f(x,y,z) = (x*x + y*y + z*z);.
static float hypot3 (int x, int y, int z)
 Euclidean distance function in 3D: f(x,y,z) = sqrt(x*x + y*y + z*z);.
static float hypot3 (float x, float y, float z)
 Euclidean distance function in 3D: f(x,y,z) = sqrt(x*x + y*y + z*z);.
static double hypot3 (double x, double y, double z)
 Euclidean distance function in 3D: f(x,y,z) = sqrt(x*x + y*y + z*z);.
static float hypot_fast (int x, int y)
 Euclidean distance in 2D for integers computed fast using a cached lookup table.
static short hypot_fast_int (int x, int y)
 Euclidean distance in 2D for integers computed fast using a cached lookup table.
static int fast_floor (float x)
 A fast way to calculate a floor, which is largest integral value not greater than argument.
static float fast_exp (const float &f)
 Returns an approximate of exp(x) using a cached table uses actual exp(x) outside the cached range.
static float fast_acos (const float &f)
 Returns an approximate of acos(x) using a cached table and linear interpolation tolerates values very slightly outside the -1 - 1 range (returning 0)
static float agauss (float a, float dx, float dy, float dz, float d)
 Calculate Gaussian value.
static int get_min (int f1, int f2)
 Get the minimum of 2 numbers.
static int get_min (int f1, int f2, int f3)
 Get the minimum of 3 numbers.
static float get_min (float f1, float f2)
 Get the minimum of 2 numbers.
static float get_min (float f1, float f2, float f3)
 Get the minimum of 3 numbers.
static float get_min (float f1, float f2, float f3, float f4)
 Get the minimum of 4 numbers.
static float get_max (float f1, float f2)
 Get the maximum of 2 numbers.
static float get_max (float f1, float f2, float f3)
 Get the maximum of 3 numbers.
static float get_max (float f1, float f2, float f3, float f4)
 Get the maximum of 4 numbers.
static float angle_sub_2pi (float x, float y)
 Calculate the difference of 2 angles and makes the equivalent result to be less than Pi.
static float angle_sub_pi (float x, float y)
 Calculate the difference of 2 angles and makes the equivalent result to be less than Pi/2.
static float angle_err_ri (float r1, float i1, float r2, float i2)
 Calculate the angular phase difference between two r/i vectors.
static int goodf (const float *p_f)
 Check whether a number is a good float.
static int goodf (const double *p_f)
static string recv_broadcast (int port)
static string get_time_label ()
 Get the current time in a string with format "mm/dd/yyyy hh:mm".
static void set_log_level (int argc, char *argv[])
 Set program logging level through command line option "-v N", where N is the level.
static float eman_copysign (float a, float b)
 copy sign of a number.
static float eman_erfc (float x)
 complementary error function.
static void equation_of_plane (const Vec3f &p1, const Vec3f &p2, const Vec3f &p3, float *plane)
 Determine the equation of a plane that intersects 3 points in 3D space.
static bool point_is_in_convex_polygon_2d (const Vec2f &p1, const Vec2f &p2, const Vec2f &p3, const Vec2f &p4, const Vec2f &actual_point)
 Determines if a point is in a 2D convex polygon described by 4 points using the Barycentric method, which is a fast way of performing the query.
static bool point_is_in_triangle_2d (const Vec2f &p1, const Vec2f &p2, const Vec2f &p3, const Vec2f &actual_point)
 Determines if a point is in a 2D triangle using the Barycentric method, which is a fast way of performing the query Triangle points can be specified in any order.
static void printMatI3D (MIArray3D &mat, const string str=string(""), ostream &out=std::cout)
 Print a 3D integer matrix to a file stream (std out by default).
template<class T >
static T sgn (T &val)
 Sign function.
static float * getBaldwinGridWeights (const int &freq_cutoff, const float &P, const float &r, const float &dfreq=1, const float &alpha=0.5, const float &beta=0.2)
 Get the isosurface value for 3D image.
static bool IsPower2 (int x)
 Return true if an integer is positive and is power of 2.
static void apply_precision (float &value, const float &precision)

Static Private Member Functions

static float ang_n (float peakp, string mode, int maxrin)

Detailed Description

Util is a collection of utility functions.

Definition at line 86 of file util.h.


Member Function Documentation

void Util::add_img ( EMData img,
EMData img1 
) [static]

Definition at line 17846 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), img_ptr, NullPointerException, nx, ny, and EMAN::EMData::update().

{
        ENTERFUNC;
        /* Exception Handle */
        if (!img || !img1) {
                throw NullPointerException("NULL input image");
        }
        /* ========= img += img1 ===================== */

        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        size_t size = (size_t)nx*ny*nz;
        float *img_ptr  = img->get_data();
        float *img1_ptr = img1->get_data();
        for (size_t i=0;i<size;++i) img_ptr[i] += img1_ptr[i];
        img->update();

        EXITFUNC;
}
void Util::add_img2 ( EMData img,
EMData img1 
) [static]

Definition at line 17884 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), img_ptr, EMAN::EMData::is_complex(), NullPointerException, nx, ny, and EMAN::EMData::update().

{
        ENTERFUNC;
        /* Exception Handle */
        if (!img) {
                throw NullPointerException("NULL input image");
        }
        /* ========= img += img1**2 ===================== */

        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        size_t size = (size_t)nx*ny*nz;
        float *img_ptr  = img->get_data();
        float *img1_ptr = img1->get_data();
        if(img->is_complex()) {
                for (size_t i=0; i<size; i+=2) img_ptr[i] += img1_ptr[i] * img1_ptr[i] + img1_ptr[i+1] * img1_ptr[i+1];
        } else {
                for (size_t i=0;i<size;++i) img_ptr[i] += img1_ptr[i]*img1_ptr[i];
        }
        img->update();

        EXITFUNC;
}
void Util::add_img_abs ( EMData img,
EMData img1 
) [static]

Definition at line 17865 of file util_sparx.cpp.

References abs, ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), img_ptr, NullPointerException, nx, ny, and EMAN::EMData::update().

{
        ENTERFUNC;
        /* Exception Handle */
        if (!img) {
                throw NullPointerException("NULL input image");
        }
        /* ========= img += img1 ===================== */

        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        size_t size = (size_t)nx*ny*nz;
        float *img_ptr  = img->get_data();
        float *img1_ptr = img1->get_data();
        for (size_t i=0;i<size;++i) img_ptr[i] += abs(img1_ptr[i]);
        img->update();

        EXITFUNC;
}
EMData * Util::addn_img ( EMData img,
EMData img1 
) [static]

Definition at line 17633 of file util_sparx.cpp.

References EMAN::EMData::copy_head(), ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), img2_ptr, img_ptr, EMAN::EMData::is_complex(), EMAN::EMData::is_fftodd(), NullPointerException, nx, ny, EMAN::EMData::set_complex(), EMAN::EMData::set_fftodd(), and EMAN::EMData::update().

{
        ENTERFUNC;
        /* Exception Handle */
        if (!img) {
                throw NullPointerException("NULL input image");
        }
        /* ==============   output = img + img1   ================ */

        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        size_t size = (size_t)nx*ny*nz;
        EMData * img2   = img->copy_head();
        float *img_ptr  = img->get_data();
        float *img2_ptr = img2->get_data();
        float *img1_ptr = img1->get_data();
        for (size_t i=0;i<size;++i) img2_ptr[i] = img_ptr[i] + img1_ptr[i];
        img2->update();
        if(img->is_complex()) {
                img2->set_complex(true);
                if(img->is_fftodd()) img2->set_fftodd(true); else img2->set_fftodd(false);
        }

        EXITFUNC;
        return img2;
}
static float EMAN::Util::agauss ( float  a,
float  dx,
float  dy,
float  dz,
float  d 
) [inline, static]

Calculate Gaussian value.

a * exp(-(dx * dx + dy * dy + dz * dz) / d)

Parameters:
[in]aamplitude
[in]dxx center
[in]dyy center
[in]dzz center
[in]dwidth of gaussian
Returns:
The Gaussian value.

Definition at line 751 of file util.h.

Referenced by EMAN::FourierReconstructorSimple2D::insert_slice().

                {
                        return (a * exp(-(dx * dx + dy * dy + dz * dz) / d));
                }
vector< float > Util::ali2d_ccf_list ( EMData image,
EMData crefim,
float  xrng,
float  yrng,
float  step,
string  mode,
vector< int >  numr,
float  cnx,
float  cny,
double  T 
) [static]

Determine shift and rotation between image and one reference image (crefim, weights have to be applied) using quadratic interpolation.

Definition at line 20060 of file util_sparx.cpp.

References Crosrng_msg_vec(), Frngs(), get_frand(), ccf_point::i, ccf_point::j, ccf_point::k, ccf_point::mirror, Polar2Dm(), t, and ccf_point::value.

                                                                           {

        int   maxrin = numr[numr.size()-1];

        int   ky = int(2*yrng/step+0.5)/2;
        int   kx = int(2*xrng/step+0.5)/2;

        float *p_ccf1ds = (float *)malloc(maxrin*sizeof(float));
        float *p_ccf1dm = (float *)malloc(maxrin*sizeof(float));
        int vol = maxrin*(2*kx+1)*(2*ky+1);
        vector<ccf_point> ccf(2*vol);
        ccf_point temp;

        int index = 0;
        for (int i = -ky; i <= ky; i++) {
                float iy = i * step;
                for (int j = -kx; j <= kx; j++) {
                        float ix = j*step;
                        EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
                        Frngs(cimage, numr);
                        Crosrng_msg_vec(crefim, cimage, numr, p_ccf1ds, p_ccf1dm);
                        for (int k=0; k<maxrin; k++) {
                                temp.value = p_ccf1ds[k];
                                temp.i = k;
                                temp.j = j;
                                temp.k = i;
                                temp.mirror = 0;
                                ccf[index] = temp;
                                index++;
                                temp.value = p_ccf1dm[k];
                                temp.mirror = 1;
                                ccf[index] = temp;
                                index++;
                        }
                        delete cimage; cimage = 0;
                }
        }

        delete p_ccf1ds;
        delete p_ccf1dm;
        std::sort(ccf.begin(), ccf.end(), ccf_value());

        double qt = (double)ccf[0].value;
        vector <double> p(2*vol), cp(2*vol);

        double sump = 0.0;
        for (int i=0; i<2*vol; i++) {
                p[i] = pow(double(ccf[i].value)/qt, 1.0/T);
                sump += p[i];
        }
        for (int i=0; i<2*vol; i++) {
                p[i] /= sump;
        }
        for (int i=1; i<2*vol; i++) {
                p[i] += p[i-1];
        }
        p[2*vol-1] = 2.0;

        float t = get_frand(0.0f, 1.0f);
        int select = 0;
        while (p[select] < t)   select += 1;

        vector<float> a(6);
        a[0] = ccf[select].value;
        a[1] = (float)ccf[select].i;
        a[2] = (float)ccf[select].j;
        a[3] = (float)ccf[select].k;
        a[4] = (float)ccf[select].mirror;
        a[5] = (float)select;
        return a;
}
void Util::alrl_ms ( float *  xim,
int  nsam,
int  nrow,
float  cns2,
float  cnr2,
int *  numr,
float *  circ,
int  lcirc,
int  nring,
char  mode 
) [static]

Definition at line 2534 of file util_sparx.cpp.

References circ, numr, quadri(), x, and y.

                                                                    {
        double dpi, dfi;
        int    it, jt, inr, l, nsim, kcirc, lt;
        float  xold, yold, fi, x, y;

        //     cns2 and cnr2 are predefined centers
        //     no need to set to zero, all elements are defined

        dpi = 2*atan(1.0);
        for (it=1; it<=nring; it++) {
                // radius of the ring
                inr = numr(1,it);

                l = numr(3,it);
                if ( mode == 'h' || mode == 'H' ) lt = l / 2;
                else                              lt = l / 4;

                nsim  = lt - 1;
                dfi   = dpi / (nsim+1);
                kcirc = numr(2,it);


                xold  = 0.0f+cns2;
                yold  = inr+cnr2;

                circ(kcirc) = quadri(xold,yold,nsam,nrow,xim);

                xold  = inr+cns2;
                yold  = 0.0f+cnr2;
                circ(lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);

                if ( mode == 'f' || mode == 'F' ) {
                        xold = 0.0f+cns2;
                        yold = -inr+cnr2;
                        circ(lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);

                        xold = -inr+cns2;
                        yold = 0.0f+cnr2;
                        circ(lt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
                }

                for (jt=1; jt<=nsim; jt++) {
                        fi   = static_cast<float>(dfi * jt);
                        x    = sin(fi) * inr;
                        y    = cos(fi) * inr;

                        xold = x+cns2;
                        yold = y+cnr2;
                        circ(jt+kcirc) = quadri(xold,yold,nsam,nrow,xim);

                        xold = y+cns2;
                        yold = -x+cnr2;
                        circ(jt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);

                        if ( mode == 'f' || mode == 'F' ) {
                                xold = -x+cns2;
                                yold = -y+cnr2;
                                circ(jt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);

                                xold = -y+cns2;
                                yold = x+cnr2;
                                circ(jt+lt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
                        }
                } // end for jt
        } //end for it
}
static float EMAN::Util::ang_diff ( float  v11,
float  v12,
float  v13,
float  v21,
float  v22,
float  v23,
int &  mirror 
) [inline, static]

Definition at line 892 of file util.h.

Referenced by group_proj_by_phitheta().

                                      {
                                r = (float) (2.0 * M_PI - r);
                        }

                        return r;
                }

float Util::ang_n ( float  peakp,
string  mode,
int  maxrin 
) [static, private]
void Util::ang_to_xyz ( double *  x,
double *  y,
double *  z,
int  len 
) [static]

Definition at line 8034 of file util_sparx.cpp.

References dgr_to_rad, ENTERFUNC, and EXITFUNC.

Referenced by voronoi().

{
        ENTERFUNC;
        double costheta,sintheta,cosphi,sinphi;
        for(int i = 0;  i<len;  i++) {
                cosphi = cos(y[i]*dgr_to_rad);
                sinphi = sin(y[i]*dgr_to_rad);
                if(fabs(x[i]-90.0)< 1.0e-5){
                        x[i] = cosphi;
                        y[i] = sinphi;
                        z[i] = 0.0;
                } else {
                        costheta = cos(x[i]*dgr_to_rad);
                        sintheta = sin(x[i]*dgr_to_rad);
                        x[i] = cosphi*sintheta;
                        y[i] = sinphi*sintheta;
                        z[i] = costheta;
                }
        }
        EXITFUNC;
}
double EMAN::Util::angle3 ( double  x1,
double  y1,
double  z1,
double  x2,
double  y2,
double  z2 
) [inline]

Given 2 vectors, it will compute the angle between them in radians.

Definition at line 590 of file util.h.

References hypot3().

                                                                                                     {
                        return acos((x1*x2+y1*y2+z1*z2)/(hypot3(x1,y1,z1)*hypot3(x2,y2,z2)));
                }
static float EMAN::Util::angle_err_ri ( float  r1,
float  i1,
float  r2,
float  i2 
) [inline, static]

Calculate the angular phase difference between two r/i vectors.

Parameters:
[in]r1/i1first vector, 2 floats
[in]r2/i2second vector, 2 floats
Returns:
angular difference

Definition at line 921 of file util.h.

References fast_acos().

Referenced by EMAN::PhaseCmp::cmp().

                                                                                      {
                        if ((r1==0 && i1==0) || (r2==0 && i2==0)) return 0;
//                      printf("%f\t%f\t%f\n",r1*r2+i1*i2,hypot(r1,i1),hypot(r2,i2));
//                      return acos((r1*r2+i1*i2)/(hypot(r1,i1)*hypot(r2,i2)));
                        return fast_acos((r1*r2+i1*i2)/(float)(hypot(r1,i1)*hypot(r2,i2)));             // fast_acos also tolerates values very slightly above 1
                }
static float EMAN::Util::angle_sub_2pi ( float  x,
float  y 
) [inline, static]

Calculate the difference of 2 angles and makes the equivalent result to be less than Pi.

Parameters:
[in]xThe first angle.
[in]yThe second angle.
Returns:
The difference of 2 angles.

Definition at line 889 of file util.h.

Referenced by EMAN::EMData::common_lines().

                {
                        float r = fmod(fabs(x - y), (float) (2 * M_PI));
                        if (r > M_PI) {
                                r = (float) (2.0 * M_PI - r);
                        }

                        return r;
                }
static float EMAN::Util::angle_sub_pi ( float  x,
float  y 
) [inline, static]

Calculate the difference of 2 angles and makes the equivalent result to be less than Pi/2.

Parameters:
[in]xThe first angle.
[in]yThe second angle.
Returns:
The difference of 2 angles.

Definition at line 906 of file util.h.

                {
                        float r = fmod(fabs(x - y), (float) M_PI);
                        if (r > M_PI / 2.0) {
                                r = (float)(M_PI - r);
                        }
                        return r;
                }
void Util::ap2ri ( float *  data,
size_t  n 
) [static]

convert complex data array from Amplitude/Phase format into Real/Imaginary format.

Parameters:
datacomplex data array.
narray size.

Definition at line 101 of file util.cpp.

References Assert, and NullPointerException.

{
        Assert(n > 0);

        if (!data) {
                throw NullPointerException("pixel data array");
        }

        for (size_t i = 0; i < n; i += 2) {
                float f = data[i] * sin(data[i + 1]);
                data[i] = data[i] * cos(data[i + 1]);
                data[i + 1] = f;
        }
}
static void EMAN::Util::apply_precision ( float &  value,
const float &  precision 
) [inline, static]

Definition at line 1133 of file util.h.

References fast_floor().

Referenced by EMAN::Transform::get_determinant(), EMAN::Transform::get_scale(), EMAN::Transform::get_scale_and_mirror(), EMAN::Transform::get_trans(), EMAN::Transform::is_identity(), EMAN::Transform::is_rot_identity(), and EMAN::Transform::set_scale().

                                                                                  {
                        float c = ceilf(value);
                        float f = (float)fast_floor(value);
                        if (fabs(value - c) < precision) value = c;
                        else if (fabs(value - f) < precision) value = f;
                }
void Util::Applyws ( EMData circ,
vector< int >  numr,
vector< float >  wr 
) [static]

This is a copy of Applyws routine from alignment.py.

Definition at line 3299 of file util_sparx.cpp.

References circ, and EMAN::EMData::get_data().

{       /*
          Apply weights to FTs of rings
        */
        const int nring = numr.size() / 3;
        const int maxrin = numr.back();
        float *circ = circp->get_data();
        for (int i = 0; i < nring; ++i) {
                const int numr3i = numr[2+i*3];
                const int numr2i = numr[1+i*3]-1;
                const float w = wr[i];
                circ[numr2i] *= w;
                if (numr3i == maxrin)  circ[numr2i+1] *= w;
                else                   circ[numr2i+1] *= 0.5*w;
                for (int j = 2+numr2i; j < numr3i+numr2i; ++j)  circ[j] *= w;
        }
}
double Util::areav_ ( int *  k,
int *  n,
double *  x,
double *  y,
double *  z__,
int *  list,
int *  lptr,
int *  lend,
int *  ier 
) [static]

Definition at line 9090 of file util_sparx.cpp.

References areas_(), circum_(), FALSE_, ierr, TRUE_, x, and y.

Referenced by voronoi().

{
    /* Initialized data */

    static double amax = 6.28;

    /* System generated locals */
    double ret_val;

    /* Local variables */
    static double a, c0[3], c2[3], c3[3];
    static int n1, n2, n3;
    static double v1[3], v2[3], v3[3];
    static int lp, lpl, ierr;
    static double asum;
    static long int first;


/* *********************************************************** */

/*                                            Robert J. Renka */
/*                                  Dept. of Computer Science */
/*                                       Univ. of North Texas */
/*                                           renka@cs.unt.edu */
/*                                                   10/25/02 */

/*   Given a Delaunay triangulation and the index K of an */
/* interior node, this subroutine returns the (surface) area */
/* of the Voronoi region associated with node K.  The Voronoi */
/* region is the polygon whose vertices are the circumcenters */
/* of the triangles that contain node K, where a triangle */
/* circumcenter is the point (unit vector) lying at the same */
/* angular distance from the three vertices and contained in */
/* the same hemisphere as the vertices. */


/* On input: */

/*       K = Nodal index in the range 1 to N. */

/*       N = Number of nodes in the triangulation.  N > 3. */

/*       X,Y,Z = Arrays of length N containing the Cartesian */
/*               coordinates of the nodes (unit vectors). */

/*       LIST,LPTR,LEND = Data structure defining the trian- */
/*                        gulation.  Refer to Subroutine */
/*                        TRMESH. */

/* Input parameters are not altered by this function. */

/* On output: */

/*       AREAV = Area of Voronoi region K unless IER > 0, */
/*               in which case AREAV = 0. */

/*       IER = Error indicator: */
/*             IER = 0 if no errors were encountered. */
/*             IER = 1 if K or N is outside its valid range */
/*                     on input. */
/*             IER = 2 if K indexes a boundary node. */
/*             IER = 3 if an error flag is returned by CIRCUM */
/*                     (null triangle). */
/*             IER = 4 if AREAS returns a value greater than */
/*                     AMAX (defined below). */

/* Modules required by AREAV:  AREAS, CIRCUM */

/* *********************************************************** */


/* Maximum valid triangle area is less than 2*Pi: */

    /* Parameter adjustments */
    --lend;
    --z__;
    --y;
    --x;
    --list;
    --lptr;

    /* Function Body */

/* Test for invalid input. */

    if (*k < 1 || *k > *n || *n <= 3) {
        goto L11;
    }

/* Initialization:  Set N3 to the last neighbor of N1 = K. */
/*   FIRST = TRUE only for the first triangle. */
/*   The Voronoi region area is accumulated in ASUM. */

    n1 = *k;
    v1[0] = x[n1];
    v1[1] = y[n1];
    v1[2] = z__[n1];
    lpl = lend[n1];
    n3 = list[lpl];
    if (n3 < 0) {
        goto L12;
    }
    lp = lpl;
    first = TRUE_;
    asum = 0.;

/* Loop on triangles (N1,N2,N3) containing N1 = K. */

L1:
    n2 = n3;
    lp = lptr[lp];
    n3 = list[lp];
    v2[0] = x[n2];
    v2[1] = y[n2];
    v2[2] = z__[n2];
    v3[0] = x[n3];
    v3[1] = y[n3];
    v3[2] = z__[n3];
    if (first) {

/* First triangle:  compute the circumcenter C3 and save a */
/*   copy in C0. */

        circum_(v1, v2, v3, c3, &ierr);
        if (ierr != 0) {
            goto L13;
        }
        c0[0] = c3[0];
        c0[1] = c3[1];
        c0[2] = c3[2];
        first = FALSE_;
    } else {

/* Set C2 to C3, compute the new circumcenter C3, and compute */
/*   the area A of triangle (V1,C2,C3). */

        c2[0] = c3[0];
        c2[1] = c3[1];
        c2[2] = c3[2];
        circum_(v1, v2, v3, c3, &ierr);
        if (ierr != 0) {
            goto L13;
        }
        a = areas_(v1, c2, c3);
        if (a > amax) {
            goto L14;
        }
        asum += a;
    }

/* Bottom on loop on neighbors of K. */

    if (lp != lpl) {
        goto L1;
    }

/* Compute the area of triangle (V1,C3,C0). */

    a = areas_(v1, c3, c0);
    if (a > amax) {
        goto L14;
    }
    asum += a;

/* No error encountered. */

    *ier = 0;
    ret_val = asum;
    return ret_val;

/* Invalid input. */

L11:
    *ier = 1;
    ret_val = 0.;
    return ret_val;

/* K indexes a boundary node. */

L12:
    *ier = 2;
    ret_val = 0.;
    return ret_val;

/* Error in CIRCUM. */

L13:
    *ier = 3;
    ret_val = 0.;
    return ret_val;

/* AREAS value larger than AMAX. */

L14:
    *ier = 4;
    ret_val = 0.;
    return ret_val;
} /* areav_ */
void Util::array_mutation ( float *  list,
int  len_list,
float  mutation_rate,
float  min_val,
float  max_val,
int  K,
int  is_mirror 
) [static]

The purpose of this function is to convert a list to grey code and mutate them and convert them back.

Definition at line 21731 of file util_sparx.cpp.

References t.

Referenced by image_mutation().

                                                                                                                            {

        if (is_mirror != 0) {
                for (int i=0; i<len_list; i++) {
                        int r = rand()%10000;
                        float f = r/10000.0f;
                        if (f < mutation_rate) list[i] = 1-list[i];
                }
        } else {
                map<int, vector<int> >  graycode;
                map<vector<int>, int> rev_graycode;
                vector <int> gray;

                int K=1;
                for (int i=0; i<L; i++) K*=2;

                for (int k=0; k<K; k++) {
                        int shift = 0;
                        vector <int> gray;
                        for (int i=L-1; i>-1; i--) {
                                int t = ((k>>i)%2-shift)%2;
                                gray.push_back(t);
                                shift += t-2;
                        }
                        graycode[k] = gray;
                        rev_graycode[gray] = k;
                }

                float gap = (K-1)/(max_val-min_val);
                for (int i=0; i<len_list; i++) {
                        float val = list[i];
                        if (val < min_val) { val = min_val; }
                        else if  (val > max_val) { val = max_val; }
                        int k = int((val-min_val)*gap+0.5);
                        vector<int> gray = graycode[k];
                        bool changed = false;
                        for (vector<int>::iterator p=gray.begin(); p!=gray.end(); p++) {
                                int r = rand()%10000;
                                float f = r/10000.0f;
                                if (f < mutation_rate) {
                                        *p = 1-*p;
                                        changed = true;
                                }
                        }
                        if (changed) {
                                k = rev_graycode[gray];
                                list[i] = k/gap+min_val;
                        }
                }
        }

}
vector< int > Util::assign_groups ( std::string  matrix_address,
int  nref,
int  nima 
) [static]

Definition at line 18381 of file util_sparx.cpp.

References matrix().

{
        const float * matrix;
        { // convert memory address sent as string to pointer to float
                size_t addr = 0;
                for ( std::string::const_iterator i = matrix_address.begin();  i != matrix_address.end();  ++i ) {
                        int digit = *i - '0';
                        addr *= 10;
                        addr += digit;
                }
                matrix = reinterpret_cast<float*>(addr);
        }

        int kt = nref;
        unsigned int maxasi = nima/nref;
        vector< vector<int> > id_list(nref);
        int group, ima;

        {
                int begin = 0;
                // allocate and sort vector of indexes
                std::vector<int> dd(nref*nima);
                for (int i=0; i<nref*nima; i++)  {
                        dd[i] = i;
                }
                assign_groups_comparator comparator(matrix);
                sort(dd.begin(), dd.end(), comparator);
                // main loop
                std::vector<bool> del_row(nref, false);
                std::vector<bool> del_column(nima, false);
                while (kt > 0) {
                        bool flag = true;
                        while (flag) {
                                int l = dd[begin];
                                group = l/nima;
                                ima = l%nima;
                                if (del_column[ima] || del_row[group]) begin++;
                                else flag = false;
                        }

                        id_list[group].push_back(ima);
                        if (kt > 1) {
                                if (id_list[group].size() < maxasi) group = -1;
                                else kt -= 1;
                        } else {
                                if (id_list[group].size() < maxasi+nima%nref) group = -1;
                                else kt -= 1;
                        }
                        del_column[ima] = true;
                        if (group != -1) {
                                del_row[group] = true;
                        }
                }
        }

        vector<int> id_list_1; 
        for (int iref=0; iref<nref; iref++)
                for (unsigned int im=0; im<maxasi; im++)
                        id_list_1.push_back(id_list[iref][im]);
        for (unsigned int im=maxasi; im<maxasi+nima%nref; im++)
                        id_list_1.push_back(id_list[group][im]);
        id_list_1.push_back(group);

        return id_list_1;
}
vector< int > Util::assign_projangles ( const vector< float > &  projangles,
const vector< float > &  refangles 
) [static]

Definition at line 18469 of file util_sparx.cpp.

References getvec(), nearest_ang(), x, and y.

Referenced by group_proj_by_phitheta().

                                                                                                   {
        int nref = refangles.size()/2;
        int nproj = projangles.size()/2;
        vector<int> asg(nproj);
        vector<float> vecref(nref*3);
        for (int i=0; i<nref; i++)
                getvec(refangles[i*2], refangles[i*2+1], vecref[i*3], vecref[i*3+1], vecref[i*3+2]);
        for (int i=0; i<nproj; i++) {
                float x, y, z;
                getvec(projangles[i*2], projangles[i*2+1], x, y, z);
                asg[i] = nearest_ang(vecref, x, y, z);
        }
        return asg;
}
vector< int > Util::bb_enumerateMPI_ ( int *  argParts,
int *  dimClasses,
int  nParts,
int  K,
int  T,
int  n_guesses,
int  LARGEST_CLASS,
int  J,
int  max_branching,
float  stmult,
int  branchfunc,
int  LIM 
) [static]

K is the number of classes in each partition (should be the same for all partitions) the first element of each class is its original index in the partition, and second is dummy var MPI: if nTop <= 0, then initial prune is called, and the pruned partitions are returned in a 1D array.

The first element is reserved for max_levels (the size of the smallest partition after pruning). if nTop > 0, then partitions are assumed to have been pruned, where only dummy variables of un-pruned partitions are set to 1, and findTopLargest is called to find the top weighted matches. The matches, where each match is preceded by its cost, is returned in a one dimensional vector.

essentially the same as bb_enumerate but with the option to do mpi version.

Definition at line 22149 of file util_sparx.cpp.

References branchMPI(), initial_prune(), and sanitycheck().

                                                          {
        
        
        // Indices is an nParts*K int array storing the index (into argparts) of the first element of the i-th class of the j-th partition
        // So Indices[j*K + i] is the offset from argparts of the first element of the first element  of the i-th class of the j-th partition
        // Make a vector of nParts vectors of K int* each
         int* Indices = new int[nParts*K];
         int ind_c = 0;
         for (int i=0; i < nParts; i++){
                 for(int j = 0; j < K; j++){
                         Indices[i*K + j] = ind_c;
                         ind_c = ind_c + dimClasses[i*K + j];
                 }
         }

        // do initial pruning on argParts and return the pruned partitions

        // Make a vector of nParts vectors of K int* each
        vector <vector <int*> > Parts(nParts,vector<int*>(K));
        ind_c = 0;
        int argParts_size=0;
        for (int i=0; i < nParts; i++){
                for(int j = 0; j < K; j++){
                        Parts[i][j] = argParts + ind_c;
                        ind_c = ind_c + dimClasses[i*K + j];
                        argParts_size = argParts_size + dimClasses[i*K + j];
                }
        }

        // in the following we call initial_prune with Parts which is a vector. This is not the most
        // efficient since vector does not allow for direct addressing. But since initial_prune doesn't have very high complexity, and
        // the running time for 7 partitions with 288 classes per partition is a couple of minutes at most, i'll just leave it for now.....

        // comment out for testing
        Util::initial_prune(Parts, dimClasses, nParts, K, T);
        for(int i = 0; i < nParts; i++){
                for(int j=0; j < K; j++){
                        argParts[Indices[i*K + j]+1] = -1;
                }
        }

        int num_classes;
        int old_index;
        for(int i=0; i<nParts; i++){
                num_classes = Parts[i].size();// number of classes in partition i after pruning
                for (int j=0; j < num_classes; j++){
                        old_index = Parts[i][j][0];
                        //cout << "old_index: " << old_index<<"\n";
                        argParts[Indices[i*K + old_index]+1] = 1;
                }
        }


        // if we're not doing mpi then keep going and call branchMPI and return the output
        //cout <<"begin partition matching\n";
        //int* dummy(0);
        int* output = Util::branchMPI(argParts, Indices,dimClasses, nParts, K, T, 0, n_guesses, LARGEST_CLASS, J, max_branching, stmult, branchfunc, LIM);
        
        //cout<<"total cost: "<<*output<<"\n";
        //cout<<"number of matches: "<<*(output+1)<<"\n";
        // now go check if the matches are sensical! i.e, if the matches are feasible, if the sum of the match weights in output is equal to *output, and if each match in output has weight at least T
        bool correct = Util::sanitycheck(argParts, Indices,dimClasses, nParts, K, T,output);

        delete[] Indices;

        // something is wrong with output of branchMPI!
        if (correct < 1){
                cout << "something is wrong with output of branchMPI!\n";
                vector<int> ret(1);
                ret[0] = -1;
                if (output != 0)  { delete[] output; output = 0; }
                return ret;
        }

        // output is not nonsense, so now put it into a single dimension vector and return
        // output is an int array, the first element is the cost of the output solution, the second element is the total number of matches in the solution
        // and the rest is the list of matches. output is one dimensional

        int output_size = 2 + output[1] * nParts;
        vector<int> ret(output_size);
        for (int i = 0; i < output_size; i++) {
                ret[i]= output[i];
        }
        if (output != 0) { delete[] output; output = 0; }
        return ret;

}
float Util::bilinear ( float  xold,
float  yold,
int  nsam,
int  nrow,
float *  xim 
) [static]

Definition at line 2485 of file util_sparx.cpp.

References xim.

{
/*
c  purpose: linear interpolation
  Optimized for speed, circular closer removed, checking of ranges removed
*/
    float bilinear;
    int   ixold, iyold;

/*
        float xdif, ydif, xrem, yrem;
        ixold   = (int) floor(xold);
        iyold   = (int) floor(yold);
        ydif = yold - iyold;
        yrem = 1.0f - ydif;

        //  May want to insert if?
//              IF ((IYOLD .GE. 1 .AND. IYOLD .LE. NROW-1) .AND.
//     &            (IXOLD .GE. 1 .AND. IXOLD .LE. NSAM-1)) THEN
//c                INSIDE BOUNDARIES OF OUTPUT IMAGE
        xdif = xold - ixold;
        xrem = 1.0f- xdif;
//                 RBUF(K) = YDIF*(BUF(NADDR+NSAM)*XREM
//     &                    +BUF(NADDR+NSAM+1)*XDIF)
//     &                    +YREM*(BUF(NADDR)*XREM + BUF(NADDR+1)*XDIF)
        bilinear = ydif*(xim(ixold,iyold+1)*xrem + xim(ixold+1,iyold+1)*xdif) +
                                        yrem*(xim(ixold,iyold)*xrem+xim(ixold+1,iyold)*xdif);

    return bilinear;
}
*/
        float xdif, ydif;

        ixold   = (int) xold;
        iyold   = (int) yold;
        ydif = yold - iyold;

        //  May want to insert it?
//              IF ((IYOLD .GE. 1 .AND. IYOLD .LE. NROW-1) .AND.
//     &            (IXOLD .GE. 1 .AND. IXOLD .LE. NSAM-1)) THEN
//c                INSIDE BOUNDARIES OF OUTPUT IMAGE
        xdif = xold - ixold;
        bilinear = xim(ixold, iyold) + ydif* (xim(ixold, iyold+1) - xim(ixold, iyold)) +
                   xdif* (xim(ixold+1, iyold) - xim(ixold, iyold) +
                           ydif* (xim(ixold+1, iyold+1) - xim(ixold+1, iyold) - xim(ixold, iyold+1) + xim(ixold, iyold)) );

        return bilinear;
}
static float EMAN::Util::bilinear_interpolate ( float  p1,
float  p2,
float  p3,
float  p4,
float  t,
float  u 
) [inline, static]

Calculate bilinear interpolation.

Parameters:
[in]p1The first number. corresponding to (x0,y0).
[in]p2The second number. corresponding to (x1,y0).
[in]p3The third number. corresponding to (x1,y1).
[in]p4The fourth number. corresponding to (x0,y1).
[in]tt
[in]uu
Returns:
The bilinear interpolation value.

Definition at line 490 of file util.h.

Referenced by EMAN::EMData::common_lines(), EMAN::EMData::cut_slice(), EMAN::EMData::sget_value_at_interp(), EMAN::TransformProcessor::transform(), and EMAN::EMData::unwrap().

                {
                        return (1-t) * (1-u) * p1 + t * (1-u) * p2 + (1-t) * u * p3 + t * u * p4;
                }
void Util::BPCQ ( EMData B,
EMData CUBE,
const int  radius 
) [static]

Definition at line 5933 of file util_sparx.cpp.

References EMAN::EMData::depad(), DM, EMAN::EMData::do_ift_inplace(), EMAN::EMData::get_attr(), EMAN::EMData::get_data(), EMAN::Transform::get_matrix(), EMAN::Transform::get_params(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), EMAN::EMData::is_complex(), t_BPCQ_line::offset, t_BPCQ_line::rX, t_BPCQ_line::xbb, and t_BPCQ_line::ybb.

{
        if (B->is_complex()) {
                B->do_ift_inplace();
                B->depad();
        }

        const Transform * transform = B->get_attr("xform.projection");
        Dict transform_params = transform->get_params("spider");

        // ---- build DM matrix (transform matrix) - convert from 3x4 matrix to 2x3 matrix (only 2 first rows are nedeed)
        std::vector<float> DM = transform->get_matrix();
        DM[3+0] = DM[4+0];
        DM[3+1] = DM[4+1];
        DM[3+2] = DM[4+2];

        delete transform;

        const int NSAM = B->get_xsize();
        const int NROW = B->get_ysize();

        // buffer "lines_to_process" should be aligned to size of cache line (usually 64 or 128 bytes)
        t_BPCQ_line * lines_to_process;
#ifdef _WIN32
        if ( (lines_to_process = (t_BPCQ_line *)_aligned_malloc( 4*radius*radius*sizeof(t_BPCQ_line), 256 )) == NULL )
#else
        if ( posix_memalign( reinterpret_cast<void**>(&lines_to_process), 256, 4*radius*radius*sizeof(t_BPCQ_line) ) != 0 )
#endif  //_WIN32
        {
                throw std::bad_alloc();
        }
        t_BPCQ_line * first_free_line = lines_to_process;

        // calculate lines parameters
        {
                //  Unsure about sign of shifts, check later PAP 06/28/09
                const float x_shift_plus_center = float(NSAM/2 +1) + float(transform_params[ "tx" ]);
                const float y_shift_plus_center = float(NROW/2 +1) + float(transform_params[ "ty" ]);

                const int sizeX = CUBE->get_xsize();
                const int sizeY = CUBE->get_ysize();
                const int sizeZ = CUBE->get_zsize();

                const int centerX = sizeX / 2;
                const int centerY = sizeY / 2;
                const int centerZ = sizeZ / 2;

                const int minRZ = (centerZ >= radius) ? (-radius) : (-centerZ);
                const int maxRZ = (sizeZ > centerZ+radius) ? (radius) : (sizeZ-centerZ-1);

                for ( int rZ=minRZ; rZ<=maxRZ; ++rZ ) {
                        for ( int rY=-radius; rY<=radius; ++rY ) {
                                const int sqRX = radius*radius - rZ*rZ - rY*rY;
                                if (sqRX >= 0) {
#ifdef  _WIN32
                                        first_free_line->rX     = static_cast<int>( floor(sqrtf(sqRX)+0.5) );
#else
                                        first_free_line->rX     = static_cast<int>( roundf(sqrtf(sqRX)) );
#endif  //_WIN32
                                        first_free_line->offset = sizeX*( centerY+rY + sizeY*(centerZ+rZ) ) + centerX - first_free_line->rX;
                                        first_free_line->xbb    = rZ*DM[2] + rY*DM[1] + x_shift_plus_center;
                                        first_free_line->ybb    = rZ*DM[5] + rY*DM[4] + y_shift_plus_center;
                                        ++first_free_line;
                                }
                        }
                }
        }

        const float * const Bptr = B->get_data();
        float * const CUBE_begin = CUBE->get_data();

        // update voxels in volume
        // this loop takes more than 95% of calculations time spent in Util::BPCQ function
        for ( t_BPCQ_line * iLine = lines_to_process; iLine < first_free_line; ++iLine ) {
                const int rX_first = -(iLine->rX);
                const int rX_last  =   iLine->rX;
                float  *CUBE_ptr = CUBE_begin + iLine->offset;
                for (int rX=rX_first; rX<=rX_last; ++rX, ++CUBE_ptr) {
                        const float XB  = rX * DM[0] + iLine->xbb;
                        const float YB  = rX * DM[3] + iLine->ybb;
                        const int IQX = int(XB);
                        const int IQY = int(YB);
                        if ( IQX < 1 || IQX >= NSAM || IQY < 1 || IQY >= NROW )  continue;
                        const float DIPX = XB-IQX;
                        const float DIPY = YB-IQY;
                        const float b00 = Bptr[IQX-1+((IQY-1)*NSAM)];
                        const float b01 = Bptr[IQX-1+((IQY-0)*NSAM)];
                        const float b10 = Bptr[IQX-0+((IQY-1)*NSAM)];
                        const float b11 = Bptr[IQX-0+((IQY-0)*NSAM)];
                        *(CUBE_ptr) = *(CUBE_ptr) + b00 + DIPY*(b01-b00) + DIPX*(b10-b00+DIPY*(b11-b10-b01+b00));
                }
        }

        free(lines_to_process);
}
int Util::branch_factor_0 ( int *  costlist,
int *  matchlist,
int  J,
int  T,
int  nParts,
int  curlevel,
int  max_branching,
int  LIM 
) [static]

Definition at line 22668 of file util_sparx.cpp.

References branch_all, costlist_global, and jiafunc().

Referenced by branchMPI().

                                                                                                                 {
        
        int ntot=0;
        for (int jit=0; jit < J; jit++){
                if (*(costlist+jit) > T) ntot++;
        }

        int cur;
        // sort matchlist by cost
        int* indx = new int[J];
        for (int jit=0; jit < J; jit++) indx[jit]=jit;
        vector<int> myindx (indx, indx+J);
        vector<int>::iterator it;
        costlist_global=costlist;
        sort(myindx.begin(), myindx.begin()+J, jiafunc);

        // put matchlist in the order of mycost
        int* templist = new int[J];
        int* temp2list = new int[J*nParts];
        int next = 0;
        
        for (it=myindx.begin(); it!=myindx.end();++it){
                cur = *(costlist + *it);
                if (cur > T){
                        
                        templist[next] = cur;
                        for (int vt = 0; vt < nParts; vt++) temp2list[next*nParts + vt] = matchlist[ (*it)*nParts + vt];
                        next = next + 1;
                }
        }
        
        for (int jit=0; jit < ntot; jit++){
                *(costlist+jit)=*(templist + jit);
                //cout <<*(costlist+jit)<<", ";
                for (int vit=0; vit < nParts; vit++) matchlist[jit*nParts + vit]= temp2list[jit*nParts + vit];
        }
        //cout <<"\n";
        
        for (int jit=1; jit < ntot; jit++){
        
             if ((costlist[jit] == costlist[0]) && costlist[jit] > T){
             
                     for (int vit=0; vit < nParts; vit++){
                             if ( matchlist[jit*nParts + vit] >  matchlist[vit])
                                 break;
                             if ( matchlist[jit*nParts + vit] ==  matchlist[vit])
                                 continue;
                             if ( matchlist[jit*nParts + vit] <  matchlist[vit])
                             {
                                 // swap
                                 for (int swp=0; swp < nParts; swp++){
                                       int tmp  = matchlist[swp];
                                       matchlist[swp]= matchlist[jit*nParts + swp];
                                       matchlist[jit*nParts + swp] = tmp;
                                 }
                                 break;
                             
                             }   
                     }
             }
        
        }
        
        
        delete [] indx;
        //compute the average 
        
        // partition matches into groups where matches in each group is mutually feasible, and matches from different groups are not feasible
        // The number of branches to explore are the mutually exclusive matches. Take the largest of these
        
        
        int B=1;
        int B_init=B;
        int infeasible=0;
        
        for (int i=B_init; i<ntot; i++){
                if (i==ntot) continue;
                // look at the i-th match. branch on it only if it is infeasible with ALL previous matches we have decided to
                // branch on
                infeasible = 0;
                if (LIM < 0) LIM = B;
                for (int j=0; j<B; j++){
                        
                        for (int vit=0; vit<nParts; vit++){
                                if (temp2list[i*nParts+vit] == matchlist[j*nParts+vit]) {infeasible++; break;}
                        }
                        if (infeasible >= LIM) break;
                }
                
                if (infeasible >= LIM){
                        *(costlist+B)=*(templist+i);
                        for (int vit=0; vit < nParts; vit++)
                                *(matchlist+B*nParts + vit)=*(temp2list+i*nParts + vit);
                        B=B+1;  
                }
        }
        
        delete [] templist;
        delete [] temp2list;
        //cout<<"**************************************** "<<B<<" ***************************\n";
        
        if (branch_all < max_branching){
                if (B>1)
                        {branch_all = branch_all + B -1 ; }
        }
        else B=1;
        
        return B;

}
int Util::branch_factor_2 ( int *  costlist,
int *  matchlist,
int  J,
int  T,
int  nParts,
int  curlevel,
int  max_branching,
int  LIM 
) [static]

Definition at line 22406 of file util_sparx.cpp.

References branch_all, costlist_global, and jiafunc().

Referenced by branchMPI().

                                                                                                                 {
        
        int ntot=0;
        for (int jit=0; jit < J; jit++){
                if (*(costlist+jit) > T) ntot++;
        }

        int cur;
        // sort matchlist by cost
        int* indx = new int[J];
        for (int jit=0; jit < J; jit++) indx[jit]=jit;
        vector<int> myindx (indx, indx+J);
        vector<int>::iterator it;
        costlist_global=costlist;
        sort(myindx.begin(), myindx.end(), jiafunc);

        // put matchlist in the order of mycost
        int* templist = new int[J];
        int* temp2list = new int[J*nParts];
        int next = 0;
        
        for (it=myindx.begin(); it!=myindx.end();++it){
                cur = *(costlist + *it);
                if (cur > T){
                        
                        templist[next] = cur;
                        for (int vt = 0; vt < nParts; vt++) temp2list[next*nParts + vt] = matchlist[ (*it)*nParts + vt];
                        next = next + 1;
                }
        }
        
        for (int jit=0; jit < ntot; jit++){
                *(costlist+jit)=*(templist + jit);
                //cout <<*(costlist+jit)<<", ";
                for (int vit=0; vit < nParts; vit++) matchlist[jit*nParts + vit]= temp2list[jit*nParts + vit];
        }
        //cout <<"\n";
        
        delete [] indx;
        //compute the average 
        
        // partition matches into groups where matches in each group is mutually feasible, and matches from different groups are not feasible
        // The number of branches to explore are the mutually exclusive matches. Take the largest of these
        
        
        int B=1;
        int B_init=B;
        int infeasible=0;
        
        for (int i=B_init; i<ntot; i++){
                if (i==ntot) continue;
                // look at the i-th match. branch on it only if it is infeasible with ALL previous matches we have decided to
                // branch on
                infeasible = 0;
                if (LIM < 0) LIM = B;
                for (int j=0; j<B; j++){
                        
                        for (int vit=0; vit<nParts; vit++){
                                if (temp2list[i*nParts+vit] == matchlist[j*nParts+vit]) {infeasible++; break;}
                        }
                        if (infeasible >= LIM) break;
                }
                
                if (infeasible >= LIM){
                        *(costlist+B)=*(templist+i);
                        for (int vit=0; vit < nParts; vit++)
                                *(matchlist+B*nParts + vit)=*(temp2list+i*nParts + vit);
                        B=B+1;  
                }
        }
        
        delete [] templist;
        delete [] temp2list;
        //cout<<"**************************************** "<<B<<" ***************************\n";
        
        if (branch_all < max_branching){
                if (B>1)
                        {branch_all = branch_all + B -1 ; }
        }
        else B=1;
        
        return B;
        

}
int Util::branch_factor_3 ( int *  costlist,
int *  matchlist,
int  J,
int  T,
int  nParts,
int  curlevel,
int  max_branching,
int  K,
int  LIM 
) [static]

Definition at line 22494 of file util_sparx.cpp.

References branch_all, costlist_global, and jiafunc().

Referenced by branchMPI().

                                                                                                                      {
        
        int ntot=0;
        for (int jit=0; jit < J; jit++){
                if (*(costlist+jit) > T) ntot++;
        }

        int cur;
        // sort matchlist by cost
        int* indx = new int[J];
        for (int jit=0; jit < J; jit++) indx[jit]=jit;
        vector<int> myindx (indx, indx+J);
        vector<int>::iterator it;
        costlist_global=costlist;
        sort(myindx.begin(), myindx.begin()+J, jiafunc);

        // put matchlist in the order of mycost
        int* templist = new int[J];
        int* temp2list = new int[J*nParts];
        int next = 0;
        
        for (it=myindx.begin(); it!=myindx.end();++it){
                cur = *(costlist + *it);
                if (cur > T){
                        
                        templist[next] = cur;
                        for (int vt = 0; vt < nParts; vt++) temp2list[next*nParts + vt] = matchlist[ (*it)*nParts + vt];
                        next = next + 1;
                }
        }
        
        for (int jit=0; jit < ntot; jit++){
                *(costlist+jit)=*(templist + jit);
                //cout <<*(costlist+jit)<<", ";
                for (int vit=0; vit < nParts; vit++) matchlist[jit*nParts + vit]= temp2list[jit*nParts + vit];
        }
        //cout <<"\n";
        
        delete [] indx;
        //compute the average 
        
        // partition matches into groups where matches in each group is mutually feasible, and matches from different groups are not feasible
        // The number of branches to explore are the mutually exclusive matches. Take the largest of these
        
        
        int B=1;
        int B_init=B;
        int infeasible=0;
        // if we're near the bottom of the tree then explore more... this is because the larger weights are not likely to change much,
        // whereas the smaller ones can have many permutations
        if (LIM < 0) LIM = ntot-1;
        for (int i=B_init; i<ntot; i++){
                if (i==ntot) continue;
                // look at the i-th match. branch on it only if it is infeasible with ALL previous matches we have decided to
                // branch on
                infeasible = 0;
                
                for (int j=0; j<ntot; j++){
                        if (j == i) continue;
                        for (int vit=0; vit<nParts; vit++){
                                if (temp2list[i*nParts+vit] == temp2list[j*nParts+vit]) {infeasible++; break;}
                        }
                        if (infeasible >= LIM) break;
                }
                
                if (infeasible >= LIM){
                        *(costlist+B)=*(templist+i);
                        for (int vit=0; vit < nParts; vit++)
                                *(matchlist+B*nParts + vit)=*(temp2list+i*nParts + vit);
                        B=B+1;  
                }
        }
        
        delete [] templist;
        delete [] temp2list;
        //cout<<"**************************************** "<<B<<" ***************************\n";
        
        
        if (branch_all < max_branching){
                if (B>1)
                        {branch_all = branch_all + B-1;}
        }
        else B=1;
        
        return B;
        

}
int Util::branch_factor_4 ( int *  costlist,
int *  matchlist,
int  J,
int  T,
int  nParts,
int  curlevel,
int  max_branching,
float  stmult 
) [static]

Definition at line 22587 of file util_sparx.cpp.

References branch_all, costlist_global, and jiafunc().

Referenced by branchMPI().

                                                                                                                      {
        int sum=0;
        float average =0;
        int ntot=0;
        for (int jit=0; jit < J; jit++){
                if (*(costlist+jit) > T) {ntot++; sum = sum +*(costlist+jit);}
        }
        average = ((float)sum)/((float)ntot);
        int cur;
        // sort matchlist by cost
        int* indx = new int[J];
        for (int jit=0; jit < J; jit++) indx[jit]=jit;
        vector<int> myindx (indx, indx+J);
        vector<int>::iterator it;
        costlist_global=costlist;
        sort(myindx.begin(), myindx.begin()+J, jiafunc);

        // put matchlist in the order of mycost
        int* templist = new int[J];
        int* temp2list = new int[J*nParts];
        int next = 0;
        
        for (it=myindx.begin(); it!=myindx.end();++it){
                cur = *(costlist + *it);
                if (cur > T){
                        
                        templist[next] = cur;
                        for (int vt = 0; vt < nParts; vt++) temp2list[next*nParts + vt] = matchlist[ (*it)*nParts + vt];
                        next = next + 1;
                }
        }
        
        for (int jit=0; jit < ntot; jit++){
                *(costlist+jit)=*(templist + jit);
                //cout <<*(costlist+jit)<<", ";
                for (int vit=0; vit < nParts; vit++) matchlist[jit*nParts + vit]= temp2list[jit*nParts + vit];
        }
        //cout <<"\n";
        
        delete [] indx;
        delete [] templist;
        delete [] temp2list;
        
        if (ntot == 1) return 1;
        
        // look at the average, standard dev etc. If standard dev very small, i.e., costs very similar, then branch on the similar
        // costs
        float sq_sum=0.0;
        //cout <<"costlist:";
        for (int i=0; i< ntot; i++){
                sq_sum = sq_sum + (float) pow((float) *(costlist+i) - average, (float)2.0);
                //cout <<*(costlist+i)<<", ";
        }       
        //cout <<"\n";
        
        float variance = sq_sum/ntot;
        float stdev = (float)pow((float)variance,(float)0.5);
        
        //cout <<"stdev: "<<int(stdev)<<"\n";
        
        int B=1;
        int largest = *costlist;
        //cout <<"largest: "<<largest<<"\n";
        for (int i=1; i<ntot; i++){
                int cur = *(costlist+i);
                if (largest-cur < (float)(stdev*stmult)) B++;
                else break;
        
        }
        //cout <<"B: "<<B<<"\n";
        if (branch_all < max_branching){
                if (B>1)
                        {branch_all = branch_all + B-1;}
        }
        else B=1;
        
        return B;
        

}
int * Util::branchMPI ( int *  argParts,
int *  Indices,
int *  dimClasses,
int  nParts,
int  K,
int  T,
int  curlevel,
int  n_guesses,
int  LARGEST_CLASS,
int  J,
int  max_branching,
float  stmult,
int  branchfunc,
int  LIM 
) [static]

same as branch except the nFirst (=Levels[0]) possibilites for the first match are already chosen firstmatches stores the matches and corresponding cost, where each match is preceded by its cost....

output is an int array, the first element is the cost of the output solution, the second element is the total number of matches in the solution and the rest is the list of matches. output is in one dimensional form.

Definition at line 22240 of file util_sparx.cpp.

References branch_all, branch_factor_0(), branch_factor_2(), branch_factor_3(), branch_factor_4(), and search2().

Referenced by bb_enumerateMPI_().

                                                                                {

//*************************************
//testing search2
if (1 == 0){
cout <<"begin test search2\n";
int* matchlist = new int[J*nParts];
int* costlist = new int[J];
for (int jit = 0; jit < nParts; jit++) *(costlist+jit) = 0;
Util::search2(argParts,Indices, dimClasses, nParts, K,  T, matchlist,costlist,J);

for (int jit = 0; jit < J; jit++) {
  cout << *(costlist +jit)<<": ";
  for (int yit = 0; yit < nParts; yit++)
        cout << *(matchlist + jit*nParts + yit)<<",";
  cout <<"\n";  

}
cout <<"end test search2\n";
int* output = new int[1];
output[0] = 1;
delete [] matchlist;
delete [] costlist;
return output;
}
//**************************************

        // Base Case: we're at a leaf, no more feasible matches possible
        if (curlevel > K -1){
                int* output = new int[2];
                output[0] = 0;
                output[1] = 0;
                return output;
        }

        // branch dynamically depending on results of search 2!
        
        int* matchlist = new int[J*nParts];
        int* costlist = new int[J];
        Util::search2(argParts, Indices, dimClasses, nParts, K,  T, matchlist, costlist, J);
        
        
        // each class in the matches found by findTopLargest is encoded by the original index of the first element of the class in argPart
        // each match contains nParts classes, with the i-th class belonging to the i-th partition.

        // if there are no feasible matches with cost gt T, then return 0
        for (int jit = 0; jit < J ; jit++){
        
                if (costlist[jit] > T) break;
                if (jit == J-1){
                        int* output = new int[2];
                        output[0] = 0;
                        output[1] = 0;
                        delete[] matchlist;
                        delete[] costlist;
                        return output;
                }
        }
        

        
        // note that costlist and matchlist are NOT sorted by weight, and branch factor takes care of that...
        if (curlevel==0) branch_all = 0;
        
        int nBranches = -1;

        if (branchfunc == 0)
                nBranches = branch_factor_0(costlist,matchlist,J, T, nParts, curlevel, max_branching, LIM); // branch based on distribution of top J (weighted) matches  with cost > T

        if (branchfunc == 2)
                nBranches = branch_factor_2(costlist,matchlist,J, T, nParts, curlevel, max_branching, LIM); // branch based on distribution of top J (weighted) matches  with cost > T

        if (branchfunc == 3)
                nBranches = branch_factor_3(costlist,matchlist,J, T, nParts, curlevel, max_branching, K, LIM); // branch based on distribution of top J (weighted) matches  with cost > T

        if (branchfunc == 4)
                nBranches = branch_factor_4(costlist,matchlist,J, T, nParts, curlevel, max_branching, stmult); // branch based on distribution of top J (weighted) matches  with cost > T

        int* newcostlist= new int[nBranches];
        int* newmatchlist = new int[nBranches*nParts];
        for (int i=0; i<nBranches; i++){
                newcostlist[i] = costlist[i];
                for (int j=0; j< nParts; j++)
                        newmatchlist[i*nParts + j] = matchlist[i*nParts + j];
        }

        delete[] costlist;
        delete[] matchlist;
        
        //int* output = new int[2];//initialize to placeholder
        int* output = new int[2+K*nParts];//initialize to placeholder
        output[0] = 0;
        output[1] = 0;
        // some temporary variables
        int old_index;
        int totalcost;
        int nmatches;
        //int offset;

        for(int i=0; i < nBranches ; i++){

                // consider the i-th match returned by findTopLargest
                //if (newcostlist[i] <= T) continue;

                // mark the classes in the i-th match of matchlist as taken (using the dummy variable and -2), and then call branch again on argParts.
                // After branch returns, compute overall cost, unmark  the classes just marked as 1 again in preparation for next loop.

                for(int j=0; j < nParts; j++){
                        // matchlist[i*nParts + j] is the original index of the class belonging to the j-th partition in the i-th match.
                        old_index = newmatchlist[i*nParts + j];
                        argParts[Indices[j*K+old_index] + 1] = -2;
                }

                
                int* ret = Util::branchMPI(argParts, Indices, dimClasses, nParts, K, T, curlevel+1, n_guesses, LARGEST_CLASS,
                J, max_branching, stmult,branchfunc, LIM);
                
                // first element of ret is the total cost of all the matches in ret, and second element is the number of matches in ret
                totalcost = newcostlist[i] + ret[0];

                //if (curlevel == 0) {
                //      cout <<"totalcost*****************************************************************: "<<totalcost<<", costlist["<<i<<"]="<<newcostlist[i]<<", *ret="<<*ret<<", level: "<<curlevel<<"\n";
                        
                //}
                if (totalcost > output[0]) // option 1
                {
                        nmatches = 1 + ret[1];
                        //delete[] output; // get rid of the old maxreturn
                        //output = new int[2+nmatches*nParts];
                        output[0] = totalcost;
                        output[1] = nmatches;
                        int nret = 2+(nmatches-1)*nParts;
                        for(int iret=2; iret < nret; iret++) output[iret] = ret[iret];
                        for(int imax=0; imax < nParts; imax++) output[nret+imax] = newmatchlist[i*nParts + imax];
                }


                delete[] ret;

                // unmark the marked classes in preparation for the next iteration

                for(int j=0; j < nParts; j++){
                        old_index = newmatchlist[i*nParts + j];
                        argParts[Indices[j*K+old_index] + 1] = 1;
                }

        }

        delete[] newmatchlist;
        delete[] newcostlist;
        
        return output;
}
static EMData* EMAN::Util::calc_bessel ( const int  n,
const float &  x 
) [static]
int Util::calc_best_fft_size ( int  low) [static]

Search the best FFT size with good primes.

It supports FFT size up to 4096 now.

Parameters:
[in]lowlow size the search starts with.
Returns:
The best FFT size.

Definition at line 1018 of file util.cpp.

References Assert, and LOGERR.

Referenced by EMAN::FRM2DAligner::align(), EMAN::RTFExhaustiveAligner::align(), EMAN::RotatePrecenterAligner::align(), EMAN::EMData::unwrap(), and EMAN::EMData::unwrap_largerR().

{
        Assert(low >= 0);

        //array containing valid sizes <1024 for speed
        static char *valid = NULL;

        if (!valid) {
                valid = (char *) calloc(4096, 1);

                for (float i2 = 1; i2 < 12.0; i2 += 1.0) {

                        float f1 = pow((float) 2.0, i2);
                        for (float i3 = 0; i3 < 8.0; i3 += 1.0) {

                                float f2 = pow((float) 3.0, i3);
                                for (float i5 = 0; i5 < 6.0; i5 += 1.0) {

                                        float f3 = pow((float) 5.0, i5);
                                        for (float i7 = 0; i7 < 5.0; i7 += 1.0) {

                                                float f = f1 * f2 * f3 * pow((float) 7.0, i7);
                                                if (f <= 4095.0) {
                                                        int n = (int) f;
                                                        valid[n] = 1;
                                                }
                                        }
                                }
                        }
                }
        }

        for (int i = low; i < 4096; i++) {
                if (valid[i]) {
                        return i;
                }
        }

        LOGERR("Sorry, can only find good fft sizes up to 4096 right now.");

        return 1;
}
Vec3f Util::calc_bilinear_least_square ( const vector< float > &  points) [static]

calculate bilinear least-square fit, z = a + b x + c y Takes a set of x,y,z vectors and produces an a,b,c vector does not accept error bars on z or return goodness of fit

Parameters:
[in]pointsa vector<float> of x,y,z values in (x1,y1,z1,x2,y2,z2...) sequence to fit a plane to
Returns:
result as a Vec3f(a,b,c)

Definition at line 582 of file util.cpp.

Referenced by EMAN::PointArray::align_2d().

                                                             {
unsigned int i;

// various sums used in the final solution
double Sx=0,Sy=0,Sxy=0,Sxx=0,Syy=0,Sz=0,Sxz=0,Syz=0,S=0;
for (i=0; i<p.size(); i+=3) {
        Sx+=p[i];
        Sy+=p[i+1];
        Sz+=p[i+2];
        Sxx+=p[i]*p[i];
        Syy+=p[i+1]*p[i+1];
        Sxy+=p[i]*p[i+1];
        S+=1.0;
        Sxz+=p[i]*p[i+2];
        Syz+=p[i+1]*p[i+2];
}
double d=S*Sxy*Sxy - 2*Sx*Sxy*Sy + Sxx*Sy*Sy  + Sx*Sx*Syy - S*Sxx*Syy;

Vec3f ret(0,0,0);

ret[0]=static_cast<float>(-((Sxy*Sxz*Sy - Sx*Sxz*Syy + Sx*Sxy*Syz - Sxx*Sy*Syz - Sxy*Sxy*Sz +Sxx*Syy*Sz)/d));
ret[1]=static_cast<float>(-((-Sxz*Sy*Sy  + S*Sxz*Syy - S*Sxy*Syz + Sx*Sy*Syz + Sxy*Sy*Sz -Sx*Syy*Sz) /d));
ret[2]=static_cast<float>(-((-S*Sxy*Sxz + Sx*Sxz*Sy - Sx*Sx*Syz + S*Sxx*Syz + Sx*Sxy*Sz -Sxx*Sy*Sz) /d));

return ret;
}
void Util::calc_least_square_fit ( size_t  nitems,
const float *  data_x,
const float *  data_y,
float *  p_slope,
float *  p_intercept,
bool  ignore_zero,
float  absmax = 0 
) [static]

calculate the least square fit value.

Parameters:
[in]nitemsNumber of items in array data_x and data_y.
[in]data_xx data array.
[in]data_yy data array. It should have the same number of items to data_x.
[out]p_slopepointer to the result slope.
[out]p_interceptpointer to the result intercept.
[in]ignore_zeroIf true, ignore data where either x or y is 0. If false, includes all 0.
[in]absmaxIgnores values in y more than absmax from zero

Definition at line 543 of file util.cpp.

References Assert, div(), NullPointerException, x, and y.

Referenced by EMAN::OptVarianceCmp::cmp(), EMAN::LowpassAutoBProcessor::create_radial_func(), and EMAN::GradientRemoverProcessor::process_inplace().

{
        Assert(nitems > 0);

        if (!data_x || !data_y || !slope || !intercept) {
                throw NullPointerException("null float pointer");
        }
        double sum = 0;
        double sum_x = 0;
        double sum_y = 0;
        double sum_xx = 0;
        double sum_xy = 0;

        for (size_t i = 0; i < nitems; i++) {
                if ((!ignore_zero || (data_x[i] != 0 && data_y[i] != 0))&&(!absmax ||(data_y[i]<absmax && data_y[i]>-absmax))) {
                        double y = data_y[i];
                        double x = i;
                        if (data_x) {
                                x = data_x[i];
                        }

                        sum_x += x;
                        sum_y += y;
                        sum_xx += x * x;
                        sum_xy += x * y;
                        sum++;
                }
        }

        double div = sum * sum_xx - sum_x * sum_x;
        if (div == 0) {
                div = 0.0000001f;
        }

        *intercept = (float) ((sum_xx * sum_y - sum_x * sum_xy) / div);
        *slope = (float) ((sum * sum_xy - sum_x * sum_y) / div);
}
vector< float > Util::call_cl1 ( long int *  k,
long int *  n,
float *  ps,
long int *  iswi,
float *  pw,
float *  q2,
double *  q,
double *  x,
double *  res,
double *  cu,
double *  s,
long int *  iu 
) [static]

Definition at line 6647 of file util_sparx.cpp.

References lsfit(), and x.

Referenced by pw_extract().

{
    long int q2_dim1, q2_offset, q_dim1, q_offset, i__1, i__2;
    float r__1;
    int tmp__i;
    long int i__, j;
    --s;
    --res;
    iu -= 3;
    cu -= 3;
    --x;
    long int klm2d;
    klm2d= *k+*k+2;
    klm2d=klm2d+klm2d;
    q_dim1 = klm2d;
    q_offset = 1 + q_dim1;
    q -= q_offset;
    q2_dim1 = klm2d;
    q2_offset = 1 + q2_dim1;
    q2 -= q2_offset;
    i__2=0;
    i__1 = *n - 1;
    tmp__i=0;
    for (j = 1; j <= i__1; ++j) {
        i__2 = *k;
        tmp__i+=1;
        for (i__ = 1; i__ <= i__2; ++i__) {
            r__1 = float(i__ - 1) /(float) *k / (*ps * 2);
            q2[i__ + j * q2_dim1] = pow(r__1, tmp__i);
            }
    }
    for  (i__ = 1; i__ <= i__2; ++i__)
      { q2[i__ + *n * q2_dim1] = 1.f;
            q2[i__ + (*n + 1) * q2_dim1] = pw[i__-1];
        }
   vector<float> fit_res;
   fit_res=Util::lsfit(k, n, &klm2d, iswi, &q2[q2_offset], &q[q_offset], &x[1], &res[1], &cu[3], &s[1], &iu[3]);
   return fit_res;
}
Dict Util::CANG ( float  PHI,
float  THETA,
float  PSI 
) [static]

Definition at line 5882 of file util_sparx.cpp.

References DGR_TO_RAD, DM, and SS.

{
        double CPHI,SPHI,CTHE,STHE,CPSI,SPSI;
        vector<float>   DM,SS;

        for(int i =0;i<9;i++) DM.push_back(0);

        for(int i =0;i<6;i++) SS.push_back(0);

        CPHI = cos(double(PHI)*DGR_TO_RAD);
        SPHI = sin(double(PHI)*DGR_TO_RAD);
        CTHE = cos(double(THETA)*DGR_TO_RAD);
        STHE = sin(double(THETA)*DGR_TO_RAD);
        CPSI = cos(double(PSI)*DGR_TO_RAD);
        SPSI = sin(double(PSI)*DGR_TO_RAD);

        SS(1) = float(CPHI);
        SS(2) = float(SPHI);
        SS(3) = float(CTHE);
        SS(4) = float(STHE);
        SS(5) = float(CPSI);
        SS(6) = float(SPSI);

        DM(1) = float(CPHI*CTHE*CPSI-SPHI*SPSI);
        DM(2) = float(SPHI*CTHE*CPSI+CPHI*SPSI);
        DM(3) = float(-STHE*CPSI);
        DM(4) = float(-CPHI*CTHE*SPSI-SPHI*CPSI);
        DM(5) = float(-SPHI*CTHE*SPSI+CPHI*CPSI);
        DM(6) = float(STHE*SPSI);
        DM(7) = float(STHE*CPHI);
        DM(8) = float(STHE*SPHI);
        DM(9) = float(CTHE);

        Dict DMnSS;
        DMnSS["DM"] = DM;
        DMnSS["SS"] = SS;

        return(DMnSS);
}
float Util::ccc_images ( EMData image,
EMData refim,
EMData mask,
float  ang,
float  sx,
float  sy 
) [static]

Definition at line 20795 of file util_sparx.cpp.

References EMAN::EMData::cmp(), and EMAN::EMData::rot_scale_trans2D().

Referenced by twoD_fine_ali_SD().

                                                                                                {

        EMData *rot= new EMData();
        float ccc;

        rot = image->rot_scale_trans2D(ang, sx, sy, 1.0);
        ccc = -rot->cmp("sqeuclidean", refim, Dict("mask", mask));
        delete rot;
        return ccc;
}
float Util::ccc_images_G ( EMData image,
EMData refim,
EMData mask,
Util::KaiserBessel kb,
float  ang,
float  sx,
float  sy 
) [static]

Definition at line 20832 of file util_sparx.cpp.

References EMAN::EMData::cmp(), pi, and EMAN::EMData::rot_scale_conv7().

Referenced by twoD_fine_ali_SD_G().

                                                                                                                        {

        EMData *rot= new EMData();
        float ccc;

        rot = image->rot_scale_conv7(static_cast<float>(ang*pi/180.0), sx, sy, kb, 1.0f);
        ccc = -rot->cmp("sqeuclidean", refim, Dict("mask", mask));
        delete rot;
        return ccc;
}
string Util::change_filename_ext ( const string &  old_filename,
const string &  new_ext 
) [static]

Change a file's extension and return the new filename.

If the given new extension is empty, the old filename is not changed. If the old filename has no extension, add the new extension to it.

Parameters:
[in]old_filenameOld filename.
[in]new_extThe new extension. It shouldn't have ".". e.g., for MRC file, it will be "mrc", not ".mrc".
Returns:
The new filename with the new extension.

Definition at line 464 of file util.cpp.

References Assert.

Referenced by EMAN::EMUtil::get_image_type(), EMAN::ImagicIO::ImagicIO(), and EMAN::ImagicIO2::ImagicIO2().

{
        Assert(old_filename != "");
        if (ext == "") {
                return old_filename;
        }

        string filename = old_filename;
        size_t dot_pos = filename.rfind(".");
        if (dot_pos != string::npos) {
                filename = filename.substr(0, dot_pos+1);
        }
        else {
                filename = filename + ".";
        }
        filename = filename + ext;
        return filename;
}
bool Util::check_file_by_magic ( const void *  first_block,
const char *  magic 
) [static]

check whether a file starts with certain magic string.

Parameters:
first_blockThe first block of the file.
magicThe magic string to identify a file format.
Returns:
True if file matches magic. Otherwise, false.

Definition at line 228 of file util.cpp.

References NullPointerException.

Referenced by EMAN::VtkIO::is_valid(), EMAN::SalIO::is_valid(), EMAN::PgmIO::is_valid(), EMAN::LstIO::is_valid(), EMAN::LstFastIO::is_valid(), and EMAN::AmiraIO::is_valid().

{
        if (!first_block || !magic) {
                throw NullPointerException("first_block/magic");
        }

        const char *buf = static_cast < const char *>(first_block);

        if (strncmp(buf, magic, strlen(magic)) == 0) {
                return true;
        }
        return false;
}
void Util::cl1 ( long int *  k,
long int *  l,
long int *  m,
long int *  n,
long int *  klm2d,
double *  q,
double *  x,
double *  res,
double *  cu,
long int *  iu,
double *  s 
) [static]

Definition at line 6832 of file util_sparx.cpp.

References abs, in, and x.

Referenced by lsfit().

{

    long int q_dim1, q_offset, i__1, i__2;
    double d__1;

    static long int i__, j;
    static double z__;
    static long int n1, n2, ia, ii, kk, in, nk, js;
    static double sn, zu, zv;
    static long int nk1, klm, nkl, jmn, jpn;
    static double cuv;
    static long int klm1, nkl1, klm2, kode, iimn, nklm, iter;
    static float xmin;
    static double xmax;
    static long int iout;
    static double xsum;
    static long int iineg, maxit;
    static double toler;
    static float error;
    static double pivot;
    static long int kforce, iphase;
    static double tpivot;

    --s;
    --res;
    iu -= 3;
    cu -= 3;
    --x;
    q_dim1 = *klm2d;
    q_offset = 1 + q_dim1;
    q -= q_offset;

    /* Function Body */
    maxit = 500;
    kode = 0;
    toler = 1e-4f;
    iter = 0;
    n1 = *n + 1;
    n2 = *n + 2;
    nk = *n + *k;
    nk1 = nk + 1;
    nkl = nk + *l;
    nkl1 = nkl + 1;
    klm = *k + *l + *m;
    klm1 = klm + 1;
    klm2 = klm + 2;
    nklm = *n + klm;
    kforce = 1;
    iter = 0;
    js = 1;
    ia = 0;
/* SET UP LABELS IN Q. */
    i__1 = *n;
    for (j = 1; j <= i__1; ++j) {
        q[klm2 + j * q_dim1] = (double) j;
/* L10: */
    }
    i__1 = klm;
    for (i__ = 1; i__ <= i__1; ++i__) {
        q[i__ + n2 * q_dim1] = (double) (*n + i__);
        if (q[i__ + n1 * q_dim1] >= 0.f) {
            goto L30;
        }
        i__2 = n2;
        for (j = 1; j <= i__2; ++j) {
            q[i__ + j * q_dim1] = -q[i__ + j * q_dim1];
/* L20: */
        }
L30:
        ;
    }
/* SET UP PHASE 1 COSTS. */
    iphase = 2;
    i__1 = nklm;
    for (j = 1; j <= i__1; ++j) {
        cu[(j << 1) + 1] = 0.f;
        cu[(j << 1) + 2] = 0.f;
        iu[(j << 1) + 1] = 0;
        iu[(j << 1) + 2] = 0;
/* L40: */
    }
    if (*l == 0) {
        goto L60;
    }
    i__1 = nkl;
    for (j = nk1; j <= i__1; ++j) {
        cu[(j << 1) + 1] = 1.f;
        cu[(j << 1) + 2] = 1.f;
        iu[(j << 1) + 1] = 1;
        iu[(j << 1) + 2] = 1;
/* L50: */
    }
    iphase = 1;
L60:
    if (*m == 0) {
        goto L80;
    }
    i__1 = nklm;
    for (j = nkl1; j <= i__1; ++j) {
        cu[(j << 1) + 2] = 1.f;
        iu[(j << 1) + 2] = 1;
        jmn = j - *n;
        if (q[jmn + n2 * q_dim1] < 0.f) {
            iphase = 1;
        }
/* L70: */
    }
L80:
    if (kode == 0) {
        goto L150;
    }
    i__1 = *n;
    for (j = 1; j <= i__1; ++j) {
        if ((d__1 = x[j]) < 0.) {
            goto L90;
        } else if (d__1 == 0) {
            goto L110;
        } else {
            goto L100;
        }
L90:
        cu[(j << 1) + 1] = 1.f;
        iu[(j << 1) + 1] = 1;
        goto L110;
L100:
        cu[(j << 1) + 2] = 1.f;
        iu[(j << 1) + 2] = 1;
L110:
        ;
    }
    i__1 = *k;
    for (j = 1; j <= i__1; ++j) {
        jpn = j + *n;
        if ((d__1 = res[j]) < 0.) {
            goto L120;
        } else if (d__1 == 0) {
            goto L140;
        } else {
            goto L130;
        }
L120:
        cu[(jpn << 1) + 1] = 1.f;
        iu[(jpn << 1) + 1] = 1;
        if (q[j + n2 * q_dim1] > 0.f) {
            iphase = 1;
        }
        goto L140;
L130:
        cu[(jpn << 1) + 2] = 1.f;
        iu[(jpn << 1) + 2] = 1;
        if (q[j + n2 * q_dim1] < 0.f) {
            iphase = 1;
        }
L140:
        ;
    }
L150:
    if (iphase == 2) {
        goto L500;
    }
/* COMPUTE THE MARGINAL COSTS. */
L160:
    i__1 = n1;
    for (j = js; j <= i__1; ++j) {
        xsum = 0.;
        i__2 = klm;
        for (i__ = 1; i__ <= i__2; ++i__) {
            ii = (long int) q[i__ + n2 * q_dim1];
            if (ii < 0) {
                goto L170;
            }
            z__ = cu[(ii << 1) + 1];
            goto L180;
L170:
            iineg = -ii;
            z__ = cu[(iineg << 1) + 2];
L180:
            xsum += q[i__ + j * q_dim1] * z__;
/*  180       XSUM = XSUM + Q(I,J)*Z */
/* L190: */
        }
        q[klm1 + j * q_dim1] = xsum;
/* L200: */
    }
    i__1 = *n;
    for (j = js; j <= i__1; ++j) {
        ii = (long int) q[klm2 + j * q_dim1];
        if (ii < 0) {
            goto L210;
        }
        z__ = cu[(ii << 1) + 1];
        goto L220;
L210:
        iineg = -ii;
        z__ = cu[(iineg << 1) + 2];
L220:
        q[klm1 + j * q_dim1] -= z__;
/* L230: */
    }
/* DETERMINE THE VECTOR TO ENTER THE BASIS. */
L240:
    xmax = 0.f;
    if (js > *n) {
        goto L490;
    }
    i__1 = *n;
    for (j = js; j <= i__1; ++j) {
        zu = q[klm1 + j * q_dim1];
        ii = (long int) q[klm2 + j * q_dim1];
        if (ii > 0) {
            goto L250;
        }
        ii = -ii;
        zv = zu;
        zu = -zu - cu[(ii << 1) + 1] - cu[(ii << 1) + 2];
        goto L260;
L250:
        zv = -zu - cu[(ii << 1) + 1] - cu[(ii << 1) + 2];
L260:
        if (kforce == 1 && ii > *n) {
            goto L280;
        }
        if (iu[(ii << 1) + 1] == 1) {
            goto L270;
        }
        if (zu <= xmax) {
            goto L270;
        }
        xmax = zu;
        in = j;
L270:
        if (iu[(ii << 1) + 2] == 1) {
            goto L280;
        }
        if (zv <= xmax) {
            goto L280;
        }
        xmax = zv;
        in = j;
L280:
        ;
    }
    if (xmax <= toler) {
        goto L490;
    }
    if (q[klm1 + in * q_dim1] == xmax) {
        goto L300;
    }
    i__1 = klm2;
    for (i__ = 1; i__ <= i__1; ++i__) {
        q[i__ + in * q_dim1] = -q[i__ + in * q_dim1];
/* L290: */
    }
    q[klm1 + in * q_dim1] = xmax;
/* DETERMINE THE VECTOR TO LEAVE THE BASIS. */
L300:
    if (iphase == 1 || ia == 0) {
        goto L330;
    }
    xmax = 0.f;
    i__1 = ia;
    for (i__ = 1; i__ <= i__1; ++i__) {
        z__ = (d__1 = q[i__ + in * q_dim1], abs(d__1));
        if (z__ <= xmax) {
            goto L310;
        }
        xmax = z__;
        iout = i__;
L310:
        ;
    }
    if (xmax <= toler) {
        goto L330;
    }
    i__1 = n2;
    for (j = 1; j <= i__1; ++j) {
        z__ = q[ia + j * q_dim1];
        q[ia + j * q_dim1] = q[iout + j * q_dim1];
        q[iout + j * q_dim1] = z__;
/* L320: */
    }
    iout = ia;
    --ia;
    pivot = q[iout + in * q_dim1];
    goto L420;
L330:
    kk = 0;
    i__1 = klm;
    for (i__ = 1; i__ <= i__1; ++i__) {
        z__ = q[i__ + in * q_dim1];
        if (z__ <= toler) {
            goto L340;
        }
        ++kk;
        res[kk] = q[i__ + n1 * q_dim1] / z__;
        s[kk] = (double) i__;
L340:
        ;
    }
L350:
    if (kk > 0) {
        goto L360;
    }
    kode = 2;
    goto L590;
L360:
    xmin = static_cast<float>( res[1] );
    iout = (long int) s[1];
    j = 1;
    if (kk == 1) {
        goto L380;
    }
    i__1 = kk;
    for (i__ = 2; i__ <= i__1; ++i__) {
        if (res[i__] >= xmin) {
            goto L370;
        }
        j = i__;
        xmin = static_cast<float>( res[i__] );
        iout = (long int) s[i__];
L370:
        ;
    }
    res[j] = res[kk];
    s[j] = s[kk];
L380:
    --kk;
    pivot = q[iout + in * q_dim1];
    ii = (long int) q[iout + n2 * q_dim1];
    if (iphase == 1) {
        goto L400;
    }
    if (ii < 0) {
        goto L390;
    }
    if (iu[(ii << 1) + 2] == 1) {
        goto L420;
    }
    goto L400;
L390:
    iineg = -ii;
    if (iu[(iineg << 1) + 1] == 1) {
        goto L420;
    }
/* 400 II = IABS(II) */
L400:
    ii = abs(ii);
    cuv = cu[(ii << 1) + 1] + cu[(ii << 1) + 2];
    if (q[klm1 + in * q_dim1] - pivot * cuv <= toler) {
        goto L420;
    }
/* BYPASS INTERMEDIATE VERTICES. */
    i__1 = n1;
    for (j = js; j <= i__1; ++j) {
        z__ = q[iout + j * q_dim1];
        q[klm1 + j * q_dim1] -= z__ * cuv;
        q[iout + j * q_dim1] = -z__;
/* L410: */
    }
    q[iout + n2 * q_dim1] = -q[iout + n2 * q_dim1];
    goto L350;
/* GAUSS-JORDAN ELIMINATION. */
L420:
    if (iter < maxit) {
        goto L430;
    }
    kode = 3;
    goto L590;
L430:
    ++iter;
    i__1 = n1;
    for (j = js; j <= i__1; ++j) {
        if (j != in) {
            q[iout + j * q_dim1] /= pivot;
        }
/* L440: */
    }
/* IF PERMITTED, USE SUBROUTINE COL OF THE DESCRIPTION */
/* SECTION AND REPLACE THE FOLLOWING SEVEN STATEMENTS DOWN */
/* TO AND INCLUDING STATEMENT NUMBER 460 BY.. */
/*     DO 460 J=JS,N1 */
/*        IF(J .EQ. IN) GO TO 460 */
/*        Z = -Q(IOUT,J) */
/*        CALL COL(Q(1,J), Q(1,IN), Z, IOUT, KLM1) */
/* 460 CONTINUE */
    i__1 = n1;
    for (j = js; j <= i__1; ++j) {
        if (j == in) {
            goto L460;
        }
        z__ = -q[iout + j * q_dim1];
        i__2 = klm1;
        for (i__ = 1; i__ <= i__2; ++i__) {
            if (i__ != iout) {
                q[i__ + j * q_dim1] += z__ * q[i__ + in * q_dim1];
            }
/* L450: */
        }
L460:
        ;
    }
    tpivot = -pivot;
    i__1 = klm1;
    for (i__ = 1; i__ <= i__1; ++i__) {
        if (i__ != iout) {
            q[i__ + in * q_dim1] /= tpivot;
        }
/* L470: */
    }
    q[iout + in * q_dim1] = 1.f / pivot;
    z__ = q[iout + n2 * q_dim1];
    q[iout + n2 * q_dim1] = q[klm2 + in * q_dim1];
    q[klm2 + in * q_dim1] = z__;
    ii = (long int) abs(z__);
    if (iu[(ii << 1) + 1] == 0 || iu[(ii << 1) + 2] == 0) {
        goto L240;
    }
    i__1 = klm2;
    for (i__ = 1; i__ <= i__1; ++i__) {
        z__ = q[i__ + in * q_dim1];
        q[i__ + in * q_dim1] = q[i__ + js * q_dim1];
        q[i__ + js * q_dim1] = z__;
/* L480: */
    }
    ++js;
    goto L240;
/* TEST FOR OPTIMALITY. */
L490:
    if (kforce == 0) {
        goto L580;
    }
    if (iphase == 1 && q[klm1 + n1 * q_dim1] <= toler) {
        goto L500;
    }
    kforce = 0;
    goto L240;
/* SET UP PHASE 2 COSTS. */
L500:
    iphase = 2;
    i__1 = nklm;
    for (j = 1; j <= i__1; ++j) {
        cu[(j << 1) + 1] = 0.f;
        cu[(j << 1) + 2] = 0.f;
/* L510: */
    }
    i__1 = nk;
    for (j = n1; j <= i__1; ++j) {
        cu[(j << 1) + 1] = 1.f;
        cu[(j << 1) + 2] = 1.f;
/* L520: */
    }
    i__1 = klm;
    for (i__ = 1; i__ <= i__1; ++i__) {
        ii = (long int) q[i__ + n2 * q_dim1];
        if (ii > 0) {
            goto L530;
        }
        ii = -ii;
        if (iu[(ii << 1) + 2] == 0) {
            goto L560;
        }
        cu[(ii << 1) + 2] = 0.f;
        goto L540;
L530:
        if (iu[(ii << 1) + 1] == 0) {
            goto L560;
        }
        cu[(ii << 1) + 1] = 0.f;
L540:
        ++ia;
        i__2 = n2;
        for (j = 1; j <= i__2; ++j) {
            z__ = q[ia + j * q_dim1];
            q[ia + j * q_dim1] = q[i__ + j * q_dim1];
            q[i__ + j * q_dim1] = z__;
/* L550: */
        }
L560:
        ;
    }
    goto L160;
L570:
    if (q[klm1 + n1 * q_dim1] <= toler) {
        goto L500;
    }
    kode = 1;
    goto L590;
L580:
    if (iphase == 1) {
        goto L570;
    }
/* PREPARE OUTPUT. */
    kode = 0;
L590:
    xsum = 0.;
    i__1 = *n;
    for (j = 1; j <= i__1; ++j) {
        x[j] = 0.f;
/* L600: */
    }
    i__1 = klm;
    for (i__ = 1; i__ <= i__1; ++i__) {
        res[i__] = 0.f;
/* L610: */
    }
    i__1 = klm;
    for (i__ = 1; i__ <= i__1; ++i__) {
        ii = (long int) q[i__ + n2 * q_dim1];
        sn = 1.f;
        if (ii > 0) {
            goto L620;
        }
        ii = -ii;
        sn = -1.f;
L620:
        if (ii > *n) {
            goto L630;
        }
        x[ii] = sn * q[i__ + n1 * q_dim1];
        goto L640;
L630:
        iimn = ii - *n;
        res[iimn] = sn * q[i__ + n1 * q_dim1];
        if (ii >= n1 && ii <= nk) {
            xsum += q[i__ + n1 * q_dim1];
        }
L640:
        ;
    }
    error = (float)xsum;
    return;
}
vector< float > Util::cluster_equalsize ( EMData d) [static]

Definition at line 21544 of file util_sparx.cpp.

References dm, EMAN::EMData::get_xsize(), nx, and sqrt().

                                               {
        //  WORKS ONLY FOR NUMBER OF OBJECTS N=l^2   !!
        int nx = d->get_xsize();
        int N = 1 + int((sqrt(1.0 + 8.0*nx)-1.0)/2.0);
        int K = N/2;
        vector<float> group(N);
        if(N*(N-1)/2 != nx) {
                //print  "  incorrect dimension"
                return group;}
        //bool active[N];       //this does not compile in VS2005. --Grant Tang
        bool * active = new bool[N];
        for(int i=0; i<N; i++) active[i] = true;

        float dm, qd;
        int   ppi = 0, ppj = 0;
        for(int k=0; k<K; k++) {
                // find pairs of most similiar objects among active
                //cout<<"  k  "<<k<<endl;
                dm = 1.0e23f;
                for(int i=1; i<N; i++) {
                        if(active[i]) {
                                for(int j=0; j<i; j++) {
                                        if(active[j]) {
                                                qd = (*d)(i*(i - 1)/2 + j);
                                                if(qd < dm) {
                                                        dm = qd;
                                                        ppi = i;
                                                        ppj = j;
                                                }
                                        }
                                }
                        }
                }
                group[2*k] = float(ppi);
                group[1+2*k] = float(ppj);
                active[ppi] = false;
                active[ppj] = false;
        }

        delete [] active;
        active = NULL;
        return  group;
}
vector< float > Util::cluster_pairwise ( EMData d,
int  K,
float  T,
float  F 
) [static]

Definition at line 21336 of file util_sparx.cpp.

References assign, cent, dt, get_irand(), EMAN::EMData::get_xsize(), mono(), nx, q, and sqrt().

                                                                       {
        int nx = d->get_xsize();
        int N = 1 + int((sqrt(1.0 + 8.0*nx)-1.0)/2.0);
        vector<float> out(N+K+2);
        if(N*(N-1)/2 != nx) {
                //print  "  incorrect dimension"
                return out;}
        //  assign random objects as centers
        for(int i=0; i<N; i++) assign(i) = float(i);
        // shuffle
        for(int i=0; i<N; i++) {
                int j = Util::get_irand(0,N-1);
                float temp = assign(i);
                assign(i) = assign(j);
                assign(j) = temp;
        }
        for(int k=0; k<K; k++) cent(k) = float(assign(k));
        //for(int k=0; k<K; k++) cout<<cent(k)<<"    ";cout<<endl;
        //
        for(int i=0; i<N; i++) assign(i) = 0.0f;
        float qm, dispold = 1.1e22f, disp = 1.0e22f, na=0.0f;
        bool change = true;
        int it = -1;
        int ct = -1;
        while ((change && disp < dispold) || ct > 0) {

                change = false;
                dispold = disp;
                it++;

                // dispersion is a sum of distance from objects to object center
                disp = 0.0f;
                ct = 0;
                for(int i=0; i<N; i++) {
                        qm = 1.0e23f;
                        for(int k=0; k<K; k++) {
                                if(float(i) == cent(k)) {
                                        qm = 0.0f;
                                        na = (float)k;
                                } else {
                                        float dt = (*d)(mono(i,int(cent(k))));
                                        if(dt < qm) {
                                                qm = dt;
                                                na = (float)k;
                                        }
                                }
                        }


                        // Simulated annealing
                        if(exp(-1.0/float(T)) > Util::get_irand(1,1000)/1000.0) {
                            na = (float)(Util::get_irand(0, K));
                            qm = (*d)(mono(i,int(na)));
                            ct++;
                        }

                        disp += qm;

                        if(na != assign(i)) {
                                assign(i) = na;
                                change = true;
                        }
                }

                //cout<<"Iteration:  "<<it<< " disp " <<disp << " T " << T << " disturb " << ct << endl;
                T = T*F;

        //for(int k=0; k<N; k++) cout<<assign(k)<<"    ";cout<<endl;
                //print disp
                //print  assign
                // find centers
                for(int k=0; k<K; k++) {
                        qm = 1.0e23f;
                        for(int i=0; i<N; i++) {
                                if(assign(i) == float(k)) {
                                        float q = 0.0;
                                        for(int j=0; j<N; j++) {
                                                if(assign(j) == float(k)) {
                                                                //it cannot be the same object
                                                        if(i != j)  q += (*d)(mono(i,j));
                                                        //cout<<q<<"   "<<i<<"   "<<j<<"   "<<k<<endl;}
                                                }
                                        }
                                        if(q < qm) {
                                                //cout<<qm<<"   "<<q<<"   "<<i<<"   "<<k<<endl;
                                                qm = q;
                                                cent(k) = float(i);
                                        }
                                }
                        }
                }
        //for(int k=0; k<K; k++) cout<<cent(k)<<"    ";cout<<endl;cout<<disp<<endl;
        }
        out[N+K] = disp;
        out[N+K+1] = float(it);
        return  out;
}
double Util::cml_disc ( const vector< EMData * > &  data,
vector< int >  com,
vector< int >  seq,
vector< float >  weights,
int  n_lines 
) [static]

2009-03-30 15:44:05 JB.

Compute the discrepancy belong all common-lines

Definition at line 5152 of file util_sparx.cpp.

                                                                                                                       {
        double res = 0;
        double buf = 0;
        float* line_1;
        float* line_2;
        int i, n, ind;
        int lnlen = data[0]->get_xsize();
        for (n=0; n<n_lines; ++n) {
                ind = n*2;
                line_1 = data[seq[ind]]->get_data() + com[ind] * lnlen;
                line_2 = data[seq[ind+1]]->get_data() + com[ind+1] *lnlen;
                buf = 0;
                for (i=0; i<lnlen; ++i) {
                    buf += (line_1[i]-line_2[i])*(line_1[i]-line_2[i]);
                }
                res += buf * weights[n];
        }

        return res;

}
vector< double > Util::cml_init_rot ( vector< float >  Ori) [static]

2009-03-25 15:35:05 JB.

This function prepare rotation matrix for common-lines

Definition at line 4929 of file util_sparx.cpp.

References deg_rad.

                                                  {
        int nb_ori = Ori.size() / 4;
        int i, ind;
        float ph, th, ps;
        double cph, cth, cps, sph, sth, sps;
        vector<double> Rot(nb_ori*9);
        for (i=0; i<nb_ori; ++i){
                ind = i*4;
                // spider convention phi=psi-90, psi=phi+90
                ph = Ori[ind+2]-90;
                th = Ori[ind+1];
                ps = Ori[ind]+90;
                ph *= deg_rad;
                th *= deg_rad;
                ps *= deg_rad;
                // pre-calculate some trigo stuffs
                cph = cos(ph);
                cth = cos(th);
                cps = cos(ps);
                sph = sin(ph);
                sth = sin(th);
                sps = sin(ps);
                // fill rotation matrix
                ind = i*9;
                Rot[ind] = cph*cps-cth*sps*sph;
                Rot[ind+1] = cph*sps+cth*cps*sph;
                Rot[ind+2] = sth*sph;
                Rot[ind+3] = -sph*cps-cth*sps*cph;
                Rot[ind+4] = -sph*sps+cth*cps*cph;
                Rot[ind+5] = sth*cph;
                Rot[ind+6] = sth*sps;
                Rot[ind+7] = -sth*cps;
                Rot[ind+8] = cth;
        }

        return Rot;
}
vector< double > Util::cml_line_in3d ( vector< float >  Ori,
vector< int >  seq,
int  nprj,
int  nlines 
) [static]

2009-03-26 10:46:14 JB.

This function calculate all common-lines in space for Voronoi

Definition at line 5097 of file util_sparx.cpp.

References deg_rad, norm(), nx, ny, rad_deg, and sqrt().

                                                                                     {
        // seq is the pairwise index ij: 0, 1, 0, 2, 0, 3, 1, 2, 1, 3, 2, 3
        vector<double> cml(2*nlines); // [phi, theta] / line
        float ph1, th1;
        float ph2, th2;
        double nx, ny, nz;
        double norm;
        double sth1=0, sph1=0, cth1=0, cph1=0;
        double sth2, sph2, cth2, cph2;
        int l, ind, c;
        int mem = -1;
        for (l=0; l<nlines; ++l){
                c = 2*l;
                if (seq[c]!=mem){
                        mem = seq[c];
                        ind = 4*seq[c];
                        ph1 = Ori[ind]*deg_rad;
                        th1 = Ori[ind+1]*deg_rad;
                        sth1 = sin(th1);
                        sph1 = sin(ph1);
                        cth1 = cos(th1);
                        cph1 = cos(ph1);
                }
                ind = 4*seq[c+1];
                ph2 = Ori[ind]*deg_rad;
                th2 = Ori[ind+1]*deg_rad;
                sth2 = sin(th2);
                cth2 = cos(th2);
                sph2 = sin(ph2);
                cph2 = cos(ph2);
                // cross product
                nx = sth1*cph1*cth2 - cth1*sth2*cph2;
                ny = cth1*sth2*sph2 - cth2*sth1*sph1;
                nz = sth1*sph1*sth2*cph2 - sth1*cph1*sth2*sph2;
                norm = sqrt(nx*nx+ny*ny+nz*nz);
                nx /= norm;
                ny /= norm;
                nz /= norm;
                // apply mirror if need
                if (nz<0) {nx=-nx; ny=-ny; nz=-nz;}
                // compute theta and phi
                cml[c+1] = acos(nz);
                if (cml[c+1] == 0) {cml[c] = 0;}
                else {
                        cml[c+1] *= rad_deg;
                        if (cml[c+1] > 89.99) {cml[c+1] = 89.99;} // this fix some pb in Voronoi
                        cml[c] = rad_deg * atan2(nx, ny);
                        cml[c] = fmod(360 + cml[c], 360);

                }
        }

        return cml;
}
vector< int > Util::cml_line_insino ( vector< float >  Rot,
int  i_prj,
int  n_prj 
) [static]

2009-03-25 15:35:53 JB.

This function calculates common-lines between sinogram

Definition at line 4998 of file util_sparx.cpp.

References abs, b, nint180(), and rad_deg.

                                                                        {
        vector<int> com(2*(n_prj - 1));
        int a = i_prj*9;
        int i, b, c;
        int n1=0, n2=0;
        float vmax = 1 - 1.0e-6f;
        double r11, r12, r13, r23, r31, r32, r33;

        c = 0;
        for (i=0; i<n_prj; ++i){
                if (i!=i_prj){
                        b = i*9;
                        // this is equivalent to R = A*B'
                        r11 = Rot[a]*Rot[b]+Rot[a+1]*Rot[b+1]+Rot[a+2]*Rot[b+2];
                        r12 = Rot[a]*Rot[b+3]+Rot[a+1]*Rot[b+4]+Rot[a+2]*Rot[b+5];
                        r13 = Rot[a]*Rot[b+6]+Rot[a+1]*Rot[b+7]+Rot[a+2]*Rot[b+8];
                        r23 = Rot[a+3]*Rot[b+6]+Rot[a+4]*Rot[b+7]+Rot[a+5]*Rot[b+8];
                        r31 = Rot[a+6]*Rot[b]+Rot[a+7]*Rot[b+1]+Rot[a+8]*Rot[b+2];
                        r32 = Rot[a+6]*Rot[b+3]+Rot[a+7]*Rot[b+4]+Rot[a+8]*Rot[b+5];
                        r33 = Rot[a+6]*Rot[b+6]+Rot[a+7]*Rot[b+7]+Rot[a+8]*Rot[b+8];
                        if (r33 > vmax) {
                            n2 = 270;
                            n1 = 270 + nint180((float)(rad_deg*atan2(r12, r11)));
                        }
                        else if (r33 < -vmax) {
                            n2 = 270;
                            n1 = 270 - nint180((float)(rad_deg*atan2(r12, r11)));
                        } else {
                            n2 = nint180((float)(rad_deg*atan2(r31, -r32)));
                            n1 = nint180((float)(rad_deg*atan2(r13, r23)));
                            if (n1 < 0) {n1 += 360;}
                            if (n2 <= 0) {n2 = abs(n2);}
                            else {n2 = 360 - n2;}
                        }

                        if (n1 >= 360){n1 = n1 % 360;}
                        if (n2 >= 360){n2 = n2 % 360;}

                        // store common-lines
                        b = c*2;
                        com[b] = n1;
                        com[b+1] = n2;
                        ++c;
                }
        }

    return com;

}
vector< int > Util::cml_line_insino_all ( vector< float >  Rot,
vector< int >  seq,
int  n_prj,
int  n_lines 
) [static]

2009-03-30 15:35:07 JB.

This function calculates all common-lines between sinogram

Definition at line 5048 of file util_sparx.cpp.

References abs, b, nint180(), and rad_deg.

                                                                                          {
        vector<int> com(2*n_lines);
        int a=0, b, c, l;
        int n1=0, n2=0, mem=-1;
        float vmax = 1 - 1.0e-6f;
        double r11, r12, r13, r23, r31, r32, r33;
        c = 0;
        for (l=0; l<n_lines; ++l){
                c = 2*l;
                if (seq[c]!=mem){
                    mem = seq[c];
                    a = seq[c]*9;
                }
                b = seq[c+1]*9;

                // this is equivalent to R = A*B'
                r11 = Rot[a]*Rot[b]+Rot[a+1]*Rot[b+1]+Rot[a+2]*Rot[b+2];
                r12 = Rot[a]*Rot[b+3]+Rot[a+1]*Rot[b+4]+Rot[a+2]*Rot[b+5];
                r13 = Rot[a]*Rot[b+6]+Rot[a+1]*Rot[b+7]+Rot[a+2]*Rot[b+8];
                r23 = Rot[a+3]*Rot[b+6]+Rot[a+4]*Rot[b+7]+Rot[a+5]*Rot[b+8];
                r31 = Rot[a+6]*Rot[b]+Rot[a+7]*Rot[b+1]+Rot[a+8]*Rot[b+2];
                r32 = Rot[a+6]*Rot[b+3]+Rot[a+7]*Rot[b+4]+Rot[a+8]*Rot[b+5];
                r33 = Rot[a+6]*Rot[b+6]+Rot[a+7]*Rot[b+7]+Rot[a+8]*Rot[b+8];
                if (r33 > vmax) {
                    n2 = 270;
                    n1 = 270 + nint180((float)(rad_deg*atan2(r12, r11)));
                }
                else if (r33 < -vmax) {
                    n2 = 270;
                    n1 = 270 - nint180((float)(rad_deg*atan2(r12, r11)));
                } else {
                    n2 = nint180((float)(rad_deg*atan2(r31, -r32)));
                    n1 = nint180((float)(rad_deg*atan2(r13, r23)));
                    if (n1 < 0) {n1 += 360;}
                    if (n2 <= 0) {n2 = abs(n2);}
                    else {n2 = 360 - n2;}
                }
                if (n1 >= 360){n1 = n1 % 360;}
                if (n2 >= 360){n2 = n2 % 360;}

                // store common-lines
                com[c] = n1;
                com[c+1] = n2;
        }

        return com;

}
void Util::cml_prepare_line ( EMData sino,
EMData line,
int  ilf,
int  ihf,
int  pos_line,
int  nblines 
) [static]

This function prepare the line from sinogram by cutting off some frequencies, and creating the mirror part (complexe conjugate of the first part).

Then both lines (mirror and without) are drop to the sinogram. line is in Fourrier space, ilf low frequency, ihf high frequency, nblines number of lines of the half sinogram (the non miror part), sino the sinogram, pos_line the position of the line in the sino.

Definition at line 4911 of file util_sparx.cpp.

References EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), nx, and EMAN::EMData::update().

                                                                                                  {
        int j;
        int nx = sino->get_xsize();
        int i = nx * pos_line;
        float r1, r2;
        float *line_ptr = line->get_data();
        float *sino_ptr = sino->get_data();
        for (j=ilf;j<=ihf; j += 2) {
                r1 = line_ptr[j];
                r2 = line_ptr[j + 1];
                sino_ptr[i + j - ilf] = r1;
                sino_ptr[i + j - ilf + 1] = r2;
                sino_ptr[i + nx * nblines + j - ilf] = r1;
                sino_ptr[i + nx * nblines + j - ilf + 1] = -r2;
        }
        sino->update();
}
vector< double > Util::cml_spin_psi ( const vector< EMData * > &  data,
vector< int >  com,
vector< float >  weights,
int  iprj,
vector< int >  iw,
int  n_psi,
int  d_psi,
int  n_prj 
) [static]

2009-03-26 11:37:53 JB.

This function spin all angle psi and evaluate the partial discrepancy belong common-lines

Definition at line 5174 of file util_sparx.cpp.

                                                                                           {
        // res: [best_disc, best_ipsi]
        // seq: pairwise indexes ij, 0, 1, 0, 2, 0, 3, 1, 2, 1, 3, 2, 3
        // iw : index to know where is the weight for the common-lines on the current projection in the all weights, [12, 4, 2, 7]
        vector<double> res(2);
        int lnlen = data[0]->get_xsize();
        int end = 2*(n_prj-1);
        double disc, buf, bdisc, tmp;
        int n, i, ipsi, ind, bipsi, c;
        float* line_1;
        float* line_2;
        bdisc = 1.0e6;
        bipsi = -1;
        // loop psi
        for(ipsi=0; ipsi<n_psi; ipsi += d_psi) {
                // discrepancy
                disc = 0;
                c = 0;
                for (n=0; n<n_prj; ++n) {
                        if(n!=iprj) {
                                ind = 2*c;
                                line_1 = data[iprj]->get_data() + com[ind] * lnlen;
                                line_2 = data[n]->get_data() + com[ind+1] * lnlen;
                                buf = 0;
                                for (i=0; i<lnlen; ++i) {
                                        tmp = line_1[i]-line_2[i];
                                        buf += tmp*tmp;
                                }
                                disc += buf * weights[iw[c]];
                                ++c;
                        }
                }
                // select the best value
                if (disc <= bdisc) {
                        bdisc = disc;
                        bipsi = ipsi;
                }
                // update common-lines
                for (i=0; i<end; i+=2){
                        com[i] += d_psi;
                        if (com[i] >= n_psi) com[i] = com[i] - n_psi;
                }
        }
        res[0] = bdisc;
        res[1] = float(bipsi);

        return res;
}
vector< double > Util::cml_spin_psi_now ( const vector< EMData * > &  data,
vector< int >  com,
int  iprj,
vector< int >  iw,
int  n_psi,
int  d_psi,
int  n_prj 
) [static]

Definition at line 5224 of file util_sparx.cpp.

                                                                                           {
        // res: [best_disc, best_ipsi]
        // seq: pairwise indexes ij, 0, 1, 0, 2, 0, 3, 1, 2, 1, 3, 2, 3
        // iw : index to know where is the weight for the common-lines on the current projection in the all weights, [12, 4, 2, 7]
        vector<double> res(2);
        int lnlen = data[0]->get_xsize();
        int end = 2*(n_prj-1);
        double disc, buf, bdisc, tmp;
        int n, i, ipsi, ind, bipsi, c;
        float* line_1;
        float* line_2;
        bdisc = 1.0e6;
        bipsi = -1;
        // loop psi
        for(ipsi=0; ipsi<n_psi; ipsi += d_psi) {
                // discrepancy
                disc = 0;
                c = 0;
                for (n=0; n<n_prj; ++n) {
                        if(n!=iprj) {
                                ind = 2*c;
                                line_1 = data[iprj]->get_data() + com[ind] * lnlen;
                                line_2 = data[n]->get_data() + com[ind+1] * lnlen;
                                buf = 0;
                                for (i=0; i<lnlen; ++i) {
                                        tmp = line_1[i]-line_2[i];
                                        buf += tmp*tmp;
                                }
                                disc += buf;
                                ++c;
                        }
                }
                // select the best value
                if (disc <= bdisc) {
                        bdisc = disc;
                        bipsi = ipsi;
                }
                // update common-lines
                for (i=0; i<end; i+=2){
                        com[i] += d_psi;
                        if (com[i] >= n_psi) com[i] = com[i] - n_psi;
                }
        }
        res[0] = bdisc;
        res[1] = float(bipsi);

        return res;
}
vector< float > Util::cml_update_rot ( vector< float >  Rot,
int  iprj,
float  nph,
float  th,
float  nps 
) [static]

2009-03-25 15:35:37 JB.

this function update only one rotation amtrix according a new orientation

Definition at line 4967 of file util_sparx.cpp.

References deg_rad.

                                                                                             {
        float ph, ps;
        double cph, cth, cps, sph, sth, sps;
        int ind = iprj*9;
        // spider convention phi=psi-90, psi=phi+90
        ph = nps-90;
        ps = nph+90;
        ph *= deg_rad;
        th *= deg_rad;
        ps *= deg_rad;
        // pre-calculate some trigo stuffs
        cph = cos(ph);
        cth = cos(th);
        cps = cos(ps);
        sph = sin(ph);
        sth = sin(th);
        sps = sin(ps);
        // fill rotation matrix
        Rot[ind] = (float)(cph*cps-cth*sps*sph);
        Rot[ind+1] = (float)(cph*sps+cth*cps*sph);
        Rot[ind+2] = (float)(sth*sph);
        Rot[ind+3] = (float)(-sph*cps-cth*sps*cph);
        Rot[ind+4] = (float)(-sph*sps+cth*cps*cph);
        Rot[ind+5] = (float)(sth*cph);
        Rot[ind+6] = (float)(sth*sps);
        Rot[ind+7] = (float)(-sth*cps);
        Rot[ind+8] = (float)(cth);

        return Rot;
}
vector< double > Util::cml_weights ( const vector< float > &  cml) [static]

Definition at line 4821 of file util_sparx.cpp.

References Assert, PI2, and vrdg().

                                                        {
        static const int NBIN = 100;
        int nline=cml.size()/2;
        vector<double> weights(nline);

        vector<ori_t> angs(nline);
        for( int i=0; i < nline; ++i ) {
                angs[i].iphi = int( NBIN*cml[2*i] );
                angs[i].itht = int( NBIN*cml[2*i+1] );
                if( angs[i].itht == 180*NBIN ) angs[i].itht = 0;
                angs[i].id = i;
        }

        //std::cout << "# of angs: " << angs.size() << std::endl;

        std::sort( angs.begin(), angs.end(), cmpang() );

        vector<float> newphi;
        vector<float> newtht;
        vector< vector<int> > indices;

        int curt_iphi = -1;
        int curt_itht = -1;
        for(unsigned int i=0 ;i < angs.size(); ++i ) {
                if( angs[i].iphi==curt_iphi && angs[i].itht==curt_itht ) {
                        Assert( indices.size() > 0 );
                        indices.back().push_back(angs[i].id);
                } else {
                        curt_iphi = angs[i].iphi;
                        curt_itht = angs[i].itht;

                        newphi.push_back( float(curt_iphi)/NBIN );
                        newtht.push_back( float(curt_itht)/NBIN );
                        indices.push_back( vector<int>(1,angs[i].id) );
                }
        }

        //std::cout << "# of indpendent ang: " << newphi.size() << std::endl;


        int num_agl = newphi.size();

        if(num_agl>2) {
                vector<double> w=Util::vrdg(newphi, newtht);

                Assert( w.size()==newphi.size() );
                Assert( indices.size()==newphi.size() );

                for(unsigned int i=0; i < newphi.size(); ++i ) {
                    /*
                    std::cout << "phi,tht,w,n: ";
                    std::cout << boost::format( "%10.3f" ) % newphi[i] << " ";
                    std::cout << boost::format( "%10.3f" ) % newtht[i] << " ";
                    std::cout << boost::format( "%8.6f"  ) % w[i] << " ";
                    std::cout << indices[i].size() << "(";
                    */

                    for(unsigned int j=0; j < indices[i].size(); ++j ) {
                            int id = indices[i][j];
                            weights[id] = w[i]/indices[i].size();
                            //std::cout << id << " ";
                    }

                    //std::cout << ")" << std::endl;

                }
        } else {
                cout<<"warning in Util.cml_weights"<<endl;
                double val = PI2/float(nline);
                for(int i=0; i<nline; i++)  weights[i]=val;
        }

        return weights;

}
bool Util::cmp1 ( tmpstruct  tmp1,
tmpstruct  tmp2 
) [static]

Definition at line 7503 of file util_sparx.cpp.

References EMAN::Util::tmpstruct::theta1.

Referenced by hsortd().

{
        return(tmp1.theta1 < tmp2.theta1);
}
bool Util::cmp2 ( tmpstruct  tmp1,
tmpstruct  tmp2 
) [static]

Definition at line 7508 of file util_sparx.cpp.

References EMAN::Util::tmpstruct::key1.

Referenced by hsortd().

{
        return(tmp1.key1 < tmp2.key1);
}
void Util::colreverse ( float *  beg,
float *  end,
int  nx 
) [static]

Definition at line 5632 of file util_sparx.cpp.

References nx.

Referenced by cyclicshift(), and slicereverse().

                                                    {
        float* tmp = new float[nx];
        int n = (end - beg)/nx;
        int nhalf = n/2;
        for (int i = 0; i < nhalf; i++) {
                // swap col i and col n-1-i
                memcpy(tmp, beg+i*nx, nx*sizeof(float));
                memcpy(beg+i*nx, beg+(n-1-i)*nx, nx*sizeof(float));
                memcpy(beg+(n-1-i)*nx, tmp, nx*sizeof(float));
        }
        delete[] tmp;
}
EMData * Util::compress_image_mask ( EMData image,
EMData mask 
) [static]

Definition at line 6386 of file util_sparx.cpp.

References EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageDimensionException, img_ptr, new_ptr, nx, ny, and EMAN::EMData::set_size().

Referenced by EMAN::PCA::dopca(), and EMAN::PCA::dopca_lan().

{
        /***********
        ***get the size of the image for validation purpose
        **************/
        int nx = image->get_xsize();
        int ny = image->get_ysize();
        int nz = image->get_zsize();
        /********
        ***Exception Handle
        *************/
        if(nx != mask->get_xsize() || ny != mask->get_ysize() || nz != mask->get_zsize())
                throw ImageDimensionException("The dimension of the image does not match the dimension of the mask!");

        size_t i, size = (size_t)nx*ny*nz;

        float* img_ptr = image->get_data();
        float* mask_ptr = mask->get_data();

        int ln=0;  //length of the output image = number of points under the mask.
        for(i = 0;i < size;i++) if(mask_ptr[i] > 0.5f) ln++;

        EMData* new_image = new EMData();
        new_image->set_size(ln,1,1); /* set size of the new image */
        float *new_ptr    = new_image->get_data();

        ln=-1;
        for(i = 0;i < size;i++){
                if(mask_ptr[i] > 0.5f) {
                        ln++;
                        new_ptr[ln]=img_ptr[i];
                }
        }

        return new_image;
}
void Util::constrained_helix_exhaustive ( vector< EMData * >  data,
vector< EMData * >  fdata,
vector< EMData * >  refproj,
vector< EMData * >  rotproj,
vector< float >  dp_dphi_rise_delta,
vector< int >  nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc,
bool  FindPsi,
float  psi_max,
vector< EMData * >  crefim,
vector< int >  numr,
int  maxrin,
string  mode,
int  cnx,
int  cny 
) [static]

Definition at line 22811 of file util_sparx.cpp.

References abs, ang_n(), EMAN::CIRCULANT, compose_transform2(), EMAN::correlation(), Crosrng_msg_s(), Frngs(), get_transform_params(), max, min, numr, Polar2Dm(), round(), sqrt(), t, tot, and window().

{
        if (dp_dphi_rise_delta.size() < 4) {
                printf("Not enough parameters (dp_dphi_rise_delta)");
                return;
        }
        if (nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc.size() < 9) {
                printf("Not enough parameters (nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc)");
                return;
        }
        float dpsi;
        //float dp       = dp_dphi_rise_delta[0];
        float dphi        = dp_dphi_rise_delta[1];
        float rise        = dp_dphi_rise_delta[2];  // Float in pixels!
        float delta       = dp_dphi_rise_delta[3];
        float ywobble     = dp_dphi_rise_delta[4];
        float ystep       = dp_dphi_rise_delta[5];
        int   nphi        = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[0];
        int   symrestrict = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[1];
        int   phiwobble   = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[2];
        int   range       = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[3];
        bool  Dsym        = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[4];
        int   nwx         = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[5];
        int   nwy         = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[6];
        int   nwxc        = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[7];
        int   nwyc        = nphi_phiwobble_range_ywobble_Dsym_nwx_nwy_nwxc_nwyc[8];

        const int ndata = data.size();

        std::vector< boost::shared_ptr<EMData> > objectsToDelete; // objects added to this vector are automatically deleted at the end of this function

        vector<float> c0 = data[0]->get_attr("ptcl_source_coord");
        vector< vector<EMData*> > ccfs(ndata, vector<EMData*>(nphi));
        vector< vector<EMData*> > ccfr(0);
        if (! Dsym)  ccfr.resize(ndata, vector<EMData*>(nphi));

        for (int im = 0; im < ndata; ++im) {
                for (int iphi = 0; iphi < nphi; ++iphi) {
                        std::auto_ptr<EMData> corr( correlation( refproj[iphi], fdata[im], CIRCULANT, true) );
                        ccfs[im][iphi] = Util::window( corr.get(), nwx, nwy);
                        objectsToDelete.push_back(boost::shared_ptr<EMData>(ccfs[im][iphi]));
                        if (! Dsym) {
                                std::auto_ptr<EMData> corr2( correlation( rotproj[iphi], fdata[im], CIRCULANT, true) );
                                ccfr[im][iphi] = Util::window( corr2.get(), nwx, nwy);
                                objectsToDelete.push_back(boost::shared_ptr<EMData>(ccfr[im][iphi]));
                        }
                }
        }

        vector<float> dxshiftlocal(ndata, 0);
        vector<float> dyshiftlocal(ndata, 0);
        vector<float> dphilocal(ndata, 0);

        vector<float> xshiftlocal(ndata, 0);
        vector<float> xrshiftlocal(ndata, 0);
        vector<float> mxshiftlocal(ndata, 0);
        vector<float> yshiftlocal(ndata, 0);
        vector<float> yrshiftlocal(ndata, 0);
        vector<float> myshiftlocal(ndata, 0);
        vector<float> philocal(ndata, 0);
        vector<float> phirlocal(ndata, 0);
        vector<float> mphilocal(ndata, 0);
        float dirma = -1.0e23f;
        for (int idir = -1; idir < 2; idir += 2) {
                float tmax = -1.0e23f;
                float mpsi;
                for (int ix = 1; ix < nwx-1; ++ix) {                                         //#  X shift
                        //#cout << "im: ", len(ccfs), ix,time()-start_time
                        int six = ix - nwxc;
                        float siy = -rise/2.0f - ystep + ywobble;
                        //for (int iy = 1+ywobble; iy < nwy-ywobble-1; ++iy)
                        while( siy < rise/2.0 - ystep - ywobble ) {                     //#  Y shift
                                siy += ystep;
                                yshiftlocal[0]  = siy;
                                yrshiftlocal[0] = siy;

                                float yiy = siy + nwyc;
                                int   fiy = int(yiy);
                                float ydif = yiy - fiy;
                                float yrem = 1.0 - ydif;
                                int   iuy = fiy;

                                float xrem = 1.0f;
                                float xdif = 0.0f;


                                for (int iphi = 0; iphi < nphi/symrestrict; ++iphi) {               //#  phi search, first segment has phi range restricted by point-group symmetry
                                        float qphi = iphi*delta;
                                        philocal[0]  = qphi;
                                        qphi = 180.0f - qphi;
                                        if(  qphi > 0.0f )     phirlocal[0] = fmod( qphi , 360.0f );
                                        else if( qphi < 0.0f ) phirlocal[0] = 360.0f + fmod( qphi , 360.0f );
                                        //# we use the first segment as a reference, so there is no interpolation, just copy the correlation
                                        //#  Select largest correlation within +/- range pixels from the location we explore
                                        float mxm = -1.0e23f;
                                        float mxr;
                                        for (int iux = max(1, ix - range); iux < min(nwx - 1, ix+range+1); ++iux) {    //#  X wobble
                                                //float qcf = ccfs[0][iphi]->get_value_at(iux,iy);
                                                float qcf =   xrem*yrem*ccfs[0][iphi]->get_value_at(iux,iuy)
                                                                        + xdif*yrem*ccfs[0][iphi]->get_value_at(iux+1,iuy)
                                                                        + xrem*ydif*ccfs[0][iphi]->get_value_at(iux,iuy+1)
                                                                        + xdif*ydif*ccfs[0][iphi]->get_value_at(iux+1,iuy+1);
                                                if (qcf > mxm) {
                                                        mxm = qcf;
                                                        xshiftlocal[0] = float(iux-nwxc);
                                                }
                                        }
                                        if (! Dsym) {
                                                mxr = -1.0e23f;
                                                for (int iux = max(1, ix - range); iux < min(nwx - 1, ix+range+1); ++iux) {     //# Xr wobble
                                                        //float qcf = ccfr[0][iphi]->get_value_at(iux,iy);
                                                        float qcf =   xrem*yrem*ccfr[0][iphi]->get_value_at(iux,iuy)
                                                                                + xdif*yrem*ccfr[0][iphi]->get_value_at(iux+1,iuy)
                                                                                + xrem*ydif*ccfr[0][iphi]->get_value_at(iux,iuy+1)
                                                                                + xdif*ydif*ccfr[0][iphi]->get_value_at(iux+1,iuy+1);
                                                        if (qcf > mxr) {
                                                                mxr = qcf;
                                                                xrshiftlocal[0] = float(iux-nwxc);
                                                        }
                                                }
                                        }
                                        //cout << "  QST "<<iphi<< "  "<<mxm<< "  "<<mxr<<endl;
                                        for ( int im = 1; im < ndata; ++im) {                                       //#  predicted locations
                                                //# dst is distance between segment 0 and current segment in pixels
                                                vector<float> cim = data[im]->get_attr("ptcl_source_coord");
                                                float dst = sqrt( (c0[0] - cim[0])*(c0[0] - cim[0]) + (c0[1] - cim[1])*(c0[1] - cim[1]));
                                                //# predict for all remaining segments assuming number 0
                                                float qd  = round((siy + dst)/rise);
                                                float piy = siy + dst - rise*qd;                                    //#  predicted y shift
                                                //cout<<siy<<"  "<<dst<<"  "<<rise<<"  "<<qd<<"  "<<piy<<"  "<<siy<<"  "<<siy<<"  "<<endl;
                                                float pphi = (philocal[0] + idir*dphi*qd);
                                                if(pphi >= 360.0f)     pphi = fmod( pphi, 360.0f);
                                                else if( pphi < 0.0f)  pphi = 360.0f + fmod( pphi, 360.0f);         //#  predicted phi with full angular accuracy, not an integer
                                                int pix = six;                                                      //# predicted x shift

                                                int xix = pix + nwxc;
                                                //#  Local x search
                                                int fix = int(xix);
                                                float xdif = xix - fix;
                                                float xrem = 1.0f - xdif;
                                                float ciq = -1.0e23f;
                                                //# interpolate correlation at pphi
                                                int ttphi = (int( pphi/delta + 0.5))%nphi;
                                                for (int lphi = -phiwobble; lphi < phiwobble+1; ++lphi) {                                               //#  phi wobble
                                                        int tphi = (ttphi+lphi+nphi)%nphi;
                                                        for (int iux = max(1, fix - range); iux < min(nwx - 1, fix+range+1); ++iux) {                       //#  X wobble
                                                                //for (int iuy = max(1, fiy - ywobble); iuy < min(nwy - 1, fiy+ywobble+1); ++iuy) {               //#  Y wobble
                                                                float tiy = -ywobble - ystep + piy + nwyc;
                                                                while( tiy <= ywobble - ystep + piy + nwyc )  {
                                                                        tiy += ystep;
                                                                        int iuy = int(tiy);
                                                                        if(iuy > -1 && iuy < nwy-1)  {
                                                                        
                                                                                float ydif = yiy - fiy;
                                                                                float yrem = 1.0f - ydif;


                                                                                float qcf =   xrem*yrem*ccfs[im][tphi]->get_value_at(iux,iuy)
                                                                                                        + xdif*yrem*ccfs[im][tphi]->get_value_at(iux+1,iuy)
                                                                                                        + xrem*ydif*ccfs[im][tphi]->get_value_at(iux,iuy+1)
                                                                                                        + xdif*ydif*ccfs[im][tphi]->get_value_at(iux+1,iuy+1);
                                //cout << lphi<< "  "<< tphi<<  "  "<<iux<< "  "<<iuy<< "  "<<fiy<< "  "<<ywobble<< "  "<<nwy<< "  "<<qcf<<endl;
                                                                                if (qcf > ciq) {
                                                                                        ciq = qcf;
                                                                                        xshiftlocal[im] = iux + xdif - nwxc;
                                                                                        yshiftlocal[im] = tiy - nwyc;
                                                                                        philocal[im]    = tphi * delta;
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                }
                                                //cout << "  St "<<iphi<< "  "<<mxm<< "  "<<ciq<< "  "<<mxm+ciq<< "  "<<endl;
                                                //cout << im<< "  "<< ttphi<<  "  "<<phiwobble<< "  "<<ywobble<< "  "<<fiy<<endl;
                                                mxm += ciq;
                                                //# now for rotated
                                                if (! Dsym) {
                                                        pphi = (phirlocal[0] + idir*dphi*qd);                               //#  predicted phi for rotated 180 defs with full angular accuracy, not an integer
                                                        if(pphi >= 360.0f)  pphi = fmod( pphi, 360.0f);
                                                        else if( pphi < 0.0f)  pphi = 360.0f + fmod( pphi, 360.0f);         //#  predicted phi with full angular accuracy, not an integer
                                                        ciq = -1.0e23f;
                                                        //# interpolate correlation at pphi
                                                        for (int lphi = -phiwobble; lphi < phiwobble+1; ++lphi) {                                           //#  phi wobble
                                                                qphi = lphi*delta;
                                                                float qtphi = fmod( pphi + qphi + (int(fabs(qphi/360.0f))+1)*360.0f , 360.0f);
                                                                qphi = fmod(540.0f-qtphi, 360.0f) / delta;
                                                                int tphi = (int( qphi + (int(fabs(qphi/nphi))+1)*nphi  + 0.5))%nphi;
                                                                for (int iux = max(1, fix - range); iux < min(nwx - 1, fix+range+1); ++iux) {                   //#  X wobble
                                                                        //for (int iuy = max(1, fiy - ywobble); iuy < min(nwy - 1, fiy+ywobble+1); ++iuy) {           //#  Y wobble

                                                                        float tiy = -ywobble - ystep + piy + nwyc;
                                                                        while( tiy <= ywobble - ystep + piy + nwyc )  {
                                                                                tiy += ystep;
                                                                                int iuy = int(tiy);
                                                                                if(iuy > -1 && iuy < nwy-1)  {
                                                                
                                                                                        float ydif = yiy - fiy;
                                                                                        float yrem = 1.0f - ydif;



                                                                                        float qcf = xrem*yrem*ccfr[im][tphi]->get_value_at(iux,iuy)
                                                                                                                + xdif*yrem*ccfr[im][tphi]->get_value_at(iux+1,iuy)
                                                                                                                + xrem*ydif*ccfr[im][tphi]->get_value_at(iux,iuy+1)
                                                                                                                + xdif*ydif*ccfr[im][tphi]->get_value_at(iux+1,iuy+1);
                                                                                        if (qcf > ciq) {
                                                                                                ciq = qcf;
                                                                                                xrshiftlocal[im] = iux + xdif - nwxc;
                                                                                                yrshiftlocal[im] = tiy - nwyc;
                                                                                                phirlocal[im]    = int(qtphi/delta+0.5f)*delta;
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                        mxr += ciq;
                                                } else {
                                                        mxr = mxm-1.e5;
                                                }
                                        }
                                        if ( mxr > mxm ) {
                                                if (mxr > tmax) {
                                                        tmax = mxr;
                                                        mpsi = 270.0f;
                                                        for (int im = 0; im < ndata; ++im) mxshiftlocal[im] = xrshiftlocal[im];
                                                        for (int im = 0; im < ndata; ++im) myshiftlocal[im] = yrshiftlocal[im];
                                                        for (int im = 0; im < ndata; ++im) mphilocal[im]    = fmod(540.0f-phirlocal[im], 360.0f);
                                                }
                                        } else {
                                                if (mxm > tmax) {
                                                        tmax = mxm;
                                                        mpsi = 90.0f;
                                                        for (int im = 0; im < ndata; ++im) mxshiftlocal[im] = xshiftlocal[im];
                                                        for (int im = 0; im < ndata; ++im) myshiftlocal[im] = yshiftlocal[im];
                                                        for (int im = 0; im < ndata; ++im) mphilocal[im]    = philocal[im];
                                                }
                                        }
                                }
                        }
                }
                //cout << idir<< "  "<< tmax<< endl;
                if (tmax > dirma) {
                        dirma = tmax;
                        dpsi = mpsi;
                        for (int im = 0; im < ndata; ++im) dxshiftlocal[im] = mxshiftlocal[im];
                        for (int im = 0; im < ndata; ++im) dyshiftlocal[im] = myshiftlocal[im];
                        for (int im = 0; im < ndata; ++im) dphilocal[im]    = mphilocal[im];
                }
        }

        for (int im = 0; im < ndata; ++im) {
                float psx  = dxshiftlocal[im];
                float psy  = dyshiftlocal[im];
                float pphi = dphilocal[im];
                float epsi;
                float bestang;
                if (FindPsi) {
                        float qphi = pphi/delta;
                        int iphi = ( int(qphi + ((int)(abs(qphi/nphi))+1)*nphi + 0.5f))%nphi ;
                        //#cout <<  " ref number and current parameters reduced to 2D  ",iphi,0.0, psx, psy
                        Dict params = Util::get_transform_params(data[im], "xform.projection", "spider");
                        float opsi3 = params["psi"];
                        float opx3 = -static_cast<float>(params["tx"]);
                        float opy3 = -static_cast<float>(params["ty"]);
                        //#cout << " old 3D params in data ",ophi, otheta, opsi3, opx3, opy3
                        float gamma = (abs(opsi3 - 90) < abs(opsi3 - 270)) ? (90) : (270);
                        float oalpha, osx, osy;
                        compose_transform2(0, opx3, opy3, gamma-opsi3, 0, 0, oalpha, osx, osy); //# reduce 3D to 2D
                        //#cout << " old 3D params, -> 2D ",oalpha, osx, osy
                        //# combine previous with the current in plane
                        //#cout << " current 2D combined with old 2D rotation",oalpha, csx, csy
                        //#  Find what the shift is without the angle
                        float junk, nnsx, nnsy;
                        compose_transform2(0, psx, psy, -oalpha, 0, 0, junk, nnsx, nnsy);
                        //#cout << " 2D shift without angle ",nnsx, nnsy

                        std::auto_ptr<EMData> cimage( Util::Polar2Dm(data[im], cnx+nnsx, cny+nnsy, numr, mode) );
                        Util::Frngs(cimage.get(), numr);
                        std::auto_ptr<EMData> temp( Util::Crosrng_msg_s( cimage.get(), crefim[iphi], numr) );

                        int ipr = int(psi_max*maxrin/360.0f + 0.5f);
                        int incpsi = (dpsi == 270.0f) ? (maxrin/2) : (0);
                        float qn = -1.0e23f;
                        for (int ips = -ipr; ips < ipr+1; ++ips) {
                                int tot = (ips + incpsi + maxrin)%maxrin;
                                float tval = temp->get_value_at(tot);
                                if (tval > qn) {
                                        qn = tval;
                                        bestang = ang_n(tot+1.0f, mode, maxrin);
                                }
                        }
                        //#cout << " best angle ",bestang
                        bestang = fmod(bestang - (dpsi-90.0f) + 720.0f, 360.0f);
                        //#cout << " angle applied ",bestang
                        //#rot_shift2D(data[im],-bestang).write_image("rotated.hdf",im)
                        //std::auto_ptr<EMData> rot_data_im( data[im]->rot_scale_trans2D_background(-bestang, 0, 0, 1) );
                        //fdata[im] = (rot_data_im->is_complex()) ? (rot_data_im->do_ift()) : (rot_data_im->do_fft());

                        //#cout <<  " New composed 3D  ",dpsi,bestang, nnsx, nnsy

                        epsi = fmod(bestang+dpsi, 360.0f);
                        psx = nnsx;
                        psy = nnsy;
                        //#cout <<  " New composed 3D  ",pphi,90.0,epsi, psx, psy
                } else {
                        epsi = dpsi;
                        bestang = 0;
                }
                data[im]->set_attr("bestang",fmod(720.0f-bestang, 360.0f));
                //printf("  %7.2f  %7.2f  %7.2f  %7.2f  %7.2f\n", pphi, 90.0, epsi, psx, psy);
                Dict t_params;
                t_params["type"]  = "spider";
                t_params["phi"]   = pphi;
        t_params["theta"] = 90.0f;
        t_params["psi"]   = epsi;
        t_params["tx"]    = -psx;
        t_params["ty"]    = -psy;
        Transform t(t_params);
        data[im]->set_attr("xform.projection", &t);
        }
}
int Util::coveig ( int  n,
float *  covmat,
float *  eigval,
float *  eigvec 
) [static]

For those util function developed by Pawel's group.

$Id$ This file is a part of util.h, To use this file's functions, you should include "util.h" NEVER directly include this file

Definition at line 6517 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, and ssyev_().

Referenced by coveig_for_py().

{
        // n size of the covariance/correlation matrix
        // covmat --- covariance/correlation matrix (n by n)
        // eigval --- returns eigenvalues
        // eigvec --- returns eigenvectors

        ENTERFUNC;

        int i;

        // make a copy of covmat so that it will not be overwritten
        for ( i = 0 ; i < n * n ; i++ )   eigvec[i] = covmat[i];

        char NEEDV = 'V';
        char UPLO = 'U';
        int lwork = -1;
        int info = 0;
        float *work, wsize;

        //  query to get optimal workspace
        ssyev_(&NEEDV, &UPLO, &n, eigvec, &n, eigval, &wsize, &lwork, &info);
        lwork = (int)wsize;

        work = (float *)calloc(lwork, sizeof(float));
        //  calculate eigs
        ssyev_(&NEEDV, &UPLO, &n, eigvec, &n, eigval, work, &lwork, &info);
        free(work);
        EXITFUNC;
        return info;
}
Dict Util::coveig_for_py ( int  ncov,
const vector< float > &  covmatpy 
) [static]

same function than Util::coveig but wrapped to use directly in python code

Definition at line 6549 of file util_sparx.cpp.

References coveig(), covmat, eigval, eigvec, ENTERFUNC, EXITFUNC, and status.

{

        ENTERFUNC;
        int len = covmatpy.size();
        float *eigvec;
        float *eigval;
        float *covmat;
        int status = 0;
        eigval = (float*)calloc(ncov,sizeof(float));
        eigvec = (float*)calloc(ncov*ncov,sizeof(float));
        covmat = (float*)calloc(ncov*ncov, sizeof(float));

        const float *covmat_ptr;
        covmat_ptr = &covmatpy[0];
        for(int i=0;i<len;i++){
            covmat[i] = covmat_ptr[i];
        }

        status = Util::coveig(ncov, covmat, eigval, eigvec);

        vector<float> eigval_py(ncov);
        const float *eigval_ptr;
        eigval_ptr = &eigval[0];
        for(int i=0;i<ncov;i++){
            eigval_py[i] = eigval_ptr[i];
        }

        vector<float> eigvec_py(ncov*ncov);
        const float *eigvec_ptr;
        eigvec_ptr = &eigvec[0];
        for(int i=0;i<ncov*ncov;i++){
            eigvec_py[i] = eigvec_ptr[i];
        }

        Dict res;
        res["eigval"] = eigval_py;
        res["eigvec"] = eigvec_py;

        EXITFUNC;
        return res;
}
Dict Util::Crosrng_e ( EMData circ1,
EMData circ2,
vector< int >  numr,
int  neg 
) [static]

Definition at line 3357 of file util_sparx.cpp.

References circ1, circ2, fftr_d(), EMAN::EMData::get_data(), log(), numr, prb1d(), q, EMAN::Dict::size(), t, t7, and tot.

Referenced by multiref_polar_ali_2d_peaklist_local().

                                                                               {
        //  neg = 0 straight,  neg = 1 mirrored
        int nring = numr.size()/3;
        //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
        int maxrin = numr[numr.size()-1];
        double qn;   float  tot;
        float *circ1 = circ1p->get_data();
        float *circ2 = circ2p->get_data();
/*
c checks single position, neg is flag for checking mirrored position
c
c  input - fourier transforms of rings!
c  first set is conjugated (mirrored) if neg
c  circ1 already multiplied by weights!
c       automatic arrays
        dimension         t(maxrin)  removed +2 as it is only needed for other ffts
        double precision  q(maxrin)
        double precision  t7(-3:3)
*/
        float *t;
        double t7[7], *q;
        int    i, j, k, ip, jc, numr3i, numr2i, jtot = 0;
        float  pos;

#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int) (log2(maxrin));
#endif  //_WIN32

        q = (double*)calloc(maxrin, sizeof(double));
        t = (float*)calloc(maxrin, sizeof(float));

//   cout << *qn <<"  " <<*tot<<"  "<<ip<<endl;
        for (i=1; i<=nring; i++) {
                numr3i = numr(3,i);
                numr2i = numr(2,i);

                t(1) = (circ1(numr2i)) * circ2(numr2i);

                if (numr3i != maxrin) {
                         // test .ne. first for speed on some compilers
                        t(numr3i+1) = circ1(numr2i+1) * circ2(numr2i+1);
                        t(2)            = 0.0;

                        if (neg) {
                                // first set is conjugated (mirrored)
                                for (j=3;j<=numr3i;j=j+2) {
                                        jc = j+numr2i-1;
                                        t(j)   =  (circ1(jc))*circ2(jc)   - (circ1(jc+1))*circ2(jc+1);
                                        t(j+1) = -(circ1(jc))*circ2(jc+1) - (circ1(jc+1))*circ2(jc);
                                }
                        } else {
                                for (j=3;j<=numr3i;j=j+2) {
                                        jc = j+numr2i-1;
                                        t(j)   =  (circ1(jc))*circ2(jc)   + (circ1(jc+1))*circ2(jc+1);
                                        t(j+1) = -(circ1(jc))*circ2(jc+1) + (circ1(jc+1))*circ2(jc);
                                }
                        }
                        for (j=1;j<=numr3i+1;j++) q(j) = q(j) + t(j);
                } else {
                        t(2) = circ1(numr2i+1) * circ2(numr2i+1);
                        if (neg) {
                                // first set is conjugated (mirrored)
                                for (j=3;j<=maxrin;j=j+2) {
                                        jc = j+numr2i-1;
                                        t(j)   =  (circ1(jc))*circ2(jc)   - (circ1(jc+1))*circ2(jc+1);
                                        t(j+1) = -(circ1(jc))*circ2(jc+1) - (circ1(jc+1))*circ2(jc);
                                }
                        } else {
                                for (j=3;j<=maxrin;j=j+2) {
                                        jc = j+numr2i-1;
                                        t(j)   =  (circ1(jc))*circ2(jc)   + (circ1(jc+1))*circ2(jc+1);
                                        t(j+1) = -(circ1(jc))*circ2(jc+1) + (circ1(jc+1))*circ2(jc);
                                }
                        }
                        for (j = 1; j <= maxrin; j++) q(j) += t(j);
                }
        }

        fftr_d(q,ip);

        qn = -1.0e20;
        for (j=1;j<=maxrin;j++) {
           if (q(j) >= qn) {
                  qn = q(j); jtot = j;
           }
        }
        for (k=-3; k<=3; k++) {
                j = (jtot+k+maxrin-1)%maxrin + 1;
                t7(k+4) = q(j);
        }

        prb1d(t7,7,&pos);

        tot = (float)jtot + pos;

        if (q) free(q);
        if (t) free(t);

        Dict retvals;
        retvals["qn"]  = qn;
        retvals["tot"] = tot;
        return  retvals;
}
Dict Util::Crosrng_ew ( EMData circ1,
EMData circ2,
vector< int >  numr,
vector< float >  w,
int  neg 
) [static]

Definition at line 3463 of file util_sparx.cpp.

References circ1, circ2, fftr_d(), EMAN::EMData::get_data(), log(), numr, prb1d(), q, EMAN::Dict::size(), t, t7, and tot.

                                                                                                 {
   //  neg = 0 straight,  neg = 1 mirrored
        int nring = numr.size()/3;
        //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
        int maxrin = numr[numr.size()-1];
        double qn;   float  tot;
        float *circ1 = circ1p->get_data();
        float *circ2 = circ2p->get_data();
/*
c checks single position, neg is flag for checking mirrored position
c
c  input - fourier transforms of rings!
c  first set is conjugated (mirrored) if neg
c  multiplication by weights!
c       automatic arrays
        dimension         t(maxrin)  removed +2 as it is only needed for other ffts
        double precision  q(maxrin)
        double precision  t7(-3:3)
*/
        float *t;
        double t7[7], *q;
        int    i, j, k, ip, jc, numr3i, numr2i, jtot = 0;
        float  pos;

#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int) (log2(maxrin));
#endif  //_WIN32

        q = (double*)calloc(maxrin, sizeof(double));
        t = (float*)calloc(maxrin, sizeof(float));

//   cout << *qn <<"  " <<*tot<<"  "<<ip<<endl;
        for (i=1;i<=nring;i++) {
                numr3i = numr(3,i);
                numr2i = numr(2,i);

                t(1) = circ1(numr2i) * circ2(numr2i);

                if (numr3i != maxrin) {
                        // test .ne. first for speed on some compilers
                        t(numr3i+1) = circ1(numr2i+1) * circ2(numr2i+1);
                        t(2)      = 0.0;

                        if (neg) {
                                // first set is conjugated (mirrored)
                                for (j=3; j<=numr3i; j=j+2) {
                                        jc = j+numr2i-1;
                                        t(j)   =  (circ1(jc))*circ2(jc)   - (circ1(jc+1))*circ2(jc+1);
                                        t(j+1) = -(circ1(jc))*circ2(jc+1) - (circ1(jc+1))*circ2(jc);
                                }
                        } else {
                                for (j=3; j<=numr3i; j=j+2) {
                                        jc     = j+numr2i-1;
                                        t(j)   =  (circ1(jc))*circ2(jc)   + (circ1(jc+1))*circ2(jc+1);
                                        t(j+1) = -(circ1(jc))*circ2(jc+1) + (circ1(jc+1))*circ2(jc);
                                }
                        }
                        for (j=1;j<=numr3i+1;j++) q(j) += t(j)*w[i-1];
                } else {
                        t(2) = circ1(numr2i+1) * circ2(numr2i+1);
                        if (neg) {
                                // first set is conjugated (mirrored)
                                for (j=3; j<=maxrin; j=j+2) {
                                        jc = j+numr2i-1;
                                        t(j)   =  (circ1(jc))*circ2(jc)   - (circ1(jc+1))*circ2(jc+1);
                                        t(j+1) = -(circ1(jc))*circ2(jc+1) - (circ1(jc+1))*circ2(jc);
                                }
                        } else {
                                for (j=3; j<=maxrin; j=j+2) {
                                jc = j+numr2i-1;
                                t(j)   =  (circ1(jc))*circ2(jc)   + (circ1(jc+1))*circ2(jc+1);
                                t(j+1) = -(circ1(jc))*circ2(jc+1) + (circ1(jc+1))*circ2(jc);
                                }
                        }
                        for (j = 1; j <= maxrin; j++) q(j) += t(j)*w[i-1];
                }
        }

        fftr_d(q,ip);

        qn = -1.0e20;
        for (j=1;j<=maxrin;j++) {
                //cout << j << "  " << q(j) << endl;
                if (q(j) >= qn) {
                        qn = q(j);
                        jtot = j;
                }
        }

        for (k=-3; k<=3; k++) {
                j = (jtot+k+maxrin-1)%maxrin + 1;
                t7(k+4) = q(j);
        }

        prb1d(t7,7,&pos);

        tot = (float)jtot + pos;

        //if (q) free(q);
        if (t) free(t);

        Dict retvals;
        //tot = 1;
        //qn = q(1);
        retvals["qn"] = qn;
        retvals["tot"] = tot;

        if (q) free(q);

        return  retvals;
}
Dict Util::Crosrng_ms ( EMData circ1,
EMData circ2,
vector< int >  numr 
) [static]

Definition at line 3577 of file util_sparx.cpp.

References circ1, circ2, fftr_d(), EMAN::EMData::get_data(), log(), numr, prb1d(), q, EMAN::Dict::size(), t, t7, tmt, and tot.

Referenced by multiref_polar_ali_2d(), multiref_polar_ali_2d_local(), and multiref_polar_ali_2d_peaklist().

                                                                      {
        int nring = numr.size()/3;
        //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
        int maxrin = numr[numr.size()-1];
        double qn; float tot; double qm; float tmt;
        float *circ1 = circ1p->get_data();
        float *circ2 = circ2p->get_data();
/*
c
c  checks both straight & mirrored positions
c
c  input - fourier transforms of rings!!
c  circ1 already multiplied by weights!
c
*/

        // dimension             circ1(lcirc),circ2(lcirc)

        // t(maxrin), q(maxrin), t7(-3:3)  //maxrin+2 removed
        double *t, *q, t7[7];

        int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
        float t1, t2, t3, t4, c1, c2, d1, d2, pos;

        qn  = 0.0f;
        qm  = 0.0f;
        tot = 0.0f;
        tmt = 0.0f;
#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int)(log2(maxrin));
#endif  //_WIN32
  //for (j=1; j<=maxrin;j++) cout <<"  "<<j<<"   "<<circ1(j)<<"   "<<circ2(j) <<endl;

        //  c - straight  = circ1 * conjg(circ2)
        //  zero q array

        q = (double*)calloc(maxrin,sizeof(double));

        //   t - mirrored  = conjg(circ1) * conjg(circ2)
        //   zero t array
        t = (double*)calloc(maxrin,sizeof(double));

   //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
        for (i=1; i<=nring; i++) {

                numr3i = numr(3,i);   // Number of samples of this ring
                numr2i = numr(2,i);   // The beginning point of this ring

                t1   = circ1(numr2i) * circ2(numr2i);
                q(1) += t1;
                t(1) += t1;

                t1   = circ1(numr2i+1) * circ2(numr2i+1);
                if (numr3i == maxrin)  {
                        q(2) += t1;
                        t(2) += t1;
                } else {
                        q(numr3i+1) += t1;
                        t(numr3i+1) += t1;
                }

                for (j=3; j<=numr3i; j += 2) {
                        jc     = j+numr2i-1;

// Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
//                                ----- -----    ----- -----
//                                 t1     t2      t3    t4
// Here, conj(c1+c2i)*conj(d1+d2i) = (c1*d1-c2*d2)+(-c1*d2-c2*d1)i
//                                    ----- -----    ----- -----
//                                     t1    t2       t3    t4

                        c1     = circ1(jc);
                        c2     = circ1(jc+1);
                        d1     = circ2(jc);
                        d2     = circ2(jc+1);

                        t1     = c1 * d1;
                        t2     = c2 * d2;
                        t3     = c1 * d2;
                        t4     = c2 * d1;

                        q(j)   += t1 + t2;
                        q(j+1) += -t3 + t4;
                        t(j)   += t1 - t2;
                        t(j+1) += -t3 - t4;
                }
        }
        //for (j=1; j<=maxrin; j++) cout <<"  "<<j<<"   "<<q(j) <<"   "<<t(j) <<endl;
        fftr_d(q,ip);

        qn  = -1.0e20;
        for (j=1; j<=maxrin; j++) {//cout <<"  "<<j<<"   "<<q(j) <<endl;
                if (q(j) >= qn) {
                        qn  = q(j);
                        jtot = j;
                }
        }

        for (k=-3; k<=3; k++) {
                j = ((jtot+k+maxrin-1)%maxrin)+1;
                t7(k+4) = q(j);
        }

        // interpolate
        prb1d(t7,7,&pos);
        tot = (float)(jtot)+pos;
        // Do not interpolate
        //tot = (float)(jtot);

        // mirrored
        fftr_d(t,ip);

        // find angle
        qm = -1.0e20;
        for (j=1; j<=maxrin;j++) {//cout <<"  "<<j<<"   "<<t(j) <<endl;
                if ( t(j) >= qm ) {
                        qm   = t(j);
                        jtot = j;
                }
        }

        for (k=-3; k<=3; k++)  {
                j = ((jtot+k+maxrin-1)%maxrin) + 1;
                t7(k+4) = t(j);
        }

        // interpolate

        prb1d(t7,7,&pos);
        tmt = float(jtot) + pos;
        // Do not interpolate
        //tmt = float(jtot);

        free(t);
        free(q);

        Dict retvals;
        retvals["qn"] = qn;
        retvals["tot"] = tot;
        retvals["qm"] = qm;
        retvals["tmt"] = tmt;
        return retvals;
}
Dict Util::Crosrng_ms_delta ( EMData circ1,
EMData circ2,
vector< int >  numr,
float  delta_start,
float  delta 
) [static]

Definition at line 3883 of file util_sparx.cpp.

References circ1, circ2, fftr_d(), EMAN::EMData::get_data(), log(), numr, q, EMAN::Dict::size(), t, tmt, and tot.

Referenced by multiref_polar_ali_2d_delta().

                                                                                                            {
        int nring = numr.size()/3;
        //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
        int maxrin = numr[numr.size()-1];
        double qn; float tot; double qm; float tmt;
        float *circ1 = circ1p->get_data();
        float *circ2 = circ2p->get_data();
/*
c
c  checks both straight & mirrored positions
c
c  input - fourier transforms of rings!!
c  circ1 already multiplied by weights!
c
*/

        // dimension             circ1(lcirc),circ2(lcirc)

        // t(maxrin), q(maxrin), t7(-3:3)  //maxrin+2 removed
        double *t, *q;

        int   ip, jc, numr3i, numr2i, i, j, jtot = 0;
        float t1, t2, t3, t4, c1, c2, d1, d2;

        qn  = 0.0f;
        qm  = 0.0f;
        tot = 0.0f;
        tmt = 0.0f;
#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int)(log2(maxrin));
#endif  //_WIN32
  //for (j=1; j<=maxrin;j++) cout <<"  "<<j<<"   "<<circ1(j)<<"   "<<circ2(j) <<endl;

        //  c - straight  = circ1 * conjg(circ2)
        //  zero q array

        q = (double*)calloc(maxrin,sizeof(double));

        //   t - mirrored  = conjg(circ1) * conjg(circ2)
        //   zero t array
        t = (double*)calloc(maxrin,sizeof(double));

   //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
        for (i=1; i<=nring; i++) {

                numr3i = numr(3,i);   // Number of samples of this ring
                numr2i = numr(2,i);   // The beginning point of this ring

                t1   = circ1(numr2i) * circ2(numr2i);
                q(1) += t1;
                t(1) += t1;

                t1   = circ1(numr2i+1) * circ2(numr2i+1);
                if (numr3i == maxrin)  {
                        q(2) += t1;
                        t(2) += t1;
                } else {
                        q(numr3i+1) += t1;
                        t(numr3i+1) += t1;
                }

                for (j=3; j<=numr3i; j += 2) {
                        jc     = j+numr2i-1;

// Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
//                                ----- -----    ----- -----
//                                 t1     t2      t3    t4
// Here, conj(c1+c2i)*conj(d1+d2i) = (c1*d1-c2*d2)+(-c1*d2-c2*d1)i
//                                    ----- -----    ----- -----
//                                     t1    t2       t3    t4

                        c1     = circ1(jc);
                        c2     = circ1(jc+1);
                        d1     = circ2(jc);
                        d2     = circ2(jc+1);

                        t1     = c1 * d1;
                        t2     = c2 * d2;
                        t3     = c1 * d2;
                        t4     = c2 * d1;

                        q(j)   += t1 + t2;
                        q(j+1) += -t3 + t4;
                        t(j)   += t1 - t2;
                        t(j+1) += -t3 - t4;
                }
        }
        //for (j=1; j<=maxrin; j++) cout <<"  "<<j<<"   "<<q(j) <<"   "<<t(j) <<endl;
        fftr_d(q,ip);

        qn  = -1.0e20;

        int jstart = 1+static_cast<int>(delta_start/360.0*maxrin);
        int jstep = static_cast<int>(delta/360.0*maxrin);
        if (jstep < 1) { jstep = 1; }

        for (j=jstart; j<=maxrin; j+=jstep) {//cout <<"  "<<j<<"   "<<q(j) <<endl;
                if (q(j) >= qn) {
                        qn  = q(j);
                        jtot = j;
                }
        }

        //for (k=-3; k<=3; k++) {
        //      j = ((jtot+k+maxrin-1)%maxrin)+1;
        //      t7(k+4) = q(j);
        //}

        // interpolate
        //prb1d(t7,7,&pos);
        //tot = (float)(jtot)+pos;
        // Do not interpolate
        tot = (float)(jtot);

        // mirrored
        fftr_d(t,ip);

        // find angle
        qm = -1.0e20;
        for (j=jstart; j<=maxrin;j+=jstep) {//cout <<"  "<<j<<" "<<t(j) <<endl;
                if ( t(j) >= qm ) {
                        qm   = t(j);
                        jtot = j;
                }
        }

        //for (k=-3; k<=3; k++)  {
        //      j = ((jtot+k+maxrin-1)%maxrin) + 1;
        //      t7(k+4) = t(j);
        //}

        // interpolate

        //prb1d(t7,7,&pos);
        //tmt = float(jtot) + pos;
        // Do not interpolate
        tmt = float(jtot);

        free(t);
        free(q);

        Dict retvals;
        retvals["qn"] = qn;
        retvals["tot"] = tot;
        retvals["qm"] = qm;
        retvals["tmt"] = tmt;
        return retvals;
}
EMData * Util::Crosrng_msg ( EMData circ1,
EMData circ2,
vector< int >  numr 
) [static]

checks both straight & mirrored positions input - fourier transforms of rings!! circ1 already multiplied by weights! returns EM object with 1D ccf

Definition at line 4334 of file util_sparx.cpp.

References circ1b, circ2b, dout, fftr_d(), EMAN::EMData::get_data(), log(), numr, q, EMAN::EMData::set_size(), and t.

                                                                        {

   // dimension         circ1(lcirc),circ2(lcirc)

        int   ip, jc, numr3i, numr2i, i, j;
        float t1, t2, t3, t4, c1, c2, d1, d2;

        int nring = numr.size()/3;
        //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
        int maxrin = numr[numr.size()-1];

        float* circ1b = circ1->get_data();
        float* circ2b = circ2->get_data();

        // t(maxrin), q(maxrin)  // removed +2
        double *t, *q;

        q = (double*)calloc(maxrin,sizeof(double));
        t = (double*)calloc(maxrin,sizeof(double));

#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int)(log2(maxrin));
#endif  //_WIN32

        //  q - straight  = circ1 * conjg(circ2)

        //   t - mirrored  = conjg(circ1) * conjg(circ2)

        //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower

        for (i=1; i<=nring; i++) {

                numr3i = numr(3,i);
                numr2i = numr(2,i);

                t1   = circ1b(numr2i) * circ2b(numr2i);
                q(1) += t1;
                t(1) += t1;

                t1   = circ1b(numr2i+1) * circ2b(numr2i+1);
                if (numr3i == maxrin)  {
                        q(2) += t1;
                        t(2) += t1;
                } else {
                        q(numr3i+1) += t1;
                        t(numr3i+1) += t1;
                }

                for (j=3; j<=numr3i; j=j+2) {
                        jc     = j+numr2i-1;

                        c1     = circ1b(jc);
                        c2     = circ1b(jc+1);
                        d1     = circ2b(jc);
                        d2     = circ2b(jc+1);

                        t1     = c1 * d1;
                        t3     = c1 * d2;
                        t2     = c2 * d2;
                        t4     = c2 * d1;

                        q(j)   +=  t1 + t2;
                        q(j+1) += -t3 + t4;
                        t(j)   +=  t1 - t2;
                        t(j+1) += -t3 - t4;
                }
        }

        // straight
        fftr_d(q,ip);

        // mirrored
        fftr_d(t,ip);

        EMData* out = new EMData();
        out->set_size(maxrin,2,1);
        float *dout = out->get_data();
        for (int i=0; i<maxrin; i++) {dout(i,0)=static_cast<float>(q[i]); dout(i,1)=static_cast<float>(t[i]);}
        //out->set_size(maxrin,1,1);
        //float *dout = out->get_data();
        //for (int i=0; i<maxrin; i++) {dout(i,0)=q[i];}
        free(t);
        free(q);
        return out;
}
EMData * Util::Crosrng_msg_m ( EMData circ1,
EMData circ2,
vector< int >  numr 
) [static]

This program is half of the Crosrng_msg.

It only checks mirrored position. input - fourier transforms of rings!! circ1 already multiplied by weights! returns EM object with 1D ccf

Definition at line 4570 of file util_sparx.cpp.

References circ1b, circ2b, dout, fftr_d(), EMAN::EMData::get_data(), log(), numr, EMAN::EMData::set_size(), and t.

{

        int   ip, jc, numr3i, numr2i, i, j;
        float c1, c2, d1, d2;

        int nring = numr.size()/3;
        int maxrin = numr[numr.size()-1];

        float* circ1b = circ1->get_data();
        float* circ2b = circ2->get_data();

        double *t;

        t = (double*)calloc(maxrin,sizeof(double));

#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int)(log2(maxrin));
#endif  //_WIN32

         //   t - mirrored  = conjg(circ1) * conjg(circ2)

        for (i=1;i<=nring;i++) {

                numr3i = numr(3,i);
                numr2i = numr(2,i);
                t(1) += circ1b(numr2i) * circ2b(numr2i);

                if (numr3i == maxrin)  t(2) += circ1b(numr2i+1) * circ2b(numr2i+1);
                else          t(numr3i+1) += circ1b(numr2i+1) * circ2b(numr2i+1);

                for (j=3;j<=numr3i;j=j+2) {
                        jc     = j+numr2i-1;

                        c1     = circ1b(jc);
                        c2     = circ1b(jc+1);
                        d1     = circ2b(jc);
                        d2     = circ2b(jc+1);

                        t(j)   +=  c1 * d1 - c2 * d2;
                        t(j+1) += -c1 * d2 - c2 * d1;
                }
        }

        // mirrored
        fftr_d(t,ip);

        EMData* out = new EMData();
        out->set_size(maxrin,1,1);
        float *dout = out->get_data();
        for (int i=0; i<maxrin; i++) dout[i]=static_cast<float>(t[i]);
        free(t);
        return out;

}
EMData * Util::Crosrng_msg_s ( EMData circ1,
EMData circ2,
vector< int >  numr 
) [static]

This program is half of the Crosrng_msg.

It only checks straight position. input - fourier transforms of rings!! circ1 already multiplied by weights! returns EM object with 1D ccf

Definition at line 4510 of file util_sparx.cpp.

References circ1b, circ2b, dout, fftr_d(), EMAN::EMData::get_data(), log(), numr, q, and EMAN::EMData::set_size().

Referenced by constrained_helix_exhaustive().

{

        int   ip, jc, numr3i, numr2i, i, j;
        float c1, c2, d1, d2;

        int nring = numr.size()/3;
        int maxrin = numr[numr.size()-1];

        float* circ1b = circ1->get_data();
        float* circ2b = circ2->get_data();

        double *q;

        q = (double*)calloc(maxrin,sizeof(double));

#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int)(log2(maxrin));
#endif  //_WIN32

         //  q - straight  = circ1 * conjg(circ2)

        for (i=1;i<=nring;i++) {

                numr3i = numr(3,i);
                numr2i = numr(2,i);

                q(1) += circ1b(numr2i) * circ2b(numr2i);

                if (numr3i == maxrin)   q(2) += circ1b(numr2i+1) * circ2b(numr2i+1);
                else             q(numr3i+1) += circ1b(numr2i+1) * circ2b(numr2i+1);

                for (j=3;j<=numr3i;j=j+2) {
                        jc     = j+numr2i-1;

                        c1     = circ1b(jc);
                        c2     = circ1b(jc+1);
                        d1     = circ2b(jc);
                        d2     = circ2b(jc+1);

                        q(j)   +=  c1 * d1 + c2 * d2;
                        q(j+1) += -c1 * d2 + c2 * d1;
                }
        }

        // straight
        fftr_d(q,ip);

        EMData* out = new EMData();
        out->set_size(maxrin,1,1);
        float *dout = out->get_data();
        for (int i=0; i<maxrin; i++) dout[i]=static_cast<float>(q[i]);
        free(q);
        return out;

}
void Util::Crosrng_msg_vec ( EMData circ1,
EMData circ2,
vector< int >  numr,
float *  q,
float *  t 
) [static]

checks both straight & mirrored positions input - fourier transforms of rings!! circ1 already multiplied by weights! returns EM object with 1D ccf

Definition at line 4438 of file util_sparx.cpp.

References circ1b, circ2b, fftr_q(), EMAN::EMData::get_data(), log(), numr, q, and t.

Referenced by ali2d_ccf_list(), Crosrng_msg_vec_p(), multiref_peaks_ali2d(), and multiref_peaks_compress_ali2d().

                                                                                             {

   // dimension         circ1(lcirc),circ2(lcirc)

        int   ip, jc, numr3i, numr2i, i, j;
        float t1, t2, t3, t4, c1, c2, d1, d2;

        int nring = numr.size()/3;
        //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
        int maxrin = numr[numr.size()-1];

        float* circ1b = circ1->get_data();
        float* circ2b = circ2->get_data();

#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int)(log2(maxrin));
#endif  //_WIN32
        for (int i=1; i<=maxrin; i++)  {q(i) = 0.0f; t(i) = 0.0f;}

        //  q - straight  = circ1 * conjg(circ2)

        //   t - mirrored  = conjg(circ1) * conjg(circ2)

        for (i=1; i<=nring; i++) {

                numr3i = numr(3,i);
                numr2i = numr(2,i);

                t1   = circ1b(numr2i) * circ2b(numr2i);
                q(1) += t1;
                t(1) += t1;

                t1   = circ1b(numr2i+1) * circ2b(numr2i+1);
                if (numr3i == maxrin)  {
                        q(2) += t1;
                        t(2) += t1;
                } else {
                        q(numr3i+1) += t1;
                        t(numr3i+1) += t1;
                }

                for (j=3; j<=numr3i; j=j+2) {
                        jc     = j+numr2i-1;

                        c1     = circ1b(jc);
                        c2     = circ1b(jc+1);
                        d1     = circ2b(jc);
                        d2     = circ2b(jc+1);

                        t1     = c1 * d1;
                        t3     = c1 * d2;
                        t2     = c2 * d2;
                        t4     = c2 * d1;

                        q(j)   +=  t1 + t2;
                        q(j+1) += -t3 + t4;
                        t(j)   +=  t1 - t2;
                        t(j+1) += -t3 - t4;
                }
        }
        // straight
        fftr_q(q,ip);
        //for (int i=0; i<maxrin; i++) cout<<i<<"  B    "<<q[i]<<"       "<<t[i]<<endl;

        // mirrored
        fftr_q(t,ip);
}
vector< float > Util::Crosrng_msg_vec_p ( EMData circ1,
EMData circ2,
vector< int >  numr 
) [static]

Definition at line 4423 of file util_sparx.cpp.

References Crosrng_msg_vec().

                                                                                     {

        int maxrin = numr[numr.size()-1];

        vector<float> r(2*maxrin);

        Crosrng_msg_vec( circ1, circ2, numr, &r[0], &r[maxrin] );

        return r;
}
Dict Util::Crosrng_ns ( EMData circ1,
EMData circ2,
vector< int >  numr 
) [static]

checks both straight & mirrored positions Find only positions within +/-pis_max of 0 and 180 (for helcal use) input - fourier transforms of rings! circ1 already multiplied by weights!

Definition at line 4235 of file util_sparx.cpp.

References circ1, circ2, fftr_d(), EMAN::EMData::get_data(), log(), numr, prb1d(), q, EMAN::Dict::size(), t7, and tot.

Referenced by multiref_polar_ali_2d_nom().

                                                                      {
        int nring = numr.size()/3;
        //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
        int maxrin = numr[numr.size()-1];
        double qn; float tot;
        float *circ1 = circ1p->get_data();
        float *circ2 = circ2p->get_data();
/*
c
c  checks only straight position
c
c  input - fourier transforms of rings!!
c  circ1 already multiplied by weights!
c
*/

        // dimension             circ1(lcirc),circ2(lcirc)

        // q(maxrin), t7(-3:3)  //maxrin+2 removed
        double *q, t7[7];

        int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
        float c1, c2, d1, d2, pos;

        qn  = 0.0;
        tot = 0.0;
#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
   ip = -(int)(log2(maxrin));
#endif  //_WIN32
        //for (j=1; j<=maxrin;j++) cout <<"  "<<j<<"   "<<circ1(j)<<"   "<<circ2(j) <<endl;

        //  c - straight  = circ1 * conjg(circ2)
        //  zero q array

        q = (double*)calloc(maxrin,sizeof(double));

                        //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
        for (i=1; i<=nring; i++) {

                numr3i = numr(3,i);   // Number of samples of this ring
                numr2i = numr(2,i);   // The beginning point of this ring

                q(1) += circ1(numr2i) * circ2(numr2i);

                if (numr3i == maxrin)   q(2) += circ1(numr2i+1) * circ2(numr2i+1);
                else                 q(numr3i+1) += circ1(numr2i+1) * circ2(numr2i+1);

                for (j=3; j<=numr3i; j += 2) {
                        jc     = j+numr2i-1;

// Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
//                                ----- -----    ----- -----
//                                 t1     t2      t3    t4

                        c1     = circ1(jc);
                        c2     = circ1(jc+1);
                        d1     = circ2(jc);
                        d2     = circ2(jc+1);

                        q(j)   +=  c1 * d1 + c2 * d2;
                        q(j+1) += -c1 * d2 + c2 * d1;
                }
        }
//for (j=1; j<=maxrin; j++) cout <<"  "<<j<<"   "<<q(j) <<endl;
        fftr_d(q,ip);

        qn  = -1.0e20;
        for (j=1; j<=maxrin; j++) {//cout <<"  "<<j<<"   "<<q(j) <<endl;
                if (q(j) >= qn) {
                        qn  = q(j);
                        jtot = j;
                }
        }

        for (k=-3; k<=3; k++)  {
                j = ((jtot+k+maxrin-1)%maxrin)+1;
                t7(k+4) = q(j);
        }

        // interpolate
        prb1d(t7,7,&pos);
        tot = (float)(jtot)+pos;
        // Do not interpolate
        //*tot = (float)(jtot);

        free(q);

        Dict retvals;
        retvals["qn"] = qn;
        retvals["tot"] = tot;
        return retvals;
}
Dict Util::Crosrng_psi ( EMData circ1,
EMData circ2,
vector< int >  numr,
float  psi,
float  psimax 
) [static]

checks both straight & mirrored position input - fourier transforms of rings!! circ1 already multiplied by weights!

Definition at line 4116 of file util_sparx.cpp.

References circ1, circ2, fftr_d(), EMAN::EMData::get_data(), log(), numr, q, EMAN::Dict::size(), t, tmt, and tot.

Referenced by multiref_polar_ali_helical().

                                                                                                 {
// Computes both straight and mirrored

        int nring = numr.size()/3;
        int maxrin = numr[numr.size()-1];
        double qn; float tot; double qm; float tmt;
        float *circ1 = circ1p->get_data();
        float *circ2 = circ2p->get_data();

        double *t, *q;

        int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
        float t1, t2, t3, t4, c1, c2, d1, d2;

        qn  = 0.0f;
        qm  = 0.0f;
        tot = 0.0f;
        tmt = 0.0f;
#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int)(log2(maxrin));
#endif  //_WIN32

        //  c - straight  = circ1 * conjg(circ2)
        //  zero q array

        q = (double*)calloc(maxrin,sizeof(double));
        
        //   t - mirrored  = conjg(circ1) * conjg(circ2)
        //   zero t array
        t = (double*)calloc(maxrin,sizeof(double));
        
   //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
        
        for (i=1; i<=nring; i++) {

                numr3i = numr(3,i);   // Number of samples of this ring
                numr2i = numr(2,i);   // The beginning point of this ring

                t1   = circ1(numr2i) * circ2(numr2i);
                q(1) += t1;
                t(1) += t1;
                
                t1   = circ1(numr2i+1) * circ2(numr2i+1);
                if (numr3i == maxrin)  {
                        q(2) += t1;
                        t(2) += t1;
                } else {
                        q(numr3i+1) += t1;
                        t(numr3i+1) += t1;
                }

                for (j=3; j<=numr3i; j += 2) {
                        jc     = j+numr2i-1;

        // Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
        //                                ----- -----    ----- -----
        //                                 t1     t2      t3    t4

                        c1     = circ1(jc);
                        c2     = circ1(jc+1);
                        d1     = circ2(jc);
                        d2     = circ2(jc+1);

                        t1     = c1 * d1;
                        t3     = c1 * d2;
                        t2     = c2 * d2;
                        t4     = c2 * d1;

                        q(j)   +=  t1 + t2;
                        q(j+1) += -t3 + t4;
                        t(j)   +=  t1 - t2;
                        t(j+1) += -t3 - t4;
                }
        }

        fftr_d(q,ip);

        qn  = -1.0e20;
        // psi = 0 should correspond to psi_pos = 1
        int psi_pos = int(psi/360.0*maxrin+0.5) + 1;
        const int psi_range = int(psi_max/360.0*maxrin + 0.5);

        for (k=-psi_range; k<=psi_range; k++) {
                j = (k+psi_pos+maxrin-1)%maxrin+1;
                if (q(j) >= qn) {
                        qn  = q(j);
                        jtot = j;
                }
        }

        tot = (float)(jtot);
        free(q);

    // mirrored
        fftr_d(t,ip);

        qm  = -1.0e20;

        for (k=-psi_range; k<=psi_range; k++) {
                j = (k+psi_pos+maxrin-1)%maxrin+1;
                if (t(j) >= qm) {
                        qm  = t(j);
                        jtot = j;
                }
        }

        tmt = (float)(jtot);
        free(t);

        Dict retvals;
        retvals["qn"] = qn;
        retvals["tot"] = tot;
        retvals["qm"] = qm;
        retvals["tmt"] = tmt;
        return retvals;
}
Dict Util::Crosrng_sm_psi ( EMData circ1,
EMData circ2,
vector< int >  numr,
float  psi,
int  flag,
float  psimax 
) [static]

checks either straight or mirrored position depending on flag input - fourier transforms of rings!! circ1 already multiplied by weights!

Definition at line 4035 of file util_sparx.cpp.

References circ1, circ2, fftr_d(), EMAN::EMData::get_data(), log(), numr, q, EMAN::Dict::size(), and tot.

Referenced by multiref_polar_ali_2d_local_psi(), multiref_polar_ali_helical_90(), multiref_polar_ali_helical_90_local(), multiref_polar_ali_helical_local(), multiref_polar_ali_helicon_90_local(), and multiref_polar_ali_helicon_local().

                                                                                                              {
// flag 0 - straight, 1 - mirror

        int nring = numr.size()/3;
        int maxrin = numr[numr.size()-1];
        double qn; float tot;
        float *circ1 = circ1p->get_data();
        float *circ2 = circ2p->get_data();

        double *q;

        int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
        float c1, c2, d1, d2;

        qn  = 0.0f;
        tot = 0.0f;
#ifdef _WIN32
        ip = -(int)(log((float)maxrin)/log(2.0f));
#else
        ip = -(int)(log2(maxrin));
#endif  //_WIN32

        //  c - straight  = circ1 * conjg(circ2)
        //  zero q array

        q = (double*)calloc(maxrin,sizeof(double));
        int neg = 1-2*flag;
   //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower

        for (i=1; i<=nring; i++) {

                numr3i = numr(3,i);   // Number of samples of this ring
                numr2i = numr(2,i);   // The beginning point of this ring

                q(1) += circ1(numr2i) * circ2(numr2i);

                float t1 = circ1(numr2i+1) * circ2(numr2i+1);
                if (numr3i == maxrin)   q(2) += t1;
                else  q(numr3i+1) += t1;

                for (j=3; j<=numr3i; j += 2) {
                        jc     = j+numr2i-1;

        // Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
        //                                ----- -----    ----- -----
        //                                 t1     t2      t3    t4

                        c1     = circ1(jc);
                        c2     = circ1(jc+1);
                        d1     = circ2(jc);
                        d2     = circ2(jc+1);

                        q(j)   +=  c1 * d1 + c2 * d2*neg;
                        q(j+1) += -c1 * d2 + c2 * d1*neg;
                }
        }

        fftr_d(q,ip);

        qn  = -1.0e20;
        // psi = 0 should correspond to psi_pos = 1 (meaning no change in in-plane rotation)
        int psi_pos = int(psi/360.0*maxrin+0.5) + 1;
        const int psi_range = int(psi_max/360.0*maxrin + 0.5);

        for (k=-psi_range; k<=psi_range; k++) {
                j = ( k + psi_pos + maxrin - 1)%maxrin+1;
                if (q(j) >= qn) {
                        qn  = q(j);
                        jtot = j;
                }
        }

        tot = (float)(jtot);
        free(q);

        Dict retvals;
        retvals["qn"] = qn;
        retvals["tot"] = tot;
        return retvals;
}
EMData * Util::ctf2_rimg ( int  nx,
int  ny,
int  nz,
float  dz,
float  ps,
float  voltage,
float  cs,
float  wgh,
float  b_factor,
float  dza,
float  azz,
float  sign 
) [static]

Definition at line 21196 of file util_sparx.cpp.

References ny, EMAN::EMData::set_size(), tf(), and EMAN::EMData::update().

{
        int    ix, iy, iz;
        int    i,  j, k;
        float  ak;
        float  scx, scy, scz;
        EMData* ctf_img1 = new EMData();
        ctf_img1->set_size(nx, ny, nz);
        float freq = 1.0f/(2.0f*ps);
        scx = 2.0f/float(nx);
        if(ny>=1) scy = 2.0f/float(ny); else scy=0.0f;
        if(nz>=1) scz = 2.0f/float(nz); else scz=0.0f;
        int ns2 = nx/2 ;
        int nr2 = ny/2 ;
        int nl2 = nz/2 ;
        int nod = nx%2 ;
        int nok = ny%2 ;
        int noz = nz%2 ;
        for ( k=0; k<nz;k++) {
                iz = k - nl2;
                int kz = (nz - k - noz)%nz;
                float oz2 = iz*scz*iz*scz;
                for ( j=0; j<ny;j++) {
                        iy = j - nr2;
                        int jy = (ny - j - nok)%ny;
                        float oy = iy*scy;
                        float oy2 = oy*oy;
                        for ( i=0; i<=ns2; i++) {
                                ix = i - ns2;
                                if( dza == 0.0f) {
                                        ak=pow(ix*ix*scx*scx + oy2 + oz2, 0.5f)*freq;
                                        (*ctf_img1) (i,j,k)   = pow(Util::tf(dz, ak, voltage, cs, wgh, b_factor, sign),2);
                                } else {
                                        float ox = ix*scx;
                                        ak=pow(ox*ox + oy2 + oz2, 0.5f)*freq;
                                        //az = atan2(oy, ox);
                                        //float dzz = dz + dza/2.0f*sin(2*(az-azz*M_PI/180.0f-pihalf));
                                        float dzz = dz - dza/2.0f*sin(2*(atan2(oy, ox)+azz*M_PI/180.0f));
                                        (*ctf_img1) (i,j,k)   = pow(Util::tf(dzz, ak, voltage, cs, wgh, b_factor, sign),2);
                                }
                                ix = nx - i - nod;
                                if(ix<nx)  (*ctf_img1) (ix,jy,kz) = (*ctf_img1) (i,j,k);
                        }
                }
        }
        ctf_img1->update();
        return ctf_img1;
}
EMData * Util::ctf_img ( int  nx,
int  ny,
int  nz,
float  dz,
float  ps,
float  voltage,
float  cs,
float  wgh,
float  b_factor,
float  dza,
float  azz,
float  sign 
) [static]

Definition at line 21096 of file util_sparx.cpp.

References ny, EMAN::EMData::set_complex(), EMAN::EMData::set_fftodd(), EMAN::EMData::set_ri(), EMAN::EMData::set_size(), tf(), and EMAN::EMData::update().

{
        int    ix, iy, iz;
        int    i,  j, k;
        int    nr2, nl2;
        float  ak;
        float  scx, scy, scz;
        int    offset = 2 - nx%2;
        int    lsm = nx + offset;
        EMData* ctf_img1 = new EMData();
        ctf_img1->set_size(lsm, ny, nz);
        float freq = 1.0f/(2.0f*ps);
        scx = 2.0f/float(nx);
        if(ny>=1) scy = 2.0f/float(ny); else scy=0.0f;
        if(nz>=1) scz = 2.0f/float(nz); else scz=0.0f;
        nr2 = ny/2 ;
        nl2 = nz/2 ;
        for ( k=0; k<nz;k++) {
                iz = k;  if(k>nl2) iz=k-nz;
                float oz2 = iz*scz*iz*scz;
                for ( j=0; j<ny;j++) {
                        iy = j;  if(j>nr2) iy=j - ny;
                        float oy = iy*scy;
                        float oy2 = oy*oy;
                        for ( i=0; i<lsm/2; i++) {
                                ix=i;
                                if( dza == 0.0f) {
                                        ak=pow(ix*ix*scx*scx + oy2 + oz2, 0.5f)*freq;
                                        (*ctf_img1) (i*2,j,k)   = Util::tf(dz, ak, voltage, cs, wgh, b_factor, sign);
                                } else {
                                        float ox = ix*scx;
                                        ak=pow(ox*ox + oy2 + oz2, 0.5f)*freq;
                                        float dzz = dz - dza/2.0f*sin(2*(atan2(oy, ox)+azz*M_PI/180.0f));
                                        (*ctf_img1) (i*2,j,k)   = Util::tf(dzz, ak, voltage, cs, wgh, b_factor, sign);
                                }
                                //(*ctf_img1) (i*2+1,j,k) = 0.0f;  PAP  I assumed new EMData sets to zero
                        }
                }
        }
        ctf_img1->update();
        ctf_img1->set_complex(true);
        ctf_img1->set_ri(true);
        //ctf_img1->attr_dict["is_complex"] = 1;
        //ctf_img1->attr_dict["is_ri"] = 1;
        if(nx%2==0) ctf_img1->set_fftodd(false); else ctf_img1->set_fftodd(true);
        return ctf_img1;
}
EMData * Util::ctf_rimg ( int  nx,
int  ny,
int  nz,
float  dz,
float  ps,
float  voltage,
float  cs,
float  wgh,
float  b_factor,
float  dza,
float  azz,
float  sign 
) [static]

Definition at line 21146 of file util_sparx.cpp.

References ny, EMAN::EMData::set_size(), tf(), and EMAN::EMData::update().

{
        int    ix, iy, iz;
        int    i,  j, k;
        float  ak;
        float  scx, scy, scz;
        EMData* ctf_img1 = new EMData();
        ctf_img1->set_size(nx, ny, nz);
        float freq = 1.0f/(2.0f*ps);
        scx = 2.0f/float(nx);
        if(ny>=1) scy = 2.0f/float(ny); else scy=0.0f;
        if(nz>=1) scz = 2.0f/float(nz); else scz=0.0f;
        int ns2 = nx/2 ;
        int nr2 = ny/2 ;
        int nl2 = nz/2 ;
        int nod = nx%2 ;
        int nok = ny%2 ;
        int noz = nz%2 ;
        for ( k=0; k<nz;k++) {
                iz = k - nl2;
                int kz = (nz - k - noz)%nz;
                float oz2 = iz*scz*iz*scz;
                for ( j=0; j<ny;j++) {
                        iy = j - nr2;
                        int jy = (ny - j - nok)%ny;
                        float oy = iy*scy;
                        float oy2 = oy*oy;
                        for ( i=0; i<=ns2; i++) {
                                ix = i - ns2;
                                if( dza == 0.0f) {
                                        ak=pow(ix*ix*scx*scx + oy2 + oz2, 0.5f)*freq;
                                        (*ctf_img1) (i,j,k)   = Util::tf(dz, ak, voltage, cs, wgh, b_factor, sign);
                                } else {
                                        float ox = ix*scx;
                                        ak=pow(ox*ox + oy2 + oz2, 0.5f)*freq;
                                        float dzz = dz - dza/2.0f*sin(2*(atan2(oy, ox)+azz*M_PI/180.0f));
                                        (*ctf_img1) (i,j,k)   = Util::tf(dzz, ak, voltage, cs, wgh, b_factor, sign);
                                }
                                ix = nx - i - nod;
                                if(ix<nx)  (*ctf_img1) (ix,jy,kz) = (*ctf_img1) (i,j,k);
                        }
                }
        }
        ctf_img1->update();
        return ctf_img1;
}
vector< float > Util::curhelixshiftali ( vector< EMData * >  ctx,
vector< vector< float > >  pcoords,
int  nsegms,
int  search_rng,
int  nx,
int  ny 
) [static]

Definition at line 209 of file util_sparx.cpp.

References circ, get_value_at(), and sqrt().

{
        int cents, six, incline, kim, ixl, sib, circnum, circ, nxc, kang;
        float q0, qt, qu, tang, dst, xl, dxl, qm, bang, circd, circR, CircR, x1, x2;
        float dst0, dst1, dst2, xmax, xmin, maxincline, Rsinang, Rcosang, RSinang, RCosang;
        vector<float> result;
        
        nxc = nx/2;
        cents = nsegms/2;
        circnum = nxc;
        qm = -1.0e23; 
        dst1 = sqrt((pcoords[cents][0] - pcoords[0][0])*(pcoords[cents][0] - pcoords[0][0]) + (pcoords[cents][1] - pcoords[0][1])*(pcoords[cents][1] - pcoords[0][1]));
        dst2 = sqrt((pcoords[cents][0] - pcoords[nsegms-1][0])*(pcoords[cents][0] - pcoords[nsegms-1][0]) + (pcoords[cents][1] - pcoords[nsegms-1][1])*(pcoords[cents][1] - pcoords[nsegms-1][1]));
        
        for ( circ= 0; circ < circnum; circ++ ) {       
                circd = 4*(circ+1.0);
                circR = circd/2.0 + ((ny*nsegms)*(ny*nsegms))/(8.0*circd);
                x1 = circR - sqrt(circR*circR-dst1*dst1);
                x2 = circR - sqrt(circR*circR-dst2*dst2);
                xmax = (x1>x2)?x1:x2;
                if ( xmax == x1 ) dst0 = dst1;
                else dst0 = dst2;
                                 
                for ( six =-search_rng; six <= search_rng; six++ ) {
                        if (nx-(six+nxc+xmax)-2 <= 0) continue;
                        xmin = (nx-(six+nxc+xmax)-2<six+nxc-2)?(nx-(six+nxc+xmax)-2):(six+nxc-2);
                        maxincline = atan2(xmin,dst0);
                        kang = (int)(dst0*tan(maxincline)+0.5);
                        q0 = ctx[cents]->get_value_at(six+nxc);
                        for ( incline = -kang; incline <= kang; incline++ ) {
                                qt = q0;
                                //qu = q0;
                                if ( kang > 0 ) {
                                        tang = tan(maxincline/kang*incline);
                                        Rsinang = circR*sin(maxincline/kang*incline);
                                        Rcosang = circR*cos(maxincline/kang*incline);}
                                else {
                                        tang = 0.0;
                                        Rsinang = 0.0;
                                        Rcosang = 1.0;}
                                for ( kim = cents+1; kim < nsegms; kim++ ) {
                                        dst = sqrt((pcoords[cents][0] - pcoords[kim][0])*(pcoords[cents][0] - pcoords[kim][0])+
                                        (pcoords[cents][1] - pcoords[kim][1])*(pcoords[cents][1] - pcoords[kim][1]));
                                        if ( tang >= 0 ) {                     //case 1 and 4.
                                                xl = Rcosang - sqrt(Rcosang*Rcosang-2*dst*Rsinang-dst*dst)+six+nxc;
                                                ixl = (int) xl;
                                                dxl = xl - ixl;
                                                qt += (1.0-dxl)*ctx[kim]->get_value_at(ixl) + dxl*ctx[kim]->get_value_at(ixl+1);}
                                                
                                        if ( tang < 0 ) {
                                                if ( -dst*tang > circR - sqrt(circR*circR-dst*dst)) {                //## case 3
                                                        if ( Rcosang*Rcosang+2*dst*Rsinang-dst*dst <0 )
                                                        printf("Rcosang**2+2*dst*Rsinang-dst**2 less than 0 in curhelicalshiftali_MPI"); 
                                                        xl = -Rcosang + sqrt(Rcosang*Rcosang+2*dst*Rsinang-dst*dst)+six+nxc;
                                                        ixl = (int) xl;
                                                        dxl = xl - ixl;
                                                        if ( ixl < 0 ) printf("kim=%d, cents=%d nsegms=%d incline=%d ixl=%d", kim, cents,nsegms, incline,ixl); 
                                                        qt += (1.0-dxl)*ctx[kim]->get_value_at(ixl) + dxl*ctx[kim]->get_value_at(ixl+1);}
                                                else {                                           //## case 2
                                                        xl = Rcosang - sqrt(Rcosang*Rcosang+2*dst*Rsinang-dst*dst)+six+nxc;
                                                        ixl = (int) xl;
                                                        dxl = xl - ixl;
                                                        qt += (1.0-dxl)*ctx[kim]->get_value_at(ixl) + dxl*ctx[kim]->get_value_at(ixl+1);}}
                                }
                                for ( kim = 0; kim < cents; kim++ ) {
                                        dst = sqrt((pcoords[cents][0] - pcoords[kim][0])*(pcoords[cents][0] - pcoords[kim][0])+
                                        (pcoords[cents][1] - pcoords[kim][1])*(pcoords[cents][1] - pcoords[kim][1]));
                                        if ( tang >= 0 ) {
                                                if ( dst*tang > circR - sqrt(circR*circR-dst*dst)) {             
                                                        xl = -Rcosang + sqrt(Rcosang*Rcosang+2*dst*Rsinang-dst*dst)+six+nxc;
                                                        ixl = (int) xl;
                                                        dxl = xl - ixl;
                                                        qt += (1.0-dxl)*ctx[kim]->get_value_at(ixl) + dxl*ctx[kim]->get_value_at(ixl+1);}
                                                else {                      
                                                        xl = Rcosang - sqrt(Rcosang*Rcosang+2*dst*Rsinang-dst*dst)+six+nxc;
                                                        ixl = (int) xl;
                                                        dxl = xl - ixl;
                                                        qt += (1.0-dxl)*ctx[kim]->get_value_at(ixl) + dxl*ctx[kim]->get_value_at(ixl+1);}}
                                        if ( tang < 0 ) {       
                                                xl = Rcosang - sqrt(Rcosang*Rcosang-2*dst*Rsinang-dst*dst)+six+nxc;
                                                ixl = (int) xl;
                                                dxl = xl - ixl;
                                                qt += (1.0-dxl)*ctx[kim]->get_value_at(ixl) + dxl*ctx[kim]->get_value_at(ixl+1);}
                                }
                                if ( qt > qm ) {
                                        qm = qt;
                                        sib = six;
                                        bang = tang;
                                        CircR = circR;
                                        RSinang = Rsinang;
                                        RCosang = Rcosang;}
                        }
                
                }
        }       
        //printf("\nqm sib bang=%f %f %f", qm, sib, bang);    
    result.push_back((float)sib); 
    result.push_back(bang);
    result.push_back(CircR);
    result.push_back(RSinang);
    result.push_back(RCosang);
    result.push_back(qm);
    return result;    
}
void Util::cyclicshift ( EMData image,
Dict  params 
) [static]

Performs inplace integer cyclic shift as specified by the "dx","dy","dz" parameters on a 3d volume.

Implements the inplace swapping using reversals as descibed in also: http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Intro/Eg01/

Author:
Phani Ivatury
Date:
18-2006
See also:
http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Intro/Eg01/

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

10 20 30 40 50 60 70 80 90 100 ------------ m = 3 (shift left three places)

Reverse the items from 0..m-1 and m..N-1:

30 20 10 100 90 80 70 60 50 40

Now reverse the entire sequence:

40 50 60 70 80 90 100 10 20 30

cycl_shift() in libpy/fundementals.py calls this function

Usage: EMData *im1 = new EMData(); im1->set_size(70,80,85); im1->to_one(); Dict params; params["dx"] = 10;params["dy"] = 10000;params["dz"] = -10; Utils::cyclicshift(im1,params); im1.peak_search(1,1)

Definition at line 5652 of file util_sparx.cpp.

References colreverse(), data, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageFormatException, EMAN::EMData::is_complex(), nx, ny, slicereverse(), and EMAN::EMData::update().

                                                 {

        if (image->is_complex()) throw ImageFormatException("Real image required for IntegerCyclicShift2DProcessor");

        int dx = params["dx"];
        int dy = params["dy"];
        int dz = params["dz"];

        // The reverse trick we're using shifts to the left (a negative shift)
        int nx = image->get_xsize();
        dx %= nx;
        if (dx < 0) dx += nx;
        int ny = image->get_ysize();
        dy %= ny;
        if (dy < 0) dy += ny;
        int nz = image->get_zsize();
        dz %= nz;
        if (dz < 0) dz += nz;

        int mx = -(dx - nx);
        int my = -(dy - ny);
        int mz = -(dz - nz);

        float* data = image->get_data();
        // x-reverses
        if (mx != 0) {
                for (int iz = 0; iz < nz; iz++)
                       for (int iy = 0; iy < ny; iy++) {
                                // reverses for column iy
                                size_t offset = nx*iy + (size_t)nx*ny*iz; // starting location for column iy in slice iz
                                reverse(&data[offset],&data[offset+mx]);
                                reverse(&data[offset+mx],&data[offset+nx]);
                                reverse(&data[offset],&data[offset+nx]);
                        }
        }
        // y-reverses
        if (my != 0) {
                for (int iz = 0; iz < nz; iz++) {
                        size_t offset = (size_t)nx*ny*iz;
                        colreverse(&data[offset], &data[offset + my*nx], nx);
                        colreverse(&data[offset + my*nx], &data[offset + ny*nx], nx);
                        colreverse(&data[offset], &data[offset + ny*nx], nx);
                }
        }
        if (mz != 0) {
                slicereverse(&data[0], &data[(size_t)mz*ny*nx], nx, ny);
                slicereverse(&data[mz*ny*nx], &data[(size_t)nz*ny*nx], nx, ny);
                slicereverse(&data[0], &data[(size_t)nz*ny*nx], nx ,ny);
        }
        image->update();
}
EMData * Util::decimate ( EMData img,
int  x_step,
int  y_step = 1,
int  z_step = 1 
) [static]

Definition at line 5446 of file util_sparx.cpp.

References EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageDimensionException, LOGERR, new_ptr, NullPointerException, nx, ny, old_ptr, EMAN::EMData::set_size(), and EMAN::EMData::update().

{
        /* Exception Handle */
        if (!img) {
                throw NullPointerException("NULL input image");
        }
        /* ============================== */

        // Get the size of the input image
        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        /* ============================== */


        /* Exception Handle */
        if ((x_step-1 > nx/2 || y_step-1 > ny/2 || z_step-1 > nz/2) || (x_step-1)<0 || (y_step-1)<0 || (z_step-1)<0)
        {
                LOGERR("Parameters for decimation cannot exceed the center of the image.");
                throw ImageDimensionException("Parameters for decimation cannot exceed the center of the image.");
        }
        /* ============================== */


        /*    Calculation of the start point */
        int new_st_x=(nx/2)%x_step, new_st_y=(ny/2)%y_step, new_st_z=(nz/2)%z_step;
        /* ============================*/


        /* Calculation of the size of the decimated image */
        int rx=2*(nx/(2*x_step)), ry=2*(ny/(2*y_step)), rz=2*(nz/(2*z_step));
        int r1=int(ceil((nx-(x_step*rx))/(1.f*x_step))), r2=int(ceil((ny-(y_step*ry))/(1.f*y_step)));
        int r3=int(ceil((nz-(z_step*rz))/(1.f*z_step)));
        if(r1>1){r1=1;}
        if(r2>1){r2=1;}
        if(r3>1){r3=1;}
        int new_nx=rx+r1, new_ny=ry+r2, new_nz=rz+r3;
        /* ===========================================*/


        EMData* img2 = new EMData();
        img2->set_size(new_nx,new_ny,new_nz);
        float *new_ptr = img2->get_data();
        float *old_ptr = img->get_data();
        int iptr, jptr, kptr = 0;
        for (int k=new_st_z; k<nz; k+=z_step) {jptr=0;
                for (int j=new_st_y; j<ny; j+=y_step) {iptr=0;
                        for (int i=new_st_x; i<nx; i+=x_step) {
                                new_ptr(iptr,jptr,kptr) = old_ptr(i,j,k);
                        iptr++;}
                jptr++;}
        kptr++;}
        img2->update();
        return img2;
}
std::vector< float > Util::diff_between_matrix_of_3D_parameters_angles ( std::vector< float >  all_params,
std::vector< float >  rotations 
) [static]

Definition at line 23199 of file util_sparx.cpp.

References diff_between_3D_parameters_angles(), and t.

{
        // number of configurations (rotations is a bottom half of a square matrix)
        unsigned conf_count = 2;
        for ( const unsigned t = rotations.size() / 3;  conf_count * (conf_count-1) / 2 < t; ++conf_count );
        // number of projections
        const unsigned projs_count = all_params.size() / 5 / conf_count;

        // decode sets of parameters
        std::vector< std::vector<float> > params(conf_count);
        for (unsigned i = 0; i < conf_count; ++i) {
                params[i].assign( all_params.begin() + i*projs_count*5, all_params.begin() + (i+1)*projs_count*5 );
        }

        std::vector<float> avg(projs_count, 0);

        unsigned ir = 0;
        for (unsigned i = 0; i < conf_count; ++i) {
                for (unsigned j = 0; j < i; ++j) {
                        diff_between_3D_parameters_angles(params[j], params[i], rotations[ir*3], rotations[ir*3+1], rotations[ir*3+2], avg);
                        ++ir;
                }
        }

        for (unsigned k = 0; k < projs_count; ++k) {
                avg[k] /= ( conf_count * (conf_count-1) / 2 );
        }

        return avg;
}
void Util::disorder2 ( double *  x,
double *  y,
int *  key,
int  len 
) [static]

Definition at line 8019 of file util_sparx.cpp.

References ENTERFUNC, and EXITFUNC.

Referenced by voronoi().

{
        ENTERFUNC;
        int k, i;
        for(i=0; i<len; i++) key[i]=i+1;

        for(i = 0; i<len;i++){
                k = rand()%len;
                std::swap(key[k], key[i]);
                std::swap(x[k], x[i]);
                std::swap(y[k], y[i]);
        }
        EXITFUNC;
}
void Util::div_filter ( EMData img,
EMData img1 
) [static]

Definition at line 18010 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageFormatException, img_ptr, EMAN::EMData::is_complex(), NullPointerException, nx, ny, and EMAN::EMData::update().

{
        ENTERFUNC;
        /* Exception Handle */
        if (!img) {
                throw NullPointerException("NULL input image");
        }
        /* ========= img /= img1 ===================== */

        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        size_t size = (size_t)nx*ny*nz;
        float *img_ptr  = img->get_data();
        float *img1_ptr = img1->get_data();
        if(img->is_complex()) {
                for (size_t i=0; i<size; i+=2) {
                        if(img1_ptr[i] > 1.e-10f) {
                                img_ptr[i]   /= img1_ptr[i];
                                img_ptr[i+1] /= img1_ptr[i];
                        } else img_ptr[i] = img_ptr[i+1] = 0.0f;
                }
        } else throw ImageFormatException("Only Fourier image allowed");

        img->update();

        EXITFUNC;
}
void Util::div_img ( EMData img,
EMData img1 
) [static]

Definition at line 17954 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), img_ptr, EMAN::EMData::is_complex(), NullPointerException, nx, ny, and EMAN::EMData::update().

{
        ENTERFUNC;
        /* Exception Handle */
        if (!img) {
                throw NullPointerException("NULL input image");
        }
        /* ========= img /= img1 ===================== */

        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        size_t size = (size_t)nx*ny*nz;
        float *img_ptr  = img->get_data();
        float *img1_ptr = img1->get_data();
        if(img->is_complex()) {
                float  sq2;
                for (size_t i=0; i<size; i+=2) {
                        sq2 = 1.0f/(img1_ptr[i] * img1_ptr[i]   + img1_ptr[i+1] * img1_ptr[i+1]);
                        float tmp    = sq2*(img_ptr[i] * img1_ptr[i]   + img_ptr[i+1] * img1_ptr[i+1]);
                        img_ptr[i+1] = sq2*(img_ptr[i+1] * img1_ptr[i] - img_ptr[i] * img1_ptr[i+1]);
                        img_ptr[i]   = tmp;
                }
        } else {
                for (size_t i=0; i<size; ++i) img_ptr[i] /= img1_ptr[i];
        }
        img->update();

        EXITFUNC;
}
EMData * Util::divn_filter ( EMData img,
EMData img1 
) [static]

Definition at line 17779 of file util_sparx.cpp.

References EMAN::EMData::copy_head(), ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageFormatException, img2_ptr, img_ptr, EMAN::EMData::is_complex(), NullPointerException, nx, ny, and EMAN::EMData::update().

{
        ENTERFUNC;
        /* Exception Handle */
        if (!img) {
                throw NullPointerException("NULL input image");
        }
        /* ========= img /= img1 ===================== */

        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        size_t size = (size_t)nx*ny*nz;
        EMData * img2   = img->copy_head();
        float *img_ptr  = img->get_data();
        float *img1_ptr = img1->get_data();
        float *img2_ptr = img2->get_data();
        if(img->is_complex()) {
                for (size_t i=0; i<size; i+=2) {
                        if(img1_ptr[i] > 1.e-10f) {
                                img2_ptr[i]   = img_ptr[i]  /img1_ptr[i];
                                img2_ptr[i+1] = img_ptr[i+1]/img1_ptr[i];
                        } else img2_ptr[i] = img2_ptr[i+1] = 0.0f;
                }
        } else  throw ImageFormatException("Only Fourier image allowed");

        img2->update();

        EXITFUNC;
        return img2;
}
EMData * Util::divn_img ( EMData img,
EMData img1 
) [static]

Definition at line 17716 of file util_sparx.cpp.

References EMAN::EMData::copy_head(), ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), img2_ptr, img_ptr, EMAN::EMData::is_complex(), EMAN::EMData::is_fftodd(), NullPointerException, nx, ny, EMAN::EMData::set_complex(), EMAN::EMData::set_fftodd(), and EMAN::EMData::update().

{
        ENTERFUNC;
        /* Exception Handle */
        if (!img) {
                throw NullPointerException("NULL input image");
        }
        /* ==============   output = img / img1   ================ */

        int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
        size_t size = (size_t)nx*ny*nz;
        EMData * img2   = img->copy_head();
        float *img_ptr  = img->get_data();
        float *img2_ptr = img2->get_data();
        float *img1_ptr = img1->get_data();
        if(img->is_complex()) {
                float  sq2;
                for (size_t i=0; i<size; i+=2) {
                        sq2 = 1.0f/(img1_ptr[i] * img1_ptr[i]   + img1_ptr[i+1] * img1_ptr[i+1]);
                        img2_ptr[i]   = sq2*(img_ptr[i] * img1_ptr[i]   + img_ptr[i+1] * img1_ptr[i+1]) ;
                        img2_ptr[i+1] = sq2*(img_ptr[i+1] * img1_ptr[i] - img_ptr[i] * img1_ptr[i+1]) ;
                }
                img2->set_complex(true);
                if(img->is_fftodd()) img2->set_fftodd(true); else img2->set_fftodd(false);
        } else {
                for (size_t i=0; i<size; ++i) img2_ptr[i] = img_ptr[i] / img1_ptr[i];
                img2->update();
        }

        EXITFUNC;
        return img2;
}
static float EMAN::Util::eman_copysign ( float  a,
float  b 
) [inline, static]

copy sign of a number.

return a value whose absolute value matches that of 'a', but whose sign matches that of 'b'. If 'a' is a NaN, then a NaN with the sign of 'b' is returned.

It is exactly copysign() on non-Windows system.

Parameters:
[in]aThe first number.
[in]bThe second number.
Returns:
Copy sign of a number.

Definition at line 981 of file util.h.

                {
#ifndef WIN32
                        return copysign(a, b);
#else
                        int flip = -1;
                        if ((a <= 0 && b <= 0) || (a > 0 && b > 0)) {
                                flip = 1;
                        }
                        return a * flip;
#endif
                }
static float EMAN::Util::eman_erfc ( float  x) [inline, static]

complementary error function.

It is exactly erfc() on non-Windows system. On Windows, it tries to simulate erfc().

The erf() function returns the error function of x; defined as erf(x) = 2/sqrt(pi)* integral from 0 to x of exp(-t*t) dt

The erfc() function returns the complementary error function of x, that is 1.0 - erf(x).

Parameters:
[in]xA float number.
Returns:
The complementary error function of x.

Definition at line 1006 of file util.h.

Referenced by EMAN::IterationAverager::finish().

                {
#ifndef WIN32
                        return (float)erfc(x);
#else
                        static double a[] = { -1.26551223, 1.00002368,
                                                                  0.37409196, 0.09678418,
                                                                  -0.18628806, 0.27886807,
                                                                  -1.13520398, 1.48851587,
                                                                  -0.82215223, 0.17087277
                        };

                        double result = 1;
                        double z = fabs(x);
                        if (z > 0) {
                                double t = 1 / (1 + 0.5 * z);
                                double f1 = t * (a[4] + t * (a[5] + t * (a[6] +
                                                        t * (a[7] + t * (a[8] + t * a[9])))));
                                result = t * exp((-z * z) + a[0] + t * (a[1] + t * (a[2] + t * (a[3] + f1))));

                                if (x < 0) {
                                        result = 2 - result;
                                }
                        }
                        return (float)result;
#endif
                }
float Util::ener ( EMData ave,
vector< int >  numr 
) [static]

Definition at line 4643 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), numr, and PI2.

Referenced by ener_tot().

                                              {
        ENTERFUNC;
        long double ener,en;

        int nring = numr.size()/3;
        float *aveptr = ave->get_data();

        ener = 0.0;
        for (int i=1; i<=nring; i++) {
                int numr3i = numr(3,i);
                int np     = numr(2,i)-1;
                float tq = static_cast<float>(PI2*numr(1,i)/numr3i);
                en = tq*(aveptr[np]*aveptr[np]+aveptr[np+1]*aveptr[np+1])*0.5;
                for (int j=np+2; j<np+numr3i-1; j++) en += tq*aveptr[j]*aveptr[j];
                ener += en/numr3i;
        }
        EXITFUNC;
        return static_cast<float>(ener);
}
float Util::ener_tot ( const vector< EMData * > &  data,
vector< int >  numr,
vector< float >  tot 
) [static]

Definition at line 4663 of file util_sparx.cpp.

References ener(), ENTERFUNC, EXITFUNC, numr, and PI2.

                                                                                     {
        ENTERFUNC;
        long double ener, en;
        float arg, cs, si;

        int nima = data.size();
        int nring = numr.size()/3;
        int maxrin = numr(3,nring);

        ener = 0.0;
        for (int i=1; i<=nring; i++) {
                int numr3i = numr(3,i);
                int np     = numr(2,i)-1;
                float tq = static_cast<float>(PI2*numr(1,i)/numr3i);
                float temp1 = 0.0, temp2 = 0.0;
                for (int kk=0; kk<nima; kk++) {
                        float *ptr = data[kk]->get_data();
                        temp1 += ptr[np];
                        temp2 += static_cast<float>(ptr[np+1]*cos(PI2*(tot[kk]-1.0f)/2.0f*numr3i/maxrin));
                }
                en = tq*(temp1*temp1+temp2*temp2)*0.5;
                for (int j=2; j<numr3i; j+=2) {
                        float tempr = 0.0, tempi = 0.0;
                        for (int kk=0; kk<nima; kk++) {
                                float *ptr = data[kk]->get_data();
                                arg = static_cast<float>( PI2*(tot[kk]-1.0)*(j/2)/maxrin );
                                cs = cos(arg);
                                si = sin(arg);
                                tempr += ptr[np + j]*cs - ptr[np + j +1]*si;
                                tempi += ptr[np + j]*si + ptr[np + j +1]*cs;
                        }
                        en += tq*(tempr*tempr+tempi*tempi);
                }
                ener += en/numr3i;
        }
        EXITFUNC;
        return static_cast<float>(ener);
}
void Util::equation_of_plane ( const Vec3f p1,
const Vec3f p2,
const Vec3f p3,
float *  plane 
) [static]

Determine the equation of a plane that intersects 3 points in 3D space.

Required by Symmetry3D::reduce IMPORTANT - does no error checking, if the float pointer is less than 4 in size will get unknown behavior

Parameters:
p1point one
p2point two
p3point three
planea float pointer - should have room for 4 elements which will be A,B,C, and D (in that order) for the equation Ax+By+Cz+D=0
Returns:
there is no explicit return - data is implicitly returned in the plane pointer
Author:
David Woolford
Date:
May 2008

Definition at line 1319 of file util.cpp.

References x, and y.

Referenced by EMAN::Symmetry3D::cache_au_planes().

{
        int x=0,y=1,z=2;
        plane[0] = p1[y]*(p2[z]-p3[z])+p2[y]*(p3[z]-p1[z])+p3[y]*(p1[z]-p2[z]);
        plane[1] = p1[z]*(p2[x]-p3[x])+p2[z]*(p3[x]-p1[x])+p3[z]*(p1[x]-p2[x]);
        plane[2] = p1[x]*(p2[y]-p3[y])+p2[x]*(p3[y]-p1[y])+p3[x]*(p1[y]-p2[y]);
        plane[3] = p1[x]*(p2[y]*p3[z]-p3[y]*p2[z])+p2[x]*(p3[y]*p1[z]-p1[y]*p3[z])+p3[x]*(p1[y]*p2[z]-p2[y]*p1[z]);
        plane[3] = -plane[3];
}
float Util::eval ( char *  images,
EMData img,
vector< int >  S,
int  N,
int  K,
int  size 
) [static]

Definition at line 7367 of file util_sparx.cpp.

References EMAN::EMData::get_data(), and EMAN::EMData::read_image().

{
        int j,d;
        EMData * e = new EMData();
        float *eptr, *imgptr;
        imgptr = img->get_data();
        float SSE = 0.f;
        for (j = 0 ; j < N ; j++) {
                e->read_image(images,S[j]);
                eptr = e->get_data();
                for (d = 0; d < size; d++) {
                        SSE += ((eptr[d] - imgptr[d])*(eptr[d] - imgptr[d]));}
                }
        delete e;
        return SSE;
}
vector< float > Util::even_angles ( float  delta,
float  t1 = 0,
float  t2 = 90,
float  p1 = 0,
float  p2 = 359.999 
) [static]

Compute a vector containing quasi-evenly spaced Euler angles.

The order of angles in the vector is phi, theta, psi.

Parameters:
[in]deltaDelta theta (spacing in theta).
[in]t1Starting (min) value of theta in degrees, default = 0.
[in]t2Ending (max) value of theta in degrees, default = 90.
[in]p1Starting (min) value of phi in degrees, default = 0.
[in]p2Ending (max) value of phi in degrees, default = 359.9.
Returns:
Vector of angles as a flat list of phi_0, theta_0, psi_0, ..., phi_N, theta_N, psi_N.

Definition at line 681 of file util_sparx.cpp.

References angles, dgr_to_rad, phi, and theta.

Referenced by ali3d_d().

{
        vector<float> angles;
        float psi = 0.0;
        if ((0.0 == t1 && 0.0 == t2)||(t1 >= t2)) {
                t1 =  0.0f;
                t2 = 90.0f;
        }
        if ((0.0 == p1 && 0.0 == p2)||(p1 >= p2)) {
                p1 =   0.0f;
                p2 = 359.9f;
        }
        bool skip = ((t1 < 90.0) && (90.0 == t2) && (0.0 == p1) && (p2 > 180.0));
        for (float theta = t1; theta <= t2; theta += delta) {
                float detphi;
                int lt;
                if ((0.0 == theta)||(180.0 == theta)) {
                        detphi = 360.0f;
                        lt = 1;
                } else {
                        detphi = delta/sin(theta*static_cast<float>(dgr_to_rad));
                        lt = int((p2 - p1)/detphi)-1;
                        if (lt < 1) lt = 1;
                        detphi = (p2 - p1)/lt;
                }
                for (int i = 0; i < lt; i++) {
                        float phi = p1 + i*detphi;
                        if (skip&&(90.0 == theta)&&(phi > 180.0)) continue;
                        angles.push_back(phi);
                        angles.push_back(theta);
                        angles.push_back(psi);
                }
        }
        return angles;
}
bool Util::explore ( vector< vector< int * > > &  Parts,
int *  dimClasses,
int  nParts,
int  K,
int  T,
int  partref,
int *  curintx,
int  size_curintx,
int *  next,
int  size_next,
int  depth 
) [static]

Each class in Parts has its dummy variable set to 0 or 1.

Only consider those with its dummy variable set to 1 (the 'active' ones) First element of each class is its original index, second is the dummy variable slot.

Definition at line 22111 of file util_sparx.cpp.

References k_means_cont_table_().

Referenced by initial_prune().

                                                                                                                                                                                {


        if (size_next <= T) return 0;

        // take the intx of next and cur
        int* curintx2(0);
        int nintx = Util::k_means_cont_table_(curintx, next+2, curintx2, size_curintx, size_next,0);
        if (nintx <= T) return 0;

        int old_depth=depth;
        if (depth == partref) depth = depth + 1; // we skip classes in partref
        if (depth == nParts &&  old_depth>0) return 1;

        // have not yet reached a leaf, and current weight is still greather than T, so keep on going.

        curintx2 = new int[nintx]; // put the intersection set in here
        Util::k_means_cont_table_(curintx,next+2,curintx2, size_curintx, size_next,1);

        // if (old_depth > 0) {delete[] curintx;} // don't delete when depth == 0 because curintx in that case is a pointer into Parts

        // we now consider each of the classes in partition (depth+1) in turn
        bool gt_thresh;
        int num_classes = Parts[depth].size(); // (TO DO) have to figure out how many classes partition (depth) has since some may have being removed from before iterations

        for (int i=0; i < num_classes; i++){
                if (Parts[depth][i][1] < 1) continue; // class is not active so move on
                size_next = dimClasses[depth*K + Parts[depth][i][0] ]-2;
                gt_thresh = explore(Parts,dimClasses, nParts, K, T, partref, curintx2,nintx, Parts[depth][i], size_next, depth+1);
                if (gt_thresh) { delete[] curintx2; return 1; }
        }
        delete[] curintx2;
        return 0;
}
void Util::explore2 ( int *  argParts,
int *  Indices,
int *  dimClasses,
int  nParts,
int  K,
int  T,
int *  curintx,
int  size_curintx,
int *  next,
int  size_next,
int  depth,
int  J,
int *  matchlist,
int *  costlist,
int *  curbranch 
) [static]

Definition at line 21945 of file util_sparx.cpp.

References k_means_cont_table_().

Referenced by search2().

                                                                                                                                                                                                                   {

// depth is the level which is going to be explored in the current iteration
        int* curintx2(0);
        int nintx = size_curintx;
        
        
        // 2. take the intx of next and cur. Prune if <= T
        if (depth >0){
                nintx = Util::k_means_cont_table_(curintx,next, curintx2, size_curintx, size_next,0);
                if (nintx <= T) return; //prune!
        }

        // 1. we're at a leaf with weight > T, so determine if there is any empty space. If so, put it in. If not, determine if current cost is larger than any of the cost in matchlist, if so, replace the  smallest one in matchlist
        if (depth == (nParts-1)) {
                
                int replace = 0;
                int ind_smallest = -1;
                int smallest_cost = -1;
                
                for (int jit = 0; jit < J; jit++){
                        if (*(costlist+jit) < nintx){
                                replace = 1;
                                if (ind_smallest == -1) {ind_smallest = jit; smallest_cost = *(costlist+jit);}
                                if (*(costlist+jit) < smallest_cost) {ind_smallest = jit; smallest_cost = *(costlist+jit);}
                        }       
                }
                
                if (replace > 0){
                        // replace the smallest cost in matchlist with the current stuff
                        *(costlist + ind_smallest) = nintx;
                        for (int xit = 0; xit < nParts; xit++)
                                *(matchlist + ind_smallest*nParts + xit) = *(curbranch+xit);
                                
                }
                
                return; 
        }
        

        // 3. have not yet reached a leaf, and current weight is still greather than T, so keep on going.

        if (depth > 0){
                curintx2 = new int[nintx]; // put the intersection set in here
                Util::k_means_cont_table_(curintx,next,curintx2, size_curintx, size_next,1);
        }

        if (depth == 0){
                // set curintx2 to curintx
                curintx2 = new int[size_curintx];
                for (int cp = 0; cp < size_curintx; cp++) *(curintx2+cp) = *(curintx+cp);
        }


        // recursion (non-leaf case)
        depth=depth+1;
        // we now consider each of the classes in partition depth and recurse upon each of them
        for (int i=0; i < K; i++){

                if (*(argParts + Indices[depth*K+i] + 1) < 1) continue; // class is not active so move on
                size_next = (*(dimClasses + depth*K+i ))-2;
                if (size_next <= T) continue;
                *(curbranch+depth) = i;
                Util::explore2(argParts,Indices, dimClasses, nParts, K, T, curintx2, nintx, argParts + Indices[depth*K+i] + 2, size_next, depth,J, matchlist,
                        costlist, curbranch);
                
        }

        delete[] curintx2;
}
complex< float > Util::extractpoint2 ( int  nx,
int  ny,
float  nuxnew,
float  nuynew,
EMData fimage,
Util::KaiserBessel kb 
) [static]

Definition at line 1859 of file util_sparx.cpp.

References EMAN::EMData::cmplx(), EMAN::Util::KaiserBessel::i0win_tab(), ImageDimensionException, and round().

                                                                                                                   {

        int nxreal = nx - 2;
        if (nxreal != ny)
                throw ImageDimensionException("extractpoint requires ny == nx");
        int nhalf = nxreal/2;
        bool flip = (nuxnew < 0.f);
        if (flip) {
                nuxnew *= -1;
                nuynew *= -1;
        }
        if (nuynew >= nhalf-0.5)  {
                nuynew -= nxreal;
        } else if (nuynew < -nhalf-0.5) {
                nuynew += nxreal;
        }

        // put (xnew,ynew) on a grid.  The indices will be wrong for
        // the Fourier elements in the image, but the grid sizing will
        // be correct.
        int ixn = int(Util::round(nuxnew));
        int iyn = int(Util::round(nuynew));

        // set up some temporary weighting arrays
        static float wy[7];
        static float wx[7];

        float iynn = nuynew - iyn;
        wy[0] = kb.i0win_tab(iynn+3);
        wy[1] = kb.i0win_tab(iynn+2);
        wy[2] = kb.i0win_tab(iynn+1);
        wy[3] = kb.i0win_tab(iynn);
        wy[4] = kb.i0win_tab(iynn-1);
        wy[5] = kb.i0win_tab(iynn-2);
        wy[6] = kb.i0win_tab(iynn-3);

        float ixnn = nuxnew - ixn;
        wx[0] = kb.i0win_tab(ixnn+3);
        wx[1] = kb.i0win_tab(ixnn+2);
        wx[2] = kb.i0win_tab(ixnn+1);
        wx[3] = kb.i0win_tab(ixnn);
        wx[4] = kb.i0win_tab(ixnn-1);
        wx[5] = kb.i0win_tab(ixnn-2);
        wx[6] = kb.i0win_tab(ixnn-3);

        float wsum = (wx[0]+wx[1]+wx[2]+wx[3]+wx[4]+wx[5]+wx[6])*(wy[0]+wy[1]+wy[2]+wy[3]+wy[4]+wy[5]+wy[6]);

        complex<float> result(0.f,0.f);
        if ((ixn >= 3) && (ixn <= nhalf-3) && (iyn >= -nhalf+3) && (iyn <= nhalf-4)) {
                // (xin,yin) not within window border from the edge
                for (int iy = 0; iy < 7; iy++) {
                        int iyp = iyn + iy - 3 ;
                        for (int ix = 0; ix < 7; ix++) {
                                int ixp = ixn + ix - 3;
                                float w = wx[ix]*wy[iy];
                                complex<float> val = fimage->cmplx(ixp,iyp);
                                result += val*w;
                        }
                }
        } else {
                // points that "stick out"
                for (int iy = 0; iy < 7; iy++) {
                        int iyp = iyn + iy - 3;
                        for (int ix = 0; ix < 7; ix++) {
                                int ixp = ixn + ix - 3;
                                bool mirror = false;
                                int ixt = ixp, iyt = iyp;
                                if (ixt < 0) {
                                        ixt = -ixt;
                                        iyt = -iyt;
                                        mirror = !mirror;
                                }
                                if (ixt > nhalf) {
                                        ixt = nxreal - ixt;
                                        iyt = -iyt;
                                        mirror = !mirror;
                                }
                                if (iyt > nhalf-1)  iyt -= nxreal;
                                if (iyt < -nhalf)   iyt += nxreal;
                                float w = wx[ix]*wy[iy];
                                complex<float> val = fimage->cmplx(ixt,iyt);
                                if (mirror)  result += conj(val)*w;
                                else         result += val*w;
                        }
                }
        }
        if (flip)  result = conj(result)/wsum;
        else result /= wsum;
        return result;
}
float Util::fast_acos ( const float &  f) [static]

Returns an approximate of acos(x) using a cached table and linear interpolation tolerates values very slightly outside the -1 - 1 range (returning 0)

Parameters:
[in]xargument to acos(x)
Returns:
(float)acos(x)

Definition at line 817 of file util.cpp.

Referenced by angle_err_ri().

                                    {
if (f>=1.0) return 0.0;
if (f<=-1.0) return M_PI;

static float *mem = (float *)malloc(sizeof(float)*2001);
static bool needinit=true;


if (needinit) {
        needinit=false;
        for (int i=0; i<=2000; i++) mem[i]=(float)acos(i/1000.0-1.0);
}
float f2=f*1000.0f+1000.0f;

int g=(int)(f2+.5);

return mem[g];

// This version interpolates, but is slower
/*int g=(int)f2;
f2-=g;
return mem[g+1]*f2+mem[g]*(1.0-f2);*/
}
float Util::fast_exp ( const float &  f) [static]

Returns an approximate of exp(x) using a cached table uses actual exp(x) outside the cached range.

Parameters:
[in]fargument to exp(f)
Returns:
(float)exp(x)

Definition at line 801 of file util.cpp.

Referenced by EMAN::FourierInserter3DMode5::insert_pixel(), EMAN::FourierInserter3DMode3::insert_pixel(), EMAN::FourierInserter3DMode2::insert_pixel(), EMAN::WienerFourierReconstructor::pixel_at(), and EMAN::FourierReconstructor::pixel_at().

                                   {
static float *mem = (float *)malloc(sizeof(float)*1000);
static bool needinit=true;

if (needinit) {
        needinit=false;
        for (int i=0; i<1000; i++) mem[i]=(float)exp(-i/50.0);
}
if (f>0 || f<-19.98) return exp(f);
int g=(int)(-f*50.0+0.5);

return mem[g];
}
static int EMAN::Util::fast_floor ( float  x) [inline, static]
void Util::fftc_d ( double *  br,
double *  bi,
int  ln,
int  ks 
) [static]

Definition at line 2792 of file util_sparx.cpp.

References abs, bi, br, sgn(), status, t, and tab1.

Referenced by fftr_d().

{
        double rni,sgn,tr1,tr2,ti1,ti2;
        double cc,c,ss,s,t,x2,x3,x4,x5;
        int    b3,b4,b5,b6,b7,b56;
        int    n, k, l, j, i, ix0, ix1, status=0;

        const double tab1[] = {
                9.58737990959775e-5,
                1.91747597310703e-4,
                3.83495187571395e-4,
                7.66990318742704e-4,
                1.53398018628476e-3,
                3.06795676296598e-3,
                6.13588464915449e-3,
                1.22715382857199e-2,
                2.45412285229123e-2,
                4.90676743274181e-2,
                9.80171403295604e-2,
                1.95090322016128e-1,
                3.82683432365090e-1,
                7.07106781186546e-1,
                1.00000000000000,
        };

        n=(int)pow(2.0f,ln);

        k=abs(ks);
        l=16-ln;
        b3=n*k;
        b6=b3;
        b7=k;
        if (ks > 0) {
                sgn=1.0f;
        } else {
                sgn=-1.0f;
                rni=1.0f/(float)(n);
                j=1;
                for (i=1; i<=n; i++) {
                        br(j)=br(j)*rni;
                        bi(j)=bi(j)*rni;
                        j=j+k;
                }
        }

L12:
   b6=b6/2;
   b5=b6;
   b4=2*b6;
   b56=b5-b6;

L14:
   tr1=br(b5+1);
   ti1=bi(b5+1);
   tr2=br(b56+1);
   ti2=bi(b56+1);

   br(b5+1)=tr2-tr1;
   bi(b5+1)=ti2-ti1;
   br(b56+1)=tr1+tr2;
   bi(b56+1)=ti1+ti2;

   b5=b5+b4;
   b56=b5-b6;
   if ( b5 <= b3 )  goto  L14;
   if ( b6 == b7 )  goto  L20;

   b4=b7;
   cc=2.0f*pow(tab1(l),2);
   c=1.0f-cc;
   l++;
   ss=sgn*tab1(l);
   s=ss;

L16:
   b5=b6+b4;
   b4=2*b6;
   b56=b5-b6;

L18:
   tr1=br(b5+1);
   ti1=bi(b5+1);
   tr2=br(b56+1);
   ti2=bi(b56+1);
   br(b5+1)=c*(tr2-tr1)-s*(ti2-ti1);
   bi(b5+1)=s*(tr2-tr1)+c*(ti2-ti1);
   br(b56+1)=tr1+tr2;
   bi(b56+1)=ti1+ti2;

   b5=b5+b4;
   b56=b5-b6;
   if ( b5 <= b3 )  goto  L18;
   b4=b5-b6;
   b5=b4-b3;
   c=-c;
   b4=b6-b5;
   if ( b5 < b4 )  goto  L16;
   b4=b4+b7;
   if ( b4 >= b5 ) goto  L12;

   t=c-cc*c-ss*s;
   s=s+ss*c-cc*s;
   c=t;
   goto  L16;

L20:
   ix0=b3/2;
   b3=b3-b7;
   b4=0;
   b5=0;
   b6=ix0;
   ix1=0;
   if (b6 == b7) goto EXIT;

L22:
   b4=b3-b4;
   b5=b3-b5;
   x2=br(b4+1);
   x3=br(b5+1);
   x4=bi(b4+1);
   x5=bi(b5+1);
   br(b4+1)=x3;
   br(b5+1)=x2;
   bi(b4+1)=x5;
   bi(b5+1)=x4;
   if(b6 < b4)  goto  L22;

L24:
   b4=b4+b7;
   b5=b6+b5;
   x2=br(b4+1);
   x3=br(b5+1);
   x4=bi(b4+1);
   x5=bi(b5+1);
   br(b4+1)=x3;
   br(b5+1)=x2;
   bi(b4+1)=x5;
   bi(b5+1)=x4;
   ix0=b6;

L26:
   ix0=ix0/2;
   ix1=ix1-ix0;
   if( ix1 >= 0)  goto L26;

   ix0=2*ix0;
   b4=b4+b7;
   ix1=ix1+ix0;
   b5=ix1;
   if ( b5 >= b4)  goto  L22;
   if ( b4 < b6)   goto  L24;

EXIT:
   status = 0;
}
void Util::fftc_q ( float *  br,
float *  bi,
int  ln,
int  ks 
) [static]

Definition at line 2949 of file util_sparx.cpp.

References abs, bi, br, sgn(), status, t, and tab1.

Referenced by fftr_q().

{
        //  dimension  br(1),bi(1)

        int b3,b4,b5,b6,b7,b56;
        int n, k, l, j, i, ix0, ix1;
        float rni, tr1, ti1, tr2, ti2, cc, c, ss, s, t, x2, x3, x4, x5, sgn;
        int status=0;

        const float tab1[] = {
                9.58737990959775e-5f,
                1.91747597310703e-4f,
                3.83495187571395e-4f,
                7.66990318742704e-4f,
                1.53398018628476e-3f,
                3.06795676296598e-3f,
                6.13588464915449e-3f,
                1.22715382857199e-2f,
                2.45412285229123e-2f,
                4.90676743274181e-2f,
                9.80171403295604e-2f,
                1.95090322016128e-1f,
                3.82683432365090e-1f,
                7.07106781186546e-1f,
                1.00000000000000f,
        };

        n=(int)pow(2.0f,ln);

        k=abs(ks);
        l=16-ln;
        b3=n*k;
        b6=b3;
        b7=k;
        if( ks > 0 ) {
                sgn=1.0f;
        } else {
                sgn=-1.0f;
                rni=1.0f/(float)n;
                j=1;
                for (i=1; i<=n; i++) {
                        br(j)=br(j)*rni;
                        bi(j)=bi(j)*rni;
                        j=j+k;
                }
        }
L12:
   b6=b6/2;
   b5=b6;
   b4=2*b6;
   b56=b5-b6;
L14:
   tr1=br(b5+1);
   ti1=bi(b5+1);

   tr2=br(b56+1);
   ti2=bi(b56+1);

   br(b5+1)=tr2-tr1;
   bi(b5+1)=ti2-ti1;
   br(b56+1)=tr1+tr2;
   bi(b56+1)=ti1+ti2;

   b5=b5+b4;
   b56=b5-b6;
   if ( b5 <= b3 )  goto  L14;
   if ( b6 == b7 )  goto  L20;

   b4=b7;
   cc=2.0f*pow(tab1(l),2);
   c=1.0f-cc;
   l++;
   ss=sgn*tab1(l);
   s=ss;
L16:
   b5=b6+b4;
   b4=2*b6;
   b56=b5-b6;
L18:
   tr1=br(b5+1);
   ti1=bi(b5+1);
   tr2=br(b56+1);
   ti2=bi(b56+1);
   br(b5+1)=c*(tr2-tr1)-s*(ti2-ti1);
   bi(b5+1)=s*(tr2-tr1)+c*(ti2-ti1);
   br(b56+1)=tr1+tr2;
   bi(b56+1)=ti1+ti2;

   b5=b5+b4;
   b56=b5-b6;
   if(b5 <= b3)  goto L18;
   b4=b5-b6;
   b5=b4-b3;
   c=-c;
   b4=b6-b5;
   if(b5 < b4)  goto  L16;
   b4=b4+b7;
   if(b4 >= b5) goto  L12;

   t=c-cc*c-ss*s;
   s=s+ss*c-cc*s;
   c=t;
   goto  L16;
L20:
   ix0=b3/2;
   b3=b3-b7;
   b4=0;
   b5=0;
   b6=ix0;
   ix1=0;
   if ( b6 == b7) goto EXIT;
L22:
   b4=b3-b4;
   b5=b3-b5;
   x2=br(b4+1);
   x3=br(b5+1);
   x4=bi(b4+1);
   x5=bi(b5+1);
   br(b4+1)=x3;
   br(b5+1)=x2;
   bi(b4+1)=x5;
   bi(b5+1)=x4;
   if (b6 < b4) goto  L22;
L24:
   b4=b4+b7;
   b5=b6+b5;
   x2=br(b4+1);
   x3=br(b5+1);
   x4=bi(b4+1);
   x5=bi(b5+1);
   br(b4+1)=x3;
   br(b5+1)=x2;
   bi(b4+1)=x5;
   bi(b5+1)=x4;
   ix0=b6;
L26:
   ix0=ix0/2;
   ix1=ix1-ix0;
   if(ix1 >= 0)  goto  L26;

   ix0=2*ix0;
   b4=b4+b7;
   ix1=ix1+ix0;
   b5=ix1;
   if (b5 >= b4)  goto  L22;
   if (b4 < b6)   goto  L24;
EXIT:
   status = 0;
}
void Util::fftr_d ( double *  xcmplx,
int  nv 
) [static]

Definition at line 3181 of file util_sparx.cpp.

References abs, fftc_d(), t, tab1, and xcmplx.

Referenced by Crosrng_e(), Crosrng_ew(), Crosrng_ms(), Crosrng_ms_delta(), Crosrng_msg(), Crosrng_msg_m(), Crosrng_msg_s(), Crosrng_ns(), Crosrng_psi(), Crosrng_rand_ms(), and Crosrng_sm_psi().

{
        // double precision  x(2,1)
        int    i1, i2,  nu, inv, nu1, n, isub, n2, i;
        double tr1,tr2,ti1,ti2,tr,ti;
        double cc,c,ss,s,t;
        const double tab1[] = {
                9.58737990959775e-5,
                1.91747597310703e-4,
                3.83495187571395e-4,
                7.66990318742704e-4,
                1.53398018628476e-3,
                3.06795676296598e-3,
                6.13588464915449e-3,
                1.22715382857199e-2,
                2.45412285229123e-2,
                4.90676743274181e-2,
                9.80171403295604e-2,
                1.95090322016128e-1,
                3.82683432365090e-1,
                7.07106781186546e-1,
                1.00000000000000,
        };

        nu=abs(nv);
        inv=nv/nu;
        nu1=nu-1;
        n=(int)pow(2.0f,nu1);
        isub=16-nu1;
        ss=-tab1(isub);
        cc=-2.0*pow(tab1(isub-1),2);
        c=1.0f;
        s=0.0f;
        n2=n/2;

        if ( inv > 0 ) {
                fftc_d(&xcmplx(1,1),&xcmplx(2,1),nu1,2);
                tr=xcmplx(1,1);
                ti=xcmplx(2,1);
                xcmplx(1,1)=tr+ti;
                xcmplx(2,1)=tr-ti;
                for (i=1;i<=n2;i++) {
                        i1=i+1;
                        i2=n-i+1;
                        tr1=xcmplx(1,i1);
                        tr2=xcmplx(1,i2);
                        ti1=xcmplx(2,i1);
                        ti2=xcmplx(2,i2);
                        t=(cc*c-ss*s)+c;
                        s=(cc*s+ss*c)+s;
                        c=t;
                        xcmplx(1,i1)=0.5*((tr1+tr2)+(ti1+ti2)*c-(tr1-tr2)*s);
                        xcmplx(1,i2)=0.5*((tr1+tr2)-(ti1+ti2)*c+(tr1-tr2)*s);
                        xcmplx(2,i1)=0.5*((ti1-ti2)-(ti1+ti2)*s-(tr1-tr2)*c);
                        xcmplx(2,i2)=0.5*(-(ti1-ti2)-(ti1+ti2)*s-(tr1-tr2)*c);
                }
        } else {
                tr=xcmplx(1,1);
                ti=xcmplx(2,1);
                xcmplx(1,1)=0.5*(tr+ti);
                xcmplx(2,1)=0.5*(tr-ti);
                for (i=1; i<=n2; i++) {
                        i1=i+1;
                        i2=n-i+1;
                        tr1=xcmplx(1,i1);
                        tr2=xcmplx(1,i2);
                        ti1=xcmplx(2,i1);
                        ti2=xcmplx(2,i2);
                        t=(cc*c-ss*s)+c;
                        s=(cc*s+ss*c)+s;
                        c=t;
                        xcmplx(1,i1)=0.5*((tr1+tr2)-(tr1-tr2)*s-(ti1+ti2)*c);
                        xcmplx(1,i2)=0.5*((tr1+tr2)+(tr1-tr2)*s+(ti1+ti2)*c);
                        xcmplx(2,i1)=0.5*((ti1-ti2)+(tr1-tr2)*c-(ti1+ti2)*s);
                        xcmplx(2,i2)=0.5*(-(ti1-ti2)+(tr1-tr2)*c-(ti1+ti2)*s);
                }
                fftc_d(&xcmplx(1,1),&xcmplx(2,1),nu1,-2);
        }
}
void Util::fftr_q ( float *  xcmplx,
int  nv 
) [static]

Definition at line 3099 of file util_sparx.cpp.

References abs, fftc_q(), t, tab1, and xcmplx.

Referenced by Crosrng_msg_vec(), Frngs(), and Frngs_inv().

{
   // dimension xcmplx(2,1); xcmplx(1,i) --- real, xcmplx(2,i) --- imaginary

        int nu, inv, nu1, n, isub, n2, i1, i2, i;
        float ss, cc, c, s, tr, ti, tr1, tr2, ti1, ti2, t;

        const float tab1[] = {
                9.58737990959775e-5f,
                1.91747597310703e-4f,
                3.83495187571395e-4f,
                7.66990318742704e-4f,
                1.53398018628476e-3f,
                3.06795676296598e-3f,
                6.13588464915449e-3f,
                1.22715382857199e-2f,
                2.45412285229123e-2f,
                4.90676743274181e-2f,
                9.80171403295604e-2f,
                1.95090322016128e-1f,
                3.82683432365090e-1f,
                7.07106781186546e-1f,
                1.00000000000000f,
        };

        nu=abs(nv);
        inv=nv/nu;
        nu1=nu-1;
        n=(int)pow(2.f,nu1);
        isub=16-nu1;

        ss=-tab1(isub);
        cc=-2.0f*pow(tab1(isub-1),2.f);
        c=1.0f;
        s=0.0f;
        n2=n/2;
        if ( inv > 0) {
                fftc_q(&xcmplx(1,1),&xcmplx(2,1),nu1,2);
                tr=xcmplx(1,1);
                ti=xcmplx(2,1);
                xcmplx(1,1)=tr+ti;
                xcmplx(2,1)=tr-ti;
                for (i=1;i<=n2;i++) {
                        i1=i+1;
                        i2=n-i+1;
                        tr1=xcmplx(1,i1);
                        tr2=xcmplx(1,i2);
                        ti1=xcmplx(2,i1);
                        ti2=xcmplx(2,i2);
                        t=(cc*c-ss*s)+c;
                        s=(cc*s+ss*c)+s;
                        c=t;
                        xcmplx(1,i1)=0.5f*((tr1+tr2)+(ti1+ti2)*c-(tr1-tr2)*s);
                        xcmplx(1,i2)=0.5f*((tr1+tr2)-(ti1+ti2)*c+(tr1-tr2)*s);
                        xcmplx(2,i1)=0.5f*((ti1-ti2)-(ti1+ti2)*s-(tr1-tr2)*c);
                        xcmplx(2,i2)=0.5f*(-(ti1-ti2)-(ti1+ti2)*s-(tr1-tr2)*c);
                }
        } else {
                tr=xcmplx(1,1);
                ti=xcmplx(2,1);
                xcmplx(1,1)=0.5f*(tr+ti);
                xcmplx(2,1)=0.5f*(tr-ti);
                for (i=1; i<=n2; i++) {
                        i1=i+1;
                        i2=n-i+1;
                        tr1=xcmplx(1,i1);
                        tr2=xcmplx(1,i2);
                        ti1=xcmplx(2,i1);
                        ti2=xcmplx(2,i2);
                        t=(cc*c-ss*s)+c;
                        s=(cc*s+ss*c)+s;
                        c=t;
                        xcmplx(1,i1)=0.5f*((tr1+tr2)-(tr1-tr2)*s-(ti1+ti2)*c);
                        xcmplx(1,i2)=0.5f*((tr1+tr2)+(tr1-tr2)*s+(ti1+ti2)*c);
                        xcmplx(2,i1)=0.5f*((ti1-ti2)+(tr1-tr2)*c-(ti1+ti2)*s);
                        xcmplx(2,i2)=0.5f*(-(ti1-ti2)+(tr1-tr2)*c-(ti1+ti2)*s);
                }
                fftc_q(&xcmplx(1,1),&xcmplx(2,1),nu1,-2);
        }
}
int Util::file_lock_wait ( FILE *  file) [static]

lock a file.

If the lock fails, wait for 1 second; then try again. Repleat this wait-try for a maxinum of 5 times unless the lock succeeds.

Parameters:
fileThe file to be locked.
Returns:
0 if lock succeeds. 1 if fails.

Definition at line 171 of file util.cpp.

References LOGERR, and NullPointerException.

{
#ifdef WIN32
        return 1;
#else

        if (!file) {
                throw NullPointerException("Tried to lock NULL file");
        }

        int fdes = fileno(file);

        struct flock fl;
        fl.l_type = F_WRLCK;
        fl.l_whence = SEEK_SET;
        fl.l_start = 0;
        fl.l_len = 0;
#ifdef WIN32
        fl.l_pid = _getpid();
#else
        fl.l_pid = getpid();
#endif

#if defined __sgi
        fl.l_sysid = getpid();
#endif

        int err = 0;
        if (fcntl(fdes, F_SETLKW, &fl) == -1) {
                LOGERR("file locking error! NFS problem?");

                int i = 0;
                for (i = 0; i < 5; i++) {
                        if (fcntl(fdes, F_SETLKW, &fl) != -1) {
                                break;
                        }
                        else {
#ifdef WIN32
                                Sleep(1000);
#else
                                sleep(1);
#endif

                        }
                }
                if (i == 5) {
                        LOGERR("Fatal file locking error");
                        err = 1;
                }
        }

        return err;
#endif
}
void Util::find_max ( const float *  data,
size_t  nitems,
float *  p_max_val,
int *  p_max_index = 0 
) [static]

Find the maximum value and (optional) its index in an array.

Parameters:
[in]datadata array.
[in]nitemsnumber of items in the data array.
[out]p_max_valpointer to the maximum value.
[out]p_max_indexpointer to index of the maximum value.

Definition at line 848 of file util.cpp.

References Assert, max, and NullPointerException.

Referenced by EMAN::RotateTranslateFlipAlignerPawel::align(), EMAN::RotateTranslateAlignerPawel::align(), EMAN::RotationalAlignerIterative::align(), EMAN::RotatePrecenterAligner::align(), and EMAN::RotationalAligner::align_180_ambiguous().

{
        Assert(nitems > 0);

        if (!data || !max_val || !max_index) {
                throw NullPointerException("data/max_val/max_index");
        }
        float max = -FLT_MAX;
        int m = 0;

        for (size_t i = 0; i < nitems; i++) {
                if (data[i] > max) {
                        max = data[i];
                        m = (int)i;
                }
        }

        *max_val = (float)max;

        if (max_index) {
                *max_index = m;
        }
}
void Util::find_min_and_max ( const float *  data,
size_t  nitems,
float *  p_max_val,
float *  p_min_val,
int *  p_max_index = 0,
int *  p_min_index = 0 
) [static]

Find the maximum value and (optional) its index, minimum value and (optional) its index in an array.

Parameters:
[in]datadata array.
[in]nitemsnumber of items in the data array.
[out]p_max_valpointer to the maximum value.
[out]p_min_valpointer to the minimum value.
[out]p_max_indexpointer to index of the maximum value.
[out]p_min_indexpointer to index of the minimum value.

Definition at line 872 of file util.cpp.

References Assert, max, min, and NullPointerException.

{
        Assert(nitems > 0);

        if (!data || !max_val || !min_val || !max_index || !min_index) {
                throw NullPointerException("data/max_val/min_val/max_index/min_index");
        }
        float max = -FLT_MAX;
        float min = FLT_MAX;
        int max_i = 0;
        int min_i = 0;

        for (size_t i = 0; i < nitems; i++) {
                if (data[i] > max) {
                        max = data[i];
                        max_i = (int)i;
                }
                if (data[i] < min) {
                        min = data[i];
                        min_i = (int)i;
                }
        }

        *max_val = max;
        *min_val = min;

        if (min_index) {
                *min_index = min_i;
        }

        if (max_index) {
                *max_index = max_i;
        }

}
void Util::flip23 ( double *  x,
double *  y,
double *  z,
int *  key,
int  k,
int  len 
) [static]

Definition at line 8056 of file util_sparx.cpp.

References ENTERFUNC, and EXITFUNC.

Referenced by voronoi().

{
        ENTERFUNC;
        int i = k;
        while( i == k )  i = rand()%len;
        std::swap(key[i], key[k]);
        std::swap(x[i], x[k]);
        std::swap(y[i], y[k]);
        std::swap(z[i], z[k]);
        EXITFUNC;
}
void Util::flip_complex_phase ( float *  data,
size_t  n 
) [static]

flip the phase of a complex data array.

Parameters:
datacomplex data array.
narray size.

Definition at line 116 of file util.cpp.

References Assert, and NullPointerException.

{
        Assert(n > 0);

        if (!data) {
                throw NullPointerException("pixel data array");
        }

        for (size_t i = 0; i < n; i += 2) {
                data[i + 1] *= -1;
        }
}
void Util::flip_image ( float *  data,
size_t  nx,
size_t  ny 
) [static]

Vertically flip the data of a 2D real image.

Parameters:
dataData array of the 2D real image.
nxImage Width.
nyImage Height.

Definition at line 251 of file util.cpp.

References Assert, NullPointerException, and nx.

{
        if (!data) {
                throw NullPointerException("image data array");
        }
        Assert(nx > 0);
        Assert(ny > 0);

        float *buf = new float[nx];
        size_t row_size = nx * sizeof(float);

        for (size_t i = 0; i < ny / 2; i++) {
                memcpy(buf, &data[i * nx], row_size);
                memcpy(&data[i * nx], &data[(ny - 1 - i) * nx], row_size);
                memcpy(&data[(ny - 1 - i) * nx], buf, row_size);
        }

        if( buf )
        {
                delete[]buf;
                buf = 0;
        }
}
void Util::Frngs ( EMData circ,
vector< int >  numr 
) [static]
void Util::Frngs_inv ( EMData circ,
vector< int >  numr 
) [static]

This function conducts the Single Precision Inverse Fourier Transform for a set of rings.

Definition at line 3282 of file util_sparx.cpp.

References circ, fftr_q(), EMAN::EMData::get_data(), log(), and numr.

                                                   {
        int nring = numr.size()/3;
        float *circ = circp->get_data();
        int i, l;
        for (i=1; i<=nring;i++) {

#ifdef _WIN32
                l = (int)( log((float)numr(3,i))/log(2.0f) );
#else
                l=(int)(log2(numr(3,i)));
#endif  //_WIN32

                fftr_q(&circ(numr(2,i)),-l);
        }
}
static int EMAN::Util::generatesubmax ( int *  argParts,
int *  Indices,
int *  dimClasses,
int  nParts,
int  K,
int  T,
int  n_guesses,
int  LARGEST_CLASS 
) [static]

make an intelligent "guess" at the largest weight of all possible feasible matches.

we make "n_guesses" guesses and return the largest one. the largest weight of all feasible matches is guaranteed to be larger than or equal to the returned guess.

EMData * Util::get_biggest_cluster ( EMData mg) [static]

Definition at line 21040 of file util_sparx.cpp.

References Assert, find_group(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), nx, ny, EMAN::EMData::set_size(), and EMAN::EMData::to_zero().

{
        int nx = mg->get_xsize();
        int ny = mg->get_ysize();
        int nz = mg->get_zsize();

        EMData* visited = new EMData();
        visited->set_size( nx, ny, nz );
        visited->to_zero();
        int grpid = 0;
        int maxgrp = 0;
        int maxsize = 0;
        for( int iz=0; iz < nz; ++iz ) {
                for( int iy=0; iy < ny; ++iy ) {
                        for( int ix=0; ix < nx; ++ix ) {
                                if( (*mg)(ix, iy, iz)==0.0 ) continue;

                                if( (*visited)(ix, iy, iz) > 0.0 ) {
                                        // visited before, must be in other group.
                                        continue;
                                }

                                grpid++;
                                int grpsize = find_group( ix, iy, iz, grpid, mg, visited );
                                if( grpsize > maxsize ) {
                                        maxsize = grpsize;
                                        maxgrp = grpid;
                                }
                        }
                }
        }

        Assert( maxgrp > 0 );

        int npoint = 0;
        EMData* result = new EMData();
        result->set_size( nx, ny, nz );
        result->to_zero();

        for( int iz=0; iz < nz; ++iz ) {
                for( int iy=0; iy < ny; ++iy ) {
                        for( int ix=0; ix < nx; ++ix ) {
                                if( (*visited)(ix, iy, iz)==maxgrp ) {
                                        (*result)(ix,iy,iz) = 1.0;
                                        npoint++;
                                }
                        }
                }
        }

        Assert( npoint==maxsize );
        delete visited;
        return result;

}
string Util::get_filename_ext ( const string &  filename) [static]

Get a filename's extension.

Parameters:
[in]filenameA given filename.
Returns:
The filename's extension, or empty string if the file has no extension.

Definition at line 526 of file util.cpp.

Referenced by EMAN::EMUtil::fast_get_image_type(), EMAN::EMUtil::get_image_type(), and EMAN::EMUtil::is_valid_filename().

{
    if (filename == "") {
        return "";
    }

        string result = "";
        const char *ext = strrchr(filename.c_str(), '.');
        if (ext) {
                ext++;
                result = string(ext);
        }
        return result;
}
float Util::get_frand ( double  low,
double  high 
) [static]

Get a float random number between low and high, [low, high)

Parameters:
[in]lowThe lower bound of the random number.
[in]highThe upper bound of the random number.
Returns:
The random number between low and high.

Definition at line 741 of file util.cpp.

References EMAN::Randnum::get_frand().

{
        Randnum* randnum = Randnum::Instance();
        return randnum->get_frand(lo, hi);
}
float Util::get_frand ( int  low,
int  high 
) [static]

Get a float random number between low and high, [low, high)

Parameters:
[in]lowThe lower bound of the random number.
[in]highThe upper bound of the random number.
Returns:
The random number between low and high.

Definition at line 730 of file util.cpp.

Referenced by EMAN::OrientationGenerator::add_orientation(), ali2d_ccf_list(), EMAN::RandomOrientationGenerator::gen_orientations(), main(), move_points(), EMAN::KmeansSegmentProcessor::process(), EMAN::TestImageFourierNoiseProfile::process_inplace(), EMAN::TestImageFourierNoiseGaussian::process_inplace(), EMAN::LowpassRandomPhaseProcessor::process_inplace(), and EMAN::PointArray::set_from_density_map().

{
        return get_frand((float)lo, (float)hi);
}
float Util::get_frand ( float  low,
float  high 
) [static]

Get a float random number between low and high, [low, high)

Parameters:
[in]lowThe lower bound of the random number.
[in]highThe upper bound of the random number.
Returns:
The random number between low and high.

Definition at line 735 of file util.cpp.

References EMAN::Randnum::get_frand().

{
        Randnum* randnum = Randnum::Instance();
        return randnum->get_frand(lo, hi);
}
float Util::get_gauss_rand ( float  mean,
float  sigma 
) [static]

Get a Gaussian random number.

Parameters:
[in]meanThe gaussian mean
[in]sigmaThe gaussian sigma
Returns:
the gaussian random number.

Definition at line 842 of file util.cpp.

References EMAN::Randnum::get_gauss_rand().

Referenced by EMAN::EmanOrientationGenerator::gen_orientations(), main(), and EMAN::MaskNoiseProcessor::process_dist_pixel().

{
        Randnum* randnum = Randnum::Instance();
        return randnum->get_gauss_rand(mean, sigma);
}
int Util::get_irand ( int  low,
int  high 
) [static]

Get an integer random number between low and high, [low, high].

Parameters:
[in]lowThe lower bound of the random number.
[in]highThe upper bound of the random number.
Returns:
The random number between low and high.

Definition at line 724 of file util.cpp.

References EMAN::Randnum::get_irand().

Referenced by EMAN::KMeansAnalyzer::analyze(), cluster_pairwise(), move_points(), EMAN::KMeansAnalyzer::reseed(), shc(), shc_multipeaks(), shuffled_range(), and EMAN::PointArray::sim_minstep_seq().

{
        Randnum* randnum = Randnum::Instance();
        return randnum->get_irand(lo, hi);
}
string Util::get_line_from_string ( char **  str) [static]

Extract a single line from a multi-line string.

The line delimiter is '
'. The multi-line string moves forward one line. If it is the last line, move to the end of the string.

Parameters:
[in,out]strA multiple-line string.
Returns:
A single line.

Definition at line 301 of file util.cpp.

References NullPointerException.

Referenced by EMAN::XplorIO::is_valid(), and EMAN::SitusIO::is_valid().

{
        if (!slines || !(*slines)) {
                throw NullPointerException("Null string");
        }

        string result = "";
        char *str = *slines;

        while (*str != '\n' && *str != '\0') {
                result.push_back(*str);
                str++;
        }
        if (*str != '\0') {
                str++;
        }
        *slines = str;

        return result;
}
static float EMAN::Util::get_max ( float  f1,
float  f2,
float  f3 
) [inline, static]

Get the maximum of 3 numbers.

Parameters:
[in]f1The first number.
[in]f2The second number.
[in]f3The third number.
Returns:
The maximum of 3 numbers.

Definition at line 849 of file util.h.

                {
                        if (f1 >= f2 && f1 >= f3) {
                                return f1;
                        }
                        if (f2 >= f1 && f2 >= f3) {
                                return f2;
                        }
                        return f3;
                }
static float EMAN::Util::get_max ( float  f1,
float  f2 
) [inline, static]

Get the maximum of 2 numbers.

Parameters:
[in]f1The first number.
[in]f2The second number.
Returns:
The maximum of 2 numbers.

Definition at line 838 of file util.h.

                {
                        return (f1 < f2 ? f2 : f1);
                }
static float EMAN::Util::get_max ( float  f1,
float  f2,
float  f3,
float  f4 
) [inline, static]

Get the maximum of 4 numbers.

Parameters:
[in]f1The first number.
[in]f2The second number.
[in]f3The third number.
[in]f4The fourth number.
Returns:
The maximum of 4 numbers.

Definition at line 867 of file util.h.

                {
                        float m = f1;
                        if (f2 > m) {
                                m = f2;
                        }
                        if (f3 > m) {
                                m = f3;
                        }
                        if (f4 > m) {
                                m = f4;
                        }
                        return m;
                }
static int EMAN::Util::get_min ( int  f1,
int  f2,
int  f3 
) [inline, static]

Get the minimum of 3 numbers.

Parameters:
[in]f1The first number.
[in]f2The second number.
[in]f3The third number.
Returns:
The minimum of 3 numbers.

Definition at line 772 of file util.h.

                {
                        if (f1 <= f2 && f1 <= f3) {
                                return f1;
                        }
                        if (f2 <= f1 && f2 <= f3) {
                                return f2;
                        }
                        return f3;
                }
static float EMAN::Util::get_min ( float  f1,
float  f2 
) [inline, static]

Get the minimum of 2 numbers.

Parameters:
[in]f1The first number.
[in]f2The second number.
Returns:
The minimum of 2 numbers.

Definition at line 788 of file util.h.

                {
                        return (f1 < f2 ? f1 : f2);
                }
static float EMAN::Util::get_min ( float  f1,
float  f2,
float  f3 
) [inline, static]

Get the minimum of 3 numbers.

Parameters:
[in]f1The first number.
[in]f2The second number.
[in]f3The third number.
Returns:
The minimum of 3 numbers.

Definition at line 799 of file util.h.

                {
                        if (f1 <= f2 && f1 <= f3) {
                                return f1;
                        }
                        if (f2 <= f1 && f2 <= f3) {
                                return f2;
                        }
                        return f3;
                }
static float EMAN::Util::get_min ( float  f1,
float  f2,
float  f3,
float  f4 
) [inline, static]

Get the minimum of 4 numbers.

Parameters:
[in]f1The first number.
[in]f2The second number.
[in]f3The third number.
[in]f4The fourth number.
Returns:
The minimum of 4 numbers.

Definition at line 818 of file util.h.

                {
                        float m = f1;
                        if (f2 < m) {
                                m = f2;
                        }
                        if (f3 < m) {
                                m = f3;
                        }
                        if (f4 < m) {
                                m = f4;
                        }
                        return m;
                }
static int EMAN::Util::get_min ( int  f1,
int  f2 
) [inline, static]

Get the minimum of 2 numbers.

Parameters:
[in]f1The first number.
[in]f2The second number.
Returns:
The minimum of 2 numbers.

Definition at line 761 of file util.h.

                {
                        return (f1 < f2 ? f1 : f2);
                }
float Util::get_pixel_conv_new ( int  nx,
int  ny,
int  nz,
float  delx,
float  dely,
float  delz,
float *  data,
Util::KaiserBessel kb 
) [static]

Definition at line 949 of file util_sparx.cpp.

References EMAN::Util::KaiserBessel::get_window_size(), EMAN::Util::KaiserBessel::i0win_tab(), nx, ny, restrict1(), and round().

Referenced by EMAN::EMData::helicise_grid(), Polar2Dmi(), EMAN::EMData::rot_scale_conv_new(), and EMAN::EMData::rot_scale_conv_new_3D().

                                                                                                                             {
        int K = kb.get_window_size();
        int kbmin = -K/2;
        int kbmax = -kbmin;
        int kbc = kbmax+1;

        float pixel =0.0f;
        float w=0.0f;

        delx = restrict1(delx, nx);
        int inxold = int(round(delx));
        if ( ny < 2 ) {  //1D
                float tablex1 = kb.i0win_tab(delx-inxold+3);
                float tablex2 = kb.i0win_tab(delx-inxold+2);
                float tablex3 = kb.i0win_tab(delx-inxold+1);
                float tablex4 = kb.i0win_tab(delx-inxold);
                float tablex5 = kb.i0win_tab(delx-inxold-1);
                float tablex6 = kb.i0win_tab(delx-inxold-2);
                float tablex7 = kb.i0win_tab(delx-inxold-3);

                int x1, x2, x3, x4, x5, x6, x7;

                if ( inxold <= kbc || inxold >=nx-kbc-2 )  {
                        x1 = (inxold-3+nx)%nx;
                        x2 = (inxold-2+nx)%nx;
                        x3 = (inxold-1+nx)%nx;
                        x4 = (inxold  +nx)%nx;
                        x5 = (inxold+1+nx)%nx;
                        x6 = (inxold+2+nx)%nx;
                        x7 = (inxold+3+nx)%nx;
                } else {
                        x1 = inxold-3;
                        x2 = inxold-2;
                        x3 = inxold-1;
                        x4 = inxold;
                        x5 = inxold+1;
                        x6 = inxold+2;
                        x7 = inxold+3;
                }

                pixel = data[x1]*tablex1 + data[x2]*tablex2 + data[x3]*tablex3 +
                        data[x4]*tablex4 + data[x5]*tablex5 + data[x6]*tablex6 +
                        data[x7]*tablex7 ;

                w = tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7;
        } else if ( nz < 2 ) {  // 2D
                dely = restrict1(dely, ny);
                int inyold = int(round(dely));
                float tablex1 = kb.i0win_tab(delx-inxold+3);
                float tablex2 = kb.i0win_tab(delx-inxold+2);
                float tablex3 = kb.i0win_tab(delx-inxold+1);
                float tablex4 = kb.i0win_tab(delx-inxold);
                float tablex5 = kb.i0win_tab(delx-inxold-1);
                float tablex6 = kb.i0win_tab(delx-inxold-2);
                float tablex7 = kb.i0win_tab(delx-inxold-3);

                float tabley1 = kb.i0win_tab(dely-inyold+3);
                float tabley2 = kb.i0win_tab(dely-inyold+2);
                float tabley3 = kb.i0win_tab(dely-inyold+1);
                float tabley4 = kb.i0win_tab(dely-inyold);
                float tabley5 = kb.i0win_tab(dely-inyold-1);
                float tabley6 = kb.i0win_tab(dely-inyold-2);
                float tabley7 = kb.i0win_tab(dely-inyold-3);

                int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7;

                if ( inxold <= kbc || inxold >=nx-kbc-2 || inyold <= kbc || inyold >=ny-kbc-2 )  {
                        x1 = (inxold-3+nx)%nx;
                        x2 = (inxold-2+nx)%nx;
                        x3 = (inxold-1+nx)%nx;
                        x4 = (inxold  +nx)%nx;
                        x5 = (inxold+1+nx)%nx;
                        x6 = (inxold+2+nx)%nx;
                        x7 = (inxold+3+nx)%nx;

                        y1 = ((inyold-3+ny)%ny)*nx;
                        y2 = ((inyold-2+ny)%ny)*nx;
                        y3 = ((inyold-1+ny)%ny)*nx;
                        y4 = ((inyold  +ny)%ny)*nx;
                        y5 = ((inyold+1+ny)%ny)*nx;
                        y6 = ((inyold+2+ny)%ny)*nx;
                        y7 = ((inyold+3+ny)%ny)*nx;
                } else {
                        x1 = inxold-3;
                        x2 = inxold-2;
                        x3 = inxold-1;
                        x4 = inxold;
                        x5 = inxold+1;
                        x6 = inxold+2;
                        x7 = inxold+3;

                        y1 = (inyold-3)*nx;
                        y2 = (inyold-2)*nx;
                        y3 = (inyold-1)*nx;
                        y4 = inyold*nx;
                        y5 = (inyold+1)*nx;
                        y6 = (inyold+2)*nx;
                        y7 = (inyold+3)*nx;
                }

                pixel    = ( data[x1+y1]*tablex1 + data[x2+y1]*tablex2 + data[x3+y1]*tablex3 +
                             data[x4+y1]*tablex4 + data[x5+y1]*tablex5 + data[x6+y1]*tablex6 +
                             data[x7+y1]*tablex7 ) * tabley1 +
                           ( data[x1+y2]*tablex1 + data[x2+y2]*tablex2 + data[x3+y2]*tablex3 +
                             data[x4+y2]*tablex4 + data[x5+y2]*tablex5 + data[x6+y2]*tablex6 +
                             data[x7+y2]*tablex7 ) * tabley2 +
                           ( data[x1+y3]*tablex1 + data[x2+y3]*tablex2 + data[x3+y3]*tablex3 +
                             data[x4+y3]*tablex4 + data[x5+y3]*tablex5 + data[x6+y3]*tablex6 +
                             data[x7+y3]*tablex7 ) * tabley3 +
                           ( data[x1+y4]*tablex1 + data[x2+y4]*tablex2 + data[x3+y4]*tablex3 +
                             data[x4+y4]*tablex4 + data[x5+y4]*tablex5 + data[x6+y4]*tablex6 +
                             data[x7+y4]*tablex7 ) * tabley4 +
                           ( data[x1+y5]*tablex1 + data[x2+y5]*tablex2 + data[x3+y5]*tablex3 +
                             data[x4+y5]*tablex4 + data[x5+y5]*tablex5 + data[x6+y5]*tablex6 +
                             data[x7+y5]*tablex7 ) * tabley5 +
                           ( data[x1+y6]*tablex1 + data[x2+y6]*tablex2 + data[x3+y6]*tablex3 +
                             data[x4+y6]*tablex4 + data[x5+y6]*tablex5 + data[x6+y6]*tablex6 +
                             data[x7+y6]*tablex7 ) * tabley6 +
                           ( data[x1+y7]*tablex1 + data[x2+y7]*tablex2 + data[x3+y7]*tablex3 +
                             data[x4+y7]*tablex4 + data[x5+y7]*tablex5 + data[x6+y7]*tablex6 +
                             data[x7+y7]*tablex7 ) * tabley7;

                w = (tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7) *
                    (tabley1+tabley2+tabley3+tabley4+tabley5+tabley6+tabley7);
        } else {  //  3D
                dely = restrict1(dely, ny);
                int inyold = int(Util::round(dely));
                delz = restrict1(delz, nz);
                int inzold = int(Util::round(delz));

                float tablex1 = kb.i0win_tab(delx-inxold+3);
                float tablex2 = kb.i0win_tab(delx-inxold+2);
                float tablex3 = kb.i0win_tab(delx-inxold+1);
                float tablex4 = kb.i0win_tab(delx-inxold);
                float tablex5 = kb.i0win_tab(delx-inxold-1);
                float tablex6 = kb.i0win_tab(delx-inxold-2);
                float tablex7 = kb.i0win_tab(delx-inxold-3);

                float tabley1 = kb.i0win_tab(dely-inyold+3);
                float tabley2 = kb.i0win_tab(dely-inyold+2);
                float tabley3 = kb.i0win_tab(dely-inyold+1);
                float tabley4 = kb.i0win_tab(dely-inyold);
                float tabley5 = kb.i0win_tab(dely-inyold-1);
                float tabley6 = kb.i0win_tab(dely-inyold-2);
                float tabley7 = kb.i0win_tab(dely-inyold-3);

                float tablez1 = kb.i0win_tab(delz-inzold+3);
                float tablez2 = kb.i0win_tab(delz-inzold+2);
                float tablez3 = kb.i0win_tab(delz-inzold+1);
                float tablez4 = kb.i0win_tab(delz-inzold);
                float tablez5 = kb.i0win_tab(delz-inzold-1);
                float tablez6 = kb.i0win_tab(delz-inzold-2);
                float tablez7 = kb.i0win_tab(delz-inzold-3);

                int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7, z1, z2, z3, z4, z5, z6, z7;

                if ( inxold <= kbc || inxold >=nx-kbc-2 || inyold <= kbc || inyold >=ny-kbc-2 || inzold <= kbc || inzold >= nz-kbc-2 )  {
                        x1 = (inxold-3+nx)%nx;
                        x2 = (inxold-2+nx)%nx;
                        x3 = (inxold-1+nx)%nx;
                        x4 = (inxold  +nx)%nx;
                        x5 = (inxold+1+nx)%nx;
                        x6 = (inxold+2+nx)%nx;
                        x7 = (inxold+3+nx)%nx;

                        y1 = ((inyold-3+ny)%ny)*nx;
                        y2 = ((inyold-2+ny)%ny)*nx;
                        y3 = ((inyold-1+ny)%ny)*nx;
                        y4 = ((inyold  +ny)%ny)*nx;
                        y5 = ((inyold+1+ny)%ny)*nx;
                        y6 = ((inyold+2+ny)%ny)*nx;
                        y7 = ((inyold+3+ny)%ny)*nx;

                        z1 = ((inzold-3+nz)%nz)*nx*ny;
                        z2 = ((inzold-2+nz)%nz)*nx*ny;
                        z3 = ((inzold-1+nz)%nz)*nx*ny;
                        z4 = ((inzold  +nz)%nz)*nx*ny;
                        z5 = ((inzold+1+nz)%nz)*nx*ny;
                        z6 = ((inzold+2+nz)%nz)*nx*ny;
                        z7 = ((inzold+3+nz)%nz)*nx*ny;
                } else {
                        x1 = inxold-3;
                        x2 = inxold-2;
                        x3 = inxold-1;
                        x4 = inxold;
                        x5 = inxold+1;
                        x6 = inxold+2;
                        x7 = inxold+3;

                        y1 = (inyold-3)*nx;
                        y2 = (inyold-2)*nx;
                        y3 = (inyold-1)*nx;
                        y4 = inyold*nx;
                        y5 = (inyold+1)*nx;
                        y6 = (inyold+2)*nx;
                        y7 = (inyold+3)*nx;

                        z1 = (inzold-3)*nx*ny;
                        z2 = (inzold-2)*nx*ny;
                        z3 = (inzold-1)*nx*ny;
                        z4 = inzold*nx*ny;
                        z5 = (inzold+1)*nx*ny;
                        z6 = (inzold+2)*nx*ny;
                        z7 = (inzold+3)*nx*ny;
                }

                pixel  = ( ( data[x1+y1+z1]*tablex1 + data[x2+y1+z1]*tablex2 + data[x3+y1+z1]*tablex3 +
                             data[x4+y1+z1]*tablex4 + data[x5+y1+z1]*tablex5 + data[x6+y1+z1]*tablex6 +
                             data[x7+y1+z1]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z1]*tablex1 + data[x2+y2+z1]*tablex2 + data[x3+y2+z1]*tablex3 +
                             data[x4+y2+z1]*tablex4 + data[x5+y2+z1]*tablex5 + data[x6+y2+z1]*tablex6 +
                             data[x7+y2+z1]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z1]*tablex1 + data[x2+y3+z1]*tablex2 + data[x3+y3+z1]*tablex3 +
                             data[x4+y3+z1]*tablex4 + data[x5+y3+z1]*tablex5 + data[x6+y3+z1]*tablex6 +
                             data[x7+y3+z1]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z1]*tablex1 + data[x2+y4+z1]*tablex2 + data[x3+y4+z1]*tablex3 +
                             data[x4+y4+z1]*tablex4 + data[x5+y4+z1]*tablex5 + data[x6+y4+z1]*tablex6 +
                             data[x7+y4+z1]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z1]*tablex1 + data[x2+y5+z1]*tablex2 + data[x3+y5+z1]*tablex3 +
                             data[x4+y5+z1]*tablex4 + data[x5+y5+z1]*tablex5 + data[x6+y5+z1]*tablex6 +
                             data[x7+y5+z1]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z1]*tablex1 + data[x2+y6+z1]*tablex2 + data[x3+y6+z1]*tablex3 +
                             data[x4+y6+z1]*tablex4 + data[x5+y6+z1]*tablex5 + data[x6+y6+z1]*tablex6 +
                             data[x7+y6+z1]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z1]*tablex1 + data[x2+y7+z1]*tablex2 + data[x3+y7+z1]*tablex3 +
                             data[x4+y7+z1]*tablex4 + data[x5+y7+z1]*tablex5 + data[x6+y7+z1]*tablex6 +
                             data[x7+y7+z1]*tablex7 ) * tabley7 ) *tablez1 +
                         ( ( data[x1+y1+z2]*tablex1 + data[x2+y1+z2]*tablex2 + data[x3+y1+z2]*tablex3 +
                             data[x4+y1+z2]*tablex4 + data[x5+y1+z2]*tablex5 + data[x6+y1+z2]*tablex6 +
                             data[x7+y1+z2]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z2]*tablex1 + data[x2+y2+z2]*tablex2 + data[x3+y2+z2]*tablex3 +
                             data[x4+y2+z2]*tablex4 + data[x5+y2+z2]*tablex5 + data[x6+y2+z2]*tablex6 +
                             data[x7+y2+z2]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z2]*tablex1 + data[x2+y3+z2]*tablex2 + data[x3+y3+z2]*tablex3 +
                             data[x4+y3+z2]*tablex4 + data[x5+y3+z2]*tablex5 + data[x6+y3+z2]*tablex6 +
                             data[x7+y3+z2]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z2]*tablex1 + data[x2+y4+z2]*tablex2 + data[x3+y4+z2]*tablex3 +
                             data[x4+y4+z2]*tablex4 + data[x5+y4+z2]*tablex5 + data[x6+y4+z2]*tablex6 +
                             data[x7+y4+z2]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z2]*tablex1 + data[x2+y5+z2]*tablex2 + data[x3+y5+z2]*tablex3 +
                             data[x4+y5+z2]*tablex4 + data[x5+y5+z2]*tablex5 + data[x6+y5+z2]*tablex6 +
                             data[x7+y5+z2]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z2]*tablex1 + data[x2+y6+z2]*tablex2 + data[x3+y6+z2]*tablex3 +
                             data[x4+y6+z2]*tablex4 + data[x5+y6+z2]*tablex5 + data[x6+y6+z2]*tablex6 +
                             data[x7+y6+z2]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z2]*tablex1 + data[x2+y7+z2]*tablex2 + data[x3+y7+z2]*tablex3 +
                             data[x4+y7+z2]*tablex4 + data[x5+y7+z2]*tablex5 + data[x6+y7+z2]*tablex6 +
                             data[x7+y7+z2]*tablex7 ) * tabley7 ) *tablez2 +
                         ( ( data[x1+y1+z3]*tablex1 + data[x2+y1+z3]*tablex2 + data[x3+y1+z3]*tablex3 +
                             data[x4+y1+z3]*tablex4 + data[x5+y1+z3]*tablex5 + data[x6+y1+z3]*tablex6 +
                             data[x7+y1+z3]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z3]*tablex1 + data[x2+y2+z3]*tablex2 + data[x3+y2+z3]*tablex3 +
                             data[x4+y2+z3]*tablex4 + data[x5+y2+z3]*tablex5 + data[x6+y2+z3]*tablex6 +
                             data[x7+y2+z3]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z3]*tablex1 + data[x2+y3+z3]*tablex2 + data[x3+y3+z3]*tablex3 +
                             data[x4+y3+z3]*tablex4 + data[x5+y3+z3]*tablex5 + data[x6+y3+z3]*tablex6 +
                             data[x7+y3+z3]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z3]*tablex1 + data[x2+y4+z3]*tablex2 + data[x3+y4+z3]*tablex3 +
                             data[x4+y4+z3]*tablex4 + data[x5+y4+z3]*tablex5 + data[x6+y4+z3]*tablex6 +
                             data[x7+y4+z3]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z3]*tablex1 + data[x2+y5+z3]*tablex2 + data[x3+y5+z3]*tablex3 +
                             data[x4+y5+z3]*tablex4 + data[x5+y5+z3]*tablex5 + data[x6+y5+z3]*tablex6 +
                             data[x7+y5+z3]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z3]*tablex1 + data[x2+y6+z3]*tablex2 + data[x3+y6+z3]*tablex3 +
                             data[x4+y6+z3]*tablex4 + data[x5+y6+z3]*tablex5 + data[x6+y6+z3]*tablex6 +
                             data[x7+y6+z3]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z3]*tablex1 + data[x2+y7+z3]*tablex2 + data[x3+y7+z3]*tablex3 +
                             data[x4+y7+z3]*tablex4 + data[x5+y7+z3]*tablex5 + data[x6+y7+z3]*tablex6 +
                             data[x7+y7+z3]*tablex7 ) * tabley7 ) *tablez3 +
                         ( ( data[x1+y1+z4]*tablex1 + data[x2+y1+z4]*tablex2 + data[x3+y1+z4]*tablex3 +
                             data[x4+y1+z4]*tablex4 + data[x5+y1+z4]*tablex5 + data[x6+y1+z4]*tablex6 +
                             data[x7+y1+z4]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z4]*tablex1 + data[x2+y2+z4]*tablex2 + data[x3+y2+z4]*tablex3 +
                             data[x4+y2+z4]*tablex4 + data[x5+y2+z4]*tablex5 + data[x6+y2+z4]*tablex6 +
                             data[x7+y2+z4]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z4]*tablex1 + data[x2+y3+z4]*tablex2 + data[x3+y3+z4]*tablex3 +
                             data[x4+y3+z4]*tablex4 + data[x5+y3+z4]*tablex5 + data[x6+y3+z4]*tablex6 +
                             data[x7+y3+z4]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z4]*tablex1 + data[x2+y4+z4]*tablex2 + data[x3+y4+z4]*tablex3 +
                             data[x4+y4+z4]*tablex4 + data[x5+y4+z4]*tablex5 + data[x6+y4+z4]*tablex6 +
                             data[x7+y4+z4]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z4]*tablex1 + data[x2+y5+z4]*tablex2 + data[x3+y5+z4]*tablex3 +
                             data[x4+y5+z4]*tablex4 + data[x5+y5+z4]*tablex5 + data[x6+y5+z4]*tablex6 +
                             data[x7+y5+z4]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z4]*tablex1 + data[x2+y6+z4]*tablex2 + data[x3+y6+z4]*tablex3 +
                             data[x4+y6+z4]*tablex4 + data[x5+y6+z4]*tablex5 + data[x6+y6+z4]*tablex6 +
                             data[x7+y6+z4]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z4]*tablex1 + data[x2+y7+z4]*tablex2 + data[x3+y7+z4]*tablex3 +
                             data[x4+y7+z4]*tablex4 + data[x5+y7+z4]*tablex5 + data[x6+y7+z4]*tablex6 +
                             data[x7+y7+z4]*tablex7 ) * tabley7 ) *tablez4 +
                         ( ( data[x1+y1+z5]*tablex1 + data[x2+y1+z5]*tablex2 + data[x3+y1+z5]*tablex3 +
                             data[x4+y1+z5]*tablex4 + data[x5+y1+z5]*tablex5 + data[x6+y1+z5]*tablex6 +
                             data[x7+y1+z5]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z5]*tablex1 + data[x2+y2+z5]*tablex2 + data[x3+y2+z5]*tablex3 +
                             data[x4+y2+z5]*tablex4 + data[x5+y2+z5]*tablex5 + data[x6+y2+z5]*tablex6 +
                             data[x7+y2+z5]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z5]*tablex1 + data[x2+y3+z5]*tablex2 + data[x3+y3+z5]*tablex3 +
                             data[x4+y3+z5]*tablex4 + data[x5+y3+z5]*tablex5 + data[x6+y3+z5]*tablex6 +
                             data[x7+y3+z5]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z5]*tablex1 + data[x2+y4+z5]*tablex2 + data[x3+y4+z5]*tablex3 +
                             data[x4+y4+z5]*tablex4 + data[x5+y4+z5]*tablex5 + data[x6+y4+z5]*tablex6 +
                             data[x7+y4+z5]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z5]*tablex1 + data[x2+y5+z5]*tablex2 + data[x3+y5+z5]*tablex3 +
                             data[x4+y5+z5]*tablex4 + data[x5+y5+z5]*tablex5 + data[x6+y5+z5]*tablex6 +
                             data[x7+y5+z5]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z5]*tablex1 + data[x2+y6+z5]*tablex2 + data[x3+y6+z5]*tablex3 +
                             data[x4+y6+z5]*tablex4 + data[x5+y6+z5]*tablex5 + data[x6+y6+z5]*tablex6 +
                             data[x7+y6+z5]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z5]*tablex1 + data[x2+y7+z5]*tablex2 + data[x3+y7+z5]*tablex3 +
                             data[x4+y7+z5]*tablex4 + data[x5+y7+z5]*tablex5 + data[x6+y7+z5]*tablex6 +
                             data[x7+y7+z5]*tablex7 ) * tabley7 ) *tablez5 +
                         ( ( data[x1+y1+z6]*tablex1 + data[x2+y1+z6]*tablex2 + data[x3+y1+z6]*tablex3 +
                             data[x4+y1+z6]*tablex4 + data[x5+y1+z6]*tablex5 + data[x6+y1+z6]*tablex6 +
                             data[x7+y1+z6]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z6]*tablex1 + data[x2+y2+z6]*tablex2 + data[x3+y2+z6]*tablex3 +
                             data[x4+y2+z6]*tablex4 + data[x5+y2+z6]*tablex5 + data[x6+y2+z6]*tablex6 +
                             data[x7+y2+z6]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z6]*tablex1 + data[x2+y3+z6]*tablex2 + data[x3+y3+z6]*tablex3 +
                             data[x4+y3+z6]*tablex4 + data[x5+y3+z6]*tablex5 + data[x6+y3+z6]*tablex6 +
                             data[x7+y3+z6]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z6]*tablex1 + data[x2+y4+z6]*tablex2 + data[x3+y4+z6]*tablex3 +
                             data[x4+y4+z6]*tablex4 + data[x5+y4+z6]*tablex5 + data[x6+y4+z6]*tablex6 +
                             data[x7+y4+z6]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z6]*tablex1 + data[x2+y5+z6]*tablex2 + data[x3+y5+z6]*tablex3 +
                             data[x4+y5+z6]*tablex4 + data[x5+y5+z6]*tablex5 + data[x6+y5+z6]*tablex6 +
                             data[x7+y5+z6]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z6]*tablex1 + data[x2+y6+z6]*tablex2 + data[x3+y6+z6]*tablex3 +
                             data[x4+y6+z6]*tablex4 + data[x5+y6+z6]*tablex5 + data[x6+y6+z6]*tablex6 +
                             data[x7+y6+z6]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z6]*tablex1 + data[x2+y7+z6]*tablex2 + data[x3+y7+z6]*tablex3 +
                             data[x4+y7+z6]*tablex4 + data[x5+y7+z6]*tablex5 + data[x6+y7+z6]*tablex6 +
                             data[x7+y7+z6]*tablex7 ) * tabley7 ) *tablez6 +
                         ( ( data[x1+y1+z7]*tablex1 + data[x2+y1+z7]*tablex2 + data[x3+y1+z7]*tablex3 +
                             data[x4+y1+z7]*tablex4 + data[x5+y1+z7]*tablex5 + data[x6+y1+z7]*tablex6 +
                             data[x7+y1+z7]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z7]*tablex1 + data[x2+y2+z7]*tablex2 + data[x3+y2+z7]*tablex3 +
                             data[x4+y2+z7]*tablex4 + data[x5+y2+z7]*tablex5 + data[x6+y2+z7]*tablex6 +
                             data[x7+y2+z7]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z7]*tablex1 + data[x2+y3+z7]*tablex2 + data[x3+y3+z7]*tablex3 +
                             data[x4+y3+z7]*tablex4 + data[x5+y3+z7]*tablex5 + data[x6+y3+z7]*tablex6 +
                             data[x7+y3+z7]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z7]*tablex1 + data[x2+y4+z7]*tablex2 + data[x3+y4+z7]*tablex3 +
                             data[x4+y4+z7]*tablex4 + data[x5+y4+z7]*tablex5 + data[x6+y4+z7]*tablex6 +
                             data[x7+y4+z7]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z7]*tablex1 + data[x2+y5+z7]*tablex2 + data[x3+y5+z7]*tablex3 +
                             data[x4+y5+z7]*tablex4 + data[x5+y5+z7]*tablex5 + data[x6+y5+z7]*tablex6 +
                             data[x7+y5+z7]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z7]*tablex1 + data[x2+y6+z7]*tablex2 + data[x3+y6+z7]*tablex3 +
                             data[x4+y6+z7]*tablex4 + data[x5+y6+z7]*tablex5 + data[x6+y6+z7]*tablex6 +
                             data[x7+y6+z7]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z7]*tablex1 + data[x2+y7+z7]*tablex2 + data[x3+y7+z7]*tablex3 +
                             data[x4+y7+z7]*tablex4 + data[x5+y7+z7]*tablex5 + data[x6+y7+z7]*tablex6 +
                             data[x7+y7+z7]*tablex7 ) * tabley7 ) *tablez7;

                w = (tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7) *
                    (tabley1+tabley2+tabley3+tabley4+tabley5+tabley6+tabley7) *
                    (tablez1+tablez2+tablez3+tablez4+tablez5+tablez6+tablez7);
        }
        return pixel/w;
}
float Util::get_pixel_conv_new_background ( int  nx,
int  ny,
int  nz,
float  delx,
float  dely,
float  delz,
float *  data,
Util::KaiserBessel kb,
int  xnew,
int  ynew 
) [static]

Definition at line 1310 of file util_sparx.cpp.

References EMAN::Util::KaiserBessel::get_window_size(), EMAN::Util::KaiserBessel::i0win_tab(), nx, ny, restrict1(), and round().

Referenced by EMAN::EMData::rot_scale_conv_new_background(), and EMAN::EMData::rot_scale_conv_new_background_3D().

                                                                                                                                                            {
        int K = kb.get_window_size();
        int kbmin = -K/2;
        int kbmax = -kbmin;
        int kbc = kbmax+1;

        float pixel =0.0f;
        float w=0.0f;

    float argdelx = delx; // adding this for 2D case where the wrap around is not done circulantly using restrict1.
        delx = restrict1(delx, nx);
        int inxold = int(round(delx));
        if ( ny < 2 ) {  //1D
                float tablex1 = kb.i0win_tab(delx-inxold+3);
                float tablex2 = kb.i0win_tab(delx-inxold+2);
                float tablex3 = kb.i0win_tab(delx-inxold+1);
                float tablex4 = kb.i0win_tab(delx-inxold);
                float tablex5 = kb.i0win_tab(delx-inxold-1);
                float tablex6 = kb.i0win_tab(delx-inxold-2);
                float tablex7 = kb.i0win_tab(delx-inxold-3);

                int x1, x2, x3, x4, x5, x6, x7;

                if ( inxold <= kbc || inxold >=nx-kbc-2 )  {
                        x1 = (inxold-3+nx)%nx;
                        x2 = (inxold-2+nx)%nx;
                        x3 = (inxold-1+nx)%nx;
                        x4 = (inxold  +nx)%nx;
                        x5 = (inxold+1+nx)%nx;
                        x6 = (inxold+2+nx)%nx;
                        x7 = (inxold+3+nx)%nx;
                } else {
                        x1 = inxold-3;
                        x2 = inxold-2;
                        x3 = inxold-1;
                        x4 = inxold;
                        x5 = inxold+1;
                        x6 = inxold+2;
                        x7 = inxold+3;
                }

                pixel = data[x1]*tablex1 + data[x2]*tablex2 + data[x3]*tablex3 +
                        data[x4]*tablex4 + data[x5]*tablex5 + data[x6]*tablex6 +
                        data[x7]*tablex7 ;

                w = tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7;
        } else if ( nz < 2 ) {  // 2D

                delx = argdelx;
                // the wrap around is not done circulantly for 2D case; if (argdelx, argdely) is not in the image, then make them (xnew, ynew) which is definitely in the image
                if ((delx < 0.0f) || (delx >= (float) (nx)) || (dely < 0.0f) || (dely >= (float) (ny)) ){
                delx = (float)xnew*2.0f;
                dely = (float)ynew*2.0f;
                }

                int inxold = int(round(delx));
                int inyold = int(round(dely));

                float tablex1 = kb.i0win_tab(delx-inxold+3);
                float tablex2 = kb.i0win_tab(delx-inxold+2);
                float tablex3 = kb.i0win_tab(delx-inxold+1);
                float tablex4 = kb.i0win_tab(delx-inxold);
                float tablex5 = kb.i0win_tab(delx-inxold-1);
                float tablex6 = kb.i0win_tab(delx-inxold-2);
                float tablex7 = kb.i0win_tab(delx-inxold-3);

                float tabley1 = kb.i0win_tab(dely-inyold+3);
                float tabley2 = kb.i0win_tab(dely-inyold+2);
                float tabley3 = kb.i0win_tab(dely-inyold+1);
                float tabley4 = kb.i0win_tab(dely-inyold);
                float tabley5 = kb.i0win_tab(dely-inyold-1);
                float tabley6 = kb.i0win_tab(dely-inyold-2);
                float tabley7 = kb.i0win_tab(dely-inyold-3);

                int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7;

                if ( inxold <= kbc || inxold >=nx-kbc-2 || inyold <= kbc || inyold >=ny-kbc-2 )  {
                        x1 = (inxold-3+nx)%nx;
                        x2 = (inxold-2+nx)%nx;
                        x3 = (inxold-1+nx)%nx;
                        x4 = (inxold  +nx)%nx;
                        x5 = (inxold+1+nx)%nx;
                        x6 = (inxold+2+nx)%nx;
                        x7 = (inxold+3+nx)%nx;

                        y1 = ((inyold-3+ny)%ny)*nx;
                        y2 = ((inyold-2+ny)%ny)*nx;
                        y3 = ((inyold-1+ny)%ny)*nx;
                        y4 = ((inyold  +ny)%ny)*nx;
                        y5 = ((inyold+1+ny)%ny)*nx;
                        y6 = ((inyold+2+ny)%ny)*nx;
                        y7 = ((inyold+3+ny)%ny)*nx;
                } else {
                        x1 = inxold-3;
                        x2 = inxold-2;
                        x3 = inxold-1;
                        x4 = inxold;
                        x5 = inxold+1;
                        x6 = inxold+2;
                        x7 = inxold+3;

                        y1 = (inyold-3)*nx;
                        y2 = (inyold-2)*nx;
                        y3 = (inyold-1)*nx;
                        y4 = inyold*nx;
                        y5 = (inyold+1)*nx;
                        y6 = (inyold+2)*nx;
                        y7 = (inyold+3)*nx;
                }

                pixel    = ( data[x1+y1]*tablex1 + data[x2+y1]*tablex2 + data[x3+y1]*tablex3 +
                             data[x4+y1]*tablex4 + data[x5+y1]*tablex5 + data[x6+y1]*tablex6 +
                             data[x7+y1]*tablex7 ) * tabley1 +
                           ( data[x1+y2]*tablex1 + data[x2+y2]*tablex2 + data[x3+y2]*tablex3 +
                             data[x4+y2]*tablex4 + data[x5+y2]*tablex5 + data[x6+y2]*tablex6 +
                             data[x7+y2]*tablex7 ) * tabley2 +
                           ( data[x1+y3]*tablex1 + data[x2+y3]*tablex2 + data[x3+y3]*tablex3 +
                             data[x4+y3]*tablex4 + data[x5+y3]*tablex5 + data[x6+y3]*tablex6 +
                             data[x7+y3]*tablex7 ) * tabley3 +
                           ( data[x1+y4]*tablex1 + data[x2+y4]*tablex2 + data[x3+y4]*tablex3 +
                             data[x4+y4]*tablex4 + data[x5+y4]*tablex5 + data[x6+y4]*tablex6 +
                             data[x7+y4]*tablex7 ) * tabley4 +
                           ( data[x1+y5]*tablex1 + data[x2+y5]*tablex2 + data[x3+y5]*tablex3 +
                             data[x4+y5]*tablex4 + data[x5+y5]*tablex5 + data[x6+y5]*tablex6 +
                             data[x7+y5]*tablex7 ) * tabley5 +
                           ( data[x1+y6]*tablex1 + data[x2+y6]*tablex2 + data[x3+y6]*tablex3 +
                             data[x4+y6]*tablex4 + data[x5+y6]*tablex5 + data[x6+y6]*tablex6 +
                             data[x7+y6]*tablex7 ) * tabley6 +
                           ( data[x1+y7]*tablex1 + data[x2+y7]*tablex2 + data[x3+y7]*tablex3 +
                             data[x4+y7]*tablex4 + data[x5+y7]*tablex5 + data[x6+y7]*tablex6 +
                             data[x7+y7]*tablex7 ) * tabley7;

                w = (tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7) *
                    (tabley1+tabley2+tabley3+tabley4+tabley5+tabley6+tabley7);
        } else {  //  3D
                dely = restrict1(dely, ny);
                int inyold = int(Util::round(dely));
                delz = restrict1(delz, nz);
                int inzold = int(Util::round(delz));

                float tablex1 = kb.i0win_tab(delx-inxold+3);
                float tablex2 = kb.i0win_tab(delx-inxold+2);
                float tablex3 = kb.i0win_tab(delx-inxold+1);
                float tablex4 = kb.i0win_tab(delx-inxold);
                float tablex5 = kb.i0win_tab(delx-inxold-1);
                float tablex6 = kb.i0win_tab(delx-inxold-2);
                float tablex7 = kb.i0win_tab(delx-inxold-3);

                float tabley1 = kb.i0win_tab(dely-inyold+3);
                float tabley2 = kb.i0win_tab(dely-inyold+2);
                float tabley3 = kb.i0win_tab(dely-inyold+1);
                float tabley4 = kb.i0win_tab(dely-inyold);
                float tabley5 = kb.i0win_tab(dely-inyold-1);
                float tabley6 = kb.i0win_tab(dely-inyold-2);
                float tabley7 = kb.i0win_tab(dely-inyold-3);

                float tablez1 = kb.i0win_tab(delz-inzold+3);
                float tablez2 = kb.i0win_tab(delz-inzold+2);
                float tablez3 = kb.i0win_tab(delz-inzold+1);
                float tablez4 = kb.i0win_tab(delz-inzold);
                float tablez5 = kb.i0win_tab(delz-inzold-1);
                float tablez6 = kb.i0win_tab(delz-inzold-2);
                float tablez7 = kb.i0win_tab(delz-inzold-3);

                int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7, z1, z2, z3, z4, z5, z6, z7;

                if ( inxold <= kbc || inxold >=nx-kbc-2 || inyold <= kbc || inyold >=ny-kbc-2 || inzold <= kbc || inzold >= nz-kbc-2 )  {
                        x1 = (inxold-3+nx)%nx;
                        x2 = (inxold-2+nx)%nx;
                        x3 = (inxold-1+nx)%nx;
                        x4 = (inxold  +nx)%nx;
                        x5 = (inxold+1+nx)%nx;
                        x6 = (inxold+2+nx)%nx;
                        x7 = (inxold+3+nx)%nx;

                        y1 = ((inyold-3+ny)%ny)*nx;
                        y2 = ((inyold-2+ny)%ny)*nx;
                        y3 = ((inyold-1+ny)%ny)*nx;
                        y4 = ((inyold  +ny)%ny)*nx;
                        y5 = ((inyold+1+ny)%ny)*nx;
                        y6 = ((inyold+2+ny)%ny)*nx;
                        y7 = ((inyold+3+ny)%ny)*nx;

                        z1 = ((inzold-3+nz)%nz)*nx*ny;
                        z2 = ((inzold-2+nz)%nz)*nx*ny;
                        z3 = ((inzold-1+nz)%nz)*nx*ny;
                        z4 = ((inzold  +nz)%nz)*nx*ny;
                        z5 = ((inzold+1+nz)%nz)*nx*ny;
                        z6 = ((inzold+2+nz)%nz)*nx*ny;
                        z7 = ((inzold+3+nz)%nz)*nx*ny;
                } else {
                        x1 = inxold-3;
                        x2 = inxold-2;
                        x3 = inxold-1;
                        x4 = inxold;
                        x5 = inxold+1;
                        x6 = inxold+2;
                        x7 = inxold+3;

                        y1 = (inyold-3)*nx;
                        y2 = (inyold-2)*nx;
                        y3 = (inyold-1)*nx;
                        y4 = inyold*nx;
                        y5 = (inyold+1)*nx;
                        y6 = (inyold+2)*nx;
                        y7 = (inyold+3)*nx;

                        z1 = (inzold-3)*nx*ny;
                        z2 = (inzold-2)*nx*ny;
                        z3 = (inzold-1)*nx*ny;
                        z4 = inzold*nx*ny;
                        z5 = (inzold+1)*nx*ny;
                        z6 = (inzold+2)*nx*ny;
                        z7 = (inzold+3)*nx*ny;
                }

                pixel  = ( ( data[x1+y1+z1]*tablex1 + data[x2+y1+z1]*tablex2 + data[x3+y1+z1]*tablex3 +
                             data[x4+y1+z1]*tablex4 + data[x5+y1+z1]*tablex5 + data[x6+y1+z1]*tablex6 +
                             data[x7+y1+z1]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z1]*tablex1 + data[x2+y2+z1]*tablex2 + data[x3+y2+z1]*tablex3 +
                             data[x4+y2+z1]*tablex4 + data[x5+y2+z1]*tablex5 + data[x6+y2+z1]*tablex6 +
                             data[x7+y2+z1]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z1]*tablex1 + data[x2+y3+z1]*tablex2 + data[x3+y3+z1]*tablex3 +
                             data[x4+y3+z1]*tablex4 + data[x5+y3+z1]*tablex5 + data[x6+y3+z1]*tablex6 +
                             data[x7+y3+z1]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z1]*tablex1 + data[x2+y4+z1]*tablex2 + data[x3+y4+z1]*tablex3 +
                             data[x4+y4+z1]*tablex4 + data[x5+y4+z1]*tablex5 + data[x6+y4+z1]*tablex6 +
                             data[x7+y4+z1]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z1]*tablex1 + data[x2+y5+z1]*tablex2 + data[x3+y5+z1]*tablex3 +
                             data[x4+y5+z1]*tablex4 + data[x5+y5+z1]*tablex5 + data[x6+y5+z1]*tablex6 +
                             data[x7+y5+z1]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z1]*tablex1 + data[x2+y6+z1]*tablex2 + data[x3+y6+z1]*tablex3 +
                             data[x4+y6+z1]*tablex4 + data[x5+y6+z1]*tablex5 + data[x6+y6+z1]*tablex6 +
                             data[x7+y6+z1]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z1]*tablex1 + data[x2+y7+z1]*tablex2 + data[x3+y7+z1]*tablex3 +
                             data[x4+y7+z1]*tablex4 + data[x5+y7+z1]*tablex5 + data[x6+y7+z1]*tablex6 +
                             data[x7+y7+z1]*tablex7 ) * tabley7 ) *tablez1 +
                         ( ( data[x1+y1+z2]*tablex1 + data[x2+y1+z2]*tablex2 + data[x3+y1+z2]*tablex3 +
                             data[x4+y1+z2]*tablex4 + data[x5+y1+z2]*tablex5 + data[x6+y1+z2]*tablex6 +
                             data[x7+y1+z2]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z2]*tablex1 + data[x2+y2+z2]*tablex2 + data[x3+y2+z2]*tablex3 +
                             data[x4+y2+z2]*tablex4 + data[x5+y2+z2]*tablex5 + data[x6+y2+z2]*tablex6 +
                             data[x7+y2+z2]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z2]*tablex1 + data[x2+y3+z2]*tablex2 + data[x3+y3+z2]*tablex3 +
                             data[x4+y3+z2]*tablex4 + data[x5+y3+z2]*tablex5 + data[x6+y3+z2]*tablex6 +
                             data[x7+y3+z2]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z2]*tablex1 + data[x2+y4+z2]*tablex2 + data[x3+y4+z2]*tablex3 +
                             data[x4+y4+z2]*tablex4 + data[x5+y4+z2]*tablex5 + data[x6+y4+z2]*tablex6 +
                             data[x7+y4+z2]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z2]*tablex1 + data[x2+y5+z2]*tablex2 + data[x3+y5+z2]*tablex3 +
                             data[x4+y5+z2]*tablex4 + data[x5+y5+z2]*tablex5 + data[x6+y5+z2]*tablex6 +
                             data[x7+y5+z2]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z2]*tablex1 + data[x2+y6+z2]*tablex2 + data[x3+y6+z2]*tablex3 +
                             data[x4+y6+z2]*tablex4 + data[x5+y6+z2]*tablex5 + data[x6+y6+z2]*tablex6 +
                             data[x7+y6+z2]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z2]*tablex1 + data[x2+y7+z2]*tablex2 + data[x3+y7+z2]*tablex3 +
                             data[x4+y7+z2]*tablex4 + data[x5+y7+z2]*tablex5 + data[x6+y7+z2]*tablex6 +
                             data[x7+y7+z2]*tablex7 ) * tabley7 ) *tablez2 +
                         ( ( data[x1+y1+z3]*tablex1 + data[x2+y1+z3]*tablex2 + data[x3+y1+z3]*tablex3 +
                             data[x4+y1+z3]*tablex4 + data[x5+y1+z3]*tablex5 + data[x6+y1+z3]*tablex6 +
                             data[x7+y1+z3]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z3]*tablex1 + data[x2+y2+z3]*tablex2 + data[x3+y2+z3]*tablex3 +
                             data[x4+y2+z3]*tablex4 + data[x5+y2+z3]*tablex5 + data[x6+y2+z3]*tablex6 +
                             data[x7+y2+z3]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z3]*tablex1 + data[x2+y3+z3]*tablex2 + data[x3+y3+z3]*tablex3 +
                             data[x4+y3+z3]*tablex4 + data[x5+y3+z3]*tablex5 + data[x6+y3+z3]*tablex6 +
                             data[x7+y3+z3]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z3]*tablex1 + data[x2+y4+z3]*tablex2 + data[x3+y4+z3]*tablex3 +
                             data[x4+y4+z3]*tablex4 + data[x5+y4+z3]*tablex5 + data[x6+y4+z3]*tablex6 +
                             data[x7+y4+z3]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z3]*tablex1 + data[x2+y5+z3]*tablex2 + data[x3+y5+z3]*tablex3 +
                             data[x4+y5+z3]*tablex4 + data[x5+y5+z3]*tablex5 + data[x6+y5+z3]*tablex6 +
                             data[x7+y5+z3]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z3]*tablex1 + data[x2+y6+z3]*tablex2 + data[x3+y6+z3]*tablex3 +
                             data[x4+y6+z3]*tablex4 + data[x5+y6+z3]*tablex5 + data[x6+y6+z3]*tablex6 +
                             data[x7+y6+z3]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z3]*tablex1 + data[x2+y7+z3]*tablex2 + data[x3+y7+z3]*tablex3 +
                             data[x4+y7+z3]*tablex4 + data[x5+y7+z3]*tablex5 + data[x6+y7+z3]*tablex6 +
                             data[x7+y7+z3]*tablex7 ) * tabley7 ) *tablez3 +
                         ( ( data[x1+y1+z4]*tablex1 + data[x2+y1+z4]*tablex2 + data[x3+y1+z4]*tablex3 +
                             data[x4+y1+z4]*tablex4 + data[x5+y1+z4]*tablex5 + data[x6+y1+z4]*tablex6 +
                             data[x7+y1+z4]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z4]*tablex1 + data[x2+y2+z4]*tablex2 + data[x3+y2+z4]*tablex3 +
                             data[x4+y2+z4]*tablex4 + data[x5+y2+z4]*tablex5 + data[x6+y2+z4]*tablex6 +
                             data[x7+y2+z4]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z4]*tablex1 + data[x2+y3+z4]*tablex2 + data[x3+y3+z4]*tablex3 +
                             data[x4+y3+z4]*tablex4 + data[x5+y3+z4]*tablex5 + data[x6+y3+z4]*tablex6 +
                             data[x7+y3+z4]*tablex7 ) * tabley3 +
                           ( data[x1+y4+z4]*tablex1 + data[x2+y4+z4]*tablex2 + data[x3+y4+z4]*tablex3 +
                             data[x4+y4+z4]*tablex4 + data[x5+y4+z4]*tablex5 + data[x6+y4+z4]*tablex6 +
                             data[x7+y4+z4]*tablex7 ) * tabley4 +
                           ( data[x1+y5+z4]*tablex1 + data[x2+y5+z4]*tablex2 + data[x3+y5+z4]*tablex3 +
                             data[x4+y5+z4]*tablex4 + data[x5+y5+z4]*tablex5 + data[x6+y5+z4]*tablex6 +
                             data[x7+y5+z4]*tablex7 ) * tabley5 +
                           ( data[x1+y6+z4]*tablex1 + data[x2+y6+z4]*tablex2 + data[x3+y6+z4]*tablex3 +
                             data[x4+y6+z4]*tablex4 + data[x5+y6+z4]*tablex5 + data[x6+y6+z4]*tablex6 +
                             data[x7+y6+z4]*tablex7 ) * tabley6 +
                           ( data[x1+y7+z4]*tablex1 + data[x2+y7+z4]*tablex2 + data[x3+y7+z4]*tablex3 +
                             data[x4+y7+z4]*tablex4 + data[x5+y7+z4]*tablex5 + data[x6+y7+z4]*tablex6 +
                             data[x7+y7+z4]*tablex7 ) * tabley7 ) *tablez4 +
                         ( ( data[x1+y1+z5]*tablex1 + data[x2+y1+z5]*tablex2 + data[x3+y1+z5]*tablex3 +
                             data[x4+y1+z5]*tablex4 + data[x5+y1+z5]*tablex5 + data[x6+y1+z5]*tablex6 +
                             data[x7+y1+z5]*tablex7 ) * tabley1 +
                           ( data[x1+y2+z5]*tablex1 + data[x2+y2+z5]*tablex2 + data[x3+y2+z5]*tablex3 +
                             data[x4+y2+z5]*tablex4 + data[x5+y2+z5]*tablex5 + data[x6+y2+z5]*tablex6 +
                             data[x7+y2+z5]*tablex7 ) * tabley2 +
                           ( data[x1+y3+z5]*tablex1 + data[x2+y3+z5]*tablex2 + data[x3+y3+z5]*tablex3 +
                             data[x4+y3+z5]*tablex4 + data[x5+y3+z5]*tablex5 + data[x6+y3+z5]*tablex6 +
                             data[x7+y3+z5]*tablex7 ) * tabley3 +
                           (