EMAN::Util Class Reference
[unit test in Python]

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

#include <util.h>

List of all members.

Static Public Member Functions

static int coveig (int n, float *covmat, float *eigval, float *eigvec)
 This file is a part of util.h, To use this file's functions, you should include "util.h" NEVER directly include this file.
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, vector< float > DM)
static vector< float > infomask (EMData *Vol, EMData *mask, bool)
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 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_psi_0_180_no_mirror (EMData *circ1, EMData *circ2, vector< int > numr, float psi_max)
 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 Dict Crosrng_ns (EMData *circ1, EMData *circ2, vector< int > numr)
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 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 div_filter (EMData *img, EMData *img1)
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< int > assign_groups (const vector< float > &d, int nref, int nima)
static void getvec (float phi, float theta, float &x, float &y, float &z)
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< 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 > 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, bool mirror_only=false, 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)
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 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 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 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 int get_randnum_seed ()
 Get the seed for Randnum class.
static void set_randnum_seed (unsigned long int 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 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 float 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)
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)

Classes

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


Detailed Description

Util is a collection of utility functions.

Definition at line 81 of file util.h.


Member Function Documentation

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

Definition at line 17558 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().

17559 {
17560         ENTERFUNC;
17561         /* Exception Handle */
17562         if (!img || !img1) {
17563                 throw NullPointerException("NULL input image");
17564         }
17565         /* ========= img += img1 ===================== */
17566 
17567         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17568         size_t size = (size_t)nx*ny*nz;
17569         float *img_ptr  = img->get_data();
17570         float *img1_ptr = img1->get_data();
17571         for (size_t i=0;i<size;++i) img_ptr[i] += img1_ptr[i];
17572         img->update();
17573 
17574         EXITFUNC;
17575 }

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

Definition at line 17596 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().

17597 {
17598         ENTERFUNC;
17599         /* Exception Handle */
17600         if (!img) {
17601                 throw NullPointerException("NULL input image");
17602         }
17603         /* ========= img += img1**2 ===================== */
17604 
17605         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17606         size_t size = (size_t)nx*ny*nz;
17607         float *img_ptr  = img->get_data();
17608         float *img1_ptr = img1->get_data();
17609         if(img->is_complex()) {
17610                 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] ;
17611         } else {
17612                 for (size_t i=0;i<size;++i) img_ptr[i] += img1_ptr[i]*img1_ptr[i];
17613         }
17614         img->update();
17615 
17616         EXITFUNC;
17617 }

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

Definition at line 17577 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().

17578 {
17579         ENTERFUNC;
17580         /* Exception Handle */
17581         if (!img) {
17582                 throw NullPointerException("NULL input image");
17583         }
17584         /* ========= img += img1 ===================== */
17585 
17586         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17587         size_t size = (size_t)nx*ny*nz;
17588         float *img_ptr  = img->get_data();
17589         float *img1_ptr = img1->get_data();
17590         for (size_t i=0;i<size;++i) img_ptr[i] += abs(img1_ptr[i]);
17591         img->update();
17592 
17593         EXITFUNC;
17594 }

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

Definition at line 17375 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().

17376 {
17377         ENTERFUNC;
17378         /* Exception Handle */
17379         if (!img) {
17380                 throw NullPointerException("NULL input image");
17381         }
17382         /* ==============   output = img + img1   ================ */
17383 
17384         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17385         size_t size = (size_t)nx*ny*nz;
17386         EMData * img2 = img->copy_head();
17387         float *img_ptr  =img->get_data();
17388         float *img2_ptr = img2->get_data();
17389         float *img1_ptr = img1->get_data();
17390         for (size_t i=0;i<size;++i) img2_ptr[i] = img_ptr[i] + img1_ptr[i];
17391         img2->update();
17392         if(img->is_complex()) {
17393                 img2->set_complex(true);
17394                 if(img->is_fftodd()) img2->set_fftodd(true); else img2->set_fftodd(false);
17395         }
17396 
17397         EXITFUNC;
17398         return img2;
17399 }

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] a amplitude
[in] dx x center
[in] dy y center
[in] dz z center
[in] d width of gaussian
Returns:
The Gaussian value.

Definition at line 1754 of file util.h.

Referenced by EMAN::FourierReconstructorSimple2D::insert_slice(), and EMAN::GaussFFTProjector::interp_ft_3d().

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 19109 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.

19111                                                                            {
19112 
19113         int   maxrin = numr[numr.size()-1];
19114 
19115         int   ky = int(2*yrng/step+0.5)/2;
19116         int   kx = int(2*xrng/step+0.5)/2;
19117 
19118         float *p_ccf1ds = (float *)malloc(maxrin*sizeof(float));
19119         float *p_ccf1dm = (float *)malloc(maxrin*sizeof(float));
19120         int vol = maxrin*(2*kx+1)*(2*ky+1);
19121         vector<ccf_point> ccf(2*vol);
19122         ccf_point temp;
19123 
19124         int index = 0;
19125         for (int i = -ky; i <= ky; i++) {
19126                 float iy = i * step;
19127                 for (int j = -kx; j <= kx; j++) {
19128                         float ix = j*step;
19129                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
19130                         Frngs(cimage, numr);
19131                         Crosrng_msg_vec(crefim, cimage, numr, p_ccf1ds, p_ccf1dm);
19132                         for (int k=0; k<maxrin; k++) {
19133                                 temp.value = p_ccf1ds[k];
19134                                 temp.i = k;
19135                                 temp.j = j;
19136                                 temp.k = i;
19137                                 temp.mirror = 0;
19138                                 ccf[index] = temp;
19139                                 index++;
19140                                 temp.value = p_ccf1dm[k];
19141                                 temp.mirror = 1;
19142                                 ccf[index] = temp;
19143                                 index++;
19144                         }
19145                         delete cimage; cimage = 0;
19146                 }
19147         }
19148 
19149         delete p_ccf1ds;
19150         delete p_ccf1dm;
19151         std::sort(ccf.begin(), ccf.end(), ccf_value());
19152 
19153         double qt = (double)ccf[0].value;
19154         vector <double> p(2*vol), cp(2*vol);
19155 
19156         double sump = 0.0;
19157         for (int i=0; i<2*vol; i++) {
19158                 p[i] = pow(double(ccf[i].value)/qt, 1.0/T);
19159                 sump += p[i];
19160         }
19161         for (int i=0; i<2*vol; i++) {
19162                 p[i] /= sump;
19163         }
19164         for (int i=1; i<2*vol; i++) {
19165                 p[i] += p[i-1];
19166         }
19167         p[2*vol-1] = 2.0;
19168 
19169         float t = get_frand(0.0f, 1.0f);
19170         int select = 0;
19171         while (p[select] < t)   select += 1;
19172 
19173         vector<float> a(6);
19174         a[0] = ccf[select].value;
19175         a[1] = (float)ccf[select].i;
19176         a[2] = (float)ccf[select].j;
19177         a[3] = (float)ccf[select].k;
19178         a[4] = (float)ccf[select].mirror;
19179         a[5] = (float)select;
19180         return a;
19181 }

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 2364 of file util_sparx.cpp.

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

02365                                                                     {
02366         double dpi, dfi;
02367         int    it, jt, inr, l, nsim, kcirc, lt;
02368         float   xold, yold, fi, x, y;
02369 
02370         //     cns2 and cnr2 are predefined centers
02371         //     no need to set to zero, all elements are defined
02372 
02373         dpi = 2*atan(1.0);
02374         for (it=1; it<=nring; it++) {
02375                 // radius of the ring
02376                 inr = numr(1,it);
02377 
02378                 l = numr(3,it);
02379                 if ( mode == 'h' || mode == 'H' ) lt = l / 2;
02380                 else                              lt = l / 4;
02381 
02382                 nsim  = lt - 1;
02383                 dfi   = dpi / (nsim+1);
02384                 kcirc = numr(2,it);
02385 
02386 
02387                 xold  = 0.0f+cns2;
02388                 yold  = inr+cnr2;
02389 
02390                 circ(kcirc) = quadri(xold,yold,nsam,nrow,xim);
02391 
02392                 xold  = inr+cns2;
02393                 yold  = 0.0f+cnr2;
02394                 circ(lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
02395 
02396                 if ( mode == 'f' || mode == 'F' ) {
02397                         xold = 0.0f+cns2;
02398                         yold = -inr+cnr2;
02399                         circ(lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
02400 
02401                         xold = -inr+cns2;
02402                         yold = 0.0f+cnr2;
02403                         circ(lt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
02404                 }
02405 
02406                 for (jt=1; jt<=nsim; jt++) {
02407                         fi   = static_cast<float>(dfi * jt);
02408                         x    = sin(fi) * inr;
02409                         y    = cos(fi) * inr;
02410 
02411                         xold = x+cns2;
02412                         yold = y+cnr2;
02413                         circ(jt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
02414 
02415                         xold = y+cns2;
02416                         yold = -x+cnr2;
02417                         circ(jt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
02418 
02419                         if ( mode == 'f' || mode == 'F' ) {
02420                                 xold = -x+cns2;
02421                                 yold = -y+cnr2;
02422                                 circ(jt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
02423 
02424                                 xold = -y+cns2;
02425                                 yold = x+cnr2;
02426                                 circ(jt+lt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);
02427                         }
02428                 } // end for jt
02429         } //end for it
02430 }

float Util::ang_n ( float  peakp,
string  mode,
int  maxrin 
) [static, private]

Definition at line 17805 of file util_sparx.cpp.

Referenced by multiref_polar_ali_2d(), multiref_polar_ali_2d_delta(), multiref_polar_ali_2d_local(), multiref_polar_ali_2d_local_psi(), multiref_polar_ali_2d_nom(), multiref_polar_ali_2d_peaklist(), multiref_polar_ali_helical(), multiref_polar_ali_helical_90(), multiref_polar_ali_helical_90_local(), and multiref_polar_ali_helical_local().

17806 {
17807     if (mode == "f" || mode == "F")
17808         return fmodf(((peakp-1.0f) / maxrin+1.0f)*360.0f,360.0f);
17809     else
17810         return fmodf(((peakp-1.0f) / maxrin+1.0f)*180.0f,180.0f);
17811 }

void Util::ang_to_xyz ( double *  x,
double *  y,
double *  z,
int  len 
) [static]

Definition at line 7773 of file util_sparx.cpp.

References dgr_to_rad, ENTERFUNC, and EXITFUNC.

07774 {
07775         ENTERFUNC;
07776         double costheta,sintheta,cosphi,sinphi;
07777         for(int i = 0;  i<len;  i++)
07778         {
07779                 cosphi = cos(y[i]*dgr_to_rad);
07780                 sinphi = sin(y[i]*dgr_to_rad);
07781                 if(fabs(x[i]-90.0)< 1.0e-5){
07782                         x[i] = cosphi;
07783                         y[i] = sinphi;
07784                         z[i] = 0.0;
07785                 }
07786                 else{
07787                         costheta = cos(x[i]*dgr_to_rad);
07788                         sintheta = sin(x[i]*dgr_to_rad);
07789                         x[i] = cosphi*sintheta;
07790                         y[i] = sinphi*sintheta;
07791                         z[i] = costheta;
07792                 }
07793         }
07794         EXITFUNC;
07795 }

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/i1 first vector, 2 floats
[in] r2/i2 second vector, 2 floats
Returns:
angular difference

Definition at line 1924 of file util.h.

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

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] x The first angle.
[in] y The second angle.
Returns:
The difference of 2 angles.

Definition at line 1892 of file util.h.

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

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] x The first angle.
[in] y The second angle.
Returns:
The difference of 2 angles.

Definition at line 1909 of file util.h.

void Util::ap2ri ( float *  data,
size_t  n 
) [static]

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

Parameters:
data complex data array.
n array size.

Definition at line 67 of file util.cpp.

References Assert, and NullPointerException.

00068 {
00069         Assert(n > 0);
00070 
00071         if (!data) {
00072                 throw NullPointerException("pixel data array");
00073         }
00074 
00075         for (size_t i = 0; i < n; i += 2) {
00076                 float f = data[i] * sin(data[i + 1]);
00077                 data[i] = data[i] * cos(data[i + 1]);
00078                 data[i + 1] = f;
00079         }
00080 }

static void EMAN::Util::apply_precision ( float &  value,
const float &  precision 
) [inline, static]

Definition at line 2136 of file util.h.

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().

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 8831 of file util_sparx.cpp.

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

Referenced by voronoi().

08834 {
08835     /* Initialized data */
08836 
08837     static double amax = 6.28;
08838 
08839     /* System generated locals */
08840     double ret_val;
08841 
08842     /* Local variables */
08843     static double a, c0[3], c2[3], c3[3];
08844     static int n1, n2, n3;
08845     static double v1[3], v2[3], v3[3];
08846     static int lp, lpl, ierr;
08847     static double asum;
08848     static long int first;
08849 
08850 
08851 /* *********************************************************** */
08852 
08853 /*                                            Robert J. Renka */
08854 /*                                  Dept. of Computer Science */
08855 /*                                       Univ. of North Texas */
08856 /*                                           renka@cs.unt.edu */
08857 /*                                                   10/25/02 */
08858 
08859 /*   Given a Delaunay triangulation and the index K of an */
08860 /* interior node, this subroutine returns the (surface) area */
08861 /* of the Voronoi region associated with node K.  The Voronoi */
08862 /* region is the polygon whose vertices are the circumcenters */
08863 /* of the triangles that contain node K, where a triangle */
08864 /* circumcenter is the point (unit vector) lying at the same */
08865 /* angular distance from the three vertices and contained in */
08866 /* the same hemisphere as the vertices. */
08867 
08868 
08869 /* On input: */
08870 
08871 /*       K = Nodal index in the range 1 to N. */
08872 
08873 /*       N = Number of nodes in the triangulation.  N > 3. */
08874 
08875 /*       X,Y,Z = Arrays of length N containing the Cartesian */
08876 /*               coordinates of the nodes (unit vectors). */
08877 
08878 /*       LIST,LPTR,LEND = Data structure defining the trian- */
08879 /*                        gulation.  Refer to Subroutine */
08880 /*                        TRMESH. */
08881 
08882 /* Input parameters are not altered by this function. */
08883 
08884 /* On output: */
08885 
08886 /*       AREAV = Area of Voronoi region K unless IER > 0, */
08887 /*               in which case AREAV = 0. */
08888 
08889 /*       IER = Error indicator: */
08890 /*             IER = 0 if no errors were encountered. */
08891 /*             IER = 1 if K or N is outside its valid range */
08892 /*                     on input. */
08893 /*             IER = 2 if K indexes a boundary node. */
08894 /*             IER = 3 if an error flag is returned by CIRCUM */
08895 /*                     (null triangle). */
08896 /*             IER = 4 if AREAS returns a value greater than */
08897 /*                     AMAX (defined below). */
08898 
08899 /* Modules required by AREAV:  AREAS, CIRCUM */
08900 
08901 /* *********************************************************** */
08902 
08903 
08904 /* Maximum valid triangle area is less than 2*Pi: */
08905 
08906     /* Parameter adjustments */
08907     --lend;
08908     --z__;
08909     --y;
08910     --x;
08911     --list;
08912     --lptr;
08913 
08914     /* Function Body */
08915 
08916 /* Test for invalid input. */
08917 
08918     if (*k < 1 || *k > *n || *n <= 3) {
08919         goto L11;
08920     }
08921 
08922 /* Initialization:  Set N3 to the last neighbor of N1 = K. */
08923 /*   FIRST = TRUE only for the first triangle. */
08924 /*   The Voronoi region area is accumulated in ASUM. */
08925 
08926     n1 = *k;
08927     v1[0] = x[n1];
08928     v1[1] = y[n1];
08929     v1[2] = z__[n1];
08930     lpl = lend[n1];
08931     n3 = list[lpl];
08932     if (n3 < 0) {
08933         goto L12;
08934     }
08935     lp = lpl;
08936     first = TRUE_;
08937     asum = 0.;
08938 
08939 /* Loop on triangles (N1,N2,N3) containing N1 = K. */
08940 
08941 L1:
08942     n2 = n3;
08943     lp = lptr[lp];
08944     n3 = list[lp];
08945     v2[0] = x[n2];
08946     v2[1] = y[n2];
08947     v2[2] = z__[n2];
08948     v3[0] = x[n3];
08949     v3[1] = y[n3];
08950     v3[2] = z__[n3];
08951     if (first) {
08952 
08953 /* First triangle:  compute the circumcenter C3 and save a */
08954 /*   copy in C0. */
08955 
08956         circum_(v1, v2, v3, c3, &ierr);
08957         if (ierr != 0) {
08958             goto L13;
08959         }
08960         c0[0] = c3[0];
08961         c0[1] = c3[1];
08962         c0[2] = c3[2];
08963         first = FALSE_;
08964     } else {
08965 
08966 /* Set C2 to C3, compute the new circumcenter C3, and compute */
08967 /*   the area A of triangle (V1,C2,C3). */
08968 
08969         c2[0] = c3[0];
08970         c2[1] = c3[1];
08971         c2[2] = c3[2];
08972         circum_(v1, v2, v3, c3, &ierr);
08973         if (ierr != 0) {
08974             goto L13;
08975         }
08976         a = areas_(v1, c2, c3);
08977         if (a > amax) {
08978             goto L14;
08979         }
08980         asum += a;
08981     }
08982 
08983 /* Bottom on loop on neighbors of K. */
08984 
08985     if (lp != lpl) {
08986         goto L1;
08987     }
08988 
08989 /* Compute the area of triangle (V1,C3,C0). */
08990 
08991     a = areas_(v1, c3, c0);
08992     if (a > amax) {
08993         goto L14;
08994     }
08995     asum += a;
08996 
08997 /* No error encountered. */
08998 
08999     *ier = 0;
09000     ret_val = asum;
09001     return ret_val;
09002 
09003 /* Invalid input. */
09004 
09005 L11:
09006     *ier = 1;
09007     ret_val = 0.;
09008     return ret_val;
09009 
09010 /* K indexes a boundary node. */
09011 
09012 L12:
09013     *ier = 2;
09014     ret_val = 0.;
09015     return ret_val;
09016 
09017 /* Error in CIRCUM. */
09018 
09019 L13:
09020     *ier = 3;
09021     ret_val = 0.;
09022     return ret_val;
09023 
09024 /* AREAS value larger than AMAX. */
09025 
09026 L14:
09027     *ier = 4;
09028     ret_val = 0.;
09029     return ret_val;
09030 } /* 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 20669 of file util_sparx.cpp.

References t.

Referenced by image_mutation().

20669                                                                                                                             {
20670 
20671         if (is_mirror != 0) {
20672                 for (int i=0; i<len_list; i++) {
20673                         int r = rand()%10000;
20674                         float f = r/10000.0f;
20675                         if (f < mutation_rate) list[i] = 1-list[i];
20676                 }
20677         } else {
20678                 map<int, vector<int> >  graycode;
20679                 map<vector<int>, int> rev_graycode;
20680                 vector <int> gray;
20681 
20682                 int K=1;
20683                 for (int i=0; i<L; i++) K*=2;
20684 
20685                 for (int k=0; k<K; k++) {
20686                         int shift = 0;
20687                         vector <int> gray;
20688                         for (int i=L-1; i>-1; i--) {
20689                                 int t = ((k>>i)%2-shift)%2;
20690                                 gray.push_back(t);
20691                                 shift += t-2;
20692                         }
20693                         graycode[k] = gray;
20694                         rev_graycode[gray] = k;
20695                 }
20696 
20697                 float gap = (K-1)/(max_val-min_val);
20698                 for (int i=0; i<len_list; i++) {
20699                         float val = list[i];
20700                         if (val < min_val) { val = min_val; }
20701                         else if  (val > max_val) { val = max_val; }
20702                         int k = int((val-min_val)*gap+0.5);
20703                         vector<int> gray = graycode[k];
20704                         bool changed = false;
20705                         for (vector<int>::iterator p=gray.begin(); p!=gray.end(); p++) {
20706                                 int r = rand()%10000;
20707                                 float f = r/10000.0f;
20708                                 if (f < mutation_rate) {
20709                                         *p = 1-*p;
20710                                         changed = true;
20711                                 }
20712                         }
20713                         if (changed) {
20714                                 k = rev_graycode[gray];
20715                                 list[i] = k/gap+min_val;
20716                         }
20717                 }
20718         }
20719 
20720 }

vector< int > Util::assign_groups ( const vector< float > &  d,
int  nref,
int  nima 
) [static]

Definition at line 17976 of file util_sparx.cpp.

References peak_table::index.

17976                                                                           {
17977 
17978         int kt = nref;
17979         unsigned int maxasi = nima/nref;
17980         vector< vector<int> > id_list;
17981         id_list.resize(nref);
17982         int group, ima;
17983 
17984         peak_table* dd = new peak_table[nref*nima];
17985         for (int i=0; i<nref*nima; i++)  {
17986                 dd[i].value = d[i];
17987                 dd[i].index = i;
17988         }
17989         sort(dd, dd+nref*nima);
17990         int begin = 0;
17991 
17992         bool* del_row = new bool[nref];
17993         for (int i=0; i<nref; i++) del_row[i] = false;
17994         bool* del_column = new bool[nima];
17995         for (int i=0; i<nima; i++) del_column[i] = false;
17996         while (kt > 0) {
17997                 bool flag = true;
17998                 while (flag) {
17999                         int l = dd[begin].index;
18000                         group = l/nima;
18001                         ima = l%nima;
18002                         if (del_column[ima] || del_row[group]) begin++;
18003                         else flag = false;
18004                 }
18005 
18006                 id_list[group].push_back(ima);
18007                 if (kt > 1) {
18008                         if (id_list[group].size() < maxasi) group = -1;
18009                         else kt -= 1;
18010                 } else {
18011                         if (id_list[group].size() < maxasi+nima%nref) group = -1;
18012                         else kt -= 1;
18013                 }
18014                 del_column[ima] = true;
18015                 if (group != -1) {
18016                         del_row[group] = true;
18017                 }
18018         }
18019 
18020         vector<int> id_list_1; 
18021         for (int iref=0; iref<nref; iref++)
18022                 for (unsigned int im=0; im<maxasi; im++)
18023                         id_list_1.push_back(id_list[iref][im]);
18024         for (unsigned int im=maxasi; im<maxasi+nima%nref; im++)
18025                         id_list_1.push_back(id_list[group][im]);
18026         id_list_1.push_back(group);
18027 
18028         delete[] del_row;
18029         delete[] del_column;
18030         delete[] dd;
18031         return id_list_1;
18032 }

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

Definition at line 18048 of file util_sparx.cpp.

References getvec(), and nearest_ang().

18048                                                                                                    {
18049         int nref = refangles.size()/2;
18050         int nproj = projangles.size()/2;
18051         
18052         vector<float> vecref(nref*3, 0.0f);
18053         vector<int> asg(nproj, 0);
18054         for (int i=0; i<nref; i++)  
18055                 getvec(refangles[i*2], refangles[i*2+1], vecref[i*3], vecref[i*3+1], vecref[i*3+2]); 
18056         for (int i=0; i<nproj; i++) {
18057                 float x, y, z;
18058                 getvec(projangles[i*2], projangles[i*2+1], x, y, z);
18059                 asg[i] = nearest_ang(vecref, x, y, z);
18060         }
18061         
18062         return asg;
18063 }

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 21087 of file util_sparx.cpp.

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

21088                                                           {
21089         
21090         
21091         // 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
21092         // 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
21093         // Make a vector of nParts vectors of K int* each
21094          int* Indices = new int[nParts*K];
21095          int ind_c = 0;
21096          for (int i=0; i < nParts; i++){
21097                  for(int j = 0; j < K; j++){
21098                          Indices[i*K + j] = ind_c;
21099                          ind_c = ind_c + dimClasses[i*K + j];
21100                  }
21101          }
21102 
21103         // do initial pruning on argParts and return the pruned partitions
21104 
21105         // Make a vector of nParts vectors of K int* each
21106         vector <vector <int*> > Parts(nParts,vector<int*>(K));
21107         ind_c = 0;
21108         int argParts_size=0;
21109         for (int i=0; i < nParts; i++){
21110                 for(int j = 0; j < K; j++){
21111                         Parts[i][j] = argParts + ind_c;
21112                         ind_c = ind_c + dimClasses[i*K + j];
21113                         argParts_size = argParts_size + dimClasses[i*K + j];
21114                 }
21115         }
21116 
21117         // in the following we call initial_prune with Parts which is a vector. This is not the most
21118         // efficient since vector does not allow for direct addressing. But since initial_prune doesn't have very high complexity, and
21119         // 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.....
21120 
21121         // comment out for testing
21122         Util::initial_prune(Parts, dimClasses, nParts, K, T);
21123         for(int i = 0; i < nParts; i++){
21124                 for(int j=0; j < K; j++){
21125                         argParts[Indices[i*K + j]+1] = -1;
21126                 }
21127         }
21128 
21129         int num_classes;
21130         int old_index;
21131         for(int i=0; i<nParts; i++){
21132                 num_classes = Parts[i].size();// number of classes in partition i after pruning
21133                 for (int j=0; j < num_classes; j++){
21134                         old_index = Parts[i][j][0];
21135                         //cout << "old_index: " << old_index<<"\n";
21136                         argParts[Indices[i*K + old_index]+1] = 1;
21137                 }
21138         }
21139 
21140 
21141         // if we're not doing mpi then keep going and call branchMPI and return the output
21142         //cout <<"begin partition matching\n";
21143         //int* dummy(0);
21144         int* output = Util::branchMPI(argParts, Indices,dimClasses, nParts, K, T, 0, n_guesses, LARGEST_CLASS, J, max_branching, stmult, branchfunc, LIM);
21145         
21146         //cout<<"total cost: "<<*output<<"\n";
21147         //cout<<"number of matches: "<<*(output+1)<<"\n";
21148         // 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
21149         bool correct = Util::sanitycheck(argParts, Indices,dimClasses, nParts, K, T,output);
21150 
21151         delete[] Indices;
21152 
21153         // something is wrong with output of branchMPI!
21154         if (correct < 1){
21155                 cout << "something is wrong with output of branchMPI!\n";
21156                 vector<int> ret(1);
21157                 ret[0] = -1;
21158                 if (output != 0)  { delete[] output; output = 0; }
21159                 return ret;
21160         }
21161 
21162         // output is not nonsense, so now put it into a single dimension vector and return
21163         // 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
21164         // and the rest is the list of matches. output is one dimensional
21165 
21166         int output_size = 2 + output[1] * nParts;
21167         vector<int> ret(output_size);
21168         for (int i = 0; i < output_size; i++) {
21169                 ret[i]= output[i];
21170         }
21171         if (output != 0) { delete[] output; output = 0; }
21172         return ret;
21173 
21174 }

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

Definition at line 2315 of file util_sparx.cpp.

References xim.

02316 {
02317 /*
02318 c  purpose: linear interpolation
02319   Optimized for speed, circular closer removed, checking of ranges removed
02320 */
02321     float bilinear;
02322     int   ixold, iyold;
02323 
02324 /*
02325         float xdif, ydif, xrem, yrem;
02326         ixold   = (int) floor(xold);
02327         iyold   = (int) floor(yold);
02328         ydif = yold - iyold;
02329         yrem = 1.0f - ydif;
02330 
02331         //  May want to insert if?
02332 //              IF ((IYOLD .GE. 1 .AND. IYOLD .LE. NROW-1) .AND.
02333 //     &            (IXOLD .GE. 1 .AND. IXOLD .LE. NSAM-1)) THEN
02334 //c                INSIDE BOUNDARIES OF OUTPUT IMAGE
02335         xdif = xold - ixold;
02336         xrem = 1.0f- xdif;
02337 //                 RBUF(K) = YDIF*(BUF(NADDR+NSAM)*XREM
02338 //     &                    +BUF(NADDR+NSAM+1)*XDIF)
02339 //     &                    +YREM*(BUF(NADDR)*XREM + BUF(NADDR+1)*XDIF)
02340         bilinear = ydif*(xim(ixold,iyold+1)*xrem + xim(ixold+1,iyold+1)*xdif) +
02341                                         yrem*(xim(ixold,iyold)*xrem+xim(ixold+1,iyold)*xdif);
02342 
02343     return bilinear;
02344 }
02345 */
02346         float xdif, ydif;
02347 
02348         ixold   = (int) xold;
02349         iyold   = (int) yold;
02350         ydif = yold - iyold;
02351 
02352         //  May want to insert it?
02353 //              IF ((IYOLD .GE. 1 .AND. IYOLD .LE. NROW-1) .AND.
02354 //     &            (IXOLD .GE. 1 .AND. IXOLD .LE. NSAM-1)) THEN
02355 //c                INSIDE BOUNDARIES OF OUTPUT IMAGE
02356         xdif = xold - ixold;
02357         bilinear = xim(ixold, iyold) + ydif* (xim(ixold, iyold+1) - xim(ixold, iyold)) +
02358                    xdif* (xim(ixold+1, iyold) - xim(ixold, iyold) +
02359                            ydif* (xim(ixold+1, iyold+1) - xim(ixold+1, iyold) - xim(ixold, iyold+1) + xim(ixold, iyold)) );
02360 
02361         return bilinear;
02362 }

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] p1 The first number. corresponding to (x0,y0).
[in] p2 The second number. corresponding to (x1,y0).
[in] p3 The third number. corresponding to (x1,y1).
[in] p4 The fourth number. corresponding to (x0,y1).
[in] t t
[in] u u
Returns:
The bilinear interpolation value.

Definition at line 1525 of file util.h.

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

void Util::BPCQ ( EMData B,
EMData CUBE,
vector< float >  DM 
) [static]

Definition at line 5731 of file util_sparx.cpp.

References B, CUBE, DM, and t.

05732 {
05733 
05734         float  *Bptr = B->get_data();
05735         float  *CUBEptr = CUBE->get_data();
05736 
05737         int NSAM,NROW,NX3D,NY3D,NZC,KZ,IQX,IQY,LDPX,LDPY,LDPZ,LDPNMX,LDPNMY,NZ1;
05738         float DIPX,DIPY,XB,YB,XBB,YBB;
05739 
05740         Transform * t = B->get_attr("xform.projection");
05741         Dict d = t->get_params("spider");
05742         if(t) {delete t; t=0;}
05743         //  Unsure about sign of shifts, check later PAP 06/28/09
05744         float x_shift = d[ "tx" ];
05745         float y_shift = d[ "ty" ];
05746         x_shift = -x_shift;
05747         y_shift = -y_shift;
05748 
05749         NSAM = B->get_xsize();
05750         NROW = B->get_ysize();
05751         NX3D = CUBE->get_xsize();
05752         NY3D = CUBE->get_ysize();
05753         NZC  = CUBE->get_zsize();
05754 
05755 
05756         LDPX   = NX3D/2 +1;
05757         LDPY   = NY3D/2 +1;
05758         LDPZ   = NZC/2 +1;
05759         LDPNMX = NSAM/2 +1;
05760         LDPNMY = NROW/2 +1;
05761         NZ1    = 1;
05762 
05763         for(int K=1;K<=NZC;K++) {
05764                 KZ=K-1+NZ1;
05765                 for(int J=1;J<=NY3D;J++) {
05766                         XBB = (1-LDPX)*DM(1)+(J-LDPY)*DM(2)+(KZ-LDPZ)*DM(3);
05767                         YBB = (1-LDPX)*DM(4)+(J-LDPY)*DM(5)+(KZ-LDPZ)*DM(6);
05768                         for(int I=1;I<=NX3D;I++) {
05769                                 XB  = (I-1)*DM(1)+XBB-x_shift;
05770                                 IQX = int(XB+float(LDPNMX));
05771                                 if (IQX <1 || IQX >= NSAM) continue;
05772                                 YB  = (I-1)*DM(4)+YBB-y_shift;
05773                                 IQY = int(YB+float(LDPNMY));
05774                                 if (IQY<1 || IQY>=NROW)  continue;
05775                                 DIPX = XB+LDPNMX-IQX;
05776                                 DIPY = YB+LDPNMY-IQY;
05777 
05778                                 CUBE(I,J,K) = CUBE(I,J,K)+B(IQX,IQY)+DIPY*(B(IQX,IQY+1)-B(IQX,IQY))+DIPX*(B(IQX+1,IQY)-B(IQX,IQY)+DIPY*(B(IQX+1,IQY+1)-B(IQX+1,IQY)-B(IQX,IQY+1)+B(IQX,IQY)));
05779                         }
05780                 }
05781         }
05782 }

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 21606 of file util_sparx.cpp.

References B, and jiafunc().

Referenced by branchMPI().

21606                                                                                                                  {
21607         
21608         int ntot=0;
21609         for (int jit=0; jit < J; jit++){
21610                 if (*(costlist+jit) > T) ntot++;
21611         }
21612 
21613         int cur;
21614         // sort matchlist by cost
21615         int* indx = new int[J];
21616         for (int jit=0; jit < J; jit++) indx[jit]=jit;
21617         vector<int> myindx (indx, indx+J);
21618         vector<int>::iterator it;
21619         costlist_global=costlist;
21620         sort(myindx.begin(), myindx.begin()+J, jiafunc);
21621 
21622         // put matchlist in the order of mycost
21623         int* templist = new int[J];
21624         int* temp2list = new int[J*nParts];
21625         int next = 0;
21626         
21627         for (it=myindx.begin(); it!=myindx.end();++it){
21628                 cur = *(costlist + *it);
21629                 if (cur > T){
21630                         
21631                         templist[next] = cur;
21632                         for (int vt = 0; vt < nParts; vt++) temp2list[next*nParts + vt] = matchlist[ (*it)*nParts + vt];
21633                         next = next + 1;
21634                 }
21635         }
21636         
21637         for (int jit=0; jit < ntot; jit++){
21638                 *(costlist+jit)=*(templist + jit);
21639                 //cout <<*(costlist+jit)<<", ";
21640                 for (int vit=0; vit < nParts; vit++) matchlist[jit*nParts + vit]= temp2list[jit*nParts + vit];
21641         }
21642         //cout <<"\n";
21643         
21644         for (int jit=1; jit < ntot; jit++){
21645         
21646              if ((costlist[jit] == costlist[0]) && costlist[jit] > T){
21647              
21648                      for (int vit=0; vit < nParts; vit++){
21649                              if ( matchlist[jit*nParts + vit] >  matchlist[vit])
21650                                  break;
21651                              if ( matchlist[jit*nParts + vit] ==  matchlist[vit])
21652                                  continue;
21653                              if ( matchlist[jit*nParts + vit] <  matchlist[vit])
21654                              {
21655                                  // swap
21656                                  for (int swp=0; swp < nParts; swp++){
21657                                        int tmp  = matchlist[swp];
21658                                        matchlist[swp]= matchlist[jit*nParts + swp];
21659                                        matchlist[jit*nParts + swp] = tmp;
21660                                  }
21661                                  break;
21662                              
21663                              }   
21664                      }
21665              }
21666         
21667         }
21668         
21669         
21670         delete [] indx;
21671         //compute the average 
21672         
21673         // partition matches into groups where matches in each group is mutually feasible, and matches from different groups are not feasible
21674         // The number of branches to explore are the mutually exclusive matches. Take the largest of these
21675         
21676         
21677         int B=1;
21678         int B_init=B;
21679         int infeasible=0;
21680         
21681         for (int i=B_init; i<ntot; i++){
21682                 if (i==ntot) continue;
21683                 // look at the i-th match. branch on it only if it is infeasible with ALL previous matches we have decided to
21684                 // branch on
21685                 infeasible = 0;
21686                 if (LIM < 0) LIM = B;
21687                 for (int j=0; j<B; j++){
21688                         
21689                         for (int vit=0; vit<nParts; vit++){
21690                                 if (temp2list[i*nParts+vit] == matchlist[j*nParts+vit]) {infeasible++; break;}
21691                         }
21692                         if (infeasible >= LIM) break;
21693                 }
21694                 
21695                 if (infeasible >= LIM){
21696                         *(costlist+B)=*(templist+i);
21697                         for (int vit=0; vit < nParts; vit++)
21698                                 *(matchlist+B*nParts + vit)=*(temp2list+i*nParts + vit);
21699                         B=B+1;  
21700                 }
21701         }
21702         
21703         delete [] templist;
21704         delete [] temp2list;
21705         //cout<<"**************************************** "<<B<<" ***************************\n";
21706         
21707         if (branch_all < max_branching){
21708                 if (B>1)
21709                         {branch_all = branch_all + B -1 ; }
21710         }
21711         else B=1;
21712         
21713         return B;
21714         
21715 
21716 }

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 21344 of file util_sparx.cpp.

References B, and jiafunc().

Referenced by branchMPI().

21344                                                                                                                  {
21345         
21346         int ntot=0;
21347         for (int jit=0; jit < J; jit++){
21348                 if (*(costlist+jit) > T) ntot++;
21349         }
21350 
21351         int cur;
21352         // sort matchlist by cost
21353         int* indx = new int[J];
21354         for (int jit=0; jit < J; jit++) indx[jit]=jit;
21355         vector<int> myindx (indx, indx+J);
21356         vector<int>::iterator it;
21357         costlist_global=costlist;
21358         sort(myindx.begin(), myindx.end(), jiafunc);
21359 
21360         // put matchlist in the order of mycost
21361         int* templist = new int[J];
21362         int* temp2list = new int[J*nParts];
21363         int next = 0;
21364         
21365         for (it=myindx.begin(); it!=myindx.end();++it){
21366                 cur = *(costlist + *it);
21367                 if (cur > T){
21368                         
21369                         templist[next] = cur;
21370                         for (int vt = 0; vt < nParts; vt++) temp2list[next*nParts + vt] = matchlist[ (*it)*nParts + vt];
21371                         next = next + 1;
21372                 }
21373         }
21374         
21375         for (int jit=0; jit < ntot; jit++){
21376                 *(costlist+jit)=*(templist + jit);
21377                 //cout <<*(costlist+jit)<<", ";
21378                 for (int vit=0; vit < nParts; vit++) matchlist[jit*nParts + vit]= temp2list[jit*nParts + vit];
21379         }
21380         //cout <<"\n";
21381         
21382         delete [] indx;
21383         //compute the average 
21384         
21385         // partition matches into groups where matches in each group is mutually feasible, and matches from different groups are not feasible
21386         // The number of branches to explore are the mutually exclusive matches. Take the largest of these
21387         
21388         
21389         int B=1;
21390         int B_init=B;
21391         int infeasible=0;
21392         
21393         for (int i=B_init; i<ntot; i++){
21394                 if (i==ntot) continue;
21395                 // look at the i-th match. branch on it only if it is infeasible with ALL previous matches we have decided to
21396                 // branch on
21397                 infeasible = 0;
21398                 if (LIM < 0) LIM = B;
21399                 for (int j=0; j<B; j++){
21400                         
21401                         for (int vit=0; vit<nParts; vit++){
21402                                 if (temp2list[i*nParts+vit] == matchlist[j*nParts+vit]) {infeasible++; break;}
21403                         }
21404                         if (infeasible >= LIM) break;
21405                 }
21406                 
21407                 if (infeasible >= LIM){
21408                         *(costlist+B)=*(templist+i);
21409                         for (int vit=0; vit < nParts; vit++)
21410                                 *(matchlist+B*nParts + vit)=*(temp2list+i*nParts + vit);
21411                         B=B+1;  
21412                 }
21413         }
21414         
21415         delete [] templist;
21416         delete [] temp2list;
21417         //cout<<"**************************************** "<<B<<" ***************************\n";
21418         
21419         if (branch_all < max_branching){
21420                 if (B>1)
21421                         {branch_all = branch_all + B -1 ; }
21422         }
21423         else B=1;
21424         
21425         return B;
21426         
21427 
21428 }

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 21432 of file util_sparx.cpp.

References B, and jiafunc().

Referenced by branchMPI().

21432                                                                                                                       {
21433         
21434         int ntot=0;
21435         for (int jit=0; jit < J; jit++){
21436                 if (*(costlist+jit) > T) ntot++;
21437         }
21438 
21439         int cur;
21440         // sort matchlist by cost
21441         int* indx = new int[J];
21442         for (int jit=0; jit < J; jit++) indx[jit]=jit;
21443         vector<int> myindx (indx, indx+J);
21444         vector<int>::iterator it;
21445         costlist_global=costlist;
21446         sort(myindx.begin(), myindx.begin()+J, jiafunc);
21447 
21448         // put matchlist in the order of mycost
21449         int* templist = new int[J];
21450         int* temp2list = new int[J*nParts];
21451         int next = 0;
21452         
21453         for (it=myindx.begin(); it!=myindx.end();++it){
21454                 cur = *(costlist + *it);
21455                 if (cur > T){
21456                         
21457                         templist[next] = cur;
21458                         for (int vt = 0; vt < nParts; vt++) temp2list[next*nParts + vt] = matchlist[ (*it)*nParts + vt];
21459                         next = next + 1;
21460                 }
21461         }
21462         
21463         for (int jit=0; jit < ntot; jit++){
21464                 *(costlist+jit)=*(templist + jit);
21465                 //cout <<*(costlist+jit)<<", ";
21466                 for (int vit=0; vit < nParts; vit++) matchlist[jit*nParts + vit]= temp2list[jit*nParts + vit];
21467         }
21468         //cout <<"\n";
21469         
21470         delete [] indx;
21471         //compute the average 
21472         
21473         // partition matches into groups where matches in each group is mutually feasible, and matches from different groups are not feasible
21474         // The number of branches to explore are the mutually exclusive matches. Take the largest of these
21475         
21476         
21477         int B=1;
21478         int B_init=B;
21479         int infeasible=0;
21480         // if we're near the bottom of the tree then explore more... this is because the larger weights are not likely to change much,
21481         // whereas the smaller ones can have many permutations
21482         if (LIM < 0) LIM = ntot-1;
21483         for (int i=B_init; i<ntot; i++){
21484                 if (i==ntot) continue;
21485                 // look at the i-th match. branch on it only if it is infeasible with ALL previous matches we have decided to
21486                 // branch on
21487                 infeasible = 0;
21488                 
21489                 for (int j=0; j<ntot; j++){
21490                         if (j == i) continue;
21491                         for (int vit=0; vit<nParts; vit++){
21492                                 if (temp2list[i*nParts+vit] == temp2list[j*nParts+vit]) {infeasible++; break;}
21493                         }
21494                         if (infeasible >= LIM) break;
21495                 }
21496                 
21497                 if (infeasible >= LIM){
21498                         *(costlist+B)=*(templist+i);
21499                         for (int vit=0; vit < nParts; vit++)
21500                                 *(matchlist+B*nParts + vit)=*(temp2list+i*nParts + vit);
21501                         B=B+1;  
21502                 }
21503         }
21504         
21505         delete [] templist;
21506         delete [] temp2list;
21507         //cout<<"**************************************** "<<B<<" ***************************\n";
21508         
21509         
21510         if (branch_all < max_branching){
21511                 if (B>1)
21512                         {branch_all = branch_all + B-1;}
21513         }
21514         else B=1;
21515         
21516         return B;
21517         
21518 
21519 }

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 21525 of file util_sparx.cpp.

References B, and jiafunc().

Referenced by branchMPI().

21525                                                                                                                       {
21526         int sum=0;
21527         float average =0;
21528         int ntot=0;
21529         for (int jit=0; jit < J; jit++){
21530                 if (*(costlist+jit) > T) {ntot++; sum = sum +*(costlist+jit);}
21531         }
21532         average = ((float)sum)/((float)ntot);
21533         int cur;
21534         // sort matchlist by cost
21535         int* indx = new int[J];
21536         for (int jit=0; jit < J; jit++) indx[jit]=jit;
21537         vector<int> myindx (indx, indx+J);
21538         vector<int>::iterator it;
21539         costlist_global=costlist;
21540         sort(myindx.begin(), myindx.begin()+J, jiafunc);
21541 
21542         // put matchlist in the order of mycost
21543         int* templist = new int[J];
21544         int* temp2list = new int[J*nParts];
21545         int next = 0;
21546         
21547         for (it=myindx.begin(); it!=myindx.end();++it){
21548                 cur = *(costlist + *it);
21549                 if (cur > T){
21550                         
21551                         templist[next] = cur;
21552                         for (int vt = 0; vt < nParts; vt++) temp2list[next*nParts + vt] = matchlist[ (*it)*nParts + vt];
21553                         next = next + 1;
21554                 }
21555         }
21556         
21557         for (int jit=0; jit < ntot; jit++){
21558                 *(costlist+jit)=*(templist + jit);
21559                 //cout <<*(costlist+jit)<<", ";
21560                 for (int vit=0; vit < nParts; vit++) matchlist[jit*nParts + vit]= temp2list[jit*nParts + vit];
21561         }
21562         //cout <<"\n";
21563         
21564         delete [] indx;
21565         delete [] templist;
21566         delete [] temp2list;
21567         
21568         if (ntot == 1) return 1;
21569         
21570         // look at the average, standard dev etc. If standard dev very small, i.e., costs very similar, then branch on the similar
21571         // costs
21572         float sq_sum=0.0;
21573         //cout <<"costlist:";
21574         for (int i=0; i< ntot; i++){
21575                 sq_sum = sq_sum + (float) pow((float) *(costlist+i) - average, (float)2.0);
21576                 //cout <<*(costlist+i)<<", ";
21577         }       
21578         //cout <<"\n";
21579         
21580         float variance = sq_sum/ntot;
21581         float stdev = (float)pow((float)variance,(float)0.5);
21582         
21583         //cout <<"stdev: "<<int(stdev)<<"\n";
21584         
21585         int B=1;
21586         int largest = *costlist;
21587         //cout <<"largest: "<<largest<<"\n";
21588         for (int i=1; i<ntot; i++){
21589                 int cur = *(costlist+i);
21590                 if (largest-cur < (float)(stdev*stmult)) B++;
21591                 else break;
21592         
21593         }
21594         //cout <<"B: "<<B<<"\n";
21595         if (branch_all < max_branching){
21596                 if (B>1)
21597                         {branch_all = branch_all + B-1;}
21598         }
21599         else B=1;
21600         
21601         return B;
21602         
21603 
21604 }

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 21178 of file util_sparx.cpp.

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

21179                                                                                 {
21180 
21181 //*************************************
21182 //testing search2
21183 if (1 == 0){
21184 cout <<"begin test search2\n";
21185 int* matchlist = new int[J*nParts];
21186 int* costlist = new int[J];
21187 for (int jit = 0; jit < nParts; jit++) *(costlist+jit) = 0;
21188 Util::search2(argParts,Indices, dimClasses, nParts, K,  T, matchlist,costlist,J);
21189 
21190 for (int jit = 0; jit < J; jit++) {
21191   cout << *(costlist +jit)<<": ";
21192   for (int yit = 0; yit < nParts; yit++)
21193         cout << *(matchlist + jit*nParts + yit)<<",";
21194   cout <<"\n";  
21195 
21196 }
21197 cout <<"end test search2\n";
21198 int* output = new int[1];
21199 output[0] = 1;
21200 delete [] matchlist;
21201 delete [] costlist;
21202 return output;
21203 }
21204 //**************************************
21205 
21206         // Base Case: we're at a leaf, no more feasible matches possible
21207         if (curlevel > K -1){
21208                 int* output = new int[2];
21209                 output[0] = 0;
21210                 output[1] = 0;
21211                 return output;
21212         }
21213 
21214         // branch dynamically depending on results of search 2!
21215         
21216         int* matchlist = new int[J*nParts];
21217         int* costlist = new int[J];
21218         Util::search2(argParts, Indices, dimClasses, nParts, K,  T, matchlist, costlist, J);
21219         
21220         
21221         // each class in the matches found by findTopLargest is encoded by the original index of the first element of the class in argPart
21222         // each match contains nParts classes, with the i-th class belonging to the i-th partition.
21223 
21224         // if there are no feasible matches with cost gt T, then return 0
21225         for (int jit = 0; jit < J ; jit++){
21226         
21227                 if (costlist[jit] > T) break;
21228                 if (jit == J-1){
21229                         int* output = new int[2];
21230                         output[0] = 0;
21231                         output[1] = 0;
21232                         delete[] matchlist;
21233                         delete[] costlist;
21234                         return output;
21235                 }
21236         }
21237         
21238 
21239         
21240         // note that costlist and matchlist are NOT sorted by weight, and branch factor takes care of that...
21241         if (curlevel==0) branch_all = 0;
21242         
21243         int nBranches = -1;
21244 
21245         if (branchfunc == 0)
21246                 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
21247 
21248         if (branchfunc == 2)
21249                 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
21250 
21251         if (branchfunc == 3)
21252                 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
21253 
21254         if (branchfunc == 4)
21255                 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
21256 
21257         int* newcostlist= new int[nBranches];
21258         int* newmatchlist = new int[nBranches*nParts];
21259         for (int i=0; i<nBranches; i++){
21260                 newcostlist[i] = costlist[i];
21261                 for (int j=0; j< nParts; j++)
21262                         newmatchlist[i*nParts + j] = matchlist[i*nParts + j];
21263         }
21264 
21265         delete[] costlist;
21266         delete[] matchlist;
21267         
21268         //int* output = new int[2];//initialize to placeholder
21269         int* output = new int[2+K*nParts];//initialize to placeholder
21270         output[0] = 0;
21271         output[1] = 0;
21272         // some temporary variables
21273         int old_index;
21274         int totalcost;
21275         int nmatches;
21276         //int offset;
21277 
21278         for(int i=0; i < nBranches ; i++){
21279 
21280                 // consider the i-th match returned by findTopLargest
21281                 //if (newcostlist[i] <= T) continue;
21282 
21283                 // 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.
21284                 // After branch returns, compute overall cost, unmark  the classes just marked as 1 again in preparation for next loop.
21285 
21286                 for(int j=0; j < nParts; j++){
21287                         // matchlist[i*nParts + j] is the original index of the class belonging to the j-th partition in the i-th match.
21288                         old_index = newmatchlist[i*nParts + j];
21289                         argParts[Indices[j*K+old_index] + 1] = -2;
21290                 }
21291 
21292                 
21293                 int* ret = Util::branchMPI(argParts, Indices, dimClasses, nParts, K, T, curlevel+1, n_guesses, LARGEST_CLASS,
21294                 J, max_branching, stmult,branchfunc, LIM);
21295                 
21296                 // first element of ret is the total cost of all the matches in ret, and second element is the number of matches in ret
21297                 totalcost = newcostlist[i] + ret[0];
21298 
21299                 //if (curlevel == 0) {
21300                 //      cout <<"totalcost*****************************************************************: "<<totalcost<<", costlist["<<i<<"]="<<newcostlist[i]<<", *ret="<<*ret<<", level: "<<curlevel<<"\n";
21301                         
21302                 //}
21303                 if (totalcost > output[0]) // option 1
21304                 {
21305                         nmatches = 1 + ret[1];
21306                         //delete[] output; // get rid of the old maxreturn
21307                         //output = new int[2+nmatches*nParts];
21308                         output[0] = totalcost;
21309                         output[1] = nmatches;
21310                         int nret = 2+(nmatches-1)*nParts;
21311                         for(int iret=2; iret < nret; iret++) output[iret] = ret[iret];
21312                         for(int imax=0; imax < nParts; imax++) output[nret+imax] = newmatchlist[i*nParts + imax];
21313                 }
21314 
21315 
21316                 delete[] ret;
21317 
21318                 // unmark the marked classes in preparation for the next iteration
21319 
21320                 for(int j=0; j < nParts; j++){
21321                         old_index = newmatchlist[i*nParts + j];
21322                         argParts[Indices[j*K+old_index] + 1] = 1;
21323                 }
21324 
21325         }
21326 
21327         delete[] newmatchlist;
21328         delete[] newcostlist;
21329         
21330         return output;
21331 }

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] low low size the search starts with.
Returns:
The best FFT size.

Definition at line 984 of file util.cpp.

References Assert, and LOGERR.

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

00985 {
00986         Assert(low >= 0);
00987 
00988         //array containing valid sizes <1024 for speed
00989         static char *valid = NULL;
00990 
00991         if (!valid) {
00992                 valid = (char *) calloc(4096, 1);
00993 
00994                 for (float i2 = 1; i2 < 12.0; i2 += 1.0) {
00995 
00996                         float f1 = pow((float) 2.0, i2);
00997                         for (float i3 = 0; i3 < 8.0; i3 += 1.0) {
00998 
00999                                 float f2 = pow((float) 3.0, i3);
01000                                 for (float i5 = 0; i5 < 6.0; i5 += 1.0) {
01001 
01002                                         float f3 = pow((float) 5.0, i5);
01003                                         for (float i7 = 0; i7 < 5.0; i7 += 1.0) {
01004 
01005                                                 float f = f1 * f2 * f3 * pow((float) 7.0, i7);
01006                                                 if (f <= 4095.0) {
01007                                                         int n = (int) f;
01008                                                         valid[n] = 1;
01009                                                 }
01010                                         }
01011                                 }
01012                         }
01013                 }
01014         }
01015 
01016         for (int i = low; i < 4096; i++) {
01017                 if (valid[i]) {
01018                         return i;
01019                 }
01020         }
01021 
01022         LOGERR("Sorry, can only find good fft sizes up to 4096 right now.");
01023 
01024         return 1;
01025 }

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] points a 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 548 of file util.cpp.

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

00548                                                              {
00549 unsigned int i;
00550 
00551 // various sums used in the final solution
00552 double Sx=0,Sy=0,Sxy=0,Sxx=0,Syy=0,Sz=0,Sxz=0,Syz=0,S=0;
00553 for (i=0; i<p.size(); i+=3) {
00554         Sx+=p[i];
00555         Sy+=p[i+1];
00556         Sz+=p[i+2];
00557         Sxx+=p[i]*p[i];
00558         Syy+=p[i+1]*p[i+1];
00559         Sxy+=p[i]*p[i+1];
00560         S+=1.0;
00561         Sxz+=p[i]*p[i+2];
00562         Syz+=p[i+1]*p[i+2];
00563 }
00564 double d=S*Sxy*Sxy - 2*Sx*Sxy*Sy + Sxx*Sy*Sy  + Sx*Sx*Syy - S*Sxx*Syy;
00565 
00566 Vec3f ret(0,0,0);
00567 
00568 ret[0]=static_cast<float>(-((Sxy*Sxz*Sy - Sx*Sxz*Syy + Sx*Sxy*Syz - Sxx*Sy*Syz - Sxy*Sxy*Sz +Sxx*Syy*Sz)/d));
00569 ret[1]=static_cast<float>(-((-Sxz*Sy*Sy  + S*Sxz*Syy - S*Sxy*Syz + Sx*Sy*Syz + Sxy*Sy*Sz -Sx*Syy*Sz) /d));
00570 ret[2]=static_cast<float>(-((-S*Sxy*Sxz + Sx*Sxz*Sy - Sx*Sx*Syz + S*Sxx*Syz + Sx*Sxy*Sz -Sxx*Sy*Sz) /d));
00571 
00572 return ret;
00573 }

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] nitems Number of items in array data_x and data_y.
[in] data_x x data array.
[in] data_y y data array. It should have the same number of items to data_x.
[out] p_slope pointer to the result slope.
[out] p_intercept pointer to the result intercept.
[in] ignore_zero If true, ignore data where either x or y is 0. If false, includes all 0.
[in] absmax Ignores values in y more than absmax from zero

Definition at line 509 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().

00511 {
00512         Assert(nitems > 0);
00513 
00514         if (!data_x || !data_y || !slope || !intercept) {
00515                 throw NullPointerException("null float pointer");
00516         }
00517         double sum = 0;
00518         double sum_x = 0;
00519         double sum_y = 0;
00520         double sum_xx = 0;
00521         double sum_xy = 0;
00522 
00523         for (size_t i = 0; i < nitems; i++) {
00524                 if ((!ignore_zero || (data_x[i] != 0 && data_y[i] != 0))&&(!absmax ||(data_y[i]<absmax && data_y[i]>-absmax))) {
00525                         double y = data_y[i];
00526                         double x = i;
00527                         if (data_x) {
00528                                 x = data_x[i];
00529                         }
00530 
00531                         sum_x += x;
00532                         sum_y += y;
00533                         sum_xx += x * x;
00534                         sum_xy += x * y;
00535                         sum++;
00536                 }
00537         }
00538 
00539         double div = sum * sum_xx - sum_x * sum_x;
00540         if (div == 0) {
00541                 div = 0.0000001f;
00542         }
00543 
00544         *intercept = (float) ((sum_xx * sum_y - sum_x * sum_xy) / div);
00545         *slope = (float) ((sum * sum_xy - sum_x * sum_y) / div);
00546 }

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 6386 of file util_sparx.cpp.

References lsfit().

06387 {
06388     long int q2_dim1, q2_offset, q_dim1, q_offset, i__1, i__2;
06389     float r__1;
06390     int tmp__i;
06391     long int i__, j;
06392     --s;
06393     --res;
06394     iu -= 3;
06395     cu -= 3;
06396     --x;
06397     long int klm2d;
06398     klm2d= *k+*k+2;
06399     klm2d=klm2d+klm2d;
06400     q_dim1 = klm2d;
06401     q_offset = 1 + q_dim1;
06402     q -= q_offset;
06403     q2_dim1 = klm2d;
06404     q2_offset = 1 + q2_dim1;
06405     q2 -= q2_offset;
06406     i__2=0;
06407     i__1 = *n - 1;
06408     tmp__i=0;
06409     for (j = 1; j <= i__1; ++j) {
06410         i__2 = *k;
06411         tmp__i+=1;
06412         for (i__ = 1; i__ <= i__2; ++i__) {
06413             r__1 = float(i__ - 1) /(float) *k / (*ps * 2);
06414             q2[i__ + j * q2_dim1] = pow(r__1, tmp__i);
06415         }
06416     }
06417     for  (i__ = 1; i__ <= i__2; ++i__)
06418       { q2[i__ + *n * q2_dim1] = 1.f;
06419             q2[i__ + (*n + 1) * q2_dim1] = pw[i__-1];
06420         }
06421    vector<float> fit_res;
06422    fit_res=Util::lsfit(k, n, &klm2d, iswi, &q2[q2_offset], &q[q_offset], &x[1], &res[1], &cu[3], &s[1], &iu[3]);
06423    return fit_res;
06424 }

Dict Util::CANG ( float  PHI,
float  THETA,
float  PSI 
) [static]

Definition at line 5683 of file util_sparx.cpp.

References DGR_TO_RAD, DM, and SS.

05684 {
05685         double CPHI,SPHI,CTHE,STHE,CPSI,SPSI;
05686         vector<float>   DM,SS;
05687 
05688         for(int i =0;i<9;i++) DM.push_back(0);
05689 
05690         for(int i =0;i<6;i++) SS.push_back(0);
05691 
05692         CPHI = cos(double(PHI)*DGR_TO_RAD);
05693         SPHI = sin(double(PHI)*DGR_TO_RAD);
05694         CTHE = cos(double(THETA)*DGR_TO_RAD);
05695         STHE = sin(double(THETA)*DGR_TO_RAD);
05696         CPSI = cos(double(PSI)*DGR_TO_RAD);
05697         SPSI = sin(double(PSI)*DGR_TO_RAD);
05698 
05699         SS(1) = float(CPHI);
05700         SS(2) = float(SPHI);
05701         SS(3) = float(CTHE);
05702         SS(4) = float(STHE);
05703         SS(5) = float(CPSI);
05704         SS(6) = float(SPSI);
05705 
05706         DM(1) = float(CPHI*CTHE*CPSI-SPHI*SPSI);
05707         DM(2) = float(SPHI*CTHE*CPSI+CPHI*SPSI);
05708         DM(3) = float(-STHE*CPSI);
05709         DM(4) = float(-CPHI*CTHE*SPSI-SPHI*CPSI);
05710         DM(5) = float(-SPHI*CTHE*SPSI+CPHI*CPSI);
05711         DM(6) = float(STHE*SPSI);
05712         DM(7) = float(STHE*CPHI);
05713         DM(8) = float(STHE*SPHI);
05714         DM(9) = float(CTHE);
05715 
05716         Dict DMnSS;
05717         DMnSS["DM"] = DM;
05718         DMnSS["SS"] = SS;
05719 
05720         return(DMnSS);
05721 }

float Util::ccc_images ( EMData ,
EMData ,
EMData ,
float  ,
float  ,
float   
) [static]

Definition at line 19838 of file util_sparx.cpp.

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

Referenced by twoD_fine_ali_SD().

19838                                                                                                 {
19839 
19840         EMData *rot= new EMData();
19841         float ccc;
19842 
19843         rot = image->rot_scale_trans2D(ang, sx, sy, 1.0);
19844         ccc = -rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19845         delete rot;
19846         return ccc;
19847 }

float Util::ccc_images_G ( EMData image,
EMData refim,
EMData mask,
Util::KaiserBessel kb,
float  ang,
float  sx,
float  sy 
) [static]

Definition at line 19875 of file util_sparx.cpp.

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

Referenced by twoD_fine_ali_SD_G().

19875                                                                                                                         {
19876 
19877         EMData *rot= new EMData();
19878         float ccc;
19879 
19880         rot = image->rot_scale_conv7(static_cast<float>(ang*pi/180.0), sx, sy, kb, 1.0f);
19881         ccc = -rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19882         delete rot;
19883         return ccc;
19884 }

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_filename Old filename.
[in] new_ext The 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 430 of file util.cpp.

References Assert.

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

00432 {
00433         Assert(old_filename != "");
00434         if (ext == "") {
00435                 return old_filename;
00436         }
00437 
00438         string filename = old_filename;
00439         size_t dot_pos = filename.rfind(".");
00440         if (dot_pos != string::npos) {
00441                 filename = filename.substr(0, dot_pos+1);
00442         }
00443         else {
00444                 filename = filename + ".";
00445         }
00446         filename = filename + ext;
00447         return filename;
00448 }

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_block The first block of the file.
magic The magic string to identify a file format.
Returns:
True if file matches magic. Otherwise, false.

Definition at line 194 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().

00195 {
00196         if (!first_block || !magic) {
00197                 throw NullPointerException("first_block/magic");
00198         }
00199 
00200         const char *buf = static_cast < const char *>(first_block);
00201 
00202         if (strncmp(buf, magic, strlen(magic)) == 0) {
00203                 return true;
00204         }
00205         return false;
00206 }

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 6571 of file util_sparx.cpp.

References abs, and in.

06573 {
06574 
06575     long int q_dim1, q_offset, i__1, i__2;
06576     double d__1;
06577 
06578     static long int i__, j;
06579     static double z__;
06580     static long int n1, n2, ia, ii, kk, in, nk, js;
06581     static double sn, zu, zv;
06582     static long int nk1, klm, nkl, jmn, jpn;
06583     static double cuv;
06584     static long int klm1, nkl1, klm2, kode, iimn, nklm, iter;
06585     static float xmin;
06586     static double xmax;
06587     static long int iout;
06588     static double xsum;
06589     static long int iineg, maxit;
06590     static double toler;
06591     static float error;
06592     static double pivot;
06593     static long int kforce, iphase;
06594     static double tpivot;
06595 
06596     --s;
06597     --res;
06598     iu -= 3;
06599     cu -= 3;
06600     --x;
06601     q_dim1 = *klm2d;
06602     q_offset = 1 + q_dim1;
06603     q -= q_offset;
06604 
06605     /* Function Body */
06606     maxit = 500;
06607     kode = 0;
06608     toler = 1e-4f;
06609     iter = 0;
06610     n1 = *n + 1;
06611     n2 = *n + 2;
06612     nk = *n + *k;
06613     nk1 = nk + 1;
06614     nkl = nk + *l;
06615     nkl1 = nkl + 1;
06616     klm = *k + *l + *m;
06617     klm1 = klm + 1;
06618     klm2 = klm + 2;
06619     nklm = *n + klm;
06620     kforce = 1;
06621     iter = 0;
06622     js = 1;
06623     ia = 0;
06624 /* SET UP LABELS IN Q. */
06625     i__1 = *n;
06626     for (j = 1; j <= i__1; ++j) {
06627         q[klm2 + j * q_dim1] = (double) j;
06628 /* L10: */
06629     }
06630     i__1 = klm;
06631     for (i__ = 1; i__ <= i__1; ++i__) {
06632         q[i__ + n2 * q_dim1] = (double) (*n + i__);
06633         if (q[i__ + n1 * q_dim1] >= 0.f) {
06634             goto L30;
06635         }
06636         i__2 = n2;
06637         for (j = 1; j <= i__2; ++j) {
06638             q[i__ + j * q_dim1] = -q[i__ + j * q_dim1];
06639 /* L20: */
06640         }
06641 L30:
06642         ;
06643     }
06644 /* SET UP PHASE 1 COSTS. */
06645     iphase = 2;
06646     i__1 = nklm;
06647     for (j = 1; j <= i__1; ++j) {
06648         cu[(j << 1) + 1] = 0.f;
06649         cu[(j << 1) + 2] = 0.f;
06650         iu[(j << 1) + 1] = 0;
06651         iu[(j << 1) + 2] = 0;
06652 /* L40: */
06653     }
06654     if (*l == 0) {
06655         goto L60;
06656     }
06657     i__1 = nkl;
06658     for (j = nk1; j <= i__1; ++j) {
06659         cu[(j << 1) + 1] = 1.f;
06660         cu[(j << 1) + 2] = 1.f;
06661         iu[(j << 1) + 1] = 1;
06662         iu[(j << 1) + 2] = 1;
06663 /* L50: */
06664     }
06665     iphase = 1;
06666 L60:
06667     if (*m == 0) {
06668         goto L80;
06669     }
06670     i__1 = nklm;
06671     for (j = nkl1; j <= i__1; ++j) {
06672         cu[(j << 1) + 2] = 1.f;
06673         iu[(j << 1) + 2] = 1;
06674         jmn = j - *n;
06675         if (q[jmn + n2 * q_dim1] < 0.f) {
06676             iphase = 1;
06677         }
06678 /* L70: */
06679     }
06680 L80:
06681     if (kode == 0) {
06682         goto L150;
06683     }
06684     i__1 = *n;
06685     for (j = 1; j <= i__1; ++j) {
06686         if ((d__1 = x[j]) < 0.) {
06687             goto L90;
06688         } else if (d__1 == 0) {
06689             goto L110;
06690         } else {
06691             goto L100;
06692         }
06693 L90:
06694         cu[(j << 1) + 1] = 1.f;
06695         iu[(j << 1) + 1] = 1;
06696         goto L110;
06697 L100:
06698         cu[(j << 1) + 2] = 1.f;
06699         iu[(j << 1) + 2] = 1;
06700 L110:
06701         ;
06702     }
06703     i__1 = *k;
06704     for (j = 1; j <= i__1; ++j) {
06705         jpn = j + *n;
06706         if ((d__1 = res[j]) < 0.) {
06707             goto L120;
06708         } else if (d__1 == 0) {
06709             goto L140;
06710         } else {
06711             goto L130;
06712         }
06713 L120:
06714         cu[(jpn << 1) + 1] = 1.f;
06715         iu[(jpn << 1) + 1] = 1;
06716         if (q[j + n2 * q_dim1] > 0.f) {
06717             iphase = 1;
06718         }
06719         goto L140;
06720 L130:
06721         cu[(jpn << 1) + 2] = 1.f;
06722         iu[(jpn << 1) + 2] = 1;
06723         if (q[j + n2 * q_dim1] < 0.f) {
06724             iphase = 1;
06725         }
06726 L140:
06727         ;
06728     }
06729 L150:
06730     if (iphase == 2) {
06731         goto L500;
06732     }
06733 /* COMPUTE THE MARGINAL COSTS. */
06734 L160:
06735     i__1 = n1;
06736     for (j = js; j <= i__1; ++j) {
06737         xsum = 0.;
06738         i__2 = klm;
06739         for (i__ = 1; i__ <= i__2; ++i__) {
06740             ii = (long int) q[i__ + n2 * q_dim1];
06741             if (ii < 0) {
06742                 goto L170;
06743             }
06744             z__ = cu[(ii << 1) + 1];
06745             goto L180;
06746 L170:
06747             iineg = -ii;
06748             z__ = cu[(iineg << 1) + 2];
06749 L180:
06750             xsum += q[i__ + j * q_dim1] * z__;
06751 /*  180       XSUM = XSUM + Q(I,J)*Z */
06752 /* L190: */
06753         }
06754         q[klm1 + j * q_dim1] = xsum;
06755 /* L200: */
06756     }
06757     i__1 = *n;
06758     for (j = js; j <= i__1; ++j) {
06759         ii = (long int) q[klm2 + j * q_dim1];
06760         if (ii < 0) {
06761             goto L210;
06762         }
06763         z__ = cu[(ii << 1) + 1];
06764         goto L220;
06765 L210:
06766         iineg = -ii;
06767         z__ = cu[(iineg << 1) + 2];
06768 L220:
06769         q[klm1 + j * q_dim1] -= z__;
06770 /* L230: */
06771     }
06772 /* DETERMINE THE VECTOR TO ENTER THE BASIS. */
06773 L240:
06774     xmax = 0.f;
06775     if (js > *n) {
06776         goto L490;
06777     }
06778     i__1 = *n;
06779     for (j = js; j <= i__1; ++j) {
06780         zu = q[klm1 + j * q_dim1];
06781         ii = (long int) q[klm2 + j * q_dim1];
06782         if (ii > 0) {
06783             goto L250;
06784         }
06785         ii = -ii;
06786         zv = zu;
06787         zu = -zu - cu[(ii << 1) + 1] - cu[(ii << 1) + 2];
06788         goto L260;
06789 L250:
06790         zv = -zu - cu[(ii << 1) + 1] - cu[(ii << 1) + 2];
06791 L260:
06792         if (kforce == 1 && ii > *n) {
06793             goto L280;
06794         }
06795         if (iu[(ii << 1) + 1] == 1) {
06796             goto L270;
06797         }
06798         if (zu <= xmax) {
06799             goto L270;
06800         }
06801         xmax = zu;
06802         in = j;
06803 L270:
06804         if (iu[(ii << 1) + 2] == 1) {
06805             goto L280;
06806         }
06807         if (zv <= xmax) {
06808             goto L280;
06809         }
06810         xmax = zv;
06811         in = j;
06812 L280:
06813         ;
06814     }
06815     if (xmax <= toler) {
06816         goto L490;
06817     }
06818     if (q[klm1 + in * q_dim1] == xmax) {
06819         goto L300;
06820     }
06821     i__1 = klm2;
06822     for (i__ = 1; i__ <= i__1; ++i__) {
06823         q[i__ + in * q_dim1] = -q[i__ + in * q_dim1];
06824 /* L290: */
06825     }
06826     q[klm1 + in * q_dim1] = xmax;
06827 /* DETERMINE THE VECTOR TO LEAVE THE BASIS. */
06828 L300:
06829     if (iphase == 1 || ia == 0) {
06830         goto L330;
06831     }
06832     xmax = 0.f;
06833     i__1 = ia;
06834     for (i__ = 1; i__ <= i__1; ++i__) {
06835         z__ = (d__1 = q[i__ + in * q_dim1], abs(d__1));
06836         if (z__ <= xmax) {
06837             goto L310;
06838         }
06839         xmax = z__;
06840         iout = i__;
06841 L310:
06842         ;
06843     }
06844     if (xmax <= toler) {
06845         goto L330;
06846     }
06847     i__1 = n2;
06848     for (j = 1; j <= i__1; ++j) {
06849         z__ = q[ia + j * q_dim1];
06850         q[ia + j * q_dim1] = q[iout + j * q_dim1];
06851         q[iout + j * q_dim1] = z__;
06852 /* L320: */
06853     }
06854     iout = ia;
06855     --ia;
06856     pivot = q[iout + in * q_dim1];
06857     goto L420;
06858 L330:
06859     kk = 0;
06860     i__1 = klm;
06861     for (i__ = 1; i__ <= i__1; ++i__) {
06862         z__ = q[i__ + in * q_dim1];
06863         if (z__ <= toler) {
06864             goto L340;
06865         }
06866         ++kk;
06867         res[kk] = q[i__ + n1 * q_dim1] / z__;
06868         s[kk] = (double) i__;
06869 L340:
06870         ;
06871     }
06872 L350:
06873     if (kk > 0) {
06874         goto L360;
06875     }
06876     kode = 2;
06877     goto L590;
06878 L360:
06879     xmin = static_cast<float>( res[1] );
06880     iout = (long int) s[1];
06881     j = 1;
06882     if (kk == 1) {
06883         goto L380;
06884     }
06885     i__1 = kk;
06886     for (i__ = 2; i__ <= i__1; ++i__) {
06887         if (res[i__] >= xmin) {
06888             goto L370;
06889         }
06890         j = i__;
06891         xmin = static_cast<float>( res[i__] );
06892         iout = (long int) s[i__];
06893 L370:
06894         ;
06895     }
06896     res[j] = res[kk];
06897     s[j] = s[kk];
06898 L380:
06899     --kk;
06900     pivot = q[iout + in * q_dim1];
06901     ii = (long int) q[iout + n2 * q_dim1];
06902     if (iphase == 1) {
06903         goto L400;
06904     }
06905     if (ii < 0) {
06906         goto L390;
06907     }
06908     if (iu[(ii << 1) + 2] == 1) {
06909         goto L420;
06910     }
06911     goto L400;
06912 L390:
06913     iineg = -ii;
06914     if (iu[(iineg << 1) + 1] == 1) {
06915         goto L420;
06916     }
06917 /* 400 II = IABS(II) */
06918 L400:
06919     ii = abs(ii);
06920     cuv = cu[(ii << 1) + 1] + cu[(ii << 1) + 2];
06921     if (q[klm1 + in * q_dim1] - pivot * cuv <= toler) {
06922         goto L420;
06923     }
06924 /* BYPASS INTERMEDIATE VERTICES. */
06925     i__1 = n1;
06926     for (j = js; j <= i__1; ++j) {
06927         z__ = q[iout + j * q_dim1];
06928         q[klm1 + j * q_dim1] -= z__ * cuv;
06929         q[iout + j * q_dim1] = -z__;
06930 /* L410: */
06931     }
06932     q[iout + n2 * q_dim1] = -q[iout + n2 * q_dim1];
06933     goto L350;
06934 /* GAUSS-JORDAN ELIMINATION. */
06935 L420:
06936     if (iter < maxit) {
06937         goto L430;
06938     }
06939     kode = 3;
06940     goto L590;
06941 L430:
06942     ++iter;
06943     i__1 = n1;
06944     for (j = js; j <= i__1; ++j) {
06945         if (j != in) {
06946             q[iout + j * q_dim1] /= pivot;
06947         }
06948 /* L440: */
06949     }
06950 /* IF PERMITTED, USE SUBROUTINE COL OF THE DESCRIPTION */
06951 /* SECTION AND REPLACE THE FOLLOWING SEVEN STATEMENTS DOWN */
06952 /* TO AND INCLUDING STATEMENT NUMBER 460 BY.. */
06953 /*     DO 460 J=JS,N1 */
06954 /*        IF(J .EQ. IN) GO TO 460 */
06955 /*        Z = -Q(IOUT,J) */
06956 /*        CALL COL(Q(1,J), Q(1,IN), Z, IOUT, KLM1) */
06957 /* 460 CONTINUE */
06958     i__1 = n1;
06959     for (j = js; j <= i__1; ++j) {
06960         if (j == in) {
06961             goto L460;
06962         }
06963         z__ = -q[iout + j * q_dim1];
06964         i__2 = klm1;
06965         for (i__ = 1; i__ <= i__2; ++i__) {
06966             if (i__ != iout) {
06967                 q[i__ + j * q_dim1] += z__ * q[i__ + in * q_dim1];
06968             }
06969 /* L450: */
06970         }
06971 L460:
06972         ;
06973     }
06974     tpivot = -pivot;
06975     i__1 = klm1;
06976     for (i__ = 1; i__ <= i__1; ++i__) {
06977         if (i__ != iout) {
06978             q[i__ + in * q_dim1] /= tpivot;
06979         }
06980 /* L470: */
06981     }
06982     q[iout + in * q_dim1] = 1.f / pivot;
06983     z__ = q[iout + n2 * q_dim1];
06984     q[iout + n2 * q_dim1] = q[klm2 + in * q_dim1];
06985     q[klm2 + in * q_dim1] = z__;
06986     ii = (long int) abs(z__);
06987     if (iu[(ii << 1) + 1] == 0 || iu[(ii << 1) + 2] == 0) {
06988         goto L240;
06989     }
06990     i__1 = klm2;
06991     for (i__ = 1; i__ <= i__1; ++i__) {
06992         z__ = q[i__ + in * q_dim1];
06993         q[i__ + in * q_dim1] = q[i__ + js * q_dim1];
06994         q[i__ + js * q_dim1] = z__;
06995 /* L480: */
06996     }
06997     ++js;
06998     goto L240;
06999 /* TEST FOR OPTIMALITY. */
07000 L490:
07001     if (kforce == 0) {
07002         goto L580;
07003     }
07004     if (iphase == 1 && q[klm1 + n1 * q_dim1] <= toler) {
07005         goto L500;
07006     }
07007     kforce = 0;
07008     goto L240;
07009 /* SET UP PHASE 2 COSTS. */
07010 L500:
07011     iphase = 2;
07012     i__1 = nklm;
07013     for (j = 1; j <= i__1; ++j) {
07014         cu[(j << 1) + 1] = 0.f;
07015         cu[(j << 1) + 2] = 0.f;
07016 /* L510: */
07017     }
07018     i__1 = nk;
07019     for (j = n1; j <= i__1; ++j) {
07020         cu[(j << 1) + 1] = 1.f;
07021         cu[(j << 1) + 2] = 1.f;
07022 /* L520: */
07023     }
07024     i__1 = klm;
07025     for (i__ = 1; i__ <= i__1; ++i__) {
07026         ii = (long int) q[i__ + n2 * q_dim1];
07027         if (ii > 0) {
07028             goto L530;
07029         }
07030         ii = -ii;
07031         if (iu[(ii << 1) + 2] == 0) {
07032             goto L560;
07033         }
07034         cu[(ii << 1) + 2] = 0.f;
07035         goto L540;
07036 L530:
07037         if (iu[(ii << 1) + 1] == 0) {
07038             goto L560;
07039         }
07040         cu[(ii << 1) + 1] = 0.f;
07041 L540:
07042         ++ia;
07043         i__2 = n2;
07044         for (j = 1; j <= i__2; ++j) {
07045             z__ = q[ia + j * q_dim1];
07046             q[ia + j * q_dim1] = q[i__ + j * q_dim1];
07047             q[i__ + j * q_dim1] = z__;
07048 /* L550: */
07049         }
07050 L560:
07051         ;
07052     }
07053     goto L160;
07054 L570:
07055     if (q[klm1 + n1 * q_dim1] <= toler) {
07056         goto L500;
07057     }
07058     kode = 1;
07059     goto L590;
07060 L580:
07061     if (iphase == 1) {
07062         goto L570;
07063     }
07064 /* PREPARE OUTPUT. */
07065     kode = 0;
07066 L590:
07067     xsum = 0.;
07068     i__1 = *n;
07069     for (j = 1; j <= i__1; ++j) {
07070         x[j] = 0.f;
07071 /* L600: */
07072     }
07073     i__1 = klm;
07074     for (i__ = 1; i__ <= i__1; ++i__) {
07075         res[i__] = 0.f;
07076 /* L610: */
07077     }
07078     i__1 = klm;
07079     for (i__ = 1; i__ <= i__1; ++i__) {
07080         ii = (long int) q[i__ + n2 * q_dim1];
07081         sn = 1.f;
07082         if (ii > 0) {
07083             goto L620;
07084         }
07085         ii = -ii;
07086         sn = -1.f;
07087 L620:
07088         if (ii > *n) {
07089             goto L630;
07090         }
07091         x[ii] = sn * q[i__ + n1 * q_dim1];
07092         goto L640;
07093 L630:
07094         iimn = ii - *n;
07095         res[iimn] = sn * q[i__ + n1 * q_dim1];
07096         if (ii >= n1 && ii <= nk) {
07097             xsum += q[i__ + n1 * q_dim1];
07098         }
07099 L640:
07100         ;
07101     }
07102     error = (float)xsum;
07103     return;
07104 }

vector< float > Util::cluster_equalsize ( EMData d  )  [static]

Definition at line 20482 of file util_sparx.cpp.

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

20482                                                {
20483         //  WORKS ONLY FOR NUMBER OF OBJECTS N=l^2   !!
20484         int nx = d->get_xsize();
20485         int N = 1 + int((sqrt(1.0 + 8.0*nx)-1.0)/2.0);
20486         int K = N/2;
20487         vector<float> group(N);
20488         if(N*(N-1)/2 != nx) {
20489                 //print  "  incorrect dimension"
20490                 return group;}
20491         //bool active[N];       //this does not compile in VS2005. --Grant Tang
20492         bool * active = new bool[N];
20493         for(int i=0; i<N; i++) active[i] = true;
20494 
20495         float dm, qd;
20496         int   ppi = 0, ppj = 0;
20497         for(int k=0; k<K; k++) {
20498                 // find pairs of most similiar objects among active
20499                 //cout<<"  k  "<<k<<endl;
20500                 dm = 1.0e23f;
20501                 for(int i=1; i<N; i++) {
20502                         if(active[i]) {
20503                                 for(int j=0; j<i; j++) {
20504                                         if(active[j]) {
20505                                                 qd = (*d)(i*(i - 1)/2 + j);
20506                                                 if(qd < dm) {
20507                                                         dm = qd;
20508                                                         ppi = i;
20509                                                         ppj = j;
20510                                                 }
20511                                         }
20512                                 }
20513                         }
20514                 }
20515                 group[2*k] = float(ppi);
20516                 group[1+2*k] = float(ppj);
20517                 active[ppi] = false;
20518                 active[ppj] = false;
20519         }
20520 
20521         delete [] active;
20522         active = NULL;
20523         return  group;
20524 }

vector< float > Util::cluster_pairwise ( EMData d,
int  K,
float  T,
float  F 
) [static]

Definition at line 20274 of file util_sparx.cpp.

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

20274                                                                        {
20275         int nx = d->get_xsize();
20276         int N = 1 + int((sqrt(1.0 + 8.0*nx)-1.0)/2.0);
20277         vector<float> out(N+K+2);
20278         if(N*(N-1)/2 != nx) {
20279                 //print  "  incorrect dimension"
20280                 return out;}
20281         //  assign random objects as centers
20282         for(int i=0; i<N; i++) assign(i) = float(i);
20283         // shuffle
20284         for(int i=0; i<N; i++) {
20285                 int j = Util::get_irand(0,N-1);
20286                 float temp = assign(i);
20287                 assign(i) = assign(j);
20288                 assign(j) = temp;
20289         }
20290         for(int k=0; k<K; k++) cent(k) = float(assign(k));
20291         //for(int k=0; k<K; k++) cout<<cent(k)<<"    ";cout<<endl;
20292         //
20293         for(int i=0; i<N; i++) assign(i) = 0.0f;
20294         float qm, dispold = 1.1e22f, disp = 1.0e22f, na=0.0f;
20295         bool change = true;
20296         int it = -1;
20297         int ct = -1;
20298         while ((change && disp < dispold) || ct > 0) {
20299 
20300                 change = false;
20301                 dispold = disp;
20302                 it++;
20303 
20304                 // dispersion is a sum of distance from objects to object center
20305                 disp = 0.0f;
20306                 ct = 0;
20307                 for(int i=0; i<N; i++) {
20308                         qm = 1.0e23f;
20309                         for(int k=0; k<K; k++) {
20310                                 if(float(i) == cent(k)) {
20311                                         qm = 0.0f;
20312                                         na = (float)k;
20313                                 } else {
20314                                         float dt = (*d)(mono(i,int(cent(k))));
20315                                         if(dt < qm) {
20316                                                 qm = dt;
20317                                                 na = (float)k;
20318                                         }
20319                                 }
20320                         }
20321 
20322 
20323                         // Simulated annealing
20324                         if(exp(-1.0/float(T)) > Util::get_irand(1,1000)/1000.0) {
20325                             na = (float)(Util::get_irand(0, K));
20326                             qm = (*d)(mono(i,int(na)));
20327                             ct++;
20328                         }
20329 
20330                         disp += qm;
20331 
20332                         if(na != assign(i)) {
20333                                 assign(i) = na;
20334                                 change = true;
20335                         }
20336                 }
20337 
20338                 //cout<<"Iteration:  "<<it<< " disp " <<disp << " T " << T << " disturb " << ct << endl;
20339                 T = T*F;
20340 
20341         //for(int k=0; k<N; k++) cout<<assign(k)<<"    ";cout<<endl;
20342                 //print disp
20343                 //print  assign
20344                 // find centers
20345                 for(int k=0; k<K; k++) {
20346                         qm = 1.0e23f;
20347                         for(int i=0; i<N; i++) {
20348                                 if(assign(i) == float(k)) {
20349                                         float q = 0.0;
20350                                         for(int j=0; j<N; j++) {
20351                                                 if(assign(j) == float(k)) {
20352                                                                 //it cannot be the same object
20353                                                         if(i != j)  q += (*d)(mono(i,j));
20354                                                         //cout<<q<<"   "<<i<<"   "<<j<<"   "<<k<<endl;}
20355                                                 }
20356                                         }
20357                                         if(q < qm) {
20358                                                 //cout<<qm<<"   "<<q<<"   "<<i<<"   "<<k<<endl;
20359                                                 qm = q;
20360                                                 cent(k) = float(i);
20361                                         }
20362                                 }
20363                         }
20364                 }
20365         //for(int k=0; k<K; k++) cout<<cent(k)<<"    ";cout<<endl;cout<<disp<<endl;
20366         }
20367         out[N+K] = disp;
20368         out[N+K+1] = float(it);
20369         return  out;
20370 }

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 4953 of file util_sparx.cpp.

04953                                                                                                                        {
04954         double res = 0;
04955         double buf = 0;
04956         float* line_1;
04957         float* line_2;
04958         int i, n, ind;
04959         int lnlen = data[0]->get_xsize();
04960         for (n=0; n<n_lines; ++n) {
04961                 ind = n*2;
04962                 line_1 = data[seq[ind]]->get_data() + com[ind] * lnlen;
04963                 line_2 = data[seq[ind+1]]->get_data() + com[ind+1] *lnlen;
04964                 buf = 0;
04965                 for (i=0; i<lnlen; ++i) {
04966                     buf += (line_1[i]-line_2[i])*(line_1[i]-line_2[i]);
04967                 }
04968                 res += buf * weights[n];
04969         }
04970 
04971         return res;
04972 
04973 }

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 4730 of file util_sparx.cpp.

References deg_rad.

04730                                                   {
04731         int nb_ori = Ori.size() / 4;
04732         int i, ind;
04733         float ph, th, ps;
04734         double cph, cth, cps, sph, sth, sps;
04735         vector<double> Rot(nb_ori*9);
04736         for (i=0; i<nb_ori; ++i){
04737                 ind = i*4;
04738                 // spider convention phi=psi-90, psi=phi+90
04739                 ph = Ori[ind+2]-90;
04740                 th = Ori[ind+1];
04741                 ps = Ori[ind]+90;
04742                 ph *= deg_rad;
04743                 th *= deg_rad;
04744                 ps *= deg_rad;
04745                 // pre-calculate some trigo stuffs
04746                 cph = cos(ph);
04747                 cth = cos(th);
04748                 cps = cos(ps);
04749                 sph = sin(ph);
04750                 sth = sin(th);
04751                 sps = sin(ps);
04752                 // fill rotation matrix
04753                 ind = i*9;
04754                 Rot[ind] = cph*cps-cth*sps*sph;
04755                 Rot[ind+1] = cph*sps+cth*cps*sph;
04756                 Rot[ind+2] = sth*sph;
04757                 Rot[ind+3] = -sph*cps-cth*sps*cph;
04758                 Rot[ind+4] = -sph*sps+cth*cps*cph;
04759                 Rot[ind+5] = sth*cph;
04760                 Rot[ind+6] = sth*sps;
04761                 Rot[ind+7] = -sth*cps;
04762                 Rot[ind+8] = cth;
04763         }
04764 
04765         return Rot;
04766 }

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 4898 of file util_sparx.cpp.

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

04898                                                                                      {
04899         // seq is the pairwise index ij: 0, 1, 0, 2, 0, 3, 1, 2, 1, 3, 2, 3
04900         vector<double> cml(2*nlines); // [phi, theta] / line
04901         float ph1, th1;
04902         float ph2, th2;
04903         double nx, ny, nz;
04904         double norm;
04905         double sth1=0, sph1=0, cth1=0, cph1=0;
04906         double sth2, sph2, cth2, cph2;
04907         int l, ind, c;
04908         int mem = -1;
04909         for (l=0; l<nlines; ++l){
04910                 c = 2*l;
04911                 if (seq[c]!=mem){
04912                         mem = seq[c];
04913                         ind = 4*seq[c];
04914                         ph1 = Ori[ind]*deg_rad;
04915                         th1 = Ori[ind+1]*deg_rad;
04916                         sth1 = sin(th1);
04917                         sph1 = sin(ph1);
04918                         cth1 = cos(th1);
04919                         cph1 = cos(ph1);
04920                 }
04921                 ind = 4*seq[c+1];
04922                 ph2 = Ori[ind]*deg_rad;
04923                 th2 = Ori[ind+1]*deg_rad;
04924                 sth2 = sin(th2);
04925                 cth2 = cos(th2);
04926                 sph2 = sin(ph2);
04927                 cph2 = cos(ph2);
04928                 // cross product
04929                 nx = sth1*cph1*cth2 - cth1*sth2*cph2;
04930                 ny = cth1*sth2*sph2 - cth2*sth1*sph1;
04931                 nz = sth1*sph1*sth2*cph2 - sth1*cph1*sth2*sph2;
04932                 norm = sqrt(nx*nx+ny*ny+nz*nz);
04933                 nx /= norm;
04934                 ny /= norm;
04935                 nz /= norm;
04936                 // apply mirror if need
04937                 if (nz<0) {nx=-nx; ny=-ny; nz=-nz;}
04938                 // compute theta and phi
04939                 cml[c+1] = acos(nz);
04940                 if (cml[c+1] == 0) {cml[c] = 0;}
04941                 else {
04942                         cml[c+1] *= rad_deg;
04943                         if (cml[c+1] > 89.99) {cml[c+1] = 89.99;} // this fix some pb in Voronoi
04944                         cml[c] = rad_deg * atan2(nx, ny);
04945                         cml[c] = fmod(360 + cml[c], 360);
04946 
04947                 }
04948         }
04949 
04950         return cml;
04951 }

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 4799 of file util_sparx.cpp.

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

04799                                                                         {
04800         vector<int> com(2*(n_prj - 1));
04801         int a = i_prj*9;
04802         int i, b, c;
04803         int n1=0, n2=0;
04804         float vmax = 1 - 1.0e-6f;
04805         double r11, r12, r13, r23, r31, r32, r33;
04806 
04807         c = 0;
04808         for (i=0; i<n_prj; ++i){
04809                 if (i!=i_prj){
04810                         b = i*9;
04811                         // this is equivalent to R = A*B'
04812                         r11 = Rot[a]*Rot[b]+Rot[a+1]*Rot[b+1]+Rot[a+2]*Rot[b+2];
04813                         r12 = Rot[a]*Rot[b+3]+Rot[a+1]*Rot[b+4]+Rot[a+2]*Rot[b+5];
04814                         r13 = Rot[a]*Rot[b+6]+Rot[a+1]*Rot[b+7]+Rot[a+2]*Rot[b+8];
04815                         r23 = Rot[a+3]*Rot[b+6]+Rot[a+4]*Rot[b+7]+Rot[a+5]*Rot[b+8];
04816                         r31 = Rot[a+6]*Rot[b]+Rot[a+7]*Rot[b+1]+Rot[a+8]*Rot[b+2];
04817                         r32 = Rot[a+6]*Rot[b+3]+Rot[a+7]*Rot[b+4]+Rot[a+8]*Rot[b+5];
04818                         r33 = Rot[a+6]*Rot[b+6]+Rot[a+7]*Rot[b+7]+Rot[a+8]*Rot[b+8];
04819                         if (r33 > vmax) {
04820                             n2 = 270;
04821                             n1 = 270 + nint180((float)(rad_deg*atan2(r12, r11)));
04822                         }
04823                         else if (r33 < -vmax) {
04824                             n2 = 270;
04825                             n1 = 270 - nint180((float)(rad_deg*atan2(r12, r11)));
04826                         } else {
04827                             n2 = nint180((float)(rad_deg*atan2(r31, -r32)));
04828                             n1 = nint180((float)(rad_deg*atan2(r13, r23)));
04829                             if (n1 < 0) {n1 += 360;}
04830                             if (n2 <= 0) {n2 = abs(n2);}
04831                             else {n2 = 360 - n2;}
04832                         }
04833 
04834                         if (n1 >= 360){n1 = n1 % 360;}
04835                         if (n2 >= 360){n2 = n2 % 360;}
04836 
04837                         // store common-lines
04838                         b = c*2;
04839                         com[b] = n1;
04840                         com[b+1] = n2;
04841                         ++c;
04842                 }
04843         }
04844 
04845     return com;
04846 
04847 }

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 4849 of file util_sparx.cpp.

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

04849                                                                                           {
04850         vector<int> com(2*n_lines);
04851         int a=0, b, c, l;
04852         int n1=0, n2=0, mem=-1;
04853         float vmax = 1 - 1.0e-6f;
04854         double r11, r12, r13, r23, r31, r32, r33;
04855         c = 0;
04856         for (l=0; l<n_lines; ++l){
04857                 c = 2*l;
04858                 if (seq[c]!=mem){
04859                     mem = seq[c];
04860                     a = seq[c]*9;
04861                 }
04862                 b = seq[c+1]*9;
04863 
04864                 // this is equivalent to R = A*B'
04865                 r11 = Rot[a]*Rot[b]+Rot[a+1]*Rot[b+1]+Rot[a+2]*Rot[b+2];
04866                 r12 = Rot[a]*Rot[b+3]+Rot[a+1]*Rot[b+4]+Rot[a+2]*Rot[b+5];
04867                 r13 = Rot[a]*Rot[b+6]+Rot[a+1]*Rot[b+7]+Rot[a+2]*Rot[b+8];
04868                 r23 = Rot[a+3]*Rot[b+6]+Rot[a+4]*Rot[b+7]+Rot[a+5]*Rot[b+8];
04869                 r31 = Rot[a+6]*Rot[b]+Rot[a+7]*Rot[b+1]+Rot[a+8]*Rot[b+2];
04870                 r32 = Rot[a+6]*Rot[b+3]+Rot[a+7]*Rot[b+4]+Rot[a+8]*Rot[b+5];
04871                 r33 = Rot[a+6]*Rot[b+6]+Rot[a+7]*Rot[b+7]+Rot[a+8]*Rot[b+8];
04872                 if (r33 > vmax) {
04873                     n2 = 270;
04874                     n1 = 270 + nint180((float)(rad_deg*atan2(r12, r11)));
04875                 }
04876                 else if (r33 < -vmax) {
04877                     n2 = 270;
04878                     n1 = 270 - nint180((float)(rad_deg*atan2(r12, r11)));
04879                 } else {
04880                     n2 = nint180((float)(rad_deg*atan2(r31, -r32)));
04881                     n1 = nint180((float)(rad_deg*atan2(r13, r23)));
04882                     if (n1 < 0) {n1 += 360;}
04883                     if (n2 <= 0) {n2 = abs(n2);}
04884                     else {n2 = 360 - n2;}
04885                 }
04886                 if (n1 >= 360){n1 = n1 % 360;}
04887                 if (n2 >= 360){n2 = n2 % 360;}
04888 
04889                 // store common-lines
04890                 com[c] = n1;
04891                 com[c+1] = n2;
04892         }
04893 
04894         return com;
04895 
04896 }

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 4712 of file util_sparx.cpp.

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

04712                                                                                                   {
04713         int j;
04714         int nx = sino->get_xsize();
04715         int i = nx * pos_line;
04716         float r1, r2;
04717         float *line_ptr = line->get_data();
04718         float *sino_ptr = sino->get_data();
04719         for (j=ilf;j<=ihf; j += 2) {
04720                 r1 = line_ptr[j];
04721                 r2 = line_ptr[j + 1];
04722                 sino_ptr[i + j - ilf] = r1;
04723                 sino_ptr[i + j - ilf + 1] = r2;
04724                 sino_ptr[i + nx * nblines + j - ilf] = r1;
04725                 sino_ptr[i + nx * nblines + j - ilf + 1] = -r2;
04726         }
04727         sino->update();
04728 }

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 4975 of file util_sparx.cpp.

04976                                                                                            {
04977         // res: [best_disc, best_ipsi]
04978         // seq: pairwise indexes ij, 0, 1, 0, 2, 0, 3, 1, 2, 1, 3, 2, 3
04979         // iw : index to know where is the weight for the common-lines on the current projection in the all weights, [12, 4, 2, 7]
04980         vector<double> res(2);
04981         int lnlen = data[0]->get_xsize();
04982         int end = 2*(n_prj-1);
04983         double disc, buf, bdisc, tmp;
04984         int n, i, ipsi, ind, bipsi, c;
04985         float* line_1;
04986         float* line_2;
04987         bdisc = 1.0e6;
04988         bipsi = -1;
04989         // loop psi
04990         for(ipsi=0; ipsi<n_psi; ipsi += d_psi) {
04991                 // discrepancy
04992                 disc = 0;
04993                 c = 0;
04994                 for (n=0; n<n_prj; ++n) {
04995                         if(n!=iprj) {
04996                                 ind = 2*c;
04997                                 line_1 = data[iprj]->get_data() + com[ind] * lnlen;
04998                                 line_2 = data[n]->get_data() + com[ind+1] * lnlen;
04999                                 buf = 0;
05000                                 for (i=0; i<lnlen; ++i) {
05001                                         tmp = line_1[i]-line_2[i];
05002                                         buf += tmp*tmp;
05003                                 }
05004                                 disc += buf * weights[iw[c]];
05005                                 ++c;
05006                         }
05007                 }
05008                 // select the best value
05009                 if (disc <= bdisc) {
05010                         bdisc = disc;
05011                         bipsi = ipsi;
05012                 }
05013                 // update common-lines
05014                 for (i=0; i<end; i+=2){
05015                         com[i] += d_psi;
05016                         if (com[i] >= n_psi) com[i] = com[i] - n_psi;
05017                 }
05018         }
05019         res[0] = bdisc;
05020         res[1] = float(bipsi);
05021 
05022         return res;
05023 }

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 5025 of file util_sparx.cpp.

05026                                                                                            {
05027         // res: [best_disc, best_ipsi]
05028         // seq: pairwise indexes ij, 0, 1, 0, 2, 0, 3, 1, 2, 1, 3, 2, 3
05029         // iw : index to know where is the weight for the common-lines on the current projection in the all weights, [12, 4, 2, 7]
05030         vector<double> res(2);
05031         int lnlen = data[0]->get_xsize();
05032         int end = 2*(n_prj-1);
05033         double disc, buf, bdisc, tmp;
05034         int n, i, ipsi, ind, bipsi, c;
05035         float* line_1;
05036         float* line_2;
05037         bdisc = 1.0e6;
05038         bipsi = -1;
05039         // loop psi
05040         for(ipsi=0; ipsi<n_psi; ipsi += d_psi) {
05041                 // discrepancy
05042                 disc = 0;
05043                 c = 0;
05044                 for (n=0; n<n_prj; ++n) {
05045                         if(n!=iprj) {
05046                                 ind = 2*c;
05047                                 line_1 = data[iprj]->get_data() + com[ind] * lnlen;
05048                                 line_2 = data[n]->get_data() + com[ind+1] * lnlen;
05049                                 buf = 0;
05050                                 for (i=0; i<lnlen; ++i) {
05051                                         tmp = line_1[i]-line_2[i];
05052                                         buf += tmp*tmp;
05053                                 }
05054                                 disc += buf;
05055                                 ++c;
05056                         }
05057                 }
05058                 // select the best value
05059                 if (disc <= bdisc) {
05060                         bdisc = disc;
05061                         bipsi = ipsi;
05062                 }
05063                 // update common-lines
05064                 for (i=0; i<end; i+=2){
05065                         com[i] += d_psi;
05066                         if (com[i] >= n_psi) com[i] = com[i] - n_psi;
05067                 }
05068         }
05069         res[0] = bdisc;
05070         res[1] = float(bipsi);
05071 
05072         return res;
05073 }

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 4768 of file util_sparx.cpp.

References deg_rad.

04768                                                                                              {
04769         float ph, ps;
04770         double cph, cth, cps, sph, sth, sps;
04771         int ind = iprj*9;
04772         // spider convention phi=psi-90, psi=phi+90
04773         ph = nps-90;
04774         ps = nph+90;
04775         ph *= deg_rad;
04776         th *= deg_rad;
04777         ps *= deg_rad;
04778         // pre-calculate some trigo stuffs
04779         cph = cos(ph);
04780         cth = cos(th);
04781         cps = cos(ps);
04782         sph = sin(ph);
04783         sth = sin(th);
04784         sps = sin(ps);
04785         // fill rotation matrix
04786         Rot[ind] = (float)(cph*cps-cth*sps*sph);
04787         Rot[ind+1] = (float)(cph*sps+cth*cps*sph);
04788         Rot[ind+2] = (float)(sth*sph);
04789         Rot[ind+3] = (float)(-sph*cps-cth*sps*cph);
04790         Rot[ind+4] = (float)(-sph*sps+cth*cps*cph);
04791         Rot[ind+5] = (float)(sth*cph);
04792         Rot[ind+6] = (float)(sth*sps);
04793         Rot[ind+7] = (float)(-sth*cps);
04794         Rot[ind+8] = (float)(cth);
04795 
04796         return Rot;
04797 }

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

Definition at line 4622 of file util_sparx.cpp.

References Assert, PI2, and vrdg().

04622                                                         {
04623         static const int NBIN = 100;
04624         int nline=cml.size()/2;
04625         vector<double> weights(nline);
04626 
04627         vector<ori_t> angs(nline);
04628         for( int i=0; i < nline; ++i ) {
04629                 angs[i].iphi = int( NBIN*cml[2*i] );
04630                 angs[i].itht = int( NBIN*cml[2*i+1] );
04631                 if( angs[i].itht == 180*NBIN ) angs[i].itht = 0;
04632                 angs[i].id = i;
04633         }
04634 
04635         //std::cout << "# of angs: " << angs.size() << std::endl;
04636 
04637         std::sort( angs.begin(), angs.end(), cmpang() );
04638 
04639         vector<float> newphi;
04640         vector<float> newtht;
04641         vector< vector<int> > indices;
04642 
04643         int curt_iphi = -1;
04644         int curt_itht = -1;
04645         for(unsigned int i=0 ;i < angs.size(); ++i ) {
04646                 if( angs[i].iphi==curt_iphi && angs[i].itht==curt_itht ) {
04647                         Assert( indices.size() > 0 );
04648                         indices.back().push_back(angs[i].id);
04649                 } else {
04650                         curt_iphi = angs[i].iphi;
04651                         curt_itht = angs[i].itht;
04652 
04653                         newphi.push_back( float(curt_iphi)/NBIN );
04654                         newtht.push_back( float(curt_itht)/NBIN );
04655                         indices.push_back( vector<int>(1,angs[i].id) );
04656                 }
04657         }
04658 
04659         //std::cout << "# of indpendent ang: " << newphi.size() << std::endl;
04660 
04661 
04662         int num_agl = newphi.size();
04663 
04664         if(num_agl>2) {
04665                 vector<double> w=Util::vrdg(newphi, newtht);
04666 
04667                 Assert( w.size()==newphi.size() );
04668                 Assert( indices.size()==newphi.size() );
04669 
04670                 for(unsigned int i=0; i < newphi.size(); ++i ) {
04671                     /*
04672                     std::cout << "phi,tht,w,n: ";
04673                     std::cout << boost::format( "%10.3f" ) % newphi[i] << " ";
04674                     std::cout << boost::format( "%10.3f" ) % newtht[i] << " ";
04675                     std::cout << boost::format( "%8.6f"  ) % w[i] << " ";
04676                     std::cout << indices[i].size() << "(";
04677                     */
04678 
04679                     for(unsigned int j=0; j < indices[i].size(); ++j ) {
04680                             int id = indices[i][j];
04681                             weights[id] = w[i]/indices[i].size();
04682                             //std::cout << id << " ";
04683                     }
04684 
04685                     //std::cout << ")" << std::endl;
04686 
04687                 }
04688         } else {
04689                 cout<<"warning in Util.cml_weights"<<endl;
04690                 double val = PI2/float(nline);
04691                 for(int i=0; i<nline; i++)  weights[i]=val;
04692         }
04693 
04694         return weights;
04695 
04696 }

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

Definition at line 7242 of file util_sparx.cpp.

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

Referenced by hsortd().

07243 {
07244         return(tmp1.theta1 < tmp2.theta1);
07245 }

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

Definition at line 7247 of file util_sparx.cpp.

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

Referenced by hsortd().

07248 {
07249         return(tmp1.key1 < tmp2.key1);
07250 }

void Util::colreverse ( float *  beg,
float *  end,
int  nx 
) [static]

Definition at line 5433 of file util_sparx.cpp.

Referenced by cyclicshift(), and slicereverse().

05433                                                     {
05434         float* tmp = new float[nx];
05435         int n = (end - beg)/nx;
05436         int nhalf = n/2;
05437         for (int i = 0; i < nhalf; i++) {
05438                 // swap col i and col n-1-i
05439                 memcpy(tmp, beg+i*nx, nx*sizeof(float));
05440                 memcpy(beg+i*nx, beg+(n-1-i)*nx, nx*sizeof(float));
05441                 memcpy(beg+(n-1-i)*nx, tmp, nx*sizeof(float));
05442         }
05443         delete[] tmp;
05444 }

EMData * Util::compress_image_mask ( EMData image,
EMData mask 
) [static]

Definition at line 6125 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().

06126 {
06127         /***********
06128         ***get the size of the image for validation purpose
06129         **************/
06130         int nx = image->get_xsize(),ny = image->get_ysize(),nz = image->get_zsize();  //Aren't  these  implied?  Please check and let me know, PAP.
06131         /********
06132         ***Exception Handle
06133         *************/
06134         if(nx != mask->get_xsize() || ny != mask->get_ysize() || nz != mask->get_zsize())
06135                 throw ImageDimensionException("The dimension of the image does not match the dimension of the mask!");
06136 
06137         size_t i, size = (size_t)nx*ny*nz;
06138 
06139         float* img_ptr = image->get_data();
06140         float* mask_ptr = mask->get_data();
06141 
06142         int ln=0;  //length of the output image = number of points under the mask.
06143         for(i = 0;i < size;i++) if(mask_ptr[i] > 0.5f) ln++;
06144 
06145         EMData* new_image = new EMData();
06146         new_image->set_size(ln,1,1); /* set size of the new image */
06147         float *new_ptr    = new_image->get_data();
06148 
06149         ln=-1;
06150         for(i = 0;i < size;i++){
06151                 if(mask_ptr[i] > 0.5f) {
06152                         ln++;
06153                         new_ptr[ln]=img_ptr[i];
06154                 }
06155         }
06156 
06157         return new_image;
06158 }

int Util::coveig ( int  n,
float *  covmat,
float *  eigval,
float *  eigvec 
) [static]

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 6254 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, and ssyev_().

06255 {
06256         // n size of the covariance/correlation matrix
06257         // covmat --- covariance/correlation matrix (n by n)
06258         // eigval --- returns eigenvalues
06259         // eigvec --- returns eigenvectors
06260 
06261         ENTERFUNC;
06262 
06263         int i;
06264 
06265         // make a copy of covmat so that it will not be overwritten
06266         for ( i = 0 ; i < n * n ; i++ )   eigvec[i] = covmat[i];
06267 
06268         char NEEDV = 'V';
06269         char UPLO = 'U';
06270         int lwork = -1;
06271         int info = 0;
06272         float *work, wsize;
06273 
06274         //  query to get optimal workspace
06275         ssyev_(&NEEDV, &UPLO, &n, eigvec, &n, eigval, &wsize, &lwork, &info);
06276         lwork = (int)wsize;
06277 
06278         work = (float *)calloc(lwork, sizeof(float));
06279         //  calculate eigs
06280         ssyev_(&NEEDV, &UPLO, &n, eigvec, &n, eigval, work, &lwork, &info);
06281         free(work);
06282         EXITFUNC;
06283         return info;
06284 }

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 6286 of file util_sparx.cpp.

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

06287 {
06288 
06289         ENTERFUNC;
06290         int len = covmatpy.size();
06291         float *eigvec;
06292         float *eigval;
06293         float *covmat;
06294         int status = 0;
06295         eigval = (float*)calloc(ncov,sizeof(float));
06296         eigvec = (float*)calloc(ncov*ncov,sizeof(float));
06297         covmat = (float*)calloc(ncov*ncov, sizeof(float));
06298 
06299         const float *covmat_ptr;
06300         covmat_ptr = &covmatpy[0];
06301         for(int i=0;i<len;i++){
06302             covmat[i] = covmat_ptr[i];
06303         }
06304 
06305         status = Util::coveig(ncov, covmat, eigval, eigvec);
06306 
06307         vector<float> eigval_py(ncov);
06308         const float *eigval_ptr;
06309         eigval_ptr = &eigval[0];
06310         for(int i=0;i<ncov;i++){
06311             eigval_py[i] = eigval_ptr[i];
06312         }
06313 
06314         vector<float> eigvec_py(ncov*ncov);
06315         const float *eigvec_ptr;
06316         eigvec_ptr = &eigvec[0];
06317         for(int i=0;i<ncov*ncov;i++){
06318             eigvec_py[i] = eigvec_ptr[i];
06319         }
06320 
06321         Dict res;
06322         res["eigval"] = eigval_py;
06323         res["eigvec"] = eigvec_py;
06324 
06325         EXITFUNC;
06326         return res;
06327 }

Dict Util::Crosrng_e ( EMData circ1,
EMData circ2,
vector< int >  numr,
int  neg 
) [static]

Definition at line 3169 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.

03169                                                                                {
03170         //  neg = 0 straight,  neg = 1 mirrored
03171         int nring = numr.size()/3;
03172         //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
03173         int maxrin = numr[numr.size()-1];
03174         double qn;   float  tot;
03175         float *circ1 = circ1p->get_data();
03176         float *circ2 = circ2p->get_data();
03177 /*
03178 c checks single position, neg is flag for checking mirrored position
03179 c
03180 c  input - fourier transforms of rings!
03181 c  first set is conjugated (mirrored) if neg
03182 c  circ1 already multiplied by weights!
03183 c       automatic arrays
03184         dimension         t(maxrin)  removed +2 as it is only needed for other ffts
03185         double precision  q(maxrin)
03186         double precision  t7(-3:3)
03187 */
03188         float *t;
03189         double t7[7], *q;
03190         int    i, j, k, ip, jc, numr3i, numr2i, jtot = 0;
03191         float  pos;
03192 
03193 #ifdef _WIN32
03194         ip = -(int)(log((float)maxrin)/log(2.0f));
03195 #else
03196         ip = -(int) (log2(maxrin));
03197 #endif  //_WIN32
03198 
03199         q = (double*)calloc(maxrin, sizeof(double));
03200         t = (float*)calloc(maxrin, sizeof(float));
03201 
03202 //   cout << *qn <<"  " <<*tot<<"  "<<ip<<endl;
03203         for (i=1; i<=nring; i++) {
03204                 numr3i = numr(3,i);
03205                 numr2i = numr(2,i);
03206 
03207                 t(1) = (circ1(numr2i)) * circ2(numr2i);
03208 
03209                 if (numr3i != maxrin) {
03210                          // test .ne. first for speed on some compilers
03211                         t(numr3i+1) = circ1(numr2i+1) * circ2(numr2i+1);
03212                         t(2)            = 0.0;
03213 
03214                         if (neg) {
03215                                 // first set is conjugated (mirrored)
03216                                 for (j=3;j<=numr3i;j=j+2) {
03217                                         jc = j+numr2i-1;
03218                                         t(j) =(circ1(jc))*circ2(jc)-(circ1(jc+1))*circ2(jc+1);
03219                                         t(j+1) = -(circ1(jc))*circ2(jc+1)-(circ1(jc+1))*circ2(jc);
03220                                 }
03221                         } else {
03222                                 for (j=3;j<=numr3i;j=j+2) {
03223                                         jc = j+numr2i-1;
03224                                         t(j) = (circ1(jc))*circ2(jc) + (circ1(jc+1))*circ2(jc+1);
03225                                         t(j+1) = -(circ1(jc))*circ2(jc+1) + (circ1(jc+1))*circ2(jc);
03226                                 }
03227                         }
03228                         for (j=1;j<=numr3i+1;j++) q(j) = q(j) + t(j);
03229                 } else {
03230                         t(2) = circ1(numr2i+1) * circ2(numr2i+1);
03231                         if (neg) {
03232                                 // first set is conjugated (mirrored)
03233                                 for (j=3;j<=maxrin;j=j+2) {
03234                                         jc = j+numr2i-1;
03235                                         t(j) = (circ1(jc))*circ2(jc) - (circ1(jc+1))*circ2(jc+1);
03236                                         t(j+1) = -(circ1(jc))*circ2(jc+1) - (circ1(jc+1))*circ2(jc);
03237                                 }
03238                         } else {
03239                                 for (j=3;j<=maxrin;j=j+2) {
03240                                         jc = j+numr2i-1;
03241                                         t(j) = (circ1(jc))*circ2(jc) + (circ1(jc+1))*circ2(jc+1);
03242                                         t(j+1) = -(circ1(jc))*circ2(jc+1) + (circ1(jc+1))*circ2(jc);
03243                                 }
03244                         }
03245                         for (j = 1; j <= maxrin; j++) q(j) += t(j);
03246                 }
03247         }
03248 
03249         fftr_d(q,ip);
03250 
03251         qn = -1.0e20;
03252         for (j=1;j<=maxrin;j++) {
03253            if (q(j) >= qn) {
03254                   qn = q(j); jtot = j;
03255            }
03256         }
03257 
03258         for (k=-3; k<=3; k++) {
03259                 j = (jtot+k+maxrin-1)%maxrin + 1;
03260                 t7(k+4) = q(j);
03261         }
03262 
03263         prb1d(t7,7,&pos);
03264 
03265         tot = (float)jtot + pos;
03266 
03267         if (q) free(q);
03268         if (t) free(t);
03269 
03270         Dict retvals;
03271         retvals["qn"] = qn;
03272         retvals["tot"] = tot;
03273         return  retvals;
03274 }

Dict Util::Crosrng_ew ( EMData circ1,
EMData circ2,
vector< int >  numr,
vector< float >  w,
int  neg 
) [static]

Definition at line 3276 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.

03276                                                                                                  {
03277    //  neg = 0 straight,  neg = 1 mirrored
03278         int nring = numr.size()/3;
03279         //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
03280         int maxrin = numr[numr.size()-1];
03281         double qn;   float  tot;
03282         float *circ1 = circ1p->get_data();
03283         float *circ2 = circ2p->get_data();
03284 /*
03285 c checks single position, neg is flag for checking mirrored position
03286 c
03287 c  input - fourier transforms of rings!
03288 c  first set is conjugated (mirrored) if neg
03289 c  multiplication by weights!
03290 c       automatic arrays
03291         dimension         t(maxrin)  removed +2 as it is only needed for other ffts
03292         double precision  q(maxrin)
03293         double precision  t7(-3:3)
03294 */
03295         float *t;
03296         double t7[7], *q;
03297         int    i, j, k, ip, jc, numr3i, numr2i, jtot = 0;
03298         float  pos;
03299 
03300 #ifdef _WIN32
03301         ip = -(int)(log((float)maxrin)/log(2.0f));
03302 #else
03303         ip = -(int) (log2(maxrin));
03304 #endif  //_WIN32
03305 
03306         q = (double*)calloc(maxrin, sizeof(double));
03307         t = (float*)calloc(maxrin, sizeof(float));
03308 
03309 //   cout << *qn <<"  " <<*tot<<"  "<<ip<<endl;
03310         for (i=1;i<=nring;i++) {
03311                 numr3i = numr(3,i);
03312                 numr2i = numr(2,i);
03313 
03314                 t(1) = circ1(numr2i) * circ2(numr2i);
03315 
03316                 if (numr3i != maxrin) {
03317                         // test .ne. first for speed on some compilers
03318                         t(numr3i+1) = circ1(numr2i+1) * circ2(numr2i+1);
03319                         t(2)      = 0.0;
03320 
03321                         if (neg) {
03322                                 // first set is conjugated (mirrored)
03323                                 for (j=3; j<=numr3i; j=j+2) {
03324                                         jc = j+numr2i-1;
03325                                         t(j)   =  (circ1(jc))*circ2(jc)-(circ1(jc+1))*circ2(jc+1);
03326                                         t(j+1) = -(circ1(jc))*circ2(jc+1)-(circ1(jc+1))*circ2(jc);
03327                                 }
03328                         } else {
03329                                 for (j=3; j<=numr3i; j=j+2) {
03330                                         jc = j+numr2i-1;
03331                                         t(j) = (circ1(jc))*circ2(jc) + (circ1(jc+1))*circ2(jc+1);
03332                                         t(j+1) = -(circ1(jc))*circ2(jc+1) + (circ1(jc+1))*circ2(jc);
03333                                 }
03334                         }
03335                         for (j=1;j<=numr3i+1;j++) q(j) += t(j)*w[i-1];
03336                 } else {
03337                         t(2) = circ1(numr2i+1) * circ2(numr2i+1);
03338                         if (neg) {
03339                                 // first set is conjugated (mirrored)
03340                                 for (j=3; j<=maxrin; j=j+2) {
03341                                         jc = j+numr2i-1;
03342                                         t(j) = (circ1(jc))*circ2(jc) - (circ1(jc+1))*circ2(jc+1);
03343                                         t(j+1) = -(circ1(jc))*circ2(jc+1) - (circ1(jc+1))*circ2(jc);
03344                                 }
03345                         } else {
03346                                 for (j=3; j<=maxrin; j=j+2) {
03347                                 jc = j+numr2i-1;
03348                                 t(j) = (circ1(jc))*circ2(jc) + (circ1(jc+1))*circ2(jc+1);
03349                                 t(j+1) = -(circ1(jc))*circ2(jc+1) + (circ1(jc+1))*circ2(jc);
03350                                 }
03351                         }
03352                         for (j = 1; j <= maxrin; j++) q(j) += t(j)*w[i-1];
03353                 }
03354         }
03355 
03356         fftr_d(q,ip);
03357 
03358         qn = -1.0e20;
03359         for (j=1;j<=maxrin;j++) {
03360                 //cout << j << "  " << q(j) << endl;
03361                 if (q(j) >= qn) {
03362                         qn = q(j);
03363                         jtot = j;
03364                 }
03365         }
03366 
03367         for (k=-3; k<=3; k++) {
03368                 j = (jtot+k+maxrin-1)%maxrin + 1;
03369                 t7(k+4) = q(j);
03370         }
03371 
03372         prb1d(t7,7,&pos);
03373 
03374         tot = (float)jtot + pos;
03375 
03376         //if (q) free(q);
03377         if (t) free(t);
03378 
03379         Dict retvals;
03380         //tot = 1;
03381         //qn = q(1);
03382         retvals["qn"] = qn;
03383         retvals["tot"] = tot;
03384 
03385         if (q) free(q);
03386 
03387         return  retvals;
03388 }

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

Definition at line 3390 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().

03390                                                                       {
03391         int nring = numr.size()/3;
03392         //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
03393         int maxrin = numr[numr.size()-1];
03394         double qn; float tot; double qm; float tmt;
03395         float *circ1 = circ1p->get_data();
03396         float *circ2 = circ2p->get_data();
03397 /*
03398 c
03399 c  checks both straight & mirrored positions
03400 c
03401 c  input - fourier transforms of rings!!
03402 c  circ1 already multiplied by weights!
03403 c
03404 */
03405 
03406         // dimension             circ1(lcirc),circ2(lcirc)
03407 
03408         // t(maxrin), q(maxrin), t7(-3:3)  //maxrin+2 removed
03409         double *t, *q, t7[7];
03410 
03411         int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
03412         float t1, t2, t3, t4, c1, c2, d1, d2, pos;
03413 
03414         qn  = 0.0f;
03415         qm  = 0.0f;
03416         tot = 0.0f;
03417         tmt = 0.0f;
03418 #ifdef _WIN32
03419         ip = -(int)(log((float)maxrin)/log(2.0f));
03420 #else
03421         ip = -(int)(log2(maxrin));
03422 #endif  //_WIN32
03423   //for (j=1; j<=maxrin;j++) cout <<"  "<<j<<"   "<<circ1(j)<<"   "<<circ2(j) <<endl;
03424 
03425         //  c - straight  = circ1 * conjg(circ2)
03426         //  zero q array
03427 
03428         q = (double*)calloc(maxrin,sizeof(double));
03429 
03430         //   t - mirrored  = conjg(circ1) * conjg(circ2)
03431         //   zero t array
03432         t = (double*)calloc(maxrin,sizeof(double));
03433 
03434    //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
03435         for (i=1; i<=nring; i++) {
03436 
03437                 numr3i = numr(3,i);   // Number of samples of this ring
03438                 numr2i = numr(2,i);   // The beginning point of this ring
03439 
03440                 t1   = circ1(numr2i) * circ2(numr2i);
03441                 q(1) += t1;
03442                 t(1) += t1;
03443 
03444                 t1   = circ1(numr2i+1) * circ2(numr2i+1);
03445                 if (numr3i == maxrin)  {
03446                         q(2) += t1;
03447                         t(2) += t1;
03448                 } else {
03449                         q(numr3i+1) += t1;
03450                         t(numr3i+1) += t1;
03451                 }
03452 
03453                 for (j=3; j<=numr3i; j += 2) {
03454                         jc     = j+numr2i-1;
03455 
03456 // Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
03457 //                                ----- -----    ----- -----
03458 //                                 t1     t2      t3    t4
03459 // Here, conj(c1+c2i)*conj(d1+d2i) = (c1*d1-c2*d2)+(-c1*d2-c2*d1)i
03460 //                                    ----- -----    ----- -----
03461 //                                     t1    t2       t3    t4
03462 
03463                         c1     = circ1(jc);
03464                         c2     = circ1(jc+1);
03465                         d1     = circ2(jc);
03466                         d2     = circ2(jc+1);
03467 
03468                         t1     = c1 * d1;
03469                         t2     = c2 * d2;
03470                         t3     = c1 * d2;
03471                         t4     = c2 * d1;
03472 
03473                         q(j)   += t1 + t2;
03474                         q(j+1) += -t3 + t4;
03475                         t(j)   += t1 - t2;
03476                         t(j+1) += -t3 - t4;
03477                 }
03478         }
03479         //for (j=1; j<=maxrin; j++) cout <<"  "<<j<<"   "<<q(j) <<"   "<<t(j) <<endl;
03480         fftr_d(q,ip);
03481 
03482         qn  = -1.0e20;
03483         for (j=1; j<=maxrin; j++) {//cout <<"  "<<j<<"   "<<q(j) <<endl;
03484                 if (q(j) >= qn) {
03485                         qn  = q(j);
03486                         jtot = j;
03487                 }
03488         }
03489 
03490         for (k=-3; k<=3; k++) {
03491                 j = ((jtot+k+maxrin-1)%maxrin)+1;
03492                 t7(k+4) = q(j);
03493         }
03494 
03495         // interpolate
03496         prb1d(t7,7,&pos);
03497         tot = (float)(jtot)+pos;
03498         // Do not interpolate
03499         //tot = (float)(jtot);
03500 
03501         // mirrored
03502         fftr_d(t,ip);
03503 
03504         // find angle
03505         qm = -1.0e20;
03506         for (j=1; j<=maxrin;j++) {//cout <<"  "<<j<<"   "<<t(j) <<endl;
03507                 if ( t(j) >= qm ) {
03508                         qm   = t(j);
03509                         jtot = j;
03510                 }
03511         }
03512 
03513         for (k=-3; k<=3; k++)  {
03514                 j = ((jtot+k+maxrin-1)%maxrin) + 1;
03515                 t7(k+4) = t(j);
03516         }
03517 
03518         // interpolate
03519 
03520         prb1d(t7,7,&pos);
03521         tmt = float(jtot) + pos;
03522         // Do not interpolate
03523         //tmt = float(jtot);
03524 
03525         free(t);
03526         free(q);
03527 
03528         Dict retvals;
03529         retvals["qn"] = qn;
03530         retvals["tot"] = tot;
03531         retvals["qm"] = qm;
03532         retvals["tmt"] = tmt;
03533         return retvals;
03534 }

Dict Util::Crosrng_ms_delta ( EMData circ1,
EMData circ2,
vector< int >  numr,
float  delta_start,
float  delta 
) [static]

Definition at line 3536 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().

03536                                                                                                             {
03537         int nring = numr.size()/3;
03538         //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
03539         int maxrin = numr[numr.size()-1];
03540         double qn; float tot; double qm; float tmt;
03541         float *circ1 = circ1p->get_data();
03542         float *circ2 = circ2p->get_data();
03543 /*
03544 c
03545 c  checks both straight & mirrored positions
03546 c
03547 c  input - fourier transforms of rings!!
03548 c  circ1 already multiplied by weights!
03549 c
03550 */
03551 
03552         // dimension             circ1(lcirc),circ2(lcirc)
03553 
03554         // t(maxrin), q(maxrin), t7(-3:3)  //maxrin+2 removed
03555         double *t, *q;
03556 
03557         int   ip, jc, numr3i, numr2i, i, j, jtot = 0;
03558         float t1, t2, t3, t4, c1, c2, d1, d2;
03559 
03560         qn  = 0.0f;
03561         qm  = 0.0f;
03562         tot = 0.0f;
03563         tmt = 0.0f;
03564 #ifdef _WIN32
03565         ip = -(int)(log((float)maxrin)/log(2.0f));
03566 #else
03567         ip = -(int)(log2(maxrin));
03568 #endif  //_WIN32
03569   //for (j=1; j<=maxrin;j++) cout <<"  "<<j<<"   "<<circ1(j)<<"   "<<circ2(j) <<endl;
03570 
03571         //  c - straight  = circ1 * conjg(circ2)
03572         //  zero q array
03573 
03574         q = (double*)calloc(maxrin,sizeof(double));
03575 
03576         //   t - mirrored  = conjg(circ1) * conjg(circ2)
03577         //   zero t array
03578         t = (double*)calloc(maxrin,sizeof(double));
03579 
03580    //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
03581         for (i=1; i<=nring; i++) {
03582 
03583                 numr3i = numr(3,i);   // Number of samples of this ring
03584                 numr2i = numr(2,i);   // The beginning point of this ring
03585 
03586                 t1   = circ1(numr2i) * circ2(numr2i);
03587                 q(1) += t1;
03588                 t(1) += t1;
03589 
03590                 t1   = circ1(numr2i+1) * circ2(numr2i+1);
03591                 if (numr3i == maxrin)  {
03592                         q(2) += t1;
03593                         t(2) += t1;
03594                 } else {
03595                         q(numr3i+1) += t1;
03596                         t(numr3i+1) += t1;
03597                 }
03598 
03599                 for (j=3; j<=numr3i; j += 2) {
03600                         jc     = j+numr2i-1;
03601 
03602 // Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
03603 //                                ----- -----    ----- -----
03604 //                                 t1     t2      t3    t4
03605 // Here, conj(c1+c2i)*conj(d1+d2i) = (c1*d1-c2*d2)+(-c1*d2-c2*d1)i
03606 //                                    ----- -----    ----- -----
03607 //                                     t1    t2       t3    t4
03608 
03609                         c1     = circ1(jc);
03610                         c2     = circ1(jc+1);
03611                         d1     = circ2(jc);
03612                         d2     = circ2(jc+1);
03613 
03614                         t1     = c1 * d1;
03615                         t2     = c2 * d2;
03616                         t3     = c1 * d2;
03617                         t4     = c2 * d1;
03618 
03619                         q(j)   += t1 + t2;
03620                         q(j+1) += -t3 + t4;
03621                         t(j)   += t1 - t2;
03622                         t(j+1) += -t3 - t4;
03623                 }
03624         }
03625         //for (j=1; j<=maxrin; j++) cout <<"  "<<j<<"   "<<q(j) <<"   "<<t(j) <<endl;
03626         fftr_d(q,ip);
03627 
03628         qn  = -1.0e20;
03629 
03630         int jstart = 1+static_cast<int>(delta_start/360.0*maxrin);
03631         int jstep = static_cast<int>(delta/360.0*maxrin);
03632         if (jstep < 1) { jstep = 1; }
03633 
03634         for (j=jstart; j<=maxrin; j+=jstep) {//cout <<"  "<<j<<"   "<<q(j) <<endl;
03635                 if (q(j) >= qn) {
03636                         qn  = q(j);
03637                         jtot = j;
03638                 }
03639         }
03640 
03641         //for (k=-3; k<=3; k++) {
03642         //      j = ((jtot+k+maxrin-1)%maxrin)+1;
03643         //      t7(k+4) = q(j);
03644         //}
03645 
03646         // interpolate
03647         //prb1d(t7,7,&pos);
03648         //tot = (float)(jtot)+pos;
03649         // Do not interpolate
03650         tot = (float)(jtot);
03651 
03652         // mirrored
03653         fftr_d(t,ip);
03654 
03655         // find angle
03656         qm = -1.0e20;
03657         for (j=jstart; j<=maxrin;j+=jstep) {//cout <<"  "<<j<<" "<<t(j) <<endl;
03658                 if ( t(j) >= qm ) {
03659                         qm   = t(j);
03660                         jtot = j;
03661                 }
03662         }
03663 
03664         //for (k=-3; k<=3; k++)  {
03665         //      j = ((jtot+k+maxrin-1)%maxrin) + 1;
03666         //      t7(k+4) = t(j);
03667         //}
03668 
03669         // interpolate
03670 
03671         //prb1d(t7,7,&pos);
03672         //tmt = float(jtot) + pos;
03673         // Do not interpolate
03674         tmt = float(jtot);
03675 
03676         free(t);
03677         free(q);
03678 
03679         Dict retvals;
03680         retvals["qn"] = qn;
03681         retvals["tot"] = tot;
03682         retvals["qm"] = qm;
03683         retvals["tmt"] = tmt;
03684         return retvals;
03685 }

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 4115 of file util_sparx.cpp.

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

04115                                                                         {
04116 
04117    // dimension         circ1(lcirc),circ2(lcirc)
04118 
04119         int   ip, jc, numr3i, numr2i, i, j;
04120         float t1, t2, t3, t4, c1, c2, d1, d2;
04121 
04122         int nring = numr.size()/3;
04123         //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
04124         int maxrin = numr[numr.size()-1];
04125 
04126         float* circ1b = circ1->get_data();
04127         float* circ2b = circ2->get_data();
04128 
04129         // t(maxrin), q(maxrin)  // removed +2
04130         double *t, *q;
04131 
04132         q = (double*)calloc(maxrin,sizeof(double));
04133         t = (double*)calloc(maxrin,sizeof(double));
04134 
04135 #ifdef _WIN32
04136         ip = -(int)(log((float)maxrin)/log(2.0f));
04137 #else
04138         ip = -(int)(log2(maxrin));
04139 #endif  //_WIN32
04140 
04141         //  q - straight  = circ1 * conjg(circ2)
04142 
04143         //   t - mirrored  = conjg(circ1) * conjg(circ2)
04144 
04145         //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
04146 
04147         for (i=1; i<=nring; i++) {
04148 
04149                 numr3i = numr(3,i);
04150                 numr2i = numr(2,i);
04151 
04152                 t1   = circ1b(numr2i) * circ2b(numr2i);
04153                 q(1) = q(1)+t1;
04154                 t(1) = t(1)+t1;
04155 
04156                 t1   = circ1b(numr2i+1) * circ2b(numr2i+1);
04157                 if (numr3i == maxrin)  {
04158                         q(2) += t1;
04159                         t(2) += t1;
04160                 } else {
04161                         q(numr3i+1) += t1;
04162                         t(numr3i+1) += t1;
04163                 }
04164 
04165                 for (j=3; j<=numr3i; j=j+2) {
04166                         jc     = j+numr2i-1;
04167 
04168                         c1     = circ1b(jc);
04169                         c2     = circ1b(jc+1);
04170                         d1     = circ2b(jc);
04171                         d2     = circ2b(jc+1);
04172 
04173                         t1     = c1 * d1;
04174                         t3     = c1 * d2;
04175                         t2     = c2 * d2;
04176                         t4     = c2 * d1;
04177 
04178                         q(j)   += t1 + t2;
04179                         q(j+1) += - t3 + t4;
04180                         t(j)   += t1 - t2;
04181                         t(j+1) += - t3 - t4;
04182                 }
04183         }
04184 
04185         // straight
04186         fftr_d(q,ip);
04187 
04188         // mirrored
04189         fftr_d(t,ip);
04190 
04191         EMData* out = new EMData();
04192         out->set_size(maxrin,2,1);
04193         float *dout = out->get_data();
04194         for (int i=0; i<maxrin; i++) {dout(i,0)=static_cast<float>(q[i]); dout(i,1)=static_cast<float>(t[i]);}
04195         //out->set_size(maxrin,1,1);
04196         //float *dout = out->get_data();
04197         //for (int i=0; i<maxrin; i++) {dout(i,0)=q[i];}
04198         free(t);
04199         free(q);
04200         return out;
04201 }

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 4362 of file util_sparx.cpp.

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

04363 {
04364 
04365         int   ip, jc, numr3i, numr2i, i, j;
04366         float t1, t2, t3, t4, c1, c2, d1, d2;
04367 
04368         int nring = numr.size()/3;
04369         int maxrin = numr[numr.size()-1];
04370 
04371         float* circ1b = circ1->get_data();
04372         float* circ2b = circ2->get_data();
04373 
04374         double *t;
04375 
04376         t = (double*)calloc(maxrin,sizeof(double));
04377 
04378 #ifdef _WIN32
04379         ip = -(int)(log((float)maxrin)/log(2.0f));
04380 #else
04381         ip = -(int)(log2(maxrin));
04382 #endif  //_WIN32
04383 
04384          //   t - mirrored  = conjg(circ1) * conjg(circ2)
04385 
04386         for (i=1;i<=nring;i++) {
04387 
04388                 numr3i = numr(3,i);
04389                 numr2i = numr(2,i);
04390 
04391                 t1   = circ1b(numr2i) * circ2b(numr2i);
04392                 t(1) = t(1)+t1;
04393 
04394                 if (numr3i == maxrin)  {
04395                         t1   = circ1b(numr2i+1) * circ2b(numr2i+1);
04396                         t(2) = t(2)+t1;
04397                 }
04398 
04399                 for (j=3;j<=numr3i;j=j+2) {
04400                         jc     = j+numr2i-1;
04401 
04402                         c1     = circ1b(jc);
04403                         c2     = circ1b(jc+1);
04404                         d1     = circ2b(jc);
04405                         d2     = circ2b(jc+1);
04406 
04407                         t1     = c1 * d1;
04408                         t3     = c1 * d2;
04409                         t2     = c2 * d2;
04410                         t4     = c2 * d1;
04411 
04412                         t(j)   = t(j)   + t1 - t2;
04413                         t(j+1) = t(j+1) - t3 - t4;
04414                 }
04415         }
04416 
04417         // mirrored
04418         fftr_d(t,ip);
04419 
04420         EMData* out = new EMData();
04421         out->set_size(maxrin,1,1);
04422         float *dout = out->get_data();
04423         for (int i=0; i<maxrin; i++) dout[i]=static_cast<float>(t[i]);
04424         free(t);
04425         return out;
04426 
04427 }

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 4291 of file util_sparx.cpp.

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

04292 {
04293 
04294         int   ip, jc, numr3i, numr2i, i, j;
04295         float t1, t2, t3, t4, c1, c2, d1, d2;
04296 
04297         int nring = numr.size()/3;
04298         int maxrin = numr[numr.size()-1];
04299 
04300         float* circ1b = circ1->get_data();
04301         float* circ2b = circ2->get_data();
04302 
04303         double *q;
04304 
04305         q = (double*)calloc(maxrin,sizeof(double));
04306 
04307 #ifdef _WIN32
04308         ip = -(int)(log((float)maxrin)/log(2.0f));
04309 #else
04310         ip = -(int)(log2(maxrin));
04311 #endif  //_WIN32
04312 
04313          //  q - straight  = circ1 * conjg(circ2)
04314 
04315         for (i=1;i<=nring;i++) {
04316 
04317                 numr3i = numr(3,i);
04318                 numr2i = numr(2,i);
04319 
04320                 t1   = circ1b(numr2i) * circ2b(numr2i);
04321                 q(1) = q(1)+t1;
04322 
04323                 if (numr3i == maxrin)  {
04324                         t1   = circ1b(numr2i+1) * circ2b(numr2i+1);
04325                         q(2) = q(2)+t1;
04326                 } else {
04327                         t1              = circ1b(numr2i+1) * circ2b(numr2i+1);
04328                         q(numr3i+1) = q(numr3i+1)+t1;
04329                 }
04330 
04331                 for (j=3;j<=numr3i;j=j+2) {
04332                         jc     = j+numr2i-1;
04333 
04334                         c1     = circ1b(jc);
04335                         c2     = circ1b(jc+1);
04336                         d1     = circ2b(jc);
04337                         d2     = circ2b(jc+1);
04338 
04339                         t1     = c1 * d1;
04340                         t3     = c1 * d2;
04341                         t2     = c2 * d2;
04342                         t4     = c2 * d1;
04343 
04344                         q(j)   = q(j)   + t1 + t2;
04345                         q(j+1) = q(j+1) - t3 + t4;
04346                 }
04347         }
04348 
04349         // straight
04350         fftr_d(q,ip);
04351 
04352         EMData* out = new EMData();
04353         out->set_size(maxrin,1,1);
04354         float *dout = out->get_data();
04355         for (int i=0; i<maxrin; i++) dout[i]=static_cast<float>(q[i]);
04356         free(q);
04357         return out;
04358 
04359 }

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 4219 of file util_sparx.cpp.

References circ1, circ1b, circ2, circ2b, fftr_q(), log(), numr, q, and t.

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

04219                                                                                              {
04220 
04221    // dimension         circ1(lcirc),circ2(lcirc)
04222 
04223         int   ip, jc, numr3i, numr2i, i, j;
04224         float t1, t2, t3, t4, c1, c2, d1, d2;
04225 
04226         int nring = numr.size()/3;
04227         //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
04228         int maxrin = numr[numr.size()-1];
04229 
04230         float* circ1b = circ1->get_data();
04231         float* circ2b = circ2->get_data();
04232 
04233 #ifdef _WIN32
04234         ip = -(int)(log((float)maxrin)/log(2.0f));
04235 #else
04236         ip = -(int)(log2(maxrin));
04237 #endif  //_WIN32
04238         for (int i=1; i<=maxrin; i++)  {q(i) = 0.0f; t(i) = 0.0f;}
04239 
04240         //  q - straight  = circ1 * conjg(circ2)
04241 
04242         //   t - mirrored  = conjg(circ1) * conjg(circ2)
04243 
04244         for (i=1; i<=nring; i++) {
04245 
04246                 numr3i = numr(3,i);
04247                 numr2i = numr(2,i);
04248 
04249                 t1   = circ1b(numr2i) * circ2b(numr2i);
04250                 q(1) += t1;
04251                 t(1) += t1;
04252 
04253                 t1   = circ1b(numr2i+1) * circ2b(numr2i+1);
04254                 if (numr3i == maxrin)  {
04255                         q(2) += t1;
04256                         t(2) += t1;
04257                 } else {
04258                         q(numr3i+1) += t1;
04259                         t(numr3i+1) += t1;
04260                 }
04261 
04262                 for (j=3; j<=numr3i; j=j+2) {
04263                         jc     = j+numr2i-1;
04264 
04265                         c1     = circ1b(jc);
04266                         c2     = circ1b(jc+1);
04267                         d1     = circ2b(jc);
04268                         d2     = circ2b(jc+1);
04269 
04270                         t1     = c1 * d1;
04271                         t3     = c1 * d2;
04272                         t2     = c2 * d2;
04273                         t4     = c2 * d1;
04274 
04275                         q(j)   += t1 + t2;
04276                         q(j+1) += -t3 + t4;
04277                         t(j)   += t1 - t2;
04278                         t(j+1) += -t3 - t4;
04279                 }
04280         }
04281         // straight
04282         fftr_q(q,ip);
04283         //for (int i=0; i<maxrin; i++) cout<<i<<"  B    "<<q[i]<<"       "<<t[i]<<endl;
04284 
04285         // mirrored
04286         fftr_q(t,ip);
04287 }

vector< float > Util::Crosrng_msg_vec_p ( EMData circ1,
EMData circ2,
vector< int >  numr 
) [static]

Definition at line 4204 of file util_sparx.cpp.

References circ1, circ2, and Crosrng_msg_vec().

04204                                                                                      {
04205 
04206         int maxrin = numr[numr.size()-1];
04207 
04208         vector<float> r(2*maxrin);
04209 
04210         Crosrng_msg_vec( circ1, circ2, numr, &r[0], &r[maxrin] );
04211 
04212         return r;
04213 }

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

Definition at line 4016 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().

04016                                                                       {
04017         int nring = numr.size()/3;
04018         //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
04019         int maxrin = numr[numr.size()-1];
04020         double qn; float tot;
04021         float *circ1 = circ1p->get_data();
04022         float *circ2 = circ2p->get_data();
04023 /*
04024 c
04025 c  checks only straight position
04026 c
04027 c  input - fourier transforms of rings!!
04028 c  circ1 already multiplied by weights!
04029 c
04030 */
04031 
04032         // dimension             circ1(lcirc),circ2(lcirc)
04033 
04034         // q(maxrin), t7(-3:3)  //maxrin+2 removed
04035         double *q, t7[7];
04036 
04037         int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
04038         float c1, c2, d1, d2, pos;
04039 
04040         qn  = 0.0;
04041         tot = 0.0;
04042 #ifdef _WIN32
04043         ip = -(int)(log((float)maxrin)/log(2.0f));
04044 #else
04045    ip = -(int)(log2(maxrin));
04046 #endif  //_WIN32
04047         //for (j=1; j<=maxrin;j++) cout <<"  "<<j<<"   "<<circ1(j)<<"   "<<circ2(j) <<endl;
04048 
04049         //  c - straight  = circ1 * conjg(circ2)
04050         //  zero q array
04051 
04052         q = (double*)calloc(maxrin,sizeof(double));
04053 
04054                         //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
04055         for (i=1; i<=nring; i++) {
04056 
04057                 numr3i = numr(3,i);   // Number of samples of this ring
04058                 numr2i = numr(2,i);   // The beginning point of this ring
04059 
04060                 q(1) += circ1(numr2i) * circ2(numr2i);
04061 
04062                 if (numr3i == maxrin)   q(2) += circ1(numr2i+1) * circ2(numr2i+1);
04063                 else  q(numr3i+1) += circ1(numr2i+1) * circ2(numr2i+1);
04064 
04065                 for (j=3; j<=numr3i; j += 2) {
04066                         jc     = j+numr2i-1;
04067 
04068 // Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
04069 //                                ----- -----    ----- -----
04070 //                                 t1     t2      t3    t4
04071 
04072                         c1     = circ1(jc);
04073                         c2     = circ1(jc+1);
04074                         d1     = circ2(jc);
04075                         d2     = circ2(jc+1);
04076 
04077                         q(j)   += c1 * d1 + c2 * d2;
04078                         q(j+1) += -c1 * d2 + c2 * d1;
04079                 }
04080         }
04081 //for (j=1; j<=maxrin; j++) cout <<"  "<<j<<"   "<<q(j) <<endl;
04082         fftr_d(q,ip);
04083 
04084         qn  = -1.0e20;
04085         for (j=1; j<=maxrin; j++) {//cout <<"  "<<j<<"   "<<q(j) <<endl;
04086                 if (q(j) >= qn) {
04087                         qn  = q(j);
04088                         jtot = j;
04089                 }
04090         }
04091 
04092         for (k=-3; k<=3; k++)  {
04093                 j = ((jtot+k+maxrin-1)%maxrin)+1;
04094                 t7(k+4) = q(j);
04095         }
04096 
04097         // interpolate
04098         prb1d(t7,7,&pos);
04099         tot = (float)(jtot)+pos;
04100         // Do not interpolate
04101         //*tot = (float)(jtot);
04102 
04103         free(q);
04104 
04105         Dict retvals;
04106         retvals["qn"] = qn;
04107         retvals["tot"] = tot;
04108         return retvals;
04109 }

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 3897 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().

03897                                                                                                  {
03898 // Computes both straight and mirrored
03899 
03900         int nring = numr.size()/3;
03901         int maxrin = numr[numr.size()-1];
03902         double qn; float tot; double qm; float tmt;
03903         float *circ1 = circ1p->get_data();
03904         float *circ2 = circ2p->get_data();
03905 
03906         double *t, *q;
03907 
03908         int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
03909         float t1, t2, t3, t4, c1, c2, d1, d2;
03910 
03911         qn  = 0.0f;
03912         qm  = 0.0f;
03913         tot = 0.0f;
03914         tmt = 0.0f;
03915 #ifdef _WIN32
03916         ip = -(int)(log((float)maxrin)/log(2.0f));
03917 #else
03918         ip = -(int)(log2(maxrin));
03919 #endif  //_WIN32
03920 
03921         //  c - straight  = circ1 * conjg(circ2)
03922         //  zero q array
03923 
03924         q = (double*)calloc(maxrin,sizeof(double));
03925         
03926         //   t - mirrored  = conjg(circ1) * conjg(circ2)
03927         //   zero t array
03928         t = (double*)calloc(maxrin,sizeof(double));
03929         
03930    //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
03931         
03932         for (i=1; i<=nring; i++) {
03933 
03934                 numr3i = numr(3,i);   // Number of samples of this ring
03935                 numr2i = numr(2,i);   // The beginning point of this ring
03936 
03937                 t1   = circ1(numr2i) * circ2(numr2i);
03938                 q(1) += t1;
03939                 t(1) += t1;
03940                 
03941                 t1   = circ1(numr2i+1) * circ2(numr2i+1);
03942                 if (numr3i == maxrin)  {
03943                         q(2) += t1;
03944                         t(2) += t1;
03945                 } else {
03946                         q(numr3i+1) += t1;
03947                         t(numr3i+1) += t1;
03948                 }
03949 
03950                 for (j=3; j<=numr3i; j += 2) {
03951                         jc     = j+numr2i-1;
03952 
03953         // Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
03954         //                                ----- -----    ----- -----
03955         //                                 t1     t2      t3    t4
03956 
03957                         c1     = circ1(jc);
03958                         c2     = circ1(jc+1);
03959                         d1     = circ2(jc);
03960                         d2     = circ2(jc+1);
03961 
03962                         t1     = c1 * d1;
03963                         t3     = c1 * d2;
03964                         t2     = c2 * d2;
03965                         t4     = c2 * d1;
03966 
03967                         q(j)   += t1 + t2;
03968                         q(j+1) += -t3 + t4;
03969                         t(j)   += t1 - t2;
03970                         t(j+1) += -t3 - t4;
03971                 }
03972         }
03973          
03974         
03975         fftr_d(q,ip);
03976 
03977         qn  = -1.0e20;
03978         int psi_pos = int(psi/360.0*maxrin+0.5);
03979         const int psi_range = int(psi_max/360.0*maxrin + 0.5);
03980         
03981         for (k=-psi_range; k<=psi_range; k++) {
03982                 j = (k+psi_pos+maxrin-1)%maxrin+1;
03983                 if (q(j) >= qn) {
03984                         qn  = q(j);
03985                         jtot = j;
03986                 }
03987         }
03988 
03989         tot = (float)(jtot);
03990         free(q);
03991 
03992     // mirrored
03993         fftr_d(t,ip);
03994         
03995         qm  = -1.0e20;
03996         
03997         for (k=-psi_range; k<=psi_range; k++) {
03998                 j = (k+psi_pos+maxrin-1)%maxrin+1;
03999                 if (t(j) >= qm) {
04000                         qm  = t(j);
04001                         jtot = j;
04002                 }
04003         }
04004 
04005         tmt = (float)(jtot);
04006         free(t);
04007          
04008         Dict retvals;
04009         retvals["qn"] = qn;
04010         retvals["tot"] = tot;
04011         retvals["qm"] = qm;
04012         retvals["tmt"] = tmt;
04013         return retvals;
04014 }

Dict Util::Crosrng_psi_0_180_no_mirror ( EMData circ1,
EMData circ2,
vector< int >  numr,
float  psi_max 
) [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 3688 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_helical_90_local().

03688                                                                                                       {
03689         int nring = numr.size()/3;
03690         //int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
03691         int maxrin = numr[numr.size()-1];
03692         double qn; float tot;
03693         float *circ1 = circ1p->get_data();
03694         float *circ2 = circ2p->get_data();
03695 
03696         // dimension             circ1(lcirc),circ2(lcirc)
03697 
03698         // t(maxrin), q(maxrin), t7(-3:3)  //maxrin+2 removed
03699         double  *q, t7[7];
03700 
03701         int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
03702         float t1, t2, t3, t4, c1, c2, d1, d2, pos;
03703 
03704         qn  = 0.0f;
03705         tot = 0.0f;
03706 #ifdef _WIN32
03707         ip = -(int)(log((float)maxrin)/log(2.0f));
03708 #else
03709         ip = -(int)(log2(maxrin));
03710 #endif  //_WIN32
03711   //for (j=1; j<=maxrin;j++) cout <<"  "<<j<<"   "<<circ1(j)<<"   "<<circ2(j) <<endl;
03712 
03713         //  c - straight  = circ1 * conjg(circ2)
03714         //  zero q array
03715 
03716         q = (double*)calloc(maxrin,sizeof(double));
03717 
03718    //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
03719         for (i=1; i<=nring; i++) {
03720 
03721                 numr3i = numr(3,i);   // Number of samples of this ring
03722                 numr2i = numr(2,i);   // The beginning point of this ring
03723 
03724                 t1   = circ1(numr2i) * circ2(numr2i);
03725                 q(1) += t1;
03726                 
03727 
03728                 t1   = circ1(numr2i+1) * circ2(numr2i+1);
03729                 if (numr3i == maxrin)  {
03730                         q(2) += t1;
03731                         
03732                 } else {
03733                         q(numr3i+1) += t1;
03734                 }
03735 
03736                 for (j=3; j<=numr3i; j += 2) {
03737                         jc     = j+numr2i-1;
03738 
03739 // Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
03740 //                                ----- -----    ----- -----
03741 //                                 t1     t2      t3    t4
03742 // Here, conj(c1+c2i)*conj(d1+d2i) = (c1*d1-c2*d2)+(-c1*d2-c2*d1)i
03743 //                                    ----- -----    ----- -----
03744 //                                     t1    t2       t3    t4
03745 
03746                         c1     = circ1(jc);
03747                         c2     = circ1(jc+1);
03748                         d1     = circ2(jc);
03749                         d2     = circ2(jc+1);
03750 
03751                         t1     = c1 * d1;
03752                         t2     = c2 * d2;
03753                         t3     = c1 * d2;
03754                         t4     = c2 * d1;
03755 
03756                         q(j)   += t1 + t2;
03757                         q(j+1) += -t3 + t4;
03758                 
03759                 }
03760         }
03761         //for (j=1; j<=maxrin; j++) cout <<"  "<<j<<"   "<<q(j) <<"   "<<t(j) <<endl;
03762         fftr_d(q,ip);
03763 
03764         int psi_range  = int(psi_max/360.0*maxrin+0.5);
03765         const int psi_0 = 0;
03766         int psi_180    = int(  180.0/360.0*maxrin+0.5);
03767 
03768         qn  = -1.0e20;
03769         for (k=-psi_range; k<=psi_range; k++) {
03770                 j = (k+psi_0+maxrin-1)%maxrin+1;//string modemo = "f";cout <<" 90   "<<j<<"   "<<ang_n(j,modemo,maxrin) <<"  "<<float(j)/maxrin*360.0<<" "<<q(j) <<endl;
03771                 if (q(j) >= qn) {
03772                         qn  = q(j);
03773                         jtot = j;
03774                 }
03775         }
03776 
03777         for (k=-psi_range; k<=psi_range; k++) {
03778                 j = (k+psi_180+maxrin-1)%maxrin+1; //cout <<" 270  "<<j<<"  "<<ang_n(j,modemo,maxrin) <<"  "<<float(j)/maxrin*360.0<<" "<<q(j) <<endl;
03779                 if (q(j) >= qn) {
03780                         qn  = q(j);
03781                         jtot = j;
03782                 }
03783         }
03784 
03785         for (k=-3; k<=3; k++) {
03786                 j = ((jtot+k+maxrin-1)%maxrin)+1;
03787                 t7(k+4) = q(j);
03788         }
03789 
03790         // interpolate
03791         prb1d(t7,7,&pos);
03792         tot = (float)(jtot)+pos;
03793         // Do not interpolate
03794         //tot = (float)(jtot);
03795 
03796         free(q);
03797 
03798         Dict retvals;
03799         retvals["qn"] = qn;
03800         retvals["tot"] = tot;
03801         
03802         return retvals;
03803 }

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 3807 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(), and multiref_polar_ali_helical_local().

03807                                                                                                               {
03808 // flag 0 - straight, 1 - mirror
03809 
03810         int nring = numr.size()/3;
03811         int maxrin = numr[numr.size()-1];
03812         double qn; float tot;
03813         float *circ1 = circ1p->get_data();
03814         float *circ2 = circ2p->get_data();
03815 
03816         double *q;
03817 
03818         int   ip, jc, numr3i, numr2i, i, j, k, jtot = 0;
03819         float t1, t2, t3, t4, c1, c2, d1, d2;
03820 
03821         qn  = 0.0f;
03822         tot = 0.0f;
03823 #ifdef _WIN32
03824         ip = -(int)(log((float)maxrin)/log(2.0f));
03825 #else
03826         ip = -(int)(log2(maxrin));
03827 #endif  //_WIN32
03828 
03829         //  c - straight  = circ1 * conjg(circ2)
03830         //  zero q array
03831 
03832         q = (double*)calloc(maxrin,sizeof(double));
03833         int neg = 1-2*flag;
03834    //   premultiply  arrays ie( circ12 = circ1 * circ2) much slower
03835         
03836         for (i=1; i<=nring; i++) {
03837 
03838                 numr3i = numr(3,i);   // Number of samples of this ring
03839                 numr2i = numr(2,i);   // The beginning point of this ring
03840 
03841                 t1   = circ1(numr2i) * circ2(numr2i);
03842                 q(1) += t1;
03843 
03844                 t1   = circ1(numr2i+1) * circ2(numr2i+1);
03845                 if (numr3i == maxrin)  {
03846                         q(2) += t1;
03847                 } else {
03848                         q(numr3i+1) += t1;
03849                 }
03850 
03851                 for (j=3; j<=numr3i; j += 2) {
03852                         jc     = j+numr2i-1;
03853 
03854         // Here, (c1+c2i)*conj(d1+d2i) = (c1*d1+c2*d2)+(-c1*d2+c2*d1)i
03855         //                                ----- -----    ----- -----
03856         //                                 t1     t2      t3    t4
03857 
03858                         c1     = circ1(jc);
03859                         c2     = circ1(jc+1);
03860                         d1     = circ2(jc);
03861                         d2     = circ2(jc+1);
03862 
03863                         t1     = c1 * d1;
03864                         t3     = c1 * d2;
03865                         t2     = c2 * d2;
03866                         t4     = c2 * d1;
03867 
03868                         q(j)   += t1 + t2*neg;
03869                         q(j+1) += -t3 + t4*neg;
03870                 }
03871         }
03872          
03873         
03874         fftr_d(q,ip);
03875 
03876         qn  = -1.0e20;
03877         int psi_pos = int(psi/360.0*maxrin+0.5);
03878         const int psi_range = int(psi_max/360.0*maxrin + 0.5);
03879         
03880         for (k=-psi_range; k<=psi_range; k++) {
03881                 j = (k+psi_pos+maxrin-1)%maxrin+1;
03882                 if (q(j) >= qn) {
03883                         qn  = q(j);
03884                         jtot = j;
03885                 }
03886         }
03887 
03888         tot = (float)(jtot);
03889         free(q);
03890 
03891         Dict retvals;
03892         retvals["qn"] = qn;
03893         retvals["tot"] = tot;
03894         return retvals;
03895 }

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 20136 of file util_sparx.cpp.

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

20137 {
20138         int    ix, iy, iz;
20139         int    i,  j, k;
20140         int    nr2, nl2;
20141         float  az, ak;
20142         float  scx, scy, scz;
20143         int    offset = 2 - nx%2;
20144         int    lsm = nx + offset;
20145         EMData* ctf_img1 = new EMData();
20146         ctf_img1->set_size(lsm, ny, nz);
20147         float freq = 1.0f/(2.0f*ps);
20148         scx = 2.0f/float(nx);
20149         if(ny>=1) scy = 2.0f/float(ny); else scy=0.0f;
20150         if(nz>=1) scz = 2.0f/float(nz); else scz=0.0f;
20151         nr2 = ny/2 ;
20152         nl2 = nz/2 ;
20153         float pihalf = M_PI/2.0f;
20154         for ( k=0; k<nz;k++) {
20155                 iz = k;  if(k>nl2) iz=k-nz;
20156                 float oz2 = iz*scz*iz*scz;
20157                 for ( j=0; j<ny;j++) {
20158                         iy = j;  if(j>nr2) iy=j - ny;
20159                         float oy = iy*scy;
20160                         float oy2 = oy*oy;
20161                         for ( i=0; i<lsm/2; i++) {
20162                                 ix=i;
20163                                 if( dza == 0.0f) {
20164                                         ak=pow(ix*ix*scx*scx + oy2 + oz2, 0.5f)*freq;
20165                                         (*ctf_img1) (i*2,j,k)   = Util::tf(dz, ak, voltage, cs, wgh, b_factor, sign);
20166                                 } else {
20167                                         float ox = ix*scx;
20168                                         ak=pow(ox*ox + oy2 + oz2, 0.5f)*freq;
20169                                         az = atan2(oy, ox);
20170                                         float dzz = dz + dza/2.0f*sin(2*(az-azz*M_PI/180.0f-pihalf));
20171                                         (*ctf_img1) (i*2,j,k)   = Util::tf(dzz, ak, voltage, cs, wgh, b_factor, sign);
20172                                 }
20173                                 //(*ctf_img1) (i*2+1,j,k) = 0.0f;  PAP  I assumed new EMData sets to zero
20174                         }
20175                 }
20176         }
20177         ctf_img1->update();
20178         ctf_img1->set_complex(true);
20179         ctf_img1->set_ri(true);
20180         //ctf_img1->attr_dict["is_complex"] = 1;
20181         //ctf_img1->attr_dict["is_ri"] = 1;
20182         if(nx%2==0) ctf_img1->set_fftodd(false); else ctf_img1->set_fftodd(true);
20183         return ctf_img1;
20184 }

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 5453 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().

05453                                                  {
05454 
05455         if (image->is_complex()) throw ImageFormatException("Real image required for IntegerCyclicShift2DProcessor");
05456 
05457         int dx = params["dx"];
05458         int dy = params["dy"];
05459         int dz = params["dz"];
05460 
05461         // The reverse trick we're using shifts to the left (a negative shift)
05462         int nx = image->get_xsize();
05463         dx %= nx;
05464         if (dx < 0) dx += nx;
05465         int ny = image->get_ysize();
05466         dy %= ny;
05467         if (dy < 0) dy += ny;
05468         int nz = image->get_zsize();
05469         dz %= nz;
05470         if (dz < 0) dz += nz;
05471 
05472         int mx = -(dx - nx);
05473         int my = -(dy - ny);
05474         int mz = -(dz - nz);
05475 
05476         float* data = image->get_data();
05477         // x-reverses
05478         if (mx != 0) {
05479                 for (int iz = 0; iz < nz; iz++)
05480                        for (int iy = 0; iy < ny; iy++) {
05481                                 // reverses for column iy
05482                                 size_t offset = nx*iy + (size_t)nx*ny*iz; // starting location for column iy in slice iz
05483                                 reverse(&data[offset],&data[offset+mx]);
05484                                 reverse(&data[offset+mx],&data[offset+nx]);
05485                                 reverse(&data[offset],&data[offset+nx]);
05486                         }
05487         }
05488         // y-reverses
05489         if (my != 0) {
05490                 for (int iz = 0; iz < nz; iz++) {
05491                         size_t offset = (size_t)nx*ny*iz;
05492                         colreverse(&data[offset], &data[offset + my*nx], nx);
05493                         colreverse(&data[offset + my*nx], &data[offset + ny*nx], nx);
05494                         colreverse(&data[offset], &data[offset + ny*nx], nx);
05495                 }
05496         }
05497         if (mz != 0) {
05498                 slicereverse(&data[0], &data[(size_t)mz*ny*nx], nx, ny);
05499                 slicereverse(&data[mz*ny*nx], &data[(size_t)nz*ny*nx], nx, ny);
05500                 slicereverse(&data[0], &data[(size_t)nz*ny*nx], nx ,ny);
05501         }
05502         image->update();
05503 }

EMData * Util::decimate ( EMData img,
int  x_step,
int  y_step = 1,
int  z_step = 1 
) [static]

Definition at line 5247 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().

05248 {
05249         /* Exception Handle */
05250         if (!img) {
05251                 throw NullPointerException("NULL input image");
05252         }
05253         /* ============================== */
05254 
05255         // Get the size of the input image
05256         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
05257         /* ============================== */
05258 
05259 
05260         /* Exception Handle */
05261         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)
05262         {
05263                 LOGERR("Parameters for decimation cannot exceed the center of the image.");
05264                 throw ImageDimensionException("Parameters for decimation cannot exceed the center of the image.");
05265         }
05266         /* ============================== */
05267 
05268 
05269         /*    Calculation of the start point */
05270         int new_st_x=(nx/2)%x_step, new_st_y=(ny/2)%y_step, new_st_z=(nz/2)%z_step;
05271         /* ============================*/
05272 
05273 
05274         /* Calculation of the size of the decimated image */
05275         int rx=2*(nx/(2*x_step)), ry=2*(ny/(2*y_step)), rz=2*(nz/(2*z_step));
05276         int r1=int(ceil((nx-(x_step*rx))/(1.f*x_step))), r2=int(ceil((ny-(y_step*ry))/(1.f*y_step)));
05277         int r3=int(ceil((nz-(z_step*rz))/(1.f*z_step)));
05278         if(r1>1){r1=1;}
05279         if(r2>1){r2=1;}
05280         if(r3>1){r3=1;}
05281         int new_nx=rx+r1, new_ny=ry+r2, new_nz=rz+r3;
05282         /* ===========================================*/
05283 
05284 
05285         EMData* img2 = new EMData();
05286         img2->set_size(new_nx,new_ny,new_nz);
05287         float *new_ptr = img2->get_data();
05288         float *old_ptr = img->get_data();
05289         int iptr, jptr, kptr = 0;
05290         for (int k=new_st_z; k<nz; k+=z_step) {jptr=0;
05291                 for (int j=new_st_y; j<ny; j+=y_step) {iptr=0;
05292                         for (int i=new_st_x; i<nx; i+=x_step) {
05293                                 new_ptr(iptr,jptr,kptr) = old_ptr(i,j,k);
05294                         iptr++;}
05295                 jptr++;}
05296         kptr++;}
05297         img2->update();
05298         return img2;
05299 }

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

Definition at line 7758 of file util_sparx.cpp.

References ENTERFUNC, and EXITFUNC.

07759 {
07760         ENTERFUNC;
07761         int k, i;
07762         for(i=0; i<len; i++) key[i]=i+1;
07763 
07764         for(i = 0; i<len;i++){
07765                 k = rand()%len;
07766                 std::swap(key[k], key[i]);
07767                 std::swap(x[k], x[i]);
07768                 std::swap(y[k], y[i]);
07769         }
07770         EXITFUNC;
07771 }

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

Definition at line 17695 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().

17696 {
17697         ENTERFUNC;
17698         /* Exception Handle */
17699         if (!img) {
17700                 throw NullPointerException("NULL input image");
17701         }
17702         /* ========= img /= img1 ===================== */
17703 
17704         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17705         size_t size = (size_t)nx*ny*nz;
17706         float *img_ptr  = img->get_data();
17707         float *img1_ptr = img1->get_data();
17708         if(img->is_complex()) {
17709                 for (size_t i=0; i<size; i+=2) {
17710                         if(img1_ptr[i] > 1.e-10f) {
17711                         img_ptr[i]   /= img1_ptr[i];
17712                         img_ptr[i+1] /= img1_ptr[i];
17713                         } else img_ptr[i] = img_ptr[i+1] = 0.0f;
17714                 }
17715         } else throw ImageFormatException("Only Fourier image allowed");
17716 
17717         img->update();
17718 
17719         EXITFUNC;
17720 }

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

Definition at line 17666 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().

17667 {
17668         ENTERFUNC;
17669         /* Exception Handle */
17670         if (!img) {
17671                 throw NullPointerException("NULL input image");
17672         }
17673         /* ========= img /= img1 ===================== */
17674 
17675         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17676         size_t size = (size_t)nx*ny*nz;
17677         float *img_ptr  = img->get_data();
17678         float *img1_ptr = img1->get_data();
17679         if(img->is_complex()) {
17680                 float  sq2;
17681                 for (size_t i=0; i<size; i+=2) {
17682                         sq2 = 1.0f/(img1_ptr[i] * img1_ptr[i]   + img1_ptr[i+1] * img1_ptr[i+1]);
17683                         float tmp    = sq2*(img_ptr[i] * img1_ptr[i]   + img_ptr[i+1] * img1_ptr[i+1]) ;
17684                         img_ptr[i+1] = sq2*(img_ptr[i+1] * img1_ptr[i] - img_ptr[i] * img1_ptr[i+1]) ;
17685                         img_ptr[i]   = tmp;
17686                 }
17687         } else {
17688                 for (size_t i=0; i<size; ++i) img_ptr[i] /= img1_ptr[i];
17689         }
17690         img->update();
17691 
17692         EXITFUNC;
17693 }

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

Definition at line 17491 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().

17492 {
17493         ENTERFUNC;
17494         /* Exception Handle */
17495         if (!img) {
17496                 throw NullPointerException("NULL input image");
17497         }
17498         /* ========= img /= img1 ===================== */
17499 
17500         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17501         size_t size = (size_t)nx*ny*nz;
17502         EMData * img2 = img->copy_head();
17503         float *img_ptr  =img->get_data();
17504         float *img1_ptr = img1->get_data();
17505         float *img2_ptr = img2->get_data();
17506         if(img->is_complex()) {
17507                 for (size_t i=0; i<size; i+=2) {
17508                         if(img1_ptr[i] > 1.e-10f) {
17509                         img2_ptr[i]   = img_ptr[i]  /img1_ptr[i];
17510                         img2_ptr[i+1] = img_ptr[i+1]/img1_ptr[i];
17511                         } else img2_ptr[i] = img2_ptr[i+1] = 0.0f;
17512                 }
17513         } else  throw ImageFormatException("Only Fourier image allowed");
17514 
17515         img->update();
17516 
17517         EXITFUNC;
17518         return img2;
17519 }

EMData * Util::divn_img ( EMData img,
EMData img1 
) [static]

Definition at line 17458 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().

17459 {
17460         ENTERFUNC;
17461         /* Exception Handle */
17462         if (!img) {
17463                 throw NullPointerException("NULL input image");
17464         }
17465         /* ==============   output = img / img1   ================ */
17466 
17467         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17468         size_t size = (size_t)nx*ny*nz;
17469         EMData * img2 = img->copy_head();
17470         float *img_ptr  =img->get_data();
17471         float *img2_ptr = img2->get_data();
17472         float *img1_ptr = img1->get_data();
17473         if(img->is_complex()) {
17474                 float  sq2;
17475                 for (size_t i=0; i<size; i+=2) {
17476                         sq2 = 1.0f/(img1_ptr[i] * img1_ptr[i]   + img1_ptr[i+1] * img1_ptr[i+1]);
17477                         img2_ptr[i]   = sq2*(img_ptr[i] * img1_ptr[i]   + img_ptr[i+1] * img1_ptr[i+1]) ;
17478                         img2_ptr[i+1] = sq2*(img_ptr[i+1] * img1_ptr[i] - img_ptr[i] * img1_ptr[i+1]) ;
17479                 }
17480                 img2->set_complex(true);
17481                 if(img->is_fftodd()) img2->set_fftodd(true); else img2->set_fftodd(false);
17482         } else {
17483                 for (size_t i=0; i<size; ++i) img2_ptr[i] = img_ptr[i] / img1_ptr[i];
17484                 img2->update();
17485         }
17486 
17487         EXITFUNC;
17488         return img2;
17489 }

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] a The first number.
[in] b The second number.
Returns:
Copy sign of a number.

Definition at line 1984 of file util.h.

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] x A float number.
Returns:
The complementary error function of x.

Definition at line 2009 of file util.h.

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

float Util::ener ( EMData ave,
vector< int >  numr 
) [static]

Definition at line 4444 of file util_sparx.cpp.

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

Referenced by ener_tot().

04444                                               {
04445         ENTERFUNC;
04446         long double ener,en;
04447 
04448         int nring = numr.size()/3;
04449         float *aveptr = ave->get_data();
04450 
04451         ener = 0.0;
04452         for (int i=1; i<=nring; i++) {
04453                 int numr3i = numr(3,i);
04454                 int np     = numr(2,i)-1;
04455                 float tq = static_cast<float>(PI2*numr(1,i)/numr3i);
04456                 en = tq*(aveptr[np]*aveptr[np]+aveptr[np+1]*aveptr[np+1])*0.5;
04457                 for (int j=np+2; j<np+numr3i-1; j++) en += tq*aveptr[j]*aveptr[j];
04458                 ener += en/numr3i;
04459         }
04460         EXITFUNC;
04461         return static_cast<float>(ener);
04462 }

float Util::ener_tot ( const vector< EMData * > &  data,
vector< int >  numr,
vector< float >  tot 
) [static]

Definition at line 4464 of file util_sparx.cpp.

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

04464                                                                                      {
04465         ENTERFUNC;
04466         long double ener, en;
04467         float arg, cs, si;
04468 
04469         int nima = data.size();
04470         int nring = numr.size()/3;
04471         int maxrin = numr(3,nring);
04472 
04473         ener = 0.0;
04474         for (int i=1; i<=nring; i++) {
04475                 int numr3i = numr(3,i);
04476                 int np     = numr(2,i)-1;
04477                 float tq = static_cast<float>(PI2*numr(1,i)/numr3i);
04478                 float temp1 = 0.0, temp2 = 0.0;
04479                 for (int kk=0; kk<nima; kk++) {
04480                         float *ptr = data[kk]->get_data();
04481                         temp1 += ptr[np];
04482                         temp2 += static_cast<float>(ptr[np+1]*cos(PI2*(tot[kk]-1.0f)/2.0f*numr3i/maxrin));
04483                 }
04484                 en = tq*(temp1*temp1+temp2*temp2)*0.5;
04485                 for (int j=2; j<numr3i; j+=2) {
04486                         float tempr = 0.0, tempi = 0.0;
04487                         for (int kk=0; kk<nima; kk++) {
04488                                 float *ptr = data[kk]->get_data();
04489                                 arg = static_cast<float>( PI2*(tot[kk]-1.0)*(j/2)/maxrin );
04490                                 cs = cos(arg);
04491                                 si = sin(arg);
04492                                 tempr += ptr[np + j]*cs - ptr[np + j +1]*si;
04493                                 tempi += ptr[np + j]*si + ptr[np + j +1]*cs;
04494                         }
04495                         en += tq*(tempr*tempr+tempi*tempi);
04496                 }
04497                 ener += en/numr3i;
04498         }
04499         EXITFUNC;
04500         return static_cast<float>(ener);
04501 }

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:
p1 point one
p2 point two
p3 point three
plane a 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 1223 of file util.cpp.

References x, and y.

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

01224 {
01225         int x=0,y=1,z=2;
01226         plane[0] = p1[y]*(p2[z]-p3[z])+p2[y]*(p3[z]-p1[z])+p3[y]*(p1[z]-p2[z]);
01227         plane[1] = p1[z]*(p2[x]-p3[x])+p2[z]*(p3[x]-p1[x])+p3[z]*(p1[x]-p2[x]);
01228         plane[2] = p1[x]*(p2[y]-p3[y])+p2[x]*(p3[y]-p1[y])+p3[x]*(p1[y]-p2[y]);
01229         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]);
01230         plane[3] = -plane[3];
01231 }

float Util::eval ( char *  images,
EMData img,
vector< int >  S,
int  N,
int  K,
int  size 
) [static]

Definition at line 7106 of file util_sparx.cpp.

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

07107 {
07108         int j,d;
07109         EMData * e = new EMData();
07110         float *eptr, *imgptr;
07111         imgptr = img->get_data();
07112         float SSE = 0.f;
07113         for (j = 0 ; j < N ; j++) {
07114                 e->read_image(images,S[j]);
07115                 eptr = e->get_data();
07116                 for (d = 0; d < size; d++) {
07117                         SSE += ((eptr[d] - imgptr[d])*(eptr[d] - imgptr[d]));}
07118                 }
07119         delete e;
07120         return SSE;
07121 }

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] delta Delta theta (spacing in theta).
[in] t1 Starting (min) value of theta in degrees, default = 0.
[in] t2 Ending (max) value of theta in degrees, default = 90.
[in] p1 Starting (min) value of phi in degrees, default = 0.
[in] p2 Ending (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 511 of file util_sparx.cpp.

References angles, dgr_to_rad, phi, and theta.

00512 {
00513         vector<float> angles;
00514         float psi = 0.0;
00515         if ((0.0 == t1 && 0.0 == t2)||(t1 >= t2)) {
00516                 t1 = 0.0f;
00517                 t2 = 90.0f;
00518         }
00519         if ((0.0 == p1 && 0.0 == p2)||(p1 >= p2)) {
00520                 p1 = 0.0f;
00521                 p2 = 359.9f;
00522         }
00523         bool skip = ((t1 < 90.0)&&(90.0 == t2)&&(0.0 == p1)&&(p2 > 180.0));
00524         for (float theta = t1; theta <= t2; theta += delta) {
00525                 float detphi;
00526                 int lt;
00527                 if ((0.0 == theta)||(180.0 == theta)) {
00528                         detphi = 360.0f;
00529                         lt = 1;
00530                 } else {
00531                         detphi = delta/sin(theta*static_cast<float>(dgr_to_rad));
00532                         lt = int((p2 - p1)/detphi)-1;
00533                         if (lt < 1) lt = 1;
00534                         detphi = (p2 - p1)/lt;
00535                 }
00536                 for (int i = 0; i < lt; i++) {
00537                         float phi = p1 + i*detphi;
00538                         if (skip&&(90.0 == theta)&&(phi > 180.0)) continue;
00539                         angles.push_back(phi);
00540                         angles.push_back(theta);
00541                         angles.push_back(psi);
00542                 }
00543         }
00544         return angles;
00545 }

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 21049 of file util_sparx.cpp.

References k_means_cont_table_().

Referenced by initial_prune().

21049                                                                                                                                                                                 {
21050 
21051 
21052         if (size_next <= T) return 0;
21053 
21054         // take the intx of next and cur
21055         int* curintx2(0);
21056         int nintx = Util::k_means_cont_table_(curintx, next+2, curintx2, size_curintx, size_next,0);
21057         if (nintx <= T) return 0;
21058 
21059         int old_depth=depth;
21060         if (depth == partref) depth = depth + 1; // we skip classes in partref
21061         if (depth == nParts &&  old_depth>0) return 1;
21062 
21063         // have not yet reached a leaf, and current weight is still greather than T, so keep on going.
21064 
21065         curintx2 = new int[nintx]; // put the intersection set in here
21066         Util::k_means_cont_table_(curintx,next+2,curintx2, size_curintx, size_next,1);
21067 
21068         // if (old_depth > 0) {delete[] curintx;} // don't delete when depth == 0 because curintx in that case is a pointer into Parts
21069 
21070         // we now consider each of the classes in partition (depth+1) in turn
21071         bool gt_thresh;
21072         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
21073 
21074         for (int i=0; i < num_classes; i++){
21075                 if (Parts[depth][i][1] < 1) continue; // class is not active so move on
21076                 size_next = dimClasses[depth*K + Parts[depth][i][0] ]-2;
21077                 gt_thresh = explore(Parts,dimClasses, nParts, K, T, partref, curintx2,nintx, Parts[depth][i], size_next, depth+1);
21078                 if (gt_thresh) { delete[] curintx2; return 1; }
21079         }
21080         delete[] curintx2;
21081         return 0;
21082 }

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 20883 of file util_sparx.cpp.

References explore2(), and k_means_cont_table_().

20883                                                                                                                                                                                                                    {
20884 
20885 // depth is the level which is going to be explored in the current iteration
20886         int* curintx2(0);
20887         int nintx = size_curintx;
20888         
20889         
20890         // 2. take the intx of next and cur. Prune if <= T
20891         if (depth >0){
20892                 nintx = Util::k_means_cont_table_(curintx,next, curintx2, size_curintx, size_next,0);
20893                 if (nintx <= T) return; //prune!
20894         }
20895 
20896         // 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
20897         if (depth == (nParts-1)) {
20898                 
20899                 int replace = 0;
20900                 int ind_smallest = -1;
20901                 int smallest_cost = -1;
20902                 
20903                 for (int jit = 0; jit < J; jit++){
20904                         if (*(costlist+jit) < nintx){
20905                                 replace = 1;
20906                                 if (ind_smallest == -1) {ind_smallest = jit; smallest_cost = *(costlist+jit);}
20907                                 if (*(costlist+jit) < smallest_cost) {ind_smallest = jit; smallest_cost = *(costlist+jit);}
20908                         }       
20909                 }
20910                 
20911                 if (replace > 0){
20912                         // replace the smallest cost in matchlist with the current stuff
20913                         *(costlist + ind_smallest) = nintx;
20914                         for (int xit = 0; xit < nParts; xit++)
20915                                 *(matchlist + ind_smallest*nParts + xit) = *(curbranch+xit);
20916                                 
20917                 }
20918                 
20919                 return; 
20920         }
20921         
20922 
20923         // 3. have not yet reached a leaf, and current weight is still greather than T, so keep on going.
20924 
20925         if (depth > 0){
20926                 curintx2 = new int[nintx]; // put the intersection set in here
20927                 Util::k_means_cont_table_(curintx,next,curintx2, size_curintx, size_next,1);
20928         }
20929 
20930         if (depth == 0){
20931                 // set curintx2 to curintx
20932                 curintx2 = new int[size_curintx];
20933                 for (int cp = 0; cp < size_curintx; cp++) *(curintx2+cp) = *(curintx+cp);
20934         }
20935 
20936 
20937         // recursion (non-leaf case)
20938         depth=depth+1;
20939         // we now consider each of the classes in partition depth and recurse upon each of them
20940         for (int i=0; i < K; i++){
20941 
20942                 if (*(argParts + Indices[depth*K+i] + 1) < 1) continue; // class is not active so move on
20943                 size_next = (*(dimClasses + depth*K+i ))-2;
20944                 if (size_next <= T) continue;
20945                 *(curbranch+depth) = i;
20946                 Util::explore2(argParts,Indices, dimClasses, nParts, K, T, curintx2, nintx, argParts + Indices[depth*K+i] + 2, size_next, depth,J, matchlist,
20947                         costlist, curbranch);
20948                 
20949         }
20950 
20951         delete[] curintx2;
20952 }

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

Definition at line 1689 of file util_sparx.cpp.

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

01689                                                                                                                    {
01690 
01691         int nxreal = nx - 2;
01692         if (nxreal != ny)
01693                 throw ImageDimensionException("extractpoint requires ny == nx");
01694         int nhalf = nxreal/2;
01695         bool flip = (nuxnew < 0.f);
01696         if (flip) {
01697                 nuxnew *= -1;
01698                 nuynew *= -1;
01699         }
01700         if (nuynew >= nhalf-0.5)  {
01701                 nuynew -= nxreal;
01702         } else if (nuynew < -nhalf-0.5) {
01703                 nuynew += nxreal;
01704         }
01705 
01706         // put (xnew,ynew) on a grid.  The indices will be wrong for
01707         // the Fourier elements in the image, but the grid sizing will
01708         // be correct.
01709         int ixn = int(Util::round(nuxnew));
01710         int iyn = int(Util::round(nuynew));
01711 
01712         // set up some temporary weighting arrays
01713         static float wy[7];
01714         static float wx[7];
01715 
01716         float iynn = nuynew - iyn;
01717         wy[0] = kb.i0win_tab(iynn+3);
01718         wy[1] = kb.i0win_tab(iynn+2);
01719         wy[2] = kb.i0win_tab(iynn+1);
01720         wy[3] = kb.i0win_tab(iynn);
01721         wy[4] = kb.i0win_tab(iynn-1);
01722         wy[5] = kb.i0win_tab(iynn-2);
01723         wy[6] = kb.i0win_tab(iynn-3);
01724 
01725         float ixnn = nuxnew - ixn;
01726         wx[0] = kb.i0win_tab(ixnn+3);
01727         wx[1] = kb.i0win_tab(ixnn+2);
01728         wx[2] = kb.i0win_tab(ixnn+1);
01729         wx[3] = kb.i0win_tab(ixnn);
01730         wx[4] = kb.i0win_tab(ixnn-1);
01731         wx[5] = kb.i0win_tab(ixnn-2);
01732         wx[6] = kb.i0win_tab(ixnn-3);
01733 
01734         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]);
01735 
01736         complex<float> result(0.f,0.f);
01737         if ((ixn >= 3) && (ixn <= nhalf-3) && (iyn >= -nhalf+3) && (iyn <= nhalf-4)) {
01738                 // (xin,yin) not within window border from the edge
01739                 for (int iy = 0; iy < 7; iy++) {
01740                         int iyp = iyn + iy - 3 ;
01741                         for (int ix = 0; ix < 7; ix++) {
01742                                 int ixp = ixn + ix - 3;
01743                                 float w = wx[ix]*wy[iy];
01744                                 complex<float> val = fimage->cmplx(ixp,iyp);
01745                                 result += val*w;
01746                         }
01747                 }
01748         } else {
01749                 // points that "stick out"
01750                 for (int iy = 0; iy < 7; iy++) {
01751                         int iyp = iyn + iy - 3;
01752                         for (int ix = 0; ix < 7; ix++) {
01753                                 int ixp = ixn + ix - 3;
01754                                 bool mirror = false;
01755                                 int ixt = ixp, iyt = iyp;
01756                                 if (ixt < 0) {
01757                                         ixt = -ixt;
01758                                         iyt = -iyt;
01759                                         mirror = !mirror;
01760                                 }
01761                                 if (ixt > nhalf) {
01762                                         ixt = nxreal - ixt;
01763                                         iyt = -iyt;
01764                                         mirror = !mirror;
01765                                 }
01766                                 if (iyt > nhalf-1)  iyt -= nxreal;
01767                                 if (iyt < -nhalf)   iyt += nxreal;
01768                                 float w = wx[ix]*wy[iy];
01769                                 complex<float> val = fimage->cmplx(ixt,iyt);
01770                                 if (mirror)  result += conj(val)*w;
01771                                 else         result += val*w;
01772                         }
01773                 }
01774         }
01775         if (flip)  result = conj(result)/wsum;
01776         else result /= wsum;
01777         return result;
01778 }

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] x argument to acos(x)
Returns:
(float)acos(x)

Definition at line 783 of file util.cpp.

00783                                     {
00784 if (f>=1.0) return 0.0;
00785 if (f<=-1.0) return M_PI;
00786 
00787 static float *mem = (float *)malloc(sizeof(float)*2001);
00788 static bool needinit=true;
00789 
00790 
00791 if (needinit) {
00792         needinit=false;
00793         for (int i=0; i<=2000; i++) mem[i]=(float)acos(i/1000.0-1.0);
00794 }
00795 float f2=f*1000.0f+1000.0f;
00796 
00797 int g=(int)(f2+.5);
00798 
00799 return mem[g];
00800 
00801 // This version interpolates, but is slower
00802 /*int g=(int)f2;
00803 f2-=g;
00804 return mem[g+1]*f2+mem[g]*(1.0-f2);*/
00805 }

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] f argument to exp(f)
Returns:
(float)exp(x)

Definition at line 767 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().

00767                                    {
00768 static float *mem = (float *)malloc(sizeof(float)*1000);
00769 static bool needinit=true;
00770 
00771 if (needinit) {
00772         needinit=false;
00773         for (int i=0; i<1000; i++) mem[i]=(float)exp(-i/50.0);
00774 }
00775 if (f>0 || f<-19.98) return exp(f);
00776 int g=(int)(-f*50.0+0.5);
00777 
00778 return mem[g];
00779 }

static int EMAN::Util::fast_floor ( float  x  )  [inline, static]

A fast way to calculate a floor, which is largest integral value not greater than argument.

Parameters:
[in] x A float point number.
Returns:
floor of x.

Definition at line 1725 of file util.h.

Referenced by EMAN::CtfAverager::add_image(), EMAN::CircularMaskProcessor::calc_locals(), EMAN::EMData::cut_slice(), EMAN::EMData::dot_rotate_translate(), EMAN::EMData::extract_box(), EMAN::CtfAverager::finish(), EMAN::SymSearchProcessor::process_inplace(), EMAN::AddRandomNoiseProcessor::process_inplace(), EMAN::RotationalAverageProcessor::process_inplace(), EMAN::StandardProjector::project3d(), EMAN::EMData::sget_value_at_interp(), EMAN::TransformProcessor::transform(), and EMAN::EMData::unwrap().

void Util::fftc_d ( double *  br,
double *  bi,
int  ln,
int  ks 
) [static]

Definition at line 2622 of file util_sparx.cpp.

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

Referenced by fftr_d().

02623 {
02624         double rni,sgn,tr1,tr2,ti1,ti2;
02625         double cc,c,ss,s,t,x2,x3,x4,x5;
02626         int    b3,b4,b5,b6,b7,b56;
02627         int    n, k, l, j, i, ix0, ix1, status=0;
02628 
02629         const double tab1[] = {
02630                 9.58737990959775e-5,
02631                 1.91747597310703e-4,
02632                 3.83495187571395e-4,
02633                 7.66990318742704e-4,
02634                 1.53398018628476e-3,
02635                 3.06795676296598e-3,
02636                 6.13588464915449e-3,
02637                 1.22715382857199e-2,
02638                 2.45412285229123e-2,
02639                 4.90676743274181e-2,
02640                 9.80171403295604e-2,
02641                 1.95090322016128e-1,
02642                 3.82683432365090e-1,
02643                 7.07106781186546e-1,
02644                 1.00000000000000,
02645         };
02646 
02647         n=(int)pow(2.0f,ln);
02648 
02649         k=abs(ks);
02650         l=16-ln;
02651         b3=n*k;
02652         b6=b3;
02653         b7=k;
02654         if (ks > 0) {
02655                 sgn=1.0f;
02656         } else {
02657                 sgn=-1.0f;
02658                 rni=1.0f/(float)(n);
02659                 j=1;
02660                 for (i=1; i<=n; i++) {
02661                         br(j)=br(j)*rni;
02662                         bi(j)=bi(j)*rni;
02663                         j=j+k;
02664                 }
02665         }
02666 
02667 L12:
02668    b6=b6/2;
02669    b5=b6;
02670    b4=2*b6;
02671    b56=b5-b6;
02672 
02673 L14:
02674    tr1=br(b5+1);
02675    ti1=bi(b5+1);
02676    tr2=br(b56+1);
02677    ti2=bi(b56+1);
02678 
02679    br(b5+1)=tr2-tr1;
02680    bi(b5+1)=ti2-ti1;
02681    br(b56+1)=tr1+tr2;
02682    bi(b56+1)=ti1+ti2;
02683 
02684    b5=b5+b4;
02685    b56=b5-b6;
02686    if ( b5 <= b3 )  goto  L14;
02687    if ( b6 == b7 )  goto  L20;
02688 
02689    b4=b7;
02690    cc=2.0f*pow(tab1(l),2);
02691    c=1.0f-cc;
02692    l++;
02693    ss=sgn*tab1(l);
02694    s=ss;
02695 
02696 L16:
02697    b5=b6+b4;
02698    b4=2*b6;
02699    b56=b5-b6;
02700 
02701 L18:
02702    tr1=br(b5+1);
02703    ti1=bi(b5+1);
02704    tr2=br(b56+1);
02705    ti2=bi(b56+1);
02706    br(b5+1)=c*(tr2-tr1)-s*(ti2-ti1);
02707    bi(b5+1)=s*(tr2-tr1)+c*(ti2-ti1);
02708    br(b56+1)=tr1+tr2;
02709    bi(b56+1)=ti1+ti2;
02710 
02711    b5=b5+b4;
02712    b56=b5-b6;
02713    if ( b5 <= b3 )  goto  L18;
02714    b4=b5-b6;
02715    b5=b4-b3;
02716    c=-c;
02717    b4=b6-b5;
02718    if ( b5 < b4 )  goto  L16;
02719    b4=b4+b7;
02720    if ( b4 >= b5 ) goto  L12;
02721 
02722    t=c-cc*c-ss*s;
02723    s=s+ss*c-cc*s;
02724    c=t;
02725    goto  L16;
02726 
02727 L20:
02728    ix0=b3/2;
02729    b3=b3-b7;
02730    b4=0;
02731    b5=0;
02732    b6=ix0;
02733    ix1=0;
02734    if (b6 == b7) goto EXIT;
02735 
02736 L22:
02737    b4=b3-b4;
02738    b5=b3-b5;
02739    x2=br(b4+1);
02740    x3=br(b5+1);
02741    x4=bi(b4+1);
02742    x5=bi(b5+1);
02743    br(b4+1)=x3;
02744    br(b5+1)=x2;
02745    bi(b4+1)=x5;
02746    bi(b5+1)=x4;
02747    if(b6 < b4)  goto  L22;
02748 
02749 L24:
02750    b4=b4+b7;
02751    b5=b6+b5;
02752    x2=br(b4+1);
02753    x3=br(b5+1);
02754    x4=bi(b4+1);
02755    x5=bi(b5+1);
02756    br(b4+1)=x3;
02757    br(b5+1)=x2;
02758    bi(b4+1)=x5;
02759    bi(b5+1)=x4;
02760    ix0=b6;
02761 
02762 L26:
02763    ix0=ix0/2;
02764    ix1=ix1-ix0;
02765    if( ix1 >= 0)  goto L26;
02766 
02767    ix0=2*ix0;
02768    b4=b4+b7;
02769    ix1=ix1+ix0;
02770    b5=ix1;
02771    if ( b5 >= b4)  goto  L22;
02772    if ( b4 < b6)   goto  L24;
02773 
02774 EXIT:
02775    status = 0;
02776 }

void Util::fftc_q ( float *  br,
float *  bi,
int  ln,
int  ks 
) [static]

Definition at line 2779 of file util_sparx.cpp.

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

Referenced by fftr_q().

02780 {
02781         //  dimension  br(1),bi(1)
02782 
02783         int b3,b4,b5,b6,b7,b56;
02784         int n, k, l, j, i, ix0, ix1;
02785         float rni, tr1, ti1, tr2, ti2, cc, c, ss, s, t, x2, x3, x4, x5, sgn;
02786         int status=0;
02787 
02788         const float tab1[] = {
02789                 9.58737990959775e-5f,
02790                 1.91747597310703e-4f,
02791                 3.83495187571395e-4f,
02792                 7.66990318742704e-4f,
02793                 1.53398018628476e-3f,
02794                 3.06795676296598e-3f,
02795                 6.13588464915449e-3f,
02796                 1.22715382857199e-2f,
02797                 2.45412285229123e-2f,
02798                 4.90676743274181e-2f,
02799                 9.80171403295604e-2f,
02800                 1.95090322016128e-1f,
02801                 3.82683432365090e-1f,
02802                 7.07106781186546e-1f,
02803                 1.00000000000000f,
02804         };
02805 
02806         n=(int)pow(2.0f,ln);
02807 
02808         k=abs(ks);
02809         l=16-ln;
02810         b3=n*k;
02811         b6=b3;
02812         b7=k;
02813         if( ks > 0 ) {
02814                 sgn=1.0f;
02815         } else {
02816                 sgn=-1.0f;
02817                 rni=1.0f/(float)n;
02818                 j=1;
02819                 for (i=1; i<=n; i++) {
02820                         br(j)=br(j)*rni;
02821                         bi(j)=bi(j)*rni;
02822                         j=j+k;
02823                 }
02824         }
02825 L12:
02826    b6=b6/2;
02827    b5=b6;
02828    b4=2*b6;
02829    b56=b5-b6;
02830 L14:
02831    tr1=br(b5+1);
02832    ti1=bi(b5+1);
02833 
02834    tr2=br(b56+1);
02835    ti2=bi(b56+1);
02836 
02837    br(b5+1)=tr2-tr1;
02838    bi(b5+1)=ti2-ti1;
02839    br(b56+1)=tr1+tr2;
02840    bi(b56+1)=ti1+ti2;
02841 
02842    b5=b5+b4;
02843    b56=b5-b6;
02844    if ( b5 <= b3 )  goto  L14;
02845    if ( b6 == b7 )  goto  L20;
02846 
02847    b4=b7;
02848    cc=2.0f*pow(tab1(l),2);
02849    c=1.0f-cc;
02850    l++;
02851    ss=sgn*tab1(l);
02852    s=ss;
02853 L16:
02854    b5=b6+b4;
02855    b4=2*b6;
02856    b56=b5-b6;
02857 L18:
02858    tr1=br(b5+1);
02859    ti1=bi(b5+1);
02860    tr2=br(b56+1);
02861    ti2=bi(b56+1);
02862    br(b5+1)=c*(tr2-tr1)-s*(ti2-ti1);
02863    bi(b5+1)=s*(tr2-tr1)+c*(ti2-ti1);
02864    br(b56+1)=tr1+tr2;
02865    bi(b56+1)=ti1+ti2;
02866 
02867    b5=b5+b4;
02868    b56=b5-b6;
02869    if(b5 <= b3)  goto L18;
02870    b4=b5-b6;
02871    b5=b4-b3;
02872    c=-c;
02873    b4=b6-b5;
02874    if(b5 < b4)  goto  L16;
02875    b4=b4+b7;
02876    if(b4 >= b5) goto  L12;
02877 
02878    t=c-cc*c-ss*s;
02879    s=s+ss*c-cc*s;
02880    c=t;
02881    goto  L16;
02882 L20:
02883    ix0=b3/2;
02884    b3=b3-b7;
02885    b4=0;
02886    b5=0;
02887    b6=ix0;
02888    ix1=0;
02889    if ( b6 == b7) goto EXIT;
02890 L22:
02891    b4=b3-b4;
02892    b5=b3-b5;
02893    x2=br(b4+1);
02894    x3=br(b5+1);
02895    x4=bi(b4+1);
02896    x5=bi(b5+1);
02897    br(b4+1)=x3;
02898    br(b5+1)=x2;
02899    bi(b4+1)=x5;
02900    bi(b5+1)=x4;
02901    if (b6 < b4) goto  L22;
02902 L24:
02903    b4=b4+b7;
02904    b5=b6+b5;
02905    x2=br(b4+1);
02906    x3=br(b5+1);
02907    x4=bi(b4+1);
02908    x5=bi(b5+1);
02909    br(b4+1)=x3;
02910    br(b5+1)=x2;
02911    bi(b4+1)=x5;
02912    bi(b5+1)=x4;
02913    ix0=b6;
02914 L26:
02915    ix0=ix0/2;
02916    ix1=ix1-ix0;
02917    if(ix1 >= 0)  goto  L26;
02918 
02919    ix0=2*ix0;
02920    b4=b4+b7;
02921    ix1=ix1+ix0;
02922    b5=ix1;
02923    if (b5 >= b4)  goto  L22;
02924    if (b4 < b6)   goto  L24;
02925 EXIT:
02926    status = 0;
02927 }

void Util::fftr_d ( double *  xcmplx,
int  nv 
) [static]

Definition at line 3011 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_psi_0_180_no_mirror(), and Crosrng_sm_psi().

03012 {
03013         // double precision  x(2,1)
03014         int    i1, i2,  nu, inv, nu1, n, isub, n2, i;
03015         double tr1,tr2,ti1,ti2,tr,ti;
03016         double cc,c,ss,s,t;
03017         const double tab1[] = {
03018                 9.58737990959775e-5,
03019                 1.91747597310703e-4,
03020                 3.83495187571395e-4,
03021                 7.66990318742704e-4,
03022                 1.53398018628476e-3,
03023                 3.06795676296598e-3,
03024                 6.13588464915449e-3,
03025                 1.22715382857199e-2,
03026                 2.45412285229123e-2,
03027                 4.90676743274181e-2,
03028                 9.80171403295604e-2,
03029                 1.95090322016128e-1,
03030                 3.82683432365090e-1,
03031                 7.07106781186546e-1,
03032                 1.00000000000000,
03033         };
03034 
03035         nu=abs(nv);
03036         inv=nv/nu;
03037         nu1=nu-1;
03038         n=(int)pow(2.0f,nu1);
03039         isub=16-nu1;
03040         ss=-tab1(isub);
03041         cc=-2.0*pow(tab1(isub-1),2);
03042         c=1.0f;
03043         s=0.0f;
03044         n2=n/2;
03045 
03046         if ( inv > 0 ) {
03047                 fftc_d(&xcmplx(1,1),&xcmplx(2,1),nu1,2);
03048                 tr=xcmplx(1,1);
03049                 ti=xcmplx(2,1);
03050                 xcmplx(1,1)=tr+ti;
03051                 xcmplx(2,1)=tr-ti;
03052                 for (i=1;i<=n2;i++) {
03053                         i1=i+1;
03054                         i2=n-i+1;
03055                         tr1=xcmplx(1,i1);
03056                         tr2=xcmplx(1,i2);
03057                         ti1=xcmplx(2,i1);
03058                         ti2=xcmplx(2,i2);
03059                         t=(cc*c-ss*s)+c;
03060                         s=(cc*s+ss*c)+s;
03061                         c=t;
03062                         xcmplx(1,i1)=0.5*((tr1+tr2)+(ti1+ti2)*c-(tr1-tr2)*s);
03063                         xcmplx(1,i2)=0.5*((tr1+tr2)-(ti1+ti2)*c+(tr1-tr2)*s);
03064                         xcmplx(2,i1)=0.5*((ti1-ti2)-(ti1+ti2)*s-(tr1-tr2)*c);
03065                         xcmplx(2,i2)=0.5*(-(ti1-ti2)-(ti1+ti2)*s-(tr1-tr2)*c);
03066                 }
03067         } else {
03068                 tr=xcmplx(1,1);
03069                 ti=xcmplx(2,1);
03070                 xcmplx(1,1)=0.5*(tr+ti);
03071                 xcmplx(2,1)=0.5*(tr-ti);
03072                 for (i=1; i<=n2; i++) {
03073                         i1=i+1;
03074                         i2=n-i+1;
03075                         tr1=xcmplx(1,i1);
03076                         tr2=xcmplx(1,i2);
03077                         ti1=xcmplx(2,i1);
03078                         ti2=xcmplx(2,i2);
03079                         t=(cc*c-ss*s)+c;
03080                         s=(cc*s+ss*c)+s;
03081                         c=t;
03082                         xcmplx(1,i1)=0.5*((tr1+tr2)-(tr1-tr2)*s-(ti1+ti2)*c);
03083                         xcmplx(1,i2)=0.5*((tr1+tr2)+(tr1-tr2)*s+(ti1+ti2)*c);
03084                         xcmplx(2,i1)=0.5*((ti1-ti2)+(tr1-tr2)*c-(ti1+ti2)*s);
03085                         xcmplx(2,i2)=0.5*(-(ti1-ti2)+(tr1-tr2)*c-(ti1+ti2)*s);
03086                 }
03087                 fftc_d(&xcmplx(1,1),&xcmplx(2,1),nu1,-2);
03088         }
03089 }

void Util::fftr_q ( float *  xcmplx,
int  nv 
) [static]

Definition at line 2929 of file util_sparx.cpp.

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

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

02930 {
02931    // dimension xcmplx(2,1); xcmplx(1,i) --- real, xcmplx(2,i) --- imaginary
02932 
02933         int nu, inv, nu1, n, isub, n2, i1, i2, i;
02934         float ss, cc, c, s, tr, ti, tr1, tr2, ti1, ti2, t;
02935 
02936         const float tab1[] = {
02937                 9.58737990959775e-5f,
02938                 1.91747597310703e-4f,
02939                 3.83495187571395e-4f,
02940                 7.66990318742704e-4f,
02941                 1.53398018628476e-3f,
02942                 3.06795676296598e-3f,
02943                 6.13588464915449e-3f,
02944                 1.22715382857199e-2f,
02945                 2.45412285229123e-2f,
02946                 4.90676743274181e-2f,
02947                 9.80171403295604e-2f,
02948                 1.95090322016128e-1f,
02949                 3.82683432365090e-1f,
02950                 7.07106781186546e-1f,
02951                 1.00000000000000f,
02952         };
02953 
02954         nu=abs(nv);
02955         inv=nv/nu;
02956         nu1=nu-1;
02957         n=(int)pow(2.f,nu1);
02958         isub=16-nu1;
02959 
02960         ss=-tab1(isub);
02961         cc=-2.0f*pow(tab1(isub-1),2.f);
02962         c=1.0f;
02963         s=0.0f;
02964         n2=n/2;
02965         if ( inv > 0) {
02966                 fftc_q(&xcmplx(1,1),&xcmplx(2,1),nu1,2);
02967                 tr=xcmplx(1,1);
02968                 ti=xcmplx(2,1);
02969                 xcmplx(1,1)=tr+ti;
02970                 xcmplx(2,1)=tr-ti;
02971                 for (i=1;i<=n2;i++) {
02972                         i1=i+1;
02973                         i2=n-i+1;
02974                         tr1=xcmplx(1,i1);
02975                         tr2=xcmplx(1,i2);
02976                         ti1=xcmplx(2,i1);
02977                         ti2=xcmplx(2,i2);
02978                         t=(cc*c-ss*s)+c;
02979                         s=(cc*s+ss*c)+s;
02980                         c=t;
02981                         xcmplx(1,i1)=0.5f*((tr1+tr2)+(ti1+ti2)*c-(tr1-tr2)*s);
02982                         xcmplx(1,i2)=0.5f*((tr1+tr2)-(ti1+ti2)*c+(tr1-tr2)*s);
02983                         xcmplx(2,i1)=0.5f*((ti1-ti2)-(ti1+ti2)*s-(tr1-tr2)*c);
02984                         xcmplx(2,i2)=0.5f*(-(ti1-ti2)-(ti1+ti2)*s-(tr1-tr2)*c);
02985                 }
02986         } else {
02987                 tr=xcmplx(1,1);
02988                 ti=xcmplx(2,1);
02989                 xcmplx(1,1)=0.5f*(tr+ti);
02990                 xcmplx(2,1)=0.5f*(tr-ti);
02991                 for (i=1; i<=n2; i++) {
02992                         i1=i+1;
02993                         i2=n-i+1;
02994                         tr1=xcmplx(1,i1);
02995                         tr2=xcmplx(1,i2);
02996                         ti1=xcmplx(2,i1);
02997                         ti2=xcmplx(2,i2);
02998                         t=(cc*c-ss*s)+c;
02999                         s=(cc*s+ss*c)+s;
03000                         c=t;
03001                         xcmplx(1,i1)=0.5f*((tr1+tr2)-(tr1-tr2)*s-(ti1+ti2)*c);
03002                         xcmplx(1,i2)=0.5f*((tr1+tr2)+(tr1-tr2)*s+(ti1+ti2)*c);
03003                         xcmplx(2,i1)=0.5f*((ti1-ti2)+(tr1-tr2)*c-(ti1+ti2)*s);
03004                         xcmplx(2,i2)=0.5f*(-(ti1-ti2)+(tr1-tr2)*c-(ti1+ti2)*s);
03005                 }
03006                 fftc_q(&xcmplx(1,1),&xcmplx(2,1),nu1,-2);
03007         }
03008 }

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:
file The file to be locked.
Returns:
0 if lock succeeds. 1 if fails.

Definition at line 137 of file util.cpp.

References LOGERR, and NullPointerException.

00138 {
00139 #ifdef WIN32
00140         return 1;
00141 #else
00142 
00143         if (!file) {
00144                 throw NullPointerException("Tried to lock NULL file");
00145         }
00146 
00147         int fdes = fileno(file);
00148 
00149         struct flock fl;
00150         fl.l_type = F_WRLCK;
00151         fl.l_whence = SEEK_SET;
00152         fl.l_start = 0;
00153         fl.l_len = 0;
00154 #ifdef WIN32
00155         fl.l_pid = _getpid();
00156 #else
00157         fl.l_pid = getpid();
00158 #endif
00159 
00160 #if defined __sgi
00161         fl.l_sysid = getpid();
00162 #endif
00163 
00164         int err = 0;
00165         if (fcntl(fdes, F_SETLKW, &fl) == -1) {
00166                 LOGERR("file locking error! NFS problem?");
00167 
00168                 int i = 0;
00169                 for (i = 0; i < 5; i++) {
00170                         if (fcntl(fdes, F_SETLKW, &fl) != -1) {
00171                                 break;
00172                         }
00173                         else {
00174 #ifdef WIN32
00175                                 Sleep(1000);
00176 #else
00177                                 sleep(1);
00178 #endif
00179 
00180                         }
00181                 }
00182                 if (i == 5) {
00183                         LOGERR("Fatal file locking error");
00184                         err = 1;
00185                 }
00186         }
00187 
00188         return err;
00189 #endif
00190 }

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] data data array.
[in] nitems number of items in the data array.
[out] p_max_val pointer to the maximum value.
[out] p_max_index pointer to index of the maximum value.

Definition at line 814 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().

00815 {
00816         Assert(nitems > 0);
00817 
00818         if (!data || !max_val || !max_index) {
00819                 throw NullPointerException("data/max_val/max_index");
00820         }
00821         float max = -FLT_MAX;
00822         int m = 0;
00823 
00824         for (size_t i = 0; i < nitems; i++) {
00825                 if (data[i] > max) {
00826                         max = data[i];
00827                         m = (int)i;
00828                 }
00829         }
00830 
00831         *max_val = (float)max;
00832 
00833         if (max_index) {
00834                 *max_index = m;
00835         }
00836 }

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] data data array.
[in] nitems number of items in the data array.
[out] p_max_val pointer to the maximum value.
[out] p_min_val pointer to the minimum value.
[out] p_max_index pointer to index of the maximum value.
[out] p_min_index pointer to index of the minimum value.

Definition at line 838 of file util.cpp.

References Assert, max, min, and NullPointerException.

00841 {
00842         Assert(nitems > 0);
00843 
00844         if (!data || !max_val || !min_val || !max_index || !min_index) {
00845                 throw NullPointerException("data/max_val/min_val/max_index/min_index");
00846         }
00847         float max = -FLT_MAX;
00848         float min = FLT_MAX;
00849         int max_i = 0;
00850         int min_i = 0;
00851 
00852         for (size_t i = 0; i < nitems; i++) {
00853                 if (data[i] > max) {
00854                         max = data[i];
00855                         max_i = (int)i;
00856                 }
00857                 if (data[i] < min) {
00858                         min = data[i];
00859                         min_i = (int)i;
00860                 }
00861         }
00862 
00863         *max_val = max;
00864         *min_val = min;
00865 
00866         if (min_index) {
00867                 *min_index = min_i;
00868         }
00869 
00870         if (max_index) {
00871                 *max_index = max_i;
00872         }
00873 
00874 }

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

Definition at line 7797 of file util_sparx.cpp.

References ENTERFUNC, and EXITFUNC.

Referenced by voronoi().

07798 {
07799         ENTERFUNC;
07800         int i = k;
07801         while( i == k )  i = rand()%len;
07802         std::swap(key[i], key[k]);
07803         std::swap(x[i], x[k]);
07804         std::swap(y[i], y[k]);
07805         std::swap(z[i], z[k]);
07806         EXITFUNC;
07807 }

void Util::flip_complex_phase ( float *  data,
size_t  n 
) [static]

flip the phase of a complex data array.

Parameters:
data complex data array.
n array size.

Definition at line 82 of file util.cpp.

References Assert, and NullPointerException.

00083 {
00084         Assert(n > 0);
00085 
00086         if (!data) {
00087                 throw NullPointerException("pixel data array");
00088         }
00089 
00090         for (size_t i = 0; i < n; i += 2) {
00091                 data[i + 1] *= -1;
00092         }
00093 }

void Util::flip_image ( float *  data,
size_t  nx,
size_t  ny 
) [static]

Vertically flip the data of a 2D real image.

Parameters:
data Data array of the 2D real image.
nx Image Width.
ny Image Height.

Definition at line 217 of file util.cpp.

References Assert, and NullPointerException.

00218 {
00219         if (!data) {
00220                 throw NullPointerException("image data array");
00221         }
00222         Assert(nx > 0);
00223         Assert(ny > 0);
00224 
00225         float *buf = new float[nx];
00226         size_t row_size = nx * sizeof(float);
00227 
00228         for (size_t i = 0; i < ny / 2; i++) {
00229                 memcpy(buf, &data[i * nx], row_size);
00230                 memcpy(&data[i * nx], &data[(ny - 1 - i) * nx], row_size);
00231                 memcpy(&data[(ny - 1 - i) * nx], buf, row_size);
00232         }
00233 
00234         if( buf )
00235         {
00236                 delete[]buf;
00237                 buf = 0;
00238         }
00239 }

void Util::Frngs ( EMData circ,
vector< int >  numr 
) [static]

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

Definition at line 3096 of file util_sparx.cpp.

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

Referenced by ali2d_ccf_list(), multiref_peaks_ali2d(), multiref_peaks_compress_ali2d(), multiref_polar_ali_2d(), multiref_polar_ali_2d_delta(), multiref_polar_ali_2d_local(), multiref_polar_ali_2d_local_psi(), multiref_polar_ali_2d_nom(), multiref_polar_ali_2d_peaklist(), multiref_polar_ali_helical(), multiref_polar_ali_helical_90(), multiref_polar_ali_helical_90_local(), and multiref_polar_ali_helical_local().

03096                                                {
03097         int nring = numr.size()/3;
03098         float *circ = circp->get_data();
03099         int i, l;
03100         for (i=1; i<=nring;i++) {
03101 
03102 #ifdef _WIN32
03103                 l = (int)( log((float)numr(3,i))/log(2.0f) );
03104 #else
03105                 l=(int)(log2(numr(3,i)));
03106 #endif  //_WIN32
03107 
03108                 fftr_q(&circ(numr(2,i)),l);
03109         }
03110 }

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 3112 of file util_sparx.cpp.

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

03112                                                    {
03113         int nring = numr.size()/3;
03114         float *circ = circp->get_data();
03115         int i, l;
03116         for (i=1; i<=nring;i++) {
03117 
03118 #ifdef _WIN32
03119                 l = (int)( log((float)numr(3,i))/log(2.0f) );
03120 #else
03121                 l=(int)(log2(numr(3,i)));
03122 #endif  //_WIN32
03123 
03124                 fftr_q(&circ(numr(2,i)),-l);
03125         }
03126 }

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 20080 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().

20081 {
20082         int nx = mg->get_xsize();
20083         int ny = mg->get_ysize();
20084         int nz = mg->get_zsize();
20085 
20086         EMData* visited = new EMData();
20087         visited->set_size( nx, ny, nz );
20088         visited->to_zero();
20089         int grpid = 0;
20090         int maxgrp = 0;
20091         int maxsize = 0;
20092         for( int iz=0; iz < nz; ++iz ) {
20093                 for( int iy=0; iy < ny; ++iy ) {
20094                         for( int ix=0; ix < nx; ++ix ) {
20095                                 if( (*mg)(ix, iy, iz)==0.0 ) continue;
20096 
20097                                 if( (*visited)(ix, iy, iz) > 0.0 ) {
20098                                         // visited before, must be in other group.
20099                                         continue;
20100                                 }
20101 
20102                                 grpid++;
20103                                 int grpsize = find_group( ix, iy, iz, grpid, mg, visited );
20104                                 if( grpsize > maxsize ) {
20105                                         maxsize = grpsize;
20106                                         maxgrp = grpid;
20107                                 }
20108                         }
20109                 }
20110         }
20111 
20112         Assert( maxgrp > 0 );
20113 
20114         int npoint = 0;
20115         EMData* result = new EMData();
20116         result->set_size( nx, ny, nz );
20117         result->to_zero();
20118 
20119         for( int iz=0; iz < nz; ++iz ) {
20120                 for( int iy=0; iy < ny; ++iy ) {
20121                         for( int ix=0; ix < nx; ++ix ) {
20122                                 if( (*visited)(ix, iy, iz)==maxgrp ) {
20123                                         (*result)(ix,iy,iz) = 1.0;
20124                                         npoint++;
20125                                 }
20126                         }
20127                 }
20128         }
20129 
20130         Assert( npoint==maxsize );
20131         delete visited;
20132         return result;
20133 
20134 }

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

Get a filename's extension.

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

Definition at line 492 of file util.cpp.

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

00493 {
00494     if (filename == "") {
00495         return "";
00496     }
00497 
00498         string result = "";
00499         const char *ext = strrchr(filename.c_str(), '.');
00500         if (ext) {
00501                 ext++;
00502                 result = string(ext);
00503         }
00504         return result;
00505 }

float Util::get_frand ( double  low,
double  high 
) [static]

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

Parameters:
[in] low The lower bound of the random number.
[in] high The upper bound of the random number.
Returns:
The random number between low and high.

Definition at line 707 of file util.cpp.

References EMAN::Randnum::get_frand(), and EMAN::Randnum::Instance().

00708 {
00709         Randnum* randnum = Randnum::Instance();
00710         return randnum->get_frand(lo, hi);
00711 }

float Util::get_frand ( float  low,
float  high 
) [static]

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

Parameters:
[in] low The lower bound of the random number.
[in] high The upper bound of the random number.
Returns:
The random number between low and high.

Definition at line 701 of file util.cpp.

References EMAN::Randnum::get_frand(), and EMAN::Randnum::Instance().

00702 {
00703         Randnum* randnum = Randnum::Instance();
00704         return randnum->get_frand(lo, hi);
00705 }

float Util::get_frand ( int  low,
int  high 
) [static]

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

Parameters:
[in] low The lower bound of the random number.
[in] high The upper bound of the random number.
Returns:
The random number between low and high.

Definition at line 696 of file util.cpp.

Referenced by EMAN::OrientationGenerator::add_orientation(), ali2d_ccf_list(), EMAN::RandomOrientationGenerator::gen_orientations(), move_points(), EMAN::KmeansSegmentProcessor::process(), EMAN::TestImageFourierNoiseProfile::process_inplace(), EMAN::TestImageFourierNoiseGaussian::process_inplace(), EMAN::LowpassRandomPhaseProcessor::process_inplace(), and EMAN::PointArray::set_from_density_map().

00697 {
00698         return get_frand((float)lo, (float)hi);
00699 }

float Util::get_gauss_rand ( float  mean,
float  sigma 
) [static]

Get a Gaussian random number.

Parameters:
[in] mean The gaussian mean
[in] sigma The gaussian sigma
Returns:
the gaussian random number.

Definition at line 808 of file util.cpp.

References EMAN::Randnum::get_gauss_rand(), and EMAN::Randnum::Instance().

Referenced by EMAN::EmanOrientationGenerator::gen_orientations(), and EMAN::MaskNoiseProcessor::process_dist_pixel().

00809 {
00810         Randnum* randnum = Randnum::Instance();
00811         return randnum->get_gauss_rand(mean, sigma);
00812 }

int Util::get_irand ( int  low,
int  high 
) [static]

Get an integer random number between low and high, [low, high].

Parameters:
[in] low The lower bound of the random number.
[in] high The upper bound of the random number.
Returns:
The random number between low and high.

Definition at line 690 of file util.cpp.

References EMAN::Randnum::get_irand(), and EMAN::Randnum::Instance().

Referenced by EMAN::KMeansAnalyzer::analyze(), cluster_pairwise(), move_points(), and EMAN::KMeansAnalyzer::reseed().

00691 {
00692         Randnum* randnum = Randnum::Instance();
00693         return randnum->get_irand(lo, hi);
00694 }

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] str A multiple-line string.
Returns:
A single line.

Definition at line 267 of file util.cpp.

References NullPointerException.

Referenced by EMAN::XplorIO::is_valid(), and EMAN::SitusIO::is_valid().

00268 {
00269         if (!slines || !(*slines)) {
00270                 throw NullPointerException("Null string");
00271         }
00272 
00273         string result = "";
00274         char *str = *slines;
00275 
00276         while (*str != '\n' && *str != '\0') {
00277                 result.push_back(*str);
00278                 str++;
00279         }
00280         if (*str != '\0') {
00281                 str++;
00282         }
00283         *slines = str;
00284 
00285         return result;
00286 }

static float EMAN::Util::get_max ( float  f1,
float  f2,
float  f3,
float  f4 
) [inline, static]

Get the maximum of 4 numbers.

Parameters:
[in] f1 The first number.
[in] f2 The second number.
[in] f3 The third number.
[in] f4 The fourth number.
Returns:
The maximum of 4 numbers.

Definition at line 1870 of file util.h.

static float EMAN::Util::get_max ( float  f1,
float  f2,
float  f3 
) [inline, static]

Get the maximum of 3 numbers.

Parameters:
[in] f1 The first number.
[in] f2 The second number.
[in] f3 The third number.
Returns:
The maximum of 3 numbers.

Definition at line 1852 of file util.h.

static float EMAN::Util::get_max ( float  f1,
float  f2 
) [inline, static]

Get the maximum of 2 numbers.

Parameters:
[in] f1 The first number.
[in] f2 The second number.
Returns:
The maximum of 2 numbers.

Definition at line 1841 of file util.h.

Referenced by EMAN::PawelProjector::project3d().

static float EMAN::Util::get_min ( float  f1,
float  f2,
float  f3,
float  f4 
) [inline, static]

Get the minimum of 4 numbers.

Parameters:
[in] f1 The first number.
[in] f2 The second number.
[in] f3 The third number.
[in] f4 The fourth number.
Returns:
The minimum of 4 numbers.

Definition at line 1821 of file util.h.

static float EMAN::Util::get_min ( float  f1,
float  f2,
float  f3 
) [inline, static]

Get the minimum of 3 numbers.

Parameters:
[in] f1 The first number.
[in] f2 The second number.
[in] f3 The third number.
Returns:
The minimum of 3 numbers.

Definition at line 1802 of file util.h.

static float EMAN::Util::get_min ( float  f1,
float  f2 
) [inline, static]

Get the minimum of 2 numbers.

Parameters:
[in] f1 The first number.
[in] f2 The second number.
Returns:
The minimum of 2 numbers.

Definition at line 1791 of file util.h.

static int EMAN::Util::get_min ( int  f1,
int  f2,
int  f3 
) [inline, static]

Get the minimum of 3 numbers.

Parameters:
[in] f1 The first number.
[in] f2 The second number.
[in] f3 The third number.
Returns:
The minimum of 3 numbers.

Definition at line 1775 of file util.h.

static int EMAN::Util::get_min ( int  f1,
int  f2 
) [inline, static]

Get the minimum of 2 numbers.

Parameters:
[in] f1 The first number.
[in] f2 The second number.
Returns:
The minimum of 2 numbers.

Definition at line 1764 of file util.h.

Referenced by EMAN::PawelProjector::backproject3d(), EMAN::ChaoProjector::backproject3d(), EMAN::ChaoProjector::project3d(), and EMAN::FourierGriddingProjector::project3d().

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 779 of file util_sparx.cpp.

References EMAN::Util::KaiserBessel::get_window_size(), EMAN::Util::KaiserBessel::i0win_tab(), restrict1(), and round().

Referenced by EMAN::EMData::helicise_grid(), Polar2Dmi(), EMAN::EMData::rot_scale_conv_new(), and EMAN::EMData::rot_scale_conv_new_3D().

00779                                                                                                                              {
00780         int K = kb.get_window_size();
00781         int kbmin = -K/2;
00782         int kbmax = -kbmin;
00783         int kbc = kbmax+1;
00784 
00785         float pixel =0.0f;
00786         float w=0.0f;
00787 
00788         delx = restrict1(delx, nx);
00789         int inxold = int(round(delx));
00790         if ( ny < 2 ) {  //1D
00791                 float tablex1 = kb.i0win_tab(delx-inxold+3);
00792                 float tablex2 = kb.i0win_tab(delx-inxold+2);
00793                 float tablex3 = kb.i0win_tab(delx-inxold+1);
00794                 float tablex4 = kb.i0win_tab(delx-inxold);
00795                 float tablex5 = kb.i0win_tab(delx-inxold-1);
00796                 float tablex6 = kb.i0win_tab(delx-inxold-2);
00797                 float tablex7 = kb.i0win_tab(delx-inxold-3);
00798 
00799                 int x1, x2, x3, x4, x5, x6, x7;
00800 
00801                 if ( inxold <= kbc || inxold >=nx-kbc-2 )  {
00802                         x1 = (inxold-3+nx)%nx;
00803                         x2 = (inxold-2+nx)%nx;
00804                         x3 = (inxold-1+nx)%nx;
00805                         x4 = (inxold  +nx)%nx;
00806                         x5 = (inxold+1+nx)%nx;
00807                         x6 = (inxold+2+nx)%nx;
00808                         x7 = (inxold+3+nx)%nx;
00809                 } else {
00810                         x1 = inxold-3;
00811                         x2 = inxold-2;
00812                         x3 = inxold-1;
00813                         x4 = inxold;
00814                         x5 = inxold+1;
00815                         x6 = inxold+2;
00816                         x7 = inxold+3;
00817                 }
00818 
00819                 pixel = data[x1]*tablex1 + data[x2]*tablex2 + data[x3]*tablex3 +
00820                         data[x4]*tablex4 + data[x5]*tablex5 + data[x6]*tablex6 +
00821                         data[x7]*tablex7 ;
00822 
00823                 w = tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7;
00824         } else if ( nz < 2 ) {  // 2D
00825                 dely = restrict1(dely, ny);
00826                 int inyold = int(round(dely));
00827                 float tablex1 = kb.i0win_tab(delx-inxold+3);
00828                 float tablex2 = kb.i0win_tab(delx-inxold+2);
00829                 float tablex3 = kb.i0win_tab(delx-inxold+1);
00830                 float tablex4 = kb.i0win_tab(delx-inxold);
00831                 float tablex5 = kb.i0win_tab(delx-inxold-1);
00832                 float tablex6 = kb.i0win_tab(delx-inxold-2);
00833                 float tablex7 = kb.i0win_tab(delx-inxold-3);
00834 
00835                 float tabley1 = kb.i0win_tab(dely-inyold+3);
00836                 float tabley2 = kb.i0win_tab(dely-inyold+2);
00837                 float tabley3 = kb.i0win_tab(dely-inyold+1);
00838                 float tabley4 = kb.i0win_tab(dely-inyold);
00839                 float tabley5 = kb.i0win_tab(dely-inyold-1);
00840                 float tabley6 = kb.i0win_tab(dely-inyold-2);
00841                 float tabley7 = kb.i0win_tab(dely-inyold-3);
00842 
00843                 int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7;
00844 
00845                 if ( inxold <= kbc || inxold >=nx-kbc-2 || inyold <= kbc || inyold >=ny-kbc-2 )  {
00846                         x1 = (inxold-3+nx)%nx;
00847                         x2 = (inxold-2+nx)%nx;
00848                         x3 = (inxold-1+nx)%nx;
00849                         x4 = (inxold  +nx)%nx;
00850                         x5 = (inxold+1+nx)%nx;
00851                         x6 = (inxold+2+nx)%nx;
00852                         x7 = (inxold+3+nx)%nx;
00853 
00854                         y1 = ((inyold-3+ny)%ny)*nx;
00855                         y2 = ((inyold-2+ny)%ny)*nx;
00856                         y3 = ((inyold-1+ny)%ny)*nx;
00857                         y4 = ((inyold  +ny)%ny)*nx;
00858                         y5 = ((inyold+1+ny)%ny)*nx;
00859                         y6 = ((inyold+2+ny)%ny)*nx;
00860                         y7 = ((inyold+3+ny)%ny)*nx;
00861                 } else {
00862                         x1 = inxold-3;
00863                         x2 = inxold-2;
00864                         x3 = inxold-1;
00865                         x4 = inxold;
00866                         x5 = inxold+1;
00867                         x6 = inxold+2;
00868                         x7 = inxold+3;
00869 
00870                         y1 = (inyold-3)*nx;
00871                         y2 = (inyold-2)*nx;
00872                         y3 = (inyold-1)*nx;
00873                         y4 = inyold*nx;
00874                         y5 = (inyold+1)*nx;
00875                         y6 = (inyold+2)*nx;
00876                         y7 = (inyold+3)*nx;
00877                 }
00878 
00879                 pixel    = ( data[x1+y1]*tablex1 + data[x2+y1]*tablex2 + data[x3+y1]*tablex3 +
00880                              data[x4+y1]*tablex4 + data[x5+y1]*tablex5 + data[x6+y1]*tablex6 +
00881                              data[x7+y1]*tablex7 ) * tabley1 +
00882                            ( data[x1+y2]*tablex1 + data[x2+y2]*tablex2 + data[x3+y2]*tablex3 +
00883                              data[x4+y2]*tablex4 + data[x5+y2]*tablex5 + data[x6+y2]*tablex6 +
00884                              data[x7+y2]*tablex7 ) * tabley2 +
00885                            ( data[x1+y3]*tablex1 + data[x2+y3]*tablex2 + data[x3+y3]*tablex3 +
00886                              data[x4+y3]*tablex4 + data[x5+y3]*tablex5 + data[x6+y3]*tablex6 +
00887                              data[x7+y3]*tablex7 ) * tabley3 +
00888                            ( data[x1+y4]*tablex1 + data[x2+y4]*tablex2 + data[x3+y4]*tablex3 +
00889                              data[x4+y4]*tablex4 + data[x5+y4]*tablex5 + data[x6+y4]*tablex6 +
00890                              data[x7+y4]*tablex7 ) * tabley4 +
00891                            ( data[x1+y5]*tablex1 + data[x2+y5]*tablex2 + data[x3+y5]*tablex3 +
00892                              data[x4+y5]*tablex4 + data[x5+y5]*tablex5 + data[x6+y5]*tablex6 +
00893                              data[x7+y5]*tablex7 ) * tabley5 +
00894                            ( data[x1+y6]*tablex1 + data[x2+y6]*tablex2 + data[x3+y6]*tablex3 +
00895                              data[x4+y6]*tablex4 + data[x5+y6]*tablex5 + data[x6+y6]*tablex6 +
00896                              data[x7+y6]*tablex7 ) * tabley6 +
00897                            ( data[x1+y7]*tablex1 + data[x2+y7]*tablex2 + data[x3+y7]*tablex3 +
00898                              data[x4+y7]*tablex4 + data[x5+y7]*tablex5 + data[x6+y7]*tablex6 +
00899                              data[x7+y7]*tablex7 ) * tabley7;
00900 
00901                 w = (tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7) *
00902                     (tabley1+tabley2+tabley3+tabley4+tabley5+tabley6+tabley7);
00903         } else {  //  3D
00904                 dely = restrict1(dely, ny);
00905                 int inyold = int(Util::round(dely));
00906                 delz = restrict1(delz, nz);
00907                 int inzold = int(Util::round(delz));
00908 
00909                 float tablex1 = kb.i0win_tab(delx-inxold+3);
00910                 float tablex2 = kb.i0win_tab(delx-inxold+2);
00911                 float tablex3 = kb.i0win_tab(delx-inxold+1);
00912                 float tablex4 = kb.i0win_tab(delx-inxold);
00913                 float tablex5 = kb.i0win_tab(delx-inxold-1);
00914                 float tablex6 = kb.i0win_tab(delx-inxold-2);
00915                 float tablex7 = kb.i0win_tab(delx-inxold-3);
00916 
00917                 float tabley1 = kb.i0win_tab(dely-inyold+3);
00918                 float tabley2 = kb.i0win_tab(dely-inyold+2);
00919                 float tabley3 = kb.i0win_tab(dely-inyold+1);
00920                 float tabley4 = kb.i0win_tab(dely-inyold);
00921                 float tabley5 = kb.i0win_tab(dely-inyold-1);
00922                 float tabley6 = kb.i0win_tab(dely-inyold-2);
00923                 float tabley7 = kb.i0win_tab(dely-inyold-3);
00924 
00925                 float tablez1 = kb.i0win_tab(delz-inzold+3);
00926                 float tablez2 = kb.i0win_tab(delz-inzold+2);
00927                 float tablez3 = kb.i0win_tab(delz-inzold+1);
00928                 float tablez4 = kb.i0win_tab(delz-inzold);
00929                 float tablez5 = kb.i0win_tab(delz-inzold-1);
00930                 float tablez6 = kb.i0win_tab(delz-inzold-2);
00931                 float tablez7 = kb.i0win_tab(delz-inzold-3);
00932 
00933                 int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7, z1, z2, z3, z4, z5, z6, z7;
00934 
00935                 if ( inxold <= kbc || inxold >=nx-kbc-2 || inyold <= kbc || inyold >=ny-kbc-2 || inzold <= kbc || inzold >= nz-kbc-2 )  {
00936                         x1 = (inxold-3+nx)%nx;
00937                         x2 = (inxold-2+nx)%nx;
00938                         x3 = (inxold-1+nx)%nx;
00939                         x4 = (inxold  +nx)%nx;
00940                         x5 = (inxold+1+nx)%nx;
00941                         x6 = (inxold+2+nx)%nx;
00942                         x7 = (inxold+3+nx)%nx;
00943 
00944                         y1 = ((inyold-3+ny)%ny)*nx;
00945                         y2 = ((inyold-2+ny)%ny)*nx;
00946                         y3 = ((inyold-1+ny)%ny)*nx;
00947                         y4 = ((inyold  +ny)%ny)*nx;
00948                         y5 = ((inyold+1+ny)%ny)*nx;
00949                         y6 = ((inyold+2+ny)%ny)*nx;
00950                         y7 = ((inyold+3+ny)%ny)*nx;
00951 
00952                         z1 = ((inzold-3+nz)%nz)*nx*ny;
00953                         z2 = ((inzold-2+nz)%nz)*nx*ny;
00954                         z3 = ((inzold-1+nz)%nz)*nx*ny;
00955                         z4 = ((inzold  +nz)%nz)*nx*ny;
00956                         z5 = ((inzold+1+nz)%nz)*nx*ny;
00957                         z6 = ((inzold+2+nz)%nz)*nx*ny;
00958                         z7 = ((inzold+3+nz)%nz)*nx*ny;
00959                 } else {
00960                         x1 = inxold-3;
00961                         x2 = inxold-2;
00962                         x3 = inxold-1;
00963                         x4 = inxold;
00964                         x5 = inxold+1;
00965                         x6 = inxold+2;
00966                         x7 = inxold+3;
00967 
00968                         y1 = (inyold-3)*nx;
00969                         y2 = (inyold-2)*nx;
00970                         y3 = (inyold-1)*nx;
00971                         y4 = inyold*nx;
00972                         y5 = (inyold+1)*nx;
00973                         y6 = (inyold+2)*nx;
00974                         y7 = (inyold+3)*nx;
00975 
00976                         z1 = (inzold-3)*nx*ny;
00977                         z2 = (inzold-2)*nx*ny;
00978                         z3 = (inzold-1)*nx*ny;
00979                         z4 = inzold*nx*ny;
00980                         z5 = (inzold+1)*nx*ny;
00981                         z6 = (inzold+2)*nx*ny;
00982                         z7 = (inzold+3)*nx*ny;
00983                 }
00984 
00985                 pixel  = ( ( data[x1+y1+z1]*tablex1 + data[x2+y1+z1]*tablex2 + data[x3+y1+z1]*tablex3 +
00986                              data[x4+y1+z1]*tablex4 + data[x5+y1+z1]*tablex5 + data[x6+y1+z1]*tablex6 +
00987                              data[x7+y1+z1]*tablex7 ) * tabley1 +
00988                            ( data[x1+y2+z1]*tablex1 + data[x2+y2+z1]*tablex2 + data[x3+y2+z1]*tablex3 +
00989                              data[x4+y2+z1]*tablex4 + data[x5+y2+z1]*tablex5 + data[x6+y2+z1]*tablex6 +
00990                              data[x7+y2+z1]*tablex7 ) * tabley2 +
00991                            ( data[x1+y3+z1]*tablex1 + data[x2+y3+z1]*tablex2 + data[x3+y3+z1]*tablex3 +
00992                              data[x4+y3+z1]*tablex4 + data[x5+y3+z1]*tablex5 + data[x6+y3+z1]*tablex6 +
00993                              data[x7+y3+z1]*tablex7 ) * tabley3 +
00994                            ( data[x1+y4+z1]*tablex1 + data[x2+y4+z1]*tablex2 + data[x3+y4+z1]*tablex3 +
00995                              data[x4+y4+z1]*tablex4 + data[x5+y4+z1]*tablex5 + data[x6+y4+z1]*tablex6 +
00996                              data[x7+y4+z1]*tablex7 ) * tabley4 +
00997                            ( data[x1+y5+z1]*tablex1 + data[x2+y5+z1]*tablex2 + data[x3+y5+z1]*tablex3 +
00998                              data[x4+y5+z1]*tablex4 + data[x5+y5+z1]*tablex5 + data[x6+y5+z1]*tablex6 +
00999                              data[x7+y5+z1]*tablex7 ) * tabley5 +
01000                            ( data[x1+y6+z1]*tablex1 + data[x2+y6+z1]*tablex2 + data[x3+y6+z1]*tablex3 +
01001                              data[x4+y6+z1]*tablex4 + data[x5+y6+z1]*tablex5 + data[x6+y6+z1]*tablex6 +
01002                              data[x7+y6+z1]*tablex7 ) * tabley6 +
01003                            ( data[x1+y7+z1]*tablex1 + data[x2+y7+z1]*tablex2 + data[x3+y7+z1]*tablex3 +
01004                              data[x4+y7+z1]*tablex4 + data[x5+y7+z1]*tablex5 + data[x6+y7+z1]*tablex6 +
01005                              data[x7+y7+z1]*tablex7 ) * tabley7 ) *tablez1 +
01006                          ( ( data[x1+y1+z2]*tablex1 + data[x2+y1+z2]*tablex2 + data[x3+y1+z2]*tablex3 +
01007                              data[x4+y1+z2]*tablex4 + data[x5+y1+z2]*tablex5 + data[x6+y1+z2]*tablex6 +
01008                              data[x7+y1+z2]*tablex7 ) * tabley1 +
01009                            ( data[x1+y2+z2]*tablex1 + data[x2+y2+z2]*tablex2 + data[x3+y2+z2]*tablex3 +
01010                              data[x4+y2+z2]*tablex4 + data[x5+y2+z2]*tablex5 + data[x6+y2+z2]*tablex6 +
01011                              data[x7+y2+z2]*tablex7 ) * tabley2 +
01012                            ( data[x1+y3+z2]*tablex1 + data[x2+y3+z2]*tablex2 + data[x3+y3+z2]*tablex3 +
01013                              data[x4+y3+z2]*tablex4 + data[x5+y3+z2]*tablex5 + data[x6+y3+z2]*tablex6 +
01014                              data[x7+y3+z2]*tablex7 ) * tabley3 +
01015                            ( data[x1+y4+z2]*tablex1 + data[x2+y4+z2]*tablex2 + data[x3+y4+z2]*tablex3 +
01016                              data[x4+y4+z2]*tablex4 + data[x5+y4+z2]*tablex5 + data[x6+y4+z2]*tablex6 +
01017                              data[x7+y4+z2]*tablex7 ) * tabley4 +
01018                            ( data[x1+y5+z2]*tablex1 + data[x2+y5+z2]*tablex2 + data[x3+y5+z2]*tablex3 +
01019                              data[x4+y5+z2]*tablex4 + data[x5+y5+z2]*tablex5 + data[x6+y5+z2]*tablex6 +
01020                              data[x7+y5+z2]*tablex7 ) * tabley5 +
01021                            ( data[x1+y6+z2]*tablex1 + data[x2+y6+z2]*tablex2 + data[x3+y6+z2]*tablex3 +
01022                              data[x4+y6+z2]*tablex4 + data[x5+y6+z2]*tablex5 + data[x6+y6+z2]*tablex6 +
01023                              data[x7+y6+z2]*tablex7 ) * tabley6 +
01024                            ( data[x1+y7+z2]*tablex1 + data[x2+y7+z2]*tablex2 + data[x3+y7+z2]*tablex3 +
01025                              data[x4+y7+z2]*tablex4 + data[x5+y7+z2]*tablex5 + data[x6+y7+z2]*tablex6 +
01026                              data[x7+y7+z2]*tablex7 ) * tabley7 ) *tablez2 +
01027                          ( ( data[x1+y1+z3]*tablex1 + data[x2+y1+z3]*tablex2 + data[x3+y1+z3]*tablex3 +
01028                              data[x4+y1+z3]*tablex4 + data[x5+y1+z3]*tablex5 + data[x6+y1+z3]*tablex6 +
01029                              data[x7+y1+z3]*tablex7 ) * tabley1 +
01030                            ( data[x1+y2+z3]*tablex1 + data[x2+y2+z3]*tablex2 + data[x3+y2+z3]*tablex3 +
01031                              data[x4+y2+z3]*tablex4 + data[x5+y2+z3]*tablex5 + data[x6+y2+z3]*tablex6 +
01032                              data[x7+y2+z3]*tablex7 ) * tabley2 +
01033                            ( data[x1+y3+z3]*tablex1 + data[x2+y3+z3]*tablex2 + data[x3+y3+z3]*tablex3 +
01034                              data[x4+y3+z3]*tablex4 + data[x5+y3+z3]*tablex5 + data[x6+y3+z3]*tablex6 +
01035                              data[x7+y3+z3]*tablex7 ) * tabley3 +
01036                            ( data[x1+y4+z3]*tablex1 + data[x2+y4+z3]*tablex2 + data[x3+y4+z3]*tablex3 +
01037                              data[x4+y4+z3]*tablex4 + data[x5+y4+z3]*tablex5 + data[x6+y4+z3]*tablex6 +
01038                              data[x7+y4+z3]*tablex7 ) * tabley4 +
01039                            ( data[x1+y5+z3]*tablex1 + data[x2+y5+z3]*tablex2 + data[x3+y5+z3]*tablex3 +
01040                              data[x4+y5+z3]*tablex4 + data[x5+y5+z3]*tablex5 + data[x6+y5+z3]*tablex6 +
01041                              data[x7+y5+z3]*tablex7 ) * tabley5 +
01042                            ( data[x1+y6+z3]*tablex1 + data[x2+y6+z3]*tablex2 + data[x3+y6+z3]*tablex3 +
01043                              data[x4+y6+z3]*tablex4 + data[x5+y6+z3]*tablex5 + data[x6+y6+z3]*tablex6 +
01044                              data[x7+y6+z3]*tablex7 ) * tabley6 +
01045                            ( data[x1+y7+z3]*tablex1 + data[x2+y7+z3]*tablex2 + data[x3+y7+z3]*tablex3 +
01046                              data[x4+y7+z3]*tablex4 + data[x5+y7+z3]*tablex5 + data[x6+y7+z3]*tablex6 +
01047                              data[x7+y7+z3]*tablex7 ) * tabley7 ) *tablez3 +
01048                          ( ( data[x1+y1+z4]*tablex1 + data[x2+y1+z4]*tablex2 + data[x3+y1+z4]*tablex3 +
01049                              data[x4+y1+z4]*tablex4 + data[x5+y1+z4]*tablex5 + data[x6+y1+z4]*tablex6 +
01050                              data[x7+y1+z4]*tablex7 ) * tabley1 +
01051                            ( data[x1+y2+z4]*tablex1 + data[x2+y2+z4]*tablex2 + data[x3+y2+z4]*tablex3 +
01052                              data[x4+y2+z4]*tablex4 + data[x5+y2+z4]*tablex5 + data[x6+y2+z4]*tablex6 +
01053                              data[x7+y2+z4]*tablex7 ) * tabley2 +
01054                            ( data[x1+y3+z4]*tablex1 + data[x2+y3+z4]*tablex2 + data[x3+y3+z4]*tablex3 +
01055                              data[x4+y3+z4]*tablex4 + data[x5+y3+z4]*tablex5 + data[x6+y3+z4]*tablex6 +
01056                              data[x7+y3+z4]*tablex7 ) * tabley3 +
01057                            ( data[x1+y4+z4]*tablex1 + data[x2+y4+z4]*tablex2 + data[x3+y4+z4]*tablex3 +
01058                              data[x4+y4+z4]*tablex4 + data[x5+y4+z4]*tablex5 + data[x6+y4+z4]*tablex6 +
01059                              data[x7+y4+z4]*tablex7 ) * tabley4 +
01060                            ( data[x1+y5+z4]*tablex1 + data[x2+y5+z4]*tablex2 + data[x3+y5+z4]*tablex3 +
01061                              data[x4+y5+z4]*tablex4 + data[x5+y5+z4]*tablex5 + data[x6+y5+z4]*tablex6 +
01062                              data[x7+y5+z4]*tablex7 ) * tabley5 +
01063                            ( data[x1+y6+z4]*tablex1 + data[x2+y6+z4]*tablex2 + data[x3+y6+z4]*tablex3 +
01064                              data[x4+y6+z4]*tablex4 + data[x5+y6+z4]*tablex5 + data[x6+y6+z4]*tablex6 +
01065                              data[x7+y6+z4]*tablex7 ) * tabley6 +
01066                            ( data[x1+y7+z4]*tablex1 + data[x2+y7+z4]*tablex2 + data[x3+y7+z4]*tablex3 +
01067                              data[x4+y7+z4]*tablex4 + data[x5+y7+z4]*tablex5 + data[x6+y7+z4]*tablex6 +
01068                              data[x7+y7+z4]*tablex7 ) * tabley7 ) *tablez4 +
01069                          ( ( data[x1+y1+z5]*tablex1 + data[x2+y1+z5]*tablex2 + data[x3+y1+z5]*tablex3 +
01070                              data[x4+y1+z5]*tablex4 + data[x5+y1+z5]*tablex5 + data[x6+y1+z5]*tablex6 +
01071                              data[x7+y1+z5]*tablex7 ) * tabley1 +
01072                            ( data[x1+y2+z5]*tablex1 + data[x2+y2+z5]*tablex2 + data[x3+y2+z5]*tablex3 +
01073                              data[x4+y2+z5]*tablex4 + data[x5+y2+z5]*tablex5 + data[x6+y2+z5]*tablex6 +
01074                              data[x7+y2+z5]*tablex7 ) * tabley2 +
01075                            ( data[x1+y3+z5]*tablex1 + data[x2+y3+z5]*tablex2 + data[x3+y3+z5]*tablex3 +
01076                              data[x4+y3+z5]*tablex4 + data[x5+y3+z5]*tablex5 + data[x6+y3+z5]*tablex6 +
01077                              data[x7+y3+z5]*tablex7 ) * tabley3 +
01078                            ( data[x1+y4+z5]*tablex1 + data[x2+y4+z5]*tablex2 + data[x3+y4+z5]*tablex3 +
01079                              data[x4+y4+z5]*tablex4 + data[x5+y4+z5]*tablex5 + data[x6+y4+z5]*tablex6 +
01080                              data[x7+y4+z5]*tablex7 ) * tabley4 +
01081                            ( data[x1+y5+z5]*tablex1 + data[x2+y5+z5]*tablex2 + data[x3+y5+z5]*tablex3 +
01082                              data[x4+y5+z5]*tablex4 + data[x5+y5+z5]*tablex5 + data[x6+y5+z5]*tablex6 +
01083                              data[x7+y5+z5]*tablex7 ) * tabley5 +
01084                            ( data[x1+y6+z5]*tablex1 + data[x2+y6+z5]*tablex2 + data[x3+y6+z5]*tablex3 +
01085                              data[x4+y6+z5]*tablex4 + data[x5+y6+z5]*tablex5 + data[x6+y6+z5]*tablex6 +
01086                              data[x7+y6+z5]*tablex7 ) * tabley6 +
01087                            ( data[x1+y7+z5]*tablex1 + data[x2+y7+z5]*tablex2 + data[x3+y7+z5]*tablex3 +
01088                              data[x4+y7+z5]*tablex4 + data[x5+y7+z5]*tablex5 + data[x6+y7+z5]*tablex6 +
01089                              data[x7+y7+z5]*tablex7 ) * tabley7 ) *tablez5 +
01090                          ( ( data[x1+y1+z6]*tablex1 + data[x2+y1+z6]*tablex2 + data[x3+y1+z6]*tablex3 +
01091                              data[x4+y1+z6]*tablex4 + data[x5+y1+z6]*tablex5 + data[x6+y1+z6]*tablex6 +
01092                              data[x7+y1+z6]*tablex7 ) * tabley1 +
01093                            ( data[x1+y2+z6]*tablex1 + data[x2+y2+z6]*tablex2 + data[x3+y2+z6]*tablex3 +
01094                              data[x4+y2+z6]*tablex4 + data[x5+y2+z6]*tablex5 + data[x6+y2+z6]*tablex6 +
01095                              data[x7+y2+z6]*tablex7 ) * tabley2 +
01096                            ( data[x1+y3+z6]*tablex1 + data[x2+y3+z6]*tablex2 + data[x3+y3+z6]*tablex3 +
01097                              data[x4+y3+z6]*tablex4 + data[x5+y3+z6]*tablex5 + data[x6+y3+z6]*tablex6 +
01098                              data[x7+y3+z6]*tablex7 ) * tabley3 +
01099                            ( data[x1+y4+z6]*tablex1 + data[x2+y4+z6]*tablex2 + data[x3+y4+z6]*tablex3 +
01100                              data[x4+y4+z6]*tablex4 + data[x5+y4+z6]*tablex5 + data[x6+y4+z6]*tablex6 +
01101                              data[x7+y4+z6]*tablex7 ) * tabley4 +
01102                            ( data[x1+y5+z6]*tablex1 + data[x2+y5+z6]*tablex2 + data[x3+y5+z6]*tablex3 +
01103                              data[x4+y5+z6]*tablex4 + data[x5+y5+z6]*tablex5 + data[x6+y5+z6]*tablex6 +
01104                              data[x7+y5+z6]*tablex7 ) * tabley5 +
01105                            ( data[x1+y6+z6]*tablex1 + data[x2+y6+z6]*tablex2 + data[x3+y6+z6]*tablex3 +
01106                              data[x4+y6+z6]*tablex4 + data[x5+y6+z6]*tablex5 + data[x6+y6+z6]*tablex6 +
01107                              data[x7+y6+z6]*tablex7 ) * tabley6 +
01108                            ( data[x1+y7+z6]*tablex1 + data[x2+y7+z6]*tablex2 + data[x3+y7+z6]*tablex3 +
01109                              data[x4+y7+z6]*tablex4 + data[x5+y7+z6]*tablex5 + data[x6+y7+z6]*tablex6 +
01110                              data[x7+y7+z6]*tablex7 ) * tabley7 ) *tablez6 +
01111                          ( ( data[x1+y1+z7]*tablex1 + data[x2+y1+z7]*tablex2 + data[x3+y1+z7]*tablex3 +
01112                              data[x4+y1+z7]*tablex4 + data[x5+y1+z7]*tablex5 + data[x6+y1+z7]*tablex6 +
01113                              data[x7+y1+z7]*tablex7 ) * tabley1 +
01114                            ( data[x1+y2+z7]*tablex1 + data[x2+y2+z7]*tablex2 + data[x3+y2+z7]*tablex3 +
01115                              data[x4+y2+z7]*tablex4 + data[x5+y2+z7]*tablex5 + data[x6+y2+z7]*tablex6 +
01116                              data[x7+y2+z7]*tablex7 ) * tabley2 +
01117                            ( data[x1+y3+z7]*tablex1 + data[x2+y3+z7]*tablex2 + data[x3+y3+z7]*tablex3 +
01118                              data[x4+y3+z7]*tablex4 + data[x5+y3+z7]*tablex5 + data[x6+y3+z7]*tablex6 +
01119                              data[x7+y3+z7]*tablex7 ) * tabley3 +
01120                            ( data[x1+y4+z7]*tablex1 + data[x2+y4+z7]*tablex2 + data[x3+y4+z7]*tablex3 +
01121                              data[x4+y4+z7]*tablex4 + data[x5+y4+z7]*tablex5 + data[x6+y4+z7]*tablex6 +
01122                              data[x7+y4+z7]*tablex7 ) * tabley4 +
01123                            ( data[x1+y5+z7]*tablex1 + data[x2+y5+z7]*tablex2 + data[x3+y5+z7]*tablex3 +
01124                              data[x4+y5+z7]*tablex4 + data[x5+y5+z7]*tablex5 + data[x6+y5+z7]*tablex6 +
01125                              data[x7+y5+z7]*tablex7 ) * tabley5 +
01126                            ( data[x1+y6+z7]*tablex1 + data[x2+y6+z7]*tablex2 + data[x3+y6+z7]*tablex3 +
01127                              data[x4+y6+z7]*tablex4 + data[x5+y6+z7]*tablex5 + data[x6+y6+z7]*tablex6 +
01128                              data[x7+y6+z7]*tablex7 ) * tabley6 +
01129                            ( data[x1+y7+z7]*tablex1 + data[x2+y7+z7]*tablex2 + data[x3+y7+z7]*tablex3 +
01130                              data[x4+y7+z7]*tablex4 + data[x5+y7+z7]*tablex5 + data[x6+y7+z7]*tablex6 +
01131                              data[x7+y7+z7]*tablex7 ) * tabley7 ) *tablez7;
01132 
01133                 w = (tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7) *
01134                     (tabley1+tabley2+tabley3+tabley4+tabley5+tabley6+tabley7) *
01135                     (tablez1+tablez2+tablez3+tablez4+tablez5+tablez6+tablez7);
01136         }
01137         return pixel/w;
01138 }

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 1140 of file util_sparx.cpp.

References EMAN::Util::KaiserBessel::get_window_size(), EMAN::Util::KaiserBessel::i0win_tab(), restrict1(), and round().

Referenced by EMAN::EMData::rot_scale_conv_new_background(), and EMAN::EMData::rot_scale_conv_new_background_3D().

01140                                                                                                                                                             {
01141         int K = kb.get_window_size();
01142         int kbmin = -K/2;
01143         int kbmax = -kbmin;
01144         int kbc = kbmax+1;
01145 
01146         float pixel =0.0f;
01147         float w=0.0f;
01148 
01149     float argdelx = delx; // adding this for 2D case where the wrap around is not done circulantly using restrict1.
01150         delx = restrict1(delx, nx);
01151         int inxold = int(round(delx));
01152         if ( ny < 2 ) {  //1D
01153                 float tablex1 = kb.i0win_tab(delx-inxold+3);
01154                 float tablex2 = kb.i0win_tab(delx-inxold+2);
01155                 float tablex3 = kb.i0win_tab(delx-inxold+1);
01156                 float tablex4 = kb.i0win_tab(delx-inxold);
01157                 float tablex5 = kb.i0win_tab(delx-inxold-1);
01158                 float tablex6 = kb.i0win_tab(delx-inxold-2);
01159                 float tablex7 = kb.i0win_tab(delx-inxold-3);
01160 
01161                 int x1, x2, x3, x4, x5, x6, x7;
01162 
01163                 if ( inxold <= kbc || inxold >=nx-kbc-2 )  {
01164                         x1 = (inxold-3+nx)%nx;
01165                         x2 = (inxold-2+nx)%nx;
01166                         x3 = (inxold-1+nx)%nx;
01167                         x4 = (inxold  +nx)%nx;
01168                         x5 = (inxold+1+nx)%nx;
01169                         x6 = (inxold+2+nx)%nx;
01170                         x7 = (inxold+3+nx)%nx;
01171                 } else {
01172                         x1 = inxold-3;
01173                         x2 = inxold-2;
01174                         x3 = inxold-1;
01175                         x4 = inxold;
01176                         x5 = inxold+1;
01177                         x6 = inxold+2;
01178                         x7 = inxold+3;
01179                 }
01180 
01181                 pixel = data[x1]*tablex1 + data[x2]*tablex2 + data[x3]*tablex3 +
01182                         data[x4]*tablex4 + data[x5]*tablex5 + data[x6]*tablex6 +
01183                         data[x7]*tablex7 ;
01184 
01185                 w = tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7;
01186         } else if ( nz < 2 ) {  // 2D
01187 
01188                 delx = argdelx;
01189                 // 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
01190                 if ((delx < 0.0f) || (delx >= (float) (nx)) || (dely < 0.0f) || (dely >= (float) (ny)) ){
01191                 delx = (float)xnew*2.0f;
01192                 dely = (float)ynew*2.0f;
01193                 }
01194 
01195                 int inxold = int(round(delx));
01196                 int inyold = int(round(dely));
01197 
01198                 float tablex1 = kb.i0win_tab(delx-inxold+3);
01199                 float tablex2 = kb.i0win_tab(delx-inxold+2);
01200                 float tablex3 = kb.i0win_tab(delx-inxold+1);
01201                 float tablex4 = kb.i0win_tab(delx-inxold);
01202                 float tablex5 = kb.i0win_tab(delx-inxold-1);
01203                 float tablex6 = kb.i0win_tab(delx-inxold-2);
01204                 float tablex7 = kb.i0win_tab(delx-inxold-3);
01205 
01206                 float tabley1 = kb.i0win_tab(dely-inyold+3);
01207                 float tabley2 = kb.i0win_tab(dely-inyold+2);
01208                 float tabley3 = kb.i0win_tab(dely-inyold+1);
01209                 float tabley4 = kb.i0win_tab(dely-inyold);
01210                 float tabley5 = kb.i0win_tab(dely-inyold-1);
01211                 float tabley6 = kb.i0win_tab(dely-inyold-2);
01212                 float tabley7 = kb.i0win_tab(dely-inyold-3);
01213 
01214                 int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7;
01215 
01216                 if ( inxold <= kbc || inxold >=nx-kbc-2 || inyold <= kbc || inyold >=ny-kbc-2 )  {
01217                         x1 = (inxold-3+nx)%nx;
01218                         x2 = (inxold-2+nx)%nx;
01219                         x3 = (inxold-1+nx)%nx;
01220                         x4 = (inxold  +nx)%nx;
01221                         x5 = (inxold+1+nx)%nx;
01222                         x6 = (inxold+2+nx)%nx;
01223                         x7 = (inxold+3+nx)%nx;
01224 
01225                         y1 = ((inyold-3+ny)%ny)*nx;
01226                         y2 = ((inyold-2+ny)%ny)*nx;
01227                         y3 = ((inyold-1+ny)%ny)*nx;
01228                         y4 = ((inyold  +ny)%ny)*nx;
01229                         y5 = ((inyold+1+ny)%ny)*nx;
01230                         y6 = ((inyold+2+ny)%ny)*nx;
01231                         y7 = ((inyold+3+ny)%ny)*nx;
01232                 } else {
01233                         x1 = inxold-3;
01234                         x2 = inxold-2;
01235                         x3 = inxold-1;
01236                         x4 = inxold;
01237                         x5 = inxold+1;
01238                         x6 = inxold+2;
01239                         x7 = inxold+3;
01240 
01241                         y1 = (inyold-3)*nx;
01242                         y2 = (inyold-2)*nx;
01243                         y3 = (inyold-1)*nx;
01244                         y4 = inyold*nx;
01245                         y5 = (inyold+1)*nx;
01246                         y6 = (inyold+2)*nx;
01247                         y7 = (inyold+3)*nx;
01248                 }
01249 
01250                 pixel    = ( data[x1+y1]*tablex1 + data[x2+y1]*tablex2 + data[x3+y1]*tablex3 +
01251                              data[x4+y1]*tablex4 + data[x5+y1]*tablex5 + data[x6+y1]*tablex6 +
01252                              data[x7+y1]*tablex7 ) * tabley1 +
01253                            ( data[x1+y2]*tablex1 + data[x2+y2]*tablex2 + data[x3+y2]*tablex3 +
01254                              data[x4+y2]*tablex4 + data[x5+y2]*tablex5 + data[x6+y2]*tablex6 +
01255                              data[x7+y2]*tablex7 ) * tabley2 +
01256                            ( data[x1+y3]*tablex1 + data[x2+y3]*tablex2 + data[x3+y3]*tablex3 +
01257                              data[x4+y3]*tablex4 + data[x5+y3]*tablex5 + data[x6+y3]*tablex6 +
01258                              data[x7+y3]*tablex7 ) * tabley3 +
01259                            ( data[x1+y4]*tablex1 + data[x2+y4]*tablex2 + data[x3+y4]*tablex3 +
01260                              data[x4+y4]*tablex4 + data[x5+y4]*tablex5 + data[x6+y4]*tablex6 +
01261                              data[x7+y4]*tablex7 ) * tabley4 +
01262                            ( data[x1+y5]*tablex1 + data[x2+y5]*tablex2 + data[x3+y5]*tablex3 +
01263                              data[x4+y5]*tablex4 + data[x5+y5]*tablex5 + data[x6+y5]*tablex6 +
01264                              data[x7+y5]*tablex7 ) * tabley5 +
01265                            ( data[x1+y6]*tablex1 + data[x2+y6]*tablex2 + data[x3+y6]*tablex3 +
01266                              data[x4+y6]*tablex4 + data[x5+y6]*tablex5 + data[x6+y6]*tablex6 +
01267                              data[x7+y6]*tablex7 ) * tabley6 +
01268                            ( data[x1+y7]*tablex1 + data[x2+y7]*tablex2 + data[x3+y7]*tablex3 +
01269                              data[x4+y7]*tablex4 + data[x5+y7]*tablex5 + data[x6+y7]*tablex6 +
01270                              data[x7+y7]*tablex7 ) * tabley7;
01271 
01272                 w = (tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7) *
01273                     (tabley1+tabley2+tabley3+tabley4+tabley5+tabley6+tabley7);
01274         } else {  //  3D
01275                 dely = restrict1(dely, ny);
01276                 int inyold = int(Util::round(dely));
01277                 delz = restrict1(delz, nz);
01278                 int inzold = int(Util::round(delz));
01279 
01280                 float tablex1 = kb.i0win_tab(delx-inxold+3);
01281                 float tablex2 = kb.i0win_tab(delx-inxold+2);
01282                 float tablex3 = kb.i0win_tab(delx-inxold+1);
01283                 float tablex4 = kb.i0win_tab(delx-inxold);
01284                 float tablex5 = kb.i0win_tab(delx-inxold-1);
01285                 float tablex6 = kb.i0win_tab(delx-inxold-2);
01286                 float tablex7 = kb.i0win_tab(delx-inxold-3);
01287 
01288                 float tabley1 = kb.i0win_tab(dely-inyold+3);
01289                 float tabley2 = kb.i0win_tab(dely-inyold+2);
01290                 float tabley3 = kb.i0win_tab(dely-inyold+1);
01291                 float tabley4 = kb.i0win_tab(dely-inyold);
01292                 float tabley5 = kb.i0win_tab(dely-inyold-1);
01293                 float tabley6 = kb.i0win_tab(dely-inyold-2);
01294                 float tabley7 = kb.i0win_tab(dely-inyold-3);
01295 
01296                 float tablez1 = kb.i0win_tab(delz-inzold+3);
01297                 float tablez2 = kb.i0win_tab(delz-inzold+2);
01298                 float tablez3 = kb.i0win_tab(delz-inzold+1);
01299                 float tablez4 = kb.i0win_tab(delz-inzold);
01300                 float tablez5 = kb.i0win_tab(delz-inzold-1);
01301                 float tablez6 = kb.i0win_tab(delz-inzold-2);
01302                 float tablez7 = kb.i0win_tab(delz-inzold-3);
01303 
01304                 int x1, x2, x3, x4, x5, x6, x7, y1, y2, y3, y4, y5, y6, y7, z1, z2, z3, z4, z5, z6, z7;
01305 
01306                 if ( inxold <= kbc || inxold >=nx-kbc-2 || inyold <= kbc || inyold >=ny-kbc-2 || inzold <= kbc || inzold >= nz-kbc-2 )  {
01307                         x1 = (inxold-3+nx)%nx;
01308                         x2 = (inxold-2+nx)%nx;
01309                         x3 = (inxold-1+nx)%nx;
01310                         x4 = (inxold  +nx)%nx;
01311                         x5 = (inxold+1+nx)%nx;
01312                         x6 = (inxold+2+nx)%nx;
01313                         x7 = (inxold+3+nx)%nx;
01314 
01315                         y1 = ((inyold-3+ny)%ny)*nx;
01316                         y2 = ((inyold-2+ny)%ny)*nx;
01317                         y3 = ((inyold-1+ny)%ny)*nx;
01318                         y4 = ((inyold  +ny)%ny)*nx;
01319                         y5 = ((inyold+1+ny)%ny)*nx;
01320                         y6 = ((inyold+2+ny)%ny)*nx;
01321                         y7 = ((inyold+3+ny)%ny)*nx;
01322 
01323                         z1 = ((inzold-3+nz)%nz)*nx*ny;
01324                         z2 = ((inzold-2+nz)%nz)*nx*ny;
01325                         z3 = ((inzold-1+nz)%nz)*nx*ny;
01326                         z4 = ((inzold  +nz)%nz)*nx*ny;
01327                         z5 = ((inzold+1+nz)%nz)*nx*ny;
01328                         z6 = ((inzold+2+nz)%nz)*nx*ny;
01329                         z7 = ((inzold+3+nz)%nz)*nx*ny;
01330                 } else {
01331                         x1 = inxold-3;
01332                         x2 = inxold-2;
01333                         x3 = inxold-1;
01334                         x4 = inxold;
01335                         x5 = inxold+1;
01336                         x6 = inxold+2;
01337                         x7 = inxold+3;
01338 
01339                         y1 = (inyold-3)*nx;
01340                         y2 = (inyold-2)*nx;
01341                         y3 = (inyold-1)*nx;
01342                         y4 = inyold*nx;
01343                         y5 = (inyold+1)*nx;
01344                         y6 = (inyold+2)*nx;
01345                         y7 = (inyold+3)*nx;
01346 
01347                         z1 = (inzold-3)*nx*ny;
01348                         z2 = (inzold-2)*nx*ny;
01349                         z3 = (inzold-1)*nx*ny;
01350                         z4 = inzold*nx*ny;
01351                         z5 = (inzold+1)*nx*ny;
01352                         z6 = (inzold+2)*nx*ny;
01353                         z7 = (inzold+3)*nx*ny;
01354                 }
01355 
01356                 pixel  = ( ( data[x1+y1+z1]*tablex1 + data[x2+y1+z1]*tablex2 + data[x3+y1+z1]*tablex3 +
01357                              data[x4+y1+z1]*tablex4 + data[x5+y1+z1]*tablex5 + data[x6+y1+z1]*tablex6 +
01358                              data[x7+y1+z1]*tablex7 ) * tabley1 +
01359                            ( data[x1+y2+z1]*tablex1 + data[x2+y2+z1]*tablex2 + data[x3+y2+z1]*tablex3 +
01360                              data[x4+y2+z1]*tablex4 + data[x5+y2+z1]*tablex5 + data[x6+y2+z1]*tablex6 +
01361                              data[x7+y2+z1]*tablex7 ) * tabley2 +
01362                            ( data[x1+y3+z1]*tablex1 + data[x2+y3+z1]*tablex2 + data[x3+y3+z1]*tablex3 +
01363                              data[x4+y3+z1]*tablex4 + data[x5+y3+z1]*tablex5 + data[x6+y3+z1]*tablex6 +
01364                              data[x7+y3+z1]*tablex7 ) * tabley3 +
01365                            ( data[x1+y4+z1]*tablex1 + data[x2+y4+z1]*tablex2 + data[x3+y4+z1]*tablex3 +
01366                              data[x4+y4+z1]*tablex4 + data[x5+y4+z1]*tablex5 + data[x6+y4+z1]*tablex6 +
01367                              data[x7+y4+z1]*tablex7 ) * tabley4 +
01368                            ( data[x1+y5+z1]*tablex1 + data[x2+y5+z1]*tablex2 + data[x3+y5+z1]*tablex3 +
01369                              data[x4+y5+z1]*tablex4 + data[x5+y5+z1]*tablex5 + data[x6+y5+z1]*tablex6 +
01370                              data[x7+y5+z1]*tablex7 ) * tabley5 +
01371                            ( data[x1+y6+z1]*tablex1 + data[x2+y6+z1]*tablex2 + data[x3+y6+z1]*tablex3 +
01372                              data[x4+y6+z1]*tablex4 + data[x5+y6+z1]*tablex5 + data[x6+y6+z1]*tablex6 +
01373                              data[x7+y6+z1]*tablex7 ) * tabley6 +
01374                            ( data[x1+y7+z1]*tablex1 + data[x2+y7+z1]*tablex2 + data[x3+y7+z1]*tablex3 +
01375                              data[x4+y7+z1]*tablex4 + data[x5+y7+z1]*tablex5 + data[x6+y7+z1]*tablex6 +
01376                              data[x7+y7+z1]*tablex7 ) * tabley7 ) *tablez1 +
01377                          ( ( data[x1+y1+z2]*tablex1 + data[x2+y1+z2]*tablex2 + data[x3+y1+z2]*tablex3 +
01378                              data[x4+y1+z2]*tablex4 + data[x5+y1+z2]*tablex5 + data[x6+y1+z2]*tablex6 +
01379                              data[x7+y1+z2]*tablex7 ) * tabley1 +
01380                            ( data[x1+y2+z2]*tablex1 + data[x2+y2+z2]*tablex2 + data[x3+y2+z2]*tablex3 +
01381                              data[x4+y2+z2]*tablex4 + data[x5+y2+z2]*tablex5 + data[x6+y2+z2]*tablex6 +
01382                              data[x7+y2+z2]*tablex7 ) * tabley2 +
01383                            ( data[x1+y3+z2]*tablex1 + data[x2+y3+z2]*tablex2 + data[x3+y3+z2]*tablex3 +
01384                              data[x4+y3+z2]*tablex4 + data[x5+y3+z2]*tablex5 + data[x6+y3+z2]*tablex6 +
01385                              data[x7+y3+z2]*tablex7 ) * tabley3 +
01386                            ( data[x1+y4+z2]*tablex1 + data[x2+y4+z2]*tablex2 + data[x3+y4+z2]*tablex3 +
01387                              data[x4+y4+z2]*tablex4 + data[x5+y4+z2]*tablex5 + data[x6+y4+z2]*tablex6 +
01388                              data[x7+y4+z2]*tablex7 ) * tabley4 +
01389                            ( data[x1+y5+z2]*tablex1 + data[x2+y5+z2]*tablex2 + data[x3+y5+z2]*tablex3 +
01390                              data[x4+y5+z2]*tablex4 + data[x5+y5+z2]*tablex5 + data[x6+y5+z2]*tablex6 +
01391                              data[x7+y5+z2]*tablex7 ) * tabley5 +
01392                            ( data[x1+y6+z2]*tablex1 + data[x2+y6+z2]*tablex2 + data[x3+y6+z2]*tablex3 +
01393                              data[x4+y6+z2]*tablex4 + data[x5+y6+z2]*tablex5 + data[x6+y6+z2]*tablex6 +
01394                              data[x7+y6+z2]*tablex7 ) * tabley6 +
01395                            ( data[x1+y7+z2]*tablex1 + data[x2+y7+z2]*tablex2 + data[x3+y7+z2]*tablex3 +
01396                              data[x4+y7+z2]*tablex4 + data[x5+y7+z2]*tablex5 + data[x6+y7+z2]*tablex6 +
01397                              data[x7+y7+z2]*tablex7 ) * tabley7 ) *tablez2 +
01398                          ( ( data[x1+y1+z3]*tablex1 + data[x2+y1+z3]*tablex2 + data[x3+y1+z3]*tablex3 +
01399                              data[x4+y1+z3]*tablex4 + data[x5+y1+z3]*tablex5 + data[x6+y1+z3]*tablex6 +
01400                              data[x7+y1+z3]*tablex7 ) * tabley1 +
01401                            ( data[x1+y2+z3]*tablex1 + data[x2+y2+z3]*tablex2 + data[x3+y2+z3]*tablex3 +
01402                              data[x4+y2+z3]*tablex4 + data[x5+y2+z3]*tablex5 + data[x6+y2+z3]*tablex6 +
01403                              data[x7+y2+z3]*tablex7 ) * tabley2 +
01404                            ( data[x1+y3+z3]*tablex1 + data[x2+y3+z3]*tablex2 + data[x3+y3+z3]*tablex3 +
01405                              data[x4+y3+z3]*tablex4 + data[x5+y3+z3]*tablex5 + data[x6+y3+z3]*tablex6 +
01406                              data[x7+y3+z3]*tablex7 ) * tabley3 +
01407                            ( data[x1+y4+z3]*tablex1 + data[x2+y4+z3]*tablex2 + data[x3+y4+z3]*tablex3 +
01408                              data[x4+y4+z3]*tablex4 + data[x5+y4+z3]*tablex5 + data[x6+y4+z3]*tablex6 +
01409                              data[x7+y4+z3]*tablex7 ) * tabley4 +
01410                            ( data[x1+y5+z3]*tablex1 + data[x2+y5+z3]*tablex2 + data[x3+y5+z3]*tablex3 +
01411                              data[x4+y5+z3]*tablex4 + data[x5+y5+z3]*tablex5 + data[x6+y5+z3]*tablex6 +
01412                              data[x7+y5+z3]*tablex7 ) * tabley5 +
01413                            ( data[x1+y6+z3]*tablex1 + data[x2+y6+z3]*tablex2 + data[x3+y6+z3]*tablex3 +
01414                              data[x4+y6+z3]*tablex4 + data[x5+y6+z3]*tablex5 + data[x6+y6+z3]*tablex6 +
01415                              data[x7+y6+z3]*tablex7 ) * tabley6 +
01416                            ( data[x1+y7+z3]*tablex1 + data[x2+y7+z3]*tablex2 + data[x3+y7+z3]*tablex3 +
01417                              data[x4+y7+z3]*tablex4 + data[x5+y7+z3]*tablex5 + data[x6+y7+z3]*tablex6 +
01418                              data[x7+y7+z3]*tablex7 ) * tabley7 ) *tablez3 +
01419                          ( ( data[x1+y1+z4]*tablex1 + data[x2+y1+z4]*tablex2 + data[x3+y1+z4]*tablex3 +
01420                              data[x4+y1+z4]*tablex4 + data[x5+y1+z4]*tablex5 + data[x6+y1+z4]*tablex6 +
01421                              data[x7+y1+z4]*tablex7 ) * tabley1 +
01422                            ( data[x1+y2+z4]*tablex1 + data[x2+y2+z4]*tablex2 + data[x3+y2+z4]*tablex3 +
01423                              data[x4+y2+z4]*tablex4 + data[x5+y2+z4]*tablex5 + data[x6+y2+z4]*tablex6 +
01424                              data[x7+y2+z4]*tablex7 ) * tabley2 +
01425                            ( data[x1+y3+z4]*tablex1 + data[x2+y3+z4]*tablex2 + data[x3+y3+z4]*tablex3 +
01426                              data[x4+y3+z4]*tablex4 + data[x5+y3+z4]*tablex5 + data[x6+y3+z4]*tablex6 +
01427                              data[x7+y3+z4]*tablex7 ) * tabley3 +
01428                            ( data[x1+y4+z4]*tablex1 + data[x2+y4+z4]*tablex2 + data[x3+y4+z4]*tablex3 +
01429                              data[x4+y4+z4]*tablex4 + data[x5+y4+z4]*tablex5 + data[x6+y4+z4]*tablex6 +
01430                              data[x7+y4+z4]*tablex7 ) * tabley4 +
01431                            ( data[x1+y5+z4]*tablex1 + data[x2+y5+z4]*tablex2 + data[x3+y5+z4]*tablex3 +
01432                              data[x4+y5+z4]*tablex4 + data[x5+y5+z4]*tablex5 + data[x6+y5+z4]*tablex6 +
01433                              data[x7+y5+z4]*tablex7 ) * tabley5 +
01434                            ( data[x1+y6+z4]*tablex1 + data[x2+y6+z4]*tablex2 + data[x3+y6+z4]*tablex3 +
01435                              data[x4+y6+z4]*tablex4 + data[x5+y6+z4]*tablex5 + data[x6+y6+z4]*tablex6 +
01436                              data[x7+y6+z4]*tablex7 ) * tabley6 +
01437                            ( data[x1+y7+z4]*tablex1 + data[x2+y7+z4]*tablex2 + data[x3+y7+z4]*tablex3 +
01438                              data[x4+y7+z4]*tablex4 + data[x5+y7+z4]*tablex5 + data[x6+y7+z4]*tablex6 +
01439                              data[x7+y7+z4]*tablex7 ) * tabley7 ) *tablez4 +
01440                          ( ( data[x1+y1+z5]*tablex1 + data[x2+y1+z5]*tablex2 + data[x3+y1+z5]*tablex3 +
01441                              data[x4+y1+z5]*tablex4 + data[x5+y1+z5]*tablex5 + data[x6+y1+z5]*tablex6 +
01442                              data[x7+y1+z5]*tablex7 ) * tabley1 +
01443                            ( data[x1+y2+z5]*tablex1 + data[x2+y2+z5]*tablex2 + data[x3+y2+z5]*tablex3 +
01444                              data[x4+y2+z5]*tablex4 + data[x5+y2+z5]*tablex5 + data[x6+y2+z5]*tablex6 +
01445                              data[x7+y2+z5]*tablex7 ) * tabley2 +
01446                            ( data[x1+y3+z5]*tablex1 + data[x2+y3+z5]*tablex2 + data[x3+y3+z5]*tablex3 +
01447                              data[x4+y3+z5]*tablex4 + data[x5+y3+z5]*tablex5 + data[x6+y3+z5]*tablex6 +
01448                              data[x7+y3+z5]*tablex7 ) * tabley3 +
01449                            ( data[x1+y4+z5]*tablex1 + data[x2+y4+z5]*tablex2 + data[x3+y4+z5]*tablex3 +
01450                              data[x4+y4+z5]*tablex4 + data[x5+y4+z5]*tablex5 + data[x6+y4+z5]*tablex6 +
01451                              data[x7+y4+z5]*tablex7 ) * tabley4 +
01452                            ( data[x1+y5+z5]*tablex1 + data[x2+y5+z5]*tablex2 + data[x3+y5+z5]*tablex3 +
01453                              data[x4+y5+z5]*tablex4 + data[x5+y5+z5]*tablex5 + data[x6+y5+z5]*tablex6 +
01454                              data[x7+y5+z5]*tablex7 ) * tabley5 +
01455                            ( data[x1+y6+z5]*tablex1 + data[x2+y6+z5]*tablex2 + data[x3+y6+z5]*tablex3 +
01456                              data[x4+y6+z5]*tablex4 + data[x5+y6+z5]*tablex5 + data[x6+y6+z5]*tablex6 +
01457                              data[x7+y6+z5]*tablex7 ) * tabley6 +
01458                            ( data[x1+y7+z5]*tablex1 + data[x2+y7+z5]*tablex2 + data[x3+y7+z5]*tablex3 +
01459                              data[x4+y7+z5]*tablex4 + data[x5+y7+z5]*tablex5 + data[x6+y7+z5]*tablex6 +
01460                              data[x7+y7+z5]*tablex7 ) * tabley7 ) *tablez5 +
01461                          ( ( data[x1+y1+z6]*tablex1 + data[x2+y1+z6]*tablex2 + data[x3+y1+z6]*tablex3 +
01462                              data[x4+y1+z6]*tablex4 + data[x5+y1+z6]*tablex5 + data[x6+y1+z6]*tablex6 +
01463                              data[x7+y1+z6]*tablex7 ) * tabley1 +
01464                            ( data[x1+y2+z6]*tablex1 + data[x2+y2+z6]*tablex2 + data[x3+y2+z6]*tablex3 +
01465                              data[x4+y2+z6]*tablex4 + data[x5+y2+z6]*tablex5 + data[x6+y2+z6]*tablex6 +
01466                              data[x7+y2+z6]*tablex7 ) * tabley2 +
01467                            ( data[x1+y3+z6]*tablex1 + data[x2+y3+z6]*tablex2 + data[x3+y3+z6]*tablex3 +
01468                              data[x4+y3+z6]*tablex4 + data[x5+y3+z6]*tablex5 + data[x6+y3+z6]*tablex6 +
01469                              data[x7+y3+z6]*tablex7 ) * tabley3 +
01470                            ( data[x1+y4+z6]*tablex1 + data[x2+y4+z6]*tablex2 + data[x3+y4+z6]*tablex3 +
01471                              data[x4+y4+z6]*tablex4 + data[x5+y4+z6]*tablex5 + data[x6+y4+z6]*tablex6 +
01472                              data[x7+y4+z6]*tablex7 ) * tabley4 +
01473                            ( data[x1+y5+z6]*tablex1 + data[x2+y5+z6]*tablex2 + data[x3+y5+z6]*tablex3 +
01474                              data[x4+y5+z6]*tablex4 + data[x5+y5+z6]*tablex5 + data[x6+y5+z6]*tablex6 +
01475                              data[x7+y5+z6]*tablex7 ) * tabley5 +
01476                            ( data[x1+y6+z6]*tablex1 + data[x2+y6+z6]*tablex2 + data[x3+y6+z6]*tablex3 +
01477                              data[x4+y6+z6]*tablex4 + data[x5+y6+z6]*tablex5 + data[x6+y6+z6]*tablex6 +
01478                              data[x7+y6+z6]*tablex7 ) * tabley6 +
01479                            ( data[x1+y7+z6]*tablex1 + data[x2+y7+z6]*tablex2 + data[x3+y7+z6]*tablex3 +
01480                              data[x4+y7+z6]*tablex4 + data[x5+y7+z6]*tablex5 + data[x6+y7+z6]*tablex6 +
01481                              data[x7+y7+z6]*tablex7 ) * tabley7 ) *tablez6 +
01482                          ( ( data[x1+y1+z7]*tablex1 + data[x2+y1+z7]*tablex2 + data[x3+y1+z7]*tablex3 +
01483                              data[x4+y1+z7]*tablex4 + data[x5+y1+z7]*tablex5 + data[x6+y1+z7]*tablex6 +
01484                              data[x7+y1+z7]*tablex7 ) * tabley1 +
01485                            ( data[x1+y2+z7]*tablex1 + data[x2+y2+z7]*tablex2 + data[x3+y2+z7]*tablex3 +
01486                              data[x4+y2+z7]*tablex4 + data[x5+y2+z7]*tablex5 + data[x6+y2+z7]*tablex6 +
01487                              data[x7+y2+z7]*tablex7 ) * tabley2 +
01488                            ( data[x1+y3+z7]*tablex1 + data[x2+y3+z7]*tablex2 + data[x3+y3+z7]*tablex3 +
01489                              data[x4+y3+z7]*tablex4 + data[x5+y3+z7]*tablex5 + data[x6+y3+z7]*tablex6 +
01490                              data[x7+y3+z7]*tablex7 ) * tabley3 +
01491                            ( data[x1+y4+z7]*tablex1 + data[x2+y4+z7]*tablex2 + data[x3+y4+z7]*tablex3 +
01492                              data[x4+y4+z7]*tablex4 + data[x5+y4+z7]*tablex5 + data[x6+y4+z7]*tablex6 +
01493                              data[x7+y4+z7]*tablex7 ) * tabley4 +
01494                            ( data[x1+y5+z7]*tablex1 + data[x2+y5+z7]*tablex2 + data[x3+y5+z7]*tablex3 +
01495                              data[x4+y5+z7]*tablex4 + data[x5+y5+z7]*tablex5 + data[x6+y5+z7]*tablex6 +
01496                              data[x7+y5+z7]*tablex7 ) * tabley5 +
01497                            ( data[x1+y6+z7]*tablex1 + data[x2+y6+z7]*tablex2 + data[x3+y6+z7]*tablex3 +
01498                              data[x4+y6+z7]*tablex4 + data[x5+y6+z7]*tablex5 + data[x6+y6+z7]*tablex6 +
01499                              data[x7+y6+z7]*tablex7 ) * tabley6 +
01500                            ( data[x1+y7+z7]*tablex1 + data[x2+y7+z7]*tablex2 + data[x3+y7+z7]*tablex3 +
01501                              data[x4+y7+z7]*tablex4 + data[x5+y7+z7]*tablex5 + data[x6+y7+z7]*tablex6 +
01502                              data[x7+y7+z7]*tablex7 ) * tabley7 ) *tablez7;
01503 
01504                 w = (tablex1+tablex2+tablex3+tablex4+tablex5+tablex6+tablex7) *
01505                     (tabley1+tabley2+tabley3+tabley4+tabley5+tabley6+tabley7) *
01506                     (tablez1+tablez2+tablez3+tablez4+tablez5+tablez6+tablez7);
01507         }
01508         return pixel/w;
01509 }

unsigned long int Util::get_randnum_seed (  )  [static]

Get the seed for Randnum class.

Returns:
the seed for current random number generator

Definition at line 684 of file util.cpp.

References EMAN::Randnum::get_seed(), and EMAN::Randnum::Instance().

00685 {
00686         Randnum* randnum = Randnum::Instance();
00687         return  randnum->get_seed();
00688 }

EMData * Util::get_slice ( EMData vol,
int  dim,
int  index 
) [static]

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.

Definition at line 20610 of file util_sparx.cpp.

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

20610                                                        {
20611 
20612         int nx = vol->get_xsize();
20613         int ny = vol->get_ysize();
20614         int nz = vol->get_zsize();
20615         float *vol_data = vol->get_data();
20616         int new_nx, new_ny;
20617 
20618         if (nz == 1)
20619                 throw ImageDimensionException("Error: Input must be a 3-D object");
20620         if ((dim < 1) || (dim > 3))
20621                 throw ImageDimensionException("Error: dim must be 1 (x-dimension), 2 (y-dimension) or 3 (z-dimension)");
20622         if (((dim == 1) && (index < 0 || index > nx-1)) ||
20623           ((dim == 1) && (index < 0 || index > nx-1)) ||
20624           ((dim == 1) && (index < 0 || index > nx-1)))
20625                 throw ImageDimensionException("Error: index exceeds the size of the 3-D object");
20626 
20627         if (dim == 1) {
20628                 new_nx = ny;
20629                 new_ny = nz;
20630         } else if (dim == 2) {
20631                 new_nx = nx;
20632                 new_ny = nz;
20633         } else {
20634                 new_nx = nx;
20635                 new_ny = ny;
20636         }
20637 
20638         EMData *slice = new EMData();
20639         slice->set_size(new_nx, new_ny, 1);
20640         float *slice_data = slice->get_data();
20641 
20642         if (dim == 1) {
20643                 for (int x=0; x<new_nx; x++)
20644                         for (int y=0; y<new_ny; y++)
20645                                 slice_data[y*new_nx+x] = vol_data[(y*ny+x)*nx+index];
20646         } else if (dim == 2) {
20647                 for (int x=0; x<new_nx; x++)
20648                         for (int y=0; y<new_ny; y++)
20649                                 slice_data[y*new_nx+x] = vol_data[(y*ny+index)*nx+x];
20650         } else {
20651                 for (int x=0; x<new_nx; x++)
20652                         for (int y=0; y<new_ny; y++)
20653                                 slice_data[y*new_nx+x] = vol_data[((size_t)index*ny+y)*nx+x];
20654         }
20655 
20656         return slice;
20657 }

Dict Util::get_stats ( const vector< float > &  data  )  [static]

Get the mean, standard deviation, skewness and kurtosis of the input data.

Parameters:
data the vector of input data
Exceptions:
EmptyContainerException when the argument vector is empty

Definition at line 876 of file util.cpp.

References EmptyContainerException, mean(), sqrt(), and square_sum().

00877 {
00878         // Note that this is a heavy STL approach using generic algorithms - some memory could be saved
00879         // using plain c style code, as in get_stats_cstyle below
00880 
00881         if (data.size() == 0) EmptyContainerException("Error, attempting to call get stats on an empty container (vector<double>)");
00882 
00883         double sum = accumulate(data.begin(), data.end(), 0.0);
00884 
00885         double mean = sum / static_cast<double> (data.size());
00886 
00887         double std_dev = 0.0, skewness = 0.0, kurtosis = 0.0;
00888 
00889         if (data.size() > 1)
00890         {
00891                 // read mm is "minus_mean"
00892                 vector<double> data_mm(data.size());
00893                 // read ts as "then squared"
00894                 vector<double> data_mm_sq(data.size());
00895 
00896                 // Subtract the mean from the data and store it in data_mm
00897                 transform(data.begin(), data.end(), data_mm.begin(), std::bind2nd(std::minus<double>(), mean));
00898 
00899                 // Get the square of the data minus the mean and store it in data_mm_sq
00900                 transform(data_mm.begin(), data_mm.end(), data_mm.begin(), data_mm_sq.begin(), std::multiplies<double>());
00901 
00902                 // Get the sum of the squares for the calculation of the standard deviation
00903                 double square_sum = accumulate(data_mm_sq.begin(), data_mm_sq.end(), 0.0);
00904 
00905                 //Calculate teh standard deviation
00906                 std_dev = sqrt(square_sum / static_cast<double>(data.size()-1));
00907                 double std_dev_sq = std_dev * std_dev;
00908 
00909                 // The numerator for the skewness fraction, as defined in http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm
00910                 double cubic_sum = inner_product(data_mm.begin(), data_mm.end(),data_mm_sq.begin(), 0.0);
00911 
00912                 // The numerator for the kurtosis fraction, as defined in http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm
00913                 double quartic_sum = inner_product(data_mm_sq.begin(), data_mm_sq.end(),data_mm_sq.begin(), 0.0);
00914 
00915                 // Finalize the calculation of the skewness and kurtosis, as defined in
00916                 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm
00917                 skewness = cubic_sum / ((data.size()-1) * std_dev_sq * std_dev );
00918                 kurtosis = quartic_sum / ((data.size()-1) * std_dev_sq * std_dev_sq );
00919 
00920         }
00921 
00922         Dict parms;
00923         parms["mean"] = mean;
00924         parms["std_dev"] = std_dev;
00925         parms["skewness"] = skewness;
00926         parms["kurtosis"] = kurtosis;
00927 
00928         return parms;
00929 }

Dict Util::get_stats_cstyle ( const vector< float > &  data  )  [static]

Performs the same calculations as in get_stats, but uses a single pass, optimized c approach Should perform better than get_stats.

Definition at line 932 of file util.cpp.

References EmptyContainerException, mean(), sqrt(), square(), and square_sum().

00933 {
00934         if (data.size() == 0) EmptyContainerException("Error, attempting to call get stats on an empty container (vector<double>)");
00935 
00936         double square_sum = 0.0, sum = 0.0, cube_sum = 0.0, quart_sum = 0.0;
00937         for( vector<float>::const_iterator it = data.begin(); it != data.end(); ++it )
00938         {
00939                 double val = *it;
00940                 double square = val*val;
00941                 quart_sum += square*square;
00942                 cube_sum += square*val;
00943                 square_sum += square;
00944                 sum += val;
00945         }
00946 
00947         double mean = sum/(double)data.size();
00948 
00949         double std_dev = 0.0, skewness = 0.0, kurtosis = 0.0;
00950 
00951         if (data.size() > 1)
00952         {
00953                 // The standard deviation is calculated here
00954                 std_dev = sqrt( (square_sum - mean*sum)/(double)(data.size()-1));
00955 
00956                 double square_mean = mean*mean;
00957                 double cube_mean = mean*square_mean;
00958 
00959                 double square_std_dev = std_dev*std_dev;
00960 
00961                 // This is the numerator of the skewness fraction, if you expand the brackets, as defined in
00962                 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm
00963                 double cubic_sum = cube_sum - 3*square_sum*mean + 3*sum*square_mean - cube_mean*data.size();
00964                 // Complete the skewness fraction
00965                 skewness = cubic_sum/((data.size()-1)*square_std_dev*std_dev);
00966 
00967                 // This is the numerator of the kurtosis fraction, if you expand the brackets, as defined in
00968                 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm
00969                 double quartic_sum = quart_sum - 4*cube_sum*mean + 6*square_sum*square_mean - 4*sum*cube_mean  + square_mean*square_mean*data.size();
00970                 // Complete the kurtosis fraction
00971                 kurtosis = quartic_sum /( (data.size()-1)*square_std_dev*square_std_dev);
00972         }
00973 
00974         Dict parms;
00975         parms["mean"] = mean;
00976         parms["std_dev"] = std_dev;
00977         parms["skewness"] = skewness;
00978         parms["kurtosis"] = kurtosis;
00979 
00980         return parms;
00981 }

bool Util::get_str_float ( const char *  str,
const char *  float_var,
int *  p_nvalues,
float *  p_v1,
float *  p_v2 
) [static]

Extract number of values and the float values, if any, from a string whose format is either "variable=value1,value2 " or "variable".

for example, if the string is "XYZ=1.1,1.2", then 'str' is "XYZ=1.1,1.2"; 'float_var' is "XYZ"; 'p_nvalues' points to 2. 'p_v1' points to 1.1; 'p_v2' points to 1.2. If the string is "XYZ", then 'str' is "XYZ"; 'float_var' is "XYZ". 'p_nvalues' points to 0. 'p_v1' and 'p_v2' unchanged.

Parameters:
[in] str A string like "XYZ=1.1,1.2" or "XYZ".
[in] float_var The variable name "XYZ".
[out] p_nvalues Number of values in the string.
[out] p_v1 The pointer to the first float, if any.
[out] p_v2 The pointer to the second float, if any.
Returns:
True if the extraction succeeds; False if extraction fails.

Definition at line 359 of file util.cpp.

References NullPointerException.

00361 {
00362         if (!s || !float_var || !p_v0 || !p_v1 || !p_v2) {
00363                 throw NullPointerException("string float");
00364         }
00365 
00366         size_t n = strlen(float_var);
00367         *p_v0 = 0;
00368         if (strncmp(s, float_var, n) == 0) {
00369                 if (s[n] == '=') {
00370                         *p_v0 = 2;
00371                         sscanf(&s[n + 1], "%f,%f", p_v1, p_v2);
00372                 }
00373                 else {
00374                         *p_v0 = 1;
00375                 }
00376                 return true;
00377         }
00378         return false;
00379 }

bool Util::get_str_float ( const char *  str,
const char *  float_var,
float *  p_v1,
float *  p_v2 
) [static]

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.

Parameters:
[in] str A string like "XYZ=1.1,1.2";
[in] float_var The variable name "XYZ=".
[out] p_v1 The pointer to the first float.
[out] p_v2 The pointer to the second float.
Returns:
True if the extraction succeeds; False if extraction fails.

Definition at line 344 of file util.cpp.

References NullPointerException.

00345 {
00346         if (!s || !float_var || !p_v1 || !p_v2) {
00347                 throw NullPointerException("string float");
00348         }
00349 
00350         size_t n = strlen(float_var);
00351         if (strncmp(s, float_var, n) == 0) {
00352                 sscanf(&s[n], "%f,%f", p_v1, p_v2);
00353                 return true;
00354         }
00355 
00356         return false;
00357 }

bool Util::get_str_float ( const char *  str,
const char *  float_var,
float *  p_val 
) [static]

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.

Parameters:
[in] str A string like "XYZ=1.1";
[in] float_var The variable name "XYZ=".
[out] p_val The pointer to the float number.
Returns:
True if the extraction succeeds; False if extraction fails.

Definition at line 330 of file util.cpp.

References NullPointerException.

00331 {
00332         if (!s || !float_var || !p_val) {
00333                 throw NullPointerException("string float");
00334         }
00335         size_t n = strlen(float_var);
00336         if (strncmp(s, float_var, n) == 0) {
00337                 *p_val = (float) atof(&s[n]);
00338                 return true;
00339         }
00340 
00341         return false;
00342 }

bool Util::get_str_int ( const char *  str,
const char *  int_var,
int *  p_nvalues,
int *  p_v1,
int *  p_v2 
) [static]

Extract number of values and the int values, if any, from a string whose format is either "variable=value1,value2 " or "variable".

for example, if the string is "XYZ=1,2", then 'str' is "XYZ=1,2"; 'int_var' is "XYZ"; 'p_nvalues' points to 2. 'p_v1' points to 1; 'p_v2' points to 2. If the string is "XYZ", then 'str' is "XYZ"; 'int_var' is "XYZ". 'p_nvalues' points to 0. 'p_v1' and 'p_v2' unchanged.

Parameters:
[in] str A string like "XYZ=1,2" or "XYZ".
[in] int_var The variable name "XYZ".
[out] p_nvalues Number of values in the string.
[out] p_v1 The pointer to the first int, if any.
[out] p_v2 The pointer to the second int, if any.
Returns:
True if the extraction succeeds; False if extraction fails.

Definition at line 409 of file util.cpp.

References NullPointerException.

00410 {
00411         if (!s || !int_var || !p_v0 || !p_v1 || !p_v2) {
00412                 throw NullPointerException("string int");
00413         }
00414 
00415         size_t n = strlen(int_var);
00416         *p_v0 = 0;
00417         if (strncmp(s, int_var, n) == 0) {
00418                 if (s[n] == '=') {
00419                         *p_v0 = 2;
00420                         sscanf(&s[n + 1], "%d,%d", p_v1, p_v2);
00421                 }
00422                 else {
00423                         *p_v0 = 1;
00424                 }
00425                 return true;
00426         }
00427         return false;
00428 }

bool Util::get_str_int ( const char *  str,
const char *  int_var,
int *  p_v1,
int *  p_v2 
) [static]

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.

Parameters:
[in] str A string like "XYZ=1";
[in] int_var The variable name "XYZ=".
[out] p_v1 The pointer to the first int.
[out] p_v2 The pointer to the second int.
Returns:
True if the extraction succeeds; False if extraction fails.

Definition at line 395 of file util.cpp.

References NullPointerException.

00396 {
00397         if (!s || !int_var || !p_v1 || !p_v2) {
00398                 throw NullPointerException("string int");
00399         }
00400 
00401         size_t n = strlen(int_var);
00402         if (strncmp(s, int_var, n) == 0) {
00403                 sscanf(&s[n], "%d,%d", p_v1, p_v2);
00404                 return true;
00405         }
00406         return false;
00407 }

bool Util::get_str_int ( const char *  str,
const char *  int_var,
int *  p_val 
) [static]

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.

Parameters:
[in] str A string like "XYZ=1";
[in] int_var The variable name "XYZ=".
[out] p_val The pointer to the int number.
Returns:
True if the extraction succeeds; False if extraction fails.

Definition at line 381 of file util.cpp.

References NullPointerException.

00382 {
00383         if (!s || !int_var || !p_val) {
00384                 throw NullPointerException("string int");
00385         }
00386 
00387         size_t n = strlen(int_var);
00388         if (strncmp(s, int_var, n) == 0) {
00389                 *p_val = atoi(&s[n]);
00390                 return true;
00391         }
00392         return false;
00393 }

string Util::get_time_label (  )  [static]

Get the current time in a string with format "mm/dd/yyyy hh:mm".

Returns:
The current time string.

Definition at line 1097 of file util.cpp.

References t.

01098 {
01099         time_t t0 = time(0);
01100         struct tm *t = localtime(&t0);
01101         char label[32];
01102         sprintf(label, "%d/%02d/%04d %d:%02d",
01103                         t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, t->tm_min);
01104         return string(label);
01105 }

float * Util::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 
) [static]

Parameters:
[in] freq_cutoff 
[in] P 
[in] r 
[in] dfreq 
[in] alpha 
[in] beta 
Returns:
float*

Definition at line 1162 of file util.cpp.

References abs, printmatrix(), printvector(), q, rhs, V, and W.

Referenced by EMAN::FourierInserter3DMode8::init().

01163 {
01164         int i = 0;
01165         int discs = (int)(1+2*freq_cutoff/dfreq);
01166 
01167         float*  W = new float[discs];
01168 
01169         int fc = (int)(2*freq_cutoff + 1);
01170         gsl_matrix* M = gsl_matrix_calloc(fc,fc);
01171 
01172         gsl_vector* rhs = gsl_vector_calloc(fc);
01173         cout << i++ << endl;
01174         for(int k = -freq_cutoff; k <= freq_cutoff; ++k){
01175                 for(int kp = -freq_cutoff; kp <= freq_cutoff; ++kp){
01176                         int kdiff =abs( k-kp);
01177                         int evenoddfac = ( kdiff % 2 == 0 ? 1 : -1);
01178 
01179                         if (kdiff !=0){
01180                                 float val = sin(M_PI*(float)kdiff*r)/(sin(M_PI*(float)kdiff/(float)P))*(alpha+2.0f*beta*evenoddfac);
01181                                 gsl_matrix_set(M,int(k+freq_cutoff),int(kp+freq_cutoff),val);
01182                         }
01183                 }
01184                 gsl_matrix_set(M,int(k+freq_cutoff),int(k+freq_cutoff),r*P* (alpha+2*beta));
01185                 float val = alpha*sin(M_PI*k*r)/(sin(M_PI*(float)k/(float)P));
01186                 if (k!=0) {
01187                         gsl_vector_set(rhs,int(k+freq_cutoff),val);
01188                 }
01189         }
01190         printmatrix(M,fc,fc,"M");
01191 
01192         gsl_vector_set(rhs,int(freq_cutoff),alpha*r*P);
01193         gsl_matrix* V = gsl_matrix_calloc(fc,fc);
01194         gsl_vector* S = gsl_vector_calloc(fc);
01195         gsl_vector* soln = gsl_vector_calloc(fc);
01196         gsl_linalg_SV_decomp(M,V,S,soln);
01197 
01198         gsl_linalg_SV_solve(M, V, S, rhs, soln); // soln now runs from -freq_cutoff to + freq_cutoff
01199         printvector(soln,fc,"soln");
01200 
01201         // we want to solve for W, which ranges from -freq_cutoff to +freq_cutoff in steps of dfreq                            2
01202         int Count=0;
01203         for(float q = (float)(-freq_cutoff); q <= (float)(freq_cutoff); q+= dfreq){
01204                 float temp=0;
01205                 for(int k = -freq_cutoff; k <= freq_cutoff; ++k){
01206                         float dtemp;
01207                         if (q!=k) {
01208                                 dtemp=(1/(float) P)* (float)gsl_vector_get(soln,int(k+freq_cutoff))  * sin(M_PI*(q-k))/sin(M_PI*(q-k)/((float) P));
01209                         } else{
01210                                 dtemp = (1/(float) P)* (float)gsl_vector_get(soln,int(k+freq_cutoff))  * P;
01211                         }
01212                         temp +=dtemp;
01213                 }
01214                 W[Count]=temp;
01215                 cout << W[Count] << " ";
01216                 Count+=1;
01217         }
01218         cout << endl;
01219         return W;
01220 }

static void EMAN::Util::getvec ( float  phi,
float  theta,
float &  x,
float &  y,
float &  z 
) [inline, static]

Definition at line 931 of file util.h.

Referenced by assign_projangles().

00938                 {
00939 #ifndef WIN32
00940                         return copysign(a, b);
00941 #else
00942                         int flip = -1;
00943                         if ((a <= 0 && b <= 0) || (a > 0 && b > 0)) {
00944                                 flip = 1;
00945                         }
00946                         return a * flip;
00947 #endif
00948                 }
00949 

static int EMAN::Util::goodf ( const double *  p_f  )  [inline, static]

Definition at line 1947 of file util.h.

static int EMAN::Util::goodf ( const float *  p_f  )  [inline, static]

Check whether a number is a good float.

A number is a good float if it is not abnormal zero, and not inf, -inf or NaN

Parameters:
[in] p_f Pointer to the given float number.
Returns:
1 if it is a good float; 0 if it's not good float.

Definition at line 1937 of file util.h.

Referenced by EMAN::FRCCmp::cmp(), EMAN::SqEuclideanCmp::cmp(), EMAN::CccCmp::cmp(), EMAN::EMObject::EMObject(), EMAN::ByteOrder::is_float_big_endian(), EMAN::NormalizeProcessor::process_inplace(), and EMAN::FiniteProcessor::process_pixel().

float Util::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]

Definition at line 5658 of file util_sparx.cpp.

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

05659 {
05660         float *img_ptr = img->get_data();
05661         float *mask_ptr = (mask == NULL)?img->get_data():mask->get_data();
05662 
05663         int *img_freq_bin = new int[3*hist_len];
05664         for(int i = 0;i < (3*hist_len);i++) img_freq_bin[i] = 0;
05665         for(size_t i = 0;i < size_img;++i) {
05666                 if(mask_ptr[i] > 0.5f) {
05667                         float img_xn = img_ptr[i]*PA + PB;
05668                         int L = static_cast<int>(((img_xn - ref_h_min)/ref_h_diff) * (hist_len-1) + hist_len+1);
05669                         if(L >= 0 && L < (3*hist_len)) img_freq_bin[L]++;
05670                 }
05671         }
05672         int freq_hist = 0;
05673 
05674         for(int i = 0;i < (3*hist_len);i++) freq_hist += (int)pow((float)((int)ref_freq_hist[i] - (int)img_freq_bin[i]),2.f);
05675         freq_hist = (-freq_hist);
05676         return static_cast<float>(freq_hist);
05677 }

Dict Util::histc ( EMData ref,
EMData img,
EMData mask 
) [static]

Definition at line 5566 of file util_sparx.cpp.

References B, data, EMAN::EMData::get_attr(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageDimensionException, ImageFormatException, infomask(), EMAN::EMData::is_complex(), and min.

05567 {
05568         /* Exception Handle */
05569         if (img->is_complex() || ref->is_complex())
05570                 throw ImageFormatException("Cannot do Histogram on Fourier Image");
05571 
05572         if(mask != NULL){
05573                 if(img->get_xsize() != mask->get_xsize() || img->get_ysize() != mask->get_ysize() || img->get_zsize() != mask->get_zsize())
05574                         throw ImageDimensionException("The size of mask image should be of same size as the input image"); }
05575         /* ===================================================== */
05576 
05577         /* Image size calculation */
05578         size_t size_ref = ((size_t)(ref->get_xsize())*(ref->get_ysize())*(ref->get_zsize()));
05579         size_t size_img = ((size_t)(img->get_xsize())*(img->get_ysize())*(img->get_zsize()));
05580         /* ===================================================== */
05581 
05582         /* The reference image attributes */
05583         float *ref_ptr = ref->get_data();
05584         float ref_h_min = ref->get_attr("minimum");
05585         float ref_h_max = ref->get_attr("maximum");
05586         float ref_h_avg = ref->get_attr("mean");
05587         float ref_h_sig = ref->get_attr("sigma");
05588         /* ===================================================== */
05589 
05590         /* Input image under mask attributes */
05591         float *mask_ptr = (mask == NULL)?img->get_data():mask->get_data();
05592 
05593         vector<float> img_data = Util::infomask(img, mask);
05594         float img_avg = img_data[0];
05595         float img_sig = img_data[1];
05596 
05597         /* The image under mask -- size calculation */
05598         int cnt=0;
05599         for(size_t i=0;i<size_img;++i)
05600                 if (mask_ptr[i]>0.5f)
05601                                 cnt++;
05602         /* ===================================================== */
05603 
05604         /* Histogram of reference image calculation */
05605         float ref_h_diff = ref_h_max - ref_h_min;
05606 
05607         #ifdef _WIN32
05608                 int hist_len = _cpp_min((unsigned long)size_ref/16,_cpp_min((unsigned long)size_img/16,256lu));
05609         #else
05610                 int hist_len = std::min((unsigned long)size_ref/16,std::min((unsigned long)size_img/16,256lu));
05611         #endif  //_WIN32
05612 
05613         float *ref_freq_bin = new float[3*hist_len];
05614 
05615         //initialize value in each bin to zero
05616         for (int i = 0;i < (3*hist_len);i++) ref_freq_bin[i] = 0.f;
05617 
05618         for (size_t i = 0;i < size_ref;++i) {
05619                 int L = static_cast<int>(((ref_ptr[i] - ref_h_min)/ref_h_diff) * (hist_len-1) + hist_len+1);
05620                 ref_freq_bin[L]++;
05621         }
05622         for (int i = 0;i < (3*hist_len);i++) ref_freq_bin[i] *= static_cast<float>(cnt)/static_cast<float>(size_ref);
05623 
05624         //Parameters Calculation (i.e) 'A' x + 'B'
05625         float A = ref_h_sig/img_sig;
05626         float B = ref_h_avg - (A*img_avg);
05627 
05628         vector<float> args;
05629         args.push_back(A);
05630         args.push_back(B);
05631 
05632         vector<float> scale;
05633         scale.push_back(1.e-7f*A);
05634         scale.push_back(-1.e-7f*B);
05635 
05636         vector<float> ref_freq_hist;
05637         for(int i = 0;i < (3*hist_len);i++) ref_freq_hist.push_back((int)ref_freq_bin[i]);
05638 
05639         vector<float> data;
05640         data.push_back(ref_h_diff);
05641         data.push_back(ref_h_min);
05642 
05643         Dict parameter;
05644 
05645         /* Parameters displaying the arguments A & B, and the scaling function and the data's */
05646         parameter["args"] = args;
05647         parameter["scale"]= scale;
05648         parameter["data"] = data;
05649         parameter["ref_freq_bin"] = ref_freq_hist;
05650         parameter["size_img"]=(double)size_img;
05651         parameter["hist_len"]=hist_len;
05652         /* ===================================================== */
05653 
05654         return parameter;
05655 }

vector< float > Util::histogram ( EMData image,
EMData mask,
int  nbins = 128,
float  hmin = 0.0f,
float  hmax = 0.0f 
) [static]

Definition at line 5508 of file util_sparx.cpp.

References EMAN::EMData::get_attr(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageDimensionException, ImageFormatException, EMAN::EMData::is_complex(), nx, and ny.

05509 {
05510         if (image->is_complex())
05511                 throw ImageFormatException("Cannot do histogram on Fourier image");
05512         //float hmax, hmin;
05513         float *imageptr=0, *maskptr=0;
05514         int nx=image->get_xsize();
05515         int ny=image->get_ysize();
05516         int nz=image->get_zsize();
05517 
05518         if(mask != NULL){
05519                 if(nx != mask->get_xsize() || ny != mask->get_ysize() || nz != mask->get_zsize())
05520                         throw ImageDimensionException("The size of mask image should be of same size as the input image");
05521                 maskptr =mask->get_data();
05522         }
05523         if( nbins == 0) nbins = nx;
05524         vector <float> freq(2*nbins, 0.0);
05525 
05526         imageptr=image->get_data();
05527         if( hmin == hmax ) {
05528                 if(mask == NULL) {
05529                         hmax = image->get_attr("maximum");
05530                         hmin = image->get_attr("minimum");
05531                 } else {
05532                         bool  First = true;
05533                         for (size_t i = 0;i < (size_t)nx*ny*nz; i++) {
05534                         if (maskptr[i]>=0.5f) {
05535                                         if(First) {
05536                                                 hmax = imageptr[i];
05537                                                 hmin = imageptr[i];
05538                                                 First = false;
05539                                         } else {
05540                                                 hmax = (hmax < imageptr[i])?imageptr[i]:hmax;
05541                                                 hmin = (hmin > imageptr[i])?imageptr[i]:hmin;
05542                                         }
05543                                 }
05544                         }
05545                 }
05546         }
05547         float hdiff = hmax - hmin;
05548         float ff = (nbins-1)/hdiff;
05549         for (int i = 0; i < nbins; i++) freq[nbins+i] = hmin + (float(i)+0.5f)/ff;
05550         if(mask == NULL) {
05551                 for(size_t i = 0; i < (size_t)nx*ny*nz; i++) {
05552                         int jbin = static_cast<int>((imageptr[i]-hmin)*ff + 1.5);
05553                         if(jbin >= 1 && jbin <= nbins)  freq[jbin-1] += 1.0;
05554                 }
05555         } else {
05556                 for(size_t i = 0; i < (size_t)nx*ny*nz; i++) {
05557                         if(maskptr[i] >= 0.5) {
05558                                 int jbin = static_cast<int>((imageptr[i]-hmin)*ff + 1.5);
05559                                 if(jbin >= 1 && jbin <= nbins)  freq[jbin-1] += 1.0;
05560                         }
05561                 }
05562         }
05563         return freq;
05564 }

void Util::hsortd ( double *  theta,
double *  phi,
int *  key,
int  len,
int  option 
) [static]

Definition at line 7218 of file util_sparx.cpp.

References cmp1(), cmp2(), ENTERFUNC, EXITFUNC, key, phi, and theta.

07219 {
07220         ENTERFUNC;
07221         vector<tmpstruct> tmp(len);
07222         int i;
07223         for(i = 1;i<=len;i++)
07224         {
07225                 tmp[i-1].theta1 = theta(i);
07226                 tmp[i-1].phi1 = phi(i);
07227                 tmp[i-1].key1 = key(i);
07228         }
07229 
07230         if (option == 1) sort(tmp.begin(),tmp.end(),Util::cmp1);
07231         if (option == 2) sort(tmp.begin(),tmp.end(),Util::cmp2);
07232 
07233         for(i = 1;i<=len;i++)
07234         {
07235                 theta(i) = tmp[i-1].theta1;
07236                 phi(i)   = tmp[i-1].phi1;
07237                 key(i)   = tmp[i-1].key1;
07238         }
07239         EXITFUNC;
07240 }

static float EMAN::Util::hypot2 ( float  x,
float  y 
) [inline, static]

Euclidean distance function in 2D: f(x,y) = sqrt(x*x + y*y);.

Parameters:
[in] x The first number.
[in] y The second number.
Returns:
sqrt(x*x + y*y);

Definition at line 1645 of file util.h.

static float EMAN::Util::hypot3 ( double  x,
double  y,
double  z 
) [inline, static]

Euclidean distance function in 3D: f(x,y,z) = sqrt(x*x + y*y + z*z);.

Parameters:
[in] x The first number.
[in] y The second number.
[in] z The third number.
Returns:
sqrt(x*x + y*y + z*z);

Definition at line 1700 of file util.h.

static float EMAN::Util::hypot3 ( float  x,
float  y,
float  z 
) [inline, static]

Euclidean distance function in 3D: f(x,y,z) = sqrt(x*x + y*y + z*z);.

Parameters:
[in] x The first number.
[in] y The second number.
[in] z The third number.
Returns:
sqrt(x*x + y*y + z*z);

Definition at line 1689 of file util.h.

static float EMAN::Util::hypot3 ( int  x,
int  y,
int  z 
) [inline, static]

Euclidean distance function in 3D: f(x,y,z) = sqrt(x*x + y*y + z*z);.

Parameters:
[in] x The first number.
[in] y The second number.
[in] z The third number.
Returns:
sqrt(x*x + y*y + z*z);

Definition at line 1678 of file util.h.

Referenced by EMAN::EMData::calc_radial_dist(), EMAN::PhaseCmp::cmp(), EMAN::Interp::init_gimx(), EMAN::FourierInserter3DMode5::insert_pixel(), EMAN::FourierInserter3DMode3::insert_pixel(), EMAN::KmeansSegmentProcessor::process(), EMAN::DistanceSegmentProcessor::process(), EMAN::TestImageSphericalWave::process_inplace(), EMAN::AddRandomNoiseProcessor::process_inplace(), EMAN::RotationalAverageProcessor::process_inplace(), and EMAN::LowpassRandomPhaseProcessor::process_inplace().

static float EMAN::Util::hypot3sq ( float  x,
float  y,
float  z 
) [inline, static]

Euclidean distance function squared in 3D: f(x,y,z) = (x*x + y*y + z*z);.

Parameters:
[in] x The first number.
[in] y The second number.
[in] z The third number.
Returns:
(x*x + y*y + z*z);

Definition at line 1667 of file util.h.

static int EMAN::Util::hypot3sq ( int  x,
int  y,
int  z 
) [inline, static]

Euclidean distance function squared in 3D: f(x,y,z) = (x*x + y*y + z*z);.

Parameters:
[in] x The first number.
[in] y The second number.
[in] z The third number.
Returns:
(int)(x*x + y*y + z*z);

Definition at line 1656 of file util.h.

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

float Util::hypot_fast ( int  x,
int  y 
) [static]

Euclidean distance in 2D for integers computed fast using a cached lookup table.

Parameters:
[in] x The first number
[in] y The second number
Returns:
sqrt(x*x+y*y)

Definition at line 713 of file util.cpp.

References abs.

Referenced by EMAN::EMData::calc_az_dist(), EMAN::EMData::calc_radial_dist(), and EMAN::EMAN2Ctf::compute_2d_complex().

00714 {
00715 static float *mem = (float *)malloc(4*128*128);
00716 static int dim = 0;
00717 x=abs(x);
00718 y=abs(y);
00719 
00720 if (x>=dim || y>=dim) {
00721         if (x>2048 || y>2048) return (float)hypot((float)x,(float)y);           // We won't cache anything bigger than 4096^2
00722         dim=x>=dim?x+1:dim;
00723         dim=y>=dim?y+1:dim;
00724         mem=(float*)realloc(mem,4*dim*dim);
00725         for (int y=0; y<dim; y++) {
00726                 for (int x=0; x<dim; x++) {
00727 #ifdef  _WIN32
00728                         mem[x+y*dim]=(float)_hypot((float)x,(float)y);
00729 #else
00730                         mem[x+y*dim]=hypot((float)x,(float)y);
00731 #endif
00732                 }
00733         }
00734 }
00735 
00736 return mem[x+y*dim];
00737 }

short Util::hypot_fast_int ( int  x,
int  y 
) [static]

Euclidean distance in 2D for integers computed fast using a cached lookup table.

Parameters:
[in] x The first number
[in] y The second number
Returns:
(int)round(sqrt(x*x+y*y))

Definition at line 739 of file util.cpp.

References abs, and round().

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

00740 {
00741 static short *mem = (short *)malloc(2*128*128);
00742 static int dim = 0;
00743 x=abs(x);
00744 y=abs(y);
00745 
00746 if (x>=dim || y>=dim) {
00747         if (x>4095 || y>4095) return (short)hypot((float)x,(float)y);           // We won't cache anything bigger than 4096^2
00748         dim=x>=dim?x+1:dim;
00749         dim=y>=dim?y+1:dim;
00750         mem=(short*)realloc(mem,2*dim*dim);
00751         for (int y=0; y<dim; y++) {
00752                 for (int x=0; x<dim; x++) {
00753 #ifdef  _WIN32
00754                         mem[x+y*dim]=(short)Util::round(_hypot((float)x,(float)y));
00755 #else
00756                         mem[x+y*dim]=(short)Util::round(hypot((float)x,(float)y));
00757 #endif
00758                 }
00759         }
00760 }
00761 
00762 return mem[x+y*dim];
00763 }

Dict Util::im_diff ( EMData V1,
EMData V2,
EMData mask = 0 
) [static]

Definition at line 148 of file util_sparx.cpp.

References B, ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), ImageFormatException, EMAN::EMUtil::is_same_size(), LOGERR, nx, ny, EMAN::EMData::set_size(), EMAN::EMData::to_one(), and EMAN::EMData::update().

00149 {
00150         ENTERFUNC;
00151 
00152         if (!EMUtil::is_same_size(V1, V2)) {
00153                 LOGERR("images not same size");
00154                 throw ImageFormatException( "images not same size");
00155         }
00156 
00157         size_t nx = V1->get_xsize();
00158         size_t ny = V1->get_ysize();
00159         size_t nz = V1->get_zsize();
00160         size_t size = (size_t)nx*ny*nz;
00161 
00162         EMData *BD = new EMData();
00163         BD->set_size(nx, ny, nz);
00164 
00165         float *params = new float[2];
00166 
00167         float *V1ptr, *V2ptr, *MASKptr, *BDptr, A, B;
00168         long double S1=0.L,S2=0.L,S3=0.L,S4=0.L;
00169         int nvox = 0L;
00170 
00171         V1ptr = V1->get_data();
00172         V2ptr = V2->get_data();
00173         BDptr = BD->get_data();
00174 
00175 
00176         if(!mask){
00177                 EMData * Mask = new EMData();
00178                 Mask->set_size(nx,ny,nz);
00179                 Mask->to_one();
00180                 MASKptr = Mask->get_data();
00181         } else {
00182                 if (!EMUtil::is_same_size(V1, mask)) {
00183                         LOGERR("mask not same size");
00184                         throw ImageFormatException( "mask not same size");
00185                 }
00186 
00187                 MASKptr = mask->get_data();
00188         }
00189 
00190 
00191 
00192 //       calculation of S1,S2,S3,S3,nvox
00193 
00194         for (size_t i = 0L;i < size; i++) {
00195               if (MASKptr[i]>0.5f) {
00196                S1 += V1ptr[i]*V2ptr[i];
00197                S2 += V1ptr[i]*V1ptr[i];
00198                S3 += V2ptr[i];
00199                S4 += V1ptr[i];
00200                nvox ++;
00201               }
00202         }
00203 
00204         if ((nvox*S1 - S3*S4) == 0. || (nvox*S2 - S4*S4) == 0) {
00205                 A =1.0f ;
00206         } else {
00207                 A = static_cast<float>( (nvox*S1 - S3*S4)/(nvox*S2 - S4*S4) );
00208         }
00209         B = static_cast<float> (A*S4  -  S3)/nvox;
00210 
00211         // calculation of the difference image
00212 
00213         for (size_t i = 0L;i < size; i++) {
00214              if (MASKptr[i]>0.5f) {
00215                BDptr[i] = A*V1ptr[i] -  B  - V2ptr[i];
00216              }  else  {
00217                BDptr[i] = 0.f;
00218              }
00219         }
00220 
00221         BD->update();
00222 
00223         params[0] = A;
00224         params[1] = B;
00225 
00226         Dict BDnParams;
00227         BDnParams["imdiff"] = BD;
00228         BDnParams["A"] = params[0];
00229         BDnParams["B"] = params[1];
00230 
00231         EXITFUNC;
00232         return BDnParams;
00233  }

void Util::image_mutation ( EMData img,
float  mutation_rate 
) [static]

Definition at line 20659 of file util_sparx.cpp.

References array_mutation(), EMAN::EMData::get_attr(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), max, min, and nx.

20659                                                           {
20660         int nx = img->get_xsize();
20661         float min = img->get_attr("minimum");
20662         float max = img->get_attr("maximum");
20663         float* img_data = img->get_data();
20664         array_mutation(img_data, nx*nx, mutation_rate, min, max, 8, 0);
20665         return;
20666 }

vector< float > Util::infomask ( EMData Vol,
EMData mask,
bool   
) [static]

Definition at line 67 of file util_sparx.cpp.

References ENTERFUNC, EMAN::EMData::get_attr(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageDimensionException, ImageFormatException, LOGERR, nx, ny, and sqrt().

00068              :  find statistics under the mask (mask >0.5)
00069 //  flip false: find statistics ourside the mask (mask <0.5)
00070 {
00071         ENTERFUNC;
00072         vector<float> stats;
00073         float *Volptr, *maskptr,MAX,MIN;
00074         long double Sum1,Sum2;
00075         long count;
00076 
00077         MAX = -FLT_MAX;
00078         MIN =  FLT_MAX;
00079         count = 0L;
00080         Sum1  = 0.0L;
00081         Sum2  = 0.0L;
00082 
00083         if (mask == NULL) {
00084                 //Vol->update_stat();
00085                 stats.push_back(Vol->get_attr("mean"));
00086                 stats.push_back(Vol->get_attr("sigma"));
00087                 stats.push_back(Vol->get_attr("minimum"));
00088                 stats.push_back(Vol->get_attr("maximum"));
00089                 return stats;
00090         }
00091 
00092         /* Check if the sizes of the mask and image are same */
00093 
00094         size_t nx = Vol->get_xsize();
00095         size_t ny = Vol->get_ysize();
00096         size_t nz = Vol->get_zsize();
00097 
00098         size_t mask_nx = mask->get_xsize();
00099         size_t mask_ny = mask->get_ysize();
00100         size_t mask_nz = mask->get_zsize();
00101 
00102         if  (nx != mask_nx || ny != mask_ny || nz != mask_nz )
00103                 throw ImageDimensionException("The dimension of the image does not match the dimension of the mask!");
00104 
00105  /*       if (nx != mask_nx ||
00106             ny != mask_ny ||
00107             nz != mask_nz  ) {
00108            // should throw an exception here!!! (will clean it up later CY)
00109            fprintf(stderr, "The dimension of the image does not match the dimension of the mask!\n");
00110            fprintf(stderr, " nx = %d, mask_nx = %d\n", nx, mask_nx);
00111            fprintf(stderr, " ny = %d, mask_ny = %d\n", ny, mask_ny);
00112            fprintf(stderr, " nz = %d, mask_nz = %d\n", nz, mask_nz);
00113            exit(1);
00114         }
00115  */
00116         Volptr = Vol->get_data();
00117         maskptr = mask->get_data();
00118 
00119         for (size_t i = 0; i < (size_t)nx*ny*nz; ++i) {
00120                 if ((maskptr[i]>0.5f) == flip) {
00121                         Sum1 += Volptr[i];
00122                         Sum2 += Volptr[i]*double(Volptr[i]);
00123                         MAX = (MAX < Volptr[i])?Volptr[i]:MAX;
00124                         MIN = (MIN > Volptr[i])?Volptr[i]:MIN;
00125                         count++;
00126                 }
00127         }
00128 
00129         if (count == 0) {
00130                 LOGERR("Invalid mask");
00131                 throw ImageFormatException( "Invalid mask");
00132         }
00133 
00134         float avg = static_cast<float>(Sum1/count);
00135         float sig = static_cast<float>(sqrt((Sum2 - Sum1*Sum1/count)/(count-1)));
00136 
00137         stats.push_back(avg);
00138         stats.push_back(sig);
00139         stats.push_back(MIN);
00140         stats.push_back(MAX);
00141 
00142         return stats;
00143 }

void Util::initial_prune ( vector< vector< int * > > &  Parts,
int *  dimClasses,
int  nParts,
int  K,
int  T 
) [static]

initial_prune removes all classes C from Parts where there does not exist ANY feasible matching containing class C which has weight gt T.

The first element of each class is its original index, and second is dummy variable

Definition at line 20954 of file util_sparx.cpp.

References b, explore(), and k_means_cont_table_().

20954                                                                                                    {
20955         //cout<<"initial_prune\n";
20956         // simple initial pruning. For class indClass of partition indPart:
20957         // For each class of partition which is not indPart, see if there is a class in the partition with which indClass has intersection greater than T
20958         // If for some partition (not equal to indPart) for which there is no such class, then return 0 immediately.
20959 
20960         // 1. For each class of partition which is not indPart, remove the class from Parts if its intx with indClass of indPart is not gt T
20961 
20962         // remember when calling k_means_cont_table, the first element of each class is an index, and actual number of elements in the class
20963         // (as stored in dimClasses) should be decremented by 1 accordingly, and second is dummy variable
20964 
20965         int* dummy(0);
20966         int* cref;
20967         int cref_size;
20968         int* ccomp;
20969         int ccomp_size;
20970         int nintx;
20971         for (int i=0; i < nParts; i++){
20972                 for (int j =0; j < K; j++){
20973 
20974                         // consider class Parts[i][j]
20975                         cref = Parts[i][j];//incr by 1 since first element is index and second is dummy
20976                         cref_size = dimClasses[i*K+cref[0]]-2;
20977 
20978 
20979                         if (cref_size <= T){
20980                                 cref[0] = -1;
20981                                 continue;
20982                         }
20983                         bool done = 0;
20984                         for (int a = 0; a < nParts; a++){
20985                                 if (a == i) continue; //consider all classes not in partition i and set to inactive all those classes whose intx with cref is not gt T
20986                                 bool hasActive=0;
20987                                 for (unsigned int b=0; b < Parts[a].size(); b++){
20988                                         // get the card of the intx between Parts[i][j] and Parts[a][b] using k_means_cont_table
20989                                         // remember first element of each class is the index of the class
20990                                         ccomp = Parts[a][b];
20991                                         ccomp_size= dimClasses[a*K+ccomp[0]]-2;
20992                                         nintx = Util::k_means_cont_table_(cref+2,ccomp+2, dummy, cref_size, ccomp_size,0);
20993 
20994 
20995                                         if (nintx <= T)
20996                                                 ccomp[1] = 0; // class Parts[a][b] is 'inactive' for cref
20997                                         else{
20998                                                 ccomp[1] = 1; // class Parts[a][b] is 'active' for cref
20999                                                 hasActive=1;
21000                                         }
21001                                 }
21002                                 // see if partition a has at least one active class.if not then we're done with cref
21003                                 if (hasActive < 1){
21004                                    done=1;
21005                                    break;
21006                                 }
21007 
21008                         }
21009 
21010                         if (done > 0){
21011                                 // remove class j from partition i
21012 
21013                                 cref[0] = -1; // mark for deletion later
21014                                 continue; // move on to class Parts[i][j+1]
21015                         }
21016 
21017                         // now we adopt more aggressive measures. we find the match with the largest weight which has class Parts[i][j] for partition i.
21018                         // We get rid of Parts[i][j] if this weight is not gt T as no other feasible match containing class Parts[i][j] can be gt T.
21019 
21020                         // (To implement later:) To reduce complexity, determine the order the partitions are to be explored based on the cardinality of the active classes of each partition.
21021                         // Note that if there are ALOT of partitions, then sorting the partititions may actually take longer than the time saved by ordering hte
21022                         // partitions. However, we do not have a lot of partitions, (usually around 10), so sorting time will be trivial compared to exploring time.
21023 
21024                         // explore returns one if there is a feasible matching containing class cref which has weight gt T, and otherwise returns 0
21025                         //bool found = 1;
21026                         bool found = explore(Parts, dimClasses, nParts, K, T, i, cref+2, cref_size, cref, cref_size,0);
21027 
21028                         if (found<1){ // There is NO feasible matching with class j (cref)  with weight greater than T, so delete this class from Parts
21029                                 // Parts[i].erase(Parts[i].begin()+j);
21030                                 cref[0] = -1;
21031                         }
21032                 }
21033 
21034                 // Erase from Parts[i] all the classes that's being designated for erasure
21035 
21036                 for (int d = K-1; d > -1; d--){
21037                         if (Parts[i][d][0] < 0) Parts[i].erase(Parts[i].begin()+d);
21038                 }
21039 
21040         }
21041         //cout <<"number of classes left in each partition after initial prune\n";      
21042         // Print out how many classes are left in each partition
21043         //for (int i =0; i < nParts; i++)
21044         //      cout << Parts[i].size()<<", ";
21045         //cout << "\n";
21046 }

string Util::int2str ( int  n  )  [static]

Get a string format of an integer, e.g.

123 will be "123".

Parameters:
[in] n The input integer.
Returns:
The string format of the given integer.

Definition at line 260 of file util.cpp.

Referenced by EMAN::Log::loc(), and EMAN::E2Exception::what().

00261 {
00262         char s[32] = {'\0'};
00263         sprintf(s, "%d", n);
00264         return string(s);
00265 }

bool Util::is_file_exist ( const string &  filename  )  [static]

check whether a file exists or not

Returns:
True if the file exists; False if not.

Definition at line 208 of file util.cpp.

Referenced by EMAN::TestUtil::check_image(), EMAN::GlobalCache::get_imageio(), EMAN::SpiderIO::SpiderIO(), and EMAN::EMData::write_image().

00209 {
00210         if (access(filename.c_str(), F_OK) == 0) {
00211                 return true;
00212         }
00213         return false;
00214 }

static bool EMAN::Util::IsPower2 ( int  x  )  [inline, static]

Return true if an integer is positive and is power of 2.

Parameters:
[in] x integer to be checked
Returns:
true if the integer is positive and is power of 2

Definition at line 2131 of file util.h.

Referenced by EMAN::WaveletProcessor::process_inplace().

int Util::k_means_cont_table_ ( int *  group1,
int *  group2,
int *  stb,
long int  s1,
long int  s2,
int  flag 
) [static]

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.

The trick is first start wihtout the flag to get number of elements stable, then again with the flag to get the list. This avoid to have two differents functions for the same thing.

Definition at line 5195 of file util_sparx.cpp.

References max.

05195                                                                                                     {
05196     long int d2 = group2[s2 - 1] - group2[0];
05197     long int p2 = 0;
05198     long int i1 = 0;
05199     long int i2 = 0;
05200     long int max = 0;
05201     long int cont = 0;
05202     long int i = 0;
05203     int stop1 = 0;
05204     int stop2 = 0;
05205 
05206     for (i=0; i<s1; i++) {
05207         p2 = (long int)(s2 * (double)group1[i] / (double)d2);
05208         if (p2 >= s2) {p2 = s2 - 1;}
05209         i1 = p2;
05210         i2 = p2;
05211         max = s2;
05212         if (group1[i] < group2[0] || group1[i] > group2[s2 - 1]) {continue;}
05213 
05214         stop1 = 0;
05215         stop2 = 0;
05216         while (max--) {
05217             if (group1[i] == group2[i1]) {
05218                 if (flag) {stb[cont] = group1[i];}
05219                 cont++;
05220                 break;
05221             }
05222             if (group2[i1] < group1[i]) {stop1=1;}
05223             if (group1[i] == group2[i2]) {
05224                 if (flag) {stb[cont] = group1[i];}
05225                 cont++;
05226                 break;
05227             }
05228             if (group2[i2] > group1[i]) {stop2=1;}
05229             //printf("i1 %li i2 %li    v2 %i v2 %i   stop1 %i stop2 %i\n", i1, i2, group2[i1], group2[i2], stop1, stop2);
05230 
05231             if (stop1 & stop2) {break;}
05232             i1--;
05233             i2++;
05234             if (i1 < 0) {i1 = 0;}
05235             if (i2 >= s2) {i2 = s2 - 1;}
05236         }
05237         //printf("v1: %i    ite: %li   cont: %li\n", group1[i], s2-max, cont);
05238     }
05239 
05240     return cont;
05241 }

static float EMAN::Util::linear_interpolate ( float  p1,
float  p2,
float  t 
) [inline, static]

Calculate linear interpolation.

Parameters:
[in] p1 The first number (at x1).
[in] p2 The second number (at x2).
[in] t (x-x1)/(x2-x1)
Returns:
The linearly interpolated value at x.

Definition at line 1511 of file util.h.

Referenced by EMAN::EMData::cut_slice(), EMAN::WienerFourierReconstructor::do_insert_slice_work(), and EMAN::StandardProjector::project3d().

vector< float > Util::list_mutation ( vector< float >  list,
float  mutation_rate,
float  min_val,
float  max_val,
int  K,
int  is_mirror 
) [static]

Definition at line 20722 of file util_sparx.cpp.

References q, and t.

20722                                                                                                                              {
20723 
20724         if (is_mirror != 0) {
20725                 for (vector<float>::iterator q=list.begin(); q!=list.end(); q++) {
20726                         int r = rand()%10000;
20727                         float f = r/10000.0f;
20728                         if (f < mutation_rate) *q = 1-*q;
20729                 }
20730         } else {
20731                 map<int, vector<int> >  graycode;
20732                 map<vector<int>, int> rev_graycode;
20733                 vector <int> gray;
20734 
20735                 int K=1;
20736                 for (int i=0; i<L; i++) K*=2;
20737 
20738                 for (int k=0; k<K; k++) {
20739                         int shift = 0;
20740                         vector <int> gray;
20741                         for (int i=L-1; i>-1; i--) {
20742                                 int t = ((k>>i)%2-shift)%2;
20743                                 gray.push_back(t);
20744                                 shift += t-2;
20745                         }
20746                         graycode[k] = gray;
20747                         rev_graycode[gray] = k;
20748                 }
20749 
20750                 float gap = (K-1)/(max_val-min_val);
20751                 for (vector<float>::iterator q=list.begin(); q!=list.end(); q++) {
20752                         float val = *q;
20753                         if (val < min_val) { val = min_val; }
20754                         else if  (val > max_val) { val = max_val; }
20755                         int k = int((val-min_val)*gap+0.5);
20756                         vector<int> gray = graycode[k];
20757                         bool changed = false;
20758                         for (vector<int>::iterator p=gray.begin(); p!=gray.end(); p++) {
20759                                 int r = rand()%10000;
20760                                 float f = r/10000.0f;
20761                                 if (f < mutation_rate) {
20762                                         *p = 1-*p;
20763                                         changed = true;
20764                                 }
20765                         }
20766                         if (changed) {
20767                                 k = rev_graycode[gray];
20768                                 *q = k/gap+min_val;
20769                         }
20770                 }
20771         }
20772         return list;
20773 }

vector< float > Util::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]

Definition at line 6425 of file util_sparx.cpp.

References cl1().

06426 {
06427     /* System generated locals */
06428     long int q_dim1, q_offset, q1_dim1, q1_offset, i__1, i__2;
06429 
06430     /* Local variables */
06431     long int i__, j, m, n1, ii, jj;
06432     double tmp;
06433     vector<float> p;
06434     --x;
06435     q_dim1 = *klm2d;
06436     q_offset = 1 + q_dim1;
06437     q -= q_offset;
06438     q1_dim1 = *klm2d;
06439     q1_offset = 1 + q1_dim1;
06440     q1 -= q1_offset;
06441     --s;
06442     --res;
06443     iu -= 3;
06444     cu -= 3;
06445 
06446     /* Function Body */
06447     long int l = 0;
06448 
06449 /* C==ZHONG HUANG,JULY,12,02;L=0,1,2,3,4,5,6 correspond to different equality constraints */
06450     m = *ks;
06451     n1 = *n + 1;
06452     if (*iswi == 1) {
06453         i__1 = n1;
06454         for (jj = 1; jj <= i__1; ++jj) {
06455             i__2 = *ks;
06456             for (ii = 1; ii <= i__2; ++ii) {
06457         /*      q[ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];*/
06458 
06459                 q[*ks + ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1]
06460                         ;
06461             }
06462         }
06463     } else if (*iswi == 2) {
06464         i__1 = *ks;
06465         for (ii = 1; ii <= i__1; ++ii) {
06466             i__2 = n1;
06467             for (jj = 1; jj <= i__2; ++jj) {
06468                 q[ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06469                 q[*ks + ii + jj * q_dim1] = -((double) q1[ii + jj * q1_dim1]);
06470             }
06471         }
06472     } else if (*iswi == 3) {
06473         l = 2;
06474         i__1 = n1;
06475         for (jj = 1; jj <= i__1; ++jj) {
06476             i__2 = *ks + 2;
06477             for (ii = 1; ii <= i__2; ++ii) {
06478                 q[ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06479             }
06480             i__2 = *ks;
06481             for (ii = 1; ii <= i__2; ++ii) {
06482                 q[*ks + 2 + ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06483             }
06484         }
06485     } else if (*iswi == 4) {
06486         l = 2;
06487         i__1 = n1;
06488         for (jj = 1; jj <= i__1; ++jj) {
06489             i__2 = *ks + 2;
06490             for (ii = 1; ii <= i__2; ++ii) {
06491                 q[ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06492             }
06493             i__2 = *ks;
06494             for (ii = 1; ii <= i__2; ++ii) {
06495                 q[*ks + 2 + ii + jj * q_dim1] = -((double) q1[ii + jj * q1_dim1]);
06496             }
06497         }
06498     } else if (*iswi == 5) {
06499         l = 1;
06500         i__1 = n1;
06501         for (jj = 1; jj <= i__1; ++jj) {
06502             i__2 = *ks + 1;
06503             for (ii = 1; ii <= i__2; ++ii) {
06504                 q[ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06505             }
06506             i__2 = *ks;
06507             for (ii = 1; ii <= i__2; ++ii) {
06508                 q[*ks + 1 + ii + jj * q_dim1] = -((double) q1[ii + jj * q1_dim1]);
06509             }
06510         }
06511     } else if (*iswi == 6) {
06512         l = 1;
06513         i__1 = n1;
06514         for (jj = 1; jj <= i__1; ++jj) {
06515             i__2 = *ks + 1;
06516             for (ii = 1; ii <= i__2; ++ii) {
06517                 q[ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06518             }
06519             i__2 = *ks;
06520             for (ii = 1; ii <= i__2; ++ii) {
06521                 q[*ks + 1 + ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06522             }
06523         }
06524     } else if (*iswi == 7) {
06525         l = 3;
06526         i__1 = n1;
06527         for (jj = 1; jj <= i__1; ++jj) {
06528             i__2 = *ks + 3;
06529             for (ii = 1; ii <= i__2; ++ii) {
06530                 q[ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06531             }
06532             i__2 = *ks;
06533             for (ii = 1; ii <= i__2; ++ii) {
06534                 q[*ks + 3 + ii + jj * q_dim1] = -((double) q1[ii + jj * q1_dim1]);
06535             }
06536         }
06537     } else if (*iswi == 8) {
06538         l = 4;
06539         i__1 = n1;
06540         for (jj = 1; jj <= i__1; ++jj) {
06541             i__2 = *ks + 4;
06542             for (ii = 1; ii <= i__2; ++ii) {
06543                 q[ii + jj * q_dim1] = (double) q1[ii + jj * q1_dim1];
06544             }
06545             i__2 = *ks;
06546             for (ii = 1; ii <= i__2; ++ii) {
06547                 q[*ks + 4 + ii + jj * q_dim1] = -((double) q1[ii + jj * q1_dim1]);
06548             }
06549         }
06550     }
06551 
06552     Util::cl1(ks, &l, &m, n, klm2d, &q[q_offset], &x[1], &res[1], &cu[3], &iu[3], &s[1]);
06553     i__1 = *ks;
06554     int tmp__j=0;
06555     for (i__ = 1; i__ <= i__1; ++i__) {
06556         tmp = 0.f;
06557         i__2 = *n - 1;
06558         for (j = 1; j <= i__2; ++j) {
06559         tmp__j=j;
06560             tmp += pow(q1[i__ + q1_dim1], tmp__j) * x[j];
06561         }
06562         tmp += x[*n];
06563         p.push_back(static_cast<float>(exp(tmp)));
06564         p.push_back(q1[i__ + q1_dim1]);
06565     }
06566     i__2=*n;
06567     for (i__=1;i__<=i__2;++i__)
06568         { p.push_back(static_cast<float>(x[i__]));}
06569     return p;
06570 }

void Util::mad_scalar ( EMData img,
EMData img1,
float  scalar 
) [static]

Definition at line 17539 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().

17540 {
17541         ENTERFUNC;
17542         /* Exception Handle */
17543         if (!img) {
17544                 throw NullPointerException("NULL input image");
17545         }
17546         /* ==============   img += scalar*img1   ================ */
17547 
17548         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17549         size_t size = (size_t)nx*ny*nz;
17550         float *img_ptr  =img->get_data();
17551         float *img1_ptr = img1->get_data();
17552         for (size_t i=0;i<size;++i)img_ptr[i] += img1_ptr[i]*scalar;
17553         img1->update();
17554 
17555         EXITFUNC;
17556 }

EMData * Util::madn_scalar ( EMData img,
EMData img1,
float  scalar 
) [static]

Definition at line 17349 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().

17350 {
17351         ENTERFUNC;
17352         /* Exception Handle */
17353         if (!img) {
17354                 throw NullPointerException("NULL input image");
17355         }
17356         /* ==============   output = img + scalar*img1   ================ */
17357 
17358         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17359         size_t size = (size_t)nx*ny*nz;
17360         EMData * img2 = img->copy_head();
17361         float *img_ptr  =img->get_data();
17362         float *img2_ptr = img2->get_data();
17363         float *img1_ptr = img1->get_data();
17364         for (size_t i=0;i<size;++i) img2_ptr[i] = img_ptr[i] + img1_ptr[i]*scalar;
17365         img2->update();
17366         if(img->is_complex()) {
17367                 img2->set_complex(true);
17368                 if(img->is_fftodd()) img2->set_fftodd(true); else img2->set_fftodd(false);
17369         }
17370 
17371         EXITFUNC;
17372         return img2;
17373 }

static double EMAN::Util::mean ( double *  x,
int  n 
) [inline, static]

Definition at line 1086 of file util.h.

Referenced by get_stats(), get_stats_cstyle(), and multi_align_error_dfunc().

01089                                                                                   {
01090                         float c = ceilf(value);

vector< float > Util::merge_peaks ( vector< float >  peak1,
vector< float >  peak2,
float  p_size 
) [static]

Definition at line 6207 of file util_sparx.cpp.

06208 {
06209         vector<float>new_peak;
06210         int n1=peak1.size()/3;
06211         float p_size2=p_size*p_size;
06212         for (int i=0;i<n1;++i) {
06213                 vector<float>::iterator it2= peak1.begin()+3*i;
06214                 bool push_back1=true;
06215                 int n2=peak2.size()/3;
06216                 /*cout<<"peak2 size==="<<n2<<"i====="<<i<<endl;
06217                        cout<<"new peak size==="<<new_peak.size()/3<<endl;*/
06218                 if(n2 ==0) {
06219                         new_peak.push_back(*it2);
06220                         new_peak.push_back(*(it2+1));
06221                         new_peak.push_back(*(it2+2));
06222                 } else  {
06223                         int j=0;
06224                         while (j< n2-1 ) {
06225                                 vector<float>::iterator it3= peak2.begin()+3*j;
06226                                 float d2=((*(it2+1))-(*(it3+1)))*((*(it2+1))-(*(it3+1)))+((*(it2+2))-(*(it3+2)))*((*(it2+2))-(*(it3+2)));
06227                                 if(d2< p_size2 ) {
06228                                         if( (*it2)<(*it3) ) {
06229                                                 new_peak.push_back(*it3);
06230                                                 new_peak.push_back(*(it3+1));
06231                                                 new_peak.push_back(*(it3+2));
06232                                                 peak2.erase(it3);
06233                                                 peak2.erase(it3);
06234                                                 peak2.erase(it3);
06235                                                 push_back1=false;
06236                                         } else {
06237                                                 peak2.erase(it3);
06238                                                 peak2.erase(it3);
06239                                                 peak2.erase(it3);
06240                                         }
06241                                 } else  j=j+1;
06242                                 n2=peak2.size()/3;
06243                         }
06244                         if(push_back1) {
06245                                 new_peak.push_back(*it2);
06246                                 new_peak.push_back(*(it2+1));
06247                                 new_peak.push_back(*(it2+2));
06248                         }
06249                 }
06250         }
06251         return new_peak;
06252 }

Dict Util::min_dist_four ( EMData image,
const vector< EMData * > &  data 
) [static]

helper function for k-means

Definition at line 5120 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), nx, and ny.

05120                                                                    {
05121         ENTERFUNC;
05122 
05123         int nima = data.size();
05124         vector<float> res(nima);
05125         double result = 0.;
05126         double valmin = 1.0e20;
05127         int valpos = -1;
05128 
05129         for (int kk=0; kk<nima; kk++){
05130         result = 0;
05131         //validate_input_args(image, data[kk]);
05132 
05133         float *y_data = data[kk]->get_data();
05134         float *x_data = image->get_data();
05135 
05136         // Implemented by PAP  01/09/06 - please do not change.  If in doubts, write/call me.
05137         int nx  = data[kk]->get_xsize();
05138         int ny  = data[kk]->get_ysize();
05139         nx = (nx - 2 + data[kk]->is_fftodd()); // nx is the real-space size of the input image
05140         int lsd2 = (nx + 2 - nx%2) ; // Extended x-dimension of the complex image
05141 
05142         int ixb = 2*((nx+1)%2);
05143         int iyb = ny%2;
05144         int iz = 0;
05145 
05146         for ( int iy = 0; iy <= ny-1; iy++) {
05147             for ( int ix = 2; ix <= lsd2 - 1 - ixb; ix++) {
05148                 int ii = ix + (iy  + iz * ny)* lsd2;
05149                 result += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
05150             }
05151         }
05152         for ( int iy = 1; iy <= ny/2-1 + iyb; iy++) {
05153             int ii = (iy  + iz * ny)* lsd2;
05154             result += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
05155             result += (x_data[ii+1] - y_data[ii+1])*double(x_data[ii+1] - y_data[ii+1]);
05156         }
05157         if(nx%2 == 0) {
05158             for ( int iy = 1; iy <= ny/2-1 + iyb; iy++) {
05159                 int ii = lsd2 - 2 + (iy  + iz * ny)* lsd2;
05160                 result += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
05161                 result += (x_data[ii+1] - y_data[ii+1])*double(x_data[ii+1] - y_data[ii+1]);
05162             }
05163 
05164         }
05165         result *= 2;
05166         result += (x_data[0] - y_data[0])*double(x_data[0] - y_data[0]);
05167         if(ny%2 == 0) {
05168             int ii = (ny/2  + iz * ny)* lsd2;
05169             result += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
05170         }
05171         if(nx%2 == 0) {
05172             int ii = lsd2 - 2 + (0  + iz * ny)* lsd2;
05173             result += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
05174             if(ny%2 == 0) {
05175                 int ii = lsd2 - 2 +(ny/2  + iz * ny)* lsd2;
05176                 result += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
05177             }
05178         }
05179 
05180         result /= (long int)nx*(long int)ny*(long int)nx*(long int)ny;
05181         res[kk] = (float)result;
05182 
05183         if(result<valmin) {valmin = result; valpos = kk;}
05184 
05185         }
05186 
05187         Dict retvals;
05188         retvals["dist"] = res;
05189         retvals["pos"]  = valpos;
05190 
05191         EXITFUNC;
05192         return retvals;
05193 }

Dict Util::min_dist_real ( EMData image,
const vector< EMData * > &  data 
) [static]

k-means helper

Definition at line 5085 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), and EMAN::EMData::get_ysize().

05085                                                                    {
05086         ENTERFUNC;
05087 
05088         int nima = data.size();
05089         vector<float> res(nima);
05090         double result = 0.;
05091         double valmin = 1.0e20;
05092         int valpos = -1;
05093 
05094         for (int kk=0; kk<nima; kk++){
05095         result = 0;
05096 
05097         float *y_data = data[kk]->get_data();
05098         float *x_data = image->get_data();
05099         long totsize = image->get_xsize()*image->get_ysize();
05100         for (long i = 0; i < totsize; i++) {
05101             double temp = x_data[i]- y_data[i];
05102             result += temp*temp;
05103         }
05104         result /= totsize;
05105         res[kk] = (float)result;
05106 
05107         if(result<valmin) {valmin = result; valpos = kk;}
05108 
05109         }
05110 
05111         Dict retvals;
05112         retvals["dist"] = res;
05113         retvals["pos"]  = valpos;
05114 
05115         EXITFUNC;
05116         return retvals;
05117 
05118 }

static int EMAN::Util::mono ( int  k1,
int  k2 
) [inline, static]

Definition at line 1071 of file util.h.

Referenced by cluster_pairwise().

01084                                             {

EMData * Util::move_points ( EMData img,
float  qprob,
int  ri,
int  ro 
) [static]

Definition at line 19888 of file util_sparx.cpp.

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

19889 {
19890         ENTERFUNC;
19891         /* Exception Handle */
19892         if (!img) {
19893                 throw NullPointerException("NULL input image");
19894         }
19895 
19896         int newx, newy, newz;
19897         bool  keep_going;
19898         cout << " entered   " <<endl;
19899         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
19900         //int size = nx*ny*nz;
19901         EMData * img2 = new EMData();
19902         img2->set_size(nx,ny,nz);
19903         img2->to_zero();
19904         float *img_ptr  =img->get_data();
19905         float *img2_ptr = img2->get_data();
19906         int r2 = ro*ro;
19907         int r3 = r2*ro;
19908         int ri2 = ri*ri;
19909         int ri3 = ri2*ri;
19910 
19911         int n2 = nx/2;
19912 
19913         for (int k=-n2; k<=n2; k++) {           //cout << " k   "<<k <<endl;
19914                 float z2 = static_cast<float>(k*k);
19915                 for (int j=-n2; j<=n2; j++) {
19916                         float y2 = z2 + j*j;
19917                         if(y2 <= r2) {
19918                                                                                         //cout << "  j  "<<j <<endl;
19919 
19920                                 for (int i=-n2; i<=n2; i++) {
19921                                         float x2 = y2 + i*i;
19922                                         if(x2 <= r3) {
19923                                                                                         //cout << "  i  "<<i <<endl;
19924                                                 int ib = i+n2; int jb = j+n2; int kb = k+n2;
19925                                                 if(x2 >= ri3) {
19926                                                         //  this is the outer shell, here points can only vanish
19927                                                         if(img_ptr(ib,jb,kb) == 1.0f) {
19928                                                                 //cout << "  1  "<<ib <<endl;
19929                                                                 if(Util::get_frand(0.0f, 1.0f) > qprob){
19930                                                                         img2_ptr(ib,jb,kb) = 0.0f;
19931                                                                         keep_going = true;
19932                                                                 //cout << "  try  "<<ib <<endl;
19933                                                                         while(keep_going) {
19934                                                                                 newx = Util::get_irand(-ro,ro);
19935                                                                                 newy = Util::get_irand(-ro,ro);
19936                                                                                 newz = Util::get_irand(-ro,ro);
19937                                                                                 if(newx*newx+newy*newy+newz*newz <= r3) {
19938                                                                                         newx += n2; newy += n2; newz += n2;
19939                                                                                         if( img_ptr(newx,newy,newz) == 0.0f) {
19940                                                                                                 img2_ptr(newx,newy,newz) = 1.0f;
19941                                                                                                 keep_going = false; }
19942                                                                                 }
19943                                                                         }
19944                                                                 }   else  img2_ptr(ib,jb,kb) = 1.0f;
19945                                                         }
19946                                                 }  else  {
19947                                                         // this is inner shell, the point can only move (or vanish, if all neighbors exist)
19948                                                         if(img_ptr(ib,jb,kb) == 1.0) {
19949                                                                 if(Util::get_frand(0.0f,1.0f) > qprob) {
19950                                                                         //  find out the number of neighbors
19951                                                                         float  numn = -1.0f;  // we already know the central one is 1
19952                                                                         for (newz = -1; newz <= 1; newz++)
19953                                                                                 for (newy = -1; newy <= 1; newy++)
19954                                                                                         for (newx = -1; newx <= 1; newx++)
19955                                                                                                 numn += img_ptr(ib+newx,jb+newy,kb+newz);
19956                                                                         img2_ptr(ib,jb,kb) = 0.0;
19957                                                                         if(numn == 26.0f) {
19958                                                                                 //  all neighbors exist, it has to vanish
19959                                                                                 keep_going = true;
19960                                                                                 while(keep_going) {
19961                                                                                         newx = Util::get_irand(-ro,ro);
19962                                                                                         newy = Util::get_irand(-ro,ro);
19963                                                                                         newz = Util::get_irand(-ro,ro);
19964                                                                                         if(newx*newx+newy*newy+newz*newz < r3) {
19965                                                                                                 newx += n2; newy += n2; newz += n2;
19966                                                                                                 if( img_ptr(newx,newy,newz) == 0.0f) {
19967                                                                                                         if(newx*newx+newy*newy+newz*newz < r3) {
19968                                                                                                                 if(newx*newx+newy*newy+newz*newz < r3) {
19969                                                                                                                         newx += n2; newy += n2; newz += n2;
19970                                                                                                                         if( img_ptr(newx,newy,newz) == 0.0f) {
19971                                                                                                                                 img2_ptr(newx,newy,newz) = 1.0f;
19972                                                                                                                                 keep_going = false; }
19973                                                                                                                 }
19974                                                                                                         }
19975                                                                                                 }
19976                                                                                         }
19977                                                                                 }
19978                                                                         }  else if(numn == 25.0f) {
19979                                                                                 // there is only one empty neighbor, move there
19980                                                                                 for (newz = -1; newz <= 1; newz++) {
19981                                                                                         for (newy = -1; newy <= 1; newy++) {
19982                                                                                                 for (newx = -1; newx <= 1; newx++) {
19983                                                                                                         if( newx != 0 && newy != 0 && newz != 0) {
19984                                                                                                                 if(img_ptr(newx+ib,newy+jb,newz+kb) == 0.0f) {
19985                                                                                                                         img2_ptr(newx+ib,newy+jb,newz+kb) = 1.0f;
19986                                                                                                                         }
19987                                                                                                         }
19988                                                                                                 }
19989                                                                                         }
19990                                                                                 }
19991                                                                         }  else {
19992                                                                                 //  more than one neighbor is zero, select randomly one and move there
19993                                                                                 keep_going = true;
19994                                                                                 while(keep_going) {
19995                                                                                         newx = Util::get_irand(-1,1);
19996                                                                                         newy = Util::get_irand(-1,1);
19997                                                                                         newz = Util::get_irand(-1,1);
19998                                                                                         if(newx != 0 && newy != 0 && newz != 0)  {
19999                                                                                                 if(img_ptr(ib+newx,jb+newy,kb+newz) == 0.0f) {
20000                                                                                                         img2_ptr(ib+newx,jb+newy,kb+newz) = 1.0f;//?????
20001                                                                                                         keep_going = false;
20002                                                                                                 }
20003                                                                                         }
20004                                                                                 }
20005                                                                         }
20006                                                                 }  else img2_ptr(ib,jb,kb) = 1.0f;
20007                                                         }
20008                                                 }
20009                                         }
20010                                 }
20011                         }
20012                 }
20013         }
20014         //for (int i=0;i<size;i++) img2_ptr[i] = img_ptr[i] + Util::get_irand(1,7);
20015         img2->update();
20016 
20017         EXITFUNC;
20018         return img2;
20019 }

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

Definition at line 17638 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().

17639 {
17640         ENTERFUNC;
17641         /* Exception Handle */
17642         if (!img) {
17643                 throw NullPointerException("NULL input image");
17644         }
17645         /* ========= img *= img1 ===================== */
17646 
17647         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17648         size_t size = (size_t)nx*ny*nz;
17649         float *img_ptr  = img->get_data();
17650         float *img1_ptr = img1->get_data();
17651         if(img->is_complex()) {
17652                 for (size_t i=0; i<size; i+=2) {
17653                         float tmp     = img_ptr[i] * img1_ptr[i]   - img_ptr[i+1] * img1_ptr[i+1] ;
17654                         img_ptr[i+1] = img_ptr[i] * img1_ptr[i+1] + img_ptr[i+1] * img1_ptr[i] ;
17655                         img_ptr[i]   = tmp;
17656 
17657                 }
17658         } else {
17659                 for (size_t i=0;i<size;++i) img_ptr[i] *= img1_ptr[i];
17660         }
17661         img->update();
17662 
17663         EXITFUNC;
17664 }

void Util::mul_scalar ( EMData img,
float  scalar 
) [static]

Definition at line 17521 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().

17522 {
17523         ENTERFUNC;
17524         /* Exception Handle */
17525         if (!img) {
17526                 throw NullPointerException("NULL input image");
17527         }
17528         /* ============  output = scalar*input  ================== */
17529 
17530         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17531         size_t size = (size_t)nx*ny*nz;
17532         float *img_ptr  =img->get_data();
17533         for (size_t i=0;i<size;++i) img_ptr[i] *= scalar;
17534         img->update();
17535 
17536         EXITFUNC;
17537 }

EMData * Util::muln_img ( EMData img,
EMData img1 
) [static]

Definition at line 17427 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().

17428 {
17429         ENTERFUNC;
17430         /* Exception Handle */
17431         if (!img) {
17432                 throw NullPointerException("NULL input image");
17433         }
17434         /* ==============   output = img * img1   ================ */
17435 
17436         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17437         size_t size = (size_t)nx*ny*nz;
17438         EMData * img2 = img->copy_head();
17439         float *img_ptr  =img->get_data();
17440         float *img2_ptr = img2->get_data();
17441         float *img1_ptr = img1->get_data();
17442         if(img->is_complex()) {
17443                 for (size_t i=0; i<size; i+=2) {
17444                         img2_ptr[i]   = img_ptr[i] * img1_ptr[i]   - img_ptr[i+1] * img1_ptr[i+1] ;
17445                         img2_ptr[i+1] = img_ptr[i] * img1_ptr[i+1] + img_ptr[i+1] * img1_ptr[i] ;
17446                 }
17447                 img2->set_complex(true);
17448                 if(img->is_fftodd()) img2->set_fftodd(true); else img2->set_fftodd(false);
17449         } else {
17450                 for (size_t i=0; i<size; ++i) img2_ptr[i] = img_ptr[i] * img1_ptr[i];
17451                 img2->update();
17452         }
17453 
17454         EXITFUNC;
17455         return img2;
17456 }

EMData * Util::mult_scalar ( EMData img,
float  scalar 
) [static]

Definition at line 17324 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().

17325 {
17326         ENTERFUNC;
17327         /* Exception Handle */
17328         if (!img) {
17329                 throw NullPointerException("NULL input image");
17330         }
17331         /* ============  output = scalar*input  ================== */
17332 
17333         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17334         size_t size = (size_t)nx*ny*nz;
17335         EMData * img2 = img->copy_head();
17336         float *img_ptr  =img->get_data();
17337         float *img2_ptr = img2->get_data();
17338         for (size_t i=0;i<size;++i)img2_ptr[i] = img_ptr[i]*scalar;
17339         img2->update();
17340 
17341         if(img->is_complex()) {
17342                 img2->set_complex(true);
17343                 if(img->is_fftodd()) img2->set_fftodd(true); else img2->set_fftodd(false);
17344         }
17345         EXITFUNC;
17346         return img2;
17347 }

vector< float > Util::multi_align_error ( vector< float >  args,
vector< float >  all_ali_params,
int  d 
) [static]

Definition at line 19623 of file util_sparx.cpp.

References multi_align_error_dfunc(), multi_align_error_func(), setulb_(), and SIXTY.

19623                                                                                              {
19624         
19625         const int nmax=args.size(), mmax=nmax;
19626         char task[60], csave[60];
19627         long int lsave[4];
19628         long int n, m, iprint, isave[44];
19629         long int* nbd = new long int[nmax];
19630         long int* iwa = new long int[3*nmax];
19631         double f, factr, pgtol;
19632         double* x = new double[nmax];
19633         double* l = new double[nmax];
19634         double* u = new double[nmax];
19635         double* g = new double[nmax];
19636         double dsave[29];
19637         double* wa = new double[2*mmax*nmax+4*nmax+12*mmax*mmax+12*mmax];
19638         long int SIXTY=60;
19639 
19640         int num_ali = nmax/3+1;
19641         int nima = all_ali_params.size()/(num_ali*4);
19642         
19643         //     We wish to have no output.
19644         iprint = -1;
19645 
19646         //c     We specify the tolerances in the stopping criteria.
19647         factr=1.0e1;
19648         pgtol=1.0e-9;
19649 
19650         //     We specify the dimension n of the sample problem and the number
19651         //        m of limited memory corrections stored.  (n and m should not
19652         //        exceed the limits nmax and mmax respectively.)
19653         n=nmax;
19654         m=mmax;
19655 
19656         //     We now provide nbd which defines the bounds on the variables:
19657         //                    l   specifies the lower bounds,
19658         //                    u   specifies the upper bounds.
19659         //                    x   specifies the initial guess
19660         for (int i=0; i<nmax; i++) {
19661                 x[i] = args[i]; 
19662                 nbd[i] = 0;
19663         }
19664 
19665         //     We start the iteration by initializing task.
19666         // (**MUST clear remaining chars in task with spaces (else crash)!**)
19667         strcpy(task,"START");
19668         for (int i=5;i<60;i++)  task[i]=' ';
19669 
19670         //     This is the call to the L-BFGS-B code.
19671         // (* call the L-BFGS-B routine with task='START' once before loop *)
19672         setulb_(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,task,&iprint,csave,lsave,isave,dsave,SIXTY,SIXTY);
19673         int step = 1;
19674 
19675         // (* while routine returns "FG" or "NEW_X" in task, keep calling it *)
19676         while (strncmp(task,"FG",2)==0 || strncmp(task,"NEW_X",5)==0) {
19677 
19678                 if (strncmp(task,"FG",2)==0) {
19679                 //   the minimization routine has returned to request the
19680                 //   function f and gradient g values at the current x
19681 
19682                 //        Compute function value f for the sample problem.
19683                 f = multi_align_error_func(x, all_ali_params, nima, num_ali, d);
19684 
19685                 //        Compute gradient g for the sample problem.
19686                 multi_align_error_dfunc(x, all_ali_params, nima, num_ali, g, d);
19687 
19688                 }
19689                 //c          go back to the minimization routine.
19690                 setulb_(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,task,&iprint,csave,lsave,isave,dsave,SIXTY,SIXTY);
19691                 step++;
19692         }
19693 
19694         //printf("Total step is %d\n", step);
19695         vector<float> res;
19696         for (int i=0; i<nmax; i++) res.push_back(static_cast<float>(x[i]));
19697         res.push_back(static_cast<float>(f));
19698 
19699         delete[] nbd;
19700         delete[] iwa;
19701         delete[] x;
19702         delete[] l;
19703         delete[] u;
19704         delete[] g;
19705         delete[] wa;
19706 
19707         return res;
19708 
19709 }

void Util::multi_align_error_dfunc ( double *  x,
vector< float >  all_ali_params,
int  nima,
int  num_ali,
double *  g,
int  d 
) [static]

Definition at line 19769 of file util_sparx.cpp.

References mean(), and sqrt().

Referenced by multi_align_error().

19769                                                                                                                {
19770 
19771         for (int i=0; i<num_ali*3-3; i++)    g[i] = 0.0;
19772 
19773         double* args = new double[num_ali*3];
19774         for (int i=0; i<3*num_ali-3; i++)   args[i] = x[i];
19775         args[3*num_ali-3] = 0.0;
19776         args[3*num_ali-2] = 0.0;
19777         args[3*num_ali-1] = 0.0;
19778         double* cosa = new double[num_ali];
19779         double* sina = new double[num_ali];
19780         for (int i=0; i<num_ali; i++) {
19781                 cosa[i] = cos(args[i*3]*M_PI/180.0);
19782                 sina[i] = sin(args[i*3]*M_PI/180.0);
19783         }
19784         double* sx = new double[num_ali];
19785         double* sy = new double[num_ali];
19786         
19787         vector<float> sqr_pixel_error(nima);
19788 
19789         for (int i=0; i<nima; i++) {
19790                 double sum_cosa = 0.0;
19791                 double sum_sina = 0.0;
19792                 for (int j=0; j<num_ali; j++) {
19793                         if (static_cast<int>(ali_params[j*nima*4+i*4+3]) == 0) {
19794                                 sum_cosa += cos((args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0);
19795                                 sum_sina += sin((args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0);
19796                                 sx[j] = args[j*3+1] + ali_params[j*nima*4+i*4+1]*cosa[j] - ali_params[j*nima*4+i*4+2]*sina[j];
19797                                 sy[j] = args[j*3+2] + ali_params[j*nima*4+i*4+1]*sina[j] + ali_params[j*nima*4+i*4+2]*cosa[j];
19798                         } else {
19799                                 sum_cosa += cos((-args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0);
19800                                 sum_sina += sin((-args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0);
19801                                 sx[j] = -args[j*3+1] + ali_params[j*nima*4+i*4+1]*cosa[j] + ali_params[j*nima*4+i*4+2]*sina[j];
19802                                 sy[j] =  args[j*3+2] - ali_params[j*nima*4+i*4+1]*sina[j] + ali_params[j*nima*4+i*4+2]*cosa[j];
19803                         }
19804                 }
19805                 double P = sqrt(sum_cosa*sum_cosa+sum_sina*sum_sina);
19806                 sum_cosa /= P;
19807                 sum_sina /= P;
19808                 for (int j=0; j<num_ali-1; j++) {
19809                         double dx = 2.0*(sx[j]-mean(sx, num_ali));
19810                         double dy = 2.0*(sy[j]-mean(sy, num_ali));
19811                         if (static_cast<int>(ali_params[j*nima*4+i*4+3]) == 0) {
19812                                 g[j*3] += (d*d/4.0*(sum_cosa*sin((args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0) -
19813                                                     sum_sina*cos((args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0)) +
19814                                                     dx*(-ali_params[j*nima*4+i*4+1]*sina[j]-ali_params[j*nima*4+i*4+2]*cosa[j])+
19815                                                     dy*( ali_params[j*nima*4+i*4+1]*cosa[j]-ali_params[j*nima*4+i*4+2]*sina[j]))*M_PI/180.0;
19816                                 g[j*3+1] += dx;
19817                                 g[j*3+2] += dy;
19818                         } else {
19819                                 g[j*3] += (d*d/4.0*(-sum_cosa*sin((-args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0) +
19820                                                      sum_sina*cos((-args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0)) +
19821                                                     dx*(-ali_params[j*nima*4+i*4+1]*sina[j]+ali_params[j*nima*4+i*4+2]*cosa[j])+
19822                                                     dy*(-ali_params[j*nima*4+i*4+1]*cosa[j]-ali_params[j*nima*4+i*4+2]*sina[j]))*M_PI/180.0;
19823                                 g[j*3+1] += -dx;
19824                                 g[j*3+2] += dy;
19825                         }
19826                 }
19827         }
19828         
19829         for (int i=0; i<3*num_ali-3; i++)  g[i] /= (num_ali*nima);
19830         
19831         delete[] args;
19832         delete[] cosa;
19833         delete[] sina;
19834         delete[] sx;
19835         delete[] sy;
19836 }

double Util::multi_align_error_func ( double *  x,
vector< float >  all_ali_params,
int  nima,
int  num_ali,
int  d 
) [static]

Definition at line 19711 of file util_sparx.cpp.

References multi_align_error_func2().

Referenced by multi_align_error().

19711                                                                                                          {
19712 
19713         vector<double> sqr_pixel_error = multi_align_error_func2(x, all_ali_params, nima, num_ali, d);
19714         double sum_sqr_pixel_error = 0.0;
19715         for (int i=0; i<nima; i++)  sum_sqr_pixel_error += sqr_pixel_error[i];
19716         return sum_sqr_pixel_error/static_cast<float>(nima);
19717 }

vector< double > Util::multi_align_error_func2 ( double *  x,
vector< float >  all_ali_params,
int  nima,
int  num_ali,
int  d 
) [static]

Definition at line 19720 of file util_sparx.cpp.

References sqrt(), and var().

Referenced by multi_align_error_func().

19720                                                                                                               {
19721 
19722         double* args = new double[num_ali*3];
19723         for (int i=0; i<3*num_ali-3; i++)   args[i] = x[i];
19724         args[3*num_ali-3] = 0.0;
19725         args[3*num_ali-2] = 0.0;
19726         args[3*num_ali-1] = 0.0;
19727         double* cosa = new double[num_ali];
19728         double* sina = new double[num_ali];
19729         for (int i=0; i<num_ali; i++) {
19730                 cosa[i] = cos(args[i*3]*M_PI/180.0);
19731                 sina[i] = sin(args[i*3]*M_PI/180.0);
19732         }
19733         double* sx = new double[num_ali];
19734         double* sy = new double[num_ali];
19735         
19736         vector<double> sqr_pixel_error(nima);
19737 
19738         for (int i=0; i<nima; i++) {
19739                 double sum_cosa = 0.0;
19740                 double sum_sina = 0.0;
19741                 for (int j=0; j<num_ali; j++) {
19742                         if (static_cast<int>(ali_params[j*nima*4+i*4+3]) == 0) {
19743                                 sum_cosa += cos((args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0);
19744                                 sum_sina += sin((args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0);
19745                                 sx[j] = args[j*3+1] + ali_params[j*nima*4+i*4+1]*cosa[j] - ali_params[j*nima*4+i*4+2]*sina[j];
19746                                 sy[j] = args[j*3+2] + ali_params[j*nima*4+i*4+1]*sina[j] + ali_params[j*nima*4+i*4+2]*cosa[j];
19747                         } else {
19748                                 sum_cosa += cos((-args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0);
19749                                 sum_sina += sin((-args[j*3]+ali_params[j*nima*4+i*4])*M_PI/180.0);
19750                                 sx[j] = -args[j*3+1] + ali_params[j*nima*4+i*4+1]*cosa[j] + ali_params[j*nima*4+i*4+2]*sina[j];
19751                                 sy[j] =  args[j*3+2] - ali_params[j*nima*4+i*4+1]*sina[j] + ali_params[j*nima*4+i*4+2]*cosa[j];
19752                         }
19753                 }
19754                 double P = sqrt(sum_cosa*sum_cosa+sum_sina*sum_sina);
19755                 sum_cosa /= P;
19756                 sum_sina /= P;
19757                 sqr_pixel_error[i] = d*d/4.0*(1.0-P/num_ali)+var(sx, num_ali)+var(sy, num_ali);
19758         }
19759         
19760         delete[] args;
19761         delete[] cosa;
19762         delete[] sina;
19763         delete[] sx;
19764         delete[] sy;
19765         
19766         return sqr_pixel_error;
19767 }

void Util::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 
) [static]

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.

ccf1d keeps 1d ccfs stored as (maxrin, -kx-1:kx+1, -ky-1:ky+1) margin is needed for peak search and both arrays are initialized with -1.0e20

Definition at line 19001 of file util_sparx.cpp.

References Crosrng_msg_vec(), Frngs(), EMAN::EMData::get_data(), Polar2Dm(), and EMAN::EMData::set_size().

19004                                                       {
19005 
19006         int   maxrin = numr[numr.size()-1];
19007 
19008         int   ky = int(2*yrng/step+0.5)/2;
19009         int   kx = int(2*xrng/step+0.5)/2;
19010 
19011         peaks->set_size(maxrin, 2*kx+3, 2*ky+3);
19012         float *p_ccf1ds = peaks->get_data();
19013 
19014         peakm->set_size(maxrin, 2*kx+3, 2*ky+3);
19015         float *p_ccf1dm = peakm->get_data();
19016 
19017         for ( int i = 0; i<maxrin*(2*kx+3)*(2*ky+3); i++) {
19018                 p_ccf1ds[i] = -1.e20f;
19019                 p_ccf1dm[i] = -1.e20f;
19020         }
19021 
19022         for (int i = -ky; i <= ky; i++) {
19023                 float iy = i * step;
19024                 for (int j = -kx; j <= kx; j++) {
19025                         float ix = j*step;
19026                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
19027                         Frngs(cimage, numr);
19028                         Crosrng_msg_vec(crefim, cimage, numr,
19029                           p_ccf1ds+(j+kx+1+((i+ky+1)*(2*kx+3)))*maxrin,
19030                           p_ccf1dm+(j+kx+1+((i+ky+1)*(2*kx+3)))*maxrin);
19031                         delete cimage; cimage = 0;
19032                 }
19033         }
19034         return;
19035 }

void Util::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 
) [static]

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.

ccf1d keeps 1d ccfs stored as (maxrin, -kx-1:kx+1, -ky-1:ky+1) margin is needed for peak search and both arrays are initialized with -1.0e20

Definition at line 19037 of file util_sparx.cpp.

References Crosrng_msg_vec(), Frngs(), EMAN::EMData::get_data(), Polar2Dm(), and EMAN::EMData::set_size().

19039                                                      {
19040 
19041         int   maxrin = numr[numr.size()-1];
19042 
19043         int   ky = int(2*yrng/step+0.5)/2;
19044         int   kx = int(2*xrng/step+0.5)/2;
19045 
19046         peaks->set_size(maxrin, 2*kx+3, 2*ky+3);
19047         float *p_ccf1ds = peaks->get_data();
19048 
19049         peakm->set_size(maxrin, 2*kx+3, 2*ky+3);
19050         float *p_ccf1dm = peakm->get_data();
19051 
19052         peaks_compress->set_size(maxrin, 1, 1);
19053         float *p_ccf1ds_compress = peaks_compress->get_data();
19054 
19055         peakm_compress->set_size(maxrin, 1, 1);
19056         float *p_ccf1dm_compress = peakm_compress->get_data();
19057 
19058         for ( int i = 0; i<maxrin*(2*kx+3)*(2*ky+3); i++) {
19059                 p_ccf1ds[i] = -1.e20f;
19060                 p_ccf1dm[i] = -1.e20f;
19061         }
19062 
19063         for (int i = -ky; i <= ky; i++) {
19064                 float iy = i * step;
19065                 for (int j = -kx; j <= kx; j++) {
19066                         float ix = j*step;
19067                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
19068                         Frngs(cimage, numr);
19069                         Crosrng_msg_vec(crefim, cimage, numr,
19070                           p_ccf1ds+(j+kx+1+((i+ky+1)*(2*kx+3)))*maxrin,
19071                           p_ccf1dm+(j+kx+1+((i+ky+1)*(2*kx+3)))*maxrin);
19072                         delete cimage; cimage = 0;
19073                 }
19074         }
19075         for (int x=0; x<maxrin; x++) {
19076                 float maxs = -1.0e22f;
19077                 float maxm = -1.0e22f;
19078                 for (int i=1; i<=2*ky+1; i++) {
19079                         for (int j=1; j<=2*kx+1; j++) {
19080                                 if (p_ccf1ds[(i*(2*kx+3)+j)*maxrin+x] > maxs) maxs = p_ccf1ds[(i*(2*kx+3)+j)*maxrin+x];
19081                                 if (p_ccf1dm[(i*(2*kx+3)+j)*maxrin+x] > maxm) maxm = p_ccf1dm[(i*(2*kx+3)+j)*maxrin+x];
19082                         }
19083                 }
19084                 p_ccf1ds_compress[x] = maxs;
19085                 p_ccf1dm_compress[x] = maxm;
19086         }
19087         return;
19088 }

vector< float > Util::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 
) [static]

formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation

Definition at line 17847 of file util_sparx.cpp.

References ang_n(), Crosrng_ms(), Frngs(), Normalize_ring(), pi, and Polar2Dm().

17849                                                        {
17850 
17851     // Manually extract.
17852 /*    vector< EMAN::EMData* > crefim;
17853     std::size_t crefim_len = PyObject_Length(crefim_list.ptr());
17854     crefim.reserve(crefim_len);
17855 
17856     for(std::size_t i=0;i<crefim_len;i++) {
17857         boost::python::extract<EMAN::EMData*> proxy(crefim_list[i]);
17858         crefim.push_back(proxy());
17859     }
17860 */
17861 
17862         size_t crefim_len = crefim.size();
17863 
17864         int   ky = int(2*yrng/step+0.5)/2;
17865         int   kx = int(2*xrng/step+0.5)/2;
17866         int   iref, nref=0, mirror=0;
17867         float iy, ix, sx=0, sy=0;
17868         float peak = -1.0E23f;
17869         float ang=0.0f;
17870         for (int i = -ky; i <= ky; i++) {
17871                 iy = i * step ;
17872                 for (int j = -kx; j <= kx; j++) {
17873                         ix = j*step ;
17874                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
17875 
17876                         Normalize_ring( cimage, numr );
17877 
17878                         Frngs(cimage, numr);
17879                         //  compare with all reference images
17880                         // for iref in xrange(len(crefim)):
17881                         for ( iref = 0; iref < (int)crefim_len; iref++) {
17882                                 Dict retvals = Crosrng_ms(crefim[iref], cimage, numr);
17883                                 double qn = retvals["qn"];
17884                                 double qm = retvals["qm"];
17885                                 if(qn >= peak || qm >= peak) {
17886                                         sx = -ix;
17887                                         sy = -iy;
17888                                         nref = iref;
17889                                         if (qn >= qm) {
17890                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
17891                                                 peak = static_cast<float>(qn);
17892                                                 mirror = 0;
17893                                         } else {
17894                                                 ang = ang_n(retvals["tmt"], mode, numr[numr.size()-1]);
17895                                                 peak = static_cast<float>(qm);
17896                                                 mirror = 1;
17897                                         }
17898                                 }
17899                         }  delete cimage; cimage = 0;
17900                 }
17901         }
17902         float co, so, sxs, sys;
17903         co = static_cast<float>( cos(ang*pi/180.0) );
17904         so = static_cast<float>( -sin(ang*pi/180.0) );
17905         sxs = sx*co - sy*so;
17906         sys = sx*so + sy*co;
17907         vector<float> res;
17908         res.push_back(ang);
17909         res.push_back(sxs);
17910         res.push_back(sys);
17911         res.push_back(static_cast<float>(mirror));
17912         res.push_back(static_cast<float>(nref));
17913         res.push_back(peak);
17914         return res;
17915 }

vector< float > Util::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 
) [static]

formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation

Definition at line 18065 of file util_sparx.cpp.

References ang_n(), Crosrng_ms_delta(), Frngs(), Normalize_ring(), pi, and Polar2Dm().

18067                                                                                        {
18068 
18069     // Manually extract.
18070 /*    vector< EMAN::EMData* > crefim;
18071     std::size_t crefim_len = PyObject_Length(crefim_list.ptr());
18072     crefim.reserve(crefim_len);
18073 
18074     for(std::size_t i=0;i<crefim_len;i++) {
18075         boost::python::extract<EMAN::EMData*> proxy(crefim_list[i]);
18076         crefim.push_back(proxy());
18077     }
18078 */
18079 
18080         size_t crefim_len = crefim.size();
18081 
18082         int   ky = int(2*yrng/step+0.5)/2;
18083         int   kx = int(2*xrng/step+0.5)/2;
18084         int   iref, nref=0, mirror=0;
18085         float iy, ix, sx=0, sy=0;
18086         float peak = -1.0E23f;
18087         float ang=0.0f;
18088         for (int i = -ky; i <= ky; i++) {
18089                 iy = i * step ;
18090                 for (int j = -kx; j <= kx; j++) {
18091                         ix = j*step ;
18092                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18093 
18094                         Normalize_ring( cimage, numr );
18095 
18096                         Frngs(cimage, numr);
18097                         //  compare with all reference images
18098                         // for iref in xrange(len(crefim)):
18099                         for ( iref = 0; iref < (int)crefim_len; iref++) {
18100                                 Dict retvals = Crosrng_ms_delta(crefim[iref], cimage, numr, delta_start, delta);
18101                                 double qn = retvals["qn"];
18102                                 double qm = retvals["qm"];
18103                                 if(qn >= peak || qm >= peak) {
18104                                         sx = -ix;
18105                                         sy = -iy;
18106                                         nref = iref;
18107                                         if (qn >= qm) {
18108                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18109                                                 peak = static_cast<float>(qn);
18110                                                 mirror = 0;
18111                                         } else {
18112                                                 ang = ang_n(retvals["tmt"], mode, numr[numr.size()-1]);
18113                                                 peak = static_cast<float>(qm);
18114                                                 mirror = 1;
18115                                         }
18116                                 }
18117                         }  delete cimage; cimage = 0;
18118                 }
18119         }
18120         float co, so, sxs, sys;
18121         co = static_cast<float>( cos(ang*pi/180.0) );
18122         so = static_cast<float>( -sin(ang*pi/180.0) );
18123         sxs = sx*co - sy*so;
18124         sys = sx*so + sy*co;
18125         vector<float> res;
18126         res.push_back(ang);
18127         res.push_back(sxs);
18128         res.push_back(sys);
18129         res.push_back(static_cast<float>(mirror));
18130         res.push_back(static_cast<float>(nref));
18131         res.push_back(peak);
18132         return res;
18133 }

vector< float > Util::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 
) [static]

formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation

Definition at line 18192 of file util_sparx.cpp.

References abs, ang_n(), Crosrng_ms(), Frngs(), get_attr(), EMAN::EMData::get_attr(), Normalize_ring(), phi, pi, Polar2Dm(), t, and theta.

18194                                                        {
18195 
18196     // Manually extract.
18197 /*    vector< EMAN::EMData* > crefim;
18198     std::size_t crefim_len = PyObject_Length(crefim_list.ptr());
18199     crefim.reserve(crefim_len);
18200 
18201     for(std::size_t i=0;i<crefim_len;i++) {
18202         boost::python::extract<EMAN::EMData*> proxy(crefim_list[i]);
18203         crefim.push_back(proxy());
18204     }
18205 */
18206         size_t crefim_len = crefim.size();
18207         const float qv = static_cast<float>( pi/180.0 );
18208 
18209         Transform * t = image->get_attr("xform.projection");
18210         Dict d = t->get_params("spider");
18211         if(t) {delete t; t=0;}
18212         float phi = d["phi"];
18213         float theta = d["theta"];
18214         int   ky = int(2*yrng/step+0.5)/2;
18215         int   kx = int(2*xrng/step+0.5)/2;
18216         int   iref, nref=0, mirror=0;
18217         float iy, ix, sx=0, sy=0;
18218         float peak = -1.0E23f;
18219         float ang=0.0f;
18220         float imn1 = sin(theta*qv)*cos(phi*qv);
18221         float imn2 = sin(theta*qv)*sin(phi*qv);
18222         float imn3 = cos(theta*qv);
18223         vector<float> n1(crefim_len);
18224         vector<float> n2(crefim_len);
18225         vector<float> n3(crefim_len);
18226         for ( iref = 0; iref < (int)crefim_len; iref++) {
18227                         n1[iref] = crefim[iref]->get_attr("n1");
18228                         n2[iref] = crefim[iref]->get_attr("n2");
18229                         n3[iref] = crefim[iref]->get_attr("n3");
18230         }
18231         for (int i = -ky; i <= ky; i++) {
18232             iy = i * step ;
18233             for (int j = -kx; j <= kx; j++) {
18234                 ix = j*step;
18235                 EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18236 
18237                 Normalize_ring( cimage, numr );
18238 
18239                 Frngs(cimage, numr);
18240                 //  compare with all reference images
18241                 // for iref in xrange(len(crefim)):
18242                 for ( iref = 0; iref < (int)crefim_len; iref++) {
18243                         if(abs(n1[iref]*imn1 + n2[iref]*imn2 + n3[iref]*imn3)>=ant) {
18244                                 Dict retvals = Crosrng_ms(crefim[iref], cimage, numr);
18245                                 double qn = retvals["qn"];
18246                                 double qm = retvals["qm"];
18247                                 if(qn >= peak || qm >= peak) {
18248                                         sx = -ix;
18249                                         sy = -iy;
18250                                         nref = iref;
18251                                         if (qn >= qm) {
18252                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18253                                                 peak = static_cast<float>( qn );
18254                                                 mirror = 0;
18255                                         } else {
18256                                                 ang = ang_n(retvals["tmt"], mode, numr[numr.size()-1]);
18257                                                 peak = static_cast<float>( qm );
18258                                                 mirror = 1;
18259                                         }
18260                                 }
18261                         }
18262                 }  delete cimage; cimage = 0;
18263             }
18264         }
18265         float co, so, sxs, sys;
18266         if(peak == -1.0E23) {
18267                 ang=0.0; sxs=0.0; sys=0.0; mirror=0;
18268                 nref = -1;
18269         } else {
18270                 co =  cos(ang*qv);
18271                 so = -sin(ang*qv);
18272                 sxs = sx*co - sy*so;
18273                 sys = sx*so + sy*co;
18274         }
18275         vector<float> res;
18276         res.push_back(ang);
18277         res.push_back(sxs);
18278         res.push_back(sys);
18279         res.push_back(static_cast<float>(mirror));
18280         res.push_back(static_cast<float>(nref));
18281         res.push_back(peak);
18282         return res;
18283 }

vector< float > Util::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 
) [static]

formerly known as apmq Determine shift and rotation between image and many reference images (crefim, weights have to be applied) quadratic interpolation

Definition at line 18285 of file util_sparx.cpp.

References abs, ang_n(), Crosrng_sm_psi(), Frngs(), get_attr(), EMAN::EMData::get_attr(), Normalize_ring(), phi, pi, Polar2Dm(), EMAN::Dict::size(), t, and theta.

18287                                                        {
18288 
18289     // Manually extract.
18290 /*    vector< EMAN::EMData* > crefim;
18291     std::size_t crefim_len = PyObject_Length(crefim_list.ptr());
18292     crefim.reserve(crefim_len);
18293 
18294     for(std::size_t i=0;i<crefim_len;i++) {
18295         boost::python::extract<EMAN::EMData*> proxy(crefim_list[i]);
18296         crefim.push_back(proxy());
18297     }
18298 */
18299         size_t crefim_len = crefim.size();
18300         const float qv = static_cast<float>(pi/180.0);
18301 
18302         Transform* t = image->get_attr("xform.projection");
18303         Dict d = t->get_params("spider");
18304         if(t) {delete t; t=0;}
18305         float phi = d["phi"];
18306         float theta = d["theta"];
18307         float psi = d["psi"];
18308         int ky = int(2*yrng/step+0.5)/2;
18309         int kx = int(2*xrng/step+0.5)/2;
18310         int iref, nref = 0, mirror = 0;
18311         float iy, ix, sx = 0, sy = 0;
18312         float peak = -1.0E23f;
18313         float ang = 0.0f;
18314         float imn1 = sin(theta*qv)*cos(phi*qv);
18315         float imn2 = sin(theta*qv)*sin(phi*qv);
18316         float imn3 = cos(theta*qv);
18317         vector<float> n1(crefim_len);
18318         vector<float> n2(crefim_len);
18319         vector<float> n3(crefim_len);
18320         for (iref = 0; iref < (int)crefim_len; iref++) {
18321                         n1[iref] = crefim[iref]->get_attr("n1");
18322                         n2[iref] = crefim[iref]->get_attr("n2");
18323                         n3[iref] = crefim[iref]->get_attr("n3");
18324         }
18325         bool nomirror = (theta<90.0) || ((theta==90.0) && (psi<psi_max));
18326         if (!nomirror) {
18327                 phi = fmod(phi+540.0f, 360.0f);
18328                 theta = 180-theta;
18329                 psi = fmod(540.0f-psi, 360.0f);
18330         }
18331         for (int i = -ky; i <= ky; i++) {
18332             iy = i * step ;
18333             for (int j = -kx; j <= kx; j++) {
18334                 ix = j*step;
18335                 EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18336 
18337                 Normalize_ring(cimage, numr);
18338 
18339                 Frngs(cimage, numr);
18340                 //  compare with all reference images
18341                 // for iref in xrange(len(crefim)):
18342                 for (iref = 0; iref < (int)crefim_len; iref++) {
18343                         if (abs(n1[iref]*imn1 + n2[iref]*imn2 + n3[iref]*imn3)>=ant) {
18344                                 if (nomirror) {
18345                                         Dict retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, psi, 0, psi_max);
18346                                         double qn = retvals["qn"];
18347                                         if (qn >= peak) {
18348                                                 sx = -ix;
18349                                                 sy = -iy;
18350                                                 nref = iref;
18351                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18352                                                 peak = static_cast<float>(qn);
18353                                                 mirror = 0;
18354                                         }
18355                                 } else {
18356                                         Dict retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, psi, 1, psi_max);
18357                                         double qn = retvals["qn"];
18358                                         if (qn >= peak) {
18359                                                 sx = -ix;
18360                                                 sy = -iy;
18361                                                 nref = iref;
18362                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18363                                                 peak = static_cast<float>(qn);
18364                                                 mirror = 1;
18365                                         }
18366                                 }
18367                         }
18368                 }  delete cimage; cimage = 0;
18369             }
18370         }
18371         float co, so, sxs, sys;
18372         if(peak == -1.0E23) {
18373                 ang=0.0; sxs=0.0; sys=0.0; mirror=0;
18374                 nref = -1;
18375         } else {
18376                 co =  cos(ang*qv);
18377                 so = -sin(ang*qv);
18378                 sxs = sx*co - sy*so;
18379                 sys = sx*so + sy*co;
18380         }
18381         vector<float> res;
18382         res.push_back(ang);
18383         res.push_back(sxs);
18384         res.push_back(sys);
18385         res.push_back(static_cast<float>(mirror));
18386         res.push_back(static_cast<float>(nref));
18387         res.push_back(peak);
18388         return res;
18389 }

vector< float > Util::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 
) [static]

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

Definition at line 18135 of file util_sparx.cpp.

References ang_n(), Crosrng_ns(), Frngs(), pi, and Polar2Dm().

18137                                                          {
18138 
18139     // Manually extract.
18140 /*    vector< EMAN::EMData* > crefim;
18141     std::size_t crefim_len = PyObject_Length(crefim_list.ptr());
18142     crefim.reserve(crefim_len);
18143 
18144     for(std::size_t i=0;i<crefim_len;i++) {
18145         boost::python::extract<EMAN::EMData*> proxy(crefim_list[i]);
18146         crefim.push_back(proxy());
18147     }
18148 */
18149         size_t crefim_len = crefim.size();
18150 
18151         int   ky = int(2*yrng/step+0.5)/2;
18152         int   kx = int(2*xrng/step+0.5)/2;
18153         int   iref, nref=0;
18154         float iy, ix, sx=0, sy=0;
18155         float peak = -1.0E23f;
18156         float ang=0.0f;
18157         for (int i = -ky; i <= ky; i++) {
18158                 iy = i * step ;
18159                 for (int j = -kx; j <= kx; j++) {
18160                         ix = j*step ;
18161                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18162                         Frngs(cimage, numr);
18163                         //  compare with all reference images
18164                         // for iref in xrange(len(crefim)):
18165                         for ( iref = 0; iref < (int)crefim_len; iref++) {
18166                                 Dict retvals = Crosrng_ns(crefim[iref], cimage, numr);
18167                                 double qn = retvals["qn"];
18168                                 if(qn >= peak) {
18169                                         sx = -ix;
18170                                         sy = -iy;
18171                                         nref = iref;
18172                                         ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18173                                         peak = static_cast<float>(qn);
18174                                 }
18175                         }  delete cimage; cimage = 0;
18176                 }
18177         }
18178         float co, so, sxs, sys;
18179         co = static_cast<float>( cos(ang*pi/180.0) );
18180         so = static_cast<float>( -sin(ang*pi/180.0) );
18181         sxs = sx*co - sy*so;
18182         sys = sx*so + sy*co;
18183         vector<float> res;
18184         res.push_back(ang);
18185         res.push_back(sxs);
18186         res.push_back(sys);
18187         res.push_back(static_cast<float>(nref));
18188         res.push_back(peak);
18189         return res;
18190 }

vector< float > Util::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]

Definition at line 17917 of file util_sparx.cpp.

References ang_n(), Crosrng_ms(), Frngs(), Normalize_ring(), pi, and Polar2Dm().

17919                                                        {
17920 
17921         size_t crefim_len = crefim.size();
17922 
17923         int   ky = int(2*yrng/step+0.5)/2;
17924         int   kx = int(2*xrng/step+0.5)/2;
17925         float iy, ix;
17926         vector<float> peak(crefim_len*5, -1.0e23f);
17927         for (int i = -ky; i <= ky; i++) {
17928                 iy = i * step ;
17929                 for (int j = -kx; j <= kx; j++) {
17930                         ix = j*step ;
17931                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
17932                         Normalize_ring( cimage, numr );
17933                         Frngs(cimage, numr);
17934                         for (int iref = 0; iref < (int)crefim_len; iref++) {
17935                                 Dict retvals = Crosrng_ms(crefim[iref], cimage, numr);
17936                                 double qn = retvals["qn"];
17937                                 double qm = retvals["qm"];
17938                                 if(qn >= peak[iref*5] || qm >= peak[iref*5]) {
17939                                         if (qn >= qm) {
17940                                                 peak[iref*5] = static_cast<float>(qn);
17941                                                 peak[iref*5+1] = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
17942                                                 peak[iref*5+2] = -ix;
17943                                                 peak[iref*5+3] = -iy;
17944                                                 peak[iref*5+4] = 0;
17945                                         } else {
17946                                                 peak[iref*5] = static_cast<float>(qm);
17947                                                 peak[iref*5+1] = ang_n(retvals["tmt"], mode, numr[numr.size()-1]);
17948                                                 peak[iref*5+2] = -ix;
17949                                                 peak[iref*5+3] = -iy;
17950                                                 peak[iref*5+4] = 1;
17951                                         }
17952                                 }
17953                         }  delete cimage; cimage = 0;
17954                 }
17955         }
17956         for (int iref = 0; iref < (int)crefim_len; iref++) {
17957                 float ang = peak[iref*5+1];
17958                 float sx = peak[iref*5+2];
17959                 float sy = peak[iref*5+3];
17960                 float co =  cos(ang*pi/180.0);
17961                 float so = -sin(ang*pi/180.0);
17962                 float sxs = sx*co - sy*so;
17963                 float sys = sx*so + sy*co;
17964                 peak[iref*5+2] = sxs;
17965                 peak[iref*5+3] = sys;
17966         }
17967         return peak;
17968 }

vector< float > Util::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 
) [static]

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)

Definition at line 18392 of file util_sparx.cpp.

References ang_n(), Crosrng_psi(), Frngs(), Normalize_ring(), pi, Polar2Dm(), and EMAN::Dict::size().

18394                                                                     {
18395         
18396         size_t crefim_len = crefim.size();
18397 
18398         int   iref, nref=0, mirror=0;
18399         float iy, ix, sx=0, sy=0;
18400         float peak = -1.0E23f;
18401         float ang=0.0f;
18402         int   kx = int(2*xrng/step+0.5)/2;
18403         //if ynumber==-1, use the old code which process x and y direction equally.
18404         //if ynumber is given, it should be even. We need to check whether it is zero
18405 
18406         int ky;
18407         float stepy;
18408         int kystart;
18409         
18410         if (ynumber == -1){
18411             ky = int(2*yrng/step+0.5)/2;
18412             stepy = step;
18413             kystart = -ky;
18414         }
18415         else if(ynumber == 0){
18416              ky = 0;
18417                  stepy = 0.0f;
18418                  kystart = ky;
18419         }
18420         else {
18421             ky = int(ynumber/2);                
18422                 stepy=2*yrng/ynumber;
18423                 kystart = -ky + 1;    
18424         }
18425         //std::cout<<"yrng="<<yrng<<"ynumber="<<ynumber<<"stepy=="<<stepy<<"stepx=="<<step<<std::endl;
18426         for (int i = kystart; i <= ky; i++) {
18427                 iy = i * stepy ;
18428                 for (int j = -kx; j <= kx; j++) {
18429                         ix = j*step ;
18430                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18431 
18432                         Normalize_ring( cimage, numr );
18433 
18434                         Frngs(cimage, numr);
18435                         //  compare with all reference images
18436                         // for iref in xrange(len(crefim)):
18437                         for ( iref = 0; iref < (int)crefim_len; iref++) {
18438                                 Dict retvals_0 = Crosrng_psi(crefim[iref], cimage, numr, 0, psi_max);
18439                                 Dict retvals_180 = Crosrng_psi(crefim[iref], cimage, numr, 180, psi_max);
18440                                 double qn_0 = retvals_0["qn"];
18441                                 double qn_180 = retvals_180["qn"];
18442                                 double qm_0 = retvals_0["qm"];
18443                                 double qm_180 = retvals_180["qm"];
18444                                 double qn;
18445                                 double qm;
18446                                 bool qn_is_zero = false;
18447                                 bool qm_is_zero = false;
18448                                 
18449                                 if (qn_0 >= qn_180){
18450                                         qn = qn_0;
18451                                         qn_is_zero = true;
18452                                 }
18453                                 else{
18454                                         qn = qn_180;
18455                                         qn_is_zero = false; 
18456                                 }
18457                                         
18458                                 if (qm_0 >= qm_180){
18459                                         qm = qm_0;
18460                                         qm_is_zero = true;
18461                                 }
18462                                 else{
18463                                         qm = qm_180;
18464                                         qm_is_zero = false; 
18465                                 }
18466                                         
18467                                 if(qn >= peak || qm >= peak) {
18468                                         sx = -ix;
18469                                         sy = -iy;
18470                                         nref = iref;
18471                                         if (qn >= qm) {
18472                                                 if (qn_is_zero){
18473                                                         ang = ang_n(retvals_0["tot"], mode, numr[numr.size()-1]);
18474                                                 }
18475                                                 else{
18476                                                         ang = ang_n(retvals_180["tot"], mode, numr[numr.size()-1]);
18477                                                 }
18478                                                 peak = static_cast<float>(qn);
18479                                                 mirror = 0;
18480                                         } else {
18481                                                 if (qm_is_zero){
18482                                                         ang = ang_n(retvals_0["tmt"], mode, numr[numr.size()-1]);
18483                                                 }
18484                                                 else{
18485                                                         ang = ang_n(retvals_180["tmt"], mode, numr[numr.size()-1]);
18486                                                 }
18487                                                 peak = static_cast<float>(qm);
18488                                                 mirror = 1;
18489                                         }
18490                                 }
18491                         }
18492                         delete cimage; cimage = 0;
18493                 }
18494         }
18495         float co, so, sxs, sys;
18496         co = static_cast<float>( cos(ang*pi/180.0) );
18497         so = static_cast<float>( -sin(ang*pi/180.0) );
18498         sxs = sx*co - sy*so;
18499         sys = sx*so + sy*co;
18500         vector<float> res;
18501         res.push_back(ang);
18502         res.push_back(sxs);
18503         res.push_back(sys);
18504         res.push_back(static_cast<float>(mirror));
18505         res.push_back(static_cast<float>(nref));
18506         res.push_back(peak);
18507         return res;
18508 }

vector< float > Util::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]

Definition at line 18766 of file util_sparx.cpp.

References ang_n(), Crosrng_sm_psi(), Frngs(), Normalize_ring(), pi, Polar2Dm(), and EMAN::Dict::size().

18768                                                                     {
18769 
18770         size_t crefim_len = crefim.size();
18771 
18772         int   iref, nref=0, mirror=0;
18773         float iy, ix, sx=0, sy=0;
18774         float peak = -1.0E23f;
18775         float ang=0.0f;
18776         int   kx = int(2*xrng/step+0.5)/2;
18777         //if ynumber==-1, use the old code which process x and y direction equally.
18778         
18779         int ky;
18780         float stepy;
18781         int kystart;
18782         
18783         if (ynumber == -1){
18784             ky = int(2*yrng/step+0.5)/2;
18785             stepy = step;
18786             kystart = -ky;
18787         }
18788         else if(ynumber == 0){
18789              ky = 0;
18790                  stepy = 0.0f;
18791                  kystart = ky;
18792         }
18793         else {
18794             ky = int(ynumber/2);                
18795                 stepy=2*yrng/ynumber;
18796                 kystart = -ky + 1;    
18797         }
18798         
18799                 //std::cout<<"yrng="<<yrng<<"ynumber="<<ynumber<<"stepy=="<<stepy<<"stepx=="<<step<<std::endl;
18800         for (int i = kystart; i <= ky; i++) {
18801                 iy = i * stepy ;
18802                 for (int j = -kx; j <= kx; j++) {
18803                         ix = j*step ;
18804                         EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18805 
18806                         Normalize_ring( cimage, numr );
18807 
18808                         Frngs(cimage, numr);
18809                         //  compare with all reference images
18810                         // for iref in xrange(len(crefim)):
18811                         for ( iref = 0; iref < (int)crefim_len; iref++) {
18812                                 Dict retvals_0 = Crosrng_sm_psi(crefim[iref], cimage, numr, 0, 0, psi_max);
18813                                 Dict retvals_180 = Crosrng_sm_psi(crefim[iref], cimage, numr, 180, 0, psi_max);
18814                                 double qn_0 = retvals_0["qn"];
18815                                 double qn_180 = retvals_180["qn"];
18816                                 double qn;
18817                                 bool qn_is_zero = false;
18818                                 
18819                                 if (qn_0 >= qn_180){
18820                                         qn = qn_0;
18821                                         qn_is_zero = true;
18822                                 }
18823                                 else{
18824                                         qn = qn_180;
18825                                         qn_is_zero = false; 
18826                                 }
18827                                         
18828                                 if(qn >= peak) {
18829                                         sx = -ix;
18830                                         sy = -iy;
18831                                         nref = iref;
18832                                         
18833                                         if (qn_is_zero){
18834                                                 ang = ang_n(retvals_0["tot"], mode, numr[numr.size()-1]);
18835                                         }
18836                                         else{
18837                                                 ang = ang_n(retvals_180["tot"], mode, numr[numr.size()-1]);
18838                                         }
18839                                         peak = static_cast<float>(qn);
18840                                         mirror = 0;
18841                                          
18842                                 }
18843                         }
18844                         delete cimage; cimage = 0;
18845                 }
18846         }       
18847         float co, so, sxs, sys;
18848         co = static_cast<float>( cos(ang*pi/180.0) );
18849         so = static_cast<float>( -sin(ang*pi/180.0) );
18850         sxs = sx*co - sy*so;
18851         sys = sx*so + sy*co;
18852         vector<float> res;
18853         res.push_back(ang);
18854         res.push_back(sxs);
18855         res.push_back(sys);
18856         res.push_back(static_cast<float>(mirror));
18857         res.push_back(static_cast<float>(nref));
18858         res.push_back(peak);
18859         return res;
18860 }

vector< float > Util::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 
) [static]

Definition at line 18863 of file util_sparx.cpp.

References ang_n(), Crosrng_psi_0_180_no_mirror(), Frngs(), get_attr(), EMAN::EMData::get_attr(), Normalize_ring(), phi, pi, Polar2Dm(), t, and theta.

18865                                                                     {
18866 
18867         size_t crefim_len = crefim.size();
18868         const float qv = static_cast<float>( pi/180.0 );
18869         Transform * t = image->get_attr("xform.projection");
18870         Dict d = t->get_params("spider");
18871         if(t) {delete t; t=0;}
18872         float phi = d["phi"];
18873         float theta = d["theta"];
18874         float imn1 = sin(theta*qv)*cos(phi*qv);
18875         float imn2 = sin(theta*qv)*sin(phi*qv);
18876         float imn3 = cos(theta*qv);
18877         vector<float> n1(crefim_len);
18878         vector<float> n2(crefim_len);
18879         vector<float> n3(crefim_len);
18880         int   iref, nref=-1, mirror=0;
18881         float iy, ix, sx=0, sy=0;
18882         float peak = -1.0E23f;
18883         float ang=0.0f;
18884         int   kx = int(2*xrng/step+0.5)/2;
18885         
18886         for ( iref = 0; iref < (int)crefim_len; iref++) {
18887                 n1[iref] = crefim[iref]->get_attr("n1");
18888                 n2[iref] = crefim[iref]->get_attr("n2");
18889                 n3[iref] = crefim[iref]->get_attr("n3");
18890         }
18891         
18892         //if ynumber==-1, use the old code which process x and y direction equally.
18893         if(ynumber==-1) {
18894                 int   ky = int(2*yrng/step+0.5)/2;
18895                 for (int i = -ky; i <= ky; i++) {
18896                         iy = i * step ;
18897                         for (int j = -kx; j <= kx; j++)  {
18898                                 ix = j*step ;
18899                                 EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18900 
18901                                 Normalize_ring( cimage, numr );
18902 
18903                                 Frngs(cimage, numr);
18904                                 //  compare with all reference images
18905                                 // for iref in xrange(len(crefim)):
18906                                 for ( iref = 0; iref < (int)crefim_len; iref++) {
18907                                         if(fabs(n1[iref]*imn1 + n2[iref]*imn2 + n3[iref]*imn3)>=ant) {
18908                                                 Dict retvals = Crosrng_psi_0_180_no_mirror(crefim[iref], cimage, numr, psi_max);
18909                                                 double qn = retvals["qn"];
18910                                                 if( qn >= peak) {
18911                                                         sx = -ix;
18912                                                         sy = -iy;
18913                                                         nref = iref;
18914                                                         ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18915                                                         peak = static_cast<float>(qn);
18916                                                         mirror = 0;
18917                                                 }
18918                                         }
18919                                 }  
18920                                 delete cimage; cimage = 0;
18921                         }
18922                    }
18923         }
18924         //if ynumber is given, it should be even. We need to check whether it is zero
18925         else if(ynumber==0) {
18926                 sy = 0.0f;
18927                 for (int j = -kx; j <= kx; j++) {
18928                         ix = j*step ;
18929                         iy = 0.0f ;
18930                         EMData* cimage = Polar2Dm(image, cnx+ix, cny, numr, mode);
18931 
18932                         Normalize_ring( cimage, numr );
18933 
18934                         Frngs(cimage, numr);
18935                         //  compare with all reference images
18936                         // for iref in xrange(len(crefim)):
18937                         for ( iref = 0; iref < (int)crefim_len; iref++)  {
18938                                 if(fabs(n1[iref]*imn1 + n2[iref]*imn2 + n3[iref]*imn3)>=ant) {
18939                                         Dict retvals = Crosrng_psi_0_180_no_mirror(crefim[iref], cimage, numr, psi_max);
18940                                         double qn = retvals["qn"];
18941                                         if( qn >= peak ) {
18942                                                 sx = -ix;
18943                                                 nref = iref;
18944                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18945                                                 peak = static_cast<float>(qn);
18946                                                 mirror = 0;
18947                                         }
18948                                 }
18949                         } 
18950                         delete cimage; cimage = 0;
18951                 }                       
18952         } else {
18953                 int   ky = int(ynumber/2);              
18954                 float stepy=2*yrng/ynumber;
18955                 //std::cout<<"yrng="<<yrng<<"ynumber="<<ynumber<<"stepy=="<<stepy<<"stepx=="<<step<<std::endl;
18956                 for (int i = -ky+1; i <= ky; i++) {
18957                         iy = i * stepy ;
18958                         for (int j = -kx; j <= kx; j++) {
18959                                 ix = j*step ;
18960                                 EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18961 
18962                                 Normalize_ring( cimage, numr );
18963 
18964                                 Frngs(cimage, numr);
18965                                 //  compare with all reference images
18966                                 // for iref in xrange(len(crefim)):
18967                                 for ( iref = 0; iref < (int)crefim_len; iref++) {
18968                                         if(fabs(n1[iref]*imn1 + n2[iref]*imn2 + n3[iref]*imn3)>=ant) {
18969                                                 Dict retvals = Crosrng_psi_0_180_no_mirror(crefim[iref], cimage, numr, psi_max);
18970                                                 double qn = retvals["qn"];
18971                                                 if( qn >= peak) {
18972                                                         sx = -ix;
18973                                                         sy = -iy;
18974                                                         nref = iref;
18975                                                         ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18976                                                         peak = static_cast<float>(qn);
18977                                                         mirror = 0;
18978                                                 }
18979                                         }
18980                                 }
18981                                 delete cimage; cimage = 0;
18982                         }
18983                 }
18984         }
18985         float co, so, sxs, sys;
18986         co = static_cast<float>( cos(ang*pi/180.0) );
18987         so = static_cast<float>( -sin(ang*pi/180.0) );
18988         sxs = sx*co - sy*so;
18989         sys = sx*so + sy*co;
18990         vector<float> res;
18991         res.push_back(ang);
18992         res.push_back(sxs);
18993         res.push_back(sys);
18994         res.push_back(static_cast<float>(mirror));
18995         res.push_back(static_cast<float>(nref));
18996         res.push_back(peak);
18997         return res;
18998 }

vector< float > Util::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,
bool  mirror_only = false,
float  yrnglocal = -1.0 
) [static]

Definition at line 18510 of file util_sparx.cpp.

References ang_n(), Crosrng_sm_psi(), Frngs(), get_attr(), EMAN::EMData::get_attr(), Normalize_ring(), phi, pi, Polar2Dm(), t, and theta.

18512                                                                                                        {
18513         //std::cout<<"multiref_polar_ali_helical_local_jia called"<<std::endl;
18514         size_t crefim_len = crefim.size();
18515 
18516         int   iref, nref=-1, mirror=0;
18517         float iy, ix, sx=0, sy=0;
18518         float peak = -1.0E23f;
18519         float ang=0.0f;
18520         const float qv = static_cast<float>( pi/180.0 );
18521         Transform * t = image->get_attr("xform.projection");
18522         Dict d = t->get_params("spider");
18523         if(t) {delete t; t=0;}
18524         float phi = d["phi"];
18525         float theta = d["theta"];
18526         float psi = d["psi"];
18527         float imn1 = sin(theta*qv)*cos(phi*qv);
18528         float imn2 = sin(theta*qv)*sin(phi*qv);
18529         float imn3 = cos(theta*qv);
18530         vector<float> n1(crefim_len);
18531         vector<float> n2(crefim_len);
18532         vector<float> n3(crefim_len);
18533         for ( iref = 0; iref < (int)crefim_len; iref++) {
18534                         n1[iref] = crefim[iref]->get_attr("n1");
18535                         n2[iref] = crefim[iref]->get_attr("n2");
18536                         n3[iref] = crefim[iref]->get_attr("n3");
18537         }
18538         float nbrinp;
18539         bool use_ref;
18540         int   kx = int(2*xrng/step+0.5)/2;
18541         
18542         //if ynumber==-1, use the old code which process x and y direction equally.
18543         if(ynumber==-1) {
18544                 int   ky = int(2*yrng/step+0.5)/2;
18545                 for (int i = -ky; i <= ky; i++) {
18546                         iy = i * step ;
18547                         for (int j = -kx; j <= kx; j++)  {
18548                                 ix = j*step ;
18549                                 EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18550 
18551                                 Normalize_ring( cimage, numr );
18552 
18553                                 Frngs(cimage, numr);
18554                                 //  compare with all reference images
18555                                 // for iref in xrange(len(crefim)):
18556                                 for ( iref = 0; iref < (int)crefim_len; iref++) {
18557                                         
18558                                         use_ref = false;
18559                                         if (!mirror_only){
18560                                                 // inner product of iref's Eulerian angles with that of the data
18561                                                 nbrinp = n1[iref]*imn1 + n2[iref]*imn2 + n3[iref]*imn3;
18562                                                 if (nbrinp >= ant){
18563                                                         use_ref = true;
18564                                                 }
18565                                         }
18566                                         else if (mirror_only) {
18567                                                 // inner product of the mirror of iref's Eulerian angles with that of the data
18568                                                 nbrinp = (-1.0*n1[iref]*imn1) + (-1.0*n2[iref]*imn2) + n3[iref]*imn3;
18569                                                 if (nbrinp >= ant){
18570                                                         use_ref = true;
18571                                                 }
18572                                         }
18573                                         
18574                                         
18575                                         if(use_ref) {
18576                                                 Dict retvals;
18577                                                 if (mirror_only == true){
18578                                                     if ((psi-90) < 90)  
18579                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 0, 1, psi_max);
18580                                                     else
18581                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 180, 1, psi_max); 
18582                                                 }       
18583                                                 else{ 
18584                                                     if ((psi-90) < 90)  
18585                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 0, 0, psi_max);
18586                                                     else
18587                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 180, 0, psi_max);
18588                                                 }   
18589                                                 double qn = retvals["qn"];
18590                                                 
18591                                                 if(qn >= peak) {
18592                                                         sx = -ix;
18593                                                         sy = -iy;
18594                                                         nref = iref;
18595                                                         if (!mirror_only) {
18596                                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18597                                                                 peak = static_cast<float>(qn);
18598                                                                 mirror = 0;
18599                                                         } else {
18600                                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18601                                                                 peak = static_cast<float>(qn);
18602                                                                 mirror = 1;
18603                                                         }
18604                                                 }
18605                                         }
18606                                 }  
18607                                 delete cimage; cimage = 0;
18608                         }
18609                    }
18610         }
18611         //if ynumber is given, it should be even. We need to check whether it is zero
18612         else if(ynumber==0) {
18613                 sy = 0.0f;
18614                 for (int j = -kx; j <= kx; j++) {
18615                         ix = j*step ;
18616                         EMData* cimage = Polar2Dm(image, cnx+ix, cny, numr, mode);
18617 
18618                         Normalize_ring( cimage, numr );
18619 
18620                         Frngs(cimage, numr);
18621                         //  compare with all reference images
18622                         // for iref in xrange(len(crefim)):
18623                         for ( iref = 0; iref < (int)crefim_len; iref++)  {
18624                                 
18625                                 use_ref = false;
18626                                 if (!mirror_only){
18627                                         // inner product of iref's Eulerian angles with that of the data
18628                                         nbrinp = n1[iref]*imn1 + n2[iref]*imn2 + n3[iref]*imn3;
18629                                         if (nbrinp >= ant){
18630                                                 use_ref = true;
18631                                         }
18632                                 }
18633                                 else if (mirror_only) {
18634                                         // inner product of the mirror of iref's Eulerian angles with that of the data
18635                                         nbrinp = (-1.0f*n1[iref]*imn1) + (-1.0f*n2[iref]*imn2) + n3[iref]*imn3;
18636                                         if (nbrinp >= ant){
18637                                                 use_ref = true;
18638                                         }
18639                                 }
18640                                 
18641                                 if(use_ref) {
18642                                                 Dict retvals;
18643                                                 if (mirror_only == true){
18644                                                     if ((psi-90) < 90)  
18645                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 0, 1, psi_max);
18646                                                     else
18647                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 180, 1, psi_max); 
18648                                                 }       
18649                                                 else{ 
18650                                                     if ((psi-90) < 90)  
18651                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 0, 0, psi_max);
18652                                                     else
18653                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 180, 0, psi_max);
18654                                                 }   
18655                                                 double qn = retvals["qn"];
18656                                                 
18657                                                 if(qn >= peak) {
18658                                                         sx = -ix;
18659                                                         sy = -iy;
18660                                                         nref = iref;
18661                                                         if (!mirror_only) {
18662                                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18663                                                                 peak = static_cast<float>(qn);
18664                                                                 mirror = 0;
18665                                                         } else {
18666                                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18667                                                                 peak = static_cast<float>(qn);
18668                                                                 mirror = 1;
18669                                                         }
18670                                                 }
18671                                 }
18672                         } 
18673                         delete cimage; cimage = 0;
18674                 }                       
18675         } else {
18676                 int   ky = int(ynumber/2);              
18677                 float stepy=2*yrng/ynumber;
18678                 // when yrnglocal is not equal to -1.0, the search range is limited to +/- yrnglocal
18679                 // leave step size the same
18680                 if (yrnglocal >= 0.0){
18681                         ky = int(yrnglocal/stepy);
18682                 }
18683                 
18684                 //std::cout<<"yrnglocal="<<yrnglocal<<"ynumber="<<ynumber<<"stepy=="<<stepy<<"stepx=="<<step<<std::endl;
18685                 //cout<<"ky stepy: "<<ky<<" "<<stepy<<endl;
18686                 for (int i = -ky+1; i <= ky; i++) {
18687                         iy = i * stepy ;
18688                         for (int j = -kx; j <= kx; j++) {
18689                                 ix = j*step ;
18690                                 EMData* cimage = Polar2Dm(image, cnx+ix, cny+iy, numr, mode);
18691 
18692                                 Normalize_ring( cimage, numr );
18693 
18694                                 Frngs(cimage, numr);
18695                                 //  compare with all reference images
18696                                 // for iref in xrange(len(crefim)):
18697                                 for ( iref = 0; iref < (int)crefim_len; iref++) {
18698                                         
18699                                         use_ref = false;
18700                                         if (!mirror_only){
18701                                                 // inner product of iref's Eulerian angles with that of the data
18702                                                 nbrinp = n1[iref]*imn1 + n2[iref]*imn2 + n3[iref]*imn3;
18703                                                 if (nbrinp >= ant){
18704                                                         use_ref = true;
18705                                                 }
18706                                         }
18707                                         else if (mirror_only) {
18708                                                 // inner product of the mirror of iref's Eulerian angles with that of the data
18709                                                 nbrinp = (-1.0*n1[iref]*imn1) + (-1.0*n2[iref]*imn2) + n3[iref]*imn3;
18710                                                 if (nbrinp >= ant){
18711                                                         use_ref = true;
18712                                                 }
18713                                         }
18714                                         if(use_ref) {
18715                                                 Dict retvals;
18716                                                 if (mirror_only == true){
18717                                                     if ((psi-90) < 90)  
18718                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 0, 1, psi_max);
18719                                                     else
18720                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 180, 1, psi_max); 
18721                                                 }       
18722                                                 else{ 
18723                                                     if ((psi-90) < 90)  
18724                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 0, 0, psi_max);
18725                                                     else
18726                                                          retvals = Crosrng_sm_psi(crefim[iref], cimage, numr, 180, 0, psi_max);
18727                                                 }   
18728                                                 double qn = retvals["qn"];
18729                                                 
18730                                                 if(qn >= peak) {
18731                                                         sx = -ix;
18732                                                         sy = -iy;
18733                                                         nref = iref;
18734                                                         if (!mirror_only) {
18735                                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18736                                                                 peak = static_cast<float>(qn);
18737                                                                 mirror = 0;
18738                                                         } else {
18739                                                                 ang = ang_n(retvals["tot"], mode, numr[numr.size()-1]);
18740                                                                 peak = static_cast<float>(qn);
18741                                                                 mirror = 1;
18742                                                         }
18743                                                 }
18744                                         }
18745                                 }
18746                                 delete cimage; cimage = 0;
18747                         }
18748                 }
18749         }
18750         float co, so, sxs, sys;
18751         co = static_cast<float>( cos(ang*pi/180.0) );
18752         so = static_cast<float>( -sin(ang*pi/180.0) );
18753         sxs = sx*co - sy*so;
18754         sys = sx*so + sy*co;
18755         vector<float> res;
18756         res.push_back(ang);
18757         res.push_back(sxs);
18758         res.push_back(sys);
18759         res.push_back(static_cast<float>(mirror));
18760         res.push_back(static_cast<float>(nref));
18761         res.push_back(peak);
18762         return res;
18763 }

int Util::nearest_ang ( const vector< float > &  vecref,
float  x,
float  y,
float  z 
) [static]

Definition at line 18034 of file util_sparx.cpp.

References abs, and v.

Referenced by assign_projangles().

18034                                                                             {
18035         float best_v = -1.0f;
18036         int best_i = -1.0;
18037         
18038         for (int i=0; i<vecref.size()/3; i++) {
18039                 float v = abs(vecref[i*3]*x+vecref[i*3+1]*y+vecref[i*3+2]*z);
18040                 if (v > best_v) {
18041                         best_v = v;
18042                         best_i = i;
18043                 }
18044         }
18045         return best_i;
18046 }

static int EMAN::Util::nint180 ( float  arg  )  [inline, static]

Definition at line 1081 of file util.h.

Referenced by cml_line_insino(), and cml_line_insino_all().

01084                                             {

void Util::Normalize_ring ( EMData ring,
const vector< int > &  numr 
) [static]

Definition at line 17814 of file util_sparx.cpp.

References data, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), nn(), sqrt(), and EMAN::EMData::update().

Referenced by multiref_polar_ali_2d(), multiref_polar_ali_2d_delta(), multiref_polar_ali_2d_local(), multiref_polar_ali_2d_local_psi(), multiref_polar_ali_2d_peaklist(), multiref_polar_ali_helical(), multiref_polar_ali_helical_90(), multiref_polar_ali_helical_90_local(), and multiref_polar_ali_helical_local().

17815 {
17816     float* data = ring->get_data();
17817     float av=0.0;
17818     float sq=0.0;
17819     float nn=0.0;
17820     int nring = numr.size()/3;
17821     for( int i=0; i < nring; ++i )
17822     {
17823         int numr3i = numr[3*i+2];
17824         int numr2i = numr[3*i+1]-1;
17825         float w = numr[3*i]*2*M_PI/float(numr[3*i+2]);
17826         for( int j=0; j < numr3i; ++j )
17827         {
17828             int jc = numr2i+j;
17829             av += data[jc] * w;
17830             sq += data[jc] * data[jc] * w;
17831             nn += w;
17832         }
17833     }
17834 
17835     float avg = av/nn;
17836     float sgm = sqrt( (sq-av*av/nn)/nn );
17837     size_t n = (size_t)ring->get_xsize() * ring->get_ysize() * ring->get_zsize();
17838     for( size_t i=0; i < n; ++i )
17839     {
17840         data[i] -= avg;
17841         data[i] /= sgm;
17842     }
17843 
17844     ring->update();
17845 }

EMData * Util::pack_complex_to_real ( EMData img  )  [static]

Definition at line 17724 of file util_sparx.cpp.

References ENTERFUNC, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), img_ptr, EMAN::EMData::is_fftodd(), NullPointerException, nx, ny, power(), EMAN::EMData::set_array_offsets(), EMAN::EMData::set_size(), and EMAN::EMData::update().

17725 {
17726         ENTERFUNC;
17727         /* Exception Handle */
17728         if (!img) {
17729                 throw NullPointerException("NULL input image");
17730         }
17731         /* ==============   img is modulus of a complex image in FFT format (so its imaginary parts are zero),
17732                               output is img packed into real image with Friedel part added,   ================ */
17733 
17734         int nxo=img->get_xsize(), ny=img->get_ysize(), nz=img->get_zsize();
17735         int nx = nxo - 2 + img->is_fftodd();
17736         int lsd2 = (nx + 2 - nx%2) / 2; // Extended x-dimension of the complex image
17737         int nyt, nzt;
17738         int nx2 = nx/2;
17739         int ny2 = ny/2; if(ny2 == 0) nyt =0; else nyt=ny;
17740         int nz2 = nz/2; if(nz2 == 0) nzt =0; else nzt=nz;
17741         int nx2p = nx2+nx%2;
17742         int ny2p = ny2+ny%2;
17743         int nz2p = nz2+nz%2;
17744         EMData& power = *(new EMData()); // output image
17745         power.set_size(nx, ny, nz);
17746         power.set_array_offsets(-nx2,-ny2,-nz2);
17747         //img->set_array_offsets(1,1,1);
17748         float *img_ptr  = img->get_data();
17749         for (int iz = 1; iz <= nz; iz++) {
17750                 int jz=iz-1;
17751                 if(jz>=nz2p) jz=jz-nzt;
17752                 for (int iy = 1; iy <= ny; iy++) {
17753                         int jy=iy-1;
17754                         if(jy>=ny2p) jy=jy-nyt;
17755                         for (int ix = 1; ix <= lsd2; ix++) {
17756                                 int jx=ix-1;
17757                                 if(jx>=nx2p) jx=jx-nx;
17758                                 power(jx,jy,jz) = img_ptr(ix,iy,iz); //real(img->cmplx(ix,iy,iz));
17759                         }
17760                 }
17761         }
17762 //  Create the Friedel related half
17763         int  nzb, nze, nyb, nye, nxb, nxe;
17764         nxb =-nx2+(nx+1)%2;
17765         nxe = nx2-(nx+1)%2;
17766         if(ny2 == 0) {nyb =0; nye = 0;} else {nyb =-ny2+(ny+1)%2; nye = ny2-(ny+1)%2;}
17767         if(nz2 == 0) {nzb =0; nze = 0;} else {nzb =-nz2+(nz+1)%2; nze = nz2-(nz+1)%2;}
17768         for (int iz = nzb; iz <= nze; iz++) {
17769                 for (int iy = nyb; iy <= nye; iy++) {
17770                         for (int ix = 1; ix <= nxe; ix++) { // Note this loop begins with 1 - FFT should create correct Friedel related 0 plane
17771                                 power(-ix,-iy,-iz) = power(ix,iy,iz);
17772                         }
17773                 }
17774         }
17775         if(ny2 != 0)  {
17776                 if(nz2 != 0)  {
17777                         if(nz%2 == 0) {  //if nz even, fix the first slice
17778                                 for (int iy = nyb; iy <= nye; iy++) {
17779                                         for (int ix = nxb; ix <= -1; ix++) {
17780                                                 power(ix,iy,-nz2) = power(-ix,-iy,-nz2);
17781                                         }
17782                                 }
17783                                 if(ny%2 == 0) {  //if ny even, fix the first line
17784                                         for (int ix = nxb; ix <= -1; ix++) {
17785                                                 power(ix,-ny2,-nz2) = power(-ix,-ny2,-nz2);
17786                                         }
17787                                 }
17788                         }
17789                 }
17790                 if(ny%2 == 0) {  //if ny even, fix the first column
17791                         for (int iz = nzb; iz <= nze; iz++) {
17792                                 for (int ix = nxb; ix <= -1; ix++) {
17793                                         power(ix,-ny2,-iz) = power(-ix,-ny2,iz);
17794                                 }
17795                         }
17796                 }
17797 
17798         }
17799         power.update();
17800         power.set_array_offsets(0,0,0);
17801         return &power;
17802 }

EMData * Util::pad ( 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]

Definition at line 5352 of file util_sparx.cpp.

References EMAN::EMData::copy_head(), EMAN::EMObject::f, EMAN::EMData::get_attr(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageDimensionException, inp, NullPointerException, nx, ny, outp, EMAN::EMData::set_size(), and EMAN::EMData::update().

05353 {
05354         /* Exception Handle */
05355         if (!img)  throw NullPointerException("NULL input image");
05356         /* ============================== */
05357 
05358         // Get the size of the input image
05359         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
05360         /* ============================== */
05361 
05362         /* Exception Handle */
05363         if(new_nx<nx || new_ny<ny || new_nz<nz)
05364                 throw ImageDimensionException("The size of the padded image cannot be lower than the input image size.");
05365         if((new_nx/2)-(nx/2)+x_offset<0 || (new_ny/2)-(ny/2)+y_offset<0 || (new_nz/2)-(nz/2)+z_offset<0)
05366                 throw ImageDimensionException("The offset inconsistent with the input image size. Solution: Change the offset parameters");
05367         if(x_offset>((new_nx-(new_nx/2))-(nx-(nx/2))) || y_offset>((new_ny-(new_ny/2))-(ny-(ny/2))) || z_offset>((new_nz-(new_nz/2))-(nz-(nz/2))))
05368                 throw ImageDimensionException("The offset inconsistent with the input image size. Solution: Change the offset parameters");
05369         /* ============================== */
05370 
05371         EMData* pading = img->copy_head();
05372         pading->set_size(new_nx, new_ny, new_nz);
05373         float *inp  = img->get_data();
05374         float *outp = pading->get_data();
05375 
05376 
05377         /* Calculation of the average and the circumference values for background substitution
05378         =======================================================================================*/
05379         float background;
05380 
05381         if (strcmp(params,"average")==0) background = img->get_attr("mean");
05382         else if (strcmp(params,"circumference")==0) {
05383                 float sum1=0.0f;
05384                 size_t cnt=0;
05385                 for(int i=0;i<nx;i++) {
05386                         sum1 += inp(i,0,0) + inp(i,ny-1,nz-1);
05387                         cnt+=2;
05388                 }
05389                 if(nz-1 == 0) {
05390                         for (int j=1;j<ny-1;j++) {
05391                                 sum1 += inp(1,j,0) + inp(nx-1,j,0);
05392                                 cnt+=2;
05393                         }
05394                 } else {
05395                         for (int k=1;k<nz-1;k++) {
05396                                 for (int j=1;j<ny-1;j++) {
05397                                         sum1 += inp(1,j,0) + inp(nx-1,j,0);
05398                                         cnt+=2;
05399                                 }
05400                         }
05401                 }
05402                 background = sum1/cnt;
05403         } else {
05404                 background = static_cast<float>( atof( params ) );
05405         }
05406         /*=====================================================================================*/
05407 
05408          /*Initial Padding */
05409         int new_st_x=0,new_st_y=0,new_st_z=0;
05410         for (int k=0;k<new_nz;k++)
05411                 for(int j=0;j<new_ny;j++)
05412                         for (int i=0;i<new_nx;i++)
05413                                 outp(i,j,k)=background;
05414         /*============================== */
05415 
05416         /*    Calculation of the start point */
05417         new_st_x=int((new_nx/2-nx/2)  + x_offset);
05418         new_st_y=int((new_ny/2-ny/2)  + y_offset);
05419         new_st_z=int((new_nz/2-nz/2)  + z_offset);
05420         /* ============================== */
05421 
05422         for (int k=0;k<nz;k++)
05423                 for(int j=0;j<ny;j++)
05424                         for(int i=0;i<nx;i++)
05425                                 outp(i,j,k)=inp(i,j,k);
05426         pading->update();
05427         return pading;
05428 }

bool Util::point_is_in_convex_polygon_2d ( const Vec2f p1,
const Vec2f p2,
const Vec2f p3,
const Vec2f p4,
const Vec2f actual_point 
) [static]

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.

The points must be ordered in the way you would encounter them if you traversed the boundary of the polygon. Direction is irrelevant. Could be generalized for polygons with more points

Parameters:
p1 point one
p2 point two
p3 point three
p4 point three
actual_point the point which might be in the polygon described by p1,p2,p3 and p4
Returns:
true if the point is in the polygon, false otherwise
Author:
David Woolford
Date:
October 2008

Definition at line 1269 of file util.cpp.

References point_is_in_triangle_2d().

01270 {
01271 
01272         if (point_is_in_triangle_2d(p1,p2,p4,actual_point)) return true;
01273         else return point_is_in_triangle_2d(p3,p2,p4,actual_point);
01274 }

bool Util::point_is_in_triangle_2d ( const Vec2f p1,
const Vec2f p2,
const Vec2f p3,
const Vec2f actual_point 
) [static]

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.

Parameters:
p1 point one
p2 point two
p3 point three
actual_point the point which might be in the triangle described by p1,p2 and p3
Returns:
true if the point is in the triangle, false otherwise
Author:
David Woolford
Date:
October 2008

Definition at line 1234 of file util.cpp.

References EMAN::Vec2< Type >::dot(), EMAN::Transform::ERR_LIMIT, t, and v.

Referenced by point_is_in_convex_polygon_2d().

01235 {
01236 
01237         Vec2f u = p2 - p1;
01238         Vec2f v = p3 - p1;
01239         Vec2f w = point - p1;
01240 
01241         float udotu = u.dot(u);
01242         float udotv = u.dot(v);
01243         float udotw = u.dot(w);
01244         float vdotv = v.dot(v);
01245         float vdotw = v.dot(w);
01246 
01247         float d = 1.0f/(udotv*udotv - udotu*vdotv);
01248         float s = udotv*vdotw - vdotv*udotw;
01249         s *= d;
01250 
01251         float t = udotv*udotw - udotu*vdotw;
01252         t *= d;
01253 
01254         // We've done a few multiplications, so detect when there are tiny residuals that may throw off the final
01255         // decision
01256         if (fabs(s) < Transform::ERR_LIMIT ) s = 0;
01257         if (fabs(t) < Transform::ERR_LIMIT ) t = 0;
01258 
01259         if ( fabs((fabs(s)-1.0)) < Transform::ERR_LIMIT ) s = 1;
01260         if ( fabs((fabs(t)-1.0)) < Transform::ERR_LIMIT ) t = 1;
01261 
01262 //      cout << "s and t are " << s << " " << t << endl;
01263 
01264         // The final decision, if this is true then we've hit the jackpot
01265         if ( s >= 0 && t >= 0 && (s+t) <= 1 ) return true;
01266         else return false;
01267 }

EMData * Util::Polar2D ( EMData image,
vector< int >  numr,
string  mode 
) [static]

Definition at line 2141 of file util_sparx.cpp.

References circ, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), numr, quadri(), EMAN::EMData::set_size(), xim, and y.

02141                                                                   {
02142         int nsam = image->get_xsize();
02143         int nrow = image->get_ysize();
02144         int nring = numr.size()/3;
02145         int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
02146         EMData* out = new EMData();
02147         out->set_size(lcirc,1,1);
02148         char mode = (cmode == "F" || cmode == "f") ? 'f' : 'h';
02149         float *xim  = image->get_data();
02150         float *circ = out->get_data();
02151 /*   alrq(image->get_data(), nsam, nrow, &numr[0], out->get_data(), lcirc, nring, cmode);
02152    return out;
02153 }
02154 void Util::alrq(float *xim,  int nsam , int nrow , int *numr,
02155           float *circ, int lcirc, int nring, char mode)
02156 {*/
02157 /*
02158 c
02159 c  purpose:
02160 c
02161 c  resmaple to polar coordinates
02162 c
02163 */
02164         //  dimension         xim(nsam,nrow),circ(lcirc)
02165         //  integer           numr(3,nring)
02166 
02167         double dfi, dpi;
02168         int    ns2, nr2, i, inr, l, nsim, kcirc, lt, j;
02169         float  yq, xold, yold, fi, x, y;
02170 
02171         ns2 = nsam/2+1;
02172         nr2 = nrow/2+1;
02173         dpi = 2.0*atan(1.0);
02174 
02175         for (i=1;i<=nring;i++) {
02176                 // radius of the ring
02177                 inr = numr(1,i);
02178                 yq  = static_cast<float>(inr);
02179                 l   = numr(3,i);
02180                 if (mode == 'h' || mode == 'H')  lt = l/2;
02181                 else                             lt = l/4;
02182 
02183                 nsim           = lt-1;
02184                 dfi            = dpi/(nsim+1);
02185                 kcirc          = numr(2,i);
02186                 xold           = 0.0f;
02187                 yold           = static_cast<float>(inr);
02188                 circ(kcirc)    = quadri(xold+(float)ns2,yold+(float)nr2,nsam,nrow,xim);
02189                 xold           = static_cast<float>(inr);
02190                 yold           = 0.0f;
02191                 circ(lt+kcirc) = quadri(xold+ns2,yold+nr2,nsam,nrow,xim);
02192 
02193                 if (mode == 'f' || mode == 'F') {
02194                         xold              = 0.0f;
02195                         yold              = static_cast<float>(-inr);
02196                         circ(lt+lt+kcirc) = quadri(xold+ns2,yold+nr2,nsam,nrow,xim);
02197                         xold              = static_cast<float>(-inr);
02198                         yold              = 0.0f;
02199                         circ(lt+lt+lt+kcirc) = quadri(xold+ns2,yold+nr2,nsam,nrow,xim);
02200                 }
02201 
02202                 for (j=1;j<=nsim;j++) {
02203                         fi               = static_cast<float>(dfi*j);
02204                         x                = sin(fi)*yq;
02205                         y                = cos(fi)*yq;
02206                         xold             = x;
02207                         yold             = y;
02208                         circ(j+kcirc)    = quadri(xold+ns2,yold+nr2,nsam,nrow,xim);
02209                         xold             =  y;
02210                         yold             = -x;
02211                         circ(j+lt+kcirc) = quadri(xold+ns2,yold+nr2,nsam,nrow,xim);
02212 
02213                         if (mode == 'f' || mode == 'F')  {
02214                                 xold                = -x;
02215                                 yold                = -y;
02216                                 circ(j+lt+lt+kcirc) = quadri(xold+ns2,yold+nr2,nsam,nrow,xim);
02217                                 xold                = -y;
02218                                 yold                =  x;
02219                                 circ(j+lt+lt+lt+kcirc) = quadri(xold+ns2,yold+nr2,nsam,nrow,xim);
02220                         }
02221                 }
02222         }
02223         return  out;
02224 }

EMData * Util::Polar2Dm ( EMData image,
float  cns2,
float  cnr2,
vector< int >  numr,
string  cmode 
) [static]

Definition at line 2226 of file util_sparx.cpp.

References Assert, circ, EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), numr, quadri(), EMAN::EMData::set_size(), xim, and y.

Referenced by ali2d_ccf_list(), multiref_peaks_ali2d(), multiref_peaks_compress_ali2d(), multiref_polar_ali_2d(), multiref_polar_ali_2d_delta(), multiref_polar_ali_2d_local(), multiref_polar_ali_2d_local_psi(), multiref_polar_ali_2d_nom(), multiref_polar_ali_2d_peaklist(), multiref_polar_ali_helical(), multiref_polar_ali_helical_90(), multiref_polar_ali_helical_90_local(), and multiref_polar_ali_helical_local().

02226                                                                                            {
02227         int nsam = image->get_xsize();
02228         int nrow = image->get_ysize();
02229         int nring = numr.size()/3;
02230         int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
02231         EMData* out = new EMData();
02232         out->set_size(lcirc,1,1);
02233         char mode = (cmode == "F" || cmode == "f") ? 'f' : 'h';
02234         float *xim  = image->get_data();
02235         float *circ = out->get_data();
02236         double dpi, dfi;
02237         int    it, jt, inr, l, nsim, kcirc, lt;
02238         float  xold, yold, fi, x, y;
02239 
02240         //     cns2 and cnr2 are predefined centers
02241         //     no need to set to zero, all elements are defined
02242         dpi = 2*atan(1.0);
02243         for (it=1; it<=nring; it++) {
02244                 // radius of the ring
02245                 inr = numr(1,it);
02246 
02247                 // "F" means a full circle interpolation
02248                 // "H" means a half circle interpolation
02249 
02250                 l = numr(3,it);
02251                 if ( mode == 'h' || mode == 'H' ) lt = l / 2;
02252                 else                              lt = l / 4;
02253 
02254                 nsim  = lt - 1;
02255                 dfi   = dpi / (nsim+1);
02256                 kcirc = numr(2,it);
02257                 xold  = 0.0f+cns2;
02258                 yold  = inr+cnr2;
02259 
02260                 Assert( kcirc <= lcirc );
02261                 circ(kcirc) = quadri(xold,yold,nsam,nrow,xim);    // Sampling on 90 degree
02262 
02263                 xold  = inr+cns2;
02264                 yold  = 0.0f+cnr2;
02265                 Assert( lt+kcirc <= lcirc );
02266                 circ(lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);  // Sampling on 0 degree
02267 
02268                 if ( mode == 'f' || mode == 'F' ) {
02269                         xold = 0.0f+cns2;
02270                         yold = -inr+cnr2;
02271                         Assert( lt+lt+kcirc <= lcirc );
02272                         circ(lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);  // Sampling on 270 degree
02273 
02274                         xold = -inr+cns2;
02275                         yold = 0.0f+cnr2;
02276                         Assert(lt+lt+lt+kcirc <= lcirc );
02277                         circ(lt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim); // Sampling on 180 degree
02278                 }
02279 
02280                 for (jt=1; jt<=nsim; jt++) {
02281                         fi   = static_cast<float>(dfi * jt);
02282                         x    = sin(fi) * inr;
02283                         y    = cos(fi) * inr;
02284 
02285                         xold = x+cns2;
02286                         yold = y+cnr2;
02287 
02288                         Assert( jt+kcirc <= lcirc );
02289                         circ(jt+kcirc) = quadri(xold,yold,nsam,nrow,xim);      // Sampling on the first quadrant
02290 
02291                         xold = y+cns2;
02292                         yold = -x+cnr2;
02293 
02294                         Assert( jt+lt+kcirc <= lcirc );
02295                         circ(jt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);    // Sampling on the fourth quadrant
02296 
02297                         if ( mode == 'f' || mode == 'F' ) {
02298                                 xold = -x+cns2;
02299                                 yold = -y+cnr2;
02300 
02301                                 Assert( jt+lt+lt+kcirc <= lcirc );
02302                                 circ(jt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim); // Sampling on the third quadrant
02303 
02304                                 xold = -y+cns2;
02305                                 yold = x+cnr2;
02306 
02307                                 Assert( jt+lt+lt+lt+kcirc <= lcirc );
02308                                 circ(jt+lt+lt+lt+kcirc) = quadri(xold,yold,nsam,nrow,xim);  // Sampling on the second quadrant
02309                         }
02310                 } // end for jt
02311         } //end for it
02312         return out;
02313 }

EMData * Util::Polar2Dmi ( EMData image,
float  cns2,
float  cnr2,
vector< int >  numr,
string  cmode,
Util::KaiserBessel kb 
) [static]

Definition at line 2509 of file util_sparx.cpp.

References circ, EMAN::EMData::get_data(), get_pixel_conv_new(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), numr, nx, ny, EMAN::EMData::set_size(), and y.

02509                                                                                                                   {
02510 // input image is twice the size of the original image
02511         int nring = numr.size()/3;
02512         int lcirc = numr[3*nring-2]+numr[3*nring-1]-1;
02513         EMData* out = new EMData();
02514         out->set_size(lcirc,1,1);
02515         char mode = (cmode == "F" || cmode == "f") ? 'f' : 'h';
02516         float *circ = out->get_data();
02517         float *fimage = image->get_data();
02518         int nx = image->get_xsize();
02519         int ny = image->get_ysize();
02520         int nz = image->get_zsize();
02521         double dpi, dfi;
02522         int    it, jt, inr, l, nsim, kcirc, lt;
02523         float  yq, xold, yold, fi, x, y;
02524 
02525         //     cns2 and cnr2 are predefined centers
02526         //     no need to set to zero, all elements are defined
02527 
02528         dpi = 2*atan(1.0);
02529         for (it=1;it<=nring;it++) {
02530                 // radius of the ring
02531                 inr = numr(1,it);
02532                 yq  = static_cast<float>(inr);
02533 
02534                 l = numr(3,it);
02535                 if ( mode == 'h' || mode == 'H' )  lt = l / 2;
02536                 else                               lt = l / 4;
02537 
02538                 nsim  = lt - 1;
02539                 dfi   = dpi / (nsim+1);
02540                 kcirc = numr(2,it);
02541                 xold  = 0.0f;
02542                 yold  = static_cast<float>(inr);
02543                 circ(kcirc) = get_pixel_conv_new(nx,ny,nz,2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,fimage,kb);
02544 //      circ(kcirc) = image->get_pixel_conv(2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,kb);
02545 
02546                 xold  = static_cast<float>(inr);
02547                 yold  = 0.0f;
02548                 circ(lt+kcirc) = get_pixel_conv_new(nx,ny,nz,2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,fimage,kb);
02549 //      circ(lt+kcirc) = image->get_pixel_conv(2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,kb);
02550 
02551         if ( mode == 'f' || mode == 'F' ) {
02552                 xold = 0.0f;
02553                 yold = static_cast<float>(-inr);
02554                 circ(lt+lt+kcirc) = get_pixel_conv_new(nx,ny,nz,2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,fimage,kb);
02555 //         circ(lt+lt+kcirc) = image->get_pixel_conv(2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,kb);
02556 
02557                 xold = static_cast<float>(-inr);
02558                 yold = 0.0f;
02559                 circ(lt+lt+lt+kcirc) = get_pixel_conv_new(nx,ny,nz,2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,fimage,kb);
02560 //         circ(lt+lt+lt+kcirc) = image->get_pixel_conv(2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,kb);
02561         }
02562 
02563         for (jt=1;jt<=nsim;jt++) {
02564                 fi   = static_cast<float>(dfi * jt);
02565                 x    = sin(fi) * yq;
02566                 y    = cos(fi) * yq;
02567 
02568                 xold = x;
02569                 yold = y;
02570                 circ(jt+kcirc) = get_pixel_conv_new(nx,ny,nz,2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,fimage,kb);
02571 //         circ(jt+kcirc) = image->get_pixel_conv(2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,kb);
02572 
02573                 xold = y;
02574                 yold = -x;
02575                 circ(jt+lt+kcirc) = get_pixel_conv_new(nx,ny,nz,2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,fimage,kb);
02576 //         circ(jt+lt+kcirc) = image->get_pixel_conv(2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,kb);
02577 
02578         if ( mode == 'f' || mode == 'F' ) {
02579                 xold = -x;
02580                 yold = -y;
02581                 circ(jt+lt+lt+kcirc) = get_pixel_conv_new(nx,ny,nz,2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,fimage,kb);
02582 //            circ(jt+lt+lt+kcirc) = image->get_pixel_conv(2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,kb);
02583 
02584                 xold = -y;
02585                 yold = x;
02586                 circ(jt+lt+lt+lt+kcirc) = get_pixel_conv_new(nx,ny,nz,2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,fimage,kb);
02587 //            circ(jt+lt+lt+lt+kcirc) = image->get_pixel_conv(2*(xold+cns2-1.0f),2*(yold+cnr2-1.0f),0,kb);
02588         }
02589         } // end for jt
02590         } //end for it
02591         return  out;
02592 }

void Util::prb1d ( double *  b,
int  npoint,
float *  pos 
) [static]

Definition at line 3130 of file util_sparx.cpp.

References b.

Referenced by Crosrng_e(), Crosrng_ew(), Crosrng_ms(), Crosrng_ns(), and Crosrng_psi_0_180_no_mirror().

03130                                                   {
03131         double  c2,c3;
03132         int     nhalf;
03133 
03134         nhalf = npoint/2 + 1;
03135         *pos  = 0.0;
03136 
03137         if (npoint == 7) {
03138                 c2 = 49.*b(1) + 6.*b(2) - 21.*b(3) - 32.*b(4) - 27.*b(5)
03139                      - 6.*b(6) + 31.*b(7);
03140                 c3 = 5.*b(1) - 3.*b(3) - 4.*b(4) - 3.*b(5) + 5.*b(7);
03141         }
03142         else if (npoint == 5) {
03143                 c2 = (74.*b(1) - 23.*b(2) - 60.*b(3) - 37.*b(4)
03144                    + 46.*b(5) ) / (-70.);
03145                 c3 = (2.*b(1) - b(2) - 2.*b(3) - b(4) + 2.*b(5) ) / 14.0;
03146         }
03147         else if (npoint == 3) {
03148                 c2 = (5.*b(1) - 8.*b(2) + 3.*b(3) ) / (-2.0);
03149                 c3 = (b(1) - 2.*b(2) + b(3) ) / 2.0;
03150         }
03151         //else if (npoint == 9) {
03152         else  { // at least one has to be true!!
03153                 c2 = (1708.*b(1) + 581.*b(2) - 246.*b(3) - 773.*b(4)
03154                      - 1000.*b(5) - 927.*b(6) - 554.*b(7) + 119.*b(8)
03155                      + 1092.*b(9) ) / (-4620.);
03156                 c3 = (28.*b(1) + 7.*b(2) - 8.*b(3) - 17.*b(4) - 20.*b(5)
03157                      - 17.*b(6) - 8.*b(7) + 7.*b(8) + 28.*b(9) ) / 924.0;
03158         }
03159         if (c3 != 0.0)  *pos = static_cast<float>(c2/(2.0*c3) - nhalf);
03160 }

void Util::printMatI3D ( MIArray3D mat,
const string  str = string(""),
ostream &  out = std::cout 
) [static]

Print a 3D integer matrix to a file stream (std out by default).

Parameters:
[in] mat integer 3-d multi_array reference
[in] str Message string to be printed.
[out] out stream; cout by default.

Definition at line 1118 of file util.cpp.

References nx, and ny.

01118                                                                      {
01119         // Note: Don't need to check if 3-D because 3D is part of
01120         //       the MIArray3D typedef.
01121         out << "Printing 3D Integer data: " << str << std::endl;
01122         const multi_array_types::size_type* sizes = mat.shape();
01123         int nx = sizes[0], ny = sizes[1], nz = sizes[2];
01124         const multi_array_types::index* indices = mat.index_bases();
01125         int bx = indices[0], by = indices[1], bz = indices[2];
01126         for (int iz = bz; iz < nz+bz; iz++) {
01127                 cout << "(z = " << iz << " slice)" << endl;
01128                 for (int ix = bx; ix < nx+bx; ix++) {
01129                         for (int iy = by; iy < ny+by; iy++) {
01130                                 cout << setiosflags(ios::fixed) << setw(5)
01131                                          << mat[ix][iy][iz] << "  ";
01132                         }
01133                         cout << endl;
01134                 }
01135         }
01136 }

vector< float > Util::pw_extract ( vector< float >  pw,
int  n,
int  iswi,
float  ps 
) [static]

Definition at line 6329 of file util_sparx.cpp.

References call_cl1(), log(), and q.

06330 {
06331         int k,m,n1,klmd,klm2d,nklmd,n2d,n_larg,l, n2;
06332 
06333         k=(int)pw.size();
06334         l=0;
06335         m=k;
06336         n2=n+2;
06337         n1=n+1;
06338         klmd=k+l+m;
06339         klm2d= k+l+m+2;
06340         nklmd=k+l+m+n;
06341         n2d=n+2;
06342         /*size has to be increased when N is large*/
06343         n_larg=klmd*2;
06344         klm2d=n_larg+klm2d;
06345         klmd=n_larg+klmd;
06346         nklmd=n_larg+nklmd;
06347         int size_q=klm2d*n2d;
06348         int size_cu=nklmd*2;
06349         static int i__;
06350 
06351          double *q ;
06352          double *x ;
06353          double *res;
06354          double *cu;
06355          float *q2;
06356          float *pw_;
06357          long int *iu;
06358          double *s;
06359          q = (double*)calloc(size_q,sizeof(double));
06360          x = (double*)calloc(n2d,sizeof(double));
06361          res = (double*)calloc(klmd,sizeof(double));
06362          cu =(double*)calloc(size_cu,sizeof(double));
06363          s = (double*)calloc(klmd,sizeof(double));
06364          q2 = (float*)calloc(size_q,sizeof(float));
06365          iu = (long int*)calloc(size_cu,sizeof(long int));
06366          pw_ = (float*)calloc(k,sizeof(float));
06367 
06368         for( i__ =0;i__<k;++i__)
06369                 {
06370                 pw_[i__]=log(pw[i__]); }
06371         long int l_k=k;
06372         long int l_n=n;
06373         long int l_iswi=iswi;
06374         vector<float> cl1_res;
06375         cl1_res=Util::call_cl1(&l_k, &l_n, &ps, &l_iswi, pw_, q2, q, x, res, cu, s, iu);
06376         free(q);
06377         free(x);
06378         free(res);
06379         free(s);
06380         free(cu);
06381         free(q2);
06382         free(iu);
06383         free(pw_);
06384         return cl1_res;
06385 }

float Util::quadri ( float  x,
float  y,
int  nx,
int  ny,
float *  image 
) [static]

Quadratic interpolation (2D).

Note: This routine starts counting from 1, not 0!

This routine uses six image points for interpolation:

See also:
M. Abramowitz & I.E. Stegun, Handbook of Mathematical Functions (Dover, New York, 1964), Sec. 25.2.67. http://www.math.sfu.ca/~cbm/aands/page_882.htm

http://www.cl.cam.ac.uk/users/nad/pubs/quad.pdf

                f3    fc
                |
                | x
         f2-----f0----f1
                |
                |
                f4
		 *

f0 - f4 are image values near the interpolated point X. f0 is the interior mesh point nearest x.

Coords:

Mesh spacings: Interpolant: f = f0 + c1*(x-x0) + c2*(x-x0)*(x-x1) + c3*(y-y0) + c4*(y-y0)*(y-y1) + c5*(x-x0)*(y-y0)

Parameters:
[in] x x-coord value
[in] y y-coord value
nx 
ny 
[in] image Image object (pointer)
Returns:
Interpolated value

Definition at line 650 of file util_sparx.cpp.

References fdata, x, and y.

Referenced by alrl_ms(), Polar2D(), Polar2Dm(), quadri_background(), and EMAN::EMData::rot_scale_trans2D().

00651 {
00652 //  purpose: quadratic interpolation
00653 //  Optimized for speed, circular closer removed, checking of ranges removed
00654         float  x, y, dx0, dy0, f0, c1, c2, c3, c4, c5, dxb, dyb;
00655         float  quadri;
00656         int    i, j, ip1, im1, jp1, jm1, ic, jc, hxc, hyc;
00657 
00658         x = xx;
00659         y = yy;
00660 
00661         //     any xx and yy
00662         while ( x < 1.0 )                 x += nxdata;
00663         while ( x >= (float)(nxdata+1) )  x -= nxdata;
00664         while ( y < 1.0 )                 y += nydata;
00665         while ( y >= (float)(nydata+1) )  y -= nydata;
00666 
00667         i   = (int) x;
00668         j   = (int) y;
00669 
00670         dx0 = x - i;
00671         dy0 = y - j;
00672 
00673         ip1 = i + 1;
00674         im1 = i - 1;
00675         jp1 = j + 1;
00676         jm1 = j - 1;
00677 
00678         if (ip1 > nxdata) ip1 -= nxdata;
00679         if (im1 < 1)      im1 += nxdata;
00680         if (jp1 > nydata) jp1 -= nydata;
00681         if (jm1 < 1)      jm1 += nydata;
00682 
00683         f0  = fdata(i,j);
00684         c1  = fdata(ip1,j) - f0;
00685         c2  = (c1 - f0 + fdata(im1,j)) * 0.5f;
00686         c3  = fdata(i,jp1) - f0;
00687         c4  = (c3 - f0 + fdata(i,jm1)) * 0.5f;
00688 
00689         dxb = dx0 - 1;
00690         dyb = dy0 - 1;
00691 
00692         // hxc & hyc are either 1 or -1
00693         if (dx0 >= 0) hxc = 1; else hxc = -1;
00694         if (dy0 >= 0) hyc = 1; else hyc = -1;
00695 
00696         ic  = i + hxc;
00697         jc  = j + hyc;
00698 
00699         if (ic > nxdata) ic -= nxdata;  else if (ic < 1) ic += nxdata;
00700         if (jc > nydata) jc -= nydata;  else if (jc < 1) jc += nydata;
00701 
00702         c5  =  ( (fdata(ic,jc) - f0 - hxc * c1 - (hxc * (hxc - 1.0f)) * c2
00703                 - hyc * c3 - (hyc * (hyc - 1.0f)) * c4) * (hxc * hyc));
00704 
00705 
00706         quadri = f0 + dx0 * (c1 + dxb * c2 + dy0 * c5) + dy0 * (c3 + dyb * c4);
00707 
00708         return quadri;
00709 }

float Util::quadri_background ( float  x,
float  y,
int  nx,
int  ny,
float *  image,
int  xnew,
int  ynew 
) [static]

Quadratic interpolation (2D).

This is identical to quadri except the wrap around is not done circulantly.

Note: This routine starts counting from 1, not 0!

This routine uses six image points for interpolation:

See also:
M. Abramowitz & I.E. Stegun, Handbook of Mathematical Functions (Dover, New York, 1964), Sec. 25.2.67. http://www.math.sfu.ca/~cbm/aands/page_882.htm

http://www.cl.cam.ac.uk/users/nad/pubs/quad.pdf

                f3    fc
                |
                | x
         f2-----f0----f1
                |
                |
                f4
		 *

f0 - f4 are image values near the interpolated point X. f0 is the interior mesh point nearest x.

Coords:

Mesh spacings: Interpolant: f = f0 + c1*(x-x0) + c2*(x-x0)*(x-x1) + c3*(y-y0) + c4*(y-y0)*(y-y1) + c5*(x-x0)*(y-y0)

Parameters:
[in] x x-coord value
[in] y y-coord value
nx 
ny 
[in] image Image object (pointer)
Returns:
Interpolated value

Definition at line 714 of file util_sparx.cpp.

References fdata, quadri(), x, and y.

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

00715 {
00716 //  purpose: quadratic interpolation
00717 //  Optimized for speed, circular closer removed, checking of ranges removed
00718         float  x, y, dx0, dy0, f0, c1, c2, c3, c4, c5, dxb, dyb;
00719         float  quadri;
00720         int    i, j, ip1, im1, jp1, jm1, ic, jc, hxc, hyc;
00721 
00722         x = xx;
00723         y = yy;
00724 
00725         // wrap around is not done circulantly; if (x,y) is not in the image, then x = xnew and y = ynew
00726         if ( (x < 1.0) || ( x >= (float)(nxdata+1) ) || ( y < 1.0 ) || ( y >= (float)(nydata+1) )){
00727               x = (float)xnew;
00728                   y = (float)ynew;
00729      }
00730 
00731 
00732         i   = (int) x;
00733         j   = (int) y;
00734 
00735         dx0 = x - i;
00736         dy0 = y - j;
00737 
00738         ip1 = i + 1;
00739         im1 = i - 1;
00740         jp1 = j + 1;
00741         jm1 = j - 1;
00742 
00743         if (ip1 > nxdata) ip1 -= nxdata;
00744         if (im1 < 1)      im1 += nxdata;
00745         if (jp1 > nydata) jp1 -= nydata;
00746         if (jm1 < 1)      jm1 += nydata;
00747 
00748         f0  = fdata(i,j);
00749         c1  = fdata(ip1,j) - f0;
00750         c2  = (c1 - f0 + fdata(im1,j)) * 0.5f;
00751         c3  = fdata(i,jp1) - f0;
00752         c4  = (c3 - f0 + fdata(i,jm1)) * 0.5f;
00753 
00754         dxb = dx0 - 1;
00755         dyb = dy0 - 1;
00756 
00757         // hxc & hyc are either 1 or -1
00758         if (dx0 >= 0) hxc = 1; else hxc = -1;
00759         if (dy0 >= 0) hyc = 1; else hyc = -1;
00760 
00761         ic  = i + hxc;
00762         jc  = j + hyc;
00763 
00764         if (ic > nxdata) ic -= nxdata;  else if (ic < 1) ic += nxdata;
00765         if (jc > nydata) jc -= nydata;  else if (jc < 1) jc += nydata;
00766 
00767         c5  =  ( (fdata(ic,jc) - f0 - hxc * c1 - (hxc * (hxc - 1.0f)) * c2
00768                 - hyc * c3 - (hyc * (hyc - 1.0f)) * c4) * (hxc * hyc));
00769 
00770 
00771         quadri = f0 + dx0 * (c1 + dxb * c2 + dy0 * c5) + dy0 * (c3 + dyb * c4);
00772 
00773         return quadri;
00774 }

void Util::Radialize ( int *  PermMatTr,
float *  kValsSorted,
float *  weightofkvalsSorted,
int  Size,
int *  SizeReturned 
) [static]

list the sorted lengths of the integer lattice sites of a square sided image of size Size.

PRB

Parameters:
[out] PermMatTr The matrix telling the ordering of the lattice sites wrt the array
[out] kValsSorted 
[out] weightofkvalsSorted the number of sites at that distance
[in] Size The length of the image
[out] SizeReturned 

Definition at line 438 of file util_sparx.cpp.

References sort_mat().

00440 {
00441         int iMax = (int) floor( (Size-1.0)/2 +.01);
00442         int CountMax = (iMax+2)*(iMax+1)/2;
00443         int Count=-1;
00444         float *kVals     = new float[CountMax];
00445         float *weightMat = new float[CountMax];
00446         int *PermMat     = new   int[CountMax];
00447         SizeReturned[0] = CountMax;
00448 
00449 //      printf("Aa \n");        fflush(stdout);
00450         for (int jkx=0; jkx< iMax+1; jkx++) {
00451                 for (int jky=0; jky< jkx+1; jky++) {
00452                         Count++;
00453                         kVals[Count] = sqrtf((float) (jkx*jkx +jky*jky));
00454                         weightMat[Count]=  1.0;
00455                         if (jkx!=0)  { weightMat[Count] *=2;}
00456                         if (jky!=0)  { weightMat[Count] *=2;}
00457                         if (jkx!=jky){ weightMat[Count] *=2;}
00458                         PermMat[Count]=Count+1;
00459                 }
00460         }
00461 
00462         int lkVals = Count+1;
00463 //      printf("Cc \n");fflush(stdout);
00464 
00465         sort_mat(&kVals[0],&kVals[Count],
00466              &PermMat[0],  &PermMat[Count]);  //PermMat is
00467                                 //also returned as well as kValsSorted
00468         fflush(stdout);
00469 
00470         int newInd;
00471 
00472         for (int iP=0; iP < lkVals ; iP++ ) {
00473                 newInd =  PermMat[iP];
00474                 PermMatTr[newInd-1] = iP+1;
00475         }
00476 
00477 //      printf("Ee \n"); fflush(stdout);
00478 
00479         int CountA=-1;
00480         int CountB=-1;
00481 
00482         while (CountB< (CountMax-1)) {
00483                 CountA++;
00484                 CountB++;
00485 //              printf("CountA=%d ; CountB=%d \n", CountA,CountB);fflush(stdout);
00486                 kValsSorted[CountA] = kVals[CountB] ;
00487                 if (CountB<(CountMax-1) ) {
00488                         while (fabs(kVals[CountB] -kVals[CountB+1])<.0000001  ) {
00489                                 SizeReturned[0]--;
00490                                 for (int iP=0; iP < lkVals; iP++){
00491 //                                      printf("iP=%d \n", iP);fflush(stdout);
00492                                         if  (PermMatTr[iP]>CountA+1) {
00493                                                 PermMatTr[iP]--;
00494                                         }
00495                                 }
00496                                 CountB++;
00497                         }
00498                 }
00499         }
00500 
00501 
00502         for (int CountD=0; CountD < CountMax; CountD++) {
00503             newInd = PermMatTr[CountD];
00504             weightofkValsSorted[newInd-1] += weightMat[CountD];
00505         }
00506 
00507 }

EMData * Util::reconstitute_image_mask ( EMData image,
EMData mask 
) [static]

Recreates a n-d image using its compressed 1-D form and the mask.

Definition at line 6160 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, EMAN::EMData::set_size(), and EMAN::EMData::update().

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

06161 {
06162         /********
06163         ***Exception Handle
06164         *************/
06165         if(mask == NULL)
06166                 throw ImageDimensionException("The mask cannot be an null image");
06167 
06168         /***********
06169         ***get the size of the mask
06170         **************/
06171         int nx = mask->get_xsize(),ny = mask->get_ysize(),nz = mask->get_zsize();
06172 
06173         size_t i,size = (size_t)nx*ny*nz;                        /* loop counters */
06174         /* new image declaration */
06175         EMData *new_image = new EMData();
06176         new_image->set_size(nx,ny,nz);           /* set the size of new image */
06177         float *new_ptr  = new_image->get_data(); /* set size of the new image */
06178         float *mask_ptr = mask->get_data();      /* assign a pointer to the mask image */
06179         float *img_ptr  = image->get_data();     /* assign a pointer to the 1D image */
06180         int count = 0;
06181         float sum_under_mask = 0.0 ;
06182         for(i = 0;i < size;i++){
06183                         if(mask_ptr[i] > 0.5f){
06184                                 new_ptr[i] = img_ptr[count];
06185                                 sum_under_mask += img_ptr[count];
06186                                 count++;
06187                                 if( count > image->get_xsize() ) {
06188                                     throw ImageDimensionException("Error: in reconstitute_image_mask, the mask doesn't match the image, it is too large");
06189                                 }
06190                         }
06191         }
06192 
06193         if( count > image->get_xsize() ) {
06194             throw ImageDimensionException("Error: in reconstitute_image_mask, the mask doesn't match the image, it is too small");
06195         }
06196 
06197         float avg_under_mask = sum_under_mask / count;
06198         for(i = 0;i < size;i++) {
06199                 if(mask_ptr[i] <= 0.5f)  new_ptr[i] = avg_under_mask;
06200         }
06201         new_image->update();
06202         return new_image;
06203 }

string Util::recv_broadcast ( int  port  )  [static]

Definition at line 1041 of file util.cpp.

References BPKT::data, BPKT::hdr, EMAN::ByteOrder::is_host_big_endian(), BPKT::len, BPKT::oseq, and BPKT::pseq.

01041                                     {
01042 //      struct sockaddr_in sadr = { AF_INET, 9989, INADDR_ANY};
01043 //      int sock=socket(AF_INET,SOCK_DGRAM,0);
01044 //      if (bind(sock,&sadr,sizeof(sockaddr_in))) return string();
01045 
01046         if (ByteOrder::is_host_big_endian()) {
01047                 printf("No cache mirroring on Big endian machines yet\n");
01048                 return string();        // FIXME: no support for big endian hosts
01049         }
01050 
01051         BPKT pkt;
01052         string ret;
01053         vector<char> fill;
01054         int obj=-1;
01055         unsigned int i=0;
01056 //      printf ("Listening\n");
01057 
01058         while (1) {
01059                 int l = recv(sock,&pkt,1044,0);
01060                 if (l<=0) {
01061                         if (obj!=-1) printf("Timeout with incomplete obj %d  %d/%d\n",obj,i,(int)fill.size());
01062                         return string();                // probably a timeout
01063                 }
01064                 if (l<20) {
01065                         printf("Bad packet from broadcast");
01066                         continue;
01067                 }
01068 
01069                 if (strncmp(pkt.hdr,"EMAN",4)!=0) continue;
01070 
01071                 // new object coming in
01072                 if (obj!=pkt.oseq) {
01073                         obj=pkt.oseq;
01074                         ret.resize(pkt.len);
01075                         fill.resize((pkt.len-1)/1024+1);
01076                         for (i=0; i<fill.size(); i++) fill[i]=0;
01077                 }
01078                 if (obj==-1) printf("Something wierd happened. please report\n");
01079 
01080                 // copy the packet into the output buffer
01081                 fill[pkt.pseq]=1;
01082                 ret.replace(pkt.pseq*1024,l-20,(char *)pkt.data,l-20);
01083 
01084                 // see if we got everything
01085                 for (i=0; i<fill.size(); i++) {
01086                         if (fill[i]!=1) break;
01087                 }
01088 //              printf("\t\t\tObj %d  %d/%d      \r",obj,i,(int)fill.size());
01089                 fflush(stdout);
01090 
01091                 if (i==fill.size()) return ret;         // Yea !  We got a good packet
01092         }
01093 
01094 }

string Util::remove_filename_ext ( const string &  filename  )  [static]

Remove a filename's extension and return the new filename.

Parameters:
[in] filename The old filename whose extension is going to be removed.
Returns:
The new filename without extension.

Definition at line 450 of file util.cpp.

00451 {
00452     if (filename == "") {
00453         return "";
00454     }
00455 
00456         char *buf = new char[filename.size()+1];
00457         strcpy(buf, filename.c_str());
00458         char *old_ext = strrchr(buf, '.');
00459         if (old_ext) {
00460                 buf[strlen(buf) - strlen(old_ext)] = '\0';
00461         }
00462         string result = string(buf);
00463         if( buf )
00464         {
00465                 delete [] buf;
00466                 buf = 0;
00467         }
00468         return result;
00469 }

static float EMAN::Util::restrict1 ( float  x,
int  nx 
) [inline, static]

Definition at line 1125 of file util.h.

Referenced by get_pixel_conv_new(), and get_pixel_conv_new_background().

void Util::rotate_phase_origin ( float *  data,
size_t  nx,
size_t  ny,
size_t  nz 
) [static]

rotate data vertically by ny/2, to make the mrc phase origin compliance with EMAN1 and TNF reader

Parameters:
data complex data array
nx x dimension size
ny y dimension size
nz z dimension size

Definition at line 95 of file util.cpp.

References t.

00096 {
00097         if(ny==1 && nz==1) {    //1D, do nothing
00098                 return;
00099         }
00100         else if(ny!=1 && nz==1) {       //2D, rotate vertically by ny/2
00101                 size_t i, j, k, l;
00102                 float re;
00103                 l=ny/2*nx;
00104                 for (i=0; i<ny/2; i++) {
00105                         for (j=0; j<nx; j++) {
00106                                 k=j+i*nx;
00107                                 re=data[k];
00108                                 data[k]=data[k+l];
00109                                 data[k+l]=re;
00110                         }
00111                 }
00112         }
00113         else {  //3D, in the y,z plane, swaps quadrants I,III and II,IV, this is the 'rotation' in y and z
00114                 size_t i, j, k, l, ii, jj;
00115                 char * t=(char *)malloc(sizeof(float)*nx);
00116 
00117                 k=nx*ny*(nz+1)/2;
00118                 l=nx*ny*(nz-1)/2;
00119                 jj=nx*sizeof(float);
00120                 for (j=ii=0; j<nz/2; ++j) {
00121                         for (i=0; i<ny; ++i,ii+=nx) {
00122                                 memcpy(t,data+ii,jj);
00123                                 if (i<ny/2) {
00124                                         memcpy(data+ii,data+ii+k,jj);
00125                                         memcpy(data+ii+k,t,jj);
00126                                 }
00127                                 else {
00128                                         memcpy(data+ii,data+ii+l,jj);
00129                                         memcpy(data+ii+l,t,jj);
00130                                 }
00131                         }
00132                 }
00133                 free(t);
00134         }
00135 }

static int EMAN::Util::round ( double  x  )  [inline, static]

Get ceiling round of a float number x.

Parameters:
[in] x Given float number.
Returns:
Ceiling round of x.

Definition at line 1497 of file util.h.

static int EMAN::Util::round ( float  x  )  [inline, static]

Get ceiling round of a float number x.

Parameters:
[in] x Given float number.
Returns:
Ceiling round of x.

Definition at line 1485 of file util.h.

Referenced by EMAN::Util::FakeKaiserBessel::build_I0table(), EMAN::Util::KaiserBessel::build_I0table(), EMAN::Util::sincBlackman::build_sBtable(), EMAN::EMData::calc_fourier_shell_correlation(), EMAN::EMData::calc_hist(), EMAN::EMData::cog(), EMAN::EMData::cut_slice(), EMAN::EMData::extract_plane(), EMAN::EMData::extract_plane_rect(), EMAN::EMData::extract_plane_rect_fast(), EMAN::EMData::extractline(), EMAN::EMData::extractpoint(), extractpoint2(), EMAN::nnSSNR_ctfReconstructor::finish(), EMAN::nnSSNR_Reconstructor::finish(), EMAN::EMData::get_pixel_conv(), get_pixel_conv_new(), get_pixel_conv_new_background(), EMAN::EMData::get_pixel_filtered(), hypot_fast_int(), EMAN::EMData::phase_cog(), EMAN::ApplyPolynomialProfileToHelix::process_inplace(), EMAN::BeamstopProcessor::process_inplace(), EMAN::EMData::rot_scale_conv(), EMAN::EMData::rot_scale_conv7(), EMAN::EMData::translate(), and EMAN::EMData::uncut_slice().

bool Util::sanitycheck ( int *  argParts,
int *  Indices,
int *  dimClasses,
int  nParts,
int  K,
int  T,
int *  output 
) [static]

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.

Definition at line 20776 of file util_sparx.cpp.

References k_means_cont_table_().

20776                                                                                                          {
20777         //cout<<"sanitycheck called\n";
20778         int total_cost = *output;
20779         int num_matches = *(output+1);
20780 
20781         int cost=0;
20782         int* intx;
20783         int intx_size;
20784         int* intx_next(0);
20785         int intx_next_size = 0;
20786         int curclass;
20787         int curclass_size;
20788         //cout<<"cost by match: [";
20789         for(int i = 0; i < num_matches; i++){
20790                 curclass = *(output+2+ i*nParts);
20791                 // check feasibility
20792                 if (*(argParts + Indices[curclass]+1) == -5) {cout<<"infeasible match!\n"; return 0;}
20793                 *(argParts + Indices[curclass]+1) = -5;
20794                 // initialize intx to the number of elements in class curclass of partition 0 of the i-th match
20795                 curclass_size = *(dimClasses+curclass)-2;
20796                 intx = new int[curclass_size];
20797                 for (int ic = 0; ic < curclass_size; ic++) *(intx+ic) = *(argParts + Indices[curclass]+2+ic);
20798                 intx_size = curclass_size;
20799 
20800                 for (int j=1; j < nParts; j++){
20801                       curclass = *(output+2+ i*nParts+j);
20802                       if (*(argParts + Indices[j*K+curclass]+1)==-5){cout<<"infeasible match!\n"; return 0;}
20803                       *(argParts + Indices[j*K+curclass]+1)=-5;
20804                       // compute the intersection of intx and class curclass of partition j of the i-th match
20805                       intx_next_size = Util::k_means_cont_table_(intx,argParts + Indices[j*K+curclass]+2, intx_next, intx_size, *(dimClasses + j*K+curclass)-2,0);
20806                       intx_next = new int[intx_next_size];
20807                       Util::k_means_cont_table_(intx,argParts + Indices[j*K+curclass]+2, intx_next, intx_size, *(dimClasses + j*K+curclass)-2,1);
20808                       delete[] intx;
20809                       intx=intx_next;
20810                       intx_size= intx_next_size;
20811                 }
20812                 delete[] intx_next;
20813 
20814                 if (intx_next_size <= T) {cout << "something wrong with solution!\n"; return 0;}
20815                 //cout <<intx_next_size<<",";
20816                 cost = cost + intx_next_size;
20817         }
20818         //cout<<"]\n";
20819         if (cost != total_cost) {cout << "something wrong with solution!\n"; return 0;}
20820 
20821         return 1;
20822 
20823 }

void Util::save_data ( float  x0,
float  dx,
float *  y_array,
size_t  array_size,
const string &  filename 
) [static]

Save x, y data into a file.

Each line of the file have the format "x1TABy1", where x1 = x0 + dx*i; y1 = y_array[i].

Parameters:
[in] x0 The starting point of x.
[in] dx delta x. The increase step of x data.
[in] y_array The y data array.
[in] array_size The y data array size.
[in] filename The output filename.

Definition at line 618 of file util.cpp.

References Assert, FileAccessException, and NullPointerException.

00620 {
00621         Assert(dx > 0);
00622         Assert(array_size > 0);
00623         Assert(filename != "");
00624 
00625         if (!y_array) {
00626                 throw NullPointerException("y array");
00627         }
00628 
00629         FILE *out = fopen(filename.c_str(), "wb");
00630         if (!out) {
00631                 throw FileAccessException(filename);
00632         }
00633 
00634         for (size_t i = 0; i < array_size; i++) {
00635                 fprintf(out, "%g\t%g\n", x0 + dx * i, y_array[i]);
00636         }
00637         fclose(out);
00638 }

void Util::save_data ( float  x0,
float  dx,
const vector< float > &  y_array,
const string &  filename 
) [static]

Save x, y data into a file.

Each line of the file have the format "x1TABy1", where x1 = x0 + dx*i; y1 = y_array[i].

Parameters:
[in] x0 The starting point of x.
[in] dx delta x. The increase step of x data.
[in] y_array The y data array.
[in] filename The output filename.

Definition at line 599 of file util.cpp.

References Assert, and FileAccessException.

00601 {
00602         Assert(dx != 0);
00603         Assert(y_array.size() > 0);
00604         Assert(filename != "");
00605 
00606         FILE *out = fopen(filename.c_str(), "wb");
00607         if (!out) {
00608                 throw FileAccessException(filename);
00609         }
00610 
00611         for (size_t i = 0; i < y_array.size(); i++) {
00612                 fprintf(out, "%g\t%g\n", x0 + dx * i, y_array[i]);
00613         }
00614         fclose(out);
00615 }

void Util::save_data ( const vector< float > &  x_array,
const vector< float > &  y_array,
const string &  filename 
) [static]

Save (x y) data array into a file.

Each line of the file have the format "x1TABy1", where x1, y1 are elements of x array and y array. The x, y arrays must have the same number of items.

Parameters:
[in] x_array The x array.
[in] y_array The y array.
[in] filename The output filename.

Definition at line 575 of file util.cpp.

References Assert, FileAccessException, and LOGERR.

Referenced by EMAN::LowpassAutoBProcessor::create_radial_func(), and EMAN::CtfAverager::finish().

00577 {
00578         Assert(x_array.size() > 0);
00579         Assert(y_array.size() > 0);
00580         Assert(filename != "");
00581 
00582         if (x_array.size() != y_array.size()) {
00583                 LOGERR("array x and array y have different size: %d != %d\n",
00584                            x_array.size(), y_array.size());
00585                 return;
00586         }
00587 
00588         FILE *out = fopen(filename.c_str(), "wb");
00589         if (!out) {
00590                 throw FileAccessException(filename);
00591         }
00592 
00593         for (size_t i = 0; i < x_array.size(); i++) {
00594                 fprintf(out, "%g\t%g\n", x_array[i], y_array[i]);
00595         }
00596         fclose(out);
00597 }

string Util::sbasename ( const string &  filename  )  [static]

Get a filename's basename.

For example, the basename of "hello.c" is still "hello.c"; The basename of "/tmp/abc/hello.c" is "hello.c".

Parameters:
[in] filename The given filename, full path or relative path.
Returns:
The basename of the filename.

Definition at line 471 of file util.cpp.

Referenced by EMAN::Log::begin(), EMAN::TestUtil::check_image(), EMAN::TestUtil::dump_emdata(), EMAN::Log::loc(), and EMAN::TestUtil::set_progname().

00472 {
00473     if (filename == "") {
00474         return "";
00475     }
00476 
00477         char s = '/';
00478 #ifdef _WIN32
00479         s = '\\';
00480 #endif
00481         const char * c = strrchr(filename.c_str(), s);
00482     if (!c) {
00483         return filename;
00484     }
00485         else {
00486                 c++;
00487         }
00488     return string(c);
00489 }

void Util::search2 ( int *  argParts,
int *  Indices,
int *  dimClasses,
int  nParts,
int  K,
int  T,
int *  matchlist,
int *  costlist,
int  J 
) [static]

return the weight of the largest weighted feasible match, along with the match in the (preallocated) argument curmax.

The returned weight has to be gt newT. If there is no such feasible matching, return 0 as *curmax

Definition at line 20830 of file util_sparx.cpp.

References explore2(), and k_means_cont_table_().

20830                                                                                                                               {
20831         
20832         // some temp variables
20833         bool flag = 0;
20834         int nintx;
20835         int* dummy(0);
20836         //int* ret;
20837         int* curbranch = new int[nParts];
20838         
20839         //initialize costlist to all 0
20840         for(int jit= 0; jit< J; jit++) *(costlist+jit) = 0;
20841         
20842         
20843         for(int a=0; a<K; a++)
20844         {
20845         
20846                 // check that class a of partition 0 is active and has greater than T elements. If not the case, then skip to the next class
20847                 if (*(argParts + Indices[a] + 1) < 1) continue;
20848                 if (*(dimClasses + a)-2 <= T) continue;
20849 
20850                 // initial pruning: for each partition j>0, set the partition to inactive if its intersection with class a of partition 0 is less than new T
20851 
20852                 for( int i=1; i < nParts; i++){
20853                         flag = 0; // if flag stays 0 then no class in this partition has more than T objects in common with a, which implies no feasible match (> T) with class a of part 0 is possible.
20854                         for(int j=0; j < K; j++){
20855                                 if (*(argParts + Indices[i*K+j] + 1) < 1) continue;
20856                                 nintx = Util::k_means_cont_table_(argParts + Indices[a]+2,argParts + Indices[i*K+j]+2, dummy, *(dimClasses + a)-2, *(dimClasses + i*K+j)-2,0);
20857                                 if (nintx > T) flag=1;
20858                                 else *(argParts + Indices[i*K+j] + 1) =-4;
20859                         }
20860                         if (flag==0) {break;}
20861                 }
20862 
20863                 // explore determines J matchs with the largest weight greater than T where class in partition 0 is class a
20864                 *curbranch = a;
20865 
20866                 if (flag > 0) // Each partition has one or more active class
20867                         Util::explore2(argParts, Indices, dimClasses, nParts, K, T, argParts+Indices[a]+2, *(dimClasses+a)-2, argParts+Indices[a]+2,
20868                         *(dimClasses+a)-2,0, J, matchlist, costlist, curbranch);
20869                         
20870                 // take all the classes marked as -4 and remark it as 1 in preparation for next round
20871                 for( int i=1; i < nParts; i++){
20872                         for(int j=0; j < K; j++){
20873                                 if (*(argParts + Indices[i*K+j] + 1) == -4) *(argParts + Indices[i*K+j] + 1) =1;
20874 
20875                         }
20876                 }
20877         }
20878         
20879         delete[] curbranch;
20880 }

void Util::set_line ( EMData img,
int  posline,
EMData line,
int  offset,
int  length 
) [static]

This function drop a line (line) to an 2D image (img).

The position of the line to the image is defined by (postline). The part of the line paste is defined by (offset), the begin position and (length) the size.

Definition at line 4702 of file util_sparx.cpp.

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

04703 {
04704         int i;
04705         int nx=img->get_xsize();
04706         float *img_ptr  = img->get_data();
04707         float *line_ptr = line->get_data();
04708         for (i=0;i<length;i++) img_ptr[nx*posline + i] = line_ptr[offset + i];
04709         img->update();
04710 }

void Util::set_log_level ( int  argc,
char *  argv[] 
) [static]

Set program logging level through command line option "-v N", where N is the level.

Parameters:
[in] argc Number of arguments.
[in] argv Argument arrays.

Definition at line 1108 of file util.cpp.

References EMAN::Log::logger(), and EMAN::Log::set_level().

01109 {
01110         if (argc > 1 && strncmp(argv[1], "-v", 2) == 0) {
01111                 char level_str[32];
01112                 strcpy(level_str, argv[1] + 2);
01113                 Log::LogLevel log_level = (Log::LogLevel) atoi(level_str);
01114                 Log::logger()->set_level(log_level);
01115         }
01116 }

void Util::set_randnum_seed ( unsigned long int  seed  )  [static]

Set the seed for Randnum class.

Parameters:
[in] seed the seed for current random number generator

Definition at line 678 of file util.cpp.

References EMAN::Randnum::Instance(), and EMAN::Randnum::set_seed().

00679 {
00680         Randnum* randnum = Randnum::Instance();
00681         randnum->set_seed(seed);
00682 }

template<class T>
static T EMAN::Util::sgn ( T &  val  )  [inline, static]

Sign function.

Parameters:
[in] val value who's sign is to be checked
Returns:
+1 if the value is positive, -1 if the value is negative.

Definition at line 2096 of file util.h.

Referenced by EMAN::EMData::extract_plane(), EMAN::EMData::extract_plane_rect(), EMAN::EMData::extract_plane_rect_fast(), EMAN::EMData::extractline(), fftc_d(), fftc_q(), and EMAN::EMData::getconvpt2d_kbi0().

void Util::slicereverse ( float *  beg,
float *  end,
int  nx,
int  ny 
) [static]

Definition at line 5446 of file util_sparx.cpp.

References colreverse().

Referenced by cyclicshift().

05447 {
05448         int nxy = nx*ny;
05449         colreverse(beg, end, nxy);
05450 }

void Util::sort_mat ( float *  left,
float *  right,
int *  leftPerm,
int *  rightPerm 
) [static]

does a sort as in Matlab.

Carries along the Permutation matrix

Parameters:
[in] left The array [left .. right] is sorted
[in] right The array [left .. right] is sorted
[in] leftPerm The array [leftPerm rightPerm] is shuffled due to the sorting
[in] rightPerm The array [leftPerm rightPerm] is shuffled due to the sorting Both arrays are reshuffled.

Definition at line 641 of file util.cpp.

Referenced by Radialize().

00643 {
00644         float *pivot ; int *pivotPerm;
00645 
00646         {
00647                 float *pLeft  =   left; int *pLeftPerm  =  leftPerm;
00648                 float *pRight =  right; int *pRightPerm = rightPerm;
00649                 float scratch =  *left; int scratchPerm = *leftPerm;
00650 
00651                 while (pLeft < pRight) {
00652                         while ((*pRight > scratch) && (pLeft < pRight)) {
00653                                 pRight--; pRightPerm--;
00654                         }
00655                         if (pLeft != pRight) {
00656                                 *pLeft = *pRight; *pLeftPerm = *pRightPerm;
00657                                 pLeft++; pLeftPerm++;
00658                         }
00659                         while ((*pLeft < scratch) && (pLeft < pRight)) {
00660                                 pLeft++; pLeftPerm++;
00661                         }
00662                         if (pLeft != pRight) {
00663                                 *pRight = *pLeft; *pRightPerm = *pLeftPerm;
00664                                 pRight--; pRightPerm--;
00665                         }
00666                 }
00667                 *pLeft = scratch; *pLeftPerm = scratchPerm;
00668                 pivot = pLeft; pivotPerm= pLeftPerm;
00669         }
00670         if (left < pivot) {
00671                 sort_mat(left, pivot - 1,leftPerm,pivotPerm-1);
00672         }
00673         if (right > pivot) {
00674                 sort_mat(pivot + 1, right,pivotPerm+1,rightPerm);
00675         }
00676 }

void Util::spline ( float *  x,
float *  y,
int  n,
float  yp1,
float  ypn,
float *  y2 
) [static]

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.

PRB This function is called by splint

Parameters:
x 
[in] y of x is the tabulated function of length n
n 
[in] yp1 : the derivatives of the first point.
[in] ypn : the derivatives of the last point.
[out] y2 is the value of the second derivatives

Definition at line 377 of file util_sparx.cpp.

00378 {
00379         int i,k;
00380         float p, qn, sig, un, *u;
00381         u = new float[n-1];
00382 
00383         if (yp1 > .99e30){
00384                 y2[0]=u[0]=0.0;
00385         } else {
00386                 y2[0]=-.5f;
00387                 u[0] =(3.0f/ (x[1] -x[0]))*( (y[1]-y[0])/(x[1]-x[0]) -yp1);
00388         }
00389 
00390         for (i=1; i < n-1; i++) {
00391                 sig= (x[i] - x[i-1])/(x[i+1] - x[i-1]);
00392                 p = sig*y2[i-1] + 2.0f;
00393                 y2[i]  = (sig-1.0f)/p;
00394                 u[i] = (y[i+1] - y[i] )/(x[i+1]-x[i] ) -  (y[i] - y[i-1] )/(x[i] -x[i-1]);
00395                 u[i] = (6.0f*u[i]/ (x[i+1]-x[i-1]) - sig*u[i-1])/p;
00396         }
00397 
00398         if (ypn>.99e30){
00399                 qn=0; un=0;
00400         } else {
00401                 qn= .5f;
00402                 un= (3.0f/(x[n-1] -x[n-2])) * (ypn -  (y[n-1]-y[n-2])/(x[n-1]-x[n-2]));
00403         }
00404         y2[n-1]= (un - qn*u[n-2])/(qn*y2[n-2]+1.0f);
00405         for (k=n-2; k>=0; k--){
00406                 y2[k]=y2[k]*y2[k+1]+u[k];
00407         }
00408         delete [] u;
00409 }

void Util::spline_mat ( float *  x,
float *  y,
int  n,
float *  xq,
float *  yq,
int  m 
) [static]

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.

Parameters:
x 
[in] y of x is the tabulated function of length n
n 
[in] xq is the x values to be splined: has m points.
[out] yq are the splined values
m 

Definition at line 352 of file util_sparx.cpp.

References spline(), and splint().

00353 {
00354 
00355         float x0= x[0];
00356         float x1= x[1];
00357         float x2= x[2];
00358         float y0= y[0];
00359         float y1= y[1];
00360         float y2= y[2];
00361         float yp1 =  (y1-y0)/(x1-x0) +  (y2-y0)/(x2-x0) - (y2-y1)/(x2-x1)  ;
00362         float xn  = x[n];
00363         float xnm1= x[n-1];
00364         float xnm2= x[n-2];
00365         float yn  = y[n];
00366         float ynm1= y[n-1];
00367         float ynm2= y[n-2];
00368         float ypn=  (yn-ynm1)/(xn-xnm1) +  (yn-ynm2)/(xn-xnm2) - (ynm1-ynm2)/(xnm1-xnm2) ;
00369         float *y2d = new float[n];
00370         Util::spline(x,y,n,yp1,ypn,y2d);
00371         Util::splint(x,y,y2d,n,xq,yq,m); //PRB
00372         delete [] y2d;
00373         return;
00374 }

void Util::splint ( float *  xa,
float *  ya,
float *  y2a,
int  n,
float *  xq,
float *  yq,
int  m 
) [static]

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.

Parameters:
xa 
[in] ya of x is the tabulated function of length n
[in] y2a is returned from spline: second derivs
n 
[in] xq is the x values to be splined: has m points.
[out] yq are the splined values
m 

Definition at line 412 of file util_sparx.cpp.

References b.

00413 {
00414         int klo, khi, k;
00415         float h, b, a;
00416 
00417 //      klo=0; // can try to put here
00418         for (int j=0; j<m;j++){
00419                 klo=0;
00420                 khi=n-1;
00421                 while (khi-klo >1) {
00422                         k=(khi+klo) >>1;
00423                         if  (xa[k]>xq[j]){ khi=k;}
00424                         else { klo=k;}
00425                 }
00426                 h=xa[khi]- xa[klo];
00427                 if (h==0.0) printf("Bad XA input to routine SPLINT \n");
00428                 a =(xa[khi]-xq[j])/h;
00429                 b=(xq[j]-xa[klo])/h;
00430                 yq[j]=a*ya[klo] + b*ya[khi]
00431                         + ((a*a*a-a)*y2a[klo]
00432                              +(b*b*b-b)*y2a[khi]) *(h*h)/6.0f;
00433         }
00434 //      printf("h=%f, a = %f, b=%f, ya[klo]=%f, ya[khi]=%f , yq=%f\n",h, a, b, ya[klo], ya[khi],yq[0]);
00435 }

static float EMAN::Util::square ( double  x  )  [inline, static]

Calculate a number's square.

Parameters:
[in] x Given number.
Returns:
(x*x).

Definition at line 1625 of file util.h.

static float EMAN::Util::square ( float  x  )  [inline, static]

Calculate a number's square.

Parameters:
[in] x Given number.
Returns:
(x*x).

Definition at line 1616 of file util.h.

static int EMAN::Util::square ( int  n  )  [inline, static]

Calculate a number's square.

Parameters:
[in] n Given number.
Returns:
(n*n).

Definition at line 1607 of file util.h.

Referenced by EMAN::EMData::calc_dist(), EMAN::EMData::calc_mutual_correlation(), EMAN::EMData::calc_sigma_diff(), EMAN::OptVarianceCmp::cmp(), EMAN::EMData::do_radon(), get_stats_cstyle(), EMAN::EMData::little_big_dot(), EMAN::SmartMaskProcessor::process_inplace(), and EMAN::PaintProcessor::process_inplace().

static float EMAN::Util::square_sum ( float  x,
float  y 
) [inline, static]

Calcuate (x*x + y*y).

Parameters:
[in] x The first number.
[in] y The second number.
Returns:
(x*x + y*y).

Definition at line 1635 of file util.h.

Referenced by EMAN::EMData::common_lines(), get_stats(), and get_stats_cstyle().

bool Util::sstrncmp ( const char *  s1,
const char *  s2 
) [static]

Safe string compare.

It compares 's2' with the first N characters of 's1', where N is the length of 's2'.

Parameters:
s1 String 1. Its first strlen(s2) characters will be used to do the comparison.
s2 String 2. Its whole string will be used to do the comparison.
Returns:
True if the comparison is equal. False if not equal.

Definition at line 247 of file util.cpp.

References NullPointerException.

Referenced by EMAN::TestUtil::dump_emdata().

00248 {
00249         if (!s1 || !s2) {
00250                 throw NullPointerException("Null string");
00251         }
00252 
00253         if (strncmp(s1, s2, strlen(s2)) == 0) {
00254                 return true;
00255         }
00256 
00257         return false;
00258 }

string Util::str_to_lower ( const string &  s  )  [static]

Return a lower case version of the argument string.

Parameters:
s the string you want to convert to lower case
Returns:
the lower case converted version s

Definition at line 241 of file util.cpp.

Referenced by EMAN::Transform::detect_problem_keys(), EMAN::Symmetry3D::gen_orientations(), EMAN::Dict::get_ci(), EMAN::Transform::get_params(), EMAN::Transform::get_params_inverse(), EMAN::Transform::get_rotation(), EMAN::Symmetry3D::get_symmetries(), EMAN::Dict::has_key_ci(), and EMAN::Transform::set_rotation().

00241                                          {
00242         string ret(s);
00243         std::transform(s.begin(),s.end(),ret.begin(), (int (*)(int) ) std::tolower);
00244         return ret;
00245 }

void Util::sub_fav ( EMData ave,
EMData dat,
float  tot,
int  mirror,
vector< int >  numr 
) [static]

Definition at line 4545 of file util_sparx.cpp.

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

04545                                                                                       {
04546         int nring = numr.size()/3;
04547         float *ave = avep->get_data();
04548         float *dat = datp->get_data();
04549         int i, j, numr3i, np;
04550         float  arg, cs, si;
04551         int maxrin = numr(3,nring);
04552         if(mirror == 1) { //for mirrored data has to be conjugated
04553                 for (i=1; i<=nring; i++) {
04554                         numr3i = numr(3,i);
04555                         np     = numr(2,i)-1;
04556                         ave[np]   -= dat[np];
04557                         ave[np+1] -= static_cast<float>( dat[np+1]*cos(PI2*(tot-1.0f)/2.0f*numr3i/maxrin) );
04558                         for (j=2; j<numr3i; j=j+2) {
04559                                 arg = static_cast<float>( PI2*(tot-1.)*(j/2)/maxrin );
04560                                 cs = cos(arg);
04561                                 si = sin(arg);
04562                                 //complex(data[np + j],data[np + j +1])*complex(cos(arg),sin(arg))
04563                                 ave[np + j]    -= dat[np + j]*cs - dat[np + j +1]*si;
04564                                 ave[np + j +1] += dat[np + j]*si + dat[np + j +1]*cs;
04565                         }
04566                 }
04567         } else {
04568                 for (i=1; i<=nring; i++) {
04569                         numr3i = numr(3,i);
04570                         np     = numr(2,i)-1;
04571                         ave[np]   -= dat[np];
04572                         ave[np+1] -= static_cast<float>( dat[np+1]*cos(PI2*(tot-1.0f)/2.0f*numr3i/maxrin) );
04573                         for (j=2; j<numr3i; j=j+2) {
04574                                 arg = static_cast<float>( PI2*(tot-1.)*(j/2)/maxrin );
04575                                 cs = cos(arg);
04576                                 si = sin(arg);
04577                                 //complex(data[np + j],data[np + j +1])*complex(cos(arg),sin(arg))
04578                                 ave[np + j]    -= dat[np + j]*cs - dat[np + j +1]*si;
04579                                 ave[np + j +1] -= dat[np + j]*si + dat[np + j +1]*cs;
04580                         }
04581                 }
04582         }
04583         avep->update();
04584         EXITFUNC;
04585 }

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

Definition at line 17619 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().

17620 {
17621         ENTERFUNC;
17622         /* Exception Handle */
17623         if (!img) {
17624                 throw NullPointerException("NULL input image");
17625         }
17626         /* ========= img -= img1 ===================== */
17627 
17628         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17629         size_t size = (size_t)nx*ny*nz;
17630         float *img_ptr  = img->get_data();
17631         float *img1_ptr = img1->get_data();
17632         for (size_t i=0;i<size;++i) img_ptr[i] -= img1_ptr[i];
17633         img->update();
17634 
17635         EXITFUNC;
17636 }

EMData * Util::subn_img ( EMData img,
EMData img1 
) [static]

Definition at line 17401 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().

17402 {
17403         ENTERFUNC;
17404         /* Exception Handle */
17405         if (!img) {
17406                 throw NullPointerException("NULL input image");
17407         }
17408         /* ==============   output = img - img1   ================ */
17409 
17410         int nx=img->get_xsize(),ny=img->get_ysize(),nz=img->get_zsize();
17411         size_t size = (size_t)nx*ny*nz;
17412         EMData * img2 = img->copy_head();
17413         float *img_ptr  =img->get_data();
17414         float *img2_ptr = img2->get_data();
17415         float *img1_ptr = img1->get_data();
17416         for (size_t i=0;i<size;++i) img2_ptr[i] = img_ptr[i] - img1_ptr[i];
17417         img2->update();
17418         if(img->is_complex()) {
17419                 img2->set_complex(true);
17420                 if(img->is_fftodd()) img2->set_fftodd(true); else img2->set_fftodd(false);
17421         }
17422 
17423         EXITFUNC;
17424         return img2;
17425 }

vector< EMData * > Util::svdcmp ( const vector< EMData * > &  data,
int  nvec 
) [static]

Perform singular value decomposition on a set of images.

Parameters:
data A List of data objects to be decomposed
nvec Number of basis vectors to return, 0 returns full decomposition
Returns:
A list of images representing basis vectors in the SVD generated subspace

Definition at line 288 of file util.cpp.

References copy_head(), get_value_at(), EMAN::EMData::set_value_at(), V, x, X, and y.

Referenced by EMAN::PCA::dopca().

00288                                                                    {
00289         int nimg=data.size();
00290         if (nvec==0) nvec=nimg;
00291         vector<EMData *> ret(nvec);
00292         if (nimg==0) return ret;
00293         int pixels=data[0]->get_xsize()*data[0]->get_ysize()*data[0]->get_zsize();
00294 
00295         // Allocate the working space
00296         gsl_vector *work=gsl_vector_alloc(nimg);
00297         gsl_vector *S=gsl_vector_alloc(nimg);
00298         gsl_matrix *A=gsl_matrix_alloc(pixels,nimg);
00299         gsl_matrix *V=gsl_matrix_alloc(nimg,nimg);
00300         gsl_matrix *X=gsl_matrix_alloc(nimg,nimg);
00301 
00302         int im,x,y,z,i;
00303         for (im=0; im<nimg; im++) {
00304                 for (z=0,i=0; z<data[0]->get_zsize(); z++) {
00305                         for (y=0; y<data[0]->get_ysize(); y++) {
00306                                 for (x=0; x<data[0]->get_xsize(); x++,i++) {
00307                                         gsl_matrix_set(A,i,im,data[im]->get_value_at(x,y,z));
00308                                 }
00309                         }
00310                 }
00311         }
00312 
00313         // This calculates the SVD
00314         gsl_linalg_SV_decomp_mod (A,X, V, S, work);
00315 
00316         for (im=0; im<nvec; im++) {
00317                 EMData *a=data[0]->copy_head();
00318                 ret[im]=a;
00319                 for (z=0,i=0; z<data[0]->get_zsize(); z++) {
00320                         for (y=0; y<data[0]->get_ysize(); y++) {
00321                                 for (x=0; x<data[0]->get_xsize(); x++,i++) {
00322                                         a->set_value_at(x,y,z,static_cast<float>(gsl_matrix_get(A,i,im)));
00323                                 }
00324                         }
00325                 }
00326         }
00327         return ret;
00328 }

float Util::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]

Definition at line 6108 of file util_sparx.cpp.

References phase(), and sqrt().

Referenced by ctf_img(), and EMAN::Processor::EMFourierFilterFunc().

06109 {
06110         float cst  = cs*1.0e7f;
06111 
06112         wgh /= 100.0;
06113         float phase = atan(wgh/sqrt(1.0f-wgh*wgh));
06114         float lambda=12.398f/sqrt(voltage*(1022.0f+voltage));
06115         float ak2 = ak*ak;
06116         float g1 = dzz*1.0e4f*lambda*ak2;
06117         float g2 = cst*lambda*lambda*lambda*ak2*ak2/2.0f;
06118 
06119         float ctfv = static_cast<float>( sin(M_PI*(g1-g2)+phase)*sign );
06120         if(b_factor != 0.0f)  ctfv *= exp(-b_factor*ak2/4.0f);
06121 
06122         return ctfv;
06123 }

static float EMAN::Util::trilinear_interpolate ( float  p1,
float  p2,
float  p3,
float  p4,
float  p5,
float  p6,
float  p7,
float  p8,
float  t,
float  u,
float  v 
) [inline, static]

Calculate trilinear interpolation.

Parameters:
[in] p1 The first number. corresponding to (x0,y0,z0).
[in] p2 The second number. corresponding to (x1,y0,z0).
[in] p3 The third number. corresponding to (x0,y1, z0).
[in] p4 The fourth number. corresponding to (x1,y1,z0).
[in] p5 The fifth number. corresponding to (x0,y0,z1).
[in] p6 The sixth number. corresponding to (x1,y0,z1).
[in] p7 The seventh number. corresponding to (x0,y1,z1).
[in] p8 The eighth number. corresponding to (x1,y1,z1).
[in] t t
[in] u u
[in] v v
Returns:
The trilinear interpolation value.

Definition at line 1546 of file util.h.

Referenced by EMAN::EMData::cut_slice(), EMAN::EMData::extract_box(), EMAN::SymSearchProcessor::process_inplace(), EMAN::StandardProjector::project3d(), EMAN::EMData::sget_value_at_interp(), and EMAN::TransformProcessor::transform().

float Util::triquad ( float  r,
float  s,
float  t,
float *  fdata 
) [static]

Quadratic interpolation (3D).

Parameters:
r 
s 
t 
fdata 
Returns:
Interpolated value

Definition at line 1908 of file util_sparx.cpp.

01909 {
01910 
01911     const float C2 = 0.5f;    //1.0 / 2.0;
01912     const float C4 = 0.25f;   //1.0 / 4.0;
01913     const float C8 = 0.125f;  //1.0 / 8.0;
01914 
01915     float  RS   = R * S;
01916     float  ST   = S * T;
01917     float  RT   = R * T;
01918     float  RST  = R * ST;
01919 
01920     float  RSQ  = 1-R*R;
01921     float  SSQ  = 1-S*S;
01922     float  TSQ  = 1-T*T;
01923 
01924     float  RM1  = (1-R);
01925     float  SM1  = (1-S);
01926     float  TM1  = (1-T);
01927 
01928     float  RP1  = (1+R);
01929     float  SP1  = (1+S);
01930     float  TP1  = (1+T);
01931 
01932     float triquad =
01933     (-C8) * RST * RM1  * SM1  * TM1 * fdata[0] +
01934         ( C4) * ST  * RSQ  * SM1  * TM1 * fdata[1] +
01935         ( C8) * RST * RP1  * SM1  * TM1 * fdata[2] +
01936         ( C4) * RT  * RM1  * SSQ  * TM1 * fdata[3] +
01937         (-C2) * T   * RSQ  * SSQ  * TM1 * fdata[4] +
01938         (-C4) * RT  * RP1  * SSQ  * TM1 * fdata[5] +
01939         ( C8) * RST * RM1  * SP1  * TM1 * fdata[6] +
01940         (-C4) * ST  * RSQ  * SP1  * TM1 * fdata[7] +
01941         (-C8) * RST * RP1  * SP1  * TM1 * fdata[8] +
01942 //
01943         ( C4) * RS  * RM1  * SM1  * TSQ * fdata[9]  +
01944         (-C2) * S   * RSQ  * SM1  * TSQ * fdata[10] +
01945         (-C4) * RS  * RP1  * SM1  * TSQ * fdata[11] +
01946         (-C2) * R   * RM1  * SSQ  * TSQ * fdata[12] +
01947                       RSQ  * SSQ  * TSQ * fdata[13] +
01948         ( C2) * R   * RP1  * SSQ  * TSQ * fdata[14] +
01949         (-C4) * RS  * RM1  * SP1  * TSQ * fdata[15] +
01950         ( C2) * S   * RSQ  * SP1  * TSQ * fdata[16] +
01951         ( C4) * RS  * RP1  * SP1  * TSQ * fdata[17] +
01952  //
01953         ( C8) * RST * RM1  * SM1  * TP1 * fdata[18] +
01954         (-C4) * ST  * RSQ  * SM1  * TP1 * fdata[19] +
01955         (-C8) * RST * RP1  * SM1  * TP1 * fdata[20] +
01956         (-C4) * RT  * RM1  * SSQ  * TP1 * fdata[21] +
01957         ( C2) * T   * RSQ  * SSQ  * TP1 * fdata[22] +
01958         ( C4) * RT  * RP1  * SSQ  * TP1 * fdata[23] +
01959         (-C8) * RST * RM1  * SP1  * TP1 * fdata[24] +
01960         ( C4) * ST  * RSQ  * SP1  * TP1 * fdata[25] +
01961         ( C8) * RST * RP1  * SP1  * TP1 * fdata[26]   ;
01962      return triquad;
01963 }

int Util::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]

Definition at line 7860 of file util_sparx.cpp.

References abs, addnod_(), and left_().

07864 {
07865     /* System generated locals */
07866     int i__1, i__2;
07867 
07868     /* Local variables */
07869     static double d__;
07870     static int i__, j;
07871     static double d1, d2, d3;
07872     static int i0, lp, kt, ku, lpl, nku;
07873     static int nexti;
07874 
07875 
07876 /* *********************************************************** */
07877 
07878 /*                                              From STRIPACK */
07879 /*                                            Robert J. Renka */
07880 /*                                  Dept. of Computer Science */
07881 /*                                       Univ. of North Texas */
07882 /*                                           renka@cs.unt.edu */
07883 /*                                                   01/20/03 */
07884 
07885 /*   This is an alternative to TRMESH with the inclusion of */
07886 /* an efficient means of removing duplicate or nearly dupli- */
07887 /* cate nodes. */
07888 
07889 /*   This subroutine creates a Delaunay triangulation of a */
07890 /* set of N arbitrarily distributed points, referred to as */
07891 /* nodes, on the surface of the unit sphere.  Refer to Sub- */
07892 /* routine TRMESH for definitions and a list of additional */
07893 /* subroutines.  This routine is an alternative to TRMESH */
07894 /* with the inclusion of an efficient means of removing dup- */
07895 /* licate or nearly duplicate nodes. */
07896 
07897 /*   The algorithm has expected time complexity O(N*log(N)) */
07898 /* for random nodal distributions. */
07899 
07900 
07901 /* On input: */
07902 
07903 /*       N0 = Number of nodes, possibly including duplicates. */
07904 /*            N0 .GE. 3. */
07905 
07906 /*       TOL = Tolerance defining a pair of duplicate nodes: */
07907 /*             bound on the deviation from 1 of the cosine of */
07908 /*             the angle between the nodes.  Note that */
07909 /*             |1-cos(A)| is approximately A*A/2. */
07910 
07911 /* The above parameters are not altered by this routine. */
07912 
07913 /*       X,Y,Z = Arrays of length at least N0 containing the */
07914 /*               Cartesian coordinates of nodes.  (X(K),Y(K), */
07915 /*               Z(K)) is referred to as node K, and K is re- */
07916 /*               ferred to as a nodal index.  It is required */
07917 /*               that X(K)**2 + Y(K)**2 + Z(K)**2 = 1 for all */
07918 /*               K.  The first three nodes must not be col- */
07919 /*               linear (lie on a common great circle). */
07920 
07921 /*       LIST,LPTR = Arrays of length at least 6*N0-12. */
07922 
07923 /*       LEND = Array of length at least N0. */
07924 
07925 /*       INDX = Array of length at least N0. */
07926 
07927 /*       LCNT = Array of length at least N0 (length N is */
07928 /*              sufficient). */
07929 
07930 /*       NEAR,NEXT,DIST = Work space arrays of length at */
07931 /*                        least N0.  The space is used to */
07932 /*                        efficiently determine the nearest */
07933 /*                        triangulation node to each un- */
07934 /*                        processed node for use by ADDNOD. */
07935 
07936 /* On output: */
07937 
07938 /*       N = Number of nodes in the triangulation.  3 .LE. N */
07939 /*           .LE. N0, or N = 0 if IER < 0. */
07940 
07941 /*       X,Y,Z = Arrays containing the Cartesian coordinates */
07942 /*               of the triangulation nodes in the first N */
07943 /*               locations.  The original array elements are */
07944 /*               shifted down as necessary to eliminate dup- */
07945 /*               licate nodes. */
07946 
07947 /*       LIST = Set of nodal indexes which, along with LPTR, */
07948 /*              LEND, and LNEW, define the triangulation as a */
07949 /*              set of N adjacency lists -- counterclockwise- */
07950 /*              ordered sequences of neighboring nodes such */
07951 /*              that the first and last neighbors of a bound- */
07952 /*              ary node are boundary nodes (the first neigh- */
07953 /*              bor of an interior node is arbitrary).  In */
07954 /*              order to distinguish between interior and */
07955 /*              boundary nodes, the last neighbor of each */
07956 /*              boundary node is represented by the negative */
07957 /*              of its index. */
07958 
07959 /*       LPTR = Set of pointers (LIST indexes) in one-to-one */
07960 /*              correspondence with the elements of LIST. */
07961 /*              LIST(LPTR(I)) indexes the node which follows */
07962 /*              LIST(I) in cyclical counterclockwise order */
07963 /*              (the first neighbor follows the last neigh- */
07964 /*              bor). */
07965 
07966 /*       LEND = Set of pointers to adjacency lists.  LEND(K) */
07967 /*              points to the last neighbor of node K for */
07968 /*              K = 1,...,N.  Thus, LIST(LEND(K)) < 0 if and */
07969 /*              only if K is a boundary node. */
07970 
07971 /*       LNEW = Pointer to the first empty location in LIST */
07972 /*              and LPTR (list length plus one).  LIST, LPTR, */
07973 /*              LEND, and LNEW are not altered if IER < 0, */
07974 /*              and are incomplete if IER > 0. */
07975 
07976 /*       INDX = Array of output (triangulation) nodal indexes */
07977 /*              associated with input nodes.  For I = 1 to */
07978 /*              N0, INDX(I) is the index (for X, Y, and Z) of */
07979 /*              the triangulation node with the same (or */
07980 /*              nearly the same) coordinates as input node I. */
07981 
07982 /*       LCNT = Array of int weights (counts) associated */
07983 /*              with the triangulation nodes.  For I = 1 to */
07984 /*              N, LCNT(I) is the number of occurrences of */
07985 /*              node I in the input node set, and thus the */
07986 /*              number of duplicates is LCNT(I)-1. */
07987 
07988 /*       NEAR,NEXT,DIST = Garbage. */
07989 
07990 /*       IER = Error indicator: */
07991 /*             IER =  0 if no errors were encountered. */
07992 /*             IER = -1 if N0 < 3 on input. */
07993 /*             IER = -2 if the first three nodes are */
07994 /*                      collinear. */
07995 /*             IER = -3 if Subroutine ADDNOD returns an error */
07996 /*                      flag.  This should not occur. */
07997 
07998 /* Modules required by TRMSH3:  ADDNOD, BDYADD, COVSPH, */
07999 /*                                INSERT, INTADD, JRAND, */
08000 /*                                LEFT, LSTPTR, STORE, SWAP, */
08001 /*                                SWPTST, TRFIND */
08002 
08003 /* Intrinsic function called by TRMSH3:  ABS */
08004 
08005 /* *********************************************************** */
08006 
08007 
08008 /* Local parameters: */
08009 
08010 /* D =        (Negative cosine of) distance from node KT to */
08011 /*              node I */
08012 /* D1,D2,D3 = Distances from node KU to nodes 1, 2, and 3, */
08013 /*              respectively */
08014 /* I,J =      Nodal indexes */
08015 /* I0 =       Index of the node preceding I in a sequence of */
08016 /*              unprocessed nodes:  I = NEXT(I0) */
08017 /* KT =       Index of a triangulation node */
08018 /* KU =       Index of an unprocessed node and DO-loop index */
08019 /* LP =       LIST index (pointer) of a neighbor of KT */
08020 /* LPL =      Pointer to the last neighbor of KT */
08021 /* NEXTI =    NEXT(I) */
08022 /* NKU =      NEAR(KU) */
08023 
08024     /* Parameter adjustments */
08025     --dist;
08026     --next;
08027     --near__;
08028     --indx;
08029     --lend;
08030     --z__;
08031     --y;
08032     --x;
08033     --list;
08034     --lptr;
08035     --lcnt;
08036 
08037     /* Function Body */
08038     if (*n0 < 3) {
08039         *n = 0;
08040         *ier = -1;
08041         return 0;
08042     }
08043 
08044 /* Store the first triangle in the linked list. */
08045 
08046     if (! left_(&x[1], &y[1], &z__[1], &x[2], &y[2], &z__[2], &x[3], &y[3], &
08047             z__[3])) {
08048 
08049 /*   The first triangle is (3,2,1) = (2,1,3) = (1,3,2). */
08050 
08051         list[1] = 3;
08052         lptr[1] = 2;
08053         list[2] = -2;
08054         lptr[2] = 1;
08055         lend[1] = 2;
08056 
08057         list[3] = 1;
08058         lptr[3] = 4;
08059         list[4] = -3;
08060         lptr[4] = 3;
08061         lend[2] = 4;
08062 
08063         list[5] = 2;
08064         lptr[5] = 6;
08065         list[6] = -1;
08066         lptr[6] = 5;
08067         lend[3] = 6;
08068 
08069     } else if (! left_(&x[2], &y[2], &z__[2], &x[1], &y[1], &z__[1], &x[3], &
08070             y[3], &z__[3])) {
08071 
08072 /*   The first triangle is (1,2,3):  3 Strictly Left 1->2, */
08073 /*     i.e., node 3 lies in the left hemisphere defined by */
08074 /*     arc 1->2. */
08075 
08076         list[1] = 2;
08077         lptr[1] = 2;
08078         list[2] = -3;
08079         lptr[2] = 1;
08080         lend[1] = 2;
08081 
08082         list[3] = 3;
08083         lptr[3] = 4;
08084         list[4] = -1;
08085         lptr[4] = 3;
08086         lend[2] = 4;
08087 
08088         list[5] = 1;
08089         lptr[5] = 6;
08090         list[6] = -2;
08091         lptr[6] = 5;
08092         lend[3] = 6;
08093 
08094 
08095     } else {
08096 
08097 /*   The first three nodes are collinear. */
08098 
08099         *n = 0;
08100         *ier = -2;
08101         return 0;
08102     }
08103 
08104     //printf("pass check colinear\n");
08105 
08106 /* Initialize LNEW, INDX, and LCNT, and test for N = 3. */
08107 
08108     *lnew = 7;
08109     indx[1] = 1;
08110     indx[2] = 2;
08111     indx[3] = 3;
08112     lcnt[1] = 1;
08113     lcnt[2] = 1;
08114     lcnt[3] = 1;
08115     if (*n0 == 3) {
08116         *n = 3;
08117         *ier = 0;
08118         return 0;
08119     }
08120 
08121 /* A nearest-node data structure (NEAR, NEXT, and DIST) is */
08122 /*   used to obtain an expected-time (N*log(N)) incremental */
08123 /*   algorithm by enabling constant search time for locating */
08124 /*   each new node in the triangulation. */
08125 
08126 /* For each unprocessed node KU, NEAR(KU) is the index of the */
08127 /*   triangulation node closest to KU (used as the starting */
08128 /*   point for the search in Subroutine TRFIND) and DIST(KU) */
08129 /*   is an increasing function of the arc length (angular */
08130 /*   distance) between nodes KU and NEAR(KU):  -Cos(a) for */
08131 /*   arc length a. */
08132 
08133 /* Since it is necessary to efficiently find the subset of */
08134 /*   unprocessed nodes associated with each triangulation */
08135 /*   node J (those that have J as their NEAR entries), the */
08136 /*   subsets are stored in NEAR and NEXT as follows:  for */
08137 /*   each node J in the triangulation, I = NEAR(J) is the */
08138 /*   first unprocessed node in J's set (with I = 0 if the */
08139 /*   set is empty), L = NEXT(I) (if I > 0) is the second, */
08140 /*   NEXT(L) (if L > 0) is the third, etc.  The nodes in each */
08141 /*   set are initially ordered by increasing indexes (which */
08142 /*   maximizes efficiency) but that ordering is not main- */
08143 /*   tained as the data structure is updated. */
08144 
08145 /* Initialize the data structure for the single triangle. */
08146 
08147     near__[1] = 0;
08148     near__[2] = 0;
08149     near__[3] = 0;
08150     for (ku = *n0; ku >= 4; --ku) {
08151         d1 = -(x[ku] * x[1] + y[ku] * y[1] + z__[ku] * z__[1]);
08152         d2 = -(x[ku] * x[2] + y[ku] * y[2] + z__[ku] * z__[2]);
08153         d3 = -(x[ku] * x[3] + y[ku] * y[3] + z__[ku] * z__[3]);
08154         if (d1 <= d2 && d1 <= d3) {
08155             near__[ku] = 1;
08156             dist[ku] = d1;
08157             next[ku] = near__[1];
08158             near__[1] = ku;
08159         } else if (d2 <= d1 && d2 <= d3) {
08160             near__[ku] = 2;
08161             dist[ku] = d2;
08162             next[ku] = near__[2];
08163             near__[2] = ku;
08164         } else {
08165             near__[ku] = 3;
08166             dist[ku] = d3;
08167             next[ku] = near__[3];
08168             near__[3] = ku;
08169         }
08170 /* L1: */
08171     }
08172 
08173 /* Loop on unprocessed nodes KU.  KT is the number of nodes */
08174 /*   in the triangulation, and NKU = NEAR(KU). */
08175 
08176     kt = 3;
08177     i__1 = *n0;
08178     for (ku = 4; ku <= i__1; ++ku) {
08179         nku = near__[ku];
08180 
08181 /* Remove KU from the set of unprocessed nodes associated */
08182 /*   with NEAR(KU). */
08183         i__ = nku;
08184         if (near__[i__] == ku) {
08185             near__[i__] = next[ku];
08186         } else {
08187             i__ = near__[i__];
08188 L2:
08189             i0 = i__;
08190             i__ = next[i0];
08191             if (i__ != ku) {
08192                 goto L2;
08193             }
08194             next[i0] = next[ku];
08195         }
08196         near__[ku] = 0;
08197 
08198 /* Bypass duplicate nodes. */
08199 
08200         if (dist[ku] <= *tol - 1.) {
08201             indx[ku] = -nku;
08202             ++lcnt[nku];
08203             goto L6;
08204         }
08205 
08206 
08207 /* Add a new triangulation node KT with LCNT(KT) = 1. */
08208         ++kt;
08209         x[kt] = x[ku];
08210         y[kt] = y[ku];
08211         z__[kt] = z__[ku];
08212         indx[ku] = kt;
08213         lcnt[kt] = 1;
08214         addnod_(&nku, &kt, &x[1], &y[1], &z__[1], &list[1], &lptr[1], &lend[1]
08215                 , lnew, ier);
08216         if (*ier != 0) {
08217             *n = 0;
08218             *ier = -3;
08219             return 0;
08220         }
08221 
08222 /* Loop on neighbors J of node KT. */
08223 
08224         lpl = lend[kt];
08225         lp = lpl;
08226 L3:
08227         lp = lptr[lp];
08228         j = (i__2 = list[lp], abs(i__2));
08229 
08230 /* Loop on elements I in the sequence of unprocessed nodes */
08231 /*   associated with J:  KT is a candidate for replacing J */
08232 /*   as the nearest triangulation node to I.  The next value */
08233 /*   of I in the sequence, NEXT(I), must be saved before I */
08234 /*   is moved because it is altered by adding I to KT's set. */
08235 
08236         i__ = near__[j];
08237 L4:
08238         if (i__ == 0) {
08239             goto L5;
08240         }
08241         nexti = next[i__];
08242 
08243 /* Test for the distance from I to KT less than the distance */
08244 /*   from I to J. */
08245 
08246         d__ = -(x[i__] * x[kt] + y[i__] * y[kt] + z__[i__] * z__[kt]);
08247         if (d__ < dist[i__]) {
08248 
08249 /* Replace J by KT as the nearest triangulation node to I: */
08250 /*   update NEAR(I) and DIST(I), and remove I from J's set */
08251 /*   of unprocessed nodes and add it to KT's set. */
08252 
08253             near__[i__] = kt;
08254             dist[i__] = d__;
08255             if (i__ == near__[j]) {
08256                 near__[j] = nexti;
08257             } else {
08258                 next[i0] = nexti;
08259             }
08260             next[i__] = near__[kt];
08261             near__[kt] = i__;
08262         } else {
08263             i0 = i__;
08264         }
08265 
08266 /* Bottom of loop on I. */
08267 
08268         i__ = nexti;
08269         goto L4;
08270 
08271 /* Bottom of loop on neighbors J. */
08272 
08273 L5:
08274         if (lp != lpl) {
08275             goto L3;
08276         }
08277 L6:
08278         ;
08279     }
08280     *n = kt;
08281     *ier = 0;
08282     return 0;
08283 } /* trmsh3_ */

vector< float > Util::twoD_fine_ali ( EMData image,
EMData refim,
EMData mask,
float  ang,
float  sxs,
float  sys 
) [static]

Definition at line 19250 of file util_sparx.cpp.

References EMAN::EMData::cmp(), dt, EMAN::EMData::rot_scale_trans2D(), setulb_(), and SIXTY.

19250                                                                                                              {
19251 
19252         EMData *rot;
19253 
19254         const int nmax=3, mmax=3;
19255         char task[60], csave[60];
19256         long int lsave[4];
19257         long int n, m, iprint, nbd[nmax], iwa[3*nmax], isave[44];
19258         double f, f1, f2, f3, factr, pgtol, x[nmax], l[nmax], u[nmax], g[nmax], dsave[29], wa[2*mmax*nmax+4*nmax+12*mmax*mmax+12*mmax];
19259         long int SIXTY=60;
19260 
19261         //     We wish to have no output.
19262         iprint = -1;
19263 
19264         //c     We specify the tolerances in the stopping criteria.
19265         factr=1.0e1;
19266         pgtol=1.0e-5;
19267 
19268         //     We specify the dimension n of the sample problem and the number
19269         //        m of limited memory corrections stored.  (n and m should not
19270         //        exceed the limits nmax and mmax respectively.)
19271         n=3;
19272         m=3;
19273 
19274         //     We now provide nbd which defines the bounds on the variables:
19275         //                    l   specifies the lower bounds,
19276         //                    u   specifies the upper bounds.
19277         //                    x   specifies the initial guess
19278         x[0] = ang; nbd[0] = 2; l[0] = ang-2.0; u[0] = ang+2.0;
19279         x[1] = sxs; nbd[1] = 2; l[1] = sxs-1.5; u[1] = sxs+1.5;
19280         x[2] = sys; nbd[2] = 2; l[2] = sys-1.5; u[2] = sys+1.5;
19281 
19282 
19283         //     We start the iteration by initializing task.
19284         // (**MUST clear remaining chars in task with spaces (else crash)!**)
19285         strcpy(task,"START");
19286         for (int i=5;i<60;i++)  task[i]=' ';
19287 
19288         //     This is the call to the L-BFGS-B code.
19289         // (* call the L-BFGS-B routine with task='START' once before loop *)
19290         setulb_(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,task,&iprint,csave,lsave,isave,dsave,SIXTY,SIXTY);
19291         //int step = 1;
19292 
19293         // (* while routine returns "FG" or "NEW_X" in task, keep calling it *)
19294         while (strncmp(task,"FG",2)==0 || strncmp(task,"NEW_X",5)==0) {
19295 
19296                 if (strncmp(task,"FG",2)==0) {
19297                 //   the minimization routine has returned to request the
19298                 //   function f and gradient g values at the current x
19299 
19300                 //        Compute function value f for the sample problem.
19301                 rot = new EMData();
19302                 rot = image->rot_scale_trans2D((float)x[0], (float)x[1], (float)x[2], 1.0f);
19303                 f = rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19304                 //f = -f;
19305                 delete rot;
19306 
19307                 //        Compute gradient g for the sample problem.
19308                 float dt = 1.0e-3f;
19309                 rot = new EMData();
19310                 rot = image->rot_scale_trans2D((float)x[0]+dt, (float)x[1], (float)x[2], 1.0f);
19311                 f1 = rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19312                 //f1 = -f1;
19313                 g[0] = (f1-f)/dt;
19314                 delete rot;
19315 
19316                 dt = 1.0e-2f;
19317                 rot = new EMData();
19318                 rot = image->rot_scale_trans2D((float)x[0], (float)x[1]+dt, (float)x[2], 1.0f);
19319                 f2 = rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19320                 //f2 = -f2;
19321                 g[1] = (f2-f)/dt;
19322                 delete rot;
19323 
19324                 rot = new EMData();
19325                 rot = image->rot_scale_trans2D((float)x[0], (float)x[1], (float)x[2]+dt, 1.0f);
19326                 f3 = rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19327                 //f3 = -f3;
19328                 g[2] = (f3-f)/dt;
19329                 delete rot;
19330                 }
19331 
19332                 //c          go back to the minimization routine.
19333                 setulb_(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,task,&iprint,csave,lsave,isave,dsave,SIXTY,SIXTY);
19334                 //step++;
19335         }
19336 
19337         //printf("Total step is %d\n", step);
19338         vector<float> res;
19339         res.push_back(static_cast<float>(x[0]));
19340         res.push_back(static_cast<float>(x[1]));
19341         res.push_back(static_cast<float>(x[2]));
19342         //res.push_back(step);
19343         return res;
19344 }

vector< float > Util::twoD_fine_ali_G ( EMData image,
EMData refim,
EMData mask,
Util::KaiserBessel kb,
float  ang,
float  sxs,
float  sys 
) [static]

Definition at line 19346 of file util_sparx.cpp.

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

19346                                                                                                                                      {
19347 
19348         EMData *rot;
19349 
19350         const int nmax=3, mmax=3;
19351         char task[60], csave[60];
19352         long int lsave[4];
19353         long int n, m, iprint, nbd[nmax], iwa[3*nmax], isave[44];
19354         double f, f1, f2, f3, factr, pgtol, x[nmax], l[nmax], u[nmax], g[nmax], dsave[29], wa[2*mmax*nmax+4*nmax+12*mmax*mmax+12*mmax];
19355         long int SIXTY=60;
19356 
19357         //     We wish to have no output.
19358         iprint = -1;
19359 
19360         //c     We specify the tolerances in the stopping criteria.
19361         factr=1.0e1;
19362         pgtol=1.0e-5;
19363 
19364         //     We specify the dimension n of the sample problem and the number
19365         //        m of limited memory corrections stored.  (n and m should not
19366         //        exceed the limits nmax and mmax respectively.)
19367         n=3;
19368         m=3;
19369 
19370         //     We now provide nbd which defines the bounds on the variables:
19371         //                    l   specifies the lower bounds,
19372         //                    u   specifies the upper bounds.
19373         //                    x   specifies the initial guess
19374         x[0] = ang; nbd[0] = 2; l[0] = ang-2.0; u[0] = ang+2.0;
19375         x[1] = sxs; nbd[1] = 2; l[1] = sxs-1.5; u[1] = sxs+1.5;
19376         x[2] = sys; nbd[2] = 2; l[2] = sys-1.5; u[2] = sys+1.5;
19377 
19378 
19379         //     We start the iteration by initializing task.
19380         // (**MUST clear remaining chars in task with spaces (else crash)!**)
19381         strcpy(task,"START");
19382         for (int i=5;i<60;i++)  task[i]=' ';
19383 
19384         //     This is the call to the L-BFGS-B code.
19385         // (* call the L-BFGS-B routine with task='START' once before loop *)
19386         setulb_(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,task,&iprint,csave,lsave,isave,dsave,SIXTY,SIXTY);
19387         //int step = 1;
19388 
19389         // (* while routine returns "FG" or "NEW_X" in task, keep calling it *)
19390         while (strncmp(task,"FG",2)==0 || strncmp(task,"NEW_X",5)==0) {
19391 
19392                 if (strncmp(task,"FG",2)==0) {
19393                 //   the minimization routine has returned to request the
19394                 //   function f and gradient g values at the current x
19395 
19396                 //        Compute function value f for the sample problem.
19397                 rot = new EMData();
19398                 rot = image->rot_scale_conv7((float)(x[0]*pi/180), (float)x[1], (float)x[2], kb, 1.0f);
19399                 f = rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19400                 //f = -f;
19401                 delete rot;
19402 
19403                 //        Compute gradient g for the sample problem.
19404                 float dt = 1.0e-3f;
19405                 rot = new EMData();
19406                 rot = image->rot_scale_conv7((float)((x[0]+dt)*pi/180), (float)x[1], (float)x[2], kb, 1.0f);
19407                 f1 = rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19408                 //f1 = -f1;
19409                 g[0] = (f1-f)/dt;
19410                 delete rot;
19411 
19412                 rot = new EMData();
19413                 rot = image->rot_scale_conv7((float)(x[0]*pi/180), (float)x[1]+dt, (float)x[2], kb, 1.0);
19414                 f2 = rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19415                 //f2 = -f2;
19416                 g[1] = (f2-f)/dt;
19417                 delete rot;
19418 
19419                 rot = new EMData();
19420                 rot = image->rot_scale_conv7((float)(x[0]*pi/180), (float)x[1], (float)x[2]+dt, kb, 1.0f);
19421                 f3 = rot->cmp("sqeuclidean", refim, Dict("mask", mask));
19422                 //f3 = -f3;
19423                 g[2] = (f3-f)/dt;
19424                 delete rot;
19425                 }
19426 
19427                 //c          go back to the minimization routine.
19428                 setulb_(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,task,&iprint,csave,lsave,isave,dsave,SIXTY,SIXTY);
19429                 //step++;
19430         }
19431 
19432         //printf("Total step is %d\n", step);
19433         vector<float> res;
19434         res.push_back(static_cast<float>(x[0]));
19435         res.push_back(static_cast<float>(x[1]));
19436         res.push_back(static_cast<float>(x[2]));
19437         //res.push_back(step);
19438         return res;
19439 }

vector< float > Util::twoD_fine_ali_SD ( EMData image,
EMData refim,
EMData mask,
float  ang,
float  sxs,
float  sys 
) [static]

Definition at line 19598 of file util_sparx.cpp.

References ccc_images(), and Steepda().

19598                                                                                                                 {
19599 
19600         double  x[4];
19601         int n;
19602         int l = 3;
19603         int m = 200;
19604         double e = 1e-9;
19605         double step = 0.01;
19606         float (*my_func)(EMData* , EMData* , EMData* , float , float , float) = ccc_images;
19607 
19608         x[1] = ang;
19609         x[2] = sxs;
19610         x[3] = sys;
19611 
19612         Steepda(x, step, e, l, m, &n, my_func, image, refim, mask);   // Call steepest descent optimization subroutine
19613         //printf("Took %d steps\n", n);
19614 
19615         vector<float> res;
19616         res.push_back(static_cast<float>(x[1]));
19617         res.push_back(static_cast<float>(x[2]));
19618         res.push_back(static_cast<float>(x[3]));
19619         res.push_back(static_cast<float>(n));
19620         return res;
19621 }

vector< float > Util::twoD_fine_ali_SD_G ( EMData image,
EMData refim,
EMData mask,
Util::KaiserBessel kb,
float  ang,
float  sxs,
float  sys 
) [static]

Definition at line 19849 of file util_sparx.cpp.

References ccc_images_G(), and Steepda_G().

19849                                                                                                                                         {
19850 
19851         double  x[4];
19852         int n;
19853         int l = 3;
19854         int m = 200;
19855         double e = 1e-9;
19856         double step = 0.001;
19857         float (*my_func)(EMData* , EMData* , EMData* , Util::KaiserBessel&, float , float , float) = ccc_images_G;
19858 
19859         x[1] = ang;
19860         x[2] = sxs;
19861         x[3] = sys;
19862 
19863         Steepda_G(x, step, e, l, m, &n, my_func, image, refim, mask, kb);   // Call steepest descent optimization subroutine
19864         //printf("Took %d steps\n", n);
19865 
19866         vector<float> res;
19867         res.push_back(static_cast<float>(x[1]));
19868         res.push_back(static_cast<float>(x[2]));
19869         res.push_back(static_cast<float>(x[3]));
19870         res.push_back(static_cast<float>(n));
19871         return res;
19872 }

vector< float > Util::twoD_to_3D_ali ( EMData volft,
Util::KaiserBessel kb,
EMData refim,
EMData mask,
float  phi,
float  theta,
float  psi,
float  sxs,
float  sxy 
) [static]

Definition at line 19441 of file util_sparx.cpp.

References EMAN::EMData::cmp(), dt, EMAN::EMData::extract_plane(), proj, setulb_(), and SIXTY.

19441                                                                                                                                                             {
19442 
19443         EMData *proj, *proj2;
19444 
19445         const int nmax=5, mmax=5;
19446         char task[60], csave[60];
19447         long int lsave[4];
19448         long int n, m, iprint, nbd[nmax], iwa[3*nmax], isave[44];
19449         double f, ft, factr, pgtol, x[nmax], l[nmax], u[nmax], g[nmax], dsave[29], wa[2*mmax*nmax+4*nmax+12*mmax*mmax+12*mmax];
19450         long int SIXTY=60;
19451 
19452         //     We wish to have no output.
19453         iprint = -1;
19454 
19455         //c     We specify the tolerances in the stopping criteria.
19456         factr=1.0e1;
19457         pgtol=1.0e-5;
19458 
19459         //     We specify the dimension n of the sample problem and the number
19460         //        m of limited memory corrections stored.  (n and m should not
19461         //        exceed the limits nmax and mmax respectively.)
19462         n=5;
19463         m=5;
19464 
19465         //     We now provide nbd which defines the bounds on the variables:
19466         //                    l   specifies the lower bounds,
19467         //                    u   specifies the upper bounds.
19468         //                    x   specifies the initial guess
19469         x[0] = phi;     nbd[0] = 2;     l[0] = phi-2.0;         u[0] = phi+2.0;
19470         x[1] = theta;   nbd[1] = 2;     l[1] = theta-2.0;       u[1] = theta+2.0;
19471         x[2] = psi;     nbd[2] = 2;     l[2] = psi-2.0;         u[2] = psi+2.0;
19472         x[3] = sxs;     nbd[3] = 2;     l[3] = sxs-2.0;         u[3] = sxs+2.0;
19473         x[4] = sys;     nbd[4] = 2;     l[4] = sys-2.0;         u[4] = sys+2.0;
19474 
19475 
19476         //     We start the iteration by initializing task.
19477         // (**MUST clear remaining chars in task with spaces (else crash)!**)
19478         strcpy(task,"START");
19479         for (int i=5;i<60;i++)  task[i]=' ';
19480 
19481         //     This is the call to the L-BFGS-B code.
19482         // (* call the L-BFGS-B routine with task='START' once before loop *)
19483         setulb_(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,task,&iprint,csave,lsave,isave,dsave,SIXTY,SIXTY);
19484         int step = 1;
19485 
19486         // (* while routine returns "FG" or "NEW_X" in task, keep calling it *)
19487         while (strncmp(task,"FG",2)==0 || strncmp(task,"NEW_X",5)==0) {
19488 
19489                 if (strncmp(task,"FG",2)==0) {
19490                 //   the minimization routine has returned to request the
19491                 //   function f and gradient g values at the current x
19492 
19493                 //        Compute function value f for the sample problem.
19494                 proj = new EMData();
19495                 proj2 = new EMData();
19496                 proj = volft->extract_plane(Transform(Dict("type", "SPIDER", "phi", (float)x[0], "theta", (float)x[1], "psi", (float)x[2])), kb);
19497                 proj->fft_shuffle();
19498                 proj->center_origin_fft();
19499                 proj->process_inplace("filter.shift", Dict("x_shift", x[3], "y_shift", x[4], "z_shift", 0.0f));
19500                 proj->do_ift_inplace();
19501                 int M = proj->get_ysize()/2;
19502                 proj2 = proj->window_center(M);
19503                 f = proj2->cmp("sqeuclidean", refim, Dict("mask", mask));
19504                 //f = -f;
19505                 delete proj;
19506                 delete proj2;
19507 
19508                 //        Compute gradient g for the sample problem.
19509                 float dt = 1.0e-3f;
19510                 proj = new EMData();
19511                 proj2 = new EMData();
19512                 proj = volft->extract_plane(Transform(Dict("type", "SPIDER", "phi", (float)x[0]+dt, "theta", (float)x[1], "psi", (float)x[2])), kb);
19513                 proj->fft_shuffle();
19514                 proj->center_origin_fft();
19515                 proj->process_inplace("filter.shift", Dict("x_shift", x[3], "y_shift", x[4], "z_shift", 0.0f));
19516                 proj->do_ift_inplace();
19517                 proj2 = proj->window_center(M);
19518                 ft = proj2->cmp("sqeuclidean", refim, Dict("mask", mask));
19519                 //ft = -ft;
19520                 delete proj;
19521                 delete proj2;
19522                 g[0] = (ft-f)/dt;
19523 
19524                 proj = new EMData();
19525                 proj2 = new EMData();
19526                 proj = volft->extract_plane(Transform(Dict("type", "SPIDER", "phi", (float)x[0], "theta", (float)x[1]+dt, "psi", (float)x[2])), kb);
19527                 proj->fft_shuffle();
19528                 proj->center_origin_fft();
19529                 proj->process_inplace("filter.shift", Dict("x_shift", x[3], "y_shift", x[4], "z_shift", 0.0f));
19530                 proj->do_ift_inplace();
19531                 proj2 = proj->window_center(M);
19532                 ft = proj2->cmp("sqeuclidean", refim, Dict("mask", mask));
19533                 //ft = -ft;
19534                 delete proj;
19535                 delete proj2;
19536                 g[1] = (ft-f)/dt;
19537 
19538                 proj = new EMData();
19539                 proj2 = new EMData();
19540                 proj = volft->extract_plane(Transform(Dict("type", "SPIDER", "phi", (float)x[0], "theta", (float)x[1], "psi", (float)x[2]+dt)), kb);
19541                 proj->fft_shuffle();
19542                 proj->center_origin_fft();
19543                 proj->process_inplace("filter.shift", Dict("x_shift", x[3], "y_shift", x[4], "z_shift", 0.0f));
19544                 proj->do_ift_inplace();
19545                 proj2 = proj->window_center(M);
19546                 ft = proj2->cmp("sqeuclidean", refim, Dict("mask", mask));
19547                 //ft = -ft;
19548                 delete proj;
19549                 delete proj2;
19550                 g[2] = (ft-f)/dt;
19551 
19552                 proj = new EMData();
19553                 proj2 = new EMData();
19554                 proj = volft->extract_plane(Transform(Dict("type", "SPIDER", "phi", (float)x[0], "theta", (float)x[1], "psi", (float)x[2])), kb);
19555                 proj->fft_shuffle();
19556                 proj->center_origin_fft();
19557                 proj->process_inplace("filter.shift", Dict("x_shift", x[3]+dt, "y_shift", x[4], "z_shift", 0.0f));
19558                 proj->do_ift_inplace();
19559                 proj2 = proj->window_center(M);
19560                 ft = proj2->cmp("sqeuclidean", refim, Dict("mask", mask));
19561                 //ft = -ft;
19562                 delete proj;
19563                 delete proj2;
19564                 g[3] = (ft-f)/dt;
19565 
19566                 proj = new EMData();
19567                 proj2 = new EMData();
19568                 proj = volft->extract_plane(Transform(Dict("type", "SPIDER", "phi", (float)x[0], "theta", (float)x[1], "psi", (float)x[2])), kb);
19569                 proj->fft_shuffle();
19570                 proj->center_origin_fft();
19571                 proj->process_inplace("filter.shift", Dict("x_shift", x[3], "y_shift", x[4]+dt, "z_shift", 0.0f));
19572                 proj->do_ift_inplace();
19573                 proj2 = proj->window_center(M);
19574                 ft = proj2->cmp("sqeuclidean", refim, Dict("mask", mask));
19575                 //ft = -ft;
19576                 delete proj;
19577                 delete proj2;
19578                 g[4] = (ft-f)/dt;
19579                 }
19580 
19581                 //c          go back to the minimization routine.
19582                 setulb_(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,task,&iprint,csave,lsave,isave,dsave,SIXTY,SIXTY);
19583                 step++;
19584         }
19585 
19586         //printf("Total step is %d\n", step);
19587         vector<float> res;
19588         res.push_back(static_cast<float>(x[0]));
19589         res.push_back(static_cast<float>(x[1]));
19590         res.push_back(static_cast<float>(x[2]));
19591         res.push_back(static_cast<float>(x[3]));
19592         res.push_back(static_cast<float>(x[4]));
19593         //res.push_back(step);
19594         return res;
19595 }

EMData * Util::TwoDTestFunc ( int  Size,
float  p,
float  q,
float  a,
float  b,
int  flag = 0,
float  alphaDeg = 0 
) [static]

Creates a Two D Test Pattern.

Parameters:
[in] Size must be odd
[in] p the x frequency
[in] q the y frequency
[in] a the x falloff
b the y falloff
flag 
[in] alphaDeg the projection angle
Returns:
The 2D test pattern in real space, fourier space, or the projection in real or fourier space or the FH of the pattern

Definition at line 239 of file util_sparx.cpp.

References C, ENTERFUNC, EXITFUNC, pi, real2FH(), set_complex(), EMAN::EMData::set_complex(), EMAN::EMData::set_fftodd(), set_ri(), EMAN::EMData::set_ri(), EMAN::EMData::set_shuffled(), EMAN::EMData::set_size(), sqrt(), EMAN::EMData::to_zero(), update(), EMAN::EMData::update(), x, and y.

00240 {
00241         ENTERFUNC;
00242         int Mid= (Size+1)/2;
00243 
00244         if (flag==0) { // This is the real function
00245                 EMData* ImBW = new EMData();
00246                 ImBW->set_size(Size,Size,1);
00247                 ImBW->to_zero();
00248 
00249                 float tempIm;
00250                 float x,y;
00251 
00252                 for (int ix=(1-Mid);  ix<Mid; ix++){
00253                         for (int iy=(1-Mid);  iy<Mid; iy++){
00254                                 x = (float)ix;
00255                                 y = (float)iy;
00256                         tempIm= static_cast<float>( (1/(2*M_PI)) * cos(p*x)* cos(q*y) * exp(-.5*x*x/(a*a))* exp(-.5*y*y/(b*b)) );
00257                                 (*ImBW)(ix+Mid-1,iy+Mid-1) = tempIm * exp(.5f*p*p*a*a)* exp(.5f*q*q*b*b);
00258                         }
00259                 }
00260                 ImBW->update();
00261                 ImBW->set_complex(false);
00262                 ImBW->set_ri(true);
00263 
00264                 return ImBW;
00265         }
00266         else if (flag==1) {  // This is the Fourier Transform
00267                 EMData* ImBWFFT = new EMData();
00268                 ImBWFFT ->set_size(2*Size,Size,1);
00269                 ImBWFFT ->to_zero();
00270 
00271                 float r,s;
00272 
00273                 for (int ir=(1-Mid);  ir<Mid; ir++){
00274                         for (int is=(1-Mid);  is<Mid; is++){
00275                                 r = (float)ir;
00276                                 s = (float)is;
00277                         (*ImBWFFT)(2*(ir+Mid-1),is+Mid-1)= cosh(p*r*a*a) * cosh(q*s*b*b) *
00278                                 exp(-.5f*r*r*a*a)* exp(-.5f*s*s*b*b);
00279                         }
00280                 }
00281                 ImBWFFT->update();
00282                 ImBWFFT->set_complex(true);
00283                 ImBWFFT->set_ri(true);
00284                 ImBWFFT->set_shuffled(true);
00285                 ImBWFFT->set_fftodd(true);
00286 
00287                 return ImBWFFT;
00288         }
00289         else if (flag==2 || flag==3) { //   This is the projection in Real Space
00290                 float alpha = static_cast<float>( alphaDeg*M_PI/180.0 );
00291                 float C=cos(alpha);
00292                 float S=sin(alpha);
00293                 float D= sqrt(S*S*b*b + C*C*a*a);
00294                 //float D2 = D*D;   PAP - to get rid of warning
00295 
00296                 float P = p * C *a*a/D ;
00297                 float Q = q * S *b*b/D ;
00298 
00299                 if (flag==2) {
00300                         EMData* pofalpha = new EMData();
00301                         pofalpha ->set_size(Size,1,1);
00302                         pofalpha ->to_zero();
00303 
00304                         float Norm0 =  D*(float)sqrt(2*pi);
00305                         float Norm1 =  exp( .5f*(P+Q)*(P+Q)) / Norm0 ;
00306                         float Norm2 =  exp( .5f*(P-Q)*(P-Q)) / Norm0 ;
00307                         float sD;
00308 
00309                         for (int is=(1-Mid);  is<Mid; is++){
00310                                 sD = is/D ;
00311                                 (*pofalpha)(is+Mid-1) =  Norm1 * exp(-.5f*sD*sD)*cos(sD*(P+Q))
00312                          + Norm2 * exp(-.5f*sD*sD)*cos(sD*(P-Q));
00313                         }
00314                         pofalpha-> update();
00315                         pofalpha-> set_complex(false);
00316                         pofalpha-> set_ri(true);
00317 
00318                         return pofalpha;
00319                 }
00320                 if (flag==3) { // This is the projection in Fourier Space
00321                         float vD;
00322 
00323                         EMData* pofalphak = new EMData();
00324                         pofalphak ->set_size(2*Size,1,1);
00325                         pofalphak ->to_zero();
00326 
00327                         for (int iv=(1-Mid);  iv<Mid; iv++){
00328                                 vD = iv*D ;
00329                                 (*pofalphak)(2*(iv+Mid-1)) =  exp(-.5f*vD*vD)*(cosh(vD*(P+Q)) + cosh(vD*(P-Q)) );
00330                         }
00331                         pofalphak-> update();
00332                         pofalphak-> set_complex(false);
00333                         pofalphak-> set_ri(true);
00334 
00335                         return pofalphak;
00336                 }
00337         }
00338         else if (flag==4) {
00339                 cout <<" FH under construction";
00340                 EMData* OutFT= TwoDTestFunc(Size, p, q, a, b, 1);
00341                 EMData* TryFH= OutFT -> real2FH(4.0);
00342                 return TryFH;
00343         } else {
00344                 cout <<" flag must be 0,1,2,3, or 4";
00345         }
00346 
00347         EXITFUNC;
00348         return 0;
00349 }

void Util::update_fav ( EMData ave,
EMData dat,
float  tot,
int  mirror,
vector< int >  numr 
) [static]

Definition at line 4503 of file util_sparx.cpp.

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

04503                                                                                           {
04504         int nring = numr.size()/3;
04505         float *ave = avep->get_data();
04506         float *dat = datp->get_data();
04507         int i, j, numr3i, np;
04508         float  arg, cs, si;
04509         int maxrin = numr(3,nring);
04510         if(mirror == 1) { //for mirrored data has to be conjugated
04511                 for (i=1; i<=nring; i++) {
04512                         numr3i = numr(3,i);
04513                         np     = numr(2,i)-1;
04514                         ave[np]   += dat[np];
04515                         ave[np+1] += static_cast<float>( dat[np+1]*cos(PI2*(tot-1.0f)/2.0f*numr3i/maxrin) );
04516                         for (j=2; j<numr3i; j=j+2) {
04517                                 arg = static_cast<float>( PI2*(tot-1.)*(j/2)/maxrin );
04518                                 cs = cos(arg);
04519                                 si = sin(arg);
04520                                 //complex(data[np + j],data[np + j +1])*complex(cos(arg),sin(arg))
04521                                 ave[np + j]    += dat[np + j]*cs - dat[np + j +1]*si;
04522                                 ave[np + j +1] -= dat[np + j]*si + dat[np + j +1]*cs;
04523                         }
04524                 }
04525         } else {
04526                 for (i=1; i<=nring; i++) {
04527                         numr3i = numr(3,i);
04528                         np     = numr(2,i)-1;
04529                         ave[np]   += dat[np];
04530                         ave[np+1] += static_cast<float>( dat[np+1]*cos(PI2*(tot-1.0f)/2.0f*numr3i/maxrin) );
04531                         for (j=2; j<numr3i; j=j+2) {
04532                                 arg = static_cast<float>( PI2*(tot-1.)*(j/2)/maxrin );
04533                                 cs = cos(arg);
04534                                 si = sin(arg);
04535                                 //complex(data[np + j],data[np + j +1])*complex(cos(arg),sin(arg))
04536                                 ave[np + j]    += dat[np + j]*cs - dat[np + j +1]*si;
04537                                 ave[np + j +1] += dat[np + j]*si + dat[np + j +1]*cs;
04538                         }
04539                 }
04540         }
04541         avep->update();
04542         EXITFUNC;
04543 }

static double EMAN::Util::var ( double *  x,
int  n 
) [inline, static]

Definition at line 1092 of file util.h.

Referenced by multi_align_error_func2().

01100       : set ts=4 noet nospell: */

vector< float > Util::vareas ( EMData d  )  [static]

Definition at line 20580 of file util_sparx.cpp.

References data, dm, EMAN::EMData::get_ysize(), and ny.

20580                                     {
20581         const float step=0.001f;
20582         int ny = d->get_ysize();
20583         //  input emdata should have size 2xN, where N is number of points
20584         //  output vector should be 2xN, first element is the number of elements
20585         //  associated with this point, second is 0 is the element is touching the border, 1 if it is interior
20586         vector<float> group(2*ny);
20587         for(int i=0; i<2*ny; i++) group[i] = 0.0f;
20588         int K = int(1.0f/step) +1;
20589         int hit = 0;
20590         for(int kx=0; kx<=K; kx++) {
20591                 float tx = kx*step;
20592                 for(int ky=0; ky<=K; ky++) {
20593                         float ty = ky*step;
20594                         float dm = 1.0e23f;
20595                         for(int i=0; i<ny; i++) {
20596                                 float qd = pow(tx-(*d)(0,i),2) + pow(ty-(*d)(1,i),2);
20597                                 if( qd < dm) {
20598                                         dm = qd;
20599                                         hit = i;
20600                                 }
20601                         }
20602                         data(0,hit) += 1.0f;
20603                         if(kx == 0 || ky == 0 || kx == K || ky == K)  data(1,hit) = 1.0f;
20604                 }
20605         }
20606         return  group;
20607 }

void Util::voronoi ( double *  phi,
double *  theta,
double *  weight,
int  nt 
) [static]

Definition at line 7570 of file util_sparx.cpp.

References ang_to_xyz(), areav_(), Assert, disorder2(), ENTERFUNC, EXITFUNC, flip23(), key, status, trmsh3_(), and y.

07571 {
07572 
07573         ENTERFUNC;
07574 
07575         int *list, *lptr, *lend, *iwk, *key,*lcnt,*indx,*good;
07576         int nt6, n, ier, nout, lnew, mdup, nd;
07577         int i,k,mt,status;
07578 
07579 
07580         double *ds, *x, *y, *z;
07581         double tol  = 1.0e-8;
07582         double dtol = 15;
07583         double a;
07584 
07585         /*if(last){
07586                 if(medium>nt)  n = nt+nt;
07587                 else           n = nt+nt-medium+1;
07588         }
07589         else{
07590                 n=nt;
07591         }*/
07592 
07593         n = nt + nt;
07594 
07595         nt6 = n*6;
07596 
07597         list = (int*)calloc(nt6,sizeof(int));
07598         lptr = (int*)calloc(nt6,sizeof(int));
07599         lend = (int*)calloc(n  ,sizeof(int));
07600         iwk  = (int*)calloc(n  ,sizeof(int));
07601         good = (int*)calloc(n  ,sizeof(int));
07602         key  = (int*)calloc(n  ,sizeof(int));
07603         indx = (int*)calloc(n  ,sizeof(int));
07604         lcnt = (int*)calloc(n  ,sizeof(int));
07605 
07606         ds      =       (double*) calloc(n,sizeof(double));
07607         x       =       (double*) calloc(n,sizeof(double));
07608         y       =       (double*) calloc(n,sizeof(double));
07609         z       =       (double*) calloc(n,sizeof(double));
07610 
07611         if (list == NULL ||
07612         lptr == NULL ||
07613         lend == NULL ||
07614         iwk  == NULL ||
07615         good == NULL ||
07616         key  == NULL ||
07617         indx == NULL ||
07618         lcnt == NULL ||
07619         x    == NULL ||
07620         y    == NULL ||
07621         z    == NULL ||
07622         ds   == NULL) {
07623                 printf("memory allocation failure!\n");
07624                 exit(1);
07625         }
07626 
07627         bool colinear=true;
07628         while(colinear)
07629         {
07630 
07631         L1:
07632             for(i = 0; i<nt; i++){
07633                 x[i] = theta[i];
07634                 y[i] = phi[i];
07635                 x[nt+i] = 180.0 - x[i];
07636                 y[nt+i] = 180.0 + y[i];
07637             }
07638 
07639             Util::disorder2(x, y, key, n);
07640 
07641             // check if the first three angles are not close, else shuffle
07642             double val;
07643             for(k=0; k<2; k++){
07644                 for(i=k+1; i<3; i++){
07645                     val = (x[i]-x[k])*(x[i]-x[k]) + (y[i]-y[k])*(y[i]-y[k]);
07646                     if( val  < dtol) {
07647                         goto L1;
07648                     }
07649                 }
07650             }
07651 
07652             Util::ang_to_xyz(x, y, z, n);
07653 
07654             //  Make sure that first three has no duplication
07655             bool dupnode=true;
07656             dupnode=true;
07657             while(dupnode)
07658             {
07659                 for(k=0; k<2; k++){
07660                     for(i=k+1; i<3; i++){
07661                         if(  x[i]*x[k]+y[i]*y[k]+z[i]*z[k] > 1.0-tol) {
07662                                 Util::flip23(x, y, z, key, k, n);
07663                                 continue;
07664                         }
07665                     }
07666                 }
07667                 dupnode = false;
07668             }
07669 
07670 
07671             ier = 0;
07672 
07673             status = Util::trmsh3_(&n,&tol,x,y,z,&nout,list,lptr,lend,&lnew, indx, lcnt, iwk, good, ds, &ier);
07674 
07675             if (status != 0) {
07676                 printf(" error in trmsh3 \n");
07677                 exit(1);
07678             }
07679 
07680             if (ier > 0) {
07681                 printf("*** Error in TRMESH:  duplicate nodes encountered ***\n");
07682                 exit(1);
07683             }
07684 
07685             mdup=n-nout;
07686             if (ier == -2) {
07687                 //printf("in TRMESH:the first three nodes are colinear*** disorder again\n");
07688             }
07689             else
07690             {
07691                 colinear=false;
07692             }
07693         }
07694 
07695 
07696         Assert( ier != -2 );
07697 //  Create a list of unique nodes GOOD, the numbers refer to locations on the full list
07698 //  INDX contains node numbers from the squeezed list
07699         nd=0;
07700         for (k=1; k<=n; k++){
07701                 if (indx[k-1]>0) {
07702                         nd++;
07703                         good[nd-1]=k;
07704                 }
07705         }
07706 
07707 //
07708 // *** Compute the Voronoi region areas.
07709 //
07710         for(i = 1; i<=nout; i++) {
07711                 k=good[i-1];
07712                 //  We only need n weights from hemisphere
07713                 if (key[k-1] <= nt) {
07714 //  CALCULATE THE AREA
07715                         a = Util::areav_(&i, &nout, x, y, z, list, lptr, lend, &ier);
07716                         if (ier != 0){
07717 //  We set the weight to -1, this will signal the error in the calling
07718 //   program, as the area will turn out incorrect
07719                                 printf("    *** error in areav:  ier = %d ***\n", ier);
07720                                 weight[key[k-1]-1] =-1.0;
07721                         } else {
07722 //  Assign the weight
07723                                 weight[key[k-1]-1]=a/lcnt[i-1];
07724                         }
07725                 }
07726         }
07727 
07728 
07729 // Fill out the duplicated weights
07730         for(i = 1; i<=n; i++){
07731                 mt =- indx[i-1];
07732                 if (mt>0){
07733                         k = good[mt-1];
07734 //  This is a duplicated entry, get the already calculated
07735 //   weight and assign it.
07736                 //  We only need n weights from hemisphere
07737                         if (key[i-1] <= nt && key[k-1] <= nt) { weight[key[i-1]-1] = weight[key[k-1]-1];}
07738                         }
07739         }
07740 
07741         free(list);
07742         free(lend);
07743         free(iwk);
07744         free(good);
07745         free(key);
07746         free(lptr);
07747         free(indx);
07748         free(lcnt);
07749         free(ds);
07750         free(x);
07751         free(y);
07752         free(z);
07753 
07754 
07755         EXITFUNC;
07756 }

static void EMAN::Util::voronoidiag ( double *  theta,
double *  phi,
double *  weight,
int  n 
) [static]

vector< double > Util::vrdg ( const vector< float > &  ph,
const vector< float > &  th 
) [static]

Definition at line 7147 of file util_sparx.cpp.

References ENTERFUNC, EXITFUNC, hsortd(), ImageFormatException, key, LOGERR, phi, theta, voronoi(), and weight.

07148 {
07149 
07150         ENTERFUNC;
07151 
07152         if ( th.size() != ph.size() ) {
07153                 LOGERR("images not same size");
07154                 throw ImageFormatException( "images not same size");
07155         }
07156 
07157         // rand_seed
07158         srand(10);
07159 
07160         int i,*key;
07161         int len = th.size();
07162         double *theta,*phi,*weight;
07163         theta   =       (double*) calloc(len,sizeof(double));
07164         phi     =       (double*) calloc(len,sizeof(double));
07165         weight  =       (double*) calloc(len,sizeof(double));
07166         key     =       (int*) calloc(len,sizeof(int));
07167         const float *thptr, *phptr;
07168 
07169         thptr = &th[0];
07170         phptr = &ph[0];
07171         for(i=1;i<=len;i++){
07172                 key(i) = i;
07173                 weight(i) = 0.0;
07174         }
07175 
07176         for(i = 0;i<len;i++){
07177                 theta[i] = thptr[i];
07178                 phi[i]   = phptr[i];
07179         }
07180 
07181         //  sort by theta
07182         Util::hsortd(theta, phi, key, len, 1);
07183 
07184         //Util::voronoidiag(theta,phi, weight, len);
07185         Util::voronoi(phi, theta, weight, len);
07186 
07187         //sort by key
07188         Util::hsortd(weight, weight, key, len, 2);
07189 
07190         free(theta);
07191         free(phi);
07192         free(key);
07193         vector<double> wt;
07194         double count = 0;
07195         for(i=1; i<= len; i++)
07196         {
07197                 wt.push_back(weight(i));
07198                 count += weight(i);
07199         }
07200 
07201         //if( abs(count-6.28) > 0.1 )
07202         //{
07203         //    printf("Warning: SUM OF VORONOI CELLS AREAS IS %lf, should 2*PI\n", count);
07204         //}
07205 
07206         free(weight);
07207 
07208         EXITFUNC;
07209         return wt;
07210 
07211 }

EMData * Util::window ( 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]

Definition at line 5305 of file util_sparx.cpp.

References EMAN::EMData::copy_empty_head(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageDimensionException, inp, NullPointerException, nx, ny, outp, EMAN::EMData::set_size(), and EMAN::EMData::update().

05306 {
05307         /* Exception Handle */
05308         if (!img) throw NullPointerException("NULL input image");
05309         /* ============================== */
05310 
05311         // Get the size of the input image
05312         int nx=img->get_xsize(), ny=img->get_ysize(), nz=img->get_zsize();
05313         /* ============================== */
05314 
05315         /* Exception Handle */
05316         if(new_nx>nx || new_ny>ny || new_nz>nz)
05317                 throw ImageDimensionException("The size of the windowed image cannot exceed the input image size. 1");
05318         if((nx/2)-(new_nx/2)+x_offset<0 || (ny/2)-(new_ny/2)+y_offset<0 || (nz/2)-(new_nz/2)+z_offset<0)
05319                 throw ImageDimensionException("The offset inconsistent with the input image size. 2");
05320         if(x_offset>((nx-(nx/2))-(new_nx-(new_nx/2))) || y_offset>((ny-(ny/2))-(new_ny-(new_ny/2))) || z_offset>((nz-(nz/2))-(new_nz-(new_nz/2))))
05321                 throw ImageDimensionException("The offset inconsistent with the input image size. 3");
05322         /* ============================== */
05323 
05324         /*    Calculation of the start point */
05325         int  new_st_x = nx/2-new_nx/2 + x_offset,
05326              new_st_y = ny/2-new_ny/2 + y_offset,
05327              new_st_z = nz/2-new_nz/2 + z_offset;
05328         /* ============================== */
05329 
05330         /* Exception Handle */
05331         if (new_st_x<0 || new_st_y<0 || new_st_z<0)   //  WHAT HAPPENS WITH THE END POINT CHECK??  PAP
05332                 throw ImageDimensionException("The offset inconsistent with the input image size. 4");
05333         /* ============================== */
05334 
05335         EMData* wind = img->copy_empty_head();
05336         wind->set_size(new_nx, new_ny, new_nz);
05337         float *outp=wind->get_data();
05338         float *inp=img->get_data();
05339 
05340         for (int k=0; k<new_nz; k++)
05341                 for(int j=0; j<new_ny; j++)
05342                         for(int i=0; i<new_nx; i++)
05343                                 outp(i,j,k) = inp(i,j,k);
05344         wind->update();
05345         return wind;
05346 }

void Util::WTF ( EMData PROJ,
vector< float >  SS,
float  SNR,
int  K 
) [static]

Definition at line 5793 of file util_sparx.cpp.

References EMAN::EMData::get_data(), max, min, PROJ, q, SS, EMAN::EMData::update(), W, and Y.

05794 {
05795         int NSAM,NROW,NNNN,NR2,L,JY,KX,NANG;
05796         float WW,OX,OY;
05797 
05798         NSAM = PROJ->get_xsize();
05799         NROW = PROJ->get_ysize();
05800         int ntotal = NSAM*NROW;
05801         float q = 2.0f;
05802         float qt = 8.0f/q;
05803         //  Fix for padding 2x
05804         int ipad = 1;
05805         NSAM *= ipad;
05806         NROW *= ipad;
05807         NNNN = NSAM+2-(NSAM%2);
05808         int NX2 = NSAM/2;
05809         NR2  = NROW/2;
05810 
05811         NANG = int(SS.size())/6;
05812 
05813         EMData* W = new EMData();
05814         int Wnx = NNNN/2;
05815         W->set_size(Wnx,NROW,1);
05816         W->to_zero();
05817         float *Wptr = W->get_data();
05818         float *PROJptr = PROJ->get_data();
05819         for (L=1; L<=NANG; L++) {
05820                 float  tmp1 = SS(3,K)*SS(4,L)*(SS(1,K)*SS(1,L) + SS(2,K)*SS(2,L)) - SS(3,L)*SS(4,K);
05821                 float  tmp2 = SS(4,L)*( SS(1,K)*SS(2,L) - SS(1,L)*SS(2,K) ); 
05822                 OX = SS(6,K)*tmp2 + SS(5,K)*tmp1;
05823                 OY = SS(5,K)*tmp2 - SS(6,K)*tmp1;
05824                 if(OX < 0.0f) {
05825                         OX = -OX;
05826                         OY = -OY;
05827                 }
05828 
05829                 if( fabs(OX) > 1.0e-6f || fabs(OY) > 1.0e6f ) {
05830                         for(int J=1;J<=NROW;J++) {
05831                                 JY = (J-1);
05832                                 if(JY > NR2) JY -= NROW;
05833 #ifdef _WIN32
05834                                 int xma = _cpp_min(int(0.5f+(q-JY*OY)/OX),NX2);
05835                                 int xmi = _cpp_max(int((-q-JY*OY)/OX+0.5+NSAM)-NSAM,0);
05836 #else
05837                                 int xma = std::min(int(0.5f+(q-JY*OY)/OX),NX2);
05838                                 int xmi = std::max(int((-q-JY*OY)/OX+0.5+NSAM)-NSAM,0);
05839 #endif  //_WIN32
05840                                 if( xmi <= xma) {
05841                                         for(int I=xmi;I<=xma;I++) {
05842                                                 float Y = fabs(OX*I + OY*JY);
05843                                                 W(I+1,J) += exp(-qt*Y*Y);
05844         //cout << " L   "<<L << " I   "<<I << " JY   "<<JY << " ARG   "<<qt*Y*Y <<endl;
05845                                         }
05846                                 }
05847                         }
05848                 } else {
05849                         for(int J=1;J<=NROW;J++) for(int I=1;I<=NNNN/2;I++)  W(I,J) += 1.0f;
05850                 }
05851         }
05852         EMData* proj_in = PROJ;
05853 
05854         PROJ = PROJ->norm_pad( false, ipad);
05855         PROJ->do_fft_inplace();
05856         PROJ->update();
05857         //cout << " x   "<<PROJ->get_xsize() << " y   "<<PROJ->get_ysize() <<endl;
05858         PROJptr = PROJ->get_data();
05859 
05860         float WNRMinv,temp;
05861         float osnr = 1.0f/SNR;
05862         WNRMinv = 1.0f/W(1,1);
05863         for(int J=1;J<=NROW;J++)  {
05864                 JY = J-1;
05865                 if( JY > NR2)  JY -= NROW;
05866                 float sy = JY;
05867                 sy /= NROW;
05868                 sy *= sy;
05869                 for(int I=1;I<=NNNN;I+=2) {
05870                         KX           = (I+1)/2;
05871                         temp         = W(KX,J)*WNRMinv;
05872                         WW           = temp/(temp*temp + osnr);
05873                         // This is supposed to fix fall-off due to Gaussian function in the weighting function
05874                         float sx = KX-1;
05875                         sx /= NSAM;
05876                         WW *= exp(qt*(sy + sx*sx));
05877                         PROJ(I,J)   *= WW;
05878                         PROJ(I+1,J) *= WW;
05879                 }
05880         }
05881         delete W; W = 0;
05882         PROJ->do_ift_inplace();
05883         PROJ->depad();
05884 
05885         float* data_src = PROJ->get_data();
05886         float* data_dst = proj_in->get_data();
05887 
05888         for( int i=0; i < ntotal; ++i )  data_dst[i] = data_src[i];
05889 
05890         proj_in->update();
05891 
05892         delete PROJ;
05893 }

void Util::WTM ( EMData PROJ,
vector< float >  SS,
int  DIAMETER,
int  NUMP 
) [static]

Definition at line 5987 of file util_sparx.cpp.

References AMAX1, AMIN1, CC, CP, EMAN::EMData::get_data(), PROJ, RI, sqrt(), SS, EMAN::EMData::update(), VP, VV, and W.

05988 {
05989         float rad2deg =(180.0f/3.1415926f);
05990         float deg2rad = (3.1415926f/180.0f);
05991 
05992         int NSAM,NROW,NNNN,NR2,NANG,L,JY;
05993 
05994         NSAM = PROJ->get_xsize();
05995         NROW = PROJ->get_ysize();
05996         NNNN = NSAM+2-(NSAM%2);
05997         NR2  = NROW/2;
05998         NANG = int(SS.size())/6;
05999 
06000         float RI[9];
06001         RI(1,1)=SS(1,NUMP)*SS(3,NUMP)*SS(5,NUMP)-SS(2,NUMP)*SS(6,NUMP);
06002         RI(2,1)=-SS(1,NUMP)*SS(3,NUMP)*SS(6,NUMP)-SS(2,NUMP)*SS(5,NUMP);
06003         RI(3,1)=SS(1,NUMP)*SS(4,NUMP);
06004         RI(1,2)=SS(2,NUMP)*SS(3,NUMP)*SS(5,NUMP)+SS(1,NUMP)*SS(6,NUMP);
06005         RI(2,2)=-SS(2,NUMP)*SS(3,NUMP)*SS(6,NUMP)+SS(1,NUMP)*SS(5,NUMP);
06006         RI(3,2)=SS(2,NUMP)*SS(4,NUMP);
06007         RI(1,3)=-SS(4,NUMP)*SS(5,NUMP);
06008         RI(2,3)=SS(4,NUMP)*SS(6,NUMP);
06009         RI(3,3)=SS(3,NUMP);
06010 
06011         float THICK=static_cast<float>( NSAM)/DIAMETER/2.0f ;
06012 
06013         EMData* W = new EMData();
06014         int Wnx = NNNN/2;
06015         W->set_size(NNNN/2,NROW,1);
06016         W->to_one();
06017         float *Wptr = W->get_data();
06018 
06019         float ALPHA,TMP,FV,RT,FM,CCN,CC[3],CP[2],VP[2],VV[3];
06020 
06021         for (L=1; L<=NANG; L++) {
06022                 if (L != NUMP) {
06023                         CC(1)=SS(2,L)*SS(4,L)*SS(3,NUMP)-SS(3,L)*SS(2,NUMP)*SS(4,NUMP);
06024                         CC(2)=SS(3,L)*SS(1,NUMP)*SS(4,NUMP)-SS(1,L)*SS(4,L)*SS(3,NUMP);
06025                         CC(3)=SS(1,L)*SS(4,L)*SS(2,NUMP)*SS(4,NUMP)-SS(2,L)*SS(4,L)*SS(1,NUMP)*SS(4,NUMP);
06026 
06027                         TMP = sqrt(CC(1)*CC(1) +  CC(2)*CC(2) + CC(3)*CC(3));
06028                         CCN=static_cast<float>( AMAX1( AMIN1(TMP,1.0) ,-1.0) );
06029                         ALPHA=rad2deg*float(asin(CCN));
06030                         if (ALPHA>180.0f) ALPHA=ALPHA-180.0f;
06031                         if (ALPHA>90.0f) ALPHA=180.0f-ALPHA;
06032                         if(ALPHA<1.0E-6) {
06033                                 for(int J=1;J<=NROW;J++) for(int I=1;I<=NNNN/2;I++) W(I,J)+=1.0;
06034                         } else {
06035                                 FM=THICK/(fabs(sin(ALPHA*deg2rad)));
06036                                 CC(1)   = CC(1)/CCN;CC(2)   = CC(2)/CCN;CC(3)   = CC(3)/CCN;
06037                                 VV(1)= SS(2,L)*SS(4,L)*CC(3)-SS(3,L)*CC(2);
06038                                 VV(2)= SS(3,L)*CC(1)-SS(1,L)*SS(4,L)*CC(3);
06039                                 VV(3)= SS(1,L)*SS(4,L)*CC(2)-SS(2,L)*SS(4,L)*CC(1);
06040                                 CP(1)   = 0.0;CP(2) = 0.0;
06041                                 VP(1)   = 0.0;VP(2) = 0.0;
06042 
06043                                 CP(1) = CP(1) + RI(1,1)*CC(1) + RI(1,2)*CC(2) + RI(1,3)*CC(3);
06044                                 CP(2) = CP(2) + RI(2,1)*CC(1) + RI(2,2)*CC(2) + RI(2,3)*CC(3);
06045                                 VP(1) = VP(1) + RI(1,1)*VV(1) + RI(1,2)*VV(2) + RI(1,3)*VV(3);
06046                                 VP(2) = VP(2) + RI(2,1)*VV(1) + RI(2,2)*VV(2) + RI(2,3)*VV(3);
06047 
06048                                 TMP = CP(1)*VP(2)-CP(2)*VP(1);
06049 
06050                                 //     PREVENT TMP TO BE TOO SMALL, SIGN IS IRRELEVANT
06051                                 TMP = AMAX1(1.0E-4f,fabs(TMP));
06052                                 float tmpinv = 1.0f/TMP;
06053                                 for(int J=1;J<=NROW;J++) {
06054                                         JY = (J-1);
06055                                         if (JY>NR2)  JY=JY-NROW;
06056                                         for(int I=1;I<=NNNN/2;I++) {
06057                                                 FV     = fabs((JY*CP(1)-(I-1)*CP(2))*tmpinv);
06058                                                 RT     = 1.0f-FV/FM;
06059                                                 W(I,J) += ((RT>0.0f)*RT);
06060                                         }
06061                                 }
06062                         }
06063                 }
06064         }
06065 
06066         EMData* proj_in = PROJ;
06067 
06068         PROJ = PROJ->norm_pad( false, 1);
06069         PROJ->do_fft_inplace();
06070         PROJ->update();
06071         float *PROJptr = PROJ->get_data();
06072 
06073         int KX;
06074         float WW;
06075         for(int J=1; J<=NROW; J++)
06076                 for(int I=1; I<=NNNN; I+=2) {
06077                         KX          =  (I+1)/2;
06078                         WW          =  1.0f/W(KX,J);
06079                         PROJ(I,J)   = PROJ(I,J)*WW;
06080                         PROJ(I+1,J) = PROJ(I+1,J)*WW;
06081                 }
06082         delete W; W = 0;
06083         PROJ->do_ift_inplace();
06084         PROJ->depad();
06085 
06086         float* data_src = PROJ->get_data();
06087         float* data_dst = proj_in->get_data();
06088 
06089         int ntotal = NSAM*NROW;
06090         for( int i=0; i < ntotal; ++i )  data_dst[i] = data_src[i];
06091 
06092         proj_in->update();
06093         delete PROJ;
06094 }


The documentation for this class was generated from the following files:
Generated on Thu May 3 10:11:48 2012 for EMAN2 by  doxygen 1.4.7