33#define eman__util_h__ 1
38 #define M_PI 3.14159265358979323846f
43#define MUTEX pthread_mutex_t
52#include "sparx/emconstants.h"
55#include <boost/multi_array.hpp>
64const int good_fft_sizes[] = { 16, 24, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 84, 96, 100, 104, 112, 120, 128, 132, 140, 168, 180, 192, 196,
65 208, 216, 220, 224, 240, 256, 260, 288, 300, 320, 352, 360, 384, 416, 440, 448, 480, 512, 540, 560, 576, 588, 600, 630, 640, 648, 672,
66 686, 700, 720, 750, 756, 768, 784, 800, 810, 840, 864, 882, 896, 900, 960, 972, 980, 1000, 1008, 1024, 1050, 1080, 1120, 1134, 1152,
67 1176, 1200, 1250, 1260, 1280, 1296, 1344, 1350, 1372, 1400, 1440, 1458, 1470, 1500, 1512, 1536, 1568, 1600, 1620, 1680, 1728, 1750,
68 1764, 1792, 1800, 1890, 1920, 1944, 1960, 2000, 2016, 2048, 2058, 2100, 2160, 2240, 2250, 2268, 2304, 2352, 2400, 2430, 2450, 2500,
69 2520, 2560, 2592, 2646, 2688, 2700, 2744, 2800, 2880, 2916, 2940, 3000, 3024, 3072, 3136, 3150, 3200, 3240, 3360, 3402, 3430, 3456,
70 3500, 3528, 3584, 3600, 3750, 3780, 3840, 3888, 3920, 4000, 4032, 4050, 4096, 4116, 4200, 4320, 4374, 4410, 4480, 4500, 4536, 4608,
71 4704, 4800, 4802, 4860, 4900, 5000, 5040, 5120, 5184, 5250, 5292, 5376, 5400, 5488, 5600, 5670, 5760, 5832, 5880, 6000, 6048, 6144,
72 6174, 6250, 6272, 6300, 6400, 6480, 6720, 6750, 6804, 6860, 6912, 7000, 7056, 7168, 7200, 7290, 7350, 7500, 7560, 7680, 7776, 7840,
73 7938, 8000, 8064, 8100, 8192, 8232, 8400, 8640, 8748, 8750, 8820, 8960, 9000, 9072, 9216, 9408, 9450, 9600, 9604, 9720, 9800, 10000,
74 10080, 10206, 10240, 10290, 10368, 10500, 10584, 10752, 10800, 10976, 11200, 11250, 11340, 11520, 11664, 11760, 12000, 12096,
75 12150, 12250, 12288, 12348, 12500, 12544, 12600, 12800, 12960, 13122, 13230, 13440, 13500, 13608, 13720, 13824, 14000, 14112,
76 14336, 14400, 14406, 14580, 14700, 15000, 15120, 15360, 15552, 15680, 15750, 15876, 16000, 16128, 16200, 16384 };
84 typedef boost::multi_array<int, 3>
MIArray3D;
92 #include "sparx/util_sparx.h"
93 #include "sphire/util_sphire.h"
105 static inline bool is_nan(
const float number)
107 return std::isnan(number);
115 static void ap2ri(
float *data,
size_t n);
116 static void ap2ri(
double *data,
size_t n);
160 static void flip_image(
float *data,
size_t nx,
size_t ny);
167 static vector<EMData *>
svdcmp(
const vector<EMData *> &data,
int nvec);
190 static bool sstrncmp(
const char *s1,
const char *s2);
219 static bool get_str_float(
const char *str,
const char *float_var,
float *p_val);
233 static bool get_str_float(
const char *str,
const char *float_var,
234 float *p_v1,
float *p_v2);
254 static bool get_str_float(
const char *str,
const char *float_var,
255 int *p_nvalues,
float *p_v1,
float *p_v2);
268 static bool get_str_int(
const char *str,
const char *int_var,
int *p_val);
282 static bool get_str_int(
const char *str,
const char *int_var,
283 int *p_v1,
int *p_v2);
303 static bool get_str_int(
const char *str,
const char *int_var,
304 int *p_nvalues,
int *p_v1,
int *p_v2);
317 const string & new_ext);
340 static string sbasename(
const string & filename);
355 const float *data_y,
float *p_slope,
356 float *p_intercept,
bool ignore_zero,
float absmax=0);
376 static void save_data(
const vector < float >&x_array,
377 const vector < float >&y_array,
378 const string & filename);
388 static void save_data(
float x0,
float dx,
389 const vector < float >&y_array,
390 const string & filename);
401 static void save_data(
float x0,
float dx,
float *y_array,
402 size_t array_size,
const string & filename);
412 static void sort_mat(
float *left,
float *right,
int *leftPerm,
437 static float get_frand(
int low,
int high);
444 static float get_frand(
float low,
float high);
451 static float get_frand(
double low,
double high);
468 return (
int) (
x - 0.5f);
470 return (
int) (
x + 0.5f);
480 return (
int) (
x - 0.5);
482 return (
int) (
x + 0.5);
491 return (
float) (
log(
x) /
log(2.0f));
500 return (
double) (
log(
x) /
log(2.0));
509 return (
float) (
log((
float)i) /
log(2.0f));
520 return (1-t) * p1 + t * p2;
531 return (1.0f-t) * p1 + t * p2;
544 float p4,
float t,
float u)
546 return (1-t) * (1-u) * p1 + t * (1-u) * p2 + (1-t) * u * p3 + t * u * p4;
559 std::complex<float> p4,
float t,
float u)
561 return (1.0f-t) * (1.0f-u) * p1 + t * (1.0f-u) * p2 + (1.0f-t) * u * p3 + t * u * p4;
573 static inline std::complex<float>
gauss_interpolate_cmplx(std::complex<float> p1, std::complex<float> p2, std::complex<float> p3,
574 std::complex<float> p4,
float t,
float u)
580 return (c1 * p1 + c2 * p2 + c3 * p3 + c4 * p4)/(c1+c2+c3+c4);
591 std::complex<float> sm=0;
593 for (
int y=0,i=0;
y<3;
y++) {
594 for (
int x=0;
x<3;
x++,i++) {
620 float p4,
float p5,
float p6,
621 float p7,
float p8,
float t,
624 return ((1 - t) * (1 - u) * (1 - v) * p1 + t * (1 - u) * (1 - v) * p2
625 + (1 - t) * u * (1 - v) * p3 + t * u * (1 - v) * p4
626 + (1 - t) * (1 - u) * v * p5 + t * (1 - u) * v * p6
627 + (1 - t) * u * v * p7 + t * u * v * p8);
646 std::complex<float> p4, std::complex<float> p5, std::complex<float> p6,
647 std::complex<float> p7, std::complex<float> p8,
float t,
650 return ((1 - t) * (1 - u) * (1 - v) * p1 + t * (1 - u) * (1 - v) * p2
651 + (1 - t) * u * (1 - v) * p3 + t * u * (1 - v) * p4
652 + (1 - t) * (1 - u) * v * p5 + t * (1 - u) * v * p6
653 + (1 - t) * u * v * p7 + t * u * v * p8);
663 static void find_max(
const float *data,
size_t nitems,
664 float *p_max_val,
int *p_max_index = 0);
677 float *p_max_val,
float *p_min_val,
678 int *p_max_index = 0,
int *p_min_index = 0);
706 static vector<float>
nonconvex(
const vector<float>&curve,
int first=3);
718 static vector<float>
windowdot(
const vector<float>&curveA,
const vector<float>&curveB,
int window,
int normA);
724 static inline double angle3(
double x1,
double y1,
double z1,
double x2,
double y2,
double z2) {
728 static inline float angle3(
float x1,
float y1,
float z1,
float x2,
float y2,
float z2) {
756 return (
float)(
x *
x);
766 return (
float)(
x *
x +
y *
y);
776 return (
float) hypot(
x,
y);
786 return (
x *
x +
y *
y);
796 return (
float)(
x *
x +
y *
y);
807 return ((
x *
x +
y *
y + z * z));
818 return (
x *
x +
y *
y + z * z);
829 return sqrtf((
float)(
x *
x +
y *
y + z * z));
838 static inline float hypot3(
float x,
float y,
float z)
840 return sqrtf(
x *
x +
y *
y + z * z);
849 static inline double hypot3(
double x,
double y,
double z)
851 return (
double)
sqrt(
x *
x +
y *
y + z * z);
877 return ((
int)
x - 1);
887 static float fast_exp(
const float &f) ;
912 static inline float agauss(
float a,
float dx,
float dy,
float dz,
float d)
914 return (a * exp(-(dx * dx + dy * dy + dz * dz) / d));
924 return (f1 < f2 ? f1 : f2);
933 static inline int get_min(
int f1,
int f2,
int f3)
935 if (f1 <= f2 && f1 <= f3) {
938 if (f2 <= f1 && f2 <= f3) {
949 static inline float get_min(
float f1,
float f2)
951 return (f1 < f2 ? f1 : f2);
960 static inline float get_min(
float f1,
float f2,
float f3)
962 if (f1 <= f2 && f1 <= f3) {
965 if (f2 <= f1 && f2 <= f3) {
978 static inline float get_min(
float f1,
float f2,
float f3,
float f4)
998 static inline float get_max(
float f1,
float f2)
1000 return (f1 < f2 ? f2 : f1);
1009 static inline float get_max(
float f1,
float f2,
float f3)
1011 if (f1 >= f2 && f1 >= f3) {
1014 if (f2 >= f1 && f2 >= f3) {
1027 static inline float get_max(
float f1,
float f2,
float f3,
float f4)
1045 float m = fmod((
float)in, (
float)(2.0*M_PI));
1047 return m<0?m+2.0*M_PI:m;
1053 float m = fmod((
float)in, (
float)(2.0*M_PI));
1054 if (m<-M_PI) m+=2.0*M_PI;
1055 return m>M_PI?m-2.0*M_PI:m;
1068 float r = fmod(fabs(
x -
y), (
float) (2.0 * M_PI));
1069 if (r<0) r+=2.0*M_PI;
1071 r = (float) (2.0 * M_PI - r);
1086 float r = fmod(fabs(
x -
y), (
float) M_PI);
1087 if (r > M_PI / 2.0) {
1088 r = (float)(M_PI - r);
1100 if ((r1==0 && i1==0) || (r2==0 && i2==0))
return 0;
1103 return fast_acos((r1*r2+i1*i2)/(
float)(hypot(r1,i1)*hypot(r2,i2)));
1112 static inline int goodf(
const float *p_f)
1117 if (((*((
int *)p_f)>>23)&255)==255)
return 0;
1122 static inline int goodf(
const double *p_f)
1127 if (((*((
long long *)p_f)>>52)&2047)==2047)
return 0;
1162 return copysign(a, b);
1165 if ((a <= 0 && b <= 0) || (a > 0 && b > 0)) {
1187 return (
float)erfc(
x);
1189 static double a[] = { -1.26551223, 1.00002368,
1190 0.37409196, 0.09678418,
1191 -0.18628806, 0.27886807,
1192 -1.13520398, 1.48851587,
1193 -0.82215223, 0.17087277
1199 double t = 1 / (1 + 0.5 * z);
1200 double f1 = t * (a[4] + t * (a[5] + t * (a[6] +
1201 t * (a[7] + t * (a[8] + t * a[9])))));
1202 result = t * exp((-z * z) + a[0] + t * (a[1] + t * (a[2] + t * (a[3] + f1))));
1205 result = 2 - result;
1208 return (
float)result;
1261 const string str =
string(
""),
1262 ostream& out = std::cout);
1269 template<
class T>
static inline T
sgn(T& val) {
1270 return (val > 0) ? T(+1) : T(-1);
1296 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);
1305 return ( (
x>1) && (
x & (
x-1))==0 );
1310 float c = ceilf(value);
1312 if (fabs(value - c) < precision) value = c;
1313 else if (fabs(value - f) < precision) value = f;
Dict is a dictionary to store <string, EMObject> pair.
EMData stores an image's data and defines core image processing routines.
Util is a collection of utility functions.
static double log_2(double x)
Get the log base 2 of a double x.
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 float fast_gauss_B2(const float &f)
Returns an approximate of exp(-2x^2) using a cached table uses actual function outside the cached ran...
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 double angle3(double x1, double y1, double z1, double x2, double y2, double z2)
Given 2 vectors, it will compute the angle between them in radians.
static float angle3(float x1, float y1, float z1, float x2, float y2, float z2)
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 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 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 angle_norm_pi(float in)
Normalize an angle in radians so it is in the -pi to pi range.
static float square(float x)
Calculate a number's square.
static int MUTEX_UNLOCK(MUTEX *mutex)
static std::complex< float > trilinear_interpolate_complex(std::complex< float > p1, std::complex< float > p2, std::complex< float > p3, std::complex< float > p4, std::complex< float > p5, std::complex< float > p6, std::complex< float > p7, std::complex< float > p8, float t, float u, float v)
Calculate trilinear interpolation.
static string remove_filename_ext(const string &filename)
Remove a filename's extension and return the new filename.
static string recv_broadcast(int port)
static EMData * calc_bessel(const int n, const float &x)
static string str_to_lower(const string &s)
Return a lower case version of the argument 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=...
static float agauss(float a, float dx, float dy, float dz, float d)
Calculate Gaussian value.
static float linear_interpolate(float p1, float p2, float t)
Calculate linear interpolation.
static T sgn(T &val)
Sign function.
static float eman_erfc(float x)
complementary error function.
static void flip_complex_phase(float *data, size_t n)
flip the phase of a complex data array.
static std::complex< float > linear_interpolate_cmplx(std::complex< float > p1, std::complex< float > p2, float t)
Calculate linear interpolation.
static int file_lock_wait(FILE *file)
lock a file.
static int goodf(const double *p_f)
static short hypot_fast_int(int x, int y)
Euclidean distance in 2D for integers computed fast using a cached lookup table.
static int MUTEX_LOCK(MUTEX *mutex)
static float fast_acos(const float &f)
Returns an approximate of acos(x) using a cached table and linear interpolation tolerates values very...
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 vector< EMData * > svdcmp(const vector< EMData * > &data, int nvec)
Perform singular value decomposition on a set of images.
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 perfor...
static int square(int n)
Calculate a number's square.
static int fast_floor(float x)
A fast way to calculate a floor, which is largest integral value not greater than argument.
static Dict get_stats(const vector< float > &data)
Get the mean, standard deviation, skewness and kurtosis of the input data.
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...
static void flip_image(float *data, size_t nx, size_t ny)
Vertically flip the data of a 2D real image.
static float square(double x)
Calculate a number's square.
static float log_2(int i)
Get the log base 2 of an integer i.
static int goodf(const float *p_f)
Check whether a number is a good float.
static float log_2(float x)
Get the log base 2 of a float x.
static string get_time_label()
Get the current time in a string with format "mm/dd/yyyy hh:mm".
static float get_min(float f1, float f2, float f3, float f4)
Get the minimum of 4 numbers.
static bool sstrncmp(const char *s1, const char *s2)
Safe string compare.
static int hypot2sq(int x, int y)
Euclidean distance function squared in 2D: f(x,y) = (x*x + y*y);.
static float angle_err_ri(float r1, float i1, float r2, float i2)
Calculate the angular phase difference between two r/i vectors.
static std::complex< float > gauss3_interpolate_cmplx(std::complex< float > *p, float t, float u)
Calculate 3x3 Gaussian interpolation.
static float get_max(float f1, float f2, float f3)
Get the maximum of 3 numbers.
static float get_min(float f1, float f2)
Get the minimum of 2 numbers.
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"; ...
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 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 get_max(float f1, float f2, float f3, float f4)
Get the maximum of 4 numbers.
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 set_randnum_seed(unsigned long long seed)
Set the seed for Randnum class.
static bool is_nan(const float number)
tell whether a float value is a NaN
static float get_frand(int low, int high)
Get a float random number between low and high, [low, high)
static string get_filename_ext(const string &filename)
Get a filename's extension.
static float get_min(float f1, float f2, float f3)
Get the minimum of 3 numbers.
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 bool is_file_exist(const string &filename)
check whether a file exists or not
static void replace_non_ascii(char *str, int max_size, char repl_char='?')
Replace any non-ASCII characters in a C string with a given character.
static string sbasename(const string &filename)
Get a filename's basename.
static int get_min(int f1, int f2, int f3)
Get the minimum of 3 numbers.
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 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).
static vector< float > nonconvex(const vector< float > &curve, int first=3)
Returns a non-convex version of a curve.
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 int round(float x)
Get ceiling round of a float number x.
static double hypot3(double x, double y, double z)
Euclidean distance function in 3D: f(x,y,z) = sqrt(x*x + y*y + z*z);.
static void ap2ri(float *data, size_t n)
convert complex data array from Amplitude/Phase format into Real/Imaginary format.
static std::complex< float > gauss_interpolate_cmplx(std::complex< float > p1, std::complex< float > p2, std::complex< float > p3, std::complex< float > p4, float t, float u)
Calculate 2x2 Gaussian interpolation.
static int calc_best_fft_size(int low)
Search the best FFT size with good primes.
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 float hypot2sq(float x, float y)
Euclidean distance function squared in 2D: f(x,y) = (x*x + y*y);.
static std::complex< float > bilinear_interpolate_cmplx(std::complex< float > p1, std::complex< float > p2, std::complex< float > p3, std::complex< float > p4, float t, float u)
Calculate bilinear interpolation.
static float trilinear_interpolate(float p1, float p2, float p3, float p4, float p5, float p6, float p7, float p8, float t, float u, float v)
Calculate trilinear interpolation.
static int get_min(int f1, int f2)
Get the minimum of 2 numbers.
static bool check_file_by_magic(const void *first_block, const char *magic)
check whether a file starts with certain magic string.
static float bilinear_interpolate(float p1, float p2, float p3, float p4, float t, float u)
Calculate bilinear interpolation.
static int MUTEX_INIT(MUTEX *mutex)
For those util function developed by Pawel's group.
static string change_filename_ext(const string &old_filename, const string &new_ext)
Change a file's extension and return the new filename.
static void apply_precision(float &value, const float &precision)
static string get_line_from_string(char **str)
Extract a single line from a multi-line string.
static vector< float > windowdot(const vector< float > &curveA, const vector< float > &curveB, int window, int normA)
Computes a windowed dot product between curve A and curve B.
static 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 int round(double x)
Get ceiling round of a float number x.
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,...
static string int2str(int n)
Get a string format of an integer, e.g.
static float square_sum(float x, float y)
Calcuate (x*x + y*y).
static void sort_mat(float *left, float *right, int *leftPerm, int *rightPerm)
does a sort as in Matlab.
static int get_irand(int low, int high)
Get an integer random number between low and high, [low, high].
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 p...
static float angle_norm_2pi(float in)
Normalize an angle in radians so it is in the 0-2pi range.
static unsigned long long get_randnum_seed()
Get the seed for Randnum class.
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 float get_gauss_rand(float mean, float sigma)
Get a Gaussian random number.
static float eman_copysign(float a, float b)
copy sign of a number.
static float get_max(float f1, float f2)
Get the maximum of 2 numbers.
static float hypot_fast(int x, int y)
Euclidean distance in 2D for integers computed fast using a cached lookup table.
static float hypot2(float x, float y)
Euclidean distance function in 2D: f(x,y) = sqrt(x*x + y*y);.
static bool IsPower2(int x)
Return true if an integer is positive and is power of 2.
The Vec2 is precisely the same as Vec3 except it works exclusively in 2D Note there are convenient ty...
EMData * log() const
return natural logarithm image for a image
EMData * sqrt() const
return square root of current image
boost::multi_array< int, 3 > MIArray3D
const int good_fft_sizes[]